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

Added device-rotation

This commit is contained in:
Darth Affe 2019-11-20 17:05:55 +01:00
parent 59d203657e
commit fab502e0e4
8 changed files with 212 additions and 45 deletions

View File

@ -27,6 +27,14 @@ namespace RGB.NET.Core
/// <inheritdoc /> /// <inheritdoc />
IRGBDeviceInfo IRGBDevice.DeviceInfo => DeviceInfo; IRGBDeviceInfo IRGBDevice.DeviceInfo => DeviceInfo;
private Point _location = new Point(0, 0);
/// <inheritdoc />
public Point Location
{
get => _location;
set => SetProperty(ref _location, value);
}
private Size _size = Size.Invalid; private Size _size = Size.Invalid;
/// <inheritdoc /> /// <inheritdoc />
public Size Size public Size Size
@ -35,19 +43,22 @@ namespace RGB.NET.Core
protected set protected set
{ {
if (SetProperty(ref _size, value)) if (SetProperty(ref _size, value))
{
OnPropertyChanged(nameof(ActualSize)); OnPropertyChanged(nameof(ActualSize));
OnPropertyChanged(nameof(DeviceRectangle));
}
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public Size ActualSize => Size * Scale; public Size ActualSize => Size * Scale;
private Point _location = new Point(0, 0); public Rectangle DeviceRectangle
/// <inheritdoc />
public Point Location
{ {
get => _location; get
set => SetProperty(ref _location, value); {
return new Rectangle(Location, new Rectangle(new Rectangle(Location, ActualSize).Rotate(Rotation)).Size);
}
} }
private Scale _scale = new Scale(1); private Scale _scale = new Scale(1);
@ -58,7 +69,24 @@ namespace RGB.NET.Core
set set
{ {
if (SetProperty(ref _scale, value)) if (SetProperty(ref _scale, value))
{
OnPropertyChanged(nameof(ActualSize)); OnPropertyChanged(nameof(ActualSize));
OnPropertyChanged(nameof(DeviceRectangle));
}
}
}
private Rotation _rotation = new Rotation(0);
/// <inheritdoc />
public Rotation Rotation
{
get => _rotation;
set
{
if (SetProperty(ref _rotation, value))
{
OnPropertyChanged(nameof(DeviceRectangle));
}
} }
} }
@ -86,11 +114,11 @@ namespace RGB.NET.Core
Led IRGBDevice.this[LedId ledId] => LedMapping.TryGetValue(ledId, out Led led) ? led : null; Led IRGBDevice.this[LedId ledId] => LedMapping.TryGetValue(ledId, out Led led) ? led : null;
/// <inheritdoc /> /// <inheritdoc />
Led IRGBDevice.this[Point location] => LedMapping.Values.FirstOrDefault(x => x.ActualLedRectangle.Contains(location)); Led IRGBDevice.this[Point location] => LedMapping.Values.FirstOrDefault(x => x.LedRectangle.Contains(location));
/// <inheritdoc /> /// <inheritdoc />
IEnumerable<Led> IRGBDevice.this[Rectangle referenceRect, double minOverlayPercentage] IEnumerable<Led> IRGBDevice.this[Rectangle referenceRect, double minOverlayPercentage]
=> LedMapping.Values.Where(x => referenceRect.CalculateIntersectPercentage(x.ActualLedRectangle) >= minOverlayPercentage); => LedMapping.Values.Where(x => referenceRect.CalculateIntersectPercentage(x.LedRectangle) >= minOverlayPercentage);
#endregion #endregion
@ -143,11 +171,21 @@ namespace RGB.NET.Core
/// <param name="ledId">The <see cref="LedId"/> to initialize.</param> /// <param name="ledId">The <see cref="LedId"/> to initialize.</param>
/// <param name="ledRectangle">The <see cref="Rectangle"/> representing the position of the <see cref="Led"/> to initialize.</param> /// <param name="ledRectangle">The <see cref="Rectangle"/> representing the position of the <see cref="Led"/> to initialize.</param>
/// <returns></returns> /// <returns></returns>
protected virtual Led InitializeLed(LedId ledId, Rectangle ledRectangle) [Obsolete("Use InitializeLed(LedId ledId, Point location, Size size) instead.")]
protected virtual Led InitializeLed(LedId ledId, Rectangle rectangle) => InitializeLed(ledId, rectangle.Location, rectangle.Size);
/// <summary>
/// Initializes the <see cref="Led"/> with the specified id.
/// </summary>
/// <param name="ledId">The <see cref="LedId"/> to initialize.</param>
/// <param name="location">The location of the <see cref="Led"/> to initialize.</param>
/// <param name="size">The size of the <see cref="Led"/> to initialize.</param>
/// <returns>The initialized led.</returns>
protected virtual Led InitializeLed(LedId ledId, Point location, Size size)
{ {
if ((ledId == LedId.Invalid) || LedMapping.ContainsKey(ledId)) return null; if ((ledId == LedId.Invalid) || LedMapping.ContainsKey(ledId)) return null;
Led led = new Led(this, ledId, ledRectangle, CreateLedCustomData(ledId)); Led led = new Led(this, ledId, location, size, CreateLedCustomData(ledId));
LedMapping.Add(ledId, led); LedMapping.Add(ledId, led);
return led; return led;
} }
@ -190,11 +228,12 @@ namespace RGB.NET.Core
if (Enum.TryParse(layoutLed.Id, true, out LedId ledId)) if (Enum.TryParse(layoutLed.Id, true, out LedId ledId))
{ {
if (!LedMapping.TryGetValue(ledId, out Led led) && createMissingLeds) if (!LedMapping.TryGetValue(ledId, out Led led) && createMissingLeds)
led = InitializeLed(ledId, new Rectangle()); led = InitializeLed(ledId, new Point(), new Size());
if (led != null) if (led != null)
{ {
led.LedRectangle = new Rectangle(new Point(layoutLed.X, layoutLed.Y), new Size(layoutLed.Width, layoutLed.Height)); led.Location = new Point(layoutLed.X, layoutLed.Y);
led.Size = new Size(layoutLed.Width, layoutLed.Height);
led.Shape = layoutLed.Shape; led.Shape = layoutLed.Shape;
led.ShapeData = layoutLed.ShapeData; led.ShapeData = layoutLed.ShapeData;

View File

@ -32,12 +32,19 @@ namespace RGB.NET.Core
/// Gets the actual <see cref="Size"/> (scaled and rotated) of the <see cref="IRGBDevice"/>. /// Gets the actual <see cref="Size"/> (scaled and rotated) of the <see cref="IRGBDevice"/>.
/// </summary> /// </summary>
Size ActualSize { get; } Size ActualSize { get; }
Rectangle DeviceRectangle { get; }
/// <summary> /// <summary>
/// Gets or sets the scale of the <see cref="IRGBDevice"/>. /// Gets or sets the scale of the <see cref="IRGBDevice"/>.
/// </summary> /// </summary>
Scale Scale { get; set; } Scale Scale { get; set; }
/// <summary>
/// Gets or sets the rotation of the <see cref="IRGBDevice"/>.
/// </summary>
Rotation Rotation { get; set; }
/// <summary> /// <summary>
/// Gets or sets the <see cref="DeviceUpdateMode"/> of the <see cref="IRGBDevice"/>. /// Gets or sets the <see cref="DeviceUpdateMode"/> of the <see cref="IRGBDevice"/>.
/// </summary> /// </summary>

View File

@ -110,6 +110,44 @@ namespace RGB.NET.Core
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)) 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)); && (rect.Location.Y <= rect2.Location.Y) && ((rect2.Location.Y + rect2.Size.Height) <= (rect.Location.Y + rect.Size.Height));
public static Point Translate(this Point point, double x = 0, double y = 0) => new Point(point.X + x, point.Y + y);
public static Point Rotate(this Point point, Rotation rotation, Point origin = new Point())
{
double sin = Math.Sin(rotation.Radians);
double cos = Math.Cos(rotation.Radians);
point = new Point(point.X - origin.X, point.Y - origin.Y);
point = new Point((point.X * cos) - (point.Y * sin), (point.X * sin) + (point.Y * cos));
return new Point(point.X + origin.X, point.Y + origin.Y); ;
}
public static Rectangle Translate(this Rectangle rect, Point point) => rect.Translate(point.X, point.Y);
public static Rectangle Translate(this Rectangle rect, double x = 0, double y = 0) => new Rectangle(rect.Location.Translate(x, y), rect.Size);
public static Point[] Rotate(this Rectangle rect, Rotation rotation, Point origin = new Point())
{
Point[] points = {
rect.Location, // top left
new Point(rect.Location.X + rect.Size.Width, rect.Location.Y), // top right
new Point(rect.Location.X + rect.Size.Width, rect.Location.Y + rect.Size.Height), // bottom right
new Point(rect.Location.X, rect.Location.Y + rect.Size.Height), // bottom right
};
double sin = Math.Sin(rotation.Radians);
double cos = Math.Cos(rotation.Radians);
for (int i = 0; i < points.Length; i++)
{
Point point = points[i];
point = new Point(point.X - origin.X, point.Y - origin.Y);
point = new Point((point.X * cos) - (point.Y * sin), (point.X * sin) + (point.Y * cos));
points[i] = new Point(point.X + origin.X, point.Y + origin.Y);
}
return points;
}
#endregion #endregion
} }
} }

