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

Moved all methods not directly tied to the surface in an extension and refactored led groups

This commit is contained in:
Darth Affe 2021-03-05 21:43:12 +01:00
parent 47fd3ff203
commit f14e3c801d
8 changed files with 118 additions and 114 deletions

View File

@ -90,9 +90,6 @@ namespace RGB.NET.Core
// Send LEDs to SDK
List<Led> ledsToUpdate = GetLedsToUpdate(flushLeds).ToList();
foreach (Led ledToUpdate in ledsToUpdate)
ledToUpdate.Update();
UpdateLeds(ledsToUpdate);
}

View File

@ -0,0 +1,64 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace RGB.NET.Core
{
public static class SurfaceExtensions
{
#region Methods
public static void Load(this RGBSurface surface, IRGBDeviceProvider deviceProvider, RGBDeviceType loadFilter = RGBDeviceType.All, bool throwExceptions = false)
{
if (!deviceProvider.IsInitialized)
deviceProvider.Initialize(loadFilter, throwExceptions);
surface.Attach(deviceProvider.Devices);
}
public static void Attach(this RGBSurface surface, IEnumerable<IRGBDevice> devices)
{
foreach (IRGBDevice device in devices)
surface.Attach(device);
}
public static void Detach(this RGBSurface surface, IEnumerable<IRGBDevice> devices)
{
foreach (IRGBDevice device in devices)
surface.Detach(device);
}
/// <summary>
/// Gets all devices of a specific type.
/// </summary>
/// <typeparam name="T">The type of devices to get.</typeparam>
/// <returns>A collection of devices with the specified type.</returns>
public static IEnumerable<T> GetDevices<T>(this RGBSurface surface)
where T : class
=> surface.Devices.Where(x => x is T).Cast<T>();
/// <summary>
/// Gets all devices of the specified <see cref="RGBDeviceType"/>.
/// </summary>
/// <param name="deviceType">The <see cref="RGBDeviceType"/> of the devices to get.</param>
/// <returns>A collection of devices matching the specified <see cref="RGBDeviceType"/>.</returns>
public static IEnumerable<IRGBDevice> GetDevices(this RGBSurface surface, RGBDeviceType deviceType)
=> surface.Devices.Where(d => deviceType.HasFlag(d.DeviceInfo.DeviceType));
/// <summary>
/// Automatically aligns all devices to prevent overlaps.
/// </summary>
public static void AlignDevices(this RGBSurface surface)
{
float posX = 0;
foreach (IRGBDevice device in surface.Devices)
{
device.Location += new Point(posX, 0);
posX += device.ActualSize.Width + 1;
}
}
#endregion
}
}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
namespace RGB.NET.Core
{
@ -11,7 +12,8 @@ namespace RGB.NET.Core
{
#region Properties & Fields
public RGBSurface? Surface { get; private set; }
RGBSurface? ILedGroup.Surface { get; set; }
public RGBSurface? Surface => ((ILedGroup)this).Surface;
/// <inheritdoc />
public IBrush? Brush { get; set; }
@ -28,21 +30,26 @@ namespace RGB.NET.Core
/// </summary>
protected AbstractLedGroup(RGBSurface? attachTo)
{
attachTo?.AttachLedGroup(this);
attachTo?.Attach(this);
}
#endregion
#region Methods
/// <inheritdoc />
public abstract IList<Led> GetLeds();
protected abstract IEnumerable<Led> GetLeds();
/// <inheritdoc />
public virtual void OnAttach(RGBSurface surface) => Surface = surface;
public virtual void OnAttach() { }
/// <inheritdoc />
public virtual void OnDetach(RGBSurface surface) => Surface = null;
public virtual void OnDetach() { }
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <inheritdoc />
public IEnumerator<Led> GetEnumerator() => GetLeds().GetEnumerator();
#endregion
}

View File

@ -5,13 +5,14 @@ using System.Collections.Generic;
namespace RGB.NET.Core
{
/// <inheritdoc />
/// <summary>
/// Represents a generic ledgroup.
/// </summary>
public interface ILedGroup : IDecoratable<ILedGroupDecorator>
public interface ILedGroup : IDecoratable<ILedGroupDecorator>, IEnumerable<Led>
{
public RGBSurface? Surface { get; }
RGBSurface? Surface { get; internal set; }
bool IsAttached => Surface != null;
/// <summary>
/// Gets or sets the <see cref="IBrush"/> which should be drawn over this <see cref="ILedGroup"/>.
@ -23,20 +24,14 @@ namespace RGB.NET.Core
/// </summary>
int ZIndex { get; set; }
/// <summary>
/// Gets a list containing all <see cref="Led"/> of this <see cref="ILedGroup"/>.
/// </summary>
/// <returns>The list containing all <see cref="Led"/> of this <see cref="ILedGroup"/>.</returns>
IList<Led> GetLeds();
/// <summary>
/// Called when the <see cref="ILedGroup"/> is attached to the <see cref="RGBSurface"/>.
/// </summary>
void OnAttach(RGBSurface surface);
void OnAttach();
/// <summary>
/// Called when the <see cref="ILedGroup"/> is detached from the <see cref="RGBSurface"/>.
/// </summary>
void OnDetach(RGBSurface surface);
void OnDetach();
}
}

View File

@ -16,11 +16,11 @@ namespace RGB.NET.Core
public static ListLedGroup ToListLedGroup(this ILedGroup ledGroup)
{
// ReSharper disable once InvertIf
if (!(ledGroup is ListLedGroup listLedGroup))
if (ledGroup is not ListLedGroup listLedGroup)
{
if (ledGroup.Surface != null)
ledGroup.Detach(ledGroup.Surface);
listLedGroup = new ListLedGroup(ledGroup.Surface, ledGroup.GetLeds()) { Brush = ledGroup.Brush };
if (ledGroup.IsAttached)
ledGroup.Detach();
listLedGroup = new ListLedGroup(ledGroup.Surface, ledGroup) { Brush = ledGroup.Brush, ZIndex = ledGroup.ZIndex };
}
return listLedGroup;
}
@ -45,13 +45,13 @@ 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 Attach(this ILedGroup ledGroup, RGBSurface surface) => surface.AttachLedGroup(ledGroup);
public static bool Attach(this ILedGroup ledGroup, RGBSurface surface) => surface.Attach(ledGroup);
/// <summary>
/// Detaches the given <see cref="ILedGroup"/> from the <see cref="RGBSurface"/>.
/// </summary>
/// <param name="ledGroup">The <see cref="ILedGroup"/> to attach.</param>
/// <returns><c>true</c> if the <see cref="ILedGroup"/> could be detached; otherwise, <c>false</c>.</returns>
public static bool Detach(this ILedGroup ledGroup, RGBSurface surface) => surface.DetachLedGroup(ledGroup);
public static bool Detach(this ILedGroup ledGroup) => ledGroup.Surface?.Detach(ledGroup) ?? false;
}
}

