1
- using GitHub . Logging ;
2
1
using System ;
3
2
using System . Collections . Generic ;
4
3
using System . Linq ;
5
- using System . Text . RegularExpressions ;
6
4
using UnityEditor ;
7
5
using UnityEngine ;
8
6
9
7
namespace GitHub . Unity
10
8
{
9
+ [ Serializable ]
10
+ public class GitLockEntryDictionary : SerializableDictionary < string , GitLockEntry > { }
11
+
11
12
[ Serializable ]
12
13
public class GitLockEntry
13
14
{
@@ -42,21 +43,35 @@ class LocksControl
42
43
{
43
44
[ SerializeField ] private Vector2 scroll ;
44
45
[ SerializeField ] private List < GitLockEntry > gitLockEntries = new List < GitLockEntry > ( ) ;
45
- [ SerializeField ] private int selectedIndex = - 1 ;
46
+ [ SerializeField ] public GitLockEntryDictionary assets = new GitLockEntryDictionary ( ) ;
46
47
47
48
[ NonSerialized ] private Action < GitLock > rightClickNextRender ;
48
49
[ NonSerialized ] private GitLockEntry rightClickNextRenderEntry ;
50
+ [ NonSerialized ] private GitLockEntry selectedEntry ;
49
51
[ NonSerialized ] private int controlId ;
52
+ [ NonSerialized ] private UnityEngine . Object lastActivatedObject ;
50
53
51
- public int SelectedIndex
54
+ public GitLockEntry SelectedEntry
52
55
{
53
- get { return selectedIndex ; }
54
- set { selectedIndex = value ; }
55
- }
56
+ get
57
+ {
58
+ return selectedEntry ;
59
+ }
60
+ set
61
+ {
62
+ selectedEntry = value ;
56
63
57
- public GitLockEntry SelectedGitLockEntry
58
- {
59
- get { return SelectedIndex < 0 ? GitLockEntry . Default : gitLockEntries [ SelectedIndex ] ; }
64
+ var activeObject = selectedEntry != null
65
+ ? AssetDatabase . LoadMainAssetAtPath ( selectedEntry . GitLock . Path )
66
+ : null ;
67
+
68
+ lastActivatedObject = activeObject ;
69
+
70
+ if ( LocksControlHasFocus )
71
+ {
72
+ Selection . activeObject = activeObject ;
73
+ }
74
+ }
60
75
}
61
76
62
77
public bool Render ( Rect containingRect , Action < GitLock > singleClick = null ,
@@ -91,7 +106,7 @@ public bool Render(Rect containingRect, Action<GitLock> singleClick = null,
91
106
var shouldRenderEntry = ! ( entryRect . y > endDisplay || entryRect . yMax < startDisplay ) ;
92
107
if ( shouldRenderEntry && Event . current . type == EventType . Repaint )
93
108
{
94
- RenderEntry ( entryRect , entry , index ) ;
109
+ RenderEntry ( entryRect , entry ) ;
95
110
}
96
111
97
112
var entryRequiresRepaint =
@@ -108,9 +123,9 @@ public bool Render(Rect containingRect, Action<GitLock> singleClick = null,
108
123
return requiresRepaint ;
109
124
}
110
125
111
- private void RenderEntry ( Rect entryRect , GitLockEntry entry , int index )
126
+ private void RenderEntry ( Rect entryRect , GitLockEntry entry )
112
127
{
113
- var isSelected = index == SelectedIndex ;
128
+ var isSelected = entry == SelectedEntry ;
114
129
115
130
var iconWidth = 48 ;
116
131
var iconHeight = 48 ;
@@ -141,7 +156,7 @@ private bool HandleInput(Rect rect, GitLockEntry entry, int index, Action<GitLoc
141
156
Event . current . Use ( ) ;
142
157
GUIUtility . keyboardControl = controlId ;
143
158
144
- SelectedIndex = index ;
159
+ SelectedEntry = entry ;
145
160
requiresRepaint = true ;
146
161
var clickCount = Event . current . clickCount ;
147
162
var mouseButton = Event . current . button ;
@@ -162,7 +177,7 @@ private bool HandleInput(Rect rect, GitLockEntry entry, int index, Action<GitLoc
162
177
}
163
178
164
179
// Keyboard navigation if this child is the current selection
165
- if ( GUIUtility . keyboardControl == controlId && index == SelectedIndex && Event . current . type == EventType . KeyDown )
180
+ if ( GUIUtility . keyboardControl == controlId && entry == SelectedEntry && Event . current . type == EventType . KeyDown )
166
181
{
167
182
var directionY = Event . current . keyCode == KeyCode . UpArrow ? - 1 : Event . current . keyCode == KeyCode . DownArrow ? 1 : 0 ;
168
183
if ( directionY != 0 )
@@ -171,11 +186,11 @@ private bool HandleInput(Rect rect, GitLockEntry entry, int index, Action<GitLoc
171
186
172
187
if ( directionY > 0 )
173
188
{
174
- requiresRepaint = SelectNext ( index ) != index ;
189
+ requiresRepaint = SelectNext ( index ) ;
175
190
}
176
191
else
177
192
{
178
- requiresRepaint = SelectPrevious ( index ) != index ;
193
+ requiresRepaint = SelectPrevious ( index ) ;
179
194
}
180
195
}
181
196
}
@@ -185,7 +200,10 @@ private bool HandleInput(Rect rect, GitLockEntry entry, int index, Action<GitLoc
185
200
186
201
public void Load ( List < GitLock > locks )
187
202
{
188
- var selectedCommitId = SelectedGitLockEntry . GitLock . ID ;
203
+ var selectedLockId = ! ( SelectedEntry . GitLock == GitLock . Default )
204
+ ? ( int ? ) SelectedEntry . GitLock . ID
205
+ : null ;
206
+
189
207
var scrollValue = scroll . y ;
190
208
191
209
var previousCount = gitLockEntries . Count ;
@@ -201,18 +219,18 @@ public void Load(List<GitLock> locks)
201
219
var selectionPresent = false ;
202
220
for ( var index = 0 ; index < gitLockEntries . Count ; index ++ )
203
221
{
204
- var gitLogEntry = gitLockEntries [ index ] ;
205
- if ( gitLogEntry . GitLock . ID . Equals ( selectedCommitId ) )
222
+ var gitLockEntry = gitLockEntries [ index ] ;
223
+ if ( selectedLockId . HasValue && selectedLockId . Value == gitLockEntry . GitLock . ID )
206
224
{
207
- selectedIndex = index ;
225
+ selectedEntry = gitLockEntry ;
208
226
selectionPresent = true ;
209
227
break ;
210
228
}
211
229
}
212
230
213
231
if ( ! selectionPresent )
214
232
{
215
- selectedIndex = - 1 ;
233
+ selectedEntry = GitLockEntry . Default ;
216
234
}
217
235
218
236
if ( scrollIndex > gitLockEntries . Count )
@@ -269,42 +287,62 @@ protected Texture GetNodeIcon(GitLock node)
269
287
return nodeIcon ;
270
288
}
271
289
272
- private int SelectNext ( int index )
290
+ protected bool LocksControlHasFocus
291
+ {
292
+ get { return GUIUtility . keyboardControl == controlId ; }
293
+ }
294
+
295
+ private bool SelectNext ( int index )
273
296
{
274
297
index ++ ;
275
298
276
299
if ( index < gitLockEntries . Count )
277
300
{
278
- SelectedIndex = index ;
279
- }
280
- else
281
- {
282
- index = - 1 ;
301
+ SelectedEntry = gitLockEntries [ index ] ;
302
+ return true ;
283
303
}
284
304
285
- return index ;
305
+ return false ;
286
306
}
287
307
288
- private int SelectPrevious ( int index )
308
+ private bool SelectPrevious ( int index )
289
309
{
290
310
index -- ;
291
311
292
312
if ( index >= 0 )
293
313
{
294
- SelectedIndex = index ;
295
- }
296
- else
297
- {
298
- SelectedIndex = - 1 ;
314
+ SelectedEntry = gitLockEntries [ index ] ;
315
+ return true ;
299
316
}
300
317
301
- return index ;
318
+ return false ;
302
319
}
303
320
304
321
public void ScrollTo ( int index , float offset = 0f )
305
322
{
306
323
scroll . Set ( scroll . x , Styles . LocksEntryHeight * index + offset ) ;
307
324
}
325
+
326
+ public bool OnSelectionChange ( )
327
+ {
328
+ if ( ! LocksControlHasFocus )
329
+ {
330
+ GitLockEntry gitLockEntry = GitLockEntry . Default ;
331
+
332
+ if ( Selection . activeObject != lastActivatedObject )
333
+ {
334
+ var activeAssetPath = AssetDatabase . GetAssetPath ( Selection . activeObject ) ;
335
+ var activeAssetGuid = AssetDatabase . AssetPathToGUID ( activeAssetPath ) ;
336
+
337
+ assets . TryGetValue ( activeAssetGuid , out gitLockEntry ) ;
338
+ }
339
+
340
+ SelectedEntry = gitLockEntry ;
341
+ return true ;
342
+ }
343
+
344
+ return false ;
345
+ }
308
346
}
309
347
310
348
[ Serializable ]
@@ -460,10 +498,18 @@ private void BuildLocksControl()
460
498
461
499
locksControl . Load ( lockedFiles ) ;
462
500
if ( ! selectedEntry . Equals ( GitLock . Default )
463
- && selectedEntry . ID != locksControl . SelectedGitLockEntry . GitLock . ID )
501
+ && selectedEntry . ID != locksControl . SelectedEntry . GitLock . ID )
464
502
{
465
503
selectedEntry = GitLock . Default ;
466
504
}
467
505
}
506
+ public override void OnSelectionChange ( )
507
+ {
508
+ base . OnSelectionChange ( ) ;
509
+ if ( locksControl . OnSelectionChange ( ) )
510
+ {
511
+ Redraw ( ) ;
512
+ }
513
+ }
468
514
}
469
515
}
0 commit comments