1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-12 17:48:31 +00:00

Changed rectangle to be immutable

This commit is contained in:
Darth Affe 2019-11-18 19:36:46 +01:00
parent 1b917c8c78
commit 65abc89605
6 changed files with 162 additions and 216 deletions

View File

@ -175,9 +175,7 @@ namespace RGB.NET.Core
if (led != null)
{
led.LedRectangle.Location = new Point(layoutLed.X, layoutLed.Y);
led.LedRectangle.Size = new Size(layoutLed.Width, layoutLed.Height);
led.LedRectangle = new Rectangle(new Point(layoutLed.X, layoutLed.Y), new Size(layoutLed.Width, layoutLed.Height));
led.Shape = layoutLed.Shape;
led.ShapeData = layoutLed.ShapeData;

View File

@ -0,0 +1,115 @@
using System;
namespace RGB.NET.Core
{
public static class RectangleExtensions
{
#region Methods
/// <summary>
/// Sets the <see cref="Rectangle.Location"/> of the given rectangle.
/// </summary>
/// <param name="rect">The rectangle to modify.</param>
/// <param name="location">The new location of the rectangle.</param>
/// <returns>The modified <see cref="Rectangle"/>.</returns>
public static Rectangle SetLocation(this Rectangle rect, Point location) => new Rectangle(location, rect.Size);
/// <summary>
/// Sets the <see cref="Point.X"/> of the <see cref="Rectangle.Location"/> of the given rectangle.
/// </summary>
/// <param name="rect">The rectangle to modify.</param>
/// <param name="x">The new x-location of the rectangle.</param>
/// <returns>The modified <see cref="Rectangle"/>.</returns>
public static Rectangle SetX(this Rectangle rect, double x) => new Rectangle(new Point(x, rect.Location.Y), rect.Size);
/// <summary>
/// Sets the <see cref="Point.Y"/> of the <see cref="Rectangle.Location"/> of the given rectangle.
/// </summary>
/// <param name="rect">The rectangle to modify.</param>
/// <param name="y">The new y-location of the rectangle.</param>
/// <returns>The modified <see cref="Rectangle"/>.</returns>
public static Rectangle SetY(this Rectangle rect, double y) => new Rectangle(new Point(rect.Location.X, y), rect.Size);
/// <summary>
/// Sets the <see cref="Rectangle.Size"/> of the given rectangle.
/// </summary>
/// <param name="rect">The rectangle to modify.</param>
/// <param name="size">The new size of the rectangle.</param>
/// <returns>The modified <see cref="Rectangle"/>.</returns>
public static Rectangle SetSize(this Rectangle rect, Size size) => new Rectangle(rect.Location, size);
/// <summary>
/// Sets the <see cref="Size.Width"/> of the <see cref="Rectangle.Size"/> of the given rectangle.
/// </summary>
/// <param name="rect">The rectangle to modify.</param>
/// <param name="width">The new width of the rectangle.</param>
/// <returns>The modified <see cref="Rectangle"/>.</returns>
public static Rectangle SetWidth(this Rectangle rect, double width) => new Rectangle(rect.Location, new Size(width, rect.Size.Height));
/// <summary>
/// Sets the <see cref="Size.Height"/> of the <see cref="Rectangle.Size"/> of the given rectangle.
/// </summary>
/// <param name="rect">The rectangle to modify.</param>
/// <param name="height">The new height of the rectangle.</param>
/// <returns>The modified <see cref="Rectangle"/>.</returns>
public static Rectangle SetHeight(this Rectangle rect, double height) => new Rectangle(rect.Location, new Size(rect.Size.Width, height));
/// <summary>
/// Calculates the percentage of intersection of a rectangle.
/// </summary>
/// <param name="intersectingRect">The intersecting rectangle.</param>
/// <returns>The percentage of intersection.</returns>
public static double CalculateIntersectPercentage(this Rectangle rect, Rectangle intersectingRect)
{
if (rect.IsEmpty || intersectingRect.IsEmpty) return 0;
Rectangle intersection = rect.CalculateIntersection(intersectingRect);
return (intersection.Size.Width * intersection.Size.Height) / (rect.Size.Width * rect.Size.Height);
}
/// <summary>
/// Calculates the <see cref="Rectangle"/> representing the intersection of this <see cref="Rectangle"/> and the one provided as parameter.
/// </summary>
/// <param name="intersectingRectangle">The intersecting <see cref="Rectangle"/></param>
/// <returns>A new <see cref="Rectangle"/> representing the intersection this <see cref="Rectangle"/> and the one provided as parameter.</returns>
public static Rectangle CalculateIntersection(this Rectangle rect, Rectangle intersectingRectangle)
{
double x1 = Math.Max(rect.Location.X, intersectingRectangle.Location.X);
double x2 = Math.Min(rect.Location.X + rect.Size.Width, intersectingRectangle.Location.X + intersectingRectangle.Size.Width);
double y1 = Math.Max(rect.Location.Y, intersectingRectangle.Location.Y);
double y2 = Math.Min(rect.Location.Y + rect.Size.Height, intersectingRectangle.Location.Y + intersectingRectangle.Size.Height);
if ((x2 >= x1) && (y2 >= y1))
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
return new Rectangle();
}
/// <summary>
/// Determines if the specified <see cref="Point"/> is contained within this <see cref="Rectangle"/>.
/// </summary>
/// <param name="point">The <see cref="Point"/> to test.</param>
/// <returns></returns>
public static bool Contains(this Rectangle rect, Point point) => rect.Contains(point.X, point.Y);
/// <summary>
/// Determines if the specified location is contained within this <see cref="Rectangle"/>.
/// </summary>
/// <param name="x">The X-location to test.</param>
/// <param name="y">The Y-location to test.</param>
/// <returns></returns>
public static bool Contains(this Rectangle rect, double x, double y) => (rect.Location.X <= x) && (x < (rect.Location.X + rect.Size.Width))
&& (rect.Location.Y <= y) && (y < (rect.Location.Y + rect.Size.Height));
/// <summary>
/// Determines if the specified <see cref="Rectangle"/> is contained within this <see cref="Rectangle"/>.
/// </summary>
/// <param name="rect">The <see cref="Rectangle"/> to test.</param>
/// <returns></returns>
public static bool Contains(this Rectangle rect, Rectangle rect2) => (rect.Location.X <= rect2.Location.X) && ((rect2.Location.X + rect2.Size.Width) <= (rect.Location.X + rect.Size.Width))
&& (rect.Location.Y <= rect2.Location.Y) && ((rect2.Location.Y + rect2.Size.Height) <= (rect.Location.Y + rect.Size.Height));
#endregion
}
}

View File

@ -44,10 +44,19 @@ namespace RGB.NET.Core
set => SetProperty(ref _shapeData, value);
}
private Rectangle _ledRectangle;
/// <summary>
/// Gets a rectangle representing the physical location of the <see cref="Led"/> relative to the <see cref="Device"/>.
/// </summary>
public Rectangle LedRectangle { get; }
public Rectangle LedRectangle
{
get => _ledRectangle;
set
{
if (SetProperty(ref _ledRectangle, value))
OnPropertyChanged(nameof(AbsoluteLedRectangle));
}
}
/// <summary>
/// Gets a rectangle representing the physical location of the <see cref="Led"/> on the <see cref="RGBSurface"/>.
@ -142,6 +151,12 @@ namespace RGB.NET.Core
this.Id = id;
this.LedRectangle = ledRectangle;
this.CustomData = customData;
device.PropertyChanged += (sender, args) =>
{
OnPropertyChanged(nameof(LedRectangle));
OnPropertyChanged(nameof(AbsoluteLedRectangle));
};
}
#endregion
@ -195,7 +210,7 @@ namespace RGB.NET.Core
/// Converts a <see cref="Led" /> to a <see cref="Rectangle" />.
/// </summary>
/// <param name="led">The <see cref="Led"/> to convert.</param>
public static implicit operator Rectangle(Led led) => led?.LedRectangle;
public static implicit operator Rectangle(Led led) => led?.LedRectangle ?? new Rectangle();
#endregion
}

