1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00
Artemis/src/Artemis.Core/Services/CoreService.cs
SpoinkyNL 1146de1fc5 Started layout editor implementation
Rearranged VMs/Views
Updated RGB.NET
2019-08-11 01:23:13 +02:00

88 lines
2.4 KiB
C#

using System;
using System.Threading.Tasks;
using Artemis.Core.Exceptions;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Services.Interfaces;
using RGB.NET.Core;
using Color = System.Drawing.Color;
namespace Artemis.Core.Services
{
/// <summary>
/// Provides Artemis's core update loop
/// </summary>
public class CoreService : ICoreService
{
private readonly IPluginService _pluginService;
private readonly IRgbService _rgbService;
internal CoreService(IPluginService pluginService, IRgbService rgbService)
{
_pluginService = pluginService;
_rgbService = rgbService;
_rgbService.Surface.Updating += SurfaceOnUpdating;
Task.Run(Initialize);
}
public void Dispose()
{
// Dispose services
_pluginService.Dispose();
}
public bool IsInitialized { get; set; }
private async Task Initialize()
{
if (IsInitialized)
throw new ArtemisCoreException("Cannot initialize the core as it is already initialized.");
// Initialize the services
await Task.Run(() => _pluginService.LoadPlugins());
await _rgbService.LoadDevices();
OnInitialized();
}
private void SurfaceOnUpdating(UpdatingEventArgs args)
{
try
{
var modules = _pluginService.GetPluginsOfType<Module>();
// Update all active modules
foreach (var module in modules)
module.Update(args.DeltaTime);
if (_rgbService.GraphicsDecorator == null)
return;
// Render all active modules
using (var g = _rgbService.GraphicsDecorator.GetGraphics())
{
g.Clear(Color.Red);
foreach (var module in modules)
module.Render(args.DeltaTime, _rgbService.Surface, g);
}
}
catch (Exception e)
{
throw new ArtemisCoreException("Exception during update", e);
}
}
#region Events
public event EventHandler Initialized;
private void OnInitialized()
{
IsInitialized = true;
Initialized?.Invoke(this, EventArgs.Empty);
}
#endregion
}
}