在标注框上方显示锁定的目标零件名称

This commit is contained in:
tfarcraw 2026-11-10 11:35:33 +08:00
parent d92214b6f4
commit 7e357bbc40
3 changed files with 39 additions and 5 deletions

View File

@ -2,6 +2,7 @@ using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
using System.Globalization;
using SopHelper.Presentation.Imaging;
namespace SopHelper.Training;
@ -9,14 +10,16 @@ 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 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);}
static AnnotationCanvas(){AffectsRender<AnnotationCanvas>(SourceProperty,BoxProperty,TargetNameProperty);}
public AnnotationCanvas(){ClipToBounds=true;Cursor=new Cursor(StandardCursorType.Cross);Focusable=true;}
private Rect ImageRect
{
@ -54,8 +57,35 @@ public sealed class AnnotationCanvas : Control
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);
if(Box is { Length:4 } b)
{var scale=image.Width/Source.PixelWidth;dc.DrawRectangle(new SolidColorBrush(Color.FromArgb(30,0,220,220)),pen,new Rect(image.X+b[0]*scale,image.Y+b[1]*scale,(b[2]-b[0])*scale,(b[3]-b[1])*scale));}
if(_start is {} start)dc.DrawRectangle(null,new Pen(Brushes.Yellow,2),DragRect(start,_end));
{
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)
{
if(string.IsNullOrWhiteSpace(TargetName)||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(TargetName,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)
{

View File

@ -56,7 +56,7 @@
</Grid.RowDefinitions>
<TextBlock Text="{Binding FrameInfo}" Margin="0,0,0,10" TextWrapping="Wrap" />
<Border Grid.Row="1" Background="#080E14" BorderBrush="{StaticResource Line}" BorderThickness="1" CornerRadius="6" ClipToBounds="True" Classes="panel">
<local:AnnotationCanvas x:Name="Canvas" Source="{Binding Preview}" Box="{Binding CurrentBox}" IsEnabled="{Binding IsIdle}" />
<local:AnnotationCanvas x:Name="Canvas" Source="{Binding Preview}" Box="{Binding CurrentBox}" TargetName="{Binding Label}" IsEnabled="{Binding IsIdle}" />
</Border>
<WrapPanel Grid.Row="2" Margin="0,12,0,0">
<Button Content="适应窗口" Click="ResetImageViewClicked" />

View File

@ -207,6 +207,7 @@ internal static class Smoke
var project=new TrainingProject { FilePath=Path.Combine(temporary,"project.json"),Name="测试零件",Width=640,Height=480,
Frames=[new FrameAnnotation {Index=0,Image="frame.png",Status="unlabeled"}] };
project.Save();trainer.Load(project.FilePath);trainer.DrawBox([10,20,100,200]);trainer.ApproveCommand.Execute();
Check(trainingView.GetVisualDescendants().OfType<AnnotationCanvas>().Single().TargetName==trainer.Label,"Annotation canvas uses the imported project's locked target name");
Check(TrainingProject.Load(trainer.Project!.FilePath).Frames[0].Status=="reviewed"&&RecipeWorkspace.IsWithin(trainer.Project.FilePath,storage.DirectoryFor(savedRecipe)),"Opened annotation projects are copied into their recipe and review saves there");
outerTabs.SelectedIndex=0;outerTabs.SelectedIndex=6;await Task.Delay(80);
Check(ReferenceEquals(trainer,trainingEditorVm.TrainingView!.Model)&&trainer.SelectedFrame?.Box is { Length:4 },"Switching editor tabs preserves annotation state");
@ -264,7 +265,10 @@ internal static class Smoke
var drawing=new Window {Width=400,Height=400,Content=canvas};drawing.Show();await Task.Delay(60);
drawing.MouseDown(new Point(300,250),MouseButton.Left);drawing.MouseMove(new Point(100,150));drawing.MouseUp(new Point(100,150),MouseButton.Left);
Check(region.X1==50&&region.Y1==25&&region.X2==150&&region.Y2==75,"Actual ROI pointer drag excludes image letterboxing and supports reverse drawing");
double[]? drawn=null;var annotation=new AnnotationCanvas {Source=image};annotation.BoxDrawn+=box=>drawn=box;drawing.Content=annotation;await Task.Delay(60);
double[]? drawn=null;var annotation=new AnnotationCanvas {Source=image,TargetName="目标零件 · 支架"};annotation.BoxDrawn+=box=>drawn=box;drawing.Content=annotation;await Task.Delay(60);
drawing.MouseDown(new Point(100,150),MouseButton.Left);drawing.MouseMove(new Point(300,250));Save(drawing,"annotation-target-drag");drawing.MouseUp(new Point(300,250),MouseButton.Left);
annotation.Box=drawn;Save(drawing,"annotation-target-saved");
annotation.Box=[0,0,100,60];Save(drawing,"annotation-target-edge");annotation.Box=null;
drawing.MouseDown(new Point(300,250),MouseButton.Left);drawing.MouseMove(new Point(100,150));drawing.MouseUp(new Point(100,150),MouseButton.Left);
Check(drawn is {Length:4}&&drawn.SequenceEqual(new double[]{50,25,150,75}),"Actual annotation drag preserves original-image box coordinates");
drawn=null;