Skip to content

Commit b187fae

Browse files
committed
Runtime GUI WIP: OpenFileDialog/OpenDirectoryDialog + runtime GUI.
1 parent c6dcc73 commit b187fae

File tree

8 files changed

+339
-0
lines changed

8 files changed

+339
-0
lines changed

Assets/Scripts/GUI.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/GUI/Components.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
using System;
2+
using System.IO;
3+
using UnityEngine;
4+
5+
namespace UnityVolumeRendering
6+
{
7+
public partial class RuntimeFileBrowser
8+
{
9+
public class RuntimeFileBrowserComponent : MonoBehaviour
10+
{
11+
public enum DialogMode
12+
{
13+
OpenFile,
14+
OpenDirectory,
15+
SaveFile
16+
}
17+
18+
public DialogMode dialogMode = DialogMode.OpenFile;
19+
public DialogCallback callback = null;
20+
21+
private string currentDirectory;
22+
private string selectedFile;
23+
private Vector2 scrollPos = Vector2.zero;
24+
25+
private Rect windowRect = new Rect(100, 50, WINDOW_WIDTH, WINDOW_HEIGHT);
26+
27+
private const int LEFT_PANEL_WIDTH = 100;
28+
private const int RIGHT_PANEL_WIDTH = 370;
29+
private const int WINDOW_WIDTH = 500;
30+
private const int WINDOW_HEIGHT = 300;
31+
32+
private void OnGUI()
33+
{
34+
windowRect = GUI.Window(0, windowRect, UpdateWindow, "File browser");
35+
}
36+
37+
private void UpdateWindow(int windowID)
38+
{
39+
GUI.DragWindow(new Rect(0, 0, 10000, 20));
40+
41+
TextAnchor oldAlignment = GUI.skin.label.alignment;
42+
GUI.skin.button.alignment = TextAnchor.MiddleLeft;
43+
44+
GUILayout.BeginVertical();
45+
46+
DrawTopPanel();
47+
48+
GUILayout.BeginHorizontal();
49+
50+
DrawLeftSideMenu();
51+
52+
DrawDirectoryView();
53+
54+
GUILayout.FlexibleSpace();
55+
56+
GUILayout.EndHorizontal();
57+
58+
DrawBottomBar();
59+
60+
GUILayout.EndVertical();
61+
62+
GUI.skin.button.alignment = oldAlignment;
63+
}
64+
65+
private void DrawTopPanel()
66+
{
67+
GUILayout.BeginHorizontal();
68+
69+
// "Back" button
70+
if (GUILayout.Button("Back", GUILayout.Width(LEFT_PANEL_WIDTH)))
71+
{
72+
DirectoryInfo parentDir = Directory.GetParent(currentDirectory);
73+
if (parentDir != null)
74+
currentDirectory = parentDir.FullName;
75+
else
76+
currentDirectory = "";
77+
scrollPos = Vector2.zero;
78+
}
79+
// Show current directory path
80+
currentDirectory = GUILayout.TextField(currentDirectory, GUILayout.Width(RIGHT_PANEL_WIDTH));
81+
82+
GUILayout.EndHorizontal();
83+
}
84+
85+
private void DrawLeftSideMenu()
86+
{
87+
GUILayout.BeginVertical(GUILayout.Width(LEFT_PANEL_WIDTH));
88+
89+
foreach (DriveInfo driveInfo in DriveInfo.GetDrives())
90+
{
91+
if (GUILayout.Button(driveInfo.Name))
92+
{
93+
currentDirectory = driveInfo.Name;
94+
scrollPos = Vector2.zero;
95+
}
96+
}
97+
98+
if (GUILayout.Button("Documents"))
99+
{
100+
currentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
101+
scrollPos = Vector2.zero;
102+
}
103+
if (GUILayout.Button("Desktop"))
104+
{
105+
currentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
106+
scrollPos = Vector2.zero;
107+
}
108+
109+
GUILayout.EndVertical();
110+
}
111+
112+
private void DrawDirectoryView()
113+
{
114+
GUILayout.BeginVertical();
115+
116+
// Draw directory content
117+
if (!string.IsNullOrEmpty(currentDirectory) && Directory.Exists(currentDirectory))
118+
{
119+
scrollPos = GUILayout.BeginScrollView(scrollPos);
120+
// Draw directories
121+
foreach (string dir in Directory.GetDirectories(currentDirectory))
122+
{
123+
DirectoryInfo dirInfo = new DirectoryInfo(dir);
124+
if (GUILayout.Button(dirInfo.Name))
125+
{
126+
currentDirectory = dir;
127+
}
128+
}
129+
// Draw files
130+
if (dialogMode == DialogMode.OpenFile || dialogMode == DialogMode.SaveFile)
131+
{
132+
foreach (string file in Directory.GetFiles(currentDirectory))
133+
{
134+
FileInfo fileInfo = new FileInfo(file);
135+
if (GUILayout.Button(fileInfo.Name))
136+
{
137+
selectedFile = fileInfo.FullName;
138+
}
139+
}
140+
}
141+
GUILayout.EndScrollView();
142+
}
143+
GUILayout.EndVertical();
144+
}
145+
146+
private void DrawBottomBar()
147+
{
148+
GUILayout.BeginHorizontal();
149+
150+
if(dialogMode == DialogMode.OpenFile || dialogMode == DialogMode.SaveFile)
151+
{
152+
if (!string.IsNullOrEmpty(selectedFile))
153+
{
154+
FileInfo fileInfo = new FileInfo(selectedFile);
155+
string fileName = Path.GetFileName(selectedFile);
156+
// Show filename textbox
157+
fileName = GUILayout.TextField(fileName, GUILayout.Width(RIGHT_PANEL_WIDTH));
158+
selectedFile = Path.Combine(fileInfo.Directory.FullName, fileName);
159+
GUILayout.FlexibleSpace();
160+
// Show button
161+
string buttonText = dialogMode == DialogMode.OpenFile ? "Open" : "Save";
162+
if (File.Exists(selectedFile) && GUILayout.Button(buttonText))
163+
{
164+
CloseBrowser(false, selectedFile);
165+
}
166+
}
167+
}
168+
else if(dialogMode == DialogMode.OpenDirectory)
169+
{
170+
if (!string.IsNullOrEmpty(currentDirectory))
171+
{
172+
// Show directory path textbox
173+
currentDirectory = GUILayout.TextField(currentDirectory, GUILayout.Width(RIGHT_PANEL_WIDTH));
174+
GUILayout.FlexibleSpace();
175+
// Show button
176+
string buttonText ="Open";
177+
if (Directory.Exists(currentDirectory) && GUILayout.Button(buttonText))
178+
{
179+
CloseBrowser(false, currentDirectory);
180+
}
181+
}
182+
}
183+
184+
GUILayout.FlexibleSpace();
185+
if (GUILayout.Button("Cancel"))
186+
{
187+
CloseBrowser(true, "");
188+
}
189+
190+
GUILayout.EndHorizontal();
191+
}
192+
193+
private void CloseBrowser(bool cancelled, string selectedPath)
194+
{
195+
DialogResult result;
196+
result.cancelled = cancelled;
197+
result.path = selectedPath;
198+
199+
callback?.Invoke(result);
200+
201+
GameObject.Destroy(this.gameObject);
202+
}
203+
}
204+
}
205+
}

