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 TextBoxAdvanced : Base { public List> Lines = new List>(); public SpriteFont Font = MrAG.Draw.Font; public Color TextColor = Color.Black; public Color BorderColor = Color.LightGray; public Color BackColor = Color.White; public Color HoverColor = Color.DarkGray; public Color ActiveColor = Color.Gray; public Color FocusColor = Color.GreenYellow; public int BorderSize = 2; public int CurrentX; public int CurrentY; private MrAG.Gui.SliderV Slider; private MrAG.Gui.Slider SliderH; private int maxitemstoshow; private int curshowing; private bool toomuchcontent; private bool hover; private bool active; private bool oldCursor; public TextBoxAdvanced() { this.Slider = new SliderV() { Render = false, Parent = this, Width = 10 }; this.SliderH = new Slider() { Render = false, Parent = this, Height = 10 }; this.BorderSize = 2; this.Height = (int)MrAG.Draw.Font.MeasureString("W").Y + 4; this.Lines.Add(new List(){ this.TextColor }); } public override void DoScrollUp() { this.Slider.DoScrollUp(); base.DoScrollUp(); } public override void DoScrollDown() { this.Slider.DoScrollDown(); base.DoScrollDown(); } public override void Update() { this.Slider.MaxValue = this.maxitemstoshow; this.Slider.Render = this.toomuchcontent; 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 SetSize(int w, int h) { this.Slider.Height = h - (this.BorderSize * 2); this.Slider.Y = this.BorderSize; this.Slider.X = w - 10 - this.BorderSize; this.SliderH.Width = w - (this.BorderSize * 2); this.SliderH.Y = this.Height - this.BorderSize - 10; this.SliderH.X = this.BorderSize; base.SetSize(w, h); } public override void LeftMouseClick(int x, int y) { int cury = this.BorderSize; for (int i = this.Slider.Value; i < this.Lines.Count; i++) { Color col = this.TextColor; int curx = this.X + this.BorderSize; int h = (int)this.Font.MeasureString("W").Y; int curchar = 0; cury += h; if (cury > this.Y + this.Height) { return; } if (cury - h <= y && cury >= y){ this.CurrentY = i; int len2 = 0; for (int i3 = 0; i3 < this.Lines[i].Count; i3++) if(this.Lines[i][i3].GetType() == typeof(string)) len2 += (this.Lines[i][i3] as string).Length; for (int i2 = 0; i2 < this.Lines[i].Count; i2++){ if (this.Lines[i][i2].GetType() == typeof(string)){ if ((string)this.Lines[i][i2] != ""){ string text = (string)this.Lines[i][i2]; Vector2 s = this.Font.MeasureString(text); h = (int)s.Y; if (curx <= x && curx + (int)s.X >= x) { for (int i3 = 0; i3 < text.Length; i3++) { int sx = (int)this.Font.MeasureString(text[i3].ToString()).X; if (curx <= x && curx + sx >= x) { this.CurrentX = curchar + 1 < len2 ? curchar + 1 : 0; return; } curx += sx; curchar++; } return; } curx += (int)s.X; curchar += text.Length; } } } this.CurrentX = curchar; } } base.LeftMouseClick(x, y); } public override void KeyPress(Key key) { List curline = this.Lines[this.CurrentY]; switch (key.Char) { case 40: if (this.CurrentY < this.Lines.Count - 1) { this.CurrentY++; int len2 = 0; for (int i2 = 0; i2 < this.Lines[this.CurrentY].Count; i2++) if(this.Lines[this.CurrentY][i2].GetType() == typeof(string)) len2 += (this.Lines[this.CurrentY][i2] as string).Length; if (len2 < this.CurrentX) { this.CurrentX = len2; } }else{ this.CurrentX = 0; for (int i2 = 0; i2 < this.Lines[this.CurrentY].Count; i2++) if(this.Lines[this.CurrentY][i2].GetType() == typeof(string)) this.CurrentX += (this.Lines[this.CurrentY][i2] as string).Length; } break; case 39: int len = 0; for (int i2 = 0; i2 < this.Lines[this.CurrentY].Count; i2++) if(this.Lines[this.CurrentY][i2].GetType() == typeof(string)) len += (this.Lines[this.CurrentY][i2] as string).Length; if (len > this.CurrentX) { this.CurrentX++; } else if(this.CurrentY < this.Lines.Count - 1) { this.CurrentX = 0; this.CurrentY++; } return; case 38: if (this.CurrentY > 0) { this.CurrentY--; int len2 = 0; for (int i2 = 0; i2 < this.Lines[this.CurrentY].Count; i2++) if(this.Lines[this.CurrentY][i2].GetType() == typeof(string)) len2 += (this.Lines[this.CurrentY][i2] as string).Length; if (len2 < this.CurrentX) { this.CurrentX = len2; } }else this.CurrentX = 0; break; case 37: if (this.CurrentX > 0) { this.CurrentX--; } else if(this.CurrentY > 0) { this.CurrentY--; this.CurrentX = 0; for (int i2 = 0; i2 < this.Lines[this.CurrentY].Count; i2++) if(this.Lines[this.CurrentY][i2].GetType() == typeof(string)) this.CurrentX += (this.Lines[this.CurrentY][i2] as string).Length; } return; case 36: this.CurrentX = 0; break; case 35: this.CurrentX = 0; for (int i2 = 0; i2 < this.Lines[this.CurrentY].Count; i2++) if(this.Lines[this.CurrentY][i2].GetType() == typeof(string)) this.CurrentX += (this.Lines[this.CurrentY][i2] as string).Length; break; case 13: this.CurrentX = 0; this.CurrentY++; this.Lines.Insert(this.CurrentY, new List(){ this.TextColor }); if (this.Slider.Render && this.CurrentY - this.Slider.Value >= this.curshowing) { this.Slider.MaxValue++; this.Slider.Value++; } return; case 8: int curchar = 0; for (int i = 0; i < curline.Count; i++) { Type t = curline[i].GetType(); if (t == typeof(Color)) { } else if(t == typeof(string)) { string text = (string)curline[i]; if (curchar + text.Length < this.CurrentX) curchar += text.Length; else { for (int i2 = 0; i2 < text.Length; i2++) { if (curchar == this.CurrentX) { curline[i] = text.Remove(i2, 1); this.CurrentX--; return; } curchar++; } if (curchar == this.CurrentX) { if (this.CurrentX == 0 && this.CurrentY > 0) { this.Lines.RemoveAt(this.CurrentY); this.CurrentY--; this.CurrentX = 0; curchar = 0; for (int i2 = 0; i2 < this.Lines[this.CurrentY].Count; i2++) if(this.Lines[this.CurrentY][i2].GetType() == typeof(string)) curchar += (this.Lines[this.CurrentY][i2] as string).Length; this.CurrentX = curchar; } else { curline[i] = text.Substring(0, text.Length > 0 ? text.Length - 1 : 0); this.CurrentX--; if (this.CurrentX < 0) this.CurrentX = 0; } return; } } } } if (this.CurrentX == 0 && this.CurrentY > 0) { this.Lines.RemoveAt(this.CurrentY); this.CurrentY--; this.CurrentX = 0; curchar = 0; for (int i2 = 0; i2 < this.Lines[this.CurrentY].Count; i2++) if(this.Lines[this.CurrentY][i2].GetType() == typeof(string)) curchar += (this.Lines[this.CurrentY][i2] as string).Length; this.CurrentX = curchar; return; } return; } int curlinelen = 0; for (int i2 = 0; i2 < curline.Count; i2++) if(curline[i2].GetType() == typeof(string)) curlinelen += (curline[i2] as string).Length; if (curlinelen == 0) { curline.Add(key.Letter); this.CurrentX += key.Letter.Length; } else { int curchar = 0; for (int i = 0; i < curline.Count; i++) { Type t = curline[i].GetType(); if (t == typeof(Color)) { } else if(t == typeof(string)) { string text = (string)curline[i]; if (curchar + text.Length < this.CurrentX) curchar += text.Length; else { for (int i2 = 0; i2 < text.Length; i2++) { if (curchar == this.CurrentX) { curline[i] = text.Insert(i2, key.Letter); this.CurrentX += key.Letter.Length; return; } curchar++; } if (curchar == this.CurrentX) { curline[i] = text += key.Letter; this.CurrentX += key.Letter.Length; return; } } } } } base.KeyPress(key); } public override void Draw() { 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.BackColor); MrAG.Draw.OutlinedBox(this.X, this.Y, this.Width, this.Height, this.BorderSize, this.BorderColor); MrAG.Draw.SetFont(this.Font); int cury = this.Y + this.BorderSize; this.maxitemstoshow = 0; this.toomuchcontent = this.Slider.Value > 0; for (int i = this.Slider.Value; i < this.Lines.Count; i++) { Color col = this.TextColor; int curx = this.X + this.BorderSize; int h = (int)this.Font.MeasureString("W").Y; int curtypeingX = 0; if (cury + h > this.Y + this.Height) { this.toomuchcontent = true; break; } bool typing_found = false; foreach (object obj in this.Lines[i]) { typing_found = false; Type t = obj.GetType(); if (t == typeof(Color)) { col = (Color)obj; } else if(t == typeof(string)) { string text = (string)obj; MrAG.Draw.Text(text, curx, cury, col); Vector2 s = this.Font.MeasureString(text); if ((int)s.Y > h) h = (int)s.Y; if (this.CurrentY == i){ int typing_pos = 0; if (curtypeingX + text.Length < this.CurrentX) curtypeingX += text.Length; else { for (int i2 = 0; i2 < text.Length; i2++) { if (curtypeingX == this.CurrentX) { typing_pos = curx + (int)this.Font.MeasureString(text.Substring(0, i2)).X; typing_found = true; curtypeingX++; break; } curtypeingX++; } if (!typing_found && curtypeingX == this.CurrentX) { typing_pos = curx + (int)s.X; typing_found = true; } if (typing_found) { MrAG.Draw.Line(typing_pos + 2, cury, typing_pos, cury + h, 1, Color.Black); } } } curx += (int)s.X; } } if (!typing_found && this.CurrentY == i && this.CurrentX == 0){ int curlinelen = 0; for (int i2 = 0; i2 < this.Lines[i].Count; i2++) if(this.Lines[i][i2].GetType() == typeof(string)) curlinelen += (this.Lines[i][i2] as string).Length; if (curlinelen == 0) MrAG.Draw.Line(curx + 2, cury, curx, cury + h, 1, Color.Black); } cury += h; this.maxitemstoshow++; } this.curshowing = this.maxitemstoshow; this.maxitemstoshow = this.Lines.Count - this.maxitemstoshow; base.Draw(); } } } }