Refactoring

This commit is contained in:
Paul Chote
2010-03-15 18:21:23 +13:00
parent ef306cbcd5
commit 928c3a69c0
11 changed files with 208 additions and 225 deletions

View File

@@ -34,9 +34,9 @@ namespace OpenRA
{
class Chrome : IHandleInput
{
readonly Renderer renderer;
public readonly Renderer renderer;
public readonly SpriteRenderer rgbaRenderer;
readonly LineRenderer lineRenderer;
readonly SpriteRenderer rgbaRenderer;
readonly SpriteRenderer shpRenderer;
string chromeCollection;
@@ -424,7 +424,7 @@ namespace OpenRA
public void DrawMainMenu( World world )
{
WidgetLoader.rootWidget.Draw(rgbaRenderer,renderer);
WidgetLoader.rootWidget.Draw();
}
public void DrawLobby( World world )

View File

@@ -6,45 +6,49 @@ namespace OpenRA.Widgets
{
class BackgroundWidget : Widget
{
public override void Draw(SpriteRenderer rgbaRenderer, Renderer renderer)
public override void Draw()
{
if (Visible)
if (!Visible)
{
base.Draw();
return;
}
string collection = "dialog";
Rectangle r = Bounds;
renderer.Device.EnableScissor(r.Left, r.Top, r.Width, r.Height);
Game.chrome.renderer.Device.EnableScissor(r.Left, r.Top, r.Width, r.Height);
string[] images = { "border-t", "border-b", "border-l", "border-r", "corner-tl", "corner-tr", "corner-bl", "corner-br", "background" };
var ss = Graphics.Util.MakeArray(9, n => ChromeProvider.GetImage(renderer, collection,images[n]));
var ss = Graphics.Util.MakeArray(9, n => ChromeProvider.GetImage(Game.chrome.renderer, collection,images[n]));
for( var x = r.Left + (int)ss[2].size.X; x < r.Right - (int)ss[3].size.X; x += (int)ss[8].size.X )
for( var y = r.Top + (int)ss[0].size.Y; y < r.Bottom - (int)ss[1].size.Y; y += (int)ss[8].size.Y )
rgbaRenderer.DrawSprite(ss[8], new float2(x, y), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[8], new float2(x, y), "chrome");
//draw borders
for (var y = r.Top + (int)ss[0].size.Y; y < r.Bottom - (int)ss[1].size.Y; y += (int)ss[2].size.Y)
{
rgbaRenderer.DrawSprite(ss[2], new float2(r.Left, y), "chrome");
rgbaRenderer.DrawSprite(ss[3], new float2(r.Right - ss[3].size.X, y), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[2], new float2(r.Left, y), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[3], new float2(r.Right - ss[3].size.X, y), "chrome");
}
for (var x = r.Left + (int)ss[2].size.X; x < r.Right - (int)ss[3].size.X; x += (int)ss[0].size.X)
{
rgbaRenderer.DrawSprite(ss[0], new float2(x, r.Top), "chrome");
rgbaRenderer.DrawSprite(ss[1], new float2(x, r.Bottom - ss[1].size.Y), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[0], new float2(x, r.Top), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[1], new float2(x, r.Bottom - ss[1].size.Y), "chrome");
}
rgbaRenderer.DrawSprite(ss[4], new float2(r.Left, r.Top), "chrome");
rgbaRenderer.DrawSprite(ss[5], new float2(r.Right - ss[5].size.X, r.Top), "chrome");
rgbaRenderer.DrawSprite(ss[6], new float2(r.Left, r.Bottom - ss[6].size.Y), "chrome");
rgbaRenderer.DrawSprite(ss[7], new float2(r.Right - ss[7].size.X, r.Bottom - ss[7].size.Y), "chrome");
rgbaRenderer.Flush();
Game.chrome.rgbaRenderer.DrawSprite(ss[4], new float2(r.Left, r.Top), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[5], new float2(r.Right - ss[5].size.X, r.Top), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[6], new float2(r.Left, r.Bottom - ss[6].size.Y), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[7], new float2(r.Right - ss[7].size.X, r.Bottom - ss[7].size.Y), "chrome");
Game.chrome.rgbaRenderer.Flush();
renderer.Device.DisableScissor();
}
Game.chrome.renderer.Device.DisableScissor();
base.Draw(rgbaRenderer,renderer);
base.Draw();
}
}
}

View File

@@ -1,6 +1,4 @@
using OpenRA.Graphics;
using OpenRA.Widgets.Actions;
using System;
using System.Drawing;
using System.Collections.Generic;
@@ -9,65 +7,51 @@ namespace OpenRA.Widgets
class ButtonWidget : Widget
{
public readonly string Text = "";
public readonly string Action = null;
public override bool HandleInput(MouseInput mi)
public override void Draw()
{
if (Action != null && mi.Event == MouseInputEvent.Down && ClickRect.Contains(mi.Location.X,mi.Location.Y))
if (!Visible)
{
foreach (var mod in WidgetLoader.WidgetActionAssemblies)
{
var act = (IWidgetAction)mod.First.CreateInstance(mod.Second + "."+Action);
if (act == null) return false;
Log.Write("Calling: "+mod.Second + "."+Action);
return act.OnClick(mi);
}
throw new InvalidOperationException("Cannot locate WidgetAction: {0}".F(Action));
}
return base.HandleInput(mi);
base.Draw();
return;
}
public override void Draw(SpriteRenderer rgbaRenderer, Renderer renderer)
{
if (Visible)
{
string collection = "dialog2";
Rectangle r = Bounds;
renderer.Device.EnableScissor(r.Left, r.Top, r.Width, r.Height);
Game.chrome.renderer.Device.EnableScissor(r.Left, r.Top, r.Width, r.Height);
string[] images = { "border-t", "border-b", "border-l", "border-r", "corner-tl", "corner-tr", "corner-bl", "corner-br", "background" };
var ss = Graphics.Util.MakeArray(9, n => ChromeProvider.GetImage(renderer, collection,images[n]));
var ss = Graphics.Util.MakeArray(9, n => ChromeProvider.GetImage(Game.chrome.renderer, collection,images[n]));
for( var x = r.Left + (int)ss[2].size.X; x < r.Right - (int)ss[3].size.X; x += (int)ss[8].size.X )
for( var y = r.Top + (int)ss[0].size.Y; y < r.Bottom - (int)ss[1].size.Y; y += (int)ss[8].size.Y )
rgbaRenderer.DrawSprite(ss[8], new float2(x, y), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[8], new float2(x, y), "chrome");
//draw borders
for (var y = r.Top + (int)ss[0].size.Y; y < r.Bottom - (int)ss[1].size.Y; y += (int)ss[2].size.Y)
{
rgbaRenderer.DrawSprite(ss[2], new float2(r.Left, y), "chrome");
rgbaRenderer.DrawSprite(ss[3], new float2(r.Right - ss[3].size.X, y), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[2], new float2(r.Left, y), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[3], new float2(r.Right - ss[3].size.X, y), "chrome");
}
for (var x = r.Left + (int)ss[2].size.X; x < r.Right - (int)ss[3].size.X; x += (int)ss[0].size.X)
{
rgbaRenderer.DrawSprite(ss[0], new float2(x, r.Top), "chrome");
rgbaRenderer.DrawSprite(ss[1], new float2(x, r.Bottom - ss[1].size.Y), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[0], new float2(x, r.Top), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[1], new float2(x, r.Bottom - ss[1].size.Y), "chrome");
}
rgbaRenderer.DrawSprite(ss[4], new float2(r.Left, r.Top), "chrome");
rgbaRenderer.DrawSprite(ss[5], new float2(r.Right - ss[5].size.X, r.Top), "chrome");
rgbaRenderer.DrawSprite(ss[6], new float2(r.Left, r.Bottom - ss[6].size.Y), "chrome");
rgbaRenderer.DrawSprite(ss[7], new float2(r.Right - ss[7].size.X, r.Bottom - ss[7].size.Y), "chrome");
rgbaRenderer.Flush();
Game.chrome.rgbaRenderer.DrawSprite(ss[4], new float2(r.Left, r.Top), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[5], new float2(r.Right - ss[5].size.X, r.Top), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[6], new float2(r.Left, r.Bottom - ss[6].size.Y), "chrome");
Game.chrome.rgbaRenderer.DrawSprite(ss[7], new float2(r.Right - ss[7].size.X, r.Bottom - ss[7].size.Y), "chrome");
Game.chrome.rgbaRenderer.Flush();
renderer.BoldFont.DrawText(rgbaRenderer, Text, new int2(X+Width/2, Y+Height/2) - new int2(renderer.BoldFont.Measure(Text).X / 2, renderer.BoldFont.Measure(Text).Y/2), Color.White);
Game.chrome.renderer.BoldFont.DrawText(Game.chrome.rgbaRenderer, Text, new int2(X+Width/2, Y+Height/2) - new int2(Game.chrome.renderer.BoldFont.Measure(Text).X / 2, Game.chrome.renderer.BoldFont.Measure(Text).Y/2), Color.White);
renderer.Device.DisableScissor();
}
Game.chrome.renderer.Device.DisableScissor();
base.Draw(rgbaRenderer,renderer);
base.Draw();
}
}
}

