1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Finished profiles refactoring

This commit is contained in:
SpoinkyNL 2016-05-17 13:15:36 +02:00
parent 9d451094cc
commit c8c4143e88
6 changed files with 71 additions and 86 deletions

View File

@ -1,8 +1,7 @@
using System; using System;
using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading; using System.Timers;
using Artemis.Events; using Artemis.Events;
using Artemis.Models; using Artemis.Models;
using Artemis.Modules.Effects.ProfilePreview; using Artemis.Modules.Effects.ProfilePreview;
@ -25,6 +24,7 @@ namespace Artemis.Managers
private readonly IEventAggregator _events; private readonly IEventAggregator _events;
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly Timer _processTimer;
public MainManager(IEventAggregator events, ILogger logger, LoopManager loopManager, public MainManager(IEventAggregator events, ILogger logger, LoopManager loopManager,
KeyboardManager keyboardManager, EffectManager effectManager, ProfileManager profileManager) KeyboardManager keyboardManager, EffectManager effectManager, ProfileManager profileManager)
@ -38,20 +38,16 @@ namespace Artemis.Managers
_logger.Info("Intializing MainManager"); _logger.Info("Intializing MainManager");
_events = events; _events = events;
//DialogService = dialogService; _processTimer = new Timer(1000);
KeyboardHook = new KeyboardHook(); _processTimer.Elapsed += ScanProcesses;
_processTimer.Start();
ProcessWorker = new BackgroundWorker {WorkerSupportsCancellation = true};
ProcessWorker.DoWork += ProcessWorker_DoWork;
ProcessWorker.RunWorkerCompleted += BackgroundWorkerExceptionCatcher;
// Process worker will always run (and just do nothing when ProgramEnabled is false)
ProcessWorker.RunWorkerAsync();
ProgramEnabled = false; ProgramEnabled = false;
Running = false; Running = false;
KeyboardHook = new KeyboardHook(); // TODO: DI
// Create and start the web server // Create and start the web server
GameStateWebServer = new GameStateWebServer(); GameStateWebServer = new GameStateWebServer();
GameStateWebServer.Start(); GameStateWebServer.Start();
@ -72,7 +68,6 @@ namespace Artemis.Managers
public ProfileManager ProfileManager { get; set; } public ProfileManager ProfileManager { get; set; }
public PipeServer PipeServer { get; set; } public PipeServer PipeServer { get; set; }
public BackgroundWorker ProcessWorker { get; set; }
public KeyboardHook KeyboardHook { get; set; } public KeyboardHook KeyboardHook { get; set; }
public GameStateWebServer GameStateWebServer { get; set; } public GameStateWebServer GameStateWebServer { get; set; }
public bool ProgramEnabled { get; private set; } public bool ProgramEnabled { get; private set; }
@ -81,8 +76,10 @@ namespace Artemis.Managers
public void Dispose() public void Dispose()
{ {
_logger.Debug("Shutting down MainManager"); _logger.Debug("Shutting down MainManager");
_processTimer.Stop();
_processTimer.Dispose();
LoopManager.Stop(); LoopManager.Stop();
ProcessWorker.CancelAsync();
GameStateWebServer.Stop(); GameStateWebServer.Stop();
PipeServer.Stop(); PipeServer.Stop();
} }
@ -109,59 +106,46 @@ namespace Artemis.Managers
_events.PublishOnUIThread(new ToggleEnabled(ProgramEnabled)); _events.PublishOnUIThread(new ToggleEnabled(ProgramEnabled));
} }
private void ProcessWorker_DoWork(object sender, DoWorkEventArgs e) /// <summary>
/// Manages active games by keeping an eye on their processes
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScanProcesses(object sender, ElapsedEventArgs e)
{ {
while (!ProcessWorker.CancellationPending) if (!ProgramEnabled)
{
if (!ProgramEnabled)
{
Thread.Sleep(1000);
continue;
}
var runningProcesses = Process.GetProcesses();
// If the currently active effect is a disabled game, get rid of it.
if (EffectManager.ActiveEffect != null)
EffectManager.DisableInactiveGame();
if (EffectManager.ActiveEffect is ProfilePreviewModel)
return;
// If the currently active effect is a no longer running game, get rid of it.
var activeGame = EffectManager.ActiveEffect as GameModel;
if (activeGame != null)
{
if (!runningProcesses.Any(p => p.ProcessName == activeGame.ProcessName && p.HasExited == false))
{
_logger.Info("Disabling game: {0}", activeGame.Name);
EffectManager.DisableGame(activeGame);
}
}
// Look for running games, stopping on the first one that's found.
var newGame = EffectManager.EnabledGames
.FirstOrDefault(g => runningProcesses
.Any(p => p.ProcessName == g.ProcessName && p.HasExited == false));
// If it's not already enabled, do so.
if (newGame != null && EffectManager.ActiveEffect != newGame)
{
_logger.Info("Detected and enabling game: {0}", newGame.Name);
EffectManager.ChangeEffect(newGame, LoopManager);
}
Thread.Sleep(1000);
}
}
private void BackgroundWorkerExceptionCatcher(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error == null)
return; return;
_logger.Error(e.Error, "Exception in the BackgroundWorker"); var runningProcesses = Process.GetProcesses();
throw e.Error;
// If the currently active effect is a disabled game, get rid of it.
if (EffectManager.ActiveEffect != null)
EffectManager.DisableInactiveGame();
if (EffectManager.ActiveEffect is ProfilePreviewModel)
return;
// If the currently active effect is a no longer running game, get rid of it.
var activeGame = EffectManager.ActiveEffect as GameModel;
if (activeGame != null)
{
if (!runningProcesses.Any(p => p.ProcessName == activeGame.ProcessName && p.HasExited == false))
{
_logger.Info("Disabling game: {0}", activeGame.Name);
EffectManager.DisableGame(activeGame);
}
}
// Look for running games, stopping on the first one that's found.
var newGame = EffectManager.EnabledGames
.FirstOrDefault(g => runningProcesses
.Any(p => p.ProcessName == g.ProcessName && p.HasExited == false));
if (newGame == null || EffectManager.ActiveEffect == newGame)
return;
// If it's not already enabled, do so.
_logger.Info("Detected and enabling game: {0}", newGame.Name);
EffectManager.ChangeEffect(newGame, LoopManager);
} }
} }
} }

View File

@ -1,4 +1,5 @@
using System.ComponentModel; using System;
using System.ComponentModel;
using Artemis.Models.Interfaces; using Artemis.Models.Interfaces;
using Artemis.Utilities; using Artemis.Utilities;
using static System.Decimal; using static System.Decimal;
@ -31,10 +32,7 @@ namespace Artemis.Models.Profiles.Properties
/// Type of property /// Type of property
/// </summary> /// </summary>
public LayerPropertyType LayerPropertyType { get; set; } public LayerPropertyType LayerPropertyType { get; set; }
internal void ApplyProperty(IGameDataModel dataModel, KeyboardPropertiesModel properties) internal void ApplyProperty(IGameDataModel dataModel, KeyboardPropertiesModel properties)
{ {
if (LayerPropertyType == LayerPropertyType.PercentageOf) if (LayerPropertyType == LayerPropertyType.PercentageOf)
@ -55,23 +53,24 @@ namespace Artemis.Models.Profiles.Properties
return; return;
var percentage = ToDouble(gameProperty)/percentageSource; var percentage = ToDouble(gameProperty)/percentageSource;
var appliedValue = percentage*(double) layerProp.GetValue(layerProp, null); var appliedValue = percentage*(double)layerProp.GetValue(properties);
// Opacity requires some special treatment as it causes an exception if it's < 0.0 or > 1.0 // Opacity requires some special treatment as it causes an exception if it's < 0.0 or > 1.0
if (LayerProperty == "Opacity") if (LayerProperty == "Opacity")
{ {
appliedValue = percentage;
if (appliedValue < 0.0) if (appliedValue < 0.0)
appliedValue = 0.0; appliedValue = 0.0;
if (appliedValue > 1.0) if (appliedValue > 1.0)
appliedValue = 1.0; appliedValue = 1.0;
} }
layerProp.SetValue(layerProp, appliedValue); layerProp.SetValue(properties, appliedValue);
} }
private void ApplyPercentageOfProperty(IGameDataModel dataModel, KeyboardPropertiesModel properties) private void ApplyPercentageOfProperty(IGameDataModel dataModel, KeyboardPropertiesModel properties)
{ {
var value = dataModel.GetPropValue<double>(PercentageProperty); var value = dataModel.GetPropValue<int>(PercentageProperty);
ApplyPercentageOf(dataModel, properties, value); ApplyPercentageOf(dataModel, properties, value);
} }
} }

View File

