Widget refactoring to support listboxes

This commit is contained in:
Paul Chote
2010-07-12 17:41:25 +12:00
parent d740cf9fcd
commit f70ac60135
12 changed files with 25 additions and 38 deletions

View File

@@ -27,9 +27,7 @@ namespace OpenRA.Widgets
public override void DrawInner(World world) public override void DrawInner(World world)
{ {
var pos = DrawPosition(); WidgetUtils.DrawPanel(Background, RenderBounds);
var rect = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height);
WidgetUtils.DrawPanel(Background, rect);
} }
public BackgroundWidget() : base() { } public BackgroundWidget() : base() { }

View File

@@ -82,15 +82,13 @@ namespace OpenRA.Widgets
public override void DrawInner(World world) public override void DrawInner(World world)
{ {
var pos = DrawPosition();
var font = (Bold) ? Game.chrome.renderer.BoldFont : Game.chrome.renderer.RegularFont; var font = (Bold) ? Game.chrome.renderer.BoldFont : Game.chrome.renderer.RegularFont;
var stateOffset = (Depressed) ? new int2(VisualHeight, VisualHeight) : new int2(0, 0); var stateOffset = (Depressed) ? new int2(VisualHeight, VisualHeight) : new int2(0, 0);
WidgetUtils.DrawPanel(Depressed ? "dialog3" : "dialog2", WidgetUtils.DrawPanel(Depressed ? "dialog3" : "dialog2", RenderBounds );
new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height ) );
var text = GetText(); var text = GetText();
font.DrawText(text, font.DrawText(text,
new int2( pos.X + Bounds.Width / 2, pos.Y + Bounds.Height / 2) new int2( RenderOrigin.X + Bounds.Width / 2, RenderOrigin.Y + Bounds.Height / 2)
- new int2(font.Measure(text).X / 2, - new int2(font.Measure(text).X / 2,
font.Measure(text).Y / 2) + stateOffset, Color.White); font.Measure(text).Y / 2) + stateOffset, Color.White);
} }

View File

@@ -42,7 +42,7 @@ namespace OpenRA.Widgets
public override void DrawInner(World world) public override void DrawInner(World world)
{ {
var pos = DrawPosition(); var pos = RenderOrigin;
var chatLogArea = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height); var chatLogArea = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height);
var chatpos = new int2(chatLogArea.X + 10, chatLogArea.Bottom - 6); var chatpos = new int2(chatLogArea.X + 10, chatLogArea.Bottom - 6);
WidgetUtils.DrawPanel("dialog3", chatLogArea); WidgetUtils.DrawPanel("dialog3", chatLogArea);

View File

@@ -33,8 +33,8 @@ namespace OpenRA.Widgets
public override void DrawInner(World world) public override void DrawInner(World world)
{ {
var font = (Bold) ? Game.chrome.renderer.BoldFont : Game.chrome.renderer.RegularFont; var font = (Bold) ? Game.chrome.renderer.BoldFont : Game.chrome.renderer.RegularFont;
var pos = DrawPosition(); var pos = RenderOrigin;
var rect = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height); var rect = RenderBounds;
WidgetUtils.DrawPanel("dialog3", new Rectangle(rect.Location, WidgetUtils.DrawPanel("dialog3", new Rectangle(rect.Location,
new Size(Bounds.Height, Bounds.Height))); new Size(Bounds.Height, Bounds.Height)));

View File

@@ -46,7 +46,7 @@ namespace OpenRA.Widgets
public override void DrawInner(World world) public override void DrawInner(World world)
{ {
var pos = DrawPosition(); var pos = RenderOrigin;
var paletteRect = new RectangleF(pos.X + Game.viewport.Location.X, pos.Y + Game.viewport.Location.Y, Bounds.Width, Bounds.Height); var paletteRect = new RectangleF(pos.X + Game.viewport.Location.X, pos.Y + Game.viewport.Location.Y, Bounds.Width, Bounds.Height);
Game.chrome.lineRenderer.FillRect(paletteRect, GetColor()); Game.chrome.lineRenderer.FillRect(paletteRect, GetColor());
} }

View File

@@ -36,8 +36,7 @@ namespace OpenRA.Widgets
{ {
var name = GetImageName(); var name = GetImageName();
var collection = GetImageCollection(); var collection = GetImageCollection();
var position = DrawPosition(); WidgetUtils.DrawRGBA(ChromeProvider.GetImage(Game.chrome.renderer, collection, name), RenderOrigin);
WidgetUtils.DrawRGBA(ChromeProvider.GetImage(Game.chrome.renderer, collection, name), position);
} }
} }
} }

View File

@@ -60,7 +60,7 @@ namespace OpenRA.Widgets
return; return;
int2 textSize = font.Measure(text); int2 textSize = font.Measure(text);
int2 position = DrawPosition() + new int2(0, (Bounds.Height - textSize.Y)/2); int2 position = RenderOrigin + new int2(0, (Bounds.Height - textSize.Y)/2);
if (Align == TextAlign.Center) if (Align == TextAlign.Center)
position += new int2((Bounds.Width - textSize.X)/2, 0); position += new int2((Bounds.Width - textSize.X)/2, 0);

View File

@@ -38,7 +38,7 @@ namespace OpenRA.Widgets
if (mi.Event == MouseInputEvent.Down && mi.Button == MouseButton.Left) if (mi.Event == MouseInputEvent.Down && mi.Button == MouseButton.Left)
{ {
var container = new Rectangle(DrawPosition().X, DrawPosition().Y, Parent.Bounds.Width, Parent.Bounds.Height); var container = new Rectangle(RenderOrigin.X, RenderOrigin.Y, Parent.Bounds.Width, Parent.Bounds.Height);
var p = map.Waypoints var p = map.Waypoints
.Select((sp, i) => Pair.New(map.ConvertToPreview(sp.Value, container), i)) .Select((sp, i) => Pair.New(map.ConvertToPreview(sp.Value, container), i))
@@ -64,9 +64,7 @@ namespace OpenRA.Widgets
lastMap = map; lastMap = map;
} }
var pos = DrawPosition(); var mapRect = map.PreviewBounds( RenderBounds );
var rect = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height);
var mapRect = map.PreviewBounds( new Rectangle( rect.X, rect.Y, rect.Width, rect.Height ) );
if( mapPreviewDirty ) if( mapPreviewDirty )
{ {
@@ -83,7 +81,7 @@ namespace OpenRA.Widgets
"chrome", "chrome",
new float2( mapRect.Size ) ); new float2( mapRect.Size ) );
DrawSpawnPoints( map, new Rectangle(pos.X, pos.Y, Parent.Bounds.Width, Parent.Bounds.Height ), world ); DrawSpawnPoints( map, new Rectangle(RenderOrigin.X, RenderOrigin.Y, Parent.Bounds.Width, Parent.Bounds.Height ), world );
} }
void DrawSpawnPoints(MapStub map, Rectangle container, World world) void DrawSpawnPoints(MapStub map, Rectangle container, World world)

View File

