Skip to content

Commit 04af0b1

Browse files
committed
Make it possible to move base nodes
1 parent e03c2f7 commit 04af0b1

4 files changed

Lines changed: 91 additions & 22 deletions

File tree

src/TSMapEditor/Rendering/Camera.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public double ZoomLevel
8484

8585
public int ScaleIntWithZoom(int value) => (int)(value * ZoomLevel);
8686

87+
public Point2D ScalePointWithZoom(Point2D value) => new Point2D((int)(value.X * ZoomLevel), (int)(value.Y * ZoomLevel));
88+
8789
public void CenterOnCell(Point2D cellCoords)
8890
{
8991
Point2D cellPixelCoords = CellMath.CellCenterPointFromCellCoords_3D(cellCoords, map);

src/TSMapEditor/UI/CursorAction.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public void OnExit()
5454
/// </summary>
5555
public virtual bool SeeThrough => true;
5656

57+
public virtual bool OnlyUniqueCellEvents => true;
58+
5759
public abstract string GetName();
5860

5961
protected Map Map => CursorActionTarget.Map;

src/TSMapEditor/UI/CursorActions/ManageBaseNodesCursorAction.cs

Lines changed: 83 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,39 @@ public ManageBaseNodesCursorAction(ICursorActionTarget cursorActionTarget) : bas
1818
{
1919
}
2020

21+
private BaseNode draggedBaseNode = null;
22+
private bool isDragging = false;
23+
2124
public override string GetName() => "Manage Base Nodes";
2225

2326
public override bool DrawCellCursor => true;
2427

2528
public override bool HandlesKeyboardInput => true;
2629

30+
public override bool OnlyUniqueCellEvents => false;
31+
2732
public override void DrawPreview(Point2D cellCoords, Point2D cameraTopLeftPoint)
2833
{
2934
string text = "Placement actions:" + Environment.NewLine +
3035
"Click on building to place a base node." + Environment.NewLine +
31-
"Hold SHIFT while clicking to also delete the building." + Environment.NewLine +
36+
"Hold SHIFT while clicking to also delete the source building." + Environment.NewLine +
3237
"Hold CTRL while clicking to erase a base node." + Environment.NewLine + Environment.NewLine +
38+
"Hold M while dragging a base node to move it." + Environment.NewLine + Environment.NewLine +
3339
"Ordering actions:" + Environment.NewLine +
3440
"Press E while hovering over a base node to shift it to be built earlier." + Environment.NewLine +
3541
"Press D while hovering over a base node to shift it to be built later.";
3642

37-
DrawText(cellCoords, cameraTopLeftPoint, text, UISettings.ActiveSettings.AltColor);
38-
}
39-
40-
private void DrawText(Point2D cellCoords, Point2D cameraTopLeftPoint, string text, Color textColor)
41-
{
42-
Point2D cellTopLeftPoint = CellMath.CellTopLeftPointFromCellCoords(cellCoords, CursorActionTarget.Map) - cameraTopLeftPoint;
43-
cellTopLeftPoint = cellTopLeftPoint.ScaleBy(CursorActionTarget.Camera.ZoomLevel);
44-
45-
var textDimensions = Renderer.GetTextDimensions(text, Constants.UIBoldFont);
46-
int x = cellTopLeftPoint.X - (int)(textDimensions.X - Constants.CellSizeX) / 2;
47-
48-
Vector2 textPosition = new Vector2(x + 60, cellTopLeftPoint.Y - 200);
49-
Rectangle textBackgroundRectangle = new Rectangle((int)textPosition.X - Constants.UIEmptySideSpace,
50-
(int)textPosition.Y - Constants.UIEmptyTopSpace,
51-
(int)textDimensions.X + Constants.UIEmptySideSpace * 2,
52-
(int)textDimensions.Y + Constants.UIEmptyBottomSpace + Constants.UIEmptyTopSpace);
43+
DrawText(cellCoords, cameraTopLeftPoint, 60, -240, text, UISettings.ActiveSettings.AltColor);
5344

54-
Renderer.FillRectangle(textBackgroundRectangle, UISettings.ActiveSettings.PanelBackgroundColor);
55-
Renderer.DrawRectangle(textBackgroundRectangle, UISettings.ActiveSettings.PanelBorderColor);
45+
if (isDragging)
46+
{
47+
var source = Is2DMode ? CellMath.CellCenterPointFromCellCoords(draggedBaseNode.Position, Map) : CellMath.CellCenterPointFromCellCoords_3D(draggedBaseNode.Position, Map);
48+
var destination = Is2DMode ? CellMath.CellCenterPointFromCellCoords(cellCoords, Map) : CellMath.CellCenterPointFromCellCoords_3D(cellCoords, Map);
49+
source = CursorActionTarget.Camera.ScalePointWithZoom(source - cameraTopLeftPoint);
50+
destination = CursorActionTarget.Camera.ScalePointWithZoom(destination - cameraTopLeftPoint);
5651

57-
Renderer.DrawStringWithShadow(text, Constants.UIBoldFont, textPosition, textColor);
52+
Renderer.DrawLine(source.ToXNAVector(), destination.ToXNAVector(), Color.White);
53+
}
5854
}
5955

6056
public override void OnKeyPressed(KeyPressEventArgs e, Point2D cellCoords)
@@ -74,8 +70,48 @@ public override void OnKeyPressed(KeyPressEventArgs e, Point2D cellCoords)
7470
}
7571
}
7672

