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)
{
if (autoAttach)
RGBSurface.AttachLedGroup(this);
RGBSurface.Instance.AttachLedGroup(this);
}
#endregion

View File

@ -11,37 +11,42 @@ namespace RGB.NET.Core
/// <summary>
/// Represents a RGB-surface containing multiple devices.
/// </summary>
public static partial class RGBSurface
public partial class RGBSurface : AbstractBindable
{
#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 static IList<IRGBDevice> _devices = new List<IRGBDevice>();
private DateTime _lastUpdate;
private IList<IRGBDeviceProvider> _deviceProvider = new List<IRGBDeviceProvider>();
private IList<IRGBDevice> _devices = new List<IRGBDevice>();
// 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
/// <summary>
/// Gets a readonly list containing all loaded <see cref="IRGBDevice"/>.
/// </summary>
public static IEnumerable<IRGBDevice> Devices => new ReadOnlyCollection<IRGBDevice>(_devices);
public IEnumerable<IRGBDevice> Devices => new ReadOnlyCollection<IRGBDevice>(_devices);
/// <summary>
/// Gets a copy of the <see cref="Rectangle"/> representing this <see cref="RGBSurface"/>.
/// </summary>
public static Rectangle SurfaceRectangle => new Rectangle(_surfaceRectangle);
public Rectangle SurfaceRectangle => new Rectangle(_surfaceRectangle);
/// <summary>
/// Gets a list of all <see cref="Led"/> on this <see cref="RGBSurface"/>.
/// </summary>
public static IEnumerable<Led> Leds => _devices.SelectMany(x => x);
public IEnumerable<Led> Leds => _devices.SelectMany(x => x);
#endregion
@ -50,7 +55,7 @@ namespace RGB.NET.Core
/// <summary>
/// Initializes a new instance of the <see cref="RGBSurface"/> class.
/// </summary>
static RGBSurface()
private RGBSurface()
{
_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.
/// </summary>
/// <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();
@ -88,7 +93,7 @@ namespace RGB.NET.Core
/// Renders a ledgroup.
/// </summary>
/// <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();
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
/// </summary>
/// <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>
public static bool AttachLedGroup(ILedGroup ledGroup)
public bool AttachLedGroup(ILedGroup ledGroup)
{
if (ledGroup == null) return false;
@ -159,7 +164,7 @@ namespace RGB.NET.Core
/// </summary>
/// <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>
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)));

View File

@ -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
/// <summary>
/// Occurs when a catched exception is thrown inside the <see cref="RGBSurface"/>.
/// </summary>
public static event ExceptionEventHandler Exception;
public event ExceptionEventHandler Exception;
/// <summary>
/// Occurs when the <see cref="RGBSurface"/> starts updating.
/// </summary>
public static event UpdatingEventHandler Updating;
public event UpdatingEventHandler Updating;
/// <summary>
/// Occurs when the <see cref="RGBSurface"/> update is done.
/// </summary>
public static event UpdatedEventHandler Updated;
public event UpdatedEventHandler Updated;
/// <summary>
/// Occurs when the layout of this <see cref="RGBSurface"/> changed.
/// </summary>
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.
/// </summary>
/// <param name="ex">The exception previously thrown.</param>
private static void OnException(Exception ex)
private void OnException(Exception ex)
{
try
{
@ -78,7 +78,7 @@ namespace RGB.NET.Core
/// <summary>
/// Handles the needed event-calls before updating.
/// </summary>
private static void OnUpdating()
private void OnUpdating()
{
try
{
@ -92,7 +92,7 @@ namespace RGB.NET.Core
/// <summary>
/// Handles the needed event-calls after an update.
/// </summary>
private static void OnUpdated()
private void OnUpdated()
{
try
{

View File

@ -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 <see cref="IRGBDeviceProvider"/> is able to provide.
/// </summary>
/// <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;
@ -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));

View File

@ -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;
/// <summary>
/// Gets or sets the update-frequency in seconds. (Calculate by using '1.0 / updates per second')
/// </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>
/// Gets or sets the update-mode.
/// </summary>
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.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown if the requested update-mode is not available.</exception>
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)
{

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>
public static bool Attach(this ILedGroup ledGroup)
{
return RGBSurface.AttachLedGroup(ledGroup);
return RGBSurface.Instance.AttachLedGroup(ledGroup);
}
/// <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>
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 />
public override void OnAttach()
{
RGBSurface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged;
RGBSurface.Instance.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged;
}
/// <inheritdoc />
public override void OnDetach()
{
RGBSurface.SurfaceLayoutChanged -= RGBSurfaceOnSurfaceLayoutChanged;
RGBSurface.Instance.SurfaceLayoutChanged -= RGBSurfaceOnSurfaceLayoutChanged;
}
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>
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()

View File

@ -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
/// </summary>
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