View File

@ -113,7 +113,7 @@ namespace RGB.NET.Core
public void MergeLeds(ILedGroup groupToMerge)
{
lock (GroupLeds)
foreach (Led led in groupToMerge.GetLeds())
foreach (Led led in groupToMerge)
if (!GroupLeds.Contains(led))
GroupLeds.Add(led);
}
@ -123,7 +123,7 @@ namespace RGB.NET.Core
/// Gets a list containing the <see cref="T:RGB.NET.Core.Led" /> from this group.
/// </summary>
/// <returns>The list containing the <see cref="T:RGB.NET.Core.Led" />.</returns>
public override IList<Led> GetLeds()
protected override IEnumerable<Led> GetLeds()
{
lock (GroupLeds)
return new List<Led>(GroupLeds);

View File

@ -14,20 +14,15 @@ namespace RGB.NET.Core
/// <summary>
/// Represents a RGB-surface containing multiple devices.
/// </summary>
public class RGBSurface : AbstractBindable, IDisposable
public sealed class RGBSurface : AbstractBindable, IDisposable
{
#region Properties & Fields
private Stopwatch _deltaTimeCounter;
private IList<IRGBDevice> _devices = new List<IRGBDevice>();
private IList<IUpdateTrigger> _updateTriggers = new List<IUpdateTrigger>();
// ReSharper disable InconsistentNaming
private readonly LinkedList<ILedGroup> _ledGroups = new();
// ReSharper restore InconsistentNaming
private readonly IList<IRGBDevice> _devices = new List<IRGBDevice>();
private readonly IList<IUpdateTrigger> _updateTriggers = new List<IUpdateTrigger>();
private readonly List<ILedGroup> _ledGroups = new List<ILedGroup>();
/// <summary>
/// Gets a readonly list containing all loaded <see cref="IRGBDevice"/>.
@ -160,7 +155,7 @@ namespace RGB.NET.Core
lock (_ledGroups)
{
// Render brushes
foreach (ILedGroup ledGroup in _ledGroups.OrderBy(x => x.ZIndex))
foreach (ILedGroup ledGroup in _ledGroups)
try { Render(ledGroup); }
catch (Exception ex) { OnException(ex); }
}
@ -170,6 +165,9 @@ namespace RGB.NET.Core
try { device.Update(flushLeds); }
catch (Exception ex) { OnException(ex); }
foreach (Led led in Leds)
led.Update();
OnUpdated();
}
}
@ -204,7 +202,7 @@ namespace RGB.NET.Core
/// <exception cref="ArgumentException">Thrown if the <see cref="IBrush.CalculationMode"/> of the Brush is not valid.</exception>
private void Render(ILedGroup ledGroup)
{
IList<Led> leds = ledGroup.GetLeds().ToList();
IList<Led> leds = ledGroup.ToList();
IBrush? brush = ledGroup.Brush;
if ((brush == null) || !brush.IsEnabled) return;
@ -234,14 +232,16 @@ 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 bool AttachLedGroup(ILedGroup ledGroup)
public bool Attach(ILedGroup ledGroup)
{
lock (_ledGroups)
{
if (_ledGroups.Contains(ledGroup)) return false;
if (ledGroup.Surface != null) return false;
_ledGroups.AddLast(ledGroup);
ledGroup.OnAttach(this);
ledGroup.Surface = this;
_ledGroups.Add(ledGroup);
_ledGroups.Sort((group1, group2) => group1.ZIndex.CompareTo(group2.ZIndex));
ledGroup.OnAttach();
return true;
}
@ -252,29 +252,18 @@ 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 bool DetachLedGroup(ILedGroup ledGroup)
public bool Detach(ILedGroup ledGroup)
{
lock (_ledGroups)
{
LinkedListNode<ILedGroup>? node = _ledGroups.Find(ledGroup);
if (node == null) return false;
_ledGroups.Remove(node);
node.Value.OnDetach(this);
if (!_ledGroups.Remove(ledGroup)) return false;
ledGroup.OnDetach();
ledGroup.Surface = null;
return true;
}
}
public void Attach(IEnumerable<IRGBDevice> devices)
{
lock (_devices)
{
foreach (IRGBDevice device in devices)
Attach(device);
}
}
public void Attach(IRGBDevice device)
{
lock (_devices)
@ -289,15 +278,6 @@ namespace RGB.NET.Core
}
}
public void Detach(IEnumerable<IRGBDevice> devices)
{
lock (_devices)
{
foreach (IRGBDevice device in devices)
Detach(device);
}
}
public void Detach(IRGBDevice device)
{
lock (_devices)
@ -313,19 +293,6 @@ namespace RGB.NET.Core
}
}
/// <summary>
/// Automatically aligns all devices to prevent overlaps.
/// </summary>
public void AlignDevices()
{
float posX = 0;
foreach (IRGBDevice device in Devices)
{
device.Location += new Point(posX, 0);
posX += device.ActualSize.Width + 1;
}
}
// ReSharper restore UnusedMember.Global
private void DeviceOnBoundaryChanged(object? sender, EventArgs args)
@ -347,29 +314,6 @@ namespace RGB.NET.Core
}
}
/// <summary>
/// Gets all devices of a specific type.
/// </summary>
/// <typeparam name="T">The type of devices to get.</typeparam>
/// <returns>A list of devices with the specified type.</returns>
public IList<T> GetDevices<T>()
where T : class
{
lock (_devices)
return new ReadOnlyCollection<T>(_devices.Where(x => x is T).Cast<T>().ToList());
}
/// <summary>
/// Gets all devices of the specified <see cref="RGBDeviceType"/>.
/// </summary>
/// <param name="deviceType">The <see cref="RGBDeviceType"/> of the devices to get.</param>
/// <returns>a list of devices matching the specified <see cref="RGBDeviceType"/>.</returns>
public IList<IRGBDevice> GetDevices(RGBDeviceType deviceType)
{
lock (_devices)
return new ReadOnlyCollection<IRGBDevice>(_devices.Where(d => deviceType.HasFlag(d.DeviceInfo.DeviceType)).ToList());
}
/// <summary>
/// Registers the provided <see cref="IUpdateTrigger"/>.
/// </summary>

