SDK/MrAG/Gui/Base.cs

357 lines
12 KiB
C#

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) {}
}
}
}