AR{Configuration,FlightController}: Added AppLauncher button stuff.
[AntennaRange.git] / ARFlightController.cs
blob:a/ARFlightController.cs -> blob:b/ARFlightController.cs
--- a/ARFlightController.cs
+++ b/ARFlightController.cs
@@ -43,6 +43,7 @@
 
 		#region Fields
 		protected Dictionary<ConnectionStatus, string> connectionTextures;
+		protected Dictionary<ConnectionStatus, Texture> appLauncherTextures;
 
 		protected IButton toolbarButton;
 
@@ -61,6 +62,14 @@
 			get
 			{
 				return this.connectionTextures[this.currentConnectionStatus];
+			}
+		}
+
+		protected Texture currentAppLauncherTexture
+		{
+			get
+			{
+				return this.appLauncherTextures[this.currentConnectionStatus];
 			}
 		}
 
@@ -115,6 +124,15 @@
 			this.connectionTextures[ConnectionStatus.None] = "AntennaRange/Textures/toolbarIconNoConnection";
 			this.connectionTextures[ConnectionStatus.Suboptimal] = "AntennaRange/Textures/toolbarIconSubOptimal";
 			this.connectionTextures[ConnectionStatus.Optimal] = "AntennaRange/Textures/toolbarIcon";
+
+			this.appLauncherTextures = new Dictionary<ConnectionStatus, Texture>();
+
+			this.appLauncherTextures[ConnectionStatus.None] =
+				GameDatabase.Instance.GetTexture("AntennaRange/Textures/appLauncherIconNoConnection", false);
+			this.appLauncherTextures[ConnectionStatus.Suboptimal] =
+				GameDatabase.Instance.GetTexture("AntennaRange/Textures/appLauncherIconSubOptimal", false);
+			this.appLauncherTextures[ConnectionStatus.Optimal] =
+				GameDatabase.Instance.GetTexture("AntennaRange/Textures/appLauncherIcon", false);
 
 			if (ToolbarManager.ToolbarAvailable)
 			{
@@ -136,11 +154,32 @@
 			{
 				this.appLauncherButton = ApplicationLauncher.Instance.AddModApplication(
 					ApplicationLauncher.AppScenes.FLIGHT,
-					GameDatabase.Instance.GetTexture("AntennaRange/Textures/toolbarIconNoConnection", false)
+					this.appLauncherTextures[ConnectionStatus.None]
 				);
 			}
 
 			Tools.DebugLogger log = Tools.DebugLogger.New(this);
+
+			VesselCommand availableCommand;
+
+			if (requireConnectionForControl)
+			{
+				availableCommand = this.vessel.CurrentCommand();
+			}
+			else
+			{
+				availableCommand = VesselCommand.Crew;
+			}
+
+			log.AppendFormat("availableCommand: {0}\n\t" +
+				"(availableCommand & VesselCommand.Crew) == VesselCommand.Crew: {1}\n\t" +
+				"(availableCommand & VesselCommand.Probe) == VesselCommand.Probe: {2}\n\t" +
+				"vessel.HasConnectedRelay(): {3}",
+				(int)availableCommand,
+				(availableCommand & VesselCommand.Crew) == VesselCommand.Crew,
+				(availableCommand & VesselCommand.Probe) == VesselCommand.Probe,
+				vessel.HasConnectedRelay()
+			);
 
 			// If we are requiring a connection for control, the vessel does not have any adequately staffed pods,
 			// and the vessel does not have any connected relays...
@@ -149,8 +188,10 @@
 				requireConnectionForControl &&
 				this.vessel != null &&
 				this.vessel.vesselType != VesselType.EVA &&
-				!this.vessel.hasCrewCommand() &&
-				!this.vessel.HasConnectedRelay())
+				!(
+				    (availableCommand & VesselCommand.Crew) == VesselCommand.Crew ||
+				    (availableCommand & VesselCommand.Probe) == VesselCommand.Probe && vessel.HasConnectedRelay()
+				))
 			{
 				// ...and if the controls are not currently locked...
 				if (currentControlLock == ControlTypes.None)
@@ -240,8 +281,7 @@
 				}
 				if (this.appLauncherButton != null)
 				{
-					this.appLauncherButton.SetTexture(
-						GameDatabase.Instance.GetTexture(this.currentConnectionTexture, false));
+					this.appLauncherButton.SetTexture(this.currentAppLauncherTexture);
 				}
 			}
 
@@ -259,7 +299,8 @@
 
 			if (this.appLauncherButton != null)
 			{
-				ApplicationLauncher.Instance.RemoveApplication(this.appLauncherButton);
+				ApplicationLauncher.Instance.RemoveModApplication(this.appLauncherButton);
+				this.appLauncherButton = null;
 			}
 
 			GameEvents.onGameSceneLoadRequested.Remove(this.onSceneChangeRequested);
@@ -272,11 +313,8 @@
 		#region Event Handlers
 		protected void onSceneChangeRequested(GameScenes scene)
 		{
-			if (scene != GameScenes.FLIGHT)
-			{
-				print("ARFlightController: Requesting Destruction.");
-				MonoBehaviour.Destroy(this);
-			}
+			print("ARFlightController: Requesting Destruction.");
+			MonoBehaviour.Destroy(this);
 		}
 
 		protected void onVesselChange(Vessel vessel)