diff --git a/Artemis/Artemis/Managers/LuaManager.cs b/Artemis/Artemis/Managers/LuaManager.cs index 1aa9ea63d..fbd499a8b 100644 --- a/Artemis/Artemis/Managers/LuaManager.cs +++ b/Artemis/Artemis/Managers/LuaManager.cs @@ -1,14 +1,10 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Text; -using Artemis.DAL; using Artemis.DeviceProviders; using Artemis.Profiles; using Artemis.Profiles.Lua; using Artemis.Profiles.Lua.Modules; -using Artemis.Properties; using Castle.Core.Internal; using MoonSharp.Interpreter; using Ninject; @@ -23,7 +19,6 @@ namespace Artemis.Managers private readonly ILogger _logger; private readonly Script _luaScript; private List _luaModules; - private FileSystemWatcher _watcher; public LuaManager(IKernel kernel, ILogger logger, DeviceManager deviceManager) { @@ -178,67 +173,5 @@ namespace Artemis.Managers } #endregion - - #region Editor - - public void OpenEditor() - { - if (ProfileModel == null) - return; - - // Create a temp file - var fileName = Guid.NewGuid() + ".lua"; - var file = File.Create(Path.GetTempPath() + fileName); - file.Dispose(); - - // Add instructions to LUA script if it's a new file - if (ProfileModel.LuaScript.IsNullOrEmpty()) - ProfileModel.LuaScript = Encoding.UTF8.GetString(Resources.lua_placeholder); - File.WriteAllText(Path.GetTempPath() + fileName, ProfileModel.LuaScript); - - // Watch the file for changes - SetupWatcher(Path.GetTempPath(), fileName); - - // Open the temp file with the default editor - System.Diagnostics.Process.Start(Path.GetTempPath() + fileName); - } - - private void SetupWatcher(string path, string fileName) - { - if (_watcher == null) - { - _watcher = new FileSystemWatcher(Path.GetTempPath(), fileName); - _watcher.Changed += LuaFileChanged; - _watcher.EnableRaisingEvents = true; - } - - _watcher.Path = path; - _watcher.Filter = fileName; - } - - private void LuaFileChanged(object sender, FileSystemEventArgs args) - { - if (args.ChangeType != WatcherChangeTypes.Changed) - return; - - if (ProfileModel == null) - return; - - lock (ProfileModel) - { - using (var fs = new FileStream(args.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - using (var sr = new StreamReader(fs)) - { - ProfileModel.LuaScript = sr.ReadToEnd(); - } - } - - ProfileProvider.AddOrUpdate(ProfileModel); - SetupLua(ProfileModel); - } - } - - #endregion } } \ No newline at end of file diff --git a/Artemis/Artemis/Models/EffectModel.cs b/Artemis/Artemis/Models/EffectModel.cs index 600f7e1fd..a2dd083df 100644 --- a/Artemis/Artemis/Models/EffectModel.cs +++ b/Artemis/Artemis/Models/EffectModel.cs @@ -56,10 +56,15 @@ namespace Artemis.Models private void DeviceManagerOnOnKeyboardChangedEvent(object sender, KeyboardChangedEventArgs args) { + if (!Initialized) + return; + if (!string.IsNullOrEmpty(Settings?.LastProfile)) Profile = ProfileProvider.GetProfile(DeviceManager.ActiveKeyboard, this, Settings.LastProfile); else Profile = ProfileProvider.GetProfile(DeviceManager.ActiveKeyboard, this, "Default"); + + Profile?.Activate(LuaManager); } // Called on creation @@ -70,6 +75,8 @@ namespace Artemis.Models Profile = ProfileProvider.GetProfile(DeviceManager.ActiveKeyboard, this, Settings.LastProfile); else Profile = ProfileProvider.GetProfile(DeviceManager.ActiveKeyboard, this, "Default"); + + Profile?.Activate(LuaManager); } // Called every frame diff --git a/Artemis/Artemis/Profiles/Lua/Modules/Brushes/LuaLinearGradientBrush.cs b/Artemis/Artemis/Profiles/Lua/Modules/Brushes/LuaLinearGradientBrush.cs index 3336119e4..b12e299f2 100644 --- a/Artemis/Artemis/Profiles/Lua/Modules/Brushes/LuaLinearGradientBrush.cs +++ b/Artemis/Artemis/Profiles/Lua/Modules/Brushes/LuaLinearGradientBrush.cs @@ -57,7 +57,7 @@ namespace Artemis.Profiles.Lua.Modules.Brushes { var collection = new GradientStopCollection(gradientColors .Select(gc => new GradientStop(gc.Key.Color, gc.Value))); - + Brush = new LinearGradientBrush(collection, new Point(startX, startY), new Point(endX, endY)); } } diff --git a/Artemis/Artemis/Profiles/Lua/Modules/LuaBrushesModule.cs b/Artemis/Artemis/Profiles/Lua/Modules/LuaBrushesModule.cs index 8a944dd59..ad17ac975 100644 --- a/Artemis/Artemis/Profiles/Lua/Modules/LuaBrushesModule.cs +++ b/Artemis/Artemis/Profiles/Lua/Modules/LuaBrushesModule.cs @@ -1,4 +1,7 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Windows.Media; using Artemis.Managers; using Artemis.Profiles.Lua.Modules.Brushes; using Artemis.Utilities; @@ -47,5 +50,35 @@ namespace Artemis.Profiles.Lua.Modules { return new LuaRadialGradientBrush(gradientColors, centerX, centerY, originX, originY); } + + public LuaColor GetRelativeColor(Dictionary gradientColors, double offset) + { + if (offset < 0) + offset = 0; + if (offset > 1) + offset = 1; + + var gsc = new GradientStopCollection(gradientColors.Select(gc => new GradientStop(gc.Key.Color, gc.Value))); + var b = gsc.First(w => Math.Abs(w.Offset - gsc.Min(m => m.Offset)) < 0.01); + var a = gsc.First(w => Math.Abs(w.Offset - gsc.Max(m => m.Offset)) < 0.01); + + foreach (var gs in gsc) + { + if (gs.Offset < offset && gs.Offset > b.Offset) + b = gs; + if (gs.Offset > offset && gs.Offset < a.Offset) + a = gs; + } + + var color = new Color + { + ScA = (float) ((offset - b.Offset) * (a.Color.ScA - b.Color.ScA) / (a.Offset - b.Offset) + b.Color.ScA), + ScR = (float) ((offset - b.Offset) * (a.Color.ScR - b.Color.ScR) / (a.Offset - b.Offset) + b.Color.ScR), + ScG = (float) ((offset - b.Offset) * (a.Color.ScG - b.Color.ScG) / (a.Offset - b.Offset) + b.Color.ScG), + ScB = (float) ((offset - b.Offset) * (a.Color.ScB - b.Color.ScB) / (a.Offset - b.Offset) + b.Color.ScB) + }; + + return new LuaColor(color); + } } } \ No newline at end of file diff --git a/Artemis/Artemis/Profiles/Lua/Modules/LuaLayerModule.cs b/Artemis/Artemis/Profiles/Lua/Modules/LuaLayerModule.cs index aca0bb2c8..77b99e7c8 100644 --- a/Artemis/Artemis/Profiles/Lua/Modules/LuaLayerModule.cs +++ b/Artemis/Artemis/Profiles/Lua/Modules/LuaLayerModule.cs @@ -20,7 +20,7 @@ namespace Artemis.Profiles.Lua.Modules _layerModel = layerModel; SavedProperties = new Wrappers.LuaLayerProperties(_layerModel); - // Triger an update to fill up the Properties + // Trigger an update to fill up the Properties _layerModel.Update(new ProfilePreviewDataModel(), true, false); } diff --git a/Artemis/Artemis/Profiles/ProfileModel.cs b/Artemis/Artemis/Profiles/ProfileModel.cs index d191f30c4..da2f65901 100644 --- a/Artemis/Artemis/Profiles/ProfileModel.cs +++ b/Artemis/Artemis/Profiles/ProfileModel.cs @@ -192,7 +192,7 @@ namespace Artemis.Profiles public void Activate(LuaManager luaManager) { - if (!Equals(luaManager.ProfileModel, this)) + if (!Equals(luaManager.ProfileModel, this) || luaManager.ProfileModel.LuaScript != LuaScript) luaManager.SetupLua(this); } diff --git a/Artemis/Artemis/Properties/AssemblyInfo.cs b/Artemis/Artemis/Properties/AssemblyInfo.cs index 7e493dfdf..7205a0d2c 100644 --- a/Artemis/Artemis/Properties/AssemblyInfo.cs +++ b/Artemis/Artemis/Properties/AssemblyInfo.cs @@ -53,7 +53,7 @@ using System.Windows; // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.7.0.0")] -[assembly: AssemblyFileVersion("1.7.0.0")] +[assembly: AssemblyVersion("1.7.1.0")] +[assembly: AssemblyFileVersion("1.7.1.0")] [assembly: InternalsVisibleTo("Artemis.Explorables")] diff --git a/Artemis/Artemis/Resources/Keyboards/default-profiles.zip b/Artemis/Artemis/Resources/Keyboards/default-profiles.zip index 8553f2868..4341a3e49 100644 Binary files a/Artemis/Artemis/Resources/Keyboards/default-profiles.zip and b/Artemis/Artemis/Resources/Keyboards/default-profiles.zip differ diff --git a/Artemis/Artemis/Resources/lua-placeholder.lua b/Artemis/Artemis/Resources/lua-placeholder.lua index 87b7a962a..ab5b2d17d 100644 --- a/Artemis/Artemis/Resources/lua-placeholder.lua +++ b/Artemis/Artemis/Resources/lua-placeholder.lua @@ -15,6 +15,11 @@ -- This event is raised after every profile update, before drawing. function updateHandler(profile, eventArgs) + -- Don't do anything when previewing (this means the editor is open) + if eventArgs.Preview then + return + end + -- In this example we only want to update once per frame when the keyboard is -- updated. If you don't do this the updateHandler will trigger on every -- device's update. @@ -27,6 +32,11 @@ end -- This event is raised after every profile draw, after updating. function drawHandler(profile, eventArgs) + -- Don't do anything when previewing (this means the editor is open) + if eventArgs.Preview then + return + end + -- In this example we only want to draw to the keyboard. Each device has it's -- own drawing event if eventArgs.DeviceType != "keyboard" then diff --git a/Artemis/Artemis/ViewModels/Profiles/ProfileEditorViewModel.cs b/Artemis/Artemis/ViewModels/Profiles/ProfileEditorViewModel.cs index ff0385b73..1a6a8d678 100644 --- a/Artemis/Artemis/ViewModels/Profiles/ProfileEditorViewModel.cs +++ b/Artemis/Artemis/ViewModels/Profiles/ProfileEditorViewModel.cs @@ -1,7 +1,9 @@ using System; using System.Collections.ObjectModel; using System.ComponentModel; +using System.IO; using System.Linq; +using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; @@ -16,9 +18,12 @@ using Artemis.Models; using Artemis.Profiles; using Artemis.Profiles.Layers.Models; using Artemis.Profiles.Layers.Types.Folder; +using Artemis.Properties; using Artemis.Services; using Artemis.Styles.DropTargetAdorners; using Artemis.Utilities; +using Caliburn.Micro; +using Castle.Core.Internal; using GongSolutions.Wpf.DragDrop; using MahApps.Metro.Controls.Dialogs; using Ninject; @@ -43,6 +48,7 @@ namespace Artemis.ViewModels.Profiles private ObservableCollection _profileNames; private bool _saving; private ProfileModel _selectedProfile; + private FileSystemWatcher _watcher; public ProfileEditorViewModel(DeviceManager deviceManager, LuaManager luaManager, EffectModel effectModel, ProfileViewModel profileViewModel, MetroDialogService dialogService, WindowService windowService, @@ -138,7 +144,7 @@ namespace Artemis.ViewModels.Profiles // Update the value _selectedProfile = value; } - + NotifyOfPropertyChange(() => SelectedProfile); NotifyOfPropertyChange(() => SelectedProfileName); } @@ -256,22 +262,26 @@ namespace Artemis.ViewModels.Profiles /// private void LoadProfiles() { - ProfileNames.Clear(); - if (_effectModel == null || _deviceManager.ActiveKeyboard == null) - return; + Execute.OnUIThread(() => + { + ProfileNames.Clear(); + if (_effectModel == null || _deviceManager.ActiveKeyboard == null) + return; - ProfileNames.AddRange(ProfileProvider.GetProfileNames(_deviceManager.ActiveKeyboard, _effectModel)); + ProfileNames.AddRange(ProfileProvider.GetProfileNames(_deviceManager.ActiveKeyboard, _effectModel)); - // If a profile name was provided, try to load it - ProfileModel lastProfileModel = null; - if (!string.IsNullOrEmpty(LastProfile)) - lastProfileModel = ProfileProvider.GetProfile(_deviceManager.ActiveKeyboard, _effectModel, LastProfile); + // If a profile name was provided, try to load it + ProfileModel lastProfileModel = null; + if (!string.IsNullOrEmpty(LastProfile)) + lastProfileModel = ProfileProvider.GetProfile(_deviceManager.ActiveKeyboard, _effectModel, + LastProfile); - if (lastProfileModel != null) - SelectedProfile = lastProfileModel; - else - SelectedProfile = ProfileProvider.GetProfile(_deviceManager.ActiveKeyboard, _effectModel, - ProfileNames.FirstOrDefault()); + if (lastProfileModel != null) + SelectedProfile = lastProfileModel; + else + SelectedProfile = ProfileProvider.GetProfile(_deviceManager.ActiveKeyboard, _effectModel, + ProfileNames.FirstOrDefault()); + }); } @@ -700,13 +710,12 @@ namespace Artemis.ViewModels.Profiles return; try { - _luaManager.SetupLua(SelectedProfile); - _luaManager.OpenEditor(); + OpenEditor(); } catch (Exception e) { DialogService.ShowMessageBox("Couldn't open LUA file", - "Please make sure you have a program such as Notepad associated with the .lua extension.\n\n" + + "Please make sure you have a text editor associated with the .lua extension.\n\n" + "Windows error message: \n" + e.Message); } } @@ -758,5 +767,67 @@ namespace Artemis.ViewModels.Profiles } _saving = false; } + + #region LUA Editor + + public void OpenEditor() + { + if (SelectedProfile == null) + return; + + // Create a temp file + var fileName = Guid.NewGuid() + ".lua"; + var file = File.Create(Path.GetTempPath() + fileName); + file.Dispose(); + + // Add instructions to LUA script if it's a new file + if (SelectedProfile.LuaScript.IsNullOrEmpty()) + SelectedProfile.LuaScript = Encoding.UTF8.GetString(Resources.lua_placeholder); + File.WriteAllText(Path.GetTempPath() + fileName, SelectedProfile.LuaScript); + + // Watch the file for changes + SetupWatcher(Path.GetTempPath(), fileName); + + // Open the temp file with the default editor + System.Diagnostics.Process.Start(Path.GetTempPath() + fileName); + } + + private void SetupWatcher(string path, string fileName) + { + if (_watcher == null) + { + _watcher = new FileSystemWatcher(Path.GetTempPath(), fileName); + _watcher.Changed += LuaFileChanged; + _watcher.EnableRaisingEvents = true; + } + + _watcher.Path = path; + _watcher.Filter = fileName; + } + + private void LuaFileChanged(object sender, FileSystemEventArgs args) + { + if (args.ChangeType != WatcherChangeTypes.Changed) + return; + + if (SelectedProfile == null) + return; + + lock (SelectedProfile) + { + using (var fs = new FileStream(args.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + { + using (var sr = new StreamReader(fs)) + { + SelectedProfile.LuaScript = sr.ReadToEnd(); + } + } + + ProfileProvider.AddOrUpdate(SelectedProfile); + _luaManager.SetupLua(SelectedProfile); + } + } + + #endregion } } \ No newline at end of file