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

Changed RGBSurface from static to singleton

This commit is contained in:
Darth Affe 2017-01-31 18:10:27 +01:00
parent 62626e91f2
commit 3554c274b9
8 changed files with 62 additions and 49 deletions

View File

@ -31,7 +31,7 @@ namespace RGB.NET.Core
protected AbstractLedGroup(bool autoAttach) protected AbstractLedGroup(bool autoAttach)
{ {
if (autoAttach) if (autoAttach)
RGBSurface.AttachLedGroup(this); RGBSurface.Instance.AttachLedGroup(this);
} }
#endregion #endregion

View File

@ -11,37 +11,42 @@ namespace RGB.NET.Core
/// <summary> /// <summary>
/// Represents a RGB-surface containing multiple devices. /// Represents a RGB-surface containing multiple devices.
/// </summary> /// </summary>
public static partial class RGBSurface public partial class RGBSurface : AbstractBindable
{ {
#region Properties & Fields #region Properties & Fields
private static DateTime _lastUpdate; /// <summary>
/// Gets the singelot-instance of the <see cref="RGBSurface"/> class.
/// </summary>
public static RGBSurface Instance { get; } = new RGBSurface();
private static IList<IRGBDeviceProvider> _deviceProvider = new List<IRGBDeviceProvider>(); private DateTime _lastUpdate;
private static IList<IRGBDevice> _devices = new List<IRGBDevice>();
private IList<IRGBDeviceProvider> _deviceProvider = new List<IRGBDeviceProvider>();
private IList<IRGBDevice> _devices = new List<IRGBDevice>();
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
private static readonly LinkedList<ILedGroup> _ledGroups = new LinkedList<ILedGroup>(); private readonly LinkedList<ILedGroup> _ledGroups = new LinkedList<ILedGroup>();
private static readonly Rectangle _surfaceRectangle = new Rectangle(); private readonly Rectangle _surfaceRectangle = new Rectangle();
// ReSharper restore InconsistentNaming // ReSharper restore InconsistentNaming
/// <summary> /// <summary>
/// Gets a readonly list containing all loaded <see cref="IRGBDevice"/>. /// Gets a readonly list containing all loaded <see cref="IRGBDevice"/>.
/// </summary> /// </summary>
public static IEnumerable<IRGBDevice> Devices => new ReadOnlyCollection<IRGBDevice>(_devices); public IEnumerable<IRGBDevice> Devices => new ReadOnlyCollection<IRGBDevice>(_devices);
/// <summary> /// <summary>
/// Gets a copy of the <see cref="Rectangle"/> representing this <see cref="RGBSurface"/>. /// Gets a copy of the <see cref="Rectangle"/> representing this <see cref="RGBSurface"/>.
/// </summary> /// </summary>
public static Rectangle SurfaceRectangle => new Rectangle(_surfaceRectangle); public Rectangle SurfaceRectangle => new Rectangle(_surfaceRectangle);
/// <summary> /// <summary>
/// Gets a list of all <see cref="Led"/> on this <see cref="RGBSurface"/>. /// Gets a list of all <see cref="Led"/> on this <see cref="RGBSurface"/>.
/// </summary> /// </summary>
public static IEnumerable<Led> Leds => _devices.SelectMany(x => x); public IEnumerable<Led> Leds => _devices.SelectMany(x => x);
#endregion #endregion
@ -50,7 +55,7 @@ namespace RGB.NET.Core
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RGBSurface"/> class. /// Initializes a new instance of the <see cref="RGBSurface"/> class.
/// </summary> /// </summary>
static RGBSurface() private RGBSurface()
{ {
_lastUpdate = DateTime.Now; _lastUpdate = DateTime.Now;
} }
@ -63,7 +68,7 @@ namespace RGB.NET.Core
/// Perform an update for all dirty <see cref="Led"/>, or all <see cref="Led"/>, if flushLeds is set to true. /// Perform an update for all dirty <see cref="Led"/>, or all <see cref="Led"/>, if flushLeds is set to true.
/// </summary> /// </summary>
/// <param name="flushLeds">Specifies whether all <see cref="Led"/>, (including clean ones) should be updated.</param> /// <param name="flushLeds">Specifies whether all <see cref="Led"/>, (including clean ones) should be updated.</param>
public static void Update(bool flushLeds = false) public void Update(bool flushLeds = false)
{ {
OnUpdating(); OnUpdating();
@ -88,7 +93,7 @@ namespace RGB.NET.Core
/// Renders a ledgroup. /// Renders a ledgroup.
/// </summary> /// </summary>
/// <param name="ledGroup">The led group to render.</param> /// <param name="ledGroup">The led group to render.</param>
private static void Render(ILedGroup ledGroup) private void Render(ILedGroup ledGroup)
{ {
IList<Led> leds = ledGroup.GetLeds().ToList(); IList<Led> leds = ledGroup.GetLeds().ToList();
IBrush brush = ledGroup.Brush; 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 return extraOffset != null
? new Rectangle(led.LedRectangle.Location + led.Device.Location + extraOffset, led.LedRectangle.Size) ? new Rectangle(led.LedRectangle.Location + led.Device.Location + extraOffset, led.LedRectangle.Size)
@ -139,7 +144,7 @@ namespace RGB.NET.Core
/// </summary> /// </summary>
/// <param name="ledGroup">The <see cref="ILedGroup"/> to attach.</param> /// <param name="ledGroup">The <see cref="ILedGroup"/> to attach.</param>
/// <returns><c>true</c> if the <see cref="ILedGroup"/> could be attached; otherwise, <c>false</c>.</returns> /// <returns><c>true</c> if the <see cref="ILedGroup"/> could be attached; otherwise, <c>false</c>.</returns>
public static bool AttachLedGroup(ILedGroup ledGroup) public bool AttachLedGroup(ILedGroup ledGroup)
{ {
if (ledGroup == null) return false; if (ledGroup == null) return false;
@ -159,7 +164,7 @@ namespace RGB.NET.Core
/// </summary> /// </summary>
/// <param name="ledGroup">The <see cref="ILedGroup"/> to detached.</param> /// <param name="ledGroup">The <see cref="ILedGroup"/> to detached.</param>
/// <returns><c>true</c> if the <see cref="ILedGroup"/> could be detached; otherwise, <c>false</c>.</returns> /// <returns><c>true</c> if the <see cref="ILedGroup"/> could be detached; otherwise, <c>false</c>.</returns>
public static bool DetachLedGroup(ILedGroup ledGroup) public bool DetachLedGroup(ILedGroup ledGroup)
{ {
if (ledGroup == null) return false; 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))); Rectangle devicesRectangle = new Rectangle(_devices.Select(d => new Rectangle(d.Location, d.Size)));

View File

@ -2,7 +2,7 @@
namespace RGB.NET.Core namespace RGB.NET.Core
{ {
public static partial class RGBSurface public partial class RGBSurface
{ {
#region EventHandler #region EventHandler
@ -39,22 +39,22 @@ namespace RGB.NET.Core
/// <summary> /// <summary>
/// Occurs when a catched exception is thrown inside the <see cref="RGBSurface"/>. /// Occurs when a catched exception is thrown inside the <see cref="RGBSurface"/>.
/// </summary> /// </summary>
public static event ExceptionEventHandler Exception; public event ExceptionEventHandler Exception;
/// <summary> /// <summary>
/// Occurs when the <see cref="RGBSurface"/> starts updating. /// Occurs when the <see cref="RGBSurface"/> starts updating.
/// </summary> /// </summary>
public static event UpdatingEventHandler Updating; public event UpdatingEventHandler Updating;
/// <summary> /// <summary>
/// Occurs when the <see cref="RGBSurface"/> update is done. /// Occurs when the <see cref="RGBSurface"/> update is done.
/// </summary> /// </summary>
public static event UpdatedEventHandler Updated; public event UpdatedEventHandler Updated;
/// <summary> /// <summary>
/// Occurs when the layout of this <see cref="RGBSurface"/> changed. /// Occurs when the layout of this <see cref="RGBSurface"/> changed.
/// </summary> /// </summary>
public static event SurfaceLayoutChangedEventHandler SurfaceLayoutChanged; public event SurfaceLayoutChangedEventHandler SurfaceLayoutChanged;
// ReSharper restore EventNeverSubscribedTo.Global // ReSharper restore EventNeverSubscribedTo.Global
@ -66,7 +66,7 @@ namespace RGB.NET.Core
/// Handles the needed event-calls for an exception. /// Handles the needed event-calls for an exception.
/// </summary> /// </summary>
/// <param name="ex">The exception previously thrown.</param> /// <param name="ex">The exception previously thrown.</param>
private static void OnException(Exception ex) private void OnException(Exception ex)
{ {
try try
{ {
@ -78,7 +78,7 @@ namespace RGB.NET.Core
/// <summary> /// <summary>
/// Handles the needed event-calls before updating. /// Handles the needed event-calls before updating.
/// </summary> /// </summary>
private static void OnUpdating() private void OnUpdating()
{ {
try try
{ {
@ -92,7 +92,7 @@ namespace RGB.NET.Core
/// <summary> /// <summary>
/// Handles the needed event-calls after an update. /// Handles the needed event-calls after an update.
/// </summary> /// </summary>
private static void OnUpdated() private void OnUpdated()
{ {
try try
{ {

View File

@ -4,7 +4,7 @@ using System.Linq;
namespace RGB.NET.Core namespace RGB.NET.Core
{ {
public static partial class RGBSurface public partial class RGBSurface
{ {
#region Methods #region Methods
@ -13,7 +13,7 @@ namespace RGB.NET.Core
/// Loads all devices the given <see cref="IRGBDeviceProvider"/> is able to provide. /// Loads all devices the given <see cref="IRGBDeviceProvider"/> is able to provide.
/// </summary> /// </summary>
/// <param name="deviceProvider"></param> /// <param name="deviceProvider"></param>
public static void LoadDevices(IRGBDeviceProvider deviceProvider) public void LoadDevices(IRGBDeviceProvider deviceProvider)
{ {
if (_deviceProvider.Contains(deviceProvider) || _deviceProvider.Any(x => x.GetType() == deviceProvider.GetType())) return; 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))) 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(); UpdateSurfaceRectangle();
SurfaceLayoutChanged?.Invoke(new SurfaceLayoutChangedEventArgs(new[] { sender as IRGBDevice }, false, true)); SurfaceLayoutChanged?.Invoke(new SurfaceLayoutChangedEventArgs(new[] { sender as IRGBDevice }, false, true));

View File

@ -4,30 +4,35 @@ using System.Threading.Tasks;
namespace RGB.NET.Core namespace RGB.NET.Core
{ {
public static partial class RGBSurface public partial class RGBSurface
{ {
#region Properties & Fields #region Properties & Fields
private static CancellationTokenSource _updateTokenSource; private CancellationTokenSource _updateTokenSource;
private static CancellationToken _updateToken; private CancellationToken _updateToken;
private static Task _updateTask; private Task _updateTask;
private double _updateFrequency = 1.0 / 30.0;
/// <summary> /// <summary>
/// Gets or sets the update-frequency in seconds. (Calculate by using '1.0 / updates per second') /// Gets or sets the update-frequency in seconds. (Calculate by using '1.0 / updates per second')
/// </summary> /// </summary>
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;
/// <summary> /// <summary>
/// Gets or sets the update-mode. /// Gets or sets the update-mode.
/// </summary> /// </summary>
public static UpdateMode UpdateMode public UpdateMode UpdateMode
{ {
get { return _updateMode; } get { return _updateMode; }
set set
{ {
_updateMode = value; if (SetProperty(ref _updateMode, value))
CheckUpdateLoop(); CheckUpdateLoop();
} }
} }
@ -39,7 +44,7 @@ namespace RGB.NET.Core
/// Checks if automatic updates should occur and starts/stops the update-loop if needed. /// Checks if automatic updates should occur and starts/stops the update-loop if needed.
/// </summary> /// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown if the requested update-mode is not available.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if the requested update-mode is not available.</exception>
private static async void CheckUpdateLoop() private async void CheckUpdateLoop()
{ {
bool shouldRun; bool shouldRun;
switch (UpdateMode) switch (UpdateMode)
@ -69,7 +74,7 @@ namespace RGB.NET.Core
} }
} }
private static void UpdateLoop() private void UpdateLoop()
{ {
while (!_updateToken.IsCancellationRequested) while (!_updateToken.IsCancellationRequested)
{ {

View File

@ -63,7 +63,7 @@ namespace RGB.NET.Groups
/// <returns><c>true</c> if the <see cref="ILedGroup"/> could be attached; otherwise, <c>false</c>.</returns> /// <returns><c>true</c> if the <see cref="ILedGroup"/> could be attached; otherwise, <c>false</c>.</returns>
public static bool Attach(this ILedGroup ledGroup) public static bool Attach(this ILedGroup ledGroup)
{ {
return RGBSurface.AttachLedGroup(ledGroup); return RGBSurface.Instance.AttachLedGroup(ledGroup);
} }
/// <summary> /// <summary>
@ -73,7 +73,7 @@ namespace RGB.NET.Groups
/// <returns><c>true</c> if the <see cref="ILedGroup"/> could be detached; otherwise, <c>false</c>.</returns> /// <returns><c>true</c> if the <see cref="ILedGroup"/> could be detached; otherwise, <c>false</c>.</returns>
public static bool Detach(this ILedGroup ledGroup) public static bool Detach(this ILedGroup ledGroup)
{ {
return RGBSurface.DetachLedGroup(ledGroup); return RGBSurface.Instance.DetachLedGroup(ledGroup);
} }
} }
} }

View File

@ -102,13 +102,13 @@ namespace RGB.NET.Groups
/// <inheritdoc /> /// <inheritdoc />
public override void OnAttach() public override void OnAttach()
{ {
RGBSurface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged; RGBSurface.Instance.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged;
} }
/// <inheritdoc /> /// <inheritdoc />
public override void OnDetach() public override void OnDetach()
{ {
RGBSurface.SurfaceLayoutChanged -= RGBSurfaceOnSurfaceLayoutChanged; RGBSurface.Instance.SurfaceLayoutChanged -= RGBSurfaceOnSurfaceLayoutChanged;
} }
private void RGBSurfaceOnSurfaceLayoutChanged(SurfaceLayoutChangedEventArgs args) private void RGBSurfaceOnSurfaceLayoutChanged(SurfaceLayoutChangedEventArgs args)
@ -122,7 +122,7 @@ namespace RGB.NET.Groups
/// <returns>The list containing all <see cref="Led"/> of this <see cref="RectangleLedGroup"/>.</returns> /// <returns>The list containing all <see cref="Led"/> of this <see cref="RectangleLedGroup"/>.</returns>
public override IEnumerable<Led> GetLeds() public override IEnumerable<Led> 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() private void InvalidateCache()

View File

@ -18,6 +18,7 @@ namespace RGB.NET.WPF.Controls
#region Properties & Fields #region Properties & Fields
private RGBSurface _surface;
private Canvas _canvas; private Canvas _canvas;
#endregion #endregion
@ -29,8 +30,10 @@ namespace RGB.NET.WPF.Controls
/// </summary> /// </summary>
public RGBSurfaceVisualizer() public RGBSurfaceVisualizer()
{ {
RGBSurface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged; _surface = RGBSurface.Instance;
foreach (IRGBDevice device in RGBSurface.Devices)
_surface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged;
foreach (IRGBDevice device in _surface.Devices)
AddDevice(device); AddDevice(device);
} }
@ -40,8 +43,8 @@ namespace RGB.NET.WPF.Controls
foreach (IRGBDevice device in args.Devices) foreach (IRGBDevice device in args.Devices)
AddDevice(device); AddDevice(device);
_canvas.Width = RGBSurface.SurfaceRectangle.Size.Width; _canvas.Width = _surface.SurfaceRectangle.Size.Width;
_canvas.Height = RGBSurface.SurfaceRectangle.Size.Height; _canvas.Height = _surface.SurfaceRectangle.Size.Height;
} }
#endregion #endregion