Initial commit (2011)

This commit is contained in:
2020-04-28 16:33:45 +02:00
commit da459a94c9
35 changed files with 6399 additions and 0 deletions

356
MrAG/Gui/Base.cs Normal file
View File

@ -0,0 +1,356 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MrAG {
public partial class Gui {
public class Base {
#region actions
public Action OnLeftMousePress;
public Action OnLeftMouseRelease;
public Action OnLeftMouseClick;
public Action OnLeftDoubleMouseClick;
public Action OnRightMousePress;
public Action OnRightMouseRelease;
public Action OnRightMouseClick;
public Action OnRightDoubleMouseClick;
public Action OnMiddleMousePress;
public Action OnMiddleMouseRelease;
public Action OnMiddleMouseClick;
public Action OnMiddleDoubleMouseClick;
public Action OnScrollUp;
public Action OnScrollDown;
public Action OnKeyPress;
public Action OnFocus;
public Action OnUnFocus;
public Action OnMove;
public Action OnResize;
public Action OnParent;
public Action OnRemove;
public Action OnUpdate;
public Action OnDraw;
public Action<object> OnArgumentedLeftMousePress;
public Action<object> OnArgumentedLeftMouseRelease;
public Action<object> OnArgumentedLeftMouseClick;
public Action<object> OnArgumentedLeftDoubleMouseClick;
public Action<object> OnArgumentedRightMousePress;
public Action<object> OnArgumentedRightMouseRelease;
public Action<object> OnArgumentedRightMouseClick;
public Action<object> OnArgumentedRightDoubleMouseClick;
public Action<object> OnArgumentedMiddleMousePress;
public Action<object> OnArgumentedMiddleMouseRelease;
public Action<object> OnArgumentedMiddleMouseClick;
public Action<object> OnArgumentedMiddleDoubleMouseClick;
public Action<object> OnArgumentedScrollUp;
public Action<object> OnArgumentedScrollDown;
public Action<object> OnArgumentedKeyPress;
public Action<object> OnArgumentedFocus;
public Action<object> OnArgumentedUnFocus;
public Action<object> OnArgumentedMove;
public Action<object> OnArgumentedResize;
public Action<object> OnArgumentedParent;
public Action<object> OnArgumentedRemove;
public Action<object> OnArgumentedUpdate;
public Action<object> OnArgumentedDraw;
#endregion
public List<Gui.Base> Children = new List<Base>();
public object Tag;
public bool Removed;
public bool Enabled = true;
public bool HasFocus{
get{ return MrAG.Gui.CurrentFocus == this; }
set{
if (value){
this.SetFocus();
}else{
if (this.HasFocus){
MrAG.Gui.CurrentFocus = null;
}
}
}
}
private int ParrentOffsetX;
private int ParrentOffsetY;
#region Interal Get/Set
private int _X;
private int _Y;
private int _Width;
private int _Height;
private Gui.Base _parent;
private bool _render = true;
public bool Render{
get { return this._render; }
set{
this._render = value;
foreach (MrAG.Gui.Base pnl in this.Children)
pnl.Render = this._render;
}
}
public int X{
get{return this._X;}
set{this.SetX(value);}
}
public int Y{
get{return this._Y;}
set{this.SetY(value);}
}
public int Width{
get{return this._Width;}
set{this.SetWidth(value);}
}
public int Height{
get{return this._Height;}
set{this.SetHeight(value);}
}
public Gui.Base Parent{
set{
this.SetParent(value);
}
get{
return this._parent;
}
}
#endregion
public Base() {
MrAG.Gui.AddObject(this);
}
public virtual void SetPos(int x, int y) {
if (this.Removed) return;
if (this.Parent != null) {
this.ParrentOffsetX = x;
this.ParrentOffsetY = y;
x += this.Parent.X;
y += this.Parent.Y;
}
this._X = x;
this._Y = y;
if (this.OnMove != null)
this.OnMove.Invoke();
for (int i = 0; i < this.Children.Count; i++) {
this.Children[i].SetPos(this.Children[i].ParrentOffsetX, this.Children[i].ParrentOffsetY);
}
}
public virtual void SetSize(int w, int h) {
if (this.Removed) return;
this._Width = w;
this._Height = h;
if (this.OnResize != null)
this.OnResize.Invoke();
}
public virtual void Center() {
if (this.Removed) return;
if (this.Parent != null){
this.X = (this.Parent.Width / 2) - (this.Width / 2);
this.Y = (this.Parent.Height / 2) - (this.Height / 2);
}else{
this.X = (MrAG.Gui.MainForm.Width / 2) - (this.Width / 2);
this.Y = (MrAG.Gui.MainForm.Height / 2) - (this.Height / 2);
}
}
public virtual void CenterX() {
if (this.Removed) return;
if (this.Parent != null){
this.X = (this.Parent.Width / 2) - (this.Width / 2);
}else{
this.X = (MrAG.Gui.MainForm.Width / 2) - (this.Width / 2);
}
}
public virtual void CenterY() {
if (this.Removed) return;
if (this.Parent != null){
this.Y = (this.Parent.Height / 2) - (this.Height / 2);
}else{
this.Y = (MrAG.Gui.MainForm.Height / 2) - (this.Height / 2);
}
}
public virtual void SetParent(Gui.Base obj) {
if (this.Removed) return;
if (this._parent == obj)
return;
if (this._parent != null) {
this._parent.Children.Remove(this);
this._parent = null;
} else {
MrAG.Gui.RemoveObject(this);
}
this.Removed = false;
this._parent = obj;
this._parent.Children.Add(this);
if (this.OnParent != null)
this.OnParent.Invoke();
}
public virtual void Remove() {
this.Removed = true;
MrAG.Gui.RemoveObject(this);
if (this.OnRemove != null)
this.OnRemove.Invoke();
}
public virtual void BringToFront() {
if (this.Removed) return;
if (this.Parent != null) {
this.Parent.BringToFront();
this.Parent.Children.Remove(this);
this.Parent.Children.Add(this);
} else {
MrAG.Gui.RemoveObject(this);
MrAG.Gui.AddObject(this);
}
MrAG.Gui.BroughtToFront = true;
}
public virtual bool Hovering() {
if (this.Removed) return false;
int x = MrAG.Gui.CurMouseState.X;
int y = MrAG.Gui.CurMouseState.Y;
return x >= this.X && x <= this.X + this.Width && y >= this.Y && y <= this.Y + this.Height;
}
//TODO: Recode this...
public virtual bool IsActive()
{
if (this.Removed) return false;
return Hovering() && Microsoft.Xna.Framework.Input.Mouse.GetState().LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed;
}
public virtual void Update(){
if (this.Removed) return;
if (this.Render)
{
bool preftofront = MrAG.Gui.BroughtToFront;
MrAG.Gui.BroughtToFront = false;
for (int i = 0; i < this.Children.Count; i++){
this.Children[i].Update();
if (BroughtToFront){
MrAG.Gui.BroughtToFront = false;
i--;
}
}
MrAG.Gui.BroughtToFront = preftofront;
if (this.OnUpdate != null)
this.OnUpdate.Invoke();
}
}
public virtual void Draw(){
if (this.Removed) return;
if (this.Render)
{
bool preftofront = MrAG.Gui.BroughtToFront;
MrAG.Gui.BroughtToFront = false;
for (int i = 0; i < this.Children.Count; i++){
if (this.Children[i].Render)
this.Children[i].Draw();
if (BroughtToFront){
MrAG.Gui.BroughtToFront = false;
i--;
}
}
MrAG.Gui.BroughtToFront = preftofront;
if (this.OnDraw != null)
this.OnDraw.Invoke();
}
}
public virtual void ClearChildren(){
if (this.Removed) return;
for (int i = 0;i < this.Children.Count; i++){
this.Children[i].Remove();
i--;
}
}
public virtual void LeftMousePress(int x, int y) {}
public virtual void RightMousePress(int x, int y) {}
public virtual void MiddleMousePress(int x, int y) {}
public virtual void LeftMouseRelease(int x, int y) {}
public virtual void RightMouseRelease(int x, int y) {}
public virtual void MiddleMouseRelease(int x, int y) {}
public virtual void LeftMouseClick(int x, int y) {}
public virtual void RightMouseClick(int x, int y) {}
public virtual void MiddleMouseClick(int x, int y) {}
public virtual void LeftDoubleMouseClick(int x, int y) { this.LeftMouseClick(x, y); }
public virtual void RightDoubleMouseClick(int x, int y) { this.RightMouseClick(x, y); }
public virtual void MiddleDoubleMouseClick(int x, int y) { this.MiddleMouseClick(x, y); }
public virtual void DoClick(int x, int y, MouseKey mouseKey) {}
public virtual void DoDoubleClick(int x, int y, MouseKey mouseKey) {}
public virtual void FocusGained() {}
public virtual void FocusLost() {}
public virtual void DoScrollUp() {}
public virtual void DoScrollDown() {}
public virtual void SetX(int x) { this.SetPos(x, this.Parent != null ? this.ParrentOffsetY : this.Y); }
public virtual void SetY(int y) { this.SetPos(this.Parent != null ? this.ParrentOffsetX : this.X, y); }
public virtual void SetWidth(int w){ this.SetSize(w, this.Height); }
public virtual void SetHeight(int h){ this.SetSize(this.Width, h); }
public virtual void SetFocus() { MrAG.Gui.CurrentFocus = this; }
public virtual void KeyPress(MrAG.Gui.Key key) {}
}
}
}

