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

Few anchor fixes

This commit is contained in:
SpoinkyNL 2020-01-31 18:32:41 +01:00
parent 960584cc3c
commit 436994129a
3 changed files with 40 additions and 30 deletions

View File

@ -179,8 +179,8 @@ namespace Artemis.Core.Models.Profile
var anchorProperty = AnchorPointProperty.CurrentValue; var anchorProperty = AnchorPointProperty.CurrentValue;
// 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 = anchorPosition.X - LayerShape.Bounds.Width / 2 - anchorProperty.X * Bounds.Width; var x = anchorPosition.X - LayerShape.Bounds.MidX - anchorProperty.X * Bounds.Width;
var y = anchorPosition.Y - LayerShape.Bounds.Height / 2 - anchorProperty.Y * Bounds.Height; var y = anchorPosition.Y - LayerShape.Bounds.MidY - anchorProperty.Y * Bounds.Height;
// Apply these before translation because anchorPosition takes translation into account // Apply these before translation because anchorPosition takes translation into account
canvas.RotateDegrees(rotationProperty, anchorPosition.X, anchorPosition.Y); canvas.RotateDegrees(rotationProperty, anchorPosition.X, anchorPosition.Y);
@ -213,7 +213,7 @@ namespace Artemis.Core.Models.Profile
var positionProperty = PositionProperty.CurrentValue; var positionProperty = PositionProperty.CurrentValue;
// Start at the center of the shape // 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 // Apply translation
position.X += positionProperty.X * Bounds.Width; position.X += positionProperty.X * Bounds.Width;

View File

@ -68,7 +68,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.GetLayerAnchorPosition(layer), GetRelativePosition(sender, e.MouseEventArgs)); _previousDragAngle = CalculateAngle(layer, sender, e.MouseEventArgs);
else else
_previousDragAngle = 0; _previousDragAngle = 0;
} }
@ -85,7 +85,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
return; return;
var previousDragAngle = _previousDragAngle; var previousDragAngle = _previousDragAngle;
var newRotation = CalculateAngle(_layerEditorService.GetLayerAnchorPosition(layer), GetRelativePosition(sender, e.MouseEventArgs)); var newRotation = CalculateAngle(layer, 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
@ -391,8 +391,14 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
return mouseEventArgs.GetPosition((IInputElement) parent); 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 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);
if (angle < 0f) if (angle < 0f)

View File

@ -50,11 +50,11 @@ namespace Artemis.UI.Services
public Point GetLayerAnchorPosition(Layer layer) public Point GetLayerAnchorPosition(Layer layer)
{ {
var layerBounds = GetLayerBounds(layer); var layerBounds = GetLayerBounds(layer);
var shapeBounds = GetLayerShapeBounds(layer.LayerShape); var shapeBounds = GetLayerShapeBounds(layer.LayerShape).ToSKRect();
var positionProperty = layer.PositionProperty.CurrentValue; var positionProperty = layer.PositionProperty.CurrentValue;
// Start at the center of the shape // 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 // Apply translation
position.X += positionProperty.X * layerBounds.Width; position.X += positionProperty.X * layerBounds.Width;
@ -77,8 +77,8 @@ namespace Artemis.UI.Services
var anchorProperty = layer.AnchorPointProperty.CurrentValue; var anchorProperty = layer.AnchorPointProperty.CurrentValue;
// 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 = anchorPosition.X - shapeBounds.Width / 2 - anchorProperty.X * layerBounds.Width; var x = anchorPosition.X - shapeBounds.MidX - anchorProperty.X * layerBounds.Width;
var y = anchorPosition.Y - shapeBounds.Height / 2 - anchorProperty.Y * layerBounds.Height; var y = anchorPosition.Y - shapeBounds.MidY - anchorProperty.Y * layerBounds.Height;
var transformGroup = new TransformGroup(); var transformGroup = new TransformGroup();
transformGroup.Children.Add(new TranslateTransform(x, y)); transformGroup.Children.Add(new TranslateTransform(x, y));
@ -99,8 +99,8 @@ namespace Artemis.UI.Services
var anchorProperty = layer.AnchorPointProperty.CurrentValue; var anchorProperty = layer.AnchorPointProperty.CurrentValue;
// 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 = anchorPosition.X - shapeBounds.Width / 2 - anchorProperty.X * layerBounds.Width; var x = anchorPosition.X - shapeBounds.MidX - anchorProperty.X * layerBounds.Width;
var y = anchorPosition.Y - shapeBounds.Height / 2 - anchorProperty.Y * layerBounds.Height; var y = anchorPosition.Y - shapeBounds.MidY - anchorProperty.Y * layerBounds.Height;
var path = new SKPath(); var path = new SKPath();
path.AddRect(shapeBounds); path.AddRect(shapeBounds);
@ -114,7 +114,6 @@ namespace Artemis.UI.Services
return path; return path;
} }
/// <inheritdoc /> /// <inheritdoc />
public void SetShapeBaseFromRectangle(LayerShape layerShape, Rect rect) public void SetShapeBaseFromRectangle(LayerShape layerShape, Rect rect)
{ {
@ -125,27 +124,32 @@ namespace Artemis.UI.Services
} }
var layerBounds = GetLayerBounds(layerShape.Layer).ToSKRect(); var layerBounds = GetLayerBounds(layerShape.Layer).ToSKRect();
var shapeBounds = GetLayerShapeBounds(layerShape).ToSKRect();
// Compensate for the current value of the position transformation var rectangle = SKRect.Create(
rect.X += rect.Width / 2; (float) rect.Left - layerBounds.Left,
rect.X -= layerBounds.Width * layerShape.Layer.PositionProperty.CurrentValue.X; (float) rect.Top - layerBounds.Top,
rect.X += layerBounds.Width * layerShape.Layer.AnchorPointProperty.CurrentValue.X * layerShape.Layer.SizeProperty.CurrentValue.Width; (float) rect.Width,
(float) rect.Height
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;
// 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 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( layerShape.ScaledRectangle = SKRect.Create(
100f / layerShape.Layer.Bounds.Width * ((float) (rect.Left * renderScale) - layerShape.Layer.Bounds.Left) / 100f, rectangle.Left / layerBounds.Width,
100f / layerShape.Layer.Bounds.Height * ((float) (rect.Top * renderScale) - layerShape.Layer.Bounds.Top) / 100f, rectangle.Top / layerBounds.Height,
100f / layerShape.Layer.Bounds.Width * (float) (rect.Width * renderScale) / 100f, rectangle.Width / layerBounds.Width,
100f / layerShape.Layer.Bounds.Height * (float) (rect.Height * renderScale) / 100f rectangle.Height / layerBounds.Height
); );
layerShape.CalculateRenderProperties(); layerShape.CalculateRenderProperties();
} }