using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using Artemis.Storage.Entities.Surface; using RGB.NET.Core; namespace Artemis.Core { /// /// Represents a surface of a specific scale, containing all the available s /// public class ArtemisSurface : CorePropertyChanged { private List _devices = new(); private ReadOnlyDictionary _ledMap = new(new Dictionary()); private bool _isActive; private string _name; internal ArtemisSurface(RGBSurface rgbSurface, string name) { SurfaceEntity = new SurfaceEntity {DeviceEntities = new List()}; EntityId = Guid.NewGuid(); RgbSurface = rgbSurface; _name = name; _isActive = false; ApplyToEntity(); } internal ArtemisSurface(RGBSurface rgbSurface, SurfaceEntity surfaceEntity) { SurfaceEntity = surfaceEntity; EntityId = surfaceEntity.Id; RgbSurface = rgbSurface; _name = surfaceEntity.Name; _isActive = surfaceEntity.IsActive; } /// /// Gets the RGB.NET surface backing this Artemis surface /// public RGBSurface RgbSurface { get; } /// /// Gets the name of the surface /// public string Name { get => _name; set => SetAndNotify(ref _name, value); } /// /// Gets a boolean indicating whether this surface is the currently active surface /// public bool IsActive { get => _isActive; internal set => SetAndNotify(ref _isActive, value); } /// /// Gets a list of devices this surface contains /// public List Devices { get => _devices; internal set => SetAndNotify(ref _devices, value); } /// /// Gets a dictionary containing all s on the surface with their corresponding RGB.NET /// as key /// public ReadOnlyDictionary LedMap { get => _ledMap; private set => SetAndNotify(ref _ledMap, value); } internal SurfaceEntity SurfaceEntity { get; set; } internal Guid EntityId { get; set; } /// /// Attempts to retrieve the that corresponds the provided RGB.NET /// /// The RGB.NET to find the corresponding for /// If found, the corresponding ; otherwise . public ArtemisLed? GetArtemisLed(Led led) { LedMap.TryGetValue(led, out ArtemisLed? artemisLed); return artemisLed; } internal void UpdateLedMap() { LedMap = new ReadOnlyDictionary( _devices.Where(d => d.IsEnabled).SelectMany(d => d.Leds.Select(al => new KeyValuePair(al.RgbLed, al))).ToDictionary(kvp => kvp.Key, kvp => kvp.Value) ); } internal void ApplyToEntity() { SurfaceEntity.Id = EntityId; SurfaceEntity.Name = Name; SurfaceEntity.IsActive = IsActive; // Add missing device entities, don't remove old ones in case they come back later foreach (DeviceEntity deviceEntity in Devices.Select(d => d.DeviceEntity).ToList()) if (!SurfaceEntity.DeviceEntities.Contains(deviceEntity)) SurfaceEntity.DeviceEntities.Add(deviceEntity); } #region Events /// /// Occurs when the scale of the surface is changed /// public event EventHandler? ScaleChanged; /// /// Invokes the event /// protected virtual void OnScaleChanged() { ScaleChanged?.Invoke(this, EventArgs.Empty); } #endregion } }