From 436994129a195dca5c854fec729625b923169c20 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Fri, 31 Jan 2020 18:32:41 +0100 Subject: [PATCH] Few anchor fixes --- src/Artemis.Core/Models/Profile/Layer.cs | 6 +-- .../Visualization/Tools/EditToolViewModel.cs | 12 +++-- src/Artemis.UI/Services/LayerShapeService.cs | 52 ++++++++++--------- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/Artemis.Core/Models/Profile/Layer.cs b/src/Artemis.Core/Models/Profile/Layer.cs index e8e4a86f4..71afae138 100644 --- a/src/Artemis.Core/Models/Profile/Layer.cs +++ b/src/Artemis.Core/Models/Profile/Layer.cs @@ -179,8 +179,8 @@ namespace Artemis.Core.Models.Profile var anchorProperty = AnchorPointProperty.CurrentValue; // Translation originates from the unscaled center of the shape and is tied to the anchor - var x = anchorPosition.X - LayerShape.Bounds.Width / 2 - anchorProperty.X * Bounds.Width; - var y = anchorPosition.Y - LayerShape.Bounds.Height / 2 - anchorProperty.Y * Bounds.Height; + var x = anchorPosition.X - LayerShape.Bounds.MidX - anchorProperty.X * Bounds.Width; + var y = anchorPosition.Y - LayerShape.Bounds.MidY - anchorProperty.Y * Bounds.Height; // Apply these before translation because anchorPosition takes translation into account canvas.RotateDegrees(rotationProperty, anchorPosition.X, anchorPosition.Y); @@ -213,7 +213,7 @@ namespace Artemis.Core.Models.Profile var positionProperty = PositionProperty.CurrentValue; // Start at the center of the shape - var position = new SKPoint(LayerShape.Bounds.Left, LayerShape.Bounds.Top); + var position = new SKPoint(LayerShape.Bounds.MidX, LayerShape.Bounds.MidY); // Apply translation position.X += positionProperty.X * Bounds.Width; diff --git a/src/Artemis.UI/Screens/Module/ProfileEditor/Visualization/Tools/EditToolViewModel.cs b/src/Artemis.UI/Screens/Module/ProfileEditor/Visualization/Tools/EditToolViewModel.cs index adda988d5..dce96197e 100644 --- a/src/Artemis.UI/Screens/Module/ProfileEditor/Visualization/Tools/EditToolViewModel.cs +++ b/src/Artemis.UI/Screens/Module/ProfileEditor/Visualization/Tools/EditToolViewModel.cs @@ -68,7 +68,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools { _rotating = true; if (ProfileEditorService.SelectedProfileElement is Layer layer) - _previousDragAngle = CalculateAngle(_layerEditorService.GetLayerAnchorPosition(layer), GetRelativePosition(sender, e.MouseEventArgs)); + _previousDragAngle = CalculateAngle(layer, sender, e.MouseEventArgs); else _previousDragAngle = 0; } @@ -85,7 +85,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools return; var previousDragAngle = _previousDragAngle; - var newRotation = CalculateAngle(_layerEditorService.GetLayerAnchorPosition(layer), GetRelativePosition(sender, e.MouseEventArgs)); + var newRotation = CalculateAngle(layer, sender, e.MouseEventArgs); _previousDragAngle = newRotation; // Allow the user to rotate the shape in increments of 5 @@ -391,8 +391,14 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools return mouseEventArgs.GetPosition((IInputElement) parent); } - private float CalculateAngle(Point start, Point arrival) + private float CalculateAngle(Layer layer, object mouseEventSender, MouseEventArgs mouseEvent) { + var layerBounds = _layerEditorService.GetLayerBounds(layer); + var start = _layerEditorService.GetLayerAnchorPosition(layer); + start.X += layerBounds.Left; + start.Y += layerBounds.Top; + var arrival = GetRelativePosition(mouseEventSender, mouseEvent); + var radian = (float) Math.Atan2(start.Y - arrival.Y, start.X - arrival.X); var angle = radian * (180f / (float) Math.PI); if (angle < 0f) diff --git a/src/Artemis.UI/Services/LayerShapeService.cs b/src/Artemis.UI/Services/LayerShapeService.cs index f4694ce89..aaa80d7a0 100644 --- a/src/Artemis.UI/Services/LayerShapeService.cs +++ b/src/Artemis.UI/Services/LayerShapeService.cs @@ -50,11 +50,11 @@ namespace Artemis.UI.Services public Point GetLayerAnchorPosition(Layer layer) { var layerBounds = GetLayerBounds(layer); - var shapeBounds = GetLayerShapeBounds(layer.LayerShape); + var shapeBounds = GetLayerShapeBounds(layer.LayerShape).ToSKRect(); var positionProperty = layer.PositionProperty.CurrentValue; // Start at the center of the shape - var position = new Point(shapeBounds.Left, shapeBounds.Top); + var position = new Point(shapeBounds.MidX, shapeBounds.MidY); // Apply translation position.X += positionProperty.X * layerBounds.Width; @@ -77,8 +77,8 @@ namespace Artemis.UI.Services var anchorProperty = layer.AnchorPointProperty.CurrentValue; // Translation originates from the unscaled center of the shape and is tied to the anchor - var x = anchorPosition.X - shapeBounds.Width / 2 - anchorProperty.X * layerBounds.Width; - var y = anchorPosition.Y - shapeBounds.Height / 2 - anchorProperty.Y * layerBounds.Height; + var x = anchorPosition.X - shapeBounds.MidX - anchorProperty.X * layerBounds.Width; + var y = anchorPosition.Y - shapeBounds.MidY - anchorProperty.Y * layerBounds.Height; var transformGroup = new TransformGroup(); transformGroup.Children.Add(new TranslateTransform(x, y)); @@ -99,8 +99,8 @@ namespace Artemis.UI.Services var anchorProperty = layer.AnchorPointProperty.CurrentValue; // Translation originates from the unscaled center of the shape and is tied to the anchor - var x = anchorPosition.X - shapeBounds.Width / 2 - anchorProperty.X * layerBounds.Width; - var y = anchorPosition.Y - shapeBounds.Height / 2 - anchorProperty.Y * layerBounds.Height; + var x = anchorPosition.X - shapeBounds.MidX - anchorProperty.X * layerBounds.Width; + var y = anchorPosition.Y - shapeBounds.MidY - anchorProperty.Y * layerBounds.Height; var path = new SKPath(); path.AddRect(shapeBounds); @@ -114,7 +114,6 @@ namespace Artemis.UI.Services return path; } - /// public void SetShapeBaseFromRectangle(LayerShape layerShape, Rect rect) { @@ -125,27 +124,32 @@ namespace Artemis.UI.Services } var layerBounds = GetLayerBounds(layerShape.Layer).ToSKRect(); + var shapeBounds = GetLayerShapeBounds(layerShape).ToSKRect(); - // Compensate for the current value of the position transformation - rect.X += rect.Width / 2; - rect.X -= layerBounds.Width * layerShape.Layer.PositionProperty.CurrentValue.X; - rect.X += layerBounds.Width * layerShape.Layer.AnchorPointProperty.CurrentValue.X * layerShape.Layer.SizeProperty.CurrentValue.Width; - - rect.Y += rect.Height / 2; - rect.Y -= layerBounds.Height * layerShape.Layer.PositionProperty.CurrentValue.Y; - rect.Y += layerBounds.Height * layerShape.Layer.AnchorPointProperty.CurrentValue.Y * layerShape.Layer.SizeProperty.CurrentValue.Height; - - // Compensate for the current value of the size transformation - rect.Height /= layerShape.Layer.SizeProperty.CurrentValue.Height; - rect.Width /= layerShape.Layer.SizeProperty.CurrentValue.Width; + var rectangle = SKRect.Create( + (float) rect.Left - layerBounds.Left, + (float) rect.Top - layerBounds.Top, + (float) rect.Width, + (float) rect.Height + ); // Adjust the provided rect for the difference in render scale - var renderScale = _settingsService.GetSetting("Core.RenderScale", 1.0).Value; + var anchorPosition = GetLayerAnchorPosition(layerShape.Layer).ToSKPoint(); + var anchorProperty = layerShape.Layer.AnchorPointProperty.CurrentValue; + var sizeProperty = layerShape.Layer.SizeProperty.CurrentValue; + + // Translation originates from the unscaled center of the shape and is tied to the anchor + var x = anchorPosition.X - shapeBounds.MidX - anchorProperty.X * layerBounds.Width; + var y = anchorPosition.Y - shapeBounds.MidY - anchorProperty.Y * layerBounds.Height; + rectangle.Offset(x * -1, y * -1); + + // TODO: Determine the new position of the anchor and scale the rectangle to the current scale + layerShape.ScaledRectangle = SKRect.Create( - 100f / layerShape.Layer.Bounds.Width * ((float) (rect.Left * renderScale) - layerShape.Layer.Bounds.Left) / 100f, - 100f / layerShape.Layer.Bounds.Height * ((float) (rect.Top * renderScale) - layerShape.Layer.Bounds.Top) / 100f, - 100f / layerShape.Layer.Bounds.Width * (float) (rect.Width * renderScale) / 100f, - 100f / layerShape.Layer.Bounds.Height * (float) (rect.Height * renderScale) / 100f + rectangle.Left / layerBounds.Width, + rectangle.Top / layerBounds.Height, + rectangle.Width / layerBounds.Width, + rectangle.Height / layerBounds.Height ); layerShape.CalculateRenderProperties(); }