67
MrAG/Gui/Button.cs Normal file
View File

@ -0,0 +1,67 @@
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 Button : Base {
public string Text {
get {
return this._text;
}
set{
this._text = value;
if (this.AutoResize) {
this.Width = (int)this.Font.MeasureString(this._text).X;
this.Height = (int)this.Font.MeasureString(this._text).Y;
}
}
}
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 ActiveColor = Color.Gray;
public int BorderSize = 2;
public MrAG.Draw.TextAlignmentX AlignmentX = MrAG.Draw.TextAlignmentX.Center;
public MrAG.Draw.TextAlignmentY AlignmentY = MrAG.Draw.TextAlignmentY.Center;
public float Rotation = 0;
public bool AutoResize = false;
private bool hover;
private bool active;
private string _text = "";
public override void Update() {
this.hover = this.Hovering();
this.active = this.IsActive();
base.Update();
}
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.Color);
MrAG.Draw.OutlinedBox(this.X, this.Y, this.Width, this.Height, this.BorderSize, this.BorderColor);
MrAG.Draw.SetFont(this.Font);
MrAG.Draw.Text(this.Text, this.X + (this.Width / 2), this.Y + (this.Height / 2), this.TextColor, this.AlignmentX, this.AlignmentY, this.Rotation);
base.Draw();
}
}
}
}

