diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index 6d13a70cd9..13d7e8921d 100644 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -232,6 +232,7 @@ + diff --git a/OpenRA.Game/Widgets/SpriteWidget.cs b/OpenRA.Game/Widgets/SpriteWidget.cs new file mode 100644 index 0000000000..6e81dc8cbf --- /dev/null +++ b/OpenRA.Game/Widgets/SpriteWidget.cs @@ -0,0 +1,72 @@ +#region Copyright & License Information +/* + * Copyright 2007-2014 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using System; +using OpenRA.Graphics; + +namespace OpenRA.Widgets +{ + public class SpriteWidget : Widget + { + public string Palette = "chrome"; + public Func GetPalette; + public Func GetSprite; + + readonly WorldRenderer worldRenderer; + + [ObjectCreator.UseCtor] + public SpriteWidget(WorldRenderer worldRenderer) + { + GetPalette = () => { return Palette; }; + + this.worldRenderer = worldRenderer; + } + + protected SpriteWidget(SpriteWidget other) + : base(other) + { + Palette = other.Palette; + GetPalette = other.GetPalette; + GetSprite = other.GetSprite; + + worldRenderer = other.worldRenderer; + } + + public override Widget Clone() { return new SpriteWidget(this); } + + Sprite cachedSprite = null; + string cachedPalette = null; + PaletteReference pr; + float2 offset = float2.Zero; + + public override void Draw() + { + var sprite = GetSprite(); + var palette = GetPalette(); + + if (sprite == null || palette == null) + return; + + if (sprite != cachedSprite) + { + offset = 0.5f * (new float2(RenderBounds.Size) - sprite.size); + cachedSprite = sprite; + } + + if (palette != cachedPalette) + { + pr = worldRenderer.Palette(palette); + cachedPalette = palette; + } + + Game.Renderer.SpriteRenderer.DrawSprite(sprite, RenderOrigin + offset, pr); + } + } +}