1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Input;
  7. using Microsoft.Xna.Framework.Graphics;
  8.  
  9. namespace NameSpace
  10. {
  11. class TextInput
  12. {
  13. public Rectangle TextLoc;
  14. public string Text;
  15. KeyboardState OldState;
  16. public bool Active = false;
  17. private SpriteFont Font;
  18. private Texture2D BackImage;
  19. public Color[] Colour = new Color[2];
  20. int MaxCharacters;
  21.  
  22. public TextInput(string Default, Rectangle DrawTo, SpriteFont _Font, Texture2D BackGround, Color Active_Colour, Color Static_Colour)
  23. {
  24. Text = Default; TextLoc = DrawTo; Font = _Font; BackImage = BackGround; Colour[0] = Active_Colour; Colour[1] = Static_Colour;
  25. OldState = new KeyboardState(); MaxCharacters = -1;
  26. }
  27.  
  28. public int Max_Characters
  29. {set { MaxCharacters = value; }}
  30. public int Remaining_Characters
  31. {
  32. get
  33. {
  34. if (MaxCharacters > 0) { return MaxCharacters - Text.Length; }
  35. return 0;
  36. }
  37. }
  38.  
  39. public void Draw(SpriteBatch SB)
  40. {
  41. int YPos = (int)((TextLoc.Y + (TextLoc.Height / 2)) - (Font.MeasureString("|").Y / 2));
  42. int XPos = (int)((TextLoc.X + (TextLoc.Width / 2)) - (Font.MeasureString(Text).X / 2));
  43. if (Active) { SB.Draw(BackImage, TextLoc, Colour[0]); }
  44. else { SB.Draw(BackImage, TextLoc, Colour[1]); }
  45. SB.DrawString(Font, Text, new Vector2(XPos, YPos), Color.Black);
  46. }
  47.  
  48. public void Update()
  49. {
  50. if (Active)
  51. {
  52. KeyboardState NewState = Keyboard.GetState();
  53. Keys[] PressedKeys = NewState.GetPressedKeys();
  54. foreach (Keys key in PressedKeys)
  55. {
  56. if (OldState.IsKeyUp(key))
  57. {
  58. //Backspace
  59. if (key == Keys.Back) { if (Text.Length > 0) { Text = Text.Remove(Text.Length - 1, 1); } }
  60. //Space
  61. else if (key == Keys.Space) { if (Text.Length > 0) { Text = Text + " "; } }
  62. //Confirm the textbox
  63. else if (key == Keys.Enter) { Active = false; }
  64. //Numbers
  65. else if (key == Keys.D0 || key == Keys.D1 || key == Keys.D2 || key == Keys.D3 || key == Keys.D4 || key == Keys.D5 || key == Keys.D6 || key == Keys.D7 || key == Keys.D8 || key == Keys.D9)
  66. { string DUD = key.ToString(); Text += DUD.Remove(0, 1); }
  67. //Letters
  68. else if (key == Keys.A || key == Keys.B || key == Keys.C || key == Keys.D || key == Keys.E || key == Keys.F || key == Keys.G || key == Keys.H || key == Keys.I || key == Keys.J || key == Keys.K || key == Keys.L || key == Keys.M || key == Keys.N || key == Keys.O || key == Keys.P || key == Keys.Q || key == Keys.R || key == Keys.S || key == Keys.T || key == Keys.U || key == Keys.V || key == Keys.W || key == Keys.X || key == Keys.Y || key == Keys.Z)
  69. { try { Text += key.ToString(); } catch { } }
  70. //The rest get ignored as their .ToString()s suck
  71. }
  72. }
  73. OldState = NewState;
  74. if (MaxCharacters > -1 && Text.Length > MaxCharacters)
  75. { Text = Text.Remove(MaxCharacters, Text.Length - MaxCharacters); }
  76. }
  77. }
  78.  
  79. }
  80. }
  81.