ToolPolygon.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. namespace DrawTools
  5. {
  6. /// <summary>
  7. /// Polygon tool
  8. /// </summary>
  9. class ToolPolygon : DrawTools.ToolObject
  10. {
  11. public ToolPolygon()
  12. {
  13. Cursor = new Cursor(GetType(), "Pencil.cur");
  14. }
  15. private int lastX;
  16. private int lastY;
  17. private DrawPolygon newPolygon;
  18. private const int minDistance = 15*15;
  19. /// <summary>
  20. /// Left nouse button is pressed
  21. /// </summary>
  22. /// <param name="drawArea"></param>
  23. /// <param name="e"></param>
  24. public override void OnMouseDown(DrawArea drawArea, MouseEventArgs e)
  25. {
  26. // Create new polygon, add it to the list
  27. // and keep reference to it
  28. newPolygon = new DrawPolygon(e.X, e.Y, e.X + 1, e.Y + 1);
  29. AddNewObject(drawArea, newPolygon);
  30. lastX = e.X;
  31. lastY = e.Y;
  32. }
  33. /// <summary>
  34. /// Mouse move - resize new polygon
  35. /// </summary>
  36. /// <param name="drawArea"></param>
  37. /// <param name="e"></param>
  38. public override void OnMouseMove(DrawArea drawArea, MouseEventArgs e)
  39. {
  40. drawArea.Cursor = Cursor;
  41. if ( e.Button != MouseButtons.Left )
  42. return;
  43. if ( newPolygon == null )
  44. return; // precaution
  45. Point point = new Point(e.X, e.Y);
  46. int distance = (e.X - lastX)*(e.X - lastX) + (e.Y - lastY)*(e.Y - lastY);
  47. if ( distance < minDistance )
  48. {
  49. // Distance between last two points is less than minimum -
  50. // move last point
  51. newPolygon.MoveHandleTo(point, newPolygon.HandleCount);
  52. }
  53. else
  54. {
  55. // Add new point
  56. newPolygon.AddPoint(point);
  57. lastX = e.X;
  58. lastY = e.Y;
  59. }
  60. drawArea.Refresh();
  61. }
  62. public override void OnMouseUp(DrawArea drawArea, MouseEventArgs e)
  63. {
  64. newPolygon = null;
  65. base.OnMouseUp (drawArea, e);
  66. }
  67. }
  68. }