TextControl.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //-----------------------------------------------------------------------------
  2. // TextControl.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. namespace UserInterfaceSample.Controls
  10. {
  11. /// <summary>
  12. /// TextControl is a control that displays a single string of text. By default, the
  13. /// size is computed from the given text and spritefont.
  14. /// </summary>
  15. public class TextControl : Control
  16. {
  17. private SpriteFont font;
  18. private string text;
  19. public Color Color;
  20. // Actual text to draw
  21. public string Text
  22. {
  23. get { return text; }
  24. set
  25. {
  26. if (text != value)
  27. {
  28. text = value;
  29. InvalidateAutoSize();
  30. }
  31. }
  32. }
  33. // Font to use
  34. public SpriteFont Font
  35. {
  36. get { return Font; }
  37. set
  38. {
  39. if (font != value)
  40. {
  41. font = value;
  42. InvalidateAutoSize();
  43. }
  44. }
  45. }
  46. public TextControl()
  47. : this(string.Empty, null, Color.White, Vector2.Zero)
  48. {
  49. }
  50. public TextControl(string text, SpriteFont font)
  51. : this(text, font, Color.White, Vector2.Zero)
  52. {
  53. }
  54. public TextControl(string text, SpriteFont font, Color color)
  55. : this(text, font, color, Vector2.Zero)
  56. {
  57. }
  58. public TextControl(string text, SpriteFont font, Color color, Vector2 position)
  59. {
  60. this.text = text;
  61. this.font = font;
  62. this.Position = position;
  63. this.Color = color;
  64. }
  65. public override void Draw(DrawContext context)
  66. {
  67. base.Draw(context);
  68. context.SpriteBatch.DrawString(font, Text, context.DrawOffset, Color);
  69. }
  70. override public Vector2 ComputeSize()
  71. {
  72. return font.MeasureString(Text);
  73. }
  74. }
  75. }