From 5d138bf7ac222bcba81f77d6c0637b837a83b9ca Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Sat, 6 May 2017 23:32:56 +0200 Subject: [PATCH] Added editor buttons to LUA API --- Artemis/Artemis/Artemis.csproj | 1 + Artemis/Artemis/Managers/LuaManager.cs | 5 + Artemis/Artemis/Models/ProfileEditorModel.cs | 10 +- .../GeneralProfile/GeneralProfileView.xaml | 12 +- .../Profiles/Lua/Modules/Gui/EditorButton.cs | 25 +++++ .../Profiles/Lua/Modules/LuaGuiModule.cs | 14 +++ .../ViewModels/ProfileEditorViewModel.cs | 12 +- Artemis/Artemis/Views/ProfileEditorView.xaml | 105 +++++++++--------- 8 files changed, 111 insertions(+), 73 deletions(-) create mode 100644 Artemis/Artemis/Profiles/Lua/Modules/Gui/EditorButton.cs diff --git a/Artemis/Artemis/Artemis.csproj b/Artemis/Artemis/Artemis.csproj index 3c88f1a4a..5f0aea86c 100644 --- a/Artemis/Artemis/Artemis.csproj +++ b/Artemis/Artemis/Artemis.csproj @@ -568,6 +568,7 @@ + diff --git a/Artemis/Artemis/Managers/LuaManager.cs b/Artemis/Artemis/Managers/LuaManager.cs index f5df058a7..a01b6b2cd 100644 --- a/Artemis/Artemis/Managers/LuaManager.cs +++ b/Artemis/Artemis/Managers/LuaManager.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using Artemis.DeviceProviders; using Artemis.Profiles; using Artemis.Profiles.Lua; using Artemis.Profiles.Lua.Modules; +using Artemis.Profiles.Lua.Modules.Gui; using Castle.Core.Internal; using MoonSharp.Interpreter; using Ninject; @@ -24,6 +26,8 @@ namespace Artemis.Managers _kernel = kernel; _logger = logger; _deviceManager = deviceManager; + + EditorButtons = new ObservableCollection(); LuaScript = new Script(CoreModules.Preset_SoftSandbox); } @@ -32,6 +36,7 @@ namespace Artemis.Managers public LuaProfileModule ProfileModule { get; private set; } public LuaEventsModule EventsModule { get; private set; } public Script LuaScript { get; } + public ObservableCollection EditorButtons { get; set; } public void SetupLua(ProfileModel profileModel) { diff --git a/Artemis/Artemis/Models/ProfileEditorModel.cs b/Artemis/Artemis/Models/ProfileEditorModel.cs index 093393770..389d5ecf7 100644 --- a/Artemis/Artemis/Models/ProfileEditorModel.cs +++ b/Artemis/Artemis/Models/ProfileEditorModel.cs @@ -27,8 +27,7 @@ namespace Artemis.Models private FileSystemWatcher _watcher; private ModuleModel _luaModule; - public ProfileEditorModel(WindowService windowService, MetroDialogService dialogService, - DeviceManager deviceManager, LuaManager luaManager) + public ProfileEditorModel(WindowService windowService, MetroDialogService dialogService, DeviceManager deviceManager, LuaManager luaManager) { _windowService = windowService; _dialogService = dialogService; @@ -345,7 +344,8 @@ namespace Artemis.Models private void DisposeLuaWatcher() { - if (_watcher == null) return; + if (_watcher == null) + return; _watcher.Changed -= LuaFileChanged; _watcher.Dispose(); _watcher = null; @@ -360,8 +360,6 @@ namespace Artemis.Models #region Rendering - - #endregion } -} \ No newline at end of file +} diff --git a/Artemis/Artemis/Modules/General/GeneralProfile/GeneralProfileView.xaml b/Artemis/Artemis/Modules/General/GeneralProfile/GeneralProfileView.xaml index ba95e4a36..a512a3593 100644 --- a/Artemis/Artemis/Modules/General/GeneralProfile/GeneralProfileView.xaml +++ b/Artemis/Artemis/Modules/General/GeneralProfile/GeneralProfileView.xaml @@ -1,12 +1,6 @@ - + diff --git a/Artemis/Artemis/Profiles/Lua/Modules/Gui/EditorButton.cs b/Artemis/Artemis/Profiles/Lua/Modules/Gui/EditorButton.cs new file mode 100644 index 000000000..7fe85734b --- /dev/null +++ b/Artemis/Artemis/Profiles/Lua/Modules/Gui/EditorButton.cs @@ -0,0 +1,25 @@ +using Artemis.Managers; +using MoonSharp.Interpreter; + +namespace Artemis.Profiles.Lua.Modules.Gui +{ + public class EditorButton + { + private readonly LuaManager _luaManager; + + public EditorButton(LuaManager luaManager, string text, DynValue action) + { + _luaManager = luaManager; + Text = text; + Action = action; + } + + public void Invoke() + { + _luaManager.Call(Action); + } + + public string Text { get; } + public DynValue Action { get; } + } +} diff --git a/Artemis/Artemis/Profiles/Lua/Modules/LuaGuiModule.cs b/Artemis/Artemis/Profiles/Lua/Modules/LuaGuiModule.cs index 64b0a411a..78412738c 100644 --- a/Artemis/Artemis/Profiles/Lua/Modules/LuaGuiModule.cs +++ b/Artemis/Artemis/Profiles/Lua/Modules/LuaGuiModule.cs @@ -41,10 +41,24 @@ namespace Artemis.Profiles.Lua.Modules } } + public void AddEditorButton(string name, DynValue action) + { + Execute.OnUIThread(() => LuaManager.EditorButtons.Add(new EditorButton(LuaManager, name, action))); + } + + public void RemoveEditorButton(string name) + { + var button = LuaManager.EditorButtons.FirstOrDefault(b => b.Text == name); + if (button != null) + Execute.OnUIThread(() => LuaManager.EditorButtons.Remove(button)); + } + public override void Dispose() { foreach (var window in _windows) window.TryClose(); + + Execute.OnUIThread(() => LuaManager.EditorButtons.Clear()); } } } diff --git a/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs b/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs index d7699475c..b0d3b1daf 100644 --- a/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs +++ b/Artemis/Artemis/ViewModels/ProfileEditorViewModel.cs @@ -19,6 +19,7 @@ using Artemis.Profiles; using Artemis.Profiles.Layers.Interfaces; using Artemis.Profiles.Layers.Models; using Artemis.Profiles.Layers.Types.Folder; +using Artemis.Profiles.Lua.Modules.Gui; using Artemis.Properties; using Artemis.Services; using Artemis.Styles.DropTargetAdorners; @@ -58,8 +59,7 @@ namespace Artemis.ViewModels private LayerModel _selectedLayer; private bool _showAll; - public ProfileEditorViewModel(ProfileEditorModel profileEditorModel, DeviceManager deviceManager, - LoopManager loopManager, ModuleModel moduleModel, MetroDialogService dialogService) + public ProfileEditorViewModel(ProfileEditorModel profileEditorModel, DeviceManager deviceManager, LoopManager loopManager, LuaManager luaManager, ModuleModel moduleModel, MetroDialogService dialogService) { _deviceManager = deviceManager; _loopManager = loopManager; @@ -71,6 +71,7 @@ namespace Artemis.ViewModels ProfileNames = new ObservableCollection(); Layers = new ObservableCollection(); ProfileEditorModel = profileEditorModel; + LuaManager = luaManager; ShowAll = true; PropertyChanged += EditorStateHandler; @@ -101,6 +102,11 @@ namespace Artemis.ViewModels #region LUA + public void ClickedLuaButton(EditorButton button) + { + button.Invoke(); + } + public void EditLua() { if (SelectedProfile == null) @@ -122,6 +128,7 @@ namespace Artemis.ViewModels #region Properties public ProfileEditorModel ProfileEditorModel { get; } + public LuaManager LuaManager { get; } public ObservableCollection ProfileNames { @@ -711,7 +718,6 @@ namespace Artemis.ViewModels } - private Point GetScaledPosition(MouseEventArgs e) { var previewSettings = _deviceManager.ActiveKeyboard.PreviewSettings; diff --git a/Artemis/Artemis/Views/ProfileEditorView.xaml b/Artemis/Artemis/Views/ProfileEditorView.xaml index 73bb14f49..36632b959 100644 --- a/Artemis/Artemis/Views/ProfileEditorView.xaml +++ b/Artemis/Artemis/Views/ProfileEditorView.xaml @@ -1,16 +1,8 @@ - + @@ -22,7 +14,8 @@ - + + @@ -32,7 +25,9 @@ - @@ -43,8 +38,8 @@ - - - - - - - - - + + + + + + + + + + + +