View File

@ -45,29 +45,53 @@ namespace RGB.NET.Core
set => SetProperty(ref _shapeData, value); set => SetProperty(ref _shapeData, value);
} }
private Rectangle _ledRectangle; public Point Location { get; set; }
/// <summary>
/// Gets a rectangle representing the physical location of the <see cref="Led"/> relative to the <see cref="Device"/>. public Size Size { get; set; }
/// </summary>
public Rectangle LedRectangle public Point ActualLocation
{ {
get => _ledRectangle; get
set
{ {
if (SetProperty(ref _ledRectangle, value)) Point point = (Location * Device.Scale);
if (!Device.Rotation.Radians.EqualsInTolerance(0))
{ {
OnPropertyChanged(nameof(ActualLedRectangle)); Point deviceCenter = new Rectangle(Device.ActualSize).Center;
OnPropertyChanged(nameof(AbsoluteLedRectangle)); Point actualDeviceCenter = Device.DeviceRectangle.Center;
Point centerOffset = new Point(actualDeviceCenter.X - deviceCenter.X, actualDeviceCenter.Y - deviceCenter.Y);
point = point.Rotate(Device.Rotation, new Rectangle(Device.ActualSize).Center) + centerOffset;
} }
return point;
} }
} }
public Rectangle ActualLedRectangle => new Rectangle(LedRectangle.Location * Device.Scale, LedRectangle.Size * Device.Scale); public Size ActualSize => Size * Device.Scale;
/// <summary> /// <summary>
/// Gets a rectangle representing the physical location of the <see cref="Led"/> on the <see cref="RGBSurface"/>. /// Gets a rectangle representing the logical location of the <see cref="Led"/> relative to the <see cref="Device"/>.
/// </summary> /// </summary>
public Rectangle AbsoluteLedRectangle => new Rectangle(ActualLedRectangle.Location + Device.Location, ActualLedRectangle.Size); public Rectangle LedRectangle
{
get
{
Rectangle rect = new Rectangle(Location * Device.Scale, Size * Device.Scale);
if (!Device.Rotation.Radians.EqualsInTolerance(0))
{
Point deviceCenter = new Rectangle(Device.ActualSize).Center;
Point actualDeviceCenter = Device.DeviceRectangle.Center;
Point centerOffset = new Point(actualDeviceCenter.X - deviceCenter.X, actualDeviceCenter.Y - deviceCenter.Y);
rect = new Rectangle(rect.Rotate(Device.Rotation, new Rectangle(Device.ActualSize).Center)).Translate(centerOffset);
}
return rect;
}
}
/// <summary>
/// Gets a rectangle representing the logical location of the <see cref="Led"/> on the <see cref="RGBSurface"/>.
/// </summary>
public Rectangle AbsoluteLedRectangle => LedRectangle.Translate(Device.Location);
/// <summary> /// <summary>
/// Indicates whether the <see cref="Led" /> is about to change it's color. /// Indicates whether the <see cref="Led" /> is about to change it's color.
@ -149,13 +173,15 @@ namespace RGB.NET.Core
/// </summary> /// </summary>
/// <param name="device">The <see cref="IRGBDevice"/> the <see cref="Led"/> is associated with.</param> /// <param name="device">The <see cref="IRGBDevice"/> the <see cref="Led"/> is associated with.</param>
/// <param name="id">The <see cref="LedId"/> of the <see cref="Led"/>.</param> /// <param name="id">The <see cref="LedId"/> of the <see cref="Led"/>.</param>
/// <param name="ledRectangle">The <see cref="Rectangle"/> representing the physical location of the <see cref="Led"/> relative to the <see cref="Device"/>.</param> /// <param name="location">The physical location of the <see cref="Led"/> relative to the <see cref="Device"/>.</param>
/// <param name="size">The size of the <see cref="Led"/>.</param>
/// <param name="customData">The provider-specific data associated with this led.</param> /// <param name="customData">The provider-specific data associated with this led.</param>
internal Led(IRGBDevice device, LedId id, Rectangle ledRectangle, object customData = null) internal Led(IRGBDevice device, LedId id, Point location, Size size, object customData = null)
{ {
this.Device = device; this.Device = device;
this.Id = id; this.Id = id;
this.LedRectangle = ledRectangle; this.Location = location;
this.Size = size;
this.CustomData = customData; this.CustomData = customData;
device.PropertyChanged += DevicePropertyChanged; device.PropertyChanged += DevicePropertyChanged;
@ -171,10 +197,12 @@ namespace RGB.NET.Core
{ {
OnPropertyChanged(nameof(AbsoluteLedRectangle)); OnPropertyChanged(nameof(AbsoluteLedRectangle));
} }
else if ((e.PropertyName == nameof(IRGBDevice.Scale))) else if ((e.PropertyName == nameof(IRGBDevice.Scale)) || (e.PropertyName == nameof(IRGBDevice.Rotation)))
{ {
OnPropertyChanged(nameof(ActualLedRectangle)); OnPropertyChanged(nameof(LedRectangle));
OnPropertyChanged(nameof(AbsoluteLedRectangle)); OnPropertyChanged(nameof(AbsoluteLedRectangle));
OnPropertyChanged(nameof(ActualLocation));
OnPropertyChanged(nameof(ActualSize));
} }
} }
@ -185,7 +213,7 @@ namespace RGB.NET.Core
public override string ToString() => $"{Id} {Color}"; public override string ToString() => $"{Id} {Color}";
/// <summary> /// <summary>
/// Updates the <see cref="LedRectangle"/> to the requested <see cref="Core.Color"/>. /// Updates the <see cref="Led"/> to the requested <see cref="Core.Color"/>.
/// </summary> /// </summary>
internal void Update() internal void Update()
{ {
@ -199,7 +227,7 @@ namespace RGB.NET.Core
} }
/// <summary> /// <summary>
/// Resets the <see cref="LedRectangle"/> back to default. /// Resets the <see cref="Led"/> back to default.
/// </summary> /// </summary>
internal void Reset() internal void Reset()
{ {
@ -225,7 +253,7 @@ namespace RGB.NET.Core
/// Converts a <see cref="Led" /> to a <see cref="Rectangle" />. /// Converts a <see cref="Led" /> to a <see cref="Rectangle" />.
/// </summary> /// </summary>
/// <param name="led">The <see cref="Led"/> to convert.</param> /// <param name="led">The <see cref="Led"/> to convert.</param>
public static implicit operator Rectangle(Led led) => led?.ActualLedRectangle ?? new Rectangle(); public static implicit operator Rectangle(Led led) => led?.LedRectangle ?? new Rectangle();
#endregion #endregion
} }

