From 3554c274b90049926adceda0d84f607dd50e653a Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Tue, 31 Jan 2017 18:10:27 +0100 Subject: [PATCH] Changed RGBSurface from static to singleton --- RGB.NET.Core/Groups/AbstractLedGroup.cs | 2 +- RGB.NET.Core/RGBSurface.cs | 37 +++++++++++-------- RGB.NET.Core/RGBSurfaceDeviceEvents.cs | 16 ++++---- RGB.NET.Core/RGBSurfaceDeviceLoader.cs | 8 ++-- RGB.NET.Core/RGBSurfaceUpdater.cs | 27 ++++++++------ .../Extensions/LedGroupExtension.cs | 4 +- RGB.NET.Groups/Groups/RectangleLedGroup.cs | 6 +-- RGB.NET.WPF/Controls/RGBSurfaceVisualizer.cs | 11 ++++-- 8 files changed, 62 insertions(+), 49 deletions(-) diff --git a/RGB.NET.Core/Groups/AbstractLedGroup.cs b/RGB.NET.Core/Groups/AbstractLedGroup.cs index a1c65e5..52891d4 100644 --- a/RGB.NET.Core/Groups/AbstractLedGroup.cs +++ b/RGB.NET.Core/Groups/AbstractLedGroup.cs @@ -31,7 +31,7 @@ namespace RGB.NET.Core protected AbstractLedGroup(bool autoAttach) { if (autoAttach) - RGBSurface.AttachLedGroup(this); + RGBSurface.Instance.AttachLedGroup(this); } #endregion diff --git a/RGB.NET.Core/RGBSurface.cs b/RGB.NET.Core/RGBSurface.cs index 13351c7..fa5a038 100644 --- a/RGB.NET.Core/RGBSurface.cs +++ b/RGB.NET.Core/RGBSurface.cs @@ -11,37 +11,42 @@ namespace RGB.NET.Core /// /// Represents a RGB-surface containing multiple devices. /// - public static partial class RGBSurface + public partial class RGBSurface : AbstractBindable { #region Properties & Fields - private static DateTime _lastUpdate; + /// + /// Gets the singelot-instance of the class. + /// + public static RGBSurface Instance { get; } = new RGBSurface(); - private static IList _deviceProvider = new List(); - private static IList _devices = new List(); + private DateTime _lastUpdate; + + private IList _deviceProvider = new List(); + private IList _devices = new List(); // ReSharper disable InconsistentNaming - private static readonly LinkedList _ledGroups = new LinkedList(); + private readonly LinkedList _ledGroups = new LinkedList(); - private static readonly Rectangle _surfaceRectangle = new Rectangle(); + private readonly Rectangle _surfaceRectangle = new Rectangle(); // ReSharper restore InconsistentNaming /// /// Gets a readonly list containing all loaded . /// - public static IEnumerable Devices => new ReadOnlyCollection(_devices); + public IEnumerable Devices => new ReadOnlyCollection(_devices); /// /// Gets a copy of the representing this . /// - public static Rectangle SurfaceRectangle => new Rectangle(_surfaceRectangle); + public Rectangle SurfaceRectangle => new Rectangle(_surfaceRectangle); /// /// Gets a list of all on this . /// - public static IEnumerable Leds => _devices.SelectMany(x => x); + public IEnumerable Leds => _devices.SelectMany(x => x); #endregion @@ -50,7 +55,7 @@ namespace RGB.NET.Core /// /// Initializes a new instance of the class. /// - static RGBSurface() + private RGBSurface() { _lastUpdate = DateTime.Now; } @@ -63,7 +68,7 @@ namespace RGB.NET.Core /// Perform an update for all dirty , or all , if flushLeds is set to true. /// /// Specifies whether all , (including clean ones) should be updated. - public static void Update(bool flushLeds = false) + public void Update(bool flushLeds = false) { OnUpdating(); @@ -88,7 +93,7 @@ namespace RGB.NET.Core /// Renders a ledgroup. /// /// The led group to render. - private static void Render(ILedGroup ledGroup) + private void Render(ILedGroup ledGroup) { IList leds = ledGroup.GetLeds().ToList(); IBrush brush = ledGroup.Brush; @@ -127,7 +132,7 @@ namespace RGB.NET.Core } } - private static Rectangle GetDeviceLedLocation(Led led, Point extraOffset = null) + private Rectangle GetDeviceLedLocation(Led led, Point extraOffset = null) { return extraOffset != null ? new Rectangle(led.LedRectangle.Location + led.Device.Location + extraOffset, led.LedRectangle.Size) @@ -139,7 +144,7 @@ namespace RGB.NET.Core /// /// The to attach. /// true if the could be attached; otherwise, false. - public static bool AttachLedGroup(ILedGroup ledGroup) + public bool AttachLedGroup(ILedGroup ledGroup) { if (ledGroup == null) return false; @@ -159,7 +164,7 @@ namespace RGB.NET.Core /// /// The to detached. /// true if the could be detached; otherwise, false. - public static bool DetachLedGroup(ILedGroup ledGroup) + public bool DetachLedGroup(ILedGroup ledGroup) { if (ledGroup == null) return false; @@ -175,7 +180,7 @@ namespace RGB.NET.Core } } - private static void UpdateSurfaceRectangle() + private void UpdateSurfaceRectangle() { Rectangle devicesRectangle = new Rectangle(_devices.Select(d => new Rectangle(d.Location, d.Size))); diff --git a/RGB.NET.Core/RGBSurfaceDeviceEvents.cs b/RGB.NET.Core/RGBSurfaceDeviceEvents.cs index 3bd5acb..ddb9a30 100644 --- a/RGB.NET.Core/RGBSurfaceDeviceEvents.cs +++ b/RGB.NET.Core/RGBSurfaceDeviceEvents.cs @@ -2,7 +2,7 @@ namespace RGB.NET.Core { - public static partial class RGBSurface + public partial class RGBSurface { #region EventHandler @@ -39,22 +39,22 @@ namespace RGB.NET.Core /// /// Occurs when a catched exception is thrown inside the . /// - public static event ExceptionEventHandler Exception; + public event ExceptionEventHandler Exception; /// /// Occurs when the starts updating. /// - public static event UpdatingEventHandler Updating; + public event UpdatingEventHandler Updating; /// /// Occurs when the update is done. /// - public static event UpdatedEventHandler Updated; + public event UpdatedEventHandler Updated; /// /// Occurs when the layout of this changed. /// - public static event SurfaceLayoutChangedEventHandler SurfaceLayoutChanged; + public event SurfaceLayoutChangedEventHandler SurfaceLayoutChanged; // ReSharper restore EventNeverSubscribedTo.Global @@ -66,7 +66,7 @@ namespace RGB.NET.Core /// Handles the needed event-calls for an exception. /// /// The exception previously thrown. - private static void OnException(Exception ex) + private void OnException(Exception ex) { try { @@ -78,7 +78,7 @@ namespace RGB.NET.Core /// /// Handles the needed event-calls before updating. /// - private static void OnUpdating() + private void OnUpdating() { try { @@ -92,7 +92,7 @@ namespace RGB.NET.Core /// /// Handles the needed event-calls after an update. /// - private static void OnUpdated() + private void OnUpdated() { try { diff --git a/RGB.NET.Core/RGBSurfaceDeviceLoader.cs b/RGB.NET.Core/RGBSurfaceDeviceLoader.cs index f7ed23f..afe9bba 100644 --- a/RGB.NET.Core/RGBSurfaceDeviceLoader.cs +++ b/RGB.NET.Core/RGBSurfaceDeviceLoader.cs @@ -4,7 +4,7 @@ using System.Linq; namespace RGB.NET.Core { - public static partial class RGBSurface + public partial class RGBSurface { #region Methods @@ -13,7 +13,7 @@ namespace RGB.NET.Core /// Loads all devices the given is able to provide. /// /// - public static void LoadDevices(IRGBDeviceProvider deviceProvider) + public void LoadDevices(IRGBDeviceProvider deviceProvider) { if (_deviceProvider.Contains(deviceProvider) || _deviceProvider.Any(x => x.GetType() == deviceProvider.GetType())) return; @@ -41,7 +41,7 @@ namespace RGB.NET.Core } } - private static void DeviceOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) + private void DeviceOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) { if (string.Equals(propertyChangedEventArgs.PropertyName, nameof(IRGBDevice.Location))) { @@ -52,7 +52,7 @@ namespace RGB.NET.Core } } - private static void DeviceLocationOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) + private void DeviceLocationOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) { UpdateSurfaceRectangle(); SurfaceLayoutChanged?.Invoke(new SurfaceLayoutChangedEventArgs(new[] { sender as IRGBDevice }, false, true)); diff --git a/RGB.NET.Core/RGBSurfaceUpdater.cs b/RGB.NET.Core/RGBSurfaceUpdater.cs index 162990e..253921d 100644 --- a/RGB.NET.Core/RGBSurfaceUpdater.cs +++ b/RGB.NET.Core/RGBSurfaceUpdater.cs @@ -4,30 +4,35 @@ using System.Threading.Tasks; namespace RGB.NET.Core { - public static partial class RGBSurface + public partial class RGBSurface { #region Properties & Fields - private static CancellationTokenSource _updateTokenSource; - private static CancellationToken _updateToken; - private static Task _updateTask; + private CancellationTokenSource _updateTokenSource; + private CancellationToken _updateToken; + private Task _updateTask; + private double _updateFrequency = 1.0 / 30.0; /// /// Gets or sets the update-frequency in seconds. (Calculate by using '1.0 / updates per second') /// - public static double UpdateFrequency { get; set; } = 1.0 / 30.0; + public double UpdateFrequency + { + get { return _updateFrequency; } + set { SetProperty(ref _updateFrequency, value); } + } - private static UpdateMode _updateMode = UpdateMode.Manual; + private UpdateMode _updateMode = UpdateMode.Manual; /// /// Gets or sets the update-mode. /// - public static UpdateMode UpdateMode + public UpdateMode UpdateMode { get { return _updateMode; } set { - _updateMode = value; - CheckUpdateLoop(); + if (SetProperty(ref _updateMode, value)) + CheckUpdateLoop(); } } @@ -39,7 +44,7 @@ namespace RGB.NET.Core /// Checks if automatic updates should occur and starts/stops the update-loop if needed. /// /// Thrown if the requested update-mode is not available. - private static async void CheckUpdateLoop() + private async void CheckUpdateLoop() { bool shouldRun; switch (UpdateMode) @@ -69,7 +74,7 @@ namespace RGB.NET.Core } } - private static void UpdateLoop() + private void UpdateLoop() { while (!_updateToken.IsCancellationRequested) { diff --git a/RGB.NET.Groups/Extensions/LedGroupExtension.cs b/RGB.NET.Groups/Extensions/LedGroupExtension.cs index 3992e42..82fec5d 100644 --- a/RGB.NET.Groups/Extensions/LedGroupExtension.cs +++ b/RGB.NET.Groups/Extensions/LedGroupExtension.cs @@ -63,7 +63,7 @@ namespace RGB.NET.Groups /// true if the could be attached; otherwise, false. public static bool Attach(this ILedGroup ledGroup) { - return RGBSurface.AttachLedGroup(ledGroup); + return RGBSurface.Instance.AttachLedGroup(ledGroup); } /// @@ -73,7 +73,7 @@ namespace RGB.NET.Groups /// true if the could be detached; otherwise, false. public static bool Detach(this ILedGroup ledGroup) { - return RGBSurface.DetachLedGroup(ledGroup); + return RGBSurface.Instance.DetachLedGroup(ledGroup); } } } diff --git a/RGB.NET.Groups/Groups/RectangleLedGroup.cs b/RGB.NET.Groups/Groups/RectangleLedGroup.cs index d5d9e73..a53e741 100644 --- a/RGB.NET.Groups/Groups/RectangleLedGroup.cs +++ b/RGB.NET.Groups/Groups/RectangleLedGroup.cs @@ -102,13 +102,13 @@ namespace RGB.NET.Groups /// public override void OnAttach() { - RGBSurface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged; + RGBSurface.Instance.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged; } /// public override void OnDetach() { - RGBSurface.SurfaceLayoutChanged -= RGBSurfaceOnSurfaceLayoutChanged; + RGBSurface.Instance.SurfaceLayoutChanged -= RGBSurfaceOnSurfaceLayoutChanged; } private void RGBSurfaceOnSurfaceLayoutChanged(SurfaceLayoutChangedEventArgs args) @@ -122,7 +122,7 @@ namespace RGB.NET.Groups /// The list containing all of this . public override IEnumerable GetLeds() { - return _ledCache ?? (_ledCache = RGBSurface.Leds.Where(x => x.LedRectangle.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList()); + return _ledCache ?? (_ledCache = RGBSurface.Instance.Leds.Where(x => x.LedRectangle.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList()); } private void InvalidateCache() diff --git a/RGB.NET.WPF/Controls/RGBSurfaceVisualizer.cs b/RGB.NET.WPF/Controls/RGBSurfaceVisualizer.cs index 1d4ef1c..8881eba 100644 --- a/RGB.NET.WPF/Controls/RGBSurfaceVisualizer.cs +++ b/RGB.NET.WPF/Controls/RGBSurfaceVisualizer.cs @@ -18,6 +18,7 @@ namespace RGB.NET.WPF.Controls #region Properties & Fields + private RGBSurface _surface; private Canvas _canvas; #endregion @@ -29,8 +30,10 @@ namespace RGB.NET.WPF.Controls /// public RGBSurfaceVisualizer() { - RGBSurface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged; - foreach (IRGBDevice device in RGBSurface.Devices) + _surface = RGBSurface.Instance; + + _surface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged; + foreach (IRGBDevice device in _surface.Devices) AddDevice(device); } @@ -40,8 +43,8 @@ namespace RGB.NET.WPF.Controls foreach (IRGBDevice device in args.Devices) AddDevice(device); - _canvas.Width = RGBSurface.SurfaceRectangle.Size.Width; - _canvas.Height = RGBSurface.SurfaceRectangle.Size.Height; + _canvas.Width = _surface.SurfaceRectangle.Size.Width; + _canvas.Height = _surface.SurfaceRectangle.Size.Height; } #endregion