Skip to content

Commit a4b4fe0

Browse files
committed
Cleaned up and reoragnised BaseTool around AreaMode, moved VirtualLayer logic from BaseTool to DrawTool
1 parent 30f7284 commit a4b4fe0

3 files changed

Lines changed: 54 additions & 123 deletions

File tree

CentrED/Tools/AltitudeGradientTool.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,6 @@ private void PreviewPath()
319319
// Generate path tiles using a better, universal algorithm
320320
private void GeneratePathTiles(int startX, int startY, int endX, int endY, sbyte startZ, sbyte endZ)
321321
{
322-
// Start area operation tracking in BaseTool
323-
// normalized for all quadrants
324-
ushort ox0 = (ushort)Math.Min(startX, endX);
325-
ushort oy0 = (ushort)Math.Min(startY, endY);
326-
ushort ox1 = (ushort)Math.Max(startX, endX);
327-
ushort oy1 = (ushort)Math.Max(startY, endY);
328-
OnAreaOperationStart(ox0, oy0);
329-
OnAreaOperationUpdate(ox1, oy1);
330-
331322
// Create a bounding box with minimal padding
332323
int padding = _pathWidth / 2 + 1;
333324
int minX = Math.Min(startX, endX) - padding;
@@ -470,14 +461,6 @@ private void GeneratePathTiles(int startX, int startY, int endX, int endY, sbyte
470461
// Area gradient method
471462
private void GenerateAreaGradient(int startX, int startY, int endX, int endY, sbyte startZ, sbyte endZ)
472463
{
473-
// Normalized overlay box for preview
474-
ushort ox0 = (ushort)Math.Min(startX, endX);
475-
ushort oy0 = (ushort)Math.Min(startY, endY);
476-
ushort ox1 = (ushort)Math.Max(startX, endX);
477-
ushort oy1 = (ushort)Math.Max(startY, endY);
478-
OnAreaOperationStart(ox0, oy0);
479-
OnAreaOperationUpdate(ox1, oy1);
480-
481464
int minX = Math.Min(startX, endX);
482465
int maxX = Math.Max(startX, endX);
483466
int minY = Math.Min(startY, endY);

CentrED/Tools/BaseTool.cs

Lines changed: 38 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using CentrED.Map;
2+
using CentrED.Network;
23
using CentrED.UI;
3-
using CentrED.UI.Windows;
44
using Microsoft.Xna.Framework.Input;
55
using static CentrED.Application;
66

@@ -10,31 +10,31 @@ namespace CentrED.Tools;
1010
public abstract class BaseTool : Tool
1111
{
1212
// Properties to track area operations
13-
//TODO: Move these to DrawTool!
14-
protected bool IsAreaOperation { get; private set; }
15-
protected ushort AreaStartX { get; private set; }
16-
protected ushort AreaStartY { get; private set; }
17-
protected ushort AreaEndX { get; private set; }
18-
protected ushort AreaEndY { get; private set; }
13+
protected bool AreaMode;
14+
protected TileObject? AreaStartTile;
15+
protected RectU16 Area;
1916

20-
public void OnAreaOperationStart(ushort x, ushort y)
17+
protected virtual void OnAreaOperationStart(TileObject? o)
2118
{
22-
IsAreaOperation = true;
23-
AreaStartX = x;
24-
AreaStartY = y;
25-
AreaEndX = x;
26-
AreaEndY = y;
19+
if (o == null)
20+
return;
21+
22+
AreaStartTile = o;
23+
Area = new RectU16(o.Tile.X, o.Tile.Y, o.Tile.X, o.Tile.Y);
2724
}
2825

29-
public void OnAreaOperationUpdate(ushort x, ushort y)
26+
protected virtual void OnAreaOperationUpdate(TileObject? to)
3027
{
31-
AreaEndX = x;
32-
AreaEndY = y;
28+
if (to == null)
29+
return;
30+
31+
Area.X2 = to.Tile.X;
32+
Area.Y2 = to.Tile.Y;
3333
}
34-
35-
public void OnAreaOperationEnd()
34+
35+
protected virtual void OnAreaOperationEnd()
3636
{
37-
IsAreaOperation = false;
37+
AreaStartTile = null;
3838
}
3939

4040
protected static readonly Random Random = Random.Shared;
@@ -44,9 +44,7 @@ public void OnAreaOperationEnd()
4444

4545
protected static int _chance = 100;
4646
protected bool Pressed;
47-
protected bool AreaMode;
4847
protected bool TopTilesOnly = true;
49-
private TileObject? _areaStartTile;
5048

5149
internal override void Draw()
5250
{
@@ -93,138 +91,77 @@ public sealed override void OnKeyReleased(Keys key)
9391
public sealed override void OnMousePressed(TileObject? o)
9492
{
9593
Pressed = true;
96-
if (AreaMode && _areaStartTile == null && o != null)
94+
if (AreaMode)
9795
{
98-
TileObject to = o;
99-
var tilesWindow = UIManager.GetWindow<TilesWindow>();
100-
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && o is VirtualLayerTile)
101-
{
102-
to = CEDGame.MapManager.LandTiles[to.Tile.X, to.Tile.Y];
103-
}
104-
_areaStartTile = to;
105-
106-
OnAreaOperationStart(to.Tile.X, to.Tile.Y);
96+
OnAreaOperationStart(o);
10797
}
10898
CEDClient.BeginUndoGroup();
10999
}
110100

111101
public sealed override void OnMouseReleased(TileObject? o)
112102
{
113-
var tilesWindow = UIManager.GetWindow<TilesWindow>();
114-
115103
if (Pressed)
116104
{
117105
if (AreaMode)
118106
{
119-
foreach (var to in MapManager.GetTiles(_areaStartTile, o, TopTilesOnly))
107+
foreach (var to in MapManager.GetTiles(AreaStartTile, o, TopTilesOnly))
120108
{
121-
TileObject to2 = to;
122-
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to2 is VirtualLayerTile)
123-
{
124-
to2 = CEDGame.MapManager.LandTiles[to2.Tile.X, to2.Tile.Y];
125-
}
126-
InternalApply(to2);
127-
GhostClear(to2);
109+
InternalApply(to);
110+
GhostClear(to);
128111
}
112+
OnAreaOperationEnd();
129113
}
130114
else
131115
{
132-
TileObject to = o;
133-
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to is VirtualLayerTile)
134-
{
135-
to = CEDGame.MapManager.LandTiles[to.Tile.X, to.Tile.Y];
136-
}
137-
InternalApply(to);
138-
GhostClear(to);
116+
InternalApply(o);
117+
GhostClear(o);
139118
}
140119
}
141120
Pressed = false;
142-
_areaStartTile = null;
143-
144-
if (AreaMode)
145-
{
146-
OnAreaOperationEnd();
147-
}
148121

149122
CEDClient.EndUndoGroup();
150123
}
151124

152125

153126
public sealed override void OnMouseEnter(TileObject? o)
154127
{
155-
if (o == null)
156-
return;
157-
158-
if (AreaMode && Pressed )
159-
{
160-
OnAreaOperationUpdate(o.Tile.X, o.Tile.Y);
161-
}
162-
163-
var tilesWindow = UIManager.GetWindow<TilesWindow>();
164-
165128
if (AreaMode && Pressed)
166129
{
167-
foreach (var to in MapManager.GetTiles(_areaStartTile, o, TopTilesOnly))
130+
OnAreaOperationUpdate(o);
131+
foreach (var to in MapManager.GetTiles(AreaStartTile, o, TopTilesOnly))
168132
{
169133
if (Random.Next(100) < _chance)
170134
{
171-
TileObject to2 = to;
172-
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to2 is VirtualLayerTile)
173-
{
174-
to2 = CEDGame.MapManager.LandTiles[to2.Tile.X, to2.Tile.Y];
175-
}
176-
GhostApply(to2);
135+
GhostApply(to);
177136
}
178137
}
179138
}
180139
else
181140
{
182141
if (Random.Next(100) < _chance)
183142
{
184-
TileObject to = o;
185-
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to is VirtualLayerTile)
186-
{
187-
to = CEDGame.MapManager.LandTiles[to.Tile.X, to.Tile.Y];
188-
}
189-
GhostApply(to);
143+
GhostApply(o);
190144
}
191145
}
192146
}
193147