View File

@@ -9,24 +9,27 @@ namespace OpenRA.Widgets
public readonly string Text = null;
public readonly string Align = "Left";
public override void Draw(SpriteRenderer rgbaRenderer, Renderer renderer)
public override void Draw()
{
if (Visible)
if (!Visible)
{
Rectangle r = Bounds;
renderer.Device.EnableScissor(r.Left, r.Top, r.Width, r.Height);
base.Draw();
return;
}
int2 bounds = renderer.BoldFont.Measure(Text);
Rectangle r = Bounds;
Game.chrome.renderer.Device.EnableScissor(r.Left, r.Top, r.Width, r.Height);
int2 bounds = Game.chrome.renderer.BoldFont.Measure(Text);
int2 position = new int2(X,Y);
if (Align == "Center")
position = new int2(X+Width/2, Y+Height/2) - new int2(bounds.X / 2, bounds.Y/2);
renderer.BoldFont.DrawText(rgbaRenderer, Text, position, Color.White);
renderer.Device.DisableScissor();
}
base.Draw(rgbaRenderer,renderer);
Game.chrome.renderer.BoldFont.DrawText(Game.chrome.rgbaRenderer, Text, position, Color.White);
Game.chrome.renderer.Device.DisableScissor();
base.Draw();
}
}
}

