Redone action menu to better support the stock toolbar.
[VesselSimulator.git] / KerbalEngineer / Flight / ActionMenu.cs
blob:a/KerbalEngineer/Flight/ActionMenu.cs -> blob:b/KerbalEngineer/Flight/ActionMenu.cs
--- a/KerbalEngineer/Flight/ActionMenu.cs
+++ b/KerbalEngineer/Flight/ActionMenu.cs
@@ -20,7 +20,6 @@
 #region Using Directives
 
 using System;
-using System.Threading;
 
 using UnityEngine;
 
@@ -28,6 +27,8 @@
 
 namespace KerbalEngineer.Flight
 {
+    using KSP.UI.Screens;
+
     /// <summary>
     ///     Graphical controller for section interaction in the form of a menu system.
     /// </summary>
@@ -41,14 +42,64 @@
 
         #endregion
 
-        #region Initialisation
-
-        private void Awake()
+        #region Methods: protected
+
+        protected void Awake()
         {
             try
             {
                 GameEvents.onGUIApplicationLauncherReady.Add(this.OnGuiAppLauncherReady);
-                Logger.Log("ActionMenu was created.");
+            }
+            catch (Exception ex)
+            {
+                Logger.Exception(ex);
+            }
+            Logger.Log("ActionMenu was created.");
+        }
+
+        protected void Start()
+        {
+            if (button == null)
+            {
+                OnGuiAppLauncherReady();
+            }
+        }
+
+        protected void OnDestroy()
+        {
+            try
+            {
+                GameEvents.onGUIApplicationLauncherReady.Remove(this.OnGuiAppLauncherReady);
+                GameEvents.onHideUI.Remove(this.OnHide);
+                GameEvents.onShowUI.Remove(this.OnShow);
+                if (button != null)
+                {
+                    ApplicationLauncher.Instance.RemoveModApplication(this.button);
+                }
+            }
+            catch (Exception ex)
+            {
+                Logger.Exception(ex);
+            }
+            Logger.Log("ActionMenu was destroyed.");
+        }
+
+        protected void Update()
+        {
+            try
+            {
+                if (this.button == null)
+                {
+                    return;
+                }
+                if (FlightEngineerCore.IsDisplayable && this.button.toggleButton.Interactable == false)
+                {
+                    this.button.Enable();
+                }
+                else if (!FlightEngineerCore.IsDisplayable && this.button.toggleButton.Interactable)
+                {
+                    this.button.Disable();
+                }
             }
             catch (Exception ex)
             {
@@ -58,7 +109,20 @@
 
         #endregion
 
-        #region Callbacks
+        #region Methods: private
+
+        private void OnFalse()
+        {
+            try
+            {
+                this.actionMenuGui.enabled = false;
+                this.actionMenuGui.StayOpen = false;
+            }
+            catch (Exception ex)
+            {
+                Logger.Exception(ex);
+            }
+        }
 
         private void OnGuiAppLauncherReady()
         {
@@ -76,6 +140,57 @@
                 this.actionMenuGui = this.button.gameObject.AddComponent<ActionMenuGui>();
                 this.actionMenuGui.transform.parent = this.button.transform;
                 ApplicationLauncher.Instance.EnableMutuallyExclusive(this.button);
+                GameEvents.onHideUI.Add(this.OnHide);
+                GameEvents.onShowUI.Add(this.OnShow);
+            }
+            catch (Exception ex)
+            {
+                Logger.Exception(ex);
+            }
+        }
+
+        private void OnHide()
+        {
+            try
+            {
+                this.actionMenuGui.Hidden = true;
+            }
+            catch (Exception ex)
+            {
+                Logger.Exception(ex);
+            }
+        }
+
+        private void OnHover()
+        {
+            try
+            {
+                this.actionMenuGui.enabled = true;
+                this.actionMenuGui.Hovering = true;
+            }
+            catch (Exception ex)
+            {
+                Logger.Exception(ex);
+            }
+        }
+
+        private void OnHoverOut()
+        {
+            try
+            {
+                this.actionMenuGui.Hovering = false;
+            }
+            catch (Exception ex)
+            {
+                Logger.Exception(ex);
+            }
+        }
+
+        private void OnShow()
+        {
+            try
+            {
+                this.actionMenuGui.Hidden = false;
             }
             catch (Exception ex)
             {
@@ -89,62 +204,6 @@
             {
                 this.actionMenuGui.enabled = true;
                 this.actionMenuGui.StayOpen = true;
-            }
-            catch (Exception ex)
-            {
-                Logger.Exception(ex);
-            }
-        }
-
-        private void OnFalse()
-        {
-            try
-            {
-                this.actionMenuGui.enabled = false;
-                this.actionMenuGui.StayOpen = false;
-            }
-            catch (Exception ex)
-            {
-                Logger.Exception(ex);
-            }
-        }
-
-        private void OnHover()
-        {
-            try
-            {
-                this.actionMenuGui.enabled = true;
-                this.actionMenuGui.Hovering = true;
-            }
-            catch (Exception ex)
-            {
-                Logger.Exception(ex);
-            }
-        }
-
-        private void OnHoverOut()
-        {
-            try
-            {
-                this.actionMenuGui.Hovering = false;
-            }
-            catch (Exception ex)
-            {
-                Logger.Exception(ex);
-            }
-        }
-
-        #endregion
-
-        #region Destruction
-
-        private void OnDestroy()
-        {
-            try
-            {
-                GameEvents.onGUIApplicationLauncherReady.Remove(this.OnGuiAppLauncherReady);
-                ApplicationLauncher.Instance.RemoveModApplication(this.button);
-                Logger.Log("ActionMenu was destroyed.");
             }
             catch (Exception ex)
             {