MagicWandTool.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using PaintDotNet.Measurement.HistoryMementos;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Windows.Forms;
  5. namespace PaintDotNet.Measurement.Tools
  6. {
  7. public class MagicWandTool : FloodToolBase
  8. {
  9. private Cursor cursorMouseUp;
  10. private Cursor cursorMouseUpMinus;
  11. private Cursor cursorMouseUpPlus;
  12. private CombineMode combineMode;
  13. private Cursor GetCursor(bool ctrlDown, bool altDown)
  14. {
  15. Cursor cursor;
  16. if (ctrlDown)
  17. {
  18. cursor = this.cursorMouseUpPlus;
  19. }
  20. else if (altDown)
  21. {
  22. cursor = this.cursorMouseUpMinus;
  23. }
  24. else
  25. {
  26. cursor = this.cursorMouseUp;
  27. }
  28. return cursor;
  29. }
  30. private Cursor GetCursor()
  31. {
  32. return GetCursor((ModifierKeys & Keys.Control) != 0, (ModifierKeys & Keys.Alt) != 0);
  33. }
  34. protected override void OnActivate()
  35. {
  36. DocumentWorkspace.SetEnableSelectionTinting(true);
  37. this.cursorMouseUp = new Cursor(PdnResources.GetResourceStream("Cursors.MagicWandToolCursor.cur"));
  38. this.cursorMouseUpMinus = new Cursor(PdnResources.GetResourceStream("Cursors.MagicWandToolCursorMinus.cur"));
  39. this.cursorMouseUpPlus = new Cursor(PdnResources.GetResourceStream("Cursors.MagicWandToolCursorPlus.cur"));
  40. this.Cursor = GetCursor();
  41. base.OnActivate();
  42. }
  43. protected override void OnDeactivate()
  44. {
  45. if (this.cursorMouseUp != null)
  46. {
  47. this.cursorMouseUp.Dispose();
  48. this.cursorMouseUp = null;
  49. }
  50. if (this.cursorMouseUpMinus != null)
  51. {
  52. this.cursorMouseUpMinus.Dispose();
  53. this.cursorMouseUpMinus = null;
  54. }
  55. if (this.cursorMouseUpPlus != null)
  56. {
  57. this.cursorMouseUpPlus.Dispose();
  58. this.cursorMouseUpPlus = null;
  59. }
  60. DocumentWorkspace.SetEnableSelectionTinting(false);
  61. base.OnDeactivate();
  62. }
  63. protected override void OnKeyDown(KeyEventArgs e)
  64. {
  65. Cursor = GetCursor();
  66. base.OnKeyDown(e);
  67. }
  68. protected override void OnKeyUp(KeyEventArgs e)
  69. {
  70. Cursor = GetCursor();
  71. base.OnKeyUp(e);
  72. }
  73. protected override void OnMouseUp(MouseEventArgs e)
  74. {
  75. Cursor = GetCursor();
  76. base.OnMouseUp(e);
  77. }
  78. protected override void OnMouseDown(MouseEventArgs e)
  79. {
  80. Cursor = Cursors.WaitCursor;
  81. if ((ModifierKeys & Keys.Control) != 0 && e.Button == MouseButtons.Left)
  82. {
  83. this.combineMode = CombineMode.Union;
  84. }
  85. else if ((ModifierKeys & Keys.Alt) != 0 && e.Button == MouseButtons.Left)
  86. {
  87. this.combineMode = CombineMode.Exclude;
  88. }
  89. else if ((ModifierKeys & Keys.Control) != 0 && e.Button == MouseButtons.Right)
  90. {
  91. this.combineMode = CombineMode.Xor;
  92. }
  93. else if ((ModifierKeys & Keys.Alt) != 0 && e.Button == MouseButtons.Right)
  94. {
  95. this.combineMode = CombineMode.Intersect;
  96. }
  97. else
  98. {
  99. this.combineMode = AppEnvironment.SelectionCombineMode();
  100. }
  101. base.OnMouseDown(e);
  102. }
  103. protected override void OnFillRegionComputed(Point[][] polygonSet)
  104. {
  105. SelectionHistoryMemento undoAction = new SelectionHistoryMemento(this.Name, this.Image, this.DocumentWorkspace);
  106. Selection.PerformChanging();
  107. Selection.SetContinuation(polygonSet, this.combineMode);
  108. Selection.CommitContinuation();
  109. Selection.PerformChanged();
  110. //HistoryStack.PushNewMemento(undoAction);
  111. }
  112. public MagicWandTool(IDocumentWorkspace documentWorkspace)
  113. : base(documentWorkspace,
  114. PdnResources.GetImageResource("Icons.MagicWandToolIcon.png"),
  115. PdnResources.GetString("MagicWandTool.Name"),
  116. PdnResources.GetString("MagicWandTool.HelpText"),
  117. 's',
  118. false,
  119. ToolBarConfigItems.SelectionCombineMode)
  120. {
  121. ClipToSelection = false;
  122. }
  123. }
  124. }