1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-31 09:43:46 +00:00

Adjusted anchor calculations for simplified render process

Project cleanup
This commit is contained in:
Robert 2020-01-31 22:15:32 +01:00
parent f2df51d40c
commit 960584cc3c
25 changed files with 160 additions and 167 deletions

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using Artemis.Core.Exceptions; using Artemis.Core.Exceptions;
using Artemis.Core.Models.Profile.LayerProperties; using Artemis.Core.Models.Profile.LayerProperties;

View File

@ -1,9 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Net.Sockets;
using Artemis.Core.Exceptions; using Artemis.Core.Exceptions;
using Artemis.Core.Extensions; using Artemis.Core.Extensions;
using Artemis.Core.Models.Profile.LayerProperties; using Artemis.Core.Models.Profile.LayerProperties;
@ -22,7 +20,6 @@ namespace Artemis.Core.Models.Profile
private LayerShape _layerShape; private LayerShape _layerShape;
private List<ArtemisLed> _leds; private List<ArtemisLed> _leds;
private SKPath _path; private SKPath _path;
private SKRect _bounds;
public Layer(Profile profile, ProfileElement parent, string name) public Layer(Profile profile, ProfileElement parent, string name)
{ {
@ -95,14 +92,14 @@ namespace Artemis.Core.Models.Profile
_path = value; _path = value;
// I can't really be sure about the performance impact of calling Bounds often but // I can't really be sure about the performance impact of calling Bounds often but
// SkiaSharp calls SkiaApi.sk_path_get_bounds (Handle, &rect); which sounds expensive // SkiaSharp calls SkiaApi.sk_path_get_bounds (Handle, &rect); which sounds expensive
_bounds = value?.Bounds ?? SKRect.Empty; Bounds = value?.Bounds ?? SKRect.Empty;
} }
} }
/// <summary> /// <summary>
/// The bounds of this layer /// The bounds of this layer
/// </summary> /// </summary>
public SKRect Bounds => _bounds; public SKRect Bounds { get; private set; }
/// <summary> /// <summary>
/// Defines the shape that is rendered by the <see cref="LayerBrush" />. /// Defines the shape that is rendered by the <see cref="LayerBrush" />.
@ -175,19 +172,21 @@ namespace Artemis.Core.Models.Profile
canvas.ClipPath(Path); canvas.ClipPath(Path);
// Apply transformations // Apply transformations
var position = PositionProperty.CurrentValue; var sizeProperty = SizeProperty.CurrentValue;
var size = SizeProperty.CurrentValue; var rotationProperty = RotationProperty.CurrentValue;
var rotation = RotationProperty.CurrentValue;
var anchor = GetLayerAnchor(); var anchorPosition = GetLayerAnchorPosition() + Bounds.Location;
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 = position.X * Bounds.Width - LayerShape.Bounds.Width / 2 - anchor.X; var x = anchorPosition.X - LayerShape.Bounds.Width / 2 - anchorProperty.X * Bounds.Width;
var y = position.Y * Bounds.Height - LayerShape.Bounds.Height / 2 - anchor.Y; var y = anchorPosition.Y - LayerShape.Bounds.Height / 2 - anchorProperty.Y * Bounds.Height;
canvas.Translate(Bounds.Left + x, Bounds.Top + y); // Apply these before translation because anchorPosition takes translation into account
canvas.Scale(size.Width, size.Height, anchor.X, anchor.Y); canvas.RotateDegrees(rotationProperty, anchorPosition.X, anchorPosition.Y);
canvas.RotateDegrees(rotation, anchor.X, anchor.Y); canvas.Scale(sizeProperty.Width, sizeProperty.Height, anchorPosition.X, anchorPosition.Y);
// Once the other transformations are done it is save to translate
canvas.Translate(x, y);
// Placeholder // Placeholder
if (LayerShape?.Path != null) if (LayerShape?.Path != null)
@ -209,15 +208,18 @@ namespace Artemis.Core.Models.Profile
canvas.Restore(); canvas.Restore();
} }
private SKPoint GetLayerAnchor() private SKPoint GetLayerAnchorPosition()
{ {
if (LayerShape == null) var positionProperty = PositionProperty.CurrentValue;
return SKPoint.Empty;
var anchor = AnchorPointProperty.CurrentValue; // Start at the center of the shape
anchor.X = anchor.X * Bounds.Width; var position = new SKPoint(LayerShape.Bounds.Left, LayerShape.Bounds.Top);
anchor.Y = anchor.Y * Bounds.Height;
return new SKPoint(anchor.X, anchor.Y); // Apply translation
position.X += positionProperty.X * Bounds.Width;
position.Y += positionProperty.Y * Bounds.Height;
return position;
} }
internal override void ApplyToEntity() internal override void ApplyToEntity()

