Advertisement
Guest User

Better Mod GUI mod (Besiege)

a guest
May 30th, 2015
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using UnityEngine;
  8. using spaar;
  9.  
  10. namespace meta{
  11.  
  12.  
  13.     [spaar.Mod("BetterModGUILoader",author="ITR",version="1.0")]
  14.  
  15.     public class BetterModGUILoader : MonoBehaviour{
  16.         public BetterModGUILoader(){
  17.             Debug.Log("Better Mod GUI mod loaded");
  18.         }
  19.  
  20.         void Start(){
  21.             gameObject.AddComponent<BetterModGUI>();
  22.         }
  23.  
  24.     }
  25.  
  26.     public class BetterModGUI : MonoBehaviour{
  27.  
  28.         List<Rect> rectList = new List<Rect>();
  29.         List<bool> hideOptions = new List<bool>();
  30.         private List<bool> forceClose = new List<bool>();
  31.         private List<int> forceOpen = new List<int>();
  32.        
  33.         private string key = "right ctrl";
  34.         private bool keyBool = false;
  35.         private string close = "x";
  36.         private bool closeBool = false;
  37.         private string open = "o";
  38.         private bool openBool = false;
  39.         private string f = "f";
  40.         private bool fBool = false;
  41.  
  42.         private bool moved = false;
  43.         private int chosen = -1;
  44.         private float x = 0;
  45.         private float y = 0;
  46.  
  47.         private bool fontWindowActive;
  48.         private bool fontsInitalized;
  49.  
  50.         private bool settingsWindowActive = false;
  51.  
  52.         private Font defaultFont;
  53.         private Font GOSTCommonFont;
  54.         private Font GOSTCommonItalicFont;
  55.  
  56.         void Start()
  57.         {
  58.             string path = Path.GetDirectoryName(Application.dataPath)+"/ModGUISettings.txt";
  59.             if (!File.Exists(path)){
  60.                 string[] lines = { "Move GUI button", "right ctrl", "Open GUI settings", "o", "Open font settings", "f", "Close GUI", "x", "EndOfFile"};
  61.                 System.IO.File.WriteAllLines(path, lines);
  62.                 Debug.Log("No settings found, created new ones in " + path);
  63.             }
  64.  
  65.             using (StreamReader sr = File.OpenText(path)){
  66.                 string s = "";
  67.                 int c = 0;
  68.                 while ((s = sr.ReadLine()) != null && c < 4){
  69.                     if (c==1){
  70.                         this.key = s.ToLower();
  71.                         try{
  72.                             Input.GetKey(key);
  73.                         }
  74.                         catch{
  75.                             this.key = "right ctrl";
  76.                         }
  77.                         if(this.key=="left ctrl")
  78.                             this.key = "right ctrl";
  79.                     }
  80.                     else if(c==3){
  81.                         this.open = s.ToLower();
  82.                         try{
  83.                             Input.GetKey(open);
  84.                         }
  85.                         catch{
  86.                             this.open = "o";
  87.                         }
  88.                     }
  89.                     else if(c==5){
  90.                         this.f = s.ToLower();
  91.                         try{
  92.                             Input.GetKey(f);
  93.                         }
  94.                         catch{
  95.                             this.f = "f";
  96.                         }
  97.                     }
  98.                     else if(c==7){
  99.                         this.close = s.ToLower();
  100.                         try{
  101.                             Input.GetKey(close);
  102.                         }
  103.                         catch{
  104.                             this.close = "x";
  105.                         }
  106.                     }
  107.                     c++;
  108.                 }
  109.             }
  110.             Debug.Log("Started Better Mod GUI mod with moveGUI key \"" + this.key + "\", Open GUI settings \""+open+"\", Open font settings \""+f+"\", Close GUI \""+close+'"');
  111.             string[] lines2 = { "Move GUI button",key,"Open GUI settings",open,"Open font settings",f,"Close GUI",close,"EndOfFile" };
  112.             try {
  113.                 System.IO.File.WriteAllLines(path,lines2);
  114.             }
  115.             catch {
  116.                 Debug.Log("Failed to save BMGUI settings");
  117.             }
  118.         }
  119.  
  120.         private Rect CreateRect(int n){
  121.             Rect rect = rectList[n];
  122.             float h = 0;
  123.             float ph = 58;
  124.             float w = 0;
  125.             for(int i = 0; i<rectList.Count&&i<n; i++){
  126.                 if(rectList[i].width==0||rectList[i].height==0||ph+rectList[i].height>=Screen.height-10)
  127.                     continue;
  128.                 w += rectList[i].width;
  129.                 if(w>=Screen.width-10){
  130.                     w = rectList[i].width;
  131.                     ph += h;
  132.                     h = rectList[i].height;
  133.                 }
  134.                 if(h<rectList[i].height){
  135.                     h = rectList[i].height;                
  136.                 }
  137.             }
  138.             if(w+rect.width>Screen.width-10){
  139. //              Debug.Log("Window not wide enough");
  140.                 w = 0;
  141.                 ph += h;
  142.             }
  143.             if(ph+rect.height>Screen.height-10){
  144. //              Debug.Log("Window not high enough");
  145.                 ph = 0;
  146.                 w = 0;
  147.             }
  148.             rect = new Rect(w,ph,rect.width,rect.height);
  149.             return rect;
  150.         }
  151.  
  152.         public bool CreateArea(float width, float height, bool settings, int n){
  153.             if (rectList.Count <= n || n < 0)
  154.             {
  155.                 return false;
  156.             }
  157.             if(moved){
  158.                 rectList[n] = new Rect(rectList[n].xMin,rectList[n].yMin,width,height);
  159.                 hideOptions[n] = settings;
  160.                 return true;
  161.             }
  162.             rectList[n] = new Rect(0, 0, width, height);
  163.             rectList[n] = CreateRect(n);
  164.             hideOptions[n] = settings;
  165.             if (!this.moved)
  166.             {
  167.                 CreateArea(n + 1);
  168.             }
  169.             return true;
  170.         }
  171.        
  172.         public bool CreateArea(float width, float height, int n){
  173.             return CreateArea(width,height,false,n);
  174.         }
  175.  
  176.         public int CreateArea(float width, float height, bool settings) {
  177.             int n = rectList.Count;
  178.             rectList.Add(new Rect(0,0,width,height));
  179.             hideOptions.Add(settings);
  180.             forceClose.Add(false);
  181.             rectList[n] = CreateRect(n);
  182.             return n;
  183.         }
  184.  
  185.         public int CreateArea(float width, float height){
  186.             return CreateArea(width,height,false);
  187.         }
  188.         private void CreateArea(int n) {
  189.             if(rectList.Count<=n||n<0){
  190.                 return;
  191.             }
  192.             rectList[n] = CreateRect(n);
  193.             CreateArea(n+1);
  194.             return;
  195.         }
  196.  
  197.         public bool DeleteArea(int n){
  198.             if(rectList.Count<=n||n<0){
  199.                 return false;
  200.             }
  201.             if(rectList[n].height==0||rectList[n].width==0){
  202.                 return false;
  203.             }
  204.  
  205.             rectList[n] = new Rect(rectList[n].xMin,rectList[n].yMin,0,0);
  206.             if(!this.moved){
  207.                 CreateArea(n+1);
  208.             }
  209.             return true;
  210.         }
  211.  
  212.         public Rect GetRect(int n){
  213.             if(n<0||rectList.Count<=n||hideOptions.Count<=n||forceClose.Count<=n){
  214.                 Debug.Log("Error! Tried to get nonexisting ID "+n);
  215.                 return new Rect(0,0,0,0);
  216.             }
  217.             if(forceClose[n])
  218.                 return new Rect(0,0,0,0);
  219.             HudInputControl hudInputControl = UnityEngine.Object.FindObjectOfType<HudInputControl>();
  220.             if(hudInputControl!=null){
  221.                 if((!hudInputControl.hudCam.enabled&&!hideOptions[n])){
  222.                     return new Rect(0,0,0,0);
  223.                 }
  224.             }
  225. //          Debug.Log(rectList[n].ToString());
  226.             return rectList[n];
  227.         }
  228.  
  229.         public Rect GetRekt(int n){
  230.             return GetRect(n);
  231.         }
  232.  
  233.         void Update(){
  234.             if(!Input.GetKey(key)) {
  235.                 keyBool = false;
  236.                 closeBool = false;
  237.                 openBool = false;
  238.                 fBool = false;
  239.             }
  240.  
  241.             if (Input.GetKey(this.key) && Input.GetKey(this.f)){
  242.                 if(!fBool)
  243.                     fontWindowActive = !fontWindowActive;
  244.                 this.fBool= true;
  245.             }
  246.             else{
  247.                 this.fBool = false;
  248.             }
  249.             if(Input.GetKey(this.key)&&Input.GetKey(this.open)){
  250.                 if(!this.openBool) {
  251. //                  this.settingsWindowActive = !this.settingsWindowActive;
  252.                     for(int i = 0; i<forceClose.Count; i++){
  253.                         forceClose[i] = false;
  254.                     }
  255.                 }
  256.                 this.openBool = true;
  257.             }
  258.             else {
  259.                 this.openBool = false;
  260.             }
  261.             if(Input.GetKey(this.key)&&Input.GetKey(this.close)){
  262.                 if(!this.closeBool){
  263.                     for(int i = rectList.Count-1; i>=0; i--){
  264.                         Rect rect = rectList[i];
  265.                         if(rect.width==0||rect.height==0||forceClose[i])
  266.                             continue;
  267.                         if(Input.mousePosition.x>rect.xMin&&Input.mousePosition.x<rect.xMax){
  268.                             if(Screen.height-Input.mousePosition.y>rect.yMin&&Screen.height-Input.mousePosition.y<rect.yMax){
  269.                                 forceClose[i] = true;
  270.                                 break;
  271.                             }
  272.                         }
  273.                     }
  274.                 }
  275.                 this.closeBool = true;
  276.             }
  277.             else {
  278.                 this.closeBool = false;
  279.             }
  280.             if (Input.GetKey(this.key)){
  281.                 if(chosen==-1){
  282.                     if(!keyBool) {
  283.                         keyBool = true;
  284.                         for(int i = rectList.Count-1; i>=0; i--){
  285.                             Rect rect = rectList[i];
  286.                             if(rect.width==0||rect.height==0||forceClose[i])
  287.                                 continue;
  288.                             if(Input.mousePosition.x>rect.xMin&&Input.mousePosition.x<rect.xMax){
  289.                                 if(Screen.height-Input.mousePosition.y>rect.yMin&&Screen.height-Input.mousePosition.y<rect.yMax){
  290.                                     chosen = i;
  291.                                     x = Input.mousePosition.x;
  292.                                     y = Input.mousePosition.y;
  293.                                     keyBool = false;
  294.                                     break;
  295.                                 }
  296.                             }
  297.                         }
  298.                     }
  299.                 }
  300.                 else{
  301.                     keyBool = false;
  302.                     float dx = Input.mousePosition.x - x;
  303.                     float dy = Input.mousePosition.y - y;
  304.                     x = Input.mousePosition.x;
  305.                     y = Input.mousePosition.y;
  306.                     if (dx != 0 || dy != 0){
  307.                         float rx = rectList[chosen].xMin+dx;
  308.                         float ry = rectList[chosen].yMin-dy;
  309.                         float rX = rectList[chosen].xMax+dx;
  310.                         float rY = rectList[chosen].yMax-dy;
  311.                         this.moved = true;
  312.                         if(rx>=Screen.width-10){
  313.                             rx = Screen.width-11;
  314.                         }
  315.                         if(ry>=Screen.height-10){
  316.                             ry = Screen.height-11;
  317.                         }
  318.                         if(rX<10){
  319.                             rx += 10-rX;
  320.                         }
  321.                         if(rY<10){
  322.                             ry += 10-rY;
  323.                         }
  324.                         rectList[chosen] = new Rect(rx,ry,rectList[chosen].width,rectList[chosen].height);
  325.                     }
  326.                 }
  327.  
  328.             }
  329.             else{
  330.                 this.chosen = -1;
  331.             }
  332.             while(forceOpen.Count>0) {
  333.                 int count = forceOpen.Count-1;
  334.                 forceClose[count] = false;
  335.                 forceOpen.RemoveAt(count);
  336.             }
  337.         }
  338.  
  339.         void OnGUI(){
  340.             //Debug.Log(GUI.skin.font.name);
  341.             if (!fontsInitalized){
  342.                 defaultFont = GUI.skin.font;
  343.                 foreach (Font f in Resources.FindObjectsOfTypeAll(typeof(Font))){
  344.                     if (f.name == "GOST Common")
  345.                         GOSTCommonFont = f;
  346.                     if (f.name == "GOST Common Italic")
  347.                         GOSTCommonItalicFont = f;
  348.                     fontsInitalized = true;
  349.                 }
  350.             }
  351.  
  352.             if (fontWindowActive)
  353.                 GUI.Window(1, new Rect(0, 0, Screen.width / 4, Screen.height / 6), FontWindow, "Choose Font");
  354.             if(settingsWindowActive) {
  355.                 GUILayout.BeginArea(new Rect(0,58,Screen.width,Screen.height-58));
  356.                     for(int i = 0; i<forceClose.Count; i++){
  357.                         if(forceClose[i]) {
  358.                             if(GUILayout.Button(rectList[i].ToString())) {
  359.                                 forceOpen.Add(i);
  360.                             }
  361.                         }
  362.                     }
  363.                 GUILayout.EndArea();
  364.             }
  365.             if (Event.current.keyCode == KeyCode.Tab){
  366.                 GUIUtility.keyboardControl = 0;
  367.             }
  368.         }
  369.  
  370.         void FontWindow(int windowID){
  371.             int h = (Screen.height / 6) / 3 - 5;
  372.             if (GUI.Button(new Rect(0, 15, Screen.width / 4, h), "Default"))
  373.                 GUI.skin.font = defaultFont;
  374.             if (GUI.Button(new Rect(0, h + 15, Screen.width / 4, h), "Besiege Font"))
  375.                 GUI.skin.font = GOSTCommonFont;
  376.             if (GUI.Button(new Rect(0, h * 2 + 15, Screen.width / 4, h), "Besiege Font Italic"))
  377.                 GUI.skin.font = GOSTCommonItalicFont;
  378.         }      
  379.     }
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement