fixed up the chrome/widgets tree

This commit is contained in:
Chris Forbes
2010-03-24 08:30:59 +13:00
parent f37eea8b04
commit 41fe6f01fc
15 changed files with 413 additions and 291 deletions

View File

@@ -0,0 +1,37 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
namespace OpenRA.Widgets
{
class BackgroundWidget : Widget
{
public override void Draw()
{
if (!Visible)
{
base.Draw();
return;
}
WidgetUtils.DrawPanel("dialog", Bounds, null);
base.Draw();
}
}
}

View File

@@ -0,0 +1,82 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Drawing;
namespace OpenRA.Widgets
{
class ButtonWidget : Widget
{
public string Text = "";
public bool Depressed = false;
public int VisualHeight = 1;
public override bool HandleInput(MouseInput mi)
{
if (Game.chrome.selectedWidget == this)
Depressed = (GetEventBounds().Contains(mi.Location.X,mi.Location.Y)) ? true : false;
// Relinquish focus
if (Game.chrome.selectedWidget == this && mi.Event == MouseInputEvent.Up)
{
Game.chrome.selectedWidget = null;
Depressed = false;
}
// Are we able to handle this event?
if (!Visible || !GetEventBounds().Contains(mi.Location.X,mi.Location.Y))
return base.HandleInput(mi);
if (base.HandleInput(mi))
return true;
// Give button focus only while the mouse is down
// This is a bit of a hack: it will become cleaner soonish
// It will also steal events from any potential children
// We also want to play a click sound
if (mi.Event == MouseInputEvent.Down)
{
Game.chrome.selectedWidget = this;
Depressed = true;
return true;
}
return false;
}
public override void Draw()
{
if (!Visible)
{
base.Draw();
return;
}
var stateOffset = (Depressed) ? new int2(VisualHeight, VisualHeight) : new int2(0, 0);
WidgetUtils.DrawPanel(Depressed ? "dialog3" : "dialog2", Bounds,
() => Game.chrome.renderer.BoldFont.DrawText(Game.chrome.rgbaRenderer, Text,
new int2(Bounds.X + Bounds.Width / 2, Bounds.Y + Bounds.Height / 2)
- new int2(Game.chrome.renderer.BoldFont.Measure(Text).X / 2,
Game.chrome.renderer.BoldFont.Measure(Text).Y / 2) + stateOffset, Color.White));
base.Draw();
}
}
}

View File

@@ -0,0 +1,62 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Drawing;
namespace OpenRA.Widgets
{
class CheckboxWidget : Widget
{
public string Text = "";
public override void Draw()
{
if (!Visible)
{
base.Draw();
return;
}
var selected = InputHandler.Value != null ? InputHandler.Value.GetState(this) : false;
WidgetUtils.DrawPanel("dialog3",
new Rectangle(Bounds.Location,
new Size(Bounds.Height, Bounds.Height)),
() => { });
Game.chrome.renderer.BoldFont.DrawText(Game.chrome.rgbaRenderer, Text,
new float2(Bounds.Left + Bounds.Height * 2, Bounds.Top), Color.White);
if (selected)
{
Game.chrome.lineRenderer.FillRect(
new RectangleF(
Game.viewport.Location.X + Bounds.Left + 4,
Game.viewport.Location.Y + Bounds.Top + 5,
Bounds.Height - 9,
Bounds.Height - 9),
Color.White);
Game.chrome.lineRenderer.Flush();
}
base.Draw();
}
}
}

View File

