mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 22:16:30 +00:00
109 lines
3.9 KiB
C#
109 lines
3.9 KiB
C#
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Shapes;
|
|
using Azaion.Annotator.DTO;
|
|
|
|
namespace Azaion.Annotator.Controls;
|
|
|
|
public class AnnotationControl : Border
|
|
{
|
|
private readonly Action<object, MouseButtonEventArgs> _resizeStart;
|
|
private const double RESIZE_RECT_SIZE = 9;
|
|
|
|
private readonly Grid _grid;
|
|
private readonly TextBlock _classNameLabel;
|
|
public TimeSpan Time { get; set; }
|
|
|
|
private AnnotationClass _annotationClass = null!;
|
|
public AnnotationClass AnnotationClass
|
|
{
|
|
get => _annotationClass;
|
|
set
|
|
{
|
|
_grid.Background = value.ColorBrush;
|
|
_classNameLabel.Text = value.Name;
|
|
_annotationClass = value;
|
|
}
|
|
}
|
|
|
|
private readonly Rectangle _selectionFrame;
|
|
|
|
private bool _isSelected;
|
|
public bool IsSelected
|
|
{
|
|
get => _isSelected;
|
|
set
|
|
{
|
|
_selectionFrame.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
|
|
_isSelected = value;
|
|
}
|
|
}
|
|
|
|
public AnnotationControl(AnnotationClass annotationClass, TimeSpan time, Action<object, MouseButtonEventArgs> resizeStart)
|
|
{
|
|
Time = time;
|
|
_resizeStart = resizeStart;
|
|
_classNameLabel = new TextBlock
|
|
{
|
|
Text = annotationClass.Name,
|
|
HorizontalAlignment = HorizontalAlignment.Center,
|
|
VerticalAlignment = VerticalAlignment.Top,
|
|
Margin = new Thickness(0, 15, 0, 0),
|
|
FontSize = 14,
|
|
Cursor = Cursors.SizeAll
|
|
};
|
|
_selectionFrame = new Rectangle
|
|
{
|
|
HorizontalAlignment = HorizontalAlignment.Stretch,
|
|
VerticalAlignment = VerticalAlignment.Stretch,
|
|
Stroke = new SolidColorBrush(Colors.Black),
|
|
StrokeThickness = 2,
|
|
Visibility = Visibility.Collapsed
|
|
};
|
|
|
|
_grid = new Grid
|
|
{
|
|
HorizontalAlignment = HorizontalAlignment.Stretch,
|
|
VerticalAlignment = VerticalAlignment.Stretch,
|
|
Children =
|
|
{
|
|
_selectionFrame,
|
|
_classNameLabel,
|
|
AddRect("rLT", HorizontalAlignment.Left, VerticalAlignment.Top, Cursors.SizeNWSE),
|
|
AddRect("rCT", HorizontalAlignment.Center, VerticalAlignment.Top, Cursors.SizeNS),
|
|
AddRect("rRT", HorizontalAlignment.Right, VerticalAlignment.Top, Cursors.SizeNESW),
|
|
AddRect("rLC", HorizontalAlignment.Left, VerticalAlignment.Center, Cursors.SizeWE),
|
|
AddRect("rRC", HorizontalAlignment.Right, VerticalAlignment.Center, Cursors.SizeWE),
|
|
AddRect("rLB", HorizontalAlignment.Left, VerticalAlignment.Bottom, Cursors.SizeNESW),
|
|
AddRect("rCB", HorizontalAlignment.Center, VerticalAlignment.Bottom, Cursors.SizeNS),
|
|
AddRect("rRB", HorizontalAlignment.Right, VerticalAlignment.Bottom, Cursors.SizeNWSE)
|
|
}
|
|
};
|
|
Child = _grid;
|
|
Cursor = Cursors.SizeAll;
|
|
AnnotationClass = annotationClass;
|
|
}
|
|
|
|
//small corners
|
|
private Rectangle AddRect(string name, HorizontalAlignment ha, VerticalAlignment va, Cursor crs)
|
|
{
|
|
var rect = new Rectangle() // small rectangles at the corners and sides
|
|
{
|
|
HorizontalAlignment = ha,
|
|
VerticalAlignment = va,
|
|
Width = RESIZE_RECT_SIZE,
|
|
Height = RESIZE_RECT_SIZE,
|
|
Stroke = new SolidColorBrush(Color.FromArgb(230, 40, 40, 40)), // small rectangles color
|
|
Fill = new SolidColorBrush(Color.FromArgb(1, 255, 255, 255)),
|
|
Cursor = crs,
|
|
Name = name,
|
|
};
|
|
rect.MouseDown += (sender, args) => _resizeStart(sender, args);
|
|
return rect;
|
|
}
|
|
|
|
public CanvasLabel Info => new(AnnotationClass.Id, Canvas.GetLeft(this), Canvas.GetTop(this), Width, Height);
|
|
}
|