Skip to content

Commit

Permalink
fixed camera listener attaching/detaching issue
Browse files Browse the repository at this point in the history
  • Loading branch information
neuoy committed Oct 12, 2014
1 parent 75912d0 commit 6c73803
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
27 changes: 25 additions & 2 deletions Plugin/MapOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,42 @@ public void OnPreRender()
}
}

private GameObject attachedCamera;
private CameraListener listener;
private List<GameObject> meshes = new List<GameObject>();
private bool displayEnabled = false;

private Material lineMaterial;
private float lineWidth = 0.002f;

private void DetachCamera()
{
if (attachedCamera == null)
return;

Debug.Log("Trajectories: detaching camera listener");

Destroy(listener);
listener = null;
attachedCamera = null;
}

public void Update()
{
if (attachedCamera != null && (MapView.MapCamera == null || MapView.MapCamera.gameObject != attachedCamera))
{
DetachCamera();
}

if ((HighLogic.LoadedScene != GameScenes.FLIGHT && HighLogic.LoadedScene != GameScenes.TRACKSTATION) || !MapView.MapIsEnabled || MapView.MapCamera == null)
return;

if (!MapView.MapCamera.gameObject.GetComponents<CameraListener>().Any())
if (listener == null)
{
MapView.MapCamera.gameObject.AddComponent<CameraListener>().overlay = this;
Debug.Log("Trajectories: attaching camera listener");
listener = MapView.MapCamera.gameObject.AddComponent<CameraListener>();
listener.overlay = this;
attachedCamera = MapView.MapCamera.gameObject;
}
}

Expand Down Expand Up @@ -365,6 +387,7 @@ private void initMeshFromImpact(Vector3 bodyPosition, Mesh mesh, Vector3 impactP
public void OnDestroy()
{
Settings.fetch.Save();
DetachCamera();
Clear();
}
}
Expand Down
6 changes: 6 additions & 0 deletions Plugin/Trajectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public Point GetInfo(float altitudeAboveSeaLevel)
if(!isAtmospheric)
throw new Exception("Trajectory info available only for atmospheric patches");

if (atmosphericTrajectory.Length == 1)
return atmosphericTrajectory[0];
else if (atmosphericTrajectory.Length == 0)
return new Point();

float absAltitude = (float)startingState.referenceBody.Radius + altitudeAboveSeaLevel;
float sqMag = absAltitude * absAltitude;

Expand All @@ -72,6 +77,7 @@ public Point GetInfo(float altitudeAboveSeaLevel)
++idx;

float coeff = (absAltitude - atmosphericTrajectory[idx].pos.magnitude) / Mathf.Max(0.00001f, atmosphericTrajectory[idx-1].pos.magnitude - atmosphericTrajectory[idx].pos.magnitude);
coeff = Math.Min(1.0f, Math.Max(0.0f, coeff));

Point res = new Point();
res.pos = atmosphericTrajectory[idx].pos * (1.0f - coeff) + atmosphericTrajectory[idx-1].pos * coeff;
Expand Down

0 comments on commit 6c73803

Please sign in to comment.