#region File Description
//-----------------------------------------------------------------------------
// TextManager.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion
namespace RobotGameData.Text
{
///
/// It displays text on screen. Each text is stored as and managed
/// as TextItem class. The texts, displayed via TextManager, do not
/// get affected by the game screen (i.e., screen fade or sprite) but
/// gets displayed independently on an overlay screen.
/// In order to display the texts, which do get affected by the game screen,
/// (i.e. Hud information) GameText class, which inherited GameSceneNode,
/// has to be created and registered to scene2DLayer node as a child.
///
public class TextManager : DrawableGameComponent
{
#region Fields
protected SpriteBatch textBatch = null;
protected List textList = new List();
#endregion
///
/// Constructor.
///
public TextManager(Game game)
: base(game) {}
///
/// Allows the game component to perform any initialization it needs
/// to before starting to run. This is where it can query for any
/// required services and load content.
///
public override void Initialize()
{
// TODO: Add your initialization code here
textBatch = new SpriteBatch(FrameworkCore.Game.GraphicsDevice);
base.Initialize();
}
///
/// Allows the game component to update itself.
///
/// Provides a snapshot of timing values.
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
base.Update(gameTime);
}
///
/// displays the message of every text time that has been registered to
/// the list on 2D screen.
///
public override void Draw(GameTime gameTime)
{
textBatch.Begin(SpriteBlendMode.AlphaBlend);
// Draw each text on screen
foreach( TextItem item in textList)
{
if (item.Visible )
{
textBatch.DrawString(item.Font,
item.Text,
item.Position,
item.Color,
item.Rotation,
Vector2.Zero,
item.Scale,
SpriteEffects.None,
1.0f);
}
}
textBatch.End();
base.Draw(gameTime);
}
protected override void Dispose(bool disposing)
{
textBatch.Dispose();
textBatch = null;
ClearTextAll();
base.Dispose(disposing);
}
///
/// adds a text item.
///
/// a text item
public void AddText(TextItem item)
{
textList.Add(item);
}
///
/// adds a text.
///
/// sprite font
/// message
/// screen x-position
/// screen y-position
/// text color
/// text item
public TextItem AddText(SpriteFont font, string text, int x, int y, Color color)
{
TextItem item = new TextItem(font, text, x, y, color);
AddText(item);
return item;
}
///
/// removes the text item.
///
/// text item
public bool RemoveText(TextItem item)
{
return textList.Remove(item);
}
public void ClearTextAll()
{
textList.Clear();
}
///
/// gets a text item by id.
///
/// text item id
/// text item
public TextItem GetText(int id)
{
foreach (TextItem item in textList)
{
if (item.Id == id)
return item;
}
return null;
}
}
}