-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEditorIconData.cs
270 lines (222 loc) · 6.29 KB
/
EditorIconData.cs
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace cmdwtf.UnityTools.Editor
{
internal enum IconProSkinState
{
Free,
Pro,
Both
}
internal class EditorIconData
{
private const string ProIconPrefix = "d_";
public string Name
{
get => _names.FirstOrDefault() ?? string.Empty;
private set
{
if (_names.Any())
{
_names[0] = value;
return;
}
_names.Add(value);
}
}
public GUIContent Content { get; private set; }
public string Path { get; private set; }
public IReadOnlyList<string> Names => _names.AsReadOnly();
public string AllNames => GetAllNames();
public bool IsValid => Content != null;
public IconProSkinState ProSkinState
{
get
{
bool hasPro = _names.Any(IsProName);
if (Count == 1)
{
return hasPro
? IconProSkinState.Pro
: IconProSkinState.Free;
}
return IconProSkinState.Both;
}
}
public float Width => Content?.image.width ?? 0;
public float Height => Content?.image.height ?? 0;
public string Dimensions => $"{Width}x{Height}";
public Texture2D Image => Content?.image as Texture2D;
public string SortName => GetSortName(Name);
/// <summary>
/// The number of names this icon represents.
/// </summary>
public int Count => _names.Count;
private readonly List<string> _names = new();
public bool IsLarge => Size == IconSize.Large;
public bool IsMedium => Size == IconSize.Medium;
public bool IsSmall => Size == IconSize.Small;
public IconSize Size
{
get
{
if (!IsValid)
{
throw new InvalidOperationException($"{nameof(Size)} should not be accessed on invalid icon data.");
}
if (Width < 32 && Height < 32)
{
return IconSize.Small;
}
if (Width < 48 && Height < 48)
{
return IconSize.Medium;
}
return IconSize.Large;
}
}
public static EditorIconData Empty { get; } = new();
public EditorIconData()
{
Name = string.Empty;
Content = null;
Path = string.Empty;
}
public static EditorIconData FromIconName(string iconName)
{
GUIContent content = GetIcon(iconName);
if (content == null || content.image == null)
{
return Empty;
}
return new EditorIconData(iconName, content);
}
public EditorIconData(Texture2D t2d)
{
if (t2d == null)
{
throw new ArgumentNullException(nameof(t2d));
}
Name = t2d.name;
Content = GetIcon(t2d.name);
Path = AssetDatabase.GetAssetPath(t2d);
if (Content != null)
{
Content.tooltip = Name;
}
}
private EditorIconData(string iconName, GUIContent iconContent)
{
Name = iconName ?? throw new ArgumentNullException(nameof(iconName));
Content = iconContent ?? throw new ArgumentNullException(nameof(iconContent));
Path = AssetDatabase.GetAssetPath(iconContent.image);
Content.tooltip = Name;
}
public bool MatchesName(string nameToTest)
{
string testSortName = GetSortName(nameToTest);
return _names.Any(n => GetSortName(n) == testSortName);
}
public void AddAlternateName(string alternateName)
{
_names.Add(alternateName);
}
public void AddSecondaryTextureName(string name) => _names.Add(name);
public bool MatchesSearchQuery(string query)
{
string lowerQuery = query.ToLowerInvariant();
return string.IsNullOrWhiteSpace(query) ||
SortName.Contains(lowerQuery) ||
(_names?.Any(alt => alt.ToLowerInvariant().Contains(lowerQuery)) ?? false);
}
public string GetAllNames() => string.Join(", ", Names);
public static bool IsProName(string name) => name.StartsWith(ProIconPrefix);
public bool SaveToFile(int selectedNameIndex)
{
string name = _names[selectedNameIndex];
var source = Content.image as Texture2D;
if (source != null)
{
string path = EditorUtility.SaveFilePanel(
"Save Icon", string.Empty, name, "png");
if (path != null)
{
try
{
#if UNITY_2019_1_OR_NEWER
var outTex = new Texture2D(
source.width, source.height,
source.format, source.mipmapCount, true);
#else
Texture2D outTex = new Texture2D(
tex.width, tex.height,
tex.format, true);
#endif
Graphics.CopyTexture(source, outTex);
System.IO.File.WriteAllBytes(path, outTex.EncodeToPNG());
return true;
}
catch (System.Exception e)
{
Debug.LogError($"Failed to save icon: {e.Message}");
}
}
}
else
{
Debug.LogError($"Failed to save icon: no texture found.");
}
return false;
}
private static GUIContent GetIcon(string iconName)
{
GUIContent valid = null;
Debug.unityLogger.logEnabled = false;
if (!string.IsNullOrEmpty(iconName))
{
valid = EditorGUIUtility.IconContent(iconName);
}
Debug.unityLogger.logEnabled = true;
return valid?.image == null ? null : valid;
}
private static string GetSortName(string name)
=> (name.StartsWith(ProIconPrefix)
? name.Substring(2)
: name).ToLowerInvariant();
// private static string Listify(string[] names, bool newLines)
// {
// if (!names.Any())
// {
// return "<empty set>";
// }
//
// return "\"" + string.Join((newLines ? "\", \"\r\n" : "\", \""), names) + "\"";
// }
//
// private static void WriteToFile(StreamWriter w, string header, IEnumerable<string> values, bool newlines = false)
// {
// string[] enumerable = values as string[] ?? values.ToArray();
// int count = enumerable.Count();
// string value = Listify(enumerable, newlines);
// w.WriteLine($"=== {header} ({count}) ===");
// w.WriteLine();
// w.WriteLine(value);
// w.WriteLine("---");
// }
//
// public static void FileIt(string header, IEnumerable<string> things)
// {
// string path = System.IO.Path.GetTempFileName() + $".{header}.txt";
// using StreamWriter file = new(path);
// WriteToFile(file, header, things);
// ProcessStartInfo psi = new()
// {
// Arguments = $"{path}", FileName = @"C:\Program Files\Notepad++\notepad++.exe",
// };
// Process.Start(psi);
// }
}
}