From bf1a38a20d3e9629d29756cfc9987181abcfc3e7 Mon Sep 17 00:00:00 2001 From: se5a Date: Sun, 2 Jun 2024 11:29:41 +1200 Subject: [PATCH] More functionality on the FileDialog. --- .../ModFileEditing/BluePrintsUI.cs | 54 +++++++++++++++++-- .../ModFileEditing/FileDialog.cs | 53 ++++++++++++++---- 2 files changed, 94 insertions(+), 13 deletions(-) diff --git a/Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs b/Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs index 2d857d262..6153562e2 100644 --- a/Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs +++ b/Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs @@ -35,6 +35,7 @@ public abstract class BluePrintsUI private protected bool _showFileDialog = false; private protected string _fileDialogPath = ""; + private protected string _fileName = ""; protected BluePrintsUI(ModDataStore modDataStore) { @@ -65,6 +66,8 @@ protected BluePrintsUI(ModDataStore modDataStore) public abstract void Refresh(); + protected abstract void Save(); + public void Display(string label) { int i = 0; @@ -111,7 +114,10 @@ public void Display(string label) if (_showFileDialog) { - FileDialog.Display(ref _fileDialogPath, ref _showFileDialog); + if (FileDialog.Display(ref _fileDialogPath, ref _fileName, ref _showFileDialog)) + { + + } } } @@ -175,8 +181,20 @@ public sealed override void Refresh() newEmpty.Name = "New Blueprint"; _newEmpty = newEmpty; } - - + + protected override void Save() + { + using (StreamWriter outputFile = new StreamWriter(Path.Combine(_fileDialogPath, _fileName))) + { + foreach (TechCategoryBlueprint blueprint in _itemBlueprints) + { + var json = JsonConvert.SerializeObject(blueprint, Formatting.Indented); + outputFile.WriteLine(json); + } + } + } + + public override void DisplayEditorWindow(int selectedIndex) { if(!_isActive[selectedIndex]) @@ -234,6 +252,11 @@ public override void Refresh() _newEmpty = newEmpty; } + protected override void Save() + { + throw new NotImplementedException(); + } + public override void DisplayEditorWindow(int selectedIndex) { @@ -350,6 +373,11 @@ public sealed override void Refresh() _newEmpty = newEmpty; } + protected override void Save() + { + throw new NotImplementedException(); + } + public override void DisplayEditorWindow(int selectedIndex) { @@ -474,6 +502,11 @@ public sealed override void Refresh() _newEmpty = newEmpty; } + protected override void Save() + { + throw new NotImplementedException(); + } + public override void DisplayEditorWindow(int selectedIndex) { @@ -545,6 +578,11 @@ public sealed override void Refresh() _newEmpty = newEmpty; } + protected override void Save() + { + throw new NotImplementedException(); + } + public override void DisplayEditorWindow(int selectedIndex) { @@ -705,6 +743,11 @@ public override void Refresh() _newEmpty = newEmpty; } + protected override void Save() + { + throw new NotImplementedException(); + } + public override void DisplayEditorWindow(int selectedIndex) { @@ -837,6 +880,11 @@ public sealed override void Refresh() } } + protected override void Save() + { + throw new NotImplementedException(); + } + public void Display() { diff --git a/Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs b/Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs index 1494f7a0e..cd719856c 100644 --- a/Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs +++ b/Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs @@ -22,8 +22,9 @@ public enum SaveOrLoad public static SaveOrLoad DialogType = SaveOrLoad.Save; - public static void Display(ref string path, ref bool IsActive) + public static bool Display(ref string path, ref string fileName, ref bool IsActive) { + bool isok = false; if (string.IsNullOrEmpty(path)) _pathString = _curDir; else @@ -32,7 +33,10 @@ public static void Display(ref string path, ref bool IsActive) ImGui.Begin("File Dialog", ref IsActive); ImGui.Text("Name:"); ImGui.SameLine(); - ImGui.InputText("##Name", _strInputBuffer, 128); + if (ImGui.InputText("##Name", _strInputBuffer, 128)) + { + fileName = ImGuiSDL2CSHelper.StringFromBytes(_strInputBuffer); + } ImGui.Columns(2); ImGui.SetColumnWidth(0,128); @@ -92,15 +96,15 @@ public static void Display(ref string path, ref bool IsActive) _i = 0; foreach (var dir in dirs) { - DirectoryInfo fi = new DirectoryInfo(dir); + DirectoryInfo di = new DirectoryInfo(dir); _b = _i == _selectedIndex; - if (ImGui.Selectable(fi.Name, _b, ImGuiSelectableFlags.SpanAllColumns | ImGuiSelectableFlags.AllowDoubleClick)) + if (ImGui.Selectable(di.Name, _b, ImGuiSelectableFlags.SpanAllColumns | ImGuiSelectableFlags.AllowDoubleClick)) { _selectedIndex = _i; if (ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left)) { - _pathString = fi.FullName; + _pathString = di.FullName; } } @@ -109,10 +113,10 @@ public static void Display(ref string path, ref bool IsActive) ImGui.Text(""); ImGui.TableNextColumn(); - ImGui.Text(fi.Extension); + ImGui.Text(di.Extension); ImGui.TableNextColumn(); - ImGui.Text(fi.LastWriteTime.ToString()); + ImGui.Text(di.LastWriteTime.ToString()); ImGui.TableNextColumn(); _i++; } @@ -124,10 +128,17 @@ public static void Display(ref string path, ref bool IsActive) FileInfo fi = new FileInfo(file); _b = _i == _selectedIndex; - if (ImGui.Selectable(fi.Name, _b, ImGuiSelectableFlags.SpanAllColumns)) + if (ImGui.Selectable(fi.Name, _b, ImGuiSelectableFlags.SpanAllColumns | ImGuiSelectableFlags.AllowDoubleClick)) { _selectedIndex = _i; _strInputBuffer = ImGuiSDL2CSHelper.BytesFromString(fi.Name); + fileName = fi.Name; + + if (ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left)) + { + isok = true; + IsActive = false; + } } _i++; ImGui.TableNextColumn(); @@ -141,13 +152,35 @@ public static void Display(ref string path, ref bool IsActive) ImGui.Text(fi.LastWriteTime.ToString()); ImGui.TableNextColumn(); - } - ImGui.EndTable(); + ImGui.Columns(1); + if (DialogType == SaveOrLoad.Load) + { + if (ImGui.Button("Load")) + { + isok = true; + IsActive = false; + } + } + else + { + if (ImGui.Button("Save")) + { + isok = true; + IsActive = false; + } + } + ImGui.SameLine(); + if (ImGui.Button("Cancel")) + { + IsActive = false; + isok = false; + } ImGui.End(); path = _pathString; + return isok; } } \ No newline at end of file