Assets/Scripts/GUI/Components/RuntimeFileBrowserComponent.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using UnityEngine;
2+
3+
namespace UnityVolumeRendering
4+
{
5+
public class RuntimeGUI : MonoBehaviour
6+
{
7+
private void OnGUI()
8+
{
9+
GUILayout.BeginVertical();
10+
11+
if(GUILayout.Button("Import RAW dataset"))
12+
{
13+
RuntimeFileBrowser.ShowOpenFileDialog(OnOpenRAWDatasetResult);
14+
}
15+
if (GUILayout.Button("Import DICOM dataset"))
16+
{
17+
RuntimeFileBrowser.ShowOpenDirectoryDialog(OnOpenDICOMDatasetResult);
18+
}
19+
20+
GUILayout.EndVertical();
21+
}
22+
23+
private void OnOpenRAWDatasetResult(RuntimeFileBrowser.DialogResult result)
24+
{
25+
if(!result.cancelled)
26+
{
27+
// TODO
28+
}
29+
}
30+
31+
private void OnOpenDICOMDatasetResult(RuntimeFileBrowser.DialogResult result)
32+
{
33+
if (!result.cancelled)
34+
{
35+
DICOMImporter importer = new DICOMImporter(result.path, true);
36+
VolumeDataset dataset = importer.Import();
37+
if(dataset != null)
38+
{
39+
VolumeObjectFactory.CreateObject(dataset);
40+
}
41+
}
42+
}
43+
}
44+
}

Assets/Scripts/GUI/Components/RuntimeGUI.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.IO;
3+
using UnityEngine;
4+
5+
namespace UnityVolumeRendering
6+
{
7+
public partial class RuntimeFileBrowser
8+
{
9+
public struct DialogResult
10+
{
11+
public bool cancelled;
12+
public string path;
13+
}
14+
15+
public delegate void DialogCallback(DialogResult result);
16+
17+
public static void ShowOpenFileDialog(DialogCallback resultCallback)
18+
{
19+
GameObject dialogObject = new GameObject("_OpenFileDialog");
20+
RuntimeFileBrowserComponent dialogComp = dialogObject.AddComponent<RuntimeFileBrowserComponent>();
21+
dialogComp.dialogMode = RuntimeFileBrowserComponent.DialogMode.OpenFile;
22+
dialogComp.callback = resultCallback;
23+
}
24+
25+
public static void ShowSaveFileDialog(DialogCallback resultCallback)
26+
{
27+
GameObject dialogObject = new GameObject("_SaveFileDialog");
28+
RuntimeFileBrowserComponent dialogComp = dialogObject.AddComponent<RuntimeFileBrowserComponent>();
29+
dialogComp.dialogMode = RuntimeFileBrowserComponent.DialogMode.SaveFile;
30+
dialogComp.callback = resultCallback;
31+
}
32+
33+
public static void ShowOpenDirectoryDialog(DialogCallback resultCallback)
34+
{
35+
GameObject dialogObject = new GameObject("_OpenDirectoryDialog");
36+
RuntimeFileBrowserComponent dialogComp = dialogObject.AddComponent<RuntimeFileBrowserComponent>();
37+
dialogComp.dialogMode = RuntimeFileBrowserComponent.DialogMode.OpenDirectory;
38+
dialogComp.callback = resultCallback;
39+
}
40+
}
41+
}

Assets/Scripts/GUI/RuntimeFileBrowser.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)