NavigationWidget.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.Interface;
  16. using MobiusEditor.Model;
  17. using System;
  18. using System.Drawing;
  19. using System.Windows.Forms;
  20. namespace MobiusEditor.Widgets
  21. {
  22. public class MouseCellChangedEventArgs : EventArgs
  23. {
  24. public Point OldCell { get; private set; }
  25. public Point NewCell { get; private set; }
  26. public MouseCellChangedEventArgs(Point oldCell, Point newCell)
  27. {
  28. OldCell = oldCell;
  29. NewCell = newCell;
  30. }
  31. }
  32. public class NavigationWidget : IWidget
  33. {
  34. private static readonly Pen defaultMouseoverPen = new Pen(Color.Yellow, 4);
  35. private readonly MapPanel mapPanel;
  36. private readonly Size cellSize;
  37. private bool dragging = false;
  38. private Point lastMouseLocation;
  39. public CellMetrics Metrics { get; private set; }
  40. public Point MouseCell { get; private set; }
  41. public Point MouseSubPixel { get; private set; }
  42. private Size mouseoverSize = new Size(1, 1);
  43. public Size MouseoverSize
  44. {
  45. get => mouseoverSize;
  46. set => mouseoverSize = !value.IsEmpty ? new Size(value.Width | 1, value.Height | 1) : Size.Empty;
  47. }
  48. public event EventHandler<MouseCellChangedEventArgs> MouseCellChanged;
  49. public NavigationWidget(MapPanel mapPanel, CellMetrics metrics, Size cellSize)
  50. {
  51. this.mapPanel = mapPanel;
  52. this.mapPanel.MouseDown += MapPanel_MouseDown;
  53. this.mapPanel.MouseMove += MapPanel_MouseMove;
  54. Metrics = metrics;
  55. this.cellSize = cellSize;
  56. }
  57. public void Refresh()
  58. {
  59. OnMouseMove(mapPanel.PointToClient(Control.MousePosition));
  60. }
  61. private void MapPanel_MouseDown(object sender, MouseEventArgs e)
  62. {
  63. if ((e.Button & MouseButtons.Middle) != MouseButtons.None)
  64. {
  65. lastMouseLocation = e.Location;
  66. dragging = true;
  67. }
  68. }
  69. private void MapPanel_MouseMove(object sender, MouseEventArgs e)
  70. {
  71. OnMouseMove(e.Location);
  72. }
  73. private void OnMouseMove(Point location)
  74. {
  75. if (dragging)
  76. {
  77. if ((Control.MouseButtons & MouseButtons.Middle) == MouseButtons.None)
  78. {
  79. dragging = false;
  80. }
  81. else
  82. {
  83. var delta = location - (Size)lastMouseLocation;
  84. if (!delta.IsEmpty)
  85. {
  86. mapPanel.AutoScrollPosition = new Point(-mapPanel.AutoScrollPosition.X - delta.X, -mapPanel.AutoScrollPosition.Y - delta.Y);
  87. }
  88. }
  89. }
  90. lastMouseLocation = location;
  91. var newMousePosition = mapPanel.ClientToMap(location);
  92. MouseSubPixel = new Point(
  93. (newMousePosition.X * Globals.PixelWidth / cellSize.Width) % Globals.PixelWidth,
  94. (newMousePosition.Y * Globals.PixelHeight / cellSize.Height) % Globals.PixelHeight
  95. );
  96. var newMouseCell = new Point(newMousePosition.X / cellSize.Width, newMousePosition.Y / cellSize.Height);
  97. if (MouseCell == newMouseCell)
  98. {
  99. return;
  100. }
  101. if (!Metrics.Contains(newMouseCell))
  102. {
  103. return;
  104. }
  105. var oldCell = MouseCell;
  106. MouseCell = newMouseCell;
  107. MouseCellChanged?.Invoke(this, new MouseCellChangedEventArgs(oldCell, MouseCell));
  108. mapPanel.Invalidate();
  109. }
  110. public void Render(Graphics graphics)
  111. {
  112. if (!MouseoverSize.IsEmpty)
  113. {
  114. var rect = new Rectangle(new Point(MouseCell.X * cellSize.Width, MouseCell.Y * cellSize.Height), cellSize);
  115. rect.Inflate(cellSize.Width * (MouseoverSize.Width / 2), cellSize.Height * (MouseoverSize.Height / 2));
  116. graphics.DrawRectangle(defaultMouseoverPen, rect);
  117. }
  118. }
  119. #region IDisposable Support
  120. private bool disposedValue = false;
  121. protected virtual void Dispose(bool disposing)
  122. {
  123. if (!disposedValue)
  124. {
  125. if (disposing)
  126. {
  127. mapPanel.MouseDown -= MapPanel_MouseDown;
  128. mapPanel.MouseMove -= MapPanel_MouseMove;
  129. }
  130. disposedValue = true;
  131. }
  132. }
  133. public void Dispose()
  134. {
  135. Dispose(true);
  136. }
  137. #endregion
  138. }
  139. }