73+
public override void LeftDown(Point2D cellCoords)
74+
{
75+
if (Keyboard.IsKeyHeldDown(Microsoft.Xna.Framework.Input.Keys.M))
76+
{
77+
if (!isDragging)
78+
{
79+
var baseNode = GetBaseNodeFromCellCoords(cellCoords);
80+
if (baseNode != null)
81+
{
82+
StartBaseNodeDrag(baseNode);
83+
}
84+
}
85+
}
86+
else
87+
{
88+
StopBaseNodeDrag();
89+
}
90+
91+
base.LeftDown(cellCoords);
92+
}
93+
94+
public override void LeftUpOnMouseMove(Point2D cellCoords)
95+
{
96+
if (isDragging && !Keyboard.IsKeyHeldDown(Microsoft.Xna.Framework.Input.Keys.M))
97+
{
98+
StopBaseNodeDrag();
99+
}
100+
101+
base.LeftUpOnMouseMove(cellCoords);
102+
}
103+
77104
public override void LeftClick(Point2D cellCoords)
78105
{
106+
if (isDragging)
107+
{
108+
draggedBaseNode.Position = cellCoords;
109+
110+
StopBaseNodeDrag();
111+
CursorActionTarget.InvalidateMap();
112+
return;
113+
}
114+
79115
if (Keyboard.IsCtrlHeldDown())
80116
{
81117
DeleteBaseNode(cellCoords);
@@ -88,6 +124,18 @@ public override void LeftClick(Point2D cellCoords)
88124
base.LeftClick(cellCoords);
89125
}
90126

127+
private void StartBaseNodeDrag(BaseNode draggedBaseNode)
128+
{
129+
this.draggedBaseNode = draggedBaseNode;
130+
isDragging = true;
131+
}
132+
133+
private void StopBaseNodeDrag()
134+
{
135+
isDragging = false;
136+
draggedBaseNode = null;
137+
}
138+
91139
// TODO implement all these manipulations as mutations so they go through the undo/redo system
92140
private void CreateBaseNode(Point2D cellCoords)
93141
{
@@ -249,5 +297,20 @@ private void ShiftBaseNodeLater(Point2D cellCoords)
249297
}
250298
}
251299
}
300+
301+
private BaseNode GetBaseNodeFromCellCoords(Point2D cellCoords)
302+
{
303+
foreach (House house in Map.GetHouses())
304+
{
305+
int index = GetBaseNodeIndexForHouse(house, cellCoords);
306+
307+
if (index > -1)
308+
{
309+
return house.BaseNodes[index];
310+
}
311+
}
312+
313+
return null;
314+
}
252315
}
253316
}

src/TSMapEditor/UI/MapUI.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,11 @@ public override void OnMouseMove()
474474
{
475475
if (Cursor.LeftDown)
476476
{
477-
if (tileUnderCursor != null && lastTileUnderCursor != tileUnderCursor)
477+
if (tileUnderCursor != null)
478478
{
479-
CursorAction.LeftDown(tileUnderCursor.CoordsToPoint());
479+
if (lastTileUnderCursor != tileUnderCursor || !CursorAction.OnlyUniqueCellEvents)
480+
CursorAction.LeftDown(tileUnderCursor.CoordsToPoint());
481+
480482
lastTileUnderCursor = tileUnderCursor;
481483
}
482484
}

0 commit comments

Comments
 (0)