Fixed button not disabling when there is no available FlightEngineerCore.
[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
@@ -49,6 +49,29 @@
             {
                 GameEvents.onGUIApplicationLauncherReady.Add(this.OnGuiAppLauncherReady);
                 Logger.Log("ActionMenu was created.");
+            }
+            catch (Exception ex)
+            {
+                Logger.Exception(ex);
+            }
+        }
+
+        #endregion
+
+        #region Updating
+
+        private void Update()
+        {
+            try
+            {
+                if (FlightEngineerCore.Instance != null && this.button.State == RUIToggleButton.ButtonState.DISABLED)
+                {
+                    this.button.Enable();
+                }
+                else if (FlightEngineerCore.Instance == null && this.button.State != RUIToggleButton.ButtonState.DISABLED)
+                {
+                    this.button.Disable();
+                }
             }
             catch (Exception ex)
             {
@@ -76,6 +99,8 @@
                 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)
             {
@@ -134,6 +159,30 @@
             }
         }
 
+        private void OnHide()
+        {
+            try
+            {
+                this.actionMenuGui.Hidden = true;
+            }
+            catch (Exception ex)
+            {
+                Logger.Exception(ex);
+            }
+        }
+
+        private void OnShow()
+        {
+            try
+            {
+                this.actionMenuGui.Hidden = false;
+            }
+            catch (Exception ex)
+            {
+                Logger.Exception(ex);
+            }
+        }
+
         #endregion
 
         #region Destruction
@@ -143,6 +192,8 @@
             try
             {
                 GameEvents.onGUIApplicationLauncherReady.Remove(this.OnGuiAppLauncherReady);
+                GameEvents.onHideUI.Remove(this.OnHide);
+                GameEvents.onShowUI.Remove(this.OnShow);
                 ApplicationLauncher.Instance.RemoveModApplication(this.button);
                 Logger.Log("ActionMenu was destroyed.");
             }