Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game lobby extra panel improvements #484

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ClientGUI/INItializableWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ static string Localize(XNAControl control, string attributeName, string defaultV
if (kvp.Value == "Disable")
control.LeftClick += (s, e) => Disable();
}
else if (kvp.Key == "$ToggleableControl" && control is XNAClientButton button)
{
button.SetToggleableControl(kvp.Value);
}
else
{
control.ParseINIAttribute(ConfigIni, kvp.Key, kvp.Value);
Expand Down
57 changes: 57 additions & 0 deletions ClientGUI/UIHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using Rampastring.XNAUI.XNAControls;

namespace ClientGUI;

/// <summary>
/// Contains helper methods for UI / UI control-related functionality
/// </summary>
public static class UIHelpers
{
/// <summary>
/// Finds a child control matching a name and optionally a type.
/// </summary>
/// <typeparam name="T">Type of the child control to find.</typeparam>
/// <param name="parent">Parent control.</param>
/// <param name="controlName">Name of the child control.</param>
/// <param name="recursive">Whether or not to look for children recursively.</param>
/// <returns>Child control matching the given name if found, otherwise type default value.</returns>
public static T FindMatchingChild<T>(XNAControl parent, string controlName, bool recursive = false)
{
if (parent == null || string.IsNullOrEmpty(controlName))
return default;

foreach (var child in parent.Children)
{
if (controlName.Equals(child.Name, StringComparison.Ordinal) && child is T returnValue)
{
return returnValue;
}
else if (recursive)
{
var match = FindMatchingChild<T>(child, controlName, recursive);

if (match != null && child is T)
return match;
}
}

return default;
}

/// <summary>
/// Finds control's parent window (instance of XNAWindow or INItializableWindow)
/// </summary>
/// <param name="control">Control to find the parent window for.</param>
/// <returns>Control's parent window if found, otherwise null</returns>
public static XNAControl FindParentWindow(XNAControl control)
{
if (control == null || control.Parent == null)
return null;

if (control.Parent is INItializableWindow or XNAWindow)
return control.Parent;

return FindParentWindow(control.Parent);
}
}
34 changes: 32 additions & 2 deletions ClientGUI/XNAClientButton.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Rampastring.XNAUI.XNAControls;
using Rampastring.XNAUI;
using Rampastring.Tools;
using System;
using ClientCore;
using ClientCore.Extensions;

namespace ClientGUI
Expand All @@ -11,6 +9,8 @@ public class XNAClientButton : XNAButton, IToolTipContainer
{
public ToolTip ToolTip { get; private set; }

public XNAControl ToggleableControl { get; set; }

private string _initialToolTipText;
public string ToolTipText
{
Expand Down Expand Up @@ -67,5 +67,35 @@ protected override void ParseControlINIAttribute(IniFile iniFile, string key, st

base.ParseControlINIAttribute(iniFile, key, value);
}

public void SetToggleableControl(string controlName)
{
if (!string.IsNullOrEmpty(controlName))
{
var parent = UIHelpers.FindParentWindow(this);

if (parent == null)
return;

ToggleableControl = UIHelpers.FindMatchingChild<XNAControl>(parent, controlName, true);
}
}

public override void OnLeftClick()
{
if (!AllowClick)
return;

if (ToggleableControl != null)
{
if (ToggleableControl.Enabled)
ToggleableControl.Disable();
else
ToggleableControl.Enable();
}

base.OnLeftClick();
}

}
}
17 changes: 1 addition & 16 deletions DTAConfig/Settings/SettingCheckBoxBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public string ParentCheckBoxName
set
{
_parentCheckBoxName = value;
UpdateParentCheckBox(FindParentCheckBox());
UpdateParentCheckBox(UIHelpers.FindMatchingChild<XNAClientCheckBox>(Parent, _parentCheckBoxName, false));
}
}

