Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AngleRangeView.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEditor.Sprites;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace UnityEditor.U2D
  7. {
  8. internal enum AngleRangeAction
  9. {
  10. SelectRange,
  11. ModifyRange,
  12. CreateRange,
  13. ModifySelector,
  14. RemoveRange,
  15. }
  16. internal interface IAngleRangeView
  17. {
  18. int hoveredRangeIndex { get; }
  19. void RequestFocusIndex(int index);
  20. float GetAngleFromPosition(Rect rect, float angleOffset);
  21. void Repaint();
  22. void SetupLayout(Rect rect, float angleOffset, float radius);
  23. bool DoAngleRange(int index, Rect rect, float radius, float angleOffset, ref float start, ref float end, bool snap, bool disabled, Color gradientMin, Color gradientMid, Color gradientMax);
  24. bool DoSelectAngleRange(int currentSelected, out int newSelected);
  25. bool DoCreateRange(Rect rect, float radius, float angleOffset, float start, float end);
  26. void DoCreateRangeTooltip();
  27. bool DoSelector(Rect rect, float angleOffset, float radius, float angle, out float newAngle);
  28. bool DoRemoveRange();
  29. bool IsActionActive(AngleRangeAction action);
  30. bool IsActionTriggering(AngleRangeAction action);
  31. bool IsActionFinishing(AngleRangeAction action);
  32. void DrawAngleRangeOutline(Rect rect, float start, float end, float angleOffset, float radius);
  33. }
  34. internal class AngleRangeView : IAngleRangeView
  35. {
  36. const string kDeleteCommandName = "Delete";
  37. const string kSoftDeleteCommandName = "SoftDelete";
  38. static readonly int kAngleRangeHashCode = "AngleRange".GetHashCode();
  39. static readonly int kCreateRangeHashCode = "CreateRange".GetHashCode();
  40. static readonly int kSelectorHashCode = "Selector".GetHashCode();
  41. private static Color kHightlightColor = new Color(0.25f, 0.5f, 1.0f);
  42. private static Color kNoKeboardFocusColor = new Color(0.3f, 0.5f, 0.65f);
  43. private static class Contents
  44. {
  45. public static readonly GUIContent addRangeTooltip = new GUIContent("", "Click to add a new range");
  46. }
  47. private int m_FocusedRangeControlID = -1;
  48. private int m_HoveredRangeIndex = -1;
  49. private int m_HoveredRangeID = -1;
  50. private int m_HoveredHandleID = -1;
  51. private int m_HotHandleID = -1;
  52. private int m_CreateRangeControlID = -1;
  53. private int m_SelectorControlID = -1;
  54. private int m_RequestFocusIndex = -1;
  55. public int hoveredRangeIndex { get { return m_HoveredRangeIndex; } }
  56. public void RequestFocusIndex(int index)
  57. {
  58. GUIUtility.keyboardControl = 0;
  59. m_RequestFocusIndex = index;
  60. }
  61. public float GetAngleFromPosition(Rect rect, float angleOffset)
  62. {
  63. return Mathf.RoundToInt(SpriteShapeHandleUtility.PosToAngle(Event.current.mousePosition, rect.center, -angleOffset));
  64. }
  65. public void Repaint()
  66. {
  67. HandleUtility.Repaint();
  68. }
  69. public void SetupLayout(Rect rect, float angleOffset, float radius)
  70. {
  71. m_CreateRangeControlID = GUIUtility.GetControlID(kCreateRangeHashCode, FocusType.Passive);
  72. m_SelectorControlID = GUIUtility.GetControlID(kSelectorHashCode, FocusType.Passive);
  73. LayoutCreateRange(rect, angleOffset, radius);
  74. if (Event.current.type == EventType.Layout)
  75. {
  76. m_HoveredRangeIndex = -1;
  77. m_HoveredRangeID = -1;
  78. m_HoveredHandleID = -1;
  79. if (GUIUtility.hotControl == 0)
  80. {
  81. m_HotHandleID = -1;
  82. }
  83. }
  84. }
  85. private void LayoutCreateRange(Rect rect, float angleOffset, float radius)
  86. {
  87. if (Event.current.type == EventType.Layout)
  88. {
  89. var mousePosition = Event.current.mousePosition;
  90. var distance = SpriteShapeHandleUtility.DistanceToArcWidth(mousePosition, rect.center, 0f, 360f, radius, AngleRangeGUI.kRangeWidth, angleOffset);
  91. HandleUtility.AddControl(m_CreateRangeControlID, distance);
  92. }
  93. }
  94. public bool DoAngleRange(int index, Rect rect, float radius, float angleOffset, ref float start, ref float end, bool snap, bool disabled, Color gradientMin, Color gradientMid, Color gradientMax)
  95. {
  96. var changed = false;
  97. var controlID = GUIUtility.GetControlID(kAngleRangeHashCode, FocusType.Passive);
  98. var leftHandleId = GUIUtility.GetControlID(AngleRangeGUI.kLeftHandleHashCode, FocusType.Passive);
  99. var rightHandleId = GUIUtility.GetControlID(AngleRangeGUI.kRightHandleHashCode, FocusType.Passive);
  100. if (Event.current.type == EventType.Layout)
  101. {
  102. var distance = SpriteShapeHandleUtility.DistanceToArcWidth(Event.current.mousePosition, rect.center, start, end, radius, AngleRangeGUI.kRangeWidth, angleOffset);
  103. HandleUtility.AddControl(controlID, distance);
  104. if (HandleUtility.nearestControl == controlID)
  105. {
  106. m_HoveredRangeIndex = index;
  107. m_HoveredRangeID = controlID;
  108. }
  109. }
  110. if (IsActionTriggering(AngleRangeAction.ModifyRange))
  111. {
  112. m_HotHandleID = m_HoveredHandleID;
  113. GrabKeyboardFocus(controlID);
  114. }
  115. if (m_RequestFocusIndex == index)
  116. {
  117. GrabKeyboardFocus(controlID);
  118. if (Event.current.type == EventType.Repaint)
  119. m_RequestFocusIndex = -1;
  120. }
  121. using (new EditorGUI.DisabledScope(disabled))
  122. {
  123. var midAngle = (end - start) * 0.5f + start;
  124. var t = 2f * (midAngle + 180f) / 360f;
  125. var color = gradientMin;
  126. if (t < 1f)
  127. color = Color.Lerp(gradientMin, gradientMid, t);
  128. else
  129. color = Color.Lerp(gradientMid, gradientMax, t - 1f);
  130. if (!disabled)
  131. {
  132. color = kNoKeboardFocusColor;
  133. if (HasKeyboardFocus())
  134. color = kHightlightColor;
  135. }
  136. EditorGUI.BeginChangeCheck();
  137. AngleRangeGUI.AngleRangeField(rect, leftHandleId, rightHandleId, ref start, ref end, angleOffset, radius, snap, false, false, color);
  138. changed = EditorGUI.EndChangeCheck();
  139. }
  140. //Extra Layout from handles
  141. if (Event.current.type == EventType.Layout &&
  142. (HandleUtility.nearestControl == leftHandleId || HandleUtility.nearestControl == rightHandleId))
  143. {
  144. m_HoveredRangeIndex = index;
  145. m_HoveredRangeID = controlID;
  146. m_HoveredHandleID = HandleUtility.nearestControl;
  147. }
  148. return changed;
  149. }
  150. public bool DoSelectAngleRange(int currentSelected, out int newSelected)
  151. {
  152. newSelected = currentSelected;
  153. if (IsActionTriggering(AngleRangeAction.SelectRange))
  154. {
  155. newSelected = m_HoveredRangeIndex;
  156. GUI.changed = true;
  157. Repaint();
  158. HandleUtility.nearestControl = m_SelectorControlID;
  159. return true;
  160. }
  161. return false;
  162. }
  163. public bool DoCreateRange(Rect rect, float radius, float angleOffset, float start, float end)
  164. {
  165. if (IsActionTriggering(AngleRangeAction.CreateRange))
  166. {
  167. GUI.changed = true;
  168. HandleUtility.nearestControl = m_SelectorControlID;
  169. return true;
  170. }
  171. if (IsActionActive(AngleRangeAction.CreateRange))
  172. {
  173. DrawAngleRangeOutline(rect, start, end, angleOffset, radius);
  174. if (Event.current.type == EventType.MouseMove)
  175. Repaint();
  176. }
  177. return false;
  178. }
  179. public void DoCreateRangeTooltip()
  180. {
  181. if (IsActionActive(AngleRangeAction.CreateRange))
  182. {
  183. var mousePosition = Event.current.mousePosition;
  184. EditorGUI.LabelField(new Rect(mousePosition, new Vector2(1f, 20f)), Contents.addRangeTooltip);
  185. }
  186. }
  187. public bool DoSelector(Rect rect, float angleOffset, float radius, float angle, out float newAngle)
  188. {
  189. EditorGUI.BeginChangeCheck();
  190. newAngle = AngleRangeGUI.AngleField(rect, m_SelectorControlID, angle, angleOffset, Vector2.down * 7.5f, angle, 15f, radius - AngleRangeGUI.kRangeWidth, true, true, false, SpriteShapeHandleUtility.PlayHeadCap);
  191. return EditorGUI.EndChangeCheck();
  192. }
  193. public bool DoRemoveRange()
  194. {
  195. EventType eventType = Event.current.type;
  196. if (IsActionTriggering(AngleRangeAction.RemoveRange))
  197. {
  198. Event.current.Use();
  199. GUI.changed = true;
  200. return true;
  201. }
  202. return false;
  203. }
  204. public bool IsActionActive(AngleRangeAction action)
  205. {
  206. if (GUIUtility.hotControl != 0)
  207. return false;
  208. if (action == AngleRangeAction.SelectRange)
  209. return HandleUtility.nearestControl == m_HoveredRangeID;
  210. if (action == AngleRangeAction.ModifyRange)
  211. return HandleUtility.nearestControl == m_HoveredHandleID;
  212. if (action == AngleRangeAction.CreateRange)
  213. return HandleUtility.nearestControl == m_CreateRangeControlID;
  214. if (action == AngleRangeAction.ModifySelector)
  215. return HandleUtility.nearestControl == m_SelectorControlID;
  216. if (action == AngleRangeAction.RemoveRange)
  217. return HasKeyboardFocus();
  218. return false;
  219. }
  220. public bool IsActionHot(AngleRangeAction action)
  221. {
  222. if (GUIUtility.hotControl == 0)
  223. return false;
  224. if (action == AngleRangeAction.ModifyRange)
  225. return GUIUtility.hotControl == m_HotHandleID;
  226. return false;
  227. }
  228. public bool IsActionTriggering(AngleRangeAction action)
  229. {
  230. if (!IsActionActive(action))
  231. return false;
  232. EventType eventType = Event.current.type;
  233. if (action == AngleRangeAction.RemoveRange)
  234. {
  235. if ((eventType == EventType.ValidateCommand || eventType == EventType.ExecuteCommand)
  236. && (Event.current.commandName == kSoftDeleteCommandName || Event.current.commandName == kDeleteCommandName))
  237. {
  238. if (eventType == EventType.ExecuteCommand)
  239. return true;
  240. Event.current.Use();
  241. }
  242. return false;
  243. }
  244. return eventType == EventType.MouseDown && Event.current.button == 0;
  245. }
  246. public bool IsActionFinishing(AngleRangeAction action)
  247. {
  248. if (!IsActionHot(action))
  249. return false;
  250. return (Event.current.type == EventType.MouseUp && Event.current.button == 0) || Event.current.type == EventType.Ignore;
  251. }
  252. public void DrawAngleRangeOutline(Rect rect, float start, float end, float angleOffset, float radius)
  253. {
  254. if (Event.current.type == EventType.Repaint)
  255. SpriteShapeHandleUtility.DrawRangeOutline(start, end, angleOffset, rect.center, radius, AngleRangeGUI.kRangeWidth - 1f);
  256. }
  257. private void GrabKeyboardFocus(int controlID)
  258. {
  259. m_FocusedRangeControlID = controlID;
  260. GUIUtility.keyboardControl = controlID;
  261. }
  262. private bool HasKeyboardFocus()
  263. {
  264. return GUIUtility.keyboardControl == m_FocusedRangeControlID;
  265. }
  266. }
  267. }