Explorar o código

avalonia: Improvements to ImageEditor

Kenric Nugteren hai 2 días
pai
achega
ada726bcde

+ 1 - 4
InABox.Avalonia/Components/ImageEditor/ImageEditor.axaml

@@ -78,10 +78,7 @@
 					<Style Selector="Thumb">
 						<Setter Property="Template">
 							<ControlTemplate>
-								<Border Background="LightGray"
-										BorderBrush="Black"
-										BorderThickness="{TemplateBinding BorderThickness}"
-										CornerRadius="3"/>
+								<Border Background="Transparent"/>
 							</ControlTemplate>
 						</Setter>
 					</Style>

+ 18 - 4
InABox.Avalonia/Components/ImageEditor/ImageEditor.axaml.cs

@@ -404,6 +404,12 @@ public partial class ImageEditor : UserControl
         Changed?.Invoke(this, new EventArgs());
     }
 
+    private void RemoveObject(IImageEditorObject obj)
+    {
+        Objects.Remove(obj);
+        UpdateUndoRedoButtons();
+    }
+
     [RelayCommand]
     private void SetMode(ImageEditingMode mode)
     {
@@ -899,7 +905,7 @@ public partial class ImageEditor : UserControl
                 dimension.Point2 = position;
                 if(dimension.Point1 == dimension.Point2)
                 {
-                    Objects.Remove(dimension);
+                    RemoveObject(dimension);
                     CurrentObject = null;
                     return;
                 }
@@ -910,10 +916,18 @@ public partial class ImageEditor : UserControl
                     x.Title = "Enter Dimension:";
                 }).ContinueWith(task =>
                 {
-                    dimension.Text = task.Result ?? "";
-                    dimension.Update();
+                    if(task.Result is null)
+                    {
+                        RemoveObject(dimension);
+                        CurrentObject = null;
+                    }
+                    else
+                    {
+                        dimension.Text = task.Result;
+                        dimension.Update();
+                        Changed?.Invoke(this, new EventArgs());
+                    }
                 }, TaskScheduler.FromCurrentSynchronizationContext());
-                Changed?.Invoke(this, new EventArgs());
                 break;
             default:
                 switch (Mode)

+ 43 - 39
InABox.Avalonia/Components/ImageEditor/Objects/DimensionObject.cs

@@ -64,10 +64,8 @@ internal class DimensionObject : IImageEditorStateObject
         return new Vector(mainVec.Y, -mainVec.X);
     }
 
-    public void SetState(ImageEditorState state)
+    private void CreateThumb()
     {
-        _state = state;
-
         var p1 = new Vector(Point1.X, Point1.Y);
         var p2 = new Vector(Point2.X, Point2.Y);
 
@@ -84,7 +82,6 @@ internal class DimensionObject : IImageEditorStateObject
 
         var perpVec = new Vector(mainVec.Y, -mainVec.X);
 
-        var arrowLength = LineThickness * 2;
         var offset = perpVec * Offset;
 
         var thumbSize = 30;
@@ -94,20 +91,25 @@ internal class DimensionObject : IImageEditorStateObject
             Thumb.RenderTransform = new TransformGroup
             {
                 Children = [
-                    new RotateTransform(Math.Atan2(mainVec.Y, mainVec.X) * 180 / Math.PI),
-                    new ScaleTransform(1 / _state.ScaleFactor, 1 / _state.ScaleFactor)
+                    new RotateTransform(Math.Atan2(mainVec.Y, mainVec.X) * 180 / Math.PI)
                     ]
             };
             Thumb.BorderThickness = new(1);
-            Thumb.Width = thumbSize;
-            Thumb.Height = thumbSize;
+            Thumb.Width = length;
+            Thumb.Height = Math.Max(20, thumbSize / _state.ScaleFactor);
 
             var thumbPos = p1 + offset + mainVec * length / 2;
-            Canvas.SetLeft(Thumb, thumbPos.X - thumbSize / 2);
-            Canvas.SetTop(Thumb, thumbPos.Y - thumbSize / 2);
+            Canvas.SetLeft(Thumb, thumbPos.X - Thumb.Width / 2);
+            Canvas.SetTop(Thumb, thumbPos.Y - Thumb.Height / 2);
         }
     }
 
