Created wrapper for handling the Application Launcher buttons.
[VesselSimulator.git] / KerbalEngineer / AppLauncherButton.cs
blob:a/KerbalEngineer/AppLauncherButton.cs -> blob:b/KerbalEngineer/AppLauncherButton.cs
--- a/KerbalEngineer/AppLauncherButton.cs
+++ b/KerbalEngineer/AppLauncherButton.cs
@@ -18,50 +18,117 @@
 
 namespace KerbalEngineer
 {
+    using KSP.UI;
     using KSP.UI.Screens;
     using UnityEngine;
 
     public class AppLauncherButton : MonoBehaviour
     {
-        public enum ButtonState
-        {
-            Disabled,
-            On,
-            Off
-        }
-
+        private static Texture m_IconTexture;
         private ApplicationLauncherButton m_Button;
 
         /// <summary>
-        ///     Sets the state of the button.
-        /// </summary>
-        public void SetState(ButtonState state)
+        ///     Gets or sets the toggle button state.
+        /// </summary>
+        public bool IsOn
+        {
+            get
+            {
+                return m_Button != null &&
+                       m_Button.toggleButton.Button.interactable &&
+                       m_Button.toggleButton.CurrentState == UIRadioButton.State.True;
+            }
+            set
+            {
+                if (m_Button == null)
+                {
+                    return;
+                }
+
+                if (value)
+                {
+                    SetOn();
+                }
+                else
+                {
+                    SetOff();
+                }
+            }
+        }
+
+        /// <summary>
+        ///     Disables the button if not already disabled.
+        /// </summary>
+        public void Disable()
+        {
+            if (m_Button != null && m_Button.toggleButton.Button.interactable)
+            {
+                m_Button.Disable();
+            }
+        }
+
+        /// <summary>
+        ///     Enables the button if not already enabled.
+        /// </summary>
+        public void Enable()
+        {
+            if (m_Button != null && m_Button.toggleButton.Button.interactable == false)
+            {
+                m_Button.Enable();
+            }
+        }
+
+        /// <summary>
+        ///     Gets the anchor position for pop-up content.
+        /// </summary>
+        public Vector3 GetAnchor()
         {
             if (m_Button == null)
             {
-                return;
-            }
-
-            switch (state)
-            {
-                case ButtonState.Disabled:
-                    Disable();
-                    break;
-
-                case ButtonState.On:
-                    Enable();
-                    m_Button.SetTrue();
-                    break;
-
-                case ButtonState.Off:
-                    Enable();
-                    m_Button.SetFalse();
-                    break;
+                return Vector3.zero;
+            }
+
+            Vector3 anchor = m_Button.GetAnchor();
+
+            anchor.x -= 3.0f;
+
+            return anchor;
+        }
+
+        /// <summary>
+        ///     Enables and sets the button to off.
+        /// </summary>
+        public void SetOff()
+        {
+            Enable();
+
+            if (m_Button != null && m_Button.toggleButton.CurrentState != UIRadioButton.State.False)
+            {
+                m_Button.SetTrue();
+            }
+        }
+
+        /// <summary>
+        ///     Enables and sets the button to on.
+        /// </summary>
+        public void SetOn()
+        {
+            Enable();
+
+            if (m_Button != null && m_Button.toggleButton.CurrentState != UIRadioButton.State.True)
+            {
+                m_Button.SetTrue();
             }
         }
 
         protected virtual void Awake()
         {
+            // cache icon texture
+            if (m_IconTexture == null && AssetBundleLoader.Images != null)
+            {
+                m_IconTexture = AssetBundleLoader.Images.LoadAsset<Texture2D>("app-launcher-icon");
+            }
+
             // subscribe event listeners
             GameEvents.onGUIApplicationLauncherReady.Add(OnGUIApplicationLauncherReady);
             GameEvents.onGUIApplicationLauncherUnreadifying.Add(OnGUIApplicationLauncherUnreadifying);
@@ -114,35 +181,12 @@
         /// </summary>
         protected virtual void OnUnreadifying() { }
 
-        /// <summary>
-        ///     Disables the button if not already disabled.
-        /// </summary>
-        private void Disable()
-        {
-            if (m_Button != null && m_Button.toggleButton.Button.interactable)
-            {
-                m_Button.Disable();
-            }
-        }
-
-        /// <summary>
-        ///     Enables the button if not already enabled.
-        /// </summary>
-        private void Enable()
-        {
-            if (m_Button != null && m_Button.toggleButton.Button.interactable == false)
-            {
-                m_Button.Enable();
-            }
-        }
-
         private void OnGUIApplicationLauncherReady()
         {
             // create button
             if (ApplicationLauncher.Instance != null)
             {
-                Texture iconTexture = GameDatabase.Instance.GetTexture("KerbalEngineer/Textures/ToolbarIcon", false);
-                m_Button = ApplicationLauncher.Instance.AddModApplication(OnTrue, OnFalse, OnHover, OnHoverOut, OnEnable, OnDisable, ApplicationLauncher.AppScenes.ALWAYS, iconTexture);
+                m_Button = ApplicationLauncher.Instance.AddModApplication(OnTrue, OnFalse, OnHover, OnHoverOut, OnEnable, OnDisable, ApplicationLauncher.AppScenes.ALWAYS, m_IconTexture);
             }
 
             OnReady();