Skip to content

Commit e59e905

Browse files
committed
Add support for brush sizes to smudge collection placement cursor action
1 parent cfb7111 commit e59e905

File tree

2 files changed

+95
-41
lines changed

2 files changed

+95
-41
lines changed
Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using TSMapEditor.GameMath;
1+
using System;
2+
using System.Collections.Generic;
3+
using TSMapEditor.GameMath;
24
using TSMapEditor.Models;
35
using TSMapEditor.UI;
46

@@ -9,43 +11,70 @@ namespace TSMapEditor.Mutations.Classes
911
/// </summary>
1012
class PlaceSmudgeCollectionMutation : Mutation
1113
{
12-
public PlaceSmudgeCollectionMutation(IMutationTarget mutationTarget, SmudgeCollection smudgeCollection, Point2D cellCoords) : base(mutationTarget)
14+
public PlaceSmudgeCollectionMutation(IMutationTarget mutationTarget, SmudgeCollection smudgeCollection, Point2D cellCoords, BrushSize brushSize) : base(mutationTarget)
1315
{
1416
this.smudgeCollection = smudgeCollection;
1517
this.cellCoords = cellCoords;
18+
this.brushSize = brushSize ?? throw new ArgumentNullException(nameof(brushSize));
1619
}
1720

18-
private Smudge oldSmudge;
19-
private readonly SmudgeCollection smudgeCollection;
20-
private readonly Point2D cellCoords;
21+
private List<CachedSmudge> oldSmudges = new List<CachedSmudge>(1);
22+
private List<CachedSmudge> placedSmudges = new List<CachedSmudge>();
23+
private SmudgeCollection smudgeCollection;
24+
private Point2D cellCoords;
25+
private BrushSize brushSize;
2126

2227
public override string GetDisplayString()
2328
{
24-
return string.Format(Translate(this, "DisplayString", "Place smudge collection {0} at {1}"),
25-
smudgeCollection.Name, cellCoords);
29+
return string.Format(Translate(this, "DisplayString", "Place smudge collection {0} at {1} with brush size of {2}"),
30+
smudgeCollection.Name, cellCoords, brushSize);
2631
}
2732

2833
public override void Perform()
2934
{
30-
var cell = MutationTarget.Map.GetTile(cellCoords);
31-
if (cell.Smudge != null)
32-
oldSmudge = cell.Smudge;
35+
oldSmudges.Clear();
3336

34-
var collectionEntry = smudgeCollection.Entries[MutationTarget.Randomizer.GetRandomNumber(0, smudgeCollection.Entries.Length - 1)];
35-
cell.Smudge = new Smudge() { SmudgeType = collectionEntry.SmudgeType, Position = cellCoords };
37+
bool placeNew = placedSmudges.Count == 0;
38+
int i = 0;
3639

37-
MutationTarget.AddRefreshPoint(cellCoords, 1);
40+
brushSize.DoForBrushSize(offset =>
41+
{
42+
var cell = MutationTarget.Map.GetTile(cellCoords + offset);
43+
44+
if (cell == null)
45+
return;
46+
47+
oldSmudges.Add(new CachedSmudge(cell.CoordsToPoint(), cell.Smudge == null ? null : cell.Smudge.SmudgeType));
48+
49+
if (placeNew)
50+
{
51+
var collectionEntry = smudgeCollection.Entries[MutationTarget.Randomizer.GetRandomNumber(0, smudgeCollection.Entries.Length - 1)];
52+
cell.Smudge = new Smudge() { SmudgeType = collectionEntry.SmudgeType, Position = cell.CoordsToPoint() };
53+
placedSmudges.Add(new CachedSmudge(cell.CoordsToPoint(), cell.Smudge.SmudgeType));
54+
}
55+
else
56+
{
57+
cell.Smudge = new Smudge() { SmudgeType = placedSmudges[i].SmudgeType, Position = placedSmudges[i].CellCoords };
58+
i++;
59+
}
60+
});
61+
62+
MutationTarget.AddRefreshPoint(cellCoords, brushSize.Max);
3863
}
3964

4065
public override void Undo()
4166
{
42-
var cell = MutationTarget.Map.GetTile(cellCoords);
67+
foreach (var oldSmudge in oldSmudges)
68+
{
69+
var cell = Map.GetTile(oldSmudge.CellCoords);
70+
71+
if (oldSmudge.SmudgeType == null)
72+
cell.Smudge = null;
73+
else
74+
cell.Smudge = new Smudge() { SmudgeType = oldSmudge.SmudgeType, Position = oldSmudge.CellCoords };
75+
}
4376

44-
// if oldSmudge is null, then this has the same effect as cell.Smudge = null
45-
// otherwise the smudge is replaced with the old smudge
46-
// iow. we don't need to handle the null case separately
47-
cell.Smudge = oldSmudge;
48-
MutationTarget.AddRefreshPoint(cellCoords, 1);
77+
MutationTarget.AddRefreshPoint(cellCoords, brushSize.Max);
4978
}
5079
}
5180
}

