Skip to content

Commit

Permalink
Add a separate dropdown for BuildingWithProperty property
Browse files Browse the repository at this point in the history
  • Loading branch information
MortonPL authored Feb 25, 2024
1 parent 5d74aeb commit cfd3bef
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 52 deletions.
15 changes: 15 additions & 0 deletions src/TSMapEditor/CCEngine/BuildingWithPropertyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,19 @@ public enum BuildingWithPropertyType
Farthest = 0x30000,
Invalid = 0x40000
}

public static class BuildingWithPropertyTypeExtension
{
public static string ToDescription(this BuildingWithPropertyType value)
{
return value switch
{
BuildingWithPropertyType.LeastThreat => "Least threat",
BuildingWithPropertyType.HighestThreat => "Highest threat",
BuildingWithPropertyType.Nearest => "Nearest",
BuildingWithPropertyType.Farthest => "Farthest",
_ => string.Empty,
};
}
}
}
15 changes: 10 additions & 5 deletions src/TSMapEditor/Config/UI/Windows/SelectBuildingTargetWindow.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
$Width=350
$Height=RESOLUTION_HEIGHT - 100
$CC0=lblDescription:XNALabel
$CC1=tbSearch:EditorSuggestionTextBox
$CC1=ddTarget:XNADropDown
$CC2=btnSelect:EditorButton
$CC3=lbObjectList:EditorListBox
$CC3=tbSearch:EditorSuggestionTextBox
$CC4=lbObjectList:EditorListBox


[lblDescription]
Expand All @@ -13,10 +14,15 @@ $Y=EMPTY_SPACE_TOP
FontIndex=1
Text=Select target:

[tbSearch]
[ddTarget]
$X=EMPTY_SPACE_SIDES
$Y=getBottom(lblDescription) + EMPTY_SPACE_TOP
$Width=getWidth(SelectBuildingTargetWindow) - (EMPTY_SPACE_SIDES * 2)

[tbSearch]
$X=EMPTY_SPACE_SIDES
$Y=getBottom(ddTarget) + VERTICAL_SPACING
$Width=getWidth(ddTarget)
Suggestion=Search target...

[btnSelect]
Expand All @@ -28,6 +34,5 @@ Text=Select
[lbObjectList]
$X=EMPTY_SPACE_SIDES
$Y=getBottom(tbSearch) + VERTICAL_SPACING
$Width=getWidth(tbSearch)
$Width=getWidth(ddTarget)
$Height=getY(btnSelect) - getY(lbObjectList) - EMPTY_SPACE_TOP

57 changes: 26 additions & 31 deletions src/TSMapEditor/UI/Windows/ScriptsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void BuildingTargetWindowDarkeningPanel_Hidden(object sender, EventArgs

if (selectBuildingTargetWindow.SelectedObject > -1)
{
tbParameterValue.Text = GetBuildingWithPropertyText(selectBuildingTargetWindow.SelectedObject);
tbParameterValue.Text = GetBuildingWithPropertyText(selectBuildingTargetWindow.SelectedObject, selectBuildingTargetWindow.Property);
}
}

Expand Down Expand Up @@ -207,7 +207,8 @@ private void BtnEditorPresetValues_LeftClick(object sender, EventArgs e)
}
else if (action.ParamType == TriggerParamType.BuildingWithProperty)
{
selectBuildingTargetWindow.Open(entry.Argument);
var (index, property) = SplitBuildingWithProperty(entry.Argument);
selectBuildingTargetWindow.Open(index, property);
}
}

Expand Down Expand Up @@ -370,40 +371,34 @@ private void SetParameterEntryText(ScriptActionEntry scriptActionEntry, ScriptAc
}
}

private string GetBuildingWithPropertyText(int argument)
private static Tuple<int, BuildingWithPropertyType> SplitBuildingWithProperty(int argument)
{
string description = "";
BuildingType buildingType = null;

if (argument < (int)BuildingWithPropertyType.HighestThreat) // range 0x00000 - 0x0FFFF
{
buildingType = map.Rules.BuildingTypes.GetElementIfInRange(argument);
description = "Least threat";
}
else if (argument < (int)BuildingWithPropertyType.Nearest) // range 0x10000 - 0x1FFFF
{
buildingType = map.Rules.BuildingTypes.GetElementIfInRange(argument - (int)BuildingWithPropertyType.HighestThreat);
description = "Highest threat";
}
else if (argument < (int)BuildingWithPropertyType.Farthest) // range 0x20000 - 0x2FFFF
{
buildingType = map.Rules.BuildingTypes.GetElementIfInRange(argument - (int)BuildingWithPropertyType.Nearest);
description = "Nearest";
}
else if (argument < (int)BuildingWithPropertyType.Invalid) // range 0x30000 - 0x3FFFF
var property = argument switch
{
buildingType = map.Rules.BuildingTypes.GetElementIfInRange(argument - (int)BuildingWithPropertyType.Farthest);
description = "Farthest";
}
else
{
description = "";
}
< (int)BuildingWithPropertyType.HighestThreat => BuildingWithPropertyType.LeastThreat,
< (int)BuildingWithPropertyType.Nearest => BuildingWithPropertyType.HighestThreat,
< (int)BuildingWithPropertyType.Farthest => BuildingWithPropertyType.Nearest,
_ => BuildingWithPropertyType.Farthest,
};
return new(argument - (int)property, property);
}

