1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2026-01-02 10:43:31 +00:00

Simplified the different properties used for layer/shape rendering

Cleaned up UI layer shape service
This commit is contained in:
SpoinkyNL 2020-01-31 00:02:01 +01:00
parent 0794966e38
commit f2df51d40c
17 changed files with 192 additions and 284 deletions

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Net.Sockets;
using Artemis.Core.Exceptions; using Artemis.Core.Exceptions;
using Artemis.Core.Extensions; using Artemis.Core.Extensions;
using Artemis.Core.Models.Profile.LayerProperties; using Artemis.Core.Models.Profile.LayerProperties;
@ -20,6 +21,8 @@ namespace Artemis.Core.Models.Profile
private readonly Dictionary<string, BaseLayerProperty> _properties; private readonly Dictionary<string, BaseLayerProperty> _properties;
private LayerShape _layerShape; private LayerShape _layerShape;
private List<ArtemisLed> _leds; private List<ArtemisLed> _leds;
private SKPath _path;
private SKRect _bounds;
public Layer(Profile profile, ProfileElement parent, string name) public Layer(Profile profile, ProfileElement parent, string name)
{ {
@ -81,22 +84,25 @@ namespace Artemis.Core.Models.Profile
public ReadOnlyCollection<ArtemisLed> Leds => _leds.AsReadOnly(); public ReadOnlyCollection<ArtemisLed> Leds => _leds.AsReadOnly();
/// <summary> /// <summary>
/// An absolute rectangle to the surface that contains all the LEDs in this layer. /// A path containing all the LEDs this layer is applied to, any rendering outside the layer Path is clipped.
/// <para>For rendering, use the RenderRectangle on <see cref="LayerShape" />.</para> /// <para>For rendering, use the Path on <see cref="LayerShape" />.</para>
/// </summary> /// </summary>
public SKRect AbsoluteRectangle { get; private set; } public SKPath Path
{
get => _path;
private set
{
_path = value;
// I can't really be sure about the performance impact of calling Bounds often but
// SkiaSharp calls SkiaApi.sk_path_get_bounds (Handle, &rect); which sounds expensive
_bounds = value?.Bounds ?? SKRect.Empty;
}
}
/// <summary> /// <summary>
/// A zero-based rectangle that contains all the LEDs in this layer. /// The bounds of this layer
/// <para>For rendering, use the RenderRectangle on <see cref="LayerShape" />.</para>
/// </summary> /// </summary>
public SKRect Rectangle { get; private set; } public SKRect Bounds => _bounds;
/// <summary>
/// A path containing all the LEDs this layer is applied to.
/// <para>For rendering, use the RenderPath on <see cref="LayerShape" />.</para>
/// </summary>
public SKPath Path { get; private set; }
/// <summary> /// <summary>
/// Defines the shape that is rendered by the <see cref="LayerBrush" />. /// Defines the shape that is rendered by the <see cref="LayerBrush" />.
@ -173,19 +179,18 @@ namespace Artemis.Core.Models.Profile
var size = SizeProperty.CurrentValue; var size = SizeProperty.CurrentValue;
var rotation = RotationProperty.CurrentValue; var rotation = RotationProperty.CurrentValue;
var anchor = GetLayerAnchor(true); var anchor = GetLayerAnchor();
var relativeAnchor = GetLayerAnchor(false);
// Translation originates from the unscaled center of the shape and is tied to the anchor // Translation originates from the unscaled center of the shape and is tied to the anchor
var x = position.X * AbsoluteRectangle.Width - LayerShape.RenderRectangle.Width / 2 - relativeAnchor.X; var x = position.X * Bounds.Width - LayerShape.Bounds.Width / 2 - anchor.X;
var y = position.Y * AbsoluteRectangle.Height - LayerShape.RenderRectangle.Height / 2 - relativeAnchor.Y; var y = position.Y * Bounds.Height - LayerShape.Bounds.Height / 2 - anchor.Y;
canvas.RotateDegrees(rotation, anchor.X, anchor.Y); canvas.Translate(Bounds.Left + x, Bounds.Top + y);
canvas.Scale(size.Width, size.Height, anchor.X, anchor.Y); canvas.Scale(size.Width, size.Height, anchor.X, anchor.Y);
canvas.Translate(x, y); canvas.RotateDegrees(rotation, anchor.X, anchor.Y);
// Placeholder // Placeholder
if (LayerShape?.RenderPath != null) if (LayerShape?.Path != null)
{ {
var testColors = new List<SKColor>(); var testColors = new List<SKColor>();
for (var i = 0; i < 9; i++) for (var i = 0; i < 9; i++)
@ -196,33 +201,25 @@ namespace Artemis.Core.Models.Profile
testColors.Add(SKColor.FromHsv(0, 100, 100)); testColors.Add(SKColor.FromHsv(0, 100, 100));
} }
var shader = SKShader.CreateSweepGradient(new SKPoint(LayerShape.RenderRectangle.MidX, LayerShape.RenderRectangle.MidY), testColors.ToArray()); var shader = SKShader.CreateSweepGradient(new SKPoint(LayerShape.Bounds.MidX, LayerShape.Bounds.MidY), testColors.ToArray());
canvas.DrawPath(LayerShape.RenderPath, new SKPaint {Shader = shader, Color = new SKColor(0, 0, 0, (byte) (OpacityProperty.CurrentValue * 2.55f))}); canvas.DrawPath(LayerShape.Path, new SKPaint {Shader = shader, Color = new SKColor(0, 0, 0, (byte) (OpacityProperty.CurrentValue * 2.55f))});
} }
LayerBrush?.Render(canvas); LayerBrush?.Render(canvas);
canvas.Restore(); canvas.Restore();
} }
private SKPoint GetLayerAnchor(bool absolute) private SKPoint GetLayerAnchor()
{ {
if (LayerShape == null) if (LayerShape == null)
return SKPoint.Empty; return SKPoint.Empty;
if (!absolute)
{
var anchor = AnchorPointProperty.CurrentValue; var anchor = AnchorPointProperty.CurrentValue;
anchor.X = anchor.X * AbsoluteRectangle.Width; anchor.X = anchor.X * Bounds.Width;
anchor.Y = anchor.Y * AbsoluteRectangle.Height; anchor.Y = anchor.Y * Bounds.Height;
return new SKPoint(anchor.X, anchor.Y); return new SKPoint(anchor.X, anchor.Y);
} }
var position = PositionProperty.CurrentValue;
position.X = position.X * AbsoluteRectangle.Width;
position.Y = position.Y * AbsoluteRectangle.Height;
return new SKPoint(position.X + LayerShape.RenderRectangle.Left, position.Y + LayerShape.RenderRectangle.Top);
}
internal override void ApplyToEntity() internal override void ApplyToEntity()
{ {
// Properties // Properties
@ -325,31 +322,22 @@ namespace Artemis.Core.Models.Profile
{ {
if (!Leds.Any()) if (!Leds.Any())
{ {
AbsoluteRectangle = SKRect.Empty;
Rectangle = SKRect.Empty;
Path = new SKPath(); Path = new SKPath();
LayerShape?.CalculateRenderProperties();
OnRenderPropertiesUpdated(); OnRenderPropertiesUpdated();
return; return;
} }
// Determine to top-left and bottom-right
var minX = Leds.Min(l => l.AbsoluteRenderRectangle.Left);
var minY = Leds.Min(l => l.AbsoluteRenderRectangle.Top);
var maxX = Leds.Max(l => l.AbsoluteRenderRectangle.Right);
var maxY = Leds.Max(l => l.AbsoluteRenderRectangle.Bottom);
AbsoluteRectangle = SKRect.Create(minX, minY, maxX - minX, maxY - minY);
Rectangle = SKRect.Create(0, 0, maxX - minX, maxY - minY);
var path = new SKPath {FillType = SKPathFillType.Winding}; var path = new SKPath {FillType = SKPathFillType.Winding};
foreach (var artemisLed in Leds) foreach (var artemisLed in Leds)
path.AddRect(artemisLed.AbsoluteRenderRectangle); path.AddRect(artemisLed.AbsoluteRenderRectangle);
Path = path; Path = path;
// This is called here so that the shape's render properties are up to date when other code // This is called here so that the shape's render properties are up to date when other code
// responds to OnRenderPropertiesUpdated // responds to OnRenderPropertiesUpdated
LayerShape?.CalculateRenderProperties(); LayerShape?.CalculateRenderProperties();
OnRenderPropertiesUpdated(); OnRenderPropertiesUpdated();
} }

View File

@ -15,12 +15,9 @@ namespace Artemis.Core.Models.Profile.LayerShapes
public override void CalculateRenderProperties() public override void CalculateRenderProperties()
{ {
var unscaled = GetUnscaledRectangle();
RenderRectangle = SKRect.Create(0,0 , unscaled.Width, unscaled.Height);
var path = new SKPath(); var path = new SKPath();
path.AddOval(RenderRectangle); path.AddOval(GetUnscaledRectangle());
RenderPath = path; Path = path;
} }
internal override void ApplyToEntity() internal override void ApplyToEntity()

View File

@ -15,12 +15,8 @@ namespace Artemis.Core.Models.Profile.LayerShapes
public override void CalculateRenderProperties() public override void CalculateRenderProperties()
{ {
RenderRectangle = GetUnscaledRectangle();
// Shape originates from the center so compensate the path for that // Shape originates from the center so compensate the path for that
var renderPath = new SKPath(Layer.Path); Path = new SKPath(Layer.Path);
renderPath.Transform(SKMatrix.MakeTranslation(RenderRectangle.Left - Layer.Path.Bounds.Left, RenderRectangle.Top - Layer.Path.Bounds.Top));
RenderPath = renderPath;
} }
internal override void ApplyToEntity() internal override void ApplyToEntity()

View File

@ -1,12 +1,13 @@
using System.Linq; using System.Linq;
using Artemis.Storage.Entities.Profile; using Artemis.Storage.Entities.Profile;
using RGB.NET.Core;
using SkiaSharp; using SkiaSharp;
namespace Artemis.Core.Models.Profile.LayerShapes namespace Artemis.Core.Models.Profile.LayerShapes
{ {
public abstract class LayerShape public abstract class LayerShape
{ {
private SKPath _path;
protected LayerShape(Layer layer) protected LayerShape(Layer layer)
{ {
Layer = layer; Layer = layer;
@ -30,14 +31,22 @@ namespace Artemis.Core.Models.Profile.LayerShapes
public SKRect ScaledRectangle { get; set; } public SKRect ScaledRectangle { get; set; }
/// <summary> /// <summary>
/// An absolute and scaled render rectangle /// A path outlining the shape
/// </summary> /// </summary>
public SKRect RenderRectangle { get; protected set; } public SKPath Path
{
get => _path;
protected set
{
_path = value;
Bounds = value?.Bounds ?? SKRect.Empty;
}
}
/// <summary> /// <summary>
/// A path relative to the layer /// The bounds of this shape
/// </summary> /// </summary>
public SKPath RenderPath { get; protected set; } public SKRect Bounds { get; private set; }
public abstract void CalculateRenderProperties(); public abstract void CalculateRenderProperties();
@ -54,10 +63,10 @@ namespace Artemis.Core.Models.Profile.LayerShapes
} }
ScaledRectangle = SKRect.Create( ScaledRectangle = SKRect.Create(
100f / Layer.AbsoluteRectangle.Width * (rect.Left - Layer.AbsoluteRectangle.Left) / 100f, 100f / Layer.Bounds.Width * rect.Left / 100f,
100f / Layer.AbsoluteRectangle.Height * (rect.Top - Layer.AbsoluteRectangle.Top) / 100f, 100f / Layer.Bounds.Height * rect.Top / 100f,
100f / Layer.AbsoluteRectangle.Width * rect.Width / 100f, 100f / Layer.Bounds.Width * rect.Width / 100f,
100f / Layer.AbsoluteRectangle.Height * rect.Height / 100f 100f / Layer.Bounds.Height * rect.Height / 100f
); );
} }
@ -67,10 +76,10 @@ namespace Artemis.Core.Models.Profile.LayerShapes
return SKRect.Empty; return SKRect.Empty;
return SKRect.Create( return SKRect.Create(
Layer.AbsoluteRectangle.Left + Layer.AbsoluteRectangle.Width * ScaledRectangle.Left, Layer.Bounds.Width * ScaledRectangle.Left,
Layer.AbsoluteRectangle.Top + Layer.AbsoluteRectangle.Height * ScaledRectangle.Top, Layer.Bounds.Height * ScaledRectangle.Top,
Layer.AbsoluteRectangle.Width * ScaledRectangle.Width, Layer.Bounds.Width * ScaledRectangle.Width,
Layer.AbsoluteRectangle.Height * ScaledRectangle.Height Layer.Bounds.Height * ScaledRectangle.Height
); );
} }

View File

@ -25,23 +25,14 @@ namespace Artemis.Core.Models.Profile.LayerShapes
/// </summary> /// </summary>
public List<SKPoint> RenderPoints public List<SKPoint> RenderPoints
{ {
get get { return Points.Select(p => new SKPoint(p.X * Layer.Bounds.Width, p.Y * Layer.Bounds.Height)).ToList(); }
{
var x = Layer.Leds.Min(l => l.RgbLed.AbsoluteLedRectangle.Location.X);
var y = Layer.Leds.Min(l => l.RgbLed.AbsoluteLedRectangle.Location.Y);
var width = Layer.Leds.Max(l => l.RgbLed.AbsoluteLedRectangle.Location.X + l.RgbLed.AbsoluteLedRectangle.Size.Width) - x;
var height = Layer.Leds.Max(l => l.RgbLed.AbsoluteLedRectangle.Location.Y + l.RgbLed.AbsoluteLedRectangle.Size.Height) - y;
return Points.Select(p => new SKPoint((float) (p.X * width), (float) (p.Y * height))).ToList();
}
} }
public override void CalculateRenderProperties() public override void CalculateRenderProperties()
{ {
var path = new SKPath(); var path = new SKPath();
path.AddPoly(RenderPoints.ToArray()); path.AddPoly(RenderPoints.ToArray());
Path = path;
RenderPath = path;
RenderRectangle = path.GetRect();
} }
internal override void ApplyToEntity() internal override void ApplyToEntity()

View File

@ -15,11 +15,9 @@ namespace Artemis.Core.Models.Profile.LayerShapes
public override void CalculateRenderProperties() public override void CalculateRenderProperties()
{ {
RenderRectangle = GetUnscaledRectangle();
var path = new SKPath(); var path = new SKPath();
path.AddRect(RenderRectangle); path.AddRect(GetUnscaledRectangle());
RenderPath = path; Path = path;
} }
internal override void ApplyToEntity() internal override void ApplyToEntity()

View File

@ -35,7 +35,7 @@ namespace Artemis.Plugins.LayerBrushes.Color
private void CreateShader() private void CreateShader()
{ {
var center = new SKPoint(Layer.AbsoluteRectangle.MidX, Layer.AbsoluteRectangle.MidY); var center = new SKPoint(Layer.LayerShape.Bounds.MidX, Layer.LayerShape.Bounds.MidY);
SKShader shader; SKShader shader;
switch (Settings.GradientType) switch (Settings.GradientType)
{ {
@ -43,10 +43,10 @@ namespace Artemis.Plugins.LayerBrushes.Color
shader = SKShader.CreateColor(_testColors.First()); shader = SKShader.CreateColor(_testColors.First());
break; break;
case GradientType.LinearGradient: case GradientType.LinearGradient:
shader = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(Layer.AbsoluteRectangle.Width, 0), _testColors.ToArray(), SKShaderTileMode.Repeat); shader = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(Layer.LayerShape.Bounds.Width, 0), _testColors.ToArray(), SKShaderTileMode.Repeat);
break; break;
case GradientType.RadialGradient: case GradientType.RadialGradient:
shader = SKShader.CreateRadialGradient(center, Math.Min(Layer.AbsoluteRectangle.Width, Layer.AbsoluteRectangle.Height), _testColors.ToArray(), SKShaderTileMode.Repeat); shader = SKShader.CreateRadialGradient(center, Math.Min(Layer.LayerShape.Bounds.Width, Layer.LayerShape.Bounds.Height), _testColors.ToArray(), SKShaderTileMode.Repeat);
break; break;
case GradientType.SweepGradient: case GradientType.SweepGradient:
shader = SKShader.CreateSweepGradient(center, _testColors.ToArray(), null, SKShaderTileMode.Clamp, 0, 360); shader = SKShader.CreateSweepGradient(center, _testColors.ToArray(), null, SKShaderTileMode.Clamp, 0, 360);
@ -70,7 +70,7 @@ namespace Artemis.Plugins.LayerBrushes.Color
public override void Render(SKCanvas canvas) public override void Render(SKCanvas canvas)
{ {
canvas.DrawPath(Layer.LayerShape.RenderPath, _paint); canvas.DrawPath(Layer.LayerShape.Path, _paint);
} }
} }
} }

View File

@ -43,8 +43,8 @@ namespace Artemis.Plugins.LayerBrushes.Noise
public override void Render(SKCanvas canvas) public override void Render(SKCanvas canvas)
{ {
// Scale down the render path to avoid computing a value for every pixel // Scale down the render path to avoid computing a value for every pixel
var width = (int) (Math.Max(Layer.AbsoluteRectangle.Width, Layer.AbsoluteRectangle.Height) / Scale); var width = (int) (Math.Max(Layer.Bounds.Width, Layer.Bounds.Height) / Scale);
var height = (int) (Math.Max(Layer.AbsoluteRectangle.Width, Layer.AbsoluteRectangle.Height) / Scale); var height = (int) (Math.Max(Layer.Bounds.Width, Layer.Bounds.Height) / Scale);
var opacity = (float) Math.Round(Settings.Color.Alpha / 255.0, 2, MidpointRounding.AwayFromZero); var opacity = (float) Math.Round(Settings.Color.Alpha / 255.0, 2, MidpointRounding.AwayFromZero);
using (var bitmap = new SKBitmap(new SKImageInfo(width, height))) using (var bitmap = new SKBitmap(new SKImageInfo(width, height)))
{ {
@ -77,7 +77,7 @@ namespace Artemis.Plugins.LayerBrushes.Noise
using (var sh = SKShader.CreateBitmap(bitmap, SKShaderTileMode.Mirror, SKShaderTileMode.Mirror, SKMatrix.MakeScale(Scale, Scale))) using (var sh = SKShader.CreateBitmap(bitmap, SKShaderTileMode.Mirror, SKShaderTileMode.Mirror, SKMatrix.MakeScale(Scale, Scale)))
using (var paint = new SKPaint {Shader = sh, BlendMode = Settings.BlendMode}) using (var paint = new SKPaint {Shader = sh, BlendMode = Settings.BlendMode})
{ {
canvas.DrawPath(Layer.LayerShape.RenderPath, paint); canvas.DrawPath(Layer.LayerShape.Path, paint);
} }
} }
} }

View File

@ -44,7 +44,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.PropertyTree.P
{ {
LayerPropertyViewModel.LayerProperty.SetCurrentValue(value, ProfileEditorService.CurrentTime); LayerPropertyViewModel.LayerProperty.SetCurrentValue(value, ProfileEditorService.CurrentTime);
// Force the keyframe engine to update, the edited keyframe might affect the current keyframe progress // Force the keyframe engine to update, the edited keyframe might affect the current keyframe progress
LayerPropertyViewModel.LayerProperty.KeyframeEngine.Update(0); LayerPropertyViewModel.LayerProperty.KeyframeEngine?.Update(0);
ProfileEditorService.UpdateSelectedProfileElement(); ProfileEditorService.UpdateSelectedProfileElement();
} }

View File

@ -32,6 +32,8 @@
</Style> </Style>
</UserControl.Resources> </UserControl.Resources>
<Canvas Style="{StaticResource SelectedStyle}"> <Canvas Style="{StaticResource SelectedStyle}">
<!-- The shape is inside the layer's bounds -->
<Canvas Canvas.Left="{Binding LayerBounds.X}" Canvas.Top="{Binding LayerBounds.Y}">
<!-- The part of the layer's shape that falls outside the layer --> <!-- The part of the layer's shape that falls outside the layer -->
<Path Data="{Binding ShapeGeometry, Mode=OneWay}"> <Path Data="{Binding ShapeGeometry, Mode=OneWay}">
<Path.Fill> <Path.Fill>
@ -58,6 +60,7 @@
</VisualBrush> </VisualBrush>
</Path.OpacityMask> </Path.OpacityMask>
</Path> </Path>
</Canvas>
<Path Data="{Binding LayerGeometry, Mode=OneWay}" ClipToBounds="False" StrokeThickness="1.5" StrokeLineJoin="Round" x:Name="LayerPath"> <Path Data="{Binding LayerGeometry, Mode=OneWay}" ClipToBounds="False" StrokeThickness="1.5" StrokeLineJoin="Round" x:Name="LayerPath">
<Path.Stroke> <Path.Stroke>

View File

@ -37,6 +37,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization
public Layer Layer { get; } public Layer Layer { get; }
public Rect LayerBounds { get; set; }
public Geometry LayerGeometry { get; set; } public Geometry LayerGeometry { get; set; }
public Geometry OpacityGeometry { get; set; } public Geometry OpacityGeometry { get; set; }
public Geometry ShapeGeometry { get; set; } public Geometry ShapeGeometry { get; set; }
@ -55,6 +56,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization
{ {
if (!Layer.Leds.Any()) if (!Layer.Leds.Any())
{ {
LayerBounds = Rect.Empty;
LayerGeometry = Geometry.Empty; LayerGeometry = Geometry.Empty;
OpacityGeometry = Geometry.Empty; OpacityGeometry = Geometry.Empty;
ViewportRectangle = Rect.Empty; ViewportRectangle = Rect.Empty;
@ -91,6 +93,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization
var layerGeometry = group.GetOutlinedPathGeometry(); var layerGeometry = group.GetOutlinedPathGeometry();
var opacityGeometry = Geometry.Combine(Geometry.Empty, layerGeometry, GeometryCombineMode.Exclude, new TranslateTransform()); var opacityGeometry = Geometry.Combine(Geometry.Empty, layerGeometry, GeometryCombineMode.Exclude, new TranslateTransform());
LayerBounds = _layerEditorService.GetLayerBounds(Layer);
LayerGeometry = layerGeometry; LayerGeometry = layerGeometry;
OpacityGeometry = opacityGeometry; OpacityGeometry = opacityGeometry;
} }
@ -105,27 +108,27 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization
Execute.PostToUIThread(() => Execute.PostToUIThread(() =>
{ {
var rect = _layerEditorService.GetShapeUntransformedRect(Layer.LayerShape); var bounds = _layerEditorService.GetLayerShapeBounds(Layer.LayerShape);
var shapeGeometry = Geometry.Empty; var shapeGeometry = Geometry.Empty;
switch (Layer.LayerShape) switch (Layer.LayerShape)
{ {
case Ellipse _: case Ellipse _:
shapeGeometry = new EllipseGeometry(rect); shapeGeometry = new EllipseGeometry(bounds);
break; break;
case Fill _: case Fill _:
// Shape originates from the center so compensate the geometry for that, create a copy // Shape originates from the center so compensate the geometry for that, create a copy
shapeGeometry = LayerGeometry.Clone(); shapeGeometry = LayerGeometry.Clone();
// Add a transformation // Add a transformation
shapeGeometry.Transform = new TranslateTransform(rect.Left - shapeGeometry.Bounds.Left, rect.Top - shapeGeometry.Bounds.Top); shapeGeometry.Transform = new TranslateTransform(bounds.Left - shapeGeometry.Bounds.Left, bounds.Top - shapeGeometry.Bounds.Top);
// Apply the transformation so that it won't be overridden // Apply the transformation so that it won't be overridden
shapeGeometry = shapeGeometry.GetOutlinedPathGeometry(); shapeGeometry = shapeGeometry.GetOutlinedPathGeometry();
break; break;
case Polygon _: case Polygon _:
// TODO // TODO
shapeGeometry = new RectangleGeometry(rect); shapeGeometry = new RectangleGeometry(bounds);
break; break;
case Rectangle _: case Rectangle _:
shapeGeometry = new RectangleGeometry(rect); shapeGeometry = new RectangleGeometry(bounds);
break; break;
} }

View File

@ -11,7 +11,9 @@
d:DesignWidth="800" d:DesignWidth="800"
d:DataContext="{d:DesignInstance {x:Type local:EditToolViewModel}}"> d:DataContext="{d:DesignInstance {x:Type local:EditToolViewModel}}">
<Canvas UseLayoutRounding="False"> <Canvas UseLayoutRounding="False">
<userControls:LayerShapeControl Zoom="{Binding ProfileViewModel.PanZoomViewModel.Zoom}" <userControls:LayerShapeControl Canvas.Left="{Binding LayerBounds.Left}"
Canvas.Top="{Binding LayerBounds.Top}"
Zoom="{Binding ProfileViewModel.PanZoomViewModel.Zoom}"
ShapePath="{Binding ShapePath}" ShapePath="{Binding ShapePath}"
ShapeAnchor="{Binding ShapeAnchor}" ShapeAnchor="{Binding ShapeAnchor}"
ShapeGeometry="{Binding ShapeGeometry}" ShapeGeometry="{Binding ShapeGeometry}"

View File

@ -33,6 +33,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
profileEditorService.ProfilePreviewUpdated += (sender, args) => Update(); profileEditorService.ProfilePreviewUpdated += (sender, args) => Update();
} }
public Rect LayerBounds { get; set; }
public SKPath ShapePath { get; set; } public SKPath ShapePath { get; set; }
public SKPoint ShapeAnchor { get; set; } public SKPoint ShapeAnchor { get; set; }
public RectangleGeometry ShapeGeometry { get; set; } public RectangleGeometry ShapeGeometry { get; set; }
@ -42,11 +43,12 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
if (!(ProfileEditorService.SelectedProfileElement is Layer layer)) if (!(ProfileEditorService.SelectedProfileElement is Layer layer))
return; return;
LayerBounds = _layerEditorService.GetLayerBounds(layer);
ShapePath = _layerEditorService.GetLayerPath(layer, true, true, true); ShapePath = _layerEditorService.GetLayerPath(layer, true, true, true);
ShapeAnchor = _layerEditorService.GetLayerAnchor(layer, true); ShapeAnchor = _layerEditorService.GetLayerAnchor(layer).ToSKPoint();
Execute.PostToUIThread(() => Execute.PostToUIThread(() =>
{ {
var shapeGeometry = new RectangleGeometry(_layerEditorService.GetShapeUntransformedRect(layer.LayerShape)) var shapeGeometry = new RectangleGeometry(_layerEditorService.GetLayerShapeBounds(layer.LayerShape))
{ {
Transform = _layerEditorService.GetLayerTransformGroup(layer) Transform = _layerEditorService.GetLayerTransformGroup(layer)
}; };
@ -67,7 +69,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
{ {
_rotating = true; _rotating = true;
if (ProfileEditorService.SelectedProfileElement is Layer layer) if (ProfileEditorService.SelectedProfileElement is Layer layer)
_previousDragAngle = CalculateAngle(_layerEditorService.GetLayerAnchor(layer, true), GetRelativePosition(sender, e.MouseEventArgs).ToSKPoint()); _previousDragAngle = CalculateAngle(_layerEditorService.GetLayerAnchor(layer), GetRelativePosition(sender, e.MouseEventArgs));
else else
_previousDragAngle = 0; _previousDragAngle = 0;
} }
@ -84,7 +86,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
return; return;
var previousDragAngle = _previousDragAngle; var previousDragAngle = _previousDragAngle;
var newRotation = CalculateAngle(_layerEditorService.GetLayerAnchor(layer, true), GetRelativePosition(sender, e.MouseEventArgs).ToSKPoint()); var newRotation = CalculateAngle(_layerEditorService.GetLayerAnchor(layer), GetRelativePosition(sender, e.MouseEventArgs));
_previousDragAngle = newRotation; _previousDragAngle = newRotation;
// Allow the user to rotate the shape in increments of 5 // Allow the user to rotate the shape in increments of 5
@ -278,7 +280,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
// Measure from the top-left of the shape (without rotation) // Measure from the top-left of the shape (without rotation)
_dragOffset = topLeft + (dragStartPosition - topLeft); _dragOffset = topLeft + (dragStartPosition - topLeft);
// Get the absolute layer anchor and make it relative to the unrotated shape // Get the absolute layer anchor and make it relative to the unrotated shape
_dragStartAnchor = _layerEditorService.GetLayerAnchor(layer, true) - topLeft; _dragStartAnchor = _layerEditorService.GetLayerAnchor(layer).ToSKPoint() - topLeft;
// Ensure the anchor starts in the center of the shape it is now relative to // Ensure the anchor starts in the center of the shape it is now relative to
_dragStartAnchor.X -= path.Bounds.Width / 2f; _dragStartAnchor.X -= path.Bounds.Width / 2f;
_dragStartAnchor.Y -= path.Bounds.Height / 2f; _dragStartAnchor.Y -= path.Bounds.Height / 2f;
@ -390,7 +392,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
return mouseEventArgs.GetPosition((IInputElement) parent); return mouseEventArgs.GetPosition((IInputElement) parent);
} }
private float CalculateAngle(SKPoint start, SKPoint arrival) private float CalculateAngle(Point start, Point arrival)
{ {
var radian = (float) Math.Atan2(start.Y - arrival.Y, start.X - arrival.X); var radian = (float) Math.Atan2(start.Y - arrival.Y, start.X - arrival.X);
var angle = radian * (180f / (float) Math.PI); var angle = radian * (180f / (float) Math.PI);

View File

@ -42,7 +42,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
layer.LayerShape = new Fill(layer); layer.LayerShape = new Fill(layer);
// Apply the full layer rectangle // Apply the full layer rectangle
_layerEditorService.SetShapeBaseFromRectangle(layer.LayerShape, _layerEditorService.GetLayerRect(layer)); _layerEditorService.SetShapeBaseFromRectangle(layer.LayerShape, _layerEditorService.GetLayerBounds(layer));
ProfileEditorService.UpdateSelectedProfileElement(); ProfileEditorService.UpdateSelectedProfileElement();
} }
} }

View File

@ -21,121 +21,56 @@ namespace Artemis.UI.Services
} }
/// <inheritdoc /> /// <inheritdoc />
public Rect GetLayerRenderRect(Layer layer) public Rect GetLayerBounds(Layer layer)
{ {
// Adjust the render rectangle for the difference in render scale // Adjust the render rectangle for the difference in render scale
var renderScale = _settingsService.GetSetting("Core.RenderScale", 1.0).Value; var renderScale = _settingsService.GetSetting("Core.RenderScale", 1.0).Value;
return new Rect( return new Rect(
layer.Rectangle.Left / renderScale * 1, layer.Bounds.Left / renderScale * 1,
layer.Rectangle.Top / renderScale * 1, layer.Bounds.Top / renderScale * 1,
Math.Max(0, layer.Rectangle.Width / renderScale * 1), Math.Max(0, layer.Bounds.Width / renderScale * 1),
Math.Max(0, layer.Rectangle.Height / renderScale * 1) Math.Max(0, layer.Bounds.Height / renderScale * 1)
); );
} }
public Rect GetLayerRect(Layer layer) /// <inheritdoc />
public Rect GetLayerShapeBounds(LayerShape layerShape)
{ {
// Adjust the render rectangle for the difference in render scale // Adjust the render rectangle for the difference in render scale
var renderScale = _settingsService.GetSetting("Core.RenderScale", 1.0).Value; var renderScale = _settingsService.GetSetting("Core.RenderScale", 1.0).Value;
return new Rect( return new Rect(
layer.AbsoluteRectangle.Left / renderScale * 1, layerShape.Bounds.Left / renderScale * 1,
layer.AbsoluteRectangle.Top / renderScale * 1, layerShape.Bounds.Top / renderScale * 1,
Math.Max(0, layer.AbsoluteRectangle.Width / renderScale * 1), Math.Max(0, layerShape.Bounds.Width / renderScale * 1),
Math.Max(0, layer.AbsoluteRectangle.Height / renderScale * 1) Math.Max(0, layerShape.Bounds.Height / renderScale * 1)
); );
} }
/// <inheritdoc /> /// <inheritdoc />
public SKPath GetLayerPath(Layer layer, bool includeTranslation, bool includeScale, bool includeRotation) public Point GetLayerAnchor(Layer layer)
{ {
var layerRect = GetLayerRenderRect(layer).ToSKRect(); var layerBounds = GetLayerBounds(layer);
var shapeRect = GetShapeUntransformedRect(layer.LayerShape).ToSKRect();
// Apply transformation like done by the core during layer rendering
var anchor = GetLayerAnchor(layer, true);
var relativeAnchor = GetLayerAnchor(layer, false);
// Translation originates from the unscaled center of the shape and is tied to the anchor
var x = layer.PositionProperty.CurrentValue.X * layerRect.Width - shapeRect.Width / 2 - relativeAnchor.X;
var y = layer.PositionProperty.CurrentValue.Y * layerRect.Height - shapeRect.Height / 2 - relativeAnchor.Y;
var path = new SKPath();
path.AddRect(shapeRect);
if (includeTranslation)
path.Transform(SKMatrix.MakeTranslation(x, y));
if (includeScale)
path.Transform(SKMatrix.MakeScale(layer.SizeProperty.CurrentValue.Width, layer.SizeProperty.CurrentValue.Height, anchor.X, anchor.Y));
if (includeRotation)
path.Transform(SKMatrix.MakeRotationDegrees(layer.RotationProperty.CurrentValue, anchor.X, anchor.Y));
return path;
}
public void ReverseLayerPath(Layer layer, SKPath path)
{
var layerRect = GetLayerRenderRect(layer).ToSKRect();
var shapeRect = GetShapeUntransformedRect(layer.LayerShape).ToSKRect();
// Apply transformation like done by the core during layer rendering
var anchor = GetLayerAnchor(layer, true);
var relativeAnchor = GetLayerAnchor(layer, false);
// Translation originates from the unscaled center of the shape and is tied to the anchor
var x = layer.PositionProperty.CurrentValue.X * layerRect.Width - shapeRect.Width / 2 - relativeAnchor.X;
var y = layer.PositionProperty.CurrentValue.Y * layerRect.Height - shapeRect.Height / 2 - relativeAnchor.Y;
SKMatrix.MakeScale(layer.SizeProperty.CurrentValue.Width, layer.SizeProperty.CurrentValue.Height, anchor.X, anchor.Y).TryInvert(out var scale);
path.Transform(SKMatrix.MakeRotationDegrees(layer.RotationProperty.CurrentValue * -1, anchor.X, anchor.Y));
path.Transform(scale);
path.Transform(SKMatrix.MakeTranslation(x * -1, y * -1));
}
/// <inheritdoc />
public SKPoint GetLayerAnchor(Layer layer, bool absolute)
{
var layerRect = GetLayerRect(layer);
if (absolute)
{
var position = layer.PositionProperty.CurrentValue;
position.X = (float) (position.X * layerRect.Width);
position.Y = (float) (position.Y * layerRect.Height);
var shapeRect = GetShapeUntransformedRect(layer.LayerShape);
return new SKPoint((float) (position.X + shapeRect.Left), (float) (position.Y + shapeRect.Top));
}
// TODO figure out what else is needed, position should matter here
var anchor = layer.AnchorPointProperty.CurrentValue; var anchor = layer.AnchorPointProperty.CurrentValue;
anchor.X = (float) (anchor.X * layerRect.Width); anchor.X = (float) (anchor.X * layerBounds.Width);
anchor.Y = (float) (anchor.Y * layerRect.Height); anchor.Y = (float) (anchor.Y * layerBounds.Height);
return new SKPoint(anchor.X, anchor.Y);
}
public void SetLayerAnchor(Layer layer, SKPoint point, bool absolute, TimeSpan? time) return new Point(anchor.X * layerBounds.Width, anchor.Y * layerBounds.Height);
{
var layerRect = GetLayerRect(layer);
if (absolute)
{
var shapeRect = GetShapeUntransformedRect(layer.LayerShape);
var position = new SKPoint((float) ((point.X - shapeRect.Left) / layerRect.Width), (float) ((point.Y - shapeRect.Top) / layerRect.Height));
layer.PositionProperty.SetCurrentValue(position, time);
}
var anchor = new SKPoint((float) (point.X / layerRect.Width), (float) (point.Y / layerRect.Height));
layer.AnchorPointProperty.SetCurrentValue(anchor, time);
} }
/// <inheritdoc /> /// <inheritdoc />
public TransformGroup GetLayerTransformGroup(Layer layer) public TransformGroup GetLayerTransformGroup(Layer layer)
{ {
var layerRect = GetLayerRenderRect(layer).ToSKRect(); var layerBounds = GetLayerBounds(layer).ToSKRect();
var shapeRect = GetShapeUntransformedRect(layer.LayerShape).ToSKRect(); var shapeBounds = GetLayerShapeBounds(layer.LayerShape).ToSKRect();
// Apply transformation like done by the core during layer rendering // Apply transformation like done by the core during layer rendering
var anchor = GetLayerAnchor(layer, true); var anchor = GetLayerAnchor(layer);
// Translation originates from the unscaled center of the shape and is tied to the anchor // Translation originates from the unscaled center of the shape and is tied to the anchor
var x = layer.PositionProperty.CurrentValue.X * layerRect.Width - shapeRect.Width / 2 - GetLayerAnchor(layer, false).X; var x = layer.PositionProperty.CurrentValue.X * layerBounds.Width - shapeBounds.Width / 2 - anchor.X;
var y = layer.PositionProperty.CurrentValue.Y * layerRect.Height - shapeRect.Height / 2 - GetLayerAnchor(layer, false).Y; var y = layer.PositionProperty.CurrentValue.Y * layerBounds.Height - shapeBounds.Height / 2 - anchor.Y;
var transformGroup = new TransformGroup(); var transformGroup = new TransformGroup();
transformGroup.Children.Add(new TranslateTransform(x, y)); transformGroup.Children.Add(new TranslateTransform(x, y));
@ -146,27 +81,30 @@ namespace Artemis.UI.Services
} }
/// <inheritdoc /> /// <inheritdoc />
public Rect GetShapeUntransformedRect(LayerShape layerShape) public SKPath GetLayerPath(Layer layer, bool includeTranslation, bool includeScale, bool includeRotation)
{ {
if (layerShape == null) var layerBounds = GetLayerBounds(layer).ToSKRect();
return Rect.Empty; var shapeBounds = GetLayerShapeBounds(layer.LayerShape).ToSKRect();
// Adjust the render rectangle for the difference in render scale // Apply transformation like done by the core during layer rendering
var renderScale = _settingsService.GetSetting("Core.RenderScale", 1.0).Value; var anchor = GetLayerAnchor(layer).ToSKPoint();
return new Rect(
layerShape.RenderRectangle.Left / renderScale * 1, // Translation originates from the unscaled center of the shape and is tied to the anchor
layerShape.RenderRectangle.Top / renderScale * 1, var x = layer.PositionProperty.CurrentValue.X * layerBounds.Width - shapeBounds.Width / 2 - anchor.X;
Math.Max(0, layerShape.RenderRectangle.Width / renderScale * 1), var y = layer.PositionProperty.CurrentValue.Y * layerBounds.Height - shapeBounds.Height / 2 - anchor.Y;
Math.Max(0, layerShape.RenderRectangle.Height / renderScale * 1)
); var path = new SKPath();
path.AddRect(shapeBounds);
if (includeTranslation)
path.Transform(SKMatrix.MakeTranslation(x, y));
if (includeScale)
path.Transform(SKMatrix.MakeScale(layer.SizeProperty.CurrentValue.Width, layer.SizeProperty.CurrentValue.Height, anchor.X, anchor.Y));
if (includeRotation)
path.Transform(SKMatrix.MakeRotationDegrees(layer.RotationProperty.CurrentValue, anchor.X, anchor.Y));
return path;
} }
/// <inheritdoc />
public Rect GetShapeTransformedRect(LayerShape layerShape)
{
var path = GetLayerPath(layerShape.Layer, true, true, false);
return path.Bounds.ToRect();
}
/// <inheritdoc /> /// <inheritdoc />
public void SetShapeBaseFromRectangle(LayerShape layerShape, Rect rect) public void SetShapeBaseFromRectangle(LayerShape layerShape, Rect rect)
@ -177,16 +115,16 @@ namespace Artemis.UI.Services
return; return;
} }
var layerRect = GetLayerRenderRect(layerShape.Layer).ToSKRect(); var layerBounds = GetLayerBounds(layerShape.Layer).ToSKRect();
// Compensate for the current value of the position transformation // Compensate for the current value of the position transformation
rect.X += rect.Width / 2; rect.X += rect.Width / 2;
rect.X -= layerRect.Width * layerShape.Layer.PositionProperty.CurrentValue.X; rect.X -= layerBounds.Width * layerShape.Layer.PositionProperty.CurrentValue.X;
rect.X += layerRect.Width * layerShape.Layer.AnchorPointProperty.CurrentValue.X * layerShape.Layer.SizeProperty.CurrentValue.Width; rect.X += layerBounds.Width * layerShape.Layer.AnchorPointProperty.CurrentValue.X * layerShape.Layer.SizeProperty.CurrentValue.Width;
rect.Y += rect.Height / 2; rect.Y += rect.Height / 2;
rect.Y -= layerRect.Height * layerShape.Layer.PositionProperty.CurrentValue.Y; rect.Y -= layerBounds.Height * layerShape.Layer.PositionProperty.CurrentValue.Y;
rect.Y += layerRect.Height * layerShape.Layer.AnchorPointProperty.CurrentValue.Y * layerShape.Layer.SizeProperty.CurrentValue.Height; rect.Y += layerBounds.Height * layerShape.Layer.AnchorPointProperty.CurrentValue.Y * layerShape.Layer.SizeProperty.CurrentValue.Height;
// Compensate for the current value of the size transformation // Compensate for the current value of the size transformation
rect.Height /= layerShape.Layer.SizeProperty.CurrentValue.Height; rect.Height /= layerShape.Layer.SizeProperty.CurrentValue.Height;
@ -195,10 +133,10 @@ namespace Artemis.UI.Services
// Adjust the provided rect for the difference in render scale // Adjust the provided rect for the difference in render scale
var renderScale = _settingsService.GetSetting("Core.RenderScale", 1.0).Value; var renderScale = _settingsService.GetSetting("Core.RenderScale", 1.0).Value;
layerShape.ScaledRectangle = SKRect.Create( layerShape.ScaledRectangle = SKRect.Create(
100f / layerShape.Layer.AbsoluteRectangle.Width * ((float) (rect.Left * renderScale) - layerShape.Layer.AbsoluteRectangle.Left) / 100f, 100f / layerShape.Layer.Bounds.Width * ((float) (rect.Left * renderScale) - layerShape.Layer.Bounds.Left) / 100f,
100f / layerShape.Layer.AbsoluteRectangle.Height * ((float) (rect.Top * renderScale) - layerShape.Layer.AbsoluteRectangle.Top) / 100f, 100f / layerShape.Layer.Bounds.Height * ((float) (rect.Top * renderScale) - layerShape.Layer.Bounds.Top) / 100f,
100f / layerShape.Layer.AbsoluteRectangle.Width * (float) (rect.Width * renderScale) / 100f, 100f / layerShape.Layer.Bounds.Width * (float) (rect.Width * renderScale) / 100f,
100f / layerShape.Layer.AbsoluteRectangle.Height * (float) (rect.Height * renderScale) / 100f 100f / layerShape.Layer.Bounds.Height * (float) (rect.Height * renderScale) / 100f
); );
layerShape.CalculateRenderProperties(); layerShape.CalculateRenderProperties();
} }
@ -210,14 +148,14 @@ namespace Artemis.UI.Services
if (absolute) if (absolute)
{ {
return new SKPoint( return new SKPoint(
100f / layer.AbsoluteRectangle.Width * ((float) (point.X * renderScale) - layer.AbsoluteRectangle.Left) / 100f, 100f / layer.Bounds.Width * ((float) (point.X * renderScale) - layer.Bounds.Left) / 100f,
100f / layer.AbsoluteRectangle.Height * ((float) (point.Y * renderScale) - layer.AbsoluteRectangle.Top) / 100f 100f / layer.Bounds.Height * ((float) (point.Y * renderScale) - layer.Bounds.Top) / 100f
); );
} }
return new SKPoint( return new SKPoint(
100f / layer.AbsoluteRectangle.Width * (float) (point.X * renderScale) / 100f, 100f / layer.Bounds.Width * (float) (point.X * renderScale) / 100f,
100f / layer.AbsoluteRectangle.Height * (float) (point.Y * renderScale) / 100f 100f / layer.Bounds.Height * (float) (point.Y * renderScale) / 100f
); );
} }
} }
@ -225,40 +163,25 @@ namespace Artemis.UI.Services
public interface ILayerEditorService : IArtemisUIService public interface ILayerEditorService : IArtemisUIService
{ {
/// <summary> /// <summary>
/// Returns an relative and scaled rectangle for the given layer that is corrected for the current render scale. /// Returns the layer's bounds, corrected for the current render scale.
/// </summary> /// </summary>
/// <param name="layer"></param> /// <param name="layer"></param>
/// <returns></returns> /// <returns></returns>
Rect GetLayerRenderRect(Layer layer); Rect GetLayerBounds(Layer layer);
/// <summary> /// <summary>
/// Returns an absolute and scaled rectangle for the given layer that is corrected for the current render scale. /// Returns the layer's anchor, corrected for the current render scale.
/// </summary> /// </summary>
/// <param name="layer"></param> /// <param name="layer"></param>
/// <returns></returns> /// <returns></returns>
Rect GetLayerRect(Layer layer); Point GetLayerAnchor(Layer layer);
/// <summary> /// <summary>
/// Returns an absolute and scaled rectangular path for the given layer that is corrected for the current render scale. /// Returns the layer shape's bounds, corrected for the current render scale.
/// </summary> /// </summary>
/// <param name="layer"></param> /// <param name="layerShape"></param>
/// <param name="includeTranslation"></param>
/// <param name="includeScale"></param>
/// <param name="includeRotation"></param>
/// <returns></returns> /// <returns></returns>
SKPath GetLayerPath(Layer layer, bool includeTranslation, bool includeScale, bool includeRotation); Rect GetLayerShapeBounds(LayerShape layerShape);
void ReverseLayerPath(Layer layer, SKPath path);
/// <summary>
/// Returns an absolute and scaled anchor for the given layer, optionally with the translation applied.
/// </summary>
/// <param name="layer"></param>
/// <param name="absolute"></param>
/// <returns></returns>
SKPoint GetLayerAnchor(Layer layer, bool absolute);
void SetLayerAnchor(Layer layer, SKPoint point, bool absolute, TimeSpan? time);
/// <summary> /// <summary>
/// Creates a WPF transform group that contains all the transformations required to render the provided layer. /// Creates a WPF transform group that contains all the transformations required to render the provided layer.
@ -269,18 +192,14 @@ namespace Artemis.UI.Services
TransformGroup GetLayerTransformGroup(Layer layer); TransformGroup GetLayerTransformGroup(Layer layer);
/// <summary> /// <summary>
/// Returns an absolute and scaled rectangle for the given shape that is corrected for the current render scale without /// Returns an absolute and scaled rectangular path for the given layer that is corrected for the current render scale.
/// any transformations applied.
/// </summary> /// </summary>
/// <param name="layer"></param>
/// <param name="includeTranslation"></param>
/// <param name="includeScale"></param>
/// <param name="includeRotation"></param>
/// <returns></returns> /// <returns></returns>
Rect GetShapeUntransformedRect(LayerShape layerShape); SKPath GetLayerPath(Layer layer, bool includeTranslation, bool includeScale, bool includeRotation);
/// <summary>
/// Returns an absolute and scaled rectangle for the given shape that is corrected for the current render scale with
/// translation and scale transformations applied.
/// </summary>
/// <returns></returns>
Rect GetShapeTransformedRect(LayerShape layerShape);
/// <summary> /// <summary>
/// Sets the base properties of the given shape to match the provided unscaled rectangle. The rectangle is corrected /// Sets the base properties of the given shape to match the provided unscaled rectangle. The rectangle is corrected