using System; using System.IO; using System.Linq; using UnityEngine; using System.Collections.Generic; public class CTAGUI : MonoBehaviour { private ChallengeThemAll challengeThemAll; private List pList = new List(); private int tab; private bool ShowWindow; private Rect principalWindow = new Rect(Screen.width/2 - 350, Screen.height/2 - 150, 700, 300); private Rect loginWindow = new Rect(Screen.width/2 - 115, Screen.height/2 - 55, 230, 140); private bool redim; private Vector2 scroll; private Vector2 scroll2; //MISSION LIST VAR private IEnumerable ordered; private int order = 7; //0 name, 1 aname, 2 creator, 3 acreator, 4 quality, 5 aquality, 6 creationdate, 7 adate private Vector2 scroll3; private Vector2 scroll4; //MISSION INFO VAR private int mts; public int missiontoshow { get { return mts; } set { mts = value; tab = 7; } } ; //CREATE MISSION VAR private MissionData mission = new MissionData(); private int menu = -1; private int showedCond = -1; private string lastPassword = ""; private List messages = new List(); private string message = ""; public CTAGUI(ChallengeThemAll challengeThemAll) { this.challengeThemAll = challengeThemAll; } public void OnGUI() { GUI.skin = HighLogic.Skin; GUI.Window(1956, new Rect(Screen.width /2+300, Screen.height - 80, 300, 80), DrawHud, "Missions"); if (ShowWindow) { if (StaticData.isLogged) principalWindow = GUI.Window(1957, principalWindow, DrawPrincipalGui, ""); else loginWindow = GUI.Window(1957, loginWindow, DrawLogginGui, ""); } ShowWindow = GUI.Toggle(new Rect(Screen.width - 150, Screen.height - 230, 150, 30), ShowWindow, "ChallengeThemAll", GUI.skin.button); } private void DrawPrincipalGui(int id) { /* DRAWING ACTUAL WINDOW */ GUI.BeginGroup(new Rect(140, 20, principalWindow.width - 145, principalWindow.height - 25), GUI.skin.box); switch (tab) { case 1: ProfileGui(); break; case 2: ChatGui(); break; case 6: MissionListGui(); break; case 7: MissionInfoGui(); break; case 8: CreateMissionGui(); break; } GUI.EndGroup(); /* DRAW WINDOW SELECTOR */ GUI.BeginGroup(new Rect(5, 20, 135, principalWindow.height - 25), GUI.skin.box); if (GUI.Button(new Rect(5, 5, 126, 25), "Home")) tab = 0; else if (GUI.Button(new Rect(5, 5 + 25, 126, 25), "Profile")) tab = 1; else if (GUI.Button(new Rect(5, 5 + 25*2, 126, 25), "Chat")) tab = 2; else if (GUI.Button(new Rect(5, 5 + 25*3, 126, 25), "Mission list")) tab = 6; else if (GUI.Button(new Rect(5, 5 + 25*5, 126, 25), "Mission info")) tab = 7; else if (GUI.Button(new Rect(5, 5 + 25*8, 126, 25), "Create a mission")) tab = 8; else if (GUI.Button(new Rect(5, 5 + 25 * 9, 126, 25), "Disconnect")) challengeThemAll.disconnect(); GUI.EndGroup(); /* RESIZING SYSTEM */ if (Input.mousePosition.x >= principalWindow.x + principalWindow.width - 10 && Input.mousePosition.x <= principalWindow.x + principalWindow.width && Screen.height - Input.mousePosition.y >= principalWindow.y + principalWindow.height - 10 && Screen.height - Input.mousePosition.y <= principalWindow.y + principalWindow.height) { redim = Input.GetMouseButton(0); } else { if (redim) { redim = Input.GetMouseButton(0); principalWindow.width = Mathf.Max(700, Input.mousePosition.x - principalWindow.x); principalWindow.height = Mathf.Max(Screen.height - Input.mousePosition.y - principalWindow.y, 300); } else GUI.DragWindow(); } } private void MissionInfoGui() { GUI.Label(new Rect(5, 5, 100, 20), StaticData.missionList[missiontoshow].missionName); if(GUI.Button(new Rect(5, 30, 40, 40), "Accept")) { } } private void MissionListGui() { int width = (int) (principalWindow.width - 145); /* ORDERING */ if (GUI.Button(new Rect(0, 0, width/4, 30), "Name")) { order = order == 0 ? 1 : 0; onMissionListModified(); } if (GUI.Button(new Rect(width/4, 0, width/4, 30), "Creator")) { order = order == 2 ? 3 : 2; onMissionListModified(); } if (GUI.Button(new Rect(width/4*2, 0, width/4, 30), "Quality")) { order = order == 4 ? 5 : 4; onMissionListModified(); } if (GUI.Button(new Rect(width/4*3, 0, width/4, 30), "Date")) { order = order == 6 ? 7 : 6; onMissionListModified(); } /* DRAWING LIST */ GUI.BeginScrollView(new Rect(0, 30, width, principalWindow.height - 55), scroll3, new Rect(0, 0, width, ordered.Count()*30)); for (int i = 0; i < ordered.Count(); i++) { MissionData m = StaticData.missionList[ordered.ElementAtOrDefault(i)]; GUI.BeginGroup(new Rect(0, i*30, width, 30), GUI.skin.box); if (GUI.Button(new Rect(0, 0, width, 30), "")) requestmissioninfo(m.missionId); GUI.Label(new Rect(3, 5, 100, 20), m.missionName); GUI.Label(new Rect(width/4, 5, 100, 20), m.creatorName); GUI.Label(new Rect(width/4*2, 5, 100, 20), m.rating + ""); GUI.Label(new Rect(width/4*3, 5, 100, 20), m.startTime + ""); GUI.EndGroup(); } GUI.EndScrollView(); } private void CreateMissionGui() { int width = (int) (principalWindow.width - 145)/2, heigth = (int) (principalWindow.height - 25); /* LEFT PANEL */ GUI.BeginGroup(new Rect(0, 0, width, heigth), GUI.skin.box); /* NAME AND DESCRIPTION */ GUI.skin.textField.padding.top = 7; GUI.SetNextControlName("mn"); mission.missionName = GUI.TextField(new Rect(5, 5, width - 10, 30), mission.missionName); GUI.skin.textField.padding.top = 3; if (mission.missionName.Length == 0 && GUI.GetNameOfFocusedControl() != "mn") GUI.Label(new Rect(11, 10, 300, 30), "Mission name"); if (mission.missionDesc.Length == 0 && GUI.GetNameOfFocusedControl() != "md") GUI.Label(new Rect(11, 42, 300, 30), "Description"); GUI.SetNextControlName("md"); mission.missionDesc = GUI.TextArea(new Rect(5, 37, width - 10, heigth - 137), mission.missionDesc); /* RIGHT PANEL SELECTOR */ menu = GUI.Button(new Rect(5 + width/4*0, heigth - 95, width/4 - 10, 30), GetTargetButton(1)) ? 0 : GUI.Button(new Rect(5 + width/4*1, heigth - 95, width/4 - 10, 30), GetTargetButton(2)) ? 1 : GUI.Button(new Rect(5 + width/4*2, heigth - 95, width/4 - 10, 30), GetTargetButton(3)) ? 2 : GUI.Button(new Rect(5 + width/4*3, heigth - 95, width/4 - 10, 30), GetTargetButton(4)) ? 3 : menu; if (menu != 10 && menu != -1 && mission.missionTarget[menu] == null) mission.missionTarget[menu] = new MissionTarget(); /* DIFFICULTY */ int startToggle = GUI.skin.toggle.padding.left; GUI.skin.toggle.padding.left = 10; if (GUI.Toggle(new Rect(0, heigth - 30, 60, 20), mission.difficulty == 0, "Easy")) mission.difficulty = 0; if (GUI.Toggle(new Rect(60, heigth - 30, 80, 20), mission.difficulty == 1, "Medium")) mission.difficulty = 1; if (GUI.Toggle(new Rect(140, heigth - 30, 60, 20), mission.difficulty == 2, "Hard")) mission.difficulty = 2; GUI.skin.toggle.padding.left = startToggle; /* CONTROLS BUTTON */ if (GUI.Button(new Rect(width - 160, heigth - 60, 80, 30), "Condition")) menu = 10; if (GUI.Button(new Rect(width - 80, heigth - 60, 75, 30), "Test")) { } if (GUI.Button(new Rect(width - 65, heigth - 30, 60, 30), "Create")) { } GUI.Label(new Rect(5, heigth - 55, 300, 20), "Duration :"); int result; if ( int.TryParse( "0" + GUI.TextField(new Rect(70, heigth - 60, 40, 25), mission.missionTime == 0 ? "" : mission.missionTime + ""), out result)) mission.missionTime = result; GUI.EndGroup(); /* RIGHT PANEL */ GUI.BeginGroup(new Rect(width, 0, width + 1, heigth), GUI.skin.box); /* CONDITIONS */ if (menu == 10) { GUI.Label(new Rect(width/2 - 35, 5, 70, 20), "Conditions"); if (GUI.Button(new Rect(3, 3, 15, 15), "+")) { new CSelector("Choose condition", Condition.conditionsType, b => mission.conditionList.Add(new Condition(b))).OpenWindow(); } scroll4 = GUI.BeginScrollView(new Rect(5, 30, width - 10, heigth - 35), scroll4, new Rect(0, 0, width - 25, mission.conditionList.Count*30)); for (int i = 0; i < mission.conditionList.Count; i++) { Condition cond = mission.conditionList[i]; GUI.BeginGroup(new Rect(0, i*30, width - 30, 30), GUI.skin.box); GUI.enabled = i != showedCond; if (GUI.Button(new Rect(0, 0, 20, 30), "-")) { mission.conditionList.RemoveAt(i); i--; continue; } if (GUI.Button(new Rect(20, 0, (width - 50), 30), cond.GetDisplayString())) { new ConditionParameter(mission, i, () => showedCond = -1).OpenWindow(); showedCond = i; } GUI.enabled = true; GUI.EndGroup(); } GUI.EndScrollView(); } /* TARGET */ else if (menu != -1) { MissionTarget target = mission.missionTarget[menu]; GUI.enabled = !target.isKerbal; if (GUI.Button(new Rect(width/2, 10, 70, 25), "Kerbal")) target.isKerbal = true; GUI.enabled = !GUI.enabled; if (GUI.Button(new Rect(width/2 - 70, 10, 70, 25), "Vessel")) target.isKerbal = false; GUI.enabled = true; /* KERBAL */ if (target.isKerbal) { GUI.Label(new Rect(5, 60, 100, 20), "Celestial body :"); if (GUI.Button(new Rect(100, 62, width - 110, 20), target.celestialBody)) new Selector("Select celestial body", FlightGlobals.Bodies, s => s.name, body => mission.missionTarget[menu].celestialBody = body.name). OpenWindow(); GUI.Label(new Rect(5, 85, 100, 20), "Target name :"); target.targetName = GUI.TextField(new Rect(90, 85, width - 100, 20), target.targetName); float a; GUI.Label(new Rect(5, 110, 100, 20), "Apoapsis :"); if ( float.TryParse( "0" + GUI.TextField(new Rect(70, 111, 100, 20), target.SMA_long.ToString().Contains(".") ? target.SMA_long + "" : target.SMA_long + ".0"), out a)) target.SMA_long = a; GUI.Label(new Rect(5, 135, 100, 20), "Eccentricity :"); if ( float.TryParse( "0" + GUI.TextField(new Rect(86, 136, 100, 20), target.eccentricity.ToString().Contains(".") ? target.eccentricity + "" : target.eccentricity + ".0"), out a)) target.eccentricity = a; GUI.Label(new Rect(5, 160, 100, 20), "Inclinaison :"); if ( float.TryParse( "0" + GUI.TextField(new Rect(80, 161, 100, 20), target.inclinaison_lat.ToString().Contains(".") ? target.inclinaison_lat + "" : target.inclinaison_lat + ".0"), out a)) target.inclinaison_lat = a; GUI.Label(new Rect(5, 185, 100, 20), "LAN :"); if ( float.TryParse( "0" + GUI.TextField(new Rect(45, 185, 100, 20), target.LAN.ToString().Contains(".") ? target.LAN + "" : target.LAN + ".0"), out a)) target.LAN = a; } else /* VESSEL */ { GUI.Label(new Rect(5, 38, 100, 20), "Vessel : "); if (GUI.Button(new Rect(60, 40, width - 70, 20), target.filepath)) { new Selector("Select vessel", new DirectoryInfo(KSPUtil.ApplicationRootPath + "/saves/" + HighLogic.SaveFolder + "/Ships/VAB/").GetFiles("*.craft"). Select(fi => fi.Name).ToList() , s => s.ToString(), s => mission.missionTarget[menu].filepath = s).OpenWindow(); } GUI.Label(new Rect(5, 60, 100, 20), "Celestial body :"); if (GUI.Button(new Rect(100, 62, width - 110, 20), target.celestialBody)) new Selector("Select celestial body", FlightGlobals.Bodies, s => s.name, body => mission.missionTarget[menu].celestialBody = body.name). OpenWindow(); GUI.Label(new Rect(5, 85, 100, 20), "Target name :"); target.targetName = GUI.TextField(new Rect(90, 85, width - 100, 20), target.targetName); float a; GUI.Label(new Rect(5, 110, 100, 20), "Apoapsis :"); if ( float.TryParse( "0" + GUI.TextField(new Rect(70, 111, 100, 20), target.SMA_long.ToString().Contains(".") ? target.SMA_long + "" : target.SMA_long + ".0"), out a)) target.SMA_long = a; GUI.Label(new Rect(5, 135, 100, 20), "Eccentricity :"); if ( float.TryParse( "0" + GUI.TextField(new Rect(86, 136, 100, 20), target.eccentricity.ToString().Contains(".") ? target.eccentricity + "" : target.eccentricity + ".0"), out a)) target.eccentricity = a; GUI.Label(new Rect(5, 160, 100, 20), "Inclinaison :"); if ( float.TryParse( "0" + GUI.TextField(new Rect(80, 161, 100, 20), target.inclinaison_lat.ToString().Contains(".") ? target.inclinaison_lat + "" : target.inclinaison_lat + ".0"), out a)) target.inclinaison_lat = a; GUI.Label(new Rect(5, 185, 100, 20), "LAN :"); if ( float.TryParse( "0" + GUI.TextField(new Rect(45, 185, 100, 20), target.LAN.ToString().Contains(".") ? target.LAN + "" : target.LAN + ".0"), out a)) target.LAN = a; } } GUI.EndGroup(); } private string GetTargetButton(int id) { if (mission.missionTarget[id - 1] != null) { if (mission.missionTarget[id - 1].isKerbal) return "Kerbal " + id; return "Vessel " + id; } return "Empty"; } private void ProfileGui() { GUI.Label(new Rect(10, 5, 400, 20), "Username : " + StaticData.username); GUI.Label(new Rect(10, 25, 400, 20), "Rank : " + StaticData.rank); GUI.Label(new Rect(10, 45, 400, 20), "Missions completed : " + StaticData.missionAccomplished); } private void ChatGui() { /* DRAW MESSAGES */ GUILayout.BeginArea(new Rect(0, 0, principalWindow.width - 290, principalWindow.height - 45)); scroll = GUILayout.BeginScrollView(scroll); foreach (string messagea in messages) { GUILayout.BeginHorizontal(); GUILayout.Label(messagea); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.Space(-10); } GUILayout.Space(5); GUILayout.EndScrollView(); GUILayout.EndArea(); /* TEXT FIELD */ GUI.SetNextControlName("textmsg"); message = GUI.TextField(new Rect(0, principalWindow.height - 45, principalWindow.width - 290, 20), message); if (Event.current.isKey && Event.current.keyCode == KeyCode.Return && GUI.GetNameOfFocusedControl() == "textmsg") { if (message.Length > 0) SendMessage(-1, message); message = ""; } /* PLAYERNAMES */ GUILayout.BeginArea(new Rect(principalWindow.width - 290, 0, 145, principalWindow.height - 25), GUI.skin.box); scroll2 = GUILayout.BeginScrollView(scroll2); foreach (string pname in pList) { GUILayout.BeginHorizontal(); GUILayout.Label(pname); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.Space(-10); } GUILayout.Space(5); GUILayout.EndScrollView(); GUILayout.EndArea(); } private void DrawLogginGui(int id) { GUI.BeginGroup(new Rect(loginWindow.width/2 - 110, 20, 220, 110)); GUI.Label(new Rect(10, 0, 100, 30), "Username : "); StaticData.username = GUI.TextField(new Rect(10, 20, 200, 18), StaticData.username); GUI.Label(new Rect(10, 40, 220, 30), lastPassword.Length > 0 ? "Please retype your password :" : "Password : "); StaticData.password = GUI.PasswordField(new Rect(10, 60, 200, 18), StaticData.password, 'ยค', 25); if (GUI.Button(new Rect(10, 80, 100, 30), "Create account")) { if (lastPassword.Length == 0) { lastPassword = StaticData.password; StaticData.password = ""; } else { if (StaticData.password == lastPassword) { StaticData.createAccount = true; challengeThemAll.connectToServer(); } else { lastPassword = ""; StaticData.password = ""; } } } if (GUI.Button(new Rect(110, 80, 100, 30), "Connect")) { StaticData.createAccount = false; challengeThemAll.connectToServer(); } GUI.EndGroup(); } private void DrawHud(int id) { } public void addChatMessage(String message) { messages.Add(message); } public void onMissionListModified() { List toOrder = new List(); foreach (int key in StaticData.missionList.Keys) toOrder.Add(key); switch (order) { case 0: ordered = toOrder.OrderBy(test => StaticData.missionList[test].missionName); break; case 1: ordered = toOrder.OrderByDescending(test => StaticData.missionList[test].missionName); break; case 2: ordered = toOrder.OrderBy(test => StaticData.missionList[test].creatorName); break; case 3: ordered = toOrder.OrderByDescending(test => StaticData.missionList[test].creatorName); break; case 4: ordered = toOrder.OrderBy(test => StaticData.missionList[test].rating); break; case 5: ordered = toOrder.OrderByDescending(test => StaticData.missionList[test].rating); break; case 6: ordered = toOrder.OrderBy(test => StaticData.missionList[test].startTime); break; case 7: ordered = toOrder.OrderByDescending(test => StaticData.missionList[test].startTime); break; } } private void SendMessage(int roomId, String message) { challengeThemAll.getServerManager().sendPacket(new Packet5ChatMessage(roomId, message)); } private void requestmissioninfo(int missionId) { } public void generalPlayerListReceived(Packet10PlayerList data) { Logger.instance.log("received player list !"); switch (data.mode) { case 0: pList.Remove(data.playerList[0]); break; case 1: pList.Add(data.playerList[0]); break; case 2: pList.Clear(); for (int i = 0; i < data.playerList.Length; i++) { pList.Add(data.playerList[i]); } break; } } }