Explorar o código

Added rectangle and line info boxes

flabbet hai 8 meses
pai
achega
0af27ffb87

+ 1 - 1
src/Drawie

@@ -1 +1 @@
-Subproject commit f94f20c30f9f09a0bc37cc663efb3ea245896eb5
+Subproject commit 63d856f0bf2240882a7a27cf256fc64ee2323a99

+ 0 - 1
src/PixiEditor.UI.Common/PixiEditor.UI.Common.csproj

@@ -11,7 +11,6 @@
       <AvaloniaResource Include="Fonts\pixiperfect.ttf" />
       <None Remove="Assets\Animations\LoadingIndicator.json" />
       <AvaloniaResource Include="Assets\Animations\LoadingIndicator.json" />
-      <None Remove="Accents\Img_illust_mikunt-3.png" />
     </ItemGroup>
 
     <ItemGroup>

+ 16 - 0
src/PixiEditor/Helpers/ResourceLoader.cs

@@ -1,5 +1,6 @@
 using Avalonia;
 using Avalonia.Media;
+using Avalonia.Platform;
 using Avalonia.Styling;
 using Drawie.Backend.Core.Surfaces.PaintImpl;
 using PixiEditor.Helpers.Extensions;
@@ -8,6 +9,11 @@ namespace PixiEditor.Helpers;
 
 public static class ResourceLoader
 {
+    public static Stream LoadResourceStream(Uri uri)
+    {
+        return AssetLoader.Open(uri);
+    }
+
     public static T GetResource<T>(string key)
     {
         if (Application.Current.Styles.TryGetResource(key, null, out object resource))
@@ -18,6 +24,16 @@ public static class ResourceLoader
         return default!;
     }
 
+    public static T GetResource<T>(string key, ThemeVariant? themeVariant)
+    {
+        if (Application.Current.Styles.TryGetResource(key, themeVariant, out object resource))
+        {
+            return (T)resource;
+        }
+
+        return default!;
+    }
+
     public static Paint? GetPaint(string key, PaintStyle style = PaintStyle.Fill, ThemeVariant? themeVariant = null)
     {
         if (Application.Current.Styles.TryGetResource(key, themeVariant, out object paint))

+ 19 - 0
src/PixiEditor/Helpers/ThemeResources.cs

@@ -0,0 +1,19 @@
+using Avalonia;
+using Avalonia.Media;
+using Drawie.Backend.Core.Text;
+using PixiEditor.Helpers.Extensions;
+using Color = Drawie.Backend.Core.ColorsImpl.Color;
+
+namespace PixiEditor.Helpers;
+
+public static class ThemeResources
+{
+    public static Font ThemeFont =>
+        Font.FromFamilyName("FiraSans") ?? Font.CreateDefault();
+
+    public static Color ForegroundColor =>
+        ResourceLoader.GetResource<SolidColorBrush>("ThemeForegroundBrush", Application.Current.ActualThemeVariant).Color.ToColor();
+    
+    public static Color BackgroundColor =>
+        ResourceLoader.GetResource<SolidColorBrush>("ThemeBackgroundBrush", Application.Current.ActualThemeVariant).Color.ToColor();
+}

+ 49 - 0
src/PixiEditor/Views/Overlays/Drawables/InfoBox.cs

@@ -0,0 +1,49 @@
+using Drawie.Backend.Core.ColorsImpl;
+using Drawie.Backend.Core.Surfaces;
+using Drawie.Backend.Core.Surfaces.PaintImpl;
+using Drawie.Backend.Core.Text;
+using Drawie.Numerics;
+using PixiEditor.Helpers;
+
+namespace PixiEditor.Views.Overlays.Drawables;
+
+public class InfoBox
+{
+    private Paint fontPen = new Paint()
+    {
+        Color = Colors.Black, StrokeWidth = 1, Style = PaintStyle.Fill, IsAntiAliased = true
+    };
+
+    private Paint backgroundPen = new Paint()
+    {
+        Color = Colors.White, StrokeWidth = 1, Style = PaintStyle.Fill, IsAntiAliased = true
+    };
+
+    public double ZoomScale { get; set; } = 1;
+
+    private Font font;
+
+    public InfoBox()
+    {
+        font = ThemeResources.ThemeFont;
+        fontPen.Color = ThemeResources.ForegroundColor;
+        backgroundPen.Color = ThemeResources.BackgroundColor;
+    }
+
+    public void DrawInfo(Canvas context, string text, VecD pointerPos)
+    {
+        font.FontSize = 14 / ZoomScale;
+
+        double widthTextSize = font.MeasureText(text);
+
+        VecD aboveCursor = pointerPos + new VecD(0, -20 / ZoomScale);
+        float rectWidth = (float)widthTextSize + (10 / (float)ZoomScale);
+        float rectHeight = 20 / (float)ZoomScale;
+        float x = (float)aboveCursor.X - rectWidth / 2;
+        float y = (float)aboveCursor.Y - ((float)font.FontSize);
+        context.DrawRoundRect(x, y, rectWidth, rectHeight, 5 / (float)ZoomScale,
+            5 / (float)ZoomScale, backgroundPen);
+
+        context.DrawText(text, aboveCursor, TextAlign.Center, font, fontPen);
+    }
+}

+ 24 - 0
src/PixiEditor/Views/Overlays/LineToolOverlay/LineToolOverlay.cs

@@ -10,6 +10,7 @@ using Drawie.Backend.Core.Surfaces.PaintImpl;
 using Drawie.Backend.Core.Surfaces.Vector;
 using PixiEditor.Extensions.UI.Overlays;
 using Drawie.Numerics;
+using PixiEditor.Views.Overlays.Drawables;
 using PixiEditor.Views.Overlays.Handles;
 using PixiEditor.Views.Overlays.TransformOverlay;
 using Colors = Drawie.Backend.Core.ColorsImpl.Colors;
@@ -68,12 +69,17 @@ internal class LineToolOverlay : Overlay
     private VecD mouseDownPos = VecD.Zero;
     private VecD lineStartOnMouseDown = VecD.Zero;
     private VecD lineEndOnMouseDown = VecD.Zero;
+    
+    private VecD lastMousePos = VecD.Zero;
 
     private bool movedWhileMouseDown = false;
 
     private RectangleHandle startHandle;
     private RectangleHandle endHandle;
     private TransformHandle moveHandle;
+    
+    private bool isDraggingHandle = false;
+    private InfoBox infoBox;
 
     private VecD startPos;
     private VecD endPos;
@@ -105,6 +111,8 @@ internal class LineToolOverlay : Overlay
         moveHandle.OnHover += handle => Refresh();
         moveHandle.OnRelease += OnHandleRelease;
         AddHandle(moveHandle);
+        
+        infoBox = new InfoBox();
     }
 
     private void OnHandleRelease(Handle obj)
@@ -115,6 +123,8 @@ internal class LineToolOverlay : Overlay
             SnappingController.HighlightedYAxis = null;
             Refresh();
         }
