From cff4c534d533e2dd92ca857611da2d78e4dfccd7 Mon Sep 17 00:00:00 2001 From: Sad Pencil Date: Wed, 16 Nov 2022 09:47:51 +0800 Subject: [PATCH] Reimplement FindChild and add FindChildrenStartWith --- ClientGUI/INItializableWindow.cs | 44 +++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/ClientGUI/INItializableWindow.cs b/ClientGUI/INItializableWindow.cs index 5c6fd43b3..f8abe894f 100644 --- a/ClientGUI/INItializableWindow.cs +++ b/ClientGUI/INItializableWindow.cs @@ -26,29 +26,43 @@ public INItializableWindow(WindowManager windowManager) : base(windowManager) /// instead of the window's name. /// protected string IniNameOverride { get; set; } + private bool VisitChild(IEnumerable list, Func 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(string childName, bool optional = false) where T : XNAControl { - T child = FindChild(Children, childName); - if (child == null && !optional) + XNAControl result = null; + VisitChild(new List() { 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(IEnumerable list, string controlName) where T : XNAControl + public List FindChildrenStartWith(string prefix) where T : XNAControl { - foreach (XNAControl child in list) + List result = new List(); + VisitChild(new List() { this }, (control) => { - if (child.Name == controlName) - return (T)child; - - T childOfChild = FindChild(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; } ///