Expand Down Expand Up @@ -104,21 +104,6 @@ protected override void ParseControlINIAttribute(IniFile iniFile, string key, st

public abstract bool Save();


private XNAClientCheckBox FindParentCheckBox()
{
if (string.IsNullOrEmpty(ParentCheckBoxName))
return null;

foreach (var control in Parent.Children)
{
if (control is XNAClientCheckBox && control.Name == ParentCheckBoxName)
return control as XNAClientCheckBox;
}

return null;
}

private void UpdateParentCheckBox(XNAClientCheckBox parentCheckBox)
{
if (ParentCheckBox != null)
Expand Down
8 changes: 8 additions & 0 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/GameLobbyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -897,9 +897,17 @@ protected void InitPlayerOptionDropdowns()
ReadINIForControl(lblTeam);

btnPlayerExtraOptionsOpen = FindChild<XNAClientButton>(nameof(btnPlayerExtraOptionsOpen), true);

if (btnPlayerExtraOptionsOpen != null)
{
PlayerExtraOptionsPanel = FindChild<PlayerExtraOptionsPanel>(nameof(PlayerExtraOptionsPanel));
ReadINIForControl(PlayerExtraOptionsPanel);

foreach (var child in PlayerExtraOptionsPanel.Children)
{
ReadINIForControl(child);
}

PlayerExtraOptionsPanel.Disable();
PlayerExtraOptionsPanel.OptionsChanged += PlayerExtraOptions_OptionsChanged;
btnPlayerExtraOptionsOpen.LeftClick += BtnPlayerExtraOptions_LeftClick;
Expand Down
24 changes: 12 additions & 12 deletions DXMainClient/DXGUI/Multiplayer/PlayerExtraOptionsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace DTAClient.DXGUI.Multiplayer
{
public class PlayerExtraOptionsPanel : XNAWindow
public class PlayerExtraOptionsPanel : XNAPanel
{
private const int maxStartCount = 8;
private const int defaultX = 24;
Expand Down Expand Up @@ -162,42 +162,42 @@ public override void Initialize()
Visible = false;

var btnClose = new XNAClientButton(WindowManager);
btnClose.Name = nameof(btnClose);
btnClose.Name = nameof(PlayerExtraOptionsPanel) + "_" + nameof(btnClose);
btnClose.ClientRectangle = new Rectangle(0, 0, 0, 0);
btnClose.IdleTexture = AssetLoader.LoadTexture("optionsButtonClose.png");
btnClose.HoverTexture = AssetLoader.LoadTexture("optionsButtonClose_c.png");
btnClose.LeftClick += (sender, args) => Disable();
AddChild(btnClose);

var lblHeader = new XNALabel(WindowManager);
lblHeader.Name = nameof(lblHeader);
lblHeader.Name = nameof(PlayerExtraOptionsPanel) + "_" + nameof(lblHeader);
lblHeader.Text = "Extra Player Options".L10N("Client:Main:ExtraPlayerOptions");
lblHeader.ClientRectangle = new Rectangle(defaultX, 4, 0, 18);
AddChild(lblHeader);

chkBoxForceRandomSides = new XNAClientCheckBox(WindowManager);
chkBoxForceRandomSides.Name = nameof(chkBoxForceRandomSides);
chkBoxForceRandomSides.Name = nameof(PlayerExtraOptionsPanel) + "_" + nameof(chkBoxForceRandomSides);
chkBoxForceRandomSides.Text = "Force Random Sides".L10N("Client:Main:ForceRandomSides");
chkBoxForceRandomSides.ClientRectangle = new Rectangle(defaultX, lblHeader.Bottom + 4, 0, 0);
chkBoxForceRandomSides.CheckedChanged += Options_Changed;
AddChild(chkBoxForceRandomSides);

chkBoxForceRandomColors = new XNAClientCheckBox(WindowManager);
chkBoxForceRandomColors.Name = nameof(chkBoxForceRandomColors);
chkBoxForceRandomColors.Name = nameof(PlayerExtraOptionsPanel) + "_" + nameof(chkBoxForceRandomColors);
chkBoxForceRandomColors.Text = "Force Random Colors".L10N("Client:Main:ForceRandomColors");
chkBoxForceRandomColors.ClientRectangle = new Rectangle(defaultX, chkBoxForceRandomSides.Bottom + 4, 0, 0);
chkBoxForceRandomColors.CheckedChanged += Options_Changed;
AddChild(chkBoxForceRandomColors);

chkBoxForceRandomTeams = new XNAClientCheckBox(WindowManager);
chkBoxForceRandomTeams.Name = nameof(chkBoxForceRandomTeams);
chkBoxForceRandomTeams.Name = nameof(PlayerExtraOptionsPanel) + "_" + nameof(chkBoxForceRandomTeams);
chkBoxForceRandomTeams.Text = "Force Random Teams".L10N("Client:Main:ForceRandomTeams");
chkBoxForceRandomTeams.ClientRectangle = new Rectangle(defaultX, chkBoxForceRandomColors.Bottom + 4, 0, 0);
chkBoxForceRandomTeams.CheckedChanged += Options_Changed;
AddChild(chkBoxForceRandomTeams);

chkBoxForceRandomStarts = new XNAClientCheckBox(WindowManager);
chkBoxForceRandomStarts.Name = nameof(chkBoxForceRandomStarts);
chkBoxForceRandomStarts.Name = nameof(PlayerExtraOptionsPanel) + "_" + nameof(chkBoxForceRandomStarts);
chkBoxForceRandomStarts.Text = "Force Random Starts".L10N("Client:Main:ForceRandomStarts");
chkBoxForceRandomStarts.ClientRectangle = new Rectangle(defaultX, chkBoxForceRandomTeams.Bottom + 4, 0, 0);
chkBoxForceRandomStarts.CheckedChanged += Options_Changed;
Expand All @@ -206,35 +206,35 @@ public override void Initialize()
/////////////////////////////

chkBoxUseTeamStartMappings = new XNAClientCheckBox(WindowManager);
chkBoxUseTeamStartMappings.Name = nameof(chkBoxUseTeamStartMappings);
chkBoxUseTeamStartMappings.Name = nameof(PlayerExtraOptionsPanel) + "_" + nameof(chkBoxUseTeamStartMappings);
chkBoxUseTeamStartMappings.Text = "Enable Auto Allying:".L10N("Client:Main:EnableAutoAllying");
chkBoxUseTeamStartMappings.ClientRectangle = new Rectangle(chkBoxForceRandomSides.X, chkBoxForceRandomStarts.Bottom + 20, 0, 0);
chkBoxUseTeamStartMappings.CheckedChanged += ChkBoxUseTeamStartMappings_Changed;
AddChild(chkBoxUseTeamStartMappings);

var btnHelp = new XNAClientButton(WindowManager);
btnHelp.Name = nameof(btnHelp);
btnHelp.Name = nameof(PlayerExtraOptionsPanel) + "_" + nameof(btnHelp);
btnHelp.IdleTexture = AssetLoader.LoadTexture("questionMark.png");
btnHelp.HoverTexture = AssetLoader.LoadTexture("questionMark_c.png");
btnHelp.LeftClick += BtnHelp_LeftClick;
btnHelp.ClientRectangle = new Rectangle(chkBoxUseTeamStartMappings.Right + 4, chkBoxUseTeamStartMappings.Y - 1, 0, 0);
AddChild(btnHelp);

var lblPreset = new XNALabel(WindowManager);
lblPreset.Name = nameof(lblPreset);
lblPreset.Name = nameof(PlayerExtraOptionsPanel) + "_" + nameof(lblPreset);
lblPreset.Text = "Presets:".L10N("Client:Main:Presets");
lblPreset.ClientRectangle = new Rectangle(chkBoxUseTeamStartMappings.X, chkBoxUseTeamStartMappings.Bottom + 8, 0, 0);
AddChild(lblPreset);

ddTeamStartMappingPreset = new XNAClientDropDown(WindowManager);
ddTeamStartMappingPreset.Name = nameof(ddTeamStartMappingPreset);
ddTeamStartMappingPreset.Name = nameof(PlayerExtraOptionsPanel) + "_" + nameof(ddTeamStartMappingPreset);
ddTeamStartMappingPreset.ClientRectangle = new Rectangle(lblPreset.X + 50, lblPreset.Y - 2, 160, 0);
ddTeamStartMappingPreset.SelectedIndexChanged += DdTeamMappingPreset_SelectedIndexChanged;
ddTeamStartMappingPreset.AllowDropDown = true;
AddChild(ddTeamStartMappingPreset);

teamStartMappingsPanel = new TeamStartMappingsPanel(WindowManager);
teamStartMappingsPanel.Name = nameof(teamStartMappingsPanel);
teamStartMappingsPanel.Name = nameof(PlayerExtraOptionsPanel) + "_" + nameof(teamStartMappingsPanel);
teamStartMappingsPanel.ClientRectangle = new Rectangle(lblPreset.X, ddTeamStartMappingPreset.Bottom + 8, Width, Height - ddTeamStartMappingPreset.Bottom + 4);
AddChild(teamStartMappingsPanel);

Expand Down
1 change: 1 addition & 0 deletions Docs/INISystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ These can ONLY be used in parent controls that inherit the `INItializableWindow`
`$Width` = ``{integer}`` the Width of the control
`$Height` = ``{integer}`` the Height of the control
`$TextAnchor`
`$ToggleableControl` = ``{control name}`` (only on `XNAClientButton` or derived classes) Name of control whose visibility this button toggles, only works if the target control is initialized before the button and is not the parent window

### Dynamic Control Property Examples
```
Expand Down