Initial commit (2011)
This commit is contained in:
commit
da459a94c9
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
bin/
|
||||
obj/
|
||||
|
||||
*.suo
|
||||
*.docstates
|
||||
*.user
|
20
MrAG.sln
Normal file
20
MrAG.sln
Normal 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
127
MrAG/Achievements.cs
Normal 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
76
MrAG/Checksum.cs
Normal 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
239
MrAG/Config.cs
Normal 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
19
MrAG/DLLChecker.cs
Normal 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
242
MrAG/Draw.cs
Normal file
File diff suppressed because one or more lines are too long
266
MrAG/Gamerservices.cs
Normal file
266
MrAG/Gamerservices.cs
Normal 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
474
MrAG/Gui.cs
Normal 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
356
MrAG/Gui/Base.cs
Normal 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
67
MrAG/Gui/Button.cs
Normal 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
42
MrAG/Gui/Checkbox.cs
Normal 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
212
MrAG/Gui/Dialog.cs
Normal 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
105
MrAG/Gui/Dropdown.cs
Normal 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
65
MrAG/Gui/Frame.cs
Normal 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
50
MrAG/Gui/GroupBox.cs
Normal 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
34
MrAG/Gui/ImageBox.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
93
MrAG/Gui/ImageBoxAnimated.cs
Normal file
93
MrAG/Gui/ImageBoxAnimated.cs
Normal 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
30
MrAG/Gui/Label.cs
Normal 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
253
MrAG/Gui/ListBox.cs
Normal 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
318
MrAG/Gui/ListView.cs
Normal 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
176
MrAG/Gui/MessageBox.cs
Normal 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
36
MrAG/Gui/ProgressBar.cs
Normal 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
99
MrAG/Gui/Slider.cs
Normal 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
88
MrAG/Gui/SliderV.cs
Normal 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
447
MrAG/Gui/TextBoxAdvanced.cs
Normal 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
388
MrAG/Gui/Textbox.cs
Normal 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;
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
146
MrAG/KeyBindings.cs
Normal file
146
MrAG/KeyBindings.cs
Normal file
@ -0,0 +1,146 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
namespace MrAG
|
||||
{
|
||||
public class KeyBindings
|
||||
{
|
||||
public class KeyBinding
|
||||
{
|
||||
public Keys Key;
|
||||
public Delegate Press;
|
||||
public Delegate Release;
|
||||
public Delegate Hold;
|
||||
public int HoldInterval = 0;
|
||||
public double LastHold = 0;
|
||||
|
||||
public bool Pressed;
|
||||
|
||||
public KeyBinding(){}
|
||||
|
||||
public KeyBinding(Keys Key)
|
||||
{
|
||||
this.Key = Key;
|
||||
}
|
||||
|
||||
public void Call_Press()
|
||||
{
|
||||
if (Press == null) return;
|
||||
|
||||
Press.DynamicInvoke();
|
||||
}
|
||||
|
||||
public void Call_Release()
|
||||
{
|
||||
if (Release == null) return;
|
||||
|
||||
Release.DynamicInvoke();
|
||||
}
|
||||
|
||||
public void Call_Hold()
|
||||
{
|
||||
if (Hold == null) return;
|
||||
|
||||
Hold.DynamicInvoke();
|
||||
}
|
||||
}
|
||||
|
||||
private static List<KeyBinding> Bindings = new List<KeyBinding>();
|
||||
|
||||
public static void Update(GameTime gameTime)
|
||||
{
|
||||
KeyboardState ks = Keyboard.GetState();
|
||||
|
||||
foreach (KeyBinding binding in Bindings)
|
||||
{
|
||||
if (ks.IsKeyDown(binding.Key))
|
||||
{
|
||||
if (!binding.Pressed)
|
||||
{
|
||||
binding.Call_Press();
|
||||
binding.LastHold = gameTime.TotalGameTime.TotalMilliseconds;
|
||||
binding.Pressed = true;
|
||||
}else{
|
||||
if (gameTime.TotalGameTime.TotalMilliseconds - binding.LastHold > binding.HoldInterval){
|
||||
binding.LastHold = gameTime.TotalGameTime.TotalMilliseconds;
|
||||
binding.Call_Hold();
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if (binding.Pressed){
|
||||
binding.Pressed = false;
|
||||
binding.Call_Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Add(KeyBinding keyBinding)
|
||||
{
|
||||
Bindings.Add(keyBinding);
|
||||
}
|
||||
|
||||
public static void Add(Keys KeyToPress, Action OnPress)
|
||||
{
|
||||
KeyBinding key = new MrAG.KeyBindings.KeyBinding(Keys.Left);
|
||||
|
||||
key.Key = KeyToPress;
|
||||
key.Press = new Action(OnPress);
|
||||
Add(key);
|
||||
}
|
||||
|
||||
public static void Add(Keys KeyToPress, Action OnHold, int HoldInterval)
|
||||
{
|
||||
KeyBinding key = new MrAG.KeyBindings.KeyBinding();
|
||||
|
||||
key.Key = KeyToPress;
|
||||
key.Hold = OnHold;
|
||||
key.HoldInterval = HoldInterval;
|
||||
Add(key);
|
||||
}
|
||||
|
||||
public static void Add(Keys KeyToPress, Action OnPress, Action OnHold, int HoldInterval)
|
||||
{
|
||||
KeyBinding key = new MrAG.KeyBindings.KeyBinding();
|
||||
|
||||
key.Key = KeyToPress;
|
||||
key.Press = new Action(OnPress);
|
||||
key.Hold = OnHold;
|
||||
key.HoldInterval = HoldInterval;
|
||||
Add(key);
|
||||
}
|
||||
|
||||
public static void Add(Keys KeyToPress, Action OnPress, Action OnRelease)
|
||||
{
|
||||
KeyBinding key = new MrAG.KeyBindings.KeyBinding();
|
||||
|
||||
key.Key = KeyToPress;
|
||||
key.Press = new Action(OnPress);
|
||||
key.Release = new Action(OnRelease);
|
||||
Add(key);
|
||||
}
|
||||
|
||||
public static void Add(Keys KeyToPress, Action OnPress, Action OnHold, int HoldInterval, Action OnRelease)
|
||||
{
|
||||
KeyBinding key = new MrAG.KeyBindings.KeyBinding();
|
||||
|
||||
key.Key = KeyToPress;
|
||||
key.Press = new Action(OnPress);
|
||||
key.Release = new Action(OnRelease);
|
||||
key.Hold = OnHold;
|
||||
key.HoldInterval = HoldInterval;
|
||||
Add(key);
|
||||
}
|
||||
|
||||
public static void Remove(Keys Key)
|
||||
{
|
||||
foreach (KeyBinding binding in Bindings)
|
||||
{
|
||||
if (binding.Key == Key)
|
||||
Bindings.Remove(binding);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
89
MrAG/Leaderboard.cs
Normal file
89
MrAG/Leaderboard.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MrAG
|
||||
{
|
||||
public class Leaderboard
|
||||
{
|
||||
public class GamerServicesLeaderboardException : Exception{
|
||||
public GamerServicesLeaderboardException(string msg): base(msg){
|
||||
}
|
||||
}
|
||||
|
||||
public static Entry SubmittedEntry;
|
||||
|
||||
public class Entry{
|
||||
public string Username;
|
||||
public string Board;
|
||||
public int Score;
|
||||
public int Rank;
|
||||
|
||||
public Entry(string user, int pos, int score){
|
||||
Rank = pos;
|
||||
Score = score;
|
||||
Username = user;
|
||||
}
|
||||
|
||||
public Entry(string user, string board, int pos, int score){
|
||||
Board = board;
|
||||
Rank = pos;
|
||||
Score = score;
|
||||
Username = user;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Submit(string board, int score){
|
||||
SubmittedEntry = new Entry(MrAG.Gamerservices.GetUser(), board, -1, score);
|
||||
Gamerservices.GetAPIConnection().Send(3, 9 + board.Length, (byte)1, board, score);
|
||||
}
|
||||
|
||||
public static List<Entry> GetList(string board, int startpos, int length){
|
||||
throw new GamerServicesLeaderboardException("NOT SUPPORTED");
|
||||
|
||||
/*
|
||||
try{
|
||||
string[] list = Networking.PostData("leaderboard", "sub=list&identifier=" + board +"&start=" + startpos + "&length=" + length);
|
||||
|
||||
List<Entry> entrys = new List<Entry>();
|
||||
for (int i = 0; i < list.Length; i++){
|
||||
entrys.Add(new Entry(list[i], int.Parse(list[i + 1]), int.Parse(list[i + 2])));
|
||||
|
||||
i += 2;
|
||||
}
|
||||
|
||||
return entrys;
|
||||
}catch{
|
||||
throw new GamerServicesLeaderboardException("Error while recieving list");
|
||||
}*/
|
||||
}
|
||||
|
||||
public static Entry GetUser(string board, string user){
|
||||
Networking.TCPClient c = Gamerservices.GetAPIConnection();
|
||||
c.Connection.Start_Recieve(); // just to make sure we are free to recieve the incomming data
|
||||
c.Connection.Send(3, 9 + board.Length + user.Length, (byte)1, board, user);
|
||||
|
||||
int pos = c.Connection.ReadInt();
|
||||
int score = c.Connection.ReadInt();
|
||||
c.Connection.Finish_Recieve();
|
||||
return new Entry(MrAG.Gamerservices.GetUser(), pos, score);
|
||||
|
||||
/*
|
||||
string[] list = Networking.PostData("leaderboard", "sub=get&identifier=" + board +"&user=" + user);
|
||||
|
||||
if (list.Length == 0 || list[0].Length == 0) return new List<Entry>(){new Entry(user, -1, -1)};
|
||||
|
||||
try{
|
||||
List<Entry> entrys = new List<Entry>();
|
||||
for (int i = 0; i < list.Length; i++){
|
||||
entrys.Add(new Entry(user, int.Parse(list[i]), int.Parse(list[i + 1])));
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
return entrys;
|
||||
}catch{
|
||||
throw new GamerServicesLeaderboardException("Error while reading data");
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
140
MrAG/MrAG.csproj
Normal file
140
MrAG/MrAG.csproj
Normal file
@ -0,0 +1,140 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<ProjectGuid>{48B30584-0795-4FDA-9561-61B2381F8656}</ProjectGuid>
|
||||
<ProjectTypeGuids>{6D335F3A-9D43-41b4-9D22-F6F17C4BE596};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>MrAG</RootNamespace>
|
||||
<AssemblyName>MrAG</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
|
||||
<XnaFrameworkVersion>v4.0</XnaFrameworkVersion>
|
||||
<XnaPlatform>Windows</XnaPlatform>
|
||||
<XnaProfile>HiDef</XnaProfile>
|
||||
<XnaCrossPlatformGroupID>4bb35431-98ed-4862-accb-c65d58850ef8</XnaCrossPlatformGroupID>
|
||||
<XnaOutputType>Library</XnaOutputType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\x86\Debug</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE;WINDOWS</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<NoStdLib>true</NoStdLib>
|
||||
<UseVSHostingProcess>false</UseVSHostingProcess>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<XnaCompressContent>false</XnaCompressContent>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\x86\Release</OutputPath>
|
||||
<DefineConstants>TRACE;WINDOWS</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<NoStdLib>true</NoStdLib>
|
||||
<UseVSHostingProcess>false</UseVSHostingProcess>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<XnaCompressContent>true</XnaCompressContent>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Xna.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Xna.Framework.Game, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Xna.Framework.Graphics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Xna.Framework.GamerServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Xna.Framework.Xact, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Xna.Framework.Video, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Xna.Framework.Avatar, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Xna.Framework.Net, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Xna.Framework.Storage, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="mscorlib">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Core">
|
||||
<RequiredTargetFramework>4.0</RequiredTargetFramework>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml.Linq">
|
||||
<RequiredTargetFramework>4.0</RequiredTargetFramework>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Net">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Checksum.cs" />
|
||||
<Compile Include="Config.cs" />
|
||||
<Compile Include="DLLChecker.cs" />
|
||||
<Compile Include="Draw.cs" />
|
||||
<Compile Include="Gui.cs" />
|
||||
<Compile Include="Gui\Base.cs" />
|
||||
<Compile Include="Gui\Button.cs" />
|
||||
<Compile Include="Gui\Checkbox.cs" />
|
||||
<Compile Include="Gui\Dropdown.cs" />
|
||||
<Compile Include="Gui\Dialog.cs" />
|
||||
<Compile Include="Gui\Frame.cs" />
|
||||
<Compile Include="Gui\GroupBox.cs" />
|
||||
<Compile Include="Gui\ImageBox.cs" />
|
||||
<Compile Include="Gui\ImageBoxAnimated.cs" />
|
||||
<Compile Include="Gui\Label.cs" />
|
||||
<Compile Include="Gui\ListBox.cs" />
|
||||
<Compile Include="Gui\ListView.cs" />
|
||||
<Compile Include="Gui\MessageBox.cs" />
|
||||
<Compile Include="Gui\ProgressBar.cs" />
|
||||
<Compile Include="Gui\Slider.cs" />
|
||||
<Compile Include="Gui\SliderV.cs" />
|
||||
<Compile Include="Gui\Textbox.cs" />
|
||||
<Compile Include="Gui\TextBoxAdvanced.cs" />
|
||||
<Compile Include="Leaderboard.cs" />
|
||||
<Compile Include="Overlay.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Gamerservices.cs" />
|
||||
<Compile Include="Achievements.cs" />
|
||||
<Compile Include="KeyBindings.cs" />
|
||||
<Compile Include="Networking.cs" />
|
||||
<Compile Include="Random.cs" />
|
||||
<Compile Include="Serverlist.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\Microsoft.Xna.GameStudio.targets" />
|
||||
<!--
|
||||
To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
1150
MrAG/Networking.cs
Normal file
1150
MrAG/Networking.cs
Normal file
File diff suppressed because it is too large
Load Diff
139
MrAG/Overlay.cs
Normal file
139
MrAG/Overlay.cs
Normal file
@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace MrAG
|
||||
{
|
||||
public class Overlay
|
||||
{
|
||||
public class GamerServicesOverlayException : Exception{
|
||||
public GamerServicesOverlayException(string msg): base(msg){
|
||||
}
|
||||
}
|
||||
|
||||
private class NoticeSystem
|
||||
{
|
||||
public class Popup
|
||||
{
|
||||
public byte MsgID = 0;
|
||||
public byte Icon;
|
||||
public string Title;
|
||||
public string Message;
|
||||
public short DisplayTime = 6000;
|
||||
public double ScrollInTime = 1500;
|
||||
|
||||
private Texture2D PrevImg;
|
||||
private double CreateTime;
|
||||
|
||||
public Popup(byte icon, string text){
|
||||
Icon = icon;
|
||||
Title = "";
|
||||
Message = text;
|
||||
CreateTime = DateTime.Now.TimeOfDay.TotalMilliseconds;
|
||||
}
|
||||
|
||||
public Popup(byte icon, string title, string text){
|
||||
Icon = icon;
|
||||
Title = title;
|
||||
Message = text;
|
||||
CreateTime = DateTime.Now.TimeOfDay.TotalMilliseconds;
|
||||
|
||||
if (icon == 1) {
|
||||
PrevImg = Gamerservices.GetAvatar(title);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Draw(SpriteBatch sb, int y){
|
||||
double restime = Math.Abs(DateTime.Now.TimeOfDay.TotalMilliseconds - CreateTime);
|
||||
if (restime > DisplayTime) return true;
|
||||
|
||||
int x = 0;
|
||||
int alpha = 255;
|
||||
|
||||
if (restime < DisplayTime - ScrollInTime + 1){
|
||||
restime = Math.Min(restime, ScrollInTime);
|
||||
alpha = (int)Math.Min((restime / ScrollInTime) * 256.0, 256);
|
||||
x = WindowWidth - alpha;
|
||||
}else{
|
||||
restime -= DisplayTime - ScrollInTime;
|
||||
alpha = (int)Math.Min((restime / ScrollInTime) * 256.0, 256);
|
||||
x = WindowWidth - 256 + alpha;
|
||||
alpha = 256 - alpha;
|
||||
}
|
||||
|
||||
|
||||
sb.Draw(NoticeTexture, new Rectangle(x - 10, y, 256, 56), new Color(255, 255, 255, alpha));
|
||||
if (PrevImg != null)
|
||||
sb.Draw(PrevImg, new Rectangle(x - 7, y + 3, 50, 50), new Color(255, 255, 255, alpha));
|
||||
|
||||
sb.DrawString(NoticeFont_Title, Title, new Vector2(x + 50, y + 4), new Color(0, 0, 0, alpha));
|
||||
sb.DrawString(NoticeFont_Text, Message, new Vector2(x + 50, y + 25), new Color(0, 0, 0, alpha));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Popup> Popups = new List<Popup>();
|
||||
}
|
||||
|
||||
private static bool EnableNotices;
|
||||
|
||||
private static SpriteFont NoticeFont_Title;
|
||||
private static SpriteFont NoticeFont_Text;
|
||||
private static Texture2D NoticeTexture;
|
||||
//private static Texture2D[] NoticeTypes;
|
||||
|
||||
private static byte CurrentMessageID;
|
||||
public static Delegate OnNotice;
|
||||
public static int WindowWidth = 0;
|
||||
|
||||
public static void LoadContent(Microsoft.Xna.Framework.Content.ContentManager man, string MrAGContentPath, int windowwidth){
|
||||
try{
|
||||
NoticeFont_Title = man.Load<SpriteFont>(MrAGContentPath + "/notice_title");
|
||||
NoticeFont_Text = man.Load<SpriteFont>(MrAGContentPath + "/notice_text");
|
||||
NoticeTexture = man.Load<Texture2D>(MrAGContentPath + "/notice_background");
|
||||
}catch{
|
||||
throw new GamerServicesOverlayException("Could not find overlay files: " + man.RootDirectory + "/" + MrAGContentPath + "/notice_*****");
|
||||
}
|
||||
|
||||
WindowWidth = windowwidth;
|
||||
}
|
||||
|
||||
public static void AddNotice(byte type, string title, string message){
|
||||
NoticeSystem.Popups.Add(new NoticeSystem.Popup(type, title, message));
|
||||
|
||||
CurrentMessageID++;
|
||||
NoticeSystem.Popups[NoticeSystem.Popups.Count - 1].MsgID = CurrentMessageID;
|
||||
}
|
||||
|
||||
public static void AddNotice(byte type, string message){
|
||||
NoticeSystem.Popups.Add(new NoticeSystem.Popup(type, "", message));
|
||||
|
||||
CurrentMessageID++;
|
||||
NoticeSystem.Popups[NoticeSystem.Popups.Count - 1].MsgID = CurrentMessageID;
|
||||
}
|
||||
|
||||
public static void InitilizeSystem(bool noticesenabled){
|
||||
EnableNotices = noticesenabled;
|
||||
if (noticesenabled) NoticeSystem.Popups.Add(new NoticeSystem.Popup(0, "Notice", "Your now online.\nUse alt + ctrl + del for MrAG overlay"));
|
||||
}
|
||||
|
||||
public static void Close(){
|
||||
}
|
||||
|
||||
public static void Draw(SpriteBatch sb){
|
||||
lock (NoticeSystem.Popups){
|
||||
for (int i = 0; i < NoticeSystem.Popups.Count; i++){
|
||||
if (NoticeSystem.Popups[i].Draw(sb, 12 + (i * 68))){
|
||||
NoticeSystem.Popups.RemoveAt(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
34
MrAG/Properties/AssemblyInfo.cs
Normal file
34
MrAG/Properties/AssemblyInfo.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("MrAG")]
|
||||
[assembly: AssemblyProduct("MrAG")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyCompany("Microsoft")]
|
||||
[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type. Only Windows
|
||||
// assemblies support COM.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// On Windows, the following GUID is for the ID of the typelib if this
|
||||
// project is exposed to COM. On other platforms, it unique identifies the
|
||||
// title storage container when deploying this assembly to the device.
|
||||
[assembly: Guid("1364b009-1d68-4e82-9c69-ff390930476a")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
40
MrAG/Random.cs
Normal file
40
MrAG/Random.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace MrAG
|
||||
{
|
||||
public class Random
|
||||
{
|
||||
private static System.Security.Cryptography.RandomNumberGenerator rnd = System.Security.Cryptography.RandomNumberGenerator.Create();
|
||||
|
||||
public static int Next(){
|
||||
byte[] tbl = new byte[1];
|
||||
rnd.GetNonZeroBytes(tbl);
|
||||
|
||||
return tbl[0];
|
||||
}
|
||||
|
||||
public static int Next(int min, int max){
|
||||
byte[] tbl = new byte[1];
|
||||
rnd.GetNonZeroBytes(tbl);
|
||||
|
||||
return (int)((double)tbl[0] / 255.0 * (max - min)) + min;
|
||||
}
|
||||
|
||||
public static double NextDouble(){
|
||||
byte[] tbl = new byte[1];
|
||||
rnd.GetNonZeroBytes(tbl);
|
||||
|
||||
return (double)tbl[0] / 255.0 * 1.0;
|
||||
}
|
||||
|
||||
public static double NextDouble(int min, int max){
|
||||
byte[] tbl = new byte[1];
|
||||
rnd.GetNonZeroBytes(tbl);
|
||||
|
||||
return ((double)tbl[0] / 255.0 * (max - min)) + min;
|
||||
}
|
||||
}
|
||||
}
|
333
MrAG/Serverlist.cs
Normal file
333
MrAG/Serverlist.cs
Normal file
@ -0,0 +1,333 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace MrAG {
|
||||
public class Serverlist {
|
||||
public class GamerServicesServerlistException : Exception{
|
||||
public GamerServicesServerlistException(string msg): base(msg){
|
||||
}
|
||||
}
|
||||
|
||||
public class ServerInfo {
|
||||
public string Servername;
|
||||
public string IP;
|
||||
public string Meta;
|
||||
public int Users;
|
||||
public int MaxUsers;
|
||||
public int Port;
|
||||
}
|
||||
|
||||
public class Server {
|
||||
public string Servername;
|
||||
public int Users;
|
||||
public int MaxUsers;
|
||||
public int Port;
|
||||
public int Timeout;
|
||||
public bool Legit;
|
||||
public bool Started;
|
||||
public byte[] Headers = new byte[] { 1, 3, 3, 7 };
|
||||
|
||||
public Delegate OnIncomingConnection;
|
||||
public Delegate OnFinishedConnection;
|
||||
|
||||
private string Meta;
|
||||
private TcpListener Connection;
|
||||
private MrAG.Networking.TCPClient GSServerConnection;
|
||||
|
||||
private double Lastupdate = 0;
|
||||
|
||||
public void Start() {
|
||||
if (this.Started) this.Connection.Stop();
|
||||
|
||||
this.Started = true;
|
||||
|
||||
this.Connection = new TcpListener(IPAddress.Any, Port);
|
||||
this.Connection.Start();
|
||||
|
||||
if (!this.Legit)
|
||||
return;
|
||||
|
||||
this.Lastupdate = DateTime.Now.TimeOfDay.TotalSeconds;
|
||||
|
||||
this.GSServerConnection = new MrAG.Networking.TCPClient("mrag.nl", 8443);
|
||||
|
||||
this.GSServerConnection.Connection.Start_Send();
|
||||
this.GSServerConnection.Connection.AddInt(0);
|
||||
this.GSServerConnection.Connection.Finish_Send();
|
||||
|
||||
this.GSServerConnection.Connection.Start_Recieve();
|
||||
bool canconnect = this.GSServerConnection.Connection.ReadBool();
|
||||
this.GSServerConnection.Connection.Finish_Recieve();
|
||||
|
||||
if (canconnect) {
|
||||
this.GSServerConnection.Connection.Start_Send();
|
||||
this.GSServerConnection.Connection.AddBool(true);
|
||||
this.GSServerConnection.Connection.Finish_Send();
|
||||
} else {
|
||||
throw new MrAG.Networking.GamerServicesNetworkingException("The masterserver dumped you in the ditch at the side of the road.");
|
||||
}
|
||||
|
||||
this.GSServerConnection.Packets[0] = new Action<MrAG.Networking.Packet>(this.Packet_00); // Register
|
||||
this.GSServerConnection.Packets[1] = new Action<MrAG.Networking.Packet>(this.Packet_01); // Update
|
||||
this.GSServerConnection.Packets[2] = new Action<MrAG.Networking.Packet>(this.Packet_02); // Shutdown/remove
|
||||
this.GSServerConnection.Packets[3] = new Action<MrAG.Networking.Packet>(this.Packet_03); // Update Playerrcount
|
||||
this.GSServerConnection.Packets[4] = new Action<MrAG.Networking.Packet>(this.Packet_04); // Update Meta
|
||||
|
||||
this.GSServerConnection.Connection.Send(0, 0, this.Servername, this.Meta, MrAG.Gamerservices.GetIdentifier(), this.Port, this.MaxUsers);
|
||||
}
|
||||
|
||||
public void Stop() {
|
||||
this.Started = false;
|
||||
this.Connection.Stop();
|
||||
|
||||
if (this.Legit && this.GSServerConnection.Connected) {
|
||||
this.GSServerConnection.Send(2, 1);
|
||||
}
|
||||
}
|
||||
|
||||
public void Close(){
|
||||
this.Stop();
|
||||
|
||||
if (this.GSServerConnection != null)
|
||||
this.GSServerConnection.Close();
|
||||
|
||||
this.GSServerConnection = null;
|
||||
this.Connection = null;
|
||||
}
|
||||
|
||||
public void Update(){
|
||||
if (this.Legit && this.GSServerConnection.Connected) {
|
||||
this.GSServerConnection.Update();
|
||||
|
||||
if (Math.Abs(this.Lastupdate - DateTime.Now.TimeOfDay.TotalSeconds) > 10) {
|
||||
this.GSServerConnection.Send(1, 1);
|
||||
this.Lastupdate = DateTime.Now.TimeOfDay.TotalSeconds;
|
||||
}
|
||||
}
|
||||
|
||||
start:
|
||||
while (Connection.Pending()) {
|
||||
Networking.TCPClient c = new Networking.TCPClient(Connection.AcceptTcpClient());
|
||||
c.Connection.SetTimeout(Timeout);
|
||||
|
||||
try {
|
||||
Networking.IncomingConnection newcon = new Networking.IncomingConnection();
|
||||
newcon.Connection = c;
|
||||
string[] tmpipport = c.Connection.GetClient().Client.RemoteEndPoint.ToString().Split(':');
|
||||
newcon.IP = tmpipport[0];
|
||||
newcon.Port = uint.Parse(tmpipport[1]);
|
||||
|
||||
c.Connection.Start_Recieve();
|
||||
|
||||
foreach(byte header in this.Headers){
|
||||
if (header != c.Connection.ReadByte()){
|
||||
c.Connection.Start_Send();
|
||||
c.Connection.AddString("Invalid Headers");
|
||||
c.Connection.Finish_Send();
|
||||
c.Close();
|
||||
goto start;
|
||||
}
|
||||
}
|
||||
|
||||
newcon.Username = c.Connection.ReadString(64);
|
||||
c.Connection.Finish_Recieve();
|
||||
|
||||
/*if (this.Legit) {
|
||||
try {
|
||||
//this.GSServerConnection.Connection.Send(0, newcon.Username);
|
||||
|
||||
string[] respons = Networking.PostData("serverlist", "sub=check&ip=" + newcon.IP + "&port=" + newcon.Port + "&servername=" + Servername + "&identifier=" + MrAG.Gamerservices.GetIdentifier() + "&user=" + newcon.Username);
|
||||
} catch {
|
||||
c.Connection.Start_Send();
|
||||
c.Connection.AddBool(false);
|
||||
c.Connection.AddText("Error while validating.");
|
||||
c.Connection.Finish_Send();
|
||||
c.Connection.Close();
|
||||
goto start;
|
||||
}
|
||||
}*/
|
||||
|
||||
bool canconnect = true;
|
||||
if (OnIncomingConnection != null) {
|
||||
OnIncomingConnection.DynamicInvoke(newcon);
|
||||
|
||||
if (newcon._KickReason != null) canconnect = newcon._KickReason.Length == 0;
|
||||
}
|
||||
|
||||
double curtime = DateTime.Now.TimeOfDay.TotalMilliseconds;
|
||||
c.Connection.Start_Send();
|
||||
c.Connection.AddBool(canconnect);
|
||||
c.Connection.Finish_Send();
|
||||
if (canconnect) {
|
||||
c.Connection.Start_Recieve();
|
||||
c.Connection.ReadBool();
|
||||
c.Connection.Finish_Recieve();
|
||||
|
||||
newcon.Ping = (int)(DateTime.Now.TimeOfDay.TotalMilliseconds - curtime);
|
||||
|
||||
if (OnFinishedConnection != null) {
|
||||
OnFinishedConnection.DynamicInvoke(newcon);
|
||||
} else {
|
||||
throw new Networking.GamerServicesNetworkingException("Unhandled connection.");
|
||||
}
|
||||
|
||||
} else {
|
||||
c.Connection.Start_Send();
|
||||
c.Connection.AddText(newcon._KickReason);
|
||||
c.Connection.Finish_Send();
|
||||
c.Connection.Close();
|
||||
}
|
||||
|
||||
newcon.FinishedConnecting = true;
|
||||
} catch {
|
||||
c.Connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetMeta(string meta) {
|
||||
this.Meta = meta;
|
||||
|
||||
if (this.Legit)
|
||||
this.GSServerConnection.Send(4, 0, this.Meta);
|
||||
}
|
||||
|
||||
public string GetMeta() {
|
||||
return this.Meta;
|
||||
}
|
||||
|
||||
public void IncreeseUserCount() {
|
||||
this.Users++;
|
||||
this.GSServerConnection.Send(3, 0, this.Users);
|
||||
}
|
||||
|
||||
public void DecreeseUserCount() {
|
||||
this.Users--;
|
||||
this.GSServerConnection.Send(3, 0, this.Users);
|
||||
}
|
||||
|
||||
private void Packet_00(MrAG.Networking.Packet p) {
|
||||
if (!p.ReadBool()) {
|
||||
throw new MrAG.Networking.GamerServicesNetworkingException("Error while attempting to host: " + p.ReadString());
|
||||
}
|
||||
}
|
||||
|
||||
private void Packet_01(MrAG.Networking.Packet p) {
|
||||
if (!p.ReadBool()) {
|
||||
p.Send(0, 0, this.Servername, this.Meta, MrAG.Gamerservices.GetIdentifier(), this.Port, this.MaxUsers);
|
||||
|
||||
p.Send(3, 9, this.Users);
|
||||
}
|
||||
}
|
||||
|
||||
private void Packet_02(MrAG.Networking.Packet p) {
|
||||
p.Send(0, 0, this.Servername, this.Meta, MrAG.Gamerservices.GetIdentifier(), this.Port, this.MaxUsers);
|
||||
p.Send(3, 9, this.Users, this.MaxUsers);
|
||||
}
|
||||
|
||||
private void Packet_03(MrAG.Networking.Packet p) {
|
||||
|
||||
}
|
||||
|
||||
private void Packet_04(MrAG.Networking.Packet p) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static Server Host(string servername, int port, string meta, bool show_in_serverlist) {
|
||||
Server s = new Server();
|
||||
s.Servername = servername;
|
||||
s.Port = port;
|
||||
s.SetMeta(meta);
|
||||
s.Legit = show_in_serverlist;
|
||||
|
||||
s.Start();
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
public static void GetList(Action<List<MrAG.Serverlist.ServerInfo>> callback) {
|
||||
MrAG.Gamerservices.ServerListCallback = callback;
|
||||
MrAG.Gamerservices.GetAPIConnection().Send(7, 0, MrAG.Gamerservices.GetIdentifier());
|
||||
}
|
||||
|
||||
public static MrAG.Networking.TCPClient Connect(string ip, int port) {
|
||||
return Connect(ip, port, new byte[] { 1, 3, 3, 7 });
|
||||
}
|
||||
|
||||
public static MrAG.Networking.TCPClient Connect(string ip, int port, byte[] headers) {
|
||||
//if (legit){
|
||||
// string[] res = MrAG.Networking.PostData("serverlist", "sub=connect&ip" + ip + "&port=" + port + "&identifier=" + MrAG.Gamerservices.GetIdentifier());
|
||||
//}
|
||||
|
||||
MrAG.Networking.TCPClient c = new MrAG.Networking.TCPClient(ip, port);
|
||||
|
||||
c.Connection.Start_Send();
|
||||
foreach (byte b in headers)
|
||||
c.Connection.AddByte(b);
|
||||
|
||||
c.Connection.AddString(MrAG.Gamerservices.GetUser());
|
||||
c.Connection.Finish_Send();
|
||||
|
||||
c.Connection.Start_Recieve();
|
||||
bool canconnect = c.Connection.ReadBool();
|
||||
c.Connection.Finish_Recieve();
|
||||
|
||||
if (canconnect) {
|
||||
c.Connection.Start_Send();
|
||||
c.Connection.AddBool(true);
|
||||
c.Connection.Finish_Send();
|
||||
} else {
|
||||
c.Connection.Start_Recieve();
|
||||
string reason = c.Connection.ReadString();
|
||||
c.Connection.Finish_Recieve();
|
||||
|
||||
throw new MrAG.Networking.GamerServicesNetworkingException("Kicked: " + reason);
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
public static MrAG.Networking.TCPClient Connect(System.Net.Sockets.TcpClient c) {
|
||||
return Connect(c, new byte[] { 1, 3, 3, 7 });
|
||||
}
|
||||
|
||||
public static MrAG.Networking.TCPClient Connect(System.Net.Sockets.TcpClient tcpc, byte[] headers) {
|
||||
//if (legit){
|
||||
// string[] res = MrAG.Networking.PostData("serverlist", "sub=connect&ip" + ip + "&port=" + port + "&identifier=" + MrAG.Gamerservices.GetIdentifier());
|
||||
//}
|
||||
|
||||
MrAG.Networking.TCPClient c = new MrAG.Networking.TCPClient(tcpc);
|
||||
|
||||
c.Connection.Start_Send();
|
||||
foreach (byte b in headers)
|
||||
c.Connection.AddByte(b);
|
||||
|
||||
c.Connection.AddString(MrAG.Gamerservices.GetUser());
|
||||
c.Connection.Finish_Send();
|
||||
|
||||
c.Connection.Start_Recieve();
|
||||
bool canconnect = c.Connection.ReadBool();
|
||||
c.Connection.Finish_Recieve();
|
||||
|
||||
if (canconnect) {
|
||||
c.Connection.Start_Send();
|
||||
c.Connection.AddBool(true);
|
||||
c.Connection.Finish_Send();
|
||||
} else {
|
||||
c.Connection.Start_Recieve();
|
||||
string reason = c.Connection.ReadString();
|
||||
c.Connection.Finish_Recieve();
|
||||
|
||||
throw new MrAG.Networking.GamerServicesNetworkingException("Kicked: " + reason);
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user