Core - Brought device load/save logic in line with the rest of the code

This commit is contained in:
Robert committed 2023-10-14 17:20:22 +02:00
1 parent 46c035851a
commit 76d424012a
11 files changed
+161 -104

No files matched your search

@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using Artemis.Core.DeviceProviders;
using Artemis.Core.Services;
using Artemis.Storage.Entities.Surface;
using RGB.NET.Core;
using SkiaSharp;
@@ -44,12 +44,16 @@ public class ArtemisDevice : CorePropertyChanged
InputIdentifiers = new List<ArtemisDeviceInputIdentifier>();
InputMappings = new Dictionary<ArtemisLed, ArtemisLed>();
Categories = new HashSet<DeviceCategory>();
UpdateLeds();
RgbDevice.ColorCorrections.Clear();
RgbDevice.ColorCorrections.Add(new ScaleColorCorrection(this));
UpdateLeds(false);
ApplyKeyboardLayout();
ApplyToEntity();
ApplyDefaultCategories();
CalculateRenderProperties();
Save();
RgbDevice.PropertyChanged += RgbDeviceOnPropertyChanged;
}
internal ArtemisDevice(IRGBDevice rgbDevice, DeviceProvider deviceProvider, DeviceEntity deviceEntity)
@@ -71,11 +75,18 @@ public class ArtemisDevice : CorePropertyChanged
foreach (DeviceInputIdentifierEntity identifierEntity in DeviceEntity.InputIdentifiers)
InputIdentifiers.Add(new ArtemisDeviceInputIdentifier(identifierEntity.InputProvider, identifierEntity.Identifier));
UpdateLeds();
RgbDevice.ColorCorrections.Clear();
RgbDevice.ColorCorrections.Add(new ScaleColorCorrection(this));
UpdateLeds(false);
Load();
ApplyKeyboardLayout();
CalculateRenderProperties();
RgbDevice.PropertyChanged += RgbDeviceOnPropertyChanged;
}
/// <summary>
/// Gets the (hopefully unique and persistent) ID of this device
/// </summary>
@@ -149,6 +160,8 @@ public class ArtemisDevice : CorePropertyChanged
set
{
DeviceEntity.X = value;
if (RgbDevice.Surface != null)
ApplyLocation(DeviceEntity.X, DeviceEntity.Y);
OnPropertyChanged(nameof(X));
}
}
@@ -162,6 +175,8 @@ public class ArtemisDevice : CorePropertyChanged
set
{
DeviceEntity.Y = value;
if (RgbDevice.Surface != null)
ApplyLocation(DeviceEntity.X, DeviceEntity.Y);
OnPropertyChanged(nameof(Y));
}
}
@@ -175,6 +190,8 @@ public class ArtemisDevice : CorePropertyChanged
set
{
DeviceEntity.Rotation = value;
if (RgbDevice.Surface != null)
RgbDevice.Rotation = DeviceEntity.Rotation;
OnPropertyChanged(nameof(Rotation));
}
}
@@ -188,6 +205,8 @@ public class ArtemisDevice : CorePropertyChanged
set
{
DeviceEntity.Scale = value;
if (RgbDevice.Surface != null)
RgbDevice.Scale = DeviceEntity.Scale;
OnPropertyChanged(nameof(Scale));
}
}
@@ -412,8 +431,7 @@ public class ArtemisDevice : CorePropertyChanged
case RGBDeviceType.Mousepad:
case RGBDeviceType.HeadsetStand:
case RGBDeviceType.Keypad:
if (!Categories.Contains(DeviceCategory.Peripherals))
Categories.Add(DeviceCategory.Peripherals);
Categories.Add(DeviceCategory.Peripherals);
break;
case RGBDeviceType.Mainboard:
case RGBDeviceType.GraphicsCard:
@@ -421,20 +439,16 @@ public class ArtemisDevice : CorePropertyChanged
case RGBDeviceType.Fan:
case RGBDeviceType.LedStripe:
case RGBDeviceType.Cooler:
if (!Categories.Contains(DeviceCategory.Case))
Categories.Add(DeviceCategory.Case);
Categories.Add(DeviceCategory.Case);
break;
case RGBDeviceType.Speaker:
if (!Categories.Contains(DeviceCategory.Desk))
Categories.Add(DeviceCategory.Desk);
Categories.Add(DeviceCategory.Desk);
break;
case RGBDeviceType.Monitor:
if (!Categories.Contains(DeviceCategory.Monitor))
Categories.Add(DeviceCategory.Monitor);
Categories.Add(DeviceCategory.Monitor);
break;
case RGBDeviceType.LedMatrix:
if (!Categories.Contains(DeviceCategory.Room))
Categories.Add(DeviceCategory.Room);
Categories.Add(DeviceCategory.Room);
break;
}
}
@@ -464,10 +478,9 @@ public class ArtemisDevice : CorePropertyChanged
if (layout == null)
{
ClearLayout();
UpdateLeds();
UpdateLeds(true);
CalculateRenderProperties();
OnDeviceUpdated();
return;
}
@@ -481,13 +494,12 @@ public class ArtemisDevice : CorePropertyChanged
ClearLayout();
if (layout.IsValid)
layout.ApplyTo(RgbDevice, createMissingLeds, removeExcessiveLeds);
UpdateLeds();
UpdateLeds(true);
Layout = layout;
Layout.ApplyDevice(this);
CalculateRenderProperties();
OnDeviceUpdated();
}
private void ClearLayout()
@@ -505,7 +517,7 @@ public class ArtemisDevice : CorePropertyChanged
RgbDevice.AddLed(originalLed.Id, originalLed.Location, originalLed.Size, originalLed.CustomData);
}
internal void ApplyToEntity()
internal void Save()
{
// Other properties are computed
DeviceEntity.Id = Identifier;
@@ -513,48 +525,30 @@ public class ArtemisDevice : CorePropertyChanged
DeviceEntity.InputIdentifiers.Clear();
foreach (ArtemisDeviceInputIdentifier identifier in InputIdentifiers)
{
DeviceEntity.InputIdentifiers.Add(new DeviceInputIdentifierEntity
{
InputProvider = identifier.InputProvider,
Identifier = identifier.Identifier
});
}
DeviceEntity.InputIdentifiers.Add(new DeviceInputIdentifierEntity {InputProvider = identifier.InputProvider, Identifier = identifier.Identifier});
DeviceEntity.InputMappings.Clear();
foreach ((ArtemisLed? original, ArtemisLed? mapped) in InputMappings)
DeviceEntity.InputMappings.Add(new InputMappingEntity {OriginalLedId = (int) original.RgbLed.Id, MappedLedId = (int) mapped.RgbLed.Id});
DeviceEntity.Categories.Clear();
foreach (DeviceCategory deviceCategory in Categories)
DeviceEntity.Categories.Add((int) deviceCategory);
}
internal void ApplyToRgbDevice()
internal void Load()
{
RgbDevice.Rotation = DeviceEntity.Rotation;
RgbDevice.Scale = DeviceEntity.Scale;
// Workaround for device rotation not applying
if (DeviceEntity.X == 0 && DeviceEntity.Y == 0)
RgbDevice.Location = new Point(1, 1);
RgbDevice.Location = new Point(DeviceEntity.X, DeviceEntity.Y);
InputIdentifiers.Clear();
foreach (DeviceInputIdentifierEntity identifierEntity in DeviceEntity.InputIdentifiers)
InputIdentifiers.Add(new ArtemisDeviceInputIdentifier(identifierEntity.InputProvider, identifierEntity.Identifier));
if (!RgbDevice.ColorCorrections.Any())
RgbDevice.ColorCorrections.Add(new ScaleColorCorrection(this));
Categories.Clear();
foreach (int deviceEntityCategory in DeviceEntity.Categories)
Categories.Add((DeviceCategory) deviceEntityCategory);
if (!Categories.Any())
ApplyDefaultCategories();
CalculateRenderProperties();
OnDeviceUpdated();
LoadInputMappings();
}
internal void CalculateRenderProperties()
@@ -571,13 +565,21 @@ public class ArtemisDevice : CorePropertyChanged
path.AddRect(artemisLed.AbsoluteRectangle);
Path = path;
OnDeviceUpdated();
}
private void UpdateLeds()
private void UpdateLeds(bool loadInputMappings)
{
Leds = RgbDevice.Select(l => new ArtemisLed(l, this)).ToList().AsReadOnly();
LedIds = new ReadOnlyDictionary<LedId, ArtemisLed>(Leds.ToDictionary(l => l.RgbLed.Id, l => l));
if (loadInputMappings)
LoadInputMappings();
}
private void LoadInputMappings()
{
InputMappings.Clear();
foreach (InputMappingEntity deviceEntityInputMapping in DeviceEntity.InputMappings)
{
@@ -604,6 +606,27 @@ public class ArtemisDevice : CorePropertyChanged
else
LogicalLayout = DeviceEntity.LogicalLayout;
}
private void RgbDeviceOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != nameof(IRGBDevice.Surface))
return;
RgbDevice.Rotation = DeviceEntity.Rotation;
RgbDevice.Scale = DeviceEntity.Scale;
ApplyLocation(DeviceEntity.X, DeviceEntity.Y);
}
private void ApplyLocation(float x, float y)
{
// Workaround for device rotation not applying
if (x == 0 && y == 0)
RgbDevice.Location = new Point(1, 1);
RgbDevice.Location = new Point(x, y);
UpdateLeds(false);
CalculateRenderProperties();
}
}
/// <summary>
+42
View File
@@ -0,0 +1,42 @@
using Artemis.Core.ScriptingProviders;
using Artemis.Core.Services.Core;
using SkiaSharp;
namespace Artemis.Core.Services;
internal class CoreRenderer : IRenderer
{
private readonly IModuleService _moduleService;
private readonly IScriptingService _scriptingService;
private readonly IProfileService _profileService;
public CoreRenderer(IModuleService moduleService, IScriptingService scriptingService, IProfileService profileService)
{
_moduleService = moduleService;
_scriptingService = scriptingService;
_profileService = profileService;
}
/// <inheritdoc />
public void Render(SKCanvas canvas, double delta)
{
foreach (GlobalScript scriptingServiceGlobalScript in _scriptingService.GlobalScripts)
scriptingServiceGlobalScript.OnCoreUpdating(delta);
_moduleService.UpdateActiveModules(delta);
if (!_profileService.ProfileRenderingDisabled)
{
_profileService.UpdateProfiles(delta);
_profileService.RenderProfiles(canvas);
}
foreach (GlobalScript scriptingServiceGlobalScript in _scriptingService.GlobalScripts)
scriptingServiceGlobalScript.OnCoreUpdated(delta);
}
/// <inheritdoc />
public void PostRender(SKTexture texture)
{
}
}
+4 -11
View File
@@ -175,7 +175,7 @@ internal class DeviceService : IDeviceService
_enabledDevices.Add(device);
device.IsEnabled = true;
device.ApplyToEntity();
device.Save();
_deviceRepository.Save(device.DeviceEntity);
OnDeviceEnabled(new DeviceEventArgs(device));
@@ -190,7 +190,7 @@ internal class DeviceService : IDeviceService
_enabledDevices.Remove(device);
device.IsEnabled = false;
device.ApplyToEntity();
device.Save();
_deviceRepository.Save(device.DeviceEntity);
OnDeviceDisabled(new DeviceEventArgs(device));
@@ -200,9 +200,7 @@ internal class DeviceService : IDeviceService
/// <inheritdoc />
public void SaveDevice(ArtemisDevice artemisDevice)
{
artemisDevice.ApplyToEntity();
artemisDevice.ApplyToRgbDevice();
artemisDevice.Save();
_deviceRepository.Save(artemisDevice.DeviceEntity);
UpdateLeds();
}
@@ -211,11 +209,7 @@ internal class DeviceService : IDeviceService
public void SaveDevices()
{
foreach (ArtemisDevice artemisDevice in _devices)
{
artemisDevice.ApplyToEntity();
artemisDevice.ApplyToRgbDevice();
}
artemisDevice.Save();
_deviceRepository.Save(_devices.Select(d => d.DeviceEntity));
UpdateLeds();
}
@@ -236,7 +230,6 @@ internal class DeviceService : IDeviceService
device = new ArtemisDevice(rgbDevice, deviceProvider);
}
device.ApplyToRgbDevice();
ApplyDeviceLayout(device, device.GetBestDeviceLayout());
return device;
}
+6 -6
View File
@@ -19,22 +19,24 @@ internal class RenderService : IRenderService, IRenderer, IDisposable
private readonly ILogger _logger;
private readonly IDeviceService _deviceService;
private readonly CoreRenderer _coreRenderer;
private readonly LazyEnumerable<IGraphicsContextProvider> _graphicsContextProviders;
private readonly PluginSetting<int> _targetFrameRateSetting;
private readonly PluginSetting<double> _renderScaleSetting;
private readonly PluginSetting<string> _preferredGraphicsContext;
private readonly SurfaceManager _surfaceManager;
private SurfaceManager _surfaceManager;
private int _frames;
private DateTime _lastExceptionLog;
private DateTime _lastFrameRateSample;
private bool _initialized;
public RenderService(ILogger logger, ISettingsService settingsService, IDeviceService deviceService, LazyEnumerable<IGraphicsContextProvider> graphicsContextProviders)
public RenderService(ILogger logger, ISettingsService settingsService, IDeviceService deviceService, CoreRenderer coreRenderer, LazyEnumerable<IGraphicsContextProvider> graphicsContextProviders)
{
_frameStopWatch = new Stopwatch();
_logger = logger;
_deviceService = deviceService;
_coreRenderer = coreRenderer;
_graphicsContextProviders = graphicsContextProviders;
_targetFrameRateSetting = settingsService.GetSetting("Core.TargetFrameRate", 30);
@@ -77,8 +79,7 @@ internal class RenderService : IRenderService, IRenderer, IDisposable
try
{
OnFrameRendering(new FrameRenderingEventArgs(canvas, delta, _surfaceManager.Surface));
foreach (IRenderer renderer in Renderers)
renderer.Render(canvas, delta);
_coreRenderer.Render(canvas, delta);
}
catch (Exception e)
{
@@ -91,8 +92,7 @@ internal class RenderService : IRenderService, IRenderer, IDisposable
{
try
{
foreach (IRenderer renderer in Renderers)
renderer.PostRender(texture);
_coreRenderer.PostRender(texture);
OnFrameRendered(new FrameRenderedEventArgs(texture, _surfaceManager.Surface));
}
catch (Exception e)
@@ -12,7 +12,7 @@ internal class ScriptingService : IScriptingService
private readonly IPluginManagementService _pluginManagementService;
private readonly IProfileService _profileService;
private readonly List<ScriptingProvider> _scriptingProviders;
public ScriptingService(IPluginManagementService pluginManagementService, IProfileService profileService)
{
_pluginManagementService = pluginManagementService;
@@ -27,7 +27,6 @@ internal class SurfaceArrangement
{
surfaceDevice.X = 0;
surfaceDevice.Y = 0;
surfaceDevice.ApplyToRgbDevice();
}
foreach (SurfaceArrangementType surfaceArrangementType in Types)
@@ -37,18 +36,16 @@ internal class SurfaceArrangement
float x = devices.Min(d => d.RgbDevice.Location.X);
float y = devices.Min(d => d.RgbDevice.Location.Y);
if (x < 0)
{
foreach (ArtemisDevice surfaceDevice in devices)
{
surfaceDevice.X += x * -1;
surfaceDevice.ApplyToRgbDevice();
}
}
if (y < 0)
{
foreach (ArtemisDevice surfaceDevice in devices)
{
surfaceDevice.Y += y * -1;
surfaceDevice.ApplyToRgbDevice();
}
}
}
internal static SurfaceArrangement GetDefaultArrangement()
@@ -80,9 +80,7 @@ internal class SurfaceArrangementConfiguration
};
}
artemisDevice.ApplyToRgbDevice();
previous = artemisDevice;
SurfaceArrangement.ArrangedDevices.Add(artemisDevice);
}
@@ -7,7 +7,6 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Artemis.Core.Modules;
using Artemis.Core.Services.Core;
using Artemis.Storage.Entities.Profile;
using Artemis.Storage.Repositories.Interfaces;
using Newtonsoft.Json;
@@ -16,7 +15,7 @@ using SkiaSharp;
namespace Artemis.Core.Services;
internal class ProfileService : IProfileService, IRenderer
internal class ProfileService : IProfileService
{
private readonly ILogger _logger;
private readonly IProfileCategoryRepository _profileCategoryRepository;
@@ -36,7 +35,6 @@ internal class ProfileService : IProfileService, IRenderer
IPluginManagementService pluginManagementService,
IInputService inputService,
IDeviceService deviceService,
IRenderService renderService,
IProfileRepository profileRepository)
{
_logger = logger;
@@ -55,8 +53,6 @@ internal class ProfileService : IProfileService, IRenderer
if (!_profileCategories.Any())
CreateDefaultProfileCategories();
UpdateModules();
renderService.Renderers.Add(this);
}
public ProfileConfiguration? FocusProfile { get; set; }
@@ -187,21 +183,6 @@ internal class ProfileService : IProfileService, IRenderer
}
}
}
/// <inheritdoc />
public void Render(SKCanvas canvas, double delta)
{
if (ProfileRenderingDisabled)
return;
UpdateProfiles(delta);
RenderProfiles(canvas);
}
/// <inheritdoc />
public void PostRender(SKTexture texture)
{
}
/// <inheritdoc />
public void LoadProfileConfigurationIcon(ProfileConfiguration profileConfiguration)