Skip to content

Commit 684733e

Browse files
committed
Improve preview of terrain pasting to better take height level into account
1 parent 787f099 commit 684733e

4 files changed

Lines changed: 38 additions & 8 deletions

File tree

src/TSMapEditor/Models/MapTile.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ public MapTile(byte[] data) : base(data) { }
5757

5858
public List<(Structure Source, double DistanceInLeptons)> LightSources { get; set; } = new();
5959

60+
/// <summary>
61+
/// Returns the display height level of this cell.
62+
/// If the cell has a valid preview height level defined, returns the preview height level.
63+
/// Otherwise returns the actual, non-preview height level.
64+
/// </summary>
65+
public int GetLevelOrPreviewLevel()
66+
{
67+
if (PreviewLevel > -1)
68+
return PreviewLevel;
69+
70+
return Level;
71+
}
72+
6073
public void ApplyPreview(TileImage previewTileImage, int previewSubTileIndex, int previewLevel, Lighting lighting, LightingPreviewMode lightingPreviewMode, bool lightDisabledLightSources)
6174
{
6275
PreviewTileImage = previewTileImage;

src/TSMapEditor/Rendering/MapView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1643,8 +1643,8 @@ public void Draw(bool isActive, TechnoBase technoUnderCursor, MapTile tileUnderC
16431643

16441644
if (isActive && tileUnderCursor != null && cursorAction != null)
16451645
{
1646-
cursorAction.PostMapDraw(tileUnderCursor.CoordsToPoint());
16471646
cursorAction.DrawPreview(tileUnderCursor.CoordsToPoint(), Camera.TopLeftPoint);
1647+
cursorAction.PostMapDraw(tileUnderCursor.CoordsToPoint());
16481648
}
16491649
}
16501650

src/TSMapEditor/UI/CursorActions/CopyTerrainCursorActionBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ protected void CopyFromCells(List<Point2D> cellsToCopy)
4747
}
4848
});
4949

50-
copiedMapData.Width = (ushort)(endX - startX);
51-
copiedMapData.Height = (ushort)(endY - startY);
50+
copiedMapData.Width = (ushort)((endX - startX) + 1);
51+
copiedMapData.Height = (ushort)((endY - startY) + 1);
5252

5353
// To handle height, we first look up the lowest height level of the copied
5454
// area. Any cell higher than that gets assigned an offset for its height.

src/TSMapEditor/UI/CursorActions/PasteTerrainCursorAction.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private void GenerateGraphicalEdges()
141141
foundationHashSet.Add(entry.Offset);
142142
});
143143

144-
edges = Helpers.CreateEdges(copiedMapData.Width + 2, copiedMapData.Height + 2, foundationHashSet.ToList());
144+
edges = Helpers.CreateEdges(copiedMapData.Width + 1, copiedMapData.Height + 1, foundationHashSet.ToList());
145145
}
146146

147147
public override void PreMapDraw(Point2D cellCoords)
@@ -259,6 +259,23 @@ public override void PostMapDraw(Point2D cellCoords)
259259
CursorActionTarget.AddRefreshPoint(cellCoords, maxOffset);
260260
}
261261

262+
private Point2D GetEdgeCoordsCompensated(Point2D originalEdgeCoords)
263+
{
264+
// Edges partially run outside of the foundation of the copied cell. For example, a 1x1 copied area has a 2x2 edge foundation.
265+
// Compensate for that here.
266+
267+
int x = originalEdgeCoords.X;
268+
int y = originalEdgeCoords.Y;
269+
270+
if (x >= copiedMapData.Width)
271+
x--;
272+
273+
if (y >= copiedMapData.Height)
274+
y--;
275+
276+
return new Point2D(x, y);
277+
}
278+
262279
public override void DrawPreview(Point2D cellCoords, Point2D cameraTopLeftPoint)
263280
{
264281
if (KeyboardCommands.Instance.PlaceTerrainBelow.AreKeysOrModifiersDown(Keyboard))
@@ -275,13 +292,13 @@ public override void DrawPreview(Point2D cellCoords, Point2D cameraTopLeftPoint)
275292

276293
if (!CursorActionTarget.Is2DMode)
277294
{
278-
var cell = Map.GetTile(edgeCell0);
295+
var cell = Map.GetTile(cellCoords + GetEdgeCoordsCompensated(edge[0]));
279296
if (cell != null)
280-
heightOffset0 = Constants.CellHeight * cell.Level;
297+
heightOffset0 = Constants.CellHeight * cell.GetLevelOrPreviewLevel();
281298

282-
cell = Map.GetTile(edgeCell1);
299+
cell = Map.GetTile(cellCoords + GetEdgeCoordsCompensated(edge[1]));
283300
if (cell != null)
284-
heightOffset1 = Constants.CellHeight * cell.Level;
301+
heightOffset1 = Constants.CellHeight * cell.GetLevelOrPreviewLevel();
285302
}
286303

287304
// Translate edge vertices from cell coordinate space to world coordinate space.

0 commit comments

Comments
 (0)