From ef19348e53201fac5fcd9e624d003606965348b0 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Tue, 21 Feb 2017 22:16:03 +0100 Subject: [PATCH] Reworked key-held keybind --- Artemis/Artemis/Managers/KeybindManager.cs | 2 +- Artemis/Artemis/Models/KeybindModel.cs | 2 +- .../Layers/Abstract/LayerCondition.cs | 30 +++++++++++++++++- .../Layers/Conditions/DataModelCondition.cs | 31 +++---------------- .../Layers/Conditions/EventCondition.cs | 28 ++--------------- Artemis/Artemis/Profiles/ProfileModel.cs | 27 ++++++++++------ 6 files changed, 55 insertions(+), 65 deletions(-) diff --git a/Artemis/Artemis/Managers/KeybindManager.cs b/Artemis/Artemis/Managers/KeybindManager.cs index 84880c46c..b75ee5741 100644 --- a/Artemis/Artemis/Managers/KeybindManager.cs +++ b/Artemis/Artemis/Managers/KeybindManager.cs @@ -37,7 +37,7 @@ namespace Artemis.Managers var hotKey = new HotKey(KeyInterop.KeyFromVirtualKey(keyEventArgs.KeyValue), modifiers); foreach (var keybindModel in KeybindModels) - keybindModel.InvokeIfMatched(hotKey, keyType); + keybindModel.InvokeIfMatched(hotKey, keyType); } private static void ProcessMouse(MouseEventArgs mouseEventArgs, KeyType keyType) diff --git a/Artemis/Artemis/Models/KeybindModel.cs b/Artemis/Artemis/Models/KeybindModel.cs index e1d71ab56..0f9c3714c 100644 --- a/Artemis/Artemis/Models/KeybindModel.cs +++ b/Artemis/Artemis/Models/KeybindModel.cs @@ -34,7 +34,7 @@ namespace Artemis.Models return; if (hotKey.Equals(HotKey)) - Action?.Invoke(); + Action?.Invoke(); } public void InvokeIfMatched(MouseButtons mouseButtons, KeyType keyType) diff --git a/Artemis/Artemis/Profiles/Layers/Abstract/LayerCondition.cs b/Artemis/Artemis/Profiles/Layers/Abstract/LayerCondition.cs index 06503f7e5..eff5be489 100644 --- a/Artemis/Artemis/Profiles/Layers/Abstract/LayerCondition.cs +++ b/Artemis/Artemis/Profiles/Layers/Abstract/LayerCondition.cs @@ -1,11 +1,39 @@ using Artemis.Modules.Abstract; using Artemis.Profiles.Layers.Models; +using Newtonsoft.Json; namespace Artemis.Profiles.Layers.Abstract { public abstract class LayerCondition { + [JsonIgnore] + public bool HotKeyMet { get; set; } + public abstract bool ConditionsMet(LayerModel layerModel, ModuleDataModel dataModel); - public abstract void KeybindTask(LayerConditionModel condition); + + + public void KeyDownTask(LayerConditionModel condition) + { + switch (condition.Field) + { + case "hotkeyEnable": + HotKeyMet = true; + break; + case "hotkeyDisable": + HotKeyMet = false; + break; + case "hotkeyToggle": + HotKeyMet = !HotKeyMet; + break; + } + } + + public void KeyUpTask(LayerConditionModel condition) + { + if (condition.Field == "hotkeyEnable") + HotKeyMet = false; + else + HotKeyMet = true; + } } } \ No newline at end of file diff --git a/Artemis/Artemis/Profiles/Layers/Conditions/DataModelCondition.cs b/Artemis/Artemis/Profiles/Layers/Conditions/DataModelCondition.cs index 8f5560da4..7588a3055 100644 --- a/Artemis/Artemis/Profiles/Layers/Conditions/DataModelCondition.cs +++ b/Artemis/Artemis/Profiles/Layers/Conditions/DataModelCondition.cs @@ -1,24 +1,18 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Windows; using Artemis.Modules.Abstract; using Artemis.Profiles.Layers.Abstract; -using Artemis.Profiles.Layers.Interfaces; using Artemis.Profiles.Layers.Models; namespace Artemis.Profiles.Layers.Conditions { public class DataModelCondition : LayerCondition { - public bool HotKeyMet { get; set; } - public override bool ConditionsMet(LayerModel layerModel, ModuleDataModel dataModel) { lock (layerModel.Properties.Conditions) { - var checkConditions = layerModel.Properties.Conditions - .Where(c => c.Field != null && !c.Field.Contains("hotkey")).ToList(); + var checkConditions = layerModel.Properties.Conditions.Where(c => c.Field != null && !c.Field.Contains("hotkey")).ToList(); if (checkConditions.Count == layerModel.Properties.Conditions.Count) return SimpleConditionsMet(layerModel, dataModel, checkConditions); @@ -41,24 +35,7 @@ namespace Artemis.Profiles.Layers.Conditions } } - public override void KeybindTask(LayerConditionModel condition) - { - switch (condition.Field) - { - case "hotkeyEnable": - HotKeyMet = true; - break; - case "hotkeyDisable": - HotKeyMet = false; - break; - case "hotkeyToggle": - HotKeyMet = !HotKeyMet; - break; - } - } - - private static bool SimpleConditionsMet(LayerModel layerModel, ModuleDataModel dataModel, - IEnumerable checkConditions) + private static bool SimpleConditionsMet(LayerModel layerModel, ModuleDataModel dataModel, IEnumerable checkConditions) { switch (layerModel.Properties.ConditionType) { @@ -73,4 +50,4 @@ namespace Artemis.Profiles.Layers.Conditions } } } -} \ No newline at end of file +} diff --git a/Artemis/Artemis/Profiles/Layers/Conditions/EventCondition.cs b/Artemis/Artemis/Profiles/Layers/Conditions/EventCondition.cs index bcc1c3f01..9856d9537 100644 --- a/Artemis/Artemis/Profiles/Layers/Conditions/EventCondition.cs +++ b/Artemis/Artemis/Profiles/Layers/Conditions/EventCondition.cs @@ -2,23 +2,17 @@ using System.Linq; using Artemis.Modules.Abstract; using Artemis.Profiles.Layers.Abstract; -using Artemis.Profiles.Layers.Interfaces; using Artemis.Profiles.Layers.Models; -using Newtonsoft.Json; namespace Artemis.Profiles.Layers.Conditions { public class EventCondition : LayerCondition { - [JsonIgnore] - public bool HotKeyMet { get; set; } - public override bool ConditionsMet(LayerModel layerModel, ModuleDataModel dataModel) { lock (layerModel.Properties.Conditions) { - var checkConditions = layerModel.Properties.Conditions - .Where(c => c.Field != null && !c.Field.Contains("hotkey")).ToList(); + var checkConditions = layerModel.Properties.Conditions.Where(c => c.Field != null && !c.Field.Contains("hotkey")).ToList(); if (checkConditions.Count == layerModel.Properties.Conditions.Count) return SimpleConditionsMet(layerModel, dataModel, checkConditions); @@ -48,24 +42,8 @@ namespace Artemis.Profiles.Layers.Conditions } } - public override void KeybindTask(LayerConditionModel condition) - { - switch (condition.Field) - { - case "hotkeyEnable": - HotKeyMet = true; - break; - case "hotkeyDisable": - HotKeyMet = false; - break; - case "hotkeyToggle": - HotKeyMet = !HotKeyMet; - break; - } - } - private static bool SimpleConditionsMet(LayerModel layerModel, ModuleDataModel dataModel, - IEnumerable checkConditions) + private static bool SimpleConditionsMet(LayerModel layerModel, ModuleDataModel dataModel, IEnumerable checkConditions) { switch (layerModel.Properties.ConditionType) { @@ -80,4 +58,4 @@ namespace Artemis.Profiles.Layers.Conditions } } } -} \ No newline at end of file +} diff --git a/Artemis/Artemis/Profiles/ProfileModel.cs b/Artemis/Artemis/Profiles/ProfileModel.cs index 42f92a303..d7e7a4ac8 100644 --- a/Artemis/Artemis/Profiles/ProfileModel.cs +++ b/Artemis/Artemis/Profiles/ProfileModel.cs @@ -19,10 +19,13 @@ namespace Artemis.Profiles public class ProfileModel { private readonly char[] _invalidFileNameChars; + private List _profileBinds; public ProfileModel() { _invalidFileNameChars = Path.GetInvalidFileNameChars(); + _profileBinds = new List(); + Layers = new ChildItemCollection(this); OnProfileUpdatedEvent += OnOnProfileUpdatedEvent; } @@ -220,6 +223,7 @@ namespace Artemis.Profiles public void ApplyKeybinds() { + _profileBinds.Clear(); foreach (var layerModel in GetLayers()) { for (var index = 0; index < layerModel.Properties.Conditions.Count; index++) @@ -231,18 +235,23 @@ namespace Artemis.Profiles // Create an action for the layer, the layer's specific condition type handles activation if (condition.Operator == "held") { - var downAction = new Action(() => layerModel.LayerCondition.KeybindTask(condition)); - var downKb = new KeybindModel($"{GameName}-{Name}-{layerModel.Name}-{index}", condition.HotKey, KeyType.KeyDown, downAction); - var upAction = new Action(() => layerModel.LayerCondition.KeybindTask(condition)); - var upKb = new KeybindModel($"{GameName}-{Name}-{layerModel.Name}-{index}", condition.HotKey, KeyType.KeyUp, upAction); + var downAction = new Action(() => layerModel.LayerCondition.KeyDownTask(condition)); + var downKb = new KeybindModel($"{GameName}-{Name}-{layerModel.Name}-down-{index}", condition.HotKey, KeyType.KeyDown, downAction); + var upAction = new Action(() => layerModel.LayerCondition.KeyUpTask(condition)); + var upKb = new KeybindModel($"{GameName}-{Name}-{layerModel.Name}-up-{index}", condition.HotKey, KeyType.KeyUp, upAction); + KeybindManager.AddOrUpdate(downKb); KeybindManager.AddOrUpdate(upKb); + _profileBinds.Add(downKb); + _profileBinds.Add(upKb); } else { - var action = new Action(() => layerModel.LayerCondition.KeybindTask(condition)); + var action = new Action(() => layerModel.LayerCondition.KeyDownTask(condition)); var kb = new KeybindModel($"{GameName}-{Name}-{layerModel.Name}-{index}", condition.HotKey, KeyType.KeyDown, action); + KeybindManager.AddOrUpdate(kb); + _profileBinds.Add(kb); } } } @@ -250,11 +259,9 @@ namespace Artemis.Profiles public void ClearKeybinds() { - foreach (var layerModel in GetLayers()) - { - for (var index = 0; index < layerModel.Properties.Conditions.Count; index++) - KeybindManager.Remove($"{GameName}-{Name}-{layerModel.Name}-{index}"); - } + foreach (var keybindModel in _profileBinds) + KeybindManager.Remove(keybindModel); + _profileBinds.Clear(); } #region Compare