475 lines
19 KiB
C#
475 lines
19 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using Microsoft.Xna.Framework;
|
|||
|
using Microsoft.Xna.Framework.Input;
|
|||
|
using Microsoft.Xna.Framework.Graphics;
|
|||
|
|
|||
|
namespace MrAG {
|
|||
|
public partial class Gui {
|
|||
|
public class Key {
|
|||
|
public bool Shift, Alt, Ctrl;
|
|||
|
public string Letter;
|
|||
|
public int Char;
|
|||
|
}
|
|||
|
|
|||
|
public enum MouseKey : byte {Left = 1, Right = 2, Middle = 3}
|
|||
|
public static MouseState CurMouseState = Mouse.GetState();
|
|||
|
public static System.Windows.Forms.Form MainForm;
|
|||
|
|
|||
|
public static Action<int, int, MouseKey> OnScreenMouseClick;
|
|||
|
public static Action<int, int, MouseKey> OnScreenMouseUp;
|
|||
|
public static Action<int, int, MouseKey> OnScreenMouseDown;
|
|||
|
public static Action<Key> OnScreenKeyPress;
|
|||
|
|
|||
|
public static bool AcceptInput = true;
|
|||
|
|
|||
|
|
|||
|
private static List<Gui.Base> Objects = new List<Base>();
|
|||
|
private static int curscrollval = 0;
|
|||
|
private static Base _currentfocus;
|
|||
|
internal static bool BroughtToFront;
|
|||
|
|
|||
|
public static Base CurrentFocus{
|
|||
|
get {
|
|||
|
return _currentfocus;
|
|||
|
}
|
|||
|
set {
|
|||
|
if (_currentfocus != null) {
|
|||
|
_currentfocus.FocusLost();
|
|||
|
|
|||
|
if (_currentfocus.OnUnFocus != null)
|
|||
|
_currentfocus.OnUnFocus.Invoke();
|
|||
|
}
|
|||
|
|
|||
|
_currentfocus = value;
|
|||
|
|
|||
|
if (_currentfocus != null){
|
|||
|
_currentfocus.FocusGained();
|
|||
|
|
|||
|
if (_currentfocus.OnFocus != null)
|
|||
|
_currentfocus.OnFocus.Invoke();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void Initialize(GameWindow w) {
|
|||
|
MainForm = (System.Windows.Forms.Form)System.Windows.Forms.Form.FromHandle(w.Handle);
|
|||
|
|
|||
|
MainForm.KeyPreview = true;
|
|||
|
MainForm.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(KeyPress);
|
|||
|
MainForm.MouseClick += new System.Windows.Forms.MouseEventHandler(MouseClick);
|
|||
|
MainForm.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(MouseDoubleClick);
|
|||
|
MainForm.MouseUp += new System.Windows.Forms.MouseEventHandler(MouseUp);
|
|||
|
MainForm.MouseDown += new System.Windows.Forms.MouseEventHandler(MouseDown);
|
|||
|
}
|
|||
|
|
|||
|
public static void AddObject(Gui.Base obj) {
|
|||
|
obj.Removed = false;
|
|||
|
|
|||
|
Objects.Add(obj);
|
|||
|
}
|
|||
|
|
|||
|
public static void RemoveObject(Gui.Base obj) {
|
|||
|
obj.Removed = true;
|
|||
|
|
|||
|
if (obj.Parent != null) {
|
|||
|
obj.Parent.Children.Remove(obj);
|
|||
|
} else {
|
|||
|
Objects.Remove(obj);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void Draw() {
|
|||
|
for (int i = 0; i < Objects.Count; i++){
|
|||
|
if (Objects[i].Render){
|
|||
|
Objects[i].Draw();
|
|||
|
|
|||
|
if (BroughtToFront){
|
|||
|
BroughtToFront = false;
|
|||
|
i--;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static string ParseOemKey(string Input, bool Shift)
|
|||
|
{
|
|||
|
switch (Input)
|
|||
|
{
|
|||
|
case "Space": return " ";
|
|||
|
case "Tab": return " ";
|
|||
|
case "OemMinus": return Shift ? "_" : "-";
|
|||
|
case "Oemplus": return Shift ? "+" : "=";
|
|||
|
case "OemPeriod": return Shift ? ">" : ".";
|
|||
|
case "Oemcomma": return Shift ? "<" : ",";
|
|||
|
case "OemQuestion": return Shift ? "?" : "/";
|
|||
|
case "OemOpenBrackets": return Shift ? "{" : "[";
|
|||
|
case "Oem1": return Shift ? ":" : ";";
|
|||
|
case "Oem5": return Shift ? "|" : "\\";
|
|||
|
case "Oem6": return Shift ? "}" : "]";
|
|||
|
case "Oem7": return Shift ? "\"" : "'";
|
|||
|
case "Return": return "\n";
|
|||
|
case "ShiftKey": return "";
|
|||
|
case "Back": return "Backspace";
|
|||
|
}
|
|||
|
|
|||
|
return "";
|
|||
|
}
|
|||
|
|
|||
|
public static string ParseNumberKey(string Input, bool Shift)
|
|||
|
{
|
|||
|
int Num = int.Parse(Input[Input.Length - 1].ToString());
|
|||
|
|
|||
|
if (Shift)
|
|||
|
{
|
|||
|
switch (int.Parse(Input[1].ToString()))
|
|||
|
{
|
|||
|
case 1: return "!";
|
|||
|
case 2: return "@";
|
|||
|
case 3: return "#";
|
|||
|
case 4: return "$";
|
|||
|
case 5: return "%";
|
|||
|
case 6: return "^";
|
|||
|
case 7: return "&";
|
|||
|
case 8: return "*";
|
|||
|
case 9: return "(";
|
|||
|
case 0: return ")";
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
return Num.ToString();
|
|||
|
|
|||
|
return "";
|
|||
|
}
|
|||
|
|
|||
|
private static void KeyPress(object sender, System.Windows.Forms.PreviewKeyDownEventArgs args) {
|
|||
|
string letter = "";
|
|||
|
string[] Parts = args.KeyData.ToString().Split(new string[] { ", " }, StringSplitOptions.None);
|
|||
|
|
|||
|
if (Parts[0].Length == 1)
|
|||
|
{
|
|||
|
if (args.Shift)
|
|||
|
letter += Parts[0].ToUpper();
|
|||
|
else
|
|||
|
letter += Parts[0].ToLower();
|
|||
|
}
|
|||
|
else if ((Parts[0].Length == 2 && Parts[0][0] == 'D') || Parts[0].StartsWith("NumPad"))
|
|||
|
letter += ParseNumberKey(Parts[0], args.Shift);
|
|||
|
else
|
|||
|
{
|
|||
|
letter += ParseOemKey(Parts[0], args.Shift);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
Key keypressed = new Key {
|
|||
|
Alt = args.Alt,
|
|||
|
Ctrl = args.Control,
|
|||
|
Shift = args.Shift,
|
|||
|
Letter = letter,
|
|||
|
Char = (int)args.KeyCode
|
|||
|
};
|
|||
|
|
|||
|
if (CurrentFocus != null && CurrentFocus.Render && AcceptInput) {
|
|||
|
if (CurrentFocus.Enabled){
|
|||
|
CurrentFocus.KeyPress(keypressed);
|
|||
|
|
|||
|
if (CurrentFocus.OnKeyPress != null)
|
|||
|
CurrentFocus.OnKeyPress.Invoke();
|
|||
|
}
|
|||
|
} else {
|
|||
|
if (OnScreenKeyPress != null)
|
|||
|
OnScreenKeyPress.Invoke(keypressed);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private static void MouseClick(object sender, System.Windows.Forms.MouseEventArgs args) {
|
|||
|
if (AcceptInput){
|
|||
|
byte btn = 0;
|
|||
|
switch (args.Button) {
|
|||
|
case System.Windows.Forms.MouseButtons.Left:
|
|||
|
btn = 1;
|
|||
|
break;
|
|||
|
|
|||
|
case System.Windows.Forms.MouseButtons.Right:
|
|||
|
btn = 2;
|
|||
|
break;
|
|||
|
|
|||
|
case System.Windows.Forms.MouseButtons.Middle:
|
|||
|
btn = 3;
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
if (CurrentFocus != null) {
|
|||
|
if (CurrentFocus.Hovering()) {
|
|||
|
if (!DoMouseAction(CurrentFocus, btn, 2)) {
|
|||
|
if (OnScreenMouseClick != null)
|
|||
|
OnScreenMouseClick.Invoke(args.X, args.Y, (MouseKey)btn);
|
|||
|
}
|
|||
|
} else {
|
|||
|
CurrentFocus = null;
|
|||
|
}
|
|||
|
} else {
|
|||
|
if (OnScreenMouseClick != null)
|
|||
|
OnScreenMouseClick.Invoke(args.X, args.Y, (MouseKey)btn);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private static void MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs args) {
|
|||
|
if (AcceptInput){
|
|||
|
byte btn = 0;
|
|||
|
switch (args.Button) {
|
|||
|
case System.Windows.Forms.MouseButtons.Left:
|
|||
|
btn = 1;
|
|||
|
break;
|
|||
|
|
|||
|
case System.Windows.Forms.MouseButtons.Right:
|
|||
|
btn = 2;
|
|||
|
break;
|
|||
|
|
|||
|
case System.Windows.Forms.MouseButtons.Middle:
|
|||
|
btn = 3;
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
if (CurrentFocus != null) {
|
|||
|
if (CurrentFocus.Hovering()) {
|
|||
|
if (!DoMouseAction(CurrentFocus, btn, 3)) {
|
|||
|
if (OnScreenMouseClick != null)
|
|||
|
OnScreenMouseClick.Invoke(args.X, args.Y, (MouseKey)btn);
|
|||
|
}
|
|||
|
} else {
|
|||
|
CurrentFocus = null;
|
|||
|
}
|
|||
|
} else {
|
|||
|
if (OnScreenMouseClick != null)
|
|||
|
OnScreenMouseClick.Invoke(args.X, args.Y, (MouseKey)btn);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private static void MouseUp(object sender, System.Windows.Forms.MouseEventArgs args) {
|
|||
|
if (AcceptInput){
|
|||
|
byte btn = 0;
|
|||
|
switch (args.Button) {
|
|||
|
case System.Windows.Forms.MouseButtons.Left:
|
|||
|
btn = 1;
|
|||
|
break;
|
|||
|
|
|||
|
case System.Windows.Forms.MouseButtons.Right:
|
|||
|
btn = 2;
|
|||
|
break;
|
|||
|
|
|||
|
case System.Windows.Forms.MouseButtons.Middle:
|
|||
|
btn = 3;
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
if (CurrentFocus != null){
|
|||
|
if (CurrentFocus.Hovering()) {
|
|||
|
if (!DoMouseAction(CurrentFocus, btn, 1)) {
|
|||
|
if (OnScreenMouseUp != null)
|
|||
|
OnScreenMouseUp.Invoke(args.X, args.Y, (MouseKey)btn);
|
|||
|
}
|
|||
|
} else {
|
|||
|
CurrentFocus = null;
|
|||
|
}
|
|||
|
}else{
|
|||
|
if (OnScreenMouseUp != null)
|
|||
|
OnScreenMouseUp.Invoke(args.X, args.Y, (MouseKey)btn);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private static void MouseDown(object sender, System.Windows.Forms.MouseEventArgs args) {
|
|||
|
byte btn = 0;
|
|||
|
switch (args.Button) {
|
|||
|
case System.Windows.Forms.MouseButtons.Left:
|
|||
|
btn = 1;
|
|||
|
break;
|
|||
|
|
|||
|
case System.Windows.Forms.MouseButtons.Right:
|
|||
|
btn = 2;
|
|||
|
break;
|
|||
|
|
|||
|
case System.Windows.Forms.MouseButtons.Middle:
|
|||
|
btn = 3;
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
if (AcceptInput){
|
|||
|
CurrentFocus = null;
|
|||
|
|
|||
|
for(int i = Objects.Count - 1; i > -1; i--)
|
|||
|
if (DoMouseAction(Objects[i], btn, 0))
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (OnScreenMouseDown != null)
|
|||
|
OnScreenMouseDown.Invoke(args.X, args.Y, (MouseKey)btn);
|
|||
|
}
|
|||
|
|
|||
|
private static bool DoMouseAction(Base obj, byte btn, byte press){
|
|||
|
if (!obj.Render || !obj.Enabled) return false;
|
|||
|
|
|||
|
int x = MrAG.Gui.CurMouseState.X;
|
|||
|
int y = MrAG.Gui.CurMouseState.Y;
|
|||
|
|
|||
|
bool yes = false;
|
|||
|
for(int i = obj.Children.Count - 1; i > -1; i--){
|
|||
|
if (DoMouseAction(obj.Children[i], btn, press)){
|
|||
|
yes = true;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (!yes)
|
|||
|
if (obj.Hovering()){
|
|||
|
yes = true;
|
|||
|
CurrentFocus = obj;
|
|||
|
obj.SetFocus();
|
|||
|
if (press != 1)obj.BringToFront();
|
|||
|
|
|||
|
if (obj.Enabled) {
|
|||
|
switch (press){
|
|||
|
case 0:
|
|||
|
switch (btn) {
|
|||
|
case 1:
|
|||
|
obj.LeftMousePress(x - obj.X, y - obj.Y);
|
|||
|
if (obj.OnLeftMousePress != null) obj.OnLeftMousePress.Invoke();
|
|||
|
if (obj.OnArgumentedLeftMousePress != null) obj.OnArgumentedLeftMousePress.Invoke(obj);
|
|||
|
break;
|
|||
|
|
|||
|
case 2:
|
|||
|
obj.RightMousePress(x - obj.X, y - obj.Y);
|
|||
|
if (obj.OnRightMousePress != null) obj.OnRightMousePress.Invoke();
|
|||
|
if (obj.OnArgumentedRightMousePress != null) obj.OnArgumentedRightMousePress.Invoke(obj);
|
|||
|
break;
|
|||
|
|
|||
|
case 3:
|
|||
|
obj.MiddleMousePress(x - obj.X, y - obj.Y);
|
|||
|
if (obj.OnLeftMousePress != null) obj.OnLeftMousePress.Invoke();
|
|||
|
if (obj.OnArgumentedLeftMousePress != null) obj.OnArgumentedLeftMousePress.Invoke(obj);
|
|||
|
break;
|
|||
|
}
|
|||
|
break;
|
|||
|
|
|||
|
case 1:
|
|||
|
switch (btn) {
|
|||
|
case 1:
|
|||
|
obj.LeftMouseRelease(x - obj.X, y - obj.Y);
|
|||
|
if (obj.OnLeftMouseRelease != null) obj.OnLeftMouseRelease.Invoke();
|
|||
|
if (obj.OnArgumentedLeftMouseRelease != null) obj.OnArgumentedLeftMouseRelease.Invoke(obj);
|
|||
|
break;
|
|||
|
|
|||
|
case 2:
|
|||
|
obj.RightMouseRelease(x - obj.X, y - obj.Y);
|
|||
|
if (obj.OnRightMouseRelease != null) obj.OnRightMouseRelease.Invoke();
|
|||
|
if (obj.OnArgumentedRightMouseRelease != null) obj.OnArgumentedRightMouseRelease.Invoke(obj);
|
|||
|
break;
|
|||
|
|
|||
|
case 3:
|
|||
|
obj.MiddleMouseRelease(x - obj.X, y - obj.Y);
|
|||
|
if (obj.OnLeftMouseRelease != null) obj.OnLeftMouseRelease.Invoke();
|
|||
|
if (obj.OnArgumentedLeftMouseRelease != null) obj.OnArgumentedLeftMouseRelease.Invoke(obj);
|
|||
|
break;
|
|||
|
}
|
|||
|
break;
|
|||
|
|
|||
|
case 2:
|
|||
|
obj.DoClick(x - obj.X, y - obj.Y, (MouseKey)btn);
|
|||
|
switch (btn) {
|
|||
|
case 1:
|
|||
|
obj.LeftMouseClick(x - obj.X, y - obj.Y);
|
|||
|
if (obj.OnLeftMouseClick != null) obj.OnLeftMouseClick.Invoke();
|
|||
|
if (obj.OnArgumentedLeftMouseClick != null) obj.OnArgumentedLeftMouseClick.Invoke(obj);
|
|||
|
break;
|
|||
|
|
|||
|
case 2:
|
|||
|
obj.RightMouseClick(x - obj.X, y - obj.Y);
|
|||
|
if (obj.OnRightMouseClick != null) obj.OnRightMouseClick.Invoke();
|
|||
|
if (obj.OnArgumentedRightMouseClick != null) obj.OnArgumentedRightMouseClick.Invoke(obj);
|
|||
|
break;
|
|||
|
|
|||
|
case 3:
|
|||
|
obj.MiddleMouseClick(x - obj.X, y - obj.Y);
|
|||
|
if (obj.OnLeftMouseClick != null) obj.OnLeftMouseClick.Invoke();
|
|||
|
if (obj.OnArgumentedLeftMouseClick != null) obj.OnArgumentedLeftMouseClick.Invoke(obj);
|
|||
|
break;
|
|||
|
}
|
|||
|
break;
|
|||
|
|
|||
|
case 3:
|
|||
|
obj.DoDoubleClick(x - obj.X, y - obj.Y, (MouseKey)btn);
|
|||
|
switch (btn) {
|
|||
|
case 1:
|
|||
|
obj.LeftMouseClick(x - obj.X, y - obj.Y);
|
|||
|
if (obj.OnLeftDoubleMouseClick != null) obj.OnLeftDoubleMouseClick.Invoke();
|
|||
|
if (obj.OnArgumentedLeftDoubleMouseClick != null) obj.OnArgumentedLeftDoubleMouseClick.Invoke(obj);
|
|||
|
break;
|
|||
|
|
|||
|
case 2:
|
|||
|
obj.RightMouseClick(x - obj.X, y - obj.Y);
|
|||
|
if (obj.OnRightDoubleMouseClick != null) obj.OnRightDoubleMouseClick.Invoke();
|
|||
|
if (obj.OnArgumentedRightDoubleMouseClick != null) obj.OnArgumentedRightDoubleMouseClick.Invoke(obj);
|
|||
|
break;
|
|||
|
|
|||
|
case 3:
|
|||
|
obj.MiddleMouseClick(x - obj.X, y - obj.Y);
|
|||
|
if (obj.OnLeftDoubleMouseClick != null) obj.OnLeftDoubleMouseClick.Invoke();
|
|||
|
if (obj.OnArgumentedLeftDoubleMouseClick != null) obj.OnArgumentedLeftDoubleMouseClick.Invoke(obj);
|
|||
|
break;
|
|||
|
}
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return yes;
|
|||
|
}
|
|||
|
|
|||
|
public static void Update() {
|
|||
|
CurMouseState = Mouse.GetState();
|
|||
|
|
|||
|
int scrollrem = curscrollval - CurMouseState.ScrollWheelValue;
|
|||
|
if (scrollrem != 0) {
|
|||
|
if (CurrentFocus != null) {
|
|||
|
bool up = scrollrem < 0;
|
|||
|
if (scrollrem < 0)
|
|||
|
scrollrem = -scrollrem;
|
|||
|
|
|||
|
while (scrollrem > 0) {
|
|||
|
if (up) {
|
|||
|
CurrentFocus.DoScrollUp();
|
|||
|
|
|||
|
if (CurrentFocus.OnScrollUp != null)
|
|||
|
CurrentFocus.OnScrollUp.Invoke();
|
|||
|
|
|||
|
} else {
|
|||
|
CurrentFocus.DoScrollDown();
|
|||
|
|
|||
|
if (CurrentFocus.OnScrollDown != null)
|
|||
|
CurrentFocus.OnScrollDown.Invoke();
|
|||
|
}
|
|||
|
|
|||
|
scrollrem -= 120;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
curscrollval = CurMouseState.ScrollWheelValue;
|
|||
|
}
|
|||
|
|
|||
|
for (int i = 0; i < Objects.Count; i++){
|
|||
|
Objects[i].Update();
|
|||
|
|
|||
|
if (BroughtToFront){
|
|||
|
BroughtToFront = false;
|
|||
|
i--;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|