diff --git a/Artemis/Artemis/Artemis.csproj b/Artemis/Artemis/Artemis.csproj index 89b315bfd..cb4942ed2 100644 --- a/Artemis/Artemis/Artemis.csproj +++ b/Artemis/Artemis/Artemis.csproj @@ -329,6 +329,7 @@ + @@ -485,6 +486,7 @@ + @@ -497,7 +499,6 @@ - @@ -869,9 +870,7 @@ false - - - + diff --git a/Artemis/Artemis/ArtemisBootstrapper.cs b/Artemis/Artemis/ArtemisBootstrapper.cs index 9d1ea7ac2..8f412eaf1 100644 --- a/Artemis/Artemis/ArtemisBootstrapper.cs +++ b/Artemis/Artemis/ArtemisBootstrapper.cs @@ -5,8 +5,11 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Forms; using Artemis.InjectionModules; +using Artemis.Profiles.Layers.Interfaces; +using Artemis.Profiles.Layers.Types.KeyPress; using Artemis.Settings; using Artemis.Utilities; +using Artemis.Utilities.Converters; using Artemis.ViewModels; using Caliburn.Micro; using Newtonsoft.Json; @@ -79,10 +82,16 @@ namespace Artemis protected override void Configure() { - JsonConvert.DefaultSettings = () => new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.Auto}; _kernel = new StandardKernel(new BaseModules(), new ArtemisModules(), new ManagerModules()); _kernel.Bind().To().InSingletonScope(); _kernel.Bind().To().InSingletonScope(); + + var settings = new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.Auto, + ContractResolver = _kernel.Get() + }; + JsonConvert.DefaultSettings = () => settings; } protected override void OnExit(object sender, EventArgs e) diff --git a/Artemis/Artemis/DeviceProviders/Corsair/CorsairKeyboards.cs b/Artemis/Artemis/DeviceProviders/Corsair/CorsairKeyboards.cs index aa67d094c..ff52332f6 100644 --- a/Artemis/Artemis/DeviceProviders/Corsair/CorsairKeyboards.cs +++ b/Artemis/Artemis/DeviceProviders/Corsair/CorsairKeyboards.cs @@ -1,5 +1,7 @@ using System.Drawing; +using System.Linq; using System.Windows; +using System.Windows.Forms; using Artemis.Properties; using Artemis.Utilities; using CUE.NET; @@ -51,6 +53,7 @@ namespace Artemis.DeviceProviders.Corsair break; case "K70 RGB": case "K70 RGB RAPIDFIRE": + case "K70 LUX RGB": Height = 7; Width = 21; PreviewSettings = new PreviewSettings(676, 210, new Thickness(0, -25, 0, 0), Resources.k70); @@ -106,5 +109,19 @@ namespace Artemis.DeviceProviders.Corsair image.Dispose(); } + + public override KeyMatch? GetKeyPosition(Keys keyCode) + { + var widthMultiplier = Width/_keyboard.KeyboardRectangle.Width; + var heightMultiplier = Height/_keyboard.KeyboardRectangle.Height; + + // TODO: Not all key codes translate to CUE keys + var cueKey = _keyboard.Keys.FirstOrDefault(k => k.KeyId.ToString() == keyCode.ToString()); + if (cueKey != null) + return new KeyMatch(keyCode, (int) (cueKey.KeyRectangle.X*widthMultiplier), + (int) (cueKey.KeyRectangle.Y*heightMultiplier)); + + return null; + } } } \ No newline at end of file diff --git a/Artemis/Artemis/DeviceProviders/KeyboardProvider.cs b/Artemis/Artemis/DeviceProviders/KeyboardProvider.cs index 887f4be74..b4cf7f1e4 100644 --- a/Artemis/Artemis/DeviceProviders/KeyboardProvider.cs +++ b/Artemis/Artemis/DeviceProviders/KeyboardProvider.cs @@ -3,6 +3,7 @@ using System.Drawing; using System.Threading; using System.Threading.Tasks; using System.Windows; +using System.Windows.Forms; using MahApps.Metro.Controls.Dialogs; using Size = System.Windows.Size; @@ -96,6 +97,27 @@ namespace Artemis.DeviceProviders throw new NotImplementedException( "KeyboardProvider doesn't implement TryEnable, use CanEnableAsync instead."); } + + /// + /// Returns the real life X and Y coordinates of the given key + /// + /// + /// + public abstract KeyMatch? GetKeyPosition(Keys keyCode); + } + + public struct KeyMatch + { + public KeyMatch(Keys keyCode, int x, int y) + { + KeyCode = keyCode; + X = x; + Y = y; + } + + public Keys KeyCode { get; set; } + public int X { get; set; } + public int Y { get; set; } } public struct PreviewSettings diff --git a/Artemis/Artemis/DeviceProviders/Logitech/G810.cs b/Artemis/Artemis/DeviceProviders/Logitech/G810.cs index 8c7b35fbb..04163d171 100644 --- a/Artemis/Artemis/DeviceProviders/Logitech/G810.cs +++ b/Artemis/Artemis/DeviceProviders/Logitech/G810.cs @@ -1,4 +1,7 @@ -using System.Windows; +using System.Linq; +using System.Windows; +using System.Windows.Forms; +using Artemis.DeviceProviders.Logitech.Utilities; using Artemis.Properties; namespace Artemis.DeviceProviders.Logitech @@ -17,5 +20,10 @@ namespace Artemis.DeviceProviders.Logitech Width = 21; PreviewSettings = new PreviewSettings(675, 185, new Thickness(0, 35, 0, 0), Resources.g810); } + + public override KeyMatch? GetKeyPosition(Keys keyCode) + { + return KeyMap.UsEnglishOrionKeys.FirstOrDefault(k => k.KeyCode == keyCode); + } } } \ No newline at end of file diff --git a/Artemis/Artemis/DeviceProviders/Logitech/G910.cs b/Artemis/Artemis/DeviceProviders/Logitech/G910.cs index 6a335d180..ac481e9a0 100644 --- a/Artemis/Artemis/DeviceProviders/Logitech/G910.cs +++ b/Artemis/Artemis/DeviceProviders/Logitech/G910.cs @@ -1,4 +1,7 @@ -using System.Windows; +using System.Linq; +using System.Windows; +using System.Windows.Forms; +using Artemis.DeviceProviders.Logitech.Utilities; using Artemis.Properties; namespace Artemis.DeviceProviders.Logitech @@ -17,5 +20,10 @@ namespace Artemis.DeviceProviders.Logitech Width = 21; PreviewSettings = new PreviewSettings(540, 154, new Thickness(25, -80, 0, 0), Resources.g910); } + + public override KeyMatch? GetKeyPosition(Keys keyCode) + { + return KeyMap.UsEnglishOrionKeys.FirstOrDefault(k => k.KeyCode == keyCode); + } } } \ No newline at end of file diff --git a/Artemis/Artemis/DeviceProviders/Logitech/Utilities/KeyMap.cs b/Artemis/Artemis/DeviceProviders/Logitech/Utilities/KeyMap.cs index f5ca002fc..6a8506a83 100644 --- a/Artemis/Artemis/DeviceProviders/Logitech/Utilities/KeyMap.cs +++ b/Artemis/Artemis/DeviceProviders/Logitech/Utilities/KeyMap.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.Windows.Forms; -using Artemis.Utilities.Keyboard; namespace Artemis.DeviceProviders.Logitech.Utilities { @@ -10,128 +9,128 @@ namespace Artemis.DeviceProviders.Logitech.Utilities { // There are several keyboard layouts // TODO: Implemented more layouts and an option to select them - UsEnglishOrionKeys = new List + UsEnglishOrionKeys = new List { // Row 1 - new Key(Keys.Escape, 0, 0), - new Key(Keys.F1, 1, 0), - new Key(Keys.F2, 2, 0), - new Key(Keys.F3, 3, 0), - new Key(Keys.F4, 4, 0), - new Key(Keys.F5, 5, 0), - new Key(Keys.F6, 6, 0), - new Key(Keys.F7, 7, 0), - new Key(Keys.F8, 8, 0), - new Key(Keys.F9, 9, 0), - new Key(Keys.F10, 10, 0), - new Key(Keys.F11, 11, 0), - new Key(Keys.F12, 12, 0), - new Key(Keys.PrintScreen, 13, 0), - new Key(Keys.Scroll, 14, 0), - new Key(Keys.Pause, 15, 0), + new KeyMatch(Keys.Escape, 0, 0), + new KeyMatch(Keys.F1, 1, 0), + new KeyMatch(Keys.F2, 2, 0), + new KeyMatch(Keys.F3, 3, 0), + new KeyMatch(Keys.F4, 4, 0), + new KeyMatch(Keys.F5, 5, 0), + new KeyMatch(Keys.F6, 6, 0), + new KeyMatch(Keys.F7, 7, 0), + new KeyMatch(Keys.F8, 8, 0), + new KeyMatch(Keys.F9, 9, 0), + new KeyMatch(Keys.F10, 10, 0), + new KeyMatch(Keys.F11, 11, 0), + new KeyMatch(Keys.F12, 12, 0), + new KeyMatch(Keys.PrintScreen, 13, 0), + new KeyMatch(Keys.Scroll, 14, 0), + new KeyMatch(Keys.Pause, 15, 0), // Row 2 - new Key(Keys.Oemtilde, 0, 1), - new Key(Keys.D1, 1, 1), - new Key(Keys.D2, 2, 1), - new Key(Keys.D3, 3, 1), - new Key(Keys.D4, 4, 1), - new Key(Keys.D5, 5, 1), - new Key(Keys.D6, 6, 1), - new Key(Keys.D7, 7, 1), - new Key(Keys.D8, 8, 1), - new Key(Keys.D9, 9, 1), - new Key(Keys.D0, 10, 1), - new Key(Keys.OemMinus, 11, 1), - new Key(Keys.Oemplus, 12, 1), - new Key(Keys.Back, 13, 1), - new Key(Keys.Insert, 14, 1), - new Key(Keys.Home, 15, 1), - new Key(Keys.PageUp, 16, 1), - new Key(Keys.NumLock, 17, 1), - new Key(Keys.Divide, 18, 1), - new Key(Keys.Multiply, 19, 1), - new Key(Keys.Subtract, 20, 1), + new KeyMatch(Keys.Oemtilde, 0, 1), + new KeyMatch(Keys.D1, 1, 1), + new KeyMatch(Keys.D2, 2, 1), + new KeyMatch(Keys.D3, 3, 1), + new KeyMatch(Keys.D4, 4, 1), + new KeyMatch(Keys.D5, 5, 1), + new KeyMatch(Keys.D6, 6, 1), + new KeyMatch(Keys.D7, 7, 1), + new KeyMatch(Keys.D8, 8, 1), + new KeyMatch(Keys.D9, 9, 1), + new KeyMatch(Keys.D0, 10, 1), + new KeyMatch(Keys.OemMinus, 11, 1), + new KeyMatch(Keys.Oemplus, 12, 1), + new KeyMatch(Keys.Back, 13, 1), + new KeyMatch(Keys.Insert, 14, 1), + new KeyMatch(Keys.Home, 15, 1), + new KeyMatch(Keys.PageUp, 16, 1), + new KeyMatch(Keys.NumLock, 17, 1), + new KeyMatch(Keys.Divide, 18, 1), + new KeyMatch(Keys.Multiply, 19, 1), + new KeyMatch(Keys.Subtract, 20, 1), // Row 3 - new Key(Keys.Tab, 0, 2), - new Key(Keys.Q, 1, 2), - new Key(Keys.W, 2, 2), - new Key(Keys.E, 3, 2), - new Key(Keys.R, 4, 2), - new Key(Keys.T, 5, 2), - new Key(Keys.Y, 6, 2), - new Key(Keys.U, 7, 2), - new Key(Keys.I, 8, 2), - new Key(Keys.O, 9, 2), - new Key(Keys.P, 10, 2), - new Key(Keys.OemOpenBrackets, 11, 2), - new Key(Keys.Oem6, 12, 2), - new Key(Keys.Delete, 14, 2), - new Key(Keys.End, 15, 2), - new Key(Keys.Next, 16, 2), - new Key(Keys.NumPad7, 17, 2), - new Key(Keys.NumPad8, 18, 2), - new Key(Keys.NumPad9, 19, 2), - new Key(Keys.Add, 20, 2), + new KeyMatch(Keys.Tab, 0, 2), + new KeyMatch(Keys.Q, 1, 2), + new KeyMatch(Keys.W, 2, 2), + new KeyMatch(Keys.E, 3, 2), + new KeyMatch(Keys.R, 4, 2), + new KeyMatch(Keys.T, 5, 2), + new KeyMatch(Keys.Y, 6, 2), + new KeyMatch(Keys.U, 7, 2), + new KeyMatch(Keys.I, 8, 2), + new KeyMatch(Keys.O, 9, 2), + new KeyMatch(Keys.P, 10, 2), + new KeyMatch(Keys.OemOpenBrackets, 11, 2), + new KeyMatch(Keys.Oem6, 12, 2), + new KeyMatch(Keys.Delete, 14, 2), + new KeyMatch(Keys.End, 15, 2), + new KeyMatch(Keys.Next, 16, 2), + new KeyMatch(Keys.NumPad7, 17, 2), + new KeyMatch(Keys.NumPad8, 18, 2), + new KeyMatch(Keys.NumPad9, 19, 2), + new KeyMatch(Keys.Add, 20, 2), // Row 4 - new Key(Keys.Capital, 0, 3), - new Key(Keys.A, 1, 3), - new Key(Keys.S, 2, 3), - new Key(Keys.D, 3, 3), - new Key(Keys.F, 4, 3), - new Key(Keys.G, 5, 3), - new Key(Keys.H, 6, 3), - new Key(Keys.J, 7, 3), - new Key(Keys.K, 8, 3), - new Key(Keys.L, 9, 3), - new Key(Keys.Oem1, 10, 3), - new Key(Keys.Oem7, 11, 3), - new Key(Keys.Oem5, 12, 3), - new Key(Keys.Return, 13, 3), - new Key(Keys.NumPad4, 17, 3), - new Key(Keys.NumPad5, 18, 3), - new Key(Keys.NumPad6, 19, 3), + new KeyMatch(Keys.Capital, 0, 3), + new KeyMatch(Keys.A, 1, 3), + new KeyMatch(Keys.S, 2, 3), + new KeyMatch(Keys.D, 3, 3), + new KeyMatch(Keys.F, 4, 3), + new KeyMatch(Keys.G, 5, 3), + new KeyMatch(Keys.H, 6, 3), + new KeyMatch(Keys.J, 7, 3), + new KeyMatch(Keys.K, 8, 3), + new KeyMatch(Keys.L, 9, 3), + new KeyMatch(Keys.Oem1, 10, 3), + new KeyMatch(Keys.Oem7, 11, 3), + new KeyMatch(Keys.Oem5, 12, 3), + new KeyMatch(Keys.Return, 13, 3), + new KeyMatch(Keys.NumPad4, 17, 3), + new KeyMatch(Keys.NumPad5, 18, 3), + new KeyMatch(Keys.NumPad6, 19, 3), // Row 5 - new Key(Keys.LShiftKey, 1, 4), - new Key(Keys.OemBackslash, 2, 4), - new Key(Keys.Z, 2, 4), - new Key(Keys.X, 3, 4), - new Key(Keys.C, 4, 4), - new Key(Keys.V, 5, 4), - new Key(Keys.B, 6, 4), - new Key(Keys.N, 7, 4), - new Key(Keys.M, 8, 4), - new Key(Keys.Oemcomma, 9, 4), - new Key(Keys.OemPeriod, 10, 4), - new Key(Keys.OemQuestion, 11, 4), - new Key(Keys.RShiftKey, 13, 4), - new Key(Keys.Up, 15, 4), - new Key(Keys.NumPad1, 17, 4), - new Key(Keys.NumPad2, 18, 4), - new Key(Keys.NumPad3, 19, 4), + new KeyMatch(Keys.LShiftKey, 1, 4), + new KeyMatch(Keys.OemBackslash, 2, 4), + new KeyMatch(Keys.Z, 2, 4), + new KeyMatch(Keys.X, 3, 4), + new KeyMatch(Keys.C, 4, 4), + new KeyMatch(Keys.V, 5, 4), + new KeyMatch(Keys.B, 6, 4), + new KeyMatch(Keys.N, 7, 4), + new KeyMatch(Keys.M, 8, 4), + new KeyMatch(Keys.Oemcomma, 9, 4), + new KeyMatch(Keys.OemPeriod, 10, 4), + new KeyMatch(Keys.OemQuestion, 11, 4), + new KeyMatch(Keys.RShiftKey, 13, 4), + new KeyMatch(Keys.Up, 15, 4), + new KeyMatch(Keys.NumPad1, 17, 4), + new KeyMatch(Keys.NumPad2, 18, 4), + new KeyMatch(Keys.NumPad3, 19, 4), // Both returns return "Return" (Yes...) // new OrionKey(System.Windows.Forms.Keys.Return, 20, 4), // Row 6 - new Key(Keys.LControlKey, 0, 5), - new Key(Keys.LWin, 1, 5), - new Key(Keys.LMenu, 2, 5), - new Key(Keys.Space, 5, 5), - new Key(Keys.RMenu, 11, 5), - new Key(Keys.RWin, 12, 5), - new Key(Keys.Apps, 13, 5), - new Key(Keys.RControlKey, 14, 5), - new Key(Keys.Left, 15, 5), - new Key(Keys.Down, 16, 5), - new Key(Keys.Right, 17, 5), - new Key(Keys.NumPad0, 18, 5), - new Key(Keys.Decimal, 19, 5) + new KeyMatch(Keys.LControlKey, 0, 5), + new KeyMatch(Keys.LWin, 1, 5), + new KeyMatch(Keys.LMenu, 2, 5), + new KeyMatch(Keys.Space, 5, 5), + new KeyMatch(Keys.RMenu, 11, 5), + new KeyMatch(Keys.RWin, 12, 5), + new KeyMatch(Keys.Apps, 13, 5), + new KeyMatch(Keys.RControlKey, 14, 5), + new KeyMatch(Keys.Left, 15, 5), + new KeyMatch(Keys.Down, 16, 5), + new KeyMatch(Keys.Right, 17, 5), + new KeyMatch(Keys.NumPad0, 18, 5), + new KeyMatch(Keys.Decimal, 19, 5) }; } - public static List UsEnglishOrionKeys { get; set; } + public static List UsEnglishOrionKeys { get; set; } } } \ No newline at end of file diff --git a/Artemis/Artemis/DeviceProviders/Razer/BlackWidow.cs b/Artemis/Artemis/DeviceProviders/Razer/BlackWidow.cs index 3f1bd656a..588640cf5 100644 --- a/Artemis/Artemis/DeviceProviders/Razer/BlackWidow.cs +++ b/Artemis/Artemis/DeviceProviders/Razer/BlackWidow.cs @@ -1,9 +1,13 @@ using System.Drawing; +using System.Linq; using System.Windows; +using System.Windows.Forms; +using Artemis.DeviceProviders.Logitech.Utilities; using Artemis.DeviceProviders.Razer.Utilities; using Artemis.Properties; using Corale.Colore.Core; using Corale.Colore.Razer; +using Corale.Colore.Razer.Keyboard; using Constants = Corale.Colore.Razer.Keyboard.Constants; namespace Artemis.DeviceProviders.Razer @@ -49,5 +53,11 @@ namespace Artemis.DeviceProviders.Razer var razerArray = RazerUtilities.BitmapColorArray(bitmap, Height, Width); Chroma.Instance.Keyboard.SetCustom(razerArray); } + + public override KeyMatch? GetKeyPosition(Keys keyCode) + { + // TODO: Needs it's own keymap or a way to get it from the Chroma SDK + return KeyMap.UsEnglishOrionKeys.FirstOrDefault(k => k.KeyCode == keyCode); + } } } \ No newline at end of file diff --git a/Artemis/Artemis/InjectionModules/ArtemisModules.cs b/Artemis/Artemis/InjectionModules/ArtemisModules.cs index 9c23101b9..7347d9e09 100644 --- a/Artemis/Artemis/InjectionModules/ArtemisModules.cs +++ b/Artemis/Artemis/InjectionModules/ArtemisModules.cs @@ -21,6 +21,7 @@ using Artemis.Profiles.Layers.Types.Generic; using Artemis.Profiles.Layers.Types.Headset; using Artemis.Profiles.Layers.Types.Keyboard; using Artemis.Profiles.Layers.Types.KeyboardGif; +using Artemis.Profiles.Layers.Types.KeyPress; using Artemis.Profiles.Layers.Types.Mouse; using Artemis.ViewModels.Abstract; using Ninject.Modules; @@ -78,9 +79,11 @@ namespace Artemis.InjectionModules Bind().To(); Bind().To(); Bind().To(); + // Conditions Bind().To(); Bind().To(); + // Types Bind().To(); Bind().To(); @@ -88,6 +91,10 @@ namespace Artemis.InjectionModules Bind().To(); Bind().To(); Bind().To(); + Bind().To(); + + // Bind some Layer Types to self as well in order to allow JSON.NET injection + Bind().ToSelf(); #endregion } diff --git a/Artemis/Artemis/Managers/MainManager.cs b/Artemis/Artemis/Managers/MainManager.cs index 3cef01e70..bec1c129d 100644 --- a/Artemis/Artemis/Managers/MainManager.cs +++ b/Artemis/Artemis/Managers/MainManager.cs @@ -43,9 +43,6 @@ namespace Artemis.Managers ProgramEnabled = false; Running = false; - // TODO: Dependency inject utilities? - KeyboardHook = new KeyboardHook(); - // Create and start the web server GameStateWebServer = new GameStateWebServer(); GameStateWebServer.Start(); @@ -67,7 +64,6 @@ namespace Artemis.Managers public ProfileManager ProfileManager { get; set; } public PipeServer PipeServer { get; set; } - public KeyboardHook KeyboardHook { get; set; } public GameStateWebServer GameStateWebServer { get; set; } public bool ProgramEnabled { get; private set; } public bool Running { get; private set; } diff --git a/Artemis/Artemis/Modules/Effects/TypeWave/TypeWaveModel.cs b/Artemis/Artemis/Modules/Effects/TypeWave/TypeWaveModel.cs index ff4eb66fa..2326f1dcc 100644 --- a/Artemis/Artemis/Modules/Effects/TypeWave/TypeWaveModel.cs +++ b/Artemis/Artemis/Modules/Effects/TypeWave/TypeWaveModel.cs @@ -1,13 +1,12 @@ using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; -using System.Linq; using System.Windows.Forms; -using Artemis.DeviceProviders.Logitech.Utilities; using Artemis.Managers; using Artemis.Models; using Artemis.Profiles.Layers.Models; using Artemis.Utilities; +using Artemis.Utilities.Keyboard; namespace Artemis.Modules.Effects.TypeWave { @@ -30,7 +29,7 @@ namespace Artemis.Modules.Effects.TypeWave public override void Dispose() { Initialized = false; - MainManager.KeyboardHook.KeyDownCallback -= KeyboardHookOnKeyDownCallback; + KeyboardHook.KeyDownCallback -= KeyboardHookOnKeyDownCallback; } private void KeyboardHookOnKeyDownCallback(KeyEventArgs e) @@ -39,23 +38,19 @@ namespace Artemis.Modules.Effects.TypeWave if (_waves.Count >= 25) return; - var keyMatch = KeyMap.UsEnglishOrionKeys.FirstOrDefault(k => k.KeyCode == e.KeyCode); + var keyMatch = MainManager.DeviceManager.ActiveKeyboard.GetKeyPosition(e.KeyCode); if (keyMatch == null) return; _waves.Add(Settings.IsRandomColors - ? new Wave(new Point(keyMatch.PosX*KeyboardScale, keyMatch.PosY*KeyboardScale), 0, _randomColor) - : new Wave(new Point(keyMatch.PosX*KeyboardScale, keyMatch.PosY*KeyboardScale), 0, + ? new Wave(new Point(keyMatch.Value.X*KeyboardScale, keyMatch.Value.Y*KeyboardScale), 0, _randomColor) + : new Wave(new Point(keyMatch.Value.X*KeyboardScale, keyMatch.Value.Y*KeyboardScale), 0, ColorHelpers.ToDrawingColor(Settings.WaveColor))); } public override void Enable() { - Initialized = false; - - // Listener won't start unless the effect is active - MainManager.KeyboardHook.KeyDownCallback += KeyboardHookOnKeyDownCallback; - + KeyboardHook.KeyDownCallback += KeyboardHookOnKeyDownCallback; Initialized = true; } diff --git a/Artemis/Artemis/Modules/Overlays/VolumeDisplay/VolumeDisplayModel.cs b/Artemis/Artemis/Modules/Overlays/VolumeDisplay/VolumeDisplayModel.cs index d9121701b..5ea452145 100644 --- a/Artemis/Artemis/Modules/Overlays/VolumeDisplay/VolumeDisplayModel.cs +++ b/Artemis/Artemis/Modules/Overlays/VolumeDisplay/VolumeDisplayModel.cs @@ -5,6 +5,7 @@ using System.Windows.Forms; using Artemis.Managers; using Artemis.Models; using Artemis.Profiles.Layers.Models; +using Artemis.Utilities.Keyboard; using NAudio.CoreAudioApi; namespace Artemis.Modules.Overlays.VolumeDisplay @@ -26,13 +27,13 @@ namespace Artemis.Modules.Overlays.VolumeDisplay public override void Dispose() { - MainManager.KeyboardHook.KeyDownCallback -= KeyPressTask; + KeyboardHook.KeyDownCallback -= KeyPressTask; } public override void Enable() { // Listener won't start unless the effect is active - MainManager.KeyboardHook.KeyDownCallback += KeyPressTask; + KeyboardHook.KeyDownCallback += KeyPressTask; } public override void Update() diff --git a/Artemis/Artemis/Profiles/Layers/Types/KeyPress/KeyPressType.cs b/Artemis/Artemis/Profiles/Layers/Types/KeyPress/KeyPressType.cs new file mode 100644 index 000000000..249f4140e --- /dev/null +++ b/Artemis/Artemis/Profiles/Layers/Types/KeyPress/KeyPressType.cs @@ -0,0 +1,120 @@ +using System.Collections.Generic; +using System.Linq; +using System.Windows; +using System.Windows.Forms; +using System.Windows.Media; +using Artemis.Managers; +using Artemis.Models.Interfaces; +using Artemis.Profiles.Layers.Abstract; +using Artemis.Profiles.Layers.Animations; +using Artemis.Profiles.Layers.Interfaces; +using Artemis.Profiles.Layers.Models; +using Artemis.Profiles.Layers.Types.Generic; +using Artemis.Properties; +using Artemis.Utilities; +using Artemis.Utilities.Keyboard; + +namespace Artemis.Profiles.Layers.Types.KeyPress +{ + internal class KeyPressType : ILayerType + { + private readonly MainManager _mainManager; + private List _keyPressLayers = new List(); + private LayerPropertiesModel _properties; + + public KeyPressType(MainManager mainManager) + { + _mainManager = mainManager; + KeyboardHook.KeyDownCallback += KeyboardHookOnKeyDownCallback; + } + + public RadialGradientBrush TempBrush { get; set; } + + + public string Name { get; } = "Keyboard - Key press"; + public bool ShowInEdtor { get; } = false; + public DrawType DrawType { get; } = DrawType.Keyboard; + + public ImageSource DrawThumbnail(LayerModel layer) + { + var thumbnailRect = new Rect(0, 0, 18, 18); + var visual = new DrawingVisual(); + using (var c = visual.RenderOpen()) + c.DrawImage(ImageUtilities.BitmapToBitmapImage(Resources.gif), thumbnailRect); + + var image = new DrawingImage(visual.Drawing); + return image; + } + + public void Draw(LayerModel layer, DrawingContext c) + { + lock (_keyPressLayers) + { + foreach (var keyPressLayer in _keyPressLayers) + keyPressLayer.LayerType.Draw(keyPressLayer, c); + } + } + + public void Update(LayerModel layerModel, IDataModel dataModel, bool isPreview = false) + { + // Key press is always as large as the entire keyboard it is drawn for + layerModel.Properties.Width = _mainManager.DeviceManager.ActiveKeyboard.Width; + layerModel.Properties.Height = _mainManager.DeviceManager.ActiveKeyboard.Height; + layerModel.Properties.X = 0; + layerModel.Properties.Y = 0; + layerModel.Properties.Contain = true; + + _properties = layerModel.Properties; + + lock (_keyPressLayers) + { + // Remove expired key presses + _keyPressLayers = _keyPressLayers.Where(k => !k.LayerAnimation.MustExpire(k)).ToList(); + // Update the ones that are still active + foreach (var keyPressLayer in _keyPressLayers) + keyPressLayer.Update(null, false, true); + } + } + + public void SetupProperties(LayerModel layerModel) + { + if (layerModel.Properties is SimplePropertiesModel) + return; + + layerModel.Properties = new SimplePropertiesModel(layerModel.Properties); + } + + public LayerPropertiesViewModel SetupViewModel(LayerPropertiesViewModel layerPropertiesViewModel, + List layerAnimations, IDataModel dataModel, LayerModel proposedLayer) + { + if (layerPropertiesViewModel is GenericPropertiesViewModel) + return layerPropertiesViewModel; + return new GenericPropertiesViewModel(proposedLayer, dataModel, layerAnimations); + } + + private void KeyboardHookOnKeyDownCallback(KeyEventArgs e) + { + if (_properties == null) + return; + + var keyMatch = _mainManager.DeviceManager.ActiveKeyboard.GetKeyPosition(e.KeyCode); + if (keyMatch == null) + return; + + lock (_keyPressLayers) + { + var layer = LayerModel.CreateLayer(); + layer.Properties.Brush = _properties.Brush.CloneCurrentValue(); + layer.Properties.X = keyMatch.Value.X - 3; + layer.Properties.Y = keyMatch.Value.Y - 3; + layer.Properties.Width = 6; + layer.Properties.Height = 6; + + layer.Properties.AnimationSpeed = 1; + layer.LayerAnimation = new GrowAnimation(); + + _keyPressLayers.Add(layer); + } + } + } +} \ No newline at end of file diff --git a/Artemis/Artemis/Profiles/ProfileModel.cs b/Artemis/Artemis/Profiles/ProfileModel.cs index e81ca8deb..2e96a518f 100644 --- a/Artemis/Artemis/Profiles/ProfileModel.cs +++ b/Artemis/Artemis/Profiles/ProfileModel.cs @@ -172,7 +172,6 @@ namespace Artemis.Profiles /// /// Resizes layers that are shown in the editor and match exactly the full keyboard widht and height /// - /// The keyboard the profile was made for /// The new keyboard to adjust the layers for public void ResizeLayers(KeyboardProvider target) { diff --git a/Artemis/Artemis/Utilities/Converters/NinjectCustomConverter.cs b/Artemis/Artemis/Utilities/Converters/NinjectCustomConverter.cs new file mode 100644 index 000000000..1021a99c7 --- /dev/null +++ b/Artemis/Artemis/Utilities/Converters/NinjectCustomConverter.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using Newtonsoft.Json.Serialization; +using Ninject; + +namespace Artemis.Utilities.Converters +{ + public class NinjectContractResolver : DefaultContractResolver + + { + private readonly IKernel _kernel; + + public NinjectContractResolver(IKernel kernel) + { + _kernel = kernel; + } + + protected override JsonObjectContract CreateObjectContract(Type objectType) + + { + var contract = base.CreateObjectContract(objectType); + if ((bool) _kernel.CanResolve(objectType)) + contract.DefaultCreator = () => _kernel.Get(objectType); + return contract; + } + } +} \ No newline at end of file diff --git a/Artemis/Artemis/Utilities/Keyboard/Key.cs b/Artemis/Artemis/Utilities/Keyboard/Key.cs deleted file mode 100644 index d671ac13c..000000000 --- a/Artemis/Artemis/Utilities/Keyboard/Key.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Windows.Forms; - -namespace Artemis.Utilities.Keyboard -{ - public class Key - { - public Key(Keys keyCode, int posX, int posY) - { - KeyCode = keyCode; - PosX = posX; - PosY = posY; - } - - public Keys KeyCode { get; set; } - public int PosX { get; set; } - public int PosY { get; set; } - } -} \ No newline at end of file diff --git a/Artemis/Artemis/Utilities/Keyboard/KeyboardHook.cs b/Artemis/Artemis/Utilities/Keyboard/KeyboardHook.cs index fe6b1d74d..326552cae 100644 --- a/Artemis/Artemis/Utilities/Keyboard/KeyboardHook.cs +++ b/Artemis/Artemis/Utilities/Keyboard/KeyboardHook.cs @@ -4,21 +4,21 @@ using VirtualInput; namespace Artemis.Utilities.Keyboard { - public class KeyboardHook + public static class KeyboardHook { public delegate void KeyDownCallbackHandler(KeyEventArgs e); - public KeyboardHook() + static KeyboardHook() { - VirtualKeyboard.KeyDown += VirtualKeyboardOnKeyDown; VirtualKeyboard.StartInterceptor(); + VirtualKeyboard.KeyDown += VirtualKeyboardOnKeyDown; } - private void VirtualKeyboardOnKeyDown(object sender, KeyEventArgs keyEventArgs) + private static void VirtualKeyboardOnKeyDown(object sender, KeyEventArgs keyEventArgs) { Task.Factory.StartNew(() => { KeyDownCallback?.Invoke(keyEventArgs); }); } - public event KeyDownCallbackHandler KeyDownCallback; + public static event KeyDownCallbackHandler KeyDownCallback; } } \ No newline at end of file