@@ -34,8 +34,7 @@ namespace OpenRA.Widgets
public override void DrawInner(World world) public override void DrawInner(World world)
{ {
var pos = DrawPosition(); var rect = RenderBounds;
var rect = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height);
float2 origin = Game.viewport.Location + new float2(rect.Right,rect.Bottom); float2 origin = Game.viewport.Location + new float2(rect.Right,rect.Bottom);
float2 basis = new float2(-rect.Width/100,-rect.Height/100); float2 basis = new float2(-rect.Width/100,-rect.Height/100);

View File

@@ -87,8 +87,8 @@ namespace OpenRA.Widgets
var powers = world.LocalPlayer.PlayerActor.traits.WithInterface<SupportPower>(); var powers = world.LocalPlayer.PlayerActor.traits.WithInterface<SupportPower>();
var numPowers = powers.Count(p => p.IsAvailable); var numPowers = powers.Count(p => p.IsAvailable);
if (numPowers == 0) return; if (numPowers == 0) return;
var position = DrawPosition(); var position = RenderOrigin;
var rectBounds = new Rectangle(position.X, position.Y, Bounds.Width, Bounds.Height); var rectBounds = RenderBounds;
WidgetUtils.DrawRGBA(WidgetUtils.GetChromeImage(world, "specialbin-top"),new float2(rectBounds.X,rectBounds.Y)); WidgetUtils.DrawRGBA(WidgetUtils.GetChromeImage(world, "specialbin-top"),new float2(rectBounds.X,rectBounds.Y));
for (var i = 1; i < numPowers; i++) for (var i = 1; i < numPowers; i++)
WidgetUtils.DrawRGBA(WidgetUtils.GetChromeImage(world,"specialbin-middle"), new float2(rectBounds.X, rectBounds.Y + i * 51)); WidgetUtils.DrawRGBA(WidgetUtils.GetChromeImage(world,"specialbin-middle"), new float2(rectBounds.X, rectBounds.Y + i * 51));

View File

@@ -133,7 +133,7 @@ namespace OpenRA.Widgets
var font = (Bold) ? Game.chrome.renderer.BoldFont : Game.chrome.renderer.RegularFont; var font = (Bold) ? Game.chrome.renderer.BoldFont : Game.chrome.renderer.RegularFont;
var cursor = (showCursor && Chrome.selectedWidget == this) ? "|" : ""; var cursor = (showCursor && Chrome.selectedWidget == this) ? "|" : "";
var textSize = font.Measure(Text + "|"); var textSize = font.Measure(Text + "|");
var pos = DrawPosition(); var pos = RenderOrigin;
WidgetUtils.DrawPanel("dialog3", WidgetUtils.DrawPanel("dialog3",
new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height ) ); new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height ) );

View File

@@ -85,16 +85,15 @@ namespace OpenRA.Widgets
public abstract Widget Clone(); public abstract Widget Clone();
public int2 DrawPosition() public virtual int2 RenderOrigin
{ {
return new int2(Bounds.X, Bounds.Y) + ParentPosition(); get {
var offset = (Parent == null) ? int2.Zero : Parent.ChildOrigin;
return new int2(Bounds.X, Bounds.Y) + offset;
}
} }
public virtual int2 ChildOrigin { get { return RenderOrigin; } }
public int2 ParentPosition() public virtual Rectangle RenderBounds { get { return new Rectangle(RenderOrigin.X, RenderOrigin.Y, Bounds.Width, Bounds.Height); } }
{
return (Parent == null) ? int2.Zero : Parent.DrawPosition();
}
public virtual void Initialize() public virtual void Initialize()
{ {
@@ -139,21 +138,17 @@ namespace OpenRA.Widgets
public bool HitTest(int2 xy) public bool HitTest(int2 xy)
{ {
if (!IsVisible()) return false; if (!IsVisible()) return false;
var pos = DrawPosition(); if (RenderBounds.Contains(xy.ToPoint()) && !ClickThrough) return true;
var rect = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height);
if (rect.Contains(xy.ToPoint()) && !ClickThrough) return true;
return Children.Any(c => c.HitTest(xy)); return Children.Any(c => c.HitTest(xy));
} }
public Rectangle GetEventBounds() public Rectangle GetEventBounds()
{ {
var pos = DrawPosition();
var rect = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height);
return Children return Children
.Where(c => c.Visible) .Where(c => c.Visible)
.Select(c => c.GetEventBounds()) .Select(c => c.GetEventBounds())
.Aggregate(rect, Rectangle.Union); .Aggregate(RenderBounds, Rectangle.Union);
} }
public virtual bool HandleInput(MouseInput mi) public virtual bool HandleInput(MouseInput mi)