Skip to content

Commit

Permalink
Replace house allies text box with a graphical ally selector window
Browse files Browse the repository at this point in the history
  • Loading branch information
Rampastring committed Feb 15, 2024
1 parent 53b8cce commit 20d2e7b
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 12 deletions.
24 changes: 24 additions & 0 deletions src/TSMapEditor/Config/UI/Windows/ConfigureAlliesWindow.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[ConfigureAlliesWindow]
$CC0=lblDescription:XNALabel
$Width=getRight(lblDescription) + 40
$CC1=panelCheckBoxes:XNAPanel
$CC2=btnApply:EditorButton
HasCloseButton=yes

[lblDescription]
$X=EMPTY_SPACE_SIDES
$Y=EMPTY_SPACE_TOP
Text=Select which houses this house is allied to.

[panelCheckBoxes]
$X=EMPTY_SPACE_SIDES
$Y=getBottom(lblDescription) + VERTICAL_SPACING
$Width=getWidth(ConfigureAlliesWindow) - (EMPTY_SPACE_SIDES * 2)
$Height=0 ; placeholder, dynamically generated
DrawBorders=no

[btnApply]
$Width=100
$X=horizontalCenterOnParent()
$Y=0 ; placeholder, dynamically generated
Text=Apply
8 changes: 4 additions & 4 deletions src/TSMapEditor/Config/UI/Windows/HousesWindow.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $CC22=ddTechnologyLevel:XNADropDown
$CC23=lblTechnologyLevel:XNALabel
$CC24=ddPercentBuilt:XNADropDown
$CC25=lblPercentBuilt:XNALabel
$CC26=tbAllies:EditorTextBox
$CC26=selAllies:EditorPopUpSelector
$CC27=lblAllies:XNALabel
$CC28=tbMoney:EditorNumberTextBox
$CC29=lblMoney:XNALabel
Expand Down Expand Up @@ -204,20 +204,20 @@ $X=getX(lblTechnologyLevel)
$Y=getY(ddPercentBuilt) + 1
Text=Percent Built (%):
[tbAllies]
[selAllies]
$X=getX(ddTechnologyLevel)
$Width=getWidth(ddTechnologyLevel)
$Y=getBottom(ddPercentBuilt) + VERTICAL_SPACING
[lblAllies]
$X=getX(lblTechnologyLevel)
$Y=getY(tbAllies) + 1
$Y=getY(selAllies) + 1
Text=Allies:
[tbMoney]
$X=getX(ddTechnologyLevel)
$Width=getWidth(ddTechnologyLevel)
$Y=getBottom(tbAllies) + VERTICAL_SPACING
$Y=getBottom(selAllies) + VERTICAL_SPACING
[lblMoney]
$X=getX(lblTechnologyLevel)
Expand Down
3 changes: 3 additions & 0 deletions src/TSMapEditor/TSMapEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@
<None Update="Config\UI\Windows\ChangeHeightWindow.ini">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Config\UI\Windows\ConfigureAlliesWindow.ini">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Config\UI\Windows\CopiedEntryTypesWindow.ini">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
99 changes: 99 additions & 0 deletions src/TSMapEditor/UI/Windows/ConfigureAlliesWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using Rampastring.XNAUI;
using Rampastring.XNAUI.XNAControls;
using System;
using System.Collections.Generic;
using System.Linq;
using TSMapEditor.Models;
using TSMapEditor.UI.Controls;

