Add extension methods to Rectangle/RectangleF, remove a pile of ToPoint()'s.

This commit is contained in:
Paul Chote
2011-01-05 22:24:11 +13:00
parent 3e1db3e8ae
commit df460d7407
7 changed files with 24 additions and 13 deletions

View File

@@ -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());
}
}
}

View File

@@ -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;

View File

@@ -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)

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);