From f70ac601353b3a38fb08f2745d69a21d88b691e1 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 12 Jul 2010 17:41:25 +1200 Subject: [PATCH] Widget refactoring to support listboxes --- OpenRA.Game/Widgets/BackgroundWidget.cs | 4 +--- OpenRA.Game/Widgets/ButtonWidget.cs | 6 ++--- OpenRA.Game/Widgets/ChatDisplayWidget.cs | 2 +- OpenRA.Game/Widgets/CheckboxWidget.cs | 4 ++-- OpenRA.Game/Widgets/ColorBlockWidget.cs | 2 +- OpenRA.Game/Widgets/ImageWidget.cs | 3 +-- OpenRA.Game/Widgets/LabelWidget.cs | 2 +- OpenRA.Game/Widgets/MapPreviewWidget.cs | 8 +++---- OpenRA.Game/Widgets/PerfGraphWidget.cs | 3 +-- OpenRA.Game/Widgets/SpecialPowerBinWidget.cs | 4 ++-- OpenRA.Game/Widgets/TextFieldWidget.cs | 2 +- OpenRA.Game/Widgets/Widget.cs | 23 ++++++++------------ 12 files changed, 25 insertions(+), 38 deletions(-) diff --git a/OpenRA.Game/Widgets/BackgroundWidget.cs b/OpenRA.Game/Widgets/BackgroundWidget.cs index 3c5f75e82e..1dcac979a3 100644 --- a/OpenRA.Game/Widgets/BackgroundWidget.cs +++ b/OpenRA.Game/Widgets/BackgroundWidget.cs @@ -27,9 +27,7 @@ namespace OpenRA.Widgets public override void DrawInner(World world) { - var pos = DrawPosition(); - var rect = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height); - WidgetUtils.DrawPanel(Background, rect); + WidgetUtils.DrawPanel(Background, RenderBounds); } public BackgroundWidget() : base() { } diff --git a/OpenRA.Game/Widgets/ButtonWidget.cs b/OpenRA.Game/Widgets/ButtonWidget.cs index bfac32414d..4b536e66b3 100644 --- a/OpenRA.Game/Widgets/ButtonWidget.cs +++ b/OpenRA.Game/Widgets/ButtonWidget.cs @@ -82,15 +82,13 @@ namespace OpenRA.Widgets public override void DrawInner(World world) { - var pos = DrawPosition(); var font = (Bold) ? Game.chrome.renderer.BoldFont : Game.chrome.renderer.RegularFont; var stateOffset = (Depressed) ? new int2(VisualHeight, VisualHeight) : new int2(0, 0); - WidgetUtils.DrawPanel(Depressed ? "dialog3" : "dialog2", - new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height ) ); + WidgetUtils.DrawPanel(Depressed ? "dialog3" : "dialog2", RenderBounds ); var text = GetText(); 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, font.Measure(text).Y / 2) + stateOffset, Color.White); } diff --git a/OpenRA.Game/Widgets/ChatDisplayWidget.cs b/OpenRA.Game/Widgets/ChatDisplayWidget.cs index 2898c55484..cac445a6cb 100644 --- a/OpenRA.Game/Widgets/ChatDisplayWidget.cs +++ b/OpenRA.Game/Widgets/ChatDisplayWidget.cs @@ -42,7 +42,7 @@ namespace OpenRA.Widgets 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 chatpos = new int2(chatLogArea.X + 10, chatLogArea.Bottom - 6); WidgetUtils.DrawPanel("dialog3", chatLogArea); diff --git a/OpenRA.Game/Widgets/CheckboxWidget.cs b/OpenRA.Game/Widgets/CheckboxWidget.cs index 1c43f52d22..39bc01baf8 100644 --- a/OpenRA.Game/Widgets/CheckboxWidget.cs +++ b/OpenRA.Game/Widgets/CheckboxWidget.cs @@ -33,8 +33,8 @@ namespace OpenRA.Widgets public override void DrawInner(World world) { var font = (Bold) ? Game.chrome.renderer.BoldFont : Game.chrome.renderer.RegularFont; - var pos = DrawPosition(); - var rect = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height); + var pos = RenderOrigin; + var rect = RenderBounds; WidgetUtils.DrawPanel("dialog3", new Rectangle(rect.Location, new Size(Bounds.Height, Bounds.Height))); diff --git a/OpenRA.Game/Widgets/ColorBlockWidget.cs b/OpenRA.Game/Widgets/ColorBlockWidget.cs index 72f4341425..2c40a17e56 100644 --- a/OpenRA.Game/Widgets/ColorBlockWidget.cs +++ b/OpenRA.Game/Widgets/ColorBlockWidget.cs @@ -46,7 +46,7 @@ namespace OpenRA.Widgets 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); Game.chrome.lineRenderer.FillRect(paletteRect, GetColor()); } diff --git a/OpenRA.Game/Widgets/ImageWidget.cs b/OpenRA.Game/Widgets/ImageWidget.cs index fe8ea459b8..40daeabe87 100644 --- a/OpenRA.Game/Widgets/ImageWidget.cs +++ b/OpenRA.Game/Widgets/ImageWidget.cs @@ -36,8 +36,7 @@ namespace OpenRA.Widgets { var name = GetImageName(); var collection = GetImageCollection(); - var position = DrawPosition(); - WidgetUtils.DrawRGBA(ChromeProvider.GetImage(Game.chrome.renderer, collection, name), position); + WidgetUtils.DrawRGBA(ChromeProvider.GetImage(Game.chrome.renderer, collection, name), RenderOrigin); } } } diff --git a/OpenRA.Game/Widgets/LabelWidget.cs b/OpenRA.Game/Widgets/LabelWidget.cs index cf438e5db1..a22c148a2a 100644 --- a/OpenRA.Game/Widgets/LabelWidget.cs +++ b/OpenRA.Game/Widgets/LabelWidget.cs @@ -60,7 +60,7 @@ namespace OpenRA.Widgets return; 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) position += new int2((Bounds.Width - textSize.X)/2, 0); diff --git a/OpenRA.Game/Widgets/MapPreviewWidget.cs b/OpenRA.Game/Widgets/MapPreviewWidget.cs index fb2f40f26b..08dd2a2e7b 100755 --- a/OpenRA.Game/Widgets/MapPreviewWidget.cs +++ b/OpenRA.Game/Widgets/MapPreviewWidget.cs @@ -38,7 +38,7 @@ namespace OpenRA.Widgets 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 .Select((sp, i) => Pair.New(map.ConvertToPreview(sp.Value, container), i)) @@ -64,9 +64,7 @@ namespace OpenRA.Widgets lastMap = map; } - var pos = DrawPosition(); - 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 ) ); + var mapRect = map.PreviewBounds( RenderBounds ); if( mapPreviewDirty ) { @@ -83,7 +81,7 @@ namespace OpenRA.Widgets "chrome", 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) diff --git a/OpenRA.Game/Widgets/PerfGraphWidget.cs b/OpenRA.Game/Widgets/PerfGraphWidget.cs index e66fc132b3..0f367b8549 100644 --- a/OpenRA.Game/Widgets/PerfGraphWidget.cs +++ b/OpenRA.Game/Widgets/PerfGraphWidget.cs @@ -34,8 +34,7 @@ namespace OpenRA.Widgets public override void DrawInner(World world) { - var pos = DrawPosition(); - var rect = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height); + var rect = RenderBounds; float2 origin = Game.viewport.Location + new float2(rect.Right,rect.Bottom); float2 basis = new float2(-rect.Width/100,-rect.Height/100); diff --git a/OpenRA.Game/Widgets/SpecialPowerBinWidget.cs b/OpenRA.Game/Widgets/SpecialPowerBinWidget.cs index 0be86ecd94..a391fdba1b 100644 --- a/OpenRA.Game/Widgets/SpecialPowerBinWidget.cs +++ b/OpenRA.Game/Widgets/SpecialPowerBinWidget.cs @@ -87,8 +87,8 @@ namespace OpenRA.Widgets var powers = world.LocalPlayer.PlayerActor.traits.WithInterface(); var numPowers = powers.Count(p => p.IsAvailable); if (numPowers == 0) return; - var position = DrawPosition(); - var rectBounds = new Rectangle(position.X, position.Y, Bounds.Width, Bounds.Height); + var position = RenderOrigin; + var rectBounds = RenderBounds; WidgetUtils.DrawRGBA(WidgetUtils.GetChromeImage(world, "specialbin-top"),new float2(rectBounds.X,rectBounds.Y)); for (var i = 1; i < numPowers; i++) WidgetUtils.DrawRGBA(WidgetUtils.GetChromeImage(world,"specialbin-middle"), new float2(rectBounds.X, rectBounds.Y + i * 51)); diff --git a/OpenRA.Game/Widgets/TextFieldWidget.cs b/OpenRA.Game/Widgets/TextFieldWidget.cs index b1cf312a07..0ba20e60f0 100644 --- a/OpenRA.Game/Widgets/TextFieldWidget.cs +++ b/OpenRA.Game/Widgets/TextFieldWidget.cs @@ -133,7 +133,7 @@ namespace OpenRA.Widgets var font = (Bold) ? Game.chrome.renderer.BoldFont : Game.chrome.renderer.RegularFont; var cursor = (showCursor && Chrome.selectedWidget == this) ? "|" : ""; var textSize = font.Measure(Text + "|"); - var pos = DrawPosition(); + var pos = RenderOrigin; WidgetUtils.DrawPanel("dialog3", new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height ) ); diff --git a/OpenRA.Game/Widgets/Widget.cs b/OpenRA.Game/Widgets/Widget.cs index b4e6b1cf53..bf78706d91 100644 --- a/OpenRA.Game/Widgets/Widget.cs +++ b/OpenRA.Game/Widgets/Widget.cs @@ -85,16 +85,15 @@ namespace OpenRA.Widgets 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 int2 ParentPosition() - { - return (Parent == null) ? int2.Zero : Parent.DrawPosition(); - } - + public virtual int2 ChildOrigin { get { return RenderOrigin; } } + public virtual Rectangle RenderBounds { get { return new Rectangle(RenderOrigin.X, RenderOrigin.Y, Bounds.Width, Bounds.Height); } } public virtual void Initialize() { @@ -139,21 +138,17 @@ namespace OpenRA.Widgets public bool HitTest(int2 xy) { if (!IsVisible()) return false; - var pos = DrawPosition(); - var rect = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height); - if (rect.Contains(xy.ToPoint()) && !ClickThrough) return true; + if (RenderBounds.Contains(xy.ToPoint()) && !ClickThrough) return true; return Children.Any(c => c.HitTest(xy)); } public Rectangle GetEventBounds() { - var pos = DrawPosition(); - var rect = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height); return Children .Where(c => c.Visible) .Select(c => c.GetEventBounds()) - .Aggregate(rect, Rectangle.Union); + .Aggregate(RenderBounds, Rectangle.Union); } public virtual bool HandleInput(MouseInput mi)