diff --git a/OpenRA.Editor/ActorTemplate.cs b/OpenRA.Editor/ActorTemplate.cs index c9c35c447f..8a6f75e4b7 100644 --- a/OpenRA.Editor/ActorTemplate.cs +++ b/OpenRA.Editor/ActorTemplate.cs @@ -14,4 +14,10 @@ namespace OpenRA.Editor public ActorInfo Info; public bool Centered; } + + class BrushTemplate + { + public Bitmap Bitmap; + public ushort N; + } } diff --git a/OpenRA.Editor/Form1.cs b/OpenRA.Editor/Form1.cs index 46ffd2d6b8..96c6265bcb 100644 --- a/OpenRA.Editor/Form1.cs +++ b/OpenRA.Editor/Form1.cs @@ -62,8 +62,8 @@ namespace OpenRA.Editor SizeMode = PictureBoxSizeMode.StretchImage }; - var p = Pair.New(n, bitmap); - ibox.Click += (_, e) => surface1.Brush = p; + var brushTemplate = new BrushTemplate { Bitmap = bitmap, N = n }; + ibox.Click += (_, e) => surface1.SetBrush(brushTemplate); var template = tileset.walk[n]; tilePalette.Controls.Add(ibox); @@ -79,6 +79,8 @@ namespace OpenRA.Editor catch { } } + var actorTemplates = new List(); + foreach (var a in Rules.Info.Keys) { try @@ -93,15 +95,21 @@ namespace OpenRA.Editor SizeMode = PictureBoxSizeMode.StretchImage }; + ibox.Click += (_, e) => surface1.SetActor(template); + actorPalette.Controls.Add(ibox); tt.SetToolTip(ibox, "{0}:{1}".F( info.Name, info.Category)); + + actorTemplates.Add( template); } catch { } } + + surface1.BindActorTemplates(actorTemplates); } void LocateGameRoot() @@ -183,7 +191,7 @@ namespace OpenRA.Editor } bitmap.UnlockBits(data); - return new ActorTemplate { Bitmap = bitmap, Info = info, Centered = !info.Traits.Contains() }; + return new ActorTemplate { Bitmap = bitmap, Info = info, Centered = !info.Traits.Contains() }; } } diff --git a/OpenRA.Editor/Surface.cs b/OpenRA.Editor/Surface.cs index 359ff4a938..6082cdd7ae 100644 --- a/OpenRA.Editor/Surface.cs +++ b/OpenRA.Editor/Surface.cs @@ -2,6 +2,7 @@ using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; +using System.Linq; using OpenRA.FileFormats; namespace OpenRA.Editor @@ -12,17 +13,29 @@ namespace OpenRA.Editor public TileSet TileSet { get; private set; } public Palette Palette { get; private set; } int2 Offset; - public Pair Brush; + + public BrushTemplate Brush; + public ActorTemplate Actor; + + Dictionary ActorTemplates = new Dictionary(); public void Bind(Map m, TileSet ts, Palette p) { Map = m; TileSet = ts; Palette = p; - Brush = Pair.New((ushort)0, null as Bitmap); + Brush = null; Chunks.Clear(); } + public void SetBrush(BrushTemplate brush) { Actor = null; Brush = brush; } + public void SetActor(ActorTemplate actor) { Brush = null; Actor = actor; } + + public void BindActorTemplates(IEnumerable templates) + { + ActorTemplates = templates.ToDictionary(a => a.Info.Name.ToLowerInvariant()); + } + Dictionary Chunks = new Dictionary(); public Surface() @@ -52,10 +65,12 @@ namespace OpenRA.Editor } else { - if (e.Button == MouseButtons.Left && Brush.Second != null) + if (e.Button == MouseButtons.Left && Brush != null) DrawWithBrush(); + if (e.Button == MouseButtons.Left && Actor != null) + DrawWithActor(); - if (Brush.Second != null) + if (Brush != null || Actor != null) Invalidate(); } } @@ -63,8 +78,8 @@ namespace OpenRA.Editor void DrawWithBrush() { // change the bits in the map - var tile = TileSet.tiles[Brush.First]; - var template = TileSet.walk[Brush.First]; + var tile = TileSet.tiles[Brush.N]; + var template = TileSet.walk[Brush.N]; var pos = GetBrushLocation(); for (var u = 0; u < template.Size.X; u++) @@ -75,7 +90,7 @@ namespace OpenRA.Editor var z = u + v * template.Size.X; if (tile.TileBitmapBytes[z] != null) Map.MapTiles[u + pos.X, v + pos.Y] = - new TileReference { type = Brush.First, image = (byte)z, index = (byte)z }; + new TileReference { type = Brush.N, image = (byte)z, index = (byte)z }; var ch = new int2((pos.X + u) / ChunkSize, (pos.Y + v) / ChunkSize); if (Chunks.ContainsKey(ch)) @@ -87,15 +102,24 @@ namespace OpenRA.Editor } } + void DrawWithActor() + { + } + protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); if (e.Button == MouseButtons.Right) - Brush = Pair.New((ushort)0, null as Bitmap); + { + Actor = null; + Brush = null; + } - if (e.Button == MouseButtons.Left && Brush.Second != null) + if (e.Button == MouseButtons.Left && Brush != null) DrawWithBrush(); + if (e.Button == MouseButtons.Left && Actor != null) + DrawWithActor(); Invalidate(); } @@ -139,6 +163,15 @@ namespace OpenRA.Editor return new int2(v.X / 24, v.Y / 24); } + void DrawActor(System.Drawing.Graphics g, int2 p, ActorTemplate t) + { + g.DrawImage(t.Bitmap, + ((24 * p + Offset + - (t.Centered + ? new int2(t.Bitmap.Width / 2 - 12, t.Bitmap.Height / 2 - 12) + : int2.Zero)).ToPoint())); + } + protected override void OnPaint(PaintEventArgs e) { if (Map == null) return; @@ -155,9 +188,15 @@ namespace OpenRA.Editor e.Graphics.DrawRectangle(CordonPen, new Rectangle(Map.XOffset * 24 + Offset.X, Map.YOffset * 24 + Offset.Y, Map.Width * 24, Map.Height * 24)); - if (Brush.Second != null) - e.Graphics.DrawImage(Brush.Second, + foreach (var ar in Map.Actors) + DrawActor(e.Graphics, ar.Value.Location, ActorTemplates[ar.Value.Name]); + + if (Brush != null) + e.Graphics.DrawImage(Brush.Bitmap, (24 * GetBrushLocation() + Offset).ToPoint()); + + if (Actor != null) + DrawActor(e.Graphics, GetBrushLocation(), Actor); } } } \ No newline at end of file