Files
OpenRA/OpenRA.Mods.RA/Render/RenderNameTag.cs
Matthias Mailänder d830a0a7ae Merge pull request #6029 from pchote/gametomods
Move *Renderables and Render* out of Game.
2014-07-23 06:54:42 +02:00

58 lines
1.6 KiB
C#

#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.Collections.Generic;
using System.Drawing;
using OpenRA.Graphics;
using OpenRA.Mods.RA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render
{
[Desc("Displays the player name above the unit")]
class RenderNameTagInfo : ITraitInfo
{
public readonly int MaxLength = 10;
public readonly string Font = "TinyBold";
public object Create(ActorInitializer init) { return new RenderNameTag(init.self, this); }
}
class RenderNameTag : IRender
{
readonly SpriteFont font;
readonly Color color;
readonly string name;
public RenderNameTag(Actor self, RenderNameTagInfo info)
{
font = Game.Renderer.Fonts[info.Font];
color = self.Owner.Color.RGB;
if (self.Owner.PlayerName.Length > info.MaxLength)
name = self.Owner.PlayerName.Substring(0, info.MaxLength);
else
name = self.Owner.PlayerName;
}
public IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr)
{
var pos = wr.ScreenPxPosition(self.CenterPosition);
var bounds = self.Bounds.Value;
bounds.Offset(pos.X, pos.Y);
var spaceBuffer = (int)(10 / wr.Viewport.Zoom);
var effectPos = wr.Position(new int2(pos.X, bounds.Y - spaceBuffer));
yield return new TextRenderable(font, effectPos, 0, color, name);
}
}
}