Skip to content

Commit

Permalink
Editor tweaks with lists of things.
Browse files Browse the repository at this point in the history
  • Loading branch information
se5a committed May 13, 2024
1 parent 0d0ff3b commit b9de572
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 30 deletions.
54 changes: 37 additions & 17 deletions Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.JavaScript;
using ImGuiNET;
using ImGuiSDL2CS;
using Pulsar4X.Blueprints;
using Pulsar4X.DataStructures;
using Pulsar4X.Engine;
using Pulsar4X.Modding;

namespace Pulsar4X.SDL2UI.ModFileEditing;

Expand All @@ -14,16 +18,28 @@ public abstract class BluePrintsUI
private protected string[] _itemNames;
private protected Blueprint[] _itemBlueprints;
private protected bool[] _isActive;

private protected ModDataStore _modDataStore;
private protected string[] _cargoTypes;
private protected string[] _techCatTypes;
private protected string[] _techTypes;
protected BluePrintsUI(ModDataStore modDataStore)
{
_modDataStore = modDataStore;
_cargoTypes = modDataStore.CargoTypes.Keys.ToArray();
_techCatTypes = modDataStore.TechCategories.Keys.ToArray();
_techTypes = modDataStore.Techs.Keys.ToArray();
}

public abstract void Display();
}

public class TechCatBlueprintUI : BluePrintsUI
{


public TechCatBlueprintUI(Dictionary<string, TechCategoryBlueprint> blueprints)
public TechCatBlueprintUI(ModDataStore modDataStore) : base(modDataStore)
{
Dictionary<string, TechCategoryBlueprint> blueprints = _modDataStore.TechCategories;

_itemNames = new string[blueprints.Count];
_itemBlueprints = new Blueprint[blueprints.Count];
_isActive = new bool[blueprints.Count];
Expand All @@ -35,8 +51,6 @@ public TechCatBlueprintUI(Dictionary<string, TechCategoryBlueprint> blueprints)
_isActive[i] = false;
i++;
}


}

public override void Display()
Expand Down Expand Up @@ -96,8 +110,11 @@ private void DisplayEditorWindow(int selectedIndex)

