From 5bc945c4f797226c6e8b0d39b7b0ad34ecaf23f1 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Tue, 29 Dec 2020 15:34:35 +0100 Subject: [PATCH] Removed static RGBSurface and DeviceSpecialParts --- .../AbstractUpdateAwareDecorator.cs | 9 ++- RGB.NET.Core/Devices/AbstractRGBDevice.cs | 20 +---- RGB.NET.Core/Devices/IRGBDevice.cs | 15 ---- RGB.NET.Core/Devices/IRGBDeviceProvider.cs | 2 +- RGB.NET.Core/Devices/IRGBDeviceSpecialPart.cs | 11 --- RGB.NET.Core/Groups/AbstractLedGroup.cs | 20 +++-- RGB.NET.Core/Groups/ILedGroup.cs | 6 +- RGB.NET.Core/RGBSurface.cs | 10 +-- RGB.NET.Decorators/Brush/FlashDecorator.cs | 8 ++ .../Gradient/MoveGradientDecorator.cs | 3 +- .../CorsairDeviceProvider.cs | 7 -- .../SpecialParts/LightbarSpecialPart.cs | 79 ------------------- .../Extensions/LedGroupExtension.cs | 11 +-- RGB.NET.Groups/Groups/ListLedGroup.cs | 32 ++------ RGB.NET.Groups/Groups/RectangleLedGroup.cs | 32 +++++--- 15 files changed, 75 insertions(+), 190 deletions(-) delete mode 100644 RGB.NET.Core/Devices/IRGBDeviceSpecialPart.cs delete mode 100644 RGB.NET.Devices.Corsair/SpecialParts/LightbarSpecialPart.cs diff --git a/RGB.NET.Core/Decorators/AbstractUpdateAwareDecorator.cs b/RGB.NET.Core/Decorators/AbstractUpdateAwareDecorator.cs index 98e64b2..7217d25 100644 --- a/RGB.NET.Core/Decorators/AbstractUpdateAwareDecorator.cs +++ b/RGB.NET.Core/Decorators/AbstractUpdateAwareDecorator.cs @@ -8,6 +8,8 @@ { #region Properties & Fields + protected RGBSurface Surface { get; } + /// /// Gets or sets if the should call even if the Decorator is disabled. /// @@ -21,8 +23,9 @@ /// Initializes a new instance of the class. /// /// Bool indicating if the should call even if the Decorator is disabled. - protected AbstractUpdateAwareDecorator(bool updateIfDisabled = false) + protected AbstractUpdateAwareDecorator(RGBSurface surface, bool updateIfDisabled = false) { + this.Surface = surface; this.UpdateIfDisabled = updateIfDisabled; } @@ -34,7 +37,7 @@ public override void OnAttached(IDecoratable decoratable) { if (DecoratedObjects.Count == 0) - RGBSurface.Instance.Updating += OnSurfaceUpdating; + Surface.Updating += OnSurfaceUpdating; base.OnAttached(decoratable); } @@ -45,7 +48,7 @@ base.OnDetached(decoratable); if (DecoratedObjects.Count == 0) - RGBSurface.Instance.Updating -= OnSurfaceUpdating; + Surface.Updating -= OnSurfaceUpdating; } private void OnSurfaceUpdating(UpdatingEventArgs args) diff --git a/RGB.NET.Core/Devices/AbstractRGBDevice.cs b/RGB.NET.Core/Devices/AbstractRGBDevice.cs index 8ea688a..7979542 100644 --- a/RGB.NET.Core/Devices/AbstractRGBDevice.cs +++ b/RGB.NET.Core/Devices/AbstractRGBDevice.cs @@ -104,11 +104,6 @@ namespace RGB.NET.Core /// protected Dictionary LedMapping { get; } = new Dictionary(); - /// - /// Gets a dictionary containing all associated with this . - /// - protected Dictionary SpecialDeviceParts { get; } = new Dictionary(); - #region Indexer /// @@ -149,13 +144,12 @@ namespace RGB.NET.Core } protected virtual IEnumerable GetLedsToUpdate(bool flushLeds) => ((RequiresFlush || flushLeds) ? LedMapping.Values : LedMapping.Values.Where(x => x.IsDirty)); - + /// public virtual void Dispose() { try { - SpecialDeviceParts.Clear(); LedMapping.Clear(); } catch { /* this really shouldn't happen */ } @@ -196,7 +190,7 @@ namespace RGB.NET.Core LedMapping.Add(ledId, led); return led; } - + /// /// Applies the given layout. /// @@ -248,16 +242,6 @@ namespace RGB.NET.Core /// The . protected virtual object CreateLedCustomData(LedId ledId) => null; - /// - public void AddSpecialDevicePart(T specialDevicePart) - where T : class, IRGBDeviceSpecialPart - => SpecialDeviceParts[typeof(T)] = specialDevicePart; - - /// - public T GetSpecialDevicePart() - where T : class, IRGBDeviceSpecialPart - => SpecialDeviceParts.TryGetValue(typeof(T), out IRGBDeviceSpecialPart devicePart) ? (T)devicePart : default; - #region Enumerator /// diff --git a/RGB.NET.Core/Devices/IRGBDevice.cs b/RGB.NET.Core/Devices/IRGBDevice.cs index f79b2b7..c21a06e 100644 --- a/RGB.NET.Core/Devices/IRGBDevice.cs +++ b/RGB.NET.Core/Devices/IRGBDevice.cs @@ -89,21 +89,6 @@ namespace RGB.NET.Core /// /// Specifies whether all (including clean ones) should be updated. void Update(bool flushLeds = false); - - /// - /// Adds the given to the device. - /// This will override existing of the same type. - /// - /// The to add. - /// The generic typeof of the to add. - void AddSpecialDevicePart(T specialDevicePart) where T : class, IRGBDeviceSpecialPart; - - /// - /// Gets the requested if available on this . - /// - /// The generic type of the requested . - /// The requested or null if not available in this . - T GetSpecialDevicePart() where T : class, IRGBDeviceSpecialPart; #endregion } diff --git a/RGB.NET.Core/Devices/IRGBDeviceProvider.cs b/RGB.NET.Core/Devices/IRGBDeviceProvider.cs index dca3645..0007614 100644 --- a/RGB.NET.Core/Devices/IRGBDeviceProvider.cs +++ b/RGB.NET.Core/Devices/IRGBDeviceProvider.cs @@ -39,7 +39,7 @@ namespace RGB.NET.Core bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false); /// - /// Resets all handled back top default. + /// Resets all handled back to default. /// void ResetDevices(); diff --git a/RGB.NET.Core/Devices/IRGBDeviceSpecialPart.cs b/RGB.NET.Core/Devices/IRGBDeviceSpecialPart.cs deleted file mode 100644 index 58c4e37..0000000 --- a/RGB.NET.Core/Devices/IRGBDeviceSpecialPart.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Collections.Generic; - -namespace RGB.NET.Core -{ - /// - /// - /// Represents a special part of a . - /// - public interface IRGBDeviceSpecialPart : IEnumerable - { } -} diff --git a/RGB.NET.Core/Groups/AbstractLedGroup.cs b/RGB.NET.Core/Groups/AbstractLedGroup.cs index eab6803..7bacd0c 100644 --- a/RGB.NET.Core/Groups/AbstractLedGroup.cs +++ b/RGB.NET.Core/Groups/AbstractLedGroup.cs @@ -11,6 +11,8 @@ namespace RGB.NET.Core { #region Properties & Fields + public RGBSurface? Surface { get; private set; } + /// public IBrush Brush { get; set; } @@ -24,11 +26,9 @@ namespace RGB.NET.Core /// /// Initializes a new instance of the class. /// - /// Specifies whether this should be automatically attached or not. - protected AbstractLedGroup(bool autoAttach) + protected AbstractLedGroup(RGBSurface? attachTo) { - if (autoAttach) - RGBSurface.Instance.AttachLedGroup(this); + attachTo?.AttachLedGroup(this); } #endregion @@ -39,12 +39,16 @@ namespace RGB.NET.Core public abstract IList GetLeds(); /// - public virtual void OnAttach() - { } + public virtual void OnAttach(RGBSurface surface) + { + Surface = surface; + } /// - public virtual void OnDetach() - { } + public virtual void OnDetach(RGBSurface surface) + { + Surface = null; + } #endregion } diff --git a/RGB.NET.Core/Groups/ILedGroup.cs b/RGB.NET.Core/Groups/ILedGroup.cs index 5db5e63..6d56a0f 100644 --- a/RGB.NET.Core/Groups/ILedGroup.cs +++ b/RGB.NET.Core/Groups/ILedGroup.cs @@ -11,6 +11,8 @@ namespace RGB.NET.Core /// public interface ILedGroup : IDecoratable { + public RGBSurface? Surface { get; } + /// /// Gets or sets the which should be drawn over this . /// @@ -30,11 +32,11 @@ namespace RGB.NET.Core /// /// Called when the is attached to the . /// - void OnAttach(); + void OnAttach(RGBSurface surface); /// /// Called when the is detached from the . /// - void OnDetach(); + void OnDetach(RGBSurface surface); } } diff --git a/RGB.NET.Core/RGBSurface.cs b/RGB.NET.Core/RGBSurface.cs index e5ce505..fdc92bb 100644 --- a/RGB.NET.Core/RGBSurface.cs +++ b/RGB.NET.Core/RGBSurface.cs @@ -18,7 +18,7 @@ namespace RGB.NET.Core public class RGBSurface : AbstractBindable, IDisposable { #region Properties & Fields - + private Stopwatch _deltaTimeCounter; private IList _deviceProvider = new List(); @@ -66,7 +66,7 @@ namespace RGB.NET.Core } #endregion - + #region EventHandler /// @@ -254,7 +254,7 @@ namespace RGB.NET.Core if (_ledGroups.Contains(ledGroup)) return false; _ledGroups.AddLast(ledGroup); - ledGroup.OnAttach(); + ledGroup.OnAttach(this); return true; } @@ -275,7 +275,7 @@ namespace RGB.NET.Core if (node == null) return false; _ledGroups.Remove(node); - node.Value.OnDetach(); + node.Value.OnDetach(this); return true; } @@ -443,7 +443,7 @@ namespace RGB.NET.Core } catch { /* Well ... that's not my fault */ } } - + #endregion } } diff --git a/RGB.NET.Decorators/Brush/FlashDecorator.cs b/RGB.NET.Decorators/Brush/FlashDecorator.cs index 580fce4..5900851 100644 --- a/RGB.NET.Decorators/Brush/FlashDecorator.cs +++ b/RGB.NET.Decorators/Brush/FlashDecorator.cs @@ -74,6 +74,14 @@ namespace RGB.NET.Decorators.Brush #endregion + #region Constructors + + public FlashDecorator(RGBSurface surface, bool updateIfDisabled = false) + : base(surface, updateIfDisabled) + { } + + #endregion + #region Methods /// diff --git a/RGB.NET.Decorators/Gradient/MoveGradientDecorator.cs b/RGB.NET.Decorators/Gradient/MoveGradientDecorator.cs index 9e7f86a..52a679b 100644 --- a/RGB.NET.Decorators/Gradient/MoveGradientDecorator.cs +++ b/RGB.NET.Decorators/Gradient/MoveGradientDecorator.cs @@ -45,7 +45,8 @@ namespace RGB.NET.Decorators.Gradient /// : 1 unit = 1 degree. /// The direction the is moved. /// True leads to an offset-increment (normaly moving to the right), false to an offset-decrement (normaly moving to the left). - public MoveGradientDecorator(double speed = 180.0, bool direction = true) + public MoveGradientDecorator(RGBSurface surface, double speed = 180.0, bool direction = true) + : base(surface) { this.Speed = speed; this.Direction = direction; diff --git a/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs b/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs index 74ac4ec..1e73657 100644 --- a/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs +++ b/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs @@ -150,7 +150,6 @@ namespace RGB.NET.Devices.Corsair deviceUpdateQueue = new CorsairDeviceUpdateQueue(UpdateTrigger, info.CorsairDeviceIndex); device.Initialize(deviceUpdateQueue); - AddSpecialParts(device); error = LastError; if (error != CorsairError.Success) @@ -265,12 +264,6 @@ namespace RGB.NET.Devices.Corsair return CorsairLedId.Invalid; } - private void AddSpecialParts(ICorsairRGBDevice device) - { - if (device.DeviceInfo.Model.Equals("K95 RGB Platinum", StringComparison.OrdinalIgnoreCase)) - device.AddSpecialDevicePart(new LightbarSpecialPart(device)); - } - /// public void ResetDevices() { diff --git a/RGB.NET.Devices.Corsair/SpecialParts/LightbarSpecialPart.cs b/RGB.NET.Devices.Corsair/SpecialParts/LightbarSpecialPart.cs deleted file mode 100644 index 705bac0..0000000 --- a/RGB.NET.Devices.Corsair/SpecialParts/LightbarSpecialPart.cs +++ /dev/null @@ -1,79 +0,0 @@ -// ReSharper disable UnusedMember.Global -// ReSharper disable MemberCanBePrivate.Global - -using System.Collections; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using RGB.NET.Core; - -namespace RGB.NET.Devices.Corsair -{ - /// - /// - /// Represents a lightbar attached to a - /// - public class LightbarSpecialPart : IRGBDeviceSpecialPart - { - #region Properties & Fields - - private List _leds; - /// - /// Gets a readonly collection of all of this . - /// - public IEnumerable Leds => new ReadOnlyCollection(_leds); - - private List _left; - /// - /// Gets a readonly collection of all in the left half of this . - /// - public IEnumerable Left => new ReadOnlyCollection(_left); - - private List _right; - /// - /// Gets a readonly collection of all in the right half of this . - /// - public IEnumerable Right => new ReadOnlyCollection(_right); - - /// - /// Gets the Center of this . - /// - public Led Center { get; } - - #endregion - - #region Constructors - - /// - /// Initializes a new instance of the class. - /// - /// The device associated with this . - public LightbarSpecialPart(IRGBDevice device) - { - _leds = device.Where(led => ((CorsairLedId)led.CustomData >= CorsairLedId.Lightbar1) && ((CorsairLedId)led.CustomData <= CorsairLedId.Lightbar19)).ToList(); - _left = _leds.Where(led => (CorsairLedId)led.CustomData < CorsairLedId.Lightbar10).ToList(); - _right = _leds.Where(led => (CorsairLedId)led.CustomData > CorsairLedId.Lightbar10).ToList(); - Center = _leds.FirstOrDefault(led => (CorsairLedId)led.CustomData == CorsairLedId.Lightbar10); - } - - #endregion - - #region Methods - - /// - /// - /// Returns an enumerator that iterates over all of the . - /// - /// An enumerator for all of the . - public IEnumerator GetEnumerator() => _leds.GetEnumerator(); - - /// - /// - /// Returns an enumerator that iterates over all of the . - /// - /// An enumerator for all of the . - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - - #endregion - } -} diff --git a/RGB.NET.Groups/Extensions/LedGroupExtension.cs b/RGB.NET.Groups/Extensions/LedGroupExtension.cs index cd09393..4391131 100644 --- a/RGB.NET.Groups/Extensions/LedGroupExtension.cs +++ b/RGB.NET.Groups/Extensions/LedGroupExtension.cs @@ -20,12 +20,13 @@ namespace RGB.NET.Groups // ReSharper disable once InvertIf if (!(ledGroup is ListLedGroup listLedGroup)) { - bool wasAttached = ledGroup.Detach(); - listLedGroup = new ListLedGroup(wasAttached, ledGroup.GetLeds()) { Brush = ledGroup.Brush }; + if (ledGroup.Surface != null) + ledGroup.Detach(ledGroup.Surface); + listLedGroup = new ListLedGroup(ledGroup.Surface, ledGroup.GetLeds()) { Brush = ledGroup.Brush }; } return listLedGroup; } - + /// /// Returns a new which contains all from the given excluding the specified ones. /// @@ -46,13 +47,13 @@ namespace RGB.NET.Groups /// /// The to attach. /// true if the could be attached; otherwise, false. - public static bool Attach(this ILedGroup ledGroup) => RGBSurface.Instance.AttachLedGroup(ledGroup); + public static bool Attach(this ILedGroup ledGroup, RGBSurface surface) => surface.AttachLedGroup(ledGroup); /// /// Detaches the given from the . /// /// The to attach. /// true if the could be detached; otherwise, false. - public static bool Detach(this ILedGroup ledGroup) => RGBSurface.Instance.DetachLedGroup(ledGroup); + public static bool Detach(this ILedGroup ledGroup, RGBSurface surface) => surface.DetachLedGroup(ledGroup); } } diff --git a/RGB.NET.Groups/Groups/ListLedGroup.cs b/RGB.NET.Groups/Groups/ListLedGroup.cs index eb833d8..94b0e26 100644 --- a/RGB.NET.Groups/Groups/ListLedGroup.cs +++ b/RGB.NET.Groups/Groups/ListLedGroup.cs @@ -28,36 +28,18 @@ namespace RGB.NET.Groups /// Initializes a new instance of the class. /// /// Specifies whether this should be automatically attached or not. - public ListLedGroup(bool autoAttach = true) - : base(autoAttach) + public ListLedGroup(RGBSurface? surface) + : base(surface) { } - - /// - /// - /// Initializes a new instance of the class. - /// - /// The initial of this . - public ListLedGroup(params Led[] leds) - : this(true, leds) - { } - - /// - /// - /// Initializes a new instance of the class. - /// - /// The initial of this . - public ListLedGroup(IEnumerable leds) - : this(true, leds) - { } - + /// /// /// Initializes a new instance of the class. /// /// Specifies whether this should be automatically attached or not. /// The initial of this . - public ListLedGroup(bool autoAttach, IEnumerable leds) - : base(autoAttach) + public ListLedGroup(RGBSurface? surface, IEnumerable leds) + : base(surface) { AddLeds(leds); } @@ -68,8 +50,8 @@ namespace RGB.NET.Groups /// /// Specifies whether this should be automatically attached or not. /// The initial of this . - public ListLedGroup(bool autoAttach, params Led[] leds) - : base(autoAttach) + public ListLedGroup(RGBSurface? surface, params Led[] leds) + : base(surface) { AddLeds(leds); } diff --git a/RGB.NET.Groups/Groups/RectangleLedGroup.cs b/RGB.NET.Groups/Groups/RectangleLedGroup.cs index 90bb88c..0d19568 100644 --- a/RGB.NET.Groups/Groups/RectangleLedGroup.cs +++ b/RGB.NET.Groups/Groups/RectangleLedGroup.cs @@ -16,7 +16,7 @@ namespace RGB.NET.Groups { #region Properties & Fields - private IList _ledCache; + private IList? _ledCache; private Rectangle _rectangle; /// @@ -58,8 +58,8 @@ namespace RGB.NET.Groups /// They second to calculate the of this from. /// (optional) The minimal percentage overlay a must have with the to be taken into the . (default: 0.5) /// (optional) Specifies whether this should be automatically attached or not. (default: true) - public RectangleLedGroup(Led fromLed, Led toLed, double minOverlayPercentage = 0.5, bool autoAttach = true) - : this(new Rectangle(fromLed.LedRectangle, toLed.LedRectangle), minOverlayPercentage, autoAttach) + public RectangleLedGroup(RGBSurface? surface, Led fromLed, Led toLed, double minOverlayPercentage = 0.5) + : this(surface, new Rectangle(fromLed.LedRectangle, toLed.LedRectangle), minOverlayPercentage) { } /// @@ -70,8 +70,8 @@ namespace RGB.NET.Groups /// They second point to calculate the of this from. /// (optional) The minimal percentage overlay a must have with the to be taken into the . (default: 0.5) /// (optional) Specifies whether this should be automatically attached or not. (default: true) - public RectangleLedGroup(Point fromPoint, Point toPoint, double minOverlayPercentage = 0.5, bool autoAttach = true) - : this(new Rectangle(fromPoint, toPoint), minOverlayPercentage, autoAttach) + public RectangleLedGroup(RGBSurface? surface, Point fromPoint, Point toPoint, double minOverlayPercentage = 0.5) + : this(surface, new Rectangle(fromPoint, toPoint), minOverlayPercentage) { } /// @@ -81,8 +81,8 @@ namespace RGB.NET.Groups /// The of this . /// (optional) The minimal percentage overlay a must have with the to be taken into the . (default: 0.5) /// (optional) Specifies whether this should be automatically attached or not. (default: true) - public RectangleLedGroup(Rectangle rectangle, double minOverlayPercentage = 0.5, bool autoAttach = true) - : base(autoAttach) + public RectangleLedGroup(RGBSurface? surface, Rectangle rectangle, double minOverlayPercentage = 0.5) + : base(surface) { this.Rectangle = rectangle; this.MinOverlayPercentage = minOverlayPercentage; @@ -93,10 +93,22 @@ namespace RGB.NET.Groups #region Methods /// - public override void OnAttach() => RGBSurface.Instance.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged; + public override void OnAttach(RGBSurface surface) + { + base.OnAttach(surface); + + if (Surface != null) + Surface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged; + } /// - public override void OnDetach() => RGBSurface.Instance.SurfaceLayoutChanged -= RGBSurfaceOnSurfaceLayoutChanged; + public override void OnDetach(RGBSurface surface) + { + if (Surface != null) + Surface.SurfaceLayoutChanged -= RGBSurfaceOnSurfaceLayoutChanged; + + base.OnDetach(surface); + } private void RGBSurfaceOnSurfaceLayoutChanged(SurfaceLayoutChangedEventArgs args) => InvalidateCache(); @@ -105,7 +117,7 @@ namespace RGB.NET.Groups /// Gets a list containing all of this . /// /// The list containing all of this . - public override IList GetLeds() => _ledCache ??= RGBSurface.Instance.Leds.Where(led => led.AbsoluteLedRectangle.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList(); + public override IList GetLeds() => _ledCache ??= (Surface?.Leds.Where(led => led.AbsoluteLedRectangle.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList() ?? new()); private void InvalidateCache() => _ledCache = null;