mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2025-12-12 17:48:31 +00:00
Merge pull request #357 from DarthAffe/LayoutFixes
Layout fixes/improvements
This commit is contained in:
commit
99225f04fc
@ -50,6 +50,12 @@ public readonly struct Scale : IEquatable<Scale>
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="Horizontal"/> and <see cref="Vertical"/> value of this <see cref="Scale"/> to a human-readable string.
|
||||
/// </summary>
|
||||
/// <returns>A string that contains the <see cref="Horizontal"/> and <see cref="Vertical"/> value of this <see cref="Scale"/>. For example "[Horizontal: 1, Vertical: 0.5]".</returns>
|
||||
public override string ToString() => $"[Horizontal: {Horizontal}, Vertical: {Vertical}]\"";
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether the specified <see cref="Scale"/> is equivalent to this <see cref="Scale" />.
|
||||
/// </summary>
|
||||
|
||||
@ -174,7 +174,7 @@ public class DeviceLayout : IDeviceLayout
|
||||
/// <returns>The deserialized custom data object.</returns>
|
||||
protected virtual object? GetCustomData(object? customData, Type? type)
|
||||
{
|
||||
XmlNode? node = (customData as XmlNode) ?? (customData as IEnumerable<XmlNode>)?.FirstOrDefault()?.ParentNode; //HACK DarthAffe 16.01.2021: This gives us the CustomData-Node
|
||||
XmlNode? node = (customData as XmlNode) ?? (customData as IEnumerable<XmlNode>)?.FirstOrDefault(x => x.ParentNode != null)?.ParentNode; //HACK DarthAffe 16.01.2021: This gives us the CustomData-Node
|
||||
if ((node == null) || (type == null)) return null;
|
||||
|
||||
using MemoryStream ms = new();
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace RGB.NET.Layout;
|
||||
@ -51,4 +53,48 @@ public static class LayoutExtension
|
||||
device.RemoveLed(led);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the specified layout to the given location.
|
||||
/// </summary>
|
||||
/// <param name="layout">The layout to save.</param>
|
||||
/// <param name="targetFile">The location to save to.</param>
|
||||
public static void Save(this IDeviceLayout layout, string targetFile)
|
||||
{
|
||||
using FileStream fs = new(targetFile, FileMode.Create);
|
||||
layout.Save(fs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the specified layout to the given stream.
|
||||
/// </summary>
|
||||
/// <param name="layout">The layout to save.</param>
|
||||
/// <param name="stream">The stream to save to.</param>
|
||||
public static void Save(this IDeviceLayout layout, Stream stream)
|
||||
{
|
||||
Type? customDataType = layout.CustomData?.GetType();
|
||||
Type? customLedDataType = layout.Leds.FirstOrDefault(x => x.CustomData != null)?.GetType();
|
||||
|
||||
Type[] customTypes;
|
||||
if ((customDataType != null) && (customLedDataType != null))
|
||||
customTypes = new[] { customDataType, customLedDataType };
|
||||
else if (customDataType != null)
|
||||
customTypes = new[] { customDataType };
|
||||
else if (customLedDataType != null)
|
||||
customTypes = new[] { customLedDataType };
|
||||
else
|
||||
customTypes = Array.Empty<Type>();
|
||||
|
||||
if (layout is DeviceLayout deviceLayout)
|
||||
{
|
||||
deviceLayout.InternalCustomData = deviceLayout.CustomData;
|
||||
|
||||
foreach (ILedLayout led in deviceLayout.Leds)
|
||||
if (led is LedLayout ledLayout)
|
||||
ledLayout.InternalCustomData = ledLayout.CustomData;
|
||||
}
|
||||
|
||||
XmlSerializer serializer = new(typeof(DeviceLayout), null, customTypes, null, null);
|
||||
serializer.Serialize(stream, layout);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user