SDK/MrAG/Gui/ListBox.cs

254 lines
8.7 KiB
C#

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 ListBox : Base {
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 Action OnSelectionChange;
public string _selectedtext = "";
public int _selindex = -1;
public string SelectedText {
get { return this._selectedtext; }
set{
for (int i = 0; i < this.Items.Count; i++) {
if (this.Items[i].text == value) {
this.SelectedIndex = i;
return;
}
}
Vector2 s = this.Font.MeasureString(value);
ListBox_Item itm = new ListBox_Item() {
text = value,
Xsize = (int)s.X,
Ysize = (int)s.Y
};
this.Items.Insert(0, itm);
this.SelectedIndex = 0;
}
}
public int SelectedIndex {
get { return this._selindex; }
set{
if (value < 0 || value >= this.Items.Count)
return;
this._selindex = value;
this._selectedtext = this.Items[value].text;
if (this.OnSelectionChange != null)
this.OnSelectionChange.Invoke();
}
}
private List<ListBox_Item> Items = new List<ListBox_Item>();
private MrAG.Gui.SliderV Slider;
private bool toomuchcontent;
private int maxitemstoshow;
private int curshowing;
private class ListBox_Item {
public string text;
public int Ysize;
public int Xsize;
}
public ListBox() {
this.Slider = new SliderV() {
Render = false,
Parent = this,
Width = 10
};
}
public void AddItem(string text) {
Vector2 s = this.Font.MeasureString(text);
ListBox_Item itm = new ListBox_Item() {
text = text,
Xsize = (int)s.X,
Ysize = (int)s.Y
};
this.Items.Add(itm);
}
public void AddItem(string text, int index) {
Vector2 s = this.Font.MeasureString(text);
ListBox_Item itm = new ListBox_Item() {
text = text,
Xsize = (int)s.X,
Ysize = (int)s.Y
};
this.Items.Insert(index, itm);
if (this.SelectedIndex > index - 1)
this.SelectedIndex++;
}
public bool RemoveItem(string text) {
for (int i = 0; i < this.Items.Count; i++) {
if (this.Items[i].text == text) {
this.Items.RemoveAt(i);
return true;
}
}
return false;
}
public bool RemoveItem(int index) {
if (this.Items.Count >= index && index >= 0) {
this.Items.RemoveAt(index);
return true;
}
return false;
}
public List<string> GetItems() {
List<string> tmp = new List<string>();
for (int i = 0; i < this.Items.Count; i++)
tmp.Add(this.Items[i].text);
return tmp;
}
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) {
int addy = this.Padding;
int slideradd = this.Padding;
for (int i = 0; i < this.Slider.Value; i++) {
slideradd += this.Items[i].Ysize;
slideradd += this.Spacing;
}
for (int i = this.Slider.Value; i < this.Items.Count; i++) {
int tsize = this.Items[i].Ysize;
if (addy + tsize > this.Height)
break;
int newy = addy;
newy += this.Spacing;
newy += tsize;
if (y >= addy && y <= newy) {
this.SelectedIndex = i;
break;
}
addy =+ newy;
}
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 addy = this.Padding;
int slideradd = this.Padding;
for (int i = 0; i < this.Slider.Value; i++) {
slideradd += this.Items[i].Ysize;
slideradd += this.Spacing;
}
this.maxitemstoshow = 0;
this.toomuchcontent = this.Slider.Value > 0;
for (int i = this.Slider.Value; i < this.Items.Count; i++) {
int tsize = this.Items[i].Ysize;
if (addy + tsize > this.Height) {
this.toomuchcontent = true;
break;
}
this.maxitemstoshow++;
if (i == this.SelectedIndex) {
MrAG.Draw.Box(this.X + this.Padding, this.Y + addy, this.Width - (this.Padding * 2), tsize, this.SelectedBackColor);
}
MrAG.Draw.Text(this.Items[i].text, this.X + this.Padding, this.Y + addy, i == this.SelectedIndex? this.SelectedTextColor : this.TextColor);
addy += this.Spacing;
addy += tsize;
if (ShowLines && i + 1 < this.Items.Count && addy + this.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);
}
}
this.curshowing = this.maxitemstoshow;
this.maxitemstoshow = this.Items.Count - this.maxitemstoshow;
base.Draw();
}
}
}
}