View File

@ -53,11 +53,18 @@ namespace RGB.NET.Core
: this(new Point(x, y), new Size(width, height)) : this(new Point(x, y), new Size(width, height))
{ } { }
/// <summary>
/// Initializes a new instance of the <see cref="Rectangle"/> class using the <see cref="Location"/>(0,0) and the given <see cref="Core.Size"/>.
/// </summary>
/// <param name="size">The size of of this <see cref="T:RGB.NET.Core.Rectangle" />.</param>
public Rectangle(Size size) : this(new Point(), size)
{ }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Rectangle"/> class using the given <see cref="Point"/> and <see cref="Core.Size"/>. /// Initializes a new instance of the <see cref="Rectangle"/> class using the given <see cref="Point"/> and <see cref="Core.Size"/>.
/// </summary> /// </summary>
/// <param name="location"></param> /// <param name="location">The location of this of this <see cref="T:RGB.NET.Core.Rectangle" />.</param>
/// <param name="size"></param> /// <param name="size">The size of of this <see cref="T:RGB.NET.Core.Rectangle" />.</param>
public Rectangle(Point location, Size size) public Rectangle(Point location, Size size)
{ {
this.Location = location; this.Location = location;

View File

@ -0,0 +1,54 @@
using System;
namespace RGB.NET.Core
{
public struct Rotation
{
#region Constants
private const double TWO_PI = Math.PI * 2.0;
private const double RADIANS_DEGREES_CONVERSION = 180.0 / Math.PI;
private const double DEGREES_RADIANS_CONVERSION = Math.PI / 180.0;
#endregion
#region Properties & Fields
public double Degrees { get; }
public double Radians { get; }
#endregion
#region Constructors
public Rotation(double degrees)
: this(degrees, degrees * DEGREES_RADIANS_CONVERSION)
{ }
private Rotation(double degrees, double radians)
{
this.Degrees = degrees % 360.0;
this.Radians = radians % TWO_PI;
}
#endregion
#region Methods
public static Rotation FromDegrees(double degrees) => new Rotation(degrees);
public static Rotation FromRadians(double radians) => new Rotation(radians * RADIANS_DEGREES_CONVERSION, radians);
public bool Equals(Rotation other) => Degrees.EqualsInTolerance(other.Degrees);
public override bool Equals(object obj) => obj is Rotation other && Equals(other);
public override int GetHashCode() => Degrees.GetHashCode();
#endregion
#region Operators
public static implicit operator Rotation(double rotation) => new Rotation(rotation);
public static implicit operator double(Rotation rotation) => rotation.Degrees;
#endregion
}
}

View File

@ -161,10 +161,10 @@ namespace RGB.NET.Core
Rectangle brushRectangle = new Rectangle(leds.Select(led => led.AbsoluteLedRectangle)); Rectangle brushRectangle = new Rectangle(leds.Select(led => led.AbsoluteLedRectangle));
Point offset = new Point(-brushRectangle.Location.X, -brushRectangle.Location.Y); Point offset = new Point(-brushRectangle.Location.X, -brushRectangle.Location.Y);
brushRectangle = brushRectangle.SetLocation(new Point(0, 0)); brushRectangle = brushRectangle.SetLocation(new Point(0, 0));
brush.PerformRender(brushRectangle, leds.Select(x => new BrushRenderTarget(x, GetDeviceLedLocation(x, offset)))); brush.PerformRender(brushRectangle, leds.Select(led => new BrushRenderTarget(led, led.AbsoluteLedRectangle.Translate(offset))));
break; break;
case BrushCalculationMode.Absolute: case BrushCalculationMode.Absolute:
brush.PerformRender(SurfaceRectangle, leds.Select(x => new BrushRenderTarget(x, x.AbsoluteLedRectangle))); brush.PerformRender(SurfaceRectangle, leds.Select(led => new BrushRenderTarget(led, led.AbsoluteLedRectangle)));
break; break;
default: default:
throw new ArgumentException(); throw new ArgumentException();
@ -177,12 +177,6 @@ namespace RGB.NET.Core
renders.Key.Led.Color = renders.Value; renders.Key.Led.Color = renders.Value;
} }
private Rectangle GetDeviceLedLocation(Led led, Point extraOffset)
{
Rectangle absoluteRectangle = led.AbsoluteLedRectangle;
return (absoluteRectangle.Location + extraOffset) + absoluteRectangle.Size;
}
/// <summary> /// <summary>
/// Attaches the given <see cref="ILedGroup"/>. /// Attaches the given <see cref="ILedGroup"/>.
/// </summary> /// </summary>
@ -226,7 +220,7 @@ namespace RGB.NET.Core
private void UpdateSurfaceRectangle() private void UpdateSurfaceRectangle()
{ {
Rectangle devicesRectangle = new Rectangle(_devices.Select(d => new Rectangle(d.Location, d.ActualSize))); Rectangle devicesRectangle = new Rectangle(_devices.Select(d => d.DeviceRectangle));
SurfaceRectangle = SurfaceRectangle.SetSize(new Size(devicesRectangle.Location.X + devicesRectangle.Size.Width, devicesRectangle.Location.Y + devicesRectangle.Size.Height)); SurfaceRectangle = SurfaceRectangle.SetSize(new Size(devicesRectangle.Location.X + devicesRectangle.Size.Width, devicesRectangle.Location.Y + devicesRectangle.Size.Height));
} }