@@ -0,0 +1,48 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
namespace OpenRA.Widgets.Delegates
{
public class ConnectionDialogsDelegate : WidgetDelegate
{
public override bool OnMouseUp(Widget w, MouseInput mi)
{
// Main Menu root
if (w.Id == "CONNECTION_BUTTON_ABORT")
{
Game.chrome.rootWidget.ShowMenu("MAINMENU_BG");
return true;
}
if (w.Id == "CONNECTION_BUTTON_CANCEL")
{
Game.chrome.rootWidget.ShowMenu("MAINMENU_BG");
return true;
}
if (w.Id == "CONNECTION_BUTTON_RETRY")
{
Game.JoinServer(Game.CurrentHost, Game.CurrentPort);
return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,79 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Net;
namespace OpenRA.Widgets.Delegates
{
public class CreateServerMenuDelegate : WidgetDelegate
{
static bool AdvertiseServerOnline = Game.Settings.InternetServer;
public override bool GetState(Widget w)
{
if (w.Id == "CREATESERVER_CHECKBOX_ONLINE")
return AdvertiseServerOnline;
return false;
}
public override bool OnMouseDown(Widget w, MouseInput mi)
{
if (w.Id == "CREATESERVER_CHECKBOX_ONLINE")
{
AdvertiseServerOnline = !AdvertiseServerOnline;
return true;
}
return false;
}
public override bool OnMouseUp(Widget w, MouseInput mi)
{
if (w.Id == "MAINMENU_BUTTON_CREATE")
{
Game.chrome.rootWidget.ShowMenu("CREATESERVER_BG");
return true;
}
if (w.Id == "CREATESERVER_BUTTON_CANCEL")
{
Game.chrome.rootWidget.ShowMenu("MAINMENU_BG");
return true;
}
if (w.Id == "CREATESERVER_BUTTON_START")
{
Game.chrome.rootWidget.ShowMenu(null);
Log.Write("Creating server");
Server.Server.ServerMain(AdvertiseServerOnline, Game.Settings.MasterServer,
Game.Settings.GameName, Game.Settings.ListenPort,
Game.Settings.ExternalPort, Game.Settings.InitialMods);
Log.Write("Joining server");
Game.JoinServer(IPAddress.Loopback.ToString(), Game.Settings.ListenPort);
return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,36 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
namespace OpenRA.Widgets.Delegates
{
public class MainMenuButtonsDelegate : WidgetDelegate
{
public override bool OnMouseUp(Widget w, MouseInput mi)
{
// Main Menu root
if (w.Id == "MAINMENU_BUTTON_QUIT")
{
Game.Exit();
return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,90 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Server;
namespace OpenRA.Widgets.Delegates
{
public class ServerBrowserDelegate : WidgetDelegate
{
static GameServer[] GameList;
static List<Widget> GameButtons = new List<Widget>();
public override bool OnMouseUp(Widget w, MouseInput mi)
{
// Main Menu root
if (w.Id == "MAINMENU_BUTTON_JOIN")
{
var bg = Game.chrome.rootWidget.ShowMenu("JOINSERVER_BG");
int height = 50;
int width = 300;
int i = 0;
GameList = MasterServerQuery.GetGameList(Game.Settings.MasterServer).ToArray();
bg.Children.RemoveAll(a => GameButtons.Contains(a));
GameButtons.Clear();
foreach (var game in GameList)
{
ButtonWidget b = new ButtonWidget();
b.Bounds = new Rectangle(bg.Bounds.X + 20, bg.Bounds.Y + height, width, 25);
b.GetType().GetField("Id").SetValue(b, "JOIN_GAME_{0}".F(i));
b.GetType().GetField("Text").SetValue(b, "{0} ({1})".F(game.Name, game.Address));
b.GetType().GetField("Delegate").SetValue(b, "ServerBrowserDelegate");
bg.AddChild(b);
GameButtons.Add(b);
height += 35;
}
return true;
}
if (w.Id == "JOINSERVER_BUTTON_DIRECTCONNECT")
{
Game.chrome.rootWidget.GetWidget("JOINSERVER_BG").Visible = false;
Game.JoinServer(Game.Settings.NetworkHost, Game.Settings.NetworkPort);
return true;
}
if (w.Id.Substring(0, 10) == "JOIN_GAME_")
{
Game.chrome.rootWidget.GetWidget("JOINSERVER_BG").Visible = false;
int index = int.Parse(w.Id.Substring(10));
var game = GameList[index];
Game.JoinServer(game.Address.Split(':')[0], int.Parse(game.Address.Split(':')[1]));
return true;
}
if (w.Id == "JOINSERVER_BUTTON_CANCEL")
{
Game.chrome.rootWidget.ShowMenu("MAINMENU_BG");
return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRA.Widgets.Delegates
{
public class SettingsMenuDelegate : WidgetDelegate
{
public override bool GetState(Widget w)
{
if (w.Id == "SETTINGS_CHECKBOX_UNITDEBUG") return Game.Settings.UnitDebug;
if (w.Id == "SETTINGS_CHECKBOX_PATHDEBUG") return Game.Settings.PathDebug;
if (w.Id == "SETTINGS_CHECKBOX_INDEXDEBUG") return Game.Settings.IndexDebug;
if (w.Id == "SETTINGS_CHECKBOX_PERFGRAPH") return Game.Settings.PerfGraph;
if (w.Id == "SETTINGS_CHECKBOX_PERFTEXT") return Game.Settings.PerfText;
return false;
}
public override bool OnMouseDown(Widget w, MouseInput mi)
{
if (w.Id == "SETTINGS_CHECKBOX_UNITDEBUG")
{
Game.Settings.UnitDebug = !Game.Settings.UnitDebug;
return true;
}
if (w.Id == "SETTINGS_CHECKBOX_PATHDEBUG")
{
Game.Settings.PathDebug = !Game.Settings.PathDebug;
return true;
}
if (w.Id == "SETTINGS_CHECKBOX_INDEXDEBUG")
{
Game.Settings.IndexDebug = !Game.Settings.IndexDebug;
return true;
}
if (w.Id == "SETTINGS_CHECKBOX_PERFGRAPH")
{
Game.Settings.PerfGraph = !Game.Settings.PerfGraph;
return true;
}
if (w.Id == "SETTINGS_CHECKBOX_PERFTEXT")
{
Game.Settings.PerfText = !Game.Settings.PerfText;
return true;
}
return false;
}
public override bool OnMouseUp(Widget w, MouseInput mi)
{
if (w.Id == "MAINMENU_BUTTON_SETTINGS")
{
Game.chrome.rootWidget.ShowMenu("SETTINGS_BG");
return true;
}
if (w.Id == "SETTINGS_BUTTON_OK")
{
Game.chrome.rootWidget.ShowMenu("MAINMENU_BG");
return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,33 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
namespace OpenRA.Widgets.Delegates
{
public class WidgetDelegate
{
// For checkboxes
public virtual bool GetState(Widget w) { return false; }
// For any widget
public virtual bool OnMouseDown(Widget w, MouseInput mi) { return false; }
public virtual bool OnMouseUp(Widget w, MouseInput mi) { return false; }
public virtual bool OnMouseMove(Widget w, MouseInput mi) { return false; }
}
}

View File

@@ -0,0 +1,53 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Drawing;
namespace OpenRA.Widgets
{
class LabelWidget : Widget
{
public string Text = "";
public string Align = "Left";
public override void Draw()
{
if (!Visible)
{
base.Draw();
return;
}
Rectangle r = Bounds;
Game.chrome.renderer.Device.EnableScissor(r.Left, r.Top, r.Width, r.Height);
int2 textSize = Game.chrome.renderer.BoldFont.Measure(Text);
int2 position = new int2(Bounds.X,Bounds.Y);
if (Align == "Center")
position = new int2(Bounds.X+Bounds.Width/2, Bounds.Y+Bounds.Height/2)
- new int2(textSize.X / 2, textSize.Y/2);
Game.chrome.renderer.BoldFont.DrawText(Game.chrome.rgbaRenderer, Text, position, Color.White);
Game.chrome.renderer.Device.DisableScissor();
base.Draw();
}
}
}

View File

@@ -0,0 +1,162 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Widgets.Delegates;
namespace OpenRA.Widgets
{
public class Widget
{
// Info defined in YAML
public readonly string Id = null;
public readonly string X = "0";
public readonly string Y = "0";
public readonly string Width = "0";
public readonly string Height = "0";
public readonly string Delegate = null;
public Lazy<WidgetDelegate> InputHandler;
public bool Visible = true;
public readonly List<Widget> Children = new List<Widget>();
// Calculated internally
public Rectangle Bounds;
public Widget Parent = null;
public Widget() { InputHandler = Lazy.New(() => BindHandler(Delegate)); }
public virtual void Initialize()
{
// Parse the YAML equations to find the widget bounds
Rectangle parentBounds = (Parent == null) ? new Rectangle(0,0,Game.viewport.Width,Game.viewport.Height) : Parent.Bounds;
Dictionary<string, int> substitutions = new Dictionary<string, int>();
substitutions.Add("WINDOW_RIGHT", Game.viewport.Width);
substitutions.Add("WINDOW_BOTTOM", Game.viewport.Height);
substitutions.Add("PARENT_RIGHT", parentBounds.Width);
substitutions.Add("PARENT_BOTTOM", parentBounds.Height);
int width = Evaluator.Evaluate(Width, substitutions);
int height = Evaluator.Evaluate(Height, substitutions);
substitutions.Add("WIDTH", width);
substitutions.Add("HEIGHT", height);
Bounds = new Rectangle(parentBounds.X + Evaluator.Evaluate(X, substitutions),
parentBounds.Y + Evaluator.Evaluate(Y, substitutions),
width,
height);
foreach (var child in Children)
child.Initialize();
}
public Rectangle GetEventBounds()
{
return Children
.Where(c => c.Visible)
.Select(c => c.GetEventBounds())
.Aggregate(Bounds, Rectangle.Union);
}
static WidgetDelegate BindHandler(string name)
{
if (name == null) return null;
foreach (var mod in Game.ModAssemblies)
{
var act = (WidgetDelegate)mod.First.CreateInstance(mod.Second + "." + name);
if (act != null) return act;
}
throw new InvalidOperationException("Cannot locate widget delegate: {0}".F(name));
}
public virtual bool HandleInput(MouseInput mi)
{
// Are we able to handle this event?
if (!Visible || !GetEventBounds().Contains(mi.Location.X,mi.Location.Y))
return false;
// Can any of our children handle this?
foreach (var child in Children)
if (child.HandleInput(mi))
return true;
// Mousedown
if (InputHandler.Value != null && mi.Event == MouseInputEvent.Down)
return InputHandler.Value.OnMouseDown(this, mi);
// Mouseup
if (InputHandler.Value != null && mi.Event == MouseInputEvent.Up)
return InputHandler.Value.OnMouseUp(this, mi);
// Mousemove
if (InputHandler.Value != null && mi.Event == MouseInputEvent.Move)
return InputHandler.Value.OnMouseMove(this, mi);
return false;
}
public virtual void Draw()
{
if (Visible)
foreach (var child in Children)
child.Draw();
}
public void AddChild(Widget child)
{
child.Parent = this;
Children.Add( child );
}
public Widget GetWidget(string id)
{
if (this.Id == id)
return this;
foreach (var child in Children)
if (child.GetWidget(id) != null)
return child;
return null;
}
public Widget GetCurrentMenu() { return Children.FirstOrDefault(c => c.Visible); }
public Widget ShowMenu(string menu)
{
GetCurrentMenu().Visible = false;
var widget = GetWidget(menu);
if (widget != null)
widget.Visible = true;
return widget;
}
}
class ContainerWidget : Widget { }
}

View File

@@ -0,0 +1,60 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.FileFormats;
using OpenRA.Widgets;
namespace OpenRA
{
class WidgetLoader
{
public static Widget LoadWidget(KeyValuePair<string, MiniYaml> node)
{
var widget = NewWidget(node.Key);
foreach (var child in node.Value.Nodes)
{
if (child.Key == "Children")
foreach (var c in child.Value.Nodes)
widget.AddChild(LoadWidget(c));
else
FieldLoader.LoadField(widget, child.Key, child.Value.Value);
}
return widget;
}
static Widget NewWidget(string widgetType)
{
widgetType = widgetType.Split('@')[0];
foreach (var mod in Game.ModAssemblies)
{
var fullTypeName = mod.Second + "." + widgetType + "Widget";
var widget = (Widget)mod.First.CreateInstance(fullTypeName);
if (widget == null) continue;
return widget;
}
throw new InvalidOperationException("Cannot locate widget: {0}".F(widgetType));
}
}
}

View File

@@ -0,0 +1,69 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Drawing;
using System.Linq;
using OpenRA.Graphics;
namespace OpenRA.Widgets
{
static class WidgetUtils
{
public static void DrawPanel(string collection, Rectangle Bounds, Action a)
{
var r = Game.chrome.renderer;
var sr = Game.chrome.rgbaRenderer;
r.Device.EnableScissor(Bounds.Left, Bounds.Top, Bounds.Width, Bounds.Height);
var images = new[] { "border-t", "border-b", "border-l", "border-r", "corner-tl", "corner-tr", "corner-bl", "corner-br", "background" };
var ss = images.Select(i => ChromeProvider.GetImage(Game.chrome.renderer, collection, i)).ToArray();
for (var x = Bounds.Left + (int)ss[2].size.X; x < Bounds.Right - (int)ss[3].size.X; x += (int)ss[8].size.X)
for (var y = Bounds.Top + (int)ss[0].size.Y; y < Bounds.Bottom - (int)ss[1].size.Y; y += (int)ss[8].size.Y)
sr.DrawSprite(ss[8], new float2(x, y), "chrome");
//draw borders
for (var y = Bounds.Top + (int)ss[0].size.Y; y < Bounds.Bottom - (int)ss[1].size.Y; y += (int)ss[2].size.Y)
{
sr.DrawSprite(ss[2], new float2(Bounds.Left, y), "chrome");
sr.DrawSprite(ss[3], new float2(Bounds.Right - ss[3].size.X, y), "chrome");
}
for (var x = Bounds.Left + (int)ss[2].size.X; x < Bounds.Right - (int)ss[3].size.X; x += (int)ss[0].size.X)
{
sr.DrawSprite(ss[0], new float2(x, Bounds.Top), "chrome");
sr.DrawSprite(ss[1], new float2(x, Bounds.Bottom - ss[1].size.Y), "chrome");
}
sr.DrawSprite(ss[4], new float2(Bounds.Left, Bounds.Top), "chrome");
sr.DrawSprite(ss[5], new float2(Bounds.Right - ss[5].size.X, Bounds.Top), "chrome");
sr.DrawSprite(ss[6], new float2(Bounds.Left, Bounds.Bottom - ss[6].size.Y), "chrome");
sr.DrawSprite(ss[7], new float2(Bounds.Right - ss[7].size.X, Bounds.Bottom - ss[7].size.Y), "chrome");
sr.Flush();
if (a != null) a();
r.Device.DisableScissor();
}
}
}