Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- namespace GUI1
- {
- /*
- If you are using applauncher with a mod that uses a partless loader, for example by adding this attribute to your class: [KSPAddon(KSPAddon.Startup.MainMenu, true)].
- http: <param name="startup">When this addon should be started.</param>
- <param name="once"> Whether KSP should start up your addon just once per game session, or every time the startup time is reached. </param>
- If you want your addon to persist forever, even through scene changes, after being started once, set once to true. call DontDestroyOnLoad(this) in your Start() function.
- <see href="https: */
- [KSPAddon(KSPAddon.Startup.SpaceCentre, true)]
- public class GUI1: MonoBehaviour
- {
- private ApplicationLauncherButton _appLauncherButton = null;
- private Texture2D _appLauncherIcon = GameDatabase.Instance.GetTexture("GUI1/gfx/applaunchericon", false);
- private const int _configWindowId = 001122;
- private bool _showWindow = false;
- private Rect _windowRect;
- public void Awake()
- {
- Utils.Log("info", "Awake ...");
- GameEvents.onGUIApplicationLauncherReady.Add(OnGUIAppLauncherReady);
- GameEvents.onGUIApplicationLauncherDestroyed.Add(OnGUIAppLauncherDestroyed);
- }
- public void Start()
- {
- DontDestroyOnLoad(this);
- Utils.Log("info", "Started !");
- if (!_showWindow) OnGUIAppLauncherReady();
- }
- public void OnGUI()
- {
- if (_showWindow)
- {
- if (_windowRect == default(Rect))
- {
- const int width = 300;
- const int height = 100;
- var x = (Screen.width / 2) - (width / 2);
- var y = (Screen.height / 2) - (height / 2);
- _windowRect = new Rect(x, y, width, height);
- }
- GUI.skin = HighLogic.Skin;
- _windowRect = GUILayout.Window(_configWindowId, _windowRect, OnConfigWindow, "GUI1");
- }
- }
- private void OnGUIAppLauncherReady()
- {
- Utils.Log("info", "OnGUIAppLauncherReady()");
- if (_appLauncherButton == null)
- {
- Utils.Log("info", "OnGUIAppLauncherReady() - Populating AppLauncher ...");
- _appLauncherButton = ApplicationLauncher.Instance.AddModApplication(
- () => OnAppLauncherEvent(AppLauncherEvent.OnTrue),
- () => OnAppLauncherEvent(AppLauncherEvent.OnFalse),
- () => OnAppLauncherEvent(AppLauncherEvent.OnHover),
- () => OnAppLauncherEvent(AppLauncherEvent.OnHoverOut),
- () => OnAppLauncherEvent(AppLauncherEvent.OnEnable),
- () => OnAppLauncherEvent(AppLauncherEvent.OnDisable),
- ApplicationLauncher.AppScenes.SPACECENTER | ApplicationLauncher.AppScenes.FLIGHT,
- _appLauncherIcon);
- }
- }
- private void OnGUIAppLauncherDestroyed()
- {
- try
- {
- Utils.Log("info", "OnGUIAppLauncherDestroyed()");
- ApplicationLauncher.Instance.RemoveModApplication(_appLauncherButton);
- _appLauncherButton = null;
- }
- catch(Exception e)
- {
- Utils.Log("err", "OnGUIAppLauncherDestroyed()");
- Debug.LogException(e);
- }
- }
- private void OnAppLauncherEvent(AppLauncherEvent appLauncherEvent)
- {
- switch (appLauncherEvent)
- {
- case AppLauncherEvent.OnTrue:
- _showWindow = true;
- break;
- case AppLauncherEvent.OnFalse:
- _showWindow = false;
- break;
- case AppLauncherEvent.OnHover:
- break;
- case AppLauncherEvent.OnHoverOut:
- break;
- case AppLauncherEvent.OnEnable:
- Utils.Log("info", "AppLauncherEvent.OnEnable");
- break;
- case AppLauncherEvent.OnDisable:
- Utils.Log("info", "AppLauncherEvent.OnDisable");
- break;
- default:
- throw new ArgumentOutOfRangeException(nameof(appLauncherEvent));
- }
- }
- private enum AppLauncherEvent
- {
- OnTrue,
- OnFalse,
- OnHover,
- OnHoverOut,
- OnEnable,
- OnDisable,
- }
- private void OnConfigWindow(int windowId)
- {
- GUILayout.BeginHorizontal();
- if (GUILayout.Button("Button 1"))
- {
- }
- if (GUILayout.Button("Button 2"))
- {
- }
- GUILayout.EndHorizontal();
- GUI.DragWindow();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment