From 292572c352503da7efcbf8d18dc2fc33ac2a348d Mon Sep 17 00:00:00 2001 From: se5a Date: Sat, 1 Jun 2024 14:24:25 +1200 Subject: [PATCH] File Dialog taking shape. --- .../ModFileEditing/BluePrintsUI.cs | 58 ++------- .../ModFileEditing/FileDialog.cs | 115 ++++++++++++++++-- 2 files changed, 119 insertions(+), 54 deletions(-) diff --git a/Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs b/Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs index a98bbdee0..2d857d262 100644 --- a/Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs +++ b/Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs @@ -32,6 +32,10 @@ public abstract class BluePrintsUI private protected string[] _guiHints; private protected Vector2 _childSize = new Vector2(640, 200); + + private protected bool _showFileDialog = false; + private protected string _fileDialogPath = ""; + protected BluePrintsUI(ModDataStore modDataStore) { _modDataStore = modDataStore; @@ -61,9 +65,6 @@ protected BluePrintsUI(ModDataStore modDataStore) public abstract void Refresh(); - public abstract void SaveAs(string path); - - public void Display(string label) { int i = 0; @@ -73,8 +74,7 @@ public void Display(string label) ImGui.SameLine(); if (ImGui.Button("SaveAs")) { - SaveAs("foo"); - //FileDialog.Display(); + _showFileDialog = true; } ImGui.SameLine(); ImGui.Button("SaveToMemory"); @@ -108,6 +108,11 @@ public void Display(string label) ImGui.EndChild(); ImGui.TreePop(); } + + if (_showFileDialog) + { + FileDialog.Display(ref _fileDialogPath, ref _showFileDialog); + } } public abstract void DisplayEditorWindow(int index); @@ -170,23 +175,7 @@ public sealed override void Refresh() newEmpty.Name = "New Blueprint"; _newEmpty = newEmpty; } - - public override void SaveAs(string path) - { - - path = "Data/basemod"; - FileDialog.Display(path); - /* - using (StreamWriter outputFile = new StreamWriter(Path.Combine(path, "tcb.json"))) - { - foreach (TechCategoryBlueprint blueprint in _itemBlueprints) - { - var json = JsonConvert.SerializeObject(blueprint, Formatting.Indented); - outputFile.WriteLine(json); - } - } - */ - } + public override void DisplayEditorWindow(int selectedIndex) { @@ -245,11 +234,6 @@ public override void Refresh() _newEmpty = newEmpty; } - public override void SaveAs(string path) - { - throw new NotImplementedException(); - } - public override void DisplayEditorWindow(int selectedIndex) { @@ -366,10 +350,6 @@ public sealed override void Refresh() _newEmpty = newEmpty; } - public override void SaveAs(string path) - { - throw new NotImplementedException(); - } public override void DisplayEditorWindow(int selectedIndex) { @@ -494,10 +474,6 @@ public sealed override void Refresh() _newEmpty = newEmpty; } - public override void SaveAs(string path) - { - throw new NotImplementedException(); - } public override void DisplayEditorWindow(int selectedIndex) { @@ -569,10 +545,6 @@ public sealed override void Refresh() _newEmpty = newEmpty; } - public override void SaveAs(string path) - { - throw new NotImplementedException(); - } public override void DisplayEditorWindow(int selectedIndex) { @@ -733,10 +705,6 @@ public override void Refresh() _newEmpty = newEmpty; } - public override void SaveAs(string path) - { - throw new NotImplementedException(); - } public override void DisplayEditorWindow(int selectedIndex) { @@ -869,10 +837,6 @@ public sealed override void Refresh() } } - public override void SaveAs(string path) - { - throw new NotImplementedException(); - } public void Display() { diff --git a/Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs b/Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs index 80c88377b..1494f7a0e 100644 --- a/Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs +++ b/Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs @@ -1,7 +1,11 @@ +using System; +using System.IO; using ImGuiNET; +using ImGuiSDL2CS; namespace Pulsar4X.SDL2UI.ModFileEditing; + public static class FileDialog { public enum SaveOrLoad @@ -11,24 +15,62 @@ public enum SaveOrLoad } private static byte[] _strInputBuffer = new byte[128]; private static string _pathString; - + private static string _curDir = Directory.GetCurrentDirectory(); + private static int _selectedIndex = -1; + private static int _i = 0; + private static bool _b = false; public static SaveOrLoad DialogType = SaveOrLoad.Save; - public static void Display(string path) + + public static void Display(ref string path, ref bool IsActive) { - _pathString = path; - ImGui.Begin("File Dialog"); + if (string.IsNullOrEmpty(path)) + _pathString = _curDir; + else + _pathString = path; + + ImGui.Begin("File Dialog", ref IsActive); ImGui.Text("Name:"); ImGui.SameLine(); ImGui.InputText("##Name", _strInputBuffer, 128); ImGui.Columns(2); ImGui.SetColumnWidth(0,128); + + + if (ImGui.Button("Docs")) + { + _pathString = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); + + } + + if (ImGui.Button("Desktop")) + { + _pathString = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); + } + + //this is Editor specific TODO: add a way to add specific dir to the LH colomn + if (ImGui.Button("Data/basemod")) + { + _pathString = "Data/basemod"; + } + + //this is Editor specific TODO: add a way to add specific dir to the LH colomn + if (ImGui.Button("GameEngine/Data/basemod")) + { + var dir = new DirectoryInfo(_curDir); + while (dir.Name != "Pulsar4X") + { + dir = Directory.GetParent(dir.FullName); + } + _pathString = Path.Combine(dir.FullName, "GameEngine/Data/basemod"); + } - ImGui.NextColumn(); - ImGui.BeginTable("", 4); + ImGui.NextColumn(); + ImGui.BeginTable("table", 4); + ImGui.TableNextColumn(); ImGui.TableHeader("Name"); ImGui.TableNextColumn(); ImGui.TableHeader("Size"); @@ -38,15 +80,74 @@ public static void Display(string path) ImGui.TableHeader("Modified"); ImGui.TableNextColumn(); - var files = System.IO.Directory.EnumerateFiles(_pathString); + if (ImGui.Selectable("..", _b, ImGuiSelectableFlags.SpanAllColumns | ImGuiSelectableFlags.AllowDoubleClick)) + { + if (ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left)) + { + _pathString = Directory.GetParent(_pathString).FullName; + } + } + + var dirs = Directory.EnumerateDirectories(_pathString); + _i = 0; + foreach (var dir in dirs) + { + DirectoryInfo fi = new DirectoryInfo(dir); + + _b = _i == _selectedIndex; + if (ImGui.Selectable(fi.Name, _b, ImGuiSelectableFlags.SpanAllColumns | ImGuiSelectableFlags.AllowDoubleClick)) + { + _selectedIndex = _i; + if (ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left)) + { + _pathString = fi.FullName; + } + } + + ImGui.TableNextColumn(); + + ImGui.Text(""); + ImGui.TableNextColumn(); + + ImGui.Text(fi.Extension); + ImGui.TableNextColumn(); + + ImGui.Text(fi.LastWriteTime.ToString()); + ImGui.TableNextColumn(); + _i++; + } + + var files = Directory.EnumerateFiles(_pathString); foreach (var file in files) { + + FileInfo fi = new FileInfo(file); + + _b = _i == _selectedIndex; + if (ImGui.Selectable(fi.Name, _b, ImGuiSelectableFlags.SpanAllColumns)) + { + _selectedIndex = _i; + _strInputBuffer = ImGuiSDL2CSHelper.BytesFromString(fi.Name); + } + _i++; + ImGui.TableNextColumn(); + ImGui.Text(fi.Length.ToString()); + ImGui.TableNextColumn(); + + ImGui.Text(fi.Extension); + ImGui.TableNextColumn(); + + ImGui.Text(fi.LastWriteTime.ToString()); + ImGui.TableNextColumn(); + + } ImGui.EndTable(); ImGui.End(); + path = _pathString; } } \ No newline at end of file