View File

@@ -1,24 +1,27 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Widgets.Delegates;
namespace OpenRA.Widgets
{
class Widget
public class Widget
{
// Typically defined in YAML
public readonly string Id = null;
public readonly int X = 0;
public readonly int Y = 0;
public readonly int Width = 0;
public readonly int Height = 0;
public readonly string Delegate = null;
public bool Visible = true;
public readonly List<Widget> Children = new List<Widget>();
public Rectangle Bounds
{
get {return new Rectangle(X,Y,Width, Height);}
}
// Calculated internally
public Rectangle Bounds {get {return new Rectangle(X,Y,Width, Height);}}
public Rectangle ClickRect;
public virtual void Initialize()
@@ -30,18 +33,12 @@ namespace OpenRA.Widgets
}
public virtual void Draw(SpriteRenderer rgbaRenderer, Renderer renderer)
{
if (Visible)
foreach (var child in Children)
child.Draw(rgbaRenderer, renderer);
}
public virtual bool HandleInput(MouseInput mi)
{
if (!Visible)
return false;
// Do any of our children handle this?
bool caught = false;
if (ClickRect.Contains(mi.Location.X,mi.Location.Y))
{
@@ -53,7 +50,31 @@ namespace OpenRA.Widgets
}
}
return caught;
// Child has handled the event
if (caught)
return true;
// Mousedown
if (Delegate != null && mi.Event == MouseInputEvent.Down && ClickRect.Contains(mi.Location.X,mi.Location.Y))
{
foreach (var mod in Game.ModAssemblies)
{
var act = (IWidgetDelegate)mod.First.CreateInstance(mod.Second + "."+Delegate);
if (act == null) continue;
return act.OnClick(this, mi);
}
throw new InvalidOperationException("Cannot locate widget delegate: {0}".F(Delegate));
}
return false;
}
public virtual void Draw()
{
if (Visible)
foreach (var child in Children)
child.Draw();
}
public void AddChild(Widget child)

View File

