GmToolSendMsgPanelCtrl.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /// #pkgName FGUI包名
  2. /// #panelName UIPanel名字
  3. /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
  4. /// 该脚本由模板创建
  5. /// created by cb 2024
  6. using FairyGUI;
  7. using FL.Data;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Reflection;
  11. using XGame.Framework.Network;
  12. using XGame.Framework.UI;
  13. namespace FL.FGUI
  14. {
  15. public struct GmToolFieldData
  16. {
  17. public string filedName; // 字段名称
  18. public Action<GmFieldParam> changeValueCallback; // 回调函数
  19. }
  20. public struct GmFieldParam
  21. {
  22. public string fieldName;
  23. public string fieldValue;
  24. }
  25. /// <summary>
  26. /// UI逻辑处理类
  27. /// </summary>
  28. /// <typeparam name=""></typeparam>
  29. public partial class GmToolSendMsgPanelCtrl : UIController<GmToolSendMsgPanelVM>
  30. {
  31. private IMsgRequest _queryMsg; // 要查询的消息
  32. private List<GmToolFieldData> _fieldDataList = new List<GmToolFieldData>();
  33. protected override void OnEnable(object intent)
  34. {
  35. AddUIListenres();
  36. InitUI();
  37. }
  38. protected override void OnDisable()
  39. {
  40. RemoveUIListenres();
  41. if (_fieldDataList?.Count > 0)
  42. {
  43. _fieldDataList.Clear();
  44. }
  45. _queryMsg = null;
  46. }
  47. #region UI事件
  48. private void AddUIListenres()
  49. {
  50. VM.QueryBtn.onClick.Add(OnClickQueryBtn);
  51. VM.SendBtn.onClick.Add(OnClickSendBtn);
  52. VM.MaskBtn.onClick.Add(OnClickMaskBtn);
  53. }
  54. private void RemoveUIListenres()
  55. {
  56. VM.QueryBtn.onClick.Remove(OnClickQueryBtn);
  57. VM.SendBtn.onClick.Remove(OnClickSendBtn);
  58. VM.MaskBtn.onClick.Remove(OnClickMaskBtn);
  59. }
  60. private void OnClickMaskBtn(EventContext context)
  61. {
  62. Context.ClosePanel();
  63. }
  64. private void OnClickQueryBtn(EventContext context)
  65. {
  66. if (string.IsNullOrEmpty(VM.MsgIdInput.text))
  67. {
  68. Context.ShowTips("请先输入消息ID");
  69. return;
  70. }
  71. ShowQueryMsgUI();
  72. }
  73. private void OnClickSendBtn(EventContext context)
  74. {
  75. SendGmMsg();
  76. }
  77. #endregion
  78. private void InitUI()
  79. {
  80. VM.MsgIdInput.text = string.Empty;
  81. VM.FieldNameList.BindDatas(_fieldDataList);
  82. }
  83. private void ResetField()
  84. {
  85. if (_fieldDataList?.Count > 0)
  86. {
  87. _fieldDataList.Clear();
  88. }
  89. VM.FieldNameList.BindDatas(_fieldDataList);
  90. }
  91. private int GetMsgId()
  92. {
  93. return int.Parse(VM.MsgIdInput.text);
  94. }
  95. /// <summary>
  96. /// 查询消息相关字段信息
  97. /// </summary>
  98. private void ShowQueryMsgUI()
  99. {
  100. ResetField();
  101. _queryMsg = GmToolsService.Instance.QueryMessage(GetMsgId());
  102. if (_queryMsg == null)
  103. {
  104. Context.ShowTips("请先输入正确的消息ID");
  105. return;
  106. }
  107. var fields = _queryMsg.GetType().GetFields();
  108. foreach (var item in fields)
  109. {
  110. _fieldDataList.Add(new GmToolFieldData()
  111. {
  112. filedName = item.Name,
  113. changeValueCallback = OnChangeFiledValue,
  114. });
  115. }
  116. foreach (var item in _fieldDataList)
  117. {
  118. XGame.Log.Info($"字段名:{item}");
  119. }
  120. VM.FieldNameList.BindDatas(_fieldDataList);
  121. }
  122. /// <summary>
  123. /// 修改字段数据监听
  124. /// </summary>
  125. /// <param name="eventId"></param>
  126. /// <param name="args"></param>
  127. private void OnChangeFiledValue(GmFieldParam fieldData)
  128. {
  129. //使用默认值来判断结构体是否被初始化
  130. if (!fieldData.Equals(default(GmFieldParam)))
  131. {
  132. string fieldName = fieldData.fieldName;
  133. if (!string.IsNullOrEmpty(fieldName))
  134. {
  135. // 获取字段的信息
  136. FieldInfo fieldInfo = _queryMsg.GetType().GetField(fieldName);
  137. XGame.Log.Debug($"字段:{fieldName}的值类型:{fieldInfo.FieldType}");
  138. Type ValueType = fieldInfo.FieldType;
  139. if (ValueType == typeof(int))
  140. {
  141. fieldInfo.SetValue(_queryMsg, int.Parse(fieldData.fieldValue));
  142. }
  143. else if (ValueType == typeof(string))
  144. {
  145. fieldInfo.SetValue(_queryMsg, fieldData.fieldValue);
  146. }
  147. else if (ValueType == typeof(bool))
  148. {
  149. fieldInfo.SetValue(_queryMsg, bool.Parse(fieldData.fieldValue));
  150. }
  151. else if (ValueType == typeof(int[]) || ValueType == typeof(string[]))
  152. {
  153. string[] arr = fieldData.fieldValue.Split(",");
  154. if (ValueType == typeof(int[]))
  155. {
  156. var valArray = new int[arr.Length];
  157. for (int i = 0; i < arr.Length; i++)
  158. {
  159. valArray[i] = string.IsNullOrEmpty(arr[i]) ? 0 : int.Parse(arr[i]);
  160. }
  161. fieldInfo.SetValue(_queryMsg, valArray);
  162. }
  163. else
  164. {
  165. fieldInfo.SetValue(_queryMsg, arr);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. /// <summary>
  172. /// 发送修改数据的消息
  173. /// </summary>
  174. private void SendGmMsg()
  175. {
  176. GmToolsService.Instance.SendMessage(_queryMsg);
  177. Context.ClosePanel();
  178. }
  179. }
  180. }