|
| 1 | +using System.Linq; |
| 2 | +using System.Windows; |
| 3 | +using System.Windows.Controls; |
| 4 | +using System.Windows.Input; |
| 5 | +using System.Windows.Media; |
| 6 | +using System.Windows.Shapes; |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | +using Brush = System.Windows.Media.Brush; |
| 9 | +using Brushes = System.Windows.Media.Brushes; |
| 10 | +using Color = System.Windows.Media.Color; |
| 11 | +using ColorConverter = System.Windows.Media.ColorConverter; |
| 12 | +using Point = System.Windows.Point; |
| 13 | +using Vector = System.Windows.Vector; |
| 14 | + |
| 15 | +namespace GhostDraw.Tools; |
| 16 | + |
| 17 | +/// <summary> |
| 18 | +/// Arrow drawing tool - click two points to draw a line with an arrowhead at the end. |
| 19 | +/// </summary> |
| 20 | +public class ArrowTool(ILogger<ArrowTool> logger) : IDrawingTool |
| 21 | +{ |
| 22 | + private readonly ILogger<ArrowTool> _logger = logger; |
| 23 | + private Path? _currentPath; |
| 24 | + private Point? _startPoint; |
| 25 | + private bool _isCreatingArrow; |
| 26 | + private string _currentColor = "#FF0000"; |
| 27 | + private double _currentThickness = 3.0; |
| 28 | + |
| 29 | + public event EventHandler<DrawingActionCompletedEventArgs>? ActionCompleted; |
| 30 | + |
| 31 | + public void OnMouseDown(Point position, Canvas canvas) |
| 32 | + { |
| 33 | + if (!_isCreatingArrow) |
| 34 | + { |
| 35 | + StartNewArrow(position, canvas); |
| 36 | + } |
| 37 | + else |
| 38 | + { |
| 39 | + FinishArrow(position); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public void OnMouseMove(Point position, Canvas canvas, MouseButtonState leftButtonState) |
| 44 | + { |
| 45 | + if (_isCreatingArrow && _currentPath != null && _startPoint.HasValue) |
| 46 | + { |
| 47 | + _currentPath.Data = BuildArrowGeometry(_startPoint.Value, position, _currentThickness); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public void OnMouseUp(Point position, Canvas canvas) |
| 52 | + { |
| 53 | + // Arrow tool uses click-click, not click-drag |
| 54 | + } |
| 55 | + |
| 56 | + public void OnActivated() |
| 57 | + { |
| 58 | + _logger.LogDebug("Arrow tool activated"); |
| 59 | + } |
| 60 | + |
| 61 | + public void OnDeactivated() |
| 62 | + { |
| 63 | + _logger.LogDebug("Arrow tool deactivated"); |
| 64 | + _currentPath = null; |
| 65 | + _startPoint = null; |
| 66 | + _isCreatingArrow = false; |
| 67 | + } |
| 68 | + |
| 69 | + public void OnColorChanged(string colorHex) |
| 70 | + { |
| 71 | + _currentColor = colorHex; |
| 72 | + |
| 73 | + if (_currentPath != null) |
| 74 | + { |
| 75 | + var brush = CreateBrushFromHex(colorHex); |
| 76 | + _currentPath.Stroke = brush; |
| 77 | + _currentPath.Fill = brush; |
| 78 | + } |
| 79 | + |
| 80 | + _logger.LogDebug("Arrow color changed to {Color}", colorHex); |
| 81 | + } |
| 82 | + |
| 83 | + public void OnThicknessChanged(double thickness) |
| 84 | + { |
| 85 | + _currentThickness = thickness; |
| 86 | + |
| 87 | + if (_currentPath != null) |
| 88 | + { |
| 89 | + _currentPath.StrokeThickness = thickness; |
| 90 | + |
| 91 | + if (_startPoint.HasValue) |
| 92 | + { |
| 93 | + // Rebuild geometry so arrowhead scales with thickness |
| 94 | + var endPoint = GetCurrentEndPoint(_currentPath) ?? _startPoint.Value; |
| 95 | + _currentPath.Data = BuildArrowGeometry(_startPoint.Value, endPoint, thickness); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + _logger.LogDebug("Arrow thickness changed to {Thickness}", thickness); |
| 100 | + } |
| 101 | + |
| 102 | + public void Cancel(Canvas canvas) |
| 103 | + { |
| 104 | + if (_currentPath != null) |
| 105 | + { |
| 106 | + canvas.Children.Remove(_currentPath); |
| 107 | + _currentPath = null; |
| 108 | + _startPoint = null; |
| 109 | + _isCreatingArrow = false; |
| 110 | + _logger.LogDebug("In-progress arrow cancelled and removed"); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void StartNewArrow(Point startPoint, Canvas canvas) |
| 115 | + { |
| 116 | + _startPoint = startPoint; |
| 117 | + _isCreatingArrow = true; |
| 118 | + |
| 119 | + var brush = CreateBrushFromHex(_currentColor); |
| 120 | + |
| 121 | + _currentPath = new Path |
| 122 | + { |
| 123 | + Stroke = brush, |
| 124 | + Fill = brush, |
| 125 | + StrokeThickness = _currentThickness, |
| 126 | + StrokeStartLineCap = PenLineCap.Round, |
| 127 | + StrokeEndLineCap = PenLineCap.Round, |
| 128 | + StrokeLineJoin = PenLineJoin.Round, |
| 129 | + Data = BuildArrowGeometry(startPoint, startPoint, _currentThickness) |
| 130 | + }; |
| 131 | + |
| 132 | + canvas.Children.Add(_currentPath); |
| 133 | + _logger.LogInformation("Arrow started at ({X:F0}, {Y:F0})", startPoint.X, startPoint.Y); |
| 134 | + } |
| 135 | + |
| 136 | + private void FinishArrow(Point endPoint) |
| 137 | + { |
| 138 | + if (_currentPath != null && _startPoint.HasValue) |
| 139 | + { |
| 140 | + _currentPath.Data = BuildArrowGeometry(_startPoint.Value, endPoint, _currentThickness); |
| 141 | + _logger.LogInformation("Arrow finished at ({X:F0}, {Y:F0})", endPoint.X, endPoint.Y); |
| 142 | + |
| 143 | + ActionCompleted?.Invoke(this, new DrawingActionCompletedEventArgs(_currentPath)); |
| 144 | + } |
| 145 | + |
| 146 | + _currentPath = null; |
| 147 | + _startPoint = null; |
| 148 | + _isCreatingArrow = false; |
| 149 | + } |
| 150 | + |
| 151 | + private Geometry BuildArrowGeometry(Point start, Point end, double thickness) |
| 152 | + { |
| 153 | + var group = new GeometryGroup |
| 154 | + { |
| 155 | + FillRule = FillRule.Nonzero |
| 156 | + }; |
| 157 | + |
| 158 | + // Shaft |
| 159 | + group.Children.Add(new LineGeometry(start, end)); |
| 160 | + |
| 161 | + // Arrowhead |
| 162 | + var delta = end - start; |
| 163 | + if (delta.Length < 0.001) |
| 164 | + { |
| 165 | + return group; |
| 166 | + } |
| 167 | + |
| 168 | + Vector dir = delta; |
| 169 | + dir.Normalize(); |
| 170 | + |
| 171 | + // Perpendicular vector |
| 172 | + var perp = new Vector(-dir.Y, dir.X); |
| 173 | + |
| 174 | + double arrowLength = Math.Max(12.0, thickness * 4.0); |
| 175 | + double arrowWidth = Math.Max(8.0, thickness * 3.0); |
| 176 | + |
| 177 | + var tip = end; |
| 178 | + var baseCenter = end - dir * arrowLength; |
| 179 | + var left = baseCenter + perp * (arrowWidth / 2.0); |
| 180 | + var right = baseCenter - perp * (arrowWidth / 2.0); |
| 181 | + |
| 182 | + var figure = new PathFigure |
| 183 | + { |
| 184 | + StartPoint = tip, |
| 185 | + IsClosed = true, |
| 186 | + IsFilled = true |
| 187 | + }; |
| 188 | + figure.Segments.Add(new LineSegment(left, true)); |
| 189 | + figure.Segments.Add(new LineSegment(right, true)); |
| 190 | + |
| 191 | + group.Children.Add(new PathGeometry(new[] { figure })); |
| 192 | + |
| 193 | + return group; |
| 194 | + } |
| 195 | + |
| 196 | + private Brush CreateBrushFromHex(string colorHex) |
| 197 | + { |
| 198 | + try |
| 199 | + { |
| 200 | + return new SolidColorBrush((Color)ColorConverter.ConvertFromString(colorHex)); |
| 201 | + } |
| 202 | + catch (Exception ex) |
| 203 | + { |
| 204 | + _logger.LogWarning(ex, "Failed to parse brush color {Color}, using default red", colorHex); |
| 205 | + return Brushes.Red; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + private static Point? GetCurrentEndPoint(Path path) |
| 210 | + { |
| 211 | + if (path.Data is GeometryGroup group) |
| 212 | + { |
| 213 | + var line = group.Children.OfType<LineGeometry>().FirstOrDefault(); |
| 214 | + return line?.EndPoint; |
| 215 | + } |
| 216 | + |
| 217 | + return null; |
| 218 | + } |
| 219 | +} |
0 commit comments