SDK/MrAG/Gui/Dropdown.cs

106 lines
4.3 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 Dropdown : Base {
public SpriteFont Font = MrAG.Draw.Font;
public Color TextColor = Color.White;
public Color BorderColor = Color.LightGray;
public Color Color = Color.Black;
public Color HoverColor = Color.DarkGray;
public Color BackColor = Color.Gray;
public int BorderSize = 2;
public MrAG.Draw.TextAlignmentX AlignmentX = MrAG.Draw.TextAlignmentX.Center;
private bool IsOpen;
private int OldHeight = 0;
public List<string> Items = new List<string>();
public string Text = "";
public Action OnSelectedChange;
public override void DoClick(int x, int y, MouseKey mouseKey) {
if (!this.IsOpen)
this.Open();
else{
for (int i = 0; i < this.Items.Count; i++ ){
if (y > this.OldHeight + (i * 20) && y < this.OldHeight + (i * 20) + 20){
this.Text = this.Items[i];
if (this.OnSelectedChange != null)
this.OnSelectedChange.Invoke();
break;
}
}
this.Close();
}
base.DoClick(x, y, mouseKey);
}
public override void Update() {
if (this.IsOpen && MrAG.Gui.CurrentFocus != this)
this.Close();
}
public void Open(){
this.SetFocus();
this.IsOpen = true;
this.OldHeight = this.Height;
this.Height += this.Items.Count * 20;
}
public void Close(){
this.IsOpen = false;
this.Height = this.OldHeight;
}
public void AddItem(string text){
this.Items.Add(text);
}
public override void Draw() {
MrAG.Draw.Box(this.X + this.BorderSize, this.Y + this.BorderSize, this.Width - (this.BorderSize * 2), (this.IsOpen ? this.OldHeight : this.Height) - (this.BorderSize * 2), this.Color);
MrAG.Draw.OutlinedBox(this.X, this.Y, this.Width, this.IsOpen ? this.OldHeight : this.Height, this.BorderSize, this.BorderColor);
MrAG.Draw.SetFont(this.Font);
MrAG.Draw.Text(this.Text, this.X + (this.Width / 2), this.Y + ((this.IsOpen ? this.OldHeight : this.Height) / 2), this.TextColor, MrAG.Draw.TextAlignmentX.Center, MrAG.Draw.TextAlignmentY.Center);
if (this.IsOpen){
MrAG.Draw.Box(this.X + this.BorderSize, this.Y + this.OldHeight + this.BorderSize, this.Width - (this.BorderSize * 2), (this.Items.Count * 20) - (this.BorderSize * 2) + (this.BorderSize * 2), this.BackColor);
MrAG.Draw.OutlinedBox(this.X, this.Y + this.OldHeight, this.Width, (this.Items.Count * 20) + (this.BorderSize * 2), this.BorderSize, this.BorderColor);
int y = MrAG.Gui.CurMouseState.Y - this.Y;
int x = MrAG.Gui.CurMouseState.X;
bool shouldhighlite = x >= this.X && x <= this.X + this.Width;
for (int i = 0; i < this.Items.Count; i++ ){
int addy = this.OldHeight + (i * 20);
if (shouldhighlite && y > addy && y < addy + 20){
MrAG.Draw.Box(this.X + this.BorderSize, this.Y + addy + this.BorderSize, this.Width - (this.BorderSize * 2), 20, this.HoverColor);
}
MrAG.Draw.Text(this.Items[i], this.X + (this.Width / 2), this.Y + addy + 10 + this.BorderSize, this.TextColor, MrAG.Draw.TextAlignmentX.Center, MrAG.Draw.TextAlignmentY.Center);
}
}
base.Draw();
}
}
}
}