ToolPointer.cs 21 KB

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