1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Fix shape positioning on layer LED selection change

This commit is contained in:
Robert 2020-01-30 22:08:38 +01:00
parent 5a44e5fbe8
commit 0794966e38
14 changed files with 89 additions and 50 deletions

View File

@ -179,8 +179,7 @@ namespace Artemis.Core.Models.Profile
// 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 * AbsoluteRectangle.Width - LayerShape.RenderRectangle.Width / 2 - relativeAnchor.X;
var y = position.Y * AbsoluteRectangle.Height - LayerShape.RenderRectangle.Height / 2 - relativeAnchor.Y; var y = position.Y * AbsoluteRectangle.Height - LayerShape.RenderRectangle.Height / 2 - relativeAnchor.Y;
canvas.RotateDegrees(rotation, anchor.X, anchor.Y); canvas.RotateDegrees(rotation, anchor.X, anchor.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.Translate(x, y);

View File

@ -15,14 +15,15 @@ namespace Artemis.Core.Models.Profile.LayerShapes
public override void CalculateRenderProperties() public override void CalculateRenderProperties()
{ {
RenderRectangle = GetUnscaledRectangle(); 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(RenderRectangle);
RenderPath = path; RenderPath = path;
} }
public override void ApplyToEntity() internal override void ApplyToEntity()
{ {
base.ApplyToEntity(); base.ApplyToEntity();
Layer.LayerEntity.ShapeEntity.Type = ShapeEntityType.Ellipse; Layer.LayerEntity.ShapeEntity.Type = ShapeEntityType.Ellipse;

View File

@ -23,7 +23,7 @@ namespace Artemis.Core.Models.Profile.LayerShapes
RenderPath = renderPath; RenderPath = renderPath;
} }
public override void ApplyToEntity() internal override void ApplyToEntity()
{ {
base.ApplyToEntity(); base.ApplyToEntity();
Layer.LayerEntity.ShapeEntity.Type = ShapeEntityType.Fill; Layer.LayerEntity.ShapeEntity.Type = ShapeEntityType.Fill;

View File

@ -1,6 +1,6 @@
using System; 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
@ -41,18 +41,27 @@ namespace Artemis.Core.Models.Profile.LayerShapes
public abstract void CalculateRenderProperties(); public abstract void CalculateRenderProperties();
public virtual void ApplyToEntity() /// <summary>
/// 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)
{ {
Layer.LayerEntity.ShapeEntity = new ShapeEntity if (!Layer.Leds.Any())
{ {
X = ScaledRectangle.Left, ScaledRectangle = SKRect.Empty;
Y = ScaledRectangle.Top, return;
Width = ScaledRectangle.Width, }
Height = ScaledRectangle.Height
}; 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
);
} }
protected SKRect GetUnscaledRectangle() public SKRect GetUnscaledRectangle()
{ {
if (!Layer.Leds.Any()) if (!Layer.Leds.Any())
return SKRect.Empty; return SKRect.Empty;
@ -64,5 +73,16 @@ namespace Artemis.Core.Models.Profile.LayerShapes
Layer.AbsoluteRectangle.Height * ScaledRectangle.Height Layer.AbsoluteRectangle.Height * ScaledRectangle.Height
); );
} }
internal virtual void ApplyToEntity()
{
Layer.LayerEntity.ShapeEntity = new ShapeEntity
{
X = ScaledRectangle.Left,
Y = ScaledRectangle.Top,
Width = ScaledRectangle.Width,
Height = ScaledRectangle.Height
};
}
} }
} }

View File

