GameText.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GameText.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 System.Text;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using RobotGameData.GameInterface;
  16. using RobotGameData.Render;
  17. using RobotGameData.Text;
  18. #endregion
  19. namespace RobotGameData.GameObject
  20. {
  21. /// <summary>
  22. /// displays text on 2D screen.
  23. /// Since it can attach directly to the 2D scene node,
  24. /// it will be affected by the 2D scene node.
  25. /// </summary>
  26. public class GameText : GameSceneNode
  27. {
  28. #region Fields
  29. TextItem textItem = new TextItem();
  30. #endregion
  31. #region Properties
  32. public SpriteFont Font
  33. {
  34. get { return this.textItem.Font; }
  35. set { this.textItem.Font = value; }
  36. }
  37. public string Text
  38. {
  39. get { return this.textItem.Text; }
  40. set { this.textItem.Text = value; }
  41. }
  42. public int PosX
  43. {
  44. get { return this.textItem.PosX; }
  45. set { this.textItem.PosX = value; }
  46. }
  47. public int PosY
  48. {
  49. get { return this.textItem.PosY; }
  50. set { this.textItem.PosY = value; }
  51. }
  52. public Color Color
  53. {
  54. get { return this.textItem.Color; }
  55. set { this.textItem.Color = value; }
  56. }
  57. public byte Alpha
  58. {
  59. get { return this.textItem.Color.A; }
  60. set
  61. {
  62. this.textItem.Color = new Color(
  63. this.textItem.Color.R,
  64. this.textItem.Color.G,
  65. this.textItem.Color.B,
  66. value);
  67. }
  68. }
  69. public float Rotation
  70. {
  71. get { return this.textItem.Rotation; }
  72. set { this.textItem.Rotation = value; }
  73. }
  74. public float Scale
  75. {
  76. get { return this.textItem.Scale; }
  77. set { this.textItem.Scale = value; }
  78. }
  79. #endregion
  80. /// <summary>
  81. /// Constructor.
  82. /// </summary>
  83. /// <param name="font">sprite font</param>
  84. /// <param name="text">message</param>
  85. /// <param name="x">position x</param>
  86. /// <param name="y">position y</param>
  87. /// <param name="color">text color</param>
  88. public GameText(SpriteFont font, string text, int x, int y, Color color)
  89. : base()
  90. {
  91. Create(font, text, x, y, color);
  92. }
  93. /// <summary>
  94. /// draws string on screens by using the information stored in TextItem.
  95. /// Internally, the sprite batch’s Begin() function and the End() function
  96. /// are not called.
  97. /// </summary>
  98. /// <param name="renderTracer"></param>
  99. protected override void OnDraw(RenderTracer renderTracer)
  100. {
  101. renderTracer.SpriteBatch.DrawString(this.textItem.Font,
  102. this.textItem.Text,
  103. new Vector2(this.textItem.PosX,
  104. this.textItem.PosY),
  105. this.textItem.Color,
  106. this.textItem.Rotation,
  107. Vector2.Zero,
  108. this.textItem.Scale,
  109. SpriteEffects.None,
  110. 1.0f);
  111. }
  112. /// <summary>
  113. /// creates a text message.
  114. /// text information is stored as TextItem structure.
  115. /// </summary>
  116. /// <param name="font">sprite font</param>
  117. /// <param name="text">string message</param>
  118. /// <param name="x">2D screen x-position (pixel)</param>
  119. /// <param name="y">2D screen y-position (pixel)</param>
  120. /// <param name="color">text color</param>
  121. public void Create(SpriteFont font, string text, int x, int y, Color color)
  122. {
  123. this.textItem.Font = font;
  124. this.textItem.Text = text;
  125. this.textItem.PosX = x;
  126. this.textItem.PosY = y;
  127. this.textItem.Color = color;
  128. }
  129. }
  130. }