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 ListView : Base { private class ListView_Colom { public string Text; public int Width; public List Items = new List(); public MrAG.Draw.TextAlignmentX AlignX; public MrAG.Draw.TextAlignmentY AlignY; } private class ListView_Colom_Item { public string text; public int Ysize; public int Xsize; } public Color BackColomColor = Color.Black; public Color TextColomColor = Color.White; public Color BorderColor = Color.Gray; public Color BackColor = Color.DarkGray; public Color TextColor = Color.Blue; public Color SelectedTextColor = Color.Black; public Color SelectedBackColor = Color.LightGray; public Color LineColor = Color.Red; public int BorderSize; public int Spacing = 2; public int Padding = 4; public int LineSize = 2; public bool ShowLines = true; public SpriteFont Font = MrAG.Draw.Font; public bool AutoScroll; public Action OnSelectionChange; public Action OnDoubleClickSelected; public string[] SelectedRow = new string[]{}; public int _selindex = -1; private double lastclicked = 0; public int SelectedIndex { get { return this._selindex; } set{ if (this.Coloms.Count == 0) return; if (value >= this.Coloms[0].Items.Count) return; if (value < 0) { this._selindex = -1; this.SelectedRow = new string[] { }; return; } this._selindex = value; List itms = new List(); foreach (ListView_Colom c in this.Coloms) { itms.Add(c.Items[value].text); } this.SelectedRow = itms.ToArray(); if (this.OnSelectionChange != null) this.OnSelectionChange.Invoke(); } } private List Coloms = new List(); private MrAG.Gui.SliderV Slider; private bool toomuchcontent; private int maxitemstoshow; private int curshowing; public ListView() { this.Slider = new SliderV() { Render = false, Parent = this, Width = 10 }; } public void AddItem(params string[] text) { if (text.Length == this.Coloms.Count) { for (int i = 0; i < this.Coloms.Count; i++ ) { Vector2 s = this.Font.MeasureString(text[i]); ListView_Colom_Item itm = new ListView_Colom_Item() { text = text[i], Xsize = (int)s.X, Ysize = (int)s.Y }; this.Coloms[i].Items.Add(itm); if (this.AutoScroll && this.Coloms[i].Items.Count - this.curshowing - 1 == this.Slider.Value && this.Slider.Render == true) { this.Slider.MaxValue++; this.Slider.Value++; this.maxitemstoshow++; } } } } public void AddItem(int index, params string[] text) { if (text.Length == this.Coloms.Count) { for (int i = 0; i < this.Coloms.Count; i++ ) { Vector2 s = this.Font.MeasureString(text[i]); ListView_Colom_Item itm = new ListView_Colom_Item() { text = text[i], Xsize = (int)s.X, Ysize = (int)s.Y }; this.Coloms[i].Items.Insert(index, itm); } } if (this.SelectedIndex > index - 1) this.SelectedIndex++; } public bool RemoveItem(int index) { if (this.Coloms.Count >= index && index >= 0) { for (int i = 0; i < this.Coloms.Count; i++ ) { this.Coloms[i].Items.RemoveAt(index); } return true; } return false; } public void AddColom(string text, int width, MrAG.Draw.TextAlignmentX alignx, MrAG.Draw.TextAlignmentY aligny) { this.Coloms.Add(new ListView_Colom() { Text = text, Width = width, AlignX = alignx, AlignY = aligny }); } public void ClearColoms() { this.Coloms.Clear(); } public void ClearItems() { for (int i = 0; i < this.Coloms.Count; i++ ) { this.Coloms[i].Items.Clear(); } } 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; base.SetSize(w, h); } 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; base.Update(); } public override void KeyPress(Key key) { switch (key.Char) { case 38: this.SelectedIndex--; this.Slider.Value--; break; case 40: this.SelectedIndex++; this.Slider.Value++; break; } base.KeyPress(key); } public override void LeftMouseClick(int x, int y) { if (this.Coloms.Count == 0) return; int addy = this.Padding + 20; for (int i = this.Slider.Value; i < this.Coloms[0].Items.Count; i++) { int tsize = this.Coloms[0].Items[i].Ysize; if (addy + tsize > this.Height) break; int newy = addy; newy += tsize + this.Spacing; if (y >= addy && y <= newy) { bool dodubbleclickthingy = false; if (this.SelectedIndex == i && DateTime.Now.TimeOfDay.TotalMilliseconds - this.lastclicked < 300 && this.OnDoubleClickSelected != null) dodubbleclickthingy = true; this.SelectedIndex = i; if (dodubbleclickthingy) this.OnDoubleClickSelected.Invoke(); this.lastclicked = DateTime.Now.TimeOfDay.TotalMilliseconds; return; } addy =+ newy; } this.lastclicked = DateTime.Now.TimeOfDay.TotalMilliseconds; this.SelectedIndex = -1; base.LeftMouseClick(x, y); } 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.BackColor); MrAG.Draw.OutlinedBox(this.X, this.Y, this.Width, this.Height, this.BorderSize, this.BorderColor); int addx = 0; this.maxitemstoshow = 0; this.toomuchcontent = this.Slider.Value > 0; bool didsliderstuff = false; for (int colomi = 0; colomi < this.Coloms.Count; colomi++) { if (colomi + 1 == this.Coloms.Count) { this.Coloms[colomi].Width = this.Width - addx; } if (ShowLines && colomi != 0 && addx + this.Coloms[colomi].Width <= this.Width) { MrAG.Draw.Box(this.X + addx, this.Y + this.Padding, this.LineSize, this.Height - (this.Padding * 2), this.LineColor); } MrAG.Draw.Box(this.X + addx, this.Y, this.Coloms[colomi].Width, 20, this.BackColomColor); switch (this.Coloms[colomi].AlignX) { case MrAG.Draw.TextAlignmentX.Center: MrAG.Draw.Text(this.Coloms[colomi].Text, this.X + addx + this.Padding + (this.Coloms[colomi].Width / 2), this.Y + this.Padding, this.TextColomColor, this.Coloms[colomi].AlignX, this.Coloms[colomi].AlignY); break; case MrAG.Draw.TextAlignmentX.Right: MrAG.Draw.Text(this.Coloms[colomi].Text, this.X + addx + this.Padding + this.Coloms[colomi].Width, this.Y + this.Padding, this.TextColomColor, this.Coloms[colomi].AlignX, this.Coloms[colomi].AlignY); break; case MrAG.Draw.TextAlignmentX.Left: MrAG.Draw.Text(this.Coloms[colomi].Text, this.X + addx + this.Padding, this.Y + this.Padding, this.TextColomColor, this.Coloms[colomi].AlignX, this.Coloms[colomi].AlignY); break; } int addy = this.Padding + 20; for (int i = this.Slider.Value; i < this.Coloms[0].Items.Count; i++) { int tsize = this.Coloms[0].Items[i].Ysize; if (addy + tsize > this.Height) { this.toomuchcontent = true; break; } if (!didsliderstuff) this.maxitemstoshow++; if (i == this.SelectedIndex && colomi == 0) { MrAG.Draw.Box(this.X + this.Padding, this.Y + addy, this.Width - (this.Padding * 2), tsize, this.SelectedBackColor); } MrAG.Draw.Text(this.Coloms[colomi].Items[i].text, this.X + addx + this.LineSize + this.Padding, this.Y + addy, i == this.SelectedIndex? this.SelectedTextColor : this.TextColor); addy += tsize + this.Spacing; if (ShowLines && colomi == 0 && i + 1 < this.Coloms[colomi].Items.Count && addy + this.Coloms[colomi].Items[i + 1].Ysize <= this.Height) { MrAG.Draw.Box(this.X + this.Padding, this.Y + addy - (this.Spacing / 2) - (this.LineSize / 2), this.Width - (this.Padding * 2), this.LineSize, this.LineColor); } } if (!didsliderstuff) { didsliderstuff = true; this.curshowing = this.maxitemstoshow; this.maxitemstoshow = this.Coloms[colomi].Items.Count - this.maxitemstoshow; } addx += this.Coloms[colomi].Width; } base.Draw(); } } } }