42
MrAG/Gui/Checkbox.cs Normal file
View File

@ -0,0 +1,42 @@
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 CheckBox : Base
{
public bool Checked;
public Color CheckBorderColor = Color.Gray;
public Color CheckBackgroundColor = Color.Black;
public Color CheckCheckedColor = Color.DarkGray;
public int BorderSize = 2;
public Action OnCheckedChange;
public override void DoClick(int x, int y, MouseKey mouseKey)
{
this.Checked = !this.Checked;
if (this.OnCheckedChange != null)
this.OnCheckedChange.Invoke();
base.DoClick(x, y, mouseKey);
}
public override void Draw()
{
MrAG.Draw.Box(this.X + this.BorderSize, this.Y + this.BorderSize, this.Width - this.BorderSize - this.BorderSize, this.Height - this.BorderSize - this.BorderSize, this.Checked ? this.CheckCheckedColor : this.CheckBackgroundColor);
MrAG.Draw.OutlinedBox(this.X, this.Y, this.Width, this.Height, this.BorderSize, this.CheckBorderColor);
base.Draw();
}
}
}
}

212
MrAG/Gui/Dialog.cs Normal file
View File

@ -0,0 +1,212 @@
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 Dialog {
string Title;
int AddY = 62;
bool DoingHalf;
bool FullSize;
public Frame Frame;
public List<Button> Buttons = new List<Button>();
public List<Label> Labels = new List<Label>();
public List<TextBox> Textboxes = new List<TextBox>();
private bool FistInputkak = true;
public Dialog(string title, string text){
this.Title = title;
int tw = 0;
List<string> lines = new List<string>();
foreach (string line in text.Split('\n')) {
string curline = "";
for (int i = 0; i < line.Length; i++) {
Vector2 TSize = MrAG.Draw.GetTextSize(curline + line[i]);
if (TSize.X > 380) {
lines.Add(curline);
curline = "";
this.FullSize = true;
} else {
curline += line[i];
}
}
if (curline.Length > 0) {
lines.Add(curline);
tw = (int)MrAG.Draw.GetTextSize(curline).X;
}
}
this.Frame = new Frame();
this.Frame.Text = title;
this.Frame.SetSize(500, 62 + (lines.Count * 20));
this.Frame.Center();
this.Frame.ContentBackColor = new Color(20, 20, 20, 255);
GroupBox gb = new GroupBox();
gb.SetParent(this.Frame);
gb.SetSize((this.FullSize ? 380 : tw) + 20, (lines.Count * 20) + 12);
gb.SetPos(240 - ((this.FullSize ? 380 : tw) / 2), 17);
gb.DrawLines = false;
gb.BackColor = new Color(39, 155, 255, 100);
for (int i = 0; i < lines.Count; i++){
Label l = new Label();
l.Text = lines[i];
l.AlignmentX = MrAG.Draw.TextAlignmentX.Center;
l.SetParent(this.Frame);
l.SetPos(250, 23 + (i * 20));
l.Color = new Color(255, 255, 255, 255);
}
}
public Button AddButton(string text, Action onclick) {
this.DoingHalf = false;
Button btn = new Button();
btn.SetParent(this.Frame);
btn.SetSize(400, 25);
btn.SetPos(50, AddY);
btn.Text = text;
btn.TextColor = Color.White;
if (onclick == null)
btn.OnLeftMouseClick = new Action(delegate { this.Frame.Remove(); });
else
btn.OnLeftMouseClick = onclick;
AddY += 30;
this.Frame.SetSize(500, AddY);
this.Frame.Center();
this.Buttons.Add(btn);
return btn;
}
public Button AddHalfButton(string text, Action onclick) {
Button btn = new Button();
btn.SetParent(this.Frame);
btn.SetSize(195, 25);
btn.SetPos(this.DoingHalf ? 255 : 50, this.DoingHalf ? this.AddY - 30 : this.AddY);
btn.Text = text;
btn.TextColor = Color.White;
this.DoingHalf = !this.DoingHalf;
if (this.DoingHalf) {
this.AddY += 30;
}
if (onclick == null)
btn.OnLeftMouseClick = new Action(delegate { this.Frame.Remove(); });
else
btn.OnLeftMouseClick = onclick;
this.Frame.SetSize(500, this.AddY);
this.Frame.Center();
this.Buttons.Add(btn);
return btn;
}
public Label AddLabel(string text) {
this.DoingHalf = false;
Label lbl = new Label();
lbl.SetParent(this.Frame);
lbl.SetSize(400, 25);
lbl.SetPos(50, AddY);
lbl.Text = text;
AddY += 30;
this.Frame.SetSize(500, AddY);
this.Frame.Center();
this.Labels.Add(lbl);
return lbl;
}
public Label AddHalfLabel(string text) {
Label lbl = new Label();
lbl.SetParent(this.Frame);
lbl.SetSize(195, 25);
lbl.SetPos(this.DoingHalf ? 255 : 50, this.DoingHalf ? this.AddY - 30 : this.AddY);
lbl.Text = text;
this.DoingHalf = !this.DoingHalf;
if (this.DoingHalf) {
this.AddY += 30;
}
this.Frame.SetSize(500, this.AddY);
this.Frame.Center();
this.Labels.Add(lbl);
return lbl;
}
public TextBox AddTextbox(string text) {
this.DoingHalf = false;
TextBox tb = new TextBox();
tb.SetParent(this.Frame);
tb.SetSize(400, 25);
tb.SetPos(50, AddY);
tb.Text = text;
AddY += 30;
this.Frame.SetSize(500, AddY);
this.Frame.Center();
if (this.FistInputkak) {
tb.SetFocus();
this.FistInputkak = false;
}
this.Textboxes.Add(tb);
return tb;
}
public TextBox AddHalfTextbox(string text) {
TextBox tb = new TextBox();
tb.SetParent(this.Frame);
tb.SetSize(195, 25);
tb.SetPos(this.DoingHalf ? 255 : 50, this.DoingHalf ? this.AddY - 30 : this.AddY);
tb.Text = text;
if (this.FistInputkak) {
tb.SetFocus();
this.FistInputkak = false;
}
this.DoingHalf = !this.DoingHalf;
if (this.DoingHalf) {
this.AddY += 30;
}
this.Frame.SetSize(500, this.AddY);
this.Frame.Center();
this.Textboxes.Add(tb);
return tb;
}
public void Close() {
this.Frame.Remove();
}
public void Remove() {
this.Frame.Remove();
}
}
}
}

