Pass WidgetArgs to Widget.Initialize. Use this to allow for custom substitutions.

This commit is contained in:
Paul Chote
2011-05-17 18:57:41 +12:00
parent b417118429
commit 5bc0ef69ad
6 changed files with 18 additions and 10 deletions

View File

@@ -95,14 +95,17 @@ namespace OpenRA.Widgets
public virtual int2 ChildOrigin { get { return RenderOrigin; } } public virtual int2 ChildOrigin { get { return RenderOrigin; } }
public virtual Rectangle RenderBounds { get { return new Rectangle(RenderOrigin.X, RenderOrigin.Y, Bounds.Width, Bounds.Height); } } public virtual Rectangle RenderBounds { get { return new Rectangle(RenderOrigin.X, RenderOrigin.Y, Bounds.Width, Bounds.Height); } }
public virtual void Initialize() public virtual void Initialize(WidgetArgs args)
{ {
// Parse the YAML equations to find the widget bounds // Parse the YAML equations to find the widget bounds
var parentBounds = (Parent == null) var parentBounds = (Parent == null)
? new Rectangle(0, 0, Game.viewport.Width, Game.viewport.Height) ? new Rectangle(0, 0, Game.viewport.Width, Game.viewport.Height)
: Parent.Bounds; : Parent.Bounds;
var substitutions = new Dictionary<string, int>(); var substitutions = args.ContainsKey("substitutions") ?
new Dictionary<string, int>((Dictionary<string, int>)args["substitutions"]) :
new Dictionary<string, int>();
substitutions.Add("WINDOW_RIGHT", Game.viewport.Width); substitutions.Add("WINDOW_RIGHT", Game.viewport.Width);
substitutions.Add("WINDOW_BOTTOM", Game.viewport.Height); substitutions.Add("WINDOW_BOTTOM", Game.viewport.Height);
substitutions.Add("PARENT_RIGHT", parentBounds.Width); substitutions.Add("PARENT_RIGHT", parentBounds.Width);

View File

@@ -52,7 +52,7 @@ namespace OpenRA
if (child.Key != "Children") if (child.Key != "Children")
FieldLoader.LoadField(widget, child.Key, child.Value.Value); FieldLoader.LoadField(widget, child.Key, child.Value.Value);
widget.Initialize(); widget.Initialize(args);
foreach (var child in node.Value.Nodes) foreach (var child in node.Value.Nodes)
if (child.Key == "Children") if (child.Key == "Children")

View File

@@ -219,9 +219,14 @@ namespace OpenRA.Mods.Cnc.Widgets
void ShowGroupModifierDropdown(CncDropDownButtonWidget dropdown) void ShowGroupModifierDropdown(CncDropDownButtonWidget dropdown)
{ {
var panel = Game.LoadWidget(world, "LABEL_DROPDOWN_TEMPLATE", null, new WidgetArgs()) as ScrollPanelWidget; var substitutions = new Dictionary<string,int>() {{ "DROPDOWN_WIDTH", dropdown.Bounds.Width }};
var itemTemplate = panel.GetWidget<ScrollItemWidget>("TEMPLATE");
var panel = (ScrollPanelWidget)Game.LoadWidget(world, "LABEL_DROPDOWN_TEMPLATE", null, new WidgetArgs()
{
{ "substitutions", substitutions }
});
var itemTemplate = panel.GetWidget<ScrollItemWidget>("TEMPLATE");
var options = new List<Pair<string, Modifiers>>() var options = new List<Pair<string, Modifiers>>()
{ {
Pair.New("Ctrl", Modifiers.Ctrl), Pair.New("Ctrl", Modifiers.Ctrl),

View File

@@ -42,9 +42,9 @@ namespace OpenRA.Mods.RA.Widgets
this.world = world; this.world = world;
} }
public override void Initialize() public override void Initialize(WidgetArgs args)
{ {
base.Initialize(); base.Initialize(args);
var size = Math.Max(world.Map.Bounds.Width, world.Map.Bounds.Height); var size = Math.Max(world.Map.Bounds.Width, world.Map.Bounds.Height);
previewScale = Math.Min(RenderBounds.Width * 1f / world.Map.Bounds.Width, RenderBounds.Height * 1f / world.Map.Bounds.Height); previewScale = Math.Min(RenderBounds.Width * 1f / world.Map.Bounds.Width, RenderBounds.Height * 1f / world.Map.Bounds.Height);

View File

@@ -34,9 +34,9 @@ namespace OpenRA.Mods.RA.Widgets
this.worldRenderer = worldRenderer; this.worldRenderer = worldRenderer;
} }
public override void Initialize() public override void Initialize(WidgetArgs args)
{ {
base.Initialize(); base.Initialize(args);
spsprites = Rules.Info.Values.SelectMany( u => u.Traits.WithInterface<SupportPowerInfo>() ) spsprites = Rules.Info.Values.SelectMany( u => u.Traits.WithInterface<SupportPowerInfo>() )
.Select(u => u.Image).Distinct() .Select(u => u.Image).Distinct()

View File

@@ -74,7 +74,7 @@ Background@COLOR_CHOOSER:
ScrollPanel@LABEL_DROPDOWN_TEMPLATE: ScrollPanel@LABEL_DROPDOWN_TEMPLATE:
Id:LABEL_DROPDOWN_TEMPLATE Id:LABEL_DROPDOWN_TEMPLATE
Width:100 Width:DROPDOWN_WIDTH
Children: Children:
ScrollItem@TEMPLATE: ScrollItem@TEMPLATE:
Id:TEMPLATE Id:TEMPLATE