UIRedDotKeyView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using Sirenix.OdinInspector;
  6. using Sirenix.Serialization;
  7. using UnityEngine;
  8. namespace YIUIFramework.Editor
  9. {
  10. [HideLabel]
  11. [HideReferenceObjectPicker]
  12. internal class UIRedDotKeyView : BaseCreateModule
  13. {
  14. private UIRedDotModule m_UIRedDotModule;
  15. private RedDotKeyAsset m_RedDotKeyAsset;
  16. private EnumPrefs<ERedDotKeyViewIndexType> m_ERedDotKeyViewIndexTypePrefs =
  17. new EnumPrefs<ERedDotKeyViewIndexType>("AutoUIRedDotModule_ERedDotKeyViewIndexTypePrefs", null,
  18. ERedDotKeyViewIndexType.Add);
  19. [EnumToggleButtons]
  20. [HideLabel]
  21. [ShowInInspector]
  22. [OnValueChanged("OnValueChangedKeyViewIndex")]
  23. private ERedDotKeyViewIndexType m_ERedDotKeyViewIndexType = ERedDotKeyViewIndexType.Add;
  24. [HideInInspector]
  25. private ERedDotKeyViewIndexType m_LastKeyViewIndex = ERedDotKeyViewIndexType.Add;
  26. private void OnValueChangedKeyViewIndex()
  27. {
  28. m_ERedDotKeyViewIndexTypePrefs.Value = m_ERedDotKeyViewIndexType;
  29. if (m_ERedDotKeyViewIndexType == ERedDotKeyViewIndexType.Add)
  30. {
  31. return;
  32. }
  33. if (m_LastKeyViewIndex != ERedDotKeyViewIndexType.Add)
  34. {
  35. return;
  36. }
  37. if (m_WaitCreate)
  38. {
  39. CreateKeyEnumClass();
  40. }
  41. }
  42. [OdinSerialize]
  43. [ReadOnly]
  44. [PropertyOrder(1000000)]
  45. [BoxGroup("预览", centerLabel: true)]
  46. [HideLabel]
  47. [ShowInInspector]
  48. [DictionaryDrawerSettings(DisplayMode = DictionaryDisplayOptions.Foldout)]
  49. private IReadOnlyDictionary<int, RedDotKeyData> m_AllRedDotKey;
  50. public UIRedDotKeyView(UIRedDotModule redDotModule)
  51. {
  52. m_UIRedDotModule = redDotModule;
  53. m_RedDotKeyAsset = redDotModule.m_RedDotKeyAsset;
  54. m_AllRedDotKey = m_RedDotKeyAsset.AllRedDotDic;
  55. }
  56. public override void Initialize()
  57. {
  58. NewAddRedDataKeyData();
  59. m_ERedDotKeyViewIndexType = m_ERedDotKeyViewIndexTypePrefs.Value;
  60. OnValueChangedKeyViewIndex();
  61. m_UIRedDotModule.OnChangeViewSetIndex += OnChangeViewSetIndex;
  62. CheckContrastRedDotKey();
  63. }
  64. private void CheckContrastRedDotKey()
  65. {
  66. var enumKeyList = GetERedDotKeyTypeList();
  67. if (m_AllRedDotKey.Count != enumKeyList.Count)
  68. {
  69. UnityTipsHelper.CallBack($"当前配置数据 长度:{m_AllRedDotKey.Count} 与 枚举数据 长度:{enumKeyList.Count} 不一致 请重新生成 或是ET没编译过 没检测到 请检查",
  70. CreateKeyEnumClass);
  71. }
  72. }
  73. private List<int> GetERedDotKeyTypeList()
  74. {
  75. var list = new List<int>();
  76. foreach (int key in RedDotKeyHelper.GetKeys())
  77. {
  78. if (key != 0)
  79. {
  80. list.Add(key);
  81. }
  82. }
  83. return list;
  84. }
  85. private void OnChangeViewSetIndex(UIRedDotModule.EUIRedDotViewType obj)
  86. {
  87. if (obj != UIRedDotModule.EUIRedDotViewType.Key)
  88. {
  89. if (m_WaitCreate)
  90. {
  91. CreateKeyEnumClass();
  92. }
  93. }
  94. }
  95. public override void OnDestroy()
  96. {
  97. m_UIRedDotModule.OnChangeViewSetIndex -= OnChangeViewSetIndex;
  98. if (m_WaitCreate)
  99. {
  100. CreateKeyEnumClass();
  101. }
  102. }
  103. private bool m_WaitCreate = false;
  104. [BoxGroup("生成", centerLabel: true)]
  105. [Button("自动生成Key枚举类", 50)]
  106. [ShowIf("m_WaitCreate")]
  107. [PropertyOrder(10000)]
  108. [GUIColor(0.4f, 0.8f, 1)]
  109. private void CreateKeyEnumClass()
  110. {
  111. m_WaitCreate = false;
  112. var createData = new UICreateRedDotKeyData
  113. {
  114. AutoRefresh = true,
  115. ShowTips = true,
  116. ClassPath = m_UIRedDotModule.UIRedDotKeyClassPath,
  117. Content = UICreateRedDotKeyGet.Get(m_RedDotKeyAsset.GetDataList()),
  118. };
  119. new UICreateRedDotKeyCode(out var resultBase, YIUIAutoTool.Author, createData);
  120. if (!resultBase) return;
  121. }
  122. #region 增删改查
  123. #region 增
  124. [HideLabel]
  125. [ShowInInspector]
  126. [ShowIf("m_ERedDotKeyViewIndexType", ERedDotKeyViewIndexType.Add)]
  127. [PropertyOrder(2001)]
  128. [BoxGroup("添加数据", centerLabel: true)]
  129. private RedDotKeyData m_AddKeyData;
  130. [ButtonGroup("添加")]
  131. [Button("添加(生成)", 50)]
  132. [GUIColor(0, 1, 0)]
  133. [ShowIf("m_ERedDotKeyViewIndexType", ERedDotKeyViewIndexType.Add)]
  134. [PropertyOrder(2002)]
  135. private void AddKeyCreateBtn()
  136. {
  137. AddKey();
  138. }
  139. [ButtonGroup("添加")]
  140. [Button("添加(不生成)", 50)]
  141. [GUIColor(1, 1, 0)]
  142. [ShowIf("m_ERedDotKeyViewIndexType", ERedDotKeyViewIndexType.Add)]
  143. [PropertyOrder(2002)]
  144. private void AddKeyBtn()
  145. {
  146. var result = AddKey(false);
  147. if (result)
  148. {
  149. m_WaitCreate = true;
  150. }
  151. }
  152. private bool AddKey(bool createKey = true)
  153. {
  154. var (result, tips) = m_RedDotKeyAsset.AddKey(m_AddKeyData);
  155. NewAddRedDataKeyData();
  156. if (!result)
  157. {
  158. UnityTipsHelper.Show($"{tips} 建议可以直接参考默认ID");
  159. }
  160. else
  161. {
  162. if (createKey)
  163. {
  164. CreateKeyEnumClass();
  165. }
  166. else
  167. {
  168. UnityTipsHelper.Show(tips);
  169. }
  170. }
  171. return result;
  172. }
  173. private void NewAddRedDataKeyData()
  174. {
  175. var id = m_RedDotKeyAsset.GetSafetyId();
  176. m_AddKeyData = new RedDotKeyData(id);
  177. }
  178. #endregion
  179. #region 删
  180. //TODO 目前删除/修改 前检测只做了配置档
  181. //最终目标应该还额外扫描其他的
  182. //1 预制体 关联了那些红点的预制体 上面会关联Key 这个对应的修改删除
  183. //2 代码 某些代码会涉及到 获取 修改等 也对应的修改删除
  184. [ShowInInspector]
  185. [ShowIf("m_ERedDotKeyViewIndexType", ERedDotKeyViewIndexType.Delete)]
  186. [LabelText("删除的ID")]
  187. [PropertyOrder(3001)]
  188. [BoxGroup("删除数据", centerLabel: true)]
  189. private int m_DeleteId = 0;
  190. [Button("删除", 50)]
  191. [GUIColor(1f, 0.3f, 0.3f)]
  192. [ShowIf("m_ERedDotKeyViewIndexType", ERedDotKeyViewIndexType.Delete)]
  193. [PropertyOrder(3002)]
  194. private void DeleteKeyBtn()
  195. {
  196. if (!m_RedDotKeyAsset.ContainsKey(m_DeleteId))
  197. {
  198. UnityTipsHelper.Show($"当前ID不存在 无法移除 {m_DeleteId}");
  199. return;
  200. }
  201. var linkData =
  202. UIRedDotKeyEditorHelper.GetCheckKeyLink(m_UIRedDotModule.m_RedDotConfigAsset, m_DeleteId);
  203. if (linkData.ConfigSet || linkData.LinkKey.Count >= 1)
  204. {
  205. var configSetTips = linkData.ConfigSet ? "已配置" : "未配置";
  206. var linkTips = $"关联引用 {linkData.LinkKey.Count}";
  207. foreach (var linkKey in linkData.LinkKey)
  208. {
  209. linkTips += $"\n已被 {linkKey} 设定为父级";
  210. }
  211. var unityTips = $"{linkData.Key} 已被使用 确定删除吗?\n\n{configSetTips}\n{linkTips}\n\n同步的将会删除所有关联数据";
  212. UnityTipsHelper.CallBack(unityTips, () =>
  213. {
  214. DeleteKey((result) =>
  215. {
  216. if (!result) return;
  217. UIRedDotKeyEditorHelper.RemoveKeyByLink(m_UIRedDotModule.m_RedDotConfigAsset, linkData);
  218. CreateKeyEnumClass();
  219. YIUIAutoTool.CloseWindowRefresh();
  220. }, false);
  221. });
  222. }
  223. else
  224. {
  225. DeleteKey((result) =>
  226. {
  227. if (!result) return;
  228. CreateKeyEnumClass();
  229. });
  230. }
  231. }
  232. private void DeleteKey(Action<bool> resultAction = null, bool showTips = true)
  233. {
  234. var (result, tips) = m_RedDotKeyAsset.RemoveKey(m_DeleteId);
  235. if (showTips)
  236. UnityTipsHelper.Show(tips);
  237. m_DeleteId = 0;
  238. resultAction?.Invoke(result);
  239. }
  240. #endregion
  241. #region 改
  242. //TODO 目前删除/修改 前检测只做了配置档
  243. //最终目标应该还额外扫描其他的
  244. //1 预制体 关联了那些红点的预制体 上面会关联Key 这个对应的修改删除
  245. //2 代码 某些代码会涉及到 获取 修改等 也对应的修改删除
  246. [HideLabel]
  247. [ShowInInspector]
  248. [ShowIf("ShowIfChangeKeyData")]
  249. [PropertyOrder(4001)]
  250. [BoxGroup("修改数据", centerLabel: true)]
  251. private RedDotKeyData m_ChangeKeyData;
  252. private bool ShowIfChangeKeyData()
  253. {
  254. if (m_ERedDotKeyViewIndexType != ERedDotKeyViewIndexType.Change)
  255. {
  256. return false;
  257. }
  258. return m_ChangeKeyData != null;
  259. }
  260. [Button("修改", 50)]
  261. [GUIColor(0.7f, 0.4f, 0.8f)]
  262. [ShowIf("m_ERedDotKeyViewIndexType", ERedDotKeyViewIndexType.Change)]
  263. [PropertyOrder(4002)]
  264. private void ChangeKeyBtn()
  265. {
  266. if (m_ChangeKeyData == null)
  267. {
  268. UnityTipsHelper.Show($"请先 查询>>设置 需要修改的数据");
  269. return;
  270. }
  271. var linkData =
  272. UIRedDotKeyEditorHelper.GetCheckKeyLink(m_UIRedDotModule.m_RedDotConfigAsset, m_GetKeyData.Id);
  273. if (linkData.ConfigSet || linkData.LinkKey.Count >= 1)
  274. {
  275. var configSetTips = linkData.ConfigSet ? "已配置" : "未配置";
  276. var linkTips = $"关联引用 {linkData.LinkKey.Count}";
  277. foreach (var linkKey in linkData.LinkKey)
  278. {
  279. linkTips += $"\n已被 {linkKey} 设定为父级";
  280. }
  281. var unityTips = $"{linkData.Key} 已被使用 确定修改吗?\n\n{configSetTips}\n{linkTips}\n\n同步的将会修改所有关联数据";
  282. UnityTipsHelper.CallBack(unityTips, () =>
  283. {
  284. ChangeKey((result) =>
  285. {
  286. if (!result) return;
  287. CreateKeyEnumClass();
  288. UIRedDotKeyEditorHelper.ChangeKeyByLink(m_UIRedDotModule.m_RedDotConfigAsset, linkData,
  289. m_ChangeKeyData);
  290. YIUIAutoTool.CloseWindowRefresh();
  291. }, false);
  292. });
  293. }
  294. else
  295. {
  296. ChangeKey((result) =>
  297. {
  298. if (!result) return;
  299. CreateKeyEnumClass();
  300. });
  301. }
  302. }
  303. private void ChangeKey(Action<bool> resultAction = null, bool showTips = true)
  304. {
  305. if (m_ChangeKeyData == null)
  306. {
  307. UnityTipsHelper.Show($"请先 查询>>设置 需要修改的数据");
  308. return;
  309. }
  310. var (result, tips) = m_RedDotKeyAsset.ChangeKey(m_GetKeyData.Id, m_ChangeKeyData);
  311. if (showTips)
  312. UnityTipsHelper.Show(tips);
  313. resultAction?.Invoke(result);
  314. if (result)
  315. {
  316. m_GetKeyData = null;
  317. m_ChangeKeyData = null;
  318. }
  319. }
  320. private void UpdateChangeData(RedDotKeyData getData)
  321. {
  322. if (getData == null)
  323. {
  324. m_ChangeKeyData = null;
  325. return;
  326. }
  327. m_ChangeKeyData = new RedDotKeyData(getData.Id, getData.Des);
  328. }
  329. #endregion
  330. #region 查
  331. [ShowInInspector]
  332. [HideIf("m_ERedDotKeyViewIndexType", ERedDotKeyViewIndexType.Add)]
  333. [LabelText("查询的ID")]
  334. [PropertyOrder(1001)]
  335. [InlineButton("GetKey", "查询")]
  336. [BoxGroup("查询数据", centerLabel: true)]
  337. [GUIColor(1, 1, 0)]
  338. private int m_GetId = 1;
  339. [ShowInInspector]
  340. [ReadOnly]
  341. [ShowIf("ShowGetKeyData")]
  342. [BoxGroup("查询数据", centerLabel: true)]
  343. [PropertyOrder(1002)]
  344. private RedDotKeyData m_GetKeyData;
  345. private bool ShowGetKeyData()
  346. {
  347. if (m_ERedDotKeyViewIndexType == ERedDotKeyViewIndexType.Add)
  348. {
  349. return false;
  350. }
  351. return m_GetKeyData != null;
  352. }
  353. private void GetKey()
  354. {
  355. var (data, tips) = m_RedDotKeyAsset.GetKey(m_GetId);
  356. m_GetKeyData = data;
  357. UnityTipsHelper.Show(tips);
  358. UpdateChangeData(data);
  359. }
  360. #endregion
  361. #endregion
  362. [HideLabel]
  363. private enum ERedDotKeyViewIndexType
  364. {
  365. [LabelText("增")]
  366. Add = 1,
  367. [LabelText("删")]
  368. Delete = 2,
  369. [LabelText("改")]
  370. Change = 3,
  371. [LabelText("查")]
  372. Get = 4,
  373. }
  374. }
  375. }
  376. #endif