Initial commit (2011)

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

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
bin/
obj/
*.suo
*.docstates
*.user

20
MrAG.sln Normal file
View File

@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual C# Express 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MrAG", "MrAG\MrAG.csproj", "{48B30584-0795-4FDA-9561-61B2381F8656}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{48B30584-0795-4FDA-9561-61B2381F8656}.Debug|x86.ActiveCfg = Debug|x86
{48B30584-0795-4FDA-9561-61B2381F8656}.Debug|x86.Build.0 = Debug|x86
{48B30584-0795-4FDA-9561-61B2381F8656}.Release|x86.ActiveCfg = Release|x86
{48B30584-0795-4FDA-9561-61B2381F8656}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

127
MrAG/Achievements.cs Normal file
View File

@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.Threading;
namespace MrAG
{
public class Achievements
{
public class GamerServicesAchievementsException : Exception{
public GamerServicesAchievementsException(string msg): base(msg){
}
}
public class Achievement{
public short ID;
public string Name;
public string Desc;
public string Hash;
public int CurrentValue;
public int TargetValue;
public bool Completed;
public byte Points;
}
public static Dictionary<string, Achievement> AchievementList = new Dictionary<string,Achievement>();
public static void Load(){
AchievementList = new Dictionary<string,Achievement>();
Gamerservices.GetAPIConnection().Send(2, 0, (byte)0);
/*
string[] lines = Networking.PostData("achievement", "sub=list");
for (short i = 0; i < lines.Length; i++){
AchievementList[lines[i]] = new Achievement();
AchievementList[lines[i]].Name = lines[i];
AchievementList[lines[i]].Desc = lines[i + 1];
AchievementList[lines[i]].CurrentValue = int.Parse(lines[i + 2]);
AchievementList[lines[i]].TargetValue = int.Parse(lines[i + 3]);
AchievementList[lines[i]].Completed = AchievementList[lines[i]].CurrentValue >= AchievementList[lines[i]].TargetValue;
AchievementList[lines[i]].ID = i;
i += 3;
}*/
}
public static Achievement Get(string ach){
if (!Gamerservices.IsLoggedIn()) return null;
if (!AchievementList.ContainsKey(ach)){
throw new GamerServicesAchievementsException("No such achievement!");
}
return AchievementList[ach];
}
public static Dictionary<string, Achievement> GetAll(){
return AchievementList;
}
public static void Add(string ach, int value){
if (!Gamerservices.IsLoggedIn()) return;
if (!AchievementList.ContainsKey(ach)){
throw new GamerServicesAchievementsException("No such achievement!");
}
if (AchievementList[ach].Completed){
return;
}else{
if (AchievementList[ach].CurrentValue + value > AchievementList[ach].TargetValue) {
value = AchievementList[ach].TargetValue - AchievementList[ach].CurrentValue;
}
AchievementList[ach].CurrentValue += value;
AchievementList[ach].Completed = AchievementList[ach].CurrentValue >= AchievementList[ach].TargetValue;
if (AchievementList[ach].Completed){
Overlay.AddNotice(0, "Achievement update", "Unlocked " + ach);
}
}
Gamerservices.GetAPIConnection().Send(2, 9 + AchievementList[ach].Hash.Length, (byte)1, AchievementList[ach].Hash, value);
/*
new Thread(new ThreadStart(delegate{
string[] res = Networking.PostData("achievement", "sub=add&achievement[0]=" + ach + "&val[0]=" + value);
if (res.Length == 1){
Overlay.AddNotice(0, "Achievement update", "Unlocked " + ach);
}
})).Start();
* */
}
public static void Set(string ach, int value){
if (!Gamerservices.IsLoggedIn()) return;
if (!AchievementList.ContainsKey(ach)){
throw new GamerServicesAchievementsException("No such achievement!");
}
if (AchievementList[ach].Completed){
return;
}else{
AchievementList[ach].CurrentValue = value;
AchievementList[ach].Completed = AchievementList[ach].CurrentValue >= AchievementList[ach].TargetValue;
if (AchievementList[ach].Completed){
Overlay.AddNotice(0, "Achievement update", "Unlocked " + ach);
}
}
Gamerservices.GetAPIConnection().Send(2, 9 + AchievementList[ach].Hash.Length, (byte)2, AchievementList[ach].Hash, value);
/*
string[] res = Networking.PostData("achievement", "sub=set&achievement[0]=" + ach + "&val[0]=" + value);
if (res.Length == 1){
Overlay.AddNotice(0, res[0]);
}
* */
}
}
}

