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:
Robert Beekman authored and Robert committed 2021-05-13 19:57:38 +02:00
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
}
}
+46 -49
View File
@@ -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
}
}
@@ -36,10 +36,12 @@ namespace Artemis.Core
InputIdentifiers = new List<ArtemisDeviceInputIdentifier>();
InputMappings = new Dictionary<ArtemisLed, ArtemisLed>();
Categories = new HashSet<DeviceCategory>();
UpdateLeds();
ApplyKeyboardLayout();
ApplyToEntity();
ApplyDefaultCategories();
CalculateRenderProperties();
}
@@ -52,6 +54,7 @@ namespace Artemis.Core
InputIdentifiers = new List<ArtemisDeviceInputIdentifier>();
InputMappings = new Dictionary<ArtemisLed, ArtemisLed>();
Categories = new HashSet<DeviceCategory>();
foreach (DeviceInputIdentifierEntity identifierEntity in DeviceEntity.InputIdentifiers)
InputIdentifiers.Add(new ArtemisDeviceInputIdentifier(identifierEntity.InputProvider, identifierEntity.Identifier));
@@ -114,6 +117,11 @@ namespace Artemis.Core
/// </summary>
public Dictionary<ArtemisLed, ArtemisLed> InputMappings { get; }
/// <summary>
/// Gets a list containing the categories of this device
/// </summary>
public HashSet<DeviceCategory> Categories { get; }
/// <summary>
/// Gets or sets the X-position of the device
/// </summary>
@@ -383,6 +391,46 @@ namespace Artemis.Core
OnDeviceUpdated();
}
/// <summary>
/// Applies the default categories for this device to the <see cref="Categories"/> list
/// </summary>
public void ApplyDefaultCategories()
{
switch (RgbDevice.DeviceInfo.DeviceType)
{
case RGBDeviceType.Keyboard:
case RGBDeviceType.Mouse:
case RGBDeviceType.Headset:
case RGBDeviceType.Mousepad:
case RGBDeviceType.HeadsetStand:
case RGBDeviceType.Keypad:
if (!Categories.Contains(DeviceCategory.Peripherals))
Categories.Add(DeviceCategory.Peripherals);
break;
case RGBDeviceType.Mainboard:
case RGBDeviceType.GraphicsCard:
case RGBDeviceType.DRAM:
case RGBDeviceType.Fan:
case RGBDeviceType.LedStripe:
case RGBDeviceType.Cooler:
if (!Categories.Contains(DeviceCategory.Case))
Categories.Add(DeviceCategory.Case);
break;
case RGBDeviceType.Speaker:
if (!Categories.Contains(DeviceCategory.Desk))
Categories.Add(DeviceCategory.Desk);
break;
case RGBDeviceType.Monitor:
if (!Categories.Contains(DeviceCategory.Monitor))
Categories.Add(DeviceCategory.Monitor);
break;
case RGBDeviceType.LedMatrix:
if (!Categories.Contains(DeviceCategory.Room))
Categories.Add(DeviceCategory.Room);
break;
}
}
internal void ApplyToEntity()
{
// Other properties are computed
@@ -401,6 +449,10 @@ namespace Artemis.Core
DeviceEntity.InputMappings.Clear();
foreach (var (original, mapped) in InputMappings)
DeviceEntity.InputMappings.Add(new InputMappingEntity {OriginalLedId = (int) original.RgbLed.Id, MappedLedId = (int) mapped.RgbLed.Id});
DeviceEntity.Categories.Clear();
foreach (DeviceCategory deviceCategory in Categories)
DeviceEntity.Categories.Add((int) deviceCategory);
}
internal void ApplyToRgbDevice()
@@ -419,6 +471,12 @@ namespace Artemis.Core
if (!RgbDevice.ColorCorrections.Any())
RgbDevice.ColorCorrections.Add(new ScaleColorCorrection(this));
Categories.Clear();
foreach (int deviceEntityCategory in DeviceEntity.Categories)
Categories.Add((DeviceCategory) deviceEntityCategory);
if (!Categories.Any())
ApplyDefaultCategories();
CalculateRenderProperties();
OnDeviceUpdated();
@@ -472,4 +530,13 @@ namespace Artemis.Core
LogicalLayout = DeviceEntity.LogicalLayout;
}
}
public enum DeviceCategory
{
Desk,
Monitor,
Case,
Room,
Peripherals
}
}