Skip to content

Commit

Permalink
Changes to ComponentDesign window.
Browse files Browse the repository at this point in the history
Changes to allow designed components to be reverse lookup and shown in the ComponentDesign window.
  • Loading branch information
se5a committed Dec 15, 2024
1 parent 894d1b0 commit a40559a
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 44 deletions.
7 changes: 6 additions & 1 deletion Pulsar4X/GameEngine/Engine/Components/ComponentDesign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class ComponentDesign : ICargoable, IConstructableDesign
public string Name { get; internal set; } //player defined name. ie "5t 2kn Thruster".
public string ComponentType { get; internal set; } = "";

public string TemplateName; //ie the name in staticData. ie "Newtonion Thruster".
public string TemplateID; //ie the UniqueID in staticData. ie "newtonion-thruster".
public List<(string atbName, Type valueType, object atbValue)> TemplateAttributes = new ();


public bool IsValid {get; set; } = true;

public string CargoTypeID { get; internal set; }
Expand All @@ -30,7 +35,7 @@ public class ComponentDesign : ICargoable, IConstructableDesign

public long ResearchCostValue;
public string TechID;
public string TypeName; //ie the name in staticData. ie "Newtonion Thruster".

public string Description;
//public int Volume_m3 = 1;
public int HTK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ public bool IsEnabled {
}
}

/// <summary>
/// this is the IComponentDesignAttribute type
/// </summary>
public Type AttributeType;

public Type EnumType;
public int ListSelection;
//public BaseDataBlob DataBlob;
internal ComponentDesigner ParentComponent;

