mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 03:48:56 +00:00
Added a layer of abstraction between RGB.NET and Artemis
This means Artemis can add extra functionality It also avoid having to reference RGB.NET for basic rendering Implemented rendering scaling
This commit is contained in:
1 parent
498b6a6111
commit
8777e8975f
41 files changed
+725
-494
No files matched your search
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using Artemis.Core.Extensions;
|
||||
using Artemis.Storage.Entities;
|
||||
using RGB.NET.Core;
|
||||
using Rectangle = System.Drawing.Rectangle;
|
||||
|
||||
namespace Artemis.Core.Models.Surface
|
||||
{
|
||||
public class Device
|
||||
{
|
||||
internal Device(IRGBDevice rgbDevice, Surface surface)
|
||||
{
|
||||
RgbDevice = rgbDevice;
|
||||
Surface = surface;
|
||||
Configuration = new DeviceEntity();
|
||||
Leds = rgbDevice.Select(l => new DeviceLed(l, this)).ToList().AsReadOnly();
|
||||
|
||||
Rotation = 0;
|
||||
ZIndex = 1;
|
||||
|
||||
ApplyToConfiguration();
|
||||
CalculateRenderRectangle();
|
||||
}
|
||||
|
||||
internal Device(IRGBDevice rgbDevice, Surface surface, DeviceEntity configuration)
|
||||
{
|
||||
RgbDevice = rgbDevice;
|
||||
Surface = surface;
|
||||
Configuration = configuration;
|
||||
Leds = rgbDevice.Select(l => new DeviceLed(l, this)).ToList().AsReadOnly();
|
||||
|
||||
Rotation = configuration.Rotation;
|
||||
ZIndex = configuration.ZIndex;
|
||||
}
|
||||
|
||||
public Rectangle RenderRectangle { get; private set; }
|
||||
|
||||
public IRGBDevice RgbDevice { get; private set; }
|
||||
public Surface Surface { get; private set; }
|
||||
public DeviceEntity Configuration { get; private set; }
|
||||
public ReadOnlyCollection<DeviceLed> Leds { get; set; }
|
||||
|
||||
public double X
|
||||
{
|
||||
get => Configuration.X;
|
||||
set => Configuration.X = value;
|
||||
}
|
||||
|
||||
public double Y
|
||||
{
|
||||
get => Configuration.Y;
|
||||
set => Configuration.Y = value;
|
||||
}
|
||||
|
||||
public double Rotation
|
||||
{
|
||||
get => Configuration.Rotation;
|
||||
set => Configuration.Rotation = value;
|
||||
}
|
||||
|
||||
public int ZIndex
|
||||
{
|
||||
get => Configuration.ZIndex;
|
||||
set => Configuration.ZIndex = value;
|
||||
}
|
||||
|
||||
internal void ApplyToConfiguration()
|
||||
{
|
||||
Configuration.SurfaceId = Surface.Guid;
|
||||
Configuration.DeviceHashCode = RgbDevice.GetDeviceHashCode();
|
||||
|
||||
// Ensure the position configuration is in the surface configuration's' collection of positions
|
||||
if (Surface.SurfaceEntity.DeviceEntities.All(p => p.Guid != Configuration.Guid))
|
||||
Surface.SurfaceEntity.DeviceEntities.Add(Configuration);
|
||||
}
|
||||
|
||||
internal void ApplyToRgbDevice()
|
||||
{
|
||||
RgbDevice.Location = new Point(Configuration.X, Configuration.Y);
|
||||
CalculateRenderRectangle();
|
||||
}
|
||||
|
||||
internal void CalculateRenderRectangle()
|
||||
{
|
||||
RenderRectangle = new Rectangle(
|
||||
(int) Math.Round(RgbDevice.Location.X * Surface.Scale, MidpointRounding.AwayFromZero),
|
||||
(int) Math.Round(RgbDevice.Location.Y * Surface.Scale, MidpointRounding.AwayFromZero),
|
||||
(int) Math.Round(RgbDevice.Size.Width * Surface.Scale, MidpointRounding.AwayFromZero),
|
||||
(int) Math.Round(RgbDevice.Size.Height * Surface.Scale, MidpointRounding.AwayFromZero)
|
||||
);
|
||||
|
||||
foreach (var led in Leds)
|
||||
led.CalculateRenderRectangle();
|
||||
}
|
||||
|
||||
internal void Destroy()
|
||||
{
|
||||
Configuration = null;
|
||||
RgbDevice = null;
|
||||
Surface = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using RGB.NET.Core;
|
||||
using Rectangle = System.Drawing.Rectangle;
|
||||
|
||||
namespace Artemis.Core.Models.Surface
|
||||
{
|
||||
public class DeviceLed
|
||||
{
|
||||
public DeviceLed(Led led, Device device)
|
||||
{
|
||||
RgbLed = led;
|
||||
Device = device;
|
||||
CalculateRenderRectangle();
|
||||
}
|
||||
|
||||
public Led RgbLed { get; }
|
||||
public Device Device { get; }
|
||||
|
||||
public Rectangle RenderRectangle { get; private set; }
|
||||
public Rectangle AbsoluteRenderRectangle { get; private set; }
|
||||
|
||||
public void CalculateRenderRectangle()
|
||||
{
|
||||
RenderRectangle = new Rectangle(
|
||||
(int) Math.Round(RgbLed.LedRectangle.X * Device.Surface.Scale, MidpointRounding.AwayFromZero),
|
||||
(int) Math.Round(RgbLed.LedRectangle.Y * Device.Surface.Scale, MidpointRounding.AwayFromZero),
|
||||
(int) Math.Round(RgbLed.LedRectangle.Width * Device.Surface.Scale, MidpointRounding.AwayFromZero),
|
||||
(int) Math.Round(RgbLed.LedRectangle.Height * Device.Surface.Scale, MidpointRounding.AwayFromZero)
|
||||
);
|
||||
AbsoluteRenderRectangle = new Rectangle(
|
||||
(int) Math.Round(RgbLed.AbsoluteLedRectangle.X * Device.Surface.Scale, MidpointRounding.AwayFromZero),
|
||||
(int) Math.Round(RgbLed.AbsoluteLedRectangle.Y * Device.Surface.Scale, MidpointRounding.AwayFromZero),
|
||||
(int) Math.Round(RgbLed.AbsoluteLedRectangle.Width * Device.Surface.Scale, MidpointRounding.AwayFromZero),
|
||||
(int) Math.Round(RgbLed.AbsoluteLedRectangle.Height * Device.Surface.Scale, MidpointRounding.AwayFromZero)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Artemis.Core.Extensions;
|
||||
using Artemis.Storage.Entities;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace Artemis.Core.Models.Surface
|
||||
{
|
||||
public class Surface
|
||||
{
|
||||
internal Surface(RGBSurface rgbSurface, string name, double scale)
|
||||
{
|
||||
SurfaceEntity = new SurfaceEntity {DeviceEntities = new List<DeviceEntity>()};
|
||||
Guid = System.Guid.NewGuid().ToString();
|
||||
|
||||
Name = name;
|
||||
Scale = scale;
|
||||
RgbSurface = rgbSurface;
|
||||
IsActive = false;
|
||||
Devices = new List<Device>();
|
||||
|
||||
ApplyToEntity();
|
||||
}
|
||||
|
||||
internal Surface(RGBSurface rgbSurface, SurfaceEntity surfaceEntity, double scale)
|
||||
{
|
||||
RgbSurface = rgbSurface;
|
||||
SurfaceEntity = surfaceEntity;
|
||||
Scale = scale;
|
||||
Guid = surfaceEntity.Guid;
|
||||
|
||||
Name = surfaceEntity.Name;
|
||||
IsActive = surfaceEntity.IsActive;
|
||||
Devices = new List<Device>();
|
||||
|
||||
if (surfaceEntity.DeviceEntities == null)
|
||||
return;
|
||||
|
||||
foreach (var position in surfaceEntity.DeviceEntities)
|
||||
{
|
||||
var device = rgbSurface.Devices.FirstOrDefault(d => d.GetDeviceHashCode() == position.DeviceHashCode);
|
||||
if (device != null)
|
||||
Devices.Add(new Device(device, this, position));
|
||||
}
|
||||
}
|
||||
|
||||
public RGBSurface RgbSurface { get; }
|
||||
public double Scale { get; private set; }
|
||||
public string Name { get; set; }
|
||||
public bool IsActive { get; internal set; }
|
||||
public List<Device> Devices { get; internal set; }
|
||||
|
||||
internal SurfaceEntity SurfaceEntity { get; set; }
|
||||
internal string Guid { get; set; }
|
||||
|
||||
internal void ApplyToEntity()
|
||||
{
|
||||
SurfaceEntity.Guid = Guid;
|
||||
SurfaceEntity.Name = Name;
|
||||
SurfaceEntity.IsActive = IsActive;
|
||||
}
|
||||
|
||||
internal void Destroy()
|
||||
{
|
||||
SurfaceEntity = null;
|
||||
|
||||
foreach (var deviceConfiguration in Devices)
|
||||
deviceConfiguration.Destroy();
|
||||
Devices.Clear();
|
||||
}
|
||||
|
||||
public void UpdateScale(double value)
|
||||
{
|
||||
Scale = value;
|
||||
foreach (var device in Devices)
|
||||
device.CalculateRenderRectangle();
|
||||
|
||||
OnScaleChanged();
|
||||
}
|
||||
|
||||
#region Events
|
||||
|
||||
public event EventHandler<EventArgs> ScaleChanged;
|
||||
|
||||
protected virtual void OnScaleChanged()
|
||||
{
|
||||
ScaleChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using Artemis.Storage.Entities;
|
||||
|
||||
namespace Artemis.Core.Models.Surface
|
||||
{
|
||||
public class SurfaceConfiguration
|
||||
{
|
||||
internal SurfaceConfiguration(string name)
|
||||
{
|
||||
SurfaceEntity = new SurfaceEntity {SurfacePositions = new List<SurfacePositionEntity>()};
|
||||
Guid = System.Guid.NewGuid().ToString();
|
||||
|
||||
Name = name;
|
||||
IsActive = false;
|
||||
DeviceConfigurations = new List<SurfaceDeviceConfiguration>();
|
||||
|
||||
ApplyToEntity();
|
||||
}
|
||||
|
||||
internal SurfaceConfiguration(SurfaceEntity surfaceEntity)
|
||||
{
|
||||
SurfaceEntity = surfaceEntity;
|
||||
Guid = surfaceEntity.Guid;
|
||||
|
||||
Name = surfaceEntity.Name;
|
||||
IsActive = surfaceEntity.IsActive;
|
||||
DeviceConfigurations = new List<SurfaceDeviceConfiguration>();
|
||||
|
||||
if (surfaceEntity.SurfacePositions == null)
|
||||
return;
|
||||
foreach (var position in surfaceEntity.SurfacePositions)
|
||||
DeviceConfigurations.Add(new SurfaceDeviceConfiguration(position, this));
|
||||
}
|
||||
|
||||
internal SurfaceEntity SurfaceEntity { get; set; }
|
||||
internal string Guid { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
public bool IsActive { get; internal set; }
|
||||
public List<SurfaceDeviceConfiguration> DeviceConfigurations { get; internal set; }
|
||||
|
||||
internal void ApplyToEntity()
|
||||
{
|
||||
SurfaceEntity.Guid = Guid;
|
||||
SurfaceEntity.Name = Name;
|
||||
SurfaceEntity.IsActive = IsActive;
|
||||
}
|
||||
|
||||
internal void Destroy()
|
||||
{
|
||||
SurfaceEntity = null;
|
||||
|
||||
foreach (var deviceConfiguration in DeviceConfigurations)
|
||||
deviceConfiguration.Destroy();
|
||||
DeviceConfigurations.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
using System.Linq;
|
||||
using Artemis.Storage.Entities;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace Artemis.Core.Models.Surface
|
||||
{
|
||||
public class SurfaceDeviceConfiguration
|
||||
{
|
||||
internal SurfaceDeviceConfiguration(IRGBDevice device, int deviceId, SurfaceConfiguration surface)
|
||||
{
|
||||
PositionEntity = new SurfacePositionEntity();
|
||||
Guid = System.Guid.NewGuid().ToString();
|
||||
|
||||
Device = device;
|
||||
DeviceId = deviceId;
|
||||
DeviceName = device.DeviceInfo.DeviceName;
|
||||
DeviceModel = device.DeviceInfo.Model;
|
||||
DeviceManufacturer = device.DeviceInfo.Manufacturer;
|
||||
|
||||
X = device.Location.X;
|
||||
Y = device.Location.Y;
|
||||
Rotation = 0;
|
||||
ZIndex = 1;
|
||||
|
||||
Surface = surface;
|
||||
|
||||
ApplyToEntity();
|
||||
}
|
||||
|
||||
internal SurfaceDeviceConfiguration(SurfacePositionEntity positionEntity, SurfaceConfiguration surfaceConfiguration)
|
||||
{
|
||||
PositionEntity = positionEntity;
|
||||
Guid = positionEntity.Guid;
|
||||
|
||||
DeviceId = positionEntity.DeviceId;
|
||||
DeviceName = positionEntity.DeviceName;
|
||||
DeviceModel = positionEntity.DeviceModel;
|
||||
DeviceManufacturer = positionEntity.DeviceManufacturer;
|
||||
|
||||
X = positionEntity.X;
|
||||
Y = positionEntity.Y;
|
||||
Rotation = positionEntity.Rotation;
|
||||
ZIndex = positionEntity.ZIndex;
|
||||
|
||||
Surface = surfaceConfiguration;
|
||||
}
|
||||
|
||||
internal SurfacePositionEntity PositionEntity { get; set; }
|
||||
internal string Guid { get; }
|
||||
|
||||
public IRGBDevice Device { get; internal set; }
|
||||
public int DeviceId { get; }
|
||||
public string DeviceName { get; }
|
||||
public string DeviceModel { get; }
|
||||
public string DeviceManufacturer { get; }
|
||||
|
||||
public double X { get; set; }
|
||||
public double Y { get; set; }
|
||||
public double Rotation { get; set; }
|
||||
public int ZIndex { get; set; }
|
||||
|
||||
public SurfaceConfiguration Surface { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Applies the configuration to the device
|
||||
/// </summary>
|
||||
internal void ApplyToDevice()
|
||||
{
|
||||
if (Device != null)
|
||||
{
|
||||
Device.Location = new Point(X, Y);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Must be called when saving to the database
|
||||
/// </summary>
|
||||
internal void ApplyToEntity()
|
||||
{
|
||||
PositionEntity.Guid = Guid;
|
||||
PositionEntity.SurfaceId = Surface.Guid;
|
||||
|
||||
PositionEntity.DeviceId = DeviceId;
|
||||
PositionEntity.DeviceName = DeviceName;
|
||||
PositionEntity.DeviceModel = DeviceModel;
|
||||
PositionEntity.DeviceManufacturer = DeviceManufacturer;
|
||||
|
||||
PositionEntity.X = X;
|
||||
PositionEntity.Y = Y;
|
||||
PositionEntity.Rotation = Rotation;
|
||||
PositionEntity.ZIndex = ZIndex;
|
||||
|
||||
// Ensure the position entity is in the surface entity's' collection of positions
|
||||
if (Surface.SurfaceEntity.SurfacePositions.All(p => p.Guid != Guid))
|
||||
Surface.SurfaceEntity.SurfacePositions.Add(PositionEntity);
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
PositionEntity = null;
|
||||
Device = null;
|
||||
Surface = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user