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

Updated keyboard hook and TypeWave, fixes #19

This commit is contained in:
SpoinkyNL 2016-02-17 17:32:19 +01:00
parent 327bd928da
commit ef270a1aa4
6 changed files with 76 additions and 43 deletions

View File

@ -101,6 +101,10 @@
<HintPath>..\packages\CUE.NET.1.0.0\lib\net45\CUE.NET.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Gma.System.MouseKeyHook, Version=5.4.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MouseKeyHook.5.4.0\lib\net40\Gma.System.MouseKeyHook.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Hardcodet.Wpf.TaskbarNotification, Version=1.0.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Hardcodet.NotifyIcon.Wpf.1.0.5\lib\net451\Hardcodet.Wpf.TaskbarNotification.dll</HintPath>
<Private>True</Private>
@ -127,10 +131,6 @@
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Open.WinKeyboardHook, Version=1.0.10.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Open.WinKeyboardHook.1.0.10.0\lib\net45\Open.WinKeyboardHook.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
@ -270,6 +270,7 @@
<Compile Include="Utilities\GameState\GameDataReceivedEventArgs.cs" />
<Compile Include="Utilities\GameState\GameStateWebServer.cs" />
<Compile Include="Utilities\ImageUtilities.cs" />
<Compile Include="Utilities\Keyboard\KeyboardHook.cs" />
<Compile Include="Utilities\Memory\Memory.cs" />
<Compile Include="Utilities\Memory\MemoryHelpers.cs" />
<Compile Include="Utilities\Memory\Win32.cs" />

View File

@ -8,6 +8,7 @@ using Artemis.Events;
using Artemis.KeyboardProviders;
using Artemis.Settings;
using Artemis.Utilities.GameState;
using Artemis.Utilities.Keyboard;
using Artemis.Utilities.Memory;
using Caliburn.Micro;
@ -23,6 +24,7 @@ namespace Artemis.Models
EffectModels = new List<EffectModel>();
KeyboardProviders = ProviderHelper.GetKeyboardProviders();
GameStateWebServer = new GameStateWebServer();
KeyboardHook = new KeyboardHook();
Events = events;
Fps = 25;
@ -33,6 +35,8 @@ namespace Artemis.Models
_processWorker.DoWork += ProcessWorker_DoWork;
}
public KeyboardHook KeyboardHook { get; set; }
public EffectModel ActiveEffect { get; set; }
public KeyboardProvider ActiveKeyboard { get; set; }
public List<EffectModel> EffectModels { get; set; }

View File

