ToolPointer.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. using PaintDotNet.Annotation.Command;
  2. using PaintDotNet.Annotation.Enum;
  3. using PaintDotNet.Annotation.FieldView;
  4. using PaintDotNet.Annotation.Label;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Reflection;
  9. using System.Windows.Forms;
  10. namespace PaintDotNet.Annotation
  11. {
  12. /// <summary>
  13. /// 通用->选择功能->需要满足可选择标注、测量、视场等功能
  14. /// </summary>
  15. public class ToolPointer : Tool
  16. {
  17. private enum SelectionMode
  18. {
  19. None,
  20. NetSelection, // group selection is active
  21. Move, // object(s) are moves
  22. Size // object is resized
  23. }
  24. private static SelectionMode selectMode = SelectionMode.None;
  25. // Object which is currently resized:
  26. private static DrawObject resizedObject;
  27. private static int resizedObjectHandle;
  28. // Keep state about last and current pointscroll (used to move and resize objects)
  29. private static Point lastPoint = new Point(0, 0);
  30. private static Point startPoint = new Point(0, 0);
  31. private static CommandChangeState commandChangeState;
  32. private static bool wasMove;
  33. private static RichTextBox lab;
  34. //private static NumericUpDown numLab;
  35. private static LabelUsedDialog labelUsedDialog;
  36. private static ISurfaceBox areaNow;
  37. private static bool isModifyTextString = false;
  38. //private static bool isModifyWorkType = false;
  39. private static DrawObject dObject;
  40. public static EventHandler GetMouseLeftClickPoint;
  41. /// <summary>
  42. /// Left mouse button is pressed
  43. /// </summary>
  44. /// <param name="drawArea"></param>
  45. /// <param name="e"></param>
  46. public static void OnMouseDown(ISurfaceBox drawArea, MouseEventArgs e)
  47. {
  48. areaNow = drawArea;
  49. if (isModifyTextString && dObject != null)
  50. {
  51. dObject.textboxMessage = lab.Text;
  52. drawArea.Controls.Remove(lab);
  53. drawArea.Refresh();
  54. isModifyTextString = false;
  55. }
  56. //if (isModifyWorkType && dObject != null)
  57. //{
  58. // if(numLab.Value > 0)
  59. // dObject.textboxMessage = numLab.Value.ToString();
  60. // drawArea.Controls.Remove(numLab);
  61. // drawArea.Refresh();
  62. // isModifyWorkType = false;
  63. //}
  64. Point pointscroll = GetEventPointInArea(drawArea, e.Location);
  65. if (e.Button == MouseButtons.Left)
  66. {
  67. for (int i = 0; i < drawArea.GraphicsList.Count; i++)
  68. {
  69. drawArea.GraphicsList[i].MouseDownPoint(pointscroll);
  70. }
  71. }
  72. commandChangeState = null;
  73. wasMove = false;
  74. selectMode = SelectionMode.None;
  75. // Test for resizing (only if control is selected, cursor is on the handle)
  76. foreach (DrawObject o in drawArea.GraphicsList.Selection)
  77. {
  78. if (!o.isLocked)
  79. {
  80. int handleNumber = o.HitTest(pointscroll);
  81. if (handleNumber > 0)
  82. {
  83. selectMode = SelectionMode.Size;
  84. // keep resized object in class member
  85. resizedObject = o;
  86. resizedObjectHandle = handleNumber;
  87. // Since we want to resize only one object, unselect all other objects
  88. drawArea.GraphicsList.UnselectAll();
  89. o.Selected = true;
  90. commandChangeState = new CommandChangeState(drawArea.GraphicsList);
  91. drawArea.GraphicsList.Dirty = true;
  92. }
  93. }
  94. }
  95. // Test for move (cursor is on the object)
  96. if (selectMode == SelectionMode.None)
  97. {
  98. int n1 = drawArea.GraphicsList.Count;
  99. DrawObject o = null;
  100. for (int i = 0; i < n1; i++)
  101. {
  102. if (drawArea.GraphicsList[i].HitTest(pointscroll) == 0)
  103. {
  104. o = drawArea.GraphicsList[i];
  105. break;
  106. }
  107. }
  108. if (o != null)
  109. {
  110. selectMode = SelectionMode.Move;
  111. // Unselect all if Ctrl is not pressed and clicked object is not selected yet
  112. if ((Control.ModifierKeys & Keys.Control) == 0 && !o.Selected)
  113. drawArea.GraphicsList.UnselectAll();
  114. // Select clicked object
  115. if (!o.isLocked)
  116. {
  117. o.Selected = true;
  118. }
  119. commandChangeState = new CommandChangeState(drawArea.GraphicsList);
  120. drawArea.Cursor = Cursors.SizeAll;
  121. drawArea.GraphicsList.Dirty = true;
  122. }
  123. }
  124. // Net selection
  125. if (selectMode == SelectionMode.None)
  126. {
  127. // click on background
  128. if ((Control.ModifierKeys & Keys.Control) == 0)
  129. drawArea.GraphicsList.UnselectAll();
  130. selectMode = SelectionMode.NetSelection;
  131. }
  132. lastPoint.X = e.X;
  133. lastPoint.Y = e.Y;
  134. startPoint.X = e.X;
  135. startPoint.Y = e.Y;
  136. //drawArea.Capture = true;
  137. drawArea.Refresh();
  138. if (selectMode == SelectionMode.NetSelection)
  139. {
  140. drawArea.DrawRectangleFlag = true;
  141. drawArea.DrawRectangle = new Rectangle(0, 0, 0, 0);
  142. // Draw selection rectangle in initial position
  143. /*ControlPaint.DrawReversibleFrame(
  144. drawArea.RectangleToScreen(DrawObject.GetNormalizedRectangle(startPoint, lastPoint)),
  145. Color.Black,
  146. FrameStyle.Dashed);*/
  147. }
  148. OnMouseDownOne(drawArea, e);
  149. if (e.Button == MouseButtons.Right)
  150. {
  151. int selectCount = 0;
  152. for (int i = 0; i < drawArea.GraphicsList.Count; i++)
  153. {
  154. if (drawArea.GraphicsList[i].Selected)
  155. {
  156. selectCount++;
  157. if (drawArea.GraphicsList[i].objectType == DrawClass.Measure)
  158. areaNow.ToolNumber = 1;
  159. else if (drawArea.GraphicsList[i].objectType == DrawClass.Label)
  160. areaNow.ToolNumber = 2;
  161. }
  162. }
  163. if (selectCount == 0)
  164. areaNow.ToolNumber = 0;
  165. }
  166. }
  167. /// <summary>
  168. /// Mouse is moved.
  169. /// None button is pressed, or left button is pressed.
  170. /// </summary>
  171. /// <param name="drawArea"></param>
  172. /// <param name="e"></param>
  173. public static void OnMouseMove(ISurfaceBox drawArea, MouseEventArgs e)
  174. {
  175. Point pointscroll = GetEventPointInArea(drawArea, e.Location);
  176. Point oldPoint = lastPoint;
  177. wasMove = true;
  178. // set cursor when mouse button is not pressed
  179. if (e.Button == MouseButtons.None)
  180. {
  181. Cursor cursor = null;
  182. for (int i = 0; i < drawArea.GraphicsList.Count; i++)
  183. {
  184. int n = drawArea.GraphicsList[i].HitTest(pointscroll);
  185. if (n > 0)
  186. {
  187. cursor = drawArea.GraphicsList[i].GetHandleCursor(n);
  188. break;
  189. }
  190. }
  191. if (cursor == null)
  192. cursor = Cursors.Default;
  193. drawArea.Cursor = cursor;
  194. return;
  195. }
  196. if (e.Button != MouseButtons.Left)
  197. return;
  198. /// Left button is pressed
  199. // Find difference between previous and current position
  200. int dx = e.X - lastPoint.X;
  201. int dy = e.Y - lastPoint.Y;
  202. lastPoint.X = e.X;
  203. lastPoint.Y = e.Y;
  204. // resize
  205. if (selectMode == SelectionMode.Size)
  206. {
  207. if (resizedObject != null)
  208. {
  209. resizedObject.MoveHandleTo(pointscroll, resizedObjectHandle);
  210. drawArea.SetDirty();
  211. drawArea.Refresh();
  212. drawArea.GraphicsList.Dirty = true;
  213. }
  214. }
  215. // move
  216. if (selectMode == SelectionMode.Move)
  217. {
  218. foreach (DrawObject o in drawArea.GraphicsList.Selection)
  219. {
  220. if (o is ViewBase)
  221. {
  222. var x = drawArea.UnscaleScalar(dx);
  223. var y = drawArea.UnscaleScalar(dy);
  224. o.Move(x, y);
  225. }
  226. else
  227. o.Move(dx, dy);
  228. }
  229. drawArea.Cursor = Cursors.SizeAll;
  230. drawArea.SetDirty();
  231. drawArea.Refresh();
  232. drawArea.GraphicsList.Dirty = true;
  233. }
  234. if (selectMode == SelectionMode.NetSelection)
  235. {
  236. drawArea.DrawRectangle = DrawObject.GetNormalizedRectangle(startPoint, new Point(e.X, e.Y));
  237. drawArea.Refresh();
  238. /*// Remove old selection rectangle
  239. ControlPaint.DrawReversibleFrame(
  240. drawArea.RectangleToScreen(DrawObject.GetNormalizedRectangle(startPoint, oldPoint)),
  241. Color.Black,
  242. FrameStyle.Dashed);
  243. // Draw new selection rectangle
  244. ControlPaint.DrawReversibleFrame(
  245. drawArea.RectangleToScreen(DrawObject.GetNormalizedRectangle(startPoint, new Point(e.X, e.Y))),
  246. Color.Black,
  247. FrameStyle.Dashed);*/
  248. return;
  249. }
  250. OnMouseMoveOne(drawArea, e);
  251. }
  252. /// <summary>
  253. /// Right mouse button is released
  254. /// </summary>
  255. /// <param name="drawArea"></param>
  256. /// <param name="e"></param>
  257. public static void OnMouseUp(ISurfaceBox drawArea, MouseEventArgs e)
  258. {
  259. if (e.Button == MouseButtons.Left)
  260. {
  261. for (int i = 0; i < drawArea.GraphicsList.Count; i++)
  262. {
  263. drawArea.GraphicsList[i].MouseUp(false);
  264. }
  265. }
  266. if (selectMode == SelectionMode.NetSelection)
  267. {
  268. drawArea.DrawRectangleFlag = false;
  269. /*// Remove old selection rectangle
  270. ControlPaint.DrawReversibleFrame(
  271. drawArea.RectangleToScreen(DrawObject.GetNormalizedRectangle(startPoint, lastPoint)),
  272. Color.Black,
  273. FrameStyle.Dashed);*/
  274. //Point pointscroll = GetEventPointInArea(drawArea, e);
  275. // Make group selection
  276. drawArea.GraphicsList.SelectInRectangle(
  277. DrawRectangle.GetNormalizedRectangle(GetEventPointInArea(drawArea, new Point(startPoint.X, startPoint.Y)),
  278. GetEventPointInArea(drawArea, new Point(lastPoint.X, lastPoint.Y))));
  279. selectMode = SelectionMode.None;
  280. }
  281. if (resizedObject != null)
  282. {
  283. // after resizing
  284. resizedObject.Normalize();
  285. resizedObject = null;
  286. }
  287. drawArea.Capture = false;
  288. drawArea.Refresh();
  289. drawArea.GraphicsList.Dirty = true;
  290. if (commandChangeState != null && wasMove)
  291. {
  292. // Keep state after moving/resizing and add command to history
  293. commandChangeState.NewState(drawArea.GraphicsList);
  294. drawArea.AddCommandToHistory(commandChangeState);
  295. commandChangeState = null;
  296. }
  297. OnMouseUpOne(drawArea, e);
  298. }
  299. /// <summary>
  300. /// Remove selected object
  301. /// </summary>
  302. /// <param name="drawArea"></param>
  303. /// <param name="e"></param>
  304. public static void OnDelKeyDown(ISurfaceBox drawArea, MouseEventArgs e)
  305. {
  306. if (drawArea != null && drawArea.GraphicsList != null && drawArea.GraphicsList.Count > 0)
  307. {
  308. for (int i = drawArea.GraphicsList.Count - 1; i >= 0; i--)
  309. {
  310. if (drawArea.GraphicsList[i].Selected == true)
  311. {
  312. //对数字标记做单独处理
  313. if (drawArea.GraphicsList[i].drawToolType == DrawToolType.DrawNumberMark)
  314. {
  315. DrawNumberMark drawNumberMark = (DrawNumberMark)drawArea.GraphicsList[i];
  316. List<DrawObject> objList = drawArea.GraphicsList.GetDrawToolTypeList(DrawToolType.DrawNumberMark);
  317. if (objList != null && objList.Count > 0)
  318. {
  319. for (int j = 0; j < objList.Count; j++)
  320. {
  321. DrawNumberMark modifyNum = (DrawNumberMark)objList[j];
  322. if (modifyNum.nowNum > drawNumberMark.nowNum)
  323. {
  324. modifyNum.nowNum -= 1;
  325. }
  326. }
  327. }
  328. }
  329. drawArea.GraphicsList.RemoveObj(drawArea.GraphicsList[i]);
  330. }
  331. }
  332. drawArea.Refresh();
  333. }
  334. }
  335. public static void OnMouseLeftDoubleClick(ISurfaceBox drawArea, MouseEventArgs e)
  336. {
  337. if (drawArea != null && drawArea.GraphicsList != null && drawArea.GraphicsList.Count > 0)
  338. {
  339. areaNow = drawArea;
  340. if (drawArea.GraphicsList.IsExsitSpecificObject(DrawToolType.DrawTextString))
  341. {
  342. Point pointscroll = GetEventPointInArea(drawArea, e.Location);
  343. List<DrawObject> objList = drawArea.GraphicsList.GetDrawToolTypeList(DrawToolType.DrawTextString);
  344. Point temp = drawArea.GetCalcOriginPoint();
  345. for (int i = 0; i < objList.Count; i++)
  346. {
  347. if (objList[i].Rectangle.Contains(pointscroll))
  348. {
  349. dObject = objList[i];
  350. lab = new RichTextBox();
  351. lab.Width = (int)(objList[i].Rectangle.Width * drawArea.ScaleRatio) + 1;
  352. lab.Height = (int)(objList[i].Rectangle.Height * drawArea.ScaleRatio) + 1;
  353. lab.BackColor = Color.White;
  354. lab.Cursor = Cursors.Default;
  355. lab.LostFocus += new EventHandler(OnTextBoxLostFocus);
  356. lab.Location = new Point((int)(objList[i].Rectangle.X * drawArea.ScaleRatio) + temp.X + 16, (int)(objList[i].Rectangle.Y * drawArea.ScaleRatio) + temp.Y + 16);
  357. lab.Text = objList[i].textboxMessage;
  358. lab.ScrollBars = 0;
  359. drawArea.Controls.Add(lab);
  360. lab.Focus();
  361. lab.BringToFront();
  362. isModifyTextString = true;
  363. break;
  364. }
  365. }
  366. }
  367. if (drawArea.GraphicsList.IsExsitSpecificObject(DrawToolType.DrawWorkType))
  368. {
  369. Point pointscroll = GetEventPointInArea(drawArea, e.Location);
  370. List<DrawObject> objList = drawArea.GraphicsList.GetDrawToolTypeList(DrawToolType.DrawWorkType);
  371. Point temp = drawArea.GetCalcOriginPoint();
  372. for (int i = 0; i < objList.Count; i++)
  373. {
  374. if (objList[i].Rectangle.Contains(pointscroll))
  375. {
  376. dObject = objList[i];
  377. labelUsedDialog = new LabelUsedDialog();
  378. labelUsedDialog.Text = "工字线距离";
  379. labelUsedDialog.textBox1.Text = objList[i].textboxMessage;
  380. labelUsedDialog.button1.Click += new EventHandler(LabelUsedDialogButton1_Click);
  381. labelUsedDialog.textBox1.KeyPress += new KeyPressEventHandler(TextBox1_KeyPress);
  382. labelUsedDialog.StartPosition = FormStartPosition.CenterParent;
  383. labelUsedDialog.ShowDialog();
  384. //numLab = new NumericUpDown();
  385. //numLab.Cursor = Cursors.Default;
  386. //numLab.LostFocus += new EventHandler(OnNumbericLostFocus);
  387. ////numLab.Location = new Point(pointscroll.X + temp.X, pointscroll.Y + temp.Y);
  388. ////numLab.Location = new Point(e.X, e.Y);
  389. //numLab.Location = new Point(temp.X + (int)(pointscroll.X * drawArea.ScaleRatio) + 16, temp.Y + (int)(pointscroll.Y * drawArea.ScaleRatio) + 16);
  390. //numLab.Maximum = 9999;
  391. //numLab.Minimum = 0;
  392. //numLab.DecimalPlaces = 2;
  393. //numLab.Increment = 0.5M;
  394. //numLab.Value = decimal.Parse(objList[i].textboxMessage);
  395. //drawArea.Controls.Add(numLab);
  396. //numLab.Focus();
  397. //numLab.BringToFront();
  398. //isModifyWorkType = true;
  399. break;
  400. }
  401. }
  402. }
  403. }
  404. //if (drawArea.ViewMoveOnMouseLeftDoubleClickEnable)
  405. //{
  406. if (GetMouseLeftClickPoint != null)
  407. {
  408. PointF pointscroll = drawArea.GetRulerPointInPanel(e.Location);
  409. GetMouseLeftClickPoint(pointscroll, null);
  410. }
  411. //}
  412. }
  413. private static void LabelUsedDialogButton1_Click(object sender, EventArgs e)
  414. {
  415. if (string.IsNullOrEmpty(labelUsedDialog.textBox1.Text))
  416. {
  417. MessageBox.Show(PdnResources.GetString("Menu.Thedistancevalue.text"));
  418. return;
  419. }
  420. else
  421. {
  422. dObject.textboxMessage = labelUsedDialog.textBox1.Text;
  423. areaNow.Refresh();
  424. labelUsedDialog.Close();
  425. }
  426. }
  427. private static void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
  428. {
  429. TextBox textBox1 = (TextBox)sender;
  430. //判断按键是不是要输入的类型
  431. if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
  432. e.Handled = true;
  433. //小数点的处理
  434. if ((int)e.KeyChar == 46)//小数点
  435. {
  436. if (textBox1.Text.Length <= 0)
  437. e.Handled = true; //小数点不能在第一位
  438. else
  439. {
  440. float f;
  441. float oldf;
  442. bool b1 = false, b2 = false;
  443. b1 = float.TryParse(textBox1.Text, out oldf);
  444. b2 = float.TryParse(textBox1.Text + e.KeyChar.ToString(), out f);
  445. if (b2 == false)
  446. {
  447. if (b1 == true)
  448. e.Handled = true;
  449. else
  450. e.Handled = false;
  451. }
  452. }
  453. }
  454. }
  455. private static void OnTextBoxLostFocus(object sender, EventArgs e)
  456. {
  457. if (areaNow != null && lab != null)
  458. {
  459. dObject.textboxMessage = lab.Text;
  460. areaNow.Controls.Remove(lab);
  461. areaNow.Refresh();
  462. isModifyTextString = false;
  463. }
  464. }
  465. //private static void OnNumbericLostFocus(object sender, EventArgs e)
  466. //{
  467. // if (areaNow != null && numLab != null)
  468. // {
  469. // if (numLab.Value > 0)
  470. // dObject.textboxMessage = numLab.Value.ToString();
  471. // areaNow.Controls.Remove(numLab);
  472. // areaNow.Refresh();
  473. // isModifyWorkType = false;
  474. // }
  475. //}
  476. public static void OnMouseClick(ISurfaceBox surfacebox, MouseEventArgs e)
  477. {
  478. }
  479. public static void beginWithNewObject()
  480. {
  481. }
  482. }
  483. }