forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorPanel.cs
More file actions
122 lines (112 loc) · 4.74 KB
/
Copy pathEditorPanel.cs
File metadata and controls
122 lines (112 loc) · 4.74 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
// 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 UnityEngine;
using UnityEngine.UIElements;
namespace UnityEditor.UIElements
{
sealed class EditorPanel : Panel
{
readonly EditorCursorManager m_CursorManager = new EditorCursorManager();
static EditorContextualMenuManager s_ContextualMenuManager = new EditorContextualMenuManager();
static Shader s_EditorShader;
static readonly int s_EditorColorSpaceID = Shader.PropertyToID("_EditorColorSpace");
static Shader EditorShader
{
get
{
if (s_EditorShader == null)
{
s_EditorShader = Shader.Find("Hidden/UIElements/EditorUIE");
}
return s_EditorShader;
}
}
public static Panel FindOrCreate(ScriptableObject ownerObject)
{
var id = ownerObject.GetInstanceID();
Panel panel;
if (UIElementsUtility.TryGetPanel(id, out panel))
return panel;
panel = new EditorPanel(ownerObject);
UIElementsUtility.RegisterCachedPanel(id, panel);
return panel;
}
EditorPanel(ScriptableObject ownerObject)
: base(ownerObject, ContextType.Editor, EventDispatcher.editorDispatcher, InitEditorUpdater)
{
name = ownerObject.GetType().Name;
cursorManager = m_CursorManager;
contextualMenuManager = s_ContextualMenuManager;
panelDebug = new PanelDebug(this);
standardShader = EditorShader;
updateMaterial += OnUpdateMaterial;
uiElementsBridge = new EditorUIElementsBridge();
UpdateScalingFromEditorWindow = true;
}
static void OnUpdateMaterial(Material mat)
{
mat?.SetFloat(s_EditorColorSpaceID, QualitySettings.activeColorSpace == ColorSpace.Linear ? 1 : 0);
}
public static void InitEditorUpdater(BaseVisualElementPanel panel, VisualTreeUpdater visualTreeUpdater)
{
var editorUpdater = new VisualTreeEditorUpdater(panel);
visualTreeUpdater.visualTreeEditorUpdater = editorUpdater;
var assetTracker = editorUpdater.GetUpdater(VisualTreeEditorUpdatePhase.AssetChange) as ILiveReloadSystem;
panel.liveReloadSystem = assetTracker;
}
internal float? GetBackingScaleFactor()
{
return GetBackingScaleFactor(ownerObject);
}
float? GetBackingScaleFactor(object obj)
{
return obj switch
{
GUIView view => view.GetBackingScaleFactor(),
View view => GetBackingScaleFactor(view.parent), //MainView and SplitView are not GUIView
EditorWindow editorWindow => GetBackingScaleFactor(editorWindow?.m_Parent),
IEditorWindowModel ewm => GetBackingScaleFactor(ewm.window),
_ => null,
} ;
}
private void CheckPanelScaling()
{
// Can be disabled for setting a manual scale for testing
if (UpdateScalingFromEditorWindow)
{
//check that the scaling is up to date
var windowScaling = GetBackingScaleFactor();
if (windowScaling == null || windowScaling.Value == -1)
{
Debug.Assert(windowScaling != null, "got -1 here!!" );
// if we have -1, we were able to get to a GuiView, but the native call returned -1 because there is no containerWindow
// if the windowScaling == null we were simply not able to get to a GuiView
// in both cases, we want to update the scaling like the old behavior.
pixelsPerPoint = GUIUtility.pixelsPerPoint;
}
else
{
Debug.Assert(pixelsPerPoint == windowScaling.Value, $"Scaling mismatch between the EditorWindow ({windowScaling.Value}) and the Editor Panel {name} ({pixelsPerPoint}). OnBackingScaleFactorChangedInternal was probably not call upon scaling change");
pixelsPerPoint = windowScaling.Value;// AKA silence the assert after the first occurence
}
}
}
public override void ValidateLayout()
{
CheckPanelScaling();
base.ValidateLayout();
}
public override void Repaint(Event e)
{
CheckPanelScaling();
base.Repaint(e);
}
public override void Render()
{
CheckPanelScaling();
base.Render();
}
}
}