@ -7,65 +7,66 @@ using System.Windows.Forms;
using Artemis.KeyboardProviders.Logitech.Utilities;
using Artemis.Models;
using Artemis.Utilities;
using Open.WinKeyboardHook;
using Gma.System.MouseKeyHook;
namespace Artemis.Modules.Effects.TypeWave
{
public class TypeWaveModel : EffectModel
{
private readonly List<Wave> _waves;
private Color _randomColor;
public TypeWaveModel(MainModel mainModel, TypeWaveSettings settings) : base(mainModel)
{
Name = "TypeWave";
Waves = new List<Wave>();
_waves = new List<Wave>();
_randomColor = Color.Red;
Settings = settings;
// KeyboardIntercepter won't start untill the effect is active
KeyboardInterceptor = new KeyboardInterceptor();
}
public TypeWaveSettings Settings { get; set; }
public List<Wave> Waves { get; set; }
public KeyboardInterceptor KeyboardInterceptor { get; set; }
public override void Dispose()
{
KeyboardInterceptor.KeyUp -= HandleKeypress;
KeyboardInterceptor.StopCapturing();
MainModel.KeyboardHook.Unsubscribe(HandleKeypress);
}
public override void Enable()
{
KeyboardInterceptor.StartCapturing();
KeyboardInterceptor.KeyUp += HandleKeypress;
// Listener won't start unless the effect is active
MainModel.KeyboardHook.Subscribe(HandleKeypress);
}
public override void Update()
{
for (var i = 0; i < Waves.Count; i++)
if (Settings.IsRandomColors)
_randomColor = ColorHelpers.ShiftColor(_randomColor, 25);
for (var i = 0; i < _waves.Count; i++)
{
// TODO: Get from settings
var fps = 25;
Waves[i].Size += Settings.SpreadSpeed;
_waves[i].Size += Settings.SpreadSpeed;
if (Settings.IsShiftColors)
Waves[i].Color = ColorHelpers.ShiftColor(Waves[i].Color, Settings.ShiftColorSpeed);
_waves[i].Color = ColorHelpers.ShiftColor(_waves[i].Color, Settings.ShiftColorSpeed);
var decreaseAmount = 255/(Settings.TimeToLive/fps);
Waves[i].Color = Color.FromArgb(Waves[i].Color.A - decreaseAmount, Waves[i].Color.R, Waves[i].Color.G,
Waves[i].Color.B);
_waves[i].Color = Color.FromArgb(_waves[i].Color.A - decreaseAmount, _waves[i].Color.R, _waves[i].Color.G,
_waves[i].Color.B);
if (Waves[i].Color.A >= decreaseAmount)
if (_waves[i].Color.A >= decreaseAmount)
continue;
Waves.RemoveAt(i);
_waves.RemoveAt(i);
i--;
}
}
public override Bitmap GenerateBitmap()
{
if (Waves.Count == 0)
if (_waves.Count == 0)
return null;
var bitmap = new Bitmap(21, 6);
@ -78,17 +79,17 @@ namespace Artemis.Modules.Effects.TypeWave
// Don't want a foreach, collection is changed in different thread
// ReSharper disable once ForCanBeConvertedToForeach
for (var i = 0; i < Waves.Count; i++)
for (var i = 0; i < _waves.Count; i++)
{
if (Waves[i].Size == 0)
if (_waves[i].Size == 0)
continue;
var path = new GraphicsPath();
path.AddEllipse(Waves[i].Point.X - Waves[i].Size/2, Waves[i].Point.Y - Waves[i].Size/2,
Waves[i].Size, Waves[i].Size);
path.AddEllipse(_waves[i].Point.X - _waves[i].Size/2, _waves[i].Point.Y - _waves[i].Size/2,
_waves[i].Size, _waves[i].Size);
var pthGrBrush = new PathGradientBrush(path)
{
SurroundColors = new[] {Waves[i].Color},
SurroundColors = new[] {_waves[i].Color},
CenterColor = Color.Transparent
};
@ -96,8 +97,8 @@ namespace Artemis.Modules.Effects.TypeWave
pthGrBrush.FocusScales = new PointF(0.3f, 0.8f);
g.FillPath(pthGrBrush, path);
g.DrawEllipse(new Pen(pthGrBrush, 1), Waves[i].Point.X - Waves[i].Size/2,
Waves[i].Point.Y - Waves[i].Size/2, Waves[i].Size, Waves[i].Size);
g.DrawEllipse(new Pen(pthGrBrush, 1), _waves[i].Point.X - _waves[i].Size/2,
_waves[i].Point.Y - _waves[i].Size/2, _waves[i].Size, _waves[i].Size);
}
}
return bitmap;
@ -114,8 +115,8 @@ namespace Artemis.Modules.Effects.TypeWave
if (keyMatch == null)
return;
Waves.Add(Settings.IsRandomColors
? new Wave(new Point(keyMatch.PosX, keyMatch.PosY), 0, ColorHelpers.GetRandomRainbowColor())
_waves.Add(Settings.IsRandomColors
? new Wave(new Point(keyMatch.PosX, keyMatch.PosY), 0, _randomColor)
: new Wave(new Point(keyMatch.PosX, keyMatch.PosY), 0,
ColorHelpers.ToDrawingColor(Settings.WaveColor)));
}

View File

@ -3,21 +3,19 @@ using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using Artemis.Models;
using Gma.System.MouseKeyHook;
using NAudio.CoreAudioApi;
using Open.WinKeyboardHook;
namespace Artemis.Modules.Overlays.VolumeDisplay
{
public class VolumeDisplayModel : OverlayModel
{
private IKeyboardMouseEvents _mGlobalHook;
public VolumeDisplayModel(MainModel mainModel, VolumeDisplaySettings settings) : base(mainModel)
{
Settings = settings;
Name = "VolumeDisplay";
KeyboardInterceptor = new KeyboardInterceptor();
KeyboardInterceptor.StartCapturing();
Enabled = Settings.Enabled;
VolumeDisplay = new VolumeDisplay(mainModel, settings);
@ -26,18 +24,16 @@ namespace Artemis.Modules.Overlays.VolumeDisplay
public VolumeDisplay VolumeDisplay { get; set; }
public VolumeDisplaySettings Settings { get; set; }
public KeyboardInterceptor KeyboardInterceptor { get; set; }
public override void Dispose()
{
KeyboardInterceptor.KeyUp -= HandleKeypress;
KeyboardInterceptor.StopCapturing();
MainModel.KeyboardHook.Unsubscribe(HandleKeypress);
}
public override void Enable()
{
KeyboardInterceptor.StartCapturing();
KeyboardInterceptor.KeyUp += HandleKeypress;
// Listener won't start unless the effect is active
MainModel.KeyboardHook.Subscribe(HandleKeypress);
}
public override void Update()

View File

@ -0,0 +1,31 @@
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
namespace Artemis.Utilities.Keyboard
{
public class KeyboardHook
{
private IKeyboardMouseEvents _mGlobalHook;
public int Subscriptions { get; set; }
public void Subscribe(KeyEventHandler handleKeypress)
{
if (Subscriptions < 1)
_mGlobalHook = Hook.GlobalEvents();
_mGlobalHook.KeyDown += handleKeypress;
Subscriptions++;
}
public void Unsubscribe(KeyEventHandler handleKeypress)
{
_mGlobalHook.KeyDown -= handleKeypress;
Subscriptions--;
if (Subscriptions >= 1)
return;
_mGlobalHook.Dispose();
_mGlobalHook = null;
}
}
}

View File

@ -11,8 +11,8 @@
<package id="log4net" version="2.0.5" targetFramework="net452" />
<package id="MahApps.Metro" version="1.3.0-ALPHA017" targetFramework="net452" />
<package id="MahApps.Metro.Resources" version="0.4.0.0" targetFramework="net452" />
<package id="MouseKeyHook" version="5.4.0" targetFramework="net452" />
<package id="NAudio" version="1.7.3" targetFramework="net452" />
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net452" />
<package id="Open.WinKeyboardHook" version="1.0.10.0" targetFramework="net452" />
<package id="WpfExceptionViewer" version="1.0.0.0" targetFramework="net452" />
</packages>