123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- //
- // Copyright 2020 Electronic Arts Inc.
- //
- // The Command & Conquer Map Editor and corresponding source code is free
- // software: you can redistribute it and/or modify it under the terms of
- // the GNU General Public License as published by the Free Software Foundation,
- // either version 3 of the License, or (at your option) any later version.
- // The Command & Conquer Map Editor and corresponding source code is distributed
- // in the hope that it will be useful, but with permitted additional restrictions
- // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
- // distributed with this program. You should have received a copy of the
- // GNU General Public License along with permitted additional restrictions
- // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
- using MobiusEditor.Controls;
- using MobiusEditor.Event;
- using MobiusEditor.Interface;
- using MobiusEditor.Model;
- using MobiusEditor.Utility;
- using MobiusEditor.Widgets;
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Linq;
- using System.Windows.Forms;
- namespace MobiusEditor.Tools
- {
- public class TerrainTool : ViewTool
- {
- private readonly TypeComboBox terrainTypeComboBox;
- private readonly MapPanel terrainTypeMapPanel;
- private readonly TerrainProperties terrainProperties;
- private Map previewMap;
- protected override Map RenderMap => previewMap;
- private bool placementMode;
- private readonly Terrain mockTerrain;
- private Terrain selectedTerrain;
- private Point selectedTerrainPivot;
- private TerrainType selectedTerrainType;
- private TerrainPropertiesPopup selectedTerrainProperties;
- private TerrainType SelectedTerrainType
- {
- get => selectedTerrainType;
- set
- {
- if (selectedTerrainType != value)
- {
- if (placementMode && (selectedTerrainType != null))
- {
- mapPanel.Invalidate(map, new Rectangle(navigationWidget.MouseCell, selectedTerrainType.OverlapBounds.Size));
- }
- selectedTerrainType = value;
- terrainTypeComboBox.SelectedValue = selectedTerrainType;
- if (placementMode && (selectedTerrainType != null))
- {
- mapPanel.Invalidate(map, new Rectangle(navigationWidget.MouseCell, selectedTerrainType.OverlapBounds.Size));
- }
- mockTerrain.Type = selectedTerrainType;
- mockTerrain.Icon = selectedTerrainType.IsTransformable ? 22 : 0;
- RefreshMapPanel();
- }
- }
- }
- public TerrainTool(MapPanel mapPanel, MapLayerFlag layers, ToolStripStatusLabel statusLbl, TypeComboBox terrainTypeComboBox, MapPanel terrainTypeMapPanel, TerrainProperties terrainProperties, IGamePlugin plugin, UndoRedoList<UndoRedoEventArgs> url)
- : base(mapPanel, layers, statusLbl, plugin, url)
- {
- previewMap = map;
- mockTerrain = new Terrain();
- mockTerrain.PropertyChanged += MockTerrain_PropertyChanged;
- this.mapPanel.MouseDown += MapPanel_MouseDown;
- this.mapPanel.MouseMove += MapPanel_MouseMove;
- this.mapPanel.MouseUp += MapPanel_MouseUp;
- this.mapPanel.MouseDoubleClick += MapPanel_MouseDoubleClick;
- (this.mapPanel as Control).KeyDown += TerrainTool_KeyDown;
- (this.mapPanel as Control).KeyUp += TerrainTool_KeyUp;
- this.terrainTypeComboBox = terrainTypeComboBox;
- this.terrainTypeComboBox.SelectedIndexChanged += TerrainTypeCombo_SelectedIndexChanged;
- this.terrainTypeMapPanel = terrainTypeMapPanel;
- this.terrainTypeMapPanel.BackColor = Color.White;
- this.terrainTypeMapPanel.MaxZoom = 1;
- this.terrainProperties = terrainProperties;
- this.terrainProperties.Terrain = mockTerrain;
- this.terrainProperties.Visible = plugin.GameType == GameType.TiberianDawn;
- navigationWidget.MouseCellChanged += MouseoverWidget_MouseCellChanged;
- SelectedTerrainType = terrainTypeComboBox.Types.First() as TerrainType;
- UpdateStatus();
- }
- private void MapPanel_MouseDoubleClick(object sender, MouseEventArgs e)
- {
- if (Control.ModifierKeys != Keys.None)
- {
- return;
- }
- if (map.Metrics.GetCell(navigationWidget.MouseCell, out int cell))
- {
- if (map.Technos[cell] is Terrain terrain)
- {
- selectedTerrain = null;
- selectedTerrainProperties?.Close();
- selectedTerrainProperties = new TerrainPropertiesPopup(terrainProperties.Plugin, terrain);
- selectedTerrainProperties.Closed += (cs, ce) =>
- {
- navigationWidget.Refresh();
- };
- selectedTerrainProperties.Show(mapPanel, mapPanel.PointToClient(Control.MousePosition));
- UpdateStatus();
- }
- }
- }
- private void MockTerrain_PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- RefreshMapPanel();
- }
- private void TerrainTypeCombo_SelectedIndexChanged(object sender, EventArgs e)
- {
- SelectedTerrainType = terrainTypeComboBox.SelectedValue as TerrainType;
- }
- private void TerrainTool_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.ShiftKey)
- {
- EnterPlacementMode();
- }
- }
- private void TerrainTool_KeyUp(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.ShiftKey)
- {
- ExitPlacementMode();
- }
- }
- private void MapPanel_MouseDown(object sender, MouseEventArgs e)
- {
- if (placementMode)
- {
- if (e.Button == MouseButtons.Left)
- {
- AddTerrain(navigationWidget.MouseCell);
- }
- else if (e.Button == MouseButtons.Right)
- {
- RemoveTerrain(navigationWidget.MouseCell);
- }
- }
- else if (e.Button == MouseButtons.Left)
- {
- SelectTerrain(navigationWidget.MouseCell);
- }
- else if (e.Button == MouseButtons.Right)
- {
- PickTerrain(navigationWidget.MouseCell);
- }
- }
- private void MapPanel_MouseUp(object sender, MouseEventArgs e)
- {
- if (selectedTerrain != null)
- {
- selectedTerrain = null;
- selectedTerrainPivot = Point.Empty;
- UpdateStatus();
- }
- }
- private void MapPanel_MouseMove(object sender, MouseEventArgs e)
- {
- if (!placementMode && (Control.ModifierKeys == Keys.Shift))
- {
- EnterPlacementMode();
- }
- else if (placementMode && (Control.ModifierKeys == Keys.None))
- {
- ExitPlacementMode();
- }
- }
- private void MouseoverWidget_MouseCellChanged(object sender, MouseCellChangedEventArgs e)
- {
- if (placementMode)
- {
- if (SelectedTerrainType != null)
- {
- mapPanel.Invalidate(map, new Rectangle(e.OldCell, SelectedTerrainType.OverlapBounds.Size));
- mapPanel.Invalidate(map, new Rectangle(e.NewCell, SelectedTerrainType.OverlapBounds.Size));
- }
- }
- else if (selectedTerrain != null)
- {
- var oldLocation = map.Technos[selectedTerrain].Value;
- var newLocation = new Point(Math.Max(0, e.NewCell.X - selectedTerrainPivot.X), Math.Max(0, e.NewCell.Y - selectedTerrainPivot.Y));
- mapPanel.Invalidate(map, selectedTerrain);
- map.Technos.Remove(selectedTerrain);
- if (map.Technos.Add(newLocation, selectedTerrain))
- {
- mapPanel.Invalidate(map, selectedTerrain);
- }
- else
- {
- map.Technos.Add(oldLocation, selectedTerrain);
- }
- }
- }
- private void AddTerrain(Point location)
- {
- if (!map.Metrics.Contains(location))
- {
- return;
- }
- if (SelectedTerrainType != null)
- {
- var terrain = mockTerrain.Clone();
- if (map.Technos.Add(location, terrain))
- {
- mapPanel.Invalidate(map, terrain);
- void undoAction(UndoRedoEventArgs e)
- {
- e.MapPanel.Invalidate(e.Map, location);
- e.Map.Technos.Remove(terrain);
- }
- void redoAction(UndoRedoEventArgs e)
- {
- e.Map.Technos.Add(location, terrain);
- e.MapPanel.Invalidate(e.Map, location);
- }
- url.Track(undoAction, redoAction);
- plugin.Dirty = true;
- }
- }
- }
- private void RemoveTerrain(Point location)
- {
- if (map.Technos[location] is Terrain terrain)
- {
- mapPanel.Invalidate(map, terrain);
- map.Technos.Remove(location);
- void undoAction(UndoRedoEventArgs e)
- {
- e.Map.Technos.Add(location, terrain);
- e.MapPanel.Invalidate(e.Map, location);
- }
- void redoAction(UndoRedoEventArgs e)
- {
- e.MapPanel.Invalidate(e.Map, location);
- e.Map.Technos.Remove(terrain);
- }
- url.Track(undoAction, redoAction);
- plugin.Dirty = true;
- }
- }
- private void EnterPlacementMode()
- {
- if (placementMode)
- {
- return;
- }
- placementMode = true;
- navigationWidget.MouseoverSize = Size.Empty;
- if (SelectedTerrainType != null)
- {
- mapPanel.Invalidate(map, new Rectangle(navigationWidget.MouseCell, selectedTerrainType.OverlapBounds.Size));
- }
- UpdateStatus();
- }
- private void ExitPlacementMode()
- {
- if (!placementMode)
- {
- return;
- }
- placementMode = false;
- navigationWidget.MouseoverSize = new Size(1, 1);
- if (SelectedTerrainType != null)
- {
- mapPanel.Invalidate(map, new Rectangle(navigationWidget.MouseCell, selectedTerrainType.OverlapBounds.Size));
- }
- UpdateStatus();
- }
- private void PickTerrain(Point location)
- {
- if (map.Metrics.GetCell(location, out int cell))
- {
- if (map.Technos[cell] is Terrain terrain)
- {
- SelectedTerrainType = terrain.Type;
- mockTerrain.Trigger = terrain.Trigger;
- }
- }
- }
- private void SelectTerrain(Point location)
- {
- if (map.Metrics.GetCell(location, out int cell))
- {
- selectedTerrain = map.Technos[cell] as Terrain;
- selectedTerrainPivot = (selectedTerrain != null) ? (location - (Size)map.Technos[selectedTerrain].Value) : Point.Empty;
- }
- UpdateStatus();
- }
- private void RefreshMapPanel()
- {
- terrainTypeMapPanel.MapImage = mockTerrain.Type.Thumbnail;
- }
- private void UpdateStatus()
- {
- if (placementMode)
- {
- statusLbl.Text = "Left-Click to place terrain, Right-Click to remove terrain";
- }
- else if (selectedTerrain != null)
- {
- statusLbl.Text = "Drag mouse to move terrain";
- }
- else
- {
- statusLbl.Text = "Shift to enter placement mode, Left-Click drag to move terrain, Double-Click update terrain properties, Right-Click to pick terrain";
- }
- }
- protected override void PreRenderMap()
- {
- base.PreRenderMap();
- previewMap = map.Clone();
- if (placementMode)
- {
- var location = navigationWidget.MouseCell;
- if (SelectedTerrainType != null)
- {
- if (previewMap.Metrics.Contains(location))
- {
- var terrain = new Terrain
- {
- Type = SelectedTerrainType,
- Icon = SelectedTerrainType.IsTransformable ? 22 : 0,
- Tint = Color.FromArgb(128, Color.White)
- };
- previewMap.Technos.Add(location, terrain);
- }
- }
- }
- }
- protected override void PostRenderMap(Graphics graphics)
- {
- base.PostRenderMap(graphics);
- var terrainPen = new Pen(Color.Green, 4.0f);
- var occupyPen = new Pen(Color.Red, 2.0f);
- foreach (var (topLeft, terrain) in previewMap.Technos.OfType<Terrain>())
- {
- var bounds = new Rectangle(new Point(topLeft.X * Globals.TileWidth, topLeft.Y * Globals.TileHeight), terrain.Type.RenderSize);
- graphics.DrawRectangle(terrainPen, bounds);
- for (var y = 0; y < terrain.Type.OccupyMask.GetLength(0); ++y)
- {
- for (var x = 0; x < terrain.Type.OccupyMask.GetLength(1); ++x)
- {
- if (terrain.Type.OccupyMask[y, x])
- {
- var occupyBounds = new Rectangle(
- new Point((topLeft.X + x) * Globals.TileWidth, (topLeft.Y + y) * Globals.TileHeight),
- Globals.TileSize
- );
- graphics.DrawRectangle(occupyPen, occupyBounds);
- }
- }
- }
- }
- }
- #region IDisposable Support
- private bool disposedValue = false;
- protected override void Dispose(bool disposing)
- {
- if (!disposedValue)
- {
- if (disposing)
- {
- selectedTerrainProperties?.Close();
- mapPanel.MouseDown -= MapPanel_MouseDown;
- mapPanel.MouseMove -= MapPanel_MouseMove;
- mapPanel.MouseUp -= MapPanel_MouseUp;
- mapPanel.MouseDoubleClick -= MapPanel_MouseDoubleClick;
- (mapPanel as Control).KeyDown -= TerrainTool_KeyDown;
- (mapPanel as Control).KeyUp -= TerrainTool_KeyUp;
- terrainTypeComboBox.SelectedIndexChanged -= TerrainTypeCombo_SelectedIndexChanged;
- navigationWidget.MouseCellChanged -= MouseoverWidget_MouseCellChanged;
- }
- disposedValue = true;
- }
- base.Dispose(disposing);
- }
- #endregion
- }
- }
|