View File

@ -13,13 +13,13 @@ namespace Artemis.Core.Plugins.Abstract
{ {
} }
public Profile ActiveProfile { get; private set; }
public override void EnablePlugin() public override void EnablePlugin()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Profile ActiveProfile { get; private set; }
/// <inheritdoc /> /// <inheritdoc />
public override void Update(double deltaTime) public override void Update(double deltaTime)
{ {

View File

@ -21,9 +21,9 @@ namespace Artemis.Core.Services
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IPluginService _pluginService; private readonly IPluginService _pluginService;
private readonly IProfileService _profileService;
private readonly IRgbService _rgbService; private readonly IRgbService _rgbService;
private readonly ISurfaceService _surfaceService; private readonly ISurfaceService _surfaceService;
private readonly IProfileService _profileService;
private List<Module> _modules; private List<Module> _modules;
internal CoreService(ILogger logger, IPluginService pluginService, IRgbService rgbService, ISurfaceService surfaceService, IProfileService profileService) internal CoreService(ILogger logger, IPluginService pluginService, IRgbService rgbService, ISurfaceService surfaceService, IProfileService profileService)

View File

@ -11,11 +11,12 @@ namespace Artemis.Core.Services.Interfaces
bool IsInitialized { get; } bool IsInitialized { get; }
/// <summary> /// <summary>
/// Gets or sets whether modules are updated each frame by calling their Update method /// Gets or sets whether modules are updated each frame by calling their Update method
/// </summary> /// </summary>
bool ModuleUpdatingDisabled { get; set; } bool ModuleUpdatingDisabled { get; set; }
/// <summary> /// <summary>
/// Gets or sets whether modules are rendered each frame by calling their Render method /// Gets or sets whether modules are rendered each frame by calling their Render method
/// </summary> /// </summary>
bool ModuleRenderingDisabled { get; set; } bool ModuleRenderingDisabled { get; set; }

View File

@ -9,9 +9,9 @@ namespace Artemis.Plugins.LayerBrushes.Color
{ {
public class ColorBrush : LayerBrush public class ColorBrush : LayerBrush
{ {
private readonly List<SKColor> _testColors;
private SKPaint _paint; private SKPaint _paint;
private SKShader _shader; private SKShader _shader;
private readonly List<SKColor> _testColors;
public ColorBrush(Layer layer, ColorBrushSettings settings, LayerBrushDescriptor descriptor) : base(layer, settings, descriptor) public ColorBrush(Layer layer, ColorBrushSettings settings, LayerBrushDescriptor descriptor) : base(layer, settings, descriptor)
{ {

View File

@ -458,9 +458,9 @@ namespace Artemis.Plugins.LayerBrushes.Noise.Utilities
{ {
public readonly double dx; public readonly double dx;
public readonly double dy; public readonly double dy;
public Contribution2 Next;
public readonly int xsb; public readonly int xsb;
public readonly int ysb; public readonly int ysb;
public Contribution2 Next;
public Contribution2(double multiplier, int xsb, int ysb) public Contribution2(double multiplier, int xsb, int ysb)
{ {
@ -476,10 +476,10 @@ namespace Artemis.Plugins.LayerBrushes.Noise.Utilities
public readonly double dx; public readonly double dx;
public readonly double dy; public readonly double dy;
public readonly double dz; public readonly double dz;
public Contribution3 Next;
public readonly int xsb; public readonly int xsb;
public readonly int ysb; public readonly int ysb;
public readonly int zsb; public readonly int zsb;
public Contribution3 Next;
public Contribution3(double multiplier, int xsb, int ysb, int zsb) public Contribution3(double multiplier, int xsb, int ysb, int zsb)
{ {
@ -494,15 +494,15 @@ namespace Artemis.Plugins.LayerBrushes.Noise.Utilities
private class Contribution4 private class Contribution4
{ {
public readonly double dw;
public readonly double dx; public readonly double dx;
public readonly double dy; public readonly double dy;
public readonly double dz; public readonly double dz;
public readonly double dw; public readonly int wsb;
public Contribution4 Next;
public readonly int xsb; public readonly int xsb;
public readonly int ysb; public readonly int ysb;
public readonly int zsb; public readonly int zsb;
public readonly int wsb; public Contribution4 Next;
public Contribution4(double multiplier, int xsb, int ysb, int zsb, int wsb) public Contribution4(double multiplier, int xsb, int ysb, int zsb, int wsb)
{ {

View File

@ -226,6 +226,7 @@
<DependentUpon>SidebarView.xaml</DependentUpon> <DependentUpon>SidebarView.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Screens\Sidebar\SidebarViewModel.cs" /> <Compile Include="Screens\Sidebar\SidebarViewModel.cs" />
<Compile Include="Services\Interfaces\ILayerEditorService.cs" />
<Compile Include="Services\Interfaces\IProfileEditorService.cs" /> <Compile Include="Services\Interfaces\IProfileEditorService.cs" />
<Compile Include="Services\LayerShapeService.cs" /> <Compile Include="Services\LayerShapeService.cs" />
<Compile Include="Services\ProfileEditorService.cs" /> <Compile Include="Services\ProfileEditorService.cs" />

View File

@ -103,12 +103,9 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties
public void PlayFromStart() public void PlayFromStart()
{ {
if (!IsActive) if (!Playing)
return; _profileEditorService.CurrentTime = TimeSpan.Zero;
if (Playing)
Pause();
_profileEditorService.CurrentTime = TimeSpan.Zero;
Play(); Play();
} }

View File

@ -196,7 +196,6 @@ namespace Artemis.UI.Screens.Module.ProfileEditor
if (!activeProfile.IsActivated) if (!activeProfile.IsActivated)
_profileService.ActivateProfile(Module, activeProfile); _profileService.ActivateProfile(Module, activeProfile);
}); });
} }
} }
} }

View File

@ -5,12 +5,9 @@ using System.Windows.Media;
using Artemis.Core.Models.Profile; using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Profile.LayerShapes; using Artemis.Core.Models.Profile.LayerShapes;
using Artemis.Core.Models.Surface; using Artemis.Core.Models.Surface;
using Artemis.Core.Services;
using Artemis.UI.Extensions; using Artemis.UI.Extensions;
using Artemis.UI.Services;
using Artemis.UI.Services.Interfaces; using Artemis.UI.Services.Interfaces;
using RGB.NET.Core; using RGB.NET.Core;
using SkiaSharp.Views.WPF;
using Stylet; using Stylet;
using Rectangle = Artemis.Core.Models.Profile.LayerShapes.Rectangle; using Rectangle = Artemis.Core.Models.Profile.LayerShapes.Rectangle;
@ -18,8 +15,8 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization
{ {
public class ProfileLayerViewModel : CanvasViewModel public class ProfileLayerViewModel : CanvasViewModel
{ {
private readonly IProfileEditorService _profileEditorService;
private readonly ILayerEditorService _layerEditorService; private readonly ILayerEditorService _layerEditorService;
private readonly IProfileEditorService _profileEditorService;
public ProfileLayerViewModel(Layer layer, IProfileEditorService profileEditorService, ILayerEditorService layerEditorService) public ProfileLayerViewModel(Layer layer, IProfileEditorService profileEditorService, ILayerEditorService layerEditorService)
{ {
@ -145,13 +142,8 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization
return; return;
} }
var x = Layer.Leds.Min(l => l.RgbLed.AbsoluteLedRectangle.Location.X); var rect = _layerEditorService.GetLayerBounds(Layer);
var y = Layer.Leds.Min(l => l.RgbLed.AbsoluteLedRectangle.Location.Y); ViewportRectangle = new Rect(0, 0, rect.Width, rect.Height);
var width = Layer.Leds.Max(l => l.RgbLed.AbsoluteLedRectangle.Location.X + l.RgbLed.AbsoluteLedRectangle.Size.Width) - x;
var height = Layer.Leds.Max(l => l.RgbLed.AbsoluteLedRectangle.Location.Y + l.RgbLed.AbsoluteLedRectangle.Size.Height) - y;
var rect = new Rect(x, y, width, height);
ViewportRectangle = rect;
} }
private Geometry CreateRectangleGeometry(ArtemisLed led) private Geometry CreateRectangleGeometry(ArtemisLed led)

View File

@ -14,7 +14,6 @@ using Artemis.UI.Events;
using Artemis.UI.Ninject.Factories; using Artemis.UI.Ninject.Factories;
using Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools; using Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools;
using Artemis.UI.Screens.Shared; using Artemis.UI.Screens.Shared;
using Artemis.UI.Services;
using Artemis.UI.Services.Interfaces; using Artemis.UI.Services.Interfaces;
using RGB.NET.Core; using RGB.NET.Core;
using Stylet; using Stylet;
@ -23,8 +22,8 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization
{ {
public class ProfileViewModel : ProfileEditorPanelViewModel, IHandle<MainWindowFocusChangedEvent>, IHandle<MainWindowKeyEvent> public class ProfileViewModel : ProfileEditorPanelViewModel, IHandle<MainWindowFocusChangedEvent>, IHandle<MainWindowKeyEvent>
{ {
private readonly IProfileEditorService _profileEditorService;
private readonly ILayerEditorService _layerEditorService; private readonly ILayerEditorService _layerEditorService;
private readonly IProfileEditorService _profileEditorService;
private readonly IProfileLayerViewModelFactory _profileLayerViewModelFactory; private readonly IProfileLayerViewModelFactory _profileLayerViewModelFactory;
private readonly ISettingsService _settingsService; private readonly ISettingsService _settingsService;
private readonly ISurfaceService _surfaceService; private readonly ISurfaceService _surfaceService;

View File

@ -4,7 +4,6 @@ using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using Artemis.Core.Models.Profile; using Artemis.Core.Models.Profile;
using Artemis.UI.Events; using Artemis.UI.Events;
using Artemis.UI.Services;
using Artemis.UI.Services.Interfaces; using Artemis.UI.Services.Interfaces;
using SkiaSharp; using SkiaSharp;
using SkiaSharp.Views.WPF; using SkiaSharp.Views.WPF;
@ -45,7 +44,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
LayerBounds = _layerEditorService.GetLayerBounds(layer); LayerBounds = _layerEditorService.GetLayerBounds(layer);
ShapePath = _layerEditorService.GetLayerPath(layer, true, true, true); ShapePath = _layerEditorService.GetLayerPath(layer, true, true, true);
ShapeAnchor = _layerEditorService.GetLayerAnchor(layer).ToSKPoint(); ShapeAnchor = _layerEditorService.GetLayerAnchorPosition(layer).ToSKPoint();
Execute.PostToUIThread(() => Execute.PostToUIThread(() =>
{ {
var shapeGeometry = new RectangleGeometry(_layerEditorService.GetLayerShapeBounds(layer.LayerShape)) var shapeGeometry = new RectangleGeometry(_layerEditorService.GetLayerShapeBounds(layer.LayerShape))
@ -69,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.GetLayerAnchor(layer), GetRelativePosition(sender, e.MouseEventArgs)); _previousDragAngle = CalculateAngle(_layerEditorService.GetLayerAnchorPosition(layer), GetRelativePosition(sender, e.MouseEventArgs));
else else
_previousDragAngle = 0; _previousDragAngle = 0;
} }
@ -86,7 +85,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
return; return;
var previousDragAngle = _previousDragAngle; var previousDragAngle = _previousDragAngle;
var newRotation = CalculateAngle(_layerEditorService.GetLayerAnchor(layer), GetRelativePosition(sender, e.MouseEventArgs)); var newRotation = CalculateAngle(_layerEditorService.GetLayerAnchorPosition(layer), GetRelativePosition(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
@ -280,7 +279,7 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
// Measure from the top-left of the shape (without rotation) // Measure from the top-left of the shape (without rotation)
_dragOffset = topLeft + (dragStartPosition - topLeft); _dragOffset = topLeft + (dragStartPosition - topLeft);
// Get the absolute layer anchor and make it relative to the unrotated shape // Get the absolute layer anchor and make it relative to the unrotated shape
_dragStartAnchor = _layerEditorService.GetLayerAnchor(layer).ToSKPoint() - topLeft; _dragStartAnchor = _layerEditorService.GetLayerAnchorPosition(layer).ToSKPoint() - topLeft;
// Ensure the anchor starts in the center of the shape it is now relative to // Ensure the anchor starts in the center of the shape it is now relative to
_dragStartAnchor.X -= path.Bounds.Width / 2f; _dragStartAnchor.X -= path.Bounds.Width / 2f;
_dragStartAnchor.Y -= path.Bounds.Height / 2f; _dragStartAnchor.Y -= path.Bounds.Height / 2f;

View File

@ -1,13 +1,10 @@
using System.Diagnostics; using System.IO;
using System.IO;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using Artemis.Core.Models.Profile; using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Profile.LayerShapes; using Artemis.Core.Models.Profile.LayerShapes;
using Artemis.UI.Properties; using Artemis.UI.Properties;
using Artemis.UI.Services;
using Artemis.UI.Services.Interfaces; using Artemis.UI.Services.Interfaces;
using SkiaSharp.Views.WPF;
namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
{ {

View File

@ -1,12 +1,9 @@
using System; using System.IO;
using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using Artemis.Core.Models.Profile.LayerShapes; using Artemis.Core.Models.Profile.LayerShapes;
using Artemis.UI.Properties; using Artemis.UI.Properties;
using Artemis.UI.Services;
using Artemis.UI.Services.Interfaces; using Artemis.UI.Services.Interfaces;
namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools

View File

@ -4,9 +4,7 @@ using System.Windows.Input;
using Artemis.Core.Models.Profile; using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Profile.LayerShapes; using Artemis.Core.Models.Profile.LayerShapes;
using Artemis.UI.Properties; using Artemis.UI.Properties;
using Artemis.UI.Services;
using Artemis.UI.Services.Interfaces; using Artemis.UI.Services.Interfaces;
using SkiaSharp.Views.WPF;
namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
{ {

View File

@ -1,5 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
@ -8,10 +7,8 @@ using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Surface; using Artemis.Core.Models.Surface;
using Artemis.UI.Extensions; using Artemis.UI.Extensions;
using Artemis.UI.Properties; using Artemis.UI.Properties;
using Artemis.UI.Services;
using Artemis.UI.Services.Interfaces; using Artemis.UI.Services.Interfaces;
using SkiaSharp; using SkiaSharp;
using SkiaSharp.Views.WPF;
namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.Tools
{ {

View File

@ -7,7 +7,6 @@ using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Surface; using Artemis.Core.Models.Surface;
using Artemis.UI.Extensions; using Artemis.UI.Extensions;
using Artemis.UI.Properties; using Artemis.UI.Properties;
using Artemis.UI.Services;
using Artemis.UI.Services.Interfaces; using Artemis.UI.Services.Interfaces;
using SkiaSharp; using SkiaSharp;

View File

@ -12,25 +12,25 @@
Cursor="/Resources/aero_rotate.cur" Cursor="/Resources/aero_rotate.cur"
MouseDown="RotationOnMouseDown" MouseDown="RotationOnMouseDown"
MouseUp="RotationOnMouseUp" MouseUp="RotationOnMouseUp"
MouseMove="RotationOnMouseMove"/> MouseMove="RotationOnMouseMove" />
<Ellipse x:Name="RotateTopRight" <Ellipse x:Name="RotateTopRight"
Fill="Transparent" Fill="Transparent"
Cursor="/Resources/aero_rotate.cur" Cursor="/Resources/aero_rotate.cur"
MouseDown="RotationOnMouseDown" MouseDown="RotationOnMouseDown"
MouseUp="RotationOnMouseUp" MouseUp="RotationOnMouseUp"
MouseMove="RotationOnMouseMove"/> MouseMove="RotationOnMouseMove" />
<Ellipse x:Name="RotateBottomRight" <Ellipse x:Name="RotateBottomRight"
Fill="Transparent" Fill="Transparent"
Cursor="/Resources/aero_rotate.cur" Cursor="/Resources/aero_rotate.cur"
MouseDown="RotationOnMouseDown" MouseDown="RotationOnMouseDown"
MouseUp="RotationOnMouseUp" MouseUp="RotationOnMouseUp"
MouseMove="RotationOnMouseMove"/> MouseMove="RotationOnMouseMove" />
<Ellipse x:Name="RotateBottomLeft" <Ellipse x:Name="RotateBottomLeft"
Fill="Transparent" Fill="Transparent"
Cursor="/Resources/aero_rotate.cur" Cursor="/Resources/aero_rotate.cur"
MouseDown="RotationOnMouseDown" MouseDown="RotationOnMouseDown"
MouseUp="RotationOnMouseUp" MouseUp="RotationOnMouseUp"
MouseMove="RotationOnMouseMove"/> MouseMove="RotationOnMouseMove" />
<!-- The part of the layer's shape that is inside the layer --> <!-- The part of the layer's shape that is inside the layer -->
<Path x:Name="LayerShapeOutline" <Path x:Name="LayerShapeOutline"
Data="{Binding ShapeGeometry, Mode=OneWay}" Data="{Binding ShapeGeometry, Mode=OneWay}"
@ -51,7 +51,7 @@
Cursor="Hand" Cursor="Hand"
MouseDown="ResizeOnMouseDown" MouseDown="ResizeOnMouseDown"
MouseUp="ResizeOnMouseUp" MouseUp="ResizeOnMouseUp"
MouseMove="ResizeOnMouseMove"/> MouseMove="ResizeOnMouseMove" />
<Rectangle x:Name="ResizeRightCenter" <Rectangle x:Name="ResizeRightCenter"
Fill="White" Fill="White"
Stroke="{DynamicResource SecondaryAccentBrush}" Stroke="{DynamicResource SecondaryAccentBrush}"
@ -118,6 +118,6 @@
Cursor="SizeAll" Cursor="SizeAll"
MouseDown="MoveOnMouseDown" MouseDown="MoveOnMouseDown"
MouseUp="MoveOnMouseUp" MouseUp="MoveOnMouseUp"
MouseMove="MoveOnMouseMove"/> MouseMove="MoveOnMouseMove" />
</Canvas> </Canvas>
</UserControl> </UserControl>

View File

@ -4,7 +4,6 @@ using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using Artemis.UI.Events; using Artemis.UI.Events;
using Artemis.UI.Utilities;
using SkiaSharp; using SkiaSharp;
namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.UserControls namespace Artemis.UI.Screens.Module.ProfileEditor.Visualization.UserControls

View File

@ -51,7 +51,7 @@ namespace Artemis.UI.Screens.Shared
} }
else else
{ {
PanX = absoluteX - relative.X * Zoom; PanX = absoluteX - relative.X * Zoom;
PanY = absoluteY - relative.Y * Zoom; PanY = absoluteY - relative.Y * Zoom;
} }
} }

View File

@ -0,0 +1,67 @@
using System.Windows;
using System.Windows.Media;
using Artemis.Core.Models.Profile;
using Artemis.Core.Models.Profile.LayerShapes;
using SkiaSharp;
namespace Artemis.UI.Services.Interfaces
{
public interface ILayerEditorService : IArtemisUIService
{
/// <summary>
/// Returns the layer's bounds, corrected for the current render scale.
/// </summary>
/// <param name="layer"></param>
/// <returns></returns>
Rect GetLayerBounds(Layer layer);
/// <summary>
/// Returns the layer's anchor, corrected for the current render scale.
/// </summary>
/// <param name="layer"></param>
/// <returns></returns>
Point GetLayerAnchorPosition(Layer layer);
/// <summary>
/// Returns the layer shape's bounds, corrected for the current render scale.
/// </summary>
/// <param name="layerShape"></param>
/// <returns></returns>
Rect GetLayerShapeBounds(LayerShape layerShape);
/// <summary>
/// Creates a WPF transform group that contains all the transformations required to render the provided layer.
/// Note: Run on UI thread.
/// </summary>
/// <param name="layer"></param>
/// <returns></returns>
TransformGroup GetLayerTransformGroup(Layer layer);
/// <summary>
/// Returns an absolute and scaled rectangular path for the given layer that is corrected for the current render scale.
/// </summary>
/// <param name="layer"></param>
/// <param name="includeTranslation"></param>
/// <param name="includeScale"></param>
/// <param name="includeRotation"></param>
/// <returns></returns>
SKPath GetLayerPath(Layer layer, bool includeTranslation, bool includeScale, bool includeRotation);
/// <summary>
/// Sets the base properties of the given shape to match the provided unscaled rectangle. The rectangle is corrected
/// for the current render scale, anchor property and size property.
/// </summary>
/// <param name="layerShape"></param>
/// <param name="rect"></param>
void SetShapeBaseFromRectangle(LayerShape layerShape, Rect rect);
/// <summary>
/// Returns a new point scaled to the layer.
/// </summary>
/// <param name="layer"></param>
/// <param name="point"></param>
/// <param name="absolute"></param>
/// <returns></returns>
SKPoint GetScaledPoint(Layer layer, SKPoint point, bool absolute);
}
}

View File

@ -47,16 +47,20 @@ namespace Artemis.UI.Services
} }
/// <inheritdoc /> /// <inheritdoc />
public Point GetLayerAnchor(Layer layer) public Point GetLayerAnchorPosition(Layer layer)
{ {
var layerBounds = GetLayerBounds(layer); var layerBounds = GetLayerBounds(layer);
var shapeBounds = GetLayerShapeBounds(layer.LayerShape);
var positionProperty = layer.PositionProperty.CurrentValue;
// TODO figure out what else is needed, position should matter here // Start at the center of the shape
var anchor = layer.AnchorPointProperty.CurrentValue; var position = new Point(shapeBounds.Left, shapeBounds.Top);
anchor.X = (float) (anchor.X * layerBounds.Width);
anchor.Y = (float) (anchor.Y * layerBounds.Height);
return new Point(anchor.X * layerBounds.Width, anchor.Y * layerBounds.Height); // Apply translation
position.X += positionProperty.X * layerBounds.Width;
position.Y += positionProperty.Y * layerBounds.Height;
return position;
} }
/// <inheritdoc /> /// <inheritdoc />
@ -65,17 +69,21 @@ namespace Artemis.UI.Services
var layerBounds = GetLayerBounds(layer).ToSKRect(); var layerBounds = GetLayerBounds(layer).ToSKRect();
var shapeBounds = GetLayerShapeBounds(layer.LayerShape).ToSKRect(); var shapeBounds = GetLayerShapeBounds(layer.LayerShape).ToSKRect();
// Apply transformation like done by the core during layer rendering // Apply transformation like done by the core during layer rendering.
var anchor = GetLayerAnchor(layer); // The order in which translations are applied are different because the UI renders the shape inside
// the layer using the structure of the XAML while the Core has to deal with that by applying the layer
// position to the translation
var anchorPosition = GetLayerAnchorPosition(layer);
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 = layer.PositionProperty.CurrentValue.X * layerBounds.Width - shapeBounds.Width / 2 - anchor.X; var x = anchorPosition.X - shapeBounds.Width / 2 - anchorProperty.X * layerBounds.Width;
var y = layer.PositionProperty.CurrentValue.Y * layerBounds.Height - shapeBounds.Height / 2 - anchor.Y; var y = anchorPosition.Y - shapeBounds.Height / 2 - 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));
transformGroup.Children.Add(new ScaleTransform(layer.SizeProperty.CurrentValue.Width, layer.SizeProperty.CurrentValue.Height, anchor.X, anchor.Y)); transformGroup.Children.Add(new ScaleTransform(layer.SizeProperty.CurrentValue.Width, layer.SizeProperty.CurrentValue.Height, anchorPosition.X, anchorPosition.Y));
transformGroup.Children.Add(new RotateTransform(layer.RotationProperty.CurrentValue, anchor.X, anchor.Y)); transformGroup.Children.Add(new RotateTransform(layer.RotationProperty.CurrentValue, anchorPosition.X, anchorPosition.Y));
return transformGroup; return transformGroup;
} }
@ -86,21 +94,22 @@ namespace Artemis.UI.Services
var layerBounds = GetLayerBounds(layer).ToSKRect(); var layerBounds = GetLayerBounds(layer).ToSKRect();
var shapeBounds = GetLayerShapeBounds(layer.LayerShape).ToSKRect(); var shapeBounds = GetLayerShapeBounds(layer.LayerShape).ToSKRect();
// Apply transformation like done by the core during layer rendering // Apply transformation like done by the core during layer rendering (same differences apply as in GetLayerTransformGroup)
var anchor = GetLayerAnchor(layer).ToSKPoint(); var anchorPosition = GetLayerAnchorPosition(layer).ToSKPoint();
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 = layer.PositionProperty.CurrentValue.X * layerBounds.Width - shapeBounds.Width / 2 - anchor.X; var x = anchorPosition.X - shapeBounds.Width / 2 - anchorProperty.X * layerBounds.Width;
var y = layer.PositionProperty.CurrentValue.Y * layerBounds.Height - shapeBounds.Height / 2 - anchor.Y; var y = anchorPosition.Y - shapeBounds.Height / 2 - anchorProperty.Y * layerBounds.Height;
var path = new SKPath(); var path = new SKPath();
path.AddRect(shapeBounds); path.AddRect(shapeBounds);
if (includeTranslation) if (includeTranslation)
path.Transform(SKMatrix.MakeTranslation(x, y)); path.Transform(SKMatrix.MakeTranslation(x, y));
if (includeScale) if (includeScale)
path.Transform(SKMatrix.MakeScale(layer.SizeProperty.CurrentValue.Width, layer.SizeProperty.CurrentValue.Height, anchor.X, anchor.Y)); path.Transform(SKMatrix.MakeScale(layer.SizeProperty.CurrentValue.Width, layer.SizeProperty.CurrentValue.Height, anchorPosition.X, anchorPosition.Y));
if (includeRotation) if (includeRotation)
path.Transform(SKMatrix.MakeRotationDegrees(layer.RotationProperty.CurrentValue, anchor.X, anchor.Y)); path.Transform(SKMatrix.MakeRotationDegrees(layer.RotationProperty.CurrentValue, anchorPosition.X, anchorPosition.Y));
return path; return path;
} }
@ -159,63 +168,4 @@ namespace Artemis.UI.Services
); );
} }
} }
public interface ILayerEditorService : IArtemisUIService
{
/// <summary>
/// Returns the layer's bounds, corrected for the current render scale.
/// </summary>
/// <param name="layer"></param>
/// <returns></returns>
Rect GetLayerBounds(Layer layer);
/// <summary>
/// Returns the layer's anchor, corrected for the current render scale.
/// </summary>
/// <param name="layer"></param>
/// <returns></returns>
Point GetLayerAnchor(Layer layer);
/// <summary>
/// Returns the layer shape's bounds, corrected for the current render scale.
/// </summary>
/// <param name="layerShape"></param>
/// <returns></returns>
Rect GetLayerShapeBounds(LayerShape layerShape);
/// <summary>
/// Creates a WPF transform group that contains all the transformations required to render the provided layer.
/// Note: Run on UI thread.
/// </summary>
/// <param name="layer"></param>
/// <returns></returns>
TransformGroup GetLayerTransformGroup(Layer layer);
/// <summary>
/// Returns an absolute and scaled rectangular path for the given layer that is corrected for the current render scale.
/// </summary>
/// <param name="layer"></param>
/// <param name="includeTranslation"></param>
/// <param name="includeScale"></param>
/// <param name="includeRotation"></param>
/// <returns></returns>
SKPath GetLayerPath(Layer layer, bool includeTranslation, bool includeScale, bool includeRotation);
/// <summary>
/// Sets the base properties of the given shape to match the provided unscaled rectangle. The rectangle is corrected
/// for the current render scale, anchor property and size property.
/// </summary>
/// <param name="layerShape"></param>
/// <param name="rect"></param>
void SetShapeBaseFromRectangle(LayerShape layerShape, Rect rect);
/// <summary>
/// Returns a new point scaled to the layer.
/// </summary>
/// <param name="layer"></param>
/// <param name="point"></param>
/// <param name="absolute"></param>
/// <returns></returns>
SKPoint GetScaledPoint(Layer layer, SKPoint point, bool absolute);
}
} }