Skip to content

Commit

Permalink
Add preview for pasting
Browse files Browse the repository at this point in the history
  • Loading branch information
Rampastring committed Oct 8, 2023
1 parent 2cf9304 commit b775c97
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/TSMapEditor/Mutations/Classes/PasteTerrainMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ public void WriteData(Stream stream)
{
stream.WriteByte((byte)EntryType);
byte[] offsetData = Offset.GetData();
stream.Write(offsetData, 0, offsetData.Length);
stream.Write(offsetData);

byte[] data = GetCustomData();
stream.Write(data, 0, data.Length);
stream.Write(data);
}

/// <summary>
Expand Down Expand Up @@ -370,15 +370,20 @@ protected override void ReadCustomData(Stream stream)
public class CopiedMapData
{
public List<CopiedMapEntry> CopiedMapEntries { get; set; } = new List<CopiedMapEntry>();
public ushort Width { get; set; }
public ushort Height { get; set; }

public byte[] Serialize()
{
byte[] bytes;

using (var memoryStream = new MemoryStream())
{
memoryStream.Write(BitConverter.GetBytes(Width));
memoryStream.Write(BitConverter.GetBytes(Height));

// Write entry count
memoryStream.Write(BitConverter.GetBytes(CopiedMapEntries.Count), 0, sizeof(int));
memoryStream.Write(BitConverter.GetBytes(CopiedMapEntries.Count));

// Write entries
foreach (var entry in CopiedMapEntries)
Expand All @@ -394,17 +399,19 @@ public byte[] Serialize()

public void Deserialize(byte[] bytes)
{
if (bytes.Length < 4)
if (bytes.Length < 8)
{
Logger.Log("Failed to deserialize copied map data: provided array is less than 4 bytes in size.");
Logger.Log("Failed to deserialize copied map data: provided array is less than 8 bytes in size.");
return;
}

int entryCount = BitConverter.ToInt32(bytes, 0);
Width = BitConverter.ToUInt16(bytes, 0);
Height = BitConverter.ToUInt16(bytes, 2);
int entryCount = BitConverter.ToInt32(bytes, 4);

using (var memoryStream = new MemoryStream(bytes))
{
memoryStream.Position = 4;
memoryStream.Position = 8;

for (int i = 0; i < entryCount; i++)
{
Expand Down
3 changes: 3 additions & 0 deletions src/TSMapEditor/UI/CursorActions/CopyTerrainCursorAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public override void LeftClick(Point2D cellCoords)
int startX = Math.Min(cellCoords.X, startCellCoords.X);
int endX = Math.Max(cellCoords.X, startCellCoords.X);

copiedMapData.Width = (ushort)(endX - startX);
copiedMapData.Height = (ushort)(endY - startY);

byte lowestHeight = byte.MaxValue;

// To handle height, we first look up the lowest height level of the copied
Expand Down
29 changes: 29 additions & 0 deletions src/TSMapEditor/UI/CursorActions/PasteTerrainCursorAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using TSMapEditor.Models;
using TSMapEditor.Mutations.Classes;
using TSMapEditor.Rendering;
using Rampastring.XNAUI;
using Microsoft.Xna.Framework;

namespace TSMapEditor.UI.CursorActions
{
Expand Down Expand Up @@ -170,6 +172,33 @@ public override void PostMapDraw(Point2D cellCoords)
CursorActionTarget.AddRefreshPoint(cellCoords, maxOffset);
}

public override void DrawPreview(Point2D cellCoords, Point2D cameraTopLeftPoint)
{
int startY = cellCoords.Y;
int endY = cellCoords.Y + copiedMapData.Height;
int startX = cellCoords.X;
int endX = cellCoords.X + copiedMapData.Width;

Func<Point2D, Map, Point2D> func = Is2DMode ? CellMath.CellTopLeftPointFromCellCoords : CellMath.CellTopLeftPointFromCellCoords_3D;

Point2D startPoint = func(new Point2D(startX, startY), CursorActionTarget.Map) - cameraTopLeftPoint + new Point2D(Constants.CellSizeX / 2, 0);
Point2D endPoint = func(new Point2D(endX, endY), CursorActionTarget.Map) - cameraTopLeftPoint + new Point2D(Constants.CellSizeX / 2, Constants.CellSizeY);
Point2D corner1 = func(new Point2D(startX, endY), CursorActionTarget.Map) - cameraTopLeftPoint + new Point2D(0, Constants.CellSizeY / 2);
Point2D corner2 = func(new Point2D(endX, startY), CursorActionTarget.Map) - cameraTopLeftPoint + new Point2D(Constants.CellSizeX, Constants.CellSizeY / 2);

startPoint = startPoint.ScaleBy(CursorActionTarget.Camera.ZoomLevel);
endPoint = endPoint.ScaleBy(CursorActionTarget.Camera.ZoomLevel);
corner1 = corner1.ScaleBy(CursorActionTarget.Camera.ZoomLevel);
corner2 = corner2.ScaleBy(CursorActionTarget.Camera.ZoomLevel);

Color lineColor = Color.Orange;
int thickness = 2;
Renderer.DrawLine(startPoint.ToXNAVector(), corner1.ToXNAVector(), lineColor, thickness);
Renderer.DrawLine(startPoint.ToXNAVector(), corner2.ToXNAVector(), lineColor, thickness);
Renderer.DrawLine(corner1.ToXNAVector(), endPoint.ToXNAVector(), lineColor, thickness);
Renderer.DrawLine(corner2.ToXNAVector(), endPoint.ToXNAVector(), lineColor, thickness);
}

public override void LeftClick(Point2D cellCoords)
{
if (CursorActionTarget.Map.GetTile(cellCoords) == null)
Expand Down

0 comments on commit b775c97

Please sign in to comment.