1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Devices - Implemented device image loading

Devices - LED image loading WIP
This commit is contained in:
Robert 2021-02-11 20:12:19 +01:00
parent e1121afdf9
commit 956ebd3313
13 changed files with 308 additions and 107 deletions

View File

@ -43,6 +43,7 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Clayerproperties_005Ctypes/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cprofile_005Clayershapes/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Csurface/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Csurface_005Clayout/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=mvvm/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=placeholders/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=plugins/@EntryIndexedValue">True</s:Boolean>

View File

@ -32,7 +32,7 @@ namespace Artemis.Core
GreenScale = 1;
BlueScale = 1;
IsEnabled = true;
InputIdentifiers = new List<ArtemisDeviceInputIdentifier>();
Leds = rgbDevice.Select(l => new ArtemisLed(l, this)).ToList().AsReadOnly();
@ -49,7 +49,7 @@ namespace Artemis.Core
RgbDevice = rgbDevice;
DeviceProvider = deviceProvider;
Surface = surface;
InputIdentifiers = new List<ArtemisDeviceInputIdentifier>();
foreach (DeviceInputIdentifierEntity identifierEntity in DeviceEntity.InputIdentifiers)
InputIdentifiers.Add(new ArtemisDeviceInputIdentifier(identifierEntity.InputProvider, identifierEntity.Identifier));
@ -95,13 +95,13 @@ namespace Artemis.Core
/// <summary>
/// Gets a read only collection containing the LEDs of this device
/// </summary>
public ReadOnlyCollection<ArtemisLed> Leds { get; }
public ReadOnlyCollection<ArtemisLed> Leds { get; private set; }
/// <summary>
/// Gets a dictionary containing all the LEDs of this device with their corresponding RGB.NET <see cref="LedId" /> as
/// key
/// </summary>
public ReadOnlyDictionary<LedId, ArtemisLed> LedIds { get; }
public ReadOnlyDictionary<LedId, ArtemisLed> LedIds { get; private set; }
/// <summary>
/// Gets a list of input identifiers associated with this device
@ -225,6 +225,34 @@ namespace Artemis.Core
}
}
/// <summary>
/// Gets or sets the physical layout of the device e.g. ISO or ANSI.
/// <para>Only applicable to keyboards</para>
/// </summary>
public string? PhysicalLayout
{
get => DeviceEntity.PhysicalLayout;
set
{
DeviceEntity.PhysicalLayout = value;
OnPropertyChanged(nameof(PhysicalLayout));
}
}
/// <summary>
/// Gets or sets the logical layout of the device e.g. DE, UK or US.
/// <para>Only applicable to keyboards</para>
/// </summary>
public string? LogicalLayout
{
get => DeviceEntity.LogicalLayout;
set
{
DeviceEntity.LogicalLayout = value;
OnPropertyChanged(nameof(LogicalLayout));
}
}
/// <summary>
/// Gets the layout of the device expanded with Artemis-specific data
/// </summary>
@ -259,13 +287,58 @@ namespace Artemis.Core
return artemisLed;
}
/// <summary>
/// Generates the default layout file name of the device
/// </summary>
/// <param name="includeExtension">If true, the .xml extension is added to the file name</param>
/// <returns>The resulting file name e.g. CORSAIR GLAIVE.xml or K95 RGB-ISO.xml</returns>
public string GetLayoutFileName(bool includeExtension = true)
{
// Take out invalid file name chars, may not be perfect but neither are you
string fileName = System.IO.Path.GetInvalidFileNameChars().Aggregate(RgbDevice.DeviceInfo.Model, (current, c) => current.Replace(c, '-'));
if (PhysicalLayout != null)
fileName = $"{fileName}-{PhysicalLayout.ToUpper()}";
if (includeExtension)
fileName = $"{fileName}.xml";
return fileName;
}
/// <summary>
/// Loads the best layout for this device, preferring user layouts and falling back to default layouts
/// </summary>
public void LoadBestLayout()
{
ArtemisLayout artemisLayout = DeviceProvider.LoadLayout(RgbDevice);
if (artemisLayout.IsValid)
artemisLayout.DeviceLayout!.ApplyTo(RgbDevice);
// If supported, detect the device layout so that we can load the correct one
if (DeviceProvider.CanDetectLogicalLayout || DeviceProvider.CanDetectPhysicalLayout && RgbDevice is IKeyboard)
DeviceProvider.DetectDeviceLayout(this);
Layout = artemisLayout;
// Look for a user layout
// ... here
// Try loading a device provider layout, if that comes back valid we use that
ArtemisLayout deviceProviderLayout = DeviceProvider.LoadLayout(this);
// Finally fall back to a default layout
// .. do it!
ApplyLayout(deviceProviderLayout);
}
/// <summary>
/// Applies the provided layout to the device
/// </summary>
/// <param name="layout">The layout to apply</param>
public void ApplyLayout(ArtemisLayout layout)
{
if (layout.IsValid)
layout.RgbLayout!.ApplyTo(RgbDevice);
Layout = layout;
Leds = RgbDevice.Select(l => new ArtemisLed(l, this)).ToList().AsReadOnly();
LedIds = new ReadOnlyDictionary<LedId, ArtemisLed>(Leds.ToDictionary(l => l.RgbLed.Id, l => l));
Layout.ApplyDevice(this);
Leds = Layout.Leds.Select(l => l.Led);
}
internal void ApplyToEntity()

