using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using RGB.NET.Layout;
namespace Artemis.Core
{
///
/// Represents a device layout decorated with extra Artemis-specific data
///
public class ArtemisLayout
{
///
/// Creates a new instance of the class
///
/// The path of the layout XML file
public ArtemisLayout(string filePath)
{
FilePath = filePath;
Leds = new List();
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();
}
///
/// Gets the file path the layout was (attempted to be) loaded from
///
public string FilePath { get; }
///
/// Gets the RGB.NET device layout
///
public DeviceLayout RgbLayout { get; }
///
/// Gets the device this layout is applied to
///
public ArtemisDevice? Device { get; private set; }
///
/// Gets a boolean indicating whether a valid layout was loaded
///
public bool IsValid { get; }
///
/// Gets the image of the device
///
public Uri? Image { get; private set; }
public List 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));
}
}
}