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