Refactored shape brushes to use the properties system

This commit is contained in:
Robert committed 2020-02-11 16:36:34 +01:00
1 parent 128a656f4b
commit ebea8b16a3
40 files changed
+650 -686

No files matched your search

@@ -90,8 +90,6 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="ColorBrushSettings.cs" />
<Compile Include="ColorBrushViewModel.cs" />
<Compile Include="ColorBrush.cs" />
<Compile Include="ColorBrushProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -114,12 +112,6 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Page Include="ColorBrushView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Profile.LayerProperties;
using Artemis.Core.Plugins.LayerBrush;
using SkiaSharp;
@@ -10,12 +11,15 @@ namespace Artemis.Plugins.LayerBrushes.Color
public class ColorBrush : LayerBrush
{
private readonly List<SKColor> _testColors;
private SKColor _color;
private SKPaint _paint;
private SKShader _shader;
public ColorBrush(Layer layer, ColorBrushSettings settings, LayerBrushDescriptor descriptor) : base(layer, settings, descriptor)
public ColorBrush(Layer layer, LayerBrushDescriptor descriptor) : base(layer, descriptor)
{
Settings = settings;
ColorProperty = RegisterLayerProperty<SKColor>("Brush.Color", "Main color", "The color of the brush.");
GradientTypeProperty = RegisterLayerProperty<GradientType>("Brush.GradientType", "Gradient type", "The scale of the noise.");
GradientTypeProperty.CanUseKeyframes = false;
_testColors = new List<SKColor>();
for (var i = 0; i < 9; i++)
@@ -28,19 +32,19 @@ namespace Artemis.Plugins.LayerBrushes.Color
CreateShader();
Layer.RenderPropertiesUpdated += (sender, args) => CreateShader();
Settings.PropertyChanged += (sender, args) => CreateShader();
}
public new ColorBrushSettings Settings { get; }
public LayerProperty<SKColor> ColorProperty { get; set; }
public LayerProperty<GradientType> GradientTypeProperty { get; set; }
private void CreateShader()
{
var center = new SKPoint(Layer.Bounds.MidX, Layer.Bounds.MidY);
SKShader shader;
switch (Settings.GradientType)
switch (GradientTypeProperty.CurrentValue)
{
case GradientType.Solid:
shader = SKShader.CreateColor(_testColors.First());
shader = SKShader.CreateColor(_color);
break;
case GradientType.LinearGradient:
shader = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(Layer.Bounds.Width, 0), _testColors.ToArray(), SKShaderTileMode.Repeat);
@@ -63,9 +67,16 @@ namespace Artemis.Plugins.LayerBrushes.Color
oldPaint?.Dispose();
}
public override LayerBrushViewModel GetViewModel()
public override void Update(double deltaTime)
{
return new ColorBrushViewModel(this);
// Only recreate the shader if the color changed
if (_color != ColorProperty.CurrentValue)
{
_color = ColorProperty.CurrentValue;
CreateShader();
}
base.Update(deltaTime);
}
public override void Render(SKCanvas canvas, SKPath path, SKPaint paint)
@@ -74,4 +85,19 @@ namespace Artemis.Plugins.LayerBrushes.Color
canvas.DrawPath(path, paint);
}
}
public enum GradientType
{
[Description("Solid")]
Solid,
[Description("Linear Gradient")]
LinearGradient,
[Description("Radial Gradient")]
RadialGradient,
[Description("Sweep Gradient")]
SweepGradient
}
}
@@ -1,46 +0,0 @@
using System.Collections.Generic;
using System.ComponentModel;
using Artemis.Core.Plugins.LayerBrush;
using SkiaSharp;
namespace Artemis.Plugins.LayerBrushes.Color
{
public class ColorBrushSettings : LayerBrushSettings
{
private List<SKColor> _colors;
private GradientType _gradientType;
public ColorBrushSettings()
{
GradientType = GradientType.Solid;
Colors = new List<SKColor>();
}
public GradientType GradientType
{
get => _gradientType;
set => SetAndNotify(ref _gradientType, value);
}
public List<SKColor> Colors
{
get => _colors;
set => SetAndNotify(ref _colors, value);
}
}
public enum GradientType
{
[Description("Solid")]
Solid,
[Description("Linear Gradient")]
LinearGradient,
[Description("Radial Gradient")]
RadialGradient,
[Description("Sweep Gradient")]
SweepGradient
}
}
@@ -1,134 +0,0 @@
<UserControl x:Class="Artemis.Plugins.LayerBrushes.Color.ColorBrushView"
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:colorBrush="clr-namespace:Artemis.Plugins.LayerBrushes.Color"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
d:DataContext="{d:DesignInstance {x:Type colorBrush:ColorBrushViewModel}}">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Teal.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Teal.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid Margin="12">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- Brush type -->
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" VerticalAlignment="Center">
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}">Brush type</TextBlock>
</StackPanel>
<StackPanel Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">
<ComboBox HorizontalAlignment="Left"
ItemsSource="{Binding Path=BrushTypes}"
SelectedValuePath="Value"
DisplayMemberPath="Description"
SelectedValue="{Binding Path=ColorBrush.Settings.GradientType}" />
</StackPanel>
<Separator Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource MaterialDesignSeparator}" />
</Grid>
<!-- Sample 1 -->
<Grid Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}">Setting title</TextBlock>
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}" Foreground="{DynamicResource MaterialDesignCheckBoxDisabled}">Setting subtitle</TextBlock>
</StackPanel>
<StackPanel Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Default ToggleButton Style" />
</StackPanel>
<Separator Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource MaterialDesignSeparator}" />
</Grid>
<!-- Sample 1 -->
<Grid Grid.Row="3">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}">Setting title</TextBlock>
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}" Foreground="{DynamicResource MaterialDesignCheckBoxDisabled}">Setting subtitle</TextBlock>
</StackPanel>
<StackPanel Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Default ToggleButton Style" />
</StackPanel>
<Separator Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource MaterialDesignSeparator}" />
</Grid>
<!-- Setting 2 -->
<Grid Grid.Row="4">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}">Setting title</TextBlock>
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}" Foreground="{DynamicResource MaterialDesignCheckBoxDisabled}">Setting subtitle</TextBlock>
</StackPanel>
<StackPanel Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Default ToggleButton Style" />
</StackPanel>
<Separator Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource MaterialDesignSeparator}" />
</Grid>
<!-- Setting 2 -->
<Grid Grid.Row="5">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}">Setting title</TextBlock>
<TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}" Foreground="{DynamicResource MaterialDesignCheckBoxDisabled}">Setting subtitle</TextBlock>
</StackPanel>
<StackPanel Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Default ToggleButton Style" />
</StackPanel>
<Separator Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource MaterialDesignSeparator}" />
</Grid>
</Grid>
</UserControl>
@@ -1,17 +0,0 @@
using System.Collections.Generic;
using Artemis.Core.Plugins.LayerBrush;
using Artemis.UI.Shared.Utilities;
namespace Artemis.Plugins.LayerBrushes.Color
{
public class ColorBrushViewModel : LayerBrushViewModel
{
public ColorBrushViewModel(ColorBrush element) : base(element)
{
ColorBrush = element;
}
public ColorBrush ColorBrush { get; }
public IEnumerable<ValueDescription> BrushTypes => EnumUtilities.GetAllValuesAndDescriptions(typeof(GradientType));
}
}