From 5efacb381d532e2ba95cc2cb0c7bbee06b91f3e0 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Tue, 1 Nov 2016 11:55:31 +0100 Subject: [PATCH] Added the UI for the AmbientLight-settings --- Artemis/Artemis/Artemis.csproj | 3 + .../AmbientLightPropertiesModel.cs | 2 + .../AmbientLightPropertiesView.xaml | 144 +++++++++++++++++- .../AmbientLightPropertiesViewModel.cs | 4 - .../Types/AmbientLight/AmbientLightType.cs | 18 ++- .../Helper/CheckboxEnumFlagHelper.cs | 93 +++++++++++ .../AmbientLight/Model/AmbienceCreatorType.cs | 10 ++ .../Model/Extensions/AvgColorExtension.cs | 4 + .../Model/Extensions/EnumExtension.cs | 26 ++++ .../Model/Extensions/PixelDataExtension.cs | 4 + .../Types/AmbientLight/Model/SmoothMode.cs | 11 +- 11 files changed, 308 insertions(+), 11 deletions(-) create mode 100644 Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Helper/CheckboxEnumFlagHelper.cs create mode 100644 Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/AmbienceCreatorType.cs create mode 100644 Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/Extensions/EnumExtension.cs diff --git a/Artemis/Artemis/Artemis.csproj b/Artemis/Artemis/Artemis.csproj index 2578762e6..8aba00f66 100644 --- a/Artemis/Artemis/Artemis.csproj +++ b/Artemis/Artemis/Artemis.csproj @@ -409,8 +409,11 @@ + + + diff --git a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightPropertiesModel.cs b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightPropertiesModel.cs index 26d1ab785..0559aea6a 100644 --- a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightPropertiesModel.cs +++ b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightPropertiesModel.cs @@ -13,6 +13,8 @@ namespace Artemis.Profiles.Layers.Types.AmbientLight [JsonIgnore] public Brush AmbientLightBrush { get; set; } + public AmbienceCreatorType AmbienceCreatorType { get; set; } = AmbienceCreatorType.Mirror; + public int OffsetLeft { get; set; } = 0; public int OffsetRight { get; set; } = 0; public int OffsetTop { get; set; } = 0; diff --git a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightPropertiesView.xaml b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightPropertiesView.xaml index 31cf9ccf7..3ff68ebaf 100644 --- a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightPropertiesView.xaml +++ b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightPropertiesView.xaml @@ -2,10 +2,150 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:converters="clr-namespace:Artemis.Utilities.Converters" + xmlns:system="clr-namespace:System;assembly=mscorlib" + xmlns:model="clr-namespace:Artemis.Profiles.Layers.Types.AmbientLight.Model" + xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls" + xmlns:helper="clr-namespace:Artemis.Profiles.Layers.Types.AmbientLight.Helper" mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="500"> + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightPropertiesViewModel.cs b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightPropertiesViewModel.cs index 10972a327..4926ff2c4 100644 --- a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightPropertiesViewModel.cs +++ b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightPropertiesViewModel.cs @@ -5,10 +5,6 @@ namespace Artemis.Profiles.Layers.Types.AmbientLight { public class AmbientLightPropertiesViewModel : LayerPropertiesViewModel { - #region Properties & Fields - - #endregion - #region Constructors public AmbientLightPropertiesViewModel(LayerEditorViewModel editorVm) diff --git a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightType.cs b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightType.cs index 138f2c02a..67c53fae2 100644 --- a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightType.cs +++ b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/AmbientLightType.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; @@ -7,6 +8,7 @@ using Artemis.Profiles.Layers.Abstract; using Artemis.Profiles.Layers.Interfaces; using Artemis.Profiles.Layers.Models; using Artemis.Profiles.Layers.Types.AmbientLight.AmbienceCreator; +using Artemis.Profiles.Layers.Types.AmbientLight.Model; using Artemis.Profiles.Layers.Types.AmbientLight.Model.Extensions; using Artemis.Profiles.Layers.Types.AmbientLight.ScreenCapturing; using Artemis.ViewModels.Profiles; @@ -22,6 +24,8 @@ namespace Artemis.Profiles.Layers.Types.AmbientLight public bool ShowInEdtor => true; public DrawType DrawType => DrawType.Keyboard; + [JsonIgnore] + private AmbienceCreatorType? _lastAmbienceCreatorType = null; [JsonIgnore] private IAmbienceCreator _lastAmbienceCreator; @@ -57,7 +61,7 @@ namespace Artemis.Profiles.Layers.Types.AmbientLight int height = (int)Math.Round(properties.Height); byte[] data = ScreenCaptureManager.GetLastScreenCapture(); - byte[] newData = GetAmbienceCreator().GetAmbience(data, ScreenCaptureManager.LastCaptureWidth, ScreenCaptureManager.LastCaptureHeight, width, height, properties); + byte[] newData = GetAmbienceCreator(properties).GetAmbience(data, ScreenCaptureManager.LastCaptureWidth, ScreenCaptureManager.LastCaptureHeight, width, height, properties); _lastData = _lastData?.Blend(newData, properties.SmoothMode) ?? newData; int stride = (width * ScreenCaptureManager.LastCapturePixelFormat.BitsPerPixel + 7) / 8; @@ -86,10 +90,16 @@ namespace Artemis.Profiles.Layers.Types.AmbientLight return new DrawingImage(visual.Drawing); } - private IAmbienceCreator GetAmbienceCreator() + private IAmbienceCreator GetAmbienceCreator(AmbientLightPropertiesModel properties) { - //TODO DarthAffe 30.10.2016: Create from settings - return _lastAmbienceCreator ?? (_lastAmbienceCreator = new AmbienceCreatorMirror()); + if (_lastAmbienceCreatorType == properties.AmbienceCreatorType) + return _lastAmbienceCreator; + + switch (properties.AmbienceCreatorType) + { + case AmbienceCreatorType.Mirror: return _lastAmbienceCreator = new AmbienceCreatorMirror(); + default: throw new InvalidEnumArgumentException(); + } } #endregion diff --git a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Helper/CheckboxEnumFlagHelper.cs b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Helper/CheckboxEnumFlagHelper.cs new file mode 100644 index 000000000..6ba6a423c --- /dev/null +++ b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Helper/CheckboxEnumFlagHelper.cs @@ -0,0 +1,93 @@ +using System; +using System.Windows; +using System.Windows.Controls; +using Artemis.Profiles.Layers.Types.AmbientLight.Model.Extensions; + +namespace Artemis.Profiles.Layers.Types.AmbientLight.Helper +{ + public class CheckboxEnumFlagHelper + { + #region DependencyProperties + // ReSharper disable InconsistentNaming + + public static readonly DependencyProperty FlagsProperty = DependencyProperty.RegisterAttached( + "Flags", typeof(Enum), typeof(CheckboxEnumFlagHelper), new FrameworkPropertyMetadata(default(Enum), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, FlagsChanged)); + + public static void SetFlags(DependencyObject element, Enum value) + { + element.SetValue(FlagsProperty, value); + } + + public static Enum GetFlags(DependencyObject element) + { + return (Enum)element.GetValue(FlagsProperty); + } + + public static readonly DependencyProperty ValueProperty = DependencyProperty.RegisterAttached( + "Value", typeof(Enum), typeof(CheckboxEnumFlagHelper), new PropertyMetadata(default(Enum), ValueChanged)); + + public static void SetValue(DependencyObject element, Enum value) + { + element.SetValue(ValueProperty, value); + } + + public static Enum GetValue(DependencyObject element) + { + return (Enum)element.GetValue(ValueProperty); + } + + // ReSharper restore InconsistentNaming + #endregion + + #region Methods + + private static void FlagsChanged(DependencyObject dependencyObject, + DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) + { + UpdateTarget(dependencyObject as CheckBox, dependencyPropertyChangedEventArgs.NewValue as Enum); + } + + private static void ValueChanged(DependencyObject dependencyObject, + DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) + { + CheckBox checkbox = dependencyObject as CheckBox; + if (checkbox == null) return; + + checkbox.Checked -= UpdateSource; + checkbox.Unchecked -= UpdateSource; + + if (dependencyPropertyChangedEventArgs.NewValue != null) + { + checkbox.Checked += UpdateSource; + checkbox.Unchecked += UpdateSource; + } + + UpdateTarget(checkbox, GetFlags(checkbox)); + } + + private static void UpdateTarget(CheckBox checkbox, Enum flags) + { + if (checkbox == null) return; + + Enum value = GetValue(checkbox); + checkbox.IsChecked = value != null && (flags?.HasFlag(value) ?? false); + } + + private static void UpdateSource(object sender, RoutedEventArgs routedEventArgs) + { + CheckBox checkbox = sender as CheckBox; + if (checkbox == null) return; + + Enum flags = GetFlags(checkbox); + Enum value = GetValue(checkbox); + if (value == null) return; + + if (checkbox.IsChecked ?? false) + SetFlags(checkbox, flags == null ? value : flags.SetFlag(value, true, flags.GetType())); + else + SetFlags(checkbox, flags?.SetFlag(value, false, flags.GetType())); + } + + #endregion + } +} diff --git a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/AmbienceCreatorType.cs b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/AmbienceCreatorType.cs new file mode 100644 index 000000000..9f4bc8acf --- /dev/null +++ b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/AmbienceCreatorType.cs @@ -0,0 +1,10 @@ +using System.ComponentModel; + +namespace Artemis.Profiles.Layers.Types.AmbientLight.Model +{ + public enum AmbienceCreatorType + { + [Description("Mirror")] + Mirror + } +} diff --git a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/Extensions/AvgColorExtension.cs b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/Extensions/AvgColorExtension.cs index a729ae89f..551f1c681 100644 --- a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/Extensions/AvgColorExtension.cs +++ b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/Extensions/AvgColorExtension.cs @@ -5,6 +5,8 @@ namespace Artemis.Profiles.Layers.Types.AmbientLight.Model.Extensions { public static class AvgColorExtension { + #region Methods + public static AvgColor[] Flip(this AvgColor[] colors, int width, FlipMode flipMode) { if (colors == null || width <= 0) return colors; @@ -55,5 +57,7 @@ namespace Artemis.Profiles.Layers.Types.AmbientLight.Model.Extensions return newData; } + + #endregion } } diff --git a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/Extensions/EnumExtension.cs b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/Extensions/EnumExtension.cs new file mode 100644 index 000000000..65f1ea6f9 --- /dev/null +++ b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/Extensions/EnumExtension.cs @@ -0,0 +1,26 @@ +using System; + +namespace Artemis.Profiles.Layers.Types.AmbientLight.Model.Extensions +{ + public static class EnumExtension + { + #region Methods + + public static Enum SetFlag(this Enum e, Enum value, bool set, Type t) + { + if (e == null || value == null || t == null) return e; + + int eValue = Convert.ToInt32(e); + int valueValue = Convert.ToInt32(value); + + if (set) + eValue |= valueValue; + else + eValue &= ~valueValue; + + return (Enum)Enum.ToObject(t, eValue); + } + + #endregion + } +} diff --git a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/Extensions/PixelDataExtension.cs b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/Extensions/PixelDataExtension.cs index 41a6d0457..5fa442669 100644 --- a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/Extensions/PixelDataExtension.cs +++ b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/Extensions/PixelDataExtension.cs @@ -4,6 +4,8 @@ namespace Artemis.Profiles.Layers.Types.AmbientLight.Model.Extensions { public static class PixelDataExtension { + #region Methods + public static int DetectBlackBarLeft(this byte[] pixels, int width, int height, int offsetLeft, int offsetRight, int offsetTop, int offsetBottom) { int bottomBorder = height - offsetBottom; @@ -103,5 +105,7 @@ namespace Artemis.Profiles.Layers.Types.AmbientLight.Model.Extensions double calcF = Math.Max(0, Math.Min(1, d)); return (byte)(calcF.Equals(1) ? 255 : calcF * 256); } + + #endregion } } diff --git a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/SmoothMode.cs b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/SmoothMode.cs index d32edbd43..19c2bd3fd 100644 --- a/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/SmoothMode.cs +++ b/Artemis/Artemis/Profiles/Layers/Types/AmbientLight/Model/SmoothMode.cs @@ -1,10 +1,19 @@ -namespace Artemis.Profiles.Layers.Types.AmbientLight.Model +using System.ComponentModel; + +namespace Artemis.Profiles.Layers.Types.AmbientLight.Model { public enum SmoothMode { + [Description("None")] None, + + [Description("Low")] Low, + + [Description("Medium")] Medium, + + [Description("High")] High } }