CircleCommandUI.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. public class CircleCommandUI : MonoBehaviour
  5. {
  6. public bool isClockwise
  7. {
  8. get
  9. {
  10. return this.m_IsClockwise;
  11. }
  12. set
  13. {
  14. this.m_IsClockwise = value;
  15. this.UpdateItemPosition();
  16. }
  17. }
  18. public uGUIListViewer listViewer
  19. {
  20. get
  21. {
  22. return this.m_ListViewer;
  23. }
  24. }
  25. public float circleRadius
  26. {
  27. get
  28. {
  29. return this.m_CircleRadius;
  30. }
  31. set
  32. {
  33. this.m_CircleRadius = value;
  34. this.UpdateItemPosition();
  35. }
  36. }
  37. public float offsetAngle
  38. {
  39. get
  40. {
  41. return this.m_OffsetAngle;
  42. }
  43. set
  44. {
  45. this.m_OffsetAngle = value;
  46. this.UpdateItemPosition();
  47. }
  48. }
  49. public CircleCommandUI.OnSelect onSelect
  50. {
  51. get
  52. {
  53. return this.m_OnSelect;
  54. }
  55. }
  56. public CircleCommandUI.OnDeselect onDeselect
  57. {
  58. get
  59. {
  60. return this.m_OnDeselect;
  61. }
  62. }
  63. public CircleCommandUI.OnDecide onDecide
  64. {
  65. get
  66. {
  67. return this.m_OnDecide;
  68. }
  69. }
  70. public Func<bool> funcDecide { get; set; }
  71. public GameObject lastSelectItem
  72. {
  73. get
  74. {
  75. return this.m_LastSelectItem;
  76. }
  77. private set
  78. {
  79. this.m_LastSelectItem = value;
  80. }
  81. }
  82. public GameObject nowSelectItem
  83. {
  84. get
  85. {
  86. return this.m_NowSelectItem;
  87. }
  88. private set
  89. {
  90. this.m_NowSelectItem = value;
  91. }
  92. }
  93. private void OnValidate()
  94. {
  95. if (!Application.isPlaying)
  96. {
  97. return;
  98. }
  99. if (this.isApplicationQuit)
  100. {
  101. return;
  102. }
  103. this.UpdateItemPosition();
  104. }
  105. private void OnApplicationQuit()
  106. {
  107. this.isApplicationQuit = true;
  108. }
  109. public void Show<T>(int itemCount, Action<int, T> callbackSetUp = null) where T : Component
  110. {
  111. this.m_ListViewer.Show<T>(itemCount, callbackSetUp);
  112. this.UpdateItemPosition();
  113. }
  114. private void UpdateItemPosition()
  115. {
  116. GameObject[] itemArray = this.m_ListViewer.ItemArray;
  117. if (itemArray == null)
  118. {
  119. return;
  120. }
  121. Transform parentItemArea = this.m_ListViewer.parentItemArea;
  122. float num = 360f / (float)itemArray.Length;
  123. float num2 = this.offsetAngle;
  124. if (this.isClockwise)
  125. {
  126. num *= -1f;
  127. }
  128. foreach (GameObject gameObject in itemArray)
  129. {
  130. Transform transform = gameObject.transform;
  131. Vector3 vector = Vector3.zero;
  132. vector.x = Mathf.Cos(num2 * 0.0174532924f);
  133. vector.y = Mathf.Sin(num2 * 0.0174532924f) * 0.75f;
  134. vector.Normalize();
  135. vector *= this.circleRadius;
  136. transform.localPosition = vector;
  137. num2 += num;
  138. }
  139. }
  140. private void Update()
  141. {
  142. if (this.funcDecide == null)
  143. {
  144. return;
  145. }
  146. if (this.funcDecide())
  147. {
  148. this.onDecide.Invoke(this.nowSelectItem);
  149. this.nowSelectItem = null;
  150. this.lastSelectItem = null;
  151. }
  152. }
  153. public void UpdateSelect(Vector2 axis)
  154. {
  155. GameObject[] itemArray = this.m_ListViewer.ItemArray;
  156. if (itemArray == null)
  157. {
  158. return;
  159. }
  160. Transform parentItemArea = this.m_ListViewer.parentItemArea;
  161. GameObject gameObject = null;
  162. Vector2 vector = Vector2.one * float.PositiveInfinity;
  163. foreach (GameObject gameObject2 in itemArray)
  164. {
  165. Vector2 a = gameObject2.transform.localPosition;
  166. Vector2 vector2 = a - axis;
  167. if (vector2.sqrMagnitude < vector.sqrMagnitude)
  168. {
  169. vector = vector2;
  170. gameObject = gameObject2;
  171. }
  172. }
  173. if (this.nowSelectItem != gameObject)
  174. {
  175. this.lastSelectItem = this.nowSelectItem;
  176. this.nowSelectItem = gameObject;
  177. this.onDeselect.Invoke(this.lastSelectItem);
  178. this.onSelect.Invoke(this.nowSelectItem);
  179. }
  180. }
  181. [SerializeField]
  182. private bool m_IsClockwise;
  183. [SerializeField]
  184. private uGUIListViewer m_ListViewer;
  185. [SerializeField]
  186. private float m_CircleRadius;
  187. [SerializeField]
  188. private float m_OffsetAngle;
  189. [SerializeField]
  190. private CircleCommandUI.OnSelect m_OnSelect = new CircleCommandUI.OnSelect();
  191. [SerializeField]
  192. private CircleCommandUI.OnDeselect m_OnDeselect = new CircleCommandUI.OnDeselect();
  193. [SerializeField]
  194. private CircleCommandUI.OnDecide m_OnDecide = new CircleCommandUI.OnDecide();
  195. private GameObject m_LastSelectItem;
  196. private GameObject m_NowSelectItem;
  197. private bool isApplicationQuit;
  198. [Serializable]
  199. public class OnSelect : UnityEvent<GameObject>
  200. {
  201. }
  202. [Serializable]
  203. public class OnDeselect : UnityEvent<GameObject>
  204. {
  205. }
  206. [Serializable]
  207. public class OnDecide : UnityEvent<GameObject>
  208. {
  209. }
  210. }