TMP_StyleSheetEditor.cs
11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using UnityEngine;
using UnityEditor;
namespace TMPro.EditorUtilities
{
[CustomPropertyDrawer(typeof(TMP_Style))]
public class StyleDrawer : PropertyDrawer
{
public static readonly float height = 95f;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return height;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
SerializedProperty nameProperty = property.FindPropertyRelative("m_Name");
SerializedProperty hashCodeProperty = property.FindPropertyRelative("m_HashCode");
SerializedProperty openingDefinitionProperty = property.FindPropertyRelative("m_OpeningDefinition");
SerializedProperty closingDefinitionProperty = property.FindPropertyRelative("m_ClosingDefinition");
SerializedProperty openingDefinitionArray = property.FindPropertyRelative("m_OpeningTagArray");
SerializedProperty closingDefinitionArray = property.FindPropertyRelative("m_ClosingTagArray");
EditorGUIUtility.labelWidth = 86;
position.height = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
float labelHeight = position.height + 2f;
EditorGUI.BeginChangeCheck();
Rect rect0 = new Rect(position.x, position.y, (position.width) / 2 + 5, position.height);
EditorGUI.PropertyField(rect0, nameProperty);
if (EditorGUI.EndChangeCheck())
{
// Recompute HashCode if name has changed.
hashCodeProperty.intValue = TMP_TextUtilities.GetSimpleHashCode(nameProperty.stringValue);
property.serializedObject.ApplyModifiedProperties();
// Dictionary needs to be updated since HashCode has changed.
TMP_StyleSheet styleSheet = property.serializedObject.targetObject as TMP_StyleSheet;
styleSheet.RefreshStyles();
}
// HashCode
Rect rect1 = new Rect(rect0.x + rect0.width + 5, position.y, 65, position.height);
GUI.Label(rect1, "HashCode");
GUI.enabled = false;
rect1.x += 65;
rect1.width = position.width / 2 - 75;
EditorGUI.PropertyField(rect1, hashCodeProperty, GUIContent.none);
GUI.enabled = true;
// Text Tags
EditorGUI.BeginChangeCheck();
// Opening Tags
position.y += labelHeight;
GUI.Label(position, "Opening Tags");
Rect textRect1 = new Rect(110, position.y, position.width - 86, 35);
openingDefinitionProperty.stringValue = EditorGUI.TextArea(textRect1, openingDefinitionProperty.stringValue);
if (EditorGUI.EndChangeCheck())
{
// If any properties have changed, we need to update the Opening and Closing Arrays.
int size = openingDefinitionProperty.stringValue.Length;
// Adjust array size to match new string length.
if (openingDefinitionArray.arraySize != size) openingDefinitionArray.arraySize = size;
for (int i = 0; i < size; i++)
{
SerializedProperty element = openingDefinitionArray.GetArrayElementAtIndex(i);
element.intValue = openingDefinitionProperty.stringValue[i];
}
}
EditorGUI.BeginChangeCheck();
// Closing Tags
position.y += 38;
GUI.Label(position, "Closing Tags");
Rect textRect2 = new Rect(110, position.y, position.width - 86, 35);
closingDefinitionProperty.stringValue = EditorGUI.TextArea(textRect2, closingDefinitionProperty.stringValue);
if (EditorGUI.EndChangeCheck())
{
// If any properties have changed, we need to update the Opening and Closing Arrays.
int size = closingDefinitionProperty.stringValue.Length;
// Adjust array size to match new string length.
if (closingDefinitionArray.arraySize != size) closingDefinitionArray.arraySize = size;
for (int i = 0; i < size; i++)
{
SerializedProperty element = closingDefinitionArray.GetArrayElementAtIndex(i);
element.intValue = closingDefinitionProperty.stringValue[i];
}
}
}
}
[CustomEditor(typeof(TMP_StyleSheet)), CanEditMultipleObjects]
public class TMP_StyleEditor : Editor
{
TMP_StyleSheet m_StyleSheet;
SerializedProperty m_StyleListProp;
int m_SelectedElement = -1;
int m_Page;
bool m_IsStyleSheetDirty;
void OnEnable()
{
m_StyleSheet = target as TMP_StyleSheet;
m_StyleListProp = serializedObject.FindProperty("m_StyleList");
}
public override void OnInspectorGUI()
{
Event currentEvent = Event.current;
serializedObject.Update();
m_IsStyleSheetDirty = false;
int elementCount = m_StyleListProp.arraySize;
int itemsPerPage = (Screen.height - 100) / 110;
if (elementCount > 0)
{
// Display each Style entry using the StyleDrawer PropertyDrawer.
for (int i = itemsPerPage * m_Page; i < elementCount && i < itemsPerPage * (m_Page + 1); i++)
{
// Define the start of the selection region of the element.
Rect elementStartRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
SerializedProperty styleProperty = m_StyleListProp.GetArrayElementAtIndex(i);
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(styleProperty);
EditorGUILayout.EndVertical();
if (EditorGUI.EndChangeCheck())
{
//
}
// Define the end of the selection region of the element.
Rect elementEndRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
// Check for Item selection
Rect selectionArea = new Rect(elementStartRegion.x, elementStartRegion.y, elementEndRegion.width, elementEndRegion.y - elementStartRegion.y);
if (DoSelectionCheck(selectionArea))
{
if (m_SelectedElement == i)
{
m_SelectedElement = -1;
}
else
{
m_SelectedElement = i;
GUIUtility.keyboardControl = 0;
}
}
// Handle Selection Highlighting
if (m_SelectedElement == i)
TMP_EditorUtility.DrawBox(selectionArea, 2f, new Color32(40, 192, 255, 255));
}
}
// STYLE LIST MANAGEMENT
Rect rect = EditorGUILayout.GetControlRect(false, 20);
rect.width /= 6;
// Move Style up.
bool guiEnabled = GUI.enabled;
if (m_SelectedElement == -1 || m_SelectedElement == 0) { GUI.enabled = false; }
if (GUI.Button(rect, "Up"))
{
SwapStyleElements(m_SelectedElement, m_SelectedElement - 1);
}
GUI.enabled = guiEnabled;
// Move Style down.
rect.x += rect.width;
if (m_SelectedElement == elementCount - 1) { GUI.enabled = false; }
if (GUI.Button(rect, "Down"))
{
SwapStyleElements(m_SelectedElement, m_SelectedElement + 1);
}
GUI.enabled = guiEnabled;
// Add Style
rect.x += rect.width * 3;
if (GUI.Button(rect, "+"))
{
int index = m_SelectedElement == -1 ? 0 : m_SelectedElement;
if (index > elementCount)
index = elementCount;
// Copy selected element
m_StyleListProp.InsertArrayElementAtIndex(index);
// Select newly inserted element
m_SelectedElement = index + 1;
serializedObject.ApplyModifiedProperties();
m_StyleSheet.RefreshStyles();
}
// Delete style
rect.x += rect.width;
if (m_SelectedElement == -1 || m_SelectedElement >= elementCount) GUI.enabled = false;
if (GUI.Button(rect, "-"))
{
int index = m_SelectedElement == -1 ? 0 : m_SelectedElement;
m_StyleListProp.DeleteArrayElementAtIndex(index);
m_SelectedElement = -1;
serializedObject.ApplyModifiedProperties();
m_StyleSheet.RefreshStyles();
return;
}
// Return if we can't display any items.
if (itemsPerPage == 0) return;
// DISPLAY PAGE CONTROLS
int shiftMultiplier = currentEvent.shift ? 10 : 1; // Page + Shift goes 10 page forward
Rect pagePos = EditorGUILayout.GetControlRect(false, 20);
pagePos.width /= 3;
// Previous Page
if (m_Page > 0) GUI.enabled = true;
else GUI.enabled = false;
if (GUI.Button(pagePos, "Previous"))
m_Page -= 1 * shiftMultiplier;
// PAGE COUNTER
GUI.enabled = true;
pagePos.x += pagePos.width;
int totalPages = (int)(elementCount / (float)itemsPerPage + 0.999f);
GUI.Label(pagePos, "Page " + (m_Page + 1) + " / " + totalPages, TMP_UIStyleManager.centeredLabel);
// Next Page
pagePos.x += pagePos.width;
if (itemsPerPage * (m_Page + 1) < elementCount) GUI.enabled = true;
else GUI.enabled = false;
if (GUI.Button(pagePos, "Next"))
m_Page += 1 * shiftMultiplier;
// Clamp page range
m_Page = Mathf.Clamp(m_Page, 0, elementCount / itemsPerPage);
if (serializedObject.ApplyModifiedProperties())
{
TMPro_EventManager.ON_TEXT_STYLE_PROPERTY_CHANGED(true);
if (m_IsStyleSheetDirty)
{
m_IsStyleSheetDirty = false;
m_StyleSheet.RefreshStyles();
}
}
// Clear selection if mouse event was not consumed.
GUI.enabled = true;
if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0)
m_SelectedElement = -1;
}
// Check if any of the Style elements were clicked on.
static bool DoSelectionCheck(Rect selectionArea)
{
Event currentEvent = Event.current;
switch (currentEvent.type)
{
case EventType.MouseDown:
if (selectionArea.Contains(currentEvent.mousePosition) && currentEvent.button == 0)
{
currentEvent.Use();
return true;
}
break;
}
return false;
}
void SwapStyleElements(int selectedIndex, int newIndex)
{
m_StyleListProp.MoveArrayElement(selectedIndex, newIndex);
m_SelectedElement = newIndex;
m_IsStyleSheetDirty = true;
}
}
}