Adjust GetImage plumbing in preparation for race-specific sequences.

This commit is contained in:
Paul Chote
2014-07-09 18:44:25 +12:00
committed by Paul Chote
parent 4c3a95ebc0
commit 00a2146299
18 changed files with 62 additions and 48 deletions

View File

@@ -238,7 +238,9 @@ namespace OpenRA.Editor
if (rsi != null && rsi.EditorPalette != null && rsi.EditorPalette.Contains("terrain")) if (rsi != null && rsi.EditorPalette != null && rsi.EditorPalette.Contains("terrain"))
templatePalette = palette; templatePalette = palette;
var template = RenderUtils.RenderActor(info, tileset, templatePalette); var race = Program.Rules.Actors["world"].Traits.WithInterface<CountryInfo>().First().Race;
var sequenceProvider = Program.Rules.Sequences[tileset.Id];
var template = RenderUtils.RenderActor(info, sequenceProvider, tileset, templatePalette, race);
var ibox = new PictureBox var ibox = new PictureBox
{ {
Image = template.Bitmap, Image = template.Bitmap,

View File

@@ -46,9 +46,9 @@ namespace OpenRA.Editor
return bitmap; return bitmap;
} }
public static ActorTemplate RenderActor(ActorInfo info, TileSet tileset, IPalette p) public static ActorTemplate RenderActor(ActorInfo info, SequenceProvider sequenceProvider, TileSet tileset, IPalette p, string race)
{ {
var image = info.Traits.Get<ILegacyEditorRenderInfo>().EditorImage(info); var image = info.Traits.Get<ILegacyEditorRenderInfo>().EditorImage(info, sequenceProvider, race);
using (var s = GlobalFileSystem.OpenWithExts(image, tileset.Extensions)) using (var s = GlobalFileSystem.OpenWithExts(image, tileset.Extensions))
{ {
var shp = new ShpTDSprite(s); var shp = new ShpTDSprite(s);

View File

@@ -102,4 +102,14 @@ namespace OpenRA
return world.Players.First(x => x.InternalName == PlayerName); return world.Players.First(x => x.InternalName == PlayerName);
} }
} }
// Allows maps / transformations to specify the race variant of an actor.
public class RaceInit : IActorInit<string>
{
[FieldFromYamlKey] public readonly string Race;
public RaceInit() { }
public RaceInit(string race) { Race = race; }
public string Value(World world) { return Race; }
}
} }

View File

@@ -40,7 +40,7 @@ namespace OpenRA.Traits
return new WRot(WAngle.Zero, WAngle.Zero, WAngle.FromFacing(facing)); return new WRot(WAngle.Zero, WAngle.Zero, WAngle.FromFacing(facing));
} }
public object Create(ActorInitializer init) { return new BodyOrientation(init.Self, this); } public object Create(ActorInitializer init) { return new BodyOrientation(init, this); }
} }
public class BodyOrientation : IBodyOrientation public class BodyOrientation : IBodyOrientation
@@ -50,9 +50,11 @@ namespace OpenRA.Traits
[Sync] public int QuantizedFacings { get { return quantizedFacings.Value; } } [Sync] public int QuantizedFacings { get { return quantizedFacings.Value; } }
public BodyOrientation(Actor self, BodyOrientationInfo info) public BodyOrientation(ActorInitializer init, BodyOrientationInfo info)
{ {
this.info = info; this.info = info;
var self = init.Self;
var race = init.Contains<RaceInit>() ? init.Get<RaceInit, string>() : self.Owner.Country.Race;
quantizedFacings = Exts.Lazy(() => quantizedFacings = Exts.Lazy(() =>
{ {
@@ -64,7 +66,7 @@ namespace OpenRA.Traits
if (qboi == null) if (qboi == null)
throw new InvalidOperationException("Actor type '" + self.Info.Name + "' does not define a quantized body orientation."); throw new InvalidOperationException("Actor type '" + self.Info.Name + "' does not define a quantized body orientation.");
return qboi.QuantizedBodyFacings(self.World.Map.SequenceProvider, self.Info); return qboi.QuantizedBodyFacings(self.Info, self.World.Map.SequenceProvider, race);
}); });
} }

View File

@@ -289,7 +289,7 @@ namespace OpenRA.Traits
WRot QuantizeOrientation(WRot orientation, int facings); WRot QuantizeOrientation(WRot orientation, int facings);
} }
public interface IQuantizeBodyOrientationInfo { int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai); } public interface IQuantizeBodyOrientationInfo { int QuantizedBodyFacings(ActorInfo ai, SequenceProvider sequenceProvider, string race); }
public interface ITargetableInfo public interface ITargetableInfo
{ {
@@ -334,6 +334,6 @@ namespace OpenRA.Traits
public interface ILegacyEditorRenderInfo public interface ILegacyEditorRenderInfo
{ {
string EditorPalette { get; } string EditorPalette { get; }
string EditorImage(ActorInfo actor); string EditorImage(ActorInfo actor, SequenceProvider sequenceProvider, string race);
} }
} }

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Mods.Cnc.Traits
public override object Create(ActorInitializer init) { return new RenderGunboat(init, this); } public override object Create(ActorInitializer init) { return new RenderGunboat(init, this); }
public int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai) public int QuantizedBodyFacings(ActorInfo ai, SequenceProvider sequenceProvider, string race)
{ {
return 2; return 2;
} }

View File

@@ -74,14 +74,4 @@ namespace OpenRA.Mods.Common.Traits
enabled = owner.PlayerActor.Trait<TechTree>().HasPrerequisites(info.RequiresPrerequisites); enabled = owner.PlayerActor.Trait<TechTree>().HasPrerequisites(info.RequiresPrerequisites);
} }
} }
// Allows maps / transformations to specify the race variant of an actor.
public class RaceInit : IActorInit<string>
{
[FieldFromYamlKey] public readonly string Race;
public RaceInit() { }
public RaceInit(string race) { Race = race; }
public string Value(World world) { return Race; }
}
} }

View File

@@ -39,9 +39,9 @@ namespace OpenRA.Mods.Common.Traits
yield return new SpriteActorPreview(anim, WVec.Zero, 0, p, rs.Scale); yield return new SpriteActorPreview(anim, WVec.Zero, 0, p, rs.Scale);
} }
public override int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai) public override int QuantizedBodyFacings(ActorInfo ai, SequenceProvider sequenceProvider, string race)
{ {
return sequenceProvider.GetSequence(RenderSprites.GetImage(ai), StandAnimations.First()).Facings; return sequenceProvider.GetSequence(GetImage(ai, sequenceProvider, race), StandAnimations.First()).Facings;
} }
} }

View File

@@ -30,13 +30,13 @@ namespace OpenRA.Mods.Common.Traits
yield return new SpriteActorPreview(anim, WVec.Zero, 0, p, rs.Scale); yield return new SpriteActorPreview(anim, WVec.Zero, 0, p, rs.Scale);
} }
public virtual int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai) public virtual int QuantizedBodyFacings(ActorInfo ai, SequenceProvider sequenceProvider, string race)
{ {
return sequenceProvider.GetSequence(RenderSprites.GetImage(ai), "idle").Facings; return sequenceProvider.GetSequence(GetImage(ai, sequenceProvider, race), "idle").Facings;
} }
public string EditorPalette { get { return Palette; } } public string EditorPalette { get { return Palette; } }
public string EditorImage(ActorInfo actor) { return RenderSimple.GetImage(actor); } public string EditorImage(ActorInfo actor, SequenceProvider sequenceProvider, string race) { return GetImage(actor, sequenceProvider, race); }
} }
public class RenderSimple : RenderSprites, IAutoSelectionSize public class RenderSimple : RenderSprites, IAutoSelectionSize

View File

@@ -37,18 +37,24 @@ namespace OpenRA.Mods.Common.Traits
public IEnumerable<IActorPreview> RenderPreview(ActorPreviewInitializer init) public IEnumerable<IActorPreview> RenderPreview(ActorPreviewInitializer init)
{ {
var sequenceProvider = init.World.Map.SequenceProvider; var sequenceProvider = init.World.Map.SequenceProvider;
var image = RenderSprites.GetImage(init.Actor); var race = init.Contains<RaceInit>() ? init.Get<RaceInit, string>() : init.Owner.Country.Race;
var palette = init.WorldRenderer.Palette(Palette ?? (init.Owner != null ? PlayerPalette + init.Owner.InternalName : null)); var image = GetImage(init.Actor, sequenceProvider, race);
var palette = init.WorldRenderer.Palette(Palette ?? PlayerPalette + init.Owner.InternalName);
var facings = 0; var facings = 0;
var body = init.Actor.Traits.GetOrDefault<BodyOrientationInfo>(); var body = init.Actor.Traits.GetOrDefault<BodyOrientationInfo>();
if (body != null) if (body != null)
facings = body.QuantizedFacings == -1 ? init.Actor.Traits.Get<IQuantizeBodyOrientationInfo>().QuantizedBodyFacings(sequenceProvider, init.Actor) : body.QuantizedFacings; facings = body.QuantizedFacings == -1 ? init.Actor.Traits.Get<IQuantizeBodyOrientationInfo>().QuantizedBodyFacings(init.Actor, sequenceProvider, init.Owner.Country.Race) : body.QuantizedFacings;
foreach (var spi in init.Actor.Traits.WithInterface<IRenderActorPreviewSpritesInfo>()) foreach (var spi in init.Actor.Traits.WithInterface<IRenderActorPreviewSpritesInfo>())
foreach (var preview in spi.RenderPreviewSprites(init, this, image, facings, palette)) foreach (var preview in spi.RenderPreviewSprites(init, this, image, facings, palette))
yield return preview; yield return preview;
} }
public string GetImage(ActorInfo actor, SequenceProvider sequenceProvider, string race)
{
return (Image ?? actor.Name).ToLowerInvariant();
}
} }
public class RenderSprites : IRender, ITick, INotifyOwnerChanged, INotifyEffectiveOwnerChanged public class RenderSprites : IRender, ITick, INotifyOwnerChanged, INotifyEffectiveOwnerChanged
@@ -88,9 +94,10 @@ namespace OpenRA.Mods.Common.Traits
} }
} }
readonly string race;
readonly RenderSpritesInfo info; readonly RenderSpritesInfo info;
string cachedImage = null; readonly Dictionary<string, AnimationWrapper> anims = new Dictionary<string, AnimationWrapper>();
Dictionary<string, AnimationWrapper> anims = new Dictionary<string, AnimationWrapper>(); string cachedImage;
public static Func<int> MakeFacingFunc(Actor self) public static Func<int> MakeFacingFunc(Actor self)
{ {
@@ -102,12 +109,7 @@ namespace OpenRA.Mods.Common.Traits
public RenderSprites(ActorInitializer init, RenderSpritesInfo info) public RenderSprites(ActorInitializer init, RenderSpritesInfo info)
{ {
this.info = info; this.info = info;
} race = init.Contains<RaceInit>() ? init.Get<RaceInit, string>() : init.Self.Owner.Country.Race;
public static string GetImage(ActorInfo actor)
{
var info = actor.Traits.Get<RenderSpritesInfo>();
return (info.Image ?? actor.Name).ToLowerInvariant();
} }
public string GetImage(Actor self) public string GetImage(Actor self)
@@ -115,7 +117,7 @@ namespace OpenRA.Mods.Common.Traits
if (cachedImage != null) if (cachedImage != null)
return cachedImage; return cachedImage;
return cachedImage = GetImage(self.Info); return cachedImage = info.GetImage(self.Info, self.World.Map.SequenceProvider, race);
} }
protected void UpdatePalette() protected void UpdatePalette()

View File

@@ -67,11 +67,13 @@ namespace OpenRA.Mods.Common.Widgets
if (current == null) if (current == null)
continue; continue;
var race = queue.Trait.Actor.Owner.Country.Race;
var actor = queue.Trait.AllItems().FirstOrDefault(a => a.Name == current.Item); var actor = queue.Trait.AllItems().FirstOrDefault(a => a.Name == current.Item);
if (actor == null) if (actor == null)
continue; continue;
var icon = new Animation(world, RenderSimple.GetImage(actor)); var rsi = actor.Traits.Get<RenderSpritesInfo>();
var icon = new Animation(world, rsi.GetImage(actor, world.Map.SequenceProvider, race));
icon.Play(actor.Traits.Get<TooltipInfo>().Icon); icon.Play(actor.Traits.Get<TooltipInfo>().Icon);
var location = new float2(RenderBounds.Location) + new float2(queue.i * (IconWidth + IconSpacing), 0); var location = new float2(RenderBounds.Location) + new float2(queue.i * (IconWidth + IconSpacing), 0);
WidgetUtils.DrawSHPCentered(icon.Image, location + 0.5f * iconSize, worldRenderer, 0.5f); WidgetUtils.DrawSHPCentered(icon.Image, location + 0.5f * iconSize, worldRenderer, 0.5f);

View File

@@ -268,13 +268,16 @@ namespace OpenRA.Mods.Common.Widgets
var ks = Game.Settings.Keys; var ks = Game.Settings.Keys;
var rb = RenderBounds; var rb = RenderBounds;
var race = CurrentQueue.Actor.Owner.Country.Race;
foreach (var item in AllBuildables.Skip(IconRowOffset * Columns).Take(MaxIconRowOffset * Columns)) foreach (var item in AllBuildables.Skip(IconRowOffset * Columns).Take(MaxIconRowOffset * Columns))
{ {
var x = DisplayedIconCount % Columns; var x = DisplayedIconCount % Columns;
var y = DisplayedIconCount / Columns; var y = DisplayedIconCount / Columns;
var rect = new Rectangle(rb.X + x * (IconSize.X + IconMargin.X), rb.Y + y * (IconSize.Y + IconMargin.Y), IconSize.X, IconSize.Y); var rect = new Rectangle(rb.X + x * (IconSize.X + IconMargin.X), rb.Y + y * (IconSize.Y + IconMargin.Y), IconSize.X, IconSize.Y);
var icon = new Animation(World, RenderSimple.GetImage(item));
var rsi = item.Traits.Get<RenderSpritesInfo>();
var icon = new Animation(World, rsi.GetImage(item, World.Map.SequenceProvider, race));
icon.Play(item.Traits.Get<TooltipInfo>().Icon); icon.Play(item.Traits.Get<TooltipInfo>().Icon);
var pi = new ProductionIcon() var pi = new ProductionIcon()

View File

@@ -152,7 +152,8 @@ namespace OpenRA.Mods.D2k.Traits
isCarrying = true; isCarrying = true;
// Create a new animation for our carryable unit // Create a new animation for our carryable unit
anim = new Animation(self.World, RenderSprites.GetImage(carryable.Info), RenderSprites.MakeFacingFunc(self)); var rs = carryable.Trait<RenderSprites>();
anim = new Animation(self.World, rs.GetImage(carryable), RenderSprites.MakeFacingFunc(self));
anim.PlayRepeating("idle"); anim.PlayRepeating("idle");
anim.IsDecoration = true; anim.IsDecoration = true;
} }

View File

@@ -21,18 +21,20 @@ namespace OpenRA.Mods.RA.Traits
[Desc("Uses the \"Cloneable\" trait to determine whether or not we should clone a produced unit.")] [Desc("Uses the \"Cloneable\" trait to determine whether or not we should clone a produced unit.")]
public readonly string[] CloneableTypes = { }; public readonly string[] CloneableTypes = { };
public object Create(ActorInitializer init) { return new ClonesProducedUnits(init.Self, this); } public object Create(ActorInitializer init) { return new ClonesProducedUnits(init, this); }
} }
public class ClonesProducedUnits : INotifyOtherProduction public class ClonesProducedUnits : INotifyOtherProduction
{ {
readonly ClonesProducedUnitsInfo info; readonly ClonesProducedUnitsInfo info;
readonly Production production; readonly Production production;
readonly string race;
public ClonesProducedUnits(Actor self, ClonesProducedUnitsInfo info) public ClonesProducedUnits(ActorInitializer init, ClonesProducedUnitsInfo info)
{ {
this.info = info; this.info = info;
production = self.Trait<Production>(); production = init.Self.Trait<Production>();
race = init.Contains<RaceInit>() ? init.Get<RaceInit, string>() : init.Self.Owner.Country.Race;
} }
public void UnitProducedByOther(Actor self, Actor producer, Actor produced) public void UnitProducedByOther(Actor self, Actor producer, Actor produced)
@@ -45,7 +47,7 @@ namespace OpenRA.Mods.RA.Traits
if (ci == null || !info.CloneableTypes.Intersect(ci.Types).Any()) if (ci == null || !info.CloneableTypes.Intersect(ci.Types).Any())
return; return;
production.Produce(self, produced.Info, self.Owner.Country.Race); production.Produce(self, produced.Info, race);
} }
} }
} }

View File

@@ -46,8 +46,8 @@ namespace OpenRA.Mods.TS.Traits
var body = init.Actor.Traits.Get<BodyOrientationInfo>(); var body = init.Actor.Traits.Get<BodyOrientationInfo>();
var sequenceProvider = init.World.Map.SequenceProvider; var sequenceProvider = init.World.Map.SequenceProvider;
var image = Image ?? init.Actor.Name; var image = Image ?? init.Actor.Name;
var facings = body.QuantizedFacings == -1 ? init.Actor.Traits.Get<IQuantizeBodyOrientationInfo>().QuantizedBodyFacings(sequenceProvider, init.Actor) : body.QuantizedFacings; var facings = body.QuantizedFacings == -1 ? init.Actor.Traits.Get<IQuantizeBodyOrientationInfo>().QuantizedBodyFacings(init.Actor, sequenceProvider, init.Owner.Country.Race) : body.QuantizedFacings;
var palette = init.WorldRenderer.Palette(Palette ?? (init.Owner != null ? PlayerPalette + init.Owner.InternalName : null)); var palette = init.WorldRenderer.Palette(Palette ?? PlayerPalette + init.Owner.InternalName);
var ifacing = init.Actor.Traits.GetOrDefault<IFacingInfo>(); var ifacing = init.Actor.Traits.GetOrDefault<IFacingInfo>();
var facing = ifacing != null ? init.Contains<FacingInit>() ? init.Get<FacingInit, int>() : ifacing.GetInitialFacing() : 0; var facing = ifacing != null ? init.Contains<FacingInit>() ? init.Get<FacingInit, int>() : ifacing.GetInitialFacing() : 0;

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Mods.TS.Traits
() => false, () => 0); () => false, () => 0);
} }
public int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai) { return 0; } public int QuantizedBodyFacings(ActorInfo ai, SequenceProvider sequenceProvider, string race) { return 0; }
} }
public class WithVoxelBody : IAutoSelectionSize public class WithVoxelBody : IAutoSelectionSize

View File

@@ -36,7 +36,7 @@ namespace OpenRA.Mods.TS.Traits
() => false, () => 0); () => false, () => 0);
} }
public int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai) { return 0; } public int QuantizedBodyFacings(ActorInfo ai, SequenceProvider sequenceProvider, string race) { return 0; }
} }
public class WithVoxelUnloadBody : IAutoSelectionSize public class WithVoxelUnloadBody : IAutoSelectionSize

View File

@@ -20,7 +20,7 @@ namespace OpenRA.Mods.TS.Traits
public readonly int TickRate = 5; public readonly int TickRate = 5;
public object Create(ActorInitializer init) { return new WithVoxelWalkerBody(init.Self, this); } public object Create(ActorInitializer init) { return new WithVoxelWalkerBody(init.Self, this); }
public int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai) { return 0; } public int QuantizedBodyFacings(ActorInfo ai, SequenceProvider sequenceProvider, string race) { return 0; }
} }
public class WithVoxelWalkerBody : IAutoSelectionSize, ITick public class WithVoxelWalkerBody : IAutoSelectionSize, ITick