Skip to content

Commit

Permalink
Reimplement FindChild and add FindChildrenStartWith
Browse files Browse the repository at this point in the history
  • Loading branch information
SadPencil committed Sep 12, 2022
1 parent 624dfcc commit d9227a2
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions ClientGUI/INItializableWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Rampastring.XNAUI.XNAControls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

Expand All @@ -26,29 +27,43 @@ public INItializableWindow(WindowManager windowManager) : base(windowManager)
/// instead of the window's name.
/// </summary>
protected string IniNameOverride { get; set; }
private bool VisitChild(IEnumerable<XNAControl> list, Func<XNAControl, bool> judge)
{
foreach (XNAControl child in list)
{
bool stop = judge(child);
if (stop) return true;
stop = VisitChild(child.Children, judge);
if (stop) return true;
}
return false;
}

public T FindChild<T>(string childName, bool optional = false) where T : XNAControl
{
T child = FindChild<T>(Children, childName);
if (child == null && !optional)
XNAControl result = null;
VisitChild(new List<XNAControl>() { this }, control =>
{
if (control.Name != childName) return false;
result = control;
return true;
});
if (result == null && !optional)
throw new KeyNotFoundException("Could not find required child control: " + childName);

return child;
return (T)result;
}

private T FindChild<T>(IEnumerable<XNAControl> list, string controlName) where T : XNAControl
public List<T> FindChildrenStartWith<T>(string prefix) where T : XNAControl
{
foreach (XNAControl child in list)
List<T> result = new List<T>();
VisitChild(new List<XNAControl>() { this }, (control) =>
{
if (child.Name == controlName)
return (T)child;

T childOfChild = FindChild<T>(child.Children, controlName);
if (childOfChild != null)
return childOfChild;
}

return null;
if (string.IsNullOrEmpty(prefix) ||
!string.IsNullOrEmpty(control.Name) && control.Name.StartsWith(prefix))
result.Add((T)control);
return false;
});
return result;
}

/// <summary>
Expand All @@ -74,7 +89,7 @@ protected string GetConfigPath()
return null; // IniNameOverride must be null, no need to continue

iniFileName = Name;

// get theme specific path
configIniPath = Path.Combine(ProgramConstants.GetResourcePath(), $"{iniFileName}.ini");
if (File.Exists(configIniPath))
Expand Down

0 comments on commit d9227a2

Please sign in to comment.