mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2025-12-12 17:48:31 +00:00
Extracted layout-logic to own project
This commit is contained in:
parent
226d29156d
commit
4a170e6af7
@ -258,10 +258,11 @@ namespace RGB.NET.Core
|
||||
if ((hexString == null) || (hexString.Length < 6))
|
||||
throw new ArgumentException("Invalid hex string", nameof(hexString));
|
||||
|
||||
if (hexString[0] == '#')
|
||||
hexString = hexString.Substring(1);
|
||||
ReadOnlySpan<char> span = hexString.AsSpan();
|
||||
if (span[0] == '#')
|
||||
span = span[1..];
|
||||
|
||||
byte[] data = ConversionHelper.HexToBytes(hexString);
|
||||
byte[] data = ConversionHelper.HexToBytes(span);
|
||||
return data.Length switch
|
||||
{
|
||||
3 => new Color(data[0], data[1], data[2]),
|
||||
|
||||
@ -2,12 +2,9 @@
|
||||
// ReSharper disable UnusedMember.Global
|
||||
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using RGB.NET.Core.Layout;
|
||||
|
||||
namespace RGB.NET.Core
|
||||
{
|
||||
|
||||
@ -28,12 +28,7 @@ namespace RGB.NET.Core
|
||||
/// Gets the model-name of the <see cref="IRGBDevice"/>.
|
||||
/// </summary>
|
||||
string Model { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the lighting capability of the <see cref="IRGBDevice"/>
|
||||
/// </summary>
|
||||
RGBDeviceLighting Lighting { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the URI of an image of the <see cref="IRGBDevice"/> or null if there is no image.
|
||||
/// </summary>
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
using RGB.NET.Core.Layout;
|
||||
|
||||
namespace RGB.NET.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains a list of different lightning-modes used by <see cref="DeviceLayout"/>.
|
||||
/// </summary>
|
||||
public enum RGBDeviceLighting
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="IRGBDevice"/> doesn't support lighting,
|
||||
/// </summary>
|
||||
None = 0,
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IRGBDevice"/> supports per-key-lightning.
|
||||
/// </summary>
|
||||
Key = 1,
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IRGBDevice"/> supports per-device-lightning.
|
||||
/// </summary>
|
||||
Device = 2,
|
||||
}
|
||||
}
|
||||
@ -37,7 +37,7 @@ namespace RGB.NET.Core
|
||||
/// </summary>
|
||||
/// <param name="hexString">The HEX-string to convert.</param>
|
||||
/// <returns>The correspondending byte array.</returns>
|
||||
public static byte[] HexToBytes(string hexString)
|
||||
public static byte[] HexToBytes(ReadOnlySpan<char> hexString)
|
||||
{
|
||||
if ((hexString.Length == 0) || ((hexString.Length % 2) != 0))
|
||||
return Array.Empty<byte>();
|
||||
|
||||
@ -1,10 +1,9 @@
|
||||
using System;
|
||||
using RGB.NET.Core.Layout;
|
||||
|
||||
namespace RGB.NET.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains a list of different shapes used by <see cref="DeviceLayout"/>.
|
||||
/// Contains a list of different shapes used to define the layout of a LED.
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public enum Shape
|
||||
|
||||
@ -3,8 +3,9 @@ using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Xml.Serialization;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Core.Layout
|
||||
namespace RGB.NET.Layout
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the serializable layout of a <see cref="IRGBDevice"/>.
|
||||
@ -19,13 +20,13 @@ namespace RGB.NET.Core.Layout
|
||||
/// Gets or sets the name of the <see cref="DeviceLayout"/>.
|
||||
/// </summary>
|
||||
[XmlElement("Name")]
|
||||
public string Name { get; set; }
|
||||
public string? Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the description of the <see cref="DeviceLayout"/>.
|
||||
/// </summary>
|
||||
[XmlElement("Description")]
|
||||
public string Description { get; set; }
|
||||
public string? Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="RGBDeviceType"/> of the <see cref="DeviceLayout"/>.
|
||||
@ -33,23 +34,17 @@ namespace RGB.NET.Core.Layout
|
||||
[XmlElement("Type")]
|
||||
public RGBDeviceType Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="RGBDeviceLighting"/> of the <see cref="DeviceLayout"/>.
|
||||
/// </summary>
|
||||
[XmlElement("Lighting")]
|
||||
public RGBDeviceLighting Lighting { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the vendor of the <see cref="DeviceLayout"/>.
|
||||
/// </summary>
|
||||
[XmlElement("Vendor")]
|
||||
public string Vendor { get; set; }
|
||||
public string? Vendor { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the model of the <see cref="DeviceLayout"/>.
|
||||
/// </summary>
|
||||
[XmlElement("Model")]
|
||||
public string Model { get; set; }
|
||||
public string? Model { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="Core.Shape"/> of the <see cref="DeviceLayout"/>.
|
||||
@ -62,13 +57,13 @@ namespace RGB.NET.Core.Layout
|
||||
/// Gets or sets the width of the <see cref="DeviceLayout"/>.
|
||||
/// </summary>
|
||||
[XmlElement("Width")]
|
||||
public double Width { get; set; }
|
||||
public double? Width { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the height of the <see cref="DeviceLayout"/>.
|
||||
/// </summary>
|
||||
[XmlElement("Height")]
|
||||
public double Height { get; set; }
|
||||
public double? Height { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the width of one 'unit' used for the calculation of led positions and sizes.
|
||||
@ -88,25 +83,25 @@ namespace RGB.NET.Core.Layout
|
||||
/// The path images for this device are collected in.
|
||||
/// </summary>
|
||||
[XmlElement("ImageBasePath")]
|
||||
public string ImageBasePath { get; set; }
|
||||
public string? ImageBasePath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The image file for this device.
|
||||
/// </summary>
|
||||
[XmlElement("DeviceImage")]
|
||||
public string DeviceImage { get; set; }
|
||||
public string? DeviceImage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a list of <see cref="LedLayout"/> representing all the <see cref="Led"/> of the <see cref="DeviceLayout"/>.
|
||||
/// </summary>
|
||||
[XmlArray("Leds")]
|
||||
public List<LedLayout> Leds { get; set; } = new List<LedLayout>();
|
||||
public List<LedLayout> Leds { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a list of <see cref="LedImageLayout"/> representing the layouts for the images of all the <see cref="Led"/> of the <see cref="DeviceLayout"/>.
|
||||
/// </summary>
|
||||
[XmlArray("LedImageLayouts")]
|
||||
public List<LedImageLayout> LedImageLayouts { get; set; } = new List<LedImageLayout>();
|
||||
public List<LedImageLayout> LedImageLayouts { get; set; } = new();
|
||||
|
||||
#endregion
|
||||
|
||||
@ -117,28 +112,27 @@ namespace RGB.NET.Core.Layout
|
||||
/// </summary>
|
||||
/// <param name="path">The path to the xml file.</param>
|
||||
/// <returns>The deserialized <see cref="DeviceLayout"/>.</returns>
|
||||
public static DeviceLayout Load(string path)
|
||||
public static DeviceLayout? Load(string path)
|
||||
{
|
||||
if (!File.Exists(path)) return null;
|
||||
|
||||
try
|
||||
{
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(DeviceLayout));
|
||||
using (StreamReader reader = new StreamReader(path))
|
||||
{
|
||||
DeviceLayout layout = serializer.Deserialize(reader) as DeviceLayout;
|
||||
if (layout?.Leds != null)
|
||||
{
|
||||
LedLayout lastLed = null;
|
||||
foreach (LedLayout led in layout.Leds)
|
||||
{
|
||||
led.CalculateValues(layout, lastLed);
|
||||
lastLed = led;
|
||||
}
|
||||
}
|
||||
XmlSerializer serializer = new(typeof(DeviceLayout));
|
||||
using StreamReader reader = new(path);
|
||||
|
||||
return layout;
|
||||
DeviceLayout? layout = serializer.Deserialize(reader) as DeviceLayout;
|
||||
if (layout?.Leds != null)
|
||||
{
|
||||
LedLayout? lastLed = null;
|
||||
foreach (LedLayout led in layout.Leds)
|
||||
{
|
||||
led.CalculateValues(layout, lastLed);
|
||||
lastLed = led;
|
||||
}
|
||||
}
|
||||
|
||||
return layout;
|
||||
}
|
||||
catch
|
||||
{
|
||||
@ -1,7 +1,8 @@
|
||||
using System;
|
||||
using System.Xml.Serialization;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Core.Layout
|
||||
namespace RGB.NET.Layout
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the serializable image-data of a specific <see cref="Led"/>.
|
||||
@ -14,12 +15,12 @@ namespace RGB.NET.Core.Layout
|
||||
/// Gets or sets the Id of the <see cref="LedImage"/>.
|
||||
/// </summary>
|
||||
[XmlAttribute("Id")]
|
||||
public string Id { get; set; }
|
||||
public string? Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the image of the <see cref="LedImage"/>.
|
||||
/// </summary>
|
||||
[XmlAttribute("Image")]
|
||||
public string Image { get; set; }
|
||||
public string? Image { get; set; }
|
||||
}
|
||||
}
|
||||
@ -2,8 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Xml.Serialization;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Core.Layout
|
||||
namespace RGB.NET.Layout
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the serializable collection of <see cref="LedImage"/> for a specific layout.
|
||||
@ -17,12 +18,12 @@ namespace RGB.NET.Core.Layout
|
||||
/// </summary>
|
||||
[XmlAttribute("Layout")]
|
||||
[DefaultValue(null)]
|
||||
public string Layout { get; set; }
|
||||
public string? Layout { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a list of <see cref="LedImage"/> representing the images of all the <see cref="Led"/> of the represented layout.
|
||||
/// </summary>
|
||||
[XmlArray("LedImages")]
|
||||
public List<LedImage> LedImages { get; set; } = new List<LedImage>();
|
||||
public List<LedImage> LedImages { get; set; } = new();
|
||||
}
|
||||
}
|
||||
@ -2,8 +2,9 @@
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Xml.Serialization;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Core.Layout
|
||||
namespace RGB.NET.Layout
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the serializable layout of a <see cref="Led"/>.
|
||||
@ -18,7 +19,7 @@ namespace RGB.NET.Core.Layout
|
||||
/// Gets or sets the Id of the <see cref="LedLayout"/>.
|
||||
/// </summary>
|
||||
[XmlAttribute("Id")]
|
||||
public string Id { get; set; }
|
||||
public string? Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the descriptive <see cref="RGB.NET.Core.Shape"/> of the <see cref="LedLayout"/>.
|
||||
@ -70,7 +71,7 @@ namespace RGB.NET.Core.Layout
|
||||
/// Gets or sets the vecor-data representing a custom-shape of the <see cref="LedLayout"/>.
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public string ShapeData { get; set; }
|
||||
public string? ShapeData { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the x-position of the <see cref="LedLayout"/>.
|
||||
@ -105,7 +106,7 @@ namespace RGB.NET.Core.Layout
|
||||
/// </summary>
|
||||
/// <param name="device">The <see cref="DeviceLayout"/> this <see cref="LedLayout"/> belongs to.</param>
|
||||
/// <param name="lastLed">The <see cref="LedLayout"/> previously calculated.</param>
|
||||
public void CalculateValues(DeviceLayout device, LedLayout lastLed)
|
||||
public void CalculateValues(DeviceLayout device, LedLayout? lastLed)
|
||||
{
|
||||
if (!Enum.TryParse(DescriptiveShape, true, out Shape shape))
|
||||
{
|
||||
@ -136,19 +137,19 @@ namespace RGB.NET.Core.Layout
|
||||
return lastValue + lastSize;
|
||||
|
||||
if (value.StartsWith("+", StringComparison.Ordinal))
|
||||
return lastValue + lastSize + double.Parse(value.Substring(1), CultureInfo.InvariantCulture);
|
||||
return lastValue + lastSize + double.Parse(value[1..], CultureInfo.InvariantCulture);
|
||||
|
||||
if (string.Equals(value, "-", StringComparison.Ordinal))
|
||||
return lastValue - currentSize;
|
||||
|
||||
if (value.StartsWith("-", StringComparison.Ordinal))
|
||||
return lastValue - currentSize - double.Parse(value.Substring(1), CultureInfo.InvariantCulture);
|
||||
return lastValue - currentSize - double.Parse(value[1..], CultureInfo.InvariantCulture);
|
||||
|
||||
if (string.Equals(value, "~", StringComparison.Ordinal))
|
||||
return (lastValue + lastSize) - currentSize;
|
||||
|
||||
if (value.StartsWith("~", StringComparison.Ordinal))
|
||||
return (lastValue + lastSize) - currentSize - double.Parse(value.Substring(1), CultureInfo.InvariantCulture);
|
||||
return (lastValue + lastSize) - currentSize - double.Parse(value[1..], CultureInfo.InvariantCulture);
|
||||
|
||||
return double.Parse(value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
@ -167,7 +168,7 @@ namespace RGB.NET.Core.Layout
|
||||
value = value.Replace(" ", string.Empty);
|
||||
|
||||
if (value.EndsWith("mm", StringComparison.OrdinalIgnoreCase))
|
||||
return double.Parse(value.Substring(0, value.Length - 2), CultureInfo.InvariantCulture);
|
||||
return double.Parse(value[..^2], CultureInfo.InvariantCulture);
|
||||
|
||||
return unitSize * double.Parse(value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
56
RGB.NET.Layout/RGB.NET.Layout.csproj
Normal file
56
RGB.NET.Layout/RGB.NET.Layout.csproj
Normal file
@ -0,0 +1,56 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net5.0</TargetFrameworks>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<Authors>Darth Affe</Authors>
|
||||
<Company>Wyrez</Company>
|
||||
<Language>en-US</Language>
|
||||
<NeutralLanguage>en-US</NeutralLanguage>
|
||||
<Title>RGB.NET.Layout</Title>
|
||||
<AssemblyName>RGB.NET.Layout</AssemblyName>
|
||||
<AssemblyTitle>RGB.NET.Layout</AssemblyTitle>
|
||||
<PackageId>RGB.NET.Layout</PackageId>
|
||||
<RootNamespace>RGB.NET.Layout</RootNamespace>
|
||||
<Description>Layout-Module of RGB.NET</Description>
|
||||
<Summary>Layout-Module of RGB.NET, a C# (.NET) library for accessing various RGB-peripherals</Summary>
|
||||
<Copyright>Copyright © Darth Affe 2021</Copyright>
|
||||
<PackageCopyright>Copyright © Darth Affe 2021</PackageCopyright>
|
||||
<PackageIconUrl>http://lib.arge.be/icon.png</PackageIconUrl>
|
||||
<PackageProjectUrl>https://github.com/DarthAffe/RGB.NET</PackageProjectUrl>
|
||||
<PackageLicenseUrl>https://raw.githubusercontent.com/DarthAffe/RGB.NET/master/LICENSE</PackageLicenseUrl>
|
||||
<RepositoryType>Github</RepositoryType>
|
||||
<RepositoryUrl>https://github.com/DarthAffe/RGB.NET</RepositoryUrl>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
|
||||
<PackageReleaseNotes></PackageReleaseNotes>
|
||||
|
||||
<Version>0.0.1</Version>
|
||||
<AssemblyVersion>0.0.1</AssemblyVersion>
|
||||
<FileVersion>0.0.1</FileVersion>
|
||||
|
||||
<OutputPath>..\bin\</OutputPath>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<IncludeSource>True</IncludeSource>
|
||||
<IncludeSymbols>True</IncludeSymbols>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<DefineConstants>$(DefineConstants);TRACE;DEBUG</DefineConstants>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<NoWarn>$(NoWarn);CS1591;CS1572;CS1573</NoWarn>
|
||||
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\RGB.NET.Core\RGB.NET.Core.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@ -45,6 +45,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RGB.NET.Devices.Asus", "RGB
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RGB.NET.Devices.Wooting", "RGB.NET.Devices.Wooting\RGB.NET.Devices.Wooting.csproj", "{DD46DB2D-85BE-4962-86AE-E38C9053A548}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RGB.NET.Layout", "RGB.NET.Layout\RGB.NET.Layout.csproj", "{8AAB3736-B443-402C-B8AC-63D1A6DAFCCB}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -123,6 +125,10 @@ Global
|
||||
{DD46DB2D-85BE-4962-86AE-E38C9053A548}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{DD46DB2D-85BE-4962-86AE-E38C9053A548}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{DD46DB2D-85BE-4962-86AE-E38C9053A548}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{8AAB3736-B443-402C-B8AC-63D1A6DAFCCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8AAB3736-B443-402C-B8AC-63D1A6DAFCCB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8AAB3736-B443-402C-B8AC-63D1A6DAFCCB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8AAB3736-B443-402C-B8AC-63D1A6DAFCCB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user