Conditions - Refactor layer properties WIP (needed for consistency..)

This commit is contained in:
Robert committed 2020-04-28 17:29:20 +02:00
1 parent 145da2edac
commit f3898b5500
38 files changed
+809 -1099

No files matched your search

@@ -1,15 +1,11 @@
using System;
using System.ComponentModel;
using System.Linq;
using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Profile.Colors;
using Artemis.Core.Models.Profile.LayerProperties;
using Artemis.Core.Plugins.LayerBrush;
using SkiaSharp;
namespace Artemis.Plugins.LayerBrushes.Color
{
public class ColorBrush : LayerBrush
public class ColorBrush : LayerBrush<ColorBrushProperties>
{
private SKColor _color;
private SKPaint _paint;
@@ -18,33 +14,16 @@ namespace Artemis.Plugins.LayerBrushes.Color
public ColorBrush(Layer layer, LayerBrushDescriptor descriptor) : base(layer, descriptor)
{
GradientTypeProperty = RegisterLayerProperty("Brush.GradientType", "Gradient type", "The type of color brush to draw", GradientType.Solid);
GradientTypeProperty.CanUseKeyframes = false;
ColorProperty = RegisterLayerProperty("Brush.Color", "Color", "The color of the brush", new SKColor(255, 0, 0));
GradientProperty = RegisterLayerProperty("Brush.Gradient", "Gradient", "The gradient of the brush", new ColorGradient());
GradientProperty.CanUseKeyframes = false;
if (!GradientProperty.Value.Stops.Any())
GradientProperty.Value.MakeFabulous();
UpdateColorProperties();
Layer.RenderPropertiesUpdated += (sender, args) => CreateShader();
GradientTypeProperty.ValueChanged += (sender, args) => UpdateColorProperties();
ColorProperty.ValueChanged += (sender, args) => CreateShader();
GradientProperty.Value.PropertyChanged += (sender, args) => CreateShader();
}
public LayerProperty<SKColor> ColorProperty { get; set; }
public LayerProperty<ColorGradient> GradientProperty { get; set; }
public LayerProperty<GradientType> GradientTypeProperty { get; set; }
public override void Update(double deltaTime)
{
// Only check if a solid is being drawn, because that can be changed by keyframes
if (GradientTypeProperty.Value == GradientType.Solid && _color != ColorProperty.CurrentValue)
if (Properties.GradientType.BaseValue == GradientType.Solid && _color != Properties.Color.CurrentValue)
{
// If the color was changed since the last frame, recreate the shader
_color = ColorProperty.CurrentValue;
_color = Properties.Color.CurrentValue;
CreateShader();
}
@@ -63,36 +42,35 @@ namespace Artemis.Plugins.LayerBrushes.Color
canvas.DrawPath(path, paint);
}
private void UpdateColorProperties()
protected override void OnPropertiesInitialized()
{
ColorProperty.IsHidden = GradientTypeProperty.Value != GradientType.Solid;
GradientProperty.IsHidden = GradientTypeProperty.Value == GradientType.Solid;
CreateShader();
Properties.GradientType.BaseValueChanged += (sender, args) => CreateShader();
Properties.Color.BaseValueChanged += (sender, args) => CreateShader();
Properties.Gradient.BaseValue.PropertyChanged += (sender, args) => CreateShader();
}
private void CreateShader()
{
var center = new SKPoint(_shaderBounds.MidX, _shaderBounds.MidY);
var shader = GradientTypeProperty.CurrentValue switch
var shader = Properties.GradientType.CurrentValue switch
{
GradientType.Solid => SKShader.CreateColor(_color),
GradientType.LinearGradient => SKShader.CreateLinearGradient(
new SKPoint(_shaderBounds.Left, _shaderBounds.Top),
new SKPoint(_shaderBounds.Right, _shaderBounds.Top),
GradientProperty.Value.GetColorsArray(),
GradientProperty.Value.GetPositionsArray(),
Properties.Gradient.BaseValue.GetColorsArray(),
Properties.Gradient.BaseValue.GetPositionsArray(),
SKShaderTileMode.Repeat),
GradientType.RadialGradient => SKShader.CreateRadialGradient(
center,
Math.Min(_shaderBounds.Width, _shaderBounds.Height),
GradientProperty.Value.GetColorsArray(),
GradientProperty.Value.GetPositionsArray(),
Properties.Gradient.BaseValue.GetColorsArray(),
Properties.Gradient.BaseValue.GetPositionsArray(),
SKShaderTileMode.Repeat),
GradientType.SweepGradient => SKShader.CreateSweepGradient(
center,
GradientProperty.Value.GetColorsArray(),
GradientProperty.Value.GetPositionsArray(),
Properties.Gradient.BaseValue.GetColorsArray(),
Properties.Gradient.BaseValue.GetPositionsArray(),
SKShaderTileMode.Clamp,
0,
360),
@@ -107,19 +85,4 @@ namespace Artemis.Plugins.LayerBrushes.Color
oldPaint?.Dispose();
}
}
public enum GradientType
{
[Description("Solid")]
Solid,
[Description("Linear Gradient")]
LinearGradient,
[Description("Radial Gradient")]
RadialGradient,
[Description("Sweep Gradient")]
SweepGradient
}
}
@@ -0,0 +1,59 @@
using System;
using System.ComponentModel;
using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Profile.Colors;
using Artemis.Core.Models.Profile.LayerProperties.Attributes;
using Artemis.Core.Models.Profile.LayerProperties.Types;
using SkiaSharp;
namespace Artemis.Plugins.LayerBrushes.Color
{
public class ColorBrushProperties : LayerPropertyGroup
{
[PropertyDescription(Description = "The type of color brush to draw")]
public EnumLayerProperty<GradientType> GradientType { get; set; }
[PropertyDescription(Description = "The color of the brush")]
public SKColorLayerProperty Color { get; set; }
[PropertyDescription(Description = "The gradient of the brush")]
public ColorGradientLayerProperty Gradient { get; set; }
protected override void OnPropertiesInitialized()
{
// Populate defaults
if (!GradientType.IsLoadedFromStorage)
GradientType.BaseValue = LayerBrushes.Color.GradientType.Solid;
if (!Color.IsLoadedFromStorage)
Color.BaseValue = new SKColor(255, 0, 0);
if (!Gradient.IsLoadedFromStorage)
{
Gradient.BaseValue = new ColorGradient();
Gradient.BaseValue.MakeFabulous();
}
GradientType.BaseValueChanged += GradientTypeOnBaseValueChanged;
}
private void GradientTypeOnBaseValueChanged(object? sender, EventArgs e)
{
Color.IsHidden = GradientType.BaseValue != LayerBrushes.Color.GradientType.Solid;
Gradient.IsHidden = GradientType.BaseValue == LayerBrushes.Color.GradientType.Solid;
}
}
public enum GradientType
{
[Description("Solid")]
Solid,
[Description("Linear Gradient")]
LinearGradient,
[Description("Radial Gradient")]
RadialGradient,
[Description("Sweep Gradient")]
SweepGradient
}
}
@@ -1,9 +1,6 @@
using System;
using System.ComponentModel;
using System.Linq;
using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Profile.Colors;
using Artemis.Core.Models.Profile.LayerProperties;
using Artemis.Core.Plugins.LayerBrush;
using Artemis.Core.Services.Interfaces;
using Artemis.Plugins.LayerBrushes.Noise.Utilities;
@@ -11,18 +8,18 @@ using SkiaSharp;
namespace Artemis.Plugins.LayerBrushes.Noise
{
public class NoiseBrush : LayerBrush
public class NoiseBrush : LayerBrush<NoiseBrushProperties>
{
private static readonly Random Rand = new Random();
private readonly OpenSimplexNoise _noise;
private readonly IRgbService _rgbService;
private SKBitmap _bitmap;
private SKColor[] _colorMap;
private float _renderScale;
private float _x;
private float _y;
private float _z;
private SKColor[] _colorMap;
public NoiseBrush(Layer layer, LayerBrushDescriptor descriptor, IRgbService rgbService) : base(layer, descriptor)
{
@@ -31,58 +28,15 @@ namespace Artemis.Plugins.LayerBrushes.Noise
_y = Rand.Next(0, 4096);
_z = Rand.Next(0, 4096);
_noise = new OpenSimplexNoise(Rand.Next(0, 4096));
DetermineRenderScale();
ColorTypeProperty = RegisterLayerProperty("Brush.ColorType", "Color mapping type", "The way the noise is converted to colors", ColorMappingType.Simple);
ColorTypeProperty.CanUseKeyframes = false;
ColorTypeProperty.ValueChanged += (sender, args) => UpdateColorProperties();
ScaleProperty = RegisterLayerProperty("Brush.Scale", "Scale", "The scale of the noise.", new SKSize(100, 100));
ScaleProperty.MinInputValue = 0f;
HardnessProperty = RegisterLayerProperty("Brush.Hardness", "Hardness", "The hardness of the noise, lower means there are gradients in the noise, higher means hard lines", 500f);
HardnessProperty.MinInputValue = 0f;
HardnessProperty.MaxInputValue = 2048f;
ScrollSpeedProperty = RegisterLayerProperty<SKPoint>("Brush.ScrollSpeed", "Movement speed", "The speed at which the noise moves vertically and horizontally");
ScrollSpeedProperty.MinInputValue = -64f;
ScrollSpeedProperty.MaxInputValue = 64f;
AnimationSpeedProperty = RegisterLayerProperty("Brush.AnimationSpeed", "Animation speed", "The speed at which the noise moves", 25f);
AnimationSpeedProperty.MinInputValue = 0f;
AnimationSpeedProperty.MaxInputValue = 64f;
ScaleProperty.InputAffix = "%";
MainColorProperty = RegisterLayerProperty("Brush.MainColor", "Main color", "The main color of the noise", new SKColor(255, 0, 0));
SecondaryColorProperty = RegisterLayerProperty("Brush.SecondaryColor", "Secondary color", "The secondary color of the noise", new SKColor(0, 0, 255));
GradientColorProperty = RegisterLayerProperty("Brush.GradientColor", "Noise gradient map", "The gradient the noise will map it's value to", new ColorGradient());
GradientColorProperty.CanUseKeyframes = false;
if (!GradientColorProperty.Value.Stops.Any())
GradientColorProperty.Value.MakeFabulous();
GradientColorProperty.Value.PropertyChanged += CreateColorMap;
UpdateColorProperties();
CreateColorMap(null, null);
}
public LayerProperty<ColorMappingType> ColorTypeProperty { get; set; }
public LayerProperty<SKColor> MainColorProperty { get; set; }
public LayerProperty<SKColor> SecondaryColorProperty { get; set; }
public LayerProperty<ColorGradient> GradientColorProperty { get; set; }
public LayerProperty<SKSize> ScaleProperty { get; set; }
public LayerProperty<float> HardnessProperty { get; set; }
public LayerProperty<SKPoint> ScrollSpeedProperty { get; set; }
public LayerProperty<float> AnimationSpeedProperty { get; set; }
private void UpdateColorProperties()
{
GradientColorProperty.IsHidden = ColorTypeProperty.Value != ColorMappingType.Gradient;
MainColorProperty.IsHidden = ColorTypeProperty.Value != ColorMappingType.Simple;
SecondaryColorProperty.IsHidden = ColorTypeProperty.Value != ColorMappingType.Simple;
}
public override void Update(double deltaTime)
{
_x += ScrollSpeedProperty.CurrentValue.X / 500f / (float) deltaTime;
_y += ScrollSpeedProperty.CurrentValue.Y / 500f / (float) deltaTime;
_z += AnimationSpeedProperty.CurrentValue / 500f / 0.04f * (float) deltaTime;
_x += Properties.ScrollSpeed.CurrentValue.X / 500f / (float) deltaTime;
_y += Properties.ScrollSpeed.CurrentValue.Y / 500f / (float) deltaTime;
_z += Properties.AnimationSpeed.CurrentValue / 500f / 0.04f * (float) deltaTime;
// A telltale sign of someone who can't do math very well
if (float.IsPositiveInfinity(_x) || float.IsNegativeInfinity(_x) || float.IsNaN(_x))
@@ -98,11 +52,11 @@ namespace Artemis.Plugins.LayerBrushes.Noise
public override void Render(SKCanvas canvas, SKImageInfo canvasInfo, SKPath path, SKPaint paint)
{
var mainColor = MainColorProperty?.CurrentValue;
var gradientColor = GradientColorProperty?.CurrentValue;
var scale = ScaleProperty.CurrentValue;
var mainColor = Properties.MainColor?.CurrentValue;
var gradientColor = Properties.GradientColor?.CurrentValue;
var scale = Properties.Scale.CurrentValue;
var opacity = mainColor != null ? (float) Math.Round(mainColor.Value.Alpha / 255.0, 2, MidpointRounding.AwayFromZero) : 0;
var hardness = 127 + HardnessProperty.CurrentValue;
var hardness = 127 + Properties.Hardness.CurrentValue;
// Scale down the render path to avoid computing a value for every pixel
var width = (int) Math.Floor(path.Bounds.Width * _renderScale);
@@ -122,10 +76,8 @@ namespace Artemis.Plugins.LayerBrushes.Noise
var v = _noise.Evaluate(evalX, evalY, _z);
var alpha = (byte) Math.Max(0, Math.Min(255, v * hardness));
if (ColorTypeProperty.Value == ColorMappingType.Simple && mainColor != null)
{
if (Properties.ColorType.BaseValue == ColorMappingType.Simple && mainColor != null)
_bitmap.SetPixel(x, y, new SKColor(mainColor.Value.Red, mainColor.Value.Green, mainColor.Value.Blue, (byte) (alpha * opacity)));
}
else if (gradientColor != null && _colorMap.Length == 101)
{
var color = _colorMap[(int) Math.Round(alpha / 255f * 100, MidpointRounding.AwayFromZero)];
@@ -141,9 +93,9 @@ namespace Artemis.Plugins.LayerBrushes.Noise
);
canvas.ClipPath(path);
if (ColorTypeProperty.Value == ColorMappingType.Simple)
if (Properties.ColorType.BaseValue == ColorMappingType.Simple)
{
using var backgroundShader = SKShader.CreateColor(SecondaryColorProperty.CurrentValue);
using var backgroundShader = SKShader.CreateColor(Properties.SecondaryColor.CurrentValue);
paint.Shader = backgroundShader;
canvas.DrawRect(path.Bounds, paint);
}
@@ -153,6 +105,17 @@ namespace Artemis.Plugins.LayerBrushes.Noise
canvas.DrawRect(path.Bounds, paint);
}
protected override void OnPropertiesInitialized()
{
Properties.GradientColor.BaseValue.PropertyChanged += GradientColorChanged;
CreateColorMap();
}
private void GradientColorChanged(object sender, PropertyChangedEventArgs e)
{
CreateColorMap();
}
private void DetermineRenderScale()
{
@@ -170,11 +133,11 @@ namespace Artemis.Plugins.LayerBrushes.Noise
}
}
private void CreateColorMap(object sender, EventArgs e)
private void CreateColorMap()
{
var colorMap = new SKColor[101];
for (var i = 0; i < 101; i++)
colorMap[i] = GradientColorProperty.Value.GetColor(i / 100f);
colorMap[i] = Properties.GradientColor.BaseValue.GetColor(i / 100f);
_colorMap = colorMap;
}
@@ -0,0 +1,67 @@
using System;
using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Profile.Colors;
using Artemis.Core.Models.Profile.LayerProperties.Attributes;
using Artemis.Core.Models.Profile.LayerProperties.Types;
using SkiaSharp;
namespace Artemis.Plugins.LayerBrushes.Noise
{
public class NoiseBrushProperties : LayerPropertyGroup
{
[PropertyDescription(Name = "Color mapping type", Description = "The way the noise is converted to colors")]
public EnumLayerProperty<ColorMappingType> ColorType { get; set; }
[PropertyDescription(Description = "The main color of the noise")]
public SKColorLayerProperty MainColor { get; set; }
[PropertyDescription(Description = "The secondary color of the noise")]
public SKColorLayerProperty SecondaryColor { get; set; }
[PropertyDescription(Name = "Noise gradient map", Description = "The gradient the noise will map it's value to")]
public ColorGradientLayerProperty GradientColor { get; set; }
[PropertyDescription(Description = "The scale of the noise", MinInputValue = 0f, InputAffix = "%")]
public SKSizeLayerProperty Scale { get; set; }
[PropertyDescription(Description = "The hardness of the noise, lower means there are gradients in the noise, higher means hard lines", MinInputValue = 0f, MaxInputValue = 2048f)]
public FloatLayerProperty Hardness { get; set; }
[PropertyDescription(Description = "The speed at which the noise moves vertically and horizontally", MinInputValue = -64f, MaxInputValue = 64f)]
public SKPointLayerProperty ScrollSpeed { get; set; }
[PropertyDescription(Description = "The speed at which the noise moves", MinInputValue = 0f, MaxInputValue = 64f)]
public FloatLayerProperty AnimationSpeed { get; set; }
protected override void OnPropertiesInitialized()
{
// Populate defaults
if (!MainColor.IsLoadedFromStorage)
MainColor.BaseValue = new SKColor(255, 0, 0);
if (!SecondaryColor.IsLoadedFromStorage)
SecondaryColor.BaseValue = new SKColor(0, 0, 255);
if (!GradientColor.IsLoadedFromStorage)
{
GradientColor.BaseValue = new ColorGradient();
GradientColor.BaseValue.MakeFabulous();
}
if (!Scale.IsLoadedFromStorage)
Scale.BaseValue = new SKSize(100, 100);
if (!Hardness.IsLoadedFromStorage)
Hardness.BaseValue = 500f;
if (!AnimationSpeed.IsLoadedFromStorage)
AnimationSpeed.BaseValue = 25f;
ColorType.BaseValueChanged += ColorTypeOnBaseValueChanged;
}
private void ColorTypeOnBaseValueChanged(object? sender, EventArgs e)
{
GradientColor.IsHidden = ColorType.BaseValue != ColorMappingType.Gradient;
MainColor.IsHidden = ColorType.BaseValue != ColorMappingType.Simple;
SecondaryColor.IsHidden = ColorType.BaseValue != ColorMappingType.Simple;
}
}
}