Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 0a0051d

Browse files
Integrating LockView selection with Unity's Global selection
1 parent e803f24 commit 0a0051d

File tree

1 file changed

+83
-37
lines changed
  • src/UnityExtension/Assets/Editor/GitHub.Unity/UI

1 file changed

+83
-37
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/LocksView.cs

Lines changed: 83 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
using GitHub.Logging;
21
using System;
32
using System.Collections.Generic;
43
using System.Linq;
5-
using System.Text.RegularExpressions;
64
using UnityEditor;
75
using UnityEngine;
86

97
namespace GitHub.Unity
108
{
9+
[Serializable]
10+
public class GitLockEntryDictionary : SerializableDictionary<string, GitLockEntry> { }
11+
1112
[Serializable]
1213
public class GitLockEntry
1314
{
@@ -42,21 +43,35 @@ class LocksControl
4243
{
4344
[SerializeField] private Vector2 scroll;
4445
[SerializeField] private List<GitLockEntry> gitLockEntries = new List<GitLockEntry>();
45-
[SerializeField] private int selectedIndex = -1;
46+
[SerializeField] public GitLockEntryDictionary assets = new GitLockEntryDictionary();
4647

4748
[NonSerialized] private Action<GitLock> rightClickNextRender;
4849
[NonSerialized] private GitLockEntry rightClickNextRenderEntry;
50+
[NonSerialized] private GitLockEntry selectedEntry;
4951
[NonSerialized] private int controlId;
52+
[NonSerialized] private UnityEngine.Object lastActivatedObject;
5053

51-
public int SelectedIndex
54+
public GitLockEntry SelectedEntry
5255
{
53-
get { return selectedIndex; }
54-
set { selectedIndex = value; }
55-
}
56+
get
57+
{
58+
return selectedEntry;
59+
}
60+
set
61+
{
62+
selectedEntry = value;
5663

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+
}
6075
}
6176

6277
public bool Render(Rect containingRect, Action<GitLock> singleClick = null,
@@ -91,7 +106,7 @@ public bool Render(Rect containingRect, Action<GitLock> singleClick = null,
91106
var shouldRenderEntry = !(entryRect.y > endDisplay || entryRect.yMax < startDisplay);
92107
if (shouldRenderEntry && Event.current.type == EventType.Repaint)
93108
{
94-
RenderEntry(entryRect, entry, index);
109+
RenderEntry(entryRect, entry);
95110
}
96111

97112
var entryRequiresRepaint =
@@ -108,9 +123,9 @@ public bool Render(Rect containingRect, Action<GitLock> singleClick = null,
108123
return requiresRepaint;
109124
}
110125

111-
private void RenderEntry(Rect entryRect, GitLockEntry entry, int index)
126+
private void RenderEntry(Rect entryRect, GitLockEntry entry)
112127
{
113-
var isSelected = index == SelectedIndex;
128+
var isSelected = entry == SelectedEntry;
114129

115130
var iconWidth = 48;
116131
var iconHeight = 48;
@@ -141,7 +156,7 @@ private bool HandleInput(Rect rect, GitLockEntry entry, int index, Action<GitLoc
141156
Event.current.Use();
142157
GUIUtility.keyboardControl = controlId;
143158

144-
SelectedIndex = index;
159+
SelectedEntry = entry;
145160
requiresRepaint = true;
146161
var clickCount = Event.current.clickCount;
147162
var mouseButton = Event.current.button;
@@ -162,7 +177,7 @@ private bool HandleInput(Rect rect, GitLockEntry entry, int index, Action<GitLoc
162177
}
163178

164179
// 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)
166181
{
167182
var directionY = Event.current.keyCode == KeyCode.UpArrow ? -1 : Event.current.keyCode == KeyCode.DownArrow ? 1 : 0;
168183
if (directionY != 0)
@@ -171,11 +186,11 @@ private bool HandleInput(Rect rect, GitLockEntry entry, int index, Action<GitLoc
171186

172187
if (directionY > 0)
173188
{
174-
requiresRepaint = SelectNext(index) != index;
189+
requiresRepaint = SelectNext(index);
175190
}
176191
else
177192
{
178-
requiresRepaint = SelectPrevious(index) != index;
193+
requiresRepaint = SelectPrevious(index);
179194
}
180195
}
181196
}
@@ -185,7 +200,10 @@ private bool HandleInput(Rect rect, GitLockEntry entry, int index, Action<GitLoc
185200

186201
public void Load(List<GitLock> locks)
187202
{
188-
var selectedCommitId = SelectedGitLockEntry.GitLock.ID;
203+
var selectedLockId = !(SelectedEntry.GitLock == GitLock.Default)
204+
? (int?) SelectedEntry.GitLock.ID
205+
: null;
206+
189207
var scrollValue = scroll.y;
190208

191209
var previousCount = gitLockEntries.Count;
@@ -201,18 +219,18 @@ public void Load(List<GitLock> locks)
201219
var selectionPresent = false;
202220
for (var index = 0; index < gitLockEntries.Count; index++)
203221
{
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)
206224
{
207-
selectedIndex = index;
225+
selectedEntry = gitLockEntry;
208226
selectionPresent = true;
209227
break;
210228
}
211229
}
212230

213231
if (!selectionPresent)
214232
{
215-
selectedIndex = -1;
233+
selectedEntry = GitLockEntry.Default;
216234
}
217235

218236
if (scrollIndex > gitLockEntries.Count)
@@ -269,42 +287,62 @@ protected Texture GetNodeIcon(GitLock node)
269287
return nodeIcon;
270288
}
271289

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)
273296
{
274297
index++;
275298

276299
if (index < gitLockEntries.Count)
277300
{
278-
SelectedIndex = index;
279-
}
280-
else
281-
{
282-
index = -1;
301+
SelectedEntry = gitLockEntries[index];
302+
return true;
283303
}
284304

285-
return index;
305+
return false;
286306
}
287307

288-
private int SelectPrevious(int index)
308+
private bool SelectPrevious(int index)
289309
{
290310
index--;
291311

292312
if (index >= 0)
293313
{
294-
SelectedIndex = index;
295-
}
296-
else
297-
{
298-
SelectedIndex = -1;
314+
SelectedEntry = gitLockEntries[index];
315+
return true;
299316
}
300317

301-
return index;
318+
return false;
302319
}
303320

304321
public void ScrollTo(int index, float offset = 0f)
305322
{
306323
scroll.Set(scroll.x, Styles.LocksEntryHeight * index + offset);
307324
}
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+
}
308346
}
309347

310348
[Serializable]
@@ -460,10 +498,18 @@ private void BuildLocksControl()
460498

461499
locksControl.Load(lockedFiles);
462500
if (!selectedEntry.Equals(GitLock.Default)
463-
&& selectedEntry.ID != locksControl.SelectedGitLockEntry.GitLock.ID)
501+
&& selectedEntry.ID != locksControl.SelectedEntry.GitLock.ID)
464502
{
465503
selectedEntry = GitLock.Default;
466504
}
467505
}
506+
public override void OnSelectionChange()
507+
{
508+
base.OnSelectionChange();
509+
if (locksControl.OnSelectionChange())
510+
{
511+
Redraw();
512+
}
513+
}
468514
}
469515
}

0 commit comments

Comments
 (0)