-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFileBrowser.cs
More file actions
410 lines (358 loc) · 16.1 KB
/
Copy pathFileBrowser.cs
File metadata and controls
410 lines (358 loc) · 16.1 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace CM3D2.AlwaysColorChangeEx.Plugin {
/*
* File browser for selecting files or folders at runtime.
*/
public enum FileBrowserType {
File,
Directory
}
public class FileBrowser {
// Called when the user clicks cancel or select
public delegate void FinishedCallback(string path);
// Defaults to working directory
public string CurrentDirectory {
get {
return currentDir;
}
set {
SetNewDirectory(value);
SwitchDirectoryNow();
}
}
protected string currentDir;
// Optional pattern for filtering selectable files/folders. See:
// http://msdn.microsoft.com/en-us/library/wz42302f(v=VS.90).aspx
// and
// http://msdn.microsoft.com/en-us/library/6ff71z1w(v=VS.90).aspx
public string[] SelectionPatterns {
get {
return filePatterns;
}
set {
filePatterns = value;
ReadDirectoryContents();
}
}
protected string[] filePatterns;
// Optional image for directories
public Texture2D DirectoryImage { get; set; }
// Optional image for files
public Texture2D FileImage { get; set; }
public Texture2D NoFileImage { get; set; }
public GUIStyle labelStyle = new GUIStyle("Label");
// Browser type. Defaults to File, but can be set to Folder
public FileBrowserType BrowserType {
get {
return browserType;
}
set {
browserType = value;
ReadDirectoryContents();
}
}
protected FileBrowserType browserType;
protected string newDir;
protected string[] currentDirParts;
protected string[] files;
protected GUIContent[] filesWithImages;
protected int selectedFile;
protected string selectedName = string.Empty;
protected string[] nonMatchingFiles;
protected GUIContent[] nonMatchingFilesWithImages;
protected int selectedNonMatchingDirs;
protected string[] directories;
protected GUIContent[] dirsWithImages;
protected int selectedDir;
protected string[] nonMatchingDirs;
protected GUIContent[] nonMatchingDirsWithImages;
protected bool currentDirMatches;
protected GUIStyle CentredText {
get {
return centredText ?? (centredText = new GUIStyle(GUI.skin.label) {
alignment = TextAnchor.MiddleLeft,
fixedHeight = GUI.skin.button.fixedHeight
});
}
}
protected GUIStyle centredText;
protected Rect screenRect;
protected Vector2 scrollPosition;
protected readonly FinishedCallback callback;
public readonly string name;
public int historySize = 20;
// Browsers need at least a rect, name and callback
public FileBrowser(Rect screenRect, string name, FinishedCallback callback) {
this.name = name;
this.screenRect = screenRect;
browserType = FileBrowserType.File;
this.callback = callback;
SetNewDirectory(Directory.GetCurrentDirectory());
SwitchDirectoryNow();
}
protected void SetNewDirectory(string directory) {
newDir = directory;
nextDirs.Clear();
}
protected void SwitchDirectoryNow() {
if (newDir == null || currentDir == newDir) return;
if (Directory.Exists(currentDir)) {
historyDirs.AddLast(currentDir);
if (historyDirs.Count > historySize) {
historyDirs.RemoveFirst();
}
}
currentDir = newDir;
reloadDir();
}
private void reloadDir() {
scrollPosition = Vector2.zero;
selectedDir = selectedNonMatchingDirs = selectedFile = -1;
//selectedName = string.Empty;
ReadDirectoryContents();
}
public void Prev() {
if (!historyDirs.Any()) return;
nextDirs.AddFirst(currentDir);
var dir = historyDirs.Last();
historyDirs.RemoveLast();
currentDir = dir;
reloadDir();
}
public void Next() {
if (!nextDirs.Any()) return;
historyDirs.AddLast(currentDir);
var dir = nextDirs.First();
nextDirs.RemoveFirst();
currentDir = dir;
reloadDir();
}
private readonly LinkedList<string> historyDirs = new LinkedList<string>();
private readonly LinkedList<string> nextDirs = new LinkedList<string>();
protected void ReadDirectoryContents() {
if (currentDir == "/") {
currentDirParts = new[] { string.Empty };
currentDirMatches = false;
} else {
currentDirParts = currentDir.Split(Path.DirectorySeparatorChar);
if (SelectionPatterns != null) {
currentDirMatches = false;
foreach (var pattern in SelectionPatterns) {
var dirName = Path.GetDirectoryName(currentDir);
if (dirName == null) continue;
var generated = Directory.GetDirectories(dirName, pattern);
currentDirMatches = Array.IndexOf(generated, currentDir) >= 0;
if (currentDirMatches) break;
}
} else {
currentDirMatches = false;
}
}
if (BrowserType == FileBrowserType.File || SelectionPatterns == null) {
directories = Directory.GetDirectories(currentDir);
nonMatchingDirs = new string[0];
} else {
var list = new List<string>();
foreach (var pattern in SelectionPatterns) {
var arr = Directory.GetDirectories(currentDir, pattern);
list.AddRange(arr);
}
directories = list.ToArray();
nonMatchingDirs = Directory.GetDirectories(currentDir)
.Where(subDir => Array.IndexOf(directories, subDir) < 0)
.ToArray();
for (var i = 0; i < nonMatchingDirs.Length; ++i) {
var lastSeparator = nonMatchingDirs[i].LastIndexOf(Path.DirectorySeparatorChar);
nonMatchingDirs[i] = nonMatchingDirs[i].Substring(lastSeparator + 1);
}
Array.Sort(nonMatchingDirs);
}
for (var i = 0; i < directories.Length; ++i) {
directories[i] = directories[i].Substring(directories[i].LastIndexOf(Path.DirectorySeparatorChar) + 1);
}
if (BrowserType == FileBrowserType.Directory || SelectionPatterns == null) {
files = Directory.GetFiles(currentDir);
nonMatchingFiles = new string[0];
} else {
if (SelectionPatterns != null) {
var list = new List<string>();
foreach (var pattern in SelectionPatterns) {
var fileList = Directory.GetFiles(currentDir, pattern);
if (fileList.Length > 0) {
list.AddRange(fileList);
}
}
files = list.ToArray();
} else {
files = new string[0];
}
nonMatchingFiles = Directory.GetFiles(currentDir)
.Where(filePath => Array.IndexOf(files, filePath) < 0)
.ToArray();
for (var i = 0; i < nonMatchingFiles.Length; ++i) {
nonMatchingFiles[i] = Path.GetFileName(nonMatchingFiles[i]);
}
Array.Sort(nonMatchingFiles);
}
for (var i = 0; i < files.Length; ++i) {
files[i] = Path.GetFileName(files[i]);
}
Array.Sort(files);
BuildContent();
newDir = null;
}
protected void BuildContent() {
dirsWithImages = new GUIContent[directories.Length];
for (var i = 0; i < dirsWithImages.Length; ++i) {
dirsWithImages[i] = new GUIContent(directories[i], DirectoryImage);
}
nonMatchingDirsWithImages = new GUIContent[nonMatchingDirs.Length];
for (var i = 0; i < nonMatchingDirsWithImages.Length; ++i) {
nonMatchingDirsWithImages[i] = new GUIContent(nonMatchingDirs[i], DirectoryImage);
}
filesWithImages = new GUIContent[files.Length];
for (var i = 0; i < filesWithImages.Length; ++i) {
filesWithImages[i] = new GUIContent(files[i], FileImage);
}
nonMatchingFilesWithImages = new GUIContent[nonMatchingFiles.Length];
for (var i = 0; i < nonMatchingFilesWithImages.Length; ++i) {
nonMatchingFilesWithImages[i] = new GUIContent(nonMatchingFiles[i], NoFileImage);
}
}
public void Update() {
SwitchDirectoryNow();
}
public void OnGUI() {
GUILayout.BeginArea(screenRect, name, GUI.skin.window);
try {
GUILayout.BeginHorizontal();
try {
for (var parentIdx = 0; parentIdx < currentDirParts.Length; ++parentIdx) {
if (parentIdx == currentDirParts.Length - 1) {
GUILayout.Label(currentDirParts[parentIdx], CentredText);
} else if (GUILayout.Button(currentDirParts[parentIdx])) {
var parentDirName = currentDir;
for (var i = currentDirParts.Length - 1; i > parentIdx; --i) {
parentDirName = Path.GetDirectoryName(parentDirName);
}
SetNewDirectory(parentDirName);
}
}
GUILayout.FlexibleSpace();
GUI.enabled = historyDirs.Any();
if (GUILayout.Button("<")) {
Prev();
}
GUI.enabled = nextDirs.Any();
if (GUILayout.Button(">")) {
Next();
}
} finally {
GUI.enabled = true;
GUILayout.EndHorizontal();
}
scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, true,
GUI.skin.horizontalScrollbar,
GUI.skin.verticalScrollbar, GUI.skin.box );
selectedDir = GUILayoutx.SelectionList(selectedDir, dirsWithImages, labelStyle, DirectoryClickCallback);
if (selectedDir > -1) {
selectedFile = selectedNonMatchingDirs = -1;
//selectedName = dirsWithImages[selectedDir].text;
}
selectedNonMatchingDirs = GUILayoutx.SelectionList(selectedNonMatchingDirs, nonMatchingDirsWithImages, labelStyle, NonMatchingDirectoryClickCallback);
if (selectedNonMatchingDirs > -1) {
selectedDir = selectedFile = -1;
//selectedName = string.Empty;
}
GUI.enabled = BrowserType == FileBrowserType.File;
selectedFile = GUILayoutx.SelectionList(selectedFile, filesWithImages, labelStyle, FileClickCallback);
GUI.enabled = true;
if (selectedFile > -1) {
selectedDir = selectedNonMatchingDirs = -1;
//selectedName = filesWithImages[selectedDir].text;
}
GUI.enabled = false;
GUILayoutx.SelectionList( -1, nonMatchingFilesWithImages, labelStyle );
GUI.enabled = true;
GUILayout.EndScrollView();
GUILayout.BeginHorizontal();
GUILayout.Label(selectedName);
GUILayout.FlexibleSpace();
if (GUILayout.Button("キャンセル", GUILayout.Width(120))) {
callback(null);
}
if (BrowserType == FileBrowserType.File) {
GUI.enabled = selectedFile > -1;
selectedName = GUI.enabled ? filesWithImages[selectedFile].text : string.Empty;
} else {
if (SelectionPatterns == null) {
GUI.enabled = selectedDir > -1;
selectedName = GUI.enabled ? dirsWithImages[selectedDir].text : string.Empty;
} else {
GUI.enabled = selectedDir > -1 ||
( currentDirMatches && selectedNonMatchingDirs == -1 && selectedFile == -1 );
selectedName = selectedDir > -1 ? dirsWithImages[selectedDir].text : string.Empty;
}
}
if (GUILayout.Button("選択", GUILayout.Width(120))) {
if (BrowserType == FileBrowserType.File) {
callback(Path.Combine(currentDir, files[selectedFile]));
} else {
callback(selectedDir > -1 ? Path.Combine(currentDir, directories[selectedDir]) : currentDir);
}
}
GUI.enabled = true;
GUILayout.EndHorizontal();
} finally {
GUILayout.EndArea();
}
}
protected void FileClickCallback(int i) {
if (BrowserType == FileBrowserType.File) {
// m_callback(Path.Combine(m_currentDirectory, m_files[i]));
}
}
protected void DirectoryClickCallback(int i) {
SetNewDirectory(Path.Combine(currentDir, directories[i]));
}
protected void NonMatchingDirectoryClickCallback(int i) {
SetNewDirectory(Path.Combine(currentDir, nonMatchingDirs[i]));
}
}
public static class GUILayoutx {
public delegate void ClickCallback(int index);
public static int SelectionList(int selected, GUIContent[] list, GUIStyle elementStyle, ClickCallback callback = null) {
for (var i = 0; i < list.Length; ++i) {
var elementRect = GUILayoutUtility.GetRect(list[i], elementStyle);
var hover = elementRect.Contains(Event.current.mousePosition);
if (hover && Event.current.type == EventType.MouseDown) {
selected = i;
if (callback != null) callback(i);
Event.current.Use();
} else if (Event.current.type == EventType.repaint) {
elementStyle.Draw(elementRect, list[i], hover, false, i == selected, false);
}
}
return selected;
}
public static int SelectionList(int selected, string[] list, GUIStyle elementStyle, ClickCallback callback=null) {
elementStyle.active.textColor = new Color(0.8f, 1f, 1f);
for (var i = 0; i < list.Length; ++i) {
var elementRect = GUILayoutUtility.GetRect(new GUIContent(list[i]), elementStyle);
var hover = elementRect.Contains(Event.current.mousePosition);
if (hover && Event.current.type == EventType.MouseDown) {
selected = i;
if (callback != null) callback(i);
Event.current.Use();
} else if (Event.current.type == EventType.repaint) {
elementStyle.Draw(elementRect, list[i], hover, false, i == selected, false);
}
}
return selected;
}
}
}