View File

@ -8,155 +8,47 @@ using System.Linq;
namespace RGB.NET.Core
{
/// <inheritdoc />
/// <summary>
/// Represents a rectangle defined by it's position and it's size.
/// </summary>
[DebuggerDisplay("[Location: {Location}, Size: {Size}]")]
public class Rectangle : AbstractBindable
public struct Rectangle
{
#region Properties & Fields
private double _x;
/// <summary>
/// Gets or sets the X-position of this <see cref="Rectangle"/>.
/// Gets the <see cref="Point"/> representing the top-left corner of the <see cref="Rectangle"/>.
/// </summary>
public double X
{
get => _x;
set
{
if (SetProperty(ref _x, value))
{
OnPropertyChanged(nameof(Location));
OnPropertyChanged(nameof(Center));
}
}
}
private double _y;
/// <summary>
/// Gets or sets the Y-position of this <see cref="Rectangle"/>.
/// </summary>
public double Y
{
get => _y;
set
{
if (SetProperty(ref _y, value))
{
OnPropertyChanged(nameof(Location));
OnPropertyChanged(nameof(Center));
}
}
}
private double _width;
/// <summary>
/// Gets or sets the width of this <see cref="Rectangle"/>.
/// </summary>
public double Width
{
get => _width;
set
{
if (SetProperty(ref _width, Math.Max(0, value)))
{
OnPropertyChanged(nameof(Size));
OnPropertyChanged(nameof(Center));
OnPropertyChanged(nameof(IsEmpty));
}
}
}
private double _height;
/// <summary>
/// Gets or sets the height of this <see cref="Rectangle"/>.
/// </summary>
public double Height
{
get => _height;
set
{
if (SetProperty(ref _height, Math.Max(0, value)))
{
OnPropertyChanged(nameof(Size));
OnPropertyChanged(nameof(Center));
OnPropertyChanged(nameof(IsEmpty));
}
}
}
public Point Location { get; }
/// <summary>
/// Gets or sets the <see cref="Point"/> representing the top-left corner of the <see cref="Rectangle"/>.
/// Gets the <see cref="Size"/> of the <see cref="Rectangle"/>.
/// </summary>
public Point Location
{
get => new Point(X, Y);
set
{
if (Location != value)
{
_x = value.X;
_y = value.Y;
OnPropertyChanged(nameof(Location));
OnPropertyChanged(nameof(Center));
}
}
}
/// <summary>
/// Gets or sets the <see cref="Size"/> of the <see cref="Rectangle"/>.
/// </summary>
public Size Size
{
get => new Size(Width, Height);
set
{
if (Size != value)
{
_width = value.Width;
_height = value.Height;
OnPropertyChanged(nameof(Size));
OnPropertyChanged(nameof(Center));
OnPropertyChanged(nameof(IsEmpty));
}
}
}
public Size Size { get; }
/// <summary>
/// Gets a new <see cref="Point"/> representing the center-point of the <see cref="Rectangle"/>.
/// </summary>
public Point Center => new Point(X + (Width / 2.0), Y + (Height / 2.0));
public Point Center { get; }
/// <summary>
/// Gets a bool indicating if both, the width and the height of the rectangle is greater than zero.
/// <c>True</c> if the rectangle has a width or a height of zero; otherwise, <c>false</c>.
/// </summary>
public bool IsEmpty => (Width <= DoubleExtensions.TOLERANCE) || (Height <= DoubleExtensions.TOLERANCE);
public bool IsEmpty => (Size.Width <= DoubleExtensions.TOLERANCE) || (Size.Height <= DoubleExtensions.TOLERANCE);
#endregion
#region Constructors
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Core.Rectangle" /> class.
/// </summary>
public Rectangle()
: this(new Point(), new Size())
{ }
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="T:RGB.NET.Core.Rectangle" /> class using the provided values for <see cref="P:RGB.NET.Core.Rectangle.Location" /> ans <see cref="P:RGB.NET.Core.Rectangle.Size" />.
/// </summary>
/// <param name="x">The <see cref="P:RGB.NET.Core.Point.X" />-position of this <see cref="T:RGB.NET.Core.Rectangle" />.</param>
/// <param name="y">The <see cref="P:RGB.NET.Core.Point.Y" />-position of this <see cref="T:RGB.NET.Core.Rectangle" />.</param>
/// <param name="width">The <see cref="P:RGB.NET.Core.Size.Width" /> of this <see cref="T:RGB.NET.Core.Rectangle" />.</param>
/// <param name="height">The <see cref="P:RGB.NET.Core.Size.Height" /> of this <see cref="T:RGB.NET.Core.Rectangle" />.</param>
/// <param name="x">The x-value of the <see cref="T:RGB.NET.Core.Location" /> of this <see cref="T:RGB.NET.Core.Rectangle" />.</param>
/// <param name="y">The y-value of the <see cref="T:RGB.NET.Core.Location" />-position of this <see cref="T:RGB.NET.Core.Rectangle" />.</param>
/// <param name="width">The width of the <see cref="T:RGB.NET.Core.Size"/> of this <see cref="T:RGB.NET.Core.Rectangle" />.</param>
/// <param name="height">The height of the <see cref="T:RGB.NET.Core.Size"/> of this <see cref="T:RGB.NET.Core.Rectangle" />.</param>
public Rectangle(double x, double y, double width, double height)
: this(new Point(x, y), new Size(width, height))
{ }
@ -170,6 +62,7 @@ namespace RGB.NET.Core
{
this.Location = location;
this.Size = size;
Center = new Point(Location.X + (Size.Width / 2.0), Location.Y + (Size.Height / 2.0));
}
/// <inheritdoc />
@ -205,10 +98,10 @@ namespace RGB.NET.Core
posY2 = Math.Max(posY2, rectangle.Location.Y + rectangle.Size.Height);
}
if (hasPoint)
InitializeFromPoints(new Point(posX, posY), new Point(posX2, posY2));
else
InitializeFromPoints(new Point(0, 0), new Point(0, 0));
(Point location, Size size) = hasPoint ? InitializeFromPoints(new Point(posX, posY), new Point(posX2, posY2)) : InitializeFromPoints(new Point(0, 0), new Point(0, 0));
Location = location;
Size = size;
Center = new Point(Location.X + (Size.Width / 2.0), Location.Y + (Size.Height / 2.0));
}
/// <inheritdoc />
@ -246,82 +139,27 @@ namespace RGB.NET.Core
posY2 = Math.Max(posY2, point.Y);
}
if (hasPoint)
InitializeFromPoints(new Point(posX, posY), new Point(posX2, posY2));
else
InitializeFromPoints(new Point(0, 0), new Point(0, 0));
(Point location, Size size) = hasPoint ? InitializeFromPoints(new Point(posX, posY), new Point(posX2, posY2)) : InitializeFromPoints(new Point(0, 0), new Point(0, 0));
Location = location;
Size = size;
Center = new Point(Location.X + (Size.Width / 2.0), Location.Y + (Size.Height / 2.0));
}
#endregion
#region Methods
private void InitializeFromPoints(Point point1, Point point2)
private static (Point location, Size size) InitializeFromPoints(Point point1, Point point2)
{
double posX = Math.Min(point1.X, point2.X);
double posY = Math.Min(point1.Y, point2.Y);
double width = Math.Max(point1.X, point2.X) - posX;
double height = Math.Max(point1.Y, point2.Y) - posY;
Location = new Point(posX, posY);
Size = new Size(width, height);
return (new Point(posX, posY), new Size(width, height));
}
/// <summary>
/// Calculates the percentage of intersection of a rectangle.
/// </summary>
/// <param name="intersectingRect">The intersecting rectangle.</param>
/// <returns>The percentage of intersection.</returns>
public double CalculateIntersectPercentage(Rectangle intersectingRect)
{
if (IsEmpty || intersectingRect.IsEmpty) return 0;
Rectangle intersection = CalculateIntersection(intersectingRect);
return (intersection.Size.Width * intersection.Size.Height) / (Size.Width * Size.Height);
}
/// <summary>
/// Calculates the <see cref="Rectangle"/> representing the intersection of this <see cref="Rectangle"/> and the one provided as parameter.
/// </summary>
/// <param name="intersectingRectangle">The intersecting <see cref="Rectangle"/></param>
/// <returns>A new <see cref="Rectangle"/> representing the intersection this <see cref="Rectangle"/> and the one provided as parameter.</returns>
public Rectangle CalculateIntersection(Rectangle intersectingRectangle)
{
double x1 = Math.Max(Location.X, intersectingRectangle.Location.X);
double x2 = Math.Min(Location.X + Size.Width, intersectingRectangle.Location.X + intersectingRectangle.Size.Width);
double y1 = Math.Max(Location.Y, intersectingRectangle.Location.Y);
double y2 = Math.Min(Location.Y + Size.Height, intersectingRectangle.Location.Y + intersectingRectangle.Size.Height);
if ((x2 >= x1) && (y2 >= y1))
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
return new Rectangle();
}
/// <summary>
/// Determines if the specified <see cref="Point"/> is contained within this <see cref="Rectangle"/>.
/// </summary>
/// <param name="point">The <see cref="Point"/> to test.</param>
/// <returns></returns>
public bool Contains(Point point) => Contains(point.X, point.Y);
/// <summary>
/// Determines if the specified location is contained within this <see cref="Rectangle"/>.
/// </summary>
/// <param name="x">The X-location to test.</param>
/// <param name="y">The Y-location to test.</param>
/// <returns></returns>
public bool Contains(double x, double y) => (Location.X <= x) && (x < (Location.X + Size.Width)) && (Location.Y <= y) && (y < (Location.Y + Size.Height));
/// <summary>
/// Determines if the specified <see cref="Rectangle"/> is contained within this <see cref="Rectangle"/>.
/// </summary>
/// <param name="rect">The <see cref="Rectangle"/> to test.</param>
/// <returns></returns>
public bool Contains(Rectangle rect) => (Location.X <= rect.Location.X) && ((rect.Location.X + rect.Size.Width) <= (Location.X + Size.Width))
&& (Location.Y <= rect.Location.Y) && ((rect.Location.Y + rect.Size.Height) <= (Location.Y + Size.Height));
/// <summary>
/// Converts the <see cref="Location"/>- and <see cref="Size"/>-position of this <see cref="Rectangle"/> to a human-readable string.
/// </summary>
@ -338,9 +176,6 @@ namespace RGB.NET.Core
if (!(obj is Rectangle compareRect))
return false;
if (ReferenceEquals(this, compareRect))
return true;
if (GetType() != compareRect.GetType())
return false;
@ -371,7 +206,7 @@ namespace RGB.NET.Core
/// <param name="rectangle1">The first <see cref="Rectangle" /> to compare.</param>
/// <param name="rectangle2">The second <see cref="Rectangle" /> to compare.</param>
/// <returns><c>true</c> if <paramref name="rectangle1" /> and <paramref name="rectangle2" /> are equal; otherwise, <c>false</c>.</returns>
public static bool operator ==(Rectangle rectangle1, Rectangle rectangle2) => rectangle1?.Equals(rectangle2) ?? ReferenceEquals(rectangle2, null);
public static bool operator ==(Rectangle rectangle1, Rectangle rectangle2) => rectangle1.Equals(rectangle2);
/// <summary>
/// Returns a value that indicates whether two specified <see cref="Rectangle" /> are equal.

