Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using UnityEngine;
- using spaar;
- namespace meta{
- [spaar.Mod("BetterModGUILoader",author="ITR",version="1.0")]
- public class BetterModGUILoader : MonoBehaviour{
- public BetterModGUILoader(){
- Debug.Log("Better Mod GUI mod loaded");
- }
- void Start(){
- gameObject.AddComponent<BetterModGUI>();
- }
- }
- public class BetterModGUI : MonoBehaviour{
- List<Rect> rectList = new List<Rect>();
- List<bool> hideOptions = new List<bool>();
- private List<bool> forceClose = new List<bool>();
- private List<int> forceOpen = new List<int>();
- private string key = "right ctrl";
- private bool keyBool = false;
- private string close = "x";
- private bool closeBool = false;
- private string open = "o";
- private bool openBool = false;
- private string f = "f";
- private bool fBool = false;
- private bool moved = false;
- private int chosen = -1;
- private float x = 0;
- private float y = 0;
- private bool fontWindowActive;
- private bool fontsInitalized;
- private bool settingsWindowActive = false;
- private Font defaultFont;
- private Font GOSTCommonFont;
- private Font GOSTCommonItalicFont;
- void Start()
- {
- string path = Path.GetDirectoryName(Application.dataPath)+"/ModGUISettings.txt";
- if (!File.Exists(path)){
- string[] lines = { "Move GUI button", "right ctrl", "Open GUI settings", "o", "Open font settings", "f", "Close GUI", "x", "EndOfFile"};
- System.IO.File.WriteAllLines(path, lines);
- Debug.Log("No settings found, created new ones in " + path);
- }
- using (StreamReader sr = File.OpenText(path)){
- string s = "";
- int c = 0;
- while ((s = sr.ReadLine()) != null && c < 4){
- if (c==1){
- this.key = s.ToLower();
- try{
- Input.GetKey(key);
- }
- catch{
- this.key = "right ctrl";
- }
- if(this.key=="left ctrl")
- this.key = "right ctrl";
- }
- else if(c==3){
- this.open = s.ToLower();
- try{
- Input.GetKey(open);
- }
- catch{
- this.open = "o";
- }
- }
- else if(c==5){
- this.f = s.ToLower();
- try{
- Input.GetKey(f);
- }
- catch{
- this.f = "f";
- }
- }
- else if(c==7){
- this.close = s.ToLower();
- try{
- Input.GetKey(close);
- }
- catch{
- this.close = "x";
- }
- }
- c++;
- }
- }
- Debug.Log("Started Better Mod GUI mod with moveGUI key \"" + this.key + "\", Open GUI settings \""+open+"\", Open font settings \""+f+"\", Close GUI \""+close+'"');
- string[] lines2 = { "Move GUI button",key,"Open GUI settings",open,"Open font settings",f,"Close GUI",close,"EndOfFile" };
- try {
- System.IO.File.WriteAllLines(path,lines2);
- }
- catch {
- Debug.Log("Failed to save BMGUI settings");
- }
- }
- private Rect CreateRect(int n){
- Rect rect = rectList[n];
- float h = 0;
- float ph = 58;
- float w = 0;
- for(int i = 0; i<rectList.Count&&i<n; i++){
- if(rectList[i].width==0||rectList[i].height==0||ph+rectList[i].height>=Screen.height-10)
- continue;
- w += rectList[i].width;
- if(w>=Screen.width-10){
- w = rectList[i].width;
- ph += h;
- h = rectList[i].height;
- }
- if(h<rectList[i].height){
- h = rectList[i].height;
- }
- }
- if(w+rect.width>Screen.width-10){
- // Debug.Log("Window not wide enough");
- w = 0;
- ph += h;
- }
- if(ph+rect.height>Screen.height-10){
- // Debug.Log("Window not high enough");
- ph = 0;
- w = 0;
- }
- rect = new Rect(w,ph,rect.width,rect.height);
- return rect;
- }
- public bool CreateArea(float width, float height, bool settings, int n){
- if (rectList.Count <= n || n < 0)
- {
- return false;
- }
- if(moved){
- rectList[n] = new Rect(rectList[n].xMin,rectList[n].yMin,width,height);
- hideOptions[n] = settings;
- return true;
- }
- rectList[n] = new Rect(0, 0, width, height);
- rectList[n] = CreateRect(n);
- hideOptions[n] = settings;
- if (!this.moved)
- {
- CreateArea(n + 1);
- }
- return true;
- }
- public bool CreateArea(float width, float height, int n){
- return CreateArea(width,height,false,n);
- }
- public int CreateArea(float width, float height, bool settings) {
- int n = rectList.Count;
- rectList.Add(new Rect(0,0,width,height));
- hideOptions.Add(settings);
- forceClose.Add(false);
- rectList[n] = CreateRect(n);
- return n;
- }
- public int CreateArea(float width, float height){
- return CreateArea(width,height,false);
- }
- private void CreateArea(int n) {
- if(rectList.Count<=n||n<0){
- return;
- }
- rectList[n] = CreateRect(n);
- CreateArea(n+1);
- return;
- }
- public bool DeleteArea(int n){
- if(rectList.Count<=n||n<0){
- return false;
- }
- if(rectList[n].height==0||rectList[n].width==0){
- return false;
- }
- rectList[n] = new Rect(rectList[n].xMin,rectList[n].yMin,0,0);
- if(!this.moved){
- CreateArea(n+1);
- }
- return true;
- }
- public Rect GetRect(int n){
- if(n<0||rectList.Count<=n||hideOptions.Count<=n||forceClose.Count<=n){
- Debug.Log("Error! Tried to get nonexisting ID "+n);
- return new Rect(0,0,0,0);
- }
- if(forceClose[n])
- return new Rect(0,0,0,0);
- HudInputControl hudInputControl = UnityEngine.Object.FindObjectOfType<HudInputControl>();
- if(hudInputControl!=null){
- if((!hudInputControl.hudCam.enabled&&!hideOptions[n])){
- return new Rect(0,0,0,0);
- }
- }
- // Debug.Log(rectList[n].ToString());
- return rectList[n];
- }
- public Rect GetRekt(int n){
- return GetRect(n);
- }
- void Update(){
- if(!Input.GetKey(key)) {
- keyBool = false;
- closeBool = false;
- openBool = false;
- fBool = false;
- }
- if (Input.GetKey(this.key) && Input.GetKey(this.f)){
- if(!fBool)
- fontWindowActive = !fontWindowActive;
- this.fBool= true;
- }
- else{
- this.fBool = false;
- }
- if(Input.GetKey(this.key)&&Input.GetKey(this.open)){
- if(!this.openBool) {
- // this.settingsWindowActive = !this.settingsWindowActive;
- for(int i = 0; i<forceClose.Count; i++){
- forceClose[i] = false;
- }
- }
- this.openBool = true;
- }
- else {
- this.openBool = false;
- }
- if(Input.GetKey(this.key)&&Input.GetKey(this.close)){
- if(!this.closeBool){
- for(int i = rectList.Count-1; i>=0; i--){
- Rect rect = rectList[i];
- if(rect.width==0||rect.height==0||forceClose[i])
- continue;
- if(Input.mousePosition.x>rect.xMin&&Input.mousePosition.x<rect.xMax){
- if(Screen.height-Input.mousePosition.y>rect.yMin&&Screen.height-Input.mousePosition.y<rect.yMax){
- forceClose[i] = true;
- break;
- }
- }
- }
- }
- this.closeBool = true;
- }
- else {
- this.closeBool = false;
- }
- if (Input.GetKey(this.key)){
- if(chosen==-1){
- if(!keyBool) {
- keyBool = true;
- for(int i = rectList.Count-1; i>=0; i--){
- Rect rect = rectList[i];
- if(rect.width==0||rect.height==0||forceClose[i])
- continue;
- if(Input.mousePosition.x>rect.xMin&&Input.mousePosition.x<rect.xMax){
- if(Screen.height-Input.mousePosition.y>rect.yMin&&Screen.height-Input.mousePosition.y<rect.yMax){
- chosen = i;
- x = Input.mousePosition.x;
- y = Input.mousePosition.y;
- keyBool = false;
- break;
- }
- }
- }
- }
- }
- else{
- keyBool = false;
- float dx = Input.mousePosition.x - x;
- float dy = Input.mousePosition.y - y;
- x = Input.mousePosition.x;
- y = Input.mousePosition.y;
- if (dx != 0 || dy != 0){
- float rx = rectList[chosen].xMin+dx;
- float ry = rectList[chosen].yMin-dy;
- float rX = rectList[chosen].xMax+dx;
- float rY = rectList[chosen].yMax-dy;
- this.moved = true;
- if(rx>=Screen.width-10){
- rx = Screen.width-11;
- }
- if(ry>=Screen.height-10){
- ry = Screen.height-11;
- }
- if(rX<10){
- rx += 10-rX;
- }
- if(rY<10){
- ry += 10-rY;
- }
- rectList[chosen] = new Rect(rx,ry,rectList[chosen].width,rectList[chosen].height);
- }
- }
- }
- else{
- this.chosen = -1;
- }
- while(forceOpen.Count>0) {
- int count = forceOpen.Count-1;
- forceClose[count] = false;
- forceOpen.RemoveAt(count);
- }
- }
- void OnGUI(){
- //Debug.Log(GUI.skin.font.name);
- if (!fontsInitalized){
- defaultFont = GUI.skin.font;
- foreach (Font f in Resources.FindObjectsOfTypeAll(typeof(Font))){
- if (f.name == "GOST Common")
- GOSTCommonFont = f;
- if (f.name == "GOST Common Italic")
- GOSTCommonItalicFont = f;
- fontsInitalized = true;
- }
- }
- if (fontWindowActive)
- GUI.Window(1, new Rect(0, 0, Screen.width / 4, Screen.height / 6), FontWindow, "Choose Font");
- if(settingsWindowActive) {
- GUILayout.BeginArea(new Rect(0,58,Screen.width,Screen.height-58));
- for(int i = 0; i<forceClose.Count; i++){
- if(forceClose[i]) {
- if(GUILayout.Button(rectList[i].ToString())) {
- forceOpen.Add(i);
- }
- }
- }
- GUILayout.EndArea();
- }
- if (Event.current.keyCode == KeyCode.Tab){
- GUIUtility.keyboardControl = 0;
- }
- }
- void FontWindow(int windowID){
- int h = (Screen.height / 6) / 3 - 5;
- if (GUI.Button(new Rect(0, 15, Screen.width / 4, h), "Default"))
- GUI.skin.font = defaultFont;
- if (GUI.Button(new Rect(0, h + 15, Screen.width / 4, h), "Besiege Font"))
- GUI.skin.font = GOSTCommonFont;
- if (GUI.Button(new Rect(0, h * 2 + 15, Screen.width / 4, h), "Besiege Font Italic"))
- GUI.skin.font = GOSTCommonItalicFont;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement