Runtime GUI WIP: OpenFileDialog/OpenDirectoryDialog + runtime GUI.

This commit is contained in:
Matias Lavik 2020-05-25 19:54:02 +02:00
Родитель c6dcc731a7
Коммит b187faeeb0
8 изменённых файлов: 339 добавлений и 0 удалений

8
Assets/Scripts/GUI.meta Normal file
Просмотреть файл

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 763a5b8aeb04c4348bc4bdc9123ae1ae
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

Просмотреть файл

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b7a9b13a433a16c40ad90065498cf32d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

Просмотреть файл

@ -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);
}
}
}
}

Просмотреть файл

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a3505220cff523a44955829adad14886
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Просмотреть файл

@ -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);
}
}
}
}
}

Просмотреть файл

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 803cbb09de19cba429d42abcff87982b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Просмотреть файл

@ -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<RuntimeFileBrowserComponent>();
dialogComp.dialogMode = RuntimeFileBrowserComponent.DialogMode.OpenFile;
dialogComp.callback = resultCallback;
}
public static void ShowSaveFileDialog(DialogCallback resultCallback)
{
GameObject dialogObject = new GameObject("_SaveFileDialog");
RuntimeFileBrowserComponent dialogComp = dialogObject.AddComponent<RuntimeFileBrowserComponent>();
dialogComp.dialogMode = RuntimeFileBrowserComponent.DialogMode.SaveFile;
dialogComp.callback = resultCallback;
}
public static void ShowOpenDirectoryDialog(DialogCallback resultCallback)
{
GameObject dialogObject = new GameObject("_OpenDirectoryDialog");
RuntimeFileBrowserComponent dialogComp = dialogObject.AddComponent<RuntimeFileBrowserComponent>();
dialogComp.dialogMode = RuntimeFileBrowserComponent.DialogMode.OpenDirectory;
dialogComp.callback = resultCallback;
}
}
}

Просмотреть файл

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 76cf32388bffdf04cbae65ee059fb111
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: