From 1126408069377af0a426eac126cac1e8b3c26ef9 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Wed, 20 Nov 2019 17:33:07 +0100 Subject: [PATCH] Added caching for actual layout data --- RGB.NET.Core/Devices/AbstractRGBDevice.cs | 34 +++---- RGB.NET.Core/Leds/Led.cs | 108 ++++++++++++++-------- RGB.NET.Core/Positioning/Rotation.cs | 2 + 3 files changed, 92 insertions(+), 52 deletions(-) diff --git a/RGB.NET.Core/Devices/AbstractRGBDevice.cs b/RGB.NET.Core/Devices/AbstractRGBDevice.cs index 8a50ae9..e1f9e10 100644 --- a/RGB.NET.Core/Devices/AbstractRGBDevice.cs +++ b/RGB.NET.Core/Devices/AbstractRGBDevice.cs @@ -43,22 +43,23 @@ namespace RGB.NET.Core protected set { if (SetProperty(ref _size, value)) - { - OnPropertyChanged(nameof(ActualSize)); - OnPropertyChanged(nameof(DeviceRectangle)); - } + UpdateActualData(); } } + private Size _actualSize; /// - public Size ActualSize => Size * Scale; + public Size ActualSize + { + get => _actualSize; + private set => SetProperty(ref _actualSize, value); + } + private Rectangle _deviceRectangle; public Rectangle DeviceRectangle { - get - { - return new Rectangle(Location, new Rectangle(new Rectangle(Location, ActualSize).Rotate(Rotation)).Size); - } + get => _deviceRectangle; + private set => SetProperty(ref _deviceRectangle, value); } private Scale _scale = new Scale(1); @@ -69,10 +70,7 @@ namespace RGB.NET.Core set { if (SetProperty(ref _scale, value)) - { - OnPropertyChanged(nameof(ActualSize)); - OnPropertyChanged(nameof(DeviceRectangle)); - } + UpdateActualData(); } } @@ -84,9 +82,7 @@ namespace RGB.NET.Core set { if (SetProperty(ref _rotation, value)) - { - OnPropertyChanged(nameof(DeviceRectangle)); - } + UpdateActualData(); } } @@ -126,6 +122,12 @@ namespace RGB.NET.Core #region Methods + private void UpdateActualData() + { + ActualSize = Size * Scale; + DeviceRectangle = new Rectangle(Location, new Rectangle(new Rectangle(Location, ActualSize).Rotate(Rotation)).Size); + } + /// public virtual void Update(bool flushLeds = false) { diff --git a/RGB.NET.Core/Leds/Led.cs b/RGB.NET.Core/Leds/Led.cs index d172269..a514d80 100644 --- a/RGB.NET.Core/Leds/Led.cs +++ b/RGB.NET.Core/Leds/Led.cs @@ -45,53 +45,67 @@ namespace RGB.NET.Core set => SetProperty(ref _shapeData, value); } - public Point Location { get; set; } - - public Size Size { get; set; } - - public Point ActualLocation + private Point _location; + public Point Location { - get + get => _location; + set { - Point point = (Location * Device.Scale); - if (!Device.Rotation.Radians.EqualsInTolerance(0)) + if (SetProperty(ref _location, value)) { - Point deviceCenter = new Rectangle(Device.ActualSize).Center; - 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; + UpdateActualData(); + UpdateAbsoluteData(); } - - 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; /// /// Gets a rectangle representing the logical location of the relative to the . /// 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; - } + get => _ledRectangle; + private set => SetProperty(ref _ledRectangle, value); } + private Rectangle _absoluteLedRectangle; /// /// Gets a rectangle representing the logical location of the on the . /// - public Rectangle AbsoluteLedRectangle => LedRectangle.Translate(Device.Location); + public Rectangle AbsoluteLedRectangle + { + get => _absoluteLedRectangle; + private set => SetProperty(ref _absoluteLedRectangle, value); + } /// /// Indicates whether the is about to change it's color. @@ -194,18 +208,40 @@ namespace RGB.NET.Core private void DevicePropertyChanged(object sender, PropertyChangedEventArgs e) { if ((e.PropertyName == nameof(IRGBDevice.Location))) - { - OnPropertyChanged(nameof(AbsoluteLedRectangle)); - } + UpdateAbsoluteData(); else if ((e.PropertyName == nameof(IRGBDevice.Scale)) || (e.PropertyName == nameof(IRGBDevice.Rotation))) { - OnPropertyChanged(nameof(LedRectangle)); - OnPropertyChanged(nameof(AbsoluteLedRectangle)); - OnPropertyChanged(nameof(ActualLocation)); - OnPropertyChanged(nameof(ActualSize)); + UpdateActualData(); + UpdateAbsoluteData(); } } + 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); + } + /// /// Converts the and the of this to a human-readable string. /// diff --git a/RGB.NET.Core/Positioning/Rotation.cs b/RGB.NET.Core/Positioning/Rotation.cs index 92e6c5b..aca7f87 100644 --- a/RGB.NET.Core/Positioning/Rotation.cs +++ b/RGB.NET.Core/Positioning/Rotation.cs @@ -17,6 +17,8 @@ namespace RGB.NET.Core public double Degrees { get; } public double Radians { get; } + public bool IsRotated => !Degrees.EqualsInTolerance(0); + #endregion #region Constructors