using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
using RGB.NET.Core;
namespace RGB.NET.Layout;
///
/// Represents the serializable layout of a .
///
[Serializable]
[XmlRoot("Device")]
public class DeviceLayout : DeviceLayout
{ }
///
/// Represents the serializable layout of a .
///
[Serializable]
[XmlRoot("Device")]
public class DeviceLayout : DeviceLayout
{ }
///
/// Represents the serializable layout of a .
///
[Serializable]
[XmlRoot("Device")]
public class DeviceLayout : IDeviceLayout
{
#region Properties & Fields
///
/// Gets or sets the name of the .
///
[XmlElement("Name")]
public string? Name { get; set; }
///
/// Gets or sets the description of the .
///
[XmlElement("Description")]
public string? Description { get; set; }
///
/// Gets or sets the author of the .
///
[XmlElement("Author")]
public string? Author { get; set; }
///
/// Gets or sets the of the .
///
[XmlElement("Type")]
public RGBDeviceType Type { get; set; }
///
/// Gets or sets the vendor of the .
///
[XmlElement("Vendor")]
public string? Vendor { get; set; }
///
/// Gets or sets the model of the .
///
[XmlElement("Model")]
public string? Model { get; set; }
///
/// Gets or sets the of the .
///
[XmlElement("Shape")]
[DefaultValue(Shape.Rectangle)]
public Shape Shape { get; set; } = Shape.Rectangle;
///
/// Gets or sets the width of the .
///
[XmlElement("Width")]
public float Width { get; set; }
///
/// Gets or sets the height of the .
///
[XmlElement("Height")]
public float Height { get; set; }
///
/// Gets or sets the width of one 'unit' used for the calculation of led positions and sizes.
///
[XmlElement("LedUnitWidth")]
[DefaultValue(19.0)]
public float LedUnitWidth { get; set; } = 19.0f;
///
/// Gets or sets the height of one 'unit' used for the calculation of led positions and sizes.
///
[XmlElement("LedUnitHeight")]
[DefaultValue(19.0)]
public float LedUnitHeight { get; set; } = 19.0f;
///
/// Gets or sets the internal list of led layouts.
/// Normally you should use to access this data.
///
[XmlArray("Leds")]
public List> InternalLeds { get; set; } = new();
///
/// Gets or sets a list of representing all the of the .
///
[XmlIgnore]
public IEnumerable Leds => InternalLeds;
///
/// Gets or sets the custom data of this layout.
///
[XmlElement("CustomData")]
public TCustomData? CustomData { get; set; }
///
/// Gets the untyped custom data of this layout.
///
public object? UntypedCustomData => CustomData;
#endregion
#region Methods
///
/// Creates a new from the specified xml.
///
/// The stream that contains the layout to be loaded.
/// The deserialized .
public static DeviceLayout? Load(Stream stream)
{
try
{
XmlSerializer serializer = new(typeof(DeviceLayout));
DeviceLayout? layout = serializer.Deserialize(stream) as DeviceLayout;
if (layout?.InternalLeds != null)
{
LedLayout? lastLed = null;
foreach (LedLayout led in layout.InternalLeds)
{
led.CalculateValues(layout, lastLed);
lastLed = led;
}
}
return layout;
}
catch
{
return null;
}
}
///
/// Creates a new from the specified xml.
///
/// The path to the xml file.
/// The deserialized .
public static DeviceLayout? Load(string path)
{
if (!File.Exists(path)) return null;
using Stream stream = File.OpenRead(path);
return Load(stream);
}
#endregion
}