From 557ab871ed1dcc0b31cba1bc8f310bcf5d4df541 Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Mon, 9 Nov 2026 23:10:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=9B=B6=E4=BB=B6=E6=A0=87?= =?UTF-8?q?=E6=B3=A8=E5=9B=BE=E5=83=8F=E6=BB=9A=E8=BD=AE=E7=BC=A9=E6=94=BE?= =?UTF-8?q?=E5=92=8C=E5=8F=B3=E9=94=AE=E6=8B=96=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Training/AnnotationCanvas.cs | 61 +++++++++++++++++-- .../Training/TrainingPage.axaml | 3 +- .../Training/TrainingPage.axaml.cs | 1 + tests/Avalonia.Smoke/Program.cs | 19 ++++++ 4 files changed, 77 insertions(+), 7 deletions(-) diff --git a/src/SopHelper.Avalonia/Training/AnnotationCanvas.cs b/src/SopHelper.Avalonia/Training/AnnotationCanvas.cs index e62d584..3bd8fe7 100644 --- a/src/SopHelper.Avalonia/Training/AnnotationCanvas.cs +++ b/src/SopHelper.Avalonia/Training/AnnotationCanvas.cs @@ -13,9 +13,42 @@ public sealed class AnnotationCanvas : Control public double[]? Box {get=>GetValue(BoxProperty);set=>SetValue(BoxProperty,value);} public event Action? BoxDrawn; private Point? _start;private Point _end;private IPointer? _pointer; + private double _zoom=1; + private Vector _offset; + private Point? _panStart; static AnnotationCanvas(){AffectsRender(SourceProperty,BoxProperty);} public AnnotationCanvas(){ClipToBounds=true;Cursor=new Cursor(StandardCursorType.Cross);Focusable=true;} - private Rect ImageRect=>Source is null?default:VVMs.RecipeEditor.RegionCanvas.FitImage(Bounds.Size,new Size(Source.PixelWidth,Source.PixelHeight)); + private Rect ImageRect + { + get + { + if(Source is null)return default; + var fit=VVMs.RecipeEditor.RegionCanvas.FitImage(Bounds.Size,new Size(Source.PixelWidth,Source.PixelHeight)); + return new Rect(fit.X+_offset.X,fit.Y+_offset.Y,fit.Width*_zoom,fit.Height*_zoom); + } + } + public void ResetView() + {_zoom=1;_offset=default;EndGesture();InvalidateVisual();} + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) + { + base.OnPropertyChanged(change); + if(change.Property==SourceProperty) + { + EndGesture(); + var previous=change.GetOldValue(); + if(Source is null||previous is null||Source.PixelWidth!=previous.PixelWidth||Source.PixelHeight!=previous.PixelHeight)ResetView(); + } + } + protected override void OnPointerWheelChanged(PointerWheelEventArgs e) + { + if(!IsEnabled||Source is null||ImageRect.Width<=0)return; + e.Handled=true; + if(_pointer is not null||e.Delta.Y==0)return; + var zoom=Math.Clamp(_zoom*Math.Pow(1.2,e.Delta.Y),0.1,20); + var position=e.GetPosition(this);var image=ImageRect;var ratio=zoom/_zoom; + _offset+=new Vector((position.X-image.X)*(1-ratio),(position.Y-image.Y)*(1-ratio)); + _zoom=zoom;InvalidateVisual(); + } public override void Render(DrawingContext dc) { dc.DrawRectangle(Brushes.Transparent,null,new Rect(Bounds.Size));var image=ImageRect;if(Source is null||image.Width<=0)return; @@ -26,18 +59,34 @@ public sealed class AnnotationCanvas : Control } protected override void OnPointerPressed(PointerPressedEventArgs e) { - if(!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed||!IsEnabled||Source is null||!ImageRect.Contains(e.GetPosition(this)))return; + if(!IsEnabled||Source is null||_pointer is not null)return; + if(e.GetCurrentPoint(this).Properties.PointerUpdateKind==PointerUpdateKind.RightButtonPressed) + { + Focus();_panStart=e.GetPosition(this);_pointer=e.Pointer;_pointer.Capture(this); + Cursor=new Cursor(StandardCursorType.SizeAll);e.Handled=true;return; + } + if(e.GetCurrentPoint(this).Properties.PointerUpdateKind!=PointerUpdateKind.LeftButtonPressed||!ImageRect.Contains(e.GetPosition(this)))return; Focus();_start=_end=e.GetPosition(this);_pointer=e.Pointer;_pointer.Capture(this);InvalidateVisual();e.Handled=true; } protected override void OnPointerMoved(PointerEventArgs e) - {if(_start is null)return;var p=e.GetPosition(this);var image=ImageRect;_end=new Point(Math.Clamp(p.X,image.Left,image.Right),Math.Clamp(p.Y,image.Top,image.Bottom));InvalidateVisual();} + { + if(_panStart is {} pan) + {var position=e.GetPosition(this);_offset+=position-pan;_panStart=position;InvalidateVisual();e.Handled=true;return;} + if(_start is null)return;var p=e.GetPosition(this);var image=ImageRect;_end=new Point(Math.Clamp(p.X,image.Left,image.Right),Math.Clamp(p.Y,image.Top,image.Bottom));InvalidateVisual(); + } protected override void OnPointerReleased(PointerReleasedEventArgs e) { - if(_start is not {} start||Source is null)return; - var rectangle=DragRect(start,_end);var image=ImageRect;_start=null;_pointer?.Capture(null);_pointer=null;InvalidateVisual(); + if(_panStart is not null) + {if(e.InitialPressMouseButton==MouseButton.Right){EndGesture();e.Handled=true;}return;} + if(e.InitialPressMouseButton!=MouseButton.Left||_start is not {} start||Source is null)return; + var p=e.GetPosition(this);var image=ImageRect; + var rectangle=DragRect(start,new Point(Math.Clamp(p.X,image.Left,image.Right),Math.Clamp(p.Y,image.Top,image.Bottom))); + EndGesture();e.Handled=true; if(rectangle.Width<3||rectangle.Height<3||image.Width<=0)return; var scale=Source.PixelWidth/image.Width;BoxDrawn?.Invoke([(rectangle.Left-image.Left)*scale,(rectangle.Top-image.Top)*scale,Math.Min(Source.PixelWidth,(rectangle.Right-image.Left)*scale),Math.Min(Source.PixelHeight,(rectangle.Bottom-image.Top)*scale)]); } - protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e){_start=null;_pointer=null;InvalidateVisual();base.OnPointerCaptureLost(e);} + private void EndGesture() + {var pointer=_pointer;_start=null;_panStart=null;_pointer=null;pointer?.Capture(null);Cursor=new Cursor(StandardCursorType.Cross);InvalidateVisual();} + protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e){EndGesture();base.OnPointerCaptureLost(e);} private static Rect DragRect(Point a,Point b)=>new(Math.Min(a.X,b.X),Math.Min(a.Y,b.Y),Math.Abs(a.X-b.X),Math.Abs(a.Y-b.Y)); } diff --git a/src/SopHelper.Avalonia/Training/TrainingPage.axaml b/src/SopHelper.Avalonia/Training/TrainingPage.axaml index f11e732..fa1b3b1 100644 --- a/src/SopHelper.Avalonia/Training/TrainingPage.axaml +++ b/src/SopHelper.Avalonia/Training/TrainingPage.axaml @@ -59,6 +59,7 @@ +