Simplified the different properties used for layer/shape rendering

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

No files matched your search

+37 -49
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using Artemis.Core.Exceptions;
using Artemis.Core.Extensions;
using Artemis.Core.Models.Profile.LayerProperties;
@@ -20,6 +21,8 @@ namespace Artemis.Core.Models.Profile
private readonly Dictionary<string, BaseLayerProperty> _properties;
private LayerShape _layerShape;
private List<ArtemisLed> _leds;
private SKPath _path;
private SKRect _bounds;
public Layer(Profile profile, ProfileElement parent, string name)
{
@@ -81,22 +84,25 @@ namespace Artemis.Core.Models.Profile
public ReadOnlyCollection<ArtemisLed> Leds => _leds.AsReadOnly();
/// <summary>
/// An absolute rectangle to the surface that contains all the LEDs in this layer.
/// <para>For rendering, use the RenderRectangle on <see cref="LayerShape" />.</para>
/// A path containing all the LEDs this layer is applied to, any rendering outside the layer Path is clipped.
/// <para>For rendering, use the Path on <see cref="LayerShape" />.</para>
/// </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>
/// A zero-based rectangle that contains all the LEDs in this layer.
/// <para>For rendering, use the RenderRectangle on <see cref="LayerShape" />.</para>
/// The bounds of this layer
/// </summary>
public SKRect Rectangle { get; private set; }
/// <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; }
public SKRect Bounds => _bounds;
/// <summary>
/// Defines the shape that is rendered by the <see cref="LayerBrush" />.
@@ -153,7 +159,7 @@ namespace Artemis.Core.Models.Profile
{
property.KeyframeEngine?.Update(deltaTime);
// This is a placeholder method of repeating the animation until repeat modes are implemented
if (property.KeyframeEngine != null && property.IsUsingKeyframes && property.KeyframeEngine.NextKeyframe == null)
if (property.KeyframeEngine != null && property.IsUsingKeyframes && property.KeyframeEngine.NextKeyframe == null)
property.KeyframeEngine.OverrideProgress(TimeSpan.Zero);
}
@@ -173,19 +179,18 @@ namespace Artemis.Core.Models.Profile
var size = SizeProperty.CurrentValue;
var rotation = RotationProperty.CurrentValue;
var anchor = GetLayerAnchor(true);
var relativeAnchor = GetLayerAnchor(false);
var anchor = GetLayerAnchor();
// 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 y = position.Y * AbsoluteRectangle.Height - LayerShape.RenderRectangle.Height / 2 - relativeAnchor.Y;
canvas.RotateDegrees(rotation, anchor.X, anchor.Y);
canvas.Scale(size.Width, size.Height, anchor.X, anchor.Y);
canvas.Translate(x, y);
var x = position.X * Bounds.Width - LayerShape.Bounds.Width / 2 - anchor.X;
var y = position.Y * Bounds.Height - LayerShape.Bounds.Height / 2 - anchor.Y;
canvas.Translate(Bounds.Left + x, Bounds.Top + y);
canvas.Scale(size.Width, size.Height, anchor.X, anchor.Y);
canvas.RotateDegrees(rotation, anchor.X, anchor.Y);
// Placeholder
if (LayerShape?.RenderPath != null)
if (LayerShape?.Path != null)
{
var testColors = new List<SKColor>();
for (var i = 0; i < 9; i++)
@@ -196,31 +201,23 @@ namespace Artemis.Core.Models.Profile
testColors.Add(SKColor.FromHsv(0, 100, 100));
}
var shader = SKShader.CreateSweepGradient(new SKPoint(LayerShape.RenderRectangle.MidX, LayerShape.RenderRectangle.MidY), testColors.ToArray());
canvas.DrawPath(LayerShape.RenderPath, new SKPaint {Shader = shader, Color = new SKColor(0, 0, 0, (byte) (OpacityProperty.CurrentValue * 2.55f))});
var shader = SKShader.CreateSweepGradient(new SKPoint(LayerShape.Bounds.MidX, LayerShape.Bounds.MidY), testColors.ToArray());
canvas.DrawPath(LayerShape.Path, new SKPaint {Shader = shader, Color = new SKColor(0, 0, 0, (byte) (OpacityProperty.CurrentValue * 2.55f))});
}
LayerBrush?.Render(canvas);
canvas.Restore();
}
private SKPoint GetLayerAnchor(bool absolute)
private SKPoint GetLayerAnchor()
{
if (LayerShape == null)
return SKPoint.Empty;
if (!absolute)
{
var anchor = AnchorPointProperty.CurrentValue;
anchor.X = anchor.X * AbsoluteRectangle.Width;
anchor.Y = anchor.Y * AbsoluteRectangle.Height;
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);
var anchor = AnchorPointProperty.CurrentValue;
anchor.X = anchor.X * Bounds.Width;
anchor.Y = anchor.Y * Bounds.Height;
return new SKPoint(anchor.X, anchor.Y);
}
internal override void ApplyToEntity()
@@ -325,31 +322,22 @@ namespace Artemis.Core.Models.Profile
{
if (!Leds.Any())
{
AbsoluteRectangle = SKRect.Empty;
Rectangle = SKRect.Empty;
Path = new SKPath();
LayerShape?.CalculateRenderProperties();
OnRenderPropertiesUpdated();
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};
foreach (var artemisLed in Leds)
path.AddRect(artemisLed.AbsoluteRenderRectangle);
Path = path;
// This is called here so that the shape's render properties are up to date when other code
// responds to OnRenderPropertiesUpdated
LayerShape?.CalculateRenderProperties();
OnRenderPropertiesUpdated();
}
@@ -15,12 +15,9 @@ namespace Artemis.Core.Models.Profile.LayerShapes
public override void CalculateRenderProperties()
{
var unscaled = GetUnscaledRectangle();
RenderRectangle = SKRect.Create(0,0 , unscaled.Width, unscaled.Height);
var path = new SKPath();
path.AddOval(RenderRectangle);
RenderPath = path;
path.AddOval(GetUnscaledRectangle());
Path = path;
}
internal override void ApplyToEntity()
@@ -15,12 +15,8 @@ namespace Artemis.Core.Models.Profile.LayerShapes
public override void CalculateRenderProperties()
{
RenderRectangle = GetUnscaledRectangle();
// Shape originates from the center so compensate the path for that
var renderPath = new SKPath(Layer.Path);
renderPath.Transform(SKMatrix.MakeTranslation(RenderRectangle.Left - Layer.Path.Bounds.Left, RenderRectangle.Top - Layer.Path.Bounds.Top));
RenderPath = renderPath;
Path = new SKPath(Layer.Path);
}
internal override void ApplyToEntity()
@@ -1,12 +1,13 @@
using System.Linq;
using Artemis.Storage.Entities.Profile;
using RGB.NET.Core;
using SkiaSharp;
namespace Artemis.Core.Models.Profile.LayerShapes
{
public abstract class LayerShape
{
private SKPath _path;
protected LayerShape(Layer layer)
{
Layer = layer;
@@ -30,19 +31,27 @@ namespace Artemis.Core.Models.Profile.LayerShapes
public SKRect ScaledRectangle { get; set; }
/// <summary>
/// An absolute and scaled render rectangle
/// A path outlining the shape
/// </summary>
public SKRect RenderRectangle { get; protected set; }
public SKPath Path
{
get => _path;
protected set
{
_path = value;
Bounds = value?.Bounds ?? SKRect.Empty;
}
}
/// <summary>
/// A path relative to the layer
/// The bounds of this shape
/// </summary>
public SKPath RenderPath { get; protected set; }
public SKRect Bounds { get; private set; }
public abstract void CalculateRenderProperties();
/// <summary>
/// Updates Position and Size using the provided unscaled rectangle
/// Updates Position and Size using the provided unscaled rectangle
/// </summary>
/// <param name="rect">An unscaled rectangle where 1px = 1mm</param>
public void SetFromUnscaledRectangle(SKRect rect)
@@ -54,10 +63,10 @@ namespace Artemis.Core.Models.Profile.LayerShapes
}
ScaledRectangle = SKRect.Create(
100f / Layer.AbsoluteRectangle.Width * (rect.Left - Layer.AbsoluteRectangle.Left) / 100f,
100f / Layer.AbsoluteRectangle.Height * (rect.Top - Layer.AbsoluteRectangle.Top) / 100f,
100f / Layer.AbsoluteRectangle.Width * rect.Width / 100f,
100f / Layer.AbsoluteRectangle.Height * rect.Height / 100f
100f / Layer.Bounds.Width * rect.Left / 100f,
100f / Layer.Bounds.Height * rect.Top / 100f,
100f / Layer.Bounds.Width * rect.Width / 100f,
100f / Layer.Bounds.Height * rect.Height / 100f
);
}
@@ -67,10 +76,10 @@ namespace Artemis.Core.Models.Profile.LayerShapes
return SKRect.Empty;
return SKRect.Create(
Layer.AbsoluteRectangle.Left + Layer.AbsoluteRectangle.Width * ScaledRectangle.Left,
Layer.AbsoluteRectangle.Top + Layer.AbsoluteRectangle.Height * ScaledRectangle.Top,
Layer.AbsoluteRectangle.Width * ScaledRectangle.Width,
Layer.AbsoluteRectangle.Height * ScaledRectangle.Height
Layer.Bounds.Width * ScaledRectangle.Left,
Layer.Bounds.Height * ScaledRectangle.Top,
Layer.Bounds.Width * ScaledRectangle.Width,
Layer.Bounds.Height * ScaledRectangle.Height
);
}
@@ -25,23 +25,14 @@ namespace Artemis.Core.Models.Profile.LayerShapes
/// </summary>
public List<SKPoint> RenderPoints
{
get
{
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();
}
get { return Points.Select(p => new SKPoint(p.X * Layer.Bounds.Width, p.Y * Layer.Bounds.Height)).ToList(); }
}
public override void CalculateRenderProperties()
{
var path = new SKPath();
path.AddPoly(RenderPoints.ToArray());
RenderPath = path;
RenderRectangle = path.GetRect();
Path = path;
}
internal override void ApplyToEntity()
@@ -15,11 +15,9 @@ namespace Artemis.Core.Models.Profile.LayerShapes
public override void CalculateRenderProperties()
{
RenderRectangle = GetUnscaledRectangle();
var path = new SKPath();
path.AddRect(RenderRectangle);
RenderPath = path;
path.AddRect(GetUnscaledRectangle());
Path = path;
}
internal override void ApplyToEntity()