76
MrAG/Checksum.cs Normal file
View File

@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace MrAG
{
public class Checksum
{
//
// "This is like FABFUKAU(*W(&!@*JKHDHD"
//
// - Bromvlieg
//
//
// "Cool story, bro."
//
// - Angelo
//
public class FileInfo{
public string Path;
public string Hash;
public string LastWritten;
public string LastAccessed;
public string DateCreated;
public long Size;
public FileInfo(){}
public FileInfo(string path){
FileInfo f = MrAG.Checksum.GetInfo(path);
this.Hash = f.Hash;
this.Path = f.Path;
this.LastWritten = f.LastWritten;
this.LastAccessed = f.LastWritten;
this.DateCreated = f.DateCreated;
this.Size = f.Size;
}
}
public static FileInfo GetInfo(string Filename){
FileInfo f = new FileInfo();
System.IO.FileInfo Fileinfo = new System.IO.FileInfo(Filename);
f.Hash = MrAG.Checksum.FileHash(Filename);
f.DateCreated = Fileinfo.CreationTimeUtc.ToString();
f.LastAccessed = Fileinfo.LastAccessTimeUtc.ToString();
f.LastWritten = Fileinfo.LastWriteTimeUtc.ToString();
f.Size = Fileinfo.Length;
return f;
}
public static String FileHash(String Filename)
{
FileStream File = new FileStream(Filename, FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] RetVal = md5.ComputeHash(File);
File.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < RetVal.Length; i++)
sb.Append(RetVal[i].ToString("x2"));
return sb.ToString().ToLower();
}
}
}

239
MrAG/Config.cs Normal file
View File

