Mouse interaction

This commit is contained in:
Paul Chote
2010-03-14 17:49:59 +13:00
parent 287d5a0399
commit ca2630ffab
6 changed files with 57 additions and 5 deletions

View File

@@ -1077,6 +1077,9 @@ namespace OpenRA
int2 lastMousePos; int2 lastMousePos;
public bool HandleInput(World world, MouseInput mi) public bool HandleInput(World world, MouseInput mi)
{ {
if (rootWidget.HandleInput(mi))
return true;
if (mi.Event == MouseInputEvent.Move) if (mi.Event == MouseInputEvent.Move)
lastMousePos = mi.Location; lastMousePos = mi.Location;

View File

@@ -10,7 +10,7 @@ namespace OpenRA.Widgets
{ {
string collection = "dialog"; string collection = "dialog";
Rectangle r = new Rectangle(X,Y,Width,Height); Rectangle r = Bounds;
renderer.Device.EnableScissor(r.Left, r.Top, r.Width, r.Height); 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" }; string[] images = { "border-t", "border-b", "border-l", "border-r", "corner-tl", "corner-tr", "corner-bl", "corner-br", "background" };

View File

@@ -8,11 +8,23 @@ namespace OpenRA.Widgets
{ {
public readonly string Text = null; public readonly string Text = null;
public override bool HandleInput(MouseInput mi)
{
// TEMPORARY: Define a default mouse button event - quit the game
if (mi.Event == MouseInputEvent.Down && ClickRect.Contains(mi.Location.X,mi.Location.Y))
{
Game.Exit();
return true;
}
return base.HandleInput(mi);
}
public override void Draw(SpriteRenderer rgbaRenderer, Renderer renderer) public override void Draw(SpriteRenderer rgbaRenderer, Renderer renderer)
{ {
string collection = "dialog2"; string collection = "dialog2";
Rectangle r = new Rectangle(X,Y,Width,Height); Rectangle r = Bounds;
renderer.Device.EnableScissor(r.Left, r.Top, r.Width, r.Height); 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" }; string[] images = { "border-t", "border-b", "border-l", "border-r", "corner-tl", "corner-tr", "corner-bl", "corner-br", "background" };

View File

@@ -10,7 +10,7 @@ namespace OpenRA.Widgets
public override void Draw(SpriteRenderer rgbaRenderer, Renderer renderer) public override void Draw(SpriteRenderer rgbaRenderer, Renderer renderer)
{ {
Rectangle r = new Rectangle(X,Y,Width,Height); Rectangle r = Bounds;
renderer.Device.EnableScissor(r.Left, r.Top, r.Width, r.Height); renderer.Device.EnableScissor(r.Left, r.Top, r.Width, r.Height);
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); 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);
renderer.Device.DisableScissor(); renderer.Device.DisableScissor();

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Graphics; using OpenRA.Graphics;
@@ -12,12 +13,48 @@ namespace OpenRA.Widgets
public readonly int Width = 0; public readonly int Width = 0;
public readonly int Height = 0; public readonly int Height = 0;
public readonly List<Widget> Children = new List<Widget>(); public readonly List<Widget> Children = new List<Widget>();
public Rectangle Bounds
{
get {return new Rectangle(X,Y,Width, Height);}
}
public Rectangle ClickRect;
public virtual void Initialize()
{
// Create the clickrect
ClickRect = Bounds;
foreach (var child in Children)
ClickRect = Rectangle.Union(ClickRect, child.Bounds);
}
public virtual void Draw(SpriteRenderer rgbaRenderer, Renderer renderer) public virtual void Draw(SpriteRenderer rgbaRenderer, Renderer renderer)
{ {
foreach (var child in Children) foreach (var child in Children)
child.Draw(rgbaRenderer, renderer); child.Draw(rgbaRenderer, renderer);
} }
public virtual bool HandleInput(MouseInput mi)
{
bool caught = false;
if (ClickRect.Contains(mi.Location.X,mi.Location.Y))
{
foreach (var child in Children)
{
caught = child.HandleInput(mi);
if (caught)
break;
}
}
return caught;
}
public void AddChild(Widget child)
{
Children.Add( child );
}
} }
class ContainerWidget : Widget { } class ContainerWidget : Widget { }
} }

View File

@@ -36,13 +36,13 @@ namespace OpenRA
{ {
// Hack around a bug in MiniYaml // Hack around a bug in MiniYaml
c.Value.Value = c.Key; c.Value.Value = c.Key;
widget.Children.Add( LoadWidget( c.Value ) ); widget.AddChild( LoadWidget( c.Value ) );
} }
} }
else else
FieldLoader.LoadField( widget, child.Key, child.Value ); FieldLoader.LoadField( widget, child.Key, child.Value );
} }
widget.Initialize();
return widget; return widget;
} }