@ -44,7 +44,7 @@ namespace Artemis.Core.Models.Profile.LayerShapes
RenderRectangle = path.GetRect(); RenderRectangle = path.GetRect();
} }
public override void ApplyToEntity() internal override void ApplyToEntity()
{ {
base.ApplyToEntity(); base.ApplyToEntity();
Layer.LayerEntity.ShapeEntity.Type = ShapeEntityType.Polygon; Layer.LayerEntity.ShapeEntity.Type = ShapeEntityType.Polygon;

View File

@ -22,7 +22,7 @@ namespace Artemis.Core.Models.Profile.LayerShapes
RenderPath = path; RenderPath = path;
} }
public override void ApplyToEntity() internal override void ApplyToEntity()
{ {
base.ApplyToEntity(); base.ApplyToEntity();
Layer.LayerEntity.ShapeEntity.Type = ShapeEntityType.Rectangle; Layer.LayerEntity.ShapeEntity.Type = ShapeEntityType.Rectangle;

View File

@ -105,7 +105,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization
Execute.PostToUIThread(() => Execute.PostToUIThread(() =>
{ {
var rect = _layerEditorService.GetShapeRenderRect(Layer.LayerShape); var rect = _layerEditorService.GetShapeUntransformedRect(Layer.LayerShape);
var shapeGeometry = Geometry.Empty; var shapeGeometry = Geometry.Empty;
switch (Layer.LayerShape) switch (Layer.LayerShape)
{ {

View File

@ -46,7 +46,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
ShapeAnchor = _layerEditorService.GetLayerAnchor(layer, true); ShapeAnchor = _layerEditorService.GetLayerAnchor(layer, true);
Execute.PostToUIThread(() => Execute.PostToUIThread(() =>
{ {
var shapeGeometry = new RectangleGeometry(_layerEditorService.GetShapeRenderRect(layer.LayerShape)) var shapeGeometry = new RectangleGeometry(_layerEditorService.GetShapeUntransformedRect(layer.LayerShape))
{ {
Transform = _layerEditorService.GetLayerTransformGroup(layer) Transform = _layerEditorService.GetLayerTransformGroup(layer)
}; };

View File

@ -53,7 +53,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
layer.LayerShape = new Ellipse(layer); layer.LayerShape = new Ellipse(layer);
// Apply the drag rectangle // Apply the drag rectangle
_layerEditorService.SetShapeRenderRect(layer.LayerShape, DragRectangle); _layerEditorService.SetShapeBaseFromRectangle(layer.LayerShape, DragRectangle);
ProfileEditorService.UpdateSelectedProfileElement(); ProfileEditorService.UpdateSelectedProfileElement();
} }
} }

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.SetShapeRenderRect(layer.LayerShape, _layerEditorService.GetLayerRect(layer)); _layerEditorService.SetShapeBaseFromRectangle(layer.LayerShape, _layerEditorService.GetLayerRect(layer));
ProfileEditorService.UpdateSelectedProfileElement(); ProfileEditorService.UpdateSelectedProfileElement();
} }
} }

View File

@ -52,7 +52,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
layer.LayerShape = new Rectangle(layer); layer.LayerShape = new Rectangle(layer);
// Apply the drag rectangle // Apply the drag rectangle
_layerEditorService.SetShapeRenderRect(layer.LayerShape, DragRectangle); _layerEditorService.SetShapeBaseFromRectangle(layer.LayerShape, DragRectangle);
ProfileEditorService.UpdateSelectedProfileElement(); ProfileEditorService.UpdateSelectedProfileElement();
} }
} }

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
@ -9,6 +10,8 @@ using Artemis.UI.Extensions;
using Artemis.UI.Properties; using Artemis.UI.Properties;
using Artemis.UI.Services; using Artemis.UI.Services;
using Artemis.UI.Services.Interfaces; using Artemis.UI.Services.Interfaces;
using SkiaSharp;
using SkiaSharp.Views.WPF;
namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
{ {
@ -52,18 +55,18 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
if (ProfileEditorService.SelectedProfileElement is Layer layer) if (ProfileEditorService.SelectedProfileElement is Layer layer)
{ {
// If the layer has a shape, save it's size // If the layer has a shape, save it's size
var shapeSize = Rect.Empty; var shapeSize = SKRect.Empty;
if (layer.LayerShape != null) if (layer.LayerShape != null)
shapeSize = _layerEditorService.GetShapeRenderRect(layer.LayerShape); shapeSize = layer.LayerShape.GetUnscaledRectangle();
var remainingLeds = layer.Leds.Except(selectedLeds).ToList(); var remainingLeds = layer.Leds.Except(selectedLeds).ToList();
layer.ClearLeds(); layer.ClearLeds();
layer.AddLeds(remainingLeds); layer.AddLeds(remainingLeds);
// Restore the saved size // Restore the saved size
if (layer.LayerShape != null) if (layer.LayerShape != null)
_layerEditorService.SetShapeRenderRect(layer.LayerShape, shapeSize); layer.LayerShape.SetFromUnscaledRectangle(shapeSize);
ProfileEditorService.UpdateSelectedProfileElement(); ProfileEditorService.UpdateSelectedProfileElement();
} }
} }

View File