private string GetBuildingWithPropertyText(int buildingTypeIndex, BuildingWithPropertyType property)
{
string description = property.ToDescription();
BuildingType buildingType = map.Rules.BuildingTypes.GetElementIfInRange(buildingTypeIndex);
int value = buildingTypeIndex + (int)property;

if (buildingType == null)
return argument + " - invalid value";
return value + " - invalid value";

return argument + " - " + buildingType.GetEditorDisplayName() + " (" + description + ")";
return value + " - " + buildingType.GetEditorDisplayName() + " (" + description + ")";
}

private string GetBuildingWithPropertyText(int argument)
{
var (index, property) = SplitBuildingWithProperty(argument);
return GetBuildingWithPropertyText(index, property);
}

private void FillPresetContextMenu(ScriptActionEntry entry, ScriptAction action)
Expand Down
41 changes: 25 additions & 16 deletions src/TSMapEditor/UI/Windows/SelectBuildingTargetWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,22 @@ public SelectBuildingTargetWindow(WindowManager windowManager, Map map) : base(w

private readonly Map map;

private XNADropDown ddTarget;

public BuildingWithPropertyType Property { get; private set; }

public override void Initialize()
{
Name = nameof(SelectBuildingTargetWindow);
base.Initialize();

ddTarget = FindChild<XNADropDown>(nameof(ddTarget));
ddTarget.AddItem(new XNADropDownItem { Text = BuildingWithPropertyType.LeastThreat.ToDescription(), Tag = BuildingWithPropertyType.LeastThreat });
ddTarget.AddItem(new XNADropDownItem { Text = BuildingWithPropertyType.HighestThreat.ToDescription(), Tag = BuildingWithPropertyType.HighestThreat });
ddTarget.AddItem(new XNADropDownItem { Text = BuildingWithPropertyType.Nearest.ToDescription(), Tag = BuildingWithPropertyType.Nearest });
ddTarget.AddItem(new XNADropDownItem { Text = BuildingWithPropertyType.Farthest.ToDescription(), Tag = BuildingWithPropertyType.Farthest });
ddTarget.SelectedIndexChanged += DdTarget_SelectedIndexChanged;
ddTarget.SelectedIndex = 0;
}

protected override void LbObjectList_SelectedIndexChanged(object sender, EventArgs e)
Expand All @@ -33,32 +45,29 @@ protected override void LbObjectList_SelectedIndexChanged(object sender, EventAr
SelectedObject = (int)lbObjectList.SelectedItem.Tag;
}

protected void DdTarget_SelectedIndexChanged(object sender, EventArgs e)
{
Property = (BuildingWithPropertyType)ddTarget.SelectedItem?.Tag;
}

protected override void ListObjects()
{
lbObjectList.Clear();

bool useDifferentColor = false;

foreach (BuildingType buildingType in map.Rules.BuildingTypes)
{
AddItem(buildingType, "Least threat", (int)BuildingWithPropertyType.LeastThreat, useDifferentColor);
AddItem(buildingType, "Highest threat", (int)BuildingWithPropertyType.HighestThreat, useDifferentColor);
AddItem(buildingType, "Nearest", (int)BuildingWithPropertyType.Nearest, useDifferentColor);
AddItem(buildingType, "Farthest", (int)BuildingWithPropertyType.Farthest, useDifferentColor);
int number = buildingType.Index;
lbObjectList.AddItem(new XNAListBoxItem() { Text = $"{number} {buildingType.GetEditorDisplayName()}", Tag = number });

useDifferentColor = !useDifferentColor;
if (SelectedObject == number)
lbObjectList.SelectedIndex = lbObjectList.Items.Count - 1;
}
}

private void AddItem(BuildingType buildingType, string description, int targetTypeNumber, bool useDifferentColor)
public void Open(int buildingTypeIndex, BuildingWithPropertyType property)
{
Color color = useDifferentColor ? lbObjectList.DefaultItemColor * 0.7f : lbObjectList.DefaultItemColor;

int number = buildingType.Index + targetTypeNumber;
lbObjectList.AddItem(new XNAListBoxItem() { Text = $"{number} {buildingType.GetEditorDisplayName()} ({description})", Tag = number, TextColor = color });

if (SelectedObject == number)
lbObjectList.SelectedIndex = lbObjectList.Items.Count - 1;
Open(buildingTypeIndex);
Property = property;
}
}
}

0 comments on commit cfd3bef

Please sign in to comment.