@@ -3,54 +3,49 @@ using System.Drawing;
using OpenRA.FileFormats;
using OpenRA.Graphics;
namespace OpenRA.Widgets.Actions
namespace OpenRA.Widgets.Delegates
{
public interface IWidgetAction { bool OnClick(MouseInput mi); }
public interface IWidgetDelegate { bool OnClick(Widget w, MouseInput mi); }
public class QuitButtonAction : IWidgetAction
public class MainMenuButtonsDelegate : IWidgetDelegate
{
public bool OnClick(MouseInput mi)
public bool OnClick(Widget w, MouseInput mi)
{
// Main Menu root
if (w.Id == "MAINMENU_BUTTON_QUIT")
{
Game.Exit();
return true;
}
}
public class JoinServerButtonAction : IWidgetAction
{
public bool OnClick(MouseInput mi)
if (w.Id == "MAINMENU_BUTTON_JOIN")
{
Game.JoinServer(Game.Settings.NetworkHost, Game.Settings.NetworkPort);
return true;
}
}
public class OpenCreateServerMenuButtonAction : IWidgetAction
{
public bool OnClick(MouseInput mi)
if (w.Id == "MAINMENU_BUTTON_CREATE")
{
WidgetLoader.rootWidget.GetWidget("MAINMENU_BG").Visible = false;
WidgetLoader.rootWidget.GetWidget("CREATESERVER_BG").Visible = true;
return true;
}
}
public class CloseCreateServerMenuButtonAction : IWidgetAction
{
public bool OnClick(MouseInput mi)
// "Create Server" submenu
if (w.Id == "CREATESERVER_BUTTON_CANCEL")
{
WidgetLoader.rootWidget.GetWidget("MAINMENU_BG").Visible = true;
WidgetLoader.rootWidget.GetWidget("CREATESERVER_BG").Visible = false;
return true;
}
}
public class CreateServerMenuButtonAction : IWidgetAction
{
public bool OnClick(MouseInput mi)
if (w.Id == "CREATESERVER_BUTTON_START")
{
Game.CreateServer();
return true;
}
return false;
}
}
}

View File

