aisop/src/SopHelper.Avalonia/Training/AnnotationCanvas.cs

134 lines
7.9 KiB
C#

using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
using System.Globalization;
using SopHelper.Presentation.Imaging;
using SopHelper.Engine.Training;
namespace SopHelper.Training;
public sealed class AnnotationCanvas : Control
{
public static readonly StyledProperty<BitmapSource?> SourceProperty=AvaloniaProperty.Register<AnnotationCanvas,BitmapSource?>(nameof(Source));
public static readonly StyledProperty<double[]?> BoxProperty=AvaloniaProperty.Register<AnnotationCanvas,double[]?>(nameof(Box));
public static readonly StyledProperty<string> TargetNameProperty=AvaloniaProperty.Register<AnnotationCanvas,string>(nameof(TargetName), "");
public static readonly StyledProperty<IReadOnlyList<ObjectAnnotation>?> AnnotationsProperty=AvaloniaProperty.Register<AnnotationCanvas,IReadOnlyList<ObjectAnnotation>?>(nameof(Annotations));
public IReadOnlyList<ObjectAnnotation>? Annotations {get=>GetValue(AnnotationsProperty);set=>SetValue(AnnotationsProperty,value);}
public BitmapSource? Source {get=>GetValue(SourceProperty);set=>SetValue(SourceProperty,value);}
public double[]? Box {get=>GetValue(BoxProperty);set=>SetValue(BoxProperty,value);}
public string TargetName {get=>GetValue(TargetNameProperty);set=>SetValue(TargetNameProperty,value);}
public event Action<double[]>? BoxDrawn;
private Point? _start;private Point _end;private IPointer? _pointer;
private double _zoom=1;
private Vector _offset;
private Point? _panStart;
static AnnotationCanvas(){AffectsRender<AnnotationCanvas>(SourceProperty,BoxProperty,TargetNameProperty,AnnotationsProperty);}
public AnnotationCanvas(){ClipToBounds=true;Cursor=new Cursor(StandardCursorType.Cross);Focusable=true;}
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<BitmapSource?>();
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;
dc.DrawImage(Source,image);var pen=new Pen(new SolidColorBrush(Color.FromRgb(0,220,220)),2);
foreach(var item in Annotations??[])
{
if(item.Label==TargetName||item.Box is not {Length:4} other)continue;
var scale=image.Width/Source.PixelWidth;
var rect=new Rect(image.X+other[0]*scale,image.Y+other[1]*scale,(other[2]-other[0])*scale,(other[3]-other[1])*scale);
dc.DrawRectangle(null,new Pen(Brushes.Orange,2),rect);DrawTargetName(dc,rect,Brushes.Orange,item.Label);
}
if(Box is { Length:4 } b)
{
var scale=image.Width/Source.PixelWidth;
var rect=new Rect(image.X+b[0]*scale,image.Y+b[1]*scale,(b[2]-b[0])*scale,(b[3]-b[1])*scale);
dc.DrawRectangle(new SolidColorBrush(Color.FromArgb(30,0,220,220)),pen,rect);
if(_start is null)DrawTargetName(dc,rect,pen.Brush!);
}
if(_start is {} start)
{
var rect=DragRect(start,_end);
dc.DrawRectangle(null,new Pen(Brushes.Yellow,2),rect);
DrawTargetName(dc,rect,Brushes.Yellow);
}
}
private void DrawTargetName(DrawingContext dc,Rect box,IBrush foreground,string? label=null)
{
label??=TargetName;
if(string.IsNullOrWhiteSpace(label)||Bounds.Width<=8||Bounds.Height<=8||
box.Right<0||box.Left>Bounds.Width||box.Bottom<0||box.Top>Bounds.Height)return;
const double padding=4;
var text=new FormattedText(label,CultureInfo.CurrentCulture,FlowDirection.LeftToRight,
new Typeface("Microsoft YaHei, Noto Sans CJK SC"),14,foreground)
{MaxTextWidth=Math.Max(1,Bounds.Width-2*padding),MaxLineCount=1,Trimming=TextTrimming.CharacterEllipsis};
var size=new Size(text.Width+2*padding,text.Height+2*padding);
var left=Math.Clamp(box.Left,0,Math.Max(0,Bounds.Width-size.Width));
// Keep the label visible when the box touches the top edge or is panned partly off-screen.
var top=box.Top-size.Height-3;
if(top<0)top=box.Top+3;
top=Math.Clamp(top,0,Math.Max(0,Bounds.Height-size.Height));
dc.DrawRectangle(new SolidColorBrush(Color.FromArgb(225,12,18,24)),null,new Rect(new Point(left,top),size));
dc.DrawText(text,new Point(left+padding,top+padding));
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
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(_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(_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)]);
}
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));
}