@ -52,20 +52,23 @@ namespace Artemis.Utilities.Layers
private static void DrawRectangle(DrawingContext c, KeyboardPropertiesModel properties, Rect rectangle, private static void DrawRectangle(DrawingContext c, KeyboardPropertiesModel properties, Rect rectangle,
Rect slide1, Rect slide2) Rect slide1, Rect slide2)
{ {
var brush = properties.Brush.CloneCurrentValue();
brush.Opacity = properties.Opacity;
// TODO: Implement clipping modes // TODO: Implement clipping modes
// Most animation types can be drawn regularly // Most animation types can be drawn regularly
if (properties.Animation == LayerAnimation.None || if (properties.Animation == LayerAnimation.None ||
properties.Animation == LayerAnimation.Grow || properties.Animation == LayerAnimation.Grow ||
properties.Animation == LayerAnimation.Pulse) properties.Animation == LayerAnimation.Pulse)
{ {
c.DrawRectangle(properties.Brush, null, rectangle); c.DrawRectangle(brush, null, rectangle);
} }
// Sliding animations however, require offsetting two rects // Sliding animations however, require offsetting two rects
else else
{ {
c.PushClip(new RectangleGeometry(rectangle)); c.PushClip(new RectangleGeometry(rectangle));
c.DrawRectangle(properties.Brush, null, slide1); c.DrawRectangle(brush, null, slide1);
c.DrawRectangle(properties.Brush, null, slide2); c.DrawRectangle(brush, null, slide2);
c.Pop(); c.Pop();
} }
} }

View File

@ -57,9 +57,10 @@ namespace Artemis.ViewModels.LayerEditor
public override LayerPropertiesModel GetAppliedProperties() public override LayerPropertiesModel GetAppliedProperties()
{ {
HeightProperties.Apply();
WidthProperties.Apply(); HeightProperties.Apply(ProposedProperties);
OpacityProperties.Apply(); WidthProperties.Apply(ProposedProperties);
OpacityProperties.Apply(ProposedProperties);
return GeneralHelpers.Clone(ProposedProperties); return GeneralHelpers.Clone(ProposedProperties);
} }

View File

@ -8,7 +8,6 @@ namespace Artemis.ViewModels.LayerEditor
{ {
public sealed class LayerDynamicPropertiesViewModel : PropertyChangedBase public sealed class LayerDynamicPropertiesViewModel : PropertyChangedBase
{ {
private readonly KeyboardPropertiesModel _keyboardProperties;
private readonly string _property; private readonly string _property;
private DynamicPropertiesModel _proposed; private DynamicPropertiesModel _proposed;
private LayerPropertyType _layerPropertyType; private LayerPropertyType _layerPropertyType;
@ -21,11 +20,10 @@ namespace Artemis.ViewModels.LayerEditor
public LayerDynamicPropertiesViewModel(string property, BindableCollection<GeneralHelpers.PropertyCollection> dataModelProps, KeyboardPropertiesModel keyboardProperties) public LayerDynamicPropertiesViewModel(string property, BindableCollection<GeneralHelpers.PropertyCollection> dataModelProps, KeyboardPropertiesModel keyboardProperties)
{ {
_property = property; _property = property;
_keyboardProperties = keyboardProperties;
// Look for the existing property model // Look for the existing property model
Proposed = new DynamicPropertiesModel(); Proposed = new DynamicPropertiesModel();
var original = _keyboardProperties.DynamicProperties.FirstOrDefault(lp => lp.LayerProperty == _property); var original = keyboardProperties.DynamicProperties.FirstOrDefault(lp => lp.LayerProperty == _property);
if (original == null) if (original == null)
{ {
Proposed.LayerProperty = property; Proposed.LayerProperty = property;
@ -142,13 +140,13 @@ namespace Artemis.ViewModels.LayerEditor
} }
} }
public void Apply() public void Apply(KeyboardPropertiesModel keyboardProperties)
{ {
var original = _keyboardProperties.DynamicProperties.FirstOrDefault(lp => lp.LayerProperty == _property); var original = keyboardProperties.DynamicProperties.FirstOrDefault(lp => lp.LayerProperty == _property);
if (original == null) if (original != null)
_keyboardProperties.DynamicProperties.Add(Proposed); keyboardProperties.DynamicProperties.Remove(original);
else
GeneralHelpers.CopyProperties(original, Proposed); keyboardProperties.DynamicProperties.Add(Proposed);
} }
} }
} }