Kill the static widget interface

This commit is contained in:
Paul Chote
2010-04-09 18:13:11 +12:00
parent 64a6ca2ef9
commit 2fa93c74e4
9 changed files with 112 additions and 193 deletions

View File

@@ -285,7 +285,6 @@
<Compile Include="Widgets\Widget.cs" /> <Compile Include="Widgets\Widget.cs" />
<Compile Include="Widgets\BackgroundWidget.cs" /> <Compile Include="Widgets\BackgroundWidget.cs" />
<Compile Include="Widgets\LabelWidget.cs" /> <Compile Include="Widgets\LabelWidget.cs" />
<Compile Include="Widgets\Delegates\WidgetDelegate.cs" />
<Compile Include="Widgets\CheckboxWidget.cs" /> <Compile Include="Widgets\CheckboxWidget.cs" />
<Compile Include="Traits\HasUnitOnBuild.cs" /> <Compile Include="Traits\HasUnitOnBuild.cs" />
<Compile Include="Traits\Activities\Wait.cs" /> <Compile Include="Traits\Activities\Wait.cs" />

View File

@@ -19,13 +19,15 @@
#endregion #endregion
using System.Drawing; using System.Drawing;
using System;
namespace OpenRA.Widgets namespace OpenRA.Widgets
{ {
class CheckboxWidget : Widget class CheckboxWidget : Widget
{ {
public string Text = ""; public string Text = "";
public Func<bool> Checked = () => {return false;};
public override void Draw() public override void Draw()
{ {
if (!Visible) if (!Visible)
@@ -34,8 +36,6 @@ namespace OpenRA.Widgets
return; return;
} }
var selected = InputHandler.Value != null ? InputHandler.Value.GetState(this) : false;
WidgetUtils.DrawPanel("dialog3", WidgetUtils.DrawPanel("dialog3",
new Rectangle(Bounds.Location, new Rectangle(Bounds.Location,
new Size(Bounds.Height, Bounds.Height)), new Size(Bounds.Height, Bounds.Height)),
@@ -44,7 +44,7 @@ namespace OpenRA.Widgets
Game.chrome.renderer.BoldFont.DrawText(Game.chrome.rgbaRenderer, Text, Game.chrome.renderer.BoldFont.DrawText(Game.chrome.rgbaRenderer, Text,
new float2(Bounds.Left + Bounds.Height * 2, Bounds.Top), Color.White); new float2(Bounds.Left + Bounds.Height * 2, Bounds.Top), Color.White);
if (selected) if (Checked())
{ {
Game.chrome.lineRenderer.FillRect( Game.chrome.lineRenderer.FillRect(
new RectangleF( new RectangleF(

View File

@@ -1,4 +1,4 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford. * Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA. * This file is part of OpenRA.
@@ -20,29 +20,23 @@
namespace OpenRA.Widgets.Delegates namespace OpenRA.Widgets.Delegates
{ {
public class ConnectionDialogsDelegate : WidgetDelegate public class ConnectionDialogsDelegate : IWidgetDelegate
{ {
public override bool OnMouseUp(Widget w, MouseInput mi) public ConnectionDialogsDelegate()
{ {
// Main Menu root var r = Game.chrome.rootWidget;
if (w.Id == "CONNECTION_BUTTON_ABORT") r.GetWidget("CONNECTION_BUTTON_ABORT").OnMouseUp = mi => {
{
Game.chrome.rootWidget.ShowMenu("MAINMENU_BG"); Game.chrome.rootWidget.ShowMenu("MAINMENU_BG");
return true; return true;
} };
r.GetWidget("CONNECTION_BUTTON_CANCEL").OnMouseUp = mi => {
if (w.Id == "CONNECTION_BUTTON_CANCEL")
{
Game.chrome.rootWidget.ShowMenu("MAINMENU_BG"); Game.chrome.rootWidget.ShowMenu("MAINMENU_BG");
return true; return true;
} };
r.GetWidget("CONNECTION_BUTTON_RETRY").OnMouseUp = mi => {
if (w.Id == "CONNECTION_BUTTON_RETRY")
{
Game.JoinServer(Game.CurrentHost, Game.CurrentPort); Game.JoinServer(Game.CurrentHost, Game.CurrentPort);
return true; return true;
} };
return false;
} }
} }
} }

View File

@@ -23,49 +23,27 @@ using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
namespace OpenRA.Widgets.Delegates namespace OpenRA.Widgets.Delegates
{ {
public class CreateServerMenuDelegate : WidgetDelegate public class CreateServerMenuDelegate : IWidgetDelegate
{ {
static bool AdvertiseServerOnline = Game.Settings.InternetServer; static bool AdvertiseServerOnline = Game.Settings.InternetServer;
public override bool GetState(Widget w) public CreateServerMenuDelegate()
{ {
if (w.Id == "CREATESERVER_CHECKBOX_ONLINE") var r = Game.chrome.rootWidget;
return AdvertiseServerOnline; r.GetWidget("MAINMENU_BUTTON_CREATE").OnMouseUp = mi => {
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"); Game.chrome.rootWidget.ShowMenu("CREATESERVER_BG");
return true; return true;
} };
if (w.Id == "CREATESERVER_BUTTON_CANCEL") r.GetWidget("CREATESERVER_BUTTON_CANCEL").OnMouseUp = mi => {
{
Game.chrome.rootWidget.ShowMenu("MAINMENU_BG"); Game.chrome.rootWidget.ShowMenu("MAINMENU_BG");
return true; return true;
} };
if (w.Id == "CREATESERVER_BUTTON_START") r.GetWidget("CREATESERVER_BUTTON_START").OnMouseUp = mi => {
{
Game.chrome.rootWidget.ShowMenu(null); Game.chrome.rootWidget.ShowMenu(null);
Log.Write("Creating server"); Log.Write("Creating server");
// TODO: Get this from a map chooser // TODO: Get this from a map chooser
string map = Game.AvailableMaps.Keys.FirstOrDefault(); string map = Game.AvailableMaps.Keys.FirstOrDefault();
@@ -82,9 +60,13 @@ namespace OpenRA.Widgets.Delegates
Log.Write("Joining server"); Log.Write("Joining server");
Game.JoinServer(IPAddress.Loopback.ToString(), Game.Settings.ListenPort); Game.JoinServer(IPAddress.Loopback.ToString(), Game.Settings.ListenPort);
return true; return true;
} };
return false; r.GetWidget<CheckboxWidget>("CREATESERVER_CHECKBOX_ONLINE").Checked = () => {return AdvertiseServerOnline;};
r.GetWidget("CREATESERVER_CHECKBOX_ONLINE").OnMouseDown = mi => {
AdvertiseServerOnline = !AdvertiseServerOnline;
return true;
};
} }
} }
} }

View File

@@ -1,4 +1,4 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford. * Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA. * This file is part of OpenRA.
@@ -17,20 +17,14 @@
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>. * along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/ */
#endregion #endregion
using System;
namespace OpenRA.Widgets.Delegates namespace OpenRA.Widgets.Delegates
{ {
public class MainMenuButtonsDelegate : WidgetDelegate public class MainMenuButtonsDelegate : IWidgetDelegate
{ {
public override bool OnMouseUp(Widget w, MouseInput mi) public MainMenuButtonsDelegate()
{ {
// Main Menu root Game.chrome.rootWidget.GetWidget("MAINMENU_BUTTON_QUIT").OnMouseUp = mi => {Game.Exit(); return true;};
if (w.Id == "MAINMENU_BUTTON_QUIT")
{
Game.Exit();
return true;
}
return false;
} }
} }
} }

View File

@@ -1,4 +1,4 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford. * Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA. * This file is part of OpenRA.
@@ -25,18 +25,17 @@ using OpenRA.Server;
namespace OpenRA.Widgets.Delegates namespace OpenRA.Widgets.Delegates
{ {
public class ServerBrowserDelegate : WidgetDelegate public class ServerBrowserDelegate : IWidgetDelegate
{ {
static GameServer[] GameList; static GameServer[] GameList;
static List<Widget> GameButtons = new List<Widget>(); static List<Widget> GameButtons = new List<Widget>();
public override bool OnMouseUp(Widget w, MouseInput mi) public ServerBrowserDelegate()
{ {
// Main Menu root var r = Game.chrome.rootWidget;
if (w.Id == "MAINMENU_BUTTON_JOIN") r.GetWidget("MAINMENU_BUTTON_JOIN").OnMouseUp =
{ mi => {
var bg = Game.chrome.rootWidget.ShowMenu("JOINSERVER_BG"); var bg = Game.chrome.rootWidget.ShowMenu("JOINSERVER_BG");
int height = 50; int height = 50;
int width = 300; int width = 300;
int i = 0; int i = 0;
@@ -52,7 +51,13 @@ namespace OpenRA.Widgets.Delegates
b.GetType().GetField("Id").SetValue(b, "JOIN_GAME_{0}".F(i)); 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("Text").SetValue(b, "{0} ({1})".F(game.Name, game.Address));
b.GetType().GetField("Delegate").SetValue(b, "ServerBrowserDelegate"); b.GetType().GetField("Delegate").SetValue(b, "ServerBrowserDelegate");
b.OnMouseUp = nmi => {
Game.chrome.rootWidget.GetWidget("JOINSERVER_BG").Visible = false;
Game.JoinServer(GameList[i].Address.Split(':')[0], int.Parse(GameList[i].Address.Split(':')[1]));
return true;
};
bg.AddChild(b); bg.AddChild(b);
GameButtons.Add(b); GameButtons.Add(b);
@@ -60,31 +65,18 @@ namespace OpenRA.Widgets.Delegates
} }
return true; return true;
} };
if (w.Id == "JOINSERVER_BUTTON_DIRECTCONNECT") r.GetWidget("JOINSERVER_BUTTON_DIRECTCONNECT").OnMouseUp = mi => {
{ r.GetWidget("JOINSERVER_BG").Visible = false;
Game.chrome.rootWidget.GetWidget("JOINSERVER_BG").Visible = false;
Game.JoinServer(Game.Settings.NetworkHost, Game.Settings.NetworkPort); Game.JoinServer(Game.Settings.NetworkHost, Game.Settings.NetworkPort);
return true; return true;
} };
if (w.Id.Substring(0, 10) == "JOIN_GAME_") r.GetWidget("JOINSERVER_BUTTON_CANCEL").OnMouseUp = mi => {
{
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"); Game.chrome.rootWidget.ShowMenu("MAINMENU_BG");
return true; return true;
} };
return false;
} }
} }
} }

View File

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

View File

@@ -1,33 +0,0 @@
#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

@@ -37,7 +37,7 @@ namespace OpenRA.Widgets
public readonly string Height = "0"; public readonly string Height = "0";
public readonly string Delegate = null; public readonly string Delegate = null;
public Lazy<WidgetDelegate> InputHandler; public Lazy<IWidgetDelegate> InputHandler;
public bool Visible = true; public bool Visible = true;
public readonly List<Widget> Children = new List<Widget>(); public readonly List<Widget> Children = new List<Widget>();
@@ -81,19 +81,17 @@ namespace OpenRA.Widgets
.Aggregate(Bounds, Rectangle.Union); .Aggregate(Bounds, Rectangle.Union);
} }
static WidgetDelegate BindHandler(string name) static IWidgetDelegate BindHandler(string name)
{ {
if (name == null) return null; if (name == null) return null;
return Game.CreateObject<IWidgetDelegate>(name);
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));
} }
// Common Funcs that most widgets will want
public Func<MouseInput,bool> OnMouseDown = mi => {return false;};
public Func<MouseInput,bool> OnMouseUp = mi => {return false;};
public Func<MouseInput,bool> OnMouseMove = mi => {return false;};
public virtual bool HandleInput(MouseInput mi) public virtual bool HandleInput(MouseInput mi)
{ {
// Are we able to handle this event? // Are we able to handle this event?
@@ -107,15 +105,15 @@ namespace OpenRA.Widgets
// Mousedown // Mousedown
if (InputHandler.Value != null && mi.Event == MouseInputEvent.Down) if (InputHandler.Value != null && mi.Event == MouseInputEvent.Down)
return InputHandler.Value.OnMouseDown(this, mi); return OnMouseDown(mi);
// Mouseup // Mouseup
if (InputHandler.Value != null && mi.Event == MouseInputEvent.Up) if (InputHandler.Value != null && mi.Event == MouseInputEvent.Up)
return InputHandler.Value.OnMouseUp(this, mi); return OnMouseUp(mi);
// Mousemove // Mousemove
if (InputHandler.Value != null && mi.Event == MouseInputEvent.Move) if (InputHandler.Value != null && mi.Event == MouseInputEvent.Move)
return InputHandler.Value.OnMouseMove(this, mi); return OnMouseMove(mi);
return false; return false;
} }
@@ -138,11 +136,17 @@ namespace OpenRA.Widgets
return this; return this;
foreach (var child in Children) foreach (var child in Children)
if (child.GetWidget(id) != null) {
return child; var w = child.GetWidget(id);
if (w != null)
return w;
}
return null; return null;
} }
public T GetWidget<T>(string id) where T : Widget
{
return (T)GetWidget(id);
}
public Widget GetCurrentMenu() { return Children.FirstOrDefault(c => c.Visible); } public Widget GetCurrentMenu() { return Children.FirstOrDefault(c => c.Visible); }
@@ -159,4 +163,5 @@ namespace OpenRA.Widgets
} }
class ContainerWidget : Widget { } class ContainerWidget : Widget { }
public interface IWidgetDelegate { }
} }