@@ -1,42 +1,17 @@
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Widgets;
using OpenRA.Widgets.Actions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Linq;
namespace OpenRA
{
class WidgetLoader
{
static Pair<Assembly, string>[] ModAssemblies;
public static Pair<Assembly, string>[] WidgetActionAssemblies;
public static void LoadModAssemblies(Manifest m)
{
var asms = new List<Pair<Assembly, string>>();
// all the core stuff is in this assembly
asms.Add(Pair.New(typeof(Widget).Assembly, typeof(Widget).Namespace));
// add the mods
foreach (var a in m.Assemblies)
asms.Add(Pair.New(Assembly.LoadFile(Path.GetFullPath(a)), Path.GetFileNameWithoutExtension(a)));
ModAssemblies = asms.ToArray();
asms.Clear();
// all the core stuff is in this assembly
asms.Add(Pair.New(typeof(IWidgetAction).Assembly, typeof(IWidgetAction).Namespace));
// add the mods
foreach (var a in m.Assemblies)
asms.Add(Pair.New(Assembly.LoadFile(Path.GetFullPath(a)), Path.GetFileNameWithoutExtension(a)));
WidgetActionAssemblies = asms.ToArray();
}
public static Widget rootWidget;
public static Widget LoadWidget( MiniYaml node )
{
@@ -64,7 +39,7 @@ namespace OpenRA
if( widgetType.Contains( "@" ) )
widgetType = widgetType.Substring( 0, widgetType.IndexOf( "@" ) );
foreach (var mod in ModAssemblies)
foreach (var mod in Game.ModAssemblies)
{
var fullTypeName = mod.Second + "." + widgetType + "Widget";
var widget = (Widget)mod.First.CreateInstance(fullTypeName);

View File

@@ -25,6 +25,7 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Windows.Forms;
using System.Reflection;
using OpenRA.FileFormats;
using OpenRA.GameRules;
using OpenRA.Graphics;
@@ -54,8 +55,7 @@ namespace OpenRA
static string mapName;
internal static Session LobbyInfo = new Session();
static bool changePending;
public static Pair<Assembly, string>[] ModAssemblies;
public static void LoadModPackages(Manifest manifest)
{
@@ -68,13 +68,32 @@ namespace OpenRA
Timer.Time("mount temporary packages: {0}");
}
internal static void LoadModAssemblies(Manifest m)
{
// All the core namespaces
var asms = new List<Pair<Assembly, string>> (typeof(Game).Assembly.GetTypes()
.Select(a => a.Namespace)
.Distinct()
.Select(b => Pair.New(typeof(Game).Assembly, b)));
// Mod assemblies assumed to contain a single namespace
foreach (var a in m.Assemblies)
asms.Add(Pair.New(Assembly.LoadFile(Path.GetFullPath(a)), Path.GetFileNameWithoutExtension(a)));
ModAssemblies = asms.ToArray();
foreach(var foo in ModAssemblies)
{
Log.Write("Tracking namespace {0} for reflection",foo.Second);
}
}
public static void ChangeMap(string mapName)
{
Timer.Time( "----ChangeMap" );
var manifest = new Manifest(LobbyInfo.GlobalSettings.Mods);
Timer.Time( "manifest: {0}" );
Game.LoadModAssemblies(manifest);
Game.changePending = false;
Game.mapName = mapName;
SheetBuilder.Initialize(renderer);

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.
* This file is part of OpenRA.
@@ -20,9 +20,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using OpenRA.FileFormats;
using OpenRA.Traits;
@@ -71,26 +69,12 @@ namespace OpenRA.GameRules
return node;
}
static Pair<Assembly, string>[] ModAssemblies;
public static void LoadModAssemblies(Manifest m)
{
var asms = new List<Pair<Assembly, string>>();
// all the core stuff is in this assembly
asms.Add(Pair.New(typeof(ITraitInfo).Assembly, typeof(ITraitInfo).Namespace));
// add the mods
foreach (var a in m.Assemblies)
asms.Add(Pair.New(Assembly.LoadFile(Path.GetFullPath(a)), Path.GetFileNameWithoutExtension(a)));
ModAssemblies = asms.ToArray();
}
static ITraitInfo LoadTraitInfo(string traitName, MiniYaml my)
{
if (traitName.Contains('@'))
traitName = traitName.Substring(0, traitName.IndexOf('@'));
foreach (var mod in ModAssemblies)
foreach (var mod in Game.ModAssemblies)
{
var fullTypeName = mod.Second + "." + traitName + "Info";
var info = (ITraitInfo)mod.First.CreateInstance(fullTypeName);

View File

@@ -65,8 +65,6 @@ namespace OpenRA
var yamlRules = m.Rules.Reverse().Select(a => MiniYaml.FromFile(a)).Aggregate(MiniYaml.Merge);
ActorInfo.LoadModAssemblies(m);
WidgetLoader.LoadModAssemblies(m);
Info = new Dictionary<string, ActorInfo>();
foreach( var kv in yamlRules )
Info.Add(kv.Key.ToLowerInvariant(), new ActorInfo(kv.Key.ToLowerInvariant(), kv.Value, yamlRules));

View File

@@ -7,7 +7,7 @@ Container:
Width:250
Height:200
Children:
Label@MAINMENU_LABEL_TITLE:
Label@MAINMENDelegateU_LABEL_TITLE:
Id:MAINMENU_LABEL_TITLE
X:445
Y:220
@@ -22,7 +22,7 @@ Container:
Width:160
Height:25
Text:Join Game
Action:JoinServerButtonAction
Delegate:MainMenuButtonsDelegate
Button@MAINMENU_BUTTON_CREATE:
Id:MAINMENU_BUTTON_CREATE
X:445
@@ -30,7 +30,7 @@ Container:
Width:160
Height:25
Text:Create Game
Action:OpenCreateServerMenuButtonAction
Delegate:MainMenuButtonsDelegate
Button@MAINMENU_BUTTON_QUIT:
Id:MAINMENU_BUTTON_QUIT
X:445
@@ -38,7 +38,7 @@ Container:
Width:160
Height:25
Text:Quit
Action:QuitButtonAction
Delegate:MainMenuButtonsDelegate
Background@CREATESERVER_BG:
Id:CREATESERVER_BG
X:300
@@ -75,7 +75,7 @@ Container:
Width:160
Height:25
Text:Cancel
Action:CloseCreateServerMenuButtonAction
Delegate:MainMenuButtonsDelegate
Button@CREATESERVER_BUTTON_START:
Id:CREATESERVER_BUTTON_START
X:570
@@ -83,4 +83,4 @@ Container:
Width:160
Height:25
Text:Create
Action:CreateServerMenuButtonAction
Delegate:MainMenuButtonsDelegate