Skip to content

Commit

Permalink
Some work on the Editor with ModInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
se5a committed Jun 23, 2024
1 parent b8000b6 commit 92c3507
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
17 changes: 14 additions & 3 deletions Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
12 changes: 5 additions & 7 deletions Pulsar4X/Pulsar4X.Client/ModFileEditing/ModFileEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand All @@ -50,7 +49,6 @@ void refresh()
_armorBlueprintUI = new ArmorBlueprintUI(modDataStore);
_processedMateralsUI = new ProcessedMateralsUI(modDataStore);
_mineralsBlueprintUI = new MineralBlueprintUI(modDataStore);

}


Expand Down
98 changes: 64 additions & 34 deletions Pulsar4X/Pulsar4X.Client/ModFileEditing/ModInfoUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Numerics;
using ImGuiNET;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pulsar4X.Modding;

Expand All @@ -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)
{
Expand Down Expand Up @@ -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);

Expand All @@ -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];
Expand All @@ -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)
Expand All @@ -113,6 +132,8 @@ public void DisplayEditorWindow(int selectedIndex)
if (TextEditWidget.Display("##name" + name, ref name))
{
selectedItem.ModName = name;
_itemNames[selectedIndex] = name;

}

ImGui.NextColumn();
Expand All @@ -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;
}
Expand All @@ -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<ModManifest>(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);
Expand Down
38 changes: 38 additions & 0 deletions Pulsar4X/Pulsar4X.Client/ModFileEditing/SelectFromListWiget.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using ImGuiNET;

namespace Pulsar4X.SDL2UI.ModFileEditing;
Expand Down Expand Up @@ -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<bool> 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;
}
}

0 comments on commit 92c3507

Please sign in to comment.