#2051 fixed -- Widget.Get() throws by default
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user