#2051 fixed -- Widget.Get() throws by default

This commit is contained in:
Chris Forbes
2012-04-27 19:39:07 +12:00
parent 5d1a071287
commit c30d46c014
52 changed files with 409 additions and 398 deletions

View File

@@ -363,25 +363,36 @@ namespace OpenRA.Widgets
c.Removed();
}
public Widget GetWidget(string id)
public Widget GetOrNull(string id)
{
if (this.Id == id)
return this;
foreach (var child in Children)
{
var w = child.GetWidget(id);
var w = child.GetOrNull(id);
if (w != null)
return w;
}
return null;
}
public T GetWidget<T>(string id) where T : Widget
public T GetOrNull<T>(string id) where T : Widget
{
var widget = GetWidget(id);
return (widget != null) ? (T)widget : null;
return (T) GetOrNull(id);
}
public T Get<T>(string id) where T : Widget
{
var t = GetOrNull<T>(id);
if (t == null)
throw new InvalidOperationException(
"Widget {0} has no child {1} of type {2}".F(
Id, id, typeof(T).Name));
return t;
}
public Widget Get(string id) { return Get<Widget>(id); }
}
public class ContainerWidget : Widget