diff --git a/InfernalRobotics/InfernalRobotics/API/IRWrapper.cs b/InfernalRobotics/InfernalRobotics/API/IRWrapper.cs index 665f8951..263c38a3 100644 --- a/InfernalRobotics/InfernalRobotics/API/IRWrapper.cs +++ b/InfernalRobotics/InfernalRobotics/API/IRWrapper.cs @@ -208,6 +208,7 @@ private class IRControlGroup : IControlGroup private readonly object actualControlGroup; private PropertyInfo nameProperty; + private PropertyInfo vesselProperty; private PropertyInfo forwardKeyProperty; private PropertyInfo expandedProperty; private PropertyInfo speedProperty; @@ -230,6 +231,7 @@ public IRControlGroup(object cg) private void FindProperties() { nameProperty = IRControlGroupType.GetProperty("Name"); + vesselProperty = IRControlGroupType.GetProperty("Vessel"); forwardKeyProperty = IRControlGroupType.GetProperty("ForwardKey"); reverseKeyProperty = IRControlGroupType.GetProperty("ReverseKey"); speedProperty = IRControlGroupType.GetProperty("Speed"); @@ -255,6 +257,11 @@ public string Name set { nameProperty.SetValue(actualControlGroup, value, null); } } + public Vessel Vessel + { + get { return vesselProperty != null ? (Vessel)vesselProperty.GetValue(actualControlGroup, null) : null; } + } + public string ForwardKey { get { return (string)forwardKeyProperty.GetValue(actualControlGroup, null); } @@ -598,6 +605,9 @@ public interface IControlGroup : IEquatable { string Name { get; set; } + //can only be used in Flight, null checking is mandatory + Vessel Vessel { get; } + string ForwardKey { get; set; } string ReverseKey { get; set; } @@ -625,6 +635,8 @@ public interface IServo : IEquatable { string Name { get; set; } + uint UID { get; } + bool Highlight { set; } float Position { get; } diff --git a/InfernalRobotics/InfernalRobotics/Command/Interpolator.cs b/InfernalRobotics/InfernalRobotics/Command/Interpolator.cs index 026a24b4..b60b61e4 100644 --- a/InfernalRobotics/InfernalRobotics/Command/Interpolator.cs +++ b/InfernalRobotics/InfernalRobotics/Command/Interpolator.cs @@ -130,14 +130,6 @@ public void Update(float deltaT) return; } - if ((Math.Abs(Velocity) < maxDeltaVel) && // end conditions - (Math.Abs(targetPos - Position) < (2f * maxDeltaVel * deltaT))) - { // (generous to avoid oscillations) - Logger.Log(string.Format("pos={0} targetPos={1}, 2f*maxDeltaVel*dalteT={2}", Position, targetPos, (2f * maxDeltaVel * deltaT)), Logger.Level.SuperVerbose); - Position = targetPos; - return; - } - float newVel = Math.Min(CmdVelocity, MaxVelocity); if (!isSpeedMode) { @@ -148,6 +140,15 @@ public void Update(float deltaT) newVel = Math.Min(newVel, Velocity + maxDeltaVel); // acceleration limit newVel = Math.Max(newVel, Velocity - maxDeltaVel); + + if ((Math.Abs(Velocity) < maxDeltaVel) && // end conditions + (Math.Abs(targetPos - Position) < (2f * newVel * deltaT))) + { // (generous to avoid oscillations) + Logger.Log(string.Format("pos={0} targetPos={1}, 2f*maxDeltaVel*dalteT={2}", Position, targetPos, (2f * maxDeltaVel * deltaT)), Logger.Level.SuperVerbose); + Position = targetPos; + return; + } + Velocity = newVel; Position += Velocity * deltaT; diff --git a/InfernalRobotics/InfernalRobotics/Command/ServoController.cs b/InfernalRobotics/InfernalRobotics/Command/ServoController.cs index 24818c24..510bb952 100644 --- a/InfernalRobotics/InfernalRobotics/Command/ServoController.cs +++ b/InfernalRobotics/InfernalRobotics/Command/ServoController.cs @@ -8,14 +8,28 @@ namespace InfernalRobotics.Command { - [KSPAddon(KSPAddon.Startup.EveryScene, false)] + [KSPAddon(KSPAddon.Startup.Flight, false)] + public class ServoControllerFlight : ServoController + { + public override string AddonName { get { return this.name; } } + } + + [KSPAddon(KSPAddon.Startup.EditorAny, false)] + public class ServoControllerEditor : ServoController + { + public override string AddonName { get { return this.name; } } + } + public class ServoController : MonoBehaviour { + public virtual String AddonName { get; set; } + protected static bool UseElectricCharge = true; protected static ServoController ControllerInstance; public List ServoGroups; private int partCounter; + private int loadedVesselCounter = 0; public static ServoController Instance { get { return ControllerInstance; } } @@ -104,40 +118,6 @@ public static void RemoveServo(IServo servo) Logger.Log("[ServoController] AddServo finished successfully", Logger.Level.Debug); } - private void OnVesselChange(Vessel v) - { - Logger.Log(string.Format("[ServoController] vessel {0}", v.name)); - ServoGroups = null; - - var groups = new List(); - var groupMap = new Dictionary(); - - foreach (var servo in v.ToServos()) - { - if (!groupMap.ContainsKey(servo.Group.Name)) - { - groups.Add(new ControlGroup(servo)); - groupMap[servo.Group.Name] = groups.Count - 1; - } - else - { - ControlGroup g = groups[groupMap[servo.Group.Name]]; - g.AddControl(servo); - } - } - - Logger.Log(string.Format("[ServoController] {0} groups", groups.Count)); - - if (groups.Count > 0) - ServoGroups = groups; - - foreach (var servo in v.ToServos()) - { - servo.RawServo.SetupJoints(); - } - Logger.Log("[ServoController] OnVesselChange finished successfully", Logger.Level.Debug); - } - private void OnPartAttach(GameEvents.HostTargetAction hostTarget) { Part part = hostTarget.host; @@ -224,9 +204,88 @@ private void OnEditorShipModified(ShipConstruct ship) Logger.Log("[ServoController] OnEditorShipModified finished successfully", Logger.Level.Debug); } + private void OnEditorRestart() + { + ServoGroups = null; + Logger.Log ("OnEditorRestart called", Logger.Level.Debug); + } + + private void OnEditorLoad(ShipConstruct s, CraftBrowser.LoadType t) + { + OnEditorShipModified (s); + Logger.Log ("OnEditorLoad called", Logger.Level.Debug); + } + /// + /// Rebuilds the servo groups. Only works in flight. + /// + private void RebuildServoGroups() + { + ServoGroups = new List(); + + for(int i=0; i(); + var groupMap = new Dictionary(); + + foreach(var servo in vessel.ToServos()) + { + if (!groupMap.ContainsKey(servo.Group.Name)) + { + groups.Add(new ControlGroup(servo, vessel)); + groupMap[servo.Group.Name] = groups.Count - 1; + } + else + { + ControlGroup g = groups[groupMap[servo.Group.Name]]; + g.AddControl(servo); + } + } + + ServoGroups.AddRange (groups); + } + + if (ServoGroups.Count == 0) + ServoGroups = null; + } + + private void OnVesselChange(Vessel v) + { + Logger.Log(string.Format("[ServoController] vessel {0}", v.name)); + + RebuildServoGroups (); + + foreach (var servo in v.ToServos()) + { + servo.RawServo.SetupJoints(); + } + Logger.Log("[ServoController] OnVesselChange finished successfully", Logger.Level.Debug); + } + + private void OnVesselWasModified(Vessel v) + { + RebuildServoGroups (); + } + + private void OnVesselLoaded (Vessel v) + { + Logger.Log("[ServoController] OnVesselLoaded, v=" + v.GetName()); + RebuildServoGroups (); + } + + private void OnVesselUnloaded (Vessel v) + { + Logger.Log("[ServoController] OnVesselUnloaded, v=" + v.GetName()); + RebuildServoGroups (); + } + private void Awake() { - Logger.Log("[ServoController] awake"); + Logger.Log("[ServoController] awake, AddonName = " + this.AddonName); GameScenes scene = HighLogic.LoadedScene; @@ -234,6 +293,9 @@ private void Awake() { GameEvents.onVesselChange.Add(OnVesselChange); GameEvents.onVesselWasModified.Add(OnVesselWasModified); + GameEvents.onVesselLoaded.Add (OnVesselLoaded); + GameEvents.onVesselDestroy.Add (OnVesselUnloaded); + GameEvents.onVesselGoOnRails.Add (OnVesselUnloaded); ControllerInstance = this; } else if (scene == GameScenes.EDITOR) @@ -250,28 +312,19 @@ private void Awake() ControllerInstance = null; } - Logger.Log("[ServoController] awake finished successfully", Logger.Level.Debug); - } - - private void OnEditorRestart() - { - ServoGroups = null; - Logger.Log ("OnEditorRestart called", Logger.Level.Debug); - } - - private void OnEditorLoad(ShipConstruct s, CraftBrowser.LoadType t) - { - OnEditorShipModified (s); - Logger.Log ("OnEditorLoad called", Logger.Level.Debug); + Logger.Log("[ServoController] awake finished successfully, AddonName = " + this.AddonName, Logger.Level.Debug); } - private void OnVesselWasModified(Vessel v) + private void FixedUpdate() { - if (v == FlightGlobals.ActiveVessel) + //because OnVesselDestroy and OnVesselGoOnRails seem to only work for active vessel I had to build this stupid workaround + if(HighLogic.LoadedSceneIsFlight) { - ServoGroups = null; - - OnVesselChange(v); + if(FlightGlobals.Vessels.Count(v => v.loaded) != loadedVesselCounter) + { + RebuildServoGroups (); + loadedVesselCounter = FlightGlobals.Vessels.Count(v => v.loaded); + } } } @@ -286,6 +339,10 @@ private void OnDestroy() GameEvents.onEditorShipModified.Remove(OnEditorShipModified); GameEvents.onEditorLoad.Remove(OnEditorLoad); GameEvents.onEditorRestart.Remove(OnEditorRestart); + + GameEvents.onVesselLoaded.Remove (OnVesselLoaded); + GameEvents.onVesselDestroy.Remove (OnVesselUnloaded); + GameEvents.onVesselGoOnRails.Remove (OnVesselUnloaded); Logger.Log("[ServoController] OnDestroy finished successfully", Logger.Level.Debug); } @@ -297,6 +354,13 @@ public class ControlGroup private string forwardKey; private string reverseKey; private readonly List servos; + private readonly Vessel vessel; + + public ControlGroup(IServo servo, Vessel v) + : this(servo) + { + vessel = v; + } public ControlGroup(IServo servo) : this() @@ -337,6 +401,11 @@ public IList Servos get { return servos; } } + public Vessel Vessel + { + get { return vessel; } + } + public string ForwardKey { get { return forwardKey; } diff --git a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs index a0295106..55c28bec 100644 --- a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs +++ b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs @@ -292,271 +292,316 @@ private void OnDestroy() Logger.Log("[GUI] OnDestroy finished successfully", Logger.Level.Debug); } - //servo control window used in flight - private void ControlWindow(int windowID) + private void DrawControlGroup (ServoController.ControlGroup g) { - GUILayoutOption width20 = GUILayout.Width(20); - - GUILayout.BeginVertical(); - const int BUTTON_HEIGHT = 22; - //use of for instead of foreach in intentional - for (int i = 0; i < ServoController.Instance.ServoGroups.Count; i++) + if (g.Servos.Any()) { - ServoController.ControlGroup g = ServoController.Instance.ServoGroups[i]; + GUILayout.BeginHorizontal(); - if (g.Servos.Any()) + if (g.Expanded) { - GUILayout.BeginHorizontal(); + g.Expanded = !GUILayout.Button(TextureLoader.CollapseIcon, buttonStyle, GUILayout.Width(20), GUILayout.Height(BUTTON_HEIGHT)); + } + else + { + g.Expanded = GUILayout.Button(TextureLoader.ExpandIcon, buttonStyle, GUILayout.Width(20), GUILayout.Height(BUTTON_HEIGHT)); + } - if (g.Expanded) - { - g.Expanded = !GUILayout.Button(TextureLoader.CollapseIcon, buttonStyle, width20, GUILayout.Height(BUTTON_HEIGHT)); - } - else - { - g.Expanded = GUILayout.Button(TextureLoader.ExpandIcon, buttonStyle, width20, GUILayout.Height(BUTTON_HEIGHT)); - } + nameStyle.fontStyle = FontStyle.Bold; - nameStyle.fontStyle = FontStyle.Bold; + GUILayout.Label(g.Name, nameStyle, GUILayout.ExpandWidth(true), GUILayout.Height(BUTTON_HEIGHT)); - GUILayout.Label(g.Name, nameStyle, GUILayout.ExpandWidth(true), GUILayout.Height(BUTTON_HEIGHT)); + nameStyle.fontStyle = FontStyle.Normal; - nameStyle.fontStyle = FontStyle.Normal; + g.Speed = GUILayout.TextField(g.Speed, GUILayout.Width(30), GUILayout.Height(BUTTON_HEIGHT)); - g.Speed = GUILayout.TextField(g.Speed, GUILayout.Width(30), GUILayout.Height(BUTTON_HEIGHT)); + Rect last = GUILayoutUtility.GetLastRect(); + Vector2 pos = Event.current.mousePosition; + if (last.Contains(pos) && Event.current.type == EventType.Repaint) + tooltipText = "Speed Multiplier"; - Rect last = GUILayoutUtility.GetLastRect(); - Vector2 pos = Event.current.mousePosition; - if (last.Contains(pos) && Event.current.type == EventType.Repaint) - tooltipText = "Speed Multiplier"; + bool toggleVal = GUILayout.Toggle(g.MovingNegative, new GUIContent(TextureLoader.LeftToggleIcon, "Toggle Move -"), buttonStyle, + GUILayout.Width(28), GUILayout.Height(BUTTON_HEIGHT)); - bool toggleVal = GUILayout.Toggle(g.MovingNegative, new GUIContent(TextureLoader.LeftToggleIcon, "Toggle Move -"), buttonStyle, - GUILayout.Width(28), GUILayout.Height(BUTTON_HEIGHT)); + SetTooltipText(); - SetTooltipText(); + if (g.MovingNegative != toggleVal) + { + if (!toggleVal) g.Stop(); + g.MovingNegative = toggleVal; + } - if (g.MovingNegative != toggleVal) - { - if (!toggleVal) g.Stop(); - g.MovingNegative = toggleVal; - } + if (g.MovingNegative) + { + g.MovingPositive = false; + g.MoveLeft(); + } - if (g.MovingNegative) + if (guiPresetMode) + { + if (GUILayout.Button(new GUIContent(TextureLoader.PrevIcon, "Previous Preset"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) { + //reset any group toggles + g.MovingNegative = false; g.MovingPositive = false; - g.MoveLeft(); + + g.MovePrevPreset(); } + SetTooltipText(); - if (guiPresetMode) + if (GUILayout.Button(new GUIContent(TextureLoader.AutoRevertIcon, "Reset"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) { - if (GUILayout.Button(new GUIContent(TextureLoader.PrevIcon, "Previous Preset"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) - { - //reset any group toggles - g.MovingNegative = false; - g.MovingPositive = false; - - g.MovePrevPreset(); - } - SetTooltipText(); - - if (GUILayout.Button(new GUIContent(TextureLoader.AutoRevertIcon, "Reset"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) - { - //reset any group toggles - g.MovingNegative = false; - g.MovingPositive = false; + //reset any group toggles + g.MovingNegative = false; + g.MovingPositive = false; - g.MoveCenter(); - } - SetTooltipText(); + g.MoveCenter(); + } + SetTooltipText(); - if (GUILayout.Button(new GUIContent(TextureLoader.NextIcon, "Next Preset"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) - { - //reset any group toggles - g.MovingNegative = false; - g.MovingPositive = false; + if (GUILayout.Button(new GUIContent(TextureLoader.NextIcon, "Next Preset"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) + { + //reset any group toggles + g.MovingNegative = false; + g.MovingPositive = false; - g.MoveNextPreset(); - } - SetTooltipText(); + g.MoveNextPreset(); } - else + SetTooltipText(); + } + else + { + if (GUILayout.RepeatButton(new GUIContent(TextureLoader.LeftIcon, "Hold to Move -"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) { - if (GUILayout.RepeatButton(new GUIContent(TextureLoader.LeftIcon, "Hold to Move -"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) - { - g.MovingNegative = false; - g.MovingPositive = false; - - g.MoveLeft(); - - g.ButtonDown = true; - } - - SetTooltipText(); - - if (GUILayout.RepeatButton(new GUIContent(TextureLoader.RevertIcon, "Hold to Center"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) - { - g.MovingNegative = false; - g.MovingPositive = false; - - g.MoveCenter(); - - g.ButtonDown = true; - } - SetTooltipText(); - - if (GUILayout.RepeatButton(new GUIContent(TextureLoader.RightIcon, "Hold to Move +"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) - { - g.MovingNegative = false; - g.MovingPositive = false; + g.MovingNegative = false; + g.MovingPositive = false; - g.MoveRight(); + g.MoveLeft(); - g.ButtonDown = true; - } - SetTooltipText(); + g.ButtonDown = true; } - toggleVal = GUILayout.Toggle(g.MovingPositive, new GUIContent(TextureLoader.RightToggleIcon, "Toggle Move +"), buttonStyle, - GUILayout.Width(28), GUILayout.Height(BUTTON_HEIGHT)); SetTooltipText(); - if (g.MovingPositive != toggleVal) + if (GUILayout.RepeatButton(new GUIContent(TextureLoader.RevertIcon, "Hold to Center"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) { - if (!toggleVal) g.Stop(); - g.MovingPositive = toggleVal; + g.MovingNegative = false; + g.MovingPositive = false; + + g.MoveCenter(); + + g.ButtonDown = true; } + SetTooltipText(); - if (g.MovingPositive) + if (GUILayout.RepeatButton(new GUIContent(TextureLoader.RightIcon, "Hold to Move +"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) { g.MovingNegative = false; + g.MovingPositive = false; + g.MoveRight(); + + g.ButtonDown = true; } + SetTooltipText(); + } + + toggleVal = GUILayout.Toggle(g.MovingPositive, new GUIContent(TextureLoader.RightToggleIcon, "Toggle Move +"), buttonStyle, + GUILayout.Width(28), GUILayout.Height(BUTTON_HEIGHT)); + SetTooltipText(); + + if (g.MovingPositive != toggleVal) + { + if (!toggleVal) g.Stop(); + g.MovingPositive = toggleVal; + } + + if (g.MovingPositive) + { + g.MovingNegative = false; + g.MoveRight(); + } + + GUILayout.EndHorizontal(); + if (g.Expanded) + { + GUILayout.BeginHorizontal(GUILayout.Height(5)); GUILayout.EndHorizontal(); - if (g.Expanded) + foreach (var servo in g.Servos) { - GUILayout.BeginHorizontal(GUILayout.Height(5)); - GUILayout.EndHorizontal(); - - foreach (var servo in g.Servos) - { - GUILayout.BeginHorizontal(); + GUILayout.BeginHorizontal(); - string servoStatus = servo.Mechanism.IsMoving ? "■" : "■"; + string servoStatus = servo.Mechanism.IsMoving ? "■" : "■"; - if (servo.Mechanism.IsLocked) - servoStatus = "■"; + if (servo.Mechanism.IsLocked) + servoStatus = "■"; - GUILayout.Label(servoStatus, dotStyle, GUILayout.Width(20), GUILayout.Height(BUTTON_HEIGHT)); + GUILayout.Label(servoStatus, dotStyle, GUILayout.Width(20), GUILayout.Height(BUTTON_HEIGHT)); - GUILayout.Label(servo.Name, nameStyle, GUILayout.ExpandWidth(true), GUILayout.Height(BUTTON_HEIGHT)); + GUILayout.Label(servo.Name, nameStyle, GUILayout.ExpandWidth(true), GUILayout.Height(BUTTON_HEIGHT)); - nameStyle.fontStyle = FontStyle.Italic; - nameStyle.alignment = TextAnchor.MiddleCenter; + nameStyle.fontStyle = FontStyle.Italic; + nameStyle.alignment = TextAnchor.MiddleCenter; - GUILayout.Label(string.Format("{0:#0.##}", servo.Mechanism.Position), servo.Mechanism.IsAxisInverted ? invPosStyle : nameStyle, GUILayout.Width(45), GUILayout.Height(BUTTON_HEIGHT)); + GUILayout.Label(string.Format("{0:#0.##}", servo.Mechanism.Position), servo.Mechanism.IsAxisInverted ? invPosStyle : nameStyle, GUILayout.Width(45), GUILayout.Height(BUTTON_HEIGHT)); - nameStyle.fontStyle = FontStyle.Normal; - nameStyle.alignment = TextAnchor.MiddleLeft; + nameStyle.fontStyle = FontStyle.Normal; + nameStyle.alignment = TextAnchor.MiddleLeft; - bool servoLocked = servo.Mechanism.IsLocked; - servoLocked = GUILayout.Toggle(servoLocked, - servoLocked ? new GUIContent(TextureLoader.LockedIcon, "Unlock Servo") : new GUIContent(TextureLoader.UnlockedIcon, "Lock Servo"), - buttonStyle, GUILayout.Width(28), GUILayout.Height(BUTTON_HEIGHT)); - servo.Mechanism.IsLocked = servoLocked; + bool servoLocked = servo.Mechanism.IsLocked; + servoLocked = GUILayout.Toggle(servoLocked, + servoLocked ? new GUIContent(TextureLoader.LockedIcon, "Unlock Servo") : new GUIContent(TextureLoader.UnlockedIcon, "Lock Servo"), + buttonStyle, GUILayout.Width(28), GUILayout.Height(BUTTON_HEIGHT)); + servo.Mechanism.IsLocked = servoLocked; - SetTooltipText(); + SetTooltipText(); - if (guiPresetMode) + if (guiPresetMode) + { + if (GUILayout.Button(new GUIContent(TextureLoader.PrevIcon, "Previous Preset"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) { - if (GUILayout.Button(new GUIContent(TextureLoader.PrevIcon, "Previous Preset"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) - { - //reset any group toggles - g.MovingNegative = false; - g.MovingPositive = false; + //reset any group toggles + g.MovingNegative = false; + g.MovingPositive = false; - servo.Preset.MovePrev(); - } - SetTooltipText(); + servo.Preset.MovePrev(); + } + SetTooltipText(); - var rowHeight = GUILayout.Height(BUTTON_HEIGHT); - DrawEditPresetButton(servo, buttonStyle, rowHeight); - SetTooltipText(); + var rowHeight = GUILayout.Height(BUTTON_HEIGHT); + DrawEditPresetButton(servo, buttonStyle, rowHeight); + SetTooltipText(); - if (GUILayout.Button(new GUIContent(TextureLoader.NextIcon, "Next Preset"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) - { - //reset any group toggles - g.MovingNegative = false; - g.MovingPositive = false; + if (GUILayout.Button(new GUIContent(TextureLoader.NextIcon, "Next Preset"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) + { + //reset any group toggles + g.MovingNegative = false; + g.MovingPositive = false; - servo.Preset.MoveNext(); - } - SetTooltipText(); + servo.Preset.MoveNext(); } - else + SetTooltipText(); + } + else + { + if (GUILayout.RepeatButton(new GUIContent(TextureLoader.LeftIcon, "Hold to Move -"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) { - if (GUILayout.RepeatButton(new GUIContent(TextureLoader.LeftIcon, "Hold to Move -"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) - { - //reset any group toggles - g.MovingNegative = false; - g.MovingPositive = false; - g.ButtonDown = true; + //reset any group toggles + g.MovingNegative = false; + g.MovingPositive = false; + g.ButtonDown = true; - servo.Mechanism.MoveLeft(); - } - SetTooltipText(); - - if (GUILayout.RepeatButton(new GUIContent(TextureLoader.RevertIcon, "Hold to Center"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) - { - //reset any group toggles - g.MovingNegative = false; - g.MovingPositive = false; - g.ButtonDown = true; - - servo.Mechanism.MoveCenter(); - } - SetTooltipText(); + servo.Mechanism.MoveLeft(); + } + SetTooltipText(); - if (GUILayout.RepeatButton(new GUIContent(TextureLoader.RightIcon, "Hold to Move +"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) - { - //reset any group toggles - g.MovingNegative = false; - g.MovingPositive = false; - g.ButtonDown = true; + if (GUILayout.RepeatButton(new GUIContent(TextureLoader.RevertIcon, "Hold to Center"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) + { + //reset any group toggles + g.MovingNegative = false; + g.MovingPositive = false; + g.ButtonDown = true; - servo.Mechanism.MoveRight(); - } - SetTooltipText(); + servo.Mechanism.MoveCenter(); } - bool servoInverted = servo.Mechanism.IsAxisInverted; + SetTooltipText(); - servoInverted = GUILayout.Toggle(servoInverted, - servoInverted ? new GUIContent(TextureLoader.InvertedIcon, "Un-invert Axis") : new GUIContent(TextureLoader.NoninvertedIcon, "Invert Axis"), - buttonStyle, GUILayout.Width(28), GUILayout.Height(BUTTON_HEIGHT)); + if (GUILayout.RepeatButton(new GUIContent(TextureLoader.RightIcon, "Hold to Move +"), buttonStyle, GUILayout.Width(22), GUILayout.Height(BUTTON_HEIGHT))) + { + //reset any group toggles + g.MovingNegative = false; + g.MovingPositive = false; + g.ButtonDown = true; + servo.Mechanism.MoveRight(); + } SetTooltipText(); + } + bool servoInverted = servo.Mechanism.IsAxisInverted; - servo.Mechanism.IsAxisInverted = servoInverted; + servoInverted = GUILayout.Toggle(servoInverted, + servoInverted ? new GUIContent(TextureLoader.InvertedIcon, "Un-invert Axis") : new GUIContent(TextureLoader.NoninvertedIcon, "Invert Axis"), + buttonStyle, GUILayout.Width(28), GUILayout.Height(BUTTON_HEIGHT)); - GUILayout.EndHorizontal(); - } + SetTooltipText(); + + servo.Mechanism.IsAxisInverted = servoInverted; - GUILayout.BeginHorizontal(GUILayout.Height(5)); GUILayout.EndHorizontal(); } - if (g.ButtonDown && Input.GetMouseButtonUp(0)) - { - //one of the repeat buttons in the group was pressed, but now mouse button is up - g.ButtonDown = false; - g.Stop(); - } + GUILayout.BeginHorizontal(GUILayout.Height(5)); + GUILayout.EndHorizontal(); + } + + if (g.ButtonDown && Input.GetMouseButtonUp(0)) + { + //one of the repeat buttons in the group was pressed, but now mouse button is up + g.ButtonDown = false; + g.Stop(); + } + } + } + + //servo control window used in flight + private void ControlWindow(int windowID) + { + GUILayout.BeginVertical(); + + //use of for instead of foreach in intentional + //assuming that ServoGroups are sorted by vessel due to the way they are created + + for (int i = 0; i < ServoController.Instance.ServoGroups.Count; i++) + { + ServoController.ControlGroup g = ServoController.Instance.ServoGroups[i]; + + if (HighLogic.LoadedSceneIsFlight && FlightGlobals.ActiveVessel != g.Vessel) + continue; + /*if (i==0) + { + GUILayout.BeginHorizontal(); + nameStyle.fontStyle = FontStyle.Bold; + GUILayout.Label(g.Vessel.GetName() + (g.Vessel == FlightGlobals.ActiveVessel ? " (Active)" : ""), nameStyle, GUILayout.ExpandWidth(true), GUILayout.Height(22)); + nameStyle.fontStyle = FontStyle.Normal; + GUILayout.EndHorizontal(); + GUILayout.BeginHorizontal(GUILayout.Height(5)); + GUILayout.EndHorizontal(); + GUILayout.BeginHorizontal(); + GUILayout.Space (10); + GUILayout.BeginVertical(); + + } + else if (g.Vessel != ServoController.Instance.ServoGroups[i-1].Vessel) + { + GUILayout.EndVertical(); + GUILayout.EndHorizontal(); + GUILayout.BeginHorizontal(); + nameStyle.fontStyle = FontStyle.Bold; + GUILayout.Label(g.Vessel.GetName() + (g.Vessel == FlightGlobals.ActiveVessel ? " (Active)" : ""), nameStyle, GUILayout.ExpandWidth(true), GUILayout.Height(22)); + nameStyle.fontStyle = FontStyle.Normal; + GUILayout.EndHorizontal(); + GUILayout.BeginHorizontal(GUILayout.Height(5)); + GUILayout.EndHorizontal(); + GUILayout.BeginHorizontal(); + GUILayout.Space (10); + GUILayout.BeginVertical(); + } + */ + DrawControlGroup (g); + /* + if (i==ServoController.Instance.ServoGroups.Count - 1) + { + GUILayout.EndVertical(); + GUILayout.EndHorizontal(); } + */ } + GUILayout.BeginHorizontal(GUILayout.Height(32)); if (GUILayout.Button(guiGroupEditorEnabled ? "Close Edit" : "Edit Groups", GUILayout.Height(32))) @@ -776,6 +821,9 @@ private void EditorWindow(int windowID) { ServoController.ControlGroup grp = ServoController.Instance.ServoGroups[i]; + if (HighLogic.LoadedSceneIsFlight && FlightGlobals.ActiveVessel != grp.Vessel) + continue; + GUILayout.BeginHorizontal(); //Call the Add Group Handle code