From 20d2e7b9c87c004c62b7a8b9fe38dded142d9dce Mon Sep 17 00:00:00 2001 From: Rampastring Date: Fri, 16 Feb 2024 00:17:49 +0200 Subject: [PATCH] Replace house allies text box with a graphical ally selector window --- .../UI/Windows/ConfigureAlliesWindow.ini | 24 +++++ .../Config/UI/Windows/HousesWindow.ini | 8 +- src/TSMapEditor/TSMapEditor.csproj | 3 + .../UI/Windows/ConfigureAlliesWindow.cs | 99 +++++++++++++++++++ src/TSMapEditor/UI/Windows/HousesWindow.cs | 30 ++++-- .../UI/Windows/WindowController.cs | 4 + 6 files changed, 156 insertions(+), 12 deletions(-) create mode 100644 src/TSMapEditor/Config/UI/Windows/ConfigureAlliesWindow.ini create mode 100644 src/TSMapEditor/UI/Windows/ConfigureAlliesWindow.cs diff --git a/src/TSMapEditor/Config/UI/Windows/ConfigureAlliesWindow.ini b/src/TSMapEditor/Config/UI/Windows/ConfigureAlliesWindow.ini new file mode 100644 index 00000000..c33143f9 --- /dev/null +++ b/src/TSMapEditor/Config/UI/Windows/ConfigureAlliesWindow.ini @@ -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 diff --git a/src/TSMapEditor/Config/UI/Windows/HousesWindow.ini b/src/TSMapEditor/Config/UI/Windows/HousesWindow.ini index 673584dd..7c22e42e 100644 --- a/src/TSMapEditor/Config/UI/Windows/HousesWindow.ini +++ b/src/TSMapEditor/Config/UI/Windows/HousesWindow.ini @@ -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 @@ -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) diff --git a/src/TSMapEditor/TSMapEditor.csproj b/src/TSMapEditor/TSMapEditor.csproj index 09728b46..3e294ea3 100644 --- a/src/TSMapEditor/TSMapEditor.csproj +++ b/src/TSMapEditor/TSMapEditor.csproj @@ -125,6 +125,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/src/TSMapEditor/UI/Windows/ConfigureAlliesWindow.cs b/src/TSMapEditor/UI/Windows/ConfigureAlliesWindow.cs new file mode 100644 index 00000000..37ea6214 --- /dev/null +++ b/src/TSMapEditor/UI/Windows/ConfigureAlliesWindow.cs @@ -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 checkBoxes = new List(); + + private House house; + + public override void Initialize() + { + Name = nameof(ConfigureAlliesWindow); + base.Initialize(); + + panelCheckBoxes = FindChild(nameof(panelCheckBoxes)); + + btnApply = FindChild("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(); + } + } +} diff --git a/src/TSMapEditor/UI/Windows/HousesWindow.cs b/src/TSMapEditor/UI/Windows/HousesWindow.cs index 513158cb..570a8e83 100644 --- a/src/TSMapEditor/UI/Windows/HousesWindow.cs +++ b/src/TSMapEditor/UI/Windows/HousesWindow.cs @@ -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; @@ -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() { @@ -62,7 +63,7 @@ public override void Initialize() ddColor = FindChild(nameof(ddColor)); ddTechnologyLevel = FindChild(nameof(ddTechnologyLevel)); ddPercentBuilt = FindChild(nameof(ddPercentBuilt)); - tbAllies = FindChild(nameof(tbAllies)); + selAllies = FindChild(nameof(selAllies)); tbMoney = FindChild(nameof(tbMoney)); chkPlayerControl = FindChild(nameof(chkPlayerControl)); @@ -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"; @@ -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; @@ -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; @@ -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; @@ -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; } @@ -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(); } @@ -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) diff --git a/src/TSMapEditor/UI/Windows/WindowController.cs b/src/TSMapEditor/UI/Windows/WindowController.cs index 117415f2..d5afd2de 100644 --- a/src/TSMapEditor/UI/Windows/WindowController.cs +++ b/src/TSMapEditor/UI/Windows/WindowController.cs @@ -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; @@ -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);