ARMapRenderer: New behaviour to render pretty lines in map view.
[AntennaRange.git] / ARMapRenderer.cs
blob:a/ARMapRenderer.cs -> blob:b/ARMapRenderer.cs
--- a/ARMapRenderer.cs
+++ b/ARMapRenderer.cs
@@ -73,6 +73,7 @@
 		#region MonoBehaviour Lifecycle
 		private void Awake()
 		{
+			this.vesselLineRenderers = new Dictionary<Guid, LineRenderer>();
 			this.vesselFrameCache = new Dictionary<Guid, bool>();
 		}
 
@@ -80,6 +81,8 @@
 		{
 			if (!HighLogic.LoadedSceneIsFlight || !MapView.MapIsEnabled)
 			{
+				this.Cleanup();
+
 				return;
 			}
 
@@ -88,30 +91,13 @@
 			try
 			{
 				log.AppendFormat("OnPreCull.\n");
-
-				double sma;
-
-				switch (MapView.MapCamera.target.type)
-				{
-					case MapObject.MapObjectType.CELESTIALBODY:
-						sma = MapView.MapCamera.target.celestialBody.orbit.semiMajorAxis;
-						break;
-					case MapObject.MapObjectType.VESSEL:
-						sma = MapView.MapCamera.target.vessel.orbit.semiMajorAxis;
-						break;
-					default:
-						sma = ScaledSpace.ScaleFactor;
-						break;
-				}
 
 				log.AppendFormat("\tMapView: Draw3DLines: {0}\n" +
 					"\tMapView.MapCamera.camera.fieldOfView: {1}\n" +
-					"\tMapView.MapCamera.Distance: {2}\n" +
-					"\tDistanceVSSMAFactor: {3}\n",
+					"\tMapView.MapCamera.Distance: {2}\n",
 					MapView.Draw3DLines,
 					MapView.MapCamera.camera.fieldOfView,
-					MapView.MapCamera.Distance,
-					(float)((double)MapView.MapCamera.Distance / Math.Abs(sma) * ScaledSpace.InverseScaleFactor)
+					MapView.MapCamera.Distance
 				);
 
 				this.vesselFrameCache.Clear();
@@ -143,6 +129,8 @@
 								continue;
 						}
 
+						log.Append("\tChecking connection status...\n");
+
 						if (vessel.HasConnectedRelay())
 						{
 							log.AppendLine("\tHas a connection, checking for the best relay to use for the line.");
@@ -185,10 +173,11 @@
 			}
 		}
 
-		private void Destroy()
-		{
-			this.vesselLineRenderers.Clear();
-			this.vesselLineRenderers = null;
+		private void OnDestroy()
+		{
+			this.Cleanup();
+
+			print("ARMapRenderer: Destroyed.");
 		}
 		#endregion
 
@@ -210,6 +199,15 @@
 
 					renderer.enabled = true;
 
+					if (relay.transmitDistance < relay.nominalTransmitDistance)
+					{
+						renderer.SetColors(Color.green, Color.green);
+					}
+					else
+					{
+						renderer.SetColors(Color.yellow, Color.yellow);
+					}
+
 					start = ScaledSpace.LocalToScaledSpace(relay.vessel.GetWorldPos3D());
 
 					if (relay.nearestRelay == null)
@@ -225,7 +223,7 @@
 
 					if (MapView.Draw3DLines)
 					{
-						lineWidth = 0.002f * MapView.MapCamera.Distance;
+						lineWidth = 0.004f * MapView.MapCamera.Distance;
 					}
 					else
 					{
@@ -235,8 +233,8 @@
 						end = MapView.MapCamera.camera.WorldToScreenPoint(end);
 
 						float d = Screen.height / 2f + 0.01f;
-						start.z = start.z >= 0.15f ? d : -d;
-						end.z = end.z >= 0.15f ? d : -d;
+						start.z = start.z >= 0f ? d : -d;
+						end.z = end.z >= 0f ? d : -d;
 					}
 
 					renderer.SetWidth(lineWidth, lineWidth);
@@ -251,6 +249,17 @@
 			}
 			while (relay != null);
 		}
+
+		public void Cleanup()
+		{
+			foreach (LineRenderer lineRenderer in this.vesselLineRenderers.Values)
+			{
+				lineRenderer.enabled = false;
+				GameObject.Destroy(lineRenderer.gameObject);
+			}
+			this.vesselLineRenderers.Clear();
+			this.vesselFrameCache.Clear();
+		}
 	}
 }