123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /// #pkgName FGUI包名
- /// #panelName UIPanel名字
- /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
- /// 该脚本由模板创建
- /// created by cb 2024
- using FairyGUI;
- using FL.Data;
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using XGame.Framework.Network;
- using XGame.Framework.UI;
- namespace FL.FGUI
- {
- public struct GmToolFieldData
- {
- public string filedName; // 字段名称
- public Action<GmFieldParam> changeValueCallback; // 回调函数
- }
- public struct GmFieldParam
- {
- public string fieldName;
- public string fieldValue;
- }
- /// <summary>
- /// UI逻辑处理类
- /// </summary>
- /// <typeparam name=""></typeparam>
- public partial class GmToolSendMsgPanelCtrl : UIController<GmToolSendMsgPanelVM>
- {
- private IMsgRequest _queryMsg; // 要查询的消息
- private List<GmToolFieldData> _fieldDataList = new List<GmToolFieldData>();
- protected override void OnEnable(object intent)
- {
- AddUIListenres();
- InitUI();
- }
- protected override void OnDisable()
- {
- RemoveUIListenres();
- if (_fieldDataList?.Count > 0)
- {
- _fieldDataList.Clear();
- }
- _queryMsg = null;
- }
- #region UI事件
- private void AddUIListenres()
- {
- VM.QueryBtn.onClick.Add(OnClickQueryBtn);
- VM.SendBtn.onClick.Add(OnClickSendBtn);
- VM.MaskBtn.onClick.Add(OnClickMaskBtn);
- }
- private void RemoveUIListenres()
- {
- VM.QueryBtn.onClick.Remove(OnClickQueryBtn);
- VM.SendBtn.onClick.Remove(OnClickSendBtn);
- VM.MaskBtn.onClick.Remove(OnClickMaskBtn);
- }
- private void OnClickMaskBtn(EventContext context)
- {
- Context.ClosePanel();
- }
- private void OnClickQueryBtn(EventContext context)
- {
- if (string.IsNullOrEmpty(VM.MsgIdInput.text))
- {
- Context.ShowTips("请先输入消息ID");
- return;
- }
- ShowQueryMsgUI();
- }
- private void OnClickSendBtn(EventContext context)
- {
- SendGmMsg();
- }
- #endregion
- private void InitUI()
- {
- VM.MsgIdInput.text = string.Empty;
- VM.FieldNameList.BindDatas(_fieldDataList);
- }
- private void ResetField()
- {
- if (_fieldDataList?.Count > 0)
- {
- _fieldDataList.Clear();
- }
- VM.FieldNameList.BindDatas(_fieldDataList);
- }
- private int GetMsgId()
- {
- return int.Parse(VM.MsgIdInput.text);
- }
- /// <summary>
- /// 查询消息相关字段信息
- /// </summary>
- private void ShowQueryMsgUI()
- {
- ResetField();
- _queryMsg = GmToolsService.Instance.QueryMessage(GetMsgId());
- if (_queryMsg == null)
- {
- Context.ShowTips("请先输入正确的消息ID");
- return;
- }
- var fields = _queryMsg.GetType().GetFields();
- foreach (var item in fields)
- {
- _fieldDataList.Add(new GmToolFieldData()
- {
- filedName = item.Name,
- changeValueCallback = OnChangeFiledValue,
- });
- }
- foreach (var item in _fieldDataList)
- {
- XGame.Log.Info($"字段名:{item}");
- }
- VM.FieldNameList.BindDatas(_fieldDataList);
- }
- /// <summary>
- /// 修改字段数据监听
- /// </summary>
- /// <param name="eventId"></param>
- /// <param name="args"></param>
- private void OnChangeFiledValue(GmFieldParam fieldData)
- {
- //使用默认值来判断结构体是否被初始化
- if (!fieldData.Equals(default(GmFieldParam)))
- {
- string fieldName = fieldData.fieldName;
- if (!string.IsNullOrEmpty(fieldName))
- {
- // 获取字段的信息
- FieldInfo fieldInfo = _queryMsg.GetType().GetField(fieldName);
- XGame.Log.Debug($"字段:{fieldName}的值类型:{fieldInfo.FieldType}");
- Type ValueType = fieldInfo.FieldType;
- if (ValueType == typeof(int))
- {
- fieldInfo.SetValue(_queryMsg, int.Parse(fieldData.fieldValue));
- }
- else if (ValueType == typeof(string))
- {
- fieldInfo.SetValue(_queryMsg, fieldData.fieldValue);
- }
- else if (ValueType == typeof(bool))
- {
- fieldInfo.SetValue(_queryMsg, bool.Parse(fieldData.fieldValue));
- }
- else if (ValueType == typeof(int[]) || ValueType == typeof(string[]))
- {
- string[] arr = fieldData.fieldValue.Split(",");
- if (ValueType == typeof(int[]))
- {
- var valArray = new int[arr.Length];
- for (int i = 0; i < arr.Length; i++)
- {
- valArray[i] = string.IsNullOrEmpty(arr[i]) ? 0 : int.Parse(arr[i]);
- }
- fieldInfo.SetValue(_queryMsg, valArray);
- }
- else
- {
- fieldInfo.SetValue(_queryMsg, arr);
- }
- }
- }
- }
- }
- /// <summary>
- /// 发送修改数据的消息
- /// </summary>
- private void SendGmMsg()
- {
- GmToolsService.Instance.SendMessage(_queryMsg);
- Context.ClosePanel();
- }
- }
- }
|