105
MrAG/Gui/Dropdown.cs Normal file
View File

@ -0,0 +1,105 @@
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();
}
}
}
}

65
MrAG/Gui/Frame.cs Normal file
View File

@ -0,0 +1,65 @@
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 Frame : Base
{
public string Text = "";
public SpriteFont Font = MrAG.Draw.Font;
public Color TopBarBackColor = Color.DimGray;
public Color TopBarBorderColor = Color.SlateGray;
public Color TopBarTextColor = Color.White;
public Color ContentBackColor = Color.DimGray;
public Color ContentBorderColor = Color.SlateGray;
public MrAG.Draw.TextAlignmentX AlignmentX = MrAG.Draw.TextAlignmentX.Left;
public MrAG.Draw.TextAlignmentY AlignmentY = MrAG.Draw.TextAlignmentY.Top;
public float Rotation = 0;
public bool AutoResize = true;
public bool HasCloseButton = true;
public bool CanMove = true;
public bool CanResize = false;
public int Padding = 6;
public override void Update()
{
base.Update();
}
public override bool Hovering() {
if (this.Removed) return false;
int x = MrAG.Gui.CurMouseState.X;
int y = MrAG.Gui.CurMouseState.Y;
return x >= this.X && x <= this.X + this.Width && y >= this.Y - 22 - Padding && y <= this.Y + this.Height;
}
public override void Draw()
{
MrAG.Draw.Box(this.X - Padding, this.Y - 22 - Padding, this.Width + Padding * 2, 22, this.TopBarBackColor);
MrAG.Draw.Box(this.X - Padding, this.Y - Padding, this.Width + Padding * 2, this.Height + Padding * 2, this.ContentBackColor);
MrAG.Draw.OutlinedBox(this.X - Padding, this.Y - 22 - Padding, this.Width + Padding * 2, 22, 2, this.TopBarBorderColor);
MrAG.Draw.OutlinedBox(this.X - Padding, this.Y - Padding, this.Width + Padding * 2, this.Height + Padding * 2, 2, this.ContentBorderColor);
MrAG.Draw.SetFont(this.Font);
MrAG.Draw.Text(this.Text, this.X, this.Y - 22, this.TopBarTextColor, this.AlignmentX, this.AlignmentY, this.Rotation);
base.Draw();
}
}
}
}

50
MrAG/Gui/GroupBox.cs Normal file
View File

