|
1 | | -using System; |
| 1 | +using Microsoft.Xna.Framework; |
| 2 | +using Rampastring.XNAUI; |
| 3 | +using System; |
2 | 4 | using System.Collections.Generic; |
3 | 5 | using TSMapEditor.GameMath; |
4 | 6 | using TSMapEditor.Models; |
@@ -49,8 +51,33 @@ public OriginalOverlayInfo(OverlayType overlayType, int frameIndex) |
49 | 51 |
|
50 | 52 | private List<OriginalOverlayInfo> originalOverlay = new List<OriginalOverlayInfo>(); |
51 | 53 |
|
| 54 | + private Point2D? lineSourceCell; |
| 55 | + private PlaceOverlayLineMutation linePreviewMutation; |
| 56 | + private bool blocked; |
| 57 | + |
| 58 | + public override void OnActionExit() |
| 59 | + { |
| 60 | + ClearLinePreview(); |
| 61 | + base.OnActionExit(); |
| 62 | + } |
| 63 | + |
| 64 | + private (Direction direction, int length) GetLineInformation(Point2D cellCoords) |
| 65 | + { |
| 66 | + Direction direction = Helpers.DirectionFromPoints(lineSourceCell.Value, cellCoords); |
| 67 | + Point2D vector = cellCoords - lineSourceCell.Value; |
| 68 | + int length = Math.Max(Math.Abs(vector.X), Math.Abs(vector.Y)); |
| 69 | + |
| 70 | + return (direction, length); |
| 71 | + } |
| 72 | + |
52 | 73 | public override void PreMapDraw(Point2D cellCoords) |
53 | 74 | { |
| 75 | + if (lineSourceCell.HasValue) |
| 76 | + { |
| 77 | + ApplyLinePreview(cellCoords); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
54 | 81 | originalOverlay.Clear(); |
55 | 82 |
|
56 | 83 | int brushSize = CursorActionTarget.BrushSize.Width * CursorActionTarget.BrushSize.Height; |
@@ -98,6 +125,12 @@ public override void PreMapDraw(Point2D cellCoords) |
98 | 125 |
|
99 | 126 | public override void PostMapDraw(Point2D cellCoords) |
100 | 127 | { |
| 128 | + if (lineSourceCell.HasValue) |
| 129 | + { |
| 130 | + ClearLinePreview(); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
101 | 134 | int index = 0; |
102 | 135 |
|
103 | 136 | CursorActionTarget.BrushSize.DoForBrushSize(offset => |
@@ -126,12 +159,107 @@ public override void PostMapDraw(Point2D cellCoords) |
126 | 159 | CursorActionTarget.AddRefreshPoint(cellCoords, Math.Max(CursorActionTarget.BrushSize.Height, CursorActionTarget.BrushSize.Width) + 1); |
127 | 160 | } |
128 | 161 |
|
| 162 | + private void ApplyLinePreview(Point2D cellCoords) |
| 163 | + { |
| 164 | + if (!lineSourceCell.HasValue || lineSourceCell.Value == cellCoords) |
| 165 | + return; |
| 166 | + |
| 167 | + (Direction direction, int length) = GetLineInformation(cellCoords); |
| 168 | + |
| 169 | + if (length < 1) |
| 170 | + return; |
| 171 | + |
| 172 | + linePreviewMutation = new PlaceOverlayLineMutation(CursorActionTarget.MutationTarget, OverlayType, FrameIndex, lineSourceCell.Value, direction, length); |
| 173 | + linePreviewMutation.Perform(); |
| 174 | + } |
| 175 | + |
| 176 | + private void ClearLinePreview() |
| 177 | + { |
| 178 | + if (linePreviewMutation != null) |
| 179 | + { |
| 180 | + linePreviewMutation.Undo(); |
| 181 | + linePreviewMutation = null; |
| 182 | + } |
| 183 | + |
| 184 | + CursorActionTarget.InvalidateMap(); |
| 185 | + } |
| 186 | + |
| 187 | + public override void DrawPreview(Point2D cellCoords, Point2D cameraTopLeftPoint) |
| 188 | + { |
| 189 | + if (!lineSourceCell.HasValue) |
| 190 | + return; |
| 191 | + |
| 192 | + if (cellCoords == lineSourceCell.Value) |
| 193 | + return; |
| 194 | + |
| 195 | + (Direction direction, int length) = GetLineInformation(cellCoords); |
| 196 | + |
| 197 | + Point2D cameraPoint1 = (CellMath.CellCenterPointFromCellCoords_3D(lineSourceCell.Value, Map) - cameraTopLeftPoint).ScaleBy(CursorActionTarget.Camera.ZoomLevel); |
| 198 | + Point2D cameraPoint2 = (CellMath.CellCenterPointFromCellCoords_3D(lineSourceCell.Value + Helpers.VisualDirectionToPoint(direction).ScaleBy(length), Map) - cameraTopLeftPoint).ScaleBy(CursorActionTarget.Camera.ZoomLevel); |
| 199 | + |
| 200 | + Renderer.DrawLine(cameraPoint1.ToXNAVector(), cameraPoint2.ToXNAVector(), Color.Orange, 2); |
| 201 | + } |
| 202 | + |
129 | 203 | public override void LeftDown(Point2D cellCoords) |
130 | 204 | { |
| 205 | + if (blocked) |
| 206 | + return; |
| 207 | + |
| 208 | + if (KeyboardCommands.Instance.PlaceTerrainLine.AreKeysOrModifiersDown(Keyboard)) |
| 209 | + { |
| 210 | + if (lineSourceCell == null && CursorActionTarget.Map.GetTile(cellCoords) != null) |
| 211 | + { |
| 212 | + lineSourceCell = cellCoords; |
| 213 | + } |
| 214 | + |
| 215 | + return; |
| 216 | + } |
| 217 | + |
131 | 218 | var mutation = new PlaceOverlayMutation(CursorActionTarget.MutationTarget, OverlayType, FrameIndex, cellCoords); |
132 | 219 | CursorActionTarget.MutationManager.PerformMutation(mutation); |
133 | 220 | } |
134 | 221 |
|
135 | | - public override void LeftClick(Point2D cellCoords) => LeftDown(cellCoords); |
| 222 | + private void ApplyLine(Point2D cellCoords) |
| 223 | + { |
| 224 | + if (OverlayType != null) |
| 225 | + { |
| 226 | + (Direction direction, int length) = GetLineInformation(cellCoords); |
| 227 | + var mutation = new PlaceOverlayLineMutation(CursorActionTarget.MutationTarget, OverlayType, FrameIndex, lineSourceCell.Value, direction, length); |
| 228 | + PerformMutation(mutation); |
| 229 | + } |
| 230 | + |
| 231 | + lineSourceCell = null; |
| 232 | + } |
| 233 | + |
| 234 | + public override void LeftClick(Point2D cellCoords) |
| 235 | + { |
| 236 | + if (KeyboardCommands.Instance.PlaceTerrainLine.AreKeysOrModifiersDown(Keyboard)) |
| 237 | + { |
| 238 | + if (lineSourceCell != null && cellCoords != lineSourceCell.Value) |
| 239 | + { |
| 240 | + ApplyLine(cellCoords); |
| 241 | + } |
| 242 | + |
| 243 | + return; |
| 244 | + } |
| 245 | + |
| 246 | + LeftDown(cellCoords); |
| 247 | + blocked = false; |
| 248 | + } |
| 249 | + |
| 250 | + public override void Update(Point2D? cellCoords) |
| 251 | + { |
| 252 | + if (lineSourceCell != null && cellCoords != null && lineSourceCell != cellCoords) |
| 253 | + { |
| 254 | + if (!KeyboardCommands.Instance.PlaceTerrainLine.AreKeysOrModifiersDown(Keyboard)) |
| 255 | + { |
| 256 | + ApplyLine(cellCoords.Value); |
| 257 | + blocked = true; |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + if (!CursorActionTarget.WindowManager.Cursor.LeftDown && !CursorActionTarget.WindowManager.Cursor.LeftClicked) |
| 262 | + blocked = false; |
| 263 | + } |
136 | 264 | } |
137 | 265 | } |
0 commit comments