ShapeEditorRectSelection.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEvent = UnityEngine.Event;
  7. using SelectionType = UnityEditor.U2D.Sprites.ShapeEditor.SelectionType;
  8. namespace UnityEditor.U2D.Sprites
  9. {
  10. internal class ShapeEditorRectSelectionTool
  11. {
  12. Vector2 m_SelectStartPoint;
  13. Vector2 m_SelectMousePoint;
  14. bool m_RectSelecting;
  15. int m_RectSelectionID;
  16. const float k_MinSelectionSize = 6f;
  17. public event Action<Rect, SelectionType> RectSelect = (i, p) => {};
  18. public event Action ClearSelection = () => {};
  19. public ShapeEditorRectSelectionTool(IGUIUtility gu)
  20. {
  21. guiUtility = gu;
  22. m_RectSelectionID = guiUtility.GetPermanentControlID();
  23. }
  24. public void OnGUI()
  25. {
  26. var evt = UnityEvent.current;
  27. Handles.BeginGUI();
  28. Vector2 mousePos = evt.mousePosition;
  29. int id = m_RectSelectionID;
  30. switch (evt.GetTypeForControl(id))
  31. {
  32. case EventType.Layout:
  33. case EventType.MouseMove:
  34. if (!Tools.viewToolActive)
  35. HandleUtility.AddDefaultControl(id);
  36. break;
  37. case EventType.MouseDown:
  38. if (HandleUtility.nearestControl == id && evt.button == 0)
  39. {
  40. guiUtility.hotControl = id;
  41. m_SelectStartPoint = mousePos;
  42. }
  43. break;
  44. case EventType.MouseDrag:
  45. if (guiUtility.hotControl == id)
  46. {
  47. if (!m_RectSelecting && (mousePos - m_SelectStartPoint).magnitude > k_MinSelectionSize)
  48. {
  49. m_RectSelecting = true;
  50. }
  51. if (m_RectSelecting)
  52. {
  53. m_SelectMousePoint = mousePos;
  54. SelectionType type = SelectionType.Normal;
  55. if (UnityEvent.current.control)
  56. type = SelectionType.Subtractive;
  57. else if (UnityEvent.current.shift)
  58. type = SelectionType.Additive;
  59. RectSelect(EditorGUIExt.FromToRect(m_SelectStartPoint, m_SelectMousePoint), type);
  60. }
  61. evt.Use();
  62. }
  63. break;
  64. case EventType.Repaint:
  65. if (guiUtility.hotControl == id && m_RectSelecting)
  66. {
  67. EditorStyles.selectionRect.Draw(EditorGUIExt.FromToRect(m_SelectStartPoint, m_SelectMousePoint), GUIContent.none,
  68. false, false, false, false);
  69. }
  70. break;
  71. case EventType.MouseUp:
  72. if (guiUtility.hotControl == id && evt.button == 0)
  73. {
  74. guiUtility.hotControl = 0;
  75. guiUtility.keyboardControl = 0;
  76. if (m_RectSelecting)
  77. {
  78. m_SelectMousePoint = new Vector2(mousePos.x, mousePos.y);
  79. SelectionType type = SelectionType.Normal;
  80. if (UnityEvent.current.control)
  81. type = SelectionType.Subtractive;
  82. else if (UnityEvent.current.shift)
  83. type = SelectionType.Additive;
  84. RectSelect(EditorGUIExt.FromToRect(m_SelectStartPoint, m_SelectMousePoint), type);
  85. m_RectSelecting = false;
  86. }
  87. else
  88. {
  89. ClearSelection();
  90. }
  91. evt.Use();
  92. }
  93. break;
  94. }
  95. Handles.EndGUI();
  96. }
  97. public bool isSelecting
  98. {
  99. get { return guiUtility.hotControl == m_RectSelectionID; }
  100. }
  101. IGUIUtility guiUtility
  102. {
  103. get; set;
  104. }
  105. }
  106. // TODO: For now we copy-paste from RectSelection. Refactor to avoid duplicate codes.
  107. internal class ShapeEditorSelection : IEnumerable<int>
  108. {
  109. HashSet<int> m_SelectedPoints = new HashSet<int>();
  110. ShapeEditor m_ShapeEditor;
  111. public ShapeEditorSelection(ShapeEditor owner)
  112. {
  113. m_ShapeEditor = owner;
  114. }
  115. public bool Contains(int i)
  116. {
  117. return m_SelectedPoints.Contains(i);
  118. }
  119. public int Count
  120. {
  121. get { return m_SelectedPoints.Count; }
  122. }
  123. public void DeleteSelection()
  124. {
  125. var sorted = m_SelectedPoints.OrderByDescending(x => x);
  126. foreach (int selectedIndex in sorted)
  127. {
  128. m_ShapeEditor.RemovePointAt(selectedIndex);
  129. }
  130. if (m_ShapeEditor.activePoint >= m_ShapeEditor.GetPointsCount())
  131. m_ShapeEditor.activePoint = m_ShapeEditor.GetPointsCount() - 1;
  132. m_SelectedPoints.Clear();
  133. }
  134. public void MoveSelection(Vector3 delta)
  135. {
  136. if (delta.sqrMagnitude < float.Epsilon)
  137. return;
  138. foreach (int selectedIndex in m_SelectedPoints)
  139. {
  140. m_ShapeEditor.SetPointPosition(selectedIndex, m_ShapeEditor.GetPointPosition(selectedIndex) + delta);
  141. }
  142. }
  143. public void Clear()
  144. {
  145. m_SelectedPoints.Clear();
  146. if (m_ShapeEditor != null)
  147. m_ShapeEditor.activePoint = -1;
  148. }
  149. public void SelectPoint(int i, SelectionType type)
  150. {
  151. switch (type)
  152. {
  153. case SelectionType.Additive:
  154. m_ShapeEditor.activePoint = i;
  155. m_SelectedPoints.Add(i);
  156. break;
  157. case SelectionType.Subtractive:
  158. m_ShapeEditor.activePoint = i > 0 ? i - 1 : 0;
  159. m_SelectedPoints.Remove(i);
  160. break;
  161. case SelectionType.Normal:
  162. m_SelectedPoints.Clear();
  163. m_ShapeEditor.activePoint = i;
  164. m_SelectedPoints.Add(i);
  165. break;
  166. default:
  167. m_ShapeEditor.activePoint = i; break;
  168. }
  169. m_ShapeEditor.Repaint();
  170. }
  171. public void RectSelect(Rect rect, SelectionType type)
  172. {
  173. if (type == SelectionType.Normal)
  174. {
  175. m_SelectedPoints.Clear();
  176. m_ShapeEditor.activePoint = -1;
  177. type = SelectionType.Additive;
  178. }
  179. for (int i = 0; i < m_ShapeEditor.GetPointsCount(); i++)
  180. {
  181. var p0 = m_ShapeEditor.GetPointPosition(i);
  182. if (rect.Contains(p0))
  183. {
  184. SelectPoint(i, type);
  185. }
  186. }
  187. m_ShapeEditor.Repaint();
  188. }
  189. public HashSet<int> indices { get { return m_SelectedPoints; } }
  190. public IEnumerator<int> GetEnumerator()
  191. {
  192. return m_SelectedPoints.GetEnumerator();
  193. }
  194. IEnumerator IEnumerable.GetEnumerator()
  195. {
  196. return GetEnumerator();
  197. }
  198. }
  199. } // namespace