namespace TSMapEditor.UI.Windows
{
public class ConfigureAlliesWindow : INItializableWindow
{
public ConfigureAlliesWindow(WindowManager windowManager, Map map) : base(windowManager)
{
this.map = map;
}

public event EventHandler AlliesUpdated;

private readonly Map map;

private XNAPanel panelCheckBoxes;
private EditorButton btnApply;

private List<XNACheckBox> checkBoxes = new List<XNACheckBox>();

private House house;

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

panelCheckBoxes = FindChild<XNAPanel>(nameof(panelCheckBoxes));

btnApply = FindChild<EditorButton>("btnApply");
btnApply.LeftClick += BtnApply_LeftClick;
}

private void BtnApply_LeftClick(object sender, EventArgs e)
{
string allies = string.Join(',', new string[] { house.ININame }.Concat(checkBoxes.FindAll(chk => chk.Checked).Select(chk => chk.Text)));
house.Allies = allies;

AlliesUpdated?.Invoke(this, EventArgs.Empty);

Hide();
}

public void Open(House house)
{
this.house = house;

RefreshCheckBoxes();

Show();
}

private void RefreshCheckBoxes()
{
checkBoxes.ForEach(chk => panelCheckBoxes.RemoveChild(chk));
checkBoxes.Clear();

string[] existingAllies = house.Allies.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

int y = 0;

bool useTwoColumns = map.Houses.Count > 16;
bool isSecondColumn = false;

foreach (var otherHouse in map.Houses)
{
if (otherHouse == house)
continue;

var checkBox = new XNACheckBox(WindowManager);
checkBox.Name = "chk" + otherHouse.ININame;
checkBox.X = isSecondColumn ? 150 : 0;
checkBox.Y = y;
checkBox.Text = otherHouse.ININame;
checkBox.Checked = Array.Exists(existingAllies, s => s == otherHouse.ININame);
panelCheckBoxes.AddChild(checkBox);
checkBoxes.Add(checkBox);

if (!useTwoColumns || isSecondColumn)
y = checkBox.Bottom + Constants.UIVerticalSpacing;

if (useTwoColumns)
isSecondColumn = !isSecondColumn;
}

panelCheckBoxes.Height = y - Constants.UIVerticalSpacing;
btnApply.Y = panelCheckBoxes.Bottom + Constants.UIEmptyTopSpace;
Height = btnApply.Bottom + Constants.UIEmptyBottomSpace;

CenterOnParent();
}
}
}
30 changes: 22 additions & 8 deletions src/TSMapEditor/UI/Windows/HousesWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public HousesWindow(WindowManager windowManager, Map map) : base(windowManager)
private XNADropDown ddColor;
private XNADropDown ddTechnologyLevel;
private XNADropDown ddPercentBuilt;
private EditorTextBox tbAllies;
private EditorPopUpSelector selAllies;
private EditorNumberTextBox tbMoney;
private XNACheckBox chkPlayerControl;

Expand All @@ -45,6 +45,7 @@ public HousesWindow(WindowManager windowManager, Map map) : base(windowManager)
private GenerateStandardHousesWindow generateStandardHousesWindow;
private EditHouseTypeWindow editHouseTypeWindow;
private NewHouseWindow newHouseWindow;
private ConfigureAlliesWindow configureAlliesWindow;

