diff --git a/Assets/Scripts/GUI.meta b/Assets/Scripts/GUI.meta new file mode 100644 index 0000000..f2b36fb --- /dev/null +++ b/Assets/Scripts/GUI.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 763a5b8aeb04c4348bc4bdc9123ae1ae +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/GUI/Components.meta b/Assets/Scripts/GUI/Components.meta new file mode 100644 index 0000000..b9490e2 --- /dev/null +++ b/Assets/Scripts/GUI/Components.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b7a9b13a433a16c40ad90065498cf32d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/GUI/Components/RuntimeFileBrowserComponent.cs b/Assets/Scripts/GUI/Components/RuntimeFileBrowserComponent.cs new file mode 100644 index 0000000..52be8a8 --- /dev/null +++ b/Assets/Scripts/GUI/Components/RuntimeFileBrowserComponent.cs @@ -0,0 +1,205 @@ +using System; +using System.IO; +using UnityEngine; + +namespace UnityVolumeRendering +{ + public partial class RuntimeFileBrowser + { + public class RuntimeFileBrowserComponent : MonoBehaviour + { + public enum DialogMode + { + OpenFile, + OpenDirectory, + SaveFile + } + + public DialogMode dialogMode = DialogMode.OpenFile; + public DialogCallback callback = null; + + private string currentDirectory; + private string selectedFile; + private Vector2 scrollPos = Vector2.zero; + + private Rect windowRect = new Rect(100, 50, WINDOW_WIDTH, WINDOW_HEIGHT); + + private const int LEFT_PANEL_WIDTH = 100; + private const int RIGHT_PANEL_WIDTH = 370; + private const int WINDOW_WIDTH = 500; + private const int WINDOW_HEIGHT = 300; + + private void OnGUI() + { + windowRect = GUI.Window(0, windowRect, UpdateWindow, "File browser"); + } + + private void UpdateWindow(int windowID) + { + GUI.DragWindow(new Rect(0, 0, 10000, 20)); + + TextAnchor oldAlignment = GUI.skin.label.alignment; + GUI.skin.button.alignment = TextAnchor.MiddleLeft; + + GUILayout.BeginVertical(); + + DrawTopPanel(); + + GUILayout.BeginHorizontal(); + + DrawLeftSideMenu(); + + DrawDirectoryView(); + + GUILayout.FlexibleSpace(); + + GUILayout.EndHorizontal(); + + DrawBottomBar(); + + GUILayout.EndVertical(); + + GUI.skin.button.alignment = oldAlignment; + } + + private void DrawTopPanel() + { + GUILayout.BeginHorizontal(); + + // "Back" button + if (GUILayout.Button("Back", GUILayout.Width(LEFT_PANEL_WIDTH))) + { + DirectoryInfo parentDir = Directory.GetParent(currentDirectory); + if (parentDir != null) + currentDirectory = parentDir.FullName; + else + currentDirectory = ""; + scrollPos = Vector2.zero; + } + // Show current directory path + currentDirectory = GUILayout.TextField(currentDirectory, GUILayout.Width(RIGHT_PANEL_WIDTH)); + + GUILayout.EndHorizontal(); + } + + private void DrawLeftSideMenu() + { + GUILayout.BeginVertical(GUILayout.Width(LEFT_PANEL_WIDTH)); + + foreach (DriveInfo driveInfo in DriveInfo.GetDrives()) + { + if (GUILayout.Button(driveInfo.Name)) + { + currentDirectory = driveInfo.Name; + scrollPos = Vector2.zero; + } + } + + if (GUILayout.Button("Documents")) + { + currentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); + scrollPos = Vector2.zero; + } + if (GUILayout.Button("Desktop")) + { + currentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); + scrollPos = Vector2.zero; + } + + GUILayout.EndVertical(); + } + + private void DrawDirectoryView() + { + GUILayout.BeginVertical(); + + // Draw directory content + if (!string.IsNullOrEmpty(currentDirectory) && Directory.Exists(currentDirectory)) + { + scrollPos = GUILayout.BeginScrollView(scrollPos); + // Draw directories + foreach (string dir in Directory.GetDirectories(currentDirectory)) + { + DirectoryInfo dirInfo = new DirectoryInfo(dir); + if (GUILayout.Button(dirInfo.Name)) + { + currentDirectory = dir; + } + } + // Draw files + if (dialogMode == DialogMode.OpenFile || dialogMode == DialogMode.SaveFile) + { + foreach (string file in Directory.GetFiles(currentDirectory)) + { + FileInfo fileInfo = new FileInfo(file); + if (GUILayout.Button(fileInfo.Name)) + { + selectedFile = fileInfo.FullName; + } + } + } + GUILayout.EndScrollView(); + } + GUILayout.EndVertical(); + } + + private void DrawBottomBar() + { + GUILayout.BeginHorizontal(); + + if(dialogMode == DialogMode.OpenFile || dialogMode == DialogMode.SaveFile) + { + if (!string.IsNullOrEmpty(selectedFile)) + { + FileInfo fileInfo = new FileInfo(selectedFile); + string fileName = Path.GetFileName(selectedFile); + // Show filename textbox + fileName = GUILayout.TextField(fileName, GUILayout.Width(RIGHT_PANEL_WIDTH)); + selectedFile = Path.Combine(fileInfo.Directory.FullName, fileName); + GUILayout.FlexibleSpace(); + // Show button + string buttonText = dialogMode == DialogMode.OpenFile ? "Open" : "Save"; + if (File.Exists(selectedFile) && GUILayout.Button(buttonText)) + { + CloseBrowser(false, selectedFile); + } + } + } + else if(dialogMode == DialogMode.OpenDirectory) + { + if (!string.IsNullOrEmpty(currentDirectory)) + { + // Show directory path textbox + currentDirectory = GUILayout.TextField(currentDirectory, GUILayout.Width(RIGHT_PANEL_WIDTH)); + GUILayout.FlexibleSpace(); + // Show button + string buttonText ="Open"; + if (Directory.Exists(currentDirectory) && GUILayout.Button(buttonText)) + { + CloseBrowser(false, currentDirectory); + } + } + } + + GUILayout.FlexibleSpace(); + if (GUILayout.Button("Cancel")) + { + CloseBrowser(true, ""); + } + + GUILayout.EndHorizontal(); + } + + private void CloseBrowser(bool cancelled, string selectedPath) + { + DialogResult result; + result.cancelled = cancelled; + result.path = selectedPath; + + callback?.Invoke(result); + + GameObject.Destroy(this.gameObject); + } + } + } +} diff --git a/Assets/Scripts/GUI/Components/RuntimeFileBrowserComponent.cs.meta b/Assets/Scripts/GUI/Components/RuntimeFileBrowserComponent.cs.meta new file mode 100644 index 0000000..17a0792 --- /dev/null +++ b/Assets/Scripts/GUI/Components/RuntimeFileBrowserComponent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a3505220cff523a44955829adad14886 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/GUI/Components/RuntimeGUI.cs b/Assets/Scripts/GUI/Components/RuntimeGUI.cs new file mode 100644 index 0000000..e5ac0f8 --- /dev/null +++ b/Assets/Scripts/GUI/Components/RuntimeGUI.cs @@ -0,0 +1,44 @@ +using UnityEngine; + +namespace UnityVolumeRendering +{ + public class RuntimeGUI : MonoBehaviour + { + private void OnGUI() + { + GUILayout.BeginVertical(); + + if(GUILayout.Button("Import RAW dataset")) + { + RuntimeFileBrowser.ShowOpenFileDialog(OnOpenRAWDatasetResult); + } + if (GUILayout.Button("Import DICOM dataset")) + { + RuntimeFileBrowser.ShowOpenDirectoryDialog(OnOpenDICOMDatasetResult); + } + + GUILayout.EndVertical(); + } + + private void OnOpenRAWDatasetResult(RuntimeFileBrowser.DialogResult result) + { + if(!result.cancelled) + { + // TODO + } + } + + private void OnOpenDICOMDatasetResult(RuntimeFileBrowser.DialogResult result) + { + if (!result.cancelled) + { + DICOMImporter importer = new DICOMImporter(result.path, true); + VolumeDataset dataset = importer.Import(); + if(dataset != null) + { + VolumeObjectFactory.CreateObject(dataset); + } + } + } + } +} diff --git a/Assets/Scripts/GUI/Components/RuntimeGUI.cs.meta b/Assets/Scripts/GUI/Components/RuntimeGUI.cs.meta new file mode 100644 index 0000000..6b21b53 --- /dev/null +++ b/Assets/Scripts/GUI/Components/RuntimeGUI.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 803cbb09de19cba429d42abcff87982b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/GUI/RuntimeFileBrowser.cs b/Assets/Scripts/GUI/RuntimeFileBrowser.cs new file mode 100644 index 0000000..aa3f3f3 --- /dev/null +++ b/Assets/Scripts/GUI/RuntimeFileBrowser.cs @@ -0,0 +1,41 @@ +using System; +using System.IO; +using UnityEngine; + +namespace UnityVolumeRendering +{ + public partial class RuntimeFileBrowser + { + public struct DialogResult + { + public bool cancelled; + public string path; + } + + public delegate void DialogCallback(DialogResult result); + + public static void ShowOpenFileDialog(DialogCallback resultCallback) + { + GameObject dialogObject = new GameObject("_OpenFileDialog"); + RuntimeFileBrowserComponent dialogComp = dialogObject.AddComponent(); + dialogComp.dialogMode = RuntimeFileBrowserComponent.DialogMode.OpenFile; + dialogComp.callback = resultCallback; + } + + public static void ShowSaveFileDialog(DialogCallback resultCallback) + { + GameObject dialogObject = new GameObject("_SaveFileDialog"); + RuntimeFileBrowserComponent dialogComp = dialogObject.AddComponent(); + dialogComp.dialogMode = RuntimeFileBrowserComponent.DialogMode.SaveFile; + dialogComp.callback = resultCallback; + } + + public static void ShowOpenDirectoryDialog(DialogCallback resultCallback) + { + GameObject dialogObject = new GameObject("_OpenDirectoryDialog"); + RuntimeFileBrowserComponent dialogComp = dialogObject.AddComponent(); + dialogComp.dialogMode = RuntimeFileBrowserComponent.DialogMode.OpenDirectory; + dialogComp.callback = resultCallback; + } + } +} diff --git a/Assets/Scripts/GUI/RuntimeFileBrowser.cs.meta b/Assets/Scripts/GUI/RuntimeFileBrowser.cs.meta new file mode 100644 index 0000000..97ce976 --- /dev/null +++ b/Assets/Scripts/GUI/RuntimeFileBrowser.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 76cf32388bffdf04cbae65ee059fb111 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: