using System.Linq;
using Artemis.Storage.Entities.Profile;
using SkiaSharp;
namespace Artemis.Core.Models.Profile.LayerShapes
{
public abstract class LayerShape
{
protected LayerShape(Layer layer)
{
Layer = layer;
}
protected LayerShape(Layer layer, ShapeEntity shapeEntity)
{
Layer = layer;
}
///
/// The layer this shape is attached to
///
public Layer Layer { get; set; }
///
/// A render rectangle relative to the layer
///
public SKRect RenderRectangle { get; protected set; }
///
/// A path relative to the layer
///
public SKPath RenderPath { get; protected set; }
public abstract void CalculateRenderProperties(SKPoint shapePosition, SKSize shapeSize);
public virtual void ApplyToEntity()
{
Layer.LayerEntity.ShapeEntity = new ShapeEntity();
}
///
/// Updates Position and Size using the provided unscaled rectangle
///
/// An unscaled rectangle where 1px = 1mm
public void SetFromUnscaledRectangle(SKRect rect)
{
if (!Layer.Leds.Any())
{
Layer.PositionProperty.Value = SKPoint.Empty;
Layer.SizeProperty.Value = SKSize.Empty;
return;
}
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;
Layer.PositionProperty.Value = new SKPoint((float) (100f / width * (rect.Left - x)) / 100f, (float) (100f / height * (rect.Top - y)) / 100f);
Layer.SizeProperty.Value = new SKSize((float) (100f / width * rect.Width) / 100f, (float) (100f / height * rect.Height) / 100f);
// TODO: Update keyframes
CalculateRenderProperties(Layer.PositionProperty.Value, Layer.SizeProperty.Value);
}
public SKRect GetUnscaledRectangle()
{
if (!Layer.Leds.Any())
return SKRect.Empty;
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 SKRect.Create(
(float) (x + width * Layer.PositionProperty.Value.X),
(float) (y + height * Layer.PositionProperty.Value.Y),
(float) (width * Layer.SizeProperty.Value.Width),
(float) (height * Layer.SizeProperty.Value.Height)
);
}
}
}