DimensionObject.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Primitives;
  4. using Avalonia.Controls.Shapes;
  5. using Avalonia.Input;
  6. using Avalonia.Media;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Xml;
  13. namespace InABox.Avalonia.Components.ImageEditing;
  14. internal class DimensionObject : IImageEditorStateObject
  15. {
  16. public IBrush? PrimaryBrush { get; set; }
  17. public Point Point1 { get; set; }
  18. public Point Point2 { get; set; }
  19. public double LineThickness { get; set; }
  20. public string Text { get; set; } = "";
  21. public double Offset { get; set; } = 10;
  22. public bool Complete { get; set; } = false;
  23. private Canvas Control = new();
  24. private Thumb? Thumb;
  25. private bool _active = true;
  26. private ImageEditorState _state = new(1.0);
  27. public Control GetControl() => Control;
  28. private Vector GetMainVec()
  29. {
  30. var p1 = new Vector(Point1.X, Point1.Y);
  31. var p2 = new Vector(Point2.X, Point2.Y);
  32. var mainVec = p2 - p1;
  33. var length = mainVec.Length;
  34. if(length == 0)
  35. {
  36. mainVec = new Vector(0, 0);
  37. }
  38. else
  39. {
  40. mainVec /= length;
  41. }
  42. return mainVec;
  43. }
  44. private Vector GetPerpVec()
  45. {
  46. var mainVec = GetMainVec();
  47. return new Vector(mainVec.Y, -mainVec.X);
  48. }
  49. private void CreateThumb()
  50. {
  51. var p1 = new Vector(Point1.X, Point1.Y);
  52. var p2 = new Vector(Point2.X, Point2.Y);
  53. var mainVec = p2 - p1;
  54. var length = mainVec.Length;
  55. if(length == 0)
  56. {
  57. mainVec = new Vector(0, 0);
  58. }
  59. else
  60. {
  61. mainVec /= length;
  62. }
  63. var perpVec = new Vector(mainVec.Y, -mainVec.X);
  64. var offset = perpVec * Offset;
  65. var thumbSize = 30;
  66. if(Thumb is not null)
  67. {
  68. Thumb.RenderTransform = new TransformGroup
  69. {
  70. Children = [
  71. new RotateTransform(Math.Atan2(mainVec.Y, mainVec.X) * 180 / Math.PI)
  72. ]
  73. };
  74. Thumb.BorderThickness = new(1);
  75. Thumb.Width = length;
  76. Thumb.Height = Math.Max(20, thumbSize / _state.ScaleFactor);
  77. var thumbPos = p1 + offset + mainVec * length / 2;
  78. Canvas.SetLeft(Thumb, thumbPos.X - Thumb.Width / 2);
  79. Canvas.SetTop(Thumb, thumbPos.Y - Thumb.Height / 2);
  80. }
  81. }
  82. public void SetState(ImageEditorState state)
  83. {
  84. _state = state;
  85. CreateThumb();
  86. }
  87. public void Update()
  88. {
  89. Control.Children.RemoveAll(Control.Children.Where(x => !(x is Thumb)));
  90. Point Point(Vector v) => new(v.X, v.Y);
  91. var p1 = new Vector(Point1.X, Point1.Y);
  92. var p2 = new Vector(Point2.X, Point2.Y);
  93. var mainVec = p2 - p1;
  94. var length = mainVec.Length;
  95. if(length == 0)
  96. {
  97. mainVec = new Vector(0, 0);
  98. }
  99. else
  100. {
  101. mainVec /= length;
  102. }
  103. var perpVec = new Vector(mainVec.Y, -mainVec.X);
  104. var arrowLength = LineThickness * 2;
  105. var offset = perpVec * Offset;
  106. var middle = p1 + offset + mainVec * length / 2;
  107. var line1 = new Line
  108. {
  109. StartPoint = Point(p1 + offset),
  110. EndPoint = Point(middle),
  111. StrokeLineCap = PenLineCap.Round
  112. };
  113. var line2 = new Line
  114. {
  115. StartPoint = Point(middle),
  116. EndPoint = Point(p2 + offset),
  117. StrokeLineCap = PenLineCap.Round
  118. };
  119. var mainLines = new List<Line>()
  120. {
  121. line1,
  122. line2,
  123. new()
  124. {
  125. StartPoint = Point(p1 + offset),
  126. EndPoint = Point(p1 + mainVec * arrowLength + perpVec * arrowLength + offset),
  127. StrokeLineCap = PenLineCap.Square
  128. },
  129. new()
  130. {
  131. StartPoint = Point(p1 + offset),
  132. EndPoint = Point(p1 + mainVec * arrowLength - perpVec * arrowLength + offset),
  133. StrokeLineCap = PenLineCap.Square
  134. },
  135. new()
  136. {
  137. StartPoint = Point(p2 + offset),
  138. EndPoint = Point(p2 - mainVec * arrowLength + perpVec * arrowLength + offset),
  139. StrokeLineCap = PenLineCap.Square
  140. },
  141. new()
  142. {
  143. StartPoint = Point(p2 + offset),
  144. EndPoint = Point(p2 - mainVec * arrowLength - perpVec * arrowLength + offset),
  145. StrokeLineCap = PenLineCap.Square
  146. }
  147. };
  148. var dotLines = new List<Line>()
  149. {
  150. new()
  151. {
  152. StartPoint = Point(p1),
  153. EndPoint = Point(p1 + offset)
  154. },
  155. new()
  156. {
  157. StartPoint = Point(p2),
  158. EndPoint = Point(p2 + offset)
  159. },
  160. };
  161. foreach (var line in dotLines)
  162. {
  163. line.StrokeThickness = LineThickness;
  164. line.Stroke = PrimaryBrush;
  165. line.StrokeDashArray = [2, 2];
  166. Control.Children.Add(line);
  167. }
  168. foreach (var line in mainLines)
  169. {
  170. line.StrokeThickness = LineThickness;
  171. line.Stroke = PrimaryBrush;
  172. Control.Children.Add(line);
  173. }
  174. var textHeight = 20;
  175. var text = new TextBlock
  176. {
  177. Text = Text,
  178. FontSize = textHeight,
  179. TextAlignment = TextAlignment.Center,
  180. Foreground = PrimaryBrush
  181. };
  182. text.RenderTransform = new RotateTransform(Math.Atan2(mainVec.Y, mainVec.X) * 180 / Math.PI);
  183. var textPos = p1 + offset + mainVec * length / 2;
  184. Canvas.SetLeft(text, textPos.X);
  185. Canvas.SetTop(text, textPos.Y);
  186. text.SizeChanged += (o, e) =>
  187. {
  188. line1.EndPoint = Point(middle - mainVec * (text.Bounds.Width / 2 + 5));
  189. line2.StartPoint = Point(middle + mainVec * (text.Bounds.Width / 2 + 5));
  190. var topCorner = textPos - mainVec * text.Bounds.Width / 2 + perpVec * text.Bounds.Height / 2;
  191. Canvas.SetLeft(text, textPos.X - text.Bounds.Width / 2);
  192. Canvas.SetTop(text, textPos.Y - text.Bounds.Height / 2);
  193. };
  194. Control.Children.Add(text);
  195. if(Thumb is null && _active && length > 0)
  196. {
  197. Thumb = new();
  198. Thumb.Cursor = new(StandardCursorType.SizeAll);
  199. Thumb.DragDelta += Thumb_DragDelta;
  200. Thumb.ZIndex = 1;
  201. Control.Children.Add(Thumb);
  202. }
  203. CreateThumb();
  204. }
  205. private Vector Rotate(Vector v, double angle)
  206. {
  207. angle = Math.Atan2(v.Y, v.X) + angle;
  208. var length = v.Length;
  209. return new Vector(
  210. length * Math.Cos(angle),
  211. length * Math.Sin(angle));
  212. }
  213. private void Thumb_DragDelta(object? sender, VectorEventArgs e)
  214. {
  215. var main = GetMainVec();
  216. var v = Vector.Dot(Rotate(e.Vector, Math.Atan2(main.Y, main.X)), GetPerpVec()) / _state.ScaleFactor;
  217. Offset += v;
  218. Update();
  219. }
  220. public void SetActive(bool active)
  221. {
  222. _active = active;
  223. if(Thumb is not null)
  224. {
  225. Control.Children.Remove(Thumb);
  226. Thumb = null;
  227. }
  228. }
  229. }