+        
+        isDraggingHandle = false;
     }
 
     protected override void ZoomChanged(double newZoom)
@@ -130,6 +140,8 @@ internal class LineToolOverlay : Overlay
         dashes[1] = whiteDashPaint.StrokeWidth * 3;
         
         whiteDashPaint.PathEffect = PathEffect.CreateDash(dashes, 2);
+        
+        infoBox.ZoomScale = newZoom;
     }
 
     public override void RenderOverlay(Canvas context, RectD canvasBounds)
@@ -151,6 +163,12 @@ internal class LineToolOverlay : Overlay
         startHandle.Draw(context);
         endHandle.Draw(context);
         moveHandle.Draw(context);
+
+        if (isDraggingHandle)
+        {
+            string length = $"L: {(mappedEnd - mappedStart).Length:0.#} px";
+            infoBox.DrawInfo(context, length, lastMousePos);
+        }
     }
 
     protected override void OnOverlayPointerPressed(OverlayPointerArgs args)
@@ -171,6 +189,9 @@ internal class LineToolOverlay : Overlay
         VecD delta = position - mouseDownPos;
         LineStart = SnapAndHighlight(lineStartOnMouseDown + delta);
         movedWhileMouseDown = true;
+        
+        lastMousePos = position;
+        isDraggingHandle = true;
     }
 
     private void EndHandleOnDrag(Handle source, VecD position)
@@ -180,6 +201,9 @@ internal class LineToolOverlay : Overlay
         
         LineEnd = final;
         movedWhileMouseDown = true;
+        
+        isDraggingHandle = true;
+        lastMousePos = position;
     }
 
     private VecD SnapAndHighlight(VecD position)

+ 25 - 1
src/PixiEditor/Views/Overlays/TransformOverlay/TransformOverlay.cs

@@ -13,10 +13,12 @@ using Drawie.Backend.Core.Numerics;
 using Drawie.Backend.Core.Surfaces;
 using Drawie.Backend.Core.Surfaces.PaintImpl;
 using Drawie.Backend.Core.Surfaces.Vector;
+using Drawie.Backend.Core.Text;
 using PixiEditor.Extensions.UI.Overlays;
 using PixiEditor.Helpers.UI;
 using PixiEditor.Models.Controllers.InputDevice;
 using Drawie.Numerics;
+using PixiEditor.Views.Overlays.Drawables;
 using PixiEditor.Views.Overlays.Handles;
 using Colors = Drawie.Backend.Core.ColorsImpl.Colors;
 using Point = Avalonia.Point;
@@ -227,7 +229,8 @@ internal class TransformOverlay : Overlay
     private bool rotationCursorActive = false;
 
     private VecD lastPointerPos;
-
+    private InfoBox infoBox;
+    
     public TransformOverlay()
     {
         topLeftHandle = new AnchorHandle(this);
@@ -280,6 +283,8 @@ internal class TransformOverlay : Overlay
 
         moveHandle.OnPress += OnMoveHandlePressed;
         moveHandle.OnRelease += OnMoveHandleReleased;
+
+        infoBox = new InfoBox();
     }
 
     private VecD pos;
@@ -405,6 +410,25 @@ internal class TransformOverlay : Overlay
         }
 
         context.RestoreToCount(saved);
+
+        infoBox.ZoomScale = ZoomScale;
+        if (capturedAnchor is not null && capturedAnchor != Anchor.Origin)
+        {
+            VecD rectSize = Corners.RectSize;
+            string sizeText = $"W: {rectSize.X:0.#} H: {rectSize.Y:0.#} px";
+            infoBox.DrawInfo(context, sizeText, lastPointerPos);
+        }
+        else if (isRotating)
+        {
+            infoBox.DrawInfo(context, $"{(RadiansToDegreesNormalized(corners.RectRotation)):0.#}\u00b0", lastPointerPos);
+        }
+    }
+    
+    private double RadiansToDegreesNormalized(double radians)
+    {
+        double degrees = double.RadiansToDegrees(radians);
+        degrees = (degrees + 360) % 360;
+        return degrees;
     }
 
     private void OnAnchorHandlePressed(Handle source, VecD position)