Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- namespace GUI1
- {
- ///<summary>
- /// <remarks>
- ///<para/>A script makes its connection with the internal workings of Unity by implementing a class which derives from the built-in class called MonoBehaviour.
- ///<para/>You can think of a class as a kind of blueprint for creating a new Component type that can be attached to GameObjects.Each time you attach a script component to a GameObject, it creates a new instance of the object defined by the blueprint.
- ///<para/>The name of the class is taken from the name you supplied when the file was created.The class name and file name must be the same to enable the script component to be attached to a GameObject.
- ///<para/>The main things to note, however, are the two functions defined inside the class.
- ///<para/>The Update function is the place to put code that will handle the frame update for the GameObject.This might include movement, triggering actions and responding to user input, basically anything that needs to be handled over time during gameplay.
- ///<para/>To enable the Update function to do its work, it is often useful to be able to set up variables, read preferences and make connections with other GameObjects before any game action takes place.
- ///<para/>The Start function will be called by Unity before gameplay begins (ie, before the Update function is called for the first time) and is an ideal place to do any initialization.
- ///</remarks>
- /// </summary>
- ///<seealso href="http://docs.unity3d.com/Manual/CreatingAndUsingScripts.html"/>
- /*
- 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://forum.kerbalspaceprogram.com/threads/86682-Appilcation-Launcher-and-Mods
- <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://anatid.github.io/XML-Documentation-for-the-KSP-API/class_k_s_p_addon.html"/>
- */
- [KSPAddon(KSPAddon.Startup.SpaceCentre, true)]
- public class GUI1: MonoBehaviour
- {
- //our button for the applauncher
- private ApplicationLauncherButton _appLauncherButton = null;
- ///<summary>the application launcher button's texture.
- ///Path is relative to gamedata folder. Need to omit file extension.</summary>
- private Texture2D _appLauncherIcon = GameDatabase.Instance.GetTexture("GUI1/gfx/applaunchericon", false);
- ///some random identifier for the window
- private const int _configWindowId = 001122;
- ///window visibility indicator
- private bool _showWindow = false;
- ///window definition
- private Rect _windowRect;
- ///<summary>
- ///<para/>Awake is called when the script instance is being loaded.
- ///<para/>Awake is used to initialize any variables or game state before the game starts.
- ///<para/>Awake is called only once during the lifetime of the script instance.
- ///<para/>Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag.
- ///<para/>Awake is always called before any Start functions. This allows you to order initialization of scripts.
- ///<para/>Awake can not act as a coroutine.
- ///</summary>
- ///<remarks>
- /// Each GameObject's Awake is called in a random order between objects.
- /// Because of this, you should use Awake to set up references between scripts, and use Start to pass any information back and forth.
- /// </remarks>
- ///<seealso href="http://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html"/>
- public void Awake()
- {
- Utils.Log("info", "Awake ...");
- /// Applauncher becomes alive and is running when going from mainmenu to spacecenter.
- ///It has a static flag ApplicationLauncher.Ready which indicates when it is ready to receive add/remove commands.
- ///The event GameEvents.onGUIApplicationLauncherReady will fire when this happens.
- ///http://forum.kerbalspaceprogram.com/threads/86682-Appilcation-Launcher-and-Mods
- ///lt's register the methods to execute when those events are fired.
- GameEvents.onGUIApplicationLauncherReady.Add(OnGUIAppLauncherReady);
- GameEvents.onGUIApplicationLauncherDestroyed.Add(OnGUIAppLauncherDestroyed);
- }
- /// <summary>
- /// Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
- /// Like the Awake function, Start is called exactly once in the lifetime of the script. However, Awake is called when the script object is initialised, regardless of whether or not the script is enabled. Start may not be called on the same frame as Awake if the script is not enabled at initialisation time.
- /// The Awake function is called on all objects in the scene before any object's Start function is called. This fact is useful in cases where object A's initialisation code needs to rely on object B's already being initialised; B's initialisation should be done in Awake while A's should be done in Start.
- /// Where objects are instantiated during gameplay, their Awake function will naturally be called after the Start functions of scene objects have already completed.
- /// </summary>
- /// <seealso href="http://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html"/>
- public void Start()
- {
- DontDestroyOnLoad(this);
- Utils.Log("info", "Started !");
- //FIX OnGUIApplicationLauncherReady(); is not fired first time on space center
- if (!_showWindow) OnGUIAppLauncherReady();
- }
- /// <summary>
- /// <para/>OnGUI is called for rendering and handling GUI events.
- /// <para/>This means that your OnGUI implementation might be called several times per frame (one call per event).
- /// <para/>For more information on GUI events see the Event reference.
- /// <para/>If the MonoBehaviour's enabled property is set to false, OnGUI() will not be called.
- /// <para/>Since the OnGUI() code gets called every frame, you don’t need to explicitly create or destroy GUI controls.
- /// <para/>The line that declares the Control is the same one that creates it. If you need to display Controls at specific times, you can use any kind of scripting logic to do so.
- /// </summary>
- /// <seealso href="http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnGUI.html"/>
- /// <seealso href="http://docs.unity3d.com/Manual/gui-Basics.html"/>
- public void OnGUI()
- {
- if (_showWindow)
- {
- // default()
- //For a reference-type, it returns null
- //For a value-type other than Nullable<T> it returns a zero-initialized value
- //For Nullable<T> it returns the empty (pseudo-null) value that (actually, this is a re-statement of the second bullet, but it is worth making it explicit)
- 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");
- }
- }
- /// <summary>
- /// Method executed when event OnGUIApplicationLauncher is fired.
- /// Means when ApllicationLauncher is ready to receive my button
- /// </summary>
- private void OnGUIAppLauncherReady()
- {
- Utils.Log("info", "OnGUIAppLauncherReady()");
- if (_appLauncherButton == null)
- {
- Utils.Log("info", "OnGUIAppLauncherReady() - Populating AppLauncher ...");
- _appLauncherButton = ApplicationLauncher.Instance.AddModApplication(
- //Callback for when the button is toggeled on
- () => OnAppLauncherEvent(AppLauncherEvent.OnTrue),
- //Callback for when the button is toggeled off
- () => OnAppLauncherEvent(AppLauncherEvent.OnFalse),
- //Callback for when the mouse is hovering over the button
- () => OnAppLauncherEvent(AppLauncherEvent.OnHover),
- //Callback for when the mouse hoveris off the button
- () => OnAppLauncherEvent(AppLauncherEvent.OnHoverOut),
- //Callback for when the button is shown or enabled by the application launcher
- () => OnAppLauncherEvent(AppLauncherEvent.OnEnable),
- //Callback for when the button is hidden or disabled by the application launcher
- () => OnAppLauncherEvent(AppLauncherEvent.OnDisable),
- // The "scenes" this button will be visible in.
- ApplicationLauncher.AppScenes.SPACECENTER | ApplicationLauncher.AppScenes.FLIGHT,
- //The 38x38 Texture to use for the button icon.
- _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);
- }
- }
- // Dunno how correct it is to do it tht way
- //but i just love to have it all at the same place
- //credit to Apokee
- //https://github.com/Apokee/HotSpot/blob/develop/Source/HotSpot/HotSpotGuiBehavior.cs
- private void OnAppLauncherEvent(AppLauncherEvent appLauncherEvent)
- {
- //Utils.Log("info","OnAppLauncherEvent()");
- 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