Skip to content

Commit

Permalink
Add toggling control visibility option to INItializableWindow XNAClie…
Browse files Browse the repository at this point in the history
…ntButtons
  • Loading branch information
Starkku committed Sep 8, 2023
1 parent 90ff7e4 commit dc6d2da
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 18 deletions.
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
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

0 comments on commit dc6d2da

Please sign in to comment.