forked from CnCNet/WorldAlteringEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlaceTubeCursorAction.cs
More file actions
258 lines (202 loc) · 8.89 KB
/
PlaceTubeCursorAction.cs
File metadata and controls
258 lines (202 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using Microsoft.Xna.Framework;
using Rampastring.XNAUI;
using Rampastring.XNAUI.Input;
using System;
using System.Collections.Generic;
using TSMapEditor.GameMath;
using TSMapEditor.Models;
using TSMapEditor.Mutations.Classes;
using TSMapEditor.Rendering;
namespace TSMapEditor.UI.CursorActions
{
public class PlaceTubeCursorAction : CursorAction
{
private const double DoubleClickTime = 0.2f;
public PlaceTubeCursorAction(ICursorActionTarget cursorActionTarget) : base(cursorActionTarget)
{
}
public override string GetName() => "Place Tube";
public override bool HandlesKeyboardInput => true;
public override bool DrawCellCursor => true;
private Tube tube;
private List<Point2D> points = new List<Point2D>();
private List<Point2D> tubeCells = new List<Point2D>();
private bool pointAddedForPreview;
private Point2D lastClickedCell;
private DateTime lastClickedCellDateTime;
public override void PreMapDraw(Point2D cellCoords)
{
if (!points.Contains(cellCoords) && !tubeCells.Contains(cellCoords))
{
points.Add(cellCoords);
pointAddedForPreview = true;
RefreshTube();
}
else
{
pointAddedForPreview = false;
}
if (tube != null)
CursorActionTarget.Map.Tubes.Add(tube);
CursorActionTarget.InvalidateMap();
}
public override void PostMapDraw(Point2D cellCoords)
{
if (tube != null)
CursorActionTarget.Map.Tubes.RemoveAt(CursorActionTarget.Map.Tubes.Count - 1);
if (pointAddedForPreview)
{
points.RemoveAt(points.Count - 1);
RefreshTube();
}
CursorActionTarget.InvalidateMap();
}
public override void DrawPreview(Point2D cellCoords, Point2D cameraTopLeftPoint)
{
Point2D cellTopLeftPoint = CellMath.CellTopLeftPointFromCellCoords_3D(cellCoords, CursorActionTarget.Map) - cameraTopLeftPoint;
cellTopLeftPoint = cellTopLeftPoint.ScaleBy(CursorActionTarget.Camera.ZoomLevel);
const string text = "Click on cells to draw a tunnel. Once ready, use one of the options below:\r\n\r\n" +
"Double-click to confirm\r\n" +
"Double-click while holding Shift to create bidirectional tunnel\r\n" +
"Press ESC to clear\r\n" +
"Press B to step back\r\n" +
"Right-click to exit";
var textDimensions = Renderer.GetTextDimensions(text, Constants.UIBoldFont);
int x = cellTopLeftPoint.X - (int)(textDimensions.X - Constants.CellSizeX) / 2;
Vector2 textPosition = new Vector2(x + 60, cellTopLeftPoint.Y - 150);
Rectangle textBackgroundRectangle = new Rectangle((int)textPosition.X - Constants.UIEmptySideSpace,
(int)textPosition.Y - Constants.UIEmptyTopSpace,
(int)textDimensions.X + Constants.UIEmptySideSpace * 2,
(int)textDimensions.Y + Constants.UIEmptyBottomSpace + Constants.UIEmptyTopSpace);
Renderer.FillRectangle(textBackgroundRectangle, UISettings.ActiveSettings.PanelBackgroundColor);
Renderer.DrawRectangle(textBackgroundRectangle, UISettings.ActiveSettings.PanelBorderColor);
Renderer.DrawStringWithShadow(text, Constants.UIBoldFont, textPosition, Color.Yellow);
}
public override void OnKeyPressed(KeyPressEventArgs e, Point2D cellCoords)
{
if (e.PressedKey == Microsoft.Xna.Framework.Input.Keys.Escape)
{
if (tube != null)
{
TubeRefreshHelper.MapViewRefreshTube(tube, CursorActionTarget.MutationTarget);
points.Clear();
RefreshTube();
}
if (CursorActionTarget.WindowManager.Keyboard.IsShiftHeldDown())
ExitAction();
e.Handled = true;
}
else if (e.PressedKey == Microsoft.Xna.Framework.Input.Keys.C && tube != null && tube.Directions.Count > 0)
{
ConfirmTube();
e.Handled = true;
}
else if (e.PressedKey == Microsoft.Xna.Framework.Input.Keys.B && tube != null && tube.Directions.Count > 0)
{
TubeRefreshHelper.MapViewRefreshTube(tube, CursorActionTarget.MutationTarget);
tube.Directions.RemoveAt(tube.Directions.Count - 1);
if (points.Count > 0)
points.RemoveAt(points.Count - 1);
RefreshTube();
e.Handled = true;
}
}
private void ConfirmTube()
{
if (tube != null && tube.Directions.Count > 0)
{
tube.Pending = false;
tube.UnitInitialFacing = tube.Directions[0];
CursorActionTarget.MutationManager.PerformMutation(new PlaceTubeMutation(CursorActionTarget.MutationTarget, tube));
tube.Directions.Add(TubeDirection.None);
if (CursorActionTarget.WindowManager.Keyboard.IsShiftHeldDown())
{
Tube reversedTube = tube.GetReversedTube();
CursorActionTarget.MutationManager.PerformMutation(new PlaceTubeMutation(CursorActionTarget.MutationTarget, reversedTube));
}
points.Clear();
RefreshTube();
}
}
private void RefreshTube()
{
tubeCells.Clear();
if (points.Count == 0)
{
tube = null;
return;
}
if (tube == null)
tube = new Tube();
tube.Pending = true;
tubeCells.Add(points[0]);
tube.EntryPoint = points[0];
tube.ExitPoint = points[^1];
tube.Directions.Clear();
for (int i = 0; i < points.Count - 1; i++)
{
Point2D current = points[i];
Point2D next = points[i + 1];
Point2D previousStep = current;
while (previousStep != next)
{
int x = previousStep.X;
int y = previousStep.Y;
if (x > next.X)
x--;
else if (x < next.X)
x++;
if (y > next.Y)
y--;
else if (y < next.Y)
y++;
Point2D newPoint = new Point2D(x, y);
// Fetch tube direction for this cell
// Check each tube direction to see if we find a fitting one
TubeDirection? nextTubeDirection = null;
for (int dir = (int)TubeDirection.First; dir <= (int)TubeDirection.Max; dir++)
{
TubeDirection tubeDirection = (TubeDirection)dir;
Point2D nextPointCandidate = previousStep.NextPointFromTubeDirection(tubeDirection);
if (nextPointCandidate == newPoint)
{
nextTubeDirection = tubeDirection;
break;
}
}
if (!nextTubeDirection.HasValue)
throw new ApplicationException("Unable to find tunnel tube direction! From: " + previousStep + " To: " + newPoint);
tube.Directions.Add(nextTubeDirection.Value);
tubeCells.Add(newPoint);
previousStep = newPoint;
}
}
tube.UnitInitialFacing = tube.Directions.Count > 0 ? tube.Directions[0] : TubeDirection.None;
}
public override void LeftClick(Point2D cellCoords)
{
if (points.Count == 0)
{
// Make sure that a tube doesn't already exist on this cell
if (CursorActionTarget.Map.Tubes.Exists(tb => tb.EntryPoint == cellCoords))
return;
}
if (!points.Contains(cellCoords) && !tubeCells.Contains(cellCoords))
{
points.Add(cellCoords);
RefreshTube();
TubeRefreshHelper.MapViewRefreshTube(tube, CursorActionTarget.MutationTarget);
}
else
{
if (points.Count > 1 && lastClickedCell == cellCoords && DateTime.Now - lastClickedCellDateTime < TimeSpan.FromSeconds(DoubleClickTime))
{
ConfirmTube();
return;
}
}
lastClickedCell = cellCoords;
lastClickedCellDateTime = DateTime.Now;
}
}
}