DimensionObject.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. public void SetState(ImageEditorState state)
  50. {
  51. _state = state;
  52. var p1 = new Vector(Point1.X, Point1.Y);
  53. var p2 = new Vector(Point2.X, Point2.Y);
  54. var mainVec = p2 - p1;
  55. var length = mainVec.Length;
  56. if(length == 0)
  57. {
  58. mainVec = new Vector(0, 0);
  59. }
  60. else
  61. {
  62. mainVec /= length;
  63. }
  64. var perpVec = new Vector(mainVec.Y, -mainVec.X);
  65. var arrowLength = LineThickness * 2;
  66. var offset = perpVec * Offset;
  67. var thumbSize = 30;
  68. if(Thumb is not null)
  69. {
  70. Thumb.RenderTransform = new TransformGroup
  71. {
  72. Children = [
  73. new RotateTransform(Math.Atan2(mainVec.Y, mainVec.X) * 180 / Math.PI),
  74. new ScaleTransform(1 / _state.ScaleFactor, 1 / _state.ScaleFactor)
  75. ]
  76. };
  77. Thumb.BorderThickness = new(1);
  78. Thumb.Width = thumbSize;
  79. Thumb.Height = thumbSize;
  80. var thumbPos = p1 + offset + mainVec * length / 2;
  81. Canvas.SetLeft(Thumb, thumbPos.X - thumbSize / 2);
  82. Canvas.SetTop(Thumb, thumbPos.Y - thumbSize / 2);
  83. }
  84. }
  85. public void Update()
  86. {
  87. Control.Children.RemoveAll(Control.Children.Where(x => !(x is Thumb)));
  88. Point Point(Vector v) => new(v.X, v.Y);
  89. var p1 = new Vector(Point1.X, Point1.Y);
  90. var p2 = new Vector(Point2.X, Point2.Y);
  91. var mainVec = p2 - p1;
  92. var length = mainVec.Length;
  93. if(length == 0)
  94. {
  95. mainVec = new Vector(0, 0);
  96. }
  97. else
  98. {
  99. mainVec /= length;
  100. }
  101. var perpVec = new Vector(mainVec.Y, -mainVec.X);
  102. var arrowLength = LineThickness * 2;
  103. var offset = perpVec * Offset;
  104. var mainLines = new List<Line>()
  105. {
  106. new()
  107. {
  108. StartPoint = Point(p1 + offset),
  109. EndPoint = Point(p2 + offset),
  110. StrokeLineCap = PenLineCap.Round
  111. },
  112. new()
  113. {
  114. StartPoint = Point(p1 + offset),
  115. EndPoint = Point(p1 + mainVec * arrowLength + perpVec * arrowLength + offset),
  116. StrokeLineCap = PenLineCap.Square
  117. },
  118. new()
  119. {
  120. StartPoint = Point(p1 + offset),
  121. EndPoint = Point(p1 + mainVec * arrowLength - perpVec * arrowLength + offset),
  122. StrokeLineCap = PenLineCap.Square
  123. },
  124. new()
  125. {
  126. StartPoint = Point(p2 + offset),
  127. EndPoint = Point(p2 - mainVec * arrowLength + perpVec * arrowLength + offset),
  128. StrokeLineCap = PenLineCap.Square
  129. },
  130. new()
  131. {
  132. StartPoint = Point(p2 + offset),
  133. EndPoint = Point(p2 - mainVec * arrowLength - perpVec * arrowLength + offset),
  134. StrokeLineCap = PenLineCap.Square
  135. }
  136. };
  137. var dotLines = new List<Line>()
  138. {
  139. new()
  140. {
  141. StartPoint = Point(p1),
  142. EndPoint = Point(p1 + offset)
  143. },
  144. new()
  145. {
  146. StartPoint = Point(p2),
  147. EndPoint = Point(p2 + offset)
  148. },
  149. };
  150. foreach (var line in dotLines)
  151. {
  152. line.StrokeThickness = LineThickness;
  153. line.Stroke = PrimaryBrush;
  154. line.StrokeDashArray = [2, 2];
  155. Control.Children.Add(line);
  156. }
  157. foreach (var line in mainLines)
  158. {
  159. line.StrokeThickness = LineThickness;
  160. line.Stroke = PrimaryBrush;
  161. Control.Children.Add(line);
  162. }
  163. var textHeight = 20;
  164. var text = new TextBlock
  165. {
  166. Text = Text,
  167. FontSize = textHeight,
  168. TextAlignment = TextAlignment.Center,
  169. Width = length,
  170. Foreground = PrimaryBrush
  171. };
  172. text.RenderTransform = new RotateTransform(Math.Atan2(mainVec.Y, mainVec.X) * 180 / Math.PI);
  173. var textPos = p1 + offset + mainVec * length / 2 + perpVec * (textHeight / 2 + 5);
  174. Canvas.SetLeft(text, textPos.X - length / 2);
  175. Canvas.SetTop(text, textPos.Y - textHeight / 2);
  176. Control.Children.Add(text);
  177. var thumbSize = 30;
  178. if(Thumb is null && _active && length > 0)
  179. {
  180. Thumb = new();
  181. Thumb.Cursor = new(StandardCursorType.SizeAll);
  182. Thumb.DragDelta += Thumb_DragDelta;
  183. Thumb.ZIndex = 1;
  184. Control.Children.Add(Thumb);
  185. }
  186. if(Thumb is not null)
  187. {
  188. Thumb.RenderTransform = new TransformGroup
  189. {
  190. Children = [
  191. new RotateTransform(Math.Atan2(mainVec.Y, mainVec.X) * 180 / Math.PI),
  192. new ScaleTransform(1 / _state.ScaleFactor, 1 / _state.ScaleFactor)
  193. ]
  194. };
  195. Thumb.BorderThickness = new(1);
  196. Thumb.Width = thumbSize;
  197. Thumb.Height = thumbSize;
  198. var thumbPos = p1 + offset + mainVec * length / 2;
  199. Canvas.SetLeft(Thumb, thumbPos.X - thumbSize / 2);
  200. Canvas.SetTop(Thumb, thumbPos.Y - thumbSize / 2);
  201. }
  202. }
  203. private Vector Rotate(Vector v, double angle)
  204. {
  205. angle = Math.Atan2(v.Y, v.X) + angle;
  206. var length = v.Length;
  207. return new Vector(
  208. length * Math.Cos(angle),
  209. length * Math.Sin(angle));
  210. }
  211. private void Thumb_DragDelta(object? sender, VectorEventArgs e)
  212. {
  213. var main = GetMainVec();
  214. var v = Vector.Dot(Rotate(e.Vector, Math.Atan2(main.Y, main.X)), GetPerpVec()) / _state.ScaleFactor;
  215. Offset += v;
  216. Update();
  217. }
  218. public void SetActive(bool active)
  219. {
  220. _active = active;
  221. if(Thumb is not null)
  222. {
  223. Control.Children.Remove(Thumb);
  224. Thumb = null;
  225. }
  226. }
  227. }