View File

@ -59,7 +59,7 @@ namespace RGB.NET.Groups
/// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a <see cref="T:RGB.NET.Core.Led" /> must have with the <see cref="P:RGB.NET.Groups.RectangleLedGroup.Rectangle" /> to be taken into the <see cref="T:RGB.NET.Groups.RectangleLedGroup" />. (default: 0.5)</param> /// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a <see cref="T:RGB.NET.Core.Led" /> must have with the <see cref="P:RGB.NET.Groups.RectangleLedGroup.Rectangle" /> to be taken into the <see cref="T:RGB.NET.Groups.RectangleLedGroup" />. (default: 0.5)</param>
/// <param name="autoAttach">(optional) Specifies whether this <see cref="T:RGB.NET.Groups.RectangleLedGroup" /> should be automatically attached or not. (default: true)</param> /// <param name="autoAttach">(optional) Specifies whether this <see cref="T:RGB.NET.Groups.RectangleLedGroup" /> should be automatically attached or not. (default: true)</param>
public RectangleLedGroup(Led fromLed, Led toLed, double minOverlayPercentage = 0.5, bool autoAttach = true) public RectangleLedGroup(Led fromLed, Led toLed, double minOverlayPercentage = 0.5, bool autoAttach = true)
: this(new Rectangle(fromLed.ActualLedRectangle, toLed.ActualLedRectangle), minOverlayPercentage, autoAttach) : this(new Rectangle(fromLed.LedRectangle, toLed.LedRectangle), minOverlayPercentage, autoAttach)
{ } { }
/// <inheritdoc /> /// <inheritdoc />
@ -105,7 +105,7 @@ namespace RGB.NET.Groups
/// Gets a list containing all <see cref="T:RGB.NET.Core.Led" /> of this <see cref="T:RGB.NET.Groups.RectangleLedGroup" />. /// Gets a list containing all <see cref="T:RGB.NET.Core.Led" /> of this <see cref="T:RGB.NET.Groups.RectangleLedGroup" />.
/// </summary> /// </summary>
/// <returns>The list containing all <see cref="T:RGB.NET.Core.Led" /> of this <see cref="T:RGB.NET.Groups.RectangleLedGroup" />.</returns> /// <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 ??= RGBSurface.Instance.Leds.Where(x => x.AbsoluteLedRectangle.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList(); public override IEnumerable<Led> GetLeds() => _ledCache ??= RGBSurface.Instance.Leds.Where(led => led.AbsoluteLedRectangle.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList();
private void InvalidateCache() => _ledCache = null; private void InvalidateCache() => _ledCache = null;