public ComponentDesignAttribute(ComponentDesigner parentComponent, ComponentTemplateAttributeBlueprint templateAtb, FactionDataStore factionDataStore, FactionTechDB factionTech)
{
ParentComponent = parentComponent;
Expand Down
61 changes: 56 additions & 5 deletions Pulsar4X/GameEngine/Engine/Components/ComponentDesigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class ComponentDesigner
internal static bool StartResearched = false;
public ComponentDesigner(ComponentTemplateBlueprint componentSD, FactionDataStore factionDataStore, FactionTechDB factionTech, string? uniqueId = null)
{
TypeName = componentSD.Name;
TemplateName = componentSD.Name;
TemplateID = componentSD.UniqueID;
Name = componentSD.Name;
if(!string.IsNullOrEmpty( componentSD.ComponentType))
_design.ComponentType = componentSD.ComponentType;
Expand Down Expand Up @@ -62,7 +63,7 @@ public ComponentDesigner(ComponentTemplateBlueprint componentSD, FactionDataStor
if(factionDataStore.CargoGoods.GetAny(kvp.Key) != null)
resourceCostForulas.Add(kvp.Key, new ChainedExpression(kvp.Value, this, factionDataStore, factionTech));
else //TODO: log don't crash.
throw new Exception("GUID object {" + kvp.Key + "} not found in resourceCosting for " + this.TypeName + " This object needs to be either a mineral, material or component defined in the Data folder");
throw new Exception("GUID object {" + kvp.Key + "} not found in resourceCosting for " + this.TemplateName + " This object needs to be either a mineral, material or component defined in the Data folder");
}

ResourceCostFormulas = resourceCostForulas;
Expand Down Expand Up @@ -148,6 +149,50 @@ public ComponentDesign CreateDesign(Entity factionEntity)

SetAttributes();

foreach (var designAttribute in ComponentDesignAttributes.Values)
{
var type = designAttribute.AttributeType;
var hint = designAttribute.GuiHint;
switch (hint)
{
case GuiHint.GuiSelectionMaxMin:
{
_design.TemplateAttributes.Add((designAttribute.Name, typeof(double), designAttribute.Value));
break;
}
case GuiHint.GuiSelectionMaxMinInt:
{
_design.TemplateAttributes.Add((designAttribute.Name, typeof(int), designAttribute.Value));
break;
}
case GuiHint.GuiFuelTypeSelection:
{
_design.TemplateAttributes.Add((designAttribute.Name, typeof(string), designAttribute.ValueString));
break;
}
case GuiHint.GuiTextDisplay:
{
//ignore it, it's not player settable.
break;
}
case GuiHint.None:
{
//ignore it, it's not player settable.
break;
}
case 0:
{
//ignore it, it's not player settable.
break;
}
default:
{
_design.TemplateAttributes.Add((designAttribute.Name, typeof(string), designAttribute.ValueString));
break;
}
}

}
if(_design.ResearchCostValue == 0 || StartResearched)
{
tech.Level = tech.MaxLevel;
Expand Down Expand Up @@ -177,10 +222,16 @@ public void EvalAll()

}

public string TypeName
public string TemplateName
{
get { return _design.TemplateName; }
set { _design.TemplateName = value; }
}

public string TemplateID
{
get { return _design.TypeName; }
set { _design.TypeName = value; }
get { return _design.TemplateID; }
set { _design.TemplateID = value; }
}

public string Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@ public void SetTemplate(ComponentTemplateBlueprint template, GlobalUIState state
NoTemplateState = NoTemplateState.Created;
}

public void SetFromComponent(ComponentDesign component, GlobalUIState state)
{


var factionData = state.Faction.GetDataBlob<FactionInfoDB>().Data;
var factionTech = state.Faction.GetDataBlob<FactionTechDB>();
Template = factionData.ComponentTemplates[component.TemplateID];
_componentDesigner = new ComponentDesigner(Template, factionData, factionTech);

NoTemplateState = NoTemplateState.Created;

var templateAttributes = component.TemplateAttributes;
//_componentDesigner.Name = component.Name;
_nameInputBuffer = ImGuiSDL2CSHelper.BytesFromString(component.Name);
foreach (var attb in templateAttributes)
{
if (attb.atbValue is string)
{
_componentDesigner.ComponentDesignAttributes[attb.atbName].SetValueFromString((string)attb.atbValue);
}
else if (attb.atbValue is Int32 || attb.atbValue is float || attb.atbValue is double )
{
_componentDesigner.ComponentDesignAttributes[attb.atbName].SetValueFromInput((double)attb.atbValue);
}
}
}

internal void Display(GlobalUIState uiState)
{
if(Template == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void Display(this ComponentInstancesDB db, EntityState entityState
ImGui.TableNextColumn();
ImGui.Text(instance.Name);
AddContextMenu(instance, uiState);
DisplayHelpers.DescriptiveTooltip(instance.Name, instance.Design.TypeName, instance.Design.Description, null, true);
DisplayHelpers.DescriptiveTooltip(instance.Name, instance.Design.TemplateName, instance.Design.Description, null, true);
ImGui.TableNextColumn();
ImGui.Text(listPerDesign.Count.ToString());
ImGui.TableNextColumn();
Expand Down
139 changes: 105 additions & 34 deletions Pulsar4X/Pulsar4X.Client/Interface/Windows/ComponentDesignWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Pulsar4X.Engine;
using Pulsar4X.Blueprints;
using Pulsar4X.Client.Interface.Widgets;
using Pulsar4X.Components;
using Pulsar4X.Datablobs;
using Pulsar4X.Factions;

Expand All @@ -17,6 +18,14 @@ public class ComponentDesignWindow : PulsarGuiWindow
private static List<ComponentTemplateBlueprint> filteredTemplates = new ();
private static string[]? sortedGroupNames;
private static int selectedFilterIndex = 0;
private static ComponentTemplateBlueprint? selectedTemplate;


private static Dictionary<string, ComponentDesign> componentDesigns = new();
private static List<ComponentDesign> componentsOfType = new();
private static string[]? componentNames = new string[0];
private static ComponentDesign selectedComponent;

private ComponentDesignWindow() { }

internal static ComponentDesignWindow GetInstance()
Expand All @@ -38,6 +47,7 @@ internal static ComponentDesignWindow GetInstance()
Array.Copy(sortedTempGroupNames, 0, sortedGroupNames, 1, sortedTempGroupNames.Length);

filteredTemplates = new List<ComponentTemplateBlueprint>(templates);
componentDesigns = _uiState.Faction.GetDataBlob<FactionInfoDB>().ComponentDesigns.ToDictionary();
}
thisitem = (ComponentDesignWindow)_uiState.LoadedWindows[typeof(ComponentDesignWindow)];

Expand All @@ -51,56 +61,117 @@ internal override void Display()
if(Window.Begin("Component Designer", ref IsActive, _flags))
{
Vector2 windowContentSize = ImGui.GetContentRegionAvail();
var firstChildSize = new Vector2(windowContentSize.X * 0.15f, windowContentSize.Y);
var secondChildSize = new Vector2(windowContentSize.X * 0.15f, windowContentSize.Y);
var thirdChildSize = new Vector2(windowContentSize.X * 0.7f - (windowContentSize.X * 0.01f), windowContentSize.Y);

if(ImGui.BeginChild("ComponentDesignSelection", new Vector2(204f, windowContentSize.Y - 24f), true))
if(ImGui.BeginChild("ComponentDesignSelection", firstChildSize, true))
{
DisplayHelpers.Header("Select a Template",
"Component Templates act as a framework for designing components.\n\n" +
"Select a template and then design the attributes of the component to your specification.\n" +
"Once the design is created it will be available to produce on the colonies with the appropriate\n" +
"installations.");

var availableSize = ImGui.GetContentRegionAvail();
ImGui.SetNextItemWidth(availableSize.X);
if(ImGui.Combo("###template-filter", ref selectedFilterIndex, sortedGroupNames, sortedGroupNames?.Length ?? 0))
DisplayTemplateSelection();
ImGui.EndChild();
}
ImGui.SameLine();
if (ImGui.BeginChild("ComponentSelection", secondChildSize, true))
{
DisplayComponentList();
ImGui.EndChild();
}
ImGui.SameLine();
if (ImGui.BeginChild("ComponentDesign", thirdChildSize, true))
{
if(selectedTemplate != null)
{
if(selectedFilterIndex == 0)
{
filteredTemplates = new List<ComponentTemplateBlueprint>(templates);
}
else
ComponentDesignDisplay.GetInstance().Display(_uiState);
}
ImGui.EndChild();
}


ImGui.SameLine();
//ImGui.SetCursorPosY(27f); // FIXME: this should somehow be calculated


Window.End();
}
}

void DisplayTemplateSelection()
{
DisplayHelpers.Header("Select a Template",
"Component Templates act as a framework for designing components.\n\n" +
"Select a template and then design the attributes of the component to your specification.\n" +
"Once the design is created it will be available to produce on the colonies with the appropriate\n" +
"installations.");

var availableSize = ImGui.GetContentRegionAvail();
ImGui.SetNextItemWidth(availableSize.X);
if(ImGui.Combo("###template-filter", ref selectedFilterIndex, sortedGroupNames, sortedGroupNames?.Length ?? 0))
{
if(selectedFilterIndex == 0)
{
filteredTemplates = new List<ComponentTemplateBlueprint>(templates);
}
else
{
filteredTemplates = templates.Where(t => t.ComponentType.Equals(sortedGroupNames?[selectedFilterIndex])).ToList();
}
}

foreach(var template in filteredTemplates)
{
bool isSelected = selectedTemplate == template;
if (ImGui.Selectable(template.Name + "###component-" + template.UniqueID, isSelected))
{
selectedTemplate = template;
ComponentDesignDisplay.GetInstance().SetTemplate(selectedTemplate, _uiState);
componentsOfType = new List<ComponentDesign>();
foreach (var cd in componentDesigns)
{
if(cd.Value.TemplateName == template.Name)
{
filteredTemplates = templates.Where(t => t.ComponentType.Equals(sortedGroupNames?[selectedFilterIndex])).ToList();
componentsOfType.Add(cd.Value);
}
}

foreach(var template in filteredTemplates)
if(componentsOfType.Count > 0)
{
var selected = ComponentDesignDisplay.GetInstance().Template?.Name.Equals(template.Name);

if (ImGui.Selectable(template.Name + "###component-" + template.UniqueID, selected.HasValue && selected.Value))
componentNames = new string[componentsOfType.Count];
for (int c = 0; c < componentsOfType.Count; c++)
{
ComponentDesignDisplay.GetInstance().SetTemplate(template, _uiState);
componentNames[c] = componentsOfType[c].Name;
}
DisplayHelpers.DescriptiveTooltip(template.Name, template.ComponentType, template.Formulas["Description"]);
}

ImGui.EndChild();

}
DisplayHelpers.DescriptiveTooltip(template.Name, template.ComponentType, template.Formulas["Description"]);
}
}

ImGui.BeginDisabled();
if(ImGui.Button("Create Template", new Vector2(204f, 0f)))
{
void DisplayComponentList()
{
DisplayHelpers.Header("Current Component Designs of this type");

var availableSize = ImGui.GetContentRegionAvail();
ImGui.SetNextItemWidth(availableSize.X);
if (componentNames.Length > 0)
{
for (int index = 0; index < componentsOfType.Count; index++)
{
ComponentDesign? component = componentsOfType[index];
bool isSelected = componentsOfType[index] == component;
if (ImGui.Selectable(component.Name + "###component-" + component.UniqueID, isSelected))
{
ComponentDesignDisplay.GetInstance().SetFromComponent(component, _uiState);
}
}
ImGui.EndDisabled();

ImGui.SameLine();
ImGui.SetCursorPosY(27f); // FIXME: this should somehow be calculated
ComponentDesignDisplay.GetInstance().Display(_uiState);
}
ImGui.BeginDisabled();
if(ImGui.Button("Create Template", new Vector2(204f, 0f)))
{

Window.End();
}
ImGui.EndDisabled();

}

public override void OnGameTickChange(DateTime newDate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public static void Display(SensorProfileDB db)
{
ImGui.Text(component.Name);
ImGui.SameLine();
ImGui.Text(" ("+ component.Design.TypeName+")");
ImGui.Text(" ("+ component.Design.TemplateName+")");
SensorSignatureAtb emmitterAtbs = (SensorSignatureAtb)component.Design.AttributesByType[typeof(SensorSignatureAtb)];
DisplayValues(emmitterAtbs.PartWaveForm, emmitterAtbs.PartWaveFormMag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ internal void DisplayComponentsTable()
if (hovered)
{
selectedItem = i;
DisplayHelpers.DescriptiveTooltip(SelectedComponents[i].design.Name, SelectedComponents[i].design.TypeName, SelectedComponents[i].design.Description);
DisplayHelpers.DescriptiveTooltip(SelectedComponents[i].design.Name, SelectedComponents[i].design.TemplateName, SelectedComponents[i].design.Description);
}

ImGui.TableNextColumn();
Expand Down Expand Up @@ -571,7 +571,7 @@ void TooltipExtension()
ImGui.Text("Crew Required: " + AvailableShipComponents[i].CrewReq);
}

DisplayHelpers.DescriptiveTooltip(AvailableShipComponents[i].Name, AvailableShipComponents[i].TypeName, AvailableShipComponents[i].Description, TooltipExtension);
DisplayHelpers.DescriptiveTooltip(AvailableShipComponents[i].Name, AvailableShipComponents[i].TemplateName, AvailableShipComponents[i].Description, TooltipExtension);
}
ImGui.TableNextColumn();
ImGui.Text(design.ComponentType);
Expand Down

0 comments on commit a40559a

Please sign in to comment.