@ -0,0 +1,50 @@
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 GroupBox : Base
{
public string Text = "";
public SpriteFont Font = MrAG.Draw.Font;
public Color TextColor = Color.Black;
public Color LineColor = Color.Green;
public Color BackColor = Color.White;
public bool DrawLines = true;
public int LineSize = 2;
public int TextSpace = 5;
public int Padding = 2;
public override void Draw()
{
MrAG.Draw.Box(this.X, this.Y, this.Width, this.Height, this.BackColor);
if (DrawLines) {
Vector2 s = this.Font.MeasureString(this.Text);
MrAG.Draw.Box(this.X + this.Padding, this.Y + this.Padding, this.Padding * 2, this.LineSize, this.LineColor); // u1
MrAG.Draw.Box(this.X + this.Padding + (int)s.X + (this.TextSpace * 2), this.Y + this.Padding, this.Width - (this.Padding * 2) - (this.TextSpace * 2) - (int)s.X, this.LineSize, this.LineColor); // u2
MrAG.Draw.Box(this.X + this.Padding, this.Y + this.Padding, this.LineSize, this.Height - (this.Padding * 2), this.LineColor); // l
MrAG.Draw.Box(this.X + this.Width - this.Padding - this.LineSize, this.Y + this.Padding, this.LineSize, this.Height - (this.Padding * 2), this.LineColor); // r
MrAG.Draw.Box(this.X + this.Padding, this.Y + this.Height - this.Padding - this.LineSize, this.Width - (this.Padding * 2), this.LineSize, this.LineColor); // d
}
if (this.Text.Length > 0) {
MrAG.Draw.SetFont(this.Font);
MrAG.Draw.Text(this.Text, this.X + this.Padding + this.TextSpace, this.Y + this.Padding, this.TextColor, MrAG.Draw.TextAlignmentX.Left, MrAG.Draw.TextAlignmentY.Center, 0);
}
base.Draw();
}
}
}
}

34
MrAG/Gui/ImageBox.cs Normal file
View File

@ -0,0 +1,34 @@
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 ImageBox : Base
{
public string Text = "";
public SpriteFont Font = MrAG.Draw.Font;
public Color BorderColor = Color.Black;
public Color ImageColor = Color.White;
public Texture2D Texture;
public int BorderSize;
public override void Draw()
{
if (this.Texture != null)
MrAG.Draw.Texture(this.Texture, this.X + this.BorderSize, this.Y + this.BorderSize, this.Width - (this.BorderSize * 2), this.Height - (this.BorderSize * 2), this.ImageColor);
MrAG.Draw.OutlinedBox(this.X, this.Y, this.Width, this.Height, this.BorderSize, this.BorderColor);
base.Draw();
}
}
}
}

View File

@ -0,0 +1,93 @@
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 ImageBoxAnimated : Base
{
public Color BorderColor = Color.Black;
public Color ImageColor = Color.White;
public Texture2D Texture;
public int BorderSize;
public bool Playing = true;
public bool FlipTextureH = false;
public bool FlipTextureV = false;
Dictionary<string, List<Vector2>> FrameList = new Dictionary<string,List<Vector2>>();
int speed;
int curcolom;
string curframe = "";
double lastanichange;
public void SetFrameData(Dictionary<string, List<Vector2>> data, int speed, string animation) {
this.FrameList = data;
this.speed = speed;
this.curframe = animation;
this.curcolom = 0;
}
public void SetAnimationSpeed(int interval) {
this.speed = interval;
lastanichange = DateTime.Now.TimeOfDay.TotalMilliseconds;
}
public void SetAnimation(string name) {
if (!this.FrameList.ContainsKey(name))
return;
this.curframe = name;
if (curcolom > this.FrameList[curframe].Count - 1) {
curcolom = 0;
}
}
public void AddAnimation(string name, Vector2 offset) {
if (!this.FrameList.ContainsKey(name))
this.FrameList[name] = new List<Vector2>();
this.FrameList[name].Add(offset);
}
public override void Update() {
base.Update();
if (!this.Playing)
return;
if (this.curframe == "")
return;
double curtime = DateTime.Now.TimeOfDay.TotalMilliseconds;
if (Math.Abs(curtime - lastanichange) > speed) {
lastanichange = curtime;
curcolom++;
if (curcolom > this.FrameList[curframe].Count - 1) {
curcolom = 0;
}
}
}
public override void Draw()
{
if (this.curframe == "")
return;
MrAG.Draw.SpriteBatch.Draw(this.Texture, new Vector2(this.X + this.BorderSize, this.Y + this.BorderSize), new Rectangle((int)this.FrameList[this.curframe][this.curcolom].X, (int)this.FrameList[this.curframe][this.curcolom].Y, this.Width - (this.BorderSize * 2), this.Height - (this.BorderSize * 2)),
this.ImageColor, 0, new Vector2(0, 0),
1, this.FlipTextureH ? SpriteEffects.FlipHorizontally : this.FlipTextureV ? SpriteEffects.FlipVertically : SpriteEffects.None, 1);
MrAG.Draw.OutlinedBox(this.X, this.Y, this.Width, this.Height, this.BorderSize, this.BorderColor);
base.Draw();
}
}
}
}

30
MrAG/Gui/Label.cs Normal file
View File