@ -0,0 +1,239 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace MrAG
{
public class Config
{
private static Hashtable Data = new Hashtable();
private static bool Busy;
public static void Clear(){
while (Busy) { };
Busy = true;
Data.Clear();
Busy = false;
}
public static void Write(string path){
while (Busy) { };
Busy = true;
StreamWriter writer = new StreamWriter(File.OpenWrite(path));
foreach( DictionaryEntry cat in Data)
{
writer.WriteLine("[\"" + cat.Key + "\"] = {");
Hashtable values = (Hashtable)Data[cat.Key];
foreach( DictionaryEntry entry in values)
{
writer.WriteLine(" [\"" + entry.Key + "\"] = \"" + entry.Value + "\";");
}
writer.WriteLine("}");
writer.WriteLine("");
}
Busy = false;
writer.Close();
}
public static bool Read(string file){
while (Busy) { };
Busy = true;
Data.Clear();
if (File.Exists(file)){
StreamReader reader = new StreamReader(File.OpenRead(file));
string[] content = reader.ReadToEnd().Replace("\r", "").Split('\n');
reader.Close();
int entrys = 0;
string CurrentSection = "";
string CurrentName = "";
foreach(string line in content){
if (line.Length > 0){
if (line.Replace(" ","").Replace(" ", "").StartsWith("#")){ // comment
}else if (line.StartsWith("[\"")){ // section
CurrentSection = line.Substring(2, line.Length - 8);
Data[CurrentSection] = new Hashtable();
}else if (line == "}"){ // end-section
CurrentSection = "";
}else if (line.StartsWith(" [\"") || line.StartsWith(" [\"")){ // name/value
int splitpos = line.IndexOf("\"] = \"");
if (line.EndsWith("\";")){
entrys++;
(Data[CurrentSection] as Hashtable)[line.Substring(3, splitpos - 3)] = line.Substring(splitpos + 6, line.Length - splitpos - 8);
}else{
CurrentName = line.Substring(3, splitpos - 3);
(Data[CurrentSection] as Hashtable)[CurrentName] = line.Substring(splitpos + 6, line.Length - splitpos - 6);
}
}else{ // resume value on next line (multilined crap :3)
if (line.EndsWith("\";")){
(Data[CurrentSection] as Hashtable)[CurrentName] += "\n" + line.Substring(1, line.Length - 3);
CurrentName = "";
}else{
(Data[CurrentSection] as Hashtable)[CurrentName] += "\n" + line;
}
}
}
}
Console.WriteLine("Finished reading '" + file + "', " + entrys + " entrys");
Busy = false;
return true;
}
Busy = false;
return false;
}
public static bool IsBusy(){
return Busy;
}
public static bool Value_Exist(string section, string name){
while (Busy) { };
Busy = true;
if (Data[section] == null){
Busy = false;
return false;
}
if ((Data[section] as Hashtable)[name] == null){
Busy = false;
return false;
}
Busy = false;
return true;
}
public static Hashtable Section_Get(string name){
while (Busy) { };
Busy = true;
if (Data[name] == null) {
Busy = false;
return new Hashtable();
}
Busy = false;
return (Hashtable)Data[name];
}
public static bool Section_Exists(string name){
while (Busy) { };
return Data[name] != null;
}
public static void Section_Set(string name, Hashtable data){
while (Busy) { };
Busy = true;
Data[name] = data;
Busy = false;
}
public static string Value_Get(string section, string name){
while (Busy) { };
Busy = true;
if (Data[section] == null) {
Busy = false;
return "";
}
Busy = false;
return (string)(Data[section] as Hashtable)[name];
}
public static string Value_Get(string section, string name, string def){
while (Busy) { };
Busy = true;
if (Data[section] == null) {
Busy = false;
return def;
}
string value = (string)(Data[section] as Hashtable)[name];
Busy = false;
return value == null ? def : value;
}
public static int Value_GetInt(string section, string name, int def){
int.TryParse(Value_Get(section, name), out def);
return def;
}
public static short Value_GetShort(string section, string name, short def){
short.TryParse(Value_Get(section, name), out def);
return def;
}
public static float Value_GetFloat(string section, string name, float def){
float.TryParse(Value_Get(section, name), out def);
return def;
}
public static double Value_GetDouble(string section, string name, double def){
double.TryParse(Value_Get(section, name), out def);
return def;
}
public static bool Value_GetBool(string section, string name, bool def){
return Value_Get(section, name, def.ToString()).ToLower() == "true";
}
public static void Value_Set(string section, string name, string value){
while (Busy) { };
Busy = true;
if (Data[section] == null)
Data[section] = new Hashtable();
(Data[section] as Hashtable)[name] = value;
Busy = false;
}
public static void Value_SetBool(string section, string name, bool value){ Value_Set(section, name, value.ToString()); }
public static void Value_SetInt(string section, string name, int value){ Value_Set(section, name, value.ToString()); }
public static void Value_SetShort(string section, string name, short value){ Value_Set(section, name, value.ToString()); }
public static void Value_SetFloat(string section, string name, float value){ Value_Set(section, name, value.ToString()); }
public static void Value_SetDouble(string section, string name, double value){ Value_Set(section, name, value.ToString()); }
public static void PrintContent(){
while (Busy) { };
Busy = true;
foreach( DictionaryEntry cat in Data )
{
Console.WriteLine("[\"" + cat.Key + "\"] = {");
Hashtable values = (Hashtable)Data[cat.Key];
foreach( DictionaryEntry entry in values)
{
Console.WriteLine(" [\"" + entry.Key + "\"] = \"" + entry.Value + "\";");
}
Console.WriteLine("}");
Console.WriteLine("");
}
Busy = false;
}
}
}

19
MrAG/DLLChecker.cs Normal file
View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MrAG {
public class DLLChecker {
class XNA{ public XNA() { new Microsoft.Xna.Framework.Color(); } }
public static bool HasXNA() {
try {
new XNA();
return true;
} catch {
return false;
}
}
}
}

242
MrAG/Draw.cs Normal file

File diff suppressed because one or more lines are too long

266
MrAG/Gamerservices.cs Normal file
View File

