diff --git a/Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs b/Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs index cadeb2fcf..827105a85 100644 --- a/Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs +++ b/Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs @@ -133,7 +133,7 @@ public void Display(string label) if (_showFileDialog) { - if (FileDialog.Display(ref _fileDialogPath, ref _fileName, ref _showFileDialog)) + if (FileDialog.DisplaySave(ref _fileDialogPath, ref _fileName, ref _showFileDialog)) { Save(); } diff --git a/Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs b/Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs index 5f926cd22..e79fbb1b8 100644 --- a/Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs +++ b/Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs @@ -26,10 +26,21 @@ public enum SaveOrLoad private static int _selectedIndex = -1; private static int _i = 0; private static bool _b = false; - public static SaveOrLoad DialogType = SaveOrLoad.Save; - + private static SaveOrLoad DialogType = SaveOrLoad.Save; + + public static bool DisplaySave(ref string path, ref string fileName, ref bool IsActive) + { + DialogType = SaveOrLoad.Save; + return Display(ref path, ref fileName, ref IsActive); + } + + public static bool DisplayLoad(ref string path, ref string fileName, ref bool IsActive) + { + DialogType = SaveOrLoad.Load; + return Display(ref path, ref fileName, ref IsActive); + } - public static bool Display(ref string path, ref string fileName, ref bool IsActive) + private static bool Display(ref string path, ref string fileName, ref bool IsActive) { bool isok = false; if (string.IsNullOrEmpty(path)) diff --git a/Pulsar4X/Pulsar4X.Client/ModFileEditing/ModFileEditor.cs b/Pulsar4X/Pulsar4X.Client/ModFileEditing/ModFileEditor.cs index 3ba740b8c..e9af41439 100644 --- a/Pulsar4X/Pulsar4X.Client/ModFileEditing/ModFileEditor.cs +++ b/Pulsar4X/Pulsar4X.Client/ModFileEditing/ModFileEditor.cs @@ -26,7 +26,10 @@ internal static ModFileEditor GetInstance() if (!_uiState.LoadedWindows.ContainsKey(typeof(ModFileEditor))) { instance = new ModFileEditor(); - instance.refresh(); + ModLoader modLoader = new ModLoader(); + ModDataStore modDataStore = new ModDataStore(); + modLoader.LoadModManifest("Data/basemod/modInfo.json", modDataStore); + instance.Refresh(modDataStore); } else { @@ -35,12 +38,8 @@ internal static ModFileEditor GetInstance() return instance; } - void refresh() + public void Refresh(ModDataStore modDataStore) { - ModLoader modLoader = new ModLoader(); - ModDataStore modDataStore = new ModDataStore(); - modLoader.LoadModManifest("Data/basemod/modInfo.json", modDataStore); - _modInfoUI = new ModInfoUI(modDataStore); _techCatBlueprintUI = new TechCatBlueprintUI(modDataStore); _techBlueprintUI = new TechBlueprintUI(modDataStore); @@ -50,7 +49,6 @@ void refresh() _armorBlueprintUI = new ArmorBlueprintUI(modDataStore); _processedMateralsUI = new ProcessedMateralsUI(modDataStore); _mineralsBlueprintUI = new MineralBlueprintUI(modDataStore); - } diff --git a/Pulsar4X/Pulsar4X.Client/ModFileEditing/ModInfoUI.cs b/Pulsar4X/Pulsar4X.Client/ModFileEditing/ModInfoUI.cs index 7e5cf5c79..585e28a9e 100644 --- a/Pulsar4X/Pulsar4X.Client/ModFileEditing/ModInfoUI.cs +++ b/Pulsar4X/Pulsar4X.Client/ModFileEditing/ModInfoUI.cs @@ -2,6 +2,7 @@ using System.IO; using System.Numerics; using ImGuiNET; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Pulsar4X.Modding; @@ -15,9 +16,11 @@ public class ModInfoUI private protected Vector2 _childSize = new Vector2(640, 200); - private protected bool _showFileDialog = false; + private protected bool _showSaveDialog = false; + private protected bool _showLoadDialog = false; private protected string _fileDialogPath = ""; private protected string _fileName = ""; + private protected int _selectedIndex = -1; private protected ModManifest _newEmpty; public ModInfoUI(ModDataStore modDataStore) { @@ -45,14 +48,7 @@ public void Display(string label) int i = 0; if(ImGui.TreeNode(label)) { - ImGui.Button("Save"); - ImGui.SameLine(); - if (ImGui.Button("SaveAs")) - { - _showFileDialog = true; - } - ImGui.SameLine(); - ImGui.Button("SaveToMemory"); + ImGui.BeginChild(label,_childSize, true); @@ -64,7 +60,17 @@ public void Display(string label) { ImGui.Text(_itemNames[i]); ImGui.NextColumn(); - + ImGui.Button("Save"); + ImGui.SameLine(); + if (ImGui.Button("SaveAs")) + { + _showSaveDialog = true; + _fileName = _itemNames[i] + ".json"; + _selectedIndex = i; + } + ImGui.SameLine(); + ImGui.Button("SaveToMemory"); + ImGui.SameLine(); if(ImGui.Button("Edit##" + label + item.Namespace)) { _isActive[i] = !_isActive[i]; @@ -79,18 +85,31 @@ public void Display(string label) ImGui.NextColumn(); i++; } - NewItem("+##"+label, _newEmpty); + NewItem("+##"+label); + ImGui.SameLine(); + if (ImGui.Button("Load")) + { + _showLoadDialog = true; + + } ImGui.EndChild(); ImGui.TreePop(); } - if (_showFileDialog) + if (_showSaveDialog) { - if (FileDialog.Display(ref _fileDialogPath, ref _fileName, ref _showFileDialog)) + if (FileDialog.DisplaySave(ref _fileDialogPath, ref _fileName, ref _showSaveDialog)) { Save(); } } + if (_showLoadDialog) + { + if (FileDialog.DisplayLoad(ref _fileDialogPath, ref _fileName, ref _showLoadDialog)) + { + Load(_fileDialogPath, _fileName); + } + } } public void DisplayEditorWindow(int selectedIndex) @@ -113,6 +132,8 @@ public void DisplayEditorWindow(int selectedIndex) if (TextEditWidget.Display("##name" + name, ref name)) { selectedItem.ModName = name; + _itemNames[selectedIndex] = name; + } ImGui.NextColumn(); @@ -134,7 +155,7 @@ public void DisplayEditorWindow(int selectedIndex) ImGui.NextColumn(); ImGui.Text("ModDir: "); ImGui.NextColumn(); - if (TextEditWidget.Display("##version" + name, ref modDir)) + if (TextEditWidget.Display("##modDir" + name, ref modDir)) { selectedItem.ModDirectory = modDir; } @@ -147,41 +168,50 @@ public void DisplayEditorWindow(int selectedIndex) selectedItem.Namespace = nameSpace; } + ImGui.NextColumn(); + ImGui.Text("DataFiles: "); + ImGui.NextColumn(); + foreach (var dataFile in selectedItem.DataFiles) + { + ImGui.Text(dataFile); + } + //selectedItem.DataFiles ImGui.End(); } } private void Save() { + var selectedItem = _modManafests[_selectedIndex]; + + //var modManifest = JsonConvert.DeserializeObject(manifestJson); + + var serialisedItem = JsonConvert.SerializeObject(selectedItem, Formatting.Indented); + using (StreamWriter outputFile = new StreamWriter(Path.Combine(_fileDialogPath, _fileName))) { - JArray output = new JArray(); - foreach (var bpt in _modManafests) - { - - /* - ModInstruction modInstruction = new ModInstruction(); - modInstruction.Type = _dataType; - modInstruction.Data = bpt; + //output.Add(selectedItem); + outputFile.Write(serialisedItem); + } + } - JObject jObject = new JObject - { - { "Type", modInstruction.Type.ToString() }, - { "Payload", JObject.FromObject(modInstruction.Data) } - }; - output.Add(jObject); - */ - } - //outputFile.Write(output); - }; + private void Load(string path, string filename) + { + ModLoader modLoader = new ModLoader(); + ModDataStore modDataStore = new ModDataStore(); + modLoader.LoadModManifest(Path.Combine(path,filename), modDataStore); + var editor = ModFileEditor.GetInstance(); + editor.Refresh(modDataStore); } - public void NewItem(string label, ModManifest newBlueprint) + public void NewItem(string label) { + ModManifest newManifest = new ModManifest(); + if (ImGui.Button(label)) { Array.Resize(ref _modManafests, _modManafests.Length + 1); - _modManafests[^1] = newBlueprint; + _modManafests[^1] = newManifest; Array.Resize(ref _itemNames, _itemNames.Length + 1); _itemNames[^1] = "newBluprint"; Array.Resize(ref _isActive, _isActive.Length + 1); diff --git a/Pulsar4X/Pulsar4X.Client/ModFileEditing/SelectFromListWiget.cs b/Pulsar4X/Pulsar4X.Client/ModFileEditing/SelectFromListWiget.cs index ddaf87e64..a967f6919 100644 --- a/Pulsar4X/Pulsar4X.Client/ModFileEditing/SelectFromListWiget.cs +++ b/Pulsar4X/Pulsar4X.Client/ModFileEditing/SelectFromListWiget.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using ImGuiNET; namespace Pulsar4X.SDL2UI.ModFileEditing; @@ -38,4 +39,41 @@ public static bool Display(string label, string[] selectFrom, ref int selected) } return hasChanged; } +} + +public static class SelectMultipleFromListWidget +{ + private static string? _editingID; + private static int _currentItem; + private static string[] _items; + private static int _itemCount; + + public static bool Display(string label, string[] selectFrom, ref List selected) + { + bool hasChanged = false; + string displayText = "null"; + + if (label != _editingID) + { + ImGui.Text(displayText); + if(ImGui.IsItemClicked()) + { + _editingID = label; + _items = selectFrom; + _itemCount = _items.Length; + } + } + else + { + ImGui.Text(displayText); + ImGui.SameLine(); + if (ImGui.ListBox(label, ref _currentItem, _items, _itemCount)) + { + //selected = _currentItem; + _editingID = null; + hasChanged = true; + } + } + return hasChanged; + } } \ No newline at end of file