@ -0,0 +1,30 @@
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 Label : Base {
public string Text = "";
public SpriteFont Font = MrAG.Draw.Font;
public Color Color = Color.White;
public MrAG.Draw.TextAlignmentX AlignmentX = MrAG.Draw.TextAlignmentX.Left;
public MrAG.Draw.TextAlignmentY AlignmentY = MrAG.Draw.TextAlignmentY.Top;
public float Rotation = 0;
public override void Draw() {
MrAG.Draw.SetFont(this.Font);
MrAG.Draw.Text(this.Text, this.X, this.Y, this.Color, this.AlignmentX, this.AlignmentY, this.Rotation);
base.Draw();
}
}
}
}

253
MrAG/Gui/ListBox.cs Normal file
View File

@ -0,0 +1,253 @@
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();
}
}
}
}

318
MrAG/Gui/ListView.cs Normal file
View File

@ -0,0 +1,318 @@
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<ListView_Colom_Item> Items = new List<ListView_Colom_Item>();
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<string> itms = new List<string>();
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<ListView_Colom> Coloms = new List<ListView_Colom>();
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();
}
}
}
}

176
MrAG/Gui/MessageBox.cs Normal file
View File

@ -0,0 +1,176 @@
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 MessageBox : Base {
private string Title;
private string Text;
private MrAG.Gui.Frame Frame;
private MrAG.Gui.Label Label;
public MrAG.Gui.Button[] Buttons;
private Action<object>[] ButtonActions_ArgLeftMouseClick;
private Action<object>[] ButtonActions_ArgLeftMousePress;
private Action<object>[] ButtonActions_ArgLeftMouseRelease;
private Action<object>[] ButtonActions_ArgRightMouseClick;
private Action<object>[] ButtonActions_ArgRightMousePress;
private Action<object>[] ButtonActions_ArgRightMouseRelease;
private Action<object>[] ButtonActions_ArgMiddleMouseClick;
private Action<object>[] ButtonActions_ArgMiddleMousePress;
private Action<object>[] ButtonActions_ArgMiddleMouseRelease;
public void SetTitle(string text){
this.Frame.Text = text;
Vector2 textsize = MrAG.Draw.GetTextSize(text);
if (textsize.X > this.Frame.Width){
this.Frame.SetSize((int)textsize.X + 20, this.Frame.Height);
this.Frame.Center();
}
}
public void SetText(string text){
this.Label.Text = text;
Vector2 textsize = MrAG.Draw.GetTextSize(text);
if (textsize.X > this.Frame.Width){
this.Frame.SetSize((int)textsize.X + 20, this.Frame.Height);
this.Frame.Center();
}
this.Label.CenterX();
}
public MessageBox(string title, string text) {
this.Title = title;
this.Text = text;
this.Setup();
}
public MessageBox(string title, string text, MrAG.Gui.Button[] bts) {
this.Title = title;
this.Text = text;
this.Buttons = bts;
this.Setup();
}
public override void Remove() {
this.Frame.Remove();
base.Remove();
}
private void Setup() {
Vector2 titlesize = MrAG.Draw.GetTextSize(this.Title);
Vector2 textsize = MrAG.Draw.GetTextSize(this.Text);
this.Frame = new MrAG.Gui.Frame();
this.Frame.SetSize((int)textsize.X + 20, (int)textsize.Y + 42);
this.Frame.Text = this.Title;
this.Label = new MrAG.Gui.Label();
this.Label.Text = this.Text;
this.Label.SetParent(this.Frame);
this.Label.SetY(0);
this.Label.AlignmentX = MrAG.Draw.TextAlignmentX.Center;
this.SetParent(this.Frame);
if (this.Buttons == null) {
MrAG.Gui.Button okbtn = new Button();
okbtn.SetParent(this.Frame);
okbtn.SetSize(100, 20);
okbtn.CenterX();
okbtn.SetY(this.Frame.Height - 20);
okbtn.Text = "Ok";
okbtn.OnLeftMouseClick = new Action(delegate {
this.Frame.Remove();
});
this.Buttons = new Button[] { okbtn };
} else {
int curx = 10;
this.ButtonActions_ArgLeftMouseClick = new Action<object>[this.Buttons.Length];
this.ButtonActions_ArgLeftMousePress = new Action<object>[this.Buttons.Length];
this.ButtonActions_ArgLeftMouseRelease = new Action<object>[this.Buttons.Length];
this.ButtonActions_ArgRightMouseClick = new Action<object>[this.Buttons.Length];
this.ButtonActions_ArgRightMousePress = new Action<object>[this.Buttons.Length];
this.ButtonActions_ArgRightMouseRelease = new Action<object>[this.Buttons.Length];
this.ButtonActions_ArgMiddleMouseClick = new Action<object>[this.Buttons.Length];
this.ButtonActions_ArgMiddleMousePress = new Action<object>[this.Buttons.Length];
this.ButtonActions_ArgMiddleMouseRelease = new Action<object>[this.Buttons.Length];
for (int i = 0; i < this.Buttons.Length; i++) {
this.Buttons[i].SetParent(this.Frame);
this.Buttons[i].SetY(this.Frame.Height - 22);
this.Buttons[i].SetX(curx);
this.Buttons[i].SetSize(100, 20);
this.ButtonActions_ArgLeftMouseClick[i] = this.Buttons[i].OnArgumentedLeftMouseClick;
this.ButtonActions_ArgLeftMousePress[i] = this.Buttons[i].OnArgumentedLeftMousePress;
this.ButtonActions_ArgLeftMouseRelease[i] = this.Buttons[i].OnArgumentedLeftMouseRelease;
this.ButtonActions_ArgRightMouseClick[i] = this.Buttons[i].OnArgumentedRightMouseClick;
this.ButtonActions_ArgRightMousePress[i] = this.Buttons[i].OnArgumentedRightMousePress;
this.ButtonActions_ArgRightMouseRelease[i] = this.Buttons[i].OnArgumentedRightMouseRelease;
this.ButtonActions_ArgMiddleMouseClick[i] = this.Buttons[i].OnArgumentedMiddleMouseClick;
this.ButtonActions_ArgMiddleMousePress[i] = this.Buttons[i].OnArgumentedMiddleMousePress;
this.ButtonActions_ArgMiddleMouseRelease[i] = this.Buttons[i].OnArgumentedMiddleMouseRelease;
this.Buttons[i].OnArgumentedLeftMouseClick = new Action<object>(this.btn_ArgLeftMouseClick);
this.Buttons[i].OnArgumentedLeftMousePress = new Action<object>(this.btn_ArgLeftMousePress);
this.Buttons[i].OnArgumentedLeftMouseRelease = new Action<object>(this.btn_ArgLeftMouseRelease);
this.Buttons[i].OnArgumentedRightMouseClick = new Action<object>(this.btn_ArgRightMouseClick);
this.Buttons[i].OnArgumentedRightMousePress = new Action<object>(this.btn_ArgRightMousePress);
this.Buttons[i].OnArgumentedRightMouseRelease = new Action<object>(this.btn_ArgRightMouseRelease);
this.Buttons[i].OnArgumentedMiddleMouseClick = new Action<object>(this.btn_ArgMiddleMouseClick);
this.Buttons[i].OnArgumentedMiddleMousePress = new Action<object>(this.btn_ArgMiddleMousePress);
this.Buttons[i].OnArgumentedMiddleMouseRelease = new Action<object>(this.btn_ArgMiddleMouseRelease);
this.Buttons[i].Tag = i;
curx += this.Buttons[i].Width + 10;
}
if (curx > this.Frame.Width) {
this.Frame.SetWidth(curx);
}
}
this.Frame.Center();
this.Label.CenterX();
}
private void btn_action(Action<object> act, object sender) {
if (act != null){
act.Invoke(sender);
this.Frame.Remove();
}
}
private void btn_ArgLeftMouseClick(object sender) { this.btn_action(this.ButtonActions_ArgLeftMouseClick[(int)(sender as MrAG.Gui.Base).Tag], sender); }
private void btn_ArgLeftMousePress(object sender) { this.btn_action(this.ButtonActions_ArgLeftMousePress[(int)(sender as MrAG.Gui.Base).Tag], sender); }
private void btn_ArgLeftMouseRelease(object sender) { this.btn_action(this.ButtonActions_ArgLeftMouseRelease[(int)(sender as MrAG.Gui.Base).Tag], sender); }
private void btn_ArgRightMouseClick(object sender) { this.btn_action(this.ButtonActions_ArgRightMouseClick[(int)(sender as MrAG.Gui.Base).Tag], sender); }
private void btn_ArgRightMousePress(object sender) { this.btn_action(this.ButtonActions_ArgRightMousePress[(int)(sender as MrAG.Gui.Base).Tag], sender); }
private void btn_ArgRightMouseRelease(object sender) { this.btn_action(this.ButtonActions_ArgRightMouseRelease[(int)(sender as MrAG.Gui.Base).Tag], sender); }
private void btn_ArgMiddleMouseClick(object sender) { this.btn_action(this.ButtonActions_ArgMiddleMouseClick[(int)(sender as MrAG.Gui.Base).Tag], sender); }
private void btn_ArgMiddleMousePress(object sender) { this.btn_action(this.ButtonActions_ArgMiddleMousePress[(int)(sender as MrAG.Gui.Base).Tag], sender); }
private void btn_ArgMiddleMouseRelease(object sender) { this.btn_action(this.ButtonActions_ArgMiddleMouseRelease[(int)(sender as MrAG.Gui.Base).Tag], sender); }
}
}
}