public class TechBlueprintUI : BluePrintsUI
{
public TechBlueprintUI(IDictionary<string, TechBlueprint> blueprints)
private int _selectedIndex = -1;
public TechBlueprintUI(ModDataStore modDataStore) : base(modDataStore)
{
var blueprints = modDataStore.Techs;

_itemNames = new string[blueprints.Count];
_itemBlueprints = new Blueprint[blueprints.Count];
_isActive = new bool[blueprints.Count];
Expand Down Expand Up @@ -139,11 +156,11 @@ public void DisplayEditorWindow(int selectedIndex)
var selectedItem = (TechBlueprint)_itemBlueprints[selectedIndex];
string name = selectedItem.Name;
string editStr;
if (ImGui.Begin("Tech Category Editor: " + name))
if (ImGui.Begin("Tech Editor: " + name))
{
ImGui.Columns(2);
ImGui.SetColumnWidth(0,100);
ImGui.SetColumnWidth(1,300);
ImGui.SetColumnWidth(1,500);
ImGui.Text("Name: ");
ImGui.NextColumn();

Expand All @@ -167,10 +184,12 @@ public void DisplayEditorWindow(int selectedIndex)

ImGui.Text("Category: ");
ImGui.NextColumn();
editStr = selectedItem.Category;
if (TextEditWidget.Display("##cat" + selectedItem.Category, ref editStr))

_selectedIndex = Array.IndexOf(_techCatTypes, selectedItem.Category);
if (SelectFromListWiget.Display("##cat" + selectedItem.Category, _techCatTypes, ref _selectedIndex))
{
selectedItem.Category = editStr;
selectedItem.Category = _techCatTypes[_selectedIndex];
_selectedIndex = -1;
}
ImGui.NextColumn();

Expand Down Expand Up @@ -207,7 +226,7 @@ public void DisplayEditorWindow(int selectedIndex)
ImGui.Text("Unlocks: ");
ImGui.NextColumn();
var editDic = selectedItem.Unlocks;
if (DictEditWidget.Display("##ul" + selectedItem.Name, ref editDic))
if (DictEditWidget.Display("##ul" + selectedItem.Name, ref editDic, _techTypes))
{

}
Expand All @@ -221,12 +240,12 @@ public void DisplayEditorWindow(int selectedIndex)

public class ComponentBluprintUI : BluePrintsUI
{
private string[] _cargoTypes;
public ComponentBluprintUI(Dictionary<string, ComponentTemplateBlueprint> blueprints, string[] cargoTypes )

public ComponentBluprintUI(ModDataStore modDataStore) : base(modDataStore)
{
Dictionary<string, ComponentTemplateBlueprint> blueprints = modDataStore.ComponentTemplates;
_itemNames = new string[blueprints.Count];
_itemBlueprints = new Blueprint[blueprints.Count];
_cargoTypes = cargoTypes;
_isActive = new bool[blueprints.Count];
int i = 0;
foreach (var kvp in blueprints)
Expand Down Expand Up @@ -258,6 +277,7 @@ public override void Display()
}
}

private int editIndex = 0;
public void DisplayEditorWindow(int selectedIndex)
{

Expand Down Expand Up @@ -296,9 +316,9 @@ public void DisplayEditorWindow(int selectedIndex)
ImGui.Text("CargoType: ");
ImGui.NextColumn();
editStr = selectedItem.CargoTypeID;
if (SelectFromListWiget.Display("##cgt" + selectedItem.CargoTypeID, _cargoTypes, ref editStr))
if (SelectFromListWiget.Display("##cgt" + selectedItem.CargoTypeID, _cargoTypes, ref editIndex))
{
selectedItem.Name = editStr;
selectedItem.Name = _cargoTypes[editIndex];
}

ImGui.NextColumn();
Expand Down
56 changes: 47 additions & 9 deletions Pulsar4X/Pulsar4X.Client/ModFileEditing/EditorWidgets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,20 @@ public static bool Display(string label, ref int num)

public static class DictEditWidget
{
private static string _editingID;
private static string? _editingID;
private static int _editInt;
private static string _editStr;

public static bool Display(string label, ref Dictionary<int, List<string>> dict)
private static uint _buffSize = 128;
private static byte[] _strInputBuffer = new byte[128];
private static int _techIndex = 0;

public static bool Display(string label, ref Dictionary<int, List<string>> dict, string[] techs)
{
ImGui.BeginChild("##dic");
ImGui.Columns(2);
bool isChanged = false;
int addnum = -1;
foreach (var kvp in dict)
{
_editInt = kvp.Key;
Expand All @@ -104,11 +109,40 @@ public static bool Display(string label, ref Dictionary<int, List<string>> dict)

}
}
if(_editingID != label+"addValue")
{
if (ImGui.Button("+"))
{
_editingID = label+"addValue";
}
}
else
{
if (SelectFromListWiget.Display(label+"addValue", techs, ref _techIndex))
{
dict[kvp.Key].Add(techs[_techIndex]);
}
}
ImGui.NextColumn();
if(_editingID != label+"addKey")
{
if (ImGui.Button("+"))
{
_editingID = label+"addKey";
}
}
else
{
addnum = dict.Keys.Count;
while (dict.ContainsKey(addnum))
addnum++;
_editingID = null;
}
}
if(addnum > -1) //do this here so we don't add in the middle of foreach
dict.Add(addnum, new List<string>());

ImGui.EndChild();

return isChanged;
}

Expand Down Expand Up @@ -146,17 +180,20 @@ public static bool Display(string label, ref Dictionary<string, string> dict)

public static class SelectFromListWiget
{
private static string _editingID;
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 string selected)
public static bool Display(string label, string[] selectFrom, ref int selected)
{
bool hasChanged = false;
string displayText = "";
if(selected > -1)
displayText = selectFrom[selected];
if (label != _editingID)
{
ImGui.Text(selected);
ImGui.Text(displayText);
if(ImGui.IsItemClicked())
{
_editingID = label;
Expand All @@ -166,14 +203,15 @@ public static bool Display(string label, string[] selectFrom, ref string selecte
}
else
{
ImGui.Text(selected);
ImGui.Text(displayText);
ImGui.SameLine();
if (ImGui.ListBox(label, ref _currentItem, _items, _itemCount))
{
selected = _items[_currentItem];
selected = _currentItem;
_editingID = null;
hasChanged = true;
}
}

return hasChanged;
}
}
7 changes: 3 additions & 4 deletions Pulsar4X/Pulsar4X.Client/ModFileEditing/ModFileEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ void refresh()
ModDataStore modDataStore = new ModDataStore();
modLoader.LoadModManifest("Data/basemod/modInfo.json", modDataStore);

_techCatBlueprintUI = new TechCatBlueprintUI(modDataStore.TechCategories);
_techBlueprintUI = new TechBlueprintUI(modDataStore.Techs);
_techCatBlueprintUI = new TechCatBlueprintUI(modDataStore);
_techBlueprintUI = new TechBlueprintUI(modDataStore);
_componentBluprintUI = new ComponentBluprintUI(modDataStore);

string[] cargoTypes = modDataStore.CargoTypes.Keys.ToArray();
_componentBluprintUI = new ComponentBluprintUI(_uiState.Game.StartingGameData.ComponentTemplates, cargoTypes);
}


Expand Down

0 comments on commit b9de572

Please sign in to comment.