View File

@ -93,21 +93,18 @@ namespace RGB.NET.Presets.Groups
#region Methods
/// <inheritdoc />
public override void OnAttach(RGBSurface surface)
public override void OnAttach()
{
base.OnAttach(surface);
if (Surface != null)
Surface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged;
base.OnAttach();
Surface!.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged;
}
/// <inheritdoc />
public override void OnDetach(RGBSurface surface)
public override void OnDetach()
{
if (Surface != null)
Surface.SurfaceLayoutChanged -= RGBSurfaceOnSurfaceLayoutChanged;
Surface!.SurfaceLayoutChanged -= RGBSurfaceOnSurfaceLayoutChanged;
base.OnDetach(surface);
base.OnDetach();
}
private void RGBSurfaceOnSurfaceLayoutChanged(SurfaceLayoutChangedEventArgs args) => InvalidateCache();
@ -117,7 +114,7 @@ namespace RGB.NET.Presets.Groups
/// Gets a list containing all <see cref="T:RGB.NET.Core.Led" /> of this <see cref="T:RGB.NET.Presets.Groups.RectangleLedGroup" />.
/// </summary>
/// <returns>The list containing all <see cref="T:RGB.NET.Core.Led" /> of this <see cref="T:RGB.NET.Presets.Groups.RectangleLedGroup" />.</returns>
public override IList<Led> GetLeds() => _ledCache ??= (Surface?.Leds.Where(led => led.AbsoluteBoundary.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList() ?? new List<Led>());
protected override IEnumerable<Led> GetLeds() => _ledCache ??= (Surface?.Leds.Where(led => led.AbsoluteBoundary.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList() ?? new List<Led>());
private void InvalidateCache() => _ledCache = null;