using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace MrAG { public partial class Gui { public class TextBox : Base { private string _Text = ""; public string Text{ get { return this._Text; } set { this.CurrentIndex = 0; this.SelectionLength = 0; this.SelectionStart = 0; if (this.MaxLength > 0 && value.Length > this.MaxLength) this._Text = value.Substring(0, this.MaxLength); else this._Text = value; } } public SpriteFont Font = MrAG.Draw.Font; public Color TextColor = Color.Black; public Color BorderColor = Color.DarkGray; public Color Color = Color.White; public Color HoverColor = Color.DarkGray; public Color ActiveColor = Color.Gray; public Color FocusColor = Color.GreenYellow; public int BorderSize; public MrAG.Draw.TextAlignmentX AlignmentX = MrAG.Draw.TextAlignmentX.Left; public MrAG.Draw.TextAlignmentY AlignmentY = MrAG.Draw.TextAlignmentY.Top; public int CurrentIndex; public int SelectionStart; public int SelectionLength; public int MaxLength; public Action OnEnter; public Action OnTextChange; public char SecretChar; private bool hover; private bool active; private bool oldCursor; private int selxstart; public TextBox() { this.BorderSize = 2; this.Height = (int)MrAG.Draw.Font.MeasureString("W").Y + 4; } public override void Update() { this.hover = this.Hovering(); this.active = this.IsActive(); if (this.hover) { if (!oldCursor) { oldCursor = true; MrAG.Gui.MainForm.Cursor = System.Windows.Forms.Cursors.IBeam; } } else { if (oldCursor) { MrAG.Gui.MainForm.Cursor = System.Windows.Forms.Cursors.Default; oldCursor = false; } } base.Update(); } public override void Draw() { string oldtext = this._Text; if (this.SecretChar != '\0') { StringBuilder sb = new StringBuilder(); for (int i = 0; i < this._Text.Length; i++) sb.Append(this.SecretChar); this._Text = sb.ToString(); } MrAG.Draw.Box(this.X + this.BorderSize, this.Y + this.BorderSize, this.Width - (this.BorderSize * 2), this.Height - (this.BorderSize * 2), this.hover ? (this.active ? this.ActiveColor : this.HoverColor) : this.HasFocus ? this.FocusColor : this.Color); MrAG.Draw.OutlinedBox(this.X, this.Y, this.Width, this.Height, this.BorderSize, this.BorderColor); if (this.SelectionLength > 0) { int addS = this.BorderSize + (int)this.Font.MeasureString(this._Text.Substring(0, this.SelectionStart)).X; MrAG.Draw.Box(this.X + addS + 3, this.Y + this.BorderSize, (int)Font.MeasureString(this._Text.Substring(this.SelectionStart, this.SelectionLength)).X, this.Height - (this.BorderSize * 2), new Color(70, 119, 207, 150)); } MrAG.Draw.SetFont(this.Font); MrAG.Draw.Text(this._Text, this.X + this.BorderSize + 3, this.Y + this.BorderSize, this.TextColor, this.AlignmentX, this.AlignmentY, 0); if (this.HasFocus){ int add = this.BorderSize + (int)this.Font.MeasureString(this._Text.Substring(0, this.CurrentIndex)).X; MrAG.Draw.Line(this.X + add + 4, this.Y + (this.BorderSize * 2), this.X + add + 2, this.Y + this.Height - (this.BorderSize * 2), 1, Color.Black); } this._Text = oldtext; base.Draw(); } public override void KeyPress(Key key) { if (!this.Render) return; switch (key.Char) { case 40: if (key.Shift) { this.SelectionStart = this.CurrentIndex; this.SelectionLength = this._Text.Length - this.CurrentIndex; } this.CurrentIndex = this._Text.Length; break; case 39: if (this._Text.Length > this.CurrentIndex) { this.CurrentIndex++; if (key.Shift) { if ((this.SelectionStart + this.SelectionLength) - this.CurrentIndex <= 0) if (this.SelectionLength + this.SelectionStart < this._Text.Length) this.SelectionLength++; } } return; case 38: if (key.Shift) { this.SelectionStart = 0; this.SelectionLength = this.CurrentIndex; } this.CurrentIndex = 0; break; case 37: if (this.CurrentIndex > 0) { this.CurrentIndex--; if (this.SelectionStart - this.CurrentIndex >= -1) { if (key.Shift) { if (this.SelectionLength + this.SelectionStart < this._Text.Length - 1) this.SelectionLength++; } this.SelectionStart = this.CurrentIndex; } } return; case 36: if (key.Shift) { this.SelectionStart = 0; this.SelectionLength = this.CurrentIndex; } this.CurrentIndex = 0; break; case 35: if (key.Shift) { this.SelectionStart = this.CurrentIndex; this.SelectionLength = this._Text.Length - this.CurrentIndex; } this.CurrentIndex = this._Text.Length; break; case 8: if (this.SelectionLength == 0){ if (this.CurrentIndex > 0){ this._Text = this._Text.Remove(this.CurrentIndex - 1, this._Text.Length > 0 ? 1 : 0); this.CurrentIndex--; } }else{ this._Text = this._Text.Remove(this.SelectionStart, this.SelectionLength); if (this.CurrentIndex >= this._Text.Length) this.CurrentIndex = this._Text.Length; } break; case 13: if (this.OnEnter != null) this.OnEnter.Invoke(); break; default: if (key.Ctrl && System.Threading.Thread.CurrentThread.GetApartmentState() == System.Threading.ApartmentState.STA) { switch (key.Char) { case 65: this.CurrentIndex = 0; this.SelectionStart = 0; this.SelectionLength = this._Text.Length; return; case 67: if (this.SelectionLength > 0) System.Windows.Forms.Clipboard.SetText(this._Text.Substring(this.SelectionStart, this.SelectionLength)); break; case 86: if (this.SelectionLength > 0){ this._Text = this._Text.Remove(this.SelectionStart, this.SelectionLength); this.CurrentIndex = this.SelectionStart; } string t = System.Windows.Forms.Clipboard.GetText().Trim('\n', '\r', '\t'); this._Text = _Text.Insert(this.CurrentIndex, t); this.CurrentIndex += t.Length; if (this.OnTextChange != null) this.OnTextChange(); break; case 88: if (this.SelectionLength > 0) { System.Windows.Forms.Clipboard.SetText(this._Text.Substring(this.SelectionStart, this.SelectionLength)); this._Text = this._Text.Remove(this.SelectionStart, this.SelectionLength); this.CurrentIndex = this.SelectionStart; if (this.OnTextChange != null) this.OnTextChange(); } break; } } else if(key.Letter.Length > 0 && key.Letter != "\n") { if (this.SelectionLength > 0){ this._Text = this._Text.Remove(this.SelectionStart, this.SelectionLength); this.CurrentIndex = this.SelectionStart; if (this.OnTextChange != null) this.OnTextChange(); } if (this.MaxLength == 0 || this._Text.Length + key.Letter.Length < this.MaxLength) { string oldtext = this._Text; this._Text = this._Text.Insert(this.CurrentIndex, key.Letter); if (this.SecretChar != '\0') { StringBuilder sb = new StringBuilder(); for (int i = 0; i < this._Text.Length; i++) sb.Append(this.SecretChar); if (this.Font.MeasureString(sb.ToString()).X > this.Width - (this.BorderSize * 2) - 3) { this._Text = oldtext; } else this.CurrentIndex += key.Letter.Length; } else { if (this.Font.MeasureString(this._Text).X > this.Width - (this.BorderSize * 2) - 3) { this._Text = oldtext; }else this.CurrentIndex += key.Letter.Length; } if (this.OnTextChange != null) this.OnTextChange(); } } break; } if (key.Letter.Length > 0 && key.Letter != "\n") { this.SelectionStart = this.CurrentIndex; this.SelectionLength = 0; } base.KeyPress(key); } public override void SetSize(int w, int h) { base.SetSize(w, h); } public override void LeftMousePress(int x, int y) { this.SelectionStart = 0; this.SelectionLength = 0; selxstart = x; x += this.BorderSize; bool found = false; float curx = 0; for (int i = 0; i < this._Text.Length; i++){ if (x <= curx){ this.CurrentIndex = i - 1; found = true; break; } curx += this.SecretChar != '\0' ? this.Font.MeasureString(this.SecretChar.ToString()).X : this.Font.MeasureString(this._Text[i].ToString()).X; } if (!found && x <= curx){ this.CurrentIndex = this._Text.Length - 1; found = true; } if (!found){ this.CurrentIndex = this._Text.Length; } if (this.CurrentIndex < 0) this.CurrentIndex = 0; this.SelectionStart = this.CurrentIndex; } public override void LeftMouseRelease(int x, int y) { if (Math.Abs(selxstart - x) < 4) return; x += this.BorderSize; bool found = false; float curx = 0; for (int i = 0; i < this._Text.Length; i++){ if (x <= curx){ this.SelectionLength = i - this.SelectionStart; found = true; break; } curx += this.SecretChar != '\0' ? this.Font.MeasureString(this.SecretChar.ToString()).X : this.Font.MeasureString(this._Text[i].ToString()).X; } if (!found && x <= curx){ this.SelectionLength = this._Text.Length - this.SelectionStart; found = true; } if (!found){ this.SelectionLength = this._Text.Length - this.SelectionStart; } if (this.SelectionLength < 0) { this.SelectionStart += this.SelectionLength; this.SelectionLength = -this.SelectionLength; } } /* public override void OnClick(int x, int y, MouseKey mk){ x += this.BorderSize; bool found = false; float curx = 0; for (int i = 0; i < this._Text.Length; i++){ if (x <= curx){ this.CurrentIndex = i - 1; found = true; break; } curx += this.Font.MeasureString(this._Text[i].ToString()).X; } if (!found && x <= curx){ this.CurrentIndex = this._Text.Length - 1; found = true; } if (!found){ this.CurrentIndex = this._Text.Length; } if (this.CurrentIndex < 0) this.CurrentIndex = 0; }*/ } } }