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

Added some basic groups

This commit is contained in:
Darth Affe 2017-01-26 20:15:09 +01:00
parent 3331496d59
commit 297a4a4af9
6 changed files with 475 additions and 0 deletions

View File

@ -0,0 +1,79 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using RGB.NET.Core;
namespace RGB.NET.Groups
{
/// <summary>
/// Offers some extensions and helper-methods for <see cref="ILedGroup"/> related things.
/// </summary>
public static class LedGroupExtension
{
/// <summary>
/// Converts the given <see cref="ILedGroup" /> to a <see cref="ListLedGroup" />.
/// </summary>
/// <param name="ledGroup">The <see cref="ILedGroup" /> to convert.</param>
/// <returns>The converted <see cref="ListLedGroup" />.</returns>
public static ListLedGroup ToListLedGroup(this ILedGroup ledGroup)
{
ListLedGroup listLedGroup = ledGroup as ListLedGroup;
// ReSharper disable once InvertIf
if (listLedGroup == null)
{
bool wasAttached = ledGroup.Detach();
listLedGroup = new ListLedGroup(wasAttached, ledGroup.GetLeds()) { Brush = ledGroup.Brush };
}
return listLedGroup;
}
/// <summary>
/// Returns a new <see cref="ListLedGroup" /> which contains all <see cref="Led"/> from the given <see cref="ILedGroup"/> excluding the specified ones.
/// </summary>
/// <param name="ledGroup">The base <see cref="ILedGroup"/>.</param>
/// <param name="ledIds">The ids of the <see cref="Led"/> to exclude.</param>
/// <returns>The new <see cref="ListLedGroup" />.</returns>
public static ListLedGroup Exclude(this ILedGroup ledGroup, params ILedId[] ledIds)
{
ListLedGroup listLedGroup = ledGroup.ToListLedGroup();
foreach (ILedId ledId in ledIds)
listLedGroup.RemoveLed(ledId);
return listLedGroup;
}
/// <summary>
/// Returns a new <see cref="ListLedGroup" /> which contains all <see cref="Led"/> from the given <see cref="ILedGroup"/> excluding the specified ones.
/// </summary>
/// <param name="ledGroup">The base <see cref="ILedGroup"/>.</param>
/// <param name="ledIds">The <see cref="Led"/> to exclude.</param>
/// <returns>The new <see cref="ListLedGroup" />.</returns>
public static ListLedGroup Exclude(this ILedGroup ledGroup, params Led[] ledIds)
{
ListLedGroup listLedGroup = ledGroup.ToListLedGroup();
foreach (Led led in ledIds)
listLedGroup.RemoveLed(led);
return listLedGroup;
}
// ReSharper disable once UnusedMethodReturnValue.Global
/// <summary>
/// Attaches the given <see cref="ILedGroup"/> to 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 attached; otherwise, <c>false</c>.</returns>
public static bool Attach(this ILedGroup ledGroup)
{
return RGBSurface.AttachLedGroup(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)
{
return RGBSurface.DetachLedGroup(ledGroup);
}
}
}

View File

@ -0,0 +1,244 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using RGB.NET.Core;
namespace RGB.NET.Groups
{
/// <summary>
/// Represents a ledgroup containing arbitrary <see cref="Led"/>.
/// </summary>
public class ListLedGroup : AbstractLedGroup
{
#region Properties & Fields
/// <inheritdoc />
protected override ILedGroup EffectTarget => this;
/// <summary>
/// Gets the list containing the <see cref="Led"/> of this <see cref="ListLedGroup"/>.
/// </summary>
protected IList<Led> GroupLeds { get; } = new List<Led>();
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="autoAttach">Specifies whether this <see cref="ListLedGroup"/> should be automatically attached or not.</param>
public ListLedGroup(bool autoAttach = true)
: base(autoAttach)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="leds">The initial <see cref="Led"/> of this <see cref="ListLedGroup"/>.</param>
public ListLedGroup(params Led[] leds)
: this(true, leds)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="leds">The initial <see cref="Led"/> of this <see cref="ListLedGroup"/>.</param>
public ListLedGroup(IEnumerable<Led> leds)
: this(true, leds)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="autoAttach">Specifies whether this <see cref="ListLedGroup"/> should be automatically attached or not.</param>
/// <param name="leds">The initial <see cref="Led"/> of this <see cref="ListLedGroup"/>.</param>
public ListLedGroup(bool autoAttach, IEnumerable<Led> leds)
: base(autoAttach)
{
AddLeds(leds);
}
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="autoAttach">Specifies whether this <see cref="ListLedGroup"/> should be automatically attached or not.</param>
/// <param name="leds">The initial <see cref="Led"/> of this <see cref="ListLedGroup"/>.</param>
public ListLedGroup(bool autoAttach, params Led[] leds)
: base(autoAttach)
{
AddLeds(leds);
}
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="leds">The IDs of the initial <see cref="Led"/> of this <see cref="ListLedGroup"/>.</param>
public ListLedGroup(params ILedId[] leds)
: this(true, leds)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="leds">The IDs of the initial <see cref="Led"/> of this <see cref="ListLedGroup"/>.</param>
public ListLedGroup(IEnumerable<ILedId> leds)
: this(true, leds)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="autoAttach">Specifies whether this <see cref="ListLedGroup"/> should be automatically attached or not.</param>
/// <param name="leds">The IDs of the initial <see cref="Led"/> of this <see cref="ListLedGroup"/>.</param>
public ListLedGroup(bool autoAttach, params ILedId[] leds)
: base(autoAttach)
{
AddLeds(leds);
}
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="autoAttach">Specifies whether this <see cref="ListLedGroup"/> should be automatically attached or not.</param>
/// <param name="leds">The IDs of the initial <see cref="Led"/> of this <see cref="ListLedGroup"/>.</param>
public ListLedGroup(bool autoAttach, IEnumerable<ILedId> leds)
: base(autoAttach)
{
AddLeds(leds);
}
#endregion
#region Methods
/// <summary>
/// Adds the given LED(s) to this <see cref="ListLedGroup"/>.
/// </summary>
/// <param name="leds">The LED(s) to add.</param>
public void AddLed(params Led[] leds)
{
AddLeds(leds);
}
/// <summary>
/// Adds the given LED(s) to this <see cref="ListLedGroup"/>.
/// </summary>
/// <param name="ledIds">The ID(s) of the LED(s) to add.</param>
public void AddLed(params ILedId[] ledIds)
{
AddLeds(ledIds);
}
/// <summary>
/// Adds the given <see cref="Led"/> to this <see cref="ListLedGroup"/>.
/// </summary>
/// <param name="leds">The <see cref="Led"/> to add.</param>
public void AddLeds(IEnumerable<Led> leds)
{
if (leds == null) return;
foreach (Led led in leds)
if ((led != null) && !ContainsLed(led))
GroupLeds.Add(led);
}
/// <summary>
/// Adds the given <see cref="Led"/> to this <see cref="ListLedGroup"/>.
/// </summary>
/// <param name="ledIds">The IDs of the <see cref="Led"/> to add.</param>
public void AddLeds(IEnumerable<ILedId> ledIds)
{
if (ledIds == null) return;
foreach (ILedId ledId in ledIds)
AddLed(ledId.Device[ledId]);
}
/// <summary>
/// Removes the given LED(s) from this <see cref="ListLedGroup"/>.
/// </summary>
/// <param name="leds">The LED(s) to remove.</param>
public void RemoveLed(params Led[] leds)
{
RemoveLeds(leds);
}
/// <summary>
/// Removes the given LED(s) from this <see cref="ListLedGroup"/>.
/// </summary>
/// <param name="ledIds">The ID(s) of the LED(s) to remove.</param>
public void RemoveLed(params ILedId[] ledIds)
{
RemoveLeds(ledIds);
}
/// <summary>
/// Removes the given <see cref="Led"/> from this <see cref="ListLedGroup"/>.
/// </summary>
/// <param name="leds">The <see cref="Led"/> to remove.</param>
public void RemoveLeds(IEnumerable<Led> leds)
{
if (leds == null) return;
foreach (Led led in leds)
if (led != null)
GroupLeds.Remove(led);
}
/// <summary>
/// Removes the given <see cref="Led"/> from this <see cref="ListLedGroup"/>.
/// </summary>
/// <param name="ledIds">The IDs of the <see cref="Led"/> to remove.</param>
public void RemoveLeds(IEnumerable<ILedId> ledIds)
{
if (ledIds == null) return;
foreach (ILedId ledId in ledIds)
RemoveLed(ledId.Device[ledId]);
}
/// <summary>
/// Checks if a given LED is contained by this ledgroup.
/// </summary>
/// <param name="led">The LED which should be checked.</param>
/// <returns><c>true</c> if the LED is contained by this ledgroup; otherwise, <c>false</c>.</returns>
public bool ContainsLed(Led led)
{
return (led != null) && GroupLeds.Contains(led);
}
/// <summary>
/// Checks if a given LED is contained by this ledgroup.
/// </summary>
/// <param name="ledId">The ID of the LED which should be checked.</param>
/// <returns><c>true</c> if the LED is contained by this ledgroup; otherwise, <c>false</c>.</returns>
public bool ContainsLed(ILedId ledId)
{
return ContainsLed(ledId.Device[ledId]);
}
/// <summary>
/// Merges the <see cref="Led"/> from the given ledgroup in this ledgroup.
/// </summary>
/// <param name="groupToMerge">The ledgroup to merge.</param>
public void MergeLeds(ILedGroup groupToMerge)
{
foreach (Led led in groupToMerge.GetLeds())
if (!GroupLeds.Contains(led))
GroupLeds.Add(led);
}
/// <summary>
/// Gets a list containing the <see cref="Led"/> from this group.
/// </summary>
/// <returns>The list containing the <see cref="Led"/>.</returns>
public override IEnumerable<Led> GetLeds()
{
return GroupLeds;
}
#endregion
}
}

View File

@ -0,0 +1,135 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using System.Linq;
using RGB.NET.Core;
namespace RGB.NET.Groups
{
/// <summary>
/// Represents a <see cref="RectangleLedGroup"/> containing <see cref="Led"/> which physically lay inside a <see cref="Core.Rectangle"/>.
/// </summary>
public class RectangleLedGroup : AbstractLedGroup
{
#region Properties & Fields
private IList<Led> _ledCache;
private Rectangle _rectangle;
/// <summary>
/// Gets or sets the <see cref="Core.Rectangle"/> the <see cref="Led"/> should be taken from.
/// </summary>
public Rectangle Rectangle
{
get { return _rectangle; }
set
{
_rectangle = value;
InvalidateCache();
}
}
private double _minOverlayPercentage;
/// <summary>
/// Gets or sets the minimal percentage overlay a <see cref="Led"/> must have with the <see cref="Core.Rectangle" /> to be taken into the <see cref="RectangleLedGroup"/>.
/// </summary>
public double MinOverlayPercentage
{
get { return _minOverlayPercentage; }
set
{
_minOverlayPercentage = value;
InvalidateCache();
}
}
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RectangleLedGroup"/> class.
/// </summary>
/// <param name="fromLed">They ID of the first <see cref="Led"/> to calculate the <see cref="Core.Rectangle"/> of this <see cref="RectangleLedGroup"/> from.</param>
/// <param name="toLed">They ID of the second <see cref="Led"/> to calculate the <see cref="Core.Rectangle"/> of this <see cref="RectangleLedGroup"/> from.</param>
/// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a <see cref="Led"/> must have with the <see cref="Rectangle" /> to be taken into the <see cref="RectangleLedGroup"/>. (default: 0.5)</param>
/// <param name="autoAttach">(optional) Specifies whether this <see cref="RectangleLedGroup"/> should be automatically attached or not. (default: true)</param>
public RectangleLedGroup(ILedId fromLed, ILedId toLed, double minOverlayPercentage = 0.5, bool autoAttach = true)
: this(fromLed.Device[fromLed], toLed.Device[toLed], minOverlayPercentage, autoAttach)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="RectangleLedGroup"/> class.
/// </summary>
/// <param name="fromLed">They first <see cref="Led"/> to calculate the <see cref="Core.Rectangle"/> of this <see cref="RectangleLedGroup"/> from.</param>
/// <param name="toLed">They second <see cref="Led"/> to calculate the <see cref="Core.Rectangle"/> of this <see cref="RectangleLedGroup"/> from.</param>
/// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a <see cref="Led"/> must have with the <see cref="Rectangle" /> to be taken into the <see cref="RectangleLedGroup"/>. (default: 0.5)</param>
/// <param name="autoAttach">(optional) Specifies whether this <see cref="RectangleLedGroup"/> should be automatically attached or not. (default: true)</param>
public RectangleLedGroup(Led fromLed, Led toLed, double minOverlayPercentage = 0.5, bool autoAttach = true)
: this(new Rectangle(fromLed.LedRectangle, toLed.LedRectangle), minOverlayPercentage, autoAttach)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="RectangleLedGroup"/> class.
/// </summary>
/// <param name="fromPoint">They first point to calculate the <see cref="Core.Rectangle"/> of this <see cref="RectangleLedGroup"/> from.</param>
/// <param name="toPoint">They second point to calculate the <see cref="Core.Rectangle"/> of this <see cref="RectangleLedGroup"/> from.</param>
/// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a <see cref="Led"/> must have with the <see cref="Rectangle" /> to be taken into the <see cref="RectangleLedGroup"/>. (default: 0.5)</param>
/// <param name="autoAttach">(optional) Specifies whether this <see cref="RectangleLedGroup"/> should be automatically attached or not. (default: true)</param>
public RectangleLedGroup(Point fromPoint, Point toPoint, double minOverlayPercentage = 0.5, bool autoAttach = true)
: this(new Rectangle(fromPoint, toPoint), minOverlayPercentage, autoAttach)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="RectangleLedGroup"/> class.
/// </summary>
/// <param name="rectangle">The <see cref="Core.Rectangle"/> of this <see cref="RectangleLedGroup"/>.</param>
/// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a <see cref="Led"/> must have with the <see cref="Rectangle" /> to be taken into the <see cref="RectangleLedGroup"/>. (default: 0.5)</param>
/// <param name="autoAttach">(optional) Specifies whether this <see cref="RectangleLedGroup"/> should be automatically attached or not. (default: true)</param>
public RectangleLedGroup(Rectangle rectangle, double minOverlayPercentage = 0.5, bool autoAttach = true)
: base(autoAttach)
{
this.Rectangle = rectangle;
this.MinOverlayPercentage = minOverlayPercentage;
}
#endregion
#region Methods
/// <inheritdoc />
public override void OnAttach()
{
RGBSurface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged;
}
/// <inheritdoc />
public override void OnDetach()
{
RGBSurface.SurfaceLayoutChanged -= RGBSurfaceOnSurfaceLayoutChanged;
}
private void RGBSurfaceOnSurfaceLayoutChanged(SurfaceLayoutChangedEventArgs args)
{
InvalidateCache();
}
/// <summary>
/// Gets a list containing all <see cref="Led"/> of this <see cref="RectangleLedGroup"/>.
/// </summary>
/// <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());
}
private void InvalidateCache()
{
_ledCache = null;
}
#endregion
}
}

View File

@ -31,6 +31,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="RGB.NET.Core, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\RGB.NET.Core.1.0.0\lib\net45\RGB.NET.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@ -41,8 +45,14 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Extensions\LedGroupExtension.cs" />
<Compile Include="Groups\ListLedGroup.cs" />
<Compile Include="Groups\RectangleLedGroup.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -0,0 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=extensions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=groups/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="RGB.NET.Core" version="1.0.0" targetFramework="net45" />
</packages>