@@ -10,46 +10,53 @@ namespace Code.LevelEditor.Editor
1010{
1111 public class BlockLibraryWindow : OdinEditorWindow
1212 {
13+ private enum SortMode { ByID , ByPrefabName , ByIconName }
14+
1315 [ MenuItem ( "Tools/Block Library 🧱" ) ]
1416 public static void ShowWindow ( )
1517 {
1618 var window = GetWindow < BlockLibraryWindow > ( false , "Block Library" , true ) ;
1719 window . minSize = new Vector2 ( 400 , 500 ) ;
1820 }
1921
20- [ BoxGroup ( "📦 Block Library" , centerLabel : true ) ]
22+ [ BoxGroup ( "📦 Block Library" , centerLabel : true ) ]
2123 [ ReadOnly , ShowInInspector , HideLabel ]
2224 private BlockLibrary _blockLibrary ;
2325
24- [ BoxGroup ( "🧱 Create Block" , centerLabel : true ) ]
25- [ LabelText ( "Block ID" ) ] [ ShowInInspector ]
26+ [ BoxGroup ( "🧱 Create Block" , centerLabel : true ) ]
27+ [ ShowInInspector , LabelText ( "Block ID" ) ]
2628 private string _newBlockId = "" ;
27- [ BoxGroup ( "🧱 Create Block" ) ]
28- [ LabelText ( "Icon" ) ] [ ShowInInspector ]
29+
30+ [ BoxGroup ( "🧱 Create Block" ) ]
31+ [ ShowInInspector , LabelText ( "Icon" ) ]
2932 private Sprite _newBlockSprite ;
30- [ BoxGroup ( "🧱 Create Block" ) ]
31- [ LabelText ( "Prefab" ) ] [ ShowInInspector ]
33+
34+ [ BoxGroup ( "🧱 Create Block" ) ]
35+ [ ShowInInspector , LabelText ( "Prefab" ) ]
3236 private GameObject _newBlockPrefab ;
3337
34- [ BoxGroup ( "🔍 Search & Sort" , centerLabel : true ) ]
35- [ LabelText ( "Filter" ) ] [ ShowInInspector ]
38+ [ BoxGroup ( "🔍 Search & Sort" , centerLabel : true ) ]
39+ [ ShowInInspector , LabelText ( "Filter" ) ]
3640 private string _searchFilter = "" ;
41+
3742 [ BoxGroup ( "🔍 Search & Sort" ) ]
3843 [ HorizontalGroup ( "🔍 Search & Sort/SortRow" ) ]
39- [ EnumToggleButtons , HideLabel ]
40- [ PropertyOrder ( 0 ) ]
44+ [ EnumToggleButtons , HideLabel , PropertyOrder ( 0 ) ]
4145 [ SerializeField ]
4246 private SortMode _sortMode = SortMode . ByID ;
4347
48+ [ HorizontalGroup ( "🔍 Search & Sort/SortRow" , width : 25 ) ]
49+ [ Button ( "@_sortAscending ? \" ▲\" : \" ▼\" " , ButtonSizes . Medium ) ]
50+ [ PropertyOrder ( 1 ) ]
51+ private void ToggleSortDirection ( ) => _sortAscending = ! _sortAscending ;
52+
4453 private bool _sortAscending = true ;
4554 private Vector2 _scroll ;
4655
47- private enum SortMode
48- {
49- ByID ,
50- ByPrefabName ,
51- ByIconName
52- }
56+ [ BoxGroup ( "🧾 Selected Block" , centerLabel : true ) ]
57+ [ ShowInInspector , InlineEditor ( InlineEditorObjectFieldModes . Hidden ) ]
58+ [ PropertyOrder ( 100 ) ]
59+ private BlockDataEditor _selectedBlock ;
5360
5461 protected override void OnEnable ( )
5562 {
@@ -68,7 +75,6 @@ protected override void DrawEditor(int index)
6875 {
6976 CreateLibrary ( ) ;
7077 }
71-
7278 return ;
7379 }
7480
@@ -78,19 +84,21 @@ protected override void DrawEditor(int index)
7884 SirenixEditorGUI . BeginBoxHeader ( ) ;
7985 SirenixEditorGUI . Title ( "📦 Existing Blocks" , null , TextAlignment . Center , true ) ;
8086 SirenixEditorGUI . EndBoxHeader ( ) ;
87+
8188 _scroll = EditorGUILayout . BeginScrollView ( _scroll ) ;
8289 DrawExistingBlocks ( ) ;
8390 GUILayout . FlexibleSpace ( ) ;
8491 EditorGUILayout . EndScrollView ( ) ;
92+
8593 SirenixEditorGUI . EndBox ( ) ;
8694 }
8795
8896 private void LoadOrCreateLibrary ( )
8997 {
90- string [ ] guids = AssetDatabase . FindAssets ( "t:BlockLibrary" ) ;
98+ var guids = AssetDatabase . FindAssets ( "t:BlockLibrary" ) ;
9199 if ( guids . Length > 0 )
92100 {
93- string path = AssetDatabase . GUIDToAssetPath ( guids [ 0 ] ) ;
101+ var path = AssetDatabase . GUIDToAssetPath ( guids [ 0 ] ) ;
94102 _blockLibrary = AssetDatabase . LoadAssetAtPath < BlockLibrary > ( path ) ;
95103 }
96104 else
@@ -102,26 +110,19 @@ private void LoadOrCreateLibrary()
102110 private void CreateLibrary ( )
103111 {
104112 var asset = CreateInstance < BlockLibrary > ( ) ;
105- string folderPath = "Assets/Resources/StaticData/BlocksData" ;
113+ const string folderPath = "Assets/Resources/StaticData/BlocksData" ;
106114 if ( ! AssetDatabase . IsValidFolder ( folderPath ) )
107115 AssetDatabase . CreateFolder ( "Assets/Resources/StaticData" , "BlocksData" ) ;
108116
109- string path = $ "{ folderPath } /BlockLibrary.asset";
117+ var path = $ "{ folderPath } /BlockLibrary.asset";
110118 AssetDatabase . CreateAsset ( asset , path ) ;
111119 AssetDatabase . SaveAssets ( ) ;
112120 AssetDatabase . Refresh ( ) ;
121+
113122 _blockLibrary = asset ;
114123 Debug . Log ( "✅ BlockLibrary created at " + path ) ;
115124 }
116125
117- [ HorizontalGroup ( "🔍 Search & Sort/SortRow" , width : 25 ) ]
118- [ Button ( "@_sortAscending ? \" ▲\" : \" ▼\" " , ButtonSizes . Medium ) ]
119- [ PropertyOrder ( 1 ) ]
120- private void ToggleSortDirection ( )
121- {
122- _sortAscending = ! _sortAscending ;
123- }
124-
125126 private void DrawExistingBlocks ( )
126127 {
127128 if ( _blockLibrary . AllBlocks == null || _blockLibrary . AllBlocks . Count == 0 )
@@ -137,18 +138,12 @@ private void DrawExistingBlocks()
137138 blocks = blocks . FindAll ( b => b != null && b . ID . ToLower ( ) . Contains ( _searchFilter . ToLower ( ) ) ) ;
138139 }
139140
140- blocks . Sort ( ( a , b ) =>
141+ blocks . Sort ( ( a , b ) => _sortMode switch
141142 {
142- switch ( _sortMode )
143- {
144- case SortMode . ByID : return string . Compare ( a . ID , b . ID , StringComparison . OrdinalIgnoreCase ) ;
145- case SortMode . ByIconName :
146- return string . Compare ( a . Icon ? . name ?? "" , b . Icon ? . name ?? "" ,
147- StringComparison . OrdinalIgnoreCase ) ;
148- case SortMode . ByPrefabName :
149- return SafeCompare ( a . Prefab , b . Prefab ) ;
150- default : return 0 ;
151- }
143+ SortMode . ByID => string . Compare ( a . ID , b . ID , StringComparison . OrdinalIgnoreCase ) ,
144+ SortMode . ByIconName => string . Compare ( a . Icon ? . name ?? "" , b . Icon ? . name ?? "" , StringComparison . OrdinalIgnoreCase ) ,
145+ SortMode . ByPrefabName => SafeCompare ( a . Prefab , b . Prefab ) ,
146+ _ => 0
152147 } ) ;
153148
154149 if ( ! _sortAscending )
@@ -164,24 +159,23 @@ private void DrawExistingBlocks()
164159 SirenixEditorGUI . EndBoxHeader ( ) ;
165160
166161 GUILayout . BeginHorizontal ( ) ;
167-
168- GUILayout . Label ( block . Icon != null ? block . Icon . texture : Texture2D . grayTexture , GUILayout . Width ( 32 ) ,
169- GUILayout . Height ( 32 ) ) ;
170-
162+ GUILayout . Label ( block . Icon != null ? block . Icon . texture : Texture2D . grayTexture , GUILayout . Width ( 32 ) , GUILayout . Height ( 32 ) ) ;
171163 GUILayout . FlexibleSpace ( ) ;
172164
173165 if ( GUILayout . Button ( "Select" , GUILayout . Width ( 60 ) ) )
166+ {
167+ _selectedBlock = block ;
174168 Selection . activeObject = block ;
169+ }
175170
176171 if ( GUILayout . Button ( "X" , GUILayout . Width ( 20 ) ) )
177172 {
178- if ( EditorUtility . DisplayDialog ( "Delete Block" , $ "Are you sure you want to delete '{ block . ID } '?",
179- "Yes" , "No" ) )
173+ if ( EditorUtility . DisplayDialog ( "Delete Block" , $ "Are you sure you want to delete '{ block . ID } '?", "Yes" , "No" ) )
180174 {
181- string path = AssetDatabase . GetAssetPath ( block ) ;
175+ var path = AssetDatabase . GetAssetPath ( block ) ;
182176 _blockLibrary . AllBlocks . Remove ( block ) ;
183- EditorUtility . SetDirty ( _blockLibrary ) ;
184177 AssetDatabase . DeleteAsset ( path ) ;
178+ EditorUtility . SetDirty ( _blockLibrary ) ;
185179 AssetDatabase . SaveAssets ( ) ;
186180 AssetDatabase . Refresh ( ) ;
187181 GUIUtility . ExitGUI ( ) ;
@@ -199,17 +193,17 @@ private void DrawExistingBlocks()
199193 [ EnableIf ( "@!string.IsNullOrEmpty(_newBlockId) && _newBlockSprite != null" ) ]
200194 private void CreateNewBlock ( )
201195 {
202- var folderPath = "Assets/Resources/StaticData/BlocksData" ;
196+ const string folderPath = "Assets/Resources/StaticData/BlocksData" ;
203197 if ( ! AssetDatabase . IsValidFolder ( folderPath ) )
204198 AssetDatabase . CreateFolder ( "Assets/Resources/StaticData" , "BlocksData" ) ;
205199
206- BlockDataEditor newBlock = CreateInstance < BlockDataEditor > ( ) ;
200+ var newBlock = CreateInstance < BlockDataEditor > ( ) ;
207201 newBlock . name = _newBlockId ;
208202 newBlock . SetID ( _newBlockId ) ;
209203 newBlock . SetIcon ( _newBlockSprite ) ;
210204 newBlock . SetPrefab ( _newBlockPrefab ) ;
211205
212- string assetPath = $ "{ folderPath } /{ _newBlockId } .asset";
206+ var assetPath = $ "{ folderPath } /{ _newBlockId } .asset";
213207 AssetDatabase . CreateAsset ( newBlock , assetPath ) ;
214208 AssetDatabase . SaveAssets ( ) ;
215209 AssetDatabase . Refresh ( ) ;
@@ -223,19 +217,17 @@ private void CreateNewBlock()
223217 _newBlockSprite = null ;
224218 _newBlockPrefab = null ;
225219 }
226-
220+
227221 private int SafeCompare ( UnityEngine . Object a , UnityEngine . Object b )
228222 {
229223 try
230224 {
231- string nameA = a != null ? a . name : string . Empty ;
232- string nameB = b != null ? b . name : string . Empty ;
233- return string . Compare ( nameA , nameB , StringComparison . OrdinalIgnoreCase ) ;
225+ return string . Compare ( a ? . name ?? string . Empty , b ? . name ?? string . Empty , StringComparison . OrdinalIgnoreCase ) ;
234226 }
235227 catch
236228 {
237229 return 0 ;
238230 }
239231 }
240232 }
241- }
233+ }
0 commit comments