+    public void SetState(ImageEditorState state)
+    {
+        _state = state;
+        CreateThumb();
+    }
+
     public void Update()
     {
         Control.Children.RemoveAll(Control.Children.Where(x => !(x is Thumb)));
@@ -133,14 +135,25 @@ internal class DimensionObject : IImageEditorStateObject
         var arrowLength = LineThickness * 2;
         var offset = perpVec * Offset;
 
+        var middle = p1 + offset + mainVec * length / 2;
+
+        var line1 = new Line
+        {
+            StartPoint = Point(p1 + offset),
+            EndPoint = Point(middle),
+            StrokeLineCap = PenLineCap.Round
+        };
+        var line2 = new Line
+        {
+            StartPoint = Point(middle),
+            EndPoint = Point(p2 + offset),
+            StrokeLineCap = PenLineCap.Round
+        };
+
         var mainLines = new List<Line>()
         {
-            new()
-            {
-                StartPoint = Point(p1 + offset),
-                EndPoint = Point(p2 + offset),
-                StrokeLineCap = PenLineCap.Round
-            },
+            line1,
+            line2,
             new()
             {
                 StartPoint = Point(p1 + offset),
@@ -203,17 +216,24 @@ internal class DimensionObject : IImageEditorStateObject
             Text = Text,
             FontSize = textHeight,
             TextAlignment = TextAlignment.Center,
-            Width = length,
             Foreground = PrimaryBrush
         };
         text.RenderTransform = new RotateTransform(Math.Atan2(mainVec.Y, mainVec.X) * 180 / Math.PI);
-        var textPos = p1 + offset + mainVec * length / 2 + perpVec * (textHeight / 2 + 5);
-        Canvas.SetLeft(text, textPos.X - length / 2);
-        Canvas.SetTop(text, textPos.Y - textHeight / 2);
+        var textPos = p1 + offset + mainVec * length / 2;
+        Canvas.SetLeft(text, textPos.X);
+        Canvas.SetTop(text, textPos.Y);
+        text.SizeChanged += (o, e) =>
+        {
+            line1.EndPoint = Point(middle - mainVec * (text.Bounds.Width / 2 + 5));
+            line2.StartPoint = Point(middle + mainVec * (text.Bounds.Width / 2 + 5));
 
-        Control.Children.Add(text);
+            var topCorner = textPos - mainVec * text.Bounds.Width / 2 + perpVec * text.Bounds.Height / 2;
 
-        var thumbSize = 30;
+            Canvas.SetLeft(text, textPos.X - text.Bounds.Width / 2);
+            Canvas.SetTop(text, textPos.Y - text.Bounds.Height / 2);
+        };
+
+        Control.Children.Add(text);
 
         if(Thumb is null && _active && length > 0)
         {
@@ -225,23 +245,7 @@ internal class DimensionObject : IImageEditorStateObject
             Control.Children.Add(Thumb);
         }
 
-        if(Thumb is not null)
-        {
-            Thumb.RenderTransform = new TransformGroup
-            {
-                Children = [
-                    new RotateTransform(Math.Atan2(mainVec.Y, mainVec.X) * 180 / Math.PI),
-                    new ScaleTransform(1 / _state.ScaleFactor, 1 / _state.ScaleFactor)
-                    ]
-            };
-            Thumb.BorderThickness = new(1);
-            Thumb.Width = thumbSize;
-            Thumb.Height = thumbSize;
-
-            var thumbPos = p1 + offset + mainVec * length / 2;
-            Canvas.SetLeft(Thumb, thumbPos.X - thumbSize / 2);
-            Canvas.SetTop(Thumb, thumbPos.Y - thumbSize / 2);
-        }
+        CreateThumb();
     }
 
     private Vector Rotate(Vector v, double angle)