194148
public sealed override void OnMouseLeave(TileObject? o)
195149
{
196-
var tilesWindow = UIManager.GetWindow<TilesWindow>();
197-
198-
if (Pressed && !AreaMode)
199-
{
200-
TileObject to = o;
201-
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to is VirtualLayerTile)
202-
{
203-
to = CEDGame.MapManager.LandTiles[to.Tile.X, to.Tile.Y];
204-
}
205-
InternalApply(to);
206-
}
207-
if (Pressed && AreaMode)
150+
if (Pressed)
208151
{
209-
foreach (var to in MapManager.GetTiles(_areaStartTile, o, TopTilesOnly))
152+
if (AreaMode)
210153
{
211-
TileObject to2 = to;
212-
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to2 is VirtualLayerTile)
154+
foreach (var to in MapManager.GetTiles(AreaStartTile, o, TopTilesOnly))
213155
{
214-
to2 = CEDGame.MapManager.LandTiles[to2.Tile.X, to2.Tile.Y];
156+
GhostClear(to);
215157
}
216-
GhostClear(to2);
217158
}
218-
}
219-
else
220-
{
221-
TileObject to = o;
222-
if (CEDGame.MapManager.UseVirtualLayer && tilesWindow.LandMode && to is VirtualLayerTile)
159+
else
223160
{
224-
to = CEDGame.MapManager.LandTiles[to.Tile.X, to.Tile.Y];
161+
InternalApply(o);
225162
}
226-
GhostClear(to);
227163
}
164+
GhostClear(o);
228165
}
229166

230167
public override void Apply(TileObject? o)

CentrED/Tools/DrawTool.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public override void OnDeactivated(TileObject? o)
128128

129129
protected override void GhostApply(TileObject? o)
130130
{
131+
o = TransformTarget(o);
131132
if (o == null)
132133
return;
133134

@@ -188,6 +189,7 @@ protected override void GhostApply(TileObject? o)
188189

189190
protected override void GhostClear(TileObject? o)
190191
{
192+
o = TransformTarget(o);
191193
if (o == null)
192194
return;
193195

@@ -201,10 +203,10 @@ protected override void GhostClear(TileObject? o)
201203

202204
protected override void InternalApply(TileObject? o)
203205
{
206+
o = TransformTarget(o);
204207
if (o == null)
205208
return;
206209

207-
208210
if (_drawMode == (int)DrawMode.REPLACE && o is StaticObject so)
209211
{
210212
Client.Remove(so.StaticTile);
@@ -233,6 +235,15 @@ protected override void InternalApply(TileObject? o)
233235
}
234236
}
235237

238+
private TileObject? TransformTarget(TileObject? o)
239+
{
240+
if (Application.CEDGame.MapManager.UseVirtualLayer && _tilesWindow.LandMode && o is VirtualLayerTile)
241+
{
242+
return Application.CEDGame.MapManager.LandTiles[o.Tile.X, o.Tile.Y];
243+
}
244+
return o;
245+
}
246+
236247
private sbyte CalculateNewZ(TileObject o)
237248
{
238249
int height = o.Tile.Z;
@@ -282,12 +293,12 @@ private bool CanDrawOn(TileObject o)
282293

283294
private ushort GetSequentialTileId(ushort x, ushort y)
284295
{
285-
if (IsAreaOperation)
296+
if (AreaMode)
286297
{
287-
var width = Math.Abs(AreaEndX - AreaStartX);
298+
var width = Math.Abs(Area.X2 - Area.X1);
288299

289-
var deltaX = Math.Abs(x - AreaStartX);
290-
var deltaY = Math.Abs(y - AreaStartY);
300+
var deltaX = Math.Abs(x - Area.X1);
301+
var deltaY = Math.Abs(y - Area.Y1);
291302

292303
var sequenceIndex = deltaY * width + deltaX;
293304

0 commit comments

Comments
 (0)