diff --git a/RasterPropMonitor/Handlers/JSIOrbitDisplay.cs b/RasterPropMonitor/Handlers/JSIOrbitDisplay.cs index e6862500..d05f912f 100644 --- a/RasterPropMonitor/Handlers/JSIOrbitDisplay.cs +++ b/RasterPropMonitor/Handlers/JSIOrbitDisplay.cs @@ -184,7 +184,6 @@ private static void DrawOrbitSegment( double midTA = (startTA + endTA) / 2.0; Vector3 midVertex = ScreenPositionFromOrbitAtTA(o, referenceBody, screenTransform, midTA, now); Vector3 midStraight = (startVertex + endVertex) * 0.5f; - // Debug.Log($"startTA: {startTA}, endTA: {endTA}, startVertex: {startVertex}, endVertex: {endVertex}, midVertex: {midVertex}, midStraight: {midStraight}"); if (Math.Abs(startTA - endTA) < 0.01 || (midStraight - midVertex).sqrMagnitude < 16.0) { diff --git a/RasterPropMonitor/Handlers/JSIPrimaryFlightDisplay.cs b/RasterPropMonitor/Handlers/JSIPrimaryFlightDisplay.cs index 943668aa..666bb100 100644 --- a/RasterPropMonitor/Handlers/JSIPrimaryFlightDisplay.cs +++ b/RasterPropMonitor/Handlers/JSIPrimaryFlightDisplay.cs @@ -20,7 +20,7 @@ ****************************************************************************/ using UnityEngine; using System; -using KSP.UI.Screens.Flight; +using FinePrint; namespace JSI { @@ -291,37 +291,8 @@ public bool RenderPFD(RenderTexture screen, float aspect) markerManeuverMinus.visible = true; } - /* - if (FinePrint.WaypointManager.navIsActive() == true) - { - // MOARdV: Code for the waypoint marker based on https://github.com/Ninenium/NavHud/blob/master/Source/WaypointMarker.cs - // However, in 1.1.2 (maybe earlier), the NavBall gameobject doesn't have children. - // And in 1.1.3, FinePrint.WaypointManager .navIsActive and .navWaypoint no longer exist... - try - { - GameObject navWaypointIndicator = GameObject.Find("NavBall").transform.FindChild("vectorsPivot").FindChild("NavWaypoint").gameObject; - Renderer markerRenderer = null; - markerNavWaypoint.GetComponentCached(ref markerRenderer); - Material material = navWaypointIndicator.GetComponent().sharedMaterial; - markerRenderer.material.mainTexture = material.mainTexture; - markerRenderer.material.mainTextureScale = Vector2.one; - markerRenderer.material.mainTextureOffset = Vector2.zero; - if (string.IsNullOrEmpty(waypointColor)) - { - markerRenderer.material.SetVector("_Color", material.GetVector("_Color")); - } + DrawWaypoint(gymbal); - Vector3d waypointPosition = vessel.mainBody.GetWorldSurfacePosition(FinePrint.WaypointManager.navWaypoint.latitude, FinePrint.WaypointManager.navWaypoint.longitude, FinePrint.WaypointManager.navWaypoint.altitude); - Vector3 waypointDirection = (waypointPosition - vessel.CoM).normalized; - MoveMarker(markerNavWaypoint, waypointDirection, gymbal); - JUtil.ShowHide(true, markerNavWaypoint); - } - catch - { - // if something's borked, let's just silently do nothing - } - } - */ ITargetable target = FlightGlobals.fetch.VesselTarget; if (target != null) { @@ -469,6 +440,42 @@ private static void DrawMarker(Marker marker) } } + private void DrawWaypoint(Quaternion gymbal) + { + NavWaypoint waypoint = NavWaypoint.fetch; + if (waypoint == null || !waypoint.IsActive) + { + return; + } + + if (waypoint.IsBlinking && Planetarium.GetUniversalTime() % 1.0 > 0.5) + { + return; + } + + try + { + Material material = waypoint.Visual.GetComponent().sharedMaterial; + markerNavWaypoint.gameObject.transform.localScale = new Vector3(0.6f, 0.6f, 0.6f); + markerNavWaypoint.material.mainTexture = material.GetTexture("_MainTexture"); + markerNavWaypoint.material.mainTextureScale = Vector2.one; + markerNavWaypoint.material.mainTextureOffset = Vector2.zero; + if (string.IsNullOrEmpty(waypointColor)) + { + markerNavWaypoint.material.SetVector("_Color", material.GetVector("_TintColor")); + } + } + catch (Exception e) + { + Debug.Log($"Waypoint material failure: {e}"); + } + + Vector3d waypointPosition = waypoint.Body.GetWorldSurfacePosition(waypoint.Latitude, waypoint.Longitude, waypoint.Altitude); + Vector3 waypointDirection = (waypointPosition - vessel.CoM).normalized; + MoveMarker(markerNavWaypoint, waypointDirection, gymbal); + markerNavWaypoint.visible = true; + } + public void Start() { if (HighLogic.LoadedSceneIsEditor) @@ -542,7 +549,7 @@ public void Start() } navBall.GetComponent().material.SetFloat("_Opacity", ballOpacity); - + navBall.SetActive(false); startupComplete = true; diff --git a/RasterPropMonitor/Handlers/JSITargetMenu.cs b/RasterPropMonitor/Handlers/JSITargetMenu.cs index dcedb21b..84342bb2 100644 --- a/RasterPropMonitor/Handlers/JSITargetMenu.cs +++ b/RasterPropMonitor/Handlers/JSITargetMenu.cs @@ -23,6 +23,7 @@ using System.Linq; using System.Reflection; using System.Text; +using FinePrint; using UnityEngine; namespace JSI @@ -73,6 +74,7 @@ public class JSITargetMenu : InternalModule private readonly TextMenu topMenu = new TextMenu(); private TextMenu activeMenu; private TextMenu.Item clearTarget; + private TextMenu.Item clearWaypoint; private TextMenu.Item removeNode; private TextMenu.Item undockMenuItem; private TextMenu.Item grappleMenuItem; @@ -85,15 +87,18 @@ public class JSITargetMenu : InternalModule private const string armGrappleText = "Arm Grapple"; private const string crewEvaText = "Crew EVA"; private const string removeNodeItemText = "Remove Node"; + private const string clearWaypointText = "Clear Waypoint"; private readonly List rootMenu = new List { "Celestials", "Vessels", "Space Objects", "Reference part", + "Waypoints", undockItemText, armGrappleText, "Filters", clearTargetItemText, + clearWaypointText, crewEvaText, removeNodeItemText, }; @@ -125,6 +130,7 @@ private enum MenuList Undock, Filters, CrewEVA, + Waypoints }; private enum SortMode @@ -144,6 +150,7 @@ private enum SortMode private readonly List undockablesList = new List(); private List portsList = new List(); private readonly List referencePoints = new List(); + private readonly List<(Waypoint, double)> waypointsList = new List<(Waypoint, double)>(); private readonly List unavailablePorts = new List(); private int partCount; private SortMode sortMode; @@ -195,10 +202,12 @@ public string ShowMenu(int width, int height) activeMenu.menuTitle = string.Format(fp, menuTitleFormatString, "Decouple ports"); break; case MenuList.Root: + NavWaypoint waypoint = NavWaypoint.fetch; activeMenu.menuTitle = MakeMenuTitle("Root menu"); grappleMenuItem.isDisabled = UpdateReferencePartAsClaw(); - clearTarget.isDisabled = (currentTarget == null); - undockMenuItem.isDisabled = (undockablesList.Count == 0); + clearTarget.isDisabled = currentTarget == null; + clearWaypoint.isDisabled = waypoint != null && waypoint.IsActive; + undockMenuItem.isDisabled = undockablesList.Count == 0; removeNode.isDisabled = vessel.patchedConicSolver == null || vessel.patchedConicSolver.maneuverNodes.Count == 0; break; case MenuList.Filters: @@ -216,6 +225,9 @@ public string ShowMenu(int width, int height) case MenuList.Reference: activeMenu.menuTitle = string.Format(fp, menuTitleFormatString, "Select reference"); break; + case MenuList.Waypoints: + activeMenu.menuTitle = MakeMenuTitle("Waypoints"); + break; case MenuList.Ports: // sanity check: if (selectedVessel == null || !selectedVessel.loaded || portsList.Count == 0) @@ -422,7 +434,6 @@ private void UpdateUndockablesList() private void UpdateLists() { - switch (currentMenu) { case MenuList.Undock: @@ -460,7 +471,6 @@ private void UpdateLists() foreach (Celestial body in celestialsList) body.UpdateDistance(vessel.transform.position); - CelestialBody currentBody = celestialsList[activeMenu.GetCurrentIndex()].body; switch (sortMode) { case SortMode.Alphabetic: @@ -516,6 +526,36 @@ private void UpdateLists() activeMenu.Add(tmi); } break; + case MenuList.Waypoints: + waypointsList.Clear(); + foreach (Waypoint waypoint in WaypointManager.Instance().Waypoints) + { + if (waypoint.celestialBody == vessel.mainBody) + { + waypointsList.Add((waypoint, WaypointDistance(waypoint, vessel))); + } + } + + if (sortMode == SortMode.Alphabetic) + { + waypointsList.Sort((a, b) => a.Item1.name.CompareTo(b.Item1.name)); + } + else if (sortMode == SortMode.Distance) + { + waypointsList.Sort((a, b) => a.Item2.CompareTo(b.Item2)); + } + + activeMenu.Clear(); + foreach ((Waypoint waypoint, double distance) in waypointsList) + { + TextMenu.Item tmi = new TextMenu.Item(); + tmi.action = SelectWaypoint; + tmi.labelText = waypoint.name; + tmi.rightText = string.Format(fp, distanceFormatString.UnMangleConfigText(), distance); + tmi.isSelected = vessel.navigationWaypoint == waypoint; + activeMenu.Add(tmi); + } + break; case MenuList.Vessels: vesselsList.Clear(); foreach (Vessel thatVessel in FlightGlobals.fetch.vessels) @@ -625,7 +665,6 @@ private void UpdateLists() } break; } - } private static string GetPortName(ModuleDockingNode port) @@ -728,17 +767,21 @@ public void Start() FindReferencePoints(); UpdateUndockablesList(); - var menuActions = new List>(); - menuActions.Add(ShowCelestialMenu); - menuActions.Add(ShowVesselMenu); - menuActions.Add(ShowSpaceObjectMenu); - menuActions.Add(ShowReferenceMenu); - menuActions.Add(ShowUndockMenu); - menuActions.Add(ArmGrapple); - menuActions.Add(ShowFiltersMenu); - menuActions.Add(ClearTarget); - menuActions.Add(ShowCrewEVA); - menuActions.Add(RemoveNode); + var menuActions = new List> + { + ShowCelestialMenu, + ShowVesselMenu, + ShowSpaceObjectMenu, + ShowReferenceMenu, + ShowWaypointsMenu, + ShowUndockMenu, + ArmGrapple, + ShowFiltersMenu, + ClearTarget, + ClearWaypoint, + ShowCrewEVA, + RemoveNode + }; for (int i = 0; i < rootMenu.Count; ++i) { @@ -751,6 +794,9 @@ public void Start() case clearTargetItemText: clearTarget = topMenu[i]; break; + case clearWaypointText: + clearWaypoint = topMenu[i]; + break; case undockItemText: undockMenuItem = topMenu[i]; break; @@ -946,6 +992,22 @@ private void ShowReferenceMenu(int index, TextMenu.Item ti) activeMenu.currentSelection = referencePoints.FindIndex(x => x.part == vessel.GetReferenceTransformPart()); } + private void ShowWaypointsMenu(int index, TextMenu.Item ti) + { + currentMenu = MenuList.Waypoints; + + activeMenu = new TextMenu(); + activeMenu.rightColumnWidth = distanceColumnWidth; + activeMenu.labelColor = nameColorTag; + activeMenu.selectedColor = selectedColorTag; + activeMenu.disabledColor = unavailableColorTag; + activeMenu.rightTextColor = distanceColorTag; + + UpdateLists(); + + activeMenu.currentSelection = waypointsList.FindIndex(waypoint => waypoint.Item1 == vessel.navigationWaypoint); + } + private void ShowUndockMenu(int index, TextMenu.Item ti) { currentMenu = MenuList.Undock; @@ -1012,6 +1074,13 @@ private static void ClearTarget(int index, TextMenu.Item ti) FlightGlobals.fetch.SetVesselTarget((ITargetable)null); } + private static void ClearWaypoint(int index, TextMenu.Item ti) + { + NavWaypoint navWaypoint = NavWaypoint.fetch; + navWaypoint.Clear(); + navWaypoint.Deactivate(); + } + private void RemoveNode(int index, TextMenu.Item ti) { if (vessel.patchedConicSolver != null && vessel.patchedConicSolver.maneuverNodes.Count > 0) @@ -1100,6 +1169,18 @@ private void TargetVessel(int index, TextMenu.Item ti) activeMenu.SetSelected(index, true); } } + + private void SelectWaypoint(int index, TextMenu.Item ti) + { + NavWaypoint navWaypoint = NavWaypoint.fetch; + navWaypoint.Clear(); + navWaypoint.Deactivate(); + navWaypoint.Setup(waypointsList[index].Item1); + navWaypoint.Activate(); + + activeMenu.SetSelected(index, true); + } + // Space Object Menu private void TargetSpaceObject(int index, TextMenu.Item ti) { @@ -1228,6 +1309,12 @@ private static IEnumerable GetUndockableNodes(List parts) } } + private static double WaypointDistance(Waypoint waypoint, Vessel vessel) + { + Vector3d waypointPosition = waypoint.celestialBody.GetWorldSurfacePosition(waypoint.latitude, waypoint.longitude, waypoint.altitude); + return Vector3d.Distance(waypointPosition, vessel.CoM); + } + private class Celestial { public string name;