WaypointsTool.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // The Command & Conquer Map Editor and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // The Command & Conquer Map Editor and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. using MobiusEditor.Controls;
  15. using MobiusEditor.Event;
  16. using MobiusEditor.Interface;
  17. using MobiusEditor.Model;
  18. using MobiusEditor.Utility;
  19. using MobiusEditor.Widgets;
  20. using System;
  21. using System.Drawing;
  22. using System.Linq;
  23. using System.Windows.Forms;
  24. namespace MobiusEditor.Tools
  25. {
  26. public class WaypointsTool : ViewTool
  27. {
  28. private readonly ComboBox waypointCombo;
  29. private (Waypoint waypoint, int? cell)? undoWaypoint;
  30. private (Waypoint waypoint, int? cell)? redoWaypoint;
  31. private bool placementMode;
  32. public WaypointsTool(MapPanel mapPanel, MapLayerFlag layers, ToolStripStatusLabel statusLbl, ComboBox waypointCombo, IGamePlugin plugin, UndoRedoList<UndoRedoEventArgs> url)
  33. : base(mapPanel, layers, statusLbl, plugin, url)
  34. {
  35. this.mapPanel.MouseDown += MapPanel_MouseDown;
  36. this.mapPanel.MouseMove += MapPanel_MouseMove;
  37. (this.mapPanel as Control).KeyDown += WaypointsTool_KeyDown;
  38. (this.mapPanel as Control).KeyUp += WaypointsTool_KeyUp;
  39. this.waypointCombo = waypointCombo;
  40. UpdateStatus();
  41. }
  42. private void MapPanel_MouseDown(object sender, MouseEventArgs e)
  43. {
  44. if (placementMode)
  45. {
  46. if (e.Button == MouseButtons.Left)
  47. {
  48. SetWaypoint(navigationWidget.MouseCell);
  49. }
  50. else if (e.Button == MouseButtons.Right)
  51. {
  52. RemoveWaypoint(navigationWidget.MouseCell);
  53. }
  54. }
  55. else if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
  56. {
  57. PickWaypoint(navigationWidget.MouseCell);
  58. }
  59. }
  60. private void WaypointsTool_KeyDown(object sender, KeyEventArgs e)
  61. {
  62. if (e.KeyCode == Keys.ShiftKey)
  63. {
  64. EnterPlacementMode();
  65. }
  66. }
  67. private void WaypointsTool_KeyUp(object sender, KeyEventArgs e)
  68. {
  69. if (e.KeyCode == Keys.ShiftKey)
  70. {
  71. ExitPlacementMode();
  72. }
  73. }
  74. private void MapPanel_MouseMove(object sender, MouseEventArgs e)
  75. {
  76. if (!placementMode && (Control.ModifierKeys == Keys.Shift))
  77. {
  78. EnterPlacementMode();
  79. }
  80. else if (placementMode && (Control.ModifierKeys == Keys.None))
  81. {
  82. ExitPlacementMode();
  83. }
  84. }
  85. private void SetWaypoint(Point location)
  86. {
  87. if (map.Metrics.GetCell(location, out int cell))
  88. {
  89. var waypoint = map.Waypoints[waypointCombo.SelectedIndex];
  90. if (waypoint.Cell != cell)
  91. {
  92. if (undoWaypoint == null)
  93. {
  94. undoWaypoint = (waypoint, waypoint.Cell);
  95. }
  96. else if (undoWaypoint.Value.cell == cell)
  97. {
  98. undoWaypoint = null;
  99. }
  100. waypoint.Cell = cell;
  101. redoWaypoint = (waypoint, waypoint.Cell);
  102. CommitChange();
  103. mapPanel.Invalidate();
  104. plugin.Dirty = true;
  105. }
  106. }
  107. }
  108. private void RemoveWaypoint(Point location)
  109. {
  110. if (map.Metrics.GetCell(location, out int cell))
  111. {
  112. var waypoint = map.Waypoints.Where(w => w.Cell == cell).FirstOrDefault();
  113. if (waypoint != null)
  114. {
  115. if (undoWaypoint == null)
  116. {
  117. undoWaypoint = (waypoint, waypoint.Cell);
  118. }
  119. waypoint.Cell = null;
  120. redoWaypoint = (waypoint, null);
  121. CommitChange();
  122. mapPanel.Invalidate();
  123. plugin.Dirty = true;
  124. }
  125. }
  126. }
  127. private void EnterPlacementMode()
  128. {
  129. if (placementMode)
  130. {
  131. return;
  132. }
  133. placementMode = true;
  134. UpdateStatus();
  135. }
  136. private void ExitPlacementMode()
  137. {
  138. if (!placementMode)
  139. {
  140. return;
  141. }
  142. placementMode = false;
  143. UpdateStatus();
  144. }
  145. private void PickWaypoint(Point location)
  146. {
  147. if (map.Metrics.GetCell(location, out int cell))
  148. {
  149. for (var i = 0; i < map.Waypoints.Length; ++i)
  150. {
  151. if (map.Waypoints[i].Cell == cell)
  152. {
  153. waypointCombo.SelectedIndex = i;
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. private void CommitChange()
  160. {
  161. var undoWaypoint2 = undoWaypoint;
  162. void undoAction(UndoRedoEventArgs e)
  163. {
  164. undoWaypoint2.Value.waypoint.Cell = undoWaypoint2.Value.cell;
  165. mapPanel.Invalidate();
  166. }
  167. var redoWaypoint2 = redoWaypoint;
  168. void redoAction(UndoRedoEventArgs e)
  169. {
  170. redoWaypoint2.Value.waypoint.Cell = redoWaypoint2.Value.cell;
  171. mapPanel.Invalidate();
  172. }
  173. undoWaypoint = null;
  174. redoWaypoint = null;
  175. url.Track(undoAction, redoAction);
  176. }
  177. private void UpdateStatus()
  178. {
  179. if (placementMode)
  180. {
  181. statusLbl.Text = "Left-Click to set cell waypoint, Right-Click to clear cell waypoint";
  182. }
  183. else
  184. {
  185. statusLbl.Text = "Shift to enter placement mode, Left-Click or Right-Click to pick cell waypoint";
  186. }
  187. }
  188. #region IDisposable Support
  189. private bool disposedValue = false;
  190. protected override void Dispose(bool disposing)
  191. {
  192. if (!disposedValue)
  193. {
  194. if (disposing)
  195. {
  196. mapPanel.MouseDown -= MapPanel_MouseDown;
  197. mapPanel.MouseMove -= MapPanel_MouseMove;
  198. (mapPanel as Control).KeyDown -= WaypointsTool_KeyDown;
  199. (mapPanel as Control).KeyUp -= WaypointsTool_KeyUp;
  200. }
  201. disposedValue = true;
  202. }
  203. base.Dispose(disposing);
  204. }
  205. #endregion
  206. }
  207. }