mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 20:06:33 +00:00
Refactored the edit tool
Fixed the fill shape but it might need some rethinking
This commit is contained in:
1 parent
a494c32f0e
commit
cd1b75750e
32 files changed
+1037
-826
No files matched your search
@@ -90,7 +90,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization
|
||||
|
||||
var layerGeometry = group.GetOutlinedPathGeometry();
|
||||
var opacityGeometry = Geometry.Combine(Geometry.Empty, layerGeometry, GeometryCombineMode.Exclude, new TranslateTransform());
|
||||
|
||||
|
||||
LayerGeometry = layerGeometry;
|
||||
OpacityGeometry = opacityGeometry;
|
||||
}
|
||||
@@ -113,7 +113,12 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization
|
||||
shapeGeometry = new EllipseGeometry(rect);
|
||||
break;
|
||||
case Fill _:
|
||||
shapeGeometry = LayerGeometry;
|
||||
// Shape originates from the center so compensate the geometry for that, create a copy
|
||||
shapeGeometry = LayerGeometry.Clone();
|
||||
// Add a transformation
|
||||
shapeGeometry.Transform = new TranslateTransform(rect.Left - shapeGeometry.Bounds.Left, rect.Top - shapeGeometry.Bounds.Top);
|
||||
// Apply the transformation so that it won't be overridden
|
||||
shapeGeometry = shapeGeometry.GetOutlinedPathGeometry();
|
||||
break;
|
||||
case Polygon _:
|
||||
// TODO
|
||||
@@ -123,7 +128,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization
|
||||
shapeGeometry = new RectangleGeometry(rect);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
shapeGeometry.Transform = _layerEditorService.GetLayerTransformGroup(Layer);
|
||||
ShapeGeometry = shapeGeometry;
|
||||
});
|
||||
|
||||
@@ -59,14 +59,11 @@
|
||||
<ListBoxItem ToolTip="Edit shape in layer">
|
||||
<materialDesign:PackIcon Kind="Edit" />
|
||||
</ListBoxItem>
|
||||
<ListBoxItem ToolTip="Change layer selection">
|
||||
<ListBoxItem ToolTip="Change layer selection (hold SHIFT to add to existing selection)">
|
||||
<materialDesign:PackIcon Kind="SelectionDrag" />
|
||||
</ListBoxItem>
|
||||
<ListBoxItem ToolTip="Add to layer selection">
|
||||
<materialDesign:PackIcon Kind="AddToPhotos" />
|
||||
</ListBoxItem>
|
||||
<ListBoxItem ToolTip="Remove from layer selection">
|
||||
<materialDesign:PackIcon Kind="MinusBoxMultiple" />
|
||||
<materialDesign:PackIcon Kind="SelectOff" />
|
||||
</ListBoxItem>
|
||||
<Separator />
|
||||
<ListBoxItem ToolTip="Create round shape in layer">
|
||||
|
||||
@@ -263,22 +263,19 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization
|
||||
ActiveToolViewModel = new SelectionToolViewModel(this, _profileEditorService, _layerEditorService);
|
||||
break;
|
||||
case 3:
|
||||
ActiveToolViewModel = new SelectionAddToolViewModel(this, _profileEditorService);
|
||||
ActiveToolViewModel = new SelectionRemoveToolViewModel(this, _profileEditorService, _layerEditorService);
|
||||
break;
|
||||
case 4:
|
||||
ActiveToolViewModel = new SelectionRemoveToolViewModel(this, _profileEditorService);
|
||||
break;
|
||||
case 6:
|
||||
case 5:
|
||||
ActiveToolViewModel = new EllipseToolViewModel(this, _profileEditorService, _layerEditorService);
|
||||
break;
|
||||
case 7:
|
||||
case 6:
|
||||
ActiveToolViewModel = new RectangleToolViewModel(this, _profileEditorService, _layerEditorService);
|
||||
break;
|
||||
case 8:
|
||||
case 7:
|
||||
ActiveToolViewModel = new PolygonToolViewModel(this, _profileEditorService);
|
||||
break;
|
||||
case 9:
|
||||
ActiveToolViewModel = new FillToolViewModel(this, _profileEditorService);
|
||||
case 8:
|
||||
ActiveToolViewModel = new FillToolViewModel(this, _profileEditorService, _layerEditorService);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
+14
-132
@@ -4,143 +4,25 @@
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools"
|
||||
xmlns:userControls="clr-namespace:Artemis.UI.Screens.Module.ProfileEditor.Visualization.UserControls"
|
||||
xmlns:s="https://github.com/canton7/Stylet"
|
||||
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450"
|
||||
d:DesignWidth="800"
|
||||
d:DataContext="{d:DesignInstance {x:Type local:EditToolViewModel}}">
|
||||
<Canvas UseLayoutRounding="False">
|
||||
|
||||
<!-- Render these first so that they are always behind the actual shape -->
|
||||
<Ellipse Width="{Binding RotateSize}"
|
||||
Height="{Binding RotateSize}"
|
||||
Margin="{Binding RotateOffset}"
|
||||
Canvas.Left="{Binding TopLeft.X}"
|
||||
Canvas.Top="{Binding TopLeft.Y}"
|
||||
Fill="Transparent"
|
||||
MouseDown="{s:Action ShapeEditMouseDown}" MouseUp="{s:Action ShapeEditMouseUp}" MouseMove="{s:Action Rotate}" Cursor="{Binding TopLeftRotateCursor}" />
|
||||
<Ellipse Width="{Binding RotateSize}"
|
||||
Height="{Binding RotateSize}"
|
||||
Margin="{Binding RotateOffset}"
|
||||
Canvas.Left="{Binding TopRight.X}"
|
||||
Canvas.Top="{Binding TopRight.Y}"
|
||||
Fill="Transparent"
|
||||
MouseDown="{s:Action ShapeEditMouseDown}" MouseUp="{s:Action ShapeEditMouseUp}" MouseMove="{s:Action Rotate}" Cursor="{Binding TopRightRotateCursor}" />
|
||||
<Ellipse Width="{Binding RotateSize}"
|
||||
Height="{Binding RotateSize}"
|
||||
Margin="{Binding RotateOffset}"
|
||||
Canvas.Left="{Binding BottomRight.X}"
|
||||
Canvas.Top="{Binding BottomRight.Y}"
|
||||
Fill="Transparent"
|
||||
MouseDown="{s:Action ShapeEditMouseDown}" MouseUp="{s:Action ShapeEditMouseUp}" MouseMove="{s:Action Rotate}" Cursor="{Binding BottomRightRotateCursor}" />
|
||||
<Ellipse Width="{Binding RotateSize}"
|
||||
Height="{Binding RotateSize}"
|
||||
Margin="{Binding RotateOffset}"
|
||||
Canvas.Left="{Binding BottomLeft.X}"
|
||||
Canvas.Top="{Binding BottomLeft.Y}"
|
||||
Fill="Transparent"
|
||||
MouseDown="{s:Action ShapeEditMouseDown}" MouseUp="{s:Action ShapeEditMouseUp}" MouseMove="{s:Action Rotate}" Cursor="{Binding BottomLeftRotateCursor}" />
|
||||
<!-- The part of the layer's shape that is inside the layer -->
|
||||
<Path Data="{Binding ShapeGeometry, Mode=OneWay}"
|
||||
Fill="Transparent"
|
||||
Stroke="{DynamicResource PrimaryHueMidBrush}"
|
||||
StrokeThickness="{Binding OutlineThickness}"
|
||||
StrokeDashArray="2 2"
|
||||
Cursor="Hand"
|
||||
MouseDown="{s:Action ShapeEditMouseDown}"
|
||||
MouseUp="{s:Action ShapeEditMouseUp}"
|
||||
MouseMove="{s:Action Move}" />
|
||||
|
||||
<!-- Mutation points -->
|
||||
<Rectangle Width="{Binding ControlSize}"
|
||||
Height="{Binding ControlSize}"
|
||||
Margin="{Binding ControlOffset}"
|
||||
Canvas.Left="{Binding TopCenter.X}"
|
||||
Canvas.Top="{Binding TopCenter.Y}"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
MouseDown="{s:Action ShapeEditMouseDown}" MouseUp="{s:Action ShapeEditMouseUp}" MouseMove="{s:Action TopCenterResize}" />
|
||||
<Rectangle Width="{Binding ControlSize}"
|
||||
Height="{Binding ControlSize}"
|
||||
Margin="{Binding ControlOffset}"
|
||||
Canvas.Left="{Binding RightCenter.X}"
|
||||
Canvas.Top="{Binding RightCenter.Y}"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
MouseDown="{s:Action ShapeEditMouseDown}" MouseUp="{s:Action ShapeEditMouseUp}" MouseMove="{s:Action RightCenterResize}" />
|
||||
<Rectangle Width="{Binding ControlSize}"
|
||||
Height="{Binding ControlSize}"
|
||||
Margin="{Binding ControlOffset}"
|
||||
Canvas.Left="{Binding BottomCenter.X}"
|
||||
Canvas.Top="{Binding BottomCenter.Y}"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
MouseDown="{s:Action ShapeEditMouseDown}" MouseUp="{s:Action ShapeEditMouseUp}" MouseMove="{s:Action BottomCenterResize}" />
|
||||
<Rectangle Width="{Binding ControlSize}"
|
||||
Height="{Binding ControlSize}"
|
||||
Margin="{Binding ControlOffset}"
|
||||
Canvas.Left="{Binding LeftCenter.X}"
|
||||
Canvas.Top="{Binding LeftCenter.Y}"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
MouseDown="{s:Action ShapeEditMouseDown}" MouseUp="{s:Action ShapeEditMouseUp}" MouseMove="{s:Action LeftCenterResize}" />
|
||||
|
||||
<Rectangle Width="{Binding ControlSize}"
|
||||
Height="{Binding ControlSize}"
|
||||
Margin="{Binding ControlOffset}"
|
||||
Canvas.Left="{Binding TopLeft.X}"
|
||||
Canvas.Top="{Binding TopLeft.Y}"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
MouseDown="{s:Action ShapeEditMouseDown}" MouseUp="{s:Action ShapeEditMouseUp}" MouseMove="{s:Action TopLeftResize}" />
|
||||
<Rectangle Width="{Binding ControlSize}"
|
||||
Height="{Binding ControlSize}"
|
||||
Margin="{Binding ControlOffset}"
|
||||
Canvas.Left="{Binding TopRight.X}"
|
||||
Canvas.Top="{Binding TopRight.Y}"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
MouseDown="{s:Action ShapeEditMouseDown}" MouseUp="{s:Action ShapeEditMouseUp}" MouseMove="{s:Action TopRightResize}" />
|
||||
<Rectangle Width="{Binding ControlSize}"
|
||||
Height="{Binding ControlSize}"
|
||||
Margin="{Binding ControlOffset}"
|
||||
Canvas.Left="{Binding BottomRight.X}"
|
||||
Canvas.Top="{Binding BottomRight.Y}"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
MouseDown="{s:Action ShapeEditMouseDown}" MouseUp="{s:Action ShapeEditMouseUp}" MouseMove="{s:Action BottomRightResize}" />
|
||||
<Rectangle Width="{Binding ControlSize}"
|
||||
Height="{Binding ControlSize}"
|
||||
Margin="{Binding ControlOffset}"
|
||||
Canvas.Left="{Binding BottomLeft.X}"
|
||||
Canvas.Top="{Binding BottomLeft.Y}"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
MouseDown="{s:Action ShapeEditMouseDown}" MouseUp="{s:Action ShapeEditMouseUp}" MouseMove="{s:Action BottomLeftResize}" />
|
||||
|
||||
<!-- Anchor point -->
|
||||
|
||||
<Ellipse MouseDown="{s:Action AnchorEditMouseDown}"
|
||||
MouseUp="{s:Action ShapeEditMouseUp}"
|
||||
MouseMove="{s:Action AnchorMove}"
|
||||
Width="{Binding ControlSize}"
|
||||
Height="{Binding ControlSize}"
|
||||
Margin="{Binding ControlOffset}"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
Canvas.Left="{Binding ShapeAnchor.X}"
|
||||
Canvas.Top="{Binding ShapeAnchor.Y}"
|
||||
Cursor="SizeAll" />
|
||||
<userControls:LayerShapeControl Zoom="{Binding ProfileViewModel.PanZoomViewModel.Zoom}"
|
||||
ShapePath="{Binding ShapePath}"
|
||||
ShapeAnchor="{Binding ShapeAnchor}"
|
||||
ShapeGeometry="{Binding ShapeGeometry}"
|
||||
RotateMouseDown="{s:Action RotateMouseDown}"
|
||||
RotateMouseUp="{s:Action RotateMouseUp}"
|
||||
RotateMouseMove="{s:Action RotateMouseMove}"
|
||||
ResizeMouseDown="{s:Action ResizeMouseDown}"
|
||||
ResizeMouseUp="{s:Action ResizeMouseUp}"
|
||||
ResizeMouseMove="{s:Action ResizeMouseMove}"
|
||||
MoveMouseDown="{s:Action MoveMouseDown}"
|
||||
MoveMouseUp="{s:Action MoveMouseUp}"
|
||||
MoveMouseMove="{s:Action MoveMouseMove}" />
|
||||
</Canvas>
|
||||
|
||||
</UserControl>
|
||||
+257
-357
@@ -1,13 +1,11 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using Artemis.Core.Models.Profile;
|
||||
using Artemis.UI.Properties;
|
||||
using Artemis.UI.Events;
|
||||
using Artemis.UI.Services;
|
||||
using Artemis.UI.Services.Interfaces;
|
||||
using Artemis.UI.Utilities;
|
||||
using SkiaSharp;
|
||||
using SkiaSharp.Views.WPF;
|
||||
using Stylet;
|
||||
@@ -17,14 +15,10 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
|
||||
public class EditToolViewModel : VisualizationToolViewModel
|
||||
{
|
||||
private readonly ILayerEditorService _layerEditorService;
|
||||
private bool _draggingHorizontally;
|
||||
private bool _draggingVertically;
|
||||
private SKPoint _dragOffset;
|
||||
private SKPoint _dragStart;
|
||||
private SKPoint _dragStartAnchor;
|
||||
private float _previousDragAngle;
|
||||
private SKSize _dragStartScale;
|
||||
private bool _isDragging;
|
||||
private SKPoint _topLeft;
|
||||
|
||||
public EditToolViewModel(ProfileViewModel profileViewModel, IProfileEditorService profileEditorService, ILayerEditorService layerEditorService)
|
||||
: base(profileViewModel, profileEditorService)
|
||||
@@ -32,66 +26,24 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
|
||||
_layerEditorService = layerEditorService;
|
||||
Cursor = Cursors.Arrow;
|
||||
Update();
|
||||
UpdateControls();
|
||||
|
||||
ProfileViewModel.PanZoomViewModel.PropertyChanged += (sender, args) => UpdateControls();
|
||||
profileEditorService.SelectedProfileChanged += (sender, args) => Update();
|
||||
profileEditorService.SelectedProfileElementChanged += (sender, args) => Update();
|
||||
profileEditorService.SelectedProfileElementUpdated += (sender, args) => Update();
|
||||
profileEditorService.ProfilePreviewUpdated += (sender, args) => Update();
|
||||
}
|
||||
|
||||
|
||||
public double ControlSize { get; set; }
|
||||
public double RotateSize { get; set; }
|
||||
public Thickness ControlOffset { get; set; }
|
||||
public Thickness RotateOffset { get; set; }
|
||||
public double OutlineThickness { get; set; }
|
||||
|
||||
public SKRect ShapeRectangle { get; set; }
|
||||
public SKPath ShapePath { get; set; }
|
||||
public SKPoint ShapeAnchor { get; set; }
|
||||
public RectangleGeometry ShapeGeometry { get; set; }
|
||||
public TransformCollection ShapeTransformCollection { get; set; }
|
||||
|
||||
public SKPoint TopLeft { get; set; }
|
||||
public SKPoint TopRight { get; set; }
|
||||
public SKPoint BottomRight { get; set; }
|
||||
public SKPoint BottomLeft { get; set; }
|
||||
public SKPoint TopCenter { get; set; }
|
||||
public SKPoint RightCenter { get; set; }
|
||||
public SKPoint BottomCenter { get; set; }
|
||||
public SKPoint LeftCenter { get; set; }
|
||||
|
||||
public Cursor TopLeftRotateCursor { get; set; }
|
||||
public Cursor TopRightRotateCursor { get; set; }
|
||||
public Cursor BottomRightRotateCursor { get; set; }
|
||||
public Cursor BottomLeftRotateCursor { get; set; }
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
ShapeRectangle = _layerEditorService.GetShapeRenderRect(layer.LayerShape).ToSKRect();
|
||||
ShapePath = _layerEditorService.GetLayerPath(layer, true, true, true);
|
||||
ShapeAnchor = _layerEditorService.GetLayerAnchor(layer, true);
|
||||
|
||||
// Get a square path to use for mutation point placement
|
||||
var path = _layerEditorService.GetLayerPath(layer, true, true, true);
|
||||
TopLeft = path.Points[0];
|
||||
TopRight = path.Points[1];
|
||||
BottomRight = path.Points[2];
|
||||
BottomLeft = path.Points[3];
|
||||
|
||||
TopCenter = new SKPoint((TopLeft.X + TopRight.X) / 2, (TopLeft.Y + TopRight.Y) / 2);
|
||||
RightCenter = new SKPoint((TopRight.X + BottomRight.X) / 2, (TopRight.Y + BottomRight.Y) / 2);
|
||||
BottomCenter = new SKPoint((BottomLeft.X + BottomRight.X) / 2, (BottomLeft.Y + BottomRight.Y) / 2);
|
||||
LeftCenter = new SKPoint((TopLeft.X + BottomLeft.X) / 2, (TopLeft.Y + BottomLeft.Y) / 2);
|
||||
|
||||
TopLeftRotateCursor = CursorUtilities.GetRotatedCursor(Resources.aero_rotate_tl_ico, layer.RotationProperty.CurrentValue);
|
||||
TopRightRotateCursor = CursorUtilities.GetRotatedCursor(Resources.aero_rotate_tr_ico, layer.RotationProperty.CurrentValue);
|
||||
BottomRightRotateCursor = CursorUtilities.GetRotatedCursor(Resources.aero_rotate_br_ico, layer.RotationProperty.CurrentValue);
|
||||
BottomLeftRotateCursor = CursorUtilities.GetRotatedCursor(Resources.aero_rotate_bl_ico, layer.RotationProperty.CurrentValue);
|
||||
|
||||
Execute.PostToUIThread(() =>
|
||||
{
|
||||
var shapeGeometry = new RectangleGeometry(_layerEditorService.GetShapeRenderRect(layer.LayerShape))
|
||||
@@ -100,283 +52,39 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
|
||||
};
|
||||
shapeGeometry.Freeze();
|
||||
ShapeGeometry = shapeGeometry;
|
||||
ShapeTransformCollection = _layerEditorService.GetLayerTransformGroup(layer).Children;
|
||||
ShapeTransformCollection.Freeze();
|
||||
});
|
||||
|
||||
// Store the last top-left for easy later on
|
||||
_topLeft = _layerEditorService.GetLayerPath(layer, true, true, true).Points[0];
|
||||
}
|
||||
|
||||
private void UpdateControls()
|
||||
{
|
||||
ControlSize = Math.Max(10 / ProfileViewModel.PanZoomViewModel.Zoom, 4);
|
||||
RotateSize = ControlSize * 8;
|
||||
ControlOffset = new Thickness(ControlSize / 2 * -1, ControlSize / 2 * -1, 0, 0);
|
||||
RotateOffset = new Thickness(RotateSize / 2 * -1, RotateSize / 2 * -1, 0, 0);
|
||||
OutlineThickness = Math.Max(2 / ProfileViewModel.PanZoomViewModel.Zoom, 1);
|
||||
}
|
||||
|
||||
public void ShapeEditMouseDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
// The path starts at 0,0 so there's no simple way to get the position relative to the top-left of the path
|
||||
_dragStart = GetRelativePosition(sender, e).ToSKPoint();
|
||||
_dragStartScale = layer.SizeProperty.CurrentValue;
|
||||
_previousDragAngle = CalculateAngle(_layerEditorService.GetLayerAnchor(layer, true), _dragStart);
|
||||
|
||||
// Store the original position and do a test to figure out the mouse offset
|
||||
var originalPosition = layer.PositionProperty.CurrentValue;
|
||||
var scaledDragStart = _layerEditorService.GetScaledPoint(layer, _dragStart, true);
|
||||
layer.PositionProperty.SetCurrentValue(scaledDragStart, ProfileEditorService.CurrentTime);
|
||||
|
||||
// TopLeft is not updated yet and acts as a snapshot of the top-left before changing the position
|
||||
// GetLayerPath will return the updated position with all transformations applied, the difference is the offset
|
||||
_dragOffset = TopLeft - _layerEditorService.GetLayerPath(layer, true, true, true).Points[0];
|
||||
_dragStart += _dragOffset;
|
||||
|
||||
// Restore the position back to before the test was done
|
||||
layer.PositionProperty.SetCurrentValue(originalPosition, ProfileEditorService.CurrentTime);
|
||||
|
||||
_isDragging = true;
|
||||
((IInputElement) sender).CaptureMouse();
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
public void ShapeEditMouseUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
ProfileEditorService.UpdateSelectedProfileElement();
|
||||
|
||||
_dragOffset = SKPoint.Empty;
|
||||
_dragStartAnchor = SKPoint.Empty;
|
||||
|
||||
_isDragging = false;
|
||||
_draggingHorizontally = false;
|
||||
_draggingVertically = false;
|
||||
|
||||
((IInputElement) sender).ReleaseMouseCapture();
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
#region Position
|
||||
|
||||
public void Move(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
var position = GetRelativePosition(sender, e).ToSKPoint() + _dragOffset;
|
||||
// Allow the user to move the shape only horizontally or vertically when holding down shift
|
||||
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
|
||||
{
|
||||
// Keep the X position static if dragging vertically
|
||||
if (_draggingVertically)
|
||||
position.X = _dragStart.X;
|
||||
// Keep the Y position static if dragging horizontally
|
||||
else if (_draggingHorizontally)
|
||||
position.Y = _dragStart.Y;
|
||||
// Snap into place only if the mouse moved atleast a full pixel
|
||||
else if (Math.Abs(position.X - _dragStart.X) > 1 || Math.Abs(position.Y - _dragStart.Y) > 1)
|
||||
{
|
||||
// Pick between X and Y by comparing which moved the furthers from the starting point
|
||||
_draggingHorizontally = Math.Abs(position.X - _dragStart.X) > Math.Abs(position.Y - _dragStart.Y);
|
||||
_draggingVertically = Math.Abs(position.X - _dragStart.X) < Math.Abs(position.Y - _dragStart.Y);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Reset both states when shift is not held down
|
||||
else
|
||||
{
|
||||
_draggingVertically = false;
|
||||
_draggingHorizontally = false;
|
||||
}
|
||||
|
||||
// Scale down the resulting position and make it relative
|
||||
var scaled = _layerEditorService.GetScaledPoint(layer, position, true);
|
||||
// Update the position property
|
||||
layer.PositionProperty.SetCurrentValue(scaled, ProfileEditorService.CurrentTime);
|
||||
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Anchor
|
||||
|
||||
public void AnchorEditMouseDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (_isDragging)
|
||||
return;
|
||||
|
||||
if (ProfileEditorService.SelectedProfileElement is Layer layer)
|
||||
{
|
||||
var dragStartPosition = GetRelativePosition(sender, e).ToSKPoint();
|
||||
|
||||
// Mouse doesn't care about rotation so get the layer path without rotation
|
||||
var path = _layerEditorService.GetLayerPath(layer, true, true, false);
|
||||
var topLeft = path.Points[0];
|
||||
// Measure from the top-left of the shape (without rotation)
|
||||
_dragOffset = topLeft + (dragStartPosition - topLeft);
|
||||
// Get the absolute layer anchor and make it relative to the unrotated shape
|
||||
_dragStartAnchor = _layerEditorService.GetLayerAnchor(layer, true) - topLeft;
|
||||
// Ensure the anchor starts in the center of the shape it is now relative to
|
||||
_dragStartAnchor.X -= path.Bounds.Width / 2f;
|
||||
_dragStartAnchor.Y -= path.Bounds.Height / 2f;
|
||||
}
|
||||
|
||||
_isDragging = true;
|
||||
((IInputElement) sender).CaptureMouse();
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
public void AnchorMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
// The start anchor is relative to an unrotated version of the shape
|
||||
var start = _dragStartAnchor;
|
||||
// Add the current position to the start anchor to determine the new position
|
||||
var current = start + (GetRelativePosition(sender, e).ToSKPoint() - _dragOffset);
|
||||
// In order to keep the mouse movement unrotated, counter-act the active rotation
|
||||
var countered = UnTransformPoints(new[] {start, current}, layer, start, true);
|
||||
var scaled = _layerEditorService.GetScaledPoint(layer, countered[1], false);
|
||||
|
||||
// Update the anchor point, this causes the shape to move
|
||||
layer.AnchorPointProperty.SetCurrentValue(RoundPoint(scaled, 5), ProfileEditorService.CurrentTime);
|
||||
// TopLeft is not updated yet and acts as a snapshot of the top-left before changing the anchor
|
||||
var path = _layerEditorService.GetLayerPath(layer, true, true, true);
|
||||
// Calculate the (scaled) difference between the old and now position
|
||||
var difference = _layerEditorService.GetScaledPoint(layer, TopLeft - path.Points[0], false);
|
||||
// Apply the difference so that the shape effectively stays in place
|
||||
layer.PositionProperty.SetCurrentValue(RoundPoint(layer.PositionProperty.CurrentValue + difference, 5), ProfileEditorService.CurrentTime);
|
||||
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
private SKPoint RoundPoint(SKPoint point, int decimals)
|
||||
{
|
||||
return new SKPoint((float) Math.Round(point.X, decimals, MidpointRounding.AwayFromZero), (float) Math.Round(point.Y, decimals, MidpointRounding.AwayFromZero));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Size
|
||||
|
||||
public void TopLeftResize(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
var position = GetRelativePosition(sender, e).ToSKPoint() + _dragOffset;
|
||||
var width = HorizontalResize(layer, position, ResizeOrigin.Left);
|
||||
var height = VerticalResize(layer, position, ResizeOrigin.Top);
|
||||
layer.SizeProperty.SetCurrentValue(new SKSize(width, height), ProfileEditorService.CurrentTime);
|
||||
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
public void TopCenterResize(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
var position = GetRelativePosition(sender, e).ToSKPoint() + _dragOffset;
|
||||
var width = layer.SizeProperty.CurrentValue.Width;
|
||||
var height = VerticalResize(layer, position, ResizeOrigin.Top);
|
||||
layer.SizeProperty.SetCurrentValue(new SKSize(width, height), ProfileEditorService.CurrentTime);
|
||||
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
public void TopRightResize(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
var position = GetRelativePosition(sender, e).ToSKPoint() + _dragOffset;
|
||||
var width = HorizontalResize(layer, position, ResizeOrigin.Right);
|
||||
var height = VerticalResize(layer, position, ResizeOrigin.Top);
|
||||
layer.SizeProperty.SetCurrentValue(new SKSize(width, height), ProfileEditorService.CurrentTime);
|
||||
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
public void RightCenterResize(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
var position = GetRelativePosition(sender, e).ToSKPoint() + _dragOffset;
|
||||
var width = HorizontalResize(layer, position, ResizeOrigin.Right);
|
||||
var height = layer.SizeProperty.CurrentValue.Height;
|
||||
layer.SizeProperty.SetCurrentValue(new SKSize(width, height), ProfileEditorService.CurrentTime);
|
||||
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
public void BottomRightResize(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
var position = GetRelativePosition(sender, e).ToSKPoint() + _dragOffset;
|
||||
var width = HorizontalResize(layer, position, ResizeOrigin.Right);
|
||||
var height = VerticalResize(layer, position, ResizeOrigin.Bottom);
|
||||
layer.SizeProperty.SetCurrentValue(new SKSize(width, height), ProfileEditorService.CurrentTime);
|
||||
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
public void BottomCenterResize(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
var position = GetRelativePosition(sender, e).ToSKPoint() + _dragOffset;
|
||||
var width = layer.SizeProperty.CurrentValue.Width;
|
||||
var height = VerticalResize(layer, position, ResizeOrigin.Bottom);
|
||||
layer.SizeProperty.SetCurrentValue(new SKSize(width, height), ProfileEditorService.CurrentTime);
|
||||
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
public void BottomLeftResize(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
var position = GetRelativePosition(sender, e).ToSKPoint() + _dragOffset;
|
||||
var width = HorizontalResize(layer, position, ResizeOrigin.Left);
|
||||
var height = VerticalResize(layer, position, ResizeOrigin.Bottom);
|
||||
layer.SizeProperty.SetCurrentValue(new SKSize(width, height), ProfileEditorService.CurrentTime);
|
||||
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
public void LeftCenterResize(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
var position = GetRelativePosition(sender, e).ToSKPoint() + _dragOffset;
|
||||
var width = HorizontalResize(layer, position, ResizeOrigin.Left);
|
||||
var height = layer.SizeProperty.CurrentValue.Height;
|
||||
layer.SizeProperty.SetCurrentValue(new SKSize(width, height), ProfileEditorService.CurrentTime);
|
||||
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rotation
|
||||
|
||||
public void Rotate(object sender, MouseEventArgs e)
|
||||
private bool _rotating;
|
||||
private float _previousDragAngle;
|
||||
|
||||
public void RotateMouseDown(object sender, ShapeControlEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
_rotating = true;
|
||||
if (ProfileEditorService.SelectedProfileElement is Layer layer)
|
||||
_previousDragAngle = CalculateAngle(_layerEditorService.GetLayerAnchor(layer, true), GetRelativePosition(sender, e.MouseEventArgs).ToSKPoint());
|
||||
else
|
||||
_previousDragAngle = 0;
|
||||
}
|
||||
|
||||
public void RotateMouseUp(object sender, ShapeControlEventArgs e)
|
||||
{
|
||||
ProfileEditorService.UpdateSelectedProfileElement();
|
||||
_rotating = false;
|
||||
}
|
||||
|
||||
public void RotateMouseMove(object sender, ShapeControlEventArgs e)
|
||||
{
|
||||
if (!_rotating || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
var previousDragAngle = _previousDragAngle;
|
||||
var newRotation = CalculateAngle(_layerEditorService.GetLayerAnchor(layer, true), GetRelativePosition(sender, e).ToSKPoint());
|
||||
var newRotation = CalculateAngle(_layerEditorService.GetLayerAnchor(layer, true), GetRelativePosition(sender, e.MouseEventArgs).ToSKPoint());
|
||||
_previousDragAngle = newRotation;
|
||||
|
||||
// Allow the user to rotate the shape in increments of 5
|
||||
@@ -396,58 +104,92 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
|
||||
else
|
||||
newRotation = (float) Math.Round(newRotation, 2, MidpointRounding.AwayFromZero);
|
||||
|
||||
Debug.WriteLine(newRotation);
|
||||
layer.RotationProperty.SetCurrentValue(newRotation, ProfileEditorService.CurrentTime);
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
public void TopRightRotate(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
}
|
||||
|
||||
public void BottomRightRotate(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
}
|
||||
|
||||
public void BottomLeftRotate(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_isDragging || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private methods
|
||||
#region Size
|
||||
|
||||
private SKPoint[] TransformPoints(SKPoint[] skPoints, Layer layer, SKPoint pivot)
|
||||
private bool _isResizing;
|
||||
|
||||
public void ResizeMouseDown(object sender, ShapeControlEventArgs e)
|
||||
{
|
||||
var counterRotatePath = new SKPath();
|
||||
counterRotatePath.AddPoly(skPoints, false);
|
||||
counterRotatePath.Transform(SKMatrix.MakeRotationDegrees(layer.RotationProperty.CurrentValue, pivot.X, pivot.Y));
|
||||
// counterRotatePath.Transform(SKMatrix.MakeScale(layer.SizeProperty.CurrentValue.Width, layer.SizeProperty.CurrentValue.Height));
|
||||
if (_isResizing || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
return counterRotatePath.Points;
|
||||
// The path starts at 0,0 so there's no simple way to get the position relative to the top-left of the path
|
||||
_dragStart = GetRelativePosition(sender, e.MouseEventArgs).ToSKPoint();
|
||||
_dragStartScale = layer.SizeProperty.CurrentValue;
|
||||
|
||||
// Store the original position and do a test to figure out the mouse offset
|
||||
var originalPosition = layer.PositionProperty.CurrentValue;
|
||||
var scaledDragStart = _layerEditorService.GetScaledPoint(layer, _dragStart, true);
|
||||
layer.PositionProperty.SetCurrentValue(scaledDragStart, ProfileEditorService.CurrentTime);
|
||||
|
||||
// TopLeft is not updated yet and acts as a snapshot of the top-left before changing the position
|
||||
// GetLayerPath will return the updated position with all transformations applied, the difference is the offset
|
||||
_dragOffset = _topLeft - _layerEditorService.GetLayerPath(layer, true, true, true).Points[0];
|
||||
_dragStart += _dragOffset;
|
||||
|
||||
// Restore the position back to before the test was done
|
||||
layer.PositionProperty.SetCurrentValue(originalPosition, ProfileEditorService.CurrentTime);
|
||||
_isResizing = true;
|
||||
}
|
||||
|
||||
private SKPoint[] UnTransformPoints(SKPoint[] skPoints, Layer layer, SKPoint pivot, bool includeScale)
|
||||
public void ResizeMouseUp(object sender, ShapeControlEventArgs e)
|
||||
{
|
||||
var counterRotatePath = new SKPath();
|
||||
counterRotatePath.AddPoly(skPoints, false);
|
||||
counterRotatePath.Transform(SKMatrix.MakeRotationDegrees(layer.RotationProperty.CurrentValue * -1, pivot.X, pivot.Y));
|
||||
if (includeScale)
|
||||
counterRotatePath.Transform(SKMatrix.MakeScale(1f / layer.SizeProperty.CurrentValue.Width, 1f / layer.SizeProperty.CurrentValue.Height));
|
||||
|
||||
return counterRotatePath.Points;
|
||||
_isResizing = false;
|
||||
}
|
||||
|
||||
private Point GetRelativePosition(object sender, MouseEventArgs mouseEventArgs)
|
||||
public void ResizeMouseMove(object sender, ShapeControlEventArgs e)
|
||||
{
|
||||
var parent = VisualTreeHelper.GetParent((DependencyObject) sender);
|
||||
return mouseEventArgs.GetPosition((IInputElement) parent);
|
||||
if (!_isResizing || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
float width, height;
|
||||
var position = GetRelativePosition(sender, e.MouseEventArgs).ToSKPoint() + _dragOffset;
|
||||
switch (e.ShapeControlPoint)
|
||||
{
|
||||
case ShapeControlPoint.TopLeft:
|
||||
height = VerticalResize(layer, position, ResizeOrigin.Top);
|
||||
width = HorizontalResize(layer, position, ResizeOrigin.Left);
|
||||
break;
|
||||
case ShapeControlPoint.TopRight:
|
||||
height = VerticalResize(layer, position, ResizeOrigin.Top);
|
||||
width = HorizontalResize(layer, position, ResizeOrigin.Right);
|
||||
break;
|
||||
case ShapeControlPoint.BottomRight:
|
||||
height = VerticalResize(layer, position, ResizeOrigin.Bottom);
|
||||
width = HorizontalResize(layer, position, ResizeOrigin.Right);
|
||||
break;
|
||||
case ShapeControlPoint.BottomLeft:
|
||||
height = VerticalResize(layer, position, ResizeOrigin.Bottom);
|
||||
width = HorizontalResize(layer, position, ResizeOrigin.Left);
|
||||
break;
|
||||
case ShapeControlPoint.TopCenter:
|
||||
height = VerticalResize(layer, position, ResizeOrigin.Top);
|
||||
width = layer.SizeProperty.CurrentValue.Width;
|
||||
break;
|
||||
case ShapeControlPoint.RightCenter:
|
||||
width = HorizontalResize(layer, position, ResizeOrigin.Right);
|
||||
height = layer.SizeProperty.CurrentValue.Height;
|
||||
break;
|
||||
case ShapeControlPoint.BottomCenter:
|
||||
width = layer.SizeProperty.CurrentValue.Width;
|
||||
height = VerticalResize(layer, position, ResizeOrigin.Bottom);
|
||||
break;
|
||||
case ShapeControlPoint.LeftCenter:
|
||||
width = HorizontalResize(layer, position, ResizeOrigin.Left);
|
||||
height = layer.SizeProperty.CurrentValue.Height;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
layer.SizeProperty.SetCurrentValue(new SKSize(width, height), ProfileEditorService.CurrentTime);
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
private float HorizontalResize(Layer layer, SKPoint position, ResizeOrigin origin)
|
||||
@@ -490,6 +232,164 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
|
||||
return Math.Max(0.001f, _dragStartScale.Height + scaleToAdd);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Position
|
||||
|
||||
private bool _movingShape;
|
||||
private bool _movingAnchor;
|
||||
private bool _draggingHorizontally;
|
||||
private bool _draggingVertically;
|
||||
private SKPoint _dragStartAnchor;
|
||||
|
||||
public void MoveMouseDown(object sender, ShapeControlEventArgs e)
|
||||
{
|
||||
if (!(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
if (e.ShapeControlPoint == ShapeControlPoint.LayerShape)
|
||||
{
|
||||
// The path starts at 0,0 so there's no simple way to get the position relative to the top-left of the path
|
||||
_dragStart = GetRelativePosition(sender, e.MouseEventArgs).ToSKPoint();
|
||||
_dragStartScale = layer.SizeProperty.CurrentValue;
|
||||
|
||||
// Store the original position and do a test to figure out the mouse offset
|
||||
var originalPosition = layer.PositionProperty.CurrentValue;
|
||||
var scaledDragStart = _layerEditorService.GetScaledPoint(layer, _dragStart, true);
|
||||
layer.PositionProperty.SetCurrentValue(scaledDragStart, ProfileEditorService.CurrentTime);
|
||||
|
||||
// TopLeft is not updated yet and acts as a snapshot of the top-left before changing the position
|
||||
// GetLayerPath will return the updated position with all transformations applied, the difference is the offset
|
||||
_dragOffset = _topLeft - _layerEditorService.GetLayerPath(layer, true, true, true).Points[0];
|
||||
_dragStart += _dragOffset;
|
||||
|
||||
// Restore the position back to before the test was done
|
||||
layer.PositionProperty.SetCurrentValue(originalPosition, ProfileEditorService.CurrentTime);
|
||||
|
||||
_movingShape = true;
|
||||
}
|
||||
else if (e.ShapeControlPoint == ShapeControlPoint.Anchor)
|
||||
{
|
||||
var dragStartPosition = GetRelativePosition(sender, e.MouseEventArgs).ToSKPoint();
|
||||
|
||||
// Mouse doesn't care about rotation so get the layer path without rotation
|
||||
var path = _layerEditorService.GetLayerPath(layer, true, true, false);
|
||||
var topLeft = path.Points[0];
|
||||
// Measure from the top-left of the shape (without rotation)
|
||||
_dragOffset = topLeft + (dragStartPosition - topLeft);
|
||||
// Get the absolute layer anchor and make it relative to the unrotated shape
|
||||
_dragStartAnchor = _layerEditorService.GetLayerAnchor(layer, true) - topLeft;
|
||||
// Ensure the anchor starts in the center of the shape it is now relative to
|
||||
_dragStartAnchor.X -= path.Bounds.Width / 2f;
|
||||
_dragStartAnchor.Y -= path.Bounds.Height / 2f;
|
||||
_movingAnchor = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveMouseUp(object sender, ShapeControlEventArgs e)
|
||||
{
|
||||
_movingShape = false;
|
||||
_movingAnchor = false;
|
||||
}
|
||||
|
||||
public void MoveMouseMove(object sender, ShapeControlEventArgs e)
|
||||
{
|
||||
if (_movingShape)
|
||||
MoveShape(sender, e.MouseEventArgs);
|
||||
else if (_movingAnchor)
|
||||
MoveAnchor(sender, e.MouseEventArgs);
|
||||
}
|
||||
|
||||
public void MoveShape(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
var position = GetRelativePosition(sender, e).ToSKPoint() + _dragOffset;
|
||||
// Allow the user to move the shape only horizontally or vertically when holding down shift
|
||||
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
|
||||
{
|
||||
// Keep the X position static if dragging vertically
|
||||
if (_draggingVertically)
|
||||
position.X = _dragStart.X;
|
||||
// Keep the Y position static if dragging horizontally
|
||||
else if (_draggingHorizontally)
|
||||
position.Y = _dragStart.Y;
|
||||
// Snap into place only if the mouse moved atleast a full pixel
|
||||
else if (Math.Abs(position.X - _dragStart.X) > 1 || Math.Abs(position.Y - _dragStart.Y) > 1)
|
||||
{
|
||||
// Pick between X and Y by comparing which moved the furthers from the starting point
|
||||
_draggingHorizontally = Math.Abs(position.X - _dragStart.X) > Math.Abs(position.Y - _dragStart.Y);
|
||||
_draggingVertically = Math.Abs(position.X - _dragStart.X) < Math.Abs(position.Y - _dragStart.Y);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Reset both states when shift is not held down
|
||||
else
|
||||
{
|
||||
_draggingVertically = false;
|
||||
_draggingHorizontally = false;
|
||||
}
|
||||
|
||||
// Scale down the resulting position and make it relative
|
||||
var scaled = _layerEditorService.GetScaledPoint(layer, position, true);
|
||||
// Update the position property
|
||||
layer.PositionProperty.SetCurrentValue(scaled, ProfileEditorService.CurrentTime);
|
||||
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
public void MoveAnchor(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (!_movingAnchor || !(ProfileEditorService.SelectedProfileElement is Layer layer))
|
||||
return;
|
||||
|
||||
// The start anchor is relative to an unrotated version of the shape
|
||||
var start = _dragStartAnchor;
|
||||
// Add the current position to the start anchor to determine the new position
|
||||
var current = start + (GetRelativePosition(sender, e).ToSKPoint() - _dragOffset);
|
||||
// In order to keep the mouse movement unrotated, counter-act the active rotation
|
||||
var countered = UnTransformPoints(new[] {start, current}, layer, start, true);
|
||||
var scaled = _layerEditorService.GetScaledPoint(layer, countered[1], false);
|
||||
|
||||
// Update the anchor point, this causes the shape to move
|
||||
layer.AnchorPointProperty.SetCurrentValue(RoundPoint(scaled, 5), ProfileEditorService.CurrentTime);
|
||||
// TopLeft is not updated yet and acts as a snapshot of the top-left before changing the anchor
|
||||
var path = _layerEditorService.GetLayerPath(layer, true, true, true);
|
||||
// Calculate the (scaled) difference between the old and now position
|
||||
var difference = _layerEditorService.GetScaledPoint(layer, _topLeft - path.Points[0], false);
|
||||
// Apply the difference so that the shape effectively stays in place
|
||||
layer.PositionProperty.SetCurrentValue(RoundPoint(layer.PositionProperty.CurrentValue + difference, 5), ProfileEditorService.CurrentTime);
|
||||
|
||||
ProfileEditorService.UpdateProfilePreview();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private methods
|
||||
|
||||
private SKPoint RoundPoint(SKPoint point, int decimals)
|
||||
{
|
||||
return new SKPoint((float) Math.Round(point.X, decimals, MidpointRounding.AwayFromZero), (float) Math.Round(point.Y, decimals, MidpointRounding.AwayFromZero));
|
||||
}
|
||||
|
||||
private SKPoint[] UnTransformPoints(SKPoint[] skPoints, Layer layer, SKPoint pivot, bool includeScale)
|
||||
{
|
||||
var counterRotatePath = new SKPath();
|
||||
counterRotatePath.AddPoly(skPoints, false);
|
||||
counterRotatePath.Transform(SKMatrix.MakeRotationDegrees(layer.RotationProperty.CurrentValue * -1, pivot.X, pivot.Y));
|
||||
if (includeScale)
|
||||
counterRotatePath.Transform(SKMatrix.MakeScale(1f / layer.SizeProperty.CurrentValue.Width, 1f / layer.SizeProperty.CurrentValue.Height));
|
||||
|
||||
return counterRotatePath.Points;
|
||||
}
|
||||
|
||||
private Point GetRelativePosition(object sender, MouseEventArgs mouseEventArgs)
|
||||
{
|
||||
var parent = VisualTreeHelper.GetParent((DependencyObject) sender);
|
||||
return mouseEventArgs.GetPosition((IInputElement) parent);
|
||||
}
|
||||
|
||||
private float CalculateAngle(SKPoint start, SKPoint arrival)
|
||||
{
|
||||
var radian = (float) Math.Atan2(start.Y - arrival.Y, start.X - arrival.X);
|
||||
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using Artemis.Core.Models.Profile;
|
||||
|
||||
+11
-2
@@ -1,17 +1,23 @@
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using Artemis.Core.Models.Profile.LayerShapes;
|
||||
using Artemis.UI.Properties;
|
||||
using Artemis.UI.Services;
|
||||
using Artemis.UI.Services.Interfaces;
|
||||
|
||||
namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
|
||||
{
|
||||
public class FillToolViewModel : VisualizationToolViewModel
|
||||
{
|
||||
public FillToolViewModel(ProfileViewModel profileViewModel, IProfileEditorService profileEditorService) : base(profileViewModel, profileEditorService)
|
||||
private readonly ILayerEditorService _layerEditorService;
|
||||
|
||||
public FillToolViewModel(ProfileViewModel profileViewModel, IProfileEditorService profileEditorService, ILayerEditorService layerEditorService) : base(profileViewModel, profileEditorService)
|
||||
{
|
||||
_layerEditorService = layerEditorService;
|
||||
using (var stream = new MemoryStream(Resources.aero_fill))
|
||||
{
|
||||
Cursor = new Cursor(stream);
|
||||
@@ -34,6 +40,9 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
|
||||
if (layer == null)
|
||||
return;
|
||||
layer.LayerShape = new Fill(layer);
|
||||
|
||||
// Apply the full layer rectangle
|
||||
_layerEditorService.SetShapeRenderRect(layer.LayerShape, _layerEditorService.GetLayerRect(layer));
|
||||
ProfileEditorService.UpdateSelectedProfileElement();
|
||||
}
|
||||
}
|
||||
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
<UserControl x:Class="Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools.SelectionAddToolView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800">
|
||||
<Grid />
|
||||
</UserControl>
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for SelectionAddToolView.xaml
|
||||
/// </summary>
|
||||
public partial class SelectionAddToolView : UserControl
|
||||
{
|
||||
public SelectionAddToolView()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
-36
@@ -1,36 +0,0 @@
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using Artemis.UI.Properties;
|
||||
using Artemis.UI.Services.Interfaces;
|
||||
|
||||
namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
|
||||
{
|
||||
public class SelectionAddToolViewModel : VisualizationToolViewModel
|
||||
{
|
||||
public SelectionAddToolViewModel(ProfileViewModel profileViewModel, IProfileEditorService profileEditorService) : base(profileViewModel, profileEditorService)
|
||||
{
|
||||
using (var stream = new MemoryStream(Resources.aero_pen_plus))
|
||||
{
|
||||
Cursor = new Cursor(stream);
|
||||
}
|
||||
}
|
||||
|
||||
public override void MouseUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
base.MouseUp(sender, e);
|
||||
|
||||
var position = e.GetPosition((IInputElement) sender);
|
||||
var selectedRect = new Rect(MouseDownStartPosition, position);
|
||||
|
||||
foreach (var device in ProfileViewModel.DeviceViewModels)
|
||||
{
|
||||
foreach (var ledViewModel in device.Leds)
|
||||
{
|
||||
if (ProfileViewModel.PanZoomViewModel.TransformContainingRect(ledViewModel.Led.RgbLed.AbsoluteLedRectangle).IntersectsWith(selectedRect))
|
||||
ledViewModel.IsSelected = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
-2
@@ -3,8 +3,43 @@
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800">
|
||||
<Grid />
|
||||
<UserControl.Resources>
|
||||
<Style TargetType="Shape" x:Key="ShowIfMouseDown">
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding IsMouseDown}" Value="True">
|
||||
<DataTrigger.EnterActions>
|
||||
<BeginStoryboard>
|
||||
<Storyboard TargetProperty="Opacity">
|
||||
<DoubleAnimation From="0" To="1" Duration="0:0:0.1" />
|
||||
</Storyboard>
|
||||
</BeginStoryboard>
|
||||
</DataTrigger.EnterActions>
|
||||
<DataTrigger.ExitActions>
|
||||
<BeginStoryboard>
|
||||
<Storyboard TargetProperty="Opacity">
|
||||
<DoubleAnimation From="1" To="0" Duration="0:0:0.2" />
|
||||
</Storyboard>
|
||||
</BeginStoryboard>
|
||||
</DataTrigger.ExitActions>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</UserControl.Resources>
|
||||
<Canvas Background="Transparent" Width="50" Height="50">
|
||||
<Rectangle Style="{StaticResource ShowIfMouseDown}"
|
||||
Width="{Binding DragRectangle.Width}"
|
||||
Height="{Binding DragRectangle.Height}"
|
||||
Canvas.Left="{Binding DragRectangle.X}"
|
||||
Canvas.Top="{Binding DragRectangle.Y}"
|
||||
Stroke="{DynamicResource PrimaryHueLightBrush}"
|
||||
StrokeDashArray="4 4"
|
||||
StrokeThickness="1"
|
||||
Opacity="0">
|
||||
<Rectangle.Fill>
|
||||
<SolidColorBrush Color="{DynamicResource Primary400}" Opacity="0.25" />
|
||||
</Rectangle.Fill>
|
||||
</Rectangle>
|
||||
</Canvas>
|
||||
</UserControl>
|
||||
+65
-4
@@ -1,36 +1,97 @@
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using Artemis.Core.Models.Profile;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.UI.Extensions;
|
||||
using Artemis.UI.Properties;
|
||||
using Artemis.UI.Services;
|
||||
using Artemis.UI.Services.Interfaces;
|
||||
|
||||
namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
|
||||
{
|
||||
public class SelectionRemoveToolViewModel : VisualizationToolViewModel
|
||||
{
|
||||
public SelectionRemoveToolViewModel(ProfileViewModel profileViewModel, IProfileEditorService profileEditorService) : base(profileViewModel, profileEditorService)
|
||||
private readonly ILayerEditorService _layerEditorService;
|
||||
|
||||
public SelectionRemoveToolViewModel(ProfileViewModel profileViewModel, IProfileEditorService profileEditorService, ILayerEditorService layerEditorService) : base(profileViewModel,
|
||||
profileEditorService)
|
||||
{
|
||||
_layerEditorService = layerEditorService;
|
||||
using (var stream = new MemoryStream(Resources.aero_pen_min))
|
||||
{
|
||||
Cursor = new Cursor(stream);
|
||||
}
|
||||
}
|
||||
|
||||
public Rect DragRectangle { get; set; }
|
||||
|
||||
public override void MouseUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
base.MouseUp(sender, e);
|
||||
|
||||
var position = e.GetPosition((IInputElement) sender);
|
||||
var position = ProfileViewModel.PanZoomViewModel.GetRelativeMousePosition(sender, e);
|
||||
var selectedRect = new Rect(MouseDownStartPosition, position);
|
||||
|
||||
// Get selected LEDs
|
||||
var selectedLeds = new List<ArtemisLed>();
|
||||
foreach (var device in ProfileViewModel.DeviceViewModels)
|
||||
{
|
||||
foreach (var ledViewModel in device.Leds)
|
||||
{
|
||||
if (ledViewModel.Led.RgbLed.AbsoluteLedRectangle.ToWindowsRect(1).IntersectsWith(selectedRect))
|
||||
selectedLeds.Add(ledViewModel.Led);
|
||||
// Unselect everything
|
||||
ledViewModel.IsSelected = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the selection to the selected layer layer
|
||||
if (ProfileEditorService.SelectedProfileElement is Layer layer)
|
||||
{
|
||||
// If the layer has a shape, save it's size
|
||||
var shapeSize = Rect.Empty;
|
||||
if (layer.LayerShape != null)
|
||||
shapeSize = _layerEditorService.GetShapeRenderRect(layer.LayerShape);
|
||||
|
||||
var remainingLeds = layer.Leds.Except(selectedLeds).ToList();
|
||||
layer.ClearLeds();
|
||||
layer.AddLeds(remainingLeds);
|
||||
|
||||
// Restore the saved size
|
||||
if (layer.LayerShape != null)
|
||||
_layerEditorService.SetShapeRenderRect(layer.LayerShape, shapeSize);
|
||||
|
||||
ProfileEditorService.UpdateSelectedProfileElement();
|
||||
}
|
||||
}
|
||||
|
||||
public override void MouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
base.MouseMove(sender, e);
|
||||
if (!IsMouseDown)
|
||||
{
|
||||
DragRectangle = new Rect(-1, -1, 0, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
var position = ProfileViewModel.PanZoomViewModel.GetRelativeMousePosition(sender, e);
|
||||
var selectedRect = new Rect(MouseDownStartPosition, position);
|
||||
|
||||
foreach (var device in ProfileViewModel.DeviceViewModels)
|
||||
{
|
||||
foreach (var ledViewModel in device.Leds)
|
||||
{
|
||||
if (ProfileViewModel.PanZoomViewModel.TransformContainingRect(ledViewModel.Led.RgbLed.AbsoluteLedRectangle).IntersectsWith(selectedRect))
|
||||
if (ledViewModel.Led.RgbLed.AbsoluteLedRectangle.ToWindowsRect(1).IntersectsWith(selectedRect))
|
||||
ledViewModel.IsSelected = true;
|
||||
else if (!Keyboard.IsKeyDown(Key.LeftShift) && !Keyboard.IsKeyDown(Key.RightShift))
|
||||
ledViewModel.IsSelected = false;
|
||||
}
|
||||
}
|
||||
|
||||
DragRectangle = selectedRect;
|
||||
}
|
||||
}
|
||||
}
|
||||
-18
@@ -3,9 +3,6 @@
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools"
|
||||
xmlns:s="https://github.com/canton7/Stylet"
|
||||
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800">
|
||||
<UserControl.Resources>
|
||||
@@ -31,21 +28,6 @@
|
||||
</Style>
|
||||
</UserControl.Resources>
|
||||
<Canvas Background="Transparent" Width="50" Height="50">
|
||||
|
||||
<Canvas.ContextMenu>
|
||||
<ContextMenu>
|
||||
<MenuItem Header="Ellipse action 1" Command="{s:Action CreateLayer}" CommandParameter="{Binding}">
|
||||
<MenuItem.Icon>
|
||||
<materialDesign:PackIcon Kind="LayersPlus" />
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
<MenuItem Header="Ellipse action 2" Command="{s:Action ApplyToLayer}" CommandParameter="{Binding}">
|
||||
<MenuItem.Icon>
|
||||
<materialDesign:PackIcon Kind="Selection" />
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
</ContextMenu>
|
||||
</Canvas.ContextMenu>
|
||||
<Rectangle Style="{StaticResource ShowIfMouseDown}"
|
||||
Width="{Binding DragRectangle.Width}"
|
||||
Height="{Binding DragRectangle.Height}"
|
||||
|
||||
+12
-4
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using Artemis.Core.Models.Profile;
|
||||
@@ -27,8 +28,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
|
||||
}
|
||||
|
||||
public Rect DragRectangle { get; set; }
|
||||
|
||||
|
||||
|
||||
public override void MouseUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
base.MouseUp(sender, e);
|
||||
@@ -56,8 +56,16 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
|
||||
var shapeSize = Rect.Empty;
|
||||
if (layer.LayerShape != null)
|
||||
shapeSize = _layerEditorService.GetShapeRenderRect(layer.LayerShape);
|
||||
layer.ClearLeds();
|
||||
layer.AddLeds(selectedLeds);
|
||||
|
||||
// If shift is held down, add to the selection instead of replacing it
|
||||
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
|
||||
layer.AddLeds(selectedLeds.Except(layer.Leds));
|
||||
else
|
||||
{
|
||||
layer.ClearLeds();
|
||||
layer.AddLeds(selectedLeds);
|
||||
}
|
||||
|
||||
// Restore the saved size
|
||||
if (layer.LayerShape != null)
|
||||
_layerEditorService.SetShapeRenderRect(layer.LayerShape, shapeSize);
|
||||
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
<UserControl x:Class="Artemis.UI.Screens.Module.ProfileEditor.Visualization.UserControls.LayerShapeControl"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800">
|
||||
<Canvas UseLayoutRounding="False">
|
||||
<!-- Render these first so that they are always behind the actual shape -->
|
||||
<Ellipse x:Name="RotateTopLeft"
|
||||
Fill="Transparent"
|
||||
Cursor="/Resources/aero_rotate.cur"
|
||||
MouseDown="RotationOnMouseDown"
|
||||
MouseUp="RotationOnMouseUp"
|
||||
MouseMove="RotationOnMouseMove"/>
|
||||
<Ellipse x:Name="RotateTopRight"
|
||||
Fill="Transparent"
|
||||
Cursor="/Resources/aero_rotate.cur"
|
||||
MouseDown="RotationOnMouseDown"
|
||||
MouseUp="RotationOnMouseUp"
|
||||
MouseMove="RotationOnMouseMove"/>
|
||||
<Ellipse x:Name="RotateBottomRight"
|
||||
Fill="Transparent"
|
||||
Cursor="/Resources/aero_rotate.cur"
|
||||
MouseDown="RotationOnMouseDown"
|
||||
MouseUp="RotationOnMouseUp"
|
||||
MouseMove="RotationOnMouseMove"/>
|
||||
<Ellipse x:Name="RotateBottomLeft"
|
||||
Fill="Transparent"
|
||||
Cursor="/Resources/aero_rotate.cur"
|
||||
MouseDown="RotationOnMouseDown"
|
||||
MouseUp="RotationOnMouseUp"
|
||||
MouseMove="RotationOnMouseMove"/>
|
||||
<!-- The part of the layer's shape that is inside the layer -->
|
||||
<Path x:Name="LayerShapeOutline"
|
||||
Data="{Binding ShapeGeometry, Mode=OneWay}"
|
||||
Fill="Transparent"
|
||||
Stroke="{DynamicResource PrimaryHueMidBrush}"
|
||||
StrokeThickness="{Binding OutlineThickness}"
|
||||
StrokeDashArray="2 2"
|
||||
Cursor="/Resources/aero_drag.cur"
|
||||
MouseDown="MoveOnMouseDown"
|
||||
MouseUp="MoveOnMouseUp"
|
||||
MouseMove="MoveOnMouseMove" />
|
||||
|
||||
<!-- Mutation points -->
|
||||
<Rectangle x:Name="ResizeTopCenter"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
Cursor="Hand"
|
||||
MouseDown="ResizeOnMouseDown"
|
||||
MouseUp="ResizeOnMouseUp"
|
||||
MouseMove="ResizeOnMouseMove"/>
|
||||
<Rectangle x:Name="ResizeRightCenter"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
Cursor="Hand"
|
||||
MouseDown="ResizeOnMouseDown"
|
||||
MouseUp="ResizeOnMouseUp"
|
||||
MouseMove="ResizeOnMouseMove" />
|
||||
<Rectangle x:Name="ResizeBottomCenter"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
Cursor="Hand"
|
||||
MouseDown="ResizeOnMouseDown"
|
||||
MouseUp="ResizeOnMouseUp"
|
||||
MouseMove="ResizeOnMouseMove" />
|
||||
<Rectangle x:Name="ResizeLeftCenter"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
Cursor="Hand"
|
||||
MouseDown="ResizeOnMouseDown"
|
||||
MouseUp="ResizeOnMouseUp"
|
||||
MouseMove="ResizeOnMouseMove" />
|
||||
|
||||
<Rectangle x:Name="ResizeTopLeft"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
Cursor="Hand"
|
||||
MouseDown="ResizeOnMouseDown"
|
||||
MouseUp="ResizeOnMouseUp"
|
||||
MouseMove="ResizeOnMouseMove" />
|
||||
<Rectangle x:Name="ResizeTopRight"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
Cursor="Hand"
|
||||
MouseDown="ResizeOnMouseDown"
|
||||
MouseUp="ResizeOnMouseUp"
|
||||
MouseMove="ResizeOnMouseMove" />
|
||||
<Rectangle x:Name="ResizeBottomRight"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
Cursor="Hand"
|
||||
MouseDown="ResizeOnMouseDown"
|
||||
MouseUp="ResizeOnMouseUp"
|
||||
MouseMove="ResizeOnMouseMove" />
|
||||
<Rectangle x:Name="ResizeBottomLeft"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
Cursor="Hand"
|
||||
MouseDown="ResizeOnMouseDown"
|
||||
MouseUp="ResizeOnMouseUp"
|
||||
MouseMove="ResizeOnMouseMove" />
|
||||
|
||||
<!-- Anchor point -->
|
||||
<Ellipse x:Name="AnchorPoint"
|
||||
Fill="White"
|
||||
Stroke="{DynamicResource SecondaryAccentBrush}"
|
||||
StrokeThickness="0.5"
|
||||
Cursor="SizeAll"
|
||||
MouseDown="MoveOnMouseDown"
|
||||
MouseUp="MoveOnMouseUp"
|
||||
MouseMove="MoveOnMouseMove"/>
|
||||
</Canvas>
|
||||
</UserControl>
|
||||
+392
@@ -0,0 +1,392 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using Artemis.UI.Events;
|
||||
using Artemis.UI.Utilities;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.UserControls
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for LayerShapeControl.xaml
|
||||
/// </summary>
|
||||
public partial class LayerShapeControl : UserControl
|
||||
{
|
||||
public static readonly DependencyProperty ZoomProperty = DependencyProperty.Register(nameof(Zoom), typeof(double), typeof(LayerShapeControl),
|
||||
new FrameworkPropertyMetadata(default(double), FrameworkPropertyMetadataOptions.None, ZoomChanged));
|
||||
|
||||
public static readonly DependencyProperty ShapePathProperty = DependencyProperty.Register(nameof(ShapePath), typeof(SKPath), typeof(LayerShapeControl),
|
||||
new FrameworkPropertyMetadata(default(SKPath), FrameworkPropertyMetadataOptions.None, ShapePathChanged));
|
||||
|
||||
public static readonly DependencyProperty ShapeAnchorProperty = DependencyProperty.Register(nameof(ShapeAnchor), typeof(SKPoint), typeof(LayerShapeControl),
|
||||
new FrameworkPropertyMetadata(default(SKPoint), FrameworkPropertyMetadataOptions.None, ShapeAnchorChanged));
|
||||
|
||||
public static readonly DependencyProperty ShapeGeometryProperty = DependencyProperty.Register(nameof(ShapeGeometry), typeof(Geometry), typeof(LayerShapeControl),
|
||||
new FrameworkPropertyMetadata(default(Geometry), FrameworkPropertyMetadataOptions.None, ShapeGeometryChanged));
|
||||
|
||||
public LayerShapeControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
UpdatePositions();
|
||||
UpdateDimensions();
|
||||
}
|
||||
|
||||
public double Zoom
|
||||
{
|
||||
get => (double) GetValue(ZoomProperty);
|
||||
set => SetValue(ZoomProperty, value);
|
||||
}
|
||||
|
||||
public SKPath ShapePath
|
||||
{
|
||||
get => (SKPath) GetValue(ShapePathProperty);
|
||||
set => SetValue(ShapePathProperty, value);
|
||||
}
|
||||
|
||||
public SKPoint ShapeAnchor
|
||||
{
|
||||
get => (SKPoint) GetValue(ShapeAnchorProperty);
|
||||
set => SetValue(ShapeAnchorProperty, value);
|
||||
}
|
||||
|
||||
public Geometry ShapeGeometry
|
||||
{
|
||||
get => (Geometry) GetValue(ShapeGeometryProperty);
|
||||
set => SetValue(ShapeGeometryProperty, value);
|
||||
}
|
||||
|
||||
private static void ZoomChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var layerShapeControl = (LayerShapeControl) d;
|
||||
|
||||
layerShapeControl.SetCurrentValue(ZoomProperty, e.NewValue);
|
||||
layerShapeControl.UpdateDimensions();
|
||||
}
|
||||
|
||||
private static void ShapePathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var layerShapeControl = (LayerShapeControl) d;
|
||||
|
||||
layerShapeControl.SetCurrentValue(ShapePathProperty, e.NewValue);
|
||||
layerShapeControl.UpdatePositions();
|
||||
}
|
||||
|
||||
private static void ShapeAnchorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var layerShapeControl = (LayerShapeControl) d;
|
||||
|
||||
layerShapeControl.SetCurrentValue(ShapeAnchorProperty, e.NewValue);
|
||||
layerShapeControl.UpdateShapeAnchor();
|
||||
}
|
||||
|
||||
private static void ShapeGeometryChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var layerShapeControl = (LayerShapeControl) d;
|
||||
|
||||
layerShapeControl.SetCurrentValue(ShapeGeometryProperty, e.NewValue);
|
||||
layerShapeControl.UpdateShapeGeometry();
|
||||
}
|
||||
|
||||
public void UpdateDimensions()
|
||||
{
|
||||
if (Zoom == 0)
|
||||
return;
|
||||
|
||||
// Rotation controls
|
||||
UpdateRotateDimensions(RotateTopLeft);
|
||||
UpdateRotateDimensions(RotateTopRight);
|
||||
UpdateRotateDimensions(RotateBottomRight);
|
||||
UpdateRotateDimensions(RotateBottomLeft);
|
||||
|
||||
// Size controls
|
||||
UpdateResizeDimensions(ResizeTopCenter);
|
||||
UpdateResizeDimensions(ResizeRightCenter);
|
||||
UpdateResizeDimensions(ResizeBottomCenter);
|
||||
UpdateResizeDimensions(ResizeLeftCenter);
|
||||
UpdateResizeDimensions(ResizeTopLeft);
|
||||
UpdateResizeDimensions(ResizeTopRight);
|
||||
UpdateResizeDimensions(ResizeBottomRight);
|
||||
UpdateResizeDimensions(ResizeBottomLeft);
|
||||
|
||||
// Anchor point
|
||||
UpdateResizeDimensions(AnchorPoint);
|
||||
|
||||
// Layer outline
|
||||
LayerShapeOutline.StrokeThickness = Math.Max(2 / Zoom, 1);
|
||||
}
|
||||
|
||||
private void UpdateShapeGeometry()
|
||||
{
|
||||
LayerShapeOutline.Data = ShapeGeometry;
|
||||
}
|
||||
|
||||
private void UpdateShapeAnchor()
|
||||
{
|
||||
UpdateControlPosition(AnchorPoint, ShapeAnchor);
|
||||
}
|
||||
|
||||
private void UpdatePositions()
|
||||
{
|
||||
if (ShapePath == null)
|
||||
return;
|
||||
|
||||
// Rotation controls
|
||||
UpdateControlPosition(RotateTopLeft, ShapePath.Points[0]);
|
||||
UpdateControlPosition(RotateTopRight, ShapePath.Points[1]);
|
||||
UpdateControlPosition(RotateBottomRight, ShapePath.Points[2]);
|
||||
UpdateControlPosition(RotateBottomLeft, ShapePath.Points[3]);
|
||||
|
||||
// Size controls
|
||||
UpdateControlPosition(ResizeTopCenter, ShapePath.Points[0], ShapePath.Points[1]);
|
||||
UpdateControlPosition(ResizeRightCenter, ShapePath.Points[1], ShapePath.Points[2]);
|
||||
UpdateControlPosition(ResizeBottomCenter, ShapePath.Points[2], ShapePath.Points[3]);
|
||||
UpdateControlPosition(ResizeLeftCenter, ShapePath.Points[3], ShapePath.Points[0]);
|
||||
UpdateControlPosition(ResizeTopLeft, ShapePath.Points[0]);
|
||||
UpdateControlPosition(ResizeTopRight, ShapePath.Points[1]);
|
||||
UpdateControlPosition(ResizeBottomRight, ShapePath.Points[2]);
|
||||
UpdateControlPosition(ResizeBottomLeft, ShapePath.Points[3]);
|
||||
}
|
||||
|
||||
#region Helpers
|
||||
|
||||
private void UpdateControlPosition(UIElement control, SKPoint point)
|
||||
{
|
||||
if (float.IsInfinity(point.X) || float.IsInfinity(point.Y))
|
||||
return;
|
||||
Canvas.SetLeft(control, point.X);
|
||||
Canvas.SetTop(control, point.Y);
|
||||
}
|
||||
|
||||
private void UpdateControlPosition(UIElement control, SKPoint point1, SKPoint point2)
|
||||
{
|
||||
var point = new SKPoint((point1.X + point2.X) / 2, (point1.Y + point2.Y) / 2);
|
||||
UpdateControlPosition(control, point);
|
||||
}
|
||||
|
||||
private void UpdateRotateDimensions(FrameworkElement rotateControl)
|
||||
{
|
||||
var controlSize = Math.Max(10 / Zoom, 4);
|
||||
var rotateSize = controlSize * 8;
|
||||
|
||||
rotateControl.Width = rotateSize;
|
||||
rotateControl.Height = rotateSize;
|
||||
rotateControl.Margin = new Thickness(rotateSize / 2 * -1, rotateSize / 2 * -1, 0, 0);
|
||||
}
|
||||
|
||||
private void UpdateResizeDimensions(FrameworkElement resizeControl)
|
||||
{
|
||||
var controlSize = Math.Max(10 / Zoom, 4);
|
||||
|
||||
resizeControl.Width = controlSize;
|
||||
resizeControl.Height = controlSize;
|
||||
resizeControl.Margin = new Thickness(controlSize / 2 * -1, controlSize / 2 * -1, 0, 0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event handlers
|
||||
|
||||
private void RotationOnMouseDown(object sender, MouseEventArgs e)
|
||||
{
|
||||
((IInputElement) sender).CaptureMouse();
|
||||
e.Handled = true;
|
||||
|
||||
if (ReferenceEquals(sender, RotateTopLeft))
|
||||
OnRotateMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.TopLeft));
|
||||
else if (ReferenceEquals(sender, RotateTopRight))
|
||||
OnRotateMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.TopRight));
|
||||
else if (ReferenceEquals(sender, RotateBottomRight))
|
||||
OnRotateMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.BottomRight));
|
||||
else if (ReferenceEquals(sender, RotateBottomLeft))
|
||||
OnRotateMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.BottomLeft));
|
||||
}
|
||||
|
||||
private void RotationOnMouseUp(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (ReferenceEquals(sender, RotateTopLeft))
|
||||
OnRotateMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.TopLeft));
|
||||
else if (ReferenceEquals(sender, RotateTopRight))
|
||||
OnRotateMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.TopRight));
|
||||
else if (ReferenceEquals(sender, RotateBottomRight))
|
||||
OnRotateMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.BottomRight));
|
||||
else if (ReferenceEquals(sender, RotateBottomLeft))
|
||||
OnRotateMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.BottomLeft));
|
||||
|
||||
((IInputElement) sender).ReleaseMouseCapture();
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private void RotationOnMouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (ReferenceEquals(sender, RotateTopLeft))
|
||||
OnRotateMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.TopLeft));
|
||||
else if (ReferenceEquals(sender, RotateTopRight))
|
||||
OnRotateMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.TopRight));
|
||||
else if (ReferenceEquals(sender, RotateBottomRight))
|
||||
OnRotateMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.BottomRight));
|
||||
else if (ReferenceEquals(sender, RotateBottomLeft))
|
||||
OnRotateMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.BottomLeft));
|
||||
}
|
||||
|
||||
private void ResizeOnMouseDown(object sender, MouseEventArgs e)
|
||||
{
|
||||
((IInputElement) sender).CaptureMouse();
|
||||
e.Handled = true;
|
||||
|
||||
if (ReferenceEquals(sender, ResizeTopCenter))
|
||||
OnResizeMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.TopCenter));
|
||||
else if (ReferenceEquals(sender, ResizeRightCenter))
|
||||
OnResizeMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.RightCenter));
|
||||
else if (ReferenceEquals(sender, ResizeBottomCenter))
|
||||
OnResizeMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.BottomCenter));
|
||||
else if (ReferenceEquals(sender, ResizeLeftCenter))
|
||||
OnResizeMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.LeftCenter));
|
||||
else if (ReferenceEquals(sender, ResizeTopLeft))
|
||||
OnResizeMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.TopLeft));
|
||||
else if (ReferenceEquals(sender, ResizeTopRight))
|
||||
OnResizeMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.TopRight));
|
||||
else if (ReferenceEquals(sender, ResizeBottomRight))
|
||||
OnResizeMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.BottomRight));
|
||||
else if (ReferenceEquals(sender, ResizeBottomLeft))
|
||||
OnResizeMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.BottomLeft));
|
||||
}
|
||||
|
||||
private void ResizeOnMouseUp(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (ReferenceEquals(sender, ResizeTopCenter))
|
||||
OnResizeMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.TopCenter));
|
||||
else if (ReferenceEquals(sender, ResizeRightCenter))
|
||||
OnResizeMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.RightCenter));
|
||||
else if (ReferenceEquals(sender, ResizeBottomCenter))
|
||||
OnResizeMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.BottomCenter));
|
||||
else if (ReferenceEquals(sender, ResizeLeftCenter))
|
||||
OnResizeMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.LeftCenter));
|
||||
else if (ReferenceEquals(sender, ResizeTopLeft))
|
||||
OnResizeMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.TopLeft));
|
||||
else if (ReferenceEquals(sender, ResizeTopRight))
|
||||
OnResizeMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.TopRight));
|
||||
else if (ReferenceEquals(sender, ResizeBottomRight))
|
||||
OnResizeMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.BottomRight));
|
||||
else if (ReferenceEquals(sender, ResizeBottomLeft))
|
||||
OnResizeMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.BottomLeft));
|
||||
|
||||
((IInputElement) sender).ReleaseMouseCapture();
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private void ResizeOnMouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (ReferenceEquals(sender, ResizeTopCenter))
|
||||
OnResizeMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.TopCenter));
|
||||
else if (ReferenceEquals(sender, ResizeRightCenter))
|
||||
OnResizeMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.RightCenter));
|
||||
else if (ReferenceEquals(sender, ResizeBottomCenter))
|
||||
OnResizeMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.BottomCenter));
|
||||
else if (ReferenceEquals(sender, ResizeLeftCenter))
|
||||
OnResizeMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.LeftCenter));
|
||||
else if (ReferenceEquals(sender, ResizeTopLeft))
|
||||
OnResizeMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.TopLeft));
|
||||
else if (ReferenceEquals(sender, ResizeTopRight))
|
||||
OnResizeMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.TopRight));
|
||||
else if (ReferenceEquals(sender, ResizeBottomRight))
|
||||
OnResizeMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.BottomRight));
|
||||
else if (ReferenceEquals(sender, ResizeBottomLeft))
|
||||
OnResizeMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.BottomLeft));
|
||||
}
|
||||
|
||||
private void MoveOnMouseDown(object sender, MouseEventArgs e)
|
||||
{
|
||||
((IInputElement) sender).CaptureMouse();
|
||||
e.Handled = true;
|
||||
|
||||
if (ReferenceEquals(sender, LayerShapeOutline))
|
||||
OnMoveMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.LayerShape));
|
||||
else if (ReferenceEquals(sender, AnchorPoint))
|
||||
OnMoveMouseDown(new ShapeControlEventArgs(e, ShapeControlPoint.Anchor));
|
||||
}
|
||||
|
||||
private void MoveOnMouseUp(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (ReferenceEquals(sender, LayerShapeOutline))
|
||||
OnMoveMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.LayerShape));
|
||||
else if (ReferenceEquals(sender, AnchorPoint))
|
||||
OnMoveMouseUp(new ShapeControlEventArgs(e, ShapeControlPoint.Anchor));
|
||||
|
||||
((IInputElement) sender).ReleaseMouseCapture();
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private void MoveOnMouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (ReferenceEquals(sender, LayerShapeOutline))
|
||||
OnMoveMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.LayerShape));
|
||||
else if (ReferenceEquals(sender, AnchorPoint))
|
||||
OnMoveMouseMove(new ShapeControlEventArgs(e, ShapeControlPoint.Anchor));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
public event EventHandler<ShapeControlEventArgs> RotateMouseDown;
|
||||
public event EventHandler<ShapeControlEventArgs> RotateMouseUp;
|
||||
public event EventHandler<ShapeControlEventArgs> RotateMouseMove;
|
||||
|
||||
public event EventHandler<ShapeControlEventArgs> ResizeMouseDown;
|
||||
public event EventHandler<ShapeControlEventArgs> ResizeMouseUp;
|
||||
public event EventHandler<ShapeControlEventArgs> ResizeMouseMove;
|
||||
|
||||
public event EventHandler<ShapeControlEventArgs> MoveMouseDown;
|
||||
public event EventHandler<ShapeControlEventArgs> MoveMouseUp;
|
||||
public event EventHandler<ShapeControlEventArgs> MoveMouseMove;
|
||||
|
||||
protected virtual void OnMoveMouseMove(ShapeControlEventArgs e)
|
||||
{
|
||||
MoveMouseMove?.Invoke(this, e);
|
||||
}
|
||||
|
||||
protected virtual void OnMoveMouseUp(ShapeControlEventArgs e)
|
||||
{
|
||||
MoveMouseUp?.Invoke(this, e);
|
||||
}
|
||||
|
||||
protected virtual void OnMoveMouseDown(ShapeControlEventArgs e)
|
||||
{
|
||||
MoveMouseDown?.Invoke(this, e);
|
||||
}
|
||||
|
||||
protected virtual void OnResizeMouseMove(ShapeControlEventArgs e)
|
||||
{
|
||||
ResizeMouseMove?.Invoke(this, e);
|
||||
}
|
||||
|
||||
protected virtual void OnResizeMouseUp(ShapeControlEventArgs e)
|
||||
{
|
||||
ResizeMouseUp?.Invoke(this, e);
|
||||
}
|
||||
|
||||
protected virtual void OnResizeMouseDown(ShapeControlEventArgs e)
|
||||
{
|
||||
ResizeMouseDown?.Invoke(this, e);
|
||||
}
|
||||
|
||||
protected virtual void OnRotateMouseMove(ShapeControlEventArgs e)
|
||||
{
|
||||
RotateMouseMove?.Invoke(this, e);
|
||||
}
|
||||
|
||||
protected virtual void OnRotateMouseUp(ShapeControlEventArgs e)
|
||||
{
|
||||
RotateMouseUp?.Invoke(this, e);
|
||||
}
|
||||
|
||||
protected virtual void OnRotateMouseDown(ShapeControlEventArgs e)
|
||||
{
|
||||
RotateMouseDown?.Invoke(this, e);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user