Merge pull request #120 from mrpimpunicorn/master

Added support for chat colors in FontRenderer
This commit is contained in:
Drew DeVault 2015-06-12 14:03:52 -06:00
commit 6da78bbbe5

View File

@ -1,6 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using TrueCraft.API;
namespace TrueCraft.Client
{
@ -25,20 +27,93 @@ namespace TrueCraft.Client
public void DrawText(SpriteBatch spriteBatch, int x, int y, string text)
{
int dx = x;
int dy = y;
foreach(char c in text)
var dx = x;
var dy = y;
var color = Color.White;
for (int i = 0; i < text.Length; i++)
{
if (text[i] == '§')
{
i++;
var code = string.Format("§{0}", text[i]);
color = GetColor(code);
}
else
{
FontChar fc;
if(_characterMap.TryGetValue(c, out fc))
if (_characterMap.TryGetValue(text[i], out fc))
{
var sourceRectangle = new Rectangle(fc.X, fc.Y, fc.Width, fc.Height);
var position = new Vector2(dx + fc.XOffset, dy + fc.YOffset);
spriteBatch.Draw(_texture, position, sourceRectangle, Color.White);
spriteBatch.Draw(_texture, position, sourceRectangle, color);
dx += fc.XAdvance;
}
}
}
}
// RGB values taken from http://minecraft.gamepedia.com/Formatting_codes
private static Color GetColor(string colorCode)
{
switch (colorCode)
{
case ChatColor.Black:
return new Color(0, 0, 0);
case ChatColor.DarkBlue:
return new Color(0, 0, 170);
case ChatColor.DarkGreen:
return new Color(0, 170, 0);
case ChatColor.DarkCyan:
return new Color(0, 170, 170);
case ChatColor.DarkRed:
return new Color(170, 0, 0);
case ChatColor.Purple:
return new Color(170, 0, 170);
case ChatColor.Orange:
return new Color(255, 170, 0);
case ChatColor.Gray:
return new Color(170, 170, 170);
case ChatColor.DarkGray:
return new Color(85, 85, 85);
case ChatColor.Blue:
return new Color(85, 85, 255);
case ChatColor.BrightGreen:
return new Color(85, 255, 85);
case ChatColor.Cyan:
return new Color(85, 255, 255);
case ChatColor.Red:
return new Color(255, 85, 85);
case ChatColor.Pink:
return new Color(255, 85, 255);
case ChatColor.Yellow:
return new Color(255, 255, 85);
case ChatColor.White:
return new Color(255, 255, 255);
default:
break;
}
// Technically this means we have an invalid color code,
// should we throw an exception?
return Color.White;
}
}
}