mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 03:48:56 +00:00
Profiles - Added profile adaption based on configurable hints
Layers - Added hints UI accesible from profile tree Surface - Added device categories used by profile adaption
This commit is contained in:
1 parent
4865254ab6
commit
57193e739e
34 files changed
+1295
-58
No files matched your search
@@ -0,0 +1,75 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Artemis.Storage.Entities.Profile.AdaptionHints;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a hint that adapts layers to a certain category of devices
|
||||
/// </summary>
|
||||
public class CategoryAdaptionHint : IAdaptionHint
|
||||
{
|
||||
public CategoryAdaptionHint()
|
||||
{
|
||||
}
|
||||
|
||||
internal CategoryAdaptionHint(CategoryAdaptionHintEntity entity)
|
||||
{
|
||||
Category = (DeviceCategory) entity.Category;
|
||||
Skip = entity.Skip;
|
||||
LimitAmount = entity.LimitAmount;
|
||||
Amount = entity.Amount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the category of devices LEDs will be applied to
|
||||
/// </summary>
|
||||
public DeviceCategory Category { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the amount of devices to skip
|
||||
/// </summary>
|
||||
public int Skip { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a boolean indicating whether a limited amount of devices should be used
|
||||
/// </summary>
|
||||
public bool LimitAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the amount of devices to limit to if <see cref="LimitAmount" /> is <see langword="true" />
|
||||
/// </summary>
|
||||
public int Amount { get; set; }
|
||||
|
||||
#region Implementation of IAdaptionHint
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Apply(Layer layer, List<ArtemisDevice> devices)
|
||||
{
|
||||
IEnumerable<ArtemisDevice> matches = devices
|
||||
.Where(d => d.Categories.Contains(Category))
|
||||
.OrderBy(d => d.Rectangle.Top)
|
||||
.ThenBy(d => d.Rectangle.Left)
|
||||
.Skip(Skip);
|
||||
if (LimitAmount)
|
||||
matches = matches.Take(Amount);
|
||||
|
||||
foreach (ArtemisDevice artemisDevice in matches)
|
||||
layer.AddLeds(artemisDevice.Leds);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IAdaptionHintEntity GetEntry()
|
||||
{
|
||||
return new CategoryAdaptionHintEntity {Amount = Amount, LimitAmount = LimitAmount, Category = (int) Category, Skip = Skip};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Category adaption - {nameof(Category)}: {Category}, {nameof(Skip)}: {Skip}, {nameof(LimitAmount)}: {LimitAmount}, {nameof(Amount)}: {Amount}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Artemis.Storage.Entities.Profile.AdaptionHints;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a hint that adapts layers to a certain type of devices
|
||||
/// </summary>
|
||||
public class DeviceAdaptionHint : IAdaptionHint
|
||||
{
|
||||
public DeviceAdaptionHint()
|
||||
{
|
||||
}
|
||||
|
||||
internal DeviceAdaptionHint(DeviceAdaptionHintEntity entity)
|
||||
{
|
||||
DeviceType = (RGBDeviceType) entity.DeviceType;
|
||||
Skip = entity.Skip;
|
||||
LimitAmount = entity.LimitAmount;
|
||||
Amount = entity.Amount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the type of devices LEDs will be applied to
|
||||
/// </summary>
|
||||
public RGBDeviceType DeviceType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the amount of devices to skip
|
||||
/// </summary>
|
||||
public int Skip { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a boolean indicating whether a limited amount of devices should be used
|
||||
/// </summary>
|
||||
public bool LimitAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the amount of devices to limit to if <see cref="LimitAmount" /> is <see langword="true" />
|
||||
/// </summary>
|
||||
public int Amount { get; set; }
|
||||
|
||||
#region Implementation of IAdaptionHint
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Apply(Layer layer, List<ArtemisDevice> devices)
|
||||
{
|
||||
IEnumerable<ArtemisDevice> matches = devices
|
||||
.Where(d => d.RgbDevice.DeviceInfo.DeviceType == DeviceType)
|
||||
.OrderBy(d => d.Rectangle.Top)
|
||||
.ThenBy(d => d.Rectangle.Left)
|
||||
.Skip(Skip);
|
||||
if (LimitAmount)
|
||||
matches = matches.Take(Amount);
|
||||
|
||||
foreach (ArtemisDevice artemisDevice in matches)
|
||||
layer.AddLeds(artemisDevice.Leds);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IAdaptionHintEntity GetEntry()
|
||||
{
|
||||
return new DeviceAdaptionHintEntity {Amount = Amount, LimitAmount = LimitAmount, DeviceType = (int) DeviceType, Skip = Skip};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Device adaption - {nameof(DeviceType)}: {DeviceType}, {nameof(Skip)}: {Skip}, {nameof(LimitAmount)}: {LimitAmount}, {nameof(Amount)}: {Amount}";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.Collections.Generic;
|
||||
using Artemis.Storage.Entities.Profile.AdaptionHints;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents an adaption hint that's used to adapt a layer to a set of devices
|
||||
/// </summary>
|
||||
public interface IAdaptionHint
|
||||
{
|
||||
/// <summary>
|
||||
/// Applies the adaptive action to the provided layer
|
||||
/// </summary>
|
||||
/// <param name="layer">The layer to adapt</param>
|
||||
/// <param name="devices">The devices to adapt the layer for</param>
|
||||
void Apply(Layer layer, List<ArtemisDevice> devices);
|
||||
|
||||
/// <summary>
|
||||
/// Returns an adaption hint entry for this adaption hint used for persistent storage
|
||||
/// </summary>
|
||||
IAdaptionHintEntity GetEntry();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Artemis.Storage.Entities.Profile.AdaptionHints;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a hint that adapts layers to a certain region of keyboards
|
||||
/// </summary>
|
||||
public class KeyboardSectionAdaptionHint : IAdaptionHint
|
||||
{
|
||||
private static readonly Dictionary<KeyboardSection, List<LedId>> RegionLedIds = new()
|
||||
{
|
||||
{KeyboardSection.MacroKeys, Enum.GetValues<LedId>().Where(l => l >= LedId.Keyboard_Programmable1 && l <= LedId.Keyboard_Programmable32).ToList()},
|
||||
{KeyboardSection.LedStrips, Enum.GetValues<LedId>().Where(l => l >= LedId.LedStripe1 && l <= LedId.LedStripe128).ToList()},
|
||||
{KeyboardSection.Extra, Enum.GetValues<LedId>().Where(l => l >= LedId.Keyboard_Custom1 && l <= LedId.Keyboard_Custom64).ToList()}
|
||||
};
|
||||
|
||||
public KeyboardSectionAdaptionHint()
|
||||
{
|
||||
}
|
||||
|
||||
internal KeyboardSectionAdaptionHint(KeyboardSectionAdaptionHintEntity entity)
|
||||
{
|
||||
Section = (KeyboardSection) entity.Section;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the section this hint will apply LEDs to
|
||||
/// </summary>
|
||||
public KeyboardSection Section { get; set; }
|
||||
|
||||
#region Implementation of IAdaptionHint
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Apply(Layer layer, List<ArtemisDevice> devices)
|
||||
{
|
||||
// Only keyboards should have the LEDs we care about
|
||||
foreach (ArtemisDevice keyboard in devices.Where(d => d.RgbDevice.DeviceInfo.DeviceType == RGBDeviceType.Keyboard))
|
||||
{
|
||||
List<LedId> ledIds = RegionLedIds[Section];
|
||||
layer.AddLeds(keyboard.Leds.Where(l => ledIds.Contains(l.RgbLed.Id)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IAdaptionHintEntity GetEntry()
|
||||
{
|
||||
return new KeyboardSectionAdaptionHintEntity {Section = (int) Section};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Keyboard section adaption - {nameof(Section)}: {Section}";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a section of LEDs on a keyboard
|
||||
/// </summary>
|
||||
public enum KeyboardSection
|
||||
{
|
||||
/// <summary>
|
||||
/// A region containing the macro keys of a keyboard
|
||||
/// </summary>
|
||||
MacroKeys,
|
||||
|
||||
/// <summary>
|
||||
/// A region containing the LED strips of a keyboard
|
||||
/// </summary>
|
||||
LedStrips,
|
||||
|
||||
/// <summary>
|
||||
/// A region containing extra non-standard LEDs of a keyboard
|
||||
/// </summary>
|
||||
Extra
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using Artemis.Core.LayerBrushes;
|
||||
using Artemis.Core.LayerEffects;
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
using Artemis.Storage.Entities.Profile.Abstract;
|
||||
using RGB.NET.Core;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Artemis.Core
|
||||
@@ -42,6 +43,7 @@ namespace Artemis.Core
|
||||
|
||||
_leds = new List<ArtemisLed>();
|
||||
|
||||
Adapter = new LayerAdapter(this);
|
||||
Initialize();
|
||||
Parent.AddChild(this, 0);
|
||||
}
|
||||
@@ -65,6 +67,7 @@ namespace Artemis.Core
|
||||
_leds = new List<ArtemisLed>();
|
||||
|
||||
Load();
|
||||
Adapter = new LayerAdapter(this);
|
||||
Initialize();
|
||||
}
|
||||
|
||||
@@ -121,6 +124,11 @@ namespace Artemis.Core
|
||||
/// </summary>
|
||||
public LayerEntity LayerEntity { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the layer adapter that can be used to adapt this layer to a different set of devices
|
||||
/// </summary>
|
||||
public LayerAdapter Adapter { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool ShouldBeEnabled => !Suspended && DisplayConditionMet;
|
||||
|
||||
@@ -147,7 +155,15 @@ namespace Artemis.Core
|
||||
return $"[Layer] {nameof(Name)}: {Name}, {nameof(Order)}: {Order}";
|
||||
}
|
||||
|
||||
#region IDisposable
|
||||
/// <summary>
|
||||
/// Occurs when a property affecting the rendering properties of this layer has been updated
|
||||
/// </summary>
|
||||
public event EventHandler? RenderPropertiesUpdated;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when the layer brush of this layer has been updated
|
||||
/// </summary>
|
||||
public event EventHandler? LayerBrushUpdated;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Dispose(bool disposing)
|
||||
@@ -162,7 +178,10 @@ namespace Artemis.Core
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#endregion
|
||||
internal void OnLayerBrushUpdated()
|
||||
{
|
||||
LayerBrushUpdated?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
@@ -190,6 +209,28 @@ namespace Artemis.Core
|
||||
Reset();
|
||||
}
|
||||
|
||||
private void LayerBrushStoreOnLayerBrushRemoved(object? sender, LayerBrushStoreEvent e)
|
||||
{
|
||||
if (LayerBrush?.Descriptor == e.Registration.LayerBrushDescriptor)
|
||||
DeactivateLayerBrush();
|
||||
}
|
||||
|
||||
private void LayerBrushStoreOnLayerBrushAdded(object? sender, LayerBrushStoreEvent e)
|
||||
{
|
||||
if (LayerBrush != null || !General.PropertiesInitialized)
|
||||
return;
|
||||
|
||||
LayerBrushReference? current = General.BrushReference.CurrentValue;
|
||||
if (e.Registration.PluginFeature.Id == current?.LayerBrushProviderId &&
|
||||
e.Registration.LayerBrushDescriptor.LayerBrushType.Name == current.BrushType)
|
||||
ActivateLayerBrush();
|
||||
}
|
||||
|
||||
private void OnRenderPropertiesUpdated()
|
||||
{
|
||||
RenderPropertiesUpdated?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
#region Storage
|
||||
|
||||
internal override void Load()
|
||||
@@ -229,7 +270,8 @@ namespace Artemis.Core
|
||||
LedEntity ledEntity = new()
|
||||
{
|
||||
DeviceIdentifier = artemisLed.Device.Identifier,
|
||||
LedName = artemisLed.RgbLed.Id.ToString()
|
||||
LedName = artemisLed.RgbLed.Id.ToString(),
|
||||
PhysicalLayout = artemisLed.Device.RgbDevice.DeviceInfo.DeviceType == RGBDeviceType.Keyboard ? (int) artemisLed.Device.PhysicalLayout : null
|
||||
};
|
||||
LayerEntity.Leds.Add(ledEntity);
|
||||
}
|
||||
@@ -575,7 +617,7 @@ namespace Artemis.Core
|
||||
if (Disposed)
|
||||
throw new ObjectDisposedException("Layer");
|
||||
|
||||
_leds.AddRange(leds);
|
||||
_leds.AddRange(leds.Except(_leds));
|
||||
CalculateRenderProperties();
|
||||
}
|
||||
|
||||
@@ -692,51 +734,6 @@ namespace Artemis.Core
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event handlers
|
||||
|
||||
private void LayerBrushStoreOnLayerBrushRemoved(object? sender, LayerBrushStoreEvent e)
|
||||
{
|
||||
if (LayerBrush?.Descriptor == e.Registration.LayerBrushDescriptor)
|
||||
DeactivateLayerBrush();
|
||||
}
|
||||
|
||||
private void LayerBrushStoreOnLayerBrushAdded(object? sender, LayerBrushStoreEvent e)
|
||||
{
|
||||
if (LayerBrush != null || !General.PropertiesInitialized)
|
||||
return;
|
||||
|
||||
LayerBrushReference? current = General.BrushReference.CurrentValue;
|
||||
if (e.Registration.PluginFeature.Id == current?.LayerBrushProviderId &&
|
||||
e.Registration.LayerBrushDescriptor.LayerBrushType.Name == current.BrushType)
|
||||
ActivateLayerBrush();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a property affecting the rendering properties of this layer has been updated
|
||||
/// </summary>
|
||||
public event EventHandler? RenderPropertiesUpdated;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when the layer brush of this layer has been updated
|
||||
/// </summary>
|
||||
public event EventHandler? LayerBrushUpdated;
|
||||
|
||||
private void OnRenderPropertiesUpdated()
|
||||
{
|
||||
RenderPropertiesUpdated?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
internal void OnLayerBrushUpdated()
|
||||
{
|
||||
LayerBrushUpdated?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Artemis.Storage.Entities.Profile;
|
||||
using Artemis.Storage.Entities.Profile.AdaptionHints;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace Artemis.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents an adapter that adapts a layer to a certain set of devices using <see cref="IAdaptionHint" />s
|
||||
/// </summary>
|
||||
public class LayerAdapter : IStorageModel
|
||||
{
|
||||
internal LayerAdapter(Layer layer)
|
||||
{
|
||||
Layer = layer;
|
||||
AdaptionHints = new List<IAdaptionHint>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the layer this adapter can adapt
|
||||
/// </summary>
|
||||
public Layer Layer { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a list containing the adaption hints used by this adapter
|
||||
/// </summary>
|
||||
public List<IAdaptionHint> AdaptionHints { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Modifies the layer, adapting it to the provided <paramref name="devices" />
|
||||
/// </summary>
|
||||
/// <param name="devices">The devices to adapt the layer to</param>
|
||||
public void Adapt(List<ArtemisDevice> devices)
|
||||
{
|
||||
// Use adaption hints if provided
|
||||
if (AdaptionHints.Any())
|
||||
{
|
||||
foreach (IAdaptionHint adaptionHint in AdaptionHints)
|
||||
adaptionHint.Apply(Layer, devices);
|
||||
}
|
||||
// If there are no hints, try to find matching LEDs anyway
|
||||
else
|
||||
{
|
||||
List<ArtemisLed> availableLeds = devices.SelectMany(d => d.Leds).ToList();
|
||||
List<ArtemisLed> usedLeds = new();
|
||||
|
||||
foreach (LedEntity ledEntity in Layer.LayerEntity.Leds)
|
||||
{
|
||||
// TODO: If this is a keyboard LED and the layouts don't match, convert it before looking for it on the devices
|
||||
|
||||
LedId ledId = Enum.Parse<LedId>(ledEntity.LedName);
|
||||
ArtemisLed? led = availableLeds.FirstOrDefault(l => l.RgbLed.Id == ledId);
|
||||
|
||||
if (led != null)
|
||||
{
|
||||
availableLeds.Remove(led);
|
||||
usedLeds.Add(led);
|
||||
}
|
||||
}
|
||||
|
||||
Layer.AddLeds(usedLeds);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Automatically determine hints for this layer
|
||||
/// </summary>
|
||||
public List<IAdaptionHint> DetermineHints(IEnumerable<ArtemisDevice> devices)
|
||||
{
|
||||
List<IAdaptionHint> newHints = new();
|
||||
// Any fully covered device will add a device adaption hint for that type
|
||||
foreach (IGrouping<ArtemisDevice, ArtemisLed> deviceLeds in Layer.Leds.GroupBy(l => l.Device))
|
||||
{
|
||||
ArtemisDevice device = deviceLeds.Key;
|
||||
// If there is already an adaption hint for this type, don't add another
|
||||
if (AdaptionHints.Any(h => h is DeviceAdaptionHint d && d.DeviceType == device.RgbDevice.DeviceInfo.DeviceType))
|
||||
continue;
|
||||
if (DoesLayerCoverDevice(device))
|
||||
{
|
||||
DeviceAdaptionHint hint = new() {DeviceType = device.RgbDevice.DeviceInfo.DeviceType};
|
||||
AdaptionHints.Add(hint);
|
||||
newHints.Add(hint);
|
||||
}
|
||||
}
|
||||
|
||||
// Any fully covered category will add a category adaption hint for its category
|
||||
foreach (DeviceCategory deviceCategory in Enum.GetValues<DeviceCategory>())
|
||||
{
|
||||
if (AdaptionHints.Any(h => h is CategoryAdaptionHint c && c.Category == deviceCategory))
|
||||
continue;
|
||||
|
||||
List<ArtemisDevice> categoryDevices = devices.Where(d => d.Categories.Contains(deviceCategory)).ToList();
|
||||
if (categoryDevices.Any() && categoryDevices.All(DoesLayerCoverDevice))
|
||||
{
|
||||
CategoryAdaptionHint hint = new() {Category = deviceCategory};
|
||||
AdaptionHints.Add(hint);
|
||||
newHints.Add(hint);
|
||||
}
|
||||
}
|
||||
|
||||
return newHints;
|
||||
}
|
||||
|
||||
private bool DoesLayerCoverDevice(ArtemisDevice device)
|
||||
{
|
||||
return device.Leds.All(l => Layer.Leds.Contains(l));
|
||||
}
|
||||
|
||||
#region Implementation of IStorageModel
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Load()
|
||||
{
|
||||
AdaptionHints.Clear();
|
||||
// Kind of meh.
|
||||
// This leaves the adapter responsible for finding the right hint for the right entity, but it's gotta be done somewhere..
|
||||
foreach (IAdaptionHintEntity hintEntity in Layer.LayerEntity.AdaptionHints)
|
||||
switch (hintEntity)
|
||||
{
|
||||
case DeviceAdaptionHintEntity entity:
|
||||
AdaptionHints.Add(new DeviceAdaptionHint(entity));
|
||||
break;
|
||||
case CategoryAdaptionHintEntity entity:
|
||||
AdaptionHints.Add(new CategoryAdaptionHint(entity));
|
||||
break;
|
||||
case KeyboardSectionAdaptionHintEntity entity:
|
||||
AdaptionHints.Add(new KeyboardSectionAdaptionHint(entity));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Save()
|
||||
{
|
||||
Layer.LayerEntity.AdaptionHints.Clear();
|
||||
foreach (IAdaptionHint adaptionHint in AdaptionHints)
|
||||
Layer.LayerEntity.AdaptionHints.Add(adaptionHint.GetEntry());
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user