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

Added caching for actual layout data

This commit is contained in:
Darth Affe 2019-11-20 17:33:07 +01:00
parent fab502e0e4
commit 1126408069
3 changed files with 92 additions and 52 deletions

View File

@ -43,22 +43,23 @@ namespace RGB.NET.Core
protected set protected set
{ {
if (SetProperty(ref _size, value)) if (SetProperty(ref _size, value))
{ UpdateActualData();
OnPropertyChanged(nameof(ActualSize));
OnPropertyChanged(nameof(DeviceRectangle));
}
} }
} }
private Size _actualSize;
/// <inheritdoc /> /// <inheritdoc />
public Size ActualSize => Size * Scale; public Size ActualSize
{
get => _actualSize;
private set => SetProperty(ref _actualSize, value);
}
private Rectangle _deviceRectangle;
public Rectangle DeviceRectangle public Rectangle DeviceRectangle
{ {
get get => _deviceRectangle;
{ private set => SetProperty(ref _deviceRectangle, 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);
@ -69,10 +70,7 @@ namespace RGB.NET.Core
set set
{ {
if (SetProperty(ref _scale, value)) if (SetProperty(ref _scale, value))
{ UpdateActualData();
OnPropertyChanged(nameof(ActualSize));
OnPropertyChanged(nameof(DeviceRectangle));
}
} }
} }
@ -84,9 +82,7 @@ namespace RGB.NET.Core
set set
{ {
if (SetProperty(ref _rotation, value)) if (SetProperty(ref _rotation, value))
{ UpdateActualData();
OnPropertyChanged(nameof(DeviceRectangle));
}
} }
} }
@ -126,6 +122,12 @@ namespace RGB.NET.Core
#region Methods #region Methods
private void UpdateActualData()
{
ActualSize = Size * Scale;
DeviceRectangle = new Rectangle(Location, new Rectangle(new Rectangle(Location, ActualSize).Rotate(Rotation)).Size);
}
/// <inheritdoc /> /// <inheritdoc />
public virtual void Update(bool flushLeds = false) public virtual void Update(bool flushLeds = false)
{ {

View File

@ -45,53 +45,67 @@ namespace RGB.NET.Core
set => SetProperty(ref _shapeData, value); set => SetProperty(ref _shapeData, value);
} }
public Point Location { get; set; } private Point _location;
public Point Location
public Size Size { get; set; }
public Point ActualLocation
{ {
get get => _location;
set
{ {
Point point = (Location * Device.Scale); if (SetProperty(ref _location, value))
if (!Device.Rotation.Radians.EqualsInTolerance(0))
{ {
Point deviceCenter = new Rectangle(Device.ActualSize).Center; UpdateActualData();
Point actualDeviceCenter = Device.DeviceRectangle.Center; UpdateAbsoluteData();
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 Size ActualSize => Size * Device.Scale; private Size _size;
public Size Size
{
get => _size;
set
{
if (SetProperty(ref _size, value))
{
UpdateActualData();
UpdateAbsoluteData();
}
}
}
private Point _actualLocation;
public Point ActualLocation
{
get => _actualLocation;
private set => SetProperty(ref _actualLocation, value);
}
private Size _actualSize;
public Size ActualSize
{
get => _actualSize;
private set => SetProperty(ref _actualSize, value);
}
private Rectangle _ledRectangle;
/// <summary> /// <summary>
/// Gets a rectangle representing the logical location of the <see cref="Led"/> relative to the <see cref="Device"/>. /// Gets a rectangle representing the logical location of the <see cref="Led"/> relative to the <see cref="Device"/>.
/// </summary> /// </summary>
public Rectangle LedRectangle public Rectangle LedRectangle
{ {
get get => _ledRectangle;
{ private set => SetProperty(ref _ledRectangle, value);
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;
}
} }
private Rectangle _absoluteLedRectangle;
/// <summary> /// <summary>
/// Gets a rectangle representing the logical location of the <see cref="Led"/> on the <see cref="RGBSurface"/>. /// Gets a rectangle representing the logical location of the <see cref="Led"/> on the <see cref="RGBSurface"/>.
/// </summary> /// </summary>
public Rectangle AbsoluteLedRectangle => LedRectangle.Translate(Device.Location); public Rectangle AbsoluteLedRectangle
{
get => _absoluteLedRectangle;
private set => SetProperty(ref _absoluteLedRectangle, value);
}
/// <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.
@ -194,18 +208,40 @@ namespace RGB.NET.Core
private void DevicePropertyChanged(object sender, PropertyChangedEventArgs e) private void DevicePropertyChanged(object sender, PropertyChangedEventArgs e)
{ {
if ((e.PropertyName == nameof(IRGBDevice.Location))) if ((e.PropertyName == nameof(IRGBDevice.Location)))
{ UpdateAbsoluteData();
OnPropertyChanged(nameof(AbsoluteLedRectangle));
}
else if ((e.PropertyName == nameof(IRGBDevice.Scale)) || (e.PropertyName == nameof(IRGBDevice.Rotation))) else if ((e.PropertyName == nameof(IRGBDevice.Scale)) || (e.PropertyName == nameof(IRGBDevice.Rotation)))
{ {
OnPropertyChanged(nameof(LedRectangle)); UpdateActualData();
OnPropertyChanged(nameof(AbsoluteLedRectangle)); UpdateAbsoluteData();
OnPropertyChanged(nameof(ActualLocation));
OnPropertyChanged(nameof(ActualSize));
} }
} }
private void UpdateActualData()
{
ActualSize = Size * Device.Scale;
Point actualLocation = (Location * Device.Scale);
Rectangle ledRectangle = new Rectangle(Location * Device.Scale, Size * Device.Scale);
if (Device.Rotation.IsRotated)
{
Point deviceCenter = new Rectangle(Device.ActualSize).Center;
Point actualDeviceCenter = Device.DeviceRectangle.Center;
Point centerOffset = new Point(actualDeviceCenter.X - deviceCenter.X, actualDeviceCenter.Y - deviceCenter.Y);
actualLocation = actualLocation.Rotate(Device.Rotation, new Rectangle(Device.ActualSize).Center) + centerOffset;
ledRectangle = new Rectangle(ledRectangle.Rotate(Device.Rotation, new Rectangle(Device.ActualSize).Center)).Translate(centerOffset);
}
ActualLocation = actualLocation;
LedRectangle = ledRectangle;
}
private void UpdateAbsoluteData()
{
AbsoluteLedRectangle = LedRectangle.Translate(Device.Location);
}
/// <summary> /// <summary>
/// Converts the <see cref="Id"/> and the <see cref="Color"/> of this <see cref="Led"/> to a human-readable string. /// Converts the <see cref="Id"/> and the <see cref="Color"/> of this <see cref="Led"/> to a human-readable string.
/// </summary> /// </summary>

View File

@ -17,6 +17,8 @@ namespace RGB.NET.Core
public double Degrees { get; } public double Degrees { get; }
public double Radians { get; } public double Radians { get; }
public bool IsRotated => !Degrees.EqualsInTolerance(0);
#endregion #endregion
#region Constructors #region Constructors