View File

@ -33,8 +33,6 @@ namespace RGB.NET.Core
private readonly LinkedList<ILedGroup> _ledGroups = new LinkedList<ILedGroup>();
private readonly Rectangle _surfaceRectangle = new Rectangle();
// ReSharper restore InconsistentNaming
/// <summary>
@ -50,7 +48,7 @@ namespace RGB.NET.Core
/// <summary>
/// Gets a copy of the <see cref="Rectangle"/> representing this <see cref="RGBSurface"/>.
/// </summary>
public Rectangle SurfaceRectangle => new Rectangle(_surfaceRectangle);
public Rectangle SurfaceRectangle { get; private set; }
/// <summary>
/// Gets a list of all <see cref="Led"/> on this <see cref="RGBSurface"/>.
@ -162,9 +160,8 @@ namespace RGB.NET.Core
case BrushCalculationMode.Relative:
Rectangle brushRectangle = new Rectangle(leds.Select(led => led.AbsoluteLedRectangle));
Point offset = new Point(-brushRectangle.Location.X, -brushRectangle.Location.Y);
brushRectangle.Location = new Point(0, 0);
brush.PerformRender(brushRectangle,
leds.Select(x => new BrushRenderTarget(x, GetDeviceLedLocation(x, offset))));
brushRectangle = brushRectangle.SetLocation(new Point(0, 0));
brush.PerformRender(brushRectangle, leds.Select(x => new BrushRenderTarget(x, GetDeviceLedLocation(x, offset))));
break;
case BrushCalculationMode.Absolute:
brush.PerformRender(SurfaceRectangle, leds.Select(x => new BrushRenderTarget(x, x.AbsoluteLedRectangle)));
@ -230,9 +227,7 @@ namespace RGB.NET.Core
private void UpdateSurfaceRectangle()
{
Rectangle devicesRectangle = new Rectangle(_devices.Select(d => new Rectangle(d.Location, d.Size)));
_surfaceRectangle.Width = devicesRectangle.Location.X + devicesRectangle.Size.Width;
_surfaceRectangle.Height = devicesRectangle.Location.Y + devicesRectangle.Size.Height;
SurfaceRectangle = SurfaceRectangle.SetSize(new Size(devicesRectangle.Location.X + devicesRectangle.Size.Width, devicesRectangle.Location.Y + devicesRectangle.Size.Height));
}
/// <summary>