@ -0,0 +1,266 @@
using System;
using System.Diagnostics;
using System.Collections.Generic;
namespace MrAG
{
public class Gamerservices
{
public class GamerServicesException : Exception{
public GamerServicesException(string msg): base(msg){
}
}
private struct AvatarRequest{
public string user;
public Action<string, Microsoft.Xna.Framework.Graphics.Texture2D> cb;
}
public static Action<List<MrAG.Serverlist.ServerInfo>> ServerListCallback;
public static Action<byte, Networking.Packet> IncommingTCPPacket;
public static Dictionary<string, Microsoft.Xna.Framework.Graphics.Texture2D> Avatars = new Dictionary<string, Microsoft.Xna.Framework.Graphics.Texture2D>();
private static string User = "";
private static string SessID = "";
private static bool LoggedIn = false;
private static string GameName = "";
private static string APIKey = "";
private static bool DebugMode = false;
private static Microsoft.Xna.Framework.Graphics.GraphicsDevice GraphicsDevice;
private static System.Collections.Generic.List<AvatarRequest> AvatarRequests = new System.Collections.Generic.List<AvatarRequest>();
private static MrAG.Networking.TCPClient APIConnection;
public static MrAG.Networking.TCPClient GetAPIConnection() {
return APIConnection;
}
public static bool GetDebug(){
return DebugMode;
}
public static void SetDebug(bool debug){
DebugMode = debug;
}
public static string GetSessID(){
return SessID;
}
public static string GetAPIKey(){
return APIKey;
}
public static string GetUser(){
return User;
}
public static void SetAPIKey(string key){
APIKey = key;
}
public static string GetIdentifier(){
return GameName;
}
public static void SetIdentifier(string id){
GameName = id;
}
public static void SetUsername(string user) {
User = user;
}
public static bool IsLoggedIn(){
return LoggedIn;
}
public static void SetGraphicsDevice(Microsoft.Xna.Framework.Graphics.GraphicsDevice gs){
GraphicsDevice = gs;
}
public static Microsoft.Xna.Framework.Graphics.Texture2D GetAvatar(string user) {
if (Avatars.ContainsKey(user))
return Avatars[user];
else
return null;
}
public static void RequestAvatar(string user, Action<string, Microsoft.Xna.Framework.Graphics.Texture2D> Callback) {
AvatarRequests.Add(new AvatarRequest(){user = user, cb = Callback});
MrAG.Gamerservices.APIConnection.Send(6, 0, user);
}
public static void Print(string head, string[] keys, string[] values){
string remotetext = head + "\r\n";
System.Diagnostics.Debug.Print(head);
for (int i = 0; i < keys.Length; i++){
System.Diagnostics.Debug.Print("\t" + keys[i] + ": " + values[i]);
remotetext += "\t" + keys[i] + ": " + values[i] + "\r\n";
}
System.Diagnostics.Debug.Print("");
GetAPIConnection().Send(5, remotetext.Length + 4, remotetext.Substring(0, remotetext.Length - 1));
}
public static void Print(string text){
System.Diagnostics.Debug.Print(text);
GetAPIConnection().Send(5, text.Length + 5, text);
}
public static bool Login(bool EnableDrawNoticeSystem){
if (LoggedIn) return false;
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1) {
string sessID = args[args.Length - 2];
int APIPort = int.Parse(args[args.Length - 1]);
try {
APIConnection = new MrAG.Networking.TCPClient("127.0.0.1", APIPort);
} catch {
throw new GamerServicesException("Failed to connect to the API service!");
}
APIConnection.Send(0, sessID.Length + APIKey.Length + GameName.Length + 13, sessID, APIKey, GameName);
APIConnection.Packets[0] = new Action<MrAG.Networking.Packet>(Packet_00);
APIConnection.Packets[1] = new Action<MrAG.Networking.Packet>(Packet_01);
APIConnection.Packets[2] = new Action<MrAG.Networking.Packet>(Packet_02);
APIConnection.Packets[3] = new Action<MrAG.Networking.Packet>(Packet_03);
APIConnection.Packets[5] = new Action<MrAG.Networking.Packet>(Packet_05);
APIConnection.Packets[6] = new Action<MrAG.Networking.Packet>(Packet_06);
APIConnection.Packets[7] = new Action<MrAG.Networking.Packet>(Packet_07);
while (User.Length == 0) {
APIConnection.Update();
}
LoggedIn = true;
Achievements.Load();
Overlay.InitilizeSystem(EnableDrawNoticeSystem);
} else {
return false;
}
return true;
}
public static void Update() {
if (APIConnection != null) {
APIConnection.OnRecieve = IncommingTCPPacket;
APIConnection.Update();
}
}
public static void LogOut(){
if (User != null && User.Length > 0)
GetAPIConnection().Send(4, 1);
User = "";
SessID = "";
LoggedIn = false;
Overlay.Close();
}
private static void Packet_00(MrAG.Networking.Packet p){
User = p.ReadString();
}
private static void Packet_01(MrAG.Networking.Packet p){
p.Send(1, 1);
}
private static void Packet_02(MrAG.Networking.Packet p){
byte type = p.ReadByte();
switch (type) {
case 0:
short loops = p.ReadShort();
for (short i = 0; i < loops; i++) {
string name = p.ReadString();
Achievements.AchievementList[name] = new Achievements.Achievement();
Achievements.AchievementList[name].Name = name;
Achievements.AchievementList[name].Hash = p.ReadString();
Achievements.AchievementList[name].Desc = p.ReadString();
Achievements.AchievementList[name].CurrentValue = p.ReadInt();
Achievements.AchievementList[name].TargetValue = p.ReadInt();
Achievements.AchievementList[name].Points = p.ReadByte();
Achievements.AchievementList[name].Completed = Achievements.AchievementList[name].CurrentValue >= Achievements.AchievementList[name].TargetValue;
Achievements.AchievementList[name].ID = i;
}
break;
}
}
private static void Packet_03(MrAG.Networking.Packet p){
MrAG.Leaderboard.SubmittedEntry.Rank = p.ReadInt();
MrAG.Gamerservices.GetAPIConnection().Send(5, 0, "Recieved leaderboard pos " + MrAG.Leaderboard.SubmittedEntry.Rank);
}
private static void Packet_05(MrAG.Networking.Packet p){
string title = p.ReadString();
string msg = p.ReadString();
byte icon = p.ReadByte();
Overlay.AddNotice(icon, title, msg);
}
private static void Packet_06(MrAG.Networking.Packet p){
string user = p.ReadString();
string url = p.ReadString();
if (GraphicsDevice == null)
return;
System.Net.WebClient wc = new System.Net.WebClient();
wc.Proxy = null;
System.IO.MemoryStream ms = new System.IO.MemoryStream(wc.DownloadData(url));
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
System.IO.MemoryStream ms2 = new System.IO.MemoryStream();
img.Save(ms2, System.Drawing.Imaging.ImageFormat.Png);
Microsoft.Xna.Framework.Graphics.Texture2D tex = Microsoft.Xna.Framework.Graphics.Texture2D.FromStream(GraphicsDevice, ms2);
ms.Close();
Avatars[user] = tex;
for (int i = 0; i < AvatarRequests.Count; i++) {
if (AvatarRequests[i].user == user) {
AvatarRequests[i].cb.DynamicInvoke(user, tex);
AvatarRequests.RemoveAt(i);
break;
}
}
}
private static void Packet_07(MrAG.Networking.Packet p){
int count = p.ReadInt();
List<MrAG.Serverlist.ServerInfo> servers = new List<Serverlist.ServerInfo>();
for (int i = 0; i < count; i++)
servers.Add(new MrAG.Serverlist.ServerInfo(){IP = p.ReadString(),
Port = p.ReadInt(),
Servername = p.ReadString(),
Meta = p.ReadString(),
Users = p.ReadInt(),
MaxUsers = p.ReadInt()});
ServerListCallback.DynamicInvoke(servers);
}
}
}

474
MrAG/Gui.cs Normal file
View File

@ -0,0 +1,474 @@
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--;
}
}
}
}
}

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