TextManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // TextManager.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. #endregion
  15. namespace RobotGameData.Text
  16. {
  17. /// <summary>
  18. /// It displays text on screen. Each text is stored as and managed
  19. /// as TextItem class. The texts, displayed via TextManager, do not
  20. /// get affected by the game screen (i.e., screen fade or sprite) but
  21. /// gets displayed independently on an overlay screen.
  22. /// In order to display the texts, which do get affected by the game screen,
  23. /// (i.e. Hud information) GameText class, which inherited GameSceneNode,
  24. /// has to be created and registered to scene2DLayer node as a child.
  25. /// </summary>
  26. public class TextManager : DrawableGameComponent
  27. {
  28. #region Fields
  29. protected SpriteBatch textBatch = null;
  30. protected List<TextItem> textList = new List<TextItem>();
  31. #endregion
  32. /// <summary>
  33. /// Constructor.
  34. /// </summary>
  35. public TextManager(Game game)
  36. : base(game) {}
  37. /// <summary>
  38. /// Allows the game component to perform any initialization it needs
  39. /// to before starting to run. This is where it can query for any
  40. /// required services and load content.
  41. /// </summary>
  42. public override void Initialize()
  43. {
  44. // TODO: Add your initialization code here
  45. textBatch = new SpriteBatch(FrameworkCore.Game.GraphicsDevice);
  46. base.Initialize();
  47. }
  48. /// <summary>
  49. /// Allows the game component to update itself.
  50. /// </summary>
  51. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  52. public override void Update(GameTime gameTime)
  53. {
  54. // TODO: Add your update code here
  55. base.Update(gameTime);
  56. }
  57. /// <summary>
  58. /// displays the message of every text time that has been registered to
  59. /// the list on 2D screen.
  60. /// </summary>
  61. public override void Draw(GameTime gameTime)
  62. {
  63. textBatch.Begin(SpriteBlendMode.AlphaBlend);
  64. // Draw each text on screen
  65. foreach( TextItem item in textList)
  66. {
  67. if (item.Visible )
  68. {
  69. textBatch.DrawString(item.Font,
  70. item.Text,
  71. item.Position,
  72. item.Color,
  73. item.Rotation,
  74. Vector2.Zero,
  75. item.Scale,
  76. SpriteEffects.None,
  77. 1.0f);
  78. }
  79. }
  80. textBatch.End();
  81. base.Draw(gameTime);
  82. }
  83. protected override void Dispose(bool disposing)
  84. {
  85. textBatch.Dispose();
  86. textBatch = null;
  87. ClearTextAll();
  88. base.Dispose(disposing);
  89. }
  90. /// <summary>
  91. /// adds a text item.
  92. /// </summary>
  93. /// <param name="item">a text item</param>
  94. public void AddText(TextItem item)
  95. {
  96. textList.Add(item);
  97. }
  98. /// <summary>
  99. /// adds a text.
  100. /// </summary>
  101. /// <param name="font">sprite font</param>
  102. /// <param name="text">message</param>
  103. /// <param name="x">screen x-position</param>
  104. /// <param name="y">screen y-position</param>
  105. /// <param name="color">text color</param>
  106. /// <returns>text item</returns>
  107. public TextItem AddText(SpriteFont font, string text, int x, int y, Color color)
  108. {
  109. TextItem item = new TextItem(font, text, x, y, color);
  110. AddText(item);
  111. return item;
  112. }
  113. /// <summary>
  114. /// removes the text item.
  115. /// </summary>
  116. /// <param name="item">text item</param>
  117. public bool RemoveText(TextItem item)
  118. {
  119. return textList.Remove(item);
  120. }
  121. public void ClearTextAll()
  122. {
  123. textList.Clear();
  124. }
  125. /// <summary>
  126. /// gets a text item by id.
  127. /// </summary>
  128. /// <param name="id">text item id</param>
  129. /// <returns>text item</returns>
  130. public TextItem GetText(int id)
  131. {
  132. foreach (TextItem item in textList)
  133. {
  134. if (item.Id == id)
  135. return item;
  136. }
  137. return null;
  138. }
  139. }
  140. }