forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetsTreeViewGUI.cs
More file actions
224 lines (187 loc) · 7.9 KB
/
Copy pathAssetsTreeViewGUI.cs
File metadata and controls
224 lines (187 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEditor.IMGUI.Controls;
using UnityEditor.ProjectWindowCallback;
using UnityEngine;
using UnityEditorInternal.VersionControl;
using UnityEditor.VersionControl;
using System.Collections.Generic;
using UnityEngine.Assertions;
namespace UnityEditor
{
internal class AssetsTreeViewGUI : TreeViewGUI
{
static bool s_VCEnabled;
const float k_IconOverlayPadding = 7f;
internal delegate void OnAssetIconDrawDelegate(Rect iconRect, string guid);
internal static event OnAssetIconDrawDelegate postAssetIconDrawCallback = null;
internal delegate bool OnAssetLabelDrawDelegate(Rect drawRect, string guid);
internal static event OnAssetLabelDrawDelegate postAssetLabelDrawCallback = null;
private static IDictionary<int, string> s_GUIDCache = null;
public AssetsTreeViewGUI(TreeViewController treeView)
: base(treeView)
{
iconOverlayGUI += OnIconOverlayGUI;
labelOverlayGUI += OnLabelOverlayGUI;
k_TopRowMargin = 4f;
}
// ---------------------
// OnGUI section
override public void BeginRowGUI()
{
s_VCEnabled = Provider.isActive;
iconLeftPadding = iconRightPadding = s_VCEnabled ? k_IconOverlayPadding : 0f;
base.BeginRowGUI();
}
//-------------------
// Create asset and Rename asset section
protected CreateAssetUtility GetCreateAssetUtility()
{
return ((TreeViewStateWithAssetUtility)m_TreeView.state).createAssetUtility;
}
virtual protected bool IsCreatingNewAsset(int instanceID)
{
return GetCreateAssetUtility().IsCreatingNewAsset() && IsRenaming(instanceID);
}
override protected void ClearRenameAndNewItemState()
{
GetCreateAssetUtility().Clear();
base.ClearRenameAndNewItemState();
}
override protected void RenameEnded()
{
string name = string.IsNullOrEmpty(GetRenameOverlay().name) ? GetRenameOverlay().originalName : GetRenameOverlay().name;
int instanceID = GetRenameOverlay().userData;
bool isCreating = GetCreateAssetUtility().IsCreatingNewAsset();
bool userAccepted = GetRenameOverlay().userAcceptedRename;
if (userAccepted)
{
if (isCreating)
{
// Create a new asset
GetCreateAssetUtility().EndNewAssetCreation(name);
}
else
{
// Rename an existing asset
ObjectNames.SetNameSmartWithInstanceID(instanceID, name);
}
}
else if (isCreating)
GetCreateAssetUtility().EndNewAssetCreationCanceled(name);
}
override protected void SyncFakeItem()
{
if (!m_TreeView.data.HasFakeItem() && GetCreateAssetUtility().IsCreatingNewAsset())
{
int parentInstanceID = AssetDatabase.GetMainAssetInstanceID(GetCreateAssetUtility().folder);
m_TreeView.data.InsertFakeItem(GetCreateAssetUtility().instanceID, parentInstanceID, GetCreateAssetUtility().originalName, GetCreateAssetUtility().icon);
}
if (m_TreeView.data.HasFakeItem() && !GetCreateAssetUtility().IsCreatingNewAsset())
{
m_TreeView.data.RemoveFakeItem();
}
}
// Not part of interface because it is very specific to creating assets
virtual public void BeginCreateNewAsset(int instanceID, EndNameEditAction endAction, string pathName, Texture2D icon, string resourceFile)
{
ClearRenameAndNewItemState();
if (GetCreateAssetUtility().BeginNewAssetCreation(instanceID, endAction, pathName, icon, resourceFile))
{
SyncFakeItem();
// Start nameing the asset
bool renameStarted = GetRenameOverlay().BeginRename(GetCreateAssetUtility().originalName, instanceID, 0f);
if (!renameStarted)
Debug.LogError("Rename not started (when creating new asset)");
}
}
// Handles fetching rename icon or cached asset database icon
protected override Texture GetIconForItem(TreeViewItem item)
{
if (item == null)
return null;
Texture icon = null;
if (IsCreatingNewAsset(item.id))
icon = GetCreateAssetUtility().icon;
if (icon == null)
icon = item.icon;
if (icon == null && item.id != 0)
{
string path = AssetDatabase.GetAssetPath(item.id);
icon = AssetDatabase.GetCachedIcon(path);
}
return icon;
}
protected override void DoItemGUI(Rect rect, int row, TreeViewItem item, bool selected, bool focused, bool useBoldFont)
{
if (item is AssetsTreeViewDataSource.RootTreeItem)
{
useBoldFont = true;
}
base.DoItemGUI(rect, row, item, selected, focused, useBoldFont);
}
private void OnIconOverlayGUI(TreeViewItem item, Rect overlayRect)
{
OnIconOverlayGUI(item.id, overlayRect, false);
}
internal static void OnIconOverlayGUI(int instanceID, Rect overlayRect, bool addPadding)
{
if (addPadding)
{
overlayRect.x -= k_IconOverlayPadding;
overlayRect.width += k_IconOverlayPadding * 2;
}
if (postAssetIconDrawCallback != null && AssetDatabase.IsMainAsset(instanceID))
{
string guid = GetGUIDForInstanceID(instanceID);
postAssetIconDrawCallback(overlayRect, guid);
}
// Draw vcs icons
if (s_VCEnabled && AssetDatabase.IsMainAsset(instanceID))
{
string guid = GetGUIDForInstanceID(instanceID);
ProjectHooks.OnProjectWindowItem(guid, overlayRect);
}
}
private void OnLabelOverlayGUI(TreeViewItem item, Rect labelRect)
{
if (postAssetLabelDrawCallback != null && AssetDatabase.IsMainAsset(item.id))
{
string guid = GetGUIDForInstanceID(item.id);
postAssetLabelDrawCallback(labelRect, guid);
}
}
// Returns a previously stored GUID for the given ID,
// else retrieves it from the asset database and stores it.
private static string GetGUIDForInstanceID(int instanceID)
{
if (s_GUIDCache == null)
{
s_GUIDCache = new Dictionary<int, string>();
}
string GUID = null;
if (!s_GUIDCache.TryGetValue(instanceID, out GUID))
{
string path = AssetDatabase.GetAssetPath(instanceID);
GUID = AssetDatabase.AssetPathToGUID(path);
Assert.IsTrue(!string.IsNullOrEmpty(GUID));
s_GUIDCache.Add(instanceID, GUID);
}
return GUID;
}
}
[System.Serializable]
internal class TreeViewStateWithAssetUtility : TreeViewState
{
[SerializeField]
CreateAssetUtility m_CreateAssetUtility = new CreateAssetUtility();
internal CreateAssetUtility createAssetUtility { get { return m_CreateAssetUtility; } set { m_CreateAssetUtility = value; } }
internal override void OnAwake()
{
base.OnAwake();
// Clear state that should not survive closing/starting Unity (If TreeViewState is in EditorWindow that are serialized in a layout file)
m_CreateAssetUtility.Clear();
}
}
}