UnitTool.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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.Render;
  19. using MobiusEditor.Utility;
  20. using MobiusEditor.Widgets;
  21. using System;
  22. using System.ComponentModel;
  23. using System.Drawing;
  24. using System.Linq;
  25. using System.Windows.Forms;
  26. namespace MobiusEditor.Tools
  27. {
  28. public class UnitTool : ViewTool
  29. {
  30. private readonly TypeComboBox unitTypeComboBox;
  31. private readonly MapPanel unitTypeMapPanel;
  32. private readonly ObjectProperties objectProperties;
  33. private Map previewMap;
  34. protected override Map RenderMap => previewMap;
  35. private bool placementMode;
  36. private readonly Unit mockUnit;
  37. private Unit selectedUnit;
  38. private ObjectPropertiesPopup selectedObjectProperties;
  39. private UnitType selectedUnitType;
  40. private UnitType SelectedUnitType
  41. {
  42. get => selectedUnitType;
  43. set
  44. {
  45. if (selectedUnitType != value)
  46. {
  47. if (placementMode && (selectedUnitType != null))
  48. {
  49. mapPanel.Invalidate(map, Rectangle.Inflate(new Rectangle(navigationWidget.MouseCell, new Size(1, 1)), 1, 1));
  50. }
  51. selectedUnitType = value;
  52. unitTypeComboBox.SelectedValue = selectedUnitType;
  53. if (placementMode && (selectedUnitType != null))
  54. {
  55. mapPanel.Invalidate(map, Rectangle.Inflate(new Rectangle(navigationWidget.MouseCell, new Size(1, 1)), 1, 1));
  56. }
  57. mockUnit.Type = selectedUnitType;
  58. RefreshMapPanel();
  59. }
  60. }
  61. }
  62. public UnitTool(MapPanel mapPanel, MapLayerFlag layers, ToolStripStatusLabel statusLbl, TypeComboBox unitTypeComboBox, MapPanel unitTypeMapPanel, ObjectProperties objectProperties, IGamePlugin plugin, UndoRedoList<UndoRedoEventArgs> url)
  63. : base(mapPanel, layers, statusLbl, plugin, url)
  64. {
  65. previewMap = map;
  66. mockUnit = new Unit()
  67. {
  68. Type = unitTypeComboBox.Types.First() as UnitType,
  69. House = map.Houses.First().Type,
  70. Strength = 256,
  71. Direction = map.DirectionTypes.Where(d => d.Equals(FacingType.North)).First(),
  72. Mission = map.MissionTypes.Where(m => m.Equals("Guard")).FirstOrDefault() ?? map.MissionTypes.First()
  73. };
  74. mockUnit.PropertyChanged += MockUnit_PropertyChanged;
  75. this.mapPanel.MouseDown += MapPanel_MouseDown;
  76. this.mapPanel.MouseUp += MapPanel_MouseUp;
  77. this.mapPanel.MouseDoubleClick += MapPanel_MouseDoubleClick;
  78. this.mapPanel.MouseMove += MapPanel_MouseMove;
  79. (this.mapPanel as Control).KeyDown += UnitTool_KeyDown;
  80. (this.mapPanel as Control).KeyUp += UnitTool_KeyUp;
  81. this.unitTypeComboBox = unitTypeComboBox;
  82. this.unitTypeComboBox.SelectedIndexChanged += UnitTypeComboBox_SelectedIndexChanged;
  83. this.unitTypeMapPanel = unitTypeMapPanel;
  84. this.unitTypeMapPanel.BackColor = Color.White;
  85. this.unitTypeMapPanel.MaxZoom = 1;
  86. this.objectProperties = objectProperties;
  87. this.objectProperties.Object = mockUnit;
  88. navigationWidget.MouseCellChanged += MouseoverWidget_MouseCellChanged;
  89. SelectedUnitType = mockUnit.Type;
  90. UpdateStatus();
  91. }
  92. private void MapPanel_MouseDoubleClick(object sender, MouseEventArgs e)
  93. {
  94. if (Control.ModifierKeys != Keys.None)
  95. {
  96. return;
  97. }
  98. if (map.Metrics.GetCell(navigationWidget.MouseCell, out int cell))
  99. {
  100. if (map.Technos[cell] is Unit unit)
  101. {
  102. selectedUnit = null;
  103. selectedObjectProperties?.Close();
  104. selectedObjectProperties = new ObjectPropertiesPopup(objectProperties.Plugin, unit);
  105. selectedObjectProperties.Closed += (cs, ce) =>
  106. {
  107. navigationWidget.Refresh();
  108. };
  109. unit.PropertyChanged += SelectedUnit_PropertyChanged;
  110. selectedObjectProperties.Show(mapPanel, mapPanel.PointToClient(Control.MousePosition));
  111. UpdateStatus();
  112. }
  113. }
  114. }
  115. private void MockUnit_PropertyChanged(object sender, PropertyChangedEventArgs e)
  116. {
  117. RefreshMapPanel();
  118. }
  119. private void SelectedUnit_PropertyChanged(object sender, PropertyChangedEventArgs e)
  120. {
  121. mapPanel.Invalidate(map, sender as Unit);
  122. }
  123. private void UnitTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
  124. {
  125. SelectedUnitType = unitTypeComboBox.SelectedValue as UnitType;
  126. }
  127. private void UnitTool_KeyDown(object sender, KeyEventArgs e)
  128. {
  129. if (e.KeyCode == Keys.ShiftKey)
  130. {
  131. EnterPlacementMode();
  132. }
  133. }
  134. private void UnitTool_KeyUp(object sender, KeyEventArgs e)
  135. {
  136. if (e.KeyCode == Keys.ShiftKey)
  137. {
  138. ExitPlacementMode();
  139. }
  140. }
  141. private void MapPanel_MouseMove(object sender, MouseEventArgs e)
  142. {
  143. if (!placementMode && (Control.ModifierKeys == Keys.Shift))
  144. {
  145. EnterPlacementMode();
  146. }
  147. else if (placementMode && (Control.ModifierKeys == Keys.None))
  148. {
  149. ExitPlacementMode();
  150. }
  151. }
  152. private void MapPanel_MouseDown(object sender, MouseEventArgs e)
  153. {
  154. if (placementMode)
  155. {
  156. if (e.Button == MouseButtons.Left)
  157. {
  158. AddUnit(navigationWidget.MouseCell);
  159. }
  160. else if (e.Button == MouseButtons.Right)
  161. {
  162. RemoveUnit(navigationWidget.MouseCell);
  163. }
  164. }
  165. else if (e.Button == MouseButtons.Left)
  166. {
  167. SelectUnit(navigationWidget.MouseCell);
  168. }
  169. else if (e.Button == MouseButtons.Right)
  170. {
  171. PickUnit(navigationWidget.MouseCell);
  172. }
  173. }
  174. private void MapPanel_MouseUp(object sender, MouseEventArgs e)
  175. {
  176. if (selectedUnit != null)
  177. {
  178. selectedUnit = null;
  179. UpdateStatus();
  180. }
  181. }
  182. private void MouseoverWidget_MouseCellChanged(object sender, MouseCellChangedEventArgs e)
  183. {
  184. if (placementMode)
  185. {
  186. if (SelectedUnitType != null)
  187. {
  188. mapPanel.Invalidate(map, Rectangle.Inflate(new Rectangle(e.OldCell, new Size(1, 1)), 1, 1));
  189. mapPanel.Invalidate(map, Rectangle.Inflate(new Rectangle(e.NewCell, new Size(1, 1)), 1, 1));
  190. }
  191. }
  192. else if (selectedUnit != null)
  193. {
  194. var oldLocation = map.Technos[selectedUnit].Value;
  195. mapPanel.Invalidate(map, selectedUnit);
  196. map.Technos.Remove(selectedUnit);
  197. if (map.Technos.Add(e.NewCell, selectedUnit))
  198. {
  199. mapPanel.Invalidate(map, selectedUnit);
  200. plugin.Dirty = true;
  201. }
  202. else
  203. {
  204. map.Technos.Add(oldLocation, selectedUnit);
  205. }
  206. }
  207. }
  208. private void AddUnit(Point location)
  209. {
  210. if (SelectedUnitType != null)
  211. {
  212. var unit = mockUnit.Clone();
  213. if (map.Technos.Add(location, unit))
  214. {
  215. mapPanel.Invalidate(map, unit);
  216. plugin.Dirty = true;
  217. }
  218. }
  219. }
  220. private void RemoveUnit(Point location)
  221. {
  222. if (map.Technos[location] is Unit unit)
  223. {
  224. mapPanel.Invalidate(map, unit);
  225. map.Technos.Remove(unit);
  226. plugin.Dirty = true;
  227. }
  228. }
  229. private void EnterPlacementMode()
  230. {
  231. if (placementMode)
  232. {
  233. return;
  234. }
  235. placementMode = true;
  236. navigationWidget.MouseoverSize = Size.Empty;
  237. if (SelectedUnitType != null)
  238. {
  239. mapPanel.Invalidate(map, Rectangle.Inflate(new Rectangle(navigationWidget.MouseCell, new Size(1, 1)), 1, 1));
  240. }
  241. UpdateStatus();
  242. }
  243. private void ExitPlacementMode()
  244. {
  245. if (!placementMode)
  246. {
  247. return;
  248. }
  249. placementMode = false;
  250. navigationWidget.MouseoverSize = new Size(1, 1);
  251. if (SelectedUnitType != null)
  252. {
  253. mapPanel.Invalidate(map, Rectangle.Inflate(new Rectangle(navigationWidget.MouseCell, new Size(1, 1)), 1, 1));
  254. }
  255. UpdateStatus();
  256. }
  257. private void PickUnit(Point location)
  258. {
  259. if (map.Metrics.GetCell(location, out int cell))
  260. {
  261. if (map.Technos[cell] is Unit unit)
  262. {
  263. SelectedUnitType = unit.Type;
  264. mockUnit.House = unit.House;
  265. mockUnit.Strength = unit.Strength;
  266. mockUnit.Direction = unit.Direction;
  267. mockUnit.Mission = unit.Mission;
  268. mockUnit.Trigger = unit.Trigger;
  269. }
  270. }
  271. }
  272. private void SelectUnit(Point location)
  273. {
  274. if (map.Metrics.GetCell(location, out int cell))
  275. {
  276. selectedUnit = map.Technos[cell] as Unit;
  277. }
  278. UpdateStatus();
  279. }
  280. private void RefreshMapPanel()
  281. {
  282. if (mockUnit.Type != null)
  283. {
  284. var unitPreview = new Bitmap(Globals.TileWidth * 3, Globals.TileHeight * 3);
  285. using (var g = Graphics.FromImage(unitPreview))
  286. {
  287. MapRenderer.Render(plugin.GameType, map.Theater, new Point(1, 1), Globals.TileSize, mockUnit).Item2(g);
  288. }
  289. unitTypeMapPanel.MapImage = unitPreview;
  290. }
  291. else
  292. {
  293. unitTypeMapPanel.MapImage = null;
  294. }
  295. }
  296. private void UpdateStatus()
  297. {
  298. if (placementMode)
  299. {
  300. statusLbl.Text = "Left-Click to place unit, Right-Click to remove unit";
  301. }
  302. else if (selectedUnit != null)
  303. {
  304. statusLbl.Text = "Drag mouse to move unit";
  305. }
  306. else
  307. {
  308. statusLbl.Text = "Shift to enter placement mode, Left-Click drag to move unit, Double-Click update unit properties, Right-Click to pick unit";
  309. }
  310. }
  311. protected override void PreRenderMap()
  312. {
  313. base.PreRenderMap();
  314. previewMap = map.Clone();
  315. if (placementMode)
  316. {
  317. var location = navigationWidget.MouseCell;
  318. if (SelectedUnitType != null)
  319. {
  320. var unit = mockUnit.Clone();
  321. unit.Tint = Color.FromArgb(128, Color.White);
  322. if (previewMap.Technos.Add(location, unit))
  323. {
  324. mapPanel.Invalidate(previewMap, unit);
  325. }
  326. }
  327. }
  328. }
  329. protected override void PostRenderMap(Graphics graphics)
  330. {
  331. base.PostRenderMap(graphics);
  332. var unitPen = new Pen(Color.Green, 4.0f);
  333. foreach (var (topLeft, _) in map.Technos.OfType<Unit>())
  334. {
  335. var bounds = new Rectangle(new Point(topLeft.X * Globals.TileWidth, topLeft.Y * Globals.TileHeight), Globals.TileSize);
  336. graphics.DrawRectangle(unitPen, bounds);
  337. }
  338. }
  339. #region IDisposable Support
  340. private bool disposedValue = false;
  341. protected override void Dispose(bool disposing)
  342. {
  343. if (!disposedValue)
  344. {
  345. if (disposing)
  346. {
  347. selectedObjectProperties?.Close();
  348. mapPanel.MouseDown -= MapPanel_MouseDown;
  349. mapPanel.MouseUp -= MapPanel_MouseUp;
  350. mapPanel.MouseDoubleClick -= MapPanel_MouseDoubleClick;
  351. mapPanel.MouseMove -= MapPanel_MouseMove;
  352. (mapPanel as Control).KeyDown -= UnitTool_KeyDown;
  353. (mapPanel as Control).KeyUp -= UnitTool_KeyUp;
  354. unitTypeComboBox.SelectedIndexChanged -= UnitTypeComboBox_SelectedIndexChanged;
  355. navigationWidget.MouseCellChanged -= MouseoverWidget_MouseCellChanged;
  356. }
  357. disposedValue = true;
  358. }
  359. base.Dispose(disposing);
  360. }
  361. #endregion
  362. }
  363. }