A game about forced loneliness, made by TACStudios
1using System;
2using UnityEngine;
3
4namespace UnityEditor.U2D.Common.Path.GUIFramework
5{
6 /// <summary>
7 /// Represents a UI control in a custom editor.
8 /// </summary>
9 internal abstract class Control
10 {
11 private string m_Name;
12 private int m_NameHashCode;
13 private int m_ID;
14 private LayoutData m_LayoutData;
15 private int m_ActionID = -1;
16 private LayoutData m_HotLayoutData;
17
18 /// <summary>
19 /// The name of the control.
20 /// </summary>
21 public string name
22 {
23 get { return m_Name; }
24 }
25
26 /// <summary>
27 /// The control ID. The GUI uses this to identify the control.
28 /// </summary>
29 public int ID
30 {
31 get { return m_ID; }
32 }
33
34 /// <summary>
35 /// The action ID.
36 /// </summary>
37 public int actionID
38 {
39 get { return m_ActionID; }
40 }
41
42 /// <summary>
43 /// The control's layout data. This contains information about the control's position and orientation.
44 /// </summary>
45 public LayoutData layoutData
46 {
47 get { return m_LayoutData; }
48 set { m_LayoutData = value; }
49 }
50
51 /// <summary>
52 /// The control's hot layout data
53 /// </summary>
54 public LayoutData hotLayoutData
55 {
56 get { return m_HotLayoutData; }
57 }
58
59 /// <summary>
60 /// Initializes and returns an instance of Control
61 /// </summary>
62 /// <param name="name">The name of the control</param>
63 public Control(string name)
64 {
65 m_Name = name;
66 m_NameHashCode = name.GetHashCode();
67 }
68
69 /// <summary>
70 /// Gets the control from the guiState.
71 /// </summary>
72 /// <param name="guiState">The current state of the custom editor.</param>
73 public void GetControl(IGUIState guiState)
74 {
75 m_ID = guiState.GetControlID(m_NameHashCode, FocusType.Passive);
76 }
77
78 internal void SetActionID(int actionID)
79 {
80 m_ActionID = actionID;
81 m_HotLayoutData = m_LayoutData;
82 }
83
84 /// <summary>
85 /// Begins the layout for this control. A call to EndLayout must always follow a call to this function.
86 /// </summary>
87 /// <param name="guiState">The current state of the custom editor.</param>
88 public void BeginLayout(IGUIState guiState)
89 {
90 Debug.Assert(guiState.eventType == EventType.Layout);
91
92 m_LayoutData = OnBeginLayout(LayoutData.zero, guiState);
93 }
94
95 /// <summary>
96 /// Gets the control's layout data from the guiState.
97 /// </summary>
98 /// <param name="guiState">The current state of the custom editor.</param>
99 public void Layout(IGUIState guiState)
100 {
101 Debug.Assert(guiState.eventType == EventType.Layout);
102
103 for (var i = 0; i < GetCount(); ++i)
104 {
105 if (guiState.hotControl == actionID && hotLayoutData.index == i)
106 continue;
107
108 var layoutData = new LayoutData()
109 {
110 index = i,
111 position = GetPosition(guiState, i),
112 distance = GetDistance(guiState, i),
113 forward = GetForward(guiState, i),
114 up = GetUp(guiState, i),
115 right = GetRight(guiState, i),
116 userData = GetUserData(guiState, i)
117 };
118
119 m_LayoutData = LayoutData.Nearest(m_LayoutData, layoutData);
120 }
121 }
122
123 /// <summary>
124 /// Ends the layout for this control. This function must always follow a call to BeginLayout().
125 /// </summary>
126 /// <param name="guiState">The current state of the custom editor.</param>
127 public void EndLayout(IGUIState guiState)
128 {
129 Debug.Assert(guiState.eventType == EventType.Layout);
130
131 OnEndLayout(guiState);
132 }
133
134 /// <summary>
135 /// Repaints the control.
136 /// </summary>
137 /// <param name="guiState">The current state of the custom editor.</param>
138 public void Repaint(IGUIState guiState)
139 {
140 for (var i = 0; i < GetCount(); ++i)
141 OnRepaint(guiState, i);
142 }
143
144 /// <summary>
145 /// Called when the control begins its layout.
146 /// </summary>
147 /// <param name="data">The layout data.</param>
148 /// <param name="guiState">The current state of the custom editor.</param>
149 /// <returns>Returns the layout data to use.</returns>
150 protected virtual LayoutData OnBeginLayout(LayoutData data, IGUIState guiState)
151 {
152 return data;
153 }
154
155 /// <summary>
156 /// Called when the control ends its layout.
157 /// /// </summary>
158 /// <param name="guiState">The current state of the custom editor.</param>
159 protected virtual void OnEndLayout(IGUIState guiState)
160 {
161 }
162
163 /// <summary>
164 /// Called when the control repaints its contents.
165 /// </summary>
166 /// <param name="guiState">The current state of the custom editor.</param>
167 /// <param name="index">The index.</param>
168 protected virtual void OnRepaint(IGUIState guiState, int index)
169 {
170 }
171
172 /// <summary>
173 /// Gets the number of sub-controllers.
174 /// </summary>
175 /// <remarks>
176 /// By default, this is `1`. If you implement your own controller and want to use multiple sub-controllers within it, you can override this function to declare how to count the sub-controllers.
177 /// </remarks>
178 /// <returns>Returns the number of sub-controllers. If you do not override this function, this returns 1.</returns>
179 protected virtual int GetCount()
180 {
181 return 1;
182 }
183
184 /// <summary>
185 /// Gets the position of the control.
186 /// </summary>
187 /// <param name="guiState">The current state of the custom editor.</param>
188 /// <param name="index">The index.</param>
189 /// <returns>Returns Vector3.zero.</returns>
190 protected virtual Vector3 GetPosition(IGUIState guiState, int index)
191 {
192 return Vector3.zero;
193 }
194
195 /// <summary>
196 /// Gets the forward vector of the control.
197 /// </summary>
198 /// <param name="guiState">The current state of the custom editor.</param>
199 /// <param name="index">The index.</param>
200 /// <returns>Returns Vector3.forward.</returns>
201 protected virtual Vector3 GetForward(IGUIState guiState, int index)
202 {
203 return Vector3.forward;
204 }
205
206 /// <summary>
207 /// Gets the up vector of the control.
208 /// </summary>
209 /// <param name="guiState">The current state of the custom editor.</param>
210 /// <param name="index">The index.</param>
211 /// <returns>Returns Vector3.up,</returns>
212 protected virtual Vector3 GetUp(IGUIState guiState, int index)
213 {
214 return Vector3.up;
215 }
216
217 /// <summary>
218 /// Gets the right vector of the control.
219 /// </summary>
220 /// <param name="guiState">The current state of the custom editor.</param>
221 /// <param name="index">The index.</param>
222 /// <returns>Returns Vector3.right.</returns>
223 protected virtual Vector3 GetRight(IGUIState guiState, int index)
224 {
225 return Vector3.right;
226 }
227
228 /// <summary>
229 /// Gets the distance from the Scene view camera to the control.
230 /// </summary>
231 /// <param name="guiState">The current state of the custom editor.</param>
232 /// <param name="index">The index.</param>
233 /// <returns>Returns layoutData.distance.</returns>
234 protected virtual float GetDistance(IGUIState guiState, int index)
235 {
236 return layoutData.distance;
237 }
238
239 /// <summary>
240 /// Gets the control's user data.
241 /// </summary>
242 /// <param name="guiState">The current state of the custom editor.</param>
243 /// <param name="index">The index.</param>
244 /// <returns>Returns `null`.</returns>
245 protected virtual object GetUserData(IGUIState guiState, int index)
246 {
247 return null;
248 }
249 }
250}