36
MrAG/Gui/ProgressBar.cs Normal file
View File

@ -0,0 +1,36 @@
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 ProgressBar : Base {
public Color BackColor = Color.Black;
public Color FrontColor = Color.Gray;
public Color BorderColor = Color.DarkOrchid;
public int BorderSize = 2;
public int _value;
public int Value {
get { return this._value; }
set{
if (value <= this.MaxValue && value >= 0)
this._value = value;
}
}
public int MaxValue = 100;
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.Box(this.X + this.BorderSize, this.Y + this.BorderSize, (int)(((double)this.Value / (double)this.MaxValue) * (this.Width - (this.BorderSize * 2))), this.Height - (this.BorderSize * 2), this.FrontColor);
MrAG.Draw.OutlinedBox(this.X, this.Y, this.Width, this.Height, this.BorderSize, this.BorderColor);
base.Draw();
}
}
}
}

99
MrAG/Gui/Slider.cs Normal file
View File

@ -0,0 +1,99 @@
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 Slider : Base {
public Color BorderColor = Color.Gray;
public Color BackColor = Color.DarkGray;
public Color SliderColor = Color.Blue;
public Color LineColor = Color.Red;
public int BorderSize;
public int Padding = 4;
public int LineSize = 2;
public int SliderSize = 6;
public Action OnValueChange;
private int _value;
public int Value{
get {
return this._value;
}
set{
int oldval = this._value;
if (value > this.MaxValue)
this._value = this.MaxValue;
else if (value < 0)
this._value = 0;
else
this._value = value;
if (oldval != this._value && this.OnValueChange != null)
this.OnValueChange.Invoke();
}
}
private int _maxvalue = 100;
public int MaxValue {
get {
return this._maxvalue;
}
set{
if (value < 0)
return;
this._maxvalue = value;
if (this._value > this._maxvalue)
this._value = this._maxvalue;
}
}
public override void Update() {
if (!this.IsActive() || !this.HasFocus)
return;
int x = MrAG.Gui.CurMouseState.X - (this.X + this.BorderSize + this.Padding - (this.SliderSize / 2));
this.Value = (int)Math.Round((((double)x / (double)(this.Width - (this.BorderSize * 2) - (this.Padding * 2))) * this.MaxValue));
if (this.Value < 0)
this.Value = 0;
if (this.Value > this.MaxValue)
this.Value = this.MaxValue;
base.Update();
}
public override void DoScrollUp() {
this.Value++;
base.DoScrollUp();
}
public override void DoScrollDown() {
this.Value--;
base.DoScrollDown();
}
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);
MrAG.Draw.Box(this.X + this.BorderSize + this.Padding, this.Y + (this.Height / 2) - (this.LineSize / 2), this.Width - (this.BorderSize * 2) - (this.Padding * 2), this.LineSize, this.LineColor);
MrAG.Draw.Box(this.X + this.BorderSize + this.Padding + (int)(((double)this.Value / (double)this.MaxValue) * (this.Width - (this.BorderSize * 2) - (this.Padding * 2))) - (this.SliderSize / 2), this.Y + (this.Height / 2) - (this.SliderSize / 2), this.SliderSize, this.SliderSize, this.SliderColor);
base.Draw();
}
}
}
}

