Made devices aware of the plugin that loaded them

Fixed Corsair LL Fans layout
Expanded render events
Added identify option to device settings, must move this to be reusable though
This commit is contained in:
SpoinkyNL committed 2019-11-03 11:38:28 +01:00
1 parent ae41c6cac3
commit 05eed5ba00
23 files changed
+400 -229

No files matched your search

+2 -1
View File
@@ -144,7 +144,8 @@
<Compile Include="Constants.cs" />
<Compile Include="Events\DeviceConfigurationEventArgs.cs" />
<Compile Include="Events\DeviceEventArgs.cs" />
<Compile Include="Events\FrameEventArgs.cs" />
<Compile Include="Events\FrameRenderedEventArgs.cs" />
<Compile Include="Events\FrameRenderingEventArgs.cs" />
<Compile Include="Exceptions\ArtemisCoreException.cs" />
<Compile Include="Extensions\DirectoryInfoExtensions.cs" />
<Compile Include="Extensions\RgbColorExtensions.cs" />
@@ -0,0 +1,18 @@
using System;
using System.Drawing;
using RGB.NET.Core;
namespace Artemis.Core.Events
{
public class FrameRenderedEventArgs : EventArgs
{
public FrameRenderedEventArgs(Bitmap bitmap, RGBSurface rgbSurface)
{
Bitmap = bitmap;
RgbSurface = rgbSurface;
}
public Bitmap Bitmap { get; }
public RGBSurface RgbSurface { get; }
}
}
@@ -6,9 +6,9 @@ using RGB.NET.Core;
namespace Artemis.Core.Events
{
public class FrameEventArgs : EventArgs
public class FrameRenderingEventArgs : EventArgs
{
public FrameEventArgs(List<Module> modules, Bitmap bitmap, double deltaTime, RGBSurface rgbSurface)
public FrameRenderingEventArgs(List<Module> modules, Bitmap bitmap, double deltaTime, RGBSurface rgbSurface)
{
Modules = modules;
Bitmap = bitmap;
+15 -3
View File
@@ -1,18 +1,23 @@
using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using Artemis.Core.Extensions;
using Artemis.Core.Plugins.Abstract;
using Artemis.Storage.Entities;
using RGB.NET.Core;
using Point = RGB.NET.Core.Point;
using Rectangle = System.Drawing.Rectangle;
namespace Artemis.Core.Models.Surface
{
public class Device
{
internal Device(IRGBDevice rgbDevice, Surface surface)
internal Device(IRGBDevice rgbDevice, Plugin plugin, Surface surface)
{
RgbDevice = rgbDevice;
Plugin = plugin;
Surface = surface;
Configuration = new DeviceEntity();
Leds = rgbDevice.Select(l => new DeviceLed(l, this)).ToList().AsReadOnly();
@@ -24,9 +29,10 @@ namespace Artemis.Core.Models.Surface
CalculateRenderRectangle();
}
internal Device(IRGBDevice rgbDevice, Surface surface, DeviceEntity configuration)
internal Device(IRGBDevice rgbDevice, Plugin plugin, Surface surface, DeviceEntity configuration)
{
RgbDevice = rgbDevice;
Plugin = plugin;
Surface = surface;
Configuration = configuration;
Leds = rgbDevice.Select(l => new DeviceLed(l, this)).ToList().AsReadOnly();
@@ -36,8 +42,10 @@ namespace Artemis.Core.Models.Surface
}
public Rectangle RenderRectangle { get; private set; }
public GraphicsPath RenderPath { get; private set; }
public IRGBDevice RgbDevice { get; private set; }
public Plugin Plugin { get; }
public Surface Surface { get; private set; }
public DeviceEntity Configuration { get; private set; }
public ReadOnlyCollection<DeviceLed> Leds { get; set; }
@@ -93,8 +101,12 @@ namespace Artemis.Core.Models.Surface
foreach (var led in Leds)
led.CalculateRenderRectangle();
}
var path = new GraphicsPath();
path.AddRectangles(Leds.Select(l => l.AbsoluteRenderRectangle).ToArray());
RenderPath = path;
}
internal void Destroy()
{
Configuration = null;
@@ -33,16 +33,6 @@ namespace Artemis.Core.Models.Surface
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; }
+7 -2
View File
@@ -1,4 +1,6 @@
using Artemis.Core.Plugins.Models;
using System;
using Artemis.Core.Plugins.Models;
using RGB.NET.Core;
namespace Artemis.Core.Plugins.Abstract
{
@@ -8,8 +10,11 @@ namespace Artemis.Core.Plugins.Abstract
/// </summary>
public abstract class Device : Plugin
{
protected Device(PluginInfo pluginInfo) : base(pluginInfo)
public IRGBDeviceProvider DeviceProvider { get; }
protected Device(PluginInfo pluginInfo, IRGBDeviceProvider deviceProvider) : base(pluginInfo)
{
DeviceProvider = deviceProvider ?? throw new ArgumentNullException(nameof(deviceProvider));
}
}
}
+1 -1
View File
@@ -12,7 +12,7 @@ namespace Artemis.Core.Plugins.Abstract
{
internal Plugin(PluginInfo pluginInfo)
{
PluginInfo = pluginInfo;
PluginInfo = pluginInfo ?? throw new ArgumentNullException(nameof(pluginInfo));
}
public PluginInfo PluginInfo { get; internal set; }
+15 -4
View File
@@ -29,7 +29,7 @@ namespace Artemis.Core.Services
_rgbService = rgbService;
_surfaceService = surfaceService;
_rgbService.Surface.Updating += SurfaceOnUpdating;
_rgbService.Surface.Updated += SurfaceOnUpdated;
Task.Run(Initialize);
}
@@ -87,7 +87,7 @@ namespace Artemis.Core.Services
module.Render(args.DeltaTime, _surfaceService.ActiveSurface, g);
}
OnFrameRendered(new FrameEventArgs(modules, _rgbService.GraphicsDecorator.GetBitmap(), args.DeltaTime, _rgbService.Surface));
OnFrameRendering(new FrameRenderingEventArgs(modules, _rgbService.GraphicsDecorator.GetBitmap(), args.DeltaTime, _rgbService.Surface));
}
catch (Exception e)
{
@@ -95,10 +95,16 @@ namespace Artemis.Core.Services
}
}
private void SurfaceOnUpdated(UpdatedEventArgs args)
{
OnFrameRendered(new FrameRenderedEventArgs(_rgbService.GraphicsDecorator.GetBitmap(), _rgbService.Surface));
}
#region Events
public event EventHandler Initialized;
public event EventHandler<FrameEventArgs> FrameRendered;
public event EventHandler<FrameRenderingEventArgs> FrameRendering;
public event EventHandler<FrameRenderedEventArgs> FrameRendered;
private void OnInitialized()
{
@@ -108,7 +114,12 @@ namespace Artemis.Core.Services
#endregion
protected virtual void OnFrameRendered(FrameEventArgs e)
protected virtual void OnFrameRendering(FrameRenderingEventArgs e)
{
FrameRendering?.Invoke(this, e);
}
protected virtual void OnFrameRendered(FrameRenderedEventArgs e)
{
FrameRendered?.Invoke(this, e);
}
@@ -16,8 +16,13 @@ namespace Artemis.Core.Services.Interfaces
event EventHandler Initialized;
/// <summary>
/// Occurs whenever a frame has finished rendering
/// Occurs whenever a frame is rendering, after modules have rendered
/// </summary>
event EventHandler<FrameEventArgs> FrameRendered;
event EventHandler<FrameRenderingEventArgs> FrameRendering;
/// <summary>
/// Occurs whenever a frame is finished rendering and processed by RGB.NET
/// </summary>
event EventHandler<FrameRenderedEventArgs> FrameRendered;
}
}
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using Artemis.Core.Events;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Models;
using RGB.NET.Core;
namespace Artemis.Core.Services.Interfaces
{
@@ -91,5 +92,7 @@ namespace Artemis.Core.Services.Interfaces
event EventHandler<PluginEventArgs> PluginDisabled;
#endregion
Plugin GetDevicePlugin(IRGBDevice device);
}
}
+7 -1
View File
@@ -14,6 +14,7 @@ using Newtonsoft.Json;
using Ninject;
using Ninject.Extensions.ChildKernel;
using Ninject.Parameters;
using RGB.NET.Core;
namespace Artemis.Core.Services
{
@@ -102,7 +103,7 @@ namespace Artemis.Core.Services
// Create a child kernel and app domain that will only contain the plugins
_childKernel = new ChildKernel(_kernel);
// Load the plugin assemblies into the plugin context
var pluginDirectory = new DirectoryInfo(Path.Combine(Constants.DataFolder, "plugins"));
foreach (var subDirectory in pluginDirectory.EnumerateDirectories())
@@ -282,6 +283,11 @@ namespace Artemis.Core.Services
}
}
public Plugin GetDevicePlugin(IRGBDevice rgbDevice)
{
return GetPluginsOfType<Device>().First(d => d.DeviceProvider.Devices.Contains(rgbDevice));
}
public void Dispose()
{
UnloadPlugins();
@@ -18,15 +18,17 @@ namespace Artemis.Core.Services.Storage
{
private readonly ILogger _logger;
private readonly IRgbService _rgbService;
private readonly IPluginService _pluginService;
private readonly List<Surface> _surfaceConfigurations;
private readonly ISurfaceRepository _surfaceRepository;
private readonly PluginSetting<double> _renderScaleSetting;
internal SurfaceService(ILogger logger, ISurfaceRepository surfaceRepository, IRgbService rgbService, ISettingsService settingsService)
internal SurfaceService(ILogger logger, ISurfaceRepository surfaceRepository, IRgbService rgbService, IPluginService pluginService, ISettingsService settingsService)
{
_logger = logger;
_surfaceRepository = surfaceRepository;
_rgbService = rgbService;
_pluginService = pluginService;
_surfaceConfigurations = new List<Surface>();
_renderScaleSetting = settingsService.GetSetting("RenderScale", 1.0);
@@ -36,8 +38,6 @@ namespace Artemis.Core.Services.Storage
_renderScaleSetting.SettingChanged += RenderScaleSettingOnSettingChanged;
}
public Surface ActiveSurface { get; private set; }
public ReadOnlyCollection<Surface> SurfaceConfigurations => _surfaceConfigurations.AsReadOnly();
@@ -47,8 +47,11 @@ namespace Artemis.Core.Services.Storage
var configuration = new Surface(_rgbService.Surface, name, _renderScaleSetting.Value);
// Add all current devices
foreach (var rgbDevice in _rgbService.LoadedDevices)
configuration.Devices.Add(new Device(rgbDevice, configuration));
foreach (var rgbDevice in _rgbService.LoadedDevices)
{
var plugin = _pluginService.GetDevicePlugin(rgbDevice);
configuration.Devices.Add(new Device(rgbDevice, plugin, configuration));
}
lock (_surfaceConfigurations)
{
@@ -140,9 +143,8 @@ namespace Artemis.Core.Services.Storage
private void RenderScaleSettingOnSettingChanged(object sender, EventArgs e)
{
foreach (var surfaceConfiguration in SurfaceConfigurations)
foreach (var surfaceConfiguration in SurfaceConfigurations)
surfaceConfiguration.UpdateScale(_renderScaleSetting.Value);
}
#endregion
@@ -156,6 +158,15 @@ namespace Artemis.Core.Services.Storage
{
// Create the surface entity
var surfaceConfiguration = new Surface(_rgbService.Surface, surfaceEntity, _renderScaleSetting.Value);
foreach (var position in surfaceEntity.DeviceEntities)
{
var device = _rgbService.Surface.Devices.FirstOrDefault(d => d.GetDeviceHashCode() == position.DeviceHashCode);
if (device != null)
{
var plugin = _pluginService.GetDevicePlugin(device);
surfaceConfiguration.Devices.Add(new Device(device, plugin, surfaceConfiguration, position));
}
}
// Finally, add the surface config to the collection
lock (_surfaceConfigurations)
@@ -189,11 +200,14 @@ namespace Artemis.Core.Services.Storage
if (device != null)
return;
// Find an existing device config and use that
var existingDeviceConfig = surface.SurfaceEntity.DeviceEntities.FirstOrDefault(d => d.DeviceHashCode == deviceHashCode);
if (existingDeviceConfig != null)
device = new Device(rgbDevice,surface, existingDeviceConfig);
if (existingDeviceConfig != null)
{
var plugin = _pluginService.GetDevicePlugin(rgbDevice);
device = new Device(rgbDevice, plugin, surface, existingDeviceConfig);
}
// Fall back on creating a new device
else
{
@@ -202,7 +216,8 @@ namespace Artemis.Core.Services.Storage
rgbDevice.DeviceInfo,
deviceHashCode
);
device = new Device(rgbDevice, surface);
var plugin = _pluginService.GetDevicePlugin(rgbDevice);
device = new Device(rgbDevice, plugin, surface);
}
surface.Devices.Add(device);