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

Reimplement FindChild and add FindChildrenStartWith #367

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
44 changes: 29 additions & 15 deletions ClientGUI/INItializableWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,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 Down