1111using AudioConductor . Editor . Core . Tools . CueSheetEditor . Presenters ;
1212using AudioConductor . Editor . Core . Tools . CueSheetEditor . Views ;
1313using AudioConductor . Editor . Core . Tools . Shared ;
14+ using AudioConductor . Editor . Core . Tools . Validation ;
1415using AudioConductor . Editor . Foundation . TinyRx ;
1516using UnityEditor ;
17+ using UnityEditor . UIElements ;
1618using UnityEngine ;
1719using UnityEngine . UIElements ;
1820
@@ -22,7 +24,6 @@ internal sealed class CueSheetAssetEditorWindow : EditorWindow
2224 {
2325 private const KeyCode UndoKey = KeyCode . Z ;
2426 private const KeyCode RedoKey = KeyCode . Y ;
25- private const string SelectedSettingsGuidPrefKey = "AudioConductor.SelectedSettingsGuid" ;
2627
2728 private static MethodInfo ? _addTabMethod ;
2829 private static bool _addTabMethodResolved ;
@@ -33,7 +34,9 @@ internal sealed class CueSheetAssetEditorWindow : EditorWindow
3334 private readonly CompositeDisposable _disposable = new ( ) ;
3435
3536 private CueSheetEditorPresenter _cueSheetEditorPresenter = null ! ;
37+ private string ? _pendingFocusCueEditorId ;
3638 private DropdownField ? _settingsDropdown ;
39+ private ToolbarButton ? _validateButton ;
3740
3841 private void OnEnable ( )
3942 {
@@ -49,13 +52,13 @@ private void OnGUI()
4952 {
5053 var e = Event . current ;
5154
52- if ( GetEventAction ( e ) && e . type == EventType . KeyDown && e . keyCode == UndoKey )
55+ if ( EventExtensions . GetEventAction ( e ) && e . type == EventType . KeyDown && e . keyCode == UndoKey )
5356 {
5457 _target . Undo ( ) ;
5558 e . Use ( ) ;
5659 }
5760
58- if ( GetEventAction ( e ) && e . type == EventType . KeyDown && e . keyCode == RedoKey )
61+ if ( EventExtensions . GetEventAction ( e ) && e . type == EventType . KeyDown && e . keyCode == RedoKey )
5962 {
6063 _target . Redo ( ) ;
6164 e . Use ( ) ;
@@ -87,7 +90,17 @@ private void CreateGUI()
8790 _settingsDropdown = rootVisualElement . Q < DropdownField > ( "SettingsDropdown" ) ;
8891 _settingsDropdown . RegisterValueChangedCallback ( _ => ApplySelectedSettings ( ) ) ;
8992
93+ _validateButton = rootVisualElement . Q < ToolbarButton > ( "ValidateButton" ) ;
94+ if ( _validateButton is not null )
95+ _validateButton . clicked += OnValidateClicked ;
96+
9097 RefreshSettingsDropdown ( ) ;
98+
99+ if ( _pendingFocusCueEditorId is not null )
100+ {
101+ _cueSheetEditorPresenter . FocusCue ( _pendingFocusCueEditorId ) ;
102+ _pendingFocusCueEditorId = null ;
103+ }
91104 }
92105
93106 private void OnFocus ( )
@@ -103,19 +116,33 @@ private void OnFocus()
103116 }
104117
105118 public static void Open ( CueSheetAsset cueSheetAsset )
119+ {
120+ OpenOrCreate ( cueSheetAsset , null ) ;
121+ }
122+
123+ internal static void OpenWithFocus ( CueSheetAsset cueSheetAsset , string ? cueEditorId )
124+ {
125+ OpenOrCreate ( cueSheetAsset , cueEditorId ) ;
126+ }
127+
128+ private static void OpenOrCreate ( CueSheetAsset cueSheetAsset , string ? cueEditorId )
106129 {
107130 var openedWindows = Resources . FindObjectsOfTypeAll < CueSheetAssetEditorWindow > ( ) ;
108131 var sameCueSheetWindow =
109132 openedWindows . FirstOrDefault ( window => window . _target . CueSheetId == cueSheetAsset . cueSheet . Id ) ;
110133 if ( sameCueSheetWindow != null )
111134 {
112135 sameCueSheetWindow . Focus ( ) ;
136+ if ( cueEditorId is not null )
137+ sameCueSheetWindow . _cueSheetEditorPresenter . FocusCue ( cueEditorId ) ;
113138 return ;
114139 }
115140
116141 var window = CreateInstance < CueSheetAssetEditorWindow > ( ) ;
117142 window . _target = new CueSheetAssetEditorWindowModel ( cueSheetAsset ) ;
118143 window . minSize = new Vector2 ( 1340 , 700 ) ;
144+ if ( cueEditorId is not null )
145+ window . _pendingFocusCueEditorId = cueEditorId ;
119146
120147 var existingWindow = openedWindows . FirstOrDefault ( ) ;
121148 if ( existingWindow != null && TryAddTab ( existingWindow , window ) )
@@ -155,30 +182,40 @@ private void Cleanup()
155182 {
156183 rootVisualElement . UnregisterCallback < KeyDownEvent > ( HandleKeyDownEvent ) ;
157184
185+ if ( _validateButton is not null )
186+ _validateButton . clicked -= OnValidateClicked ;
187+
158188 _disposable . Clear ( ) ;
159189
160- _cueSheetEditorPresenter ? . Dispose ( ) ;
190+ _cueSheetEditorPresenter . Dispose ( ) ;
191+ }
192+
193+ private void OnValidateClicked ( )
194+ {
195+ CueSheetValidationWindow . Open ( new [ ] { _target . Asset } , ValidationScope . Selected ) ;
161196 }
162197
163198 private void RefreshSettingsDropdown ( )
164199 {
165200 _settingsDropdown ??= rootVisualElement ? . Q < DropdownField > ( "SettingsDropdown" ) ;
166201
167202 var allSettings = AudioConductorSettingsRepository . instance . AllSettings ;
168- if ( allSettings == null || allSettings . Length == 0 )
203+ switch ( allSettings . Length )
169204 {
170- if ( _settingsDropdown != null )
171- _settingsDropdown . style . display = DisplayStyle . None ;
172- return ;
173- }
174-
175- if ( allSettings . Length == 1 )
176- {
177- if ( _settingsDropdown != null )
178- _settingsDropdown . style . display = DisplayStyle . None ;
179- _selectedSettingsGuid =
180- AssetDatabase . AssetPathToGUID ( AssetDatabase . GetAssetPath ( allSettings [ 0 ] ) ) ;
181- return ;
205+ case 0 :
206+ {
207+ if ( _settingsDropdown != null )
208+ _settingsDropdown . style . display = DisplayStyle . None ;
209+ return ;
210+ }
211+ case 1 :
212+ {
213+ if ( _settingsDropdown != null )
214+ _settingsDropdown . style . display = DisplayStyle . None ;
215+ _selectedSettingsGuid =
216+ AssetDatabase . AssetPathToGUID ( AssetDatabase . GetAssetPath ( allSettings [ 0 ] ) ) ;
217+ return ;
218+ }
182219 }
183220
184221 var choices = allSettings . Select ( s => s != null ? s . name : "(Missing)" ) . ToList ( ) ;
@@ -187,7 +224,7 @@ private void RefreshSettingsDropdown()
187224 var index = FindSettingsIndex ( allSettings , _selectedSettingsGuid ) ;
188225 if ( index < 0 )
189226 index = FindSettingsIndex ( allSettings ,
190- EditorPrefs . GetString ( SelectedSettingsGuidPrefKey , string . Empty ) ) ;
227+ EditorPrefs . GetString ( EditorPrefsKeys . SelectedSettingsGuid , string . Empty ) ) ;
191228 if ( index < 0 )
192229 index = 0 ;
193230
@@ -217,7 +254,7 @@ private static int FindSettingsIndex(AudioConductorSettings[] allSettings, strin
217254 private void ApplySelectedSettings ( )
218255 {
219256 var allSettings = AudioConductorSettingsRepository . instance . AllSettings ;
220- if ( _settingsDropdown == null || allSettings == null )
257+ if ( _settingsDropdown == null )
221258 return ;
222259
223260 var index = _settingsDropdown . index ;
@@ -232,43 +269,25 @@ private void ApplySelectedSettings()
232269 var assetPath = AssetDatabase . GetAssetPath ( selected ) ;
233270 var guid = AssetDatabase . AssetPathToGUID ( assetPath ) ;
234271 _selectedSettingsGuid = guid ;
235- EditorPrefs . SetString ( SelectedSettingsGuidPrefKey , guid ) ;
272+ EditorPrefs . SetString ( EditorPrefsKeys . SelectedSettingsGuid , guid ) ;
236273 }
237274
238275 CategoryListRepository . instance . Refresh ( selected ) ;
239276 }
240277
241- private static bool GetEventAction ( Event e )
242- {
243- #if UNITY_EDITOR_WIN
244- return e . control ;
245- #else
246- return e . command ;
247- #endif
248- }
249-
250278 private void HandleKeyDownEvent ( KeyDownEvent e )
251279 {
252- if ( GetEventAction ( e ) && e . keyCode == UndoKey )
280+ if ( EventExtensions . GetEventAction ( e ) && e . keyCode == UndoKey )
253281 {
254282 _target . Undo ( ) ;
255283 e . StopPropagation ( ) ;
256284 }
257285
258- if ( GetEventAction ( e ) && e . keyCode == RedoKey )
286+ if ( EventExtensions . GetEventAction ( e ) && e . keyCode == RedoKey )
259287 {
260288 _target . Redo ( ) ;
261289 e . StopPropagation ( ) ;
262290 }
263291 }
264-
265- private static bool GetEventAction ( IKeyboardEvent e )
266- {
267- #if UNITY_EDITOR_WIN
268- return e . ctrlKey ;
269- #else
270- return e . commandKey ;
271- #endif
272- }
273292 }
274293}
0 commit comments