View File

@ -2,7 +2,6 @@
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.Linq;
using RGB.NET.Core;
@ -28,17 +27,8 @@ namespace RGB.NET.Groups
get => _rectangle;
set
{
Rectangle oldValue = _rectangle;
if (SetProperty(ref _rectangle, value))
{
if (oldValue != null)
oldValue.PropertyChanged -= RectangleChanged;
if (_rectangle != null)
_rectangle.PropertyChanged += RectangleChanged;
InvalidateCache();
}
}
}
@ -110,14 +100,12 @@ namespace RGB.NET.Groups
private void RGBSurfaceOnSurfaceLayoutChanged(SurfaceLayoutChangedEventArgs args) => InvalidateCache();
private void RectangleChanged(object sender, EventArgs eventArgs) => InvalidateCache();
/// <inheritdoc />
/// <summary>
/// Gets a list containing all <see cref="T:RGB.NET.Core.Led" /> of this <see cref="T:RGB.NET.Groups.RectangleLedGroup" />.
/// </summary>
/// <returns>The list containing all <see cref="T:RGB.NET.Core.Led" /> of this <see cref="T:RGB.NET.Groups.RectangleLedGroup" />.</returns>
public override IEnumerable<Led> GetLeds() => _ledCache ?? (_ledCache = RGBSurface.Instance.Leds.Where(x => x.AbsoluteLedRectangle.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList());
public override IEnumerable<Led> GetLeds() => _ledCache ??= RGBSurface.Instance.Leds.Where(x => x.AbsoluteLedRectangle.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList();
private void InvalidateCache() => _ledCache = null;