TemplateTool.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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.Drawing.Drawing2D;
  24. using System.Linq;
  25. using System.Text.RegularExpressions;
  26. using System.Windows.Forms;
  27. namespace MobiusEditor.Tools
  28. {
  29. public class TemplateTool : ViewTool
  30. {
  31. private static readonly Regex CategoryRegex = new Regex(@"^([a-z]*)", RegexOptions.Compiled);
  32. private readonly ListView templateTypeListView;
  33. private readonly MapPanel templateTypeMapPanel;
  34. private readonly ToolTip mouseTooltip;
  35. private readonly Dictionary<int, Template> undoTemplates = new Dictionary<int, Template>();
  36. private readonly Dictionary<int, Template> redoTemplates = new Dictionary<int, Template>();
  37. private Map previewMap;
  38. protected override Map RenderMap => previewMap;
  39. private bool placementMode;
  40. private bool boundsMode;
  41. private Rectangle dragBounds;
  42. private int dragEdge = -1;
  43. private TemplateType selectedTemplateType;
  44. private TemplateType SelectedTemplateType
  45. {
  46. get => selectedTemplateType;
  47. set
  48. {
  49. if (selectedTemplateType != value)
  50. {
  51. if (placementMode && (selectedTemplateType != null))
  52. {
  53. for (var y = 0; y < selectedTemplateType.IconHeight; ++y)
  54. {
  55. for (var x = 0; x < selectedTemplateType.IconWidth; ++x)
  56. {
  57. mapPanel.Invalidate(map, new Point(navigationWidget.MouseCell.X + x, navigationWidget.MouseCell.Y + y));
  58. }
  59. }
  60. }
  61. selectedTemplateType = value;
  62. templateTypeListView.BeginUpdate();
  63. templateTypeListView.SelectedIndexChanged -= TemplateTypeListView_SelectedIndexChanged;
  64. foreach (ListViewItem item in templateTypeListView.Items)
  65. {
  66. item.Selected = item.Tag == selectedTemplateType;
  67. }
  68. if (templateTypeListView.SelectedIndices.Count > 0)
  69. {
  70. templateTypeListView.EnsureVisible(templateTypeListView.SelectedIndices[0]);
  71. }
  72. templateTypeListView.SelectedIndexChanged += TemplateTypeListView_SelectedIndexChanged;
  73. templateTypeListView.EndUpdate();
  74. if (placementMode && (selectedTemplateType != null))
  75. {
  76. for (var y = 0; y < selectedTemplateType.IconHeight; ++y)
  77. {
  78. for (var x = 0; x < selectedTemplateType.IconWidth; ++x)
  79. {
  80. mapPanel.Invalidate(map, new Point(navigationWidget.MouseCell.X + x, navigationWidget.MouseCell.Y + y));
  81. }
  82. }
  83. }
  84. RefreshMapPanel();
  85. }
  86. }
  87. }
  88. private Point? selectedIcon;
  89. private Point? SelectedIcon
  90. {
  91. get => selectedIcon;
  92. set
  93. {
  94. if (selectedIcon != value)
  95. {
  96. selectedIcon = value;
  97. templateTypeMapPanel.Invalidate();
  98. if (placementMode && (SelectedTemplateType != null))
  99. {
  100. for (var y = 0; y < SelectedTemplateType.IconHeight; ++y)
  101. {
  102. for (var x = 0; x < SelectedTemplateType.IconWidth; ++x)
  103. {
  104. mapPanel.Invalidate(map, new Point(navigationWidget.MouseCell.X + x, navigationWidget.MouseCell.Y + y));
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. private NavigationWidget templateTypeNavigationWidget;
  112. public TemplateTool(MapPanel mapPanel, MapLayerFlag layers, ToolStripStatusLabel statusLbl, ListView templateTypeListView, MapPanel templateTypeMapPanel, ToolTip mouseTooltip, IGamePlugin plugin, UndoRedoList<UndoRedoEventArgs> url)
  113. : base(mapPanel, layers, statusLbl, plugin, url)
  114. {
  115. previewMap = map;
  116. this.mapPanel.MouseDown += MapPanel_MouseDown;
  117. this.mapPanel.MouseUp += MapPanel_MouseUp;
  118. this.mapPanel.MouseMove += MapPanel_MouseMove;
  119. (this.mapPanel as Control).KeyDown += TemplateTool_KeyDown;
  120. (this.mapPanel as Control).KeyUp += TemplateTool_KeyUp;
  121. this.templateTypeListView = templateTypeListView;
  122. this.templateTypeListView.SelectedIndexChanged += TemplateTypeListView_SelectedIndexChanged;
  123. string templateCategory(TemplateType template)
  124. {
  125. var m = CategoryRegex.Match(template.Name);
  126. return m.Success ? m.Groups[1].Value : string.Empty;
  127. }
  128. var templateTypes = plugin.Map.TemplateTypes
  129. .Where(t =>
  130. (t.Thumbnail != null) &&
  131. t.Theaters.Contains(plugin.Map.Theater) &&
  132. ((t.Flag & TemplateTypeFlag.Clear) == TemplateTypeFlag.None))
  133. .GroupBy(t => templateCategory(t)).OrderBy(g => g.Key);
  134. var templateTypeImages = templateTypes.SelectMany(g => g).Select(t => t.Thumbnail);
  135. var maxWidth = templateTypeImages.Max(t => t.Width);
  136. var maxHeight = templateTypeImages.Max(t => t.Height);
  137. var imageList = new ImageList();
  138. imageList.Images.AddRange(templateTypeImages.ToArray());
  139. imageList.ImageSize = new Size(maxWidth, maxHeight);
  140. imageList.ColorDepth = ColorDepth.Depth24Bit;
  141. this.templateTypeListView.BeginUpdate();
  142. this.templateTypeListView.LargeImageList = imageList;
  143. var imageIndex = 0;
  144. foreach (var templateTypeGroup in templateTypes)
  145. {
  146. var group = new ListViewGroup(templateTypeGroup.Key);
  147. this.templateTypeListView.Groups.Add(group);
  148. foreach (var templateType in templateTypeGroup)
  149. {
  150. var item = new ListViewItem(templateType.DisplayName, imageIndex++)
  151. {
  152. Group = group,
  153. Tag = templateType
  154. };
  155. this.templateTypeListView.Items.Add(item);
  156. }
  157. }
  158. this.templateTypeListView.EndUpdate();
  159. this.templateTypeMapPanel = templateTypeMapPanel;
  160. this.templateTypeMapPanel.MouseDown += TemplateTypeMapPanel_MouseDown;
  161. this.templateTypeMapPanel.PostRender += TemplateTypeMapPanel_PostRender;
  162. this.templateTypeMapPanel.BackColor = Color.Black;
  163. this.templateTypeMapPanel.MaxZoom = 1;
  164. this.mouseTooltip = mouseTooltip;
  165. navigationWidget.MouseCellChanged += MouseoverWidget_MouseCellChanged;
  166. url.Undone += Url_Undone;
  167. url.Redone += Url_Redone;
  168. SelectedTemplateType = templateTypes.First().First();
  169. UpdateStatus();
  170. }
  171. private void Url_Redone(object sender, EventArgs e)
  172. {
  173. if (boundsMode && (map.Bounds != dragBounds))
  174. {
  175. dragBounds = map.Bounds;
  176. dragEdge = -1;
  177. UpdateTooltip();
  178. mapPanel.Invalidate();
  179. }
  180. }
  181. private void Url_Undone(object sender, EventArgs e)
  182. {
  183. if (boundsMode && (map.Bounds != dragBounds))
  184. {
  185. dragBounds = map.Bounds;
  186. dragEdge = -1;
  187. UpdateTooltip();
  188. mapPanel.Invalidate();
  189. }
  190. }
  191. private void TemplateTypeMapPanel_MouseDown(object sender, MouseEventArgs e)
  192. {
  193. if ((SelectedTemplateType == null) || ((SelectedTemplateType.IconWidth * SelectedTemplateType.IconHeight) == 1))
  194. {
  195. SelectedIcon = null;
  196. }
  197. else
  198. {
  199. if (e.Button == MouseButtons.Left)
  200. {
  201. var templateTypeMouseCell = templateTypeNavigationWidget.MouseCell;
  202. if ((templateTypeMouseCell.X >= 0) && (templateTypeMouseCell.X < SelectedTemplateType.IconWidth))
  203. {
  204. if ((templateTypeMouseCell.Y >= 0) && (templateTypeMouseCell.Y < SelectedTemplateType.IconHeight))
  205. {
  206. if (SelectedTemplateType.IconMask[templateTypeMouseCell.X, templateTypeMouseCell.Y])
  207. {
  208. SelectedIcon = templateTypeMouseCell;
  209. }
  210. }
  211. }
  212. }
  213. else if (e.Button == MouseButtons.Right)
  214. {
  215. SelectedIcon = null;
  216. }
  217. }
  218. }
  219. private void TemplateTypeMapPanel_PostRender(object sender, RenderEventArgs e)
  220. {
  221. if (SelectedIcon.HasValue)
  222. {
  223. var selectedIconPen = new Pen(Color.Yellow, 2);
  224. var cellSize = new Size(Globals.OriginalTileWidth / 4, Globals.OriginalTileHeight / 4);
  225. var rect = new Rectangle(new Point(SelectedIcon.Value.X * cellSize.Width, SelectedIcon.Value.Y * cellSize.Height), cellSize);
  226. e.Graphics.DrawRectangle(selectedIconPen, rect);
  227. }
  228. if (SelectedTemplateType != null)
  229. {
  230. var sizeStringFormat = new StringFormat
  231. {
  232. Alignment = StringAlignment.Center,
  233. LineAlignment = StringAlignment.Center
  234. };
  235. var sizeBackgroundBrush = new SolidBrush(Color.FromArgb(128, Color.Black));
  236. var sizeTextBrush = new SolidBrush(Color.White);
  237. var text = string.Format("{0} ({1}x{2})", SelectedTemplateType.DisplayName, SelectedTemplateType.IconWidth, SelectedTemplateType.IconHeight);
  238. var textSize = e.Graphics.MeasureString(text, SystemFonts.CaptionFont) + new SizeF(6.0f, 6.0f);
  239. var textBounds = new RectangleF(new PointF(0, 0), textSize);
  240. e.Graphics.Transform = new Matrix();
  241. e.Graphics.FillRectangle(sizeBackgroundBrush, textBounds);
  242. e.Graphics.DrawString(text, SystemFonts.CaptionFont, sizeTextBrush, textBounds, sizeStringFormat);
  243. }
  244. }
  245. private void TemplateTool_KeyDown(object sender, KeyEventArgs e)
  246. {
  247. if (e.KeyCode == Keys.ShiftKey)
  248. {
  249. EnterPlacementMode();
  250. }
  251. else if (e.KeyCode == Keys.ControlKey)
  252. {
  253. EnterBoundsMode();
  254. }
  255. }
  256. private void TemplateTool_KeyUp(object sender, KeyEventArgs e)
  257. {
  258. if ((e.KeyCode == Keys.ShiftKey) || (e.KeyCode == Keys.ControlKey))
  259. {
  260. ExitAllModes();
  261. }
  262. }
  263. private void TemplateTypeListView_SelectedIndexChanged(object sender, EventArgs e)
  264. {
  265. SelectedTemplateType = (templateTypeListView.SelectedItems.Count > 0) ? (templateTypeListView.SelectedItems[0].Tag as TemplateType) : null;
  266. SelectedIcon = null;
  267. }
  268. private void MapPanel_MouseDown(object sender, MouseEventArgs e)
  269. {
  270. if (boundsMode)
  271. {
  272. dragEdge = DetectDragEdge();
  273. UpdateStatus();
  274. }
  275. else if (placementMode)
  276. {
  277. if (e.Button == MouseButtons.Left)
  278. {
  279. SetTemplate(navigationWidget.MouseCell);
  280. }
  281. else if (e.Button == MouseButtons.Right)
  282. {
  283. RemoveTemplate(navigationWidget.MouseCell);
  284. }
  285. }
  286. else if ((e.Button == MouseButtons.Left) || (e.Button == MouseButtons.Right))
  287. {
  288. PickTemplate(navigationWidget.MouseCell, e.Button == MouseButtons.Left);
  289. }
  290. }
  291. private void MapPanel_MouseUp(object sender, MouseEventArgs e)
  292. {
  293. if (boundsMode)
  294. {
  295. if (dragBounds != map.Bounds)
  296. {
  297. var oldBounds = map.Bounds;
  298. void undoAction(UndoRedoEventArgs ure)
  299. {
  300. ure.Map.Bounds = oldBounds;
  301. ure.MapPanel.Invalidate();
  302. }
  303. void redoAction(UndoRedoEventArgs ure)
  304. {
  305. ure.Map.Bounds = dragBounds;
  306. ure.MapPanel.Invalidate();
  307. }
  308. map.Bounds = dragBounds;
  309. url.Track(undoAction, redoAction);
  310. mapPanel.Invalidate();
  311. }
  312. dragEdge = -1;
  313. UpdateStatus();
  314. }
  315. else
  316. {
  317. if ((undoTemplates.Count > 0) || (redoTemplates.Count > 0))
  318. {
  319. CommitChange();
  320. }
  321. }
  322. }
  323. private void MapPanel_MouseMove(object sender, MouseEventArgs e)
  324. {
  325. if (!placementMode && (Control.ModifierKeys == Keys.Shift))
  326. {
  327. EnterPlacementMode();
  328. }
  329. else if (!boundsMode && (Control.ModifierKeys == Keys.Control))
  330. {
  331. EnterBoundsMode();
  332. }
  333. else if ((placementMode || boundsMode) && (Control.ModifierKeys == Keys.None))
  334. {
  335. ExitAllModes();
  336. }
  337. var cursor = Cursors.Default;
  338. if (boundsMode)
  339. {
  340. switch ((dragEdge >= 0) ? dragEdge : DetectDragEdge())
  341. {
  342. case 0:
  343. case 4:
  344. cursor = Cursors.SizeNS;
  345. break;
  346. case 2:
  347. case 6:
  348. cursor = Cursors.SizeWE;
  349. break;
  350. case 1:
  351. case 5:
  352. cursor = Cursors.SizeNESW;
  353. break;
  354. case 3:
  355. case 7:
  356. cursor = Cursors.SizeNWSE;
  357. break;
  358. }
  359. }
  360. Cursor.Current = cursor;
  361. UpdateTooltip();
  362. }
  363. private void MouseoverWidget_MouseCellChanged(object sender, MouseCellChangedEventArgs e)
  364. {
  365. if (dragEdge >= 0)
  366. {
  367. var endDrag = navigationWidget.MouseCell;
  368. map.Metrics.Clip(ref endDrag, new Size(1, 1), Size.Empty);
  369. switch (dragEdge)
  370. {
  371. case 0:
  372. case 1:
  373. case 7:
  374. if (endDrag.Y < dragBounds.Bottom)
  375. {
  376. dragBounds.Height = dragBounds.Bottom - endDrag.Y;
  377. dragBounds.Y = endDrag.Y;
  378. }
  379. break;
  380. }
  381. switch (dragEdge)
  382. {
  383. case 5:
  384. case 6:
  385. case 7:
  386. if (endDrag.X < dragBounds.Right)
  387. {
  388. dragBounds.Width = dragBounds.Right - endDrag.X;
  389. dragBounds.X = endDrag.X;
  390. }
  391. break;
  392. }
  393. switch (dragEdge)
  394. {
  395. case 3:
  396. case 4:
  397. case 5:
  398. if (endDrag.Y > dragBounds.Top)
  399. {
  400. dragBounds.Height = endDrag.Y - dragBounds.Top;
  401. }
  402. break;
  403. }
  404. switch (dragEdge)
  405. {
  406. case 1:
  407. case 2:
  408. case 3:
  409. if (endDrag.X > dragBounds.Left)
  410. {
  411. dragBounds.Width = endDrag.X - dragBounds.Left;
  412. }
  413. break;
  414. }
  415. mapPanel.Invalidate();
  416. }
  417. else if (placementMode)
  418. {
  419. if (Control.MouseButtons == MouseButtons.Right)
  420. {
  421. RemoveTemplate(navigationWidget.MouseCell);
  422. }
  423. if (SelectedTemplateType != null)
  424. {
  425. foreach (var location in new Point[] { e.OldCell, e.NewCell })
  426. {
  427. for (var y = 0; y < SelectedTemplateType.IconHeight; ++y)
  428. {
  429. for (var x = 0; x < SelectedTemplateType.IconWidth; ++x)
  430. {
  431. mapPanel.Invalidate(map, new Point(location.X + x, location.Y + y));
  432. }
  433. }
  434. }
  435. }
  436. }
  437. else if((Control.MouseButtons == MouseButtons.Left) || (Control.MouseButtons == MouseButtons.Right))
  438. {
  439. PickTemplate(navigationWidget.MouseCell, Control.MouseButtons == MouseButtons.Left);
  440. }
  441. }
  442. private void RefreshMapPanel()
  443. {
  444. if (templateTypeNavigationWidget != null)
  445. {
  446. templateTypeNavigationWidget.Dispose();
  447. templateTypeNavigationWidget = null;
  448. }
  449. if (SelectedTemplateType != null)
  450. {
  451. templateTypeMapPanel.MapImage = SelectedTemplateType.Thumbnail;
  452. var templateTypeMetrics = new CellMetrics(SelectedTemplateType.IconWidth, SelectedTemplateType.IconHeight);
  453. templateTypeNavigationWidget = new NavigationWidget(templateTypeMapPanel, templateTypeMetrics,
  454. new Size(Globals.OriginalTileWidth / 4, Globals.OriginalTileHeight / 4));
  455. templateTypeNavigationWidget.MouseoverSize = Size.Empty;
  456. }
  457. else
  458. {
  459. templateTypeMapPanel.MapImage = null;
  460. }
  461. }
  462. private void SetTemplate(Point location)
  463. {
  464. if (SelectedTemplateType != null)
  465. {
  466. if (SelectedIcon.HasValue)
  467. {
  468. if (map.Metrics.GetCell(location, out int cell))
  469. {
  470. if (!undoTemplates.ContainsKey(cell))
  471. {
  472. undoTemplates[cell] = map.Templates[location];
  473. }
  474. var icon = (SelectedIcon.Value.Y * SelectedTemplateType.IconWidth) + SelectedIcon.Value.X;
  475. var template = new Template { Type = SelectedTemplateType, Icon = icon };
  476. map.Templates[cell] = template;
  477. redoTemplates[cell] = template;
  478. mapPanel.Invalidate(map, cell);
  479. plugin.Dirty = true;
  480. }
  481. }
  482. else
  483. {
  484. for (int y = 0, icon = 0; y < SelectedTemplateType.IconHeight; ++y)
  485. {
  486. for (var x = 0; x < SelectedTemplateType.IconWidth; ++x, ++icon)
  487. {
  488. var subLocation = new Point(location.X + x, location.Y + y);
  489. if (map.Metrics.GetCell(subLocation, out int cell))
  490. {
  491. if (!undoTemplates.ContainsKey(cell))
  492. {
  493. undoTemplates[cell] = map.Templates[subLocation];
  494. }
  495. }
  496. }
  497. }
  498. for (int y = 0, icon = 0; y < SelectedTemplateType.IconHeight; ++y)
  499. {
  500. for (var x = 0; x < SelectedTemplateType.IconWidth; ++x, ++icon)
  501. {
  502. if (!SelectedTemplateType.IconMask[x, y])
  503. {
  504. continue;
  505. }
  506. var subLocation = new Point(location.X + x, location.Y + y);
  507. if (map.Metrics.GetCell(subLocation, out int cell))
  508. {
  509. var template = new Template { Type = SelectedTemplateType, Icon = icon };
  510. map.Templates[cell] = template;
  511. redoTemplates[cell] = template;
  512. mapPanel.Invalidate(map, cell);
  513. plugin.Dirty = true;
  514. }
  515. }
  516. }
  517. }
  518. }
  519. }
  520. private void RemoveTemplate(Point location)
  521. {
  522. if (SelectedTemplateType != null)
  523. {
  524. if (SelectedIcon.HasValue)
  525. {
  526. if (map.Metrics.GetCell(location, out int cell))
  527. {
  528. if (!undoTemplates.ContainsKey(cell))
  529. {
  530. undoTemplates[cell] = map.Templates[location];
  531. }
  532. map.Templates[cell] = null;
  533. redoTemplates[cell] = null;
  534. mapPanel.Invalidate(map, cell);
  535. plugin.Dirty = true;
  536. }
  537. }
  538. else
  539. {
  540. for (int y = 0, icon = 0; y < SelectedTemplateType.IconHeight; ++y)
  541. {
  542. for (var x = 0; x < SelectedTemplateType.IconWidth; ++x, ++icon)
  543. {
  544. var subLocation = new Point(location.X + x, location.Y + y);
  545. if (map.Metrics.GetCell(subLocation, out int cell))
  546. {
  547. if (!undoTemplates.ContainsKey(cell))
  548. {
  549. undoTemplates[cell] = map.Templates[subLocation];
  550. }
  551. }
  552. }
  553. }
  554. for (int y = 0, icon = 0; y < SelectedTemplateType.IconHeight; ++y)
  555. {
  556. for (var x = 0; x < SelectedTemplateType.IconWidth; ++x, ++icon)
  557. {
  558. var subLocation = new Point(location.X + x, location.Y + y);
  559. if (map.Metrics.GetCell(subLocation, out int cell))
  560. {
  561. map.Templates[cell] = null;
  562. redoTemplates[cell] = null;
  563. mapPanel.Invalidate(map, cell);
  564. plugin.Dirty = true;
  565. }
  566. }
  567. }
  568. }
  569. }
  570. }
  571. private void EnterPlacementMode()
  572. {
  573. if (placementMode || boundsMode)
  574. {
  575. return;
  576. }
  577. placementMode = true;
  578. navigationWidget.MouseoverSize = Size.Empty;
  579. if (SelectedTemplateType != null)
  580. {
  581. for (var y = 0; y < SelectedTemplateType.IconHeight; ++y)
  582. {
  583. for (var x = 0; x < SelectedTemplateType.IconWidth; ++x)
  584. {
  585. mapPanel.Invalidate(map, new Point(navigationWidget.MouseCell.X + x, navigationWidget.MouseCell.Y + y));
  586. }
  587. }
  588. }
  589. UpdateStatus();
  590. }
  591. private void EnterBoundsMode()
  592. {
  593. if (boundsMode || placementMode)
  594. {
  595. return;
  596. }
  597. boundsMode = true;
  598. dragBounds = map.Bounds;
  599. navigationWidget.MouseoverSize = Size.Empty;
  600. if (SelectedTemplateType != null)
  601. {
  602. for (var y = 0; y < SelectedTemplateType.IconHeight; ++y)
  603. {
  604. for (var x = 0; x < SelectedTemplateType.IconWidth; ++x)
  605. {
  606. mapPanel.Invalidate(map, new Point(navigationWidget.MouseCell.X + x, navigationWidget.MouseCell.Y + y));
  607. }
  608. }
  609. }
  610. UpdateTooltip();
  611. UpdateStatus();
  612. }
  613. private void ExitAllModes()
  614. {
  615. if (!placementMode && !boundsMode)
  616. {
  617. return;
  618. }
  619. boundsMode = false;
  620. dragEdge = -1;
  621. dragBounds = Rectangle.Empty;
  622. placementMode = false;
  623. navigationWidget.MouseoverSize = new Size(1, 1);
  624. if (SelectedTemplateType != null)
  625. {
  626. for (var y = 0; y < SelectedTemplateType.IconHeight; ++y)
  627. {
  628. for (var x = 0; x < SelectedTemplateType.IconWidth; ++x)
  629. {
  630. mapPanel.Invalidate(map, new Point(navigationWidget.MouseCell.X + x, navigationWidget.MouseCell.Y + y));
  631. }
  632. }
  633. }
  634. UpdateTooltip();
  635. UpdateStatus();
  636. }
  637. private void UpdateTooltip()
  638. {
  639. if (boundsMode)
  640. {
  641. var tooltip = string.Format("X = {0}\nY = {1}\nWidth = {2}\nHeight = {3}", dragBounds.Left, dragBounds.Top, dragBounds.Width, dragBounds.Height);
  642. var textSize = TextRenderer.MeasureText(tooltip, SystemFonts.CaptionFont);
  643. var tooltipSize = new Size(textSize.Width + 6, textSize.Height + 6);
  644. var tooltipPosition = mapPanel.PointToClient(Control.MousePosition);
  645. switch (dragEdge)
  646. {
  647. case -1:
  648. case 0:
  649. case 1:
  650. case 7:
  651. tooltipPosition.Y -= tooltipSize.Height;
  652. break;
  653. }
  654. switch (dragEdge)
  655. {
  656. case -1:
  657. case 5:
  658. case 6:
  659. case 7:
  660. tooltipPosition.X -= tooltipSize.Width;
  661. break;
  662. }
  663. var screenPosition = mapPanel.PointToScreen(tooltipPosition);
  664. var screen = Screen.FromControl(mapPanel);
  665. screenPosition.X = Math.Max(0, Math.Min(screen.WorkingArea.Width - tooltipSize.Width, screenPosition.X));
  666. screenPosition.Y = Math.Max(0, Math.Min(screen.WorkingArea.Height - tooltipSize.Height, screenPosition.Y));
  667. tooltipPosition = mapPanel.PointToClient(screenPosition);
  668. mouseTooltip.Show(tooltip, mapPanel, tooltipPosition.X, tooltipPosition.Y);
  669. }
  670. else
  671. {
  672. mouseTooltip.Hide(mapPanel);
  673. }
  674. }
  675. private void PickTemplate(Point location, bool wholeTemplate)
  676. {
  677. if (map.Metrics.GetCell(location, out int cell))
  678. {
  679. var template = map.Templates[cell];
  680. if (template != null)
  681. {
  682. SelectedTemplateType = template.Type;
  683. }
  684. else
  685. {
  686. SelectedTemplateType = map.TemplateTypes.Where(t => t.Equals("clear1")).FirstOrDefault();
  687. }
  688. if (!wholeTemplate && ((SelectedTemplateType.IconWidth * SelectedTemplateType.IconHeight) > 1))
  689. {
  690. var icon = template?.Icon ?? 0;
  691. SelectedIcon = new Point(icon % SelectedTemplateType.IconWidth, icon / SelectedTemplateType.IconWidth);
  692. }
  693. else
  694. {
  695. SelectedIcon = null;
  696. }
  697. }
  698. }
  699. private int DetectDragEdge()
  700. {
  701. var mouseCell = navigationWidget.MouseCell;
  702. var mousePixel = navigationWidget.MouseSubPixel;
  703. var topEdge =
  704. ((mouseCell.Y == dragBounds.Top) && (mousePixel.Y <= (Globals.PixelHeight / 4))) ||
  705. ((mouseCell.Y == dragBounds.Top - 1) && (mousePixel.Y >= (3 * Globals.PixelHeight / 4)));
  706. var bottomEdge =
  707. ((mouseCell.Y == dragBounds.Bottom) && (mousePixel.Y <= (Globals.PixelHeight / 4))) ||
  708. ((mouseCell.Y == dragBounds.Bottom - 1) && (mousePixel.Y >= (3 * Globals.PixelHeight / 4)));
  709. var leftEdge =
  710. ((mouseCell.X == dragBounds.Left) && (mousePixel.X <= (Globals.PixelWidth / 4))) ||
  711. ((mouseCell.X == dragBounds.Left - 1) && (mousePixel.X >= (3 * Globals.PixelWidth / 4)));
  712. var rightEdge =
  713. ((mouseCell.X == dragBounds.Right) && (mousePixel.X <= (Globals.PixelHeight / 4))) ||
  714. ((mouseCell.X == dragBounds.Right - 1) && (mousePixel.X >= (3 * Globals.PixelHeight / 4)));
  715. if (topEdge)
  716. {
  717. if (rightEdge)
  718. {
  719. return 1;
  720. }
  721. else if (leftEdge)
  722. {
  723. return 7;
  724. }
  725. else
  726. {
  727. return 0;
  728. }
  729. }
  730. else if (bottomEdge)
  731. {
  732. if (rightEdge)
  733. {
  734. return 3;
  735. }
  736. else if (leftEdge)
  737. {
  738. return 5;
  739. }
  740. else
  741. {
  742. return 4;
  743. }
  744. }
  745. else if (rightEdge)
  746. {
  747. return 2;
  748. }
  749. else if (leftEdge)
  750. {
  751. return 6;
  752. }
  753. else
  754. {
  755. return -1;
  756. }
  757. }
  758. private void CommitChange()
  759. {
  760. var undoTemplates2 = new Dictionary<int, Template>(undoTemplates);
  761. void undoAction(UndoRedoEventArgs e)
  762. {
  763. foreach (var kv in undoTemplates2)
  764. {
  765. e.Map.Templates[kv.Key] = kv.Value;
  766. }
  767. e.MapPanel.Invalidate(e.Map, undoTemplates2.Keys);
  768. }
  769. var redoTemplates2 = new Dictionary<int, Template>(redoTemplates);
  770. void redoAction(UndoRedoEventArgs e)
  771. {
  772. foreach (var kv in redoTemplates2)
  773. {
  774. e.Map.Templates[kv.Key] = kv.Value;
  775. }
  776. e.MapPanel.Invalidate(e.Map, redoTemplates2.Keys);
  777. }
  778. undoTemplates.Clear();
  779. redoTemplates.Clear();
  780. url.Track(undoAction, redoAction);
  781. }
  782. private void UpdateStatus()
  783. {
  784. if (placementMode)
  785. {
  786. statusLbl.Text = "Left-Click to place template, Right-Click to clear template";
  787. }
  788. else if (boundsMode)
  789. {
  790. if (dragEdge >= 0)
  791. {
  792. statusLbl.Text = "Release left button to end dragging map bounds edge";
  793. }
  794. else
  795. {
  796. statusLbl.Text = "Left-Click a map bounds edge to start dragging";
  797. }
  798. }
  799. else
  800. {
  801. statusLbl.Text = "Shift to enter placement mode, Ctrl to enter map bounds mode, Left-Click to pick whole template, Right-Click to pick individual template tile";
  802. }
  803. }
  804. protected override void PreRenderMap()
  805. {
  806. base.PreRenderMap();
  807. previewMap = map.Clone();
  808. if (placementMode)
  809. {
  810. var location = navigationWidget.MouseCell;
  811. if (SelectedTemplateType != null)
  812. {
  813. if (SelectedIcon.HasValue)
  814. {
  815. if (previewMap.Metrics.GetCell(location, out int cell))
  816. {
  817. var icon = (SelectedIcon.Value.Y * SelectedTemplateType.IconWidth) + SelectedIcon.Value.X;
  818. previewMap.Templates[cell] = new Template { Type = SelectedTemplateType, Icon = icon };
  819. }
  820. }
  821. else
  822. {
  823. int icon = 0;
  824. for (var y = 0; y < SelectedTemplateType.IconHeight; ++y)
  825. {
  826. for (var x = 0; x < SelectedTemplateType.IconWidth; ++x, ++icon)
  827. {
  828. if (!SelectedTemplateType.IconMask[x, y])
  829. {
  830. continue;
  831. }
  832. var subLocation = new Point(location.X + x, location.Y + y);
  833. if (previewMap.Metrics.GetCell(subLocation, out int cell))
  834. {
  835. previewMap.Templates[cell] = new Template { Type = SelectedTemplateType, Icon = icon };
  836. }
  837. }
  838. }
  839. }
  840. }
  841. }
  842. }
  843. protected override void PostRenderMap(Graphics graphics)
  844. {
  845. base.PostRenderMap(graphics);
  846. if (boundsMode)
  847. {
  848. var bounds = Rectangle.FromLTRB(
  849. dragBounds.Left * Globals.TileWidth,
  850. dragBounds.Top * Globals.TileHeight,
  851. dragBounds.Right * Globals.TileWidth,
  852. dragBounds.Bottom * Globals.TileHeight
  853. );
  854. var boundsPen = new Pen(Color.Red, 8.0f);
  855. graphics.DrawRectangle(boundsPen, bounds);
  856. }
  857. else if (placementMode)
  858. {
  859. var location = navigationWidget.MouseCell;
  860. if (SelectedTemplateType != null)
  861. {
  862. var previewPen = new Pen(Color.Green, 4.0f);
  863. var previewBounds = new Rectangle(
  864. location.X * Globals.TileWidth,
  865. location.Y * Globals.TileHeight,
  866. (SelectedIcon.HasValue ? 1 : SelectedTemplateType.IconWidth) * Globals.TileWidth,
  867. (SelectedIcon.HasValue ? 1 : SelectedTemplateType.IconHeight) * Globals.TileHeight
  868. );
  869. graphics.DrawRectangle(previewPen, previewBounds);
  870. }
  871. }
  872. }
  873. #region IDisposable Support
  874. private bool disposedValue = false;
  875. protected override void Dispose(bool disposing)
  876. {
  877. if (!disposedValue)
  878. {
  879. if (disposing)
  880. {
  881. mapPanel.MouseDown -= MapPanel_MouseDown;
  882. mapPanel.MouseUp -= MapPanel_MouseUp;
  883. mapPanel.MouseMove -= MapPanel_MouseMove;
  884. (mapPanel as Control).KeyDown -= TemplateTool_KeyDown;
  885. (mapPanel as Control).KeyUp -= TemplateTool_KeyUp;
  886. templateTypeListView.SelectedIndexChanged -= TemplateTypeListView_SelectedIndexChanged;
  887. templateTypeMapPanel.MouseDown -= TemplateTypeMapPanel_MouseDown;
  888. templateTypeMapPanel.PostRender -= TemplateTypeMapPanel_PostRender;
  889. navigationWidget.MouseCellChanged -= MouseoverWidget_MouseCellChanged;
  890. url.Undone -= Url_Undone;
  891. url.Redone -= Url_Redone;
  892. }
  893. disposedValue = true;
  894. }
  895. base.Dispose(disposing);
  896. }
  897. #endregion
  898. }
  899. }