src/TSMapEditor/UI/CursorActions/PlaceSmudgeCollectionCursorAction.cs

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using TSMapEditor.GameMath;
34
using TSMapEditor.Models;
45
using TSMapEditor.Mutations.Classes;
@@ -13,9 +14,6 @@ public PlaceSmudgeCollectionCursorAction(ICursorActionTarget cursorActionTarget)
1314

1415
public override string GetName() => Translate("Name", "Place Smudge Collection");
1516

16-
private Smudge oldSmudge;
17-
private Smudge smudge;
18-
1917
private SmudgeCollection _smudgeCollection;
2018
public SmudgeCollection SmudgeCollection
2119
{
@@ -28,44 +26,71 @@ public SmudgeCollection SmudgeCollection
2826
}
2927

3028
_smudgeCollection = value;
31-
smudge = new Smudge { SmudgeType = _smudgeCollection.Entries[0].SmudgeType };
3229
}
3330
}
3431

32+
private List<Smudge> previewSmudges = new List<Smudge>();
33+
private List<Smudge> existingSmudges = new List<Smudge>();
34+
3535
public override void PreMapDraw(Point2D cellCoords)
3636
{
37-
var cell = CursorActionTarget.Map.GetTile(cellCoords);
37+
Point2D centeredBrushSizeCellCoords = CursorActionTarget.BrushSize.CenterWithinBrush(cellCoords);
38+
existingSmudges.Clear();
39+
40+
int i = 0;
41+
CursorActionTarget.BrushSize.DoForBrushSize(offset =>
42+
{
43+
var cell = CursorActionTarget.Map.GetTile(centeredBrushSizeCellCoords + offset);
44+
if (cell == null)
45+
return;
3846

39-
// "Randomize" the smudge image, it makes it clearer that we're placing down one from a collection.
40-
// If we used actual RNG here we'd need to avoid doing it every frame to avoid a constantly
41-
// changing smudge even when the cursor is still. Using cell numbers gives the intended
42-
// effect without pointless flickering.
43-
int cellnum = cellCoords.X + cellCoords.Y;
44-
int smudgeNumber = cellnum % SmudgeCollection.Entries.Length;
45-
smudge.SmudgeType = SmudgeCollection.Entries[smudgeNumber].SmudgeType;
47+
if (previewSmudges.Count <= i)
48+
{
49+
previewSmudges.Add(new Smudge());
50+
}
4651

47-
oldSmudge = cell.Smudge;
52+
// "Randomize" the smudge image, it makes it clearer that we're placing down one from a collection.
53+
// If we used actual RNG here we'd need to avoid doing it every frame to avoid a constantly
54+
// changing smudge even when the cursor is still. Using cell numbers gives the intended
55+
// effect without pointless flickering.
56+
int cellnum = cell.X + cell.Y;
57+
int smudgeNumber = cellnum % SmudgeCollection.Entries.Length;
58+
previewSmudges[i].SmudgeType = SmudgeCollection.Entries[smudgeNumber].SmudgeType;
59+
previewSmudges[i].Position = cell.CoordsToPoint();
60+
existingSmudges.Add(cell.Smudge);
4861

49-
smudge.Position = cell.CoordsToPoint();
50-
cell.Smudge = smudge;
62+
cell.Smudge = previewSmudges[i];
5163

52-
CursorActionTarget.AddRefreshPoint(cellCoords);
64+
i++;
65+
});
66+
67+
CursorActionTarget.AddRefreshPoint(centeredBrushSizeCellCoords, MutationTarget.BrushSize.Max);
5368
}
5469

5570
public override void PostMapDraw(Point2D cellCoords)
5671
{
57-
var cell = CursorActionTarget.Map.GetTile(cellCoords);
58-
if (cell.Smudge == smudge)
72+
base.PostMapDraw(cellCoords);
73+
74+
Point2D centeredBrushSizeCellCoords = CursorActionTarget.BrushSize.CenterWithinBrush(cellCoords);
75+
76+
int i = 0;
77+
CursorActionTarget.BrushSize.DoForBrushSize(offset =>
5978
{
60-
cell.Smudge = oldSmudge;
61-
}
79+
var cell = CursorActionTarget.Map.GetTile(centeredBrushSizeCellCoords + offset);
80+
if (cell == null)
81+
return;
82+
83+
cell.Smudge = existingSmudges[i];
84+
i++;
85+
});
6286

63-
CursorActionTarget.AddRefreshPoint(cellCoords);
87+
CursorActionTarget.AddRefreshPoint(centeredBrushSizeCellCoords, CursorActionTarget.BrushSize.Max);
6488
}
6589

6690
public override void LeftDown(Point2D cellCoords)
6791
{
68-
var mutation = new PlaceSmudgeCollectionMutation(CursorActionTarget.MutationTarget, SmudgeCollection, cellCoords);
92+
Point2D centeredBrushSizeCellCoords = CursorActionTarget.BrushSize.CenterWithinBrush(cellCoords);
93+
var mutation = new PlaceSmudgeCollectionMutation(CursorActionTarget.MutationTarget, SmudgeCollection, centeredBrushSizeCellCoords, MutationTarget.BrushSize);
6994
CursorActionTarget.MutationManager.PerformMutation(mutation);
7095
}
7196

0 commit comments

Comments
 (0)