Moved RenderSprites.cs from Mods.RA to Common

This commit is contained in:
penev92
2014-11-02 18:33:47 +02:00
parent 78039bde31
commit 623642f9d5
35 changed files with 35 additions and 15 deletions

View File

@@ -12,6 +12,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Mods.RA.Render;
using OpenRA.Traits;

View File

@@ -9,7 +9,7 @@
#endregion
using OpenRA.Graphics;
using OpenRA.Mods.RA.Render;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Buildings

View File

@@ -9,7 +9,7 @@
#endregion
using OpenRA.Graphics;
using OpenRA.Mods.RA.Render;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA

View File

@@ -10,9 +10,9 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Mods.RA.Move;
using OpenRA.Mods.RA.Render;
using OpenRA.Primitives;
using OpenRA.Traits;

View File

@@ -12,7 +12,7 @@ using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Mods.Common.Orders;
using OpenRA.Mods.RA.Render;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA

View File

@@ -474,7 +474,6 @@
<Compile Include="Render\WithActiveAnimation.cs" />
<Compile Include="SupportPowers\SpawnActorPower.cs" />
<Compile Include="Render\RenderSimple.cs" />
<Compile Include="Render\RenderSprites.cs" />
<Compile Include="Air\FlyAwayOnIdle.cs" />
<Compile Include="Buildings\ClonesProducedUnits.cs" />
<Compile Include="Cloneable.cs" />
@@ -569,4 +568,4 @@ copy "FuzzyLogicLibrary.dll" "$(SolutionDir)"
cd "$(SolutionDir)"</PostBuildEvent>
</PropertyGroup>
<ItemGroup />
</Project>
</Project>

View File

@@ -12,6 +12,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Mods.RA.Render;

View File

@@ -10,6 +10,7 @@
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
using OpenRA.Mods.Common.Graphics;

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render

View File

@@ -12,6 +12,7 @@ using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render

View File

@@ -11,6 +11,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;

View File

@@ -10,6 +10,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Mods.RA.Effects;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Graphics;

View File

@@ -12,6 +12,7 @@ using System;
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render

View File

