From 731df1daddb3569be7c5465b5a79c7f8cbb2634b Mon Sep 17 00:00:00 2001 From: Sander Hoksbergen Date: Thu, 26 Dec 2013 16:36:13 +0100 Subject: [PATCH] Stuff Strikes Back. Christmas Remaster. --- .../FlightComputer/Commands/AbstractCommand.cs | 2 ++ .../FlightComputer/Commands/AttitudeCommand.cs | 12 +++++++++++- .../FlightComputer/Commands/BurnCommand.cs | 10 ++++++++++ .../FlightComputer/Commands/CancelCommand.cs | 18 +++++++++++++++++- .../FlightComputer/Commands/ICommand.cs | 1 + .../FlightComputer/FlightComputer.cs | 9 +++++++++ src/RemoteTech2/NetworkRenderer.cs | 6 ++++-- src/RemoteTech2/RTUtil.cs | 17 +++++++++++++---- src/RemoteTech2/UI/FocusFragment.cs | 10 ++++++++-- src/RemoteTech2/UI/FocusOverlay.cs | 4 +++- src/RemoteTech2/UI/NetworkCone.cs | 1 + src/RemoteTech2/UI/QueueFragment.cs | 14 +++++++++++--- 12 files changed, 90 insertions(+), 14 deletions(-) diff --git a/src/RemoteTech2/FlightComputer/Commands/AbstractCommand.cs b/src/RemoteTech2/FlightComputer/Commands/AbstractCommand.cs index eb97bae..54ca9c9 100644 --- a/src/RemoteTech2/FlightComputer/Commands/AbstractCommand.cs +++ b/src/RemoteTech2/FlightComputer/Commands/AbstractCommand.cs @@ -30,6 +30,8 @@ public abstract class AbstractCommand : ICommand // true: delete afterwards. public virtual bool Execute(FlightComputer f, FlightCtrlState fcs) { return true; } + public virtual void Abort() { } + public int CompareTo(ICommand dc) { return TimeStamp.CompareTo(dc.TimeStamp); diff --git a/src/RemoteTech2/FlightComputer/Commands/AttitudeCommand.cs b/src/RemoteTech2/FlightComputer/Commands/AttitudeCommand.cs index 5833a9a..62b7c07 100644 --- a/src/RemoteTech2/FlightComputer/Commands/AttitudeCommand.cs +++ b/src/RemoteTech2/FlightComputer/Commands/AttitudeCommand.cs @@ -41,7 +41,7 @@ public class AttitudeCommand : AbstractCommand { public static readonly Dictionary FormatMode = new Dictionary() { - { FlightMode.Off, "Mode: Off," }, + { FlightMode.Off, "Mode: Off" }, { FlightMode.KillRot, "Mode: Kill rotation" }, { FlightMode.AttitudeHold, "Mode: Hold {0} {1}" }, { FlightMode.AltitudeHold, "Mode: Hold {0}" }, @@ -101,6 +101,8 @@ public override string Description } } + private bool mAbort; + public override bool Pop(FlightComputer f) { if (Mode == FlightMode.KillRot) @@ -112,6 +114,12 @@ public override bool Pop(FlightComputer f) public override bool Execute(FlightComputer f, FlightCtrlState fcs) { + if (mAbort) + { + Mode = FlightMode.Off; + mAbort = false; + } + switch (Mode) { case FlightMode.Off: @@ -129,6 +137,8 @@ public override bool Execute(FlightComputer f, FlightCtrlState fcs) return false; } + public override void Abort() { mAbort = true; } + public static AttitudeCommand Off() { return new AttitudeCommand() diff --git a/src/RemoteTech2/FlightComputer/Commands/BurnCommand.cs b/src/RemoteTech2/FlightComputer/Commands/BurnCommand.cs index c3e7237..dc9475c 100644 --- a/src/RemoteTech2/FlightComputer/Commands/BurnCommand.cs +++ b/src/RemoteTech2/FlightComputer/Commands/BurnCommand.cs @@ -20,6 +20,8 @@ public override String Description } } + private bool mAbort; + public override bool Pop(FlightComputer f) { return true; @@ -27,6 +29,12 @@ public override bool Pop(FlightComputer f) public override bool Execute(FlightComputer f, FlightCtrlState fcs) { + if (mAbort) + { + fcs.mainThrottle = 0.0f; + return true; + } + if (Duration > 0) { fcs.mainThrottle = Throttle; @@ -45,6 +53,8 @@ public override bool Execute(FlightComputer f, FlightCtrlState fcs) return false; } + public override void Abort() { mAbort = true; } + public static BurnCommand Off() { return new BurnCommand() diff --git a/src/RemoteTech2/FlightComputer/Commands/CancelCommand.cs b/src/RemoteTech2/FlightComputer/Commands/CancelCommand.cs index 839831c..aa5b57c 100644 --- a/src/RemoteTech2/FlightComputer/Commands/CancelCommand.cs +++ b/src/RemoteTech2/FlightComputer/Commands/CancelCommand.cs @@ -14,7 +14,14 @@ public class CancelCommand : AbstractCommand public override bool Pop(FlightComputer f) { - f.Remove(Command); + if (Command == null) + { + f.Reset(); + } + else + { + f.Remove(Command); + } return false; } @@ -26,5 +33,14 @@ public static CancelCommand WithCommand(ICommand cmd) TimeStamp = RTUtil.GameTime, }; } + + public static CancelCommand ResetActive() + { + return new CancelCommand() + { + Command = null, + TimeStamp = RTUtil.GameTime, + }; + } } } diff --git a/src/RemoteTech2/FlightComputer/Commands/ICommand.cs b/src/RemoteTech2/FlightComputer/Commands/ICommand.cs index 5146f17..b75cf65 100644 --- a/src/RemoteTech2/FlightComputer/Commands/ICommand.cs +++ b/src/RemoteTech2/FlightComputer/Commands/ICommand.cs @@ -14,5 +14,6 @@ public interface ICommand : IComparable bool Pop(FlightComputer f); bool Execute(FlightComputer f, FlightCtrlState fcs); + void Abort(); } } diff --git a/src/RemoteTech2/FlightComputer/FlightComputer.cs b/src/RemoteTech2/FlightComputer/FlightComputer.cs index dfc622c..c991015 100644 --- a/src/RemoteTech2/FlightComputer/FlightComputer.cs +++ b/src/RemoteTech2/FlightComputer/FlightComputer.cs @@ -79,6 +79,7 @@ public FlightComputer(ISignalProcessor s) SignalProcessor = s; Vessel = s.Vessel; SanctionedPilots = new List>(); + var target = TargetCommand.WithTarget(FlightGlobals.fetch.VesselTarget); mActiveCommands[target.Priority] = target; var attitude = AttitudeCommand.Off(); @@ -99,6 +100,14 @@ public void Dispose() } } + public void Reset() + { + foreach (var cmd in mActiveCommands.Values) + { + cmd.Abort(); + } + } + public void Enqueue(ICommand cmd, bool ignore_control = false, bool ignore_delay = false, bool ignore_extra = false) { if (!InputAllowed && !ignore_control) return; diff --git a/src/RemoteTech2/NetworkRenderer.cs b/src/RemoteTech2/NetworkRenderer.cs index 1f5779e..e5c8a72 100644 --- a/src/RemoteTech2/NetworkRenderer.cs +++ b/src/RemoteTech2/NetworkRenderer.cs @@ -95,6 +95,7 @@ private void UpdateNetworkCones() for (int i = newLength; i < oldLength; i++) { GameObject.Destroy(mCones[i]); + mCones[i] = null; } mCones.RemoveRange(Math.Min(oldLength, newLength), Math.Max(oldLength - newLength, 0)); mCones.AddRange(Enumerable.Repeat((NetworkCone) null, Math.Max(newLength - oldLength, 0))); @@ -121,6 +122,7 @@ private void UpdateNetworkEdges() for (int i = newLength; i < oldLength; i++) { GameObject.Destroy(mLines[i]); + mLines[i] = null; } mLines.RemoveRange(Math.Min(oldLength, newLength), Math.Max(oldLength - newLength, 0)); mLines.AddRange(Enumerable.Repeat(null, Math.Max(newLength - oldLength, 0))); @@ -195,12 +197,12 @@ public void Detach() { for (int i = 0; i < mLines.Count; i++) { - GameObject.Destroy(mLines[i]); + GameObject.DestroyImmediate(mLines[i]); } mLines.Clear(); for (int i = 0; i < mCones.Count; i++) { - GameObject.Destroy(mCones[i]); + GameObject.DestroyImmediate(mCones[i]); } mCones.Clear(); DestroyImmediate(this); diff --git a/src/RemoteTech2/RTUtil.cs b/src/RemoteTech2/RTUtil.cs index 7adbc55..a7bca09 100644 --- a/src/RemoteTech2/RTUtil.cs +++ b/src/RemoteTech2/RTUtil.cs @@ -122,14 +122,14 @@ public static float Format180To360(float degrees) public static String FormatDuration(double duration) { - TimeSpan time = TimeSpan.FromSeconds(duration); - StringBuilder s = new StringBuilder(); - if (time.TotalDays > DaysInAYear) + var time = TimeSpan.FromSeconds(duration); + var s = new StringBuilder(); + if (time.TotalDays / DaysInAYear >= 1) { s.Append(Math.Floor(time.TotalDays / DaysInAYear)); s.Append("y"); } - if (time.TotalDays > 0) + if (time.TotalDays % DaysInAYear >= 1) { s.Append(Math.Floor(time.TotalDays % DaysInAYear)); s.Append("d"); @@ -263,6 +263,15 @@ public static void GroupButton(int wide, String[] text, ref int group, Action onStateChange, params GUILayoutOption[] options) + { + bool result; + if ((result = GUILayout.Toggle(Object.Equals(state, value), text, GUI.skin.button, options)) != Object.Equals(state, value)) + { + onStateChange.Invoke(result ? value : ~value); + } + } + public static void StateButton(GUIContent text, T state, T value, Action onStateChange, params GUILayoutOption[] options) { bool result; diff --git a/src/RemoteTech2/UI/FocusFragment.cs b/src/RemoteTech2/UI/FocusFragment.cs index b8dec25..5df3077 100644 --- a/src/RemoteTech2/UI/FocusFragment.cs +++ b/src/RemoteTech2/UI/FocusFragment.cs @@ -26,7 +26,7 @@ public void Draw() mSelection = (s > 0) ? sat : null; if (mSelection != null) { - var newTarget = PlanetariumCamera.fetch.targets.FirstOrDefault(t => t.gameObject.name == sat.Name); + var newTarget = PlanetariumCamera.fetch.targets.FirstOrDefault(t => t != null && t.gameObject.name == sat.Name); if (newTarget == null) { var vessel = sat.SignalProcessor.Vessel; @@ -37,8 +37,14 @@ public void Draw() scaledMovement.vessel = vessel; scaledMovement.type = MapObject.MapObjectType.VESSEL; newTarget = scaledMovement; + PlanetariumCamera.fetch.SetTarget(PlanetariumCamera.fetch.AddTarget(newTarget)); + PlanetariumCamera.fetch.targets.Remove(newTarget); } - PlanetariumCamera.fetch.SetTarget(PlanetariumCamera.fetch.AddTarget(newTarget)); + else + { + PlanetariumCamera.fetch.SetTarget(PlanetariumCamera.fetch.AddTarget(newTarget)); + } + } }); } diff --git a/src/RemoteTech2/UI/FocusOverlay.cs b/src/RemoteTech2/UI/FocusOverlay.cs index 419f6bf..2dbc56b 100644 --- a/src/RemoteTech2/UI/FocusOverlay.cs +++ b/src/RemoteTech2/UI/FocusOverlay.cs @@ -35,10 +35,12 @@ private Rect PositionButton { if (!KnowledgeBase.Instance) return new Rect(0, 0, 0, 0); var position = KnowledgeBase.Instance.KnowledgeContainer.transform.position; - return new Rect(Screen.width - Texture.Satellite.width + (position.x - 613.5f), + var position2 = UIManager.instance.rayCamera.WorldToScreenPoint(position); + var rect = new Rect(position2.x + 154, 250 + 2 * 31, Texture.Satellite.width, Texture.Satellite.height); + return rect; } } diff --git a/src/RemoteTech2/UI/NetworkCone.cs b/src/RemoteTech2/UI/NetworkCone.cs index 9a3c989..ab42d2b 100644 --- a/src/RemoteTech2/UI/NetworkCone.cs +++ b/src/RemoteTech2/UI/NetworkCone.cs @@ -129,6 +129,7 @@ private void SetupMesh() mMeshFilter.mesh.vertices = new Vector3[8]; mMeshFilter.mesh.uv = new Vector2[8] { new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 1), new Vector2(1, 0), new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 1), new Vector2(1, 0) }; mMeshFilter.mesh.SetIndices(new int[] { 0, 2, 1, 2, 3, 1, 4, 6, 5, 6, 7, 5}, MeshTopology.Triangles, 0); + mMeshFilter.mesh.MarkDynamic(); Active = false; } diff --git a/src/RemoteTech2/UI/QueueFragment.cs b/src/RemoteTech2/UI/QueueFragment.cs index dfe6389..cca0f3c 100644 --- a/src/RemoteTech2/UI/QueueFragment.cs +++ b/src/RemoteTech2/UI/QueueFragment.cs @@ -82,7 +82,7 @@ public void Draw() mScrollPosition = GUILayout.BeginScrollView(mScrollPosition, GUILayout.Width(250)); { { - GUILayout.BeginVertical(GUI.skin.box); + GUILayout.BeginHorizontal(GUI.skin.box); { var s = new StringBuilder(); foreach (var c in mFlightComputer.ActiveCommands) @@ -90,8 +90,10 @@ public void Draw() s.Append(c.Description); } GUILayout.Label(s.ToString().TrimEnd(Environment.NewLine.ToCharArray())); + GUILayout.FlexibleSpace(); + RTUtil.Button("x", () => RTCore.Instance.StartCoroutine(OnClickReset()), GUILayout.Width(21), GUILayout.Height(21)); } - GUILayout.EndVertical(); + GUILayout.EndHorizontal(); foreach (var c in mFlightComputer.QueuedCommands) { @@ -99,7 +101,7 @@ public void Draw() { GUILayout.Label(c.Description); GUILayout.FlexibleSpace(); - RTUtil.Button("x", () => { RTCore.Instance.StartCoroutine(OnClickCancel(c)); }, GUILayout.Width(21), GUILayout.Height(21)); + RTUtil.Button("x", () => RTCore.Instance.StartCoroutine(OnClickCancel(c)), GUILayout.Width(21), GUILayout.Height(21)); } GUILayout.EndHorizontal(); } @@ -125,5 +127,11 @@ public IEnumerator OnClickCancel(ICommand c) yield return null; mFlightComputer.Enqueue(CancelCommand.WithCommand(c)); } + + public IEnumerator OnClickReset() + { + yield return null; + mFlightComputer.Enqueue(CancelCommand.ResetActive()); + } } } \ No newline at end of file