-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
BlackboardEditor.cs
405 lines (336 loc) · 11.2 KB
/
BlackboardEditor.cs
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright (c) 2020-2023 Vladimir Popov [email protected] https://github.com/ZorPastaman/Simple-Blackboard
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
using Zor.SimpleBlackboard.Core;
namespace Zor.SimpleBlackboard.EditorTools
{
/// <summary>
/// Static helper for drawing a <see cref="Blackboard"/> in the editor.
/// </summary>
public static class BlackboardEditor
{
[NotNull] private const string TablesElementName = "Tables";
[NotNull] private const string NoEditorElementName = "NoEditor";
[NotNull] private const string NoEditorContainerElementName = "NoEditorContainer";
[NotNull] private const string AddElementName = "Add";
[NotNull] private const string OtherTypesLabel = "Other Types";
[NotNull] private const string AddButtonLabel = "Add";
[NotNull] private static readonly Comparison<Type> s_typeByNameComparison = (left, right) =>
string.CompareOrdinal(left.Name, right.Name);
[NotNull]
private static readonly Comparison<VisualElement> s_visualElementByTypeNameComparison = (left, right) =>
{
if (left.userData is Type leftType && right.userData is Type rightType)
{
return s_typeByNameComparison(leftType, rightType);
}
if (!(left.userData is Type) && !(right.userData is Type))
{
return 0;
}
return left.userData is Type ? 1 : -1;
};
[NotNull]
private static readonly EventCallback<PointerDownEvent, ToolbarMenu> s_onToolbarPointerDown = (c, menu) =>
{
menu.userData = GUIUtility.GUIToScreenPoint(c.originalMousePosition);
};
[NotNull] private static readonly Action<DropdownMenuAction> s_onDropdownAction = a =>
{
var popupInfo = (UIElementsPopupInfo)a.userData;
if (popupInfo.blackboardRoot.userData is Blackboard blackboard)
{
Vector2 position = popupInfo.menuButton.userData is Vector2 pos
? pos
: popupInfo.menuButton.LocalToWorld(Vector2.zero);
BlackboardEditorToolsCollection.CreateAddPopup(popupInfo.type, blackboard,
popupInfo.type.Name, position);
}
};
[NotNull] private static readonly GenericMenu.MenuFunction2 s_onCreatePopup = OnCreatePopup;
[NotNull] private static readonly List<Type> s_tableTypes = new List<Type>();
[NotNull] private static readonly List<Type> s_noEditorTables = new List<Type>();
/// <summary>
/// Draws an editor for the <paramref name="blackboard"/>.
/// </summary>
/// <param name="blackboard">An editor is drawn for this.</param>
public static void DrawBlackboard([CanBeNull] Blackboard blackboard)
{
if (blackboard == null)
{
return;
}
lock (blackboard)
{
try
{
blackboard.GetValueTypes(s_tableTypes);
s_tableTypes.Sort(s_typeByNameComparison);
DrawTables(blackboard);
s_tableTypes.Clear();
DrawNoEditor();
DrawAdd(blackboard);
}
finally
{
s_tableTypes.Clear();
s_noEditorTables.Clear();
}
}
}
/// <summary>
/// Creates a <see cref="VisualElement"/> for a <see cref="Blackboard"/>.
/// </summary>
/// <returns>View template for a <see cref="Blackboard"/>.</returns>
/// <remarks>
/// <para>
/// The returned <see cref="VisualElement"/> is used in <see cref="UpdateBlackboardVisualElement"/>.
/// </para>
/// <para>Do not modify the returned <see cref="VisualElement"/>.</para>
/// </remarks>
[NotNull, Pure]
public static VisualElement CreateBlackboardVisualElement()
{
var blackboardRoot = new VisualElement();
blackboardRoot.style.display = DisplayStyle.None;
var tables = new VisualElement {name = TablesElementName};
blackboardRoot.Add(tables);
var noEditor = new VisualElement {name = NoEditorElementName};
noEditor.style.flexDirection = FlexDirection.Row;
var noEditorLabel = new Label(OtherTypesLabel);
IStyle noEditorLabelStyle = noEditorLabel.style;
noEditorLabelStyle.width = 140f;
noEditorLabelStyle.unityFontStyleAndWeight = FontStyle.Bold;
var noEditorContainer = new VisualElement {name = NoEditorContainerElementName};
noEditor.Add(noEditorLabel);
noEditor.Add(noEditorContainer);
blackboardRoot.Add(noEditor);
var toolbar = new Toolbar();
blackboardRoot.Add(toolbar);
try
{
BlackboardEditorToolsCollection.GetValueViewTypes(s_tableTypes);
s_tableTypes.Sort(s_typeByNameComparison);
var toolbarMenu = new ToolbarMenu {name = AddElementName, text = AddButtonLabel};
toolbarMenu.RegisterCallback(s_onToolbarPointerDown, toolbarMenu);
DropdownMenu menu = toolbarMenu.menu;
for (int i = 0, count = s_tableTypes.Count; i < count; ++i)
{
Type type = s_tableTypes[i];
var popupInfo = new UIElementsPopupInfo(blackboardRoot, toolbarMenu, type);
menu.AppendAction(type.Name, s_onDropdownAction, a => DropdownMenuAction.Status.Normal, popupInfo);
}
toolbar.Add(toolbarMenu);
}
finally
{
s_tableTypes.Clear();
}
return blackboardRoot;
}
/// <summary>
/// Updates the <paramref name="blackboardVisualElement"/> with the <paramref name="blackboard"/>.
/// </summary>
/// <param name="blackboardVisualElement">View template for a <see cref="Blackboard"/>.</param>
/// <param name="blackboard">Blackboard used to update the view.</param>
/// <remarks>
/// <paramref name="blackboardVisualElement"/> must be created in <see cref="CreateBlackboardVisualElement"/>.
/// </remarks>
public static void UpdateBlackboardVisualElement([NotNull] VisualElement blackboardVisualElement,
[CanBeNull] Blackboard blackboard)
{
blackboardVisualElement.userData = blackboard;
if (blackboard == null)
{
blackboardVisualElement.style.display = DisplayStyle.None;
return;
}
blackboardVisualElement.style.display = DisplayStyle.Flex;
lock (blackboard)
{
try
{
blackboard.GetValueTypes(s_tableTypes);
VisualElement tables = blackboardVisualElement.Q(TablesElementName);
UpdateTables(tables, blackboardVisualElement);
VisualElement noEditor = blackboardVisualElement.Q(NoEditorElementName);
UpdateNoEditor(noEditor);
}
finally
{
s_tableTypes.Clear();
s_noEditorTables.Clear();
}
}
}
private static void DrawTables([NotNull] Blackboard blackboard)
{
EditorGUI.BeginChangeCheck();
for (int i = 0, count = s_tableTypes.Count; i < count; ++i)
{
Type type = s_tableTypes[i];
if (!BlackboardEditorToolsCollection.TryGetTableEditor(type, out BlackboardTableEditor_Base editor))
{
s_noEditorTables.Add(type);
continue;
}
editor.Draw(blackboard);
}
}
private static void DrawNoEditor()
{
int noEditorCount = s_noEditorTables.Count;
if (noEditorCount == 0)
{
return;
}
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel(OtherTypesLabel);
EditorGUILayout.BeginVertical();
for (int i = 0; i < noEditorCount; ++i)
{
EditorGUILayout.LabelField(s_noEditorTables[i].Name);
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
}
private static void DrawAdd([NotNull] Blackboard blackboard)
{
EditorGUILayout.Separator();
if (GUILayout.Button(AddButtonLabel))
{
var menu = new GenericMenu();
Vector2 position = Event.current.mousePosition;
Vector2 screenPoint = GUIUtility.GUIToScreenPoint(new Vector2(position.x, position.y));
BlackboardEditorToolsCollection.GetValueViewTypes(s_tableTypes);
s_tableTypes.Sort(s_typeByNameComparison);
for (int i = 0, count = s_tableTypes.Count; i < count; ++i)
{
Type type = s_tableTypes[i];
var popupInfo = new PopupInfo (type, blackboard, screenPoint);
menu.AddItem(new GUIContent(type.Name), false, s_onCreatePopup, popupInfo);
}
menu.ShowAsContext();
}
}
private static void UpdateTables([NotNull] VisualElement tablesRoot, [NotNull] VisualElement blackboardRoot)
{
bool structureChanged = false;
for (int i = tablesRoot.childCount - 1; i >= 0; --i)
{
VisualElement element = tablesRoot[i];
if (element.userData is Type type && !s_tableTypes.Contains(type))
{
tablesRoot.RemoveAt(i);
structureChanged = true;
}
}
for (int i = 0, count = s_tableTypes.Count; i < count; ++i)
{
Type type = s_tableTypes[i];
if (!BlackboardEditorToolsCollection.TryGetTableEditor(type, out BlackboardTableEditor_Base editor))
{
s_noEditorTables.Add(type);
continue;
}
VisualElement tableRoot = GetElementInChildrenOfType(tablesRoot, type);
if (tableRoot == null)
{
tableRoot = editor.CreateTable();
tableRoot.userData = type;
tablesRoot.Add(tableRoot);
structureChanged = true;
}
editor.UpdateTable(tableRoot, blackboardRoot);
}
if (structureChanged)
{
tablesRoot.Sort(s_visualElementByTypeNameComparison);
}
}
private static void UpdateNoEditor([NotNull] VisualElement noEditorRoot)
{
int noEditorCount = s_noEditorTables.Count;
if (noEditorCount == 0)
{
noEditorRoot.style.display = DisplayStyle.None;
return;
}
noEditorRoot.style.display = DisplayStyle.Flex;
VisualElement container = noEditorRoot.Q(NoEditorContainerElementName);
bool structureChanged = false;
for (int i = container.childCount - 1; i >= 0; --i)
{
VisualElement element = container[i];
if (element.userData is Type type && !s_noEditorTables.Contains(type))
{
container.RemoveAt(i);
structureChanged = true;
}
}
for (int i = 0; i < noEditorCount; ++i)
{
Type type = s_noEditorTables[i];
if (GetElementInChildrenOfType(container, type) == null)
{
var element = new Label(type.Name) {userData = type};
container.Add(element);
structureChanged = true;
}
}
if (structureChanged)
{
container.Sort(s_visualElementByTypeNameComparison);
}
}
[CanBeNull, Pure]
private static VisualElement GetElementInChildrenOfType([NotNull] VisualElement root, [NotNull] Type type)
{
for (int i = 0, count = root.childCount; i < count; ++i)
{
VisualElement element = root[i];
if (element.userData is Type containedType && containedType == type)
{
return element;
}
}
return null;
}
private static void OnCreatePopup([NotNull] object popupInfoObject)
{
var popupInfo = (PopupInfo)popupInfoObject;
BlackboardEditorToolsCollection.CreateAddPopup(popupInfo.type, popupInfo.blackboard, popupInfo.type.Name,
popupInfo.screenPoint);
}
private readonly struct PopupInfo
{
[NotNull] public readonly Type type;
[NotNull] public readonly Blackboard blackboard;
public readonly Vector2 screenPoint;
public PopupInfo([NotNull] Type type, [NotNull] Blackboard blackboard, Vector2 screenPoint)
{
this.type = type;
this.blackboard = blackboard;
this.screenPoint = screenPoint;
}
}
private readonly struct UIElementsPopupInfo
{
[NotNull] public readonly VisualElement blackboardRoot;
[NotNull] public readonly VisualElement menuButton;
[NotNull] public readonly Type type;
public UIElementsPopupInfo([NotNull] VisualElement blackboardRoot, [NotNull] VisualElement menuButton,
[NotNull] Type type)
{
this.blackboardRoot = blackboardRoot;
this.menuButton = menuButton;
this.type = type;
}
}
}
}