From df460d7407cbea30bb8566c35e6d5d741db4d48d Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Wed, 5 Jan 2011 22:24:11 +1300 Subject: [PATCH] Add extension methods to Rectangle/RectangleF, remove a pile of ToPoint()'s. --- OpenRA.FileFormats/Exts.cs | 11 +++++++++++ OpenRA.Game/Widgets/SliderWidget.cs | 6 +++--- OpenRA.Game/Widgets/Widget.cs | 4 ++-- OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs | 8 ++++---- OpenRA.Mods.RA/Widgets/OrderButtonWidget.cs | 2 +- OpenRA.Mods.RA/Widgets/RadarBinWidget.cs | 2 +- OpenRA.Mods.RA/Widgets/SpecialPowerBinWidget.cs | 4 ++-- 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/OpenRA.FileFormats/Exts.cs b/OpenRA.FileFormats/Exts.cs index 8ef0f7986f..916e458336 100755 --- a/OpenRA.FileFormats/Exts.cs +++ b/OpenRA.FileFormats/Exts.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; +using System.Drawing; namespace OpenRA { @@ -95,5 +96,15 @@ namespace OpenRA else return val; } + + public static bool Contains(this Rectangle r, int2 p) + { + return r.Contains(p.ToPoint()); + } + + public static bool Contains(this RectangleF r, int2 p) + { + return r.Contains(p.ToPointF()); + } } } diff --git a/OpenRA.Game/Widgets/SliderWidget.cs b/OpenRA.Game/Widgets/SliderWidget.cs index 79bb2c2d0e..10cf3f2c28 100755 --- a/OpenRA.Game/Widgets/SliderWidget.cs +++ b/OpenRA.Game/Widgets/SliderWidget.cs @@ -88,7 +88,7 @@ namespace OpenRA.Widgets case MouseInputEvent.Down: { - if (thumbRect.Contains(mi.Location.ToPoint())) + if (thumbRect.Contains(mi.Location)) { isMoving = true; lastMouseLocation = mi.Location; @@ -103,7 +103,7 @@ namespace OpenRA.Widgets : Math.Floor((pos - delta) * (Ticks - 1))); OnChange(targetTick / (Ticks - 1)); - if (thumbRect.Contains(mi.Location.ToPoint())) + if (thumbRect.Contains(mi.Location)) { isMoving = true; lastMouseLocation = mi.Location; @@ -119,7 +119,7 @@ namespace OpenRA.Widgets { OnChange(newOffset); - if (thumbRect.Contains(mi.Location.ToPoint())) + if (thumbRect.Contains(mi.Location)) { isMoving = true; lastMouseLocation = mi.Location; diff --git a/OpenRA.Game/Widgets/Widget.cs b/OpenRA.Game/Widgets/Widget.cs index 5ebbb27eba..8c8e2bc1a0 100644 --- a/OpenRA.Game/Widgets/Widget.cs +++ b/OpenRA.Game/Widgets/Widget.cs @@ -180,7 +180,7 @@ namespace OpenRA.Widgets public string GetCursorOuter(int2 pos) { // Is the cursor on top of us? - if (!(IsVisible() && GetEventBounds().Contains(pos.ToPoint()))) + if (!(IsVisible() && GetEventBounds().Contains(pos))) return null; // Do any of our children specify a cursor? @@ -191,7 +191,7 @@ namespace OpenRA.Widgets return cc; } - return EventBounds.Contains(pos.ToPoint()) ? GetCursor(pos) : null; + return EventBounds.Contains(pos) ? GetCursor(pos) : null; } public static bool HandleInput(MouseInput mi) diff --git a/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs b/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs index 49d6bd1a8e..709c603a08 100755 --- a/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs +++ b/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs @@ -162,10 +162,10 @@ namespace OpenRA.Mods.RA.Widgets if (mi.Event != MouseInputEvent.Down) return false; - var action = tabs.Where(a => a.First.Contains(mi.Location.ToPoint())) + var action = tabs.Where(a => a.First.Contains(mi.Location)) .Select(a => a.Second).FirstOrDefault(); if (action == null && paletteOpen) - action = buttons.Where(a => a.First.Contains(mi.Location.ToPoint())) + action = buttons.Where(a => a.First.Contains(mi.Location)) .Select(a => a.Second).FirstOrDefault(); if (action == null) @@ -223,7 +223,7 @@ namespace OpenRA.Mods.RA.Widgets var firstOfThis = queue.AllQueued().FirstOrDefault(a => a.Item == item.Name); - if (rect.Contains(Viewport.LastMousePos.ToPoint())) + if (rect.Contains(Viewport.LastMousePos)) tooltipItem = item.Name; var overlayPos = drawPos + new float2((64 - ready.Image.size.X) / 2, 2); @@ -412,7 +412,7 @@ namespace OpenRA.Mods.RA.Widgets var rect = new Rectangle((int)x,(int)y,(int)tabWidth,(int)tabHeight); tabs.Add(Pair.New(rect, HandleTabClick(queue, world))); - if (rect.Contains(Viewport.LastMousePos.ToPoint())) + if (rect.Contains(Viewport.LastMousePos)) { var text = queue.Info.Type; var sz = Game.Renderer.BoldFont.Measure(text); diff --git a/OpenRA.Mods.RA/Widgets/OrderButtonWidget.cs b/OpenRA.Mods.RA/Widgets/OrderButtonWidget.cs index 1ca3562cc3..5ab9187e16 100755 --- a/OpenRA.Mods.RA/Widgets/OrderButtonWidget.cs +++ b/OpenRA.Mods.RA/Widgets/OrderButtonWidget.cs @@ -36,7 +36,7 @@ namespace OpenRA.Mods.RA.Widgets var image = ChromeProvider.GetImage(Image + "-button", GetImage()); var rect = new Rectangle(RenderBounds.X, RenderBounds.Y, (int)image.size.X, (int)image.size.Y); - if (rect.Contains(Viewport.LastMousePos.ToPoint())) + if (rect.Contains(Viewport.LastMousePos)) { rect = rect.InflateBy(3, 3, 3, 3); var pos = new int2(rect.Left, rect.Top); diff --git a/OpenRA.Mods.RA/Widgets/RadarBinWidget.cs b/OpenRA.Mods.RA/Widgets/RadarBinWidget.cs index c4894ae43a..a3a2a65a1f 100755 --- a/OpenRA.Mods.RA/Widgets/RadarBinWidget.cs +++ b/OpenRA.Mods.RA/Widgets/RadarBinWidget.cs @@ -88,7 +88,7 @@ namespace OpenRA.Mods.RA.Widgets { if (!hasRadar || radarAnimating) return false; // we're not set up for this. - if (!mapRect.Contains(mi.Location.ToPointF())) + if (!mapRect.Contains(mi.Location)) return false; var loc = MinimapPixelToCell(mi.Location); diff --git a/OpenRA.Mods.RA/Widgets/SpecialPowerBinWidget.cs b/OpenRA.Mods.RA/Widgets/SpecialPowerBinWidget.cs index c150a8f086..9965df00e6 100755 --- a/OpenRA.Mods.RA/Widgets/SpecialPowerBinWidget.cs +++ b/OpenRA.Mods.RA/Widgets/SpecialPowerBinWidget.cs @@ -57,7 +57,7 @@ namespace OpenRA.Mods.RA.Widgets { if (mi.Event == MouseInputEvent.Down) { - var action = buttons.Where(a => a.First.Contains(mi.Location.ToPoint())) + var action = buttons.Where(a => a.First.Contains(mi.Location)) .Select(a => a.Second).FirstOrDefault(); if (action == null) return false; @@ -99,7 +99,7 @@ namespace OpenRA.Mods.RA.Widgets var drawPos = new float2(rectBounds.X + 5, y); var rect = new Rectangle(rectBounds.X + 5, y, 64, 48); - if (rect.Contains(Viewport.LastMousePos.ToPoint())) + if (rect.Contains(Viewport.LastMousePos)) { var pos = drawPos.ToInt2(); var tl = new int2(pos.X-3,pos.Y-3);