1
+ #if UNITY_EDITOR
2
+
3
+ using System ;
4
+ using UnityEditor ;
5
+ using UnityEngine ;
6
+ using UnityEngine . UIElements ;
7
+
8
+ public class RosalinaPropertiesEditorWindow : EditorWindow
9
+ {
10
+ [ SerializeField ]
11
+ private VisualTreeAsset _visualTreeAsset ;
12
+ private RosalinaFileSetting _fileSettings = null ;
13
+
14
+ public VisualElement Container { get ; private set ; }
15
+
16
+ public VisualElement BasicSettingsContainer { get ; private set ; }
17
+
18
+ public Toggle EnableFile { get ; private set ; }
19
+
20
+ public EnumField GeneratorTypeSelector { get ; private set ; }
21
+
22
+ public Button GenerateBindingsButton { get ; private set ; }
23
+
24
+ public Button GenerateScriptButton { get ; private set ; }
25
+
26
+ public Button ClearBindingsButton { get ; private set ; }
27
+
28
+ private void OnEnable ( )
29
+ {
30
+ }
31
+
32
+ private void OnDestroy ( )
33
+ {
34
+ EnableFile . UnregisterValueChangedCallback ( OnEnableFileChanged ) ;
35
+ GeneratorTypeSelector . UnregisterValueChangedCallback ( OnGeneratorTypeSelectionChanged ) ;
36
+ GenerateBindingsButton . clicked -= OnGenerateBindings ;
37
+ GenerateScriptButton . clicked -= OnGenerateScripts ;
38
+ ClearBindingsButton . clicked -= OnClearBindings ;
39
+ }
40
+
41
+ private void OnSelectionChange ( )
42
+ {
43
+ bool isActive = Selection . activeObject != null && Selection . activeObject . GetType ( ) == typeof ( VisualTreeAsset ) ;
44
+
45
+ Container . SetEnabled ( isActive ) ;
46
+
47
+ if ( isActive )
48
+ {
49
+ string assetPath = AssetDatabase . GetAssetPath ( Selection . activeObject ) ;
50
+ _fileSettings = RosalinaSettings . instance . GetFileSetting ( assetPath ) ;
51
+ }
52
+ else
53
+ {
54
+ _fileSettings = null ;
55
+ }
56
+
57
+ RefreshFileSettings ( ) ;
58
+ }
59
+
60
+ private void CreateGUI ( )
61
+ {
62
+ rootVisualElement . Add ( _visualTreeAsset . Instantiate ( ) ) ;
63
+
64
+ Container = rootVisualElement . Q < VisualElement > ( "Container" ) ;
65
+ BasicSettingsContainer = rootVisualElement . Q < VisualElement > ( "BasicSettingsContainer" ) ;
66
+ EnableFile = rootVisualElement . Q < Toggle > ( "EnableFile" ) ;
67
+ GeneratorTypeSelector = rootVisualElement . Q < EnumField > ( "GeneratorTypeSelector" ) ;
68
+ GenerateBindingsButton = rootVisualElement . Q < Button > ( "GenerateBindingsButton" ) ;
69
+ GenerateScriptButton = rootVisualElement . Q < Button > ( "GenerateScriptButton" ) ;
70
+ ClearBindingsButton = rootVisualElement . Q < Button > ( "ClearBindingsButton" ) ;
71
+
72
+ OnSelectionChange ( ) ;
73
+ EnableFile . RegisterValueChangedCallback ( OnEnableFileChanged ) ;
74
+ GeneratorTypeSelector . RegisterValueChangedCallback ( OnGeneratorTypeSelectionChanged ) ;
75
+ GenerateBindingsButton . clicked += OnGenerateBindings ;
76
+ GenerateScriptButton . clicked += OnGenerateScripts ;
77
+ ClearBindingsButton . clicked += OnClearBindings ;
78
+ }
79
+
80
+ private void RefreshFileSettings ( )
81
+ {
82
+ EnableFile . SetValueWithoutNotify ( _fileSettings != null ) ;
83
+ BasicSettingsContainer . SetEnabled ( _fileSettings != null ) ;
84
+
85
+ if ( _fileSettings != null )
86
+ {
87
+ GeneratorTypeSelector . value = _fileSettings . Type ;
88
+ }
89
+ else
90
+ {
91
+ GeneratorTypeSelector . value = null ;
92
+ }
93
+ }
94
+
95
+ private void OnEnableFileChanged ( ChangeEvent < bool > @event )
96
+ {
97
+ if ( @event . newValue )
98
+ {
99
+ _fileSettings = new RosalinaFileSetting
100
+ {
101
+ Path = AssetDatabase . GetAssetPath ( Selection . activeObject ) ,
102
+ Type = RosalinaGenerationType . Document
103
+ } ;
104
+ RosalinaSettings . instance . Files . Add ( _fileSettings ) ;
105
+ }
106
+ else
107
+ {
108
+ RosalinaSettings . instance . Files . Remove ( _fileSettings ) ;
109
+ _fileSettings = null ;
110
+ }
111
+
112
+ RefreshFileSettings ( ) ;
113
+ OnSettingsChanged ( ) ;
114
+ }
115
+
116
+ private void OnGeneratorTypeSelectionChanged ( ChangeEvent < Enum > @event )
117
+ {
118
+ if ( @event . newValue != null && @event . newValue != @event . previousValue )
119
+ {
120
+ _fileSettings . Type = ( RosalinaGenerationType ) @event . newValue ;
121
+
122
+ RefreshFileSettings ( ) ;
123
+ OnSettingsChanged ( ) ;
124
+ }
125
+ }
126
+
127
+ private void OnGenerateBindings ( )
128
+ {
129
+ string assetPath = AssetDatabase . GetAssetPath ( Selection . activeObject ) ;
130
+ var document = new UIDocumentAsset ( assetPath ) ;
131
+
132
+ document . GenerateBindings ( ) ;
133
+ AssetDatabase . Refresh ( ) ;
134
+ }
135
+
136
+ private void OnGenerateScripts ( )
137
+ {
138
+ string assetPath = AssetDatabase . GetAssetPath ( Selection . activeObject ) ;
139
+ var document = new UIDocumentAsset ( assetPath ) ;
140
+
141
+ bool bindingsGenerated = RosalinaScriptGeneratorUtilities . TryGenerateBindings ( document ) ;
142
+ bool scriptGenerated = RosalinaScriptGeneratorUtilities . TryGenerateScript ( document ) ;
143
+
144
+ if ( bindingsGenerated || scriptGenerated )
145
+ {
146
+ AssetDatabase . Refresh ( ) ;
147
+ }
148
+ }
149
+
150
+ private void OnClearBindings ( )
151
+ {
152
+ string assetPath = AssetDatabase . GetAssetPath ( Selection . activeObject ) ;
153
+ var document = new UIDocumentAsset ( assetPath ) ;
154
+
155
+ document . ClearBindings ( ) ;
156
+ AssetDatabase . Refresh ( ) ;
157
+ }
158
+
159
+ private void OnSettingsChanged ( )
160
+ {
161
+ RosalinaSettings . instance . Save ( ) ;
162
+ }
163
+
164
+ [ MenuItem ( "Assets/Rosalina/Properties..." , validate = true ) ]
165
+ public static bool ShowWindowValidation ( )
166
+ {
167
+ return RosalinaSettings . instance . IsEnabled && Selection . activeObject != null && Selection . activeObject . GetType ( ) == typeof ( VisualTreeAsset ) ;
168
+ }
169
+
170
+ [ MenuItem ( "Assets/Rosalina/Properties..." , priority = 1200 ) ]
171
+ public static void ShowWindow ( )
172
+ {
173
+ if ( HasOpenInstances < RosalinaPropertiesEditorWindow > ( ) )
174
+ {
175
+ FocusWindowIfItsOpen < RosalinaPropertiesEditorWindow > ( ) ;
176
+ return ;
177
+ }
178
+
179
+ Type inspectorWindowType = typeof ( Editor ) . Assembly . GetType ( "UnityEditor.InspectorWindow" ) ;
180
+ EditorWindow window = CreateWindow < RosalinaPropertiesEditorWindow > ( inspectorWindowType ) ;
181
+
182
+ window . titleContent = new GUIContent ( "Rosalina" , EditorGUIUtility . FindTexture ( "SettingsIcon" ) ) ;
183
+ }
184
+ }
185
+
186
+ #endif
0 commit comments