View File

@ -1,57 +0,0 @@
using System;
using RGB.NET.Layout;
namespace Artemis.Core
{
/// <summary>
/// Represents a device layout decorated with extra Artemis-specific data
/// </summary>
public class ArtemisLayout
{
/// <summary>
/// Creates a new instance of the <see cref="ArtemisLayout" /> class
/// </summary>
/// <param name="filePath">The path of the layout XML file</param>
public ArtemisLayout(string filePath)
{
FilePath = filePath;
DeviceLayout = DeviceLayout.Load(FilePath);
IsValid = DeviceLayout != null;
}
/// <summary>
/// Gets the file path the layout was (attempted to be) loaded from
/// </summary>
public string FilePath { get; }
/// <summary>
/// Gets the RGB.NET device layout
/// </summary>
public DeviceLayout? DeviceLayout { get; }
/// <summary>
/// Gets a boolean indicating whether a valid layout was loaded
/// </summary>
public bool IsValid { get; }
/// <summary>
/// Gets the device this image is applied to
/// </summary>
public ArtemisDevice? Device { get; internal set; }
/// <summary>
/// Gets or sets the physical layout of the device. Only applicable to keyboards
/// </summary>
public string? PhysicalLayout { get; set; }
/// <summary>
/// Gets or sets the logical layout of the device. Only applicable to keyboards
/// </summary>
public string? LogicalLayout { get; set; }
/// <summary>
/// Gets or sets the image of the device
/// </summary>
public Uri? Image { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using RGB.NET.Core;
using System.Linq;
using RGB.NET.Core;
using SkiaSharp;
namespace Artemis.Core
@ -16,6 +17,9 @@ namespace Artemis.Core
RgbLed = led;
Device = device;
CalculateRectangles();
Layout = Device.Layout?.Leds.FirstOrDefault(l => l.RgbLayout.Id == led.Id.ToString());
}
/// <summary>
@ -46,7 +50,7 @@ namespace Artemis.Core
private set => SetAndNotify(ref _absoluteRectangle, value);
}
public ArtemisLedLayout? Layout { get; set; }
public ArtemisLedLayout? Layout { get; }
internal void CalculateRectangles()
{

View File

@ -1,10 +0,0 @@
using System;
namespace Artemis.Core
{
public class ArtemisLedLayout
{
public ArtemisLed Led { get; set; }
public Uri Image { get; set; }
}
}

View File

@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using RGB.NET.Layout;
namespace Artemis.Core
{
/// <summary>
/// Represents a device layout decorated with extra Artemis-specific data
/// </summary>
public class ArtemisLayout
{
/// <summary>
/// Creates a new instance of the <see cref="ArtemisLayout" /> class
/// </summary>
/// <param name="filePath">The path of the layout XML file</param>
public ArtemisLayout(string filePath)
{
FilePath = filePath;
Leds = new List<ArtemisLedLayout>();
DeviceLayout? deviceLayout = DeviceLayout.Load(FilePath, typeof(LayoutCustomDeviceData), typeof(LayoutCustomLedData));
if (deviceLayout != null)
{
RgbLayout = deviceLayout;
IsValid = true;
}
else
{
RgbLayout = new DeviceLayout();
IsValid = false;
}
if (IsValid)
Leds.AddRange(RgbLayout.Leds.Select(l => new ArtemisLedLayout(this, l)));
LayoutCustomDeviceData = (LayoutCustomDeviceData?) RgbLayout.CustomData ?? new LayoutCustomDeviceData();
ApplyCustomDeviceData();
}
/// <summary>
/// Gets the file path the layout was (attempted to be) loaded from
/// </summary>
public string FilePath { get; }
/// <summary>
/// Gets the RGB.NET device layout
/// </summary>
public DeviceLayout RgbLayout { get; }
/// <summary>
/// Gets the device this layout is applied to
/// </summary>
public ArtemisDevice? Device { get; private set; }
/// <summary>
/// Gets a boolean indicating whether a valid layout was loaded
/// </summary>
public bool IsValid { get; }
/// <summary>
/// Gets or sets the image of the device
/// </summary>
public Uri? Image { get; set; }
public List<ArtemisLedLayout> Leds { get; }
internal LayoutCustomDeviceData LayoutCustomDeviceData { get; set; }
internal void ApplyDevice(ArtemisDevice artemisDevice)
{
Device = artemisDevice;
foreach (ArtemisLedLayout artemisLedLayout in Leds)
artemisLedLayout.ApplyDevice(Device);
}
private void ApplyCustomDeviceData()
{
Uri layoutDirectory = new(Path.GetDirectoryName(FilePath)! + "\\", UriKind.Absolute);
if (LayoutCustomDeviceData.DeviceImage != null)
Image = new Uri(layoutDirectory, new Uri(LayoutCustomDeviceData.DeviceImage, UriKind.Relative));
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.IO;
using System.Linq;
using RGB.NET.Layout;
namespace Artemis.Core
{
public class ArtemisLedLayout
{
internal ArtemisLedLayout(ArtemisLayout layout, ILedLayout led)
{
Layout = layout;
RgbLayout = led;
LayoutCustomLedData = (LayoutCustomLedData?) led.CustomData ?? new LayoutCustomLedData();
}
public ArtemisLayout Layout { get; }
/// <summary>
/// Gets the RGB.NET LED Layout of this Artemis LED layout
/// </summary>
public ILedLayout RgbLayout { get; }
/// <summary>
/// Gets the LED this layout is applied to
/// </summary>
public ArtemisLed? Led { get; protected set; }
/// <summary>
/// Gets the name of the logical layout this LED belongs to
/// </summary>
public string? LogicalName { get; private set; }
/// <summary>
/// Gets the image of the LED
/// </summary>
public Uri? Image { get; private set; }
internal LayoutCustomLedData LayoutCustomLedData { get; set; }
public void ApplyDevice(ArtemisDevice device)
{
Led = device.Leds.FirstOrDefault(d => d.RgbLed.Id.ToString() == RgbLayout.Id);
ApplyCustomLedData();
}
private void ApplyCustomLedData()
{
if (Led == null)
return;
Uri layoutDirectory = new(Path.GetDirectoryName(Layout.FilePath)! + "\\", UriKind.Absolute);
}
}
}

View File

@ -0,0 +1,15 @@
using System.Xml.Serialization;
#pragma warning disable 1591
namespace Artemis.Core
{
/// <summary>
/// Represents extra Artemis-specific information stored in RGB.NET layouts
/// </summary>
[XmlRoot("CustomData")]
public class LayoutCustomDeviceData
{
[XmlElement("DeviceImage")]
public string? DeviceImage { get; set; }
}
}

View File

@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Xml.Serialization;
#pragma warning disable 1591
namespace Artemis.Core
{
/// <summary>
/// Represents extra Artemis-specific information stored in RGB.NET layouts
/// </summary>
[XmlRoot("CustomData")]
public class LayoutCustomLedData
{
[XmlArray("LogicalLayouts")]
public List<LayoutCustomLedDataLogicalLayout>? LogicalLayouts { get; set; }
}
/// <summary>
/// Represents extra Artemis-specific information stored in RGB.NET layouts
/// </summary>
[XmlType("LogicalLayout")]
public class LayoutCustomLedDataLogicalLayout
{
[XmlAttribute("Name")]
public string? Name { get; set; }
[XmlAttribute("Image")]
public string? Image { get; set; }
}
}

View File

@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Linq;
using Ninject;
using RGB.NET.Core;
using Serilog;
@ -35,12 +34,20 @@ namespace Artemis.Core.DeviceProviders
public ILogger? Logger { get; set; }
/// <summary>
/// A boolean indicating whether this device provider detects the physical layout of connected keyboards
/// A boolean indicating whether this device provider detects the physical layout of connected keyboards.
/// <para>
/// Note: <see cref="DetectDeviceLayout" /> is only called when this or <see cref="CanDetectLogicalLayout" />
/// is <see langword="true" />.
/// </para>
/// </summary>
public bool CanDetectPhysicalLayout { get; protected set; }
/// <summary>
/// A boolean indicating whether this device provider detects the logical layout of connected keyboards
/// <para>
/// Note: <see cref="DetectDeviceLayout" /> is only called when this or <see cref="CanDetectPhysicalLayout" />
/// is <see langword="true" />.
/// </para>
/// </summary>
public bool CanDetectLogicalLayout { get; protected set; }
@ -55,33 +62,27 @@ namespace Artemis.Core.DeviceProviders
/// </summary>
/// <param name="rgbDevice">The device to load the layout for</param>
/// <returns>The resulting Artemis layout</returns>
public virtual ArtemisLayout LoadLayout(IRGBDevice rgbDevice)
public virtual ArtemisLayout LoadLayout(ArtemisDevice rgbDevice)
{
// Take out invalid file name chars, may not be perfect but neither are you
string model = Path.GetInvalidFileNameChars().Aggregate(rgbDevice.DeviceInfo.Model, (current, c) => current.Replace(c, '-'));
string layoutDir = Path.Combine(Plugin.Directory.FullName, "Layouts");
string filePath;
// if (rgbDevice.DeviceInfo is IPhysicalLayoutDeviceInfo)
// {
// filePath = Path.Combine(
// layoutDir,
// rgbDevice.DeviceInfo.Manufacturer,
// rgbDevice.DeviceInfo.DeviceType.ToString(),
// model,
// keyboard.DeviceInfo.
// ) + ".xml";
// }
// else
// {
filePath = Path.Combine(
string filePath = Path.Combine(
layoutDir,
rgbDevice.DeviceInfo.Manufacturer,
rgbDevice.DeviceInfo.DeviceType.ToString(),
model
) + ".xml";
// }
rgbDevice.RgbDevice.DeviceInfo.Manufacturer,
rgbDevice.RgbDevice.DeviceInfo.DeviceType.ToString(),
rgbDevice.GetLayoutFileName()
);
return new ArtemisLayout(filePath);
}
/// <summary>
/// Called when a specific RGB device's logical and physical layout must be detected
/// <para>
/// Note: Only called when <see cref="CanDetectPhysicalLayout" /> or <see cref="CanDetectLogicalLayout" /> is <see langword="true" />.
/// </para>
/// </summary>
/// <param name="rgbDevice">The device to detect the layout for, always a keyboard</param>
public virtual void DetectDeviceLayout(ArtemisDevice rgbDevice)
{
}
}
}

View File

@ -20,6 +20,9 @@ namespace Artemis.Storage.Entities.Surface
public double BlueScale { get; set; }
public bool IsEnabled { get; set; }
public string PhysicalLayout { get; set; }
public string LogicalLayout { get; set; }
public List<DeviceInputIdentifierEntity> InputIdentifiers { get; set; }
}

View File

@ -235,8 +235,8 @@ namespace Artemis.UI.Shared
UpdateTransform();
// Load the device main image
// if (Device.RgbDevice.DeviceInfo?.Image?.AbsolutePath != null && File.Exists(Device.RgbDevice.DeviceInfo.Image.AbsolutePath))
// _deviceImage = new BitmapImage(Device.RgbDevice.DeviceInfo.Image);
if (Device.Layout?.Image != null && File.Exists(Device.Layout.Image.LocalPath))
_deviceImage = new BitmapImage(Device.Layout.Image);
// Create all the LEDs
foreach (ArtemisLed artemisLed in Device.Leds)

View File

@ -11,6 +11,8 @@ namespace Artemis.UI.Shared
{
internal class DeviceVisualizerLed
{
private SolidColorBrush? _renderColorBrush;
public DeviceVisualizerLed(ArtemisLed led)
{
Led = led;
@ -43,9 +45,9 @@ namespace Artemis.UI.Shared
byte g = originalColor.GetG();
byte b = originalColor.GetB();
drawingContext.DrawRectangle(isDimmed
? new SolidColorBrush(Color.FromArgb(100, r, g, b))
: new SolidColorBrush(Color.FromRgb(r, g, b)), null, LedRect);
_renderColorBrush ??= new SolidColorBrush();
_renderColorBrush.Color = isDimmed ? Color.FromArgb(100, r, g, b) : Color.FromRgb(r, g, b);
drawingContext.DrawRectangle(_renderColorBrush, null, LedRect);
}
public void RenderImage(DrawingContext drawingContext)