public override void Initialize()
{
Expand All @@ -62,7 +63,7 @@ public override void Initialize()
ddColor = FindChild<XNADropDown>(nameof(ddColor));
ddTechnologyLevel = FindChild<XNADropDown>(nameof(ddTechnologyLevel));
ddPercentBuilt = FindChild<XNADropDown>(nameof(ddPercentBuilt));
tbAllies = FindChild<EditorTextBox>(nameof(tbAllies));
selAllies = FindChild<EditorPopUpSelector>(nameof(selAllies));
tbMoney = FindChild<EditorNumberTextBox>(nameof(tbMoney));
chkPlayerControl = FindChild<XNACheckBox>(nameof(chkPlayerControl));

Expand Down Expand Up @@ -100,6 +101,10 @@ public override void Initialize()
var editHouseTypeWindowDarkeningPanel = DarkeningPanel.InitializeAndAddToParentControlWithChild(WindowManager, Parent, editHouseTypeWindow);
editHouseTypeWindowDarkeningPanel.Hidden += (s, e) => RefreshHouseInfo();

configureAlliesWindow = new ConfigureAlliesWindow(WindowManager, map);
var configureAlliesWindowDarkeningPanel = DarkeningPanel.InitializeAndAddToParentControlWithChild(WindowManager, Parent, configureAlliesWindow);
configureAlliesWindow.AlliesUpdated += (s, e) => RefreshHouseInfo();

if (Constants.UseCountries)
{
btnEditHouseType.Text = "Edit Country";
Expand Down Expand Up @@ -266,7 +271,7 @@ private void RefreshHouseInfo()
ddColor.SelectedIndexChanged -= DdColor_SelectedIndexChanged;
ddTechnologyLevel.SelectedIndexChanged -= DdTechnologyLevel_SelectedIndexChanged;
ddPercentBuilt.SelectedIndexChanged -= DdPercentBuilt_SelectedIndexChanged;
tbAllies.TextChanged -= TbAllies_TextChanged;
selAllies.LeftClick -= SelAllies_LeftClick;
tbMoney.TextChanged -= TbMoney_TextChanged;
chkPlayerControl.CheckedChanged -= ChkPlayerControl_CheckedChanged;

Expand All @@ -281,7 +286,7 @@ private void RefreshHouseInfo()
ddColor.SelectedIndex = -1;
ddTechnologyLevel.SelectedIndex = -1;
ddPercentBuilt.SelectedIndex = -1;
tbAllies.Text = string.Empty;
selAllies.Text = string.Empty;
tbMoney.Text = string.Empty;
chkPlayerControl.Checked = false;
lblStatsValue.Text = string.Empty;
Expand All @@ -307,7 +312,7 @@ private void RefreshHouseInfo()
ddColor.SelectedIndex = ddColor.Items.FindIndex(item => item.Text == editedHouse.Color);
ddTechnologyLevel.SelectedIndex = ddTechnologyLevel.Items.FindIndex(item => Conversions.IntFromString(item.Text, -1) == editedHouse.TechLevel);
ddPercentBuilt.SelectedIndex = ddPercentBuilt.Items.FindIndex(item => Conversions.IntFromString(item.Text, -1) == editedHouse.PercentBuilt);
tbAllies.Text = editedHouse.Allies ?? "";
selAllies.Text = editedHouse.Allies ?? "";
tbMoney.Value = editedHouse.Credits;
chkPlayerControl.Checked = editedHouse.PlayerControl;

Expand All @@ -320,7 +325,7 @@ private void RefreshHouseInfo()
ddColor.SelectedIndexChanged += DdColor_SelectedIndexChanged;
ddTechnologyLevel.SelectedIndexChanged += DdTechnologyLevel_SelectedIndexChanged;
ddPercentBuilt.SelectedIndexChanged += DdPercentBuilt_SelectedIndexChanged;
tbAllies.TextChanged += TbAllies_TextChanged;
selAllies.LeftClick += SelAllies_LeftClick;
tbMoney.TextChanged += TbMoney_TextChanged;
chkPlayerControl.CheckedChanged += ChkPlayerControl_CheckedChanged;
}
Expand All @@ -332,6 +337,15 @@ private void TbName_TextChanged(object sender, System.EventArgs e)
if (!Constants.UseCountries)
editedHouse.HouseType.ININame = editedHouse.ININame;

if (!string.IsNullOrWhiteSpace(editedHouse.ININame))
{
editedHouse.Allies = string.Join(',',
new string[] { editedHouse.ININame }
.Concat(editedHouse.Allies.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[1..]));

selAllies.Text = editedHouse.Allies;
}

ListHouses();
}

Expand Down Expand Up @@ -393,9 +407,9 @@ private void DdPercentBuilt_SelectedIndexChanged(object sender, System.EventArgs
editedHouse.PercentBuilt = Conversions.IntFromString(ddPercentBuilt.SelectedItem.Text, 100);
}

private void TbAllies_TextChanged(object sender, System.EventArgs e)
private void SelAllies_LeftClick(object sender, EventArgs e)
{
editedHouse.Allies = tbAllies.Text;
configureAlliesWindow.Open(editedHouse);
}

private void TbMoney_TextChanged(object sender, System.EventArgs e)
Expand Down
4 changes: 4 additions & 0 deletions src/TSMapEditor/UI/Windows/WindowController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class WindowController
public FindWaypointWindow FindWaypointWindow { get; private set; }
public DeletionModeConfigurationWindow DeletionModeConfigurationWindow { get; private set; }
public RenderedObjectsConfigurationWindow RenderedObjectsConfigurationWindow { get; private set; }
public ConfigureAlliesWindow ConfigureAlliesWindow { get; private set; }
public AboutWindow AboutWindow { get; private set; }

private IWindowParentControl windowParentControl;
Expand Down Expand Up @@ -193,6 +194,9 @@ public void Initialize(IWindowParentControl windowParentControl, Map map, Editor
RenderedObjectsConfigurationWindow = new RenderedObjectsConfigurationWindow(windowParentControl.WindowManager, editorState);
Windows.Add(RenderedObjectsConfigurationWindow);

ConfigureAlliesWindow = new ConfigureAlliesWindow(windowParentControl.WindowManager, map);
Windows.Add(ConfigureAlliesWindow);

AboutWindow = new AboutWindow(windowParentControl.WindowManager);
Windows.Add(AboutWindow);

Expand Down

0 comments on commit 20d2e7b

Please sign in to comment.