@ -53,9 +53,9 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
if (ProfileEditorService.SelectedProfileElement is Layer layer) if (ProfileEditorService.SelectedProfileElement is Layer layer)
{ {
// If the layer has a shape, save it's size // If the layer has a shape, save it's size
var shapeSize = Rect.Empty; var shapeSize = SKRect.Empty;
if (layer.LayerShape != null) if (layer.LayerShape != null)
shapeSize = _layerEditorService.GetShapeRenderRect(layer.LayerShape); shapeSize = layer.LayerShape.GetUnscaledRectangle();
// If shift is held down, add to the selection instead of replacing it // If shift is held down, add to the selection instead of replacing it
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
@ -68,7 +68,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
// Restore the saved size // Restore the saved size
if (layer.LayerShape != null) if (layer.LayerShape != null)
_layerEditorService.SetShapeRenderRect(layer.LayerShape, shapeSize); layer.LayerShape.SetFromUnscaledRectangle(shapeSize);
ProfileEditorService.UpdateSelectedProfileElement(); ProfileEditorService.UpdateSelectedProfileElement();
} }

View File

@ -49,7 +49,7 @@ namespace Artemis.UI.Services
public SKPath GetLayerPath(Layer layer, bool includeTranslation, bool includeScale, bool includeRotation) public SKPath GetLayerPath(Layer layer, bool includeTranslation, bool includeScale, bool includeRotation)
{ {
var layerRect = GetLayerRenderRect(layer).ToSKRect(); var layerRect = GetLayerRenderRect(layer).ToSKRect();
var shapeRect = GetShapeRenderRect(layer.LayerShape).ToSKRect(); var shapeRect = GetShapeUntransformedRect(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, true);
@ -74,7 +74,7 @@ namespace Artemis.UI.Services
public void ReverseLayerPath(Layer layer, SKPath path) public void ReverseLayerPath(Layer layer, SKPath path)
{ {
var layerRect = GetLayerRenderRect(layer).ToSKRect(); var layerRect = GetLayerRenderRect(layer).ToSKRect();
var shapeRect = GetShapeRenderRect(layer.LayerShape).ToSKRect(); var shapeRect = GetShapeUntransformedRect(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, true);
@ -100,7 +100,7 @@ namespace Artemis.UI.Services
var position = layer.PositionProperty.CurrentValue; var position = layer.PositionProperty.CurrentValue;
position.X = (float) (position.X * layerRect.Width); position.X = (float) (position.X * layerRect.Width);
position.Y = (float) (position.Y * layerRect.Height); position.Y = (float) (position.Y * layerRect.Height);
var shapeRect = GetShapeRenderRect(layer.LayerShape); var shapeRect = GetShapeUntransformedRect(layer.LayerShape);
return new SKPoint((float) (position.X + shapeRect.Left), (float) (position.Y + shapeRect.Top)); return new SKPoint((float) (position.X + shapeRect.Left), (float) (position.Y + shapeRect.Top));
} }
@ -115,7 +115,7 @@ namespace Artemis.UI.Services
var layerRect = GetLayerRect(layer); var layerRect = GetLayerRect(layer);
if (absolute) if (absolute)
{ {
var shapeRect = GetShapeRenderRect(layer.LayerShape); var shapeRect = GetShapeUntransformedRect(layer.LayerShape);
var position = new SKPoint((float) ((point.X - shapeRect.Left) / layerRect.Width), (float) ((point.Y - shapeRect.Top) / layerRect.Height)); var position = new SKPoint((float) ((point.X - shapeRect.Left) / layerRect.Width), (float) ((point.Y - shapeRect.Top) / layerRect.Height));
layer.PositionProperty.SetCurrentValue(position, time); layer.PositionProperty.SetCurrentValue(position, time);
} }
@ -128,7 +128,7 @@ namespace Artemis.UI.Services
public TransformGroup GetLayerTransformGroup(Layer layer) public TransformGroup GetLayerTransformGroup(Layer layer)
{ {
var layerRect = GetLayerRenderRect(layer).ToSKRect(); var layerRect = GetLayerRenderRect(layer).ToSKRect();
var shapeRect = GetShapeRenderRect(layer.LayerShape).ToSKRect(); var shapeRect = GetShapeUntransformedRect(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, true);
@ -146,11 +146,11 @@ namespace Artemis.UI.Services
} }
/// <inheritdoc /> /// <inheritdoc />
public Rect GetShapeRenderRect(LayerShape layerShape) public Rect GetShapeUntransformedRect(LayerShape layerShape)
{ {
if (layerShape == null) if (layerShape == null)
return Rect.Empty; return Rect.Empty;
// 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(
@ -162,13 +162,21 @@ namespace Artemis.UI.Services
} }
/// <inheritdoc /> /// <inheritdoc />
public void SetShapeRenderRect(LayerShape layerShape, Rect rect) public Rect GetShapeTransformedRect(LayerShape layerShape)
{
var path = GetLayerPath(layerShape.Layer, true, true, false);
return path.Bounds.ToRect();
}
/// <inheritdoc />
public void SetShapeBaseFromRectangle(LayerShape layerShape, Rect rect)
{ {
if (!layerShape.Layer.Leds.Any()) if (!layerShape.Layer.Leds.Any())
{ {
layerShape.ScaledRectangle = SKRect.Empty; layerShape.ScaledRectangle = SKRect.Empty;
return; return;
} }
var layerRect = GetLayerRenderRect(layerShape.Layer).ToSKRect(); var layerRect = GetLayerRenderRect(layerShape.Layer).ToSKRect();
// Compensate for the current value of the position transformation // Compensate for the current value of the position transformation
@ -179,7 +187,7 @@ namespace Artemis.UI.Services
rect.Y += rect.Height / 2; rect.Y += rect.Height / 2;
rect.Y -= layerRect.Height * layerShape.Layer.PositionProperty.CurrentValue.Y; rect.Y -= layerRect.Height * layerShape.Layer.PositionProperty.CurrentValue.Y;
rect.Y += layerRect.Height * layerShape.Layer.AnchorPointProperty.CurrentValue.Y * layerShape.Layer.SizeProperty.CurrentValue.Height; rect.Y += layerRect.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;
rect.Width /= layerShape.Layer.SizeProperty.CurrentValue.Width; rect.Width /= layerShape.Layer.SizeProperty.CurrentValue.Width;
@ -201,15 +209,15 @@ namespace Artemis.UI.Services
var renderScale = _settingsService.GetSetting("Core.RenderScale", 1.0).Value; var renderScale = _settingsService.GetSetting("Core.RenderScale", 1.0).Value;
if (absolute) if (absolute)
{ {
return new SKPoint( return new SKPoint(
100f / layer.AbsoluteRectangle.Width * ((float) (point.X * renderScale) - layer.AbsoluteRectangle.Left) / 100f, 100f / layer.AbsoluteRectangle.Width * ((float) (point.X * renderScale) - layer.AbsoluteRectangle.Left) / 100f,
100f / layer.AbsoluteRectangle.Height * ((float) (point.Y * renderScale) - layer.AbsoluteRectangle.Top) / 100f 100f / layer.AbsoluteRectangle.Height * ((float) (point.Y * renderScale) - layer.AbsoluteRectangle.Top) / 100f
); );
} }
return new SKPoint( return new SKPoint(
100f / layer.AbsoluteRectangle.Width * (float)(point.X * renderScale) / 100f, 100f / layer.AbsoluteRectangle.Width * (float) (point.X * renderScale) / 100f,
100f / layer.AbsoluteRectangle.Height * (float)(point.Y * renderScale) / 100f 100f / layer.AbsoluteRectangle.Height * (float) (point.Y * renderScale) / 100f
); );
} }
} }
@ -249,7 +257,7 @@ namespace Artemis.UI.Services
/// <param name="absolute"></param> /// <param name="absolute"></param>
/// <returns></returns> /// <returns></returns>
SKPoint GetLayerAnchor(Layer layer, bool absolute); SKPoint GetLayerAnchor(Layer layer, bool absolute);
void SetLayerAnchor(Layer layer, SKPoint point, bool absolute, TimeSpan? time); void SetLayerAnchor(Layer layer, SKPoint point, bool absolute, TimeSpan? time);
/// <summary> /// <summary>
@ -261,21 +269,29 @@ 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. /// Returns an absolute and scaled rectangle for the given shape that is corrected for the current render scale without
/// any transformations applied.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
Rect GetShapeRenderRect(LayerShape layerShape); Rect GetShapeUntransformedRect(LayerShape layerShape);
/// <summary> /// <summary>
/// Sets the render rectangle of the given shape to match the provided unscaled rectangle. The rectangle is corrected /// Returns an absolute and scaled rectangle for the given shape that is corrected for the current render scale with
/// for the current render scale. /// translation and scale transformations applied.
/// </summary>
/// <returns></returns>
Rect GetShapeTransformedRect(LayerShape layerShape);
/// <summary>
/// Sets the base properties of the given shape to match the provided unscaled rectangle. The rectangle is corrected
/// for the current render scale, anchor property and size property.
/// </summary> /// </summary>
/// <param name="layerShape"></param> /// <param name="layerShape"></param>
/// <param name="rect"></param> /// <param name="rect"></param>
void SetShapeRenderRect(LayerShape layerShape, Rect rect); void SetShapeBaseFromRectangle(LayerShape layerShape, Rect rect);
/// <summary> /// <summary>
/// Returns a new point scaled to the layer. /// Returns a new point scaled to the layer.
/// </summary> /// </summary>
/// <param name="layer"></param> /// <param name="layer"></param>
/// <param name="point"></param> /// <param name="point"></param>