ResourcesTool.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.Collections.Generic;
  22. using System.Drawing;
  23. using System.Linq;
  24. using System.Windows.Forms;
  25. namespace MobiusEditor.Tools
  26. {
  27. public class ResourcesTool : ViewTool
  28. {
  29. private readonly Label totalResourcesLbl;
  30. private readonly NumericUpDown brushSizeNud;
  31. private readonly CheckBox gemsCheckBox;
  32. private bool placementMode;
  33. private bool additivePlacement;
  34. private readonly Dictionary<int, Overlay> undoOverlays = new Dictionary<int, Overlay>();
  35. private readonly Dictionary<int, Overlay> redoOverlays = new Dictionary<int, Overlay>();
  36. public ResourcesTool(MapPanel mapPanel, MapLayerFlag layers, ToolStripStatusLabel statusLbl, Label totalResourcesLbl, NumericUpDown brushSizeNud, CheckBox gemsCheckBox, IGamePlugin plugin, UndoRedoList<UndoRedoEventArgs> url)
  37. : base(mapPanel, layers, statusLbl, plugin, url)
  38. {
  39. this.mapPanel.MouseDown += MapPanel_MouseDown;
  40. this.mapPanel.MouseUp += MapPanel_MouseUp;
  41. (this.mapPanel as Control).KeyDown += ResourceTool_KeyDown;
  42. this.totalResourcesLbl = totalResourcesLbl;
  43. this.brushSizeNud = brushSizeNud;
  44. this.gemsCheckBox = gemsCheckBox;
  45. this.brushSizeNud.ValueChanged += BrushSizeNud_ValueChanged;
  46. navigationWidget.MouseCellChanged += MouseoverWidget_MouseCellChanged;
  47. navigationWidget.MouseoverSize = new Size((int)brushSizeNud.Value, (int)brushSizeNud.Value);
  48. url.Undone += Url_UndoRedo;
  49. url.Redone += Url_UndoRedo;
  50. Update();
  51. UpdateStatus();
  52. }
  53. private void Url_UndoRedo(object sender, EventArgs e)
  54. {
  55. Update();
  56. }
  57. private void BrushSizeNud_ValueChanged(object sender, EventArgs e)
  58. {
  59. navigationWidget.MouseoverSize = new Size((int)brushSizeNud.Value, (int)brushSizeNud.Value);
  60. }
  61. private void ResourceTool_KeyDown(object sender, KeyEventArgs e)
  62. {
  63. if (e.KeyCode == Keys.OemOpenBrackets)
  64. {
  65. brushSizeNud.DownButton();
  66. mapPanel.Invalidate();
  67. }
  68. else if (e.KeyCode == Keys.OemCloseBrackets)
  69. {
  70. brushSizeNud.UpButton();
  71. mapPanel.Invalidate();
  72. }
  73. }
  74. private void MapPanel_MouseDown(object sender, MouseEventArgs e)
  75. {
  76. if (e.Button == MouseButtons.Left)
  77. {
  78. if (!placementMode)
  79. {
  80. EnterPlacementMode(true);
  81. AddResource(navigationWidget.MouseCell);
  82. }
  83. }
  84. else if (e.Button == MouseButtons.Right)
  85. {
  86. if (!placementMode)
  87. {
  88. EnterPlacementMode(false);
  89. RemoveResource(navigationWidget.MouseCell);
  90. }
  91. }
  92. }
  93. private void MapPanel_MouseUp(object sender, MouseEventArgs e)
  94. {
  95. if (placementMode)
  96. {
  97. if (((e.Button == MouseButtons.Left) && additivePlacement) ||
  98. ((e.Button == MouseButtons.Right) && !additivePlacement))
  99. {
  100. ExitPlacementMode();
  101. }
  102. }
  103. if ((undoOverlays.Count > 0) || (redoOverlays.Count > 0))
  104. {
  105. CommitChange();
  106. }
  107. }
  108. private void MouseoverWidget_MouseCellChanged(object sender, MouseCellChangedEventArgs e)
  109. {
  110. if (placementMode)
  111. {
  112. if (additivePlacement)
  113. {
  114. AddResource(e.NewCell);
  115. }
  116. else
  117. {
  118. RemoveResource(e.NewCell);
  119. }
  120. }
  121. if (brushSizeNud.Value > 1)
  122. {
  123. foreach (var cell in new Point[] { e.OldCell, e.NewCell })
  124. {
  125. mapPanel.Invalidate(mapPanel.MapToClient(new Rectangle(
  126. new Point(cell.X - ((int)brushSizeNud.Value / 2), cell.Y - ((int)brushSizeNud.Value / 2)),
  127. new Size((int)brushSizeNud.Value, (int)brushSizeNud.Value)
  128. )));
  129. }
  130. }
  131. }
  132. private void AddResource(Point location)
  133. {
  134. Rectangle rectangle = new Rectangle(location, new Size(1, 1));
  135. rectangle.Inflate(navigationWidget.MouseoverSize.Width / 2, navigationWidget.MouseoverSize.Height / 2);
  136. foreach (var subLocation in rectangle.Points())
  137. {
  138. if ((subLocation.Y == 0) || (subLocation.Y == (map.Metrics.Height - 1)))
  139. {
  140. continue;
  141. }
  142. if (map.Metrics.GetCell(subLocation, out int cell))
  143. {
  144. if (map.Overlay[cell] == null)
  145. {
  146. var resourceType = gemsCheckBox.Checked ?
  147. map.OverlayTypes.Where(t => t.IsGem).FirstOrDefault() :
  148. map.OverlayTypes.Where(t => t.IsTiberiumOrGold).FirstOrDefault();
  149. if (resourceType != null)
  150. {
  151. if (!undoOverlays.ContainsKey(cell))
  152. {
  153. undoOverlays[cell] = map.Overlay[cell];
  154. }
  155. var overlay = new Overlay { Type = resourceType, Icon = 0 };
  156. map.Overlay[cell] = overlay;
  157. redoOverlays[cell] = overlay;
  158. plugin.Dirty = true;
  159. }
  160. }
  161. }
  162. }
  163. rectangle.Inflate(1, 1);
  164. mapPanel.Invalidate(map, rectangle);
  165. Update();
  166. }
  167. private void RemoveResource(Point location)
  168. {
  169. Rectangle rectangle = new Rectangle(location, new Size(1, 1));
  170. rectangle.Inflate(navigationWidget.MouseoverSize.Width / 2, navigationWidget.MouseoverSize.Height / 2);
  171. foreach (var subLocation in rectangle.Points())
  172. {
  173. if (map.Metrics.GetCell(subLocation, out int cell))
  174. {
  175. if (map.Overlay[cell]?.Type.IsResource ?? false)
  176. {
  177. if (!undoOverlays.ContainsKey(cell))
  178. {
  179. undoOverlays[cell] = map.Overlay[cell];
  180. }
  181. map.Overlay[cell] = null;
  182. redoOverlays[cell] = null;
  183. plugin.Dirty = true;
  184. }
  185. }
  186. }
  187. rectangle.Inflate(1, 1);
  188. mapPanel.Invalidate(map, rectangle);
  189. Update();
  190. }
  191. private void EnterPlacementMode(bool additive)
  192. {
  193. if (placementMode)
  194. {
  195. return;
  196. }
  197. placementMode = true;
  198. additivePlacement = additive;
  199. UpdateStatus();
  200. }
  201. private void ExitPlacementMode()
  202. {
  203. if (!placementMode)
  204. {
  205. return;
  206. }
  207. placementMode = false;
  208. UpdateStatus();
  209. }
  210. private void CommitChange()
  211. {
  212. var undoOverlays2 = new Dictionary<int, Overlay>(undoOverlays);
  213. void undoAction(UndoRedoEventArgs e)
  214. {
  215. foreach (var kv in undoOverlays2)
  216. {
  217. e.Map.Overlay[kv.Key] = kv.Value;
  218. }
  219. e.MapPanel.Invalidate(e.Map, undoOverlays2.Keys.Select(k =>
  220. {
  221. e.Map.Metrics.GetLocation(k, out Point location);
  222. var rectangle = new Rectangle(location, new Size(1, 1));
  223. rectangle.Inflate(1, 1);
  224. return rectangle;
  225. }));
  226. }
  227. var redoOverlays2 = new Dictionary<int, Overlay>(redoOverlays);
  228. void redoAction(UndoRedoEventArgs e)
  229. {
  230. foreach (var kv in redoOverlays2)
  231. {
  232. e.Map.Overlay[kv.Key] = kv.Value;
  233. }
  234. e.MapPanel.Invalidate(e.Map, redoOverlays2.Keys.Select(k =>
  235. {
  236. e.Map.Metrics.GetLocation(k, out Point location);
  237. var rectangle = new Rectangle(location, new Size(1, 1));
  238. rectangle.Inflate(1, 1);
  239. return rectangle;
  240. }));
  241. }
  242. undoOverlays.Clear();
  243. redoOverlays.Clear();
  244. url.Track(undoAction, redoAction);
  245. }
  246. private void Update()
  247. {
  248. totalResourcesLbl.Text = map.TotalResources.ToString();
  249. if (map.OverlayTypes.Any(t => t.IsGem))
  250. {
  251. gemsCheckBox.Visible = true;
  252. }
  253. else
  254. {
  255. gemsCheckBox.Visible = false;
  256. gemsCheckBox.Checked = false;
  257. }
  258. }
  259. private void UpdateStatus()
  260. {
  261. if (placementMode)
  262. {
  263. if (additivePlacement)
  264. {
  265. statusLbl.Text = "Drag mouse to add resources";
  266. }
  267. else
  268. {
  269. statusLbl.Text = "Drag mouse to remove resources";
  270. }
  271. }
  272. else
  273. {
  274. statusLbl.Text = "Left-Click drag to add resources, Right-Click drag to remove resources";
  275. }
  276. }
  277. protected override void PostRenderMap(Graphics graphics)
  278. {
  279. base.PostRenderMap(graphics);
  280. var resourcePen = new Pen(Color.Green, 4.0f);
  281. foreach (var (cell, overlay) in map.Overlay)
  282. {
  283. if (overlay.Type.IsResource)
  284. {
  285. map.Metrics.GetLocation(cell, out Point topLeft);
  286. var bounds = new Rectangle(new Point(topLeft.X * Globals.TileWidth, topLeft.Y * Globals.TileHeight), Globals.TileSize);
  287. graphics.DrawRectangle(resourcePen, bounds);
  288. }
  289. }
  290. }
  291. #region IDisposable Support
  292. private bool disposedValue = false;
  293. protected override void Dispose(bool disposing)
  294. {
  295. if (!disposedValue)
  296. {
  297. if (disposing)
  298. {
  299. mapPanel.MouseDown -= MapPanel_MouseDown;
  300. mapPanel.MouseUp -= MapPanel_MouseUp;
  301. (mapPanel as Control).KeyDown -= ResourceTool_KeyDown;
  302. brushSizeNud.ValueChanged -= BrushSizeNud_ValueChanged;
  303. navigationWidget.MouseCellChanged -= MouseoverWidget_MouseCellChanged;
  304. url.Undone -= Url_UndoRedo;
  305. url.Redone -= Url_UndoRedo;
  306. }
  307. disposedValue = true;
  308. }
  309. base.Dispose(disposing);
  310. }
  311. #endregion
  312. }
  313. }