LatticeBrain.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using RayNeo.Native;
  6. using RayNeo.Tool;
  7. using System;
  8. /// <summary>
  9. /// 晶格操作按钮
  10. /// </summary>
  11. public class LatticeBrain : MonoBehaviour
  12. {
  13. private static LatticeBrain m_brain;
  14. public static LatticeBrain Brain
  15. {
  16. get
  17. {
  18. if (m_brain == null)
  19. {
  20. m_brain = FindObjectOfType<LatticeBrain>();
  21. if (m_brain == null)
  22. {
  23. m_brain = new GameObject("LatticeBrain").AddComponent<LatticeBrain>();
  24. m_brain.transform.parent = null;
  25. }
  26. }
  27. return m_brain;
  28. }
  29. }
  30. public Action OnDoubleTap;
  31. private Dictionary<int, LatticeSelectInfo> levelInButtons = new Dictionary<int, LatticeSelectInfo>();
  32. //private
  33. private LatticeSelectInfo m_curInteractionInfo;
  34. /// <summary>
  35. /// 聚焦该层级.
  36. /// </summary>
  37. /// <param name="level"></param>
  38. public static void FocusLevel(int level)
  39. {
  40. if (Brain.levelInButtons.TryGetValue(level, out LatticeSelectInfo info))
  41. {
  42. Brain.m_curInteractionInfo = info;
  43. }
  44. }
  45. public static void RegButton(LatticeButton btn)
  46. {
  47. LatticeSelectInfo lsi;
  48. if (!Brain.levelInButtons.TryGetValue(btn.level, out lsi))
  49. {
  50. lsi = Brain.levelInButtons[btn.level] = new LatticeSelectInfo();
  51. }
  52. lsi.AddButton(btn);
  53. }
  54. public static void CleanLevel(int level)
  55. {
  56. if (Brain.levelInButtons.TryGetValue(level, out LatticeSelectInfo lsi))
  57. {
  58. Brain.levelInButtons.Remove(level);
  59. }
  60. }
  61. public static void RemoveButton(LatticeButton btn)
  62. {
  63. if (Brain.levelInButtons.TryGetValue(btn.level, out LatticeSelectInfo lsi))
  64. {
  65. lsi.RemoveButton(btn);
  66. if (!lsi.HasBtn)
  67. {
  68. Brain.levelInButtons.Remove(btn.level);
  69. }
  70. }
  71. }
  72. public static void SelectButton(LatticeButton btn, bool focusLevel = false)
  73. {
  74. foreach (var item in Brain.levelInButtons)
  75. {
  76. if (item.Key == btn.level)
  77. {
  78. item.Value.SelectButton(btn);
  79. if (focusLevel)
  80. {
  81. Brain.m_curInteractionInfo = item.Value;
  82. }
  83. break;
  84. }
  85. }
  86. }
  87. private void Awake()
  88. {
  89. TouchEventCtrl.Instance.OnDoubleTap += OnDoubleTapCall;
  90. TouchEventCtrl.Instance.OnSimpleTap += OnSimpleTap;
  91. TouchEventCtrl.Instance.OnSwipeLeftEnd += OnSwipeLeftEnd;
  92. TouchEventCtrl.Instance.OnSwipeRightEnd += OnSwipeRightEnd;
  93. }
  94. private void OnDisable()
  95. {
  96. m_brain = null;
  97. }
  98. private void OnDestroy()
  99. {
  100. TouchEventCtrl.Instance.OnDoubleTap -= OnDoubleTapCall;
  101. TouchEventCtrl.Instance.OnSimpleTap -= OnSimpleTap;
  102. TouchEventCtrl.Instance.OnSwipeLeftEnd -= OnSwipeLeftEnd;
  103. TouchEventCtrl.Instance.OnSwipeRightEnd -= OnSwipeRightEnd;
  104. }
  105. private void OnSwipeLeftEnd()
  106. {
  107. if (m_curInteractionInfo == null)
  108. {
  109. return;
  110. }
  111. m_curInteractionInfo.SelectPreview();
  112. }
  113. private void OnSwipeRightEnd()
  114. {
  115. if (m_curInteractionInfo == null)
  116. {
  117. return;
  118. }
  119. m_curInteractionInfo.SelectNext();
  120. }
  121. private void OnDoubleTapCall()
  122. {
  123. if (m_curInteractionInfo == null)
  124. {
  125. return;
  126. }
  127. OnDoubleTap?.Invoke();
  128. }
  129. private void OnSimpleTap()
  130. {
  131. if (m_curInteractionInfo == null)
  132. {
  133. return;
  134. }
  135. m_curInteractionInfo.OnBtnClick();
  136. }
  137. private class SameLevelBtnCompair : IComparer<LatticeButton>
  138. {
  139. public int Compare(LatticeButton x, LatticeButton y)
  140. {
  141. return x.order - y.order;
  142. }
  143. }
  144. private class LatticeSelectInfo
  145. {
  146. private bool m_btnSelected = false;//有没有按钮选择着
  147. public LatticeButton mono;//有可能被销毁了.
  148. public List<LatticeButton> btns = new List<LatticeButton>();
  149. private SameLevelBtnCompair m_compair = new SameLevelBtnCompair();
  150. public bool HasBtn { get { return btns.Count > 0; } }
  151. public void AddButton(LatticeButton btn)
  152. {
  153. for (int i = 0; i < btns.Count; i++)
  154. {
  155. if (btns[i] == btn)
  156. {
  157. return;//重复
  158. }
  159. }
  160. btns.Add(btn);
  161. btns.Sort(m_compair);
  162. }
  163. public void RemoveButton(LatticeButton lb)
  164. {
  165. if (!btns.Contains(lb))
  166. {
  167. return;
  168. }
  169. if (mono == lb)
  170. {
  171. //相等
  172. m_btnSelected = false;
  173. }
  174. btns.Remove(lb);
  175. lb.MonoUnFocus();
  176. }
  177. public void SelectButton(LatticeButton lb)
  178. {
  179. if (!btns.Contains(lb))
  180. {
  181. return;
  182. }
  183. if(mono == lb)
  184. {
  185. return;
  186. }
  187. lb.MonoFocus();
  188. if (!m_btnSelected)
  189. {
  190. //之前没有选中的。
  191. mono = lb;
  192. m_btnSelected = true;
  193. return;
  194. }
  195. if (mono != null)
  196. {
  197. mono.MonoUnFocus();//上一个按钮取消聚焦。
  198. }
  199. mono = lb;
  200. }
  201. public void SelectNext()
  202. {
  203. if (!OnBtnModifyCheck())
  204. {
  205. return;
  206. }
  207. for (int i = 0; i < btns.Count; i++)
  208. {
  209. if (btns[i] == mono)
  210. {
  211. if (i == (btns.Count - 1))
  212. {
  213. SelectButton(btns[0]);
  214. }
  215. else
  216. {
  217. SelectButton(btns[i + 1]);
  218. }
  219. break;
  220. }
  221. }
  222. }
  223. public void SelectPreview()
  224. {
  225. if (!OnBtnModifyCheck())
  226. {
  227. return;
  228. }
  229. for (int i = 0; i < btns.Count; i++)
  230. {
  231. if (btns[i] == mono)
  232. {
  233. if (i == 0)
  234. {
  235. SelectButton(btns[btns.Count - 1]);
  236. }
  237. else
  238. {
  239. SelectButton(btns[i - 1]);
  240. }
  241. break;
  242. }
  243. }
  244. }
  245. private bool OnBtnModifyCheck()
  246. {
  247. if (btns.Count <= 0)
  248. {
  249. return false;
  250. }
  251. if (mono == null || !btns.Contains(mono))
  252. {
  253. SelectButton(btns[0]);
  254. return false;
  255. }
  256. if (btns.Count == 1 && btns.Contains(mono))
  257. {
  258. //只有一个
  259. return false;
  260. }
  261. return true;
  262. }
  263. public void OnBtnClick()
  264. {
  265. if (mono != null)
  266. {
  267. mono.onClick?.Invoke();
  268. }
  269. }
  270. }
  271. }