From 92ba40da1a3929a90d4af34f2ee51d1e2eab4964 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Wed, 26 May 2010 19:47:46 +1200 Subject: [PATCH] floodfill in editor on holding 'shift' --- OpenRA.Editor/Surface.cs | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/OpenRA.Editor/Surface.cs b/OpenRA.Editor/Surface.cs index 49a79a92a0..944a312c99 100644 --- a/OpenRA.Editor/Surface.cs +++ b/OpenRA.Editor/Surface.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Windows.Forms; using OpenRA.FileFormats; using OpenRA.Thirdparty; +using System.Collections; namespace OpenRA.Editor { @@ -91,6 +92,47 @@ namespace OpenRA.Editor } } + void FloodFillWithBrush(int2 pos) + { + var queue = new Queue(); + var replace = Map.MapTiles[pos.X, pos.Y]; + + queue.Enqueue(pos); + while (queue.Count > 0) + { + var p = queue.Dequeue(); + if (!Map.MapTiles[p.X, p.Y].Equals(replace)) + continue; + + var a = FindEdge(p, new int2(-1, 0), replace); + var b = FindEdge(p, new int2(1, 0), replace); + + for (var x = a.X; x <= b.X; x++) + { + Map.MapTiles[x, p.Y] = new TileReference { type = Brush.N, image = (byte)0, index = (byte)0 }; + if (Map.MapTiles[x, p.Y - 1].Equals(replace) && Map.IsInMap(x, p.Y - 1)) + queue.Enqueue(new int2(x, p.Y - 1)); + if (Map.MapTiles[x, p.Y + 1].Equals(replace) && Map.IsInMap(x, p.Y + 1)) + queue.Enqueue(new int2(x, p.Y + 1)); + } + } + + /* todo: optimize */ + foreach (var ch in Chunks.Values) ch.Dispose(); + Chunks.Clear(); + } + + int2 FindEdge(int2 p, int2 d, TileReference replace) + { + for (; ; ) + { + var q = p+d; + if (!Map.IsInMap(q)) return p; + if (!Map.MapTiles[q.X, q.Y].Equals(replace)) return p; + p = q; + } + } + void DrawWithBrush() { // change the bits in the map @@ -98,6 +140,12 @@ namespace OpenRA.Editor var template = TileSet.walk[Brush.N]; var pos = GetBrushLocation(); + if (ModifierKeys == Keys.Shift) + { + FloodFillWithBrush(pos); + return; + } + for (var u = 0; u < template.Size.X; u++) for (var v = 0; v < template.Size.Y; v++) {