forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanel.cs
More file actions
684 lines (551 loc) · 23.2 KB
/
Panel.cs
File metadata and controls
684 lines (551 loc) · 23.2 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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using UnityEngine.Profiling;
namespace UnityEngine.UIElements
{
public enum ContextType
{
Player = 0,
Editor = 1
}
[Flags]
internal enum VersionChangeType
{
// Some data was bound
Bindings = 1 << 8,
// persistent data ready
ViewData = 1 << 7,
// changes to hierarchy
Hierarchy = 1 << 6,
// changes to properties that may have an impact on layout
Layout = 1 << 5,
// changes to StyleSheet, USS class
StyleSheet = 1 << 4,
// changes to styles, colors and other render properties
Styles = 1 << 3,
// changes that may impact the world transform (e.g. laid out position, local transform)
Transform = 1 << 2,
// changes to the size of the element after layout has been performed, without taking the local transform into account
Size = 1 << 1,
// The visuals of the element have changed
// TODO: Rename to visuals
Repaint = 1 << 0,
}
[Flags]
internal enum RenderHint
{
None = 0,
GroupTransform = 1 << 0, // Use uniform matrix to transform children
BoneTransform = 1 << 1, // Use GPU buffer to store transform matrices
ClipWithScissors = 1 << 2 // If clipping is requested on this element, prefer scissoring
}
internal class RepaintData
{
public Matrix4x4 currentOffset { get; set; } = Matrix4x4.identity;
public Vector2 mousePosition { get; set; }
public Rect currentWorldClip { get; set; }
public Event repaintEvent { get; set; }
}
internal delegate void HierarchyEvent(VisualElement ve, HierarchyChangeType changeType);
internal interface IGlobalPanelDebugger
{
bool InterceptMouseEvent(IPanel panel, IMouseEvent ev);
void OnPostMouseEvent(IPanel panel, IMouseEvent ev);
}
internal interface IPanelDebugger
{
IPanelDebug panelDebug { get; set; }
void Disconnect();
void Refresh();
void OnVersionChanged(VisualElement ele, VersionChangeType changeTypeFlag);
bool InterceptEvent(EventBase ev);
void PostProcessEvent(EventBase ev);
}
internal interface IPanelDebug
{
IPanel panel { get; }
VisualElement visualTree { get; }
void AttachDebugger(IPanelDebugger debugger);
void DetachDebugger(IPanelDebugger debugger);
void DetachAllDebuggers();
IEnumerable<IPanelDebugger> GetAttachedDebuggers();
void MarkDirtyRepaint();
void Refresh();
void OnVersionChanged(VisualElement ele, VersionChangeType changeTypeFlag);
bool InterceptEvent(EventBase ev);
void PostProcessEvent(EventBase ev);
}
// This is the required interface to IPanel for Runtime game components.
internal interface IRuntimePanel
{
void Update(Vector2 size);
void Repaint(Event e);
}
// Passed-in to every element of the visual tree
public interface IPanel : IDisposable
{
VisualElement visualTree { get; }
EventDispatcher dispatcher { get; }
ContextType contextType { get; }
FocusController focusController { get; }
VisualElement Pick(Vector2 point);
VisualElement PickAll(Vector2 point, List<VisualElement> picked);
}
abstract class BaseVisualElementPanel : IPanel, IRuntimePanel
{
public abstract EventInterests IMGUIEventInterests { get; set; }
public abstract ScriptableObject ownerObject { get; protected set; }
public abstract SavePersistentViewData saveViewData { get; set; }
public abstract GetViewDataDictionary getViewDataDictionary { get; set; }
public abstract int IMGUIContainersCount { get; set; }
public abstract FocusController focusController { get; set; }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
if (panelDebug != null)
{
panelDebug.DetachAllDebuggers();
panelDebug = null;
}
UIElementsUtility.RemoveCachedPanel(ownerObject.GetInstanceID());
}
else
DisposeHelper.NotifyMissingDispose(this);
disposed = true;
}
public abstract void Repaint(Event e);
public abstract void ValidateLayout();
public abstract void UpdateBindings();
public abstract void ApplyStyles();
public abstract void DirtyStyleSheets();
internal float currentPixelsPerPoint { get; set; } = 1.0f;
internal bool duringLayoutPhase {get; set;}
internal bool isDirty
{
get { return version != repaintVersion; }
}
internal abstract uint version { get; }
internal abstract uint repaintVersion { get; }
internal abstract void OnVersionChanged(VisualElement ele, VersionChangeType changeTypeFlag);
internal abstract void SetUpdater(IVisualTreeUpdater updater, VisualTreeUpdatePhase phase);
internal virtual RepaintData repaintData { get; set; }
internal virtual ICursorManager cursorManager { get; set; }
internal virtual ContextualMenuManager contextualMenuManager { get; set; }
//IPanel
public abstract VisualElement visualTree { get; }
public abstract EventDispatcher dispatcher { get; protected set; }
internal void SendEvent(EventBase e, DispatchMode dispatchMode = DispatchMode.Queued)
{
Debug.Assert(dispatcher != null);
dispatcher?.Dispatch(e, this, dispatchMode);
}
internal abstract IScheduler scheduler { get; }
public abstract ContextType contextType { get; protected set; }
public abstract VisualElement Pick(Vector2 point);
public abstract VisualElement PickAll(Vector2 point, List<VisualElement> picked);
internal bool disposed { get; private set; }
internal bool allowPixelCaching { get; set; }
public abstract bool keepPixelCacheOnWorldBoundChange { get; set; }
internal abstract IVisualTreeUpdater GetUpdater(VisualTreeUpdatePhase phase);
internal VisualElement topElementUnderMouse { get; private set; }
internal abstract Shader standardShader { get; set; }
internal event Action standardShaderChanged;
protected void InvokeStandardShaderChanged() { if (standardShaderChanged != null) standardShaderChanged(); }
internal event HierarchyEvent hierarchyChanged;
internal void InvokeHierarchyChanged(VisualElement ve, HierarchyChangeType changeType) { if (hierarchyChanged != null) hierarchyChanged(ve, changeType); }
internal void SetElementUnderMouse(VisualElement newElementUnderMouse, EventBase triggerEvent)
{
if (newElementUnderMouse == topElementUnderMouse)
return;
VisualElement previousTopElementUnderMouse = topElementUnderMouse;
topElementUnderMouse = newElementUnderMouse;
IMouseEvent mouseEvent = triggerEvent == null ? null : triggerEvent as IMouseEvent;
var mousePosition = mouseEvent == null
? MousePositionTracker.mousePosition
: mouseEvent?.mousePosition ?? Vector2.zero;
var sendMouseOverOut = (triggerEvent == null ||
triggerEvent.eventTypeId == MouseMoveEvent.TypeId() ||
triggerEvent.eventTypeId == MouseDownEvent.TypeId() ||
triggerEvent.eventTypeId == MouseUpEvent.TypeId() ||
triggerEvent.eventTypeId == MouseEnterWindowEvent.TypeId() ||
triggerEvent.eventTypeId == MouseLeaveWindowEvent.TypeId() ||
triggerEvent.eventTypeId == WheelEvent.TypeId());
var sendDragEnterLeave = triggerEvent != null && (triggerEvent.eventTypeId == DragUpdatedEvent.TypeId() || triggerEvent.eventTypeId == DragExitedEvent.TypeId());
using (new EventDispatcherGate(dispatcher))
{
// mouse enter/leave must be dispatched *any* time the element under mouse changes
MouseEventsHelper.SendEnterLeave<MouseLeaveEvent, MouseEnterEvent>(previousTopElementUnderMouse, topElementUnderMouse, mouseEvent, mousePosition);
if (sendMouseOverOut)
MouseEventsHelper.SendMouseOverMouseOut(previousTopElementUnderMouse, topElementUnderMouse, mouseEvent, mousePosition);
if (sendDragEnterLeave)
MouseEventsHelper.SendEnterLeave<DragLeaveEvent, DragEnterEvent>(previousTopElementUnderMouse, topElementUnderMouse, mouseEvent, mousePosition);
}
}
internal void UpdateElementUnderMouse()
{
if (MousePositionTracker.panel != this)
{
SetElementUnderMouse(null, null);
}
else
{
VisualElement elementUnderMouse = Pick(MousePositionTracker.mousePosition);
SetElementUnderMouse(elementUnderMouse, null);
}
}
public IPanelDebug panelDebug { get; set; }
public void Update(Vector2 size)
{
scheduler.UpdateScheduledEvents();
if (size != visualTree.layout.size)
{
visualTree.SetSize(size);
}
ValidateLayout();
UpdateBindings();
}
}
// Strategy to load assets must be provided in the context of Editor or Runtime
internal delegate Object LoadResourceFunction(string pathName, System.Type type);
// Strategy to fetch real time since startup in the context of Editor or Runtime
internal delegate long TimeMsFunction();
// Getting the view data dictionary relies on the Editor window.
internal delegate ISerializableJsonDictionary GetViewDataDictionary();
// Strategy to save persistent data must be provided in the context of Editor or Runtime
internal delegate void SavePersistentViewData();
// Default panel implementation
internal class Panel : BaseVisualElementPanel
{
private VisualElement m_RootContainer;
private VisualTreeUpdater m_VisualTreeUpdater;
private string m_PanelName;
private string m_ProfileUpdateName;
private string m_ProfileLayoutName;
private string m_ProfileBindingsName;
private uint m_Version = 0;
private uint m_RepaintVersion = 0;
#pragma warning disable CS0649
internal static Action BeforeUpdaterChange;
internal static Action AfterUpdaterChange;
#pragma warning restore CS0649
public override VisualElement visualTree
{
get { return m_RootContainer; }
}
public override EventDispatcher dispatcher { get; protected set; }
TimerEventScheduler m_Scheduler;
public TimerEventScheduler timerEventScheduler
{
get { return m_Scheduler ?? (m_Scheduler = new TimerEventScheduler()); }
}
internal override IScheduler scheduler
{
get { return timerEventScheduler; }
}
public override ScriptableObject ownerObject { get; protected set; }
public override ContextType contextType { get; protected set; }
public override SavePersistentViewData saveViewData { get; set; }
public override GetViewDataDictionary getViewDataDictionary { get; set; }
public override FocusController focusController { get; set; }
public override EventInterests IMGUIEventInterests { get; set; }
internal static LoadResourceFunction loadResourceFunc { private get; set; }
internal static Object LoadResource(string pathName, Type type)
{
// TODO make the LoadResource function non-static.
// if (panel.contextType = ContextType.Player)
// obj = Resources.Load(pathName, type);
// else
// ...
Object obj = null;
if (loadResourceFunc != null)
{
obj = loadResourceFunc(pathName, type);
}
else
{
obj = Resources.Load(pathName, type);
}
return obj;
}
private Focusable m_SavedFocusedElement;
internal void Focus()
{
if (m_SavedFocusedElement != null && !(m_SavedFocusedElement is IMGUIContainer))
m_SavedFocusedElement.Focus();
m_SavedFocusedElement = null;
}
internal void Blur()
{
m_SavedFocusedElement = focusController?.GetLeafFocusedElement();
if (m_SavedFocusedElement != null && !(m_SavedFocusedElement is IMGUIContainer))
m_SavedFocusedElement.Blur();
}
internal string name
{
get { return m_PanelName; }
set
{
m_PanelName = value;
if (!string.IsNullOrEmpty(m_PanelName))
{
m_ProfileUpdateName = $"PanelUpdate.{m_PanelName}";
m_ProfileLayoutName = $"PanelLayout.{m_PanelName}";
m_ProfileBindingsName = $"PanelBindings.{m_PanelName}";
}
else
{
m_ProfileUpdateName = "PanelUpdate";
m_ProfileLayoutName = "PanelLayout";
m_ProfileBindingsName = "PanelBindings";
}
}
}
private static TimeMsFunction s_TimeSinceStartup;
internal static TimeMsFunction TimeSinceStartup
{
get { return s_TimeSinceStartup; }
set
{
if (value == null)
{
value = DefaultTimeSinceStartupMs;
}
s_TimeSinceStartup = value;
}
}
private bool m_KeepPixelCacheOnWorldBoundChange;
public override bool keepPixelCacheOnWorldBoundChange
{
get { return m_KeepPixelCacheOnWorldBoundChange; }
set
{
if (m_KeepPixelCacheOnWorldBoundChange == value)
return;
m_KeepPixelCacheOnWorldBoundChange = value;
// We only need to force a repaint if this flag was set from
// true (do NOT update pixel cache) to false (update pixel cache).
// When it was true, the pixel cache was just being transformed and
// now we want to regenerate it at the correct resolution. Going from
// false to true does not need a repaint because the pixel cache is
// already valid (was being updated each transform repaint).
if (!value)
{
m_RootContainer.IncrementVersion(VersionChangeType.Transform | VersionChangeType.Repaint);
}
}
}
public override int IMGUIContainersCount { get; set; }
internal override uint version
{
get { return m_Version; }
}
internal override uint repaintVersion
{
get { return m_RepaintVersion; }
}
private Shader m_StandardShader;
internal override Shader standardShader
{
get { return m_StandardShader; }
set
{
if (m_StandardShader != value)
{
m_StandardShader = value;
InvokeStandardShaderChanged();
}
}
}
public Panel(ScriptableObject ownerObject, ContextType contextType, EventDispatcher dispatcher = null)
{
m_VisualTreeUpdater = new VisualTreeUpdater(this);
this.ownerObject = ownerObject;
this.contextType = contextType;
this.dispatcher = dispatcher ?? EventDispatcher.instance;
repaintData = new RepaintData();
cursorManager = new CursorManager();
contextualMenuManager = null;
m_RootContainer = new VisualElement
{
name = VisualElementUtils.GetUniqueName("unity-panel-container"),
viewDataKey = "PanelContainer"
};
// Required!
visualTree.SetPanel(this);
focusController = new FocusController(new VisualElementFocusRing(visualTree));
m_ProfileUpdateName = "PanelUpdate";
m_ProfileLayoutName = "PanelLayout";
m_ProfileBindingsName = "PanelBindings";
allowPixelCaching = true;
InvokeHierarchyChanged(visualTree, HierarchyChangeType.Add);
}
protected override void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
m_VisualTreeUpdater.Dispose();
base.Dispose(disposing);
}
public static long TimeSinceStartupMs()
{
return (s_TimeSinceStartup == null) ? DefaultTimeSinceStartupMs() : s_TimeSinceStartup();
}
internal static long DefaultTimeSinceStartupMs()
{
return (long)(Time.realtimeSinceStartup * 1000.0f);
}
// For tests only.
internal static VisualElement PickAllWithoutValidatingLayout(VisualElement root, Vector2 point)
{
return PickAll(root, point);
}
private static VisualElement PickAll(VisualElement root, Vector2 point, List<VisualElement> picked = null)
{
Profiler.BeginSample("Panel.PickAll");
var result = PerformPick(root, point, picked);
Profiler.EndSample();
return result;
}
private static VisualElement PerformPick(VisualElement root, Vector2 point, List<VisualElement> picked = null)
{
// Skip picking for elements with display: none
if (root.resolvedStyle.display == DisplayStyle.None)
return null;
if (root.pickingMode == PickingMode.Ignore && root.hierarchy.childCount == 0)
{
return null;
}
Vector2 localPoint = root.WorldToLocal(point);
if (!root.boundingBox.Contains(localPoint))
{
return null;
}
bool containsPoint = root.ContainsPoint(localPoint);
// we only skip children in the case we visually clip them
if (!containsPoint && root.ShouldClip())
{
return null;
}
VisualElement returnedChild = null;
// Depth first in reverse order, do children
for (int i = root.hierarchy.childCount - 1; i >= 0; i--)
{
var child = root.hierarchy[i];
var result = PerformPick(child, point, picked);
if (returnedChild == null && result != null && result.visible)
returnedChild = result;
}
if (picked != null && root.enabledInHierarchy && root.visible && root.pickingMode == PickingMode.Position && containsPoint)
{
picked.Add(root);
}
if (returnedChild != null)
return returnedChild;
switch (root.pickingMode)
{
case PickingMode.Position:
{
if (containsPoint && root.enabledInHierarchy && root.visible)
{
return root;
}
}
break;
case PickingMode.Ignore:
break;
}
return null;
}
public override VisualElement PickAll(Vector2 point, List<VisualElement> picked)
{
ValidateLayout();
if (picked != null)
picked.Clear();
return PickAll(visualTree, point, picked);
}
public override VisualElement Pick(Vector2 point)
{
ValidateLayout();
return PickAll(visualTree, point);
}
private bool m_ValidatingLayout = false;
public override void ValidateLayout()
{
// Reentrancy proofing: ValidateLayout() could be in the code path of updaters.
// Actual case: TransformClip update phase recomputes elements under mouse, which does a pick, which validates layout.
// Updaters use version numbers for early exit, but it may happen that an updater invalidates a subsequent updater.
if (!m_ValidatingLayout)
{
m_ValidatingLayout = true;
Profiler.BeginSample(m_ProfileLayoutName);
m_VisualTreeUpdater.UpdateVisualTreePhase(VisualTreeUpdatePhase.Styles);
m_VisualTreeUpdater.UpdateVisualTreePhase(VisualTreeUpdatePhase.Layout);
m_VisualTreeUpdater.UpdateVisualTreePhase(VisualTreeUpdatePhase.TransformClip);
Profiler.EndSample();
m_ValidatingLayout = false;
}
}
public override void UpdateBindings()
{
Profiler.BeginSample(m_ProfileBindingsName);
m_VisualTreeUpdater.UpdateVisualTreePhase(VisualTreeUpdatePhase.Bindings);
Profiler.EndSample();
}
public override void ApplyStyles()
{
m_VisualTreeUpdater.UpdateVisualTreePhase(VisualTreeUpdatePhase.Styles);
}
public override void DirtyStyleSheets()
{
m_VisualTreeUpdater.DirtyStyleSheets();
}
public override void Repaint(Event e)
{
Debug.Assert(GUIClip.Internal_GetCount() == 0, "UIElement is not compatible with IMGUI GUIClips, only GUIClip.ParentClipScope");
m_RepaintVersion = version;
// if the surface DPI changes we need to invalidate styles
if (!Mathf.Approximately(currentPixelsPerPoint, GUIUtility.pixelsPerPoint))
{
currentPixelsPerPoint = GUIUtility.pixelsPerPoint;
visualTree.IncrementVersion(VersionChangeType.StyleSheet);
}
repaintData.repaintEvent = e;
Profiler.BeginSample(m_ProfileUpdateName);
try
{
m_VisualTreeUpdater.UpdateVisualTree();
}
finally
{
Profiler.EndSample();
}
panelDebug?.Refresh();
}
internal override void OnVersionChanged(VisualElement ve, VersionChangeType versionChangeType)
{
++m_Version;
m_VisualTreeUpdater.OnVersionChanged(ve, versionChangeType);
panelDebug?.OnVersionChanged(ve, versionChangeType);
}
internal override void SetUpdater(IVisualTreeUpdater updater, VisualTreeUpdatePhase phase)
{
m_VisualTreeUpdater.SetUpdater(updater, phase);
}
internal override IVisualTreeUpdater GetUpdater(VisualTreeUpdatePhase phase)
{
return m_VisualTreeUpdater.GetUpdater(phase);
}
}
}