From ac7da4424be0678f7cef19b5b99a65ccdc1fc681 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Fri, 19 Aug 2016 20:56:44 +0200 Subject: [PATCH] Added LastRender to layer to fix keypress wave stacking on inactive profile --- .../Layers/Animations/GrowAnimation.cs | 4 +- .../Layers/Animations/PulseAnimation.cs | 4 +- .../Profiles/Layers/Models/LayerModel.cs | 6 +++ .../Layers/Types/KeyPress/KeyPressType.cs | 38 +++++++++++++------ Artemis/Artemis/Profiles/ProfileModel.cs | 37 +++++------------- 5 files changed, 43 insertions(+), 46 deletions(-) diff --git a/Artemis/Artemis/Profiles/Layers/Animations/GrowAnimation.cs b/Artemis/Artemis/Profiles/Layers/Animations/GrowAnimation.cs index ab8d9279c..0ff983797 100644 --- a/Artemis/Artemis/Profiles/Layers/Animations/GrowAnimation.cs +++ b/Artemis/Artemis/Profiles/Layers/Animations/GrowAnimation.cs @@ -8,15 +8,13 @@ namespace Artemis.Profiles.Layers.Animations { public class GrowAnimation : ILayerAnimation { - private DateTime _lastUpdate; public string Name { get; } = "Grow"; public void Update(LayerModel layerModel, bool updateAnimations) { // Reset animation progress if layer wasn't drawn for 100ms - if (new TimeSpan(0, 0, 0, 0, 100) < DateTime.Now - _lastUpdate) + if (new TimeSpan(0, 0, 0, 0, 100) < DateTime.Now - layerModel.LastRender) layerModel.Properties.AnimationProgress = 0; - _lastUpdate = DateTime.Now; var progress = layerModel.Properties.AnimationProgress; diff --git a/Artemis/Artemis/Profiles/Layers/Animations/PulseAnimation.cs b/Artemis/Artemis/Profiles/Layers/Animations/PulseAnimation.cs index 28a8f83aa..f68eee6cb 100644 --- a/Artemis/Artemis/Profiles/Layers/Animations/PulseAnimation.cs +++ b/Artemis/Artemis/Profiles/Layers/Animations/PulseAnimation.cs @@ -8,15 +8,13 @@ namespace Artemis.Profiles.Layers.Animations { public class PulseAnimation : ILayerAnimation { - private DateTime _lastUpdate; public string Name { get; } = "Pulse"; public void Update(LayerModel layerModel, bool updateAnimations) { // Reset animation progress if layer wasn't drawn for 100ms - if (new TimeSpan(0, 0, 0, 0, 100) < DateTime.Now - _lastUpdate) + if (new TimeSpan(0, 0, 0, 0, 100) > DateTime.Now - layerModel.LastRender) layerModel.Properties.AnimationProgress = 0; - _lastUpdate = DateTime.Now; var progress = layerModel.Properties.AnimationProgress; diff --git a/Artemis/Artemis/Profiles/Layers/Models/LayerModel.cs b/Artemis/Artemis/Profiles/Layers/Models/LayerModel.cs index 5b8a58cfb..e0f96a457 100644 --- a/Artemis/Artemis/Profiles/Layers/Models/LayerModel.cs +++ b/Artemis/Artemis/Profiles/Layers/Models/LayerModel.cs @@ -53,6 +53,9 @@ namespace Artemis.Profiles.Layers.Models [JsonIgnore] public GifImage GifImage { get; set; } + [JsonIgnore] + public DateTime LastRender { get; set; } + /// /// Checks whether this layers conditions are met. /// If they are met and this layer is an event, this also triggers that event. @@ -69,6 +72,9 @@ namespace Artemis.Profiles.Layers.Models { LayerType.Update(this, dataModel, preview); LayerAnimation?.Update(this, updateAnimations); + + if (!preview) + LastRender = DateTime.Now; } public void Draw(IDataModel dataModel, DrawingContext c, bool preview, bool updateAnimations) diff --git a/Artemis/Artemis/Profiles/Layers/Types/KeyPress/KeyPressType.cs b/Artemis/Artemis/Profiles/Layers/Types/KeyPress/KeyPressType.cs index 72f9aa46d..43d0ac4e0 100644 --- a/Artemis/Artemis/Profiles/Layers/Types/KeyPress/KeyPressType.cs +++ b/Artemis/Artemis/Profiles/Layers/Types/KeyPress/KeyPressType.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Forms; @@ -19,7 +20,7 @@ namespace Artemis.Profiles.Layers.Types.KeyPress { private readonly MainManager _mainManager; private List _keyPressLayers = new List(); - private KeyPressPropertiesModel _properties; + private LayerModel _layerModel; public KeyPressType(MainManager mainManager) { @@ -39,7 +40,9 @@ namespace Artemis.Profiles.Layers.Types.KeyPress 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; @@ -63,7 +66,7 @@ namespace Artemis.Profiles.Layers.Types.KeyPress layerModel.Properties.Y = 0; layerModel.Properties.Contain = true; - _properties = (KeyPressPropertiesModel) layerModel.Properties; + _layerModel = layerModel; if (isPreview) return; @@ -96,27 +99,38 @@ namespace Artemis.Profiles.Layers.Types.KeyPress private void KeyboardHookOnKeyDownCallback(KeyEventArgs e) { - if (_properties == null) + if (_layerModel == null) return; + // Reset animation progress if layer wasn't drawn for 100ms + if (new TimeSpan(0, 0, 0, 0, 100) < DateTime.Now - _layerModel.LastRender) + return; + + lock (_keyPressLayers) + { + if (_keyPressLayers.Count >= 25) + return; + } + var keyMatch = _mainManager.DeviceManager.ActiveKeyboard.GetKeyPosition(e.KeyCode); if (keyMatch == null) return; lock (_keyPressLayers) { + var properties = (KeyPressPropertiesModel) _layerModel.Properties; var layer = LayerModel.CreateLayer(); - layer.Properties.X = keyMatch.Value.X - _properties.Scale/2; - layer.Properties.Y = keyMatch.Value.Y - _properties.Scale/2; - layer.Properties.Width = _properties.Scale; - layer.Properties.Height = _properties.Scale; - layer.Properties.AnimationSpeed = _properties.AnimationSpeed; + layer.Properties.X = keyMatch.Value.X - properties.Scale/2; + layer.Properties.Y = keyMatch.Value.Y - properties.Scale/2; + layer.Properties.Width = properties.Scale; + layer.Properties.Height = properties.Scale; + layer.Properties.AnimationSpeed = properties.AnimationSpeed; layer.LayerAnimation = new GrowAnimation(); // Setup the brush according to settings - layer.Properties.Brush = _properties.RandomColor - ? ColorHelpers.RandomizeBrush(_properties.Brush) - : _properties.Brush.CloneCurrentValue(); + layer.Properties.Brush = properties.RandomColor + ? ColorHelpers.RandomizeBrush(properties.Brush) + : properties.Brush.CloneCurrentValue(); _keyPressLayers.Add(layer); } diff --git a/Artemis/Artemis/Profiles/ProfileModel.cs b/Artemis/Artemis/Profiles/ProfileModel.cs index 3208d3a59..145eb1917 100644 --- a/Artemis/Artemis/Profiles/ProfileModel.cs +++ b/Artemis/Artemis/Profiles/ProfileModel.cs @@ -25,8 +25,13 @@ namespace Artemis.Profiles DrawingVisual = new DrawingVisual(); } - public ChildItemCollection Layers { get; } + /// + /// Indicates whether the profile is actively being rendered + /// + [JsonIgnore] + public bool IsActive { get; set; } + public ChildItemCollection Layers { get; } public string Name { get; set; } public bool IsDefault { get; set; } public string KeyboardSlug { get; set; } @@ -44,30 +49,6 @@ namespace Artemis.Profiles Layers[i].Order = i; } - public void DrawLayers(Graphics keyboard, Rect keyboardRect, IDataModel dataModel, bool preview, - bool updateAnimations) - { - var visual = new DrawingVisual(); - using (var c = visual.RenderOpen()) - { - // Setup the DrawingVisual's size - c.PushClip(new RectangleGeometry(keyboardRect)); - c.DrawRectangle(new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)), null, keyboardRect); - - // Draw the layers - foreach (var layerModel in Layers.OrderByDescending(l => l.Order)) - { - layerModel.Update(dataModel, preview, updateAnimations); - layerModel.Draw(dataModel, c, preview, updateAnimations); - } - // Remove the clip - c.Pop(); - } - - using (var bmp = ImageUtilities.DrawingVisualToBitmap(visual, keyboardRect)) - keyboard.DrawImage(bmp, new PointF(0, 0)); - } - /// /// Gives all the layers and their children in a flat list /// @@ -97,14 +78,12 @@ namespace Artemis.Profiles var layers = new List(); foreach (var layerModel in Layers.OrderByDescending(l => l.Order)) { - if (!layerModel.Enabled || keyboardOnly && layerModel.LayerType.DrawType != DrawType.Keyboard) + if (!layerModel.Enabled || (keyboardOnly && (layerModel.LayerType.DrawType != DrawType.Keyboard))) continue; if (!ignoreConditions) - { if (!layerModel.ConditionsMet(dataModel)) continue; - } layers.Add(layerModel); layers.AddRange(layerModel.GetRenderLayers(dataModel, keyboardOnly, ignoreConditions)); @@ -144,7 +123,9 @@ namespace Artemis.Profiles } using (var bmp = ImageUtilities.DrawingVisualToBitmap(visual, rect)) + { g.DrawImage(bmp, new PointF(0, 0)); + } } ///