88
MrAG/Gui/SliderV.cs Normal file
View File

@ -0,0 +1,88 @@
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 SliderV : Base {
public Color BorderColor = Color.Gray;
public Color BackColor = Color.DarkGray;
public Color SliderColor = Color.Blue;
public Color LineColor = Color.Red;
public int BorderSize;
public int Padding = 4;
public int LineSize = 2;
public int SliderSize = 6;
public Action OnValueChange;
private int _value;
public int Value{
get {
return this._value;
}
set{
int oldval = this._value;
if (value > this.MaxValue)
this._value = this.MaxValue;
else if (value < 0)
this._value = 0;
else
this._value = value;
if (oldval != this._value && this.OnValueChange != null)
this.OnValueChange.Invoke();
}
}
public int MaxValue = 100;
public override void Update() {
if (!this.IsActive() || !this.HasFocus)
return;
int y = MrAG.Gui.CurMouseState.Y - (this.Y + this.BorderSize + this.Padding - (this.SliderSize / 2));
this.Value = (int)Math.Round((((double)y / (double)(this.Height - (this.BorderSize * 2) - (this.Padding * 2))) * this.MaxValue));
if (this.Value < 0)
this.Value = 0;
if (this.Value > this.MaxValue)
this.Value = this.MaxValue;
base.Update();
}
public override void DoScrollUp() {
this.Value--;
base.DoScrollUp();
}
public override void DoScrollDown() {
this.Value++;
base.DoScrollDown();
}
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);
MrAG.Draw.Box(this.X + (this.Width / 2) - (this.LineSize / 2), this.Y + this.BorderSize + this.Padding, this.LineSize, this.Height - (this.BorderSize * 2) - (this.Padding * 2), this.LineColor);
MrAG.Draw.Box(
this.X + (this.Width / 2) - (this.SliderSize / 2),
this.Y + this.BorderSize + this.Padding + (int)(((double)this.Value / (double)this.MaxValue) * (this.Height - (this.BorderSize * 2) - (this.Padding * 2))) - (this.SliderSize / 2),
this.SliderSize, this.SliderSize, this.SliderColor);
base.Draw();
}
}
}
}

447
MrAG/Gui/TextBoxAdvanced.cs Normal file
View File

@ -0,0 +1,447 @@
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<List<object>> Lines = new List<List<object>>();
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<object>(){ 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<object> 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<object>(){ 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();
}
}
}
}

388
MrAG/Gui/Textbox.cs Normal file
View File

@ -0,0 +1,388 @@
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;
}*/
}
}
}