TextControl.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #region File Information
  2. //-----------------------------------------------------------------------------
  3. // ATextControl.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.Linq;
  13. using System.Text;
  14. using Microsoft.Xna.Framework.Content;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using Microsoft.Xna.Framework;
  17. #endregion
  18. namespace DynamicMenu.Controls
  19. {
  20. /// <summary>
  21. /// A control implementing the ITextControl interface, containing text
  22. /// </summary>
  23. public abstract class TextControl : Control, ITextControl
  24. {
  25. #region Fields
  26. /// <summary>
  27. /// The amount of space to the left and right of the text when auto-sized using the
  28. /// AutoPickWidth function
  29. /// </summary>
  30. private const int SPACE = 10;
  31. #endregion
  32. #region Properties
  33. /// <summary>
  34. /// The text shown in the control
  35. /// </summary>
  36. public string Text { get; set; }
  37. /// <summary>
  38. /// The name of the font to use for this control (full path to a spritefont)
  39. /// </summary>
  40. public string FontName { get; set; }
  41. /// <summary>
  42. /// The font loaded from the FontName
  43. /// </summary>
  44. [ContentSerializerIgnore]
  45. public SpriteFont Font { get; set; }
  46. /// <summary>
  47. /// The color of the text
  48. /// </summary>
  49. [ContentSerializer(Optional = true)]
  50. public Color TextColor { get; set; }
  51. #endregion
  52. #region Initialization
  53. public TextControl()
  54. {
  55. TextColor = Color.Black;
  56. }
  57. /// <summary>
  58. /// Call this to automatically pick the width for the text control based on the length of the text
  59. /// </summary>
  60. public void AutoPickWidth()
  61. {
  62. Vector2 dim = Font.MeasureString(Text);
  63. Width = SPACE * 2 + (int)dim.X;
  64. }
  65. /// <summary>
  66. /// Loads the content for this control
  67. /// </summary>
  68. public override void LoadContent(GraphicsDevice _graphics, ContentManager _content)
  69. {
  70. base.LoadContent(_graphics, _content);
  71. if (!string.IsNullOrEmpty(FontName))
  72. {
  73. Font = _content.Load<SpriteFont>(FontName);
  74. }
  75. }
  76. #endregion
  77. }
  78. }