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

Added LUA keybind module

This commit is contained in:
SpoinkyNL 2017-02-07 23:39:47 +01:00
parent 0e1c7d9e3b
commit b53e08fdcb
5 changed files with 165 additions and 73 deletions

View File

@ -548,6 +548,7 @@
<Compile Include="Profiles\Lua\Modules\LuaBrushesModule.cs" /> <Compile Include="Profiles\Lua\Modules\LuaBrushesModule.cs" />
<Compile Include="Profiles\Lua\LuaModule.cs" /> <Compile Include="Profiles\Lua\LuaModule.cs" />
<Compile Include="Profiles\Lua\Modules\Brushes\LuaSolidColorBrush.cs" /> <Compile Include="Profiles\Lua\Modules\Brushes\LuaSolidColorBrush.cs" />
<Compile Include="Profiles\Lua\Modules\LuaKeybindModule.cs" />
<Compile Include="Profiles\Lua\Modules\LuaKeyboardModule.cs" /> <Compile Include="Profiles\Lua\Modules\LuaKeyboardModule.cs" />
<Compile Include="Profiles\Lua\Modules\LuaLayerModule.cs" /> <Compile Include="Profiles\Lua\Modules\LuaLayerModule.cs" />
<Compile Include="Profiles\Lua\Modules\LuaMouseModule.cs" /> <Compile Include="Profiles\Lua\Modules\LuaMouseModule.cs" />

View File

@ -1,60 +1,60 @@
using System.Linq; using System.Linq;
using Artemis.Modules.Abstract; using Artemis.Modules.Abstract;
using Artemis.Profiles.Layers.Interfaces; using Artemis.Profiles.Layers.Interfaces;
using Artemis.Profiles.Layers.Models; using Artemis.Profiles.Layers.Models;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Artemis.Profiles.Layers.Conditions namespace Artemis.Profiles.Layers.Conditions
{ {
public class EventCondition : ILayerCondition public class EventCondition : ILayerCondition
{ {
[JsonIgnore] [JsonIgnore]
public bool HotKeyMet { get;set; } public bool HotKeyMet { get;set; }
public bool ConditionsMet(LayerModel layerModel, ModuleDataModel dataModel) public bool ConditionsMet(LayerModel layerModel, ModuleDataModel dataModel)
{ {
lock (layerModel.Properties.Conditions) lock (layerModel.Properties.Conditions)
{ {
var checkConditions = layerModel.Properties.Conditions.Where(c => !c.Field.Contains("hotkey")); var checkConditions = layerModel.Properties.Conditions.Where(c => !c.Field.Contains("hotkey"));
var conditionsMet = false; var conditionsMet = false;
switch (layerModel.Properties.ConditionType) switch (layerModel.Properties.ConditionType)
{ {
case ConditionType.AnyMet: case ConditionType.AnyMet:
conditionsMet = HotKeyMet || checkConditions.Any(cm => cm.ConditionMet(dataModel)); conditionsMet = HotKeyMet || checkConditions.Any(cm => cm.ConditionMet(dataModel));
break; break;
case ConditionType.AllMet: case ConditionType.AllMet:
conditionsMet = HotKeyMet && checkConditions.All(cm => cm.ConditionMet(dataModel)); conditionsMet = HotKeyMet && checkConditions.All(cm => cm.ConditionMet(dataModel));
break; break;
case ConditionType.NoneMet: case ConditionType.NoneMet:
conditionsMet = !HotKeyMet && !checkConditions.Any(cm => cm.ConditionMet(dataModel)); conditionsMet = !HotKeyMet && !checkConditions.Any(cm => cm.ConditionMet(dataModel));
break; break;
} }
layerModel.EventProperties.Update(layerModel, conditionsMet); layerModel.EventProperties.Update(layerModel, conditionsMet);
if (conditionsMet) if (conditionsMet)
layerModel.EventProperties.TriggerEvent(layerModel); layerModel.EventProperties.TriggerEvent(layerModel);
if (layerModel.EventProperties.MustStop(layerModel)) if (layerModel.EventProperties.MustStop(layerModel))
HotKeyMet = false; HotKeyMet = false;
return layerModel.EventProperties.MustDraw; return layerModel.EventProperties.MustDraw;
} }
} }
public void KeybindTask(LayerConditionModel condition) public void KeybindTask(LayerConditionModel condition)
{ {
switch (condition.Field) switch (condition.Field)
{ {
case "hotkeyEnable": case "hotkeyEnable":
HotKeyMet = true; HotKeyMet = true;
break; break;
case "hotkeyDisable": case "hotkeyDisable":
HotKeyMet = false; HotKeyMet = false;
break; break;
case "hotkeyToggle": case "hotkeyToggle":
HotKeyMet = !HotKeyMet; HotKeyMet = !HotKeyMet;
break; break;
} }
} }
} }
} }

View File

@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Artemis.Managers;
using Artemis.Models;
using MahApps.Metro.Controls;
using MoonSharp.Interpreter;
namespace Artemis.Profiles.Lua.Modules
{
[MoonSharpUserData]
public class LuaKeybindModule : LuaModule
{
private readonly List<KeybindModel> _keybindModels;
public LuaKeybindModule(LuaManager luaManager) : base(luaManager)
{
_keybindModels = new List<KeybindModel>();
LuaManager.ProfileModel.OnProfileUpdatedEvent += ProfileModelOnOnProfileUpdatedEvent;
}
public override string ModuleName => "Keybind";
private void ProfileModelOnOnProfileUpdatedEvent(object sender, EventArgs e)
{
foreach (var keybindModel in _keybindModels)
KeybindManager.AddOrUpdate(keybindModel);
}
/// <summary>
/// Sets a keybind to call the provided function
/// </summary>
/// <param name="name">Name of the keybind</param>
/// <param name="hotKey">Hotkey in string format, per example: ALT+CTRL+SHIFT+D</param>
/// <param name="function">LUA function to call</param>
/// <param name="args">Optional arguments for the passed function</param>
public void SetKeybind(string name, string hotKey, DynValue function, params DynValue[] args)
{
var modifierKeys = ModifierKeys.None;
var key = Key.System;
var hotKeyParts = hotKey.Split('+').Select(p => p.Trim());
foreach (var hotKeyPart in hotKeyParts)
if (hotKeyPart == "ALT")
modifierKeys |= ModifierKeys.Alt;
else if (hotKeyPart == "CTRL")
modifierKeys |= ModifierKeys.Control;
else if (hotKeyPart == "SHIFT")
modifierKeys |= ModifierKeys.Shift;
else
Enum.TryParse(hotKeyPart, true, out key);
if (key == Key.System)
throw new ScriptRuntimeException($"Hotkey '{hotKey}' couldn't be parsed.");
var hk = new HotKey(key, modifierKeys);
var model = args != null
? new KeybindModel("LUA-" + name, hk, () => LuaManager.Call(function, args))
: new KeybindModel("LUA-" + name, hk, () => LuaManager.Call(function));
KeybindManager.AddOrUpdate(model);
var existing = _keybindModels.FirstOrDefault(k => k.Name == model.Name);
if (existing != null)
_keybindModels.Remove(existing);
_keybindModels.Add(model);
}
/// <summary>
/// If found, removes a keybind with the given name
/// </summary>
/// <param name="name"></param>
public void RemoveKeybind(string name)
{
var existing = _keybindModels.FirstOrDefault(k => k.Name == name);
if (existing != null)
_keybindModels.Remove(existing);
KeybindManager.Remove(name);
}
public override void Dispose()
{
foreach (var keybindModel in _keybindModels)
KeybindManager.Remove(keybindModel);
LuaManager.ProfileModel.OnProfileUpdatedEvent -= ProfileModelOnOnProfileUpdatedEvent;
}
}
}

View File

@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
@ -13,12 +11,8 @@ using Artemis.Models;
using Artemis.Modules.Abstract; using Artemis.Modules.Abstract;
using Artemis.Profiles.Layers.Interfaces; using Artemis.Profiles.Layers.Interfaces;
using Artemis.Profiles.Layers.Models; using Artemis.Profiles.Layers.Models;
using Artemis.Utilities;
using Artemis.Utilities.ParentChild; using Artemis.Utilities.ParentChild;
using Newtonsoft.Json; using Newtonsoft.Json;
using Color = System.Windows.Media.Color;
using Point = System.Windows.Point;
using Size = System.Windows.Size;
namespace Artemis.Profiles namespace Artemis.Profiles
{ {
@ -30,6 +24,7 @@ namespace Artemis.Profiles
{ {
_invalidFileNameChars = Path.GetInvalidFileNameChars(); _invalidFileNameChars = Path.GetInvalidFileNameChars();
Layers = new ChildItemCollection<ProfileModel, LayerModel>(this); Layers = new ChildItemCollection<ProfileModel, LayerModel>(this);
OnProfileUpdatedEvent += OnOnProfileUpdatedEvent;
} }
public ChildItemCollection<ProfileModel, LayerModel> Layers { get; } public ChildItemCollection<ProfileModel, LayerModel> Layers { get; }
@ -44,8 +39,14 @@ namespace Artemis.Profiles
[JsonIgnore] [JsonIgnore]
public string Slug => new string(Name.Where(ch => !_invalidFileNameChars.Contains(ch)).ToArray()); public string Slug => new string(Name.Where(ch => !_invalidFileNameChars.Contains(ch)).ToArray());
private void OnOnProfileUpdatedEvent(object sender, EventArgs e)
{
ApplyKeybinds();
}
public event EventHandler<ProfileDeviceEventsArg> OnDeviceUpdatedEvent; public event EventHandler<ProfileDeviceEventsArg> OnDeviceUpdatedEvent;
public event EventHandler<ProfileDeviceEventsArg> OnDeviceDrawnEvent; public event EventHandler<ProfileDeviceEventsArg> OnDeviceDrawnEvent;
public event EventHandler<EventArgs> OnProfileUpdatedEvent;
public void FixOrder() public void FixOrder()
{ {
@ -133,14 +134,17 @@ namespace Artemis.Profiles
private void RaiseDeviceUpdatedEvent(ProfileDeviceEventsArg e) private void RaiseDeviceUpdatedEvent(ProfileDeviceEventsArg e)
{ {
var handler = OnDeviceUpdatedEvent; OnDeviceUpdatedEvent?.Invoke(this, e);
handler?.Invoke(this, e);
} }
public void RaiseDeviceDrawnEvent(ProfileDeviceEventsArg e) public void RaiseDeviceDrawnEvent(ProfileDeviceEventsArg e)
{ {
var handler = OnDeviceDrawnEvent; OnDeviceDrawnEvent?.Invoke(this, e);
handler?.Invoke(this, e); }
public virtual void OnOnProfileUpdatedEvent()
{
OnProfileUpdatedEvent?.Invoke(this, EventArgs.Empty);
} }
/// <summary> /// <summary>
@ -216,7 +220,6 @@ namespace Artemis.Profiles
public void ApplyKeybinds() public void ApplyKeybinds()
{ {
foreach (var layerModel in Layers) foreach (var layerModel in Layers)
{
for (var index = 0; index < layerModel.Properties.Conditions.Count; index++) for (var index = 0; index < layerModel.Properties.Conditions.Count; index++)
{ {
var condition = layerModel.Properties.Conditions[index]; var condition = layerModel.Properties.Conditions[index];
@ -228,7 +231,6 @@ namespace Artemis.Profiles
var kb = new KeybindModel($"{GameName}-{Name}-{layerModel.Name}-{index}", condition.HotKey, action); var kb = new KeybindModel($"{GameName}-{Name}-{layerModel.Name}-{index}", condition.HotKey, action);
KeybindManager.AddOrUpdate(kb); KeybindManager.AddOrUpdate(kb);
} }
}
} }
#region Compare #region Compare

View File

@ -319,9 +319,7 @@ namespace Artemis.ViewModels
{ {
Thread.Sleep(100); Thread.Sleep(100);
SelectedLayer = selectModel; SelectedLayer = selectModel;
SelectedProfile?.OnOnProfileUpdatedEvent();
// Let the profile reapply keybinds after messing with layers
SelectedProfile.ApplyKeybinds();
}); });
} }