No Description
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.

RectSelector.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEditor.U2D.Common.Path.GUIFramework;
  6. namespace UnityEditor.U2D.Common.Path
  7. {
  8. internal abstract class RectSelector<T> : ISelector<T>
  9. {
  10. internal class Styles
  11. {
  12. public readonly GUIStyle selectionRectStyle;
  13. public Styles()
  14. {
  15. selectionRectStyle = GUI.skin.FindStyle("selectionRect");
  16. }
  17. }
  18. public Action<ISelector<T>, bool> onSelectionBegin;
  19. public Action<ISelector<T>> onSelectionChanged;
  20. public Action<ISelector<T>> onSelectionEnd;
  21. private GUISystem m_GUISystem;
  22. private Control m_RectSelectorControl;
  23. private GUIAction m_RectSelectAction;
  24. private Rect m_GUIRect;
  25. private Styles m_Styles;
  26. public Rect guiRect
  27. {
  28. get { return m_GUIRect; }
  29. }
  30. private Styles styles
  31. {
  32. get
  33. {
  34. if (m_Styles == null)
  35. m_Styles = new Styles();
  36. return m_Styles;
  37. }
  38. }
  39. public RectSelector() : this(new GUISystem(new GUIState())) { }
  40. public RectSelector(GUISystem guiSystem)
  41. {
  42. m_GUISystem = guiSystem;
  43. m_RectSelectorControl = new GenericDefaultControl("RectSelector");
  44. var start = Vector2.zero;
  45. var rectEnd = Vector2.zero;
  46. m_RectSelectAction = new SliderAction(m_RectSelectorControl)
  47. {
  48. enable = (guiState, action) => !IsAltDown(guiState),
  49. enableRepaint = (guiState, action) =>
  50. {
  51. var size = start - rectEnd;
  52. return size != Vector2.zero && guiState.hotControl == action.ID;
  53. },
  54. onSliderBegin = (guiState, control, position) =>
  55. {
  56. start = guiState.mousePosition;
  57. rectEnd = guiState.mousePosition;
  58. m_GUIRect = FromToRect(start, rectEnd);
  59. if (onSelectionBegin != null)
  60. onSelectionBegin(this, guiState.isShiftDown);
  61. },
  62. onSliderChanged = (guiState, control, position) =>
  63. {
  64. rectEnd = guiState.mousePosition;
  65. m_GUIRect = FromToRect(start, rectEnd);
  66. if (onSelectionChanged != null)
  67. onSelectionChanged(this);
  68. },
  69. onSliderEnd = (guiState, control, position) =>
  70. {
  71. if (onSelectionEnd != null)
  72. onSelectionEnd(this);
  73. },
  74. onRepaint = (guiState, action) =>
  75. {
  76. Handles.BeginGUI();
  77. styles.selectionRectStyle.Draw(m_GUIRect, GUIContent.none, false, false, false, false);
  78. Handles.EndGUI();
  79. }
  80. };
  81. m_GUISystem.AddControl(m_RectSelectorControl);
  82. m_GUISystem.AddAction(m_RectSelectAction);
  83. }
  84. private bool IsAltDown(IGUIState guiState)
  85. {
  86. return guiState.hotControl == 0 && guiState.isAltDown;
  87. }
  88. private Rect FromToRect(Vector2 start, Vector2 end)
  89. {
  90. Rect r = new Rect(start.x, start.y, end.x - start.x, end.y - start.y);
  91. if (r.width < 0)
  92. {
  93. r.x += r.width;
  94. r.width = -r.width;
  95. }
  96. if (r.height < 0)
  97. {
  98. r.y += r.height;
  99. r.height = -r.height;
  100. }
  101. return r;
  102. }
  103. public void OnGUI()
  104. {
  105. m_GUISystem.OnGUI();
  106. }
  107. bool ISelector<T>.Select(T element)
  108. {
  109. return Select(element);
  110. }
  111. protected abstract bool Select(T element);
  112. }
  113. }