@@ -1,209 +0,0 @@
#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 System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;
using OpenRA.Primitives;
namespace OpenRA.Mods.RA.Render
{
public interface IRenderActorPreviewSpritesInfo { IEnumerable<IActorPreview> RenderPreviewSprites(ActorPreviewInitializer init, RenderSpritesInfo rs, string image, int facings, PaletteReference p); }
public class RenderSpritesInfo : IRenderActorPreviewInfo, ITraitInfo
{
[Desc("Defaults to the actor name.")]
public readonly string Image = null;
[Desc("Custom palette name")]
public readonly string Palette = null;
[Desc("Custom PlayerColorPalette: BaseName")]
public readonly string PlayerPalette = "player";
[Desc("Change the sprite image size.")]
public readonly float Scale = 1f;
public virtual object Create(ActorInitializer init) { return new RenderSprites(init.self); }
public IEnumerable<IActorPreview> RenderPreview(ActorPreviewInitializer init)
{
var sequenceProvider = init.World.Map.SequenceProvider;
var image = RenderSprites.GetImage(init.Actor);
var palette = init.WorldRenderer.Palette(Palette ?? (init.Owner != null ? PlayerPalette + init.Owner.InternalName : null));
var facings = 0;
var body = init.Actor.Traits.GetOrDefault<BodyOrientationInfo>();
if (body != null)
facings = body.QuantizedFacings == -1 ? init.Actor.Traits.Get<IQuantizeBodyOrientationInfo>().QuantizedBodyFacings(sequenceProvider, init.Actor) : body.QuantizedFacings;
foreach (var spi in init.Actor.Traits.WithInterface<IRenderActorPreviewSpritesInfo>())
foreach (var preview in spi.RenderPreviewSprites(init, this, image, facings, palette))
yield return preview;
}
}
public class RenderSprites : IRender, ITick, INotifyOwnerChanged, INotifyEffectiveOwnerChanged
{
class AnimationWrapper
{
public readonly AnimationWithOffset Animation;
public readonly string Palette;
public readonly bool IsPlayerPalette;
public PaletteReference PaletteReference { get; private set; }
public AnimationWrapper(AnimationWithOffset animation, string palette, bool isPlayerPalette)
{
Animation = animation;
Palette = palette;
IsPlayerPalette = isPlayerPalette;
}
public void CachePalette(WorldRenderer wr, Player owner)
{
PaletteReference = wr.Palette(IsPlayerPalette ? Palette + owner.InternalName : Palette);
}
public void OwnerChanged()
{
// Update the palette reference next time we draw
if (IsPlayerPalette)
PaletteReference = null;
}
public bool IsVisible
{
get
{
return Animation.DisableFunc == null || !Animation.DisableFunc();
}
}
}
Dictionary<string, AnimationWrapper> anims = new Dictionary<string, AnimationWrapper>();
public static Func<int> MakeFacingFunc(Actor self)
{
var facing = self.TraitOrDefault<IFacing>();
if (facing == null) return () => 0;
return () => facing.Facing;
}
readonly RenderSpritesInfo info;
string cachedImage = null;
public RenderSprites(Actor self)
{
info = self.Info.Traits.Get<RenderSpritesInfo>();
}
public static string GetImage(ActorInfo actor)
{
var Info = actor.Traits.Get<RenderSpritesInfo>();
return (Info.Image ?? actor.Name).ToLowerInvariant();
}
public string GetImage(Actor self)
{
if (cachedImage != null)
return cachedImage;
return cachedImage = GetImage(self.Info);
}
protected void UpdatePalette()
{
foreach (var anim in anims.Values)
anim.OwnerChanged();
}
public virtual void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) { UpdatePalette(); }
public void OnEffectiveOwnerChanged(Actor self, Player oldEffectiveOwner, Player newEffectiveOwner) { UpdatePalette(); }
public virtual IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr)
{
foreach (var a in anims.Values)
{
if (!a.IsVisible)
continue;
if (a.PaletteReference == null)
{
var owner = self.EffectiveOwner != null && self.EffectiveOwner.Disguised ? self.EffectiveOwner.Owner : self.Owner;
a.CachePalette(wr, owner);
}
foreach (var r in a.Animation.Render(self, wr, a.PaletteReference, info.Scale))
yield return r;
}
}
public virtual void Tick(Actor self)
{
foreach (var a in anims.Values)
a.Animation.Animation.Tick();
}
public void Add(string key, AnimationWithOffset anim, string palette = null, bool isPlayerPalette = false)
{
// Use defaults
if (palette == null)
{
palette = info.Palette ?? info.PlayerPalette;
isPlayerPalette = info.Palette == null;
}
anims.Add(key, new AnimationWrapper(anim, palette, isPlayerPalette));
}
public void Remove(string key)
{
anims.Remove(key);
}
public static string NormalizeSequence(Animation anim, DamageState state, string sequence)
{
var states = new Pair<DamageState, string>[]
{
Pair.New(DamageState.Critical, "critical-"),
Pair.New(DamageState.Heavy, "damaged-"),
Pair.New(DamageState.Medium, "scratched-"),
Pair.New(DamageState.Light, "scuffed-")
};
// Remove existing damage prefix
foreach (var s in states)
{
if (sequence.StartsWith(s.Second))
{
sequence = sequence.Substring(s.Second.Length);
break;
}
}
foreach (var s in states)
if (state >= s.First && anim.HasSequence(s.Second + sequence))
return s.Second + sequence;
return sequence;
}
// Required by RenderSimple
protected int2 AutoSelectionSize(Actor self)
{
return anims.Values.Where(b => b.IsVisible
&& b.Animation.Animation.CurrentSequence != null)
.Select(a => (a.Animation.Animation.Image.size*info.Scale).ToInt2())
.FirstOrDefault();
}
}
}

View File

@@ -11,6 +11,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common;
using OpenRA.Traits;
using OpenRA.Mods.Common.Graphics;

View File

@@ -12,6 +12,7 @@ using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render

View File

@@ -11,6 +11,7 @@
using System;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render

View File

@@ -9,6 +9,7 @@
#endregion
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render

View File

@@ -11,6 +11,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;

View File

@@ -12,6 +12,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render

View File

@@ -11,6 +11,7 @@
using System.Linq;
using OpenRA.Effects;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Traits;

View File

@@ -12,6 +12,7 @@ using System;
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render

View File

@@ -9,6 +9,7 @@
#endregion
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render

View File

@@ -12,6 +12,7 @@ using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render

View File

@@ -45,5 +45,4 @@ namespace OpenRA.Mods.RA
public interface INotifyParachuteLanded { void OnLanded(); }
public interface INotifyTransform { void BeforeTransform(Actor self); void OnTransform(Actor self); void AfterTransform(Actor toActor); }
public interface INotifyAttack { void Attacking(Actor self, Target target, Armament a, Barrel barrel); }
public interface IRenderActorPreviewInfo { IEnumerable<IActorPreview> RenderPreview(ActorPreviewInitializer init); }
}