Skip to content

Commit

Permalink
Support tile placement height adjustment keys when pasting terrain
Browse files Browse the repository at this point in the history
  • Loading branch information
Rampastring committed Feb 15, 2024
1 parent db11269 commit 53b8cce
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/TSMapEditor/Mutations/Classes/PasteTerrainMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,12 @@ public void Deserialize(byte[] bytes)
/// </summary>
public class PasteTerrainMutation : Mutation
{
public PasteTerrainMutation(IMutationTarget mutationTarget, CopiedMapData copiedMapData, Point2D origin, bool allowOverlap) : base(mutationTarget)
public PasteTerrainMutation(IMutationTarget mutationTarget, CopiedMapData copiedMapData, Point2D origin, bool allowOverlap, int heightOffset) : base(mutationTarget)
{
this.copiedMapData = copiedMapData;
this.origin = origin;
this.allowOverlap = allowOverlap;
this.heightOffset = heightOffset;
}

private struct PlacedInfantryInfo
Expand All @@ -481,6 +482,7 @@ public PlacedInfantryInfo(Point2D coords, SubCell subCell)
private readonly CopiedMapData copiedMapData;
private readonly Point2D origin;
private readonly bool allowOverlap;
private readonly int heightOffset;

private OriginalCellTerrainData[] terrainUndoData;
private OriginalOverlayInfo[] overlayUndoData;
Expand Down Expand Up @@ -525,7 +527,7 @@ public override void Perform()
cell.TileImage = null;
cell.TileIndex = copiedTerrainData.TileIndex;
cell.SubTileIndex = copiedTerrainData.SubTileIndex;
cell.Level = (byte)(originLevel + copiedTerrainData.HeightOffset);
cell.Level = (byte)Math.Max(0, Math.Min(Constants.MaxMapHeightLevel, originLevel + copiedTerrainData.HeightOffset + heightOffset));
}

this.terrainUndoData = terrainUndoData.ToArray();
Expand Down
32 changes: 30 additions & 2 deletions src/TSMapEditor/UI/CursorActions/PasteTerrainCursorAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public PasteTerrainCursorAction(ICursorActionTarget cursorActionTarget, RKeyboar

public override string GetName() => "Paste Copied Terrain";

public override bool HandlesKeyboardInput => true;

struct OriginalOverlayInfo
{
public Point2D CellCoords;
Expand Down Expand Up @@ -52,10 +54,36 @@ struct OriginalSmudgeInfo

private RKeyboard keyboard;

private int originLevelOffset;


public override void OnKeyPressed(KeyPressEventArgs e)
{
if (Constants.IsFlatWorld)
return;

if (KeyboardCommands.Instance.AdjustTileHeightDown.Key.Key == e.PressedKey)
{
if (originLevelOffset > -Constants.MaxMapHeight)
originLevelOffset--;

e.Handled = true;
}
else if (KeyboardCommands.Instance.AdjustTileHeightUp.Key.Key == e.PressedKey)
{
if (originLevelOffset < Constants.MaxMapHeight)
originLevelOffset++;

e.Handled = true;
}
}

public override void OnActionEnter()
{
base.OnActionEnter();

originLevelOffset = 0;

if (!System.Windows.Forms.Clipboard.ContainsData(Constants.ClipboardMapDataFormatValue))
{
Logger.Log(nameof(PasteTerrainCursorAction) + ": invalid clipboard data format, exiting action");
Expand Down Expand Up @@ -98,7 +126,7 @@ public override void PreMapDraw(Point2D cellCoords)
var terrainEntry = entry as CopiedTerrainEntry;
cell.PreviewTileImage = CursorActionTarget.TheaterGraphics.GetTileGraphics(terrainEntry.TileIndex, 0);
cell.PreviewSubTileIndex = terrainEntry.SubTileIndex;
cell.PreviewLevel = Math.Min(Constants.MaxMapHeightLevel, originLevel + terrainEntry.HeightOffset);
cell.PreviewLevel = Math.Max(0, Math.Min(Constants.MaxMapHeightLevel, originLevel + terrainEntry.HeightOffset + originLevelOffset));
}
else if (entry.EntryType == CopiedEntryType.Overlay)
{
Expand Down Expand Up @@ -208,7 +236,7 @@ public override void LeftClick(Point2D cellCoords)

bool allowOverlap = KeyboardCommands.Instance.OverlapObjects.AreKeysOrModifiersDown(keyboard);

var mutation = new PasteTerrainMutation(CursorActionTarget.MutationTarget, copiedMapData, cellCoords, allowOverlap);
var mutation = new PasteTerrainMutation(CursorActionTarget.MutationTarget, copiedMapData, cellCoords, allowOverlap, originLevelOffset);
CursorActionTarget.MutationManager.PerformMutation(mutation);
}
}
Expand Down

0 comments on commit 53b8cce

Please sign in to comment.