From 0f9e7b9242cc673ed488f012ff3dc4626698ee08 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Mon, 11 May 2015 12:08:52 +0100 Subject: [PATCH 01/19] Fixes to TweakScale Interaction and Symmetry --- .../InfernalRobotics/Command/Translator.cs | 3 ++ .../InfernalRobotics/Control/IMechanism.cs | 5 +++ .../Control/Servo/MechanismBase.cs | 14 +++++++ .../Control/Servo/ServoPreset.cs | 2 +- .../InfernalRobotics/Gui/ControlsGUI.cs | 5 +++ .../InfernalRobotics/Module/MuMechToggle.cs | 38 +++++++----------- .../Properties/AssemblyInfo.cs | 4 +- .../InfernalRobotics/Utility/TextureLoader.cs | 4 ++ .../MagicSmokeIndustries/Textures/clone.png | Bin 0 -> 273 bytes 9 files changed, 49 insertions(+), 26 deletions(-) create mode 100755 Resources/GameData/MagicSmokeIndustries/Textures/clone.png diff --git a/InfernalRobotics/InfernalRobotics/Command/Translator.cs b/InfernalRobotics/InfernalRobotics/Command/Translator.cs index 83de12bf..08519f77 100644 --- a/InfernalRobotics/InfernalRobotics/Command/Translator.cs +++ b/InfernalRobotics/InfernalRobotics/Command/Translator.cs @@ -30,6 +30,9 @@ public void Init(bool motionLock, IServo servo, Interpolator interpolator) public bool IsMotionLock { get; set; } public float GetSpeedUnit() { + if (servo == null) + return 0f; + return servo.Mechanism.DefaultSpeed; } diff --git a/InfernalRobotics/InfernalRobotics/Control/IMechanism.cs b/InfernalRobotics/InfernalRobotics/Control/IMechanism.cs index 03bfff95..38245e97 100644 --- a/InfernalRobotics/InfernalRobotics/Control/IMechanism.cs +++ b/InfernalRobotics/InfernalRobotics/Control/IMechanism.cs @@ -111,5 +111,10 @@ public interface IMechanism /// Used in the editor to reset a parts state to Default /// void Reset(); + + /// + /// Used in the editor to apply Servo limits to symmetry counterparts + /// + void ApplyLimitsToSymmetry(); } } \ No newline at end of file diff --git a/InfernalRobotics/InfernalRobotics/Control/Servo/MechanismBase.cs b/InfernalRobotics/InfernalRobotics/Control/Servo/MechanismBase.cs index bd43986b..19e37e58 100644 --- a/InfernalRobotics/InfernalRobotics/Control/Servo/MechanismBase.cs +++ b/InfernalRobotics/InfernalRobotics/Control/Servo/MechanismBase.cs @@ -161,5 +161,19 @@ public void Reconfigure() } public abstract void Reset(); + + public void ApplyLimitsToSymmetry() + { + foreach (Part counterPart in RawServo.part.symmetryCounterparts) + { + ((MuMechToggle) counterPart.Modules["MuMechToggle"]).rotateMin = RawServo.rotateMin; + ((MuMechToggle) counterPart.Modules["MuMechToggle"]).rotateMax = RawServo.rotateMax; + ((MuMechToggle) counterPart.Modules["MuMechToggle"]).translateMin = RawServo.translateMin; + ((MuMechToggle) counterPart.Modules["MuMechToggle"]).translateMax = RawServo.translateMax; + ((MuMechToggle) counterPart.Modules["MuMechToggle"]).minTweak = RawServo.minTweak; + ((MuMechToggle) counterPart.Modules["MuMechToggle"]).maxTweak = RawServo.maxTweak; + } + Logger.Log ("ApplyingSymmetry, number of counterparts: " + RawServo.part.symmetryCounterparts.Count, Logger.Level.Debug); + } } } \ No newline at end of file diff --git a/InfernalRobotics/InfernalRobotics/Control/Servo/ServoPreset.cs b/InfernalRobotics/InfernalRobotics/Control/Servo/ServoPreset.cs index 4f63cef7..02c870d2 100644 --- a/InfernalRobotics/InfernalRobotics/Control/Servo/ServoPreset.cs +++ b/InfernalRobotics/InfernalRobotics/Control/Servo/ServoPreset.cs @@ -50,7 +50,7 @@ public void Save(bool symmetry = false) rawServo.presetPositionsSerialized = rawServo.SerializePresets(); - if (symmetry && rawServo.part.symmetryCounterparts.Count > 1) + if (symmetry && rawServo.part.symmetryCounterparts.Count >= 1) { foreach (Part part in rawServo.part.symmetryCounterparts) { diff --git a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs index 70a26487..512e9c33 100644 --- a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs +++ b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs @@ -995,6 +995,11 @@ private void EditorWindow(int windowID) SetTooltipText(); servo.Mechanism.IsAxisInverted = servoInverted; + + if (GUILayout.Button(new GUIContent(TextureLoader.CloneIcon, "Apply Symmetry"), buttonStyle, GUILayout.Width(28), rowHeight)) + { + servo.Mechanism.ApplyLimitsToSymmetry (); + } } if (isEditor) diff --git a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs index 690a74c3..e11a0e51 100644 --- a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs +++ b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs @@ -9,10 +9,11 @@ using KSP.IO; using KSPAPIExtensions; using UnityEngine; +using TweakScale; namespace InfernalRobotics.Module { - public class MuMechToggle : PartModule + public class MuMechToggle : PartModule, IRescalable { private const string ELECTRIC_CHARGE_RESOURCE_NAME = "ElectricCharge"; @@ -922,7 +923,7 @@ protected void DoTranslation() } } - public void OnRescale(float factor) + public void OnRescale(ScalingFactor factor) { if (rotateJoint) return; @@ -933,35 +934,24 @@ public void OnRescale(float factor) //translateMin *= factor; //translateMax *= factor; - minTweak *= factor; - maxTweak *= factor; + minTweak *= factor.relative.linear; + maxTweak *= factor.relative.linear; // The part center is the origin of the moving mesh // so if translation!=0, the fixed mesh moves on rescale. // We need to move the part back so the fixed mesh stays at the same place. - transform.Translate(-translateAxis * translation * (factor-1f) ); + transform.Translate(-translateAxis * translation * (factor.relative.linear-1f) ); if (HighLogic.LoadedSceneIsEditor) - translation *= factor; + translation *= factor.relative.linear; // update the window so the new limits are applied UpdateMinMaxTweaks(); - UIPartActionWindow[] actionWindows = FindObjectsOfType(); - if (actionWindows.Length > 0) - { - foreach (UIPartActionWindow actionWindow in actionWindows) - { - if (actionWindow.part == part) - { - TweakWindow = actionWindow; - TweakIsDirty = true; - } - } - } - else - { - TweakWindow = null; - } + + TweakWindow = part.FindActionWindow (); + TweakIsDirty = true; + + Logger.Log ("OnRescale called, TweakWindow is null? = " + (TweakWindow == null), Logger.Level.Debug); } @@ -972,12 +962,14 @@ public void RefreshTweakUI() UpdateMinMaxTweaks(); - if (part.symmetryCounterparts.Count > 1) + if (part.symmetryCounterparts.Count >= 1) { foreach (Part counterPart in part.symmetryCounterparts) { ((MuMechToggle) counterPart.Modules["MuMechToggle"]).rotateMin = rotateMin; ((MuMechToggle) counterPart.Modules["MuMechToggle"]).rotateMax = rotateMax; + ((MuMechToggle) counterPart.Modules["MuMechToggle"]).translateMin = translateMin; + ((MuMechToggle) counterPart.Modules["MuMechToggle"]).translateMax = translateMax; ((MuMechToggle) counterPart.Modules["MuMechToggle"]).minTweak = minTweak; ((MuMechToggle) counterPart.Modules["MuMechToggle"]).maxTweak = maxTweak; } diff --git a/InfernalRobotics/InfernalRobotics/Properties/AssemblyInfo.cs b/InfernalRobotics/InfernalRobotics/Properties/AssemblyInfo.cs index e07eb763..f0c4e935 100644 --- a/InfernalRobotics/InfernalRobotics/Properties/AssemblyInfo.cs +++ b/InfernalRobotics/InfernalRobotics/Properties/AssemblyInfo.cs @@ -35,5 +35,5 @@ // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.19.0.0")] -[assembly: AssemblyFileVersion("0.19.0.0")] \ No newline at end of file +[assembly: AssemblyVersion("0.21.3.0")] +[assembly: AssemblyFileVersion("0.21.3.0")] \ No newline at end of file diff --git a/InfernalRobotics/InfernalRobotics/Utility/TextureLoader.cs b/InfernalRobotics/InfernalRobotics/Utility/TextureLoader.cs index 2cd64c20..607fae3e 100644 --- a/InfernalRobotics/InfernalRobotics/Utility/TextureLoader.cs +++ b/InfernalRobotics/InfernalRobotics/Utility/TextureLoader.cs @@ -31,6 +31,7 @@ public class TextureLoader internal static Texture2D NoninvertedIcon { get; private set; } internal static Texture2D NextIcon { get; private set; } internal static Texture2D PrevIcon { get; private set; } + internal static Texture2D CloneIcon { get; private set; } protected static TextureLoader LoaderInstance; @@ -113,6 +114,9 @@ public static void InitTextures() PrevIcon = new Texture2D(32, 32, TextureFormat.ARGB32, false); LoadImageFromFile(PrevIcon, "prev.png"); + CloneIcon = new Texture2D(32, 32, TextureFormat.ARGB32, false); + LoadImageFromFile(CloneIcon, "clone.png"); + isReady = true; } } diff --git a/Resources/GameData/MagicSmokeIndustries/Textures/clone.png b/Resources/GameData/MagicSmokeIndustries/Textures/clone.png new file mode 100755 index 0000000000000000000000000000000000000000..1829b24793fbdb55a22c91f9233ca0e682e667b2 GIT binary patch literal 273 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdzmSQK*5Dp-y;YjHK@;M7UB8wRq z7?VJlF<$*bFi=pk#5JNMI6tkVJh3R1p}f3YFEcN@I61K(RWH9NefB#WDWIZ0PZ!6K zh}O4L5ArrB@VIhUlnBcnx+u$J{;;d!Q~n@* z_5FpGeAUc02PVuqbG0HUc;7zWDM491FS6Op+O(})r#)bMz_@Dmh4$K$vDs7N=05}T zy(e6{rQ$GIZo@hDb? Date: Mon, 11 May 2015 13:50:28 +0100 Subject: [PATCH 02/19] Minor clean-up --- .../InfernalRobotics/Gui/ControlsGUI.cs | 56 +------------------ .../InfernalRobotics/Module/MuMechToggle.cs | 14 ++--- 2 files changed, 8 insertions(+), 62 deletions(-) diff --git a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs index 512e9c33..ed8d3dba 100644 --- a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs +++ b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs @@ -618,31 +618,7 @@ private void DrawTooltip() private void DrawPresetSelector(IServo servo, GUILayoutOption rowHeight) { - //commented out parts are for Future experimental UI with mouse hover - /*var gc = new GUIContent (string.Format ("{0:#0.##}", servo.Mechanism.Position), "Add preset"); - - var customStyle = new GUIStyle (GUI.skin.button); - customStyle.normal.textColor = servo.Mechanism.IsAxisInverted ? Color.yellow : Color.white; - customStyle.alignment = TextAnchor.MiddleCenter; - customStyle.fontSize = 12; - customStyle.fontStyle = servo.Mechanism.IsAxisInverted ? FontStyle.Italic : FontStyle.Normal; - customStyle.padding = new RectOffset(2, 2, 2, 2); - customStyle.fixedWidth = 40; - customStyle.fixedHeight = 22; - - var isPresetPosition = false; - var presetIndex = -1; - - for (int i = 0; i < servo.Preset.Count; i++) - { - if (Math.Abs (servo.Preset [i] - servo.Mechanism.Position) < 0.00001) - { - isPresetPosition = true; - presetIndex = i; - break; - } - } - */ + int floor, ceiling; servo.Preset.GetNearestPresets(out floor, out ceiling); @@ -654,34 +630,6 @@ private void DrawPresetSelector(IServo servo, GUILayoutOption rowHeight) } SetTooltipText(); - /*var rect = GUILayoutUtility.GetRect(gc, customStyle); - if (rect.Contains(Event.current.mousePosition)) - { - if (isPresetPosition) - { - gc.image = TextureLoader.TrashIcon; - gc.text = ""; - gc.tooltip = "Delete Preset"; - tooltipText = gc.tooltip; - } - else - { - gc.text = "Add"; - } - } - - if (GUI.Button(rect, gc, customStyle)) - { - if (isPresetPosition) - { - servo.Preset.RemoveAt (presetIndex); - } - else - { - servo.Preset.Add (servo.Mechanism.Position); - } - } - */ DrawServoPosition(servo, rowHeight); if (GUILayout.Button(new GUIContent(TextureLoader.NextIcon, "Next Preset" + ((ceiling >= 0) ? ": " + servo.Preset[ceiling] : "")), @@ -1170,7 +1118,7 @@ private void PresetsEditWindow(int windowID) associatedServo.Preset.Save(true); } - if (GUILayout.Button("Save&Exit", buttonStyle, GUILayout.Width(70))) + if (GUILayout.Button("Close", buttonStyle, GUILayout.Width(70))) { associatedServo.Preset.Save(); guiPresetsEnabled = false; diff --git a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs index e11a0e51..29f4d577 100644 --- a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs +++ b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs @@ -347,6 +347,9 @@ public override void OnAwake() try { + Events["InvertAxisToggle"].guiName = invertAxis ? "Un-invert Axis" : "Invert Axis"; + Events["MotionLockToggle"].guiName = isMotionLock ? "Disengage Lock" : "Engage Lock"; + if (rotateJoint) { minTweak = rotateMin; @@ -355,6 +358,7 @@ public override void OnAwake() if (limitTweakable) { Events["LimitTweakableToggle"].active = true; + Events["LimitTweakableToggle"].guiName = limitTweakableFlag ? "Disengage Limits" : "Engage Limits"; } if (freeMoving) @@ -656,6 +660,7 @@ public override void OnStart(StartState state) if (limitTweakable) { Events["LimitTweakableToggle"].active = rotateJoint; + Events["LimitTweakableToggle"].guiName = limitTweakableFlag ? "Disengage Limits" : "Engage Limits"; } //it seems like we do need to call this one more time as OnVesselChange was called after Awake //for some reason it was not necessary for legacy parts, but needed for rework parts. @@ -868,12 +873,6 @@ protected bool KeyUnPressed(string key) protected void CheckInputs() { - if (part.isConnected && KeyPressed(onKey)) - { - on = !on; - UpdateState(); - } - if (KeyPressed(rotateKey) || KeyPressed(translateKey)) { Translator.Move(float.PositiveInfinity, speedTweak * customSpeed); @@ -885,8 +884,7 @@ protected void CheckInputs() else if (KeyUnPressed(rotateKey) || KeyUnPressed(translateKey) || KeyUnPressed(revRotateKey) || KeyUnPressed(revTranslateKey)) { Translator.Stop(); - } - + } } protected void DoRotation() From a7bfd3cafca46168d6bfbc36c2dfd17a145fb22b Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Mon, 11 May 2015 13:51:46 +0100 Subject: [PATCH 03/19] Minor clean-up 2 --- .../InfernalRobotics/Module/MuMechToggle.cs | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs index 29f4d577..5acd669c 100644 --- a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs +++ b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs @@ -1195,15 +1195,6 @@ public void MoveNextPresetAction(KSPActionParam param) Translator.Stop(); else MoveNextPreset(); - /*switch (param.type) - { - case KSPActionType.Activate: - MoveNextPreset (); - break; - case KSPActionType.Deactivate: - Translator.Stop(); - break; - }*/ } [KSPAction("Move To Previous Preset")] @@ -1213,16 +1204,6 @@ public void MovePrevPresetAction(KSPActionParam param) Translator.Stop(); else MovePrevPreset(); - - /*switch (param.type) - { - case KSPActionType.Activate: - MovePrevPreset (); - break; - case KSPActionType.Deactivate: - Translator.Stop(); - break; - }*/ } @@ -1321,8 +1302,6 @@ public void ApplyDeltaPos(float deltaPos) } } - - public void MoveLeft() { Move(-1); From 0d0a7236423df8622bdf2c73dd06e77485434deb Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Mon, 11 May 2015 23:06:06 +0100 Subject: [PATCH 04/19] Small API implementation change. --- InfernalRobotics/InfernalRobotics/Control/Servo/Servo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InfernalRobotics/InfernalRobotics/Control/Servo/Servo.cs b/InfernalRobotics/InfernalRobotics/Control/Servo/Servo.cs index b81e7aec..32e172a9 100644 --- a/InfernalRobotics/InfernalRobotics/Control/Servo/Servo.cs +++ b/InfernalRobotics/InfernalRobotics/Control/Servo/Servo.cs @@ -35,7 +35,7 @@ public string Name } public uint UID { - get {return rawServo.part.flightID; } + get {return rawServo.part.craftID; } } public bool Highlight { From 84442e5bc7d3a7362867173f8946754aaa531d13 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Tue, 12 May 2015 12:01:09 +0100 Subject: [PATCH 05/19] Symmetry handling optimization --- .../InfernalRobotics/Control/Servo/MechanismBase.cs | 13 +++++++------ .../InfernalRobotics/Control/Servo/ServoPreset.cs | 5 +++-- .../InfernalRobotics/Module/MuMechToggle.cs | 13 +++++++------ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/InfernalRobotics/InfernalRobotics/Control/Servo/MechanismBase.cs b/InfernalRobotics/InfernalRobotics/Control/Servo/MechanismBase.cs index 19e37e58..2b7b4cba 100644 --- a/InfernalRobotics/InfernalRobotics/Control/Servo/MechanismBase.cs +++ b/InfernalRobotics/InfernalRobotics/Control/Servo/MechanismBase.cs @@ -166,12 +166,13 @@ public void ApplyLimitsToSymmetry() { foreach (Part counterPart in RawServo.part.symmetryCounterparts) { - ((MuMechToggle) counterPart.Modules["MuMechToggle"]).rotateMin = RawServo.rotateMin; - ((MuMechToggle) counterPart.Modules["MuMechToggle"]).rotateMax = RawServo.rotateMax; - ((MuMechToggle) counterPart.Modules["MuMechToggle"]).translateMin = RawServo.translateMin; - ((MuMechToggle) counterPart.Modules["MuMechToggle"]).translateMax = RawServo.translateMax; - ((MuMechToggle) counterPart.Modules["MuMechToggle"]).minTweak = RawServo.minTweak; - ((MuMechToggle) counterPart.Modules["MuMechToggle"]).maxTweak = RawServo.maxTweak; + var module = ((MuMechToggle)counterPart.Modules ["MuMechToggle"]); + module.rotateMin = RawServo.rotateMin; + module.rotateMax = RawServo.rotateMax; + module.translateMin = RawServo.translateMin; + module.translateMax = RawServo.translateMax; + module.minTweak = RawServo.minTweak; + module.maxTweak = RawServo.maxTweak; } Logger.Log ("ApplyingSymmetry, number of counterparts: " + RawServo.part.symmetryCounterparts.Count, Logger.Level.Debug); } diff --git a/InfernalRobotics/InfernalRobotics/Control/Servo/ServoPreset.cs b/InfernalRobotics/InfernalRobotics/Control/Servo/ServoPreset.cs index 02c870d2..40720f49 100644 --- a/InfernalRobotics/InfernalRobotics/Control/Servo/ServoPreset.cs +++ b/InfernalRobotics/InfernalRobotics/Control/Servo/ServoPreset.cs @@ -54,8 +54,9 @@ public void Save(bool symmetry = false) { foreach (Part part in rawServo.part.symmetryCounterparts) { - ((MuMechToggle)part.Modules["MuMechToggle"]).presetPositionsSerialized = rawServo.presetPositionsSerialized; - ((MuMechToggle)part.Modules["MuMechToggle"]).ParsePresetPositions(); + var module = ((MuMechToggle)part.Modules ["MuMechToggle"]); + module.presetPositionsSerialized = rawServo.presetPositionsSerialized; + module.ParsePresetPositions(); } } } diff --git a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs index 5acd669c..dabf4167 100644 --- a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs +++ b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs @@ -964,12 +964,13 @@ public void RefreshTweakUI() { foreach (Part counterPart in part.symmetryCounterparts) { - ((MuMechToggle) counterPart.Modules["MuMechToggle"]).rotateMin = rotateMin; - ((MuMechToggle) counterPart.Modules["MuMechToggle"]).rotateMax = rotateMax; - ((MuMechToggle) counterPart.Modules["MuMechToggle"]).translateMin = translateMin; - ((MuMechToggle) counterPart.Modules["MuMechToggle"]).translateMax = translateMax; - ((MuMechToggle) counterPart.Modules["MuMechToggle"]).minTweak = minTweak; - ((MuMechToggle) counterPart.Modules["MuMechToggle"]).maxTweak = maxTweak; + var module = ((MuMechToggle)counterPart.Modules ["MuMechToggle"]); + module.rotateMin = rotateMin; + module.rotateMax = rotateMax; + module.translateMin = translateMin; + module.translateMax = translateMax; + module.minTweak = minTweak; + module.maxTweak = maxTweak; } } } From a1b32f4ac360edae950fb9bd5dd7727e148248b3 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Tue, 12 May 2015 19:35:56 +0100 Subject: [PATCH 06/19] nuFAR compatibility Every 60th FixedUpdate MuMechToggle needs to send a message to its parent part and all children to update the shape, which will trigger nuFARs voxel model rebuild --- .../InfernalRobotics/Module/MuMechToggle.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs index dabf4167..9df16999 100644 --- a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs +++ b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs @@ -15,6 +15,10 @@ namespace InfernalRobotics.Module { public class MuMechToggle : PartModule, IRescalable { + //these 3 are for sending messages to inform nuFAR of shape changes to the craft. + private const int shapeUpdateTimeout = 60; //it will send message every xx FixedUpdates + private int shapeUpdateCounter = 0; + private float lastPosition = 0f; private const string ELECTRIC_CHARGE_RESOURCE_NAME = "ElectricCharge"; @@ -1012,6 +1016,32 @@ void Update() } } + /// + /// This method sends a message every shapeUpdateTimeout FixedUpdate to part and its + /// children to update the shape. This is needed for nuFAR to rebuild the voxel shape accordingly. + /// + private void ProcessShapeUpdates() + { + if (shapeUpdateCounter < shapeUpdateTimeout) + { + shapeUpdateCounter++; + return; + } + + if (Math.Abs(lastPosition - this.Position) <= 0.005) + { + part.SendMessage("UpdateShapeWithAnims"); + foreach (var p in part.children) + { + p.SendMessage("UpdateShapeWithAnims"); + } + + lastPosition = this.Position; + } + + shapeUpdateCounter = 0; + } + public void FixedUpdate() { if (HighLogic.LoadedScene != GameScenes.FLIGHT && HighLogic.LoadedScene != GameScenes.EDITOR) @@ -1072,6 +1102,8 @@ public void FixedUpdate() child.UpdateOrgPosAndRot(vessel.rootPart); } } + + ProcessShapeUpdates(); } public void HandleElectricCharge() From 00833f6713080f7da6742874dde4a82511a671e0 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Tue, 12 May 2015 20:24:03 +0100 Subject: [PATCH 07/19] Somehow four-eye principle failed. --- InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs index 9df16999..1552b422 100644 --- a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs +++ b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs @@ -1028,7 +1028,7 @@ private void ProcessShapeUpdates() return; } - if (Math.Abs(lastPosition - this.Position) <= 0.005) + if (Math.Abs(lastPosition - this.Position) >= 0.005) { part.SendMessage("UpdateShapeWithAnims"); foreach (var p in part.children) From 5e545ef03dc1e4bc7db052a620f7990612cd8b40 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Fri, 15 May 2015 10:33:01 +0100 Subject: [PATCH 08/19] Minor tweaks to Preset textField parsing --- .../InfernalRobotics/Gui/ControlsGUI.cs | 76 +++++++++++++++---- 1 file changed, 63 insertions(+), 13 deletions(-) diff --git a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs index ed8d3dba..cac2318c 100644 --- a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs +++ b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs @@ -652,11 +652,11 @@ private void DrawServoPosition(IServo servo, GUILayoutOption rowHeight) fontStyle = servo.Mechanism.IsAxisInverted ? FontStyle.Italic : FontStyle.Normal }; var posFormat = Math.Abs(servo.Mechanism.MaxPosition - servo.Mechanism.MinPosition) > 10 ? "{0:#0.0#}" : "{0:#0.0##}"; - lastFocusedTextFieldValue = GUILayout.TextField(string.Format(posFormat, servo.Mechanism.Position), customStyle, GUILayout.Width(40), rowHeight); + string tmp = GUILayout.TextField(string.Format(posFormat, servo.Mechanism.Position), customStyle, GUILayout.Width(40), rowHeight); float tmpValue; - if (float.TryParse(lastFocusedTextFieldValue, out tmpValue)) + if (float.TryParse(tmp, out tmpValue)) { tmpValue = Mathf.Clamp(tmpValue, servo.Mechanism.MinPositionLimit, servo.Mechanism.MaxPositionLimit); @@ -1063,22 +1063,78 @@ private void PresetsEditWindow(int windowID) } GUILayout.EndHorizontal(); + if (lastFocusedControlName != GUI.GetNameOfFocusedControl()) + { + var temp = lastFocusedControlName.Split (' '); + Logger.Log ("[GUI] Focus change, lastName = " + lastFocusedControlName + + ", lastValue = " + lastFocusedTextFieldValue + + ", temp.Length = " + temp.Length, Logger.Level.Debug); + + if (temp[0] == "Preset" && temp.Length == 2) + { + int tmpVal = -1; + if(int.TryParse(temp[1], out tmpVal)) + { + if (tmpVal >= 0 && tmpVal < associatedServo.Preset.Count) + { + float tmpValue; + + if (float.TryParse (lastFocusedTextFieldValue, out tmpValue)) + { + if (tmpValue != associatedServo.Preset [tmpVal] && associatedServo.Preset [tmpVal] == associatedServo.Mechanism.DefaultPosition) + { + associatedServo.Mechanism.DefaultPosition = tmpValue; + } + associatedServo.Preset [tmpVal] = tmpValue; + } + } + } + } + associatedServo.Preset.Sort(); + lastFocusedControlName = GUI.GetNameOfFocusedControl(); + lastFocusedTextFieldValue = ""; + } + buttonStyle.padding = padding2px; buttonStyle.alignment = TextAnchor.MiddleCenter; for (int i = 0; i < associatedServo.Preset.Count; i++) { GUILayout.BeginHorizontal(); - GUI.SetNextControlName("Preset " + i); - string tmp = GUILayout.TextField(string.Format("{0:#0.0#}", associatedServo.Preset[i]), GUILayout.ExpandWidth(true), rowHeight); + + string focusedControlName = GUI.GetNameOfFocusedControl (); + string thisControlName = "Preset " + i; + + if (thisControlName == focusedControlName +// && focusedControlName != lastFocusedControlName + && lastFocusedTextFieldValue == "") + { + lastFocusedTextFieldValue = string.Format ("{0:#0.0#}", associatedServo.Preset [i]); + } + + string tmp = (thisControlName == focusedControlName) + ? lastFocusedTextFieldValue + : string.Format("{0:#0.0#}", associatedServo.Preset[i]); + + GUI.SetNextControlName(thisControlName); + tmp = GUILayout.TextField(tmp, GUILayout.ExpandWidth(true), rowHeight); + + if (thisControlName == focusedControlName + && focusedControlName == lastFocusedControlName) + lastFocusedTextFieldValue = tmp; + + var valueChanged = Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter; float tmpValue; - if (float.TryParse(tmp, out tmpValue)) + + if (float.TryParse (tmp, out tmpValue) && valueChanged) { - if (tmpValue != associatedServo.Preset[i] && associatedServo.Preset[i] == associatedServo.Mechanism.DefaultPosition) + //focus changes are handled elsewhere + if (tmpValue != associatedServo.Preset [i] && associatedServo.Preset [i] == associatedServo.Mechanism.DefaultPosition) { associatedServo.Mechanism.DefaultPosition = tmpValue; } - associatedServo.Preset[i] = tmpValue; + associatedServo.Preset [i] = tmpValue; + lastFocusedTextFieldValue = ""; } bool isDefault = (associatedServo.Preset[i] == associatedServo.Mechanism.DefaultPosition); @@ -1105,12 +1161,6 @@ private void PresetsEditWindow(int windowID) GUILayout.EndHorizontal(); } - if (lastFocusedControlName != GUI.GetNameOfFocusedControl()) - { - associatedServo.Preset.Sort(); - lastFocusedControlName = GUI.GetNameOfFocusedControl(); - } - GUILayout.BeginHorizontal(); if (GUILayout.Button("Apply Symmetry", buttonStyle)) From 888bb842f2d155f7e17977584ce740f3a1a36865 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Fri, 15 May 2015 10:41:32 +0100 Subject: [PATCH 09/19] Version bump --- InfernalRobotics.version | 4 ++-- .../GameData/MagicSmokeIndustries/InfernalRobotics.version | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/InfernalRobotics.version b/InfernalRobotics.version index b23f342d..d9ba6aa8 100644 --- a/InfernalRobotics.version +++ b/InfernalRobotics.version @@ -4,11 +4,11 @@ "VERSION": { "MAJOR": 0, "MINOR": 21, - "PATCH": 1 + "PATCH": 3 }, "KSP_VERSION": { "MAJOR": 1, "MINOR": 0, - "PATCH": 0 + "PATCH": 2 } } diff --git a/Resources/GameData/MagicSmokeIndustries/InfernalRobotics.version b/Resources/GameData/MagicSmokeIndustries/InfernalRobotics.version index b23f342d..d9ba6aa8 100644 --- a/Resources/GameData/MagicSmokeIndustries/InfernalRobotics.version +++ b/Resources/GameData/MagicSmokeIndustries/InfernalRobotics.version @@ -4,11 +4,11 @@ "VERSION": { "MAJOR": 0, "MINOR": 21, - "PATCH": 1 + "PATCH": 3 }, "KSP_VERSION": { "MAJOR": 1, "MINOR": 0, - "PATCH": 0 + "PATCH": 2 } } From 4f724106b9e3afcb0f1cea0656a26654acbbd4c2 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Fri, 15 May 2015 11:14:51 +0100 Subject: [PATCH 10/19] Logic is hard. --- InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs index cac2318c..2dfb3bc6 100644 --- a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs +++ b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs @@ -1122,7 +1122,8 @@ private void PresetsEditWindow(int windowID) && focusedControlName == lastFocusedControlName) lastFocusedTextFieldValue = tmp; - var valueChanged = Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter; + var valueChanged = (thisControlName == focusedControlName && + (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter)); float tmpValue; From 42b26aad2fb6678fdca70c4160ebffe503afd278 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Mon, 18 May 2015 15:04:17 +0100 Subject: [PATCH 11/19] Better Position TextField handling --- .../InfernalRobotics/Gui/ControlsGUI.cs | 139 +++++++++++++----- 1 file changed, 105 insertions(+), 34 deletions(-) diff --git a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs index 2dfb3bc6..cc5aa9f4 100644 --- a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs +++ b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs @@ -6,6 +6,7 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Collections.Generic; using UnityEngine; using File = System.IO.File; @@ -652,16 +653,41 @@ private void DrawServoPosition(IServo servo, GUILayoutOption rowHeight) fontStyle = servo.Mechanism.IsAxisInverted ? FontStyle.Italic : FontStyle.Normal }; var posFormat = Math.Abs(servo.Mechanism.MaxPosition - servo.Mechanism.MinPosition) > 10 ? "{0:#0.0#}" : "{0:#0.0##}"; - string tmp = GUILayout.TextField(string.Format(posFormat, servo.Mechanism.Position), customStyle, GUILayout.Width(40), rowHeight); + + string focusedControlName = GUI.GetNameOfFocusedControl (); + string thisControlName = "Position " + servo.UID; + + if (thisControlName == focusedControlName + && lastFocusedTextFieldValue == "") + { + lastFocusedTextFieldValue = string.Format (posFormat, servo.Mechanism.Position); + } + + string tmp = (thisControlName == focusedControlName) + ? lastFocusedTextFieldValue + : string.Format (posFormat, servo.Mechanism.Position); + + GUI.SetNextControlName(thisControlName); + tmp = GUILayout.TextField(tmp, customStyle, GUILayout.Width(40), rowHeight); + + if (thisControlName == focusedControlName + && focusedControlName == lastFocusedControlName) + lastFocusedTextFieldValue = tmp; + + var valueChanged = (thisControlName == focusedControlName && + (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter)); float tmpValue; - if (float.TryParse(tmp, out tmpValue)) + if (float.TryParse (tmp, out tmpValue) && valueChanged) { + //focus changers are handled elsewhere tmpValue = Mathf.Clamp(tmpValue, servo.Mechanism.MinPositionLimit, servo.Mechanism.MaxPositionLimit); - if (Math.Abs(servo.Mechanism.Position - tmpValue) > 0.005 && GUI.changed) + if (Math.Abs(servo.Mechanism.Position - tmpValue) > 0.005) servo.Mechanism.MoveTo(tmpValue); + + lastFocusedTextFieldValue = ""; } } @@ -907,6 +933,7 @@ private void EditorWindow(int windowID) GUILayout.BeginHorizontal(); GUILayout.Label("Range: ", GUILayout.Width(40), rowHeight); + //GUI.SetNextControlName ("MinPositionLimit " + servo.UID); tmpMin = GUILayout.TextField(string.Format("{0:#0.0#}", servo.Mechanism.MinPositionLimit), GUILayout.Width(40), rowHeight); float tmpValue; @@ -1050,51 +1077,84 @@ internal static void SetEditorScrollYPosition(Single newY) editorScroll.y = newY; } - private void PresetsEditWindow(int windowID) + private void ProcessFocusChange() { - GUILayoutOption rowHeight = GUILayout.Height(22); - GUILayout.BeginVertical(); + var temp = lastFocusedControlName.Split (' '); + Logger.Log ("[GUI] Focus change, lastName = " + lastFocusedControlName + + ", lastValue = " + lastFocusedTextFieldValue + + ", temp.Length = " + temp.Length, Logger.Level.Debug); - GUILayout.BeginHorizontal(); - GUILayout.Label("Preset position" + (associatedServo.Mechanism.IsAxisInverted ? " (Inv axis)" : ""), GUILayout.ExpandWidth(true), rowHeight); - if (GUILayout.Button("Add", buttonStyle, GUILayout.Width(30), rowHeight)) + if (temp[0] == "Preset" && temp.Length == 2 && associatedServo != null) { - associatedServo.Preset.Add(); - } - GUILayout.EndHorizontal(); + int tmpVal = -1; + if(int.TryParse(temp[1], out tmpVal)) + { + if (tmpVal >= 0 && tmpVal < associatedServo.Preset.Count) + { + float tmpValue; - if (lastFocusedControlName != GUI.GetNameOfFocusedControl()) + if (float.TryParse (lastFocusedTextFieldValue, out tmpValue)) + { + if (tmpValue != associatedServo.Preset [tmpVal] && associatedServo.Preset [tmpVal] == associatedServo.Mechanism.DefaultPosition) + { + associatedServo.Mechanism.DefaultPosition = tmpValue; + } + associatedServo.Preset [tmpVal] = tmpValue; + } + } + } + } + else if (temp[0] == "Position" && temp.Length == 2) { - var temp = lastFocusedControlName.Split (' '); - Logger.Log ("[GUI] Focus change, lastName = " + lastFocusedControlName - + ", lastValue = " + lastFocusedTextFieldValue - + ", temp.Length = " + temp.Length, Logger.Level.Debug); - - if (temp[0] == "Preset" && temp.Length == 2) + uint servoUID = 0; + if(uint.TryParse(temp[1], out servoUID)) { - int tmpVal = -1; - if(int.TryParse(temp[1], out tmpVal)) + //find servo with UID and update its position + var allServos = new List(); + foreach (var g in ServoController.Instance.ServoGroups) { - if (tmpVal >= 0 && tmpVal < associatedServo.Preset.Count) + allServos.AddRange (g.Servos); + } + var s = allServos.Find (p => p.UID == servoUID); + + if (s != null) + { + float tmpValue; + + if (float.TryParse (lastFocusedTextFieldValue, out tmpValue)) { - float tmpValue; + tmpValue = Mathf.Clamp(tmpValue, s.Mechanism.MinPositionLimit, s.Mechanism.MaxPositionLimit); - if (float.TryParse (lastFocusedTextFieldValue, out tmpValue)) - { - if (tmpValue != associatedServo.Preset [tmpVal] && associatedServo.Preset [tmpVal] == associatedServo.Mechanism.DefaultPosition) - { - associatedServo.Mechanism.DefaultPosition = tmpValue; - } - associatedServo.Preset [tmpVal] = tmpValue; - } + if (Math.Abs(s.Mechanism.Position - tmpValue) > 0.005) + s.Mechanism.MoveTo(tmpValue); + } } } + } + + if (associatedServo != null) + { associatedServo.Preset.Sort(); - lastFocusedControlName = GUI.GetNameOfFocusedControl(); - lastFocusedTextFieldValue = ""; } + lastFocusedControlName = GUI.GetNameOfFocusedControl(); + lastFocusedTextFieldValue = ""; + } + + private void PresetsEditWindow(int windowID) + { + GUILayoutOption rowHeight = GUILayout.Height(22); + GUILayout.BeginVertical(); + + GUILayout.BeginHorizontal(); + GUILayout.Label("Preset position" + (associatedServo.Mechanism.IsAxisInverted ? " (Inv axis)" : ""), GUILayout.ExpandWidth(true), rowHeight); + if (GUILayout.Button("Add", buttonStyle, GUILayout.Width(30), rowHeight)) + { + associatedServo.Preset.Add(); + } + GUILayout.EndHorizontal(); + buttonStyle.padding = padding2px; buttonStyle.alignment = TextAnchor.MiddleCenter; for (int i = 0; i < associatedServo.Preset.Count; i++) @@ -1105,7 +1165,6 @@ private void PresetsEditWindow(int windowID) string thisControlName = "Preset " + i; if (thisControlName == focusedControlName -// && focusedControlName != lastFocusedControlName && lastFocusedTextFieldValue == "") { lastFocusedTextFieldValue = string.Format ("{0:#0.0#}", associatedServo.Preset [i]); @@ -1265,6 +1324,18 @@ private void OnGUI() if (editorWindowWidth > Screen.width * 0.7) editorWindowWidth = (int)Math.Round(Screen.width * 0.7f); + + if (lastFocusedControlName != GUI.GetNameOfFocusedControl()) + { + ProcessFocusChange (); + } + + //this code defocuses the TexFields if you click mouse elsewhere + if (GUIUtility.hotControl > 0 && GUIUtility.hotControl != GUIUtility.keyboardControl) + { + GUIUtility.keyboardControl = 0; + } + if (scene == GameScenes.FLIGHT) { GUILayoutOption height = GUILayout.Height(Screen.height / 2f); From 46bb5d11db95e66074fd6b91b6e634fff9d989f8 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Tue, 19 May 2015 10:47:23 +0100 Subject: [PATCH 12/19] Applied better input handling to all float textields --- .../InfernalRobotics/Gui/ControlsGUI.cs | 146 ++++++++++++++---- .../InfernalRobotics/Module/MuMechToggle.cs | 2 + 2 files changed, 116 insertions(+), 32 deletions(-) diff --git a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs index cc5aa9f4..379d0406 100644 --- a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs +++ b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs @@ -45,8 +45,6 @@ public class ControlsGUI : MonoBehaviour private bool guiGroupEditorEnabled; private bool guiPresetsEnabled; private IServo associatedServo; - private string tmpMax = ""; - private string tmpMin = ""; private bool guiPresetMode; private bool guiHidden; private string tooltipText = ""; @@ -640,6 +638,39 @@ private void DrawPresetSelector(IServo servo, GUILayoutOption rowHeight) } SetTooltipText(); } + /// + /// Draws the text field and returns its value + /// + /// Entered value + /// Control name. + /// Value. + /// Format. + /// Style. + /// Width. + /// Height. + private string DrawTextField(string controlName, float value, string format, GUIStyle style, GUILayoutOption width, GUILayoutOption height) + { + string focusedControlName = GUI.GetNameOfFocusedControl (); + + if (controlName == focusedControlName + && lastFocusedTextFieldValue == "") + { + lastFocusedTextFieldValue = string.Format (format, value); + } + + string tmp = (controlName == focusedControlName) + ? lastFocusedTextFieldValue + : string.Format (format, value); + + GUI.SetNextControlName(controlName); + tmp = GUILayout.TextField(tmp, style, width, height); + + if (controlName == focusedControlName + && focusedControlName == lastFocusedControlName) + lastFocusedTextFieldValue = tmp; + + return tmp; + } private void DrawServoPosition(IServo servo, GUILayoutOption rowHeight) { @@ -657,22 +688,7 @@ private void DrawServoPosition(IServo servo, GUILayoutOption rowHeight) string focusedControlName = GUI.GetNameOfFocusedControl (); string thisControlName = "Position " + servo.UID; - if (thisControlName == focusedControlName - && lastFocusedTextFieldValue == "") - { - lastFocusedTextFieldValue = string.Format (posFormat, servo.Mechanism.Position); - } - - string tmp = (thisControlName == focusedControlName) - ? lastFocusedTextFieldValue - : string.Format (posFormat, servo.Mechanism.Position); - - GUI.SetNextControlName(thisControlName); - tmp = GUILayout.TextField(tmp, customStyle, GUILayout.Width(40), rowHeight); - - if (thisControlName == focusedControlName - && focusedControlName == lastFocusedControlName) - lastFocusedTextFieldValue = tmp; + string tmp = DrawTextField (thisControlName, servo.Mechanism.Position, posFormat, customStyle, GUILayout.Width (40), rowHeight); var valueChanged = (thisControlName == focusedControlName && (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter)); @@ -934,32 +950,72 @@ private void EditorWindow(int windowID) GUILayout.Label("Range: ", GUILayout.Width(40), rowHeight); //GUI.SetNextControlName ("MinPositionLimit " + servo.UID); - tmpMin = GUILayout.TextField(string.Format("{0:#0.0#}", servo.Mechanism.MinPositionLimit), GUILayout.Width(40), rowHeight); + + string focusedControlName = GUI.GetNameOfFocusedControl (); + string thisControlName = "MinPositionLimit " + servo.UID; + + tmp = DrawTextField (thisControlName, servo.Mechanism.MinPositionLimit, "{0:#0.0#}", + GUI.skin.textField, GUILayout.Width (40), rowHeight); + + var valueChanged = (thisControlName == focusedControlName && + (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter)); + float tmpValue; - if (float.TryParse(tmpMin, out tmpValue)) + if (float.TryParse (tmp, out tmpValue) && valueChanged) { + //focus changers are handled elsewhere servo.Mechanism.MinPositionLimit = tmpValue; + lastFocusedTextFieldValue = ""; } - tmpMax = GUILayout.TextField(string.Format("{0:#0.0#}", servo.Mechanism.MaxPositionLimit), GUILayout.Width(40), rowHeight); - if (float.TryParse(tmpMax, out tmpValue)) + thisControlName = "MaxPositionLimit " + servo.UID; + + tmp = DrawTextField (thisControlName, servo.Mechanism.MaxPositionLimit, "{0:#0.0#}", + GUI.skin.textField, GUILayout.Width (40), rowHeight); + + valueChanged = (thisControlName == focusedControlName && + (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter)); + + if (float.TryParse (tmp, out tmpValue) && valueChanged) { + //focus changers are handled elsewhere servo.Mechanism.MaxPositionLimit = tmpValue; + lastFocusedTextFieldValue = ""; } GUILayout.Label("Spd: ", GUILayout.Width(30), rowHeight); - tmpMin = GUILayout.TextField(string.Format("{0:#0.0##}", servo.Mechanism.SpeedLimit), GUILayout.Width(30), rowHeight); - if (float.TryParse(tmpMin, out tmpValue)) + + thisControlName = "Speed " + servo.UID; + + tmp = DrawTextField (thisControlName, servo.Mechanism.SpeedLimit, "{0:#0.0#}", + GUI.skin.textField, GUILayout.Width (30), rowHeight); + + valueChanged = (thisControlName == focusedControlName && + (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter)); + + if (float.TryParse (tmp, out tmpValue) && valueChanged) { + //focus changers are handled elsewhere servo.Mechanism.SpeedLimit = tmpValue; + lastFocusedTextFieldValue = ""; } GUILayout.Label("Acc: ", GUILayout.Width(30), rowHeight); - tmpMin = GUILayout.TextField(string.Format("{0:#0.0##}", servo.Mechanism.AccelerationLimit), GUILayout.Width(30), rowHeight); - if (float.TryParse(tmpMin, out tmpValue)) + + thisControlName = "Acceleration " + servo.UID; + + tmp = DrawTextField (thisControlName, servo.Mechanism.AccelerationLimit, "{0:#0.0#}", + GUI.skin.textField, GUILayout.Width (30), rowHeight); + + valueChanged = (thisControlName == focusedControlName && + (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter)); + + if (float.TryParse (tmp, out tmpValue) && valueChanged) { + //focus changers are handled elsewhere servo.Mechanism.AccelerationLimit = tmpValue; + lastFocusedTextFieldValue = ""; } bool servoInverted = servo.Mechanism.IsAxisInverted; @@ -1084,7 +1140,11 @@ private void ProcessFocusChange() + ", lastValue = " + lastFocusedTextFieldValue + ", temp.Length = " + temp.Length, Logger.Level.Debug); - if (temp[0] == "Preset" && temp.Length == 2 && associatedServo != null) + var servoFields = new string[6] {"Preset", "Position", "MinPositionLimit", "MaxPositionLimit", "Speed", "Acceleration"}; + + var pos = Array.IndexOf (servoFields, temp [0]); + + if (pos == 0 && temp.Length == 2 && associatedServo != null) { int tmpVal = -1; if(int.TryParse(temp[1], out tmpVal)) @@ -1104,7 +1164,7 @@ private void ProcessFocusChange() } } } - else if (temp[0] == "Position" && temp.Length == 2) + else if (temp.Length == 2 && pos > 0 && pos < 6) { uint servoUID = 0; if(uint.TryParse(temp[1], out servoUID)) @@ -1123,11 +1183,29 @@ private void ProcessFocusChange() if (float.TryParse (lastFocusedTextFieldValue, out tmpValue)) { - tmpValue = Mathf.Clamp(tmpValue, s.Mechanism.MinPositionLimit, s.Mechanism.MaxPositionLimit); + if (pos == 1) + { + tmpValue = Mathf.Clamp(tmpValue, s.Mechanism.MinPositionLimit, s.Mechanism.MaxPositionLimit); - if (Math.Abs(s.Mechanism.Position - tmpValue) > 0.005) - s.Mechanism.MoveTo(tmpValue); - + if (Math.Abs(s.Mechanism.Position - tmpValue) > 0.005) + s.Mechanism.MoveTo(tmpValue); + } + else if (pos == 2) + { + s.Mechanism.MinPositionLimit = tmpValue; + } + else if (pos == 3) + { + s.Mechanism.MaxPositionLimit = tmpValue; + } + else if (pos == 4) + { + s.Mechanism.SpeedLimit = tmpValue; + } + else if (pos == 5) + { + s.Mechanism.AccelerationLimit = tmpValue; + } } } } @@ -1152,6 +1230,7 @@ private void PresetsEditWindow(int windowID) if (GUILayout.Button("Add", buttonStyle, GUILayout.Width(30), rowHeight)) { associatedServo.Preset.Add(); + associatedServo.Preset.Sort(); } GUILayout.EndHorizontal(); @@ -1194,6 +1273,9 @@ private void PresetsEditWindow(int windowID) associatedServo.Mechanism.DefaultPosition = tmpValue; } associatedServo.Preset [i] = tmpValue; + associatedServo.Preset.Sort (); + //unfocus control as the list is sorted + GUIUtility.keyboardControl = 0; lastFocusedTextFieldValue = ""; } diff --git a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs index 1552b422..3565c8aa 100644 --- a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs +++ b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs @@ -477,6 +477,8 @@ public override void OnLoad(ConfigNode config) ParsePresetPositions(); + UpdateMinMaxTweaks (); + Logger.Log("[OnLoad] End", Logger.Level.Debug); } From a03e898a3e701aaab9046a60f56399dff06c8f69 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Tue, 19 May 2015 11:52:36 +0100 Subject: [PATCH 13/19] Focusing logic to be active when GUI is visible --- .../InfernalRobotics/Gui/ControlsGUI.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs index 379d0406..75bcdd40 100644 --- a/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs +++ b/InfernalRobotics/InfernalRobotics/Gui/ControlsGUI.cs @@ -1406,16 +1406,18 @@ private void OnGUI() if (editorWindowWidth > Screen.width * 0.7) editorWindowWidth = (int)Math.Round(Screen.width * 0.7f); - - if (lastFocusedControlName != GUI.GetNameOfFocusedControl()) + if (GUIEnabled && !guiHidden) { - ProcessFocusChange (); - } + if (lastFocusedControlName != GUI.GetNameOfFocusedControl ()) + { + ProcessFocusChange (); + } - //this code defocuses the TexFields if you click mouse elsewhere - if (GUIUtility.hotControl > 0 && GUIUtility.hotControl != GUIUtility.keyboardControl) - { - GUIUtility.keyboardControl = 0; + //this code defocuses the TexFields if you click mouse elsewhere + if (GUIUtility.hotControl > 0 && GUIUtility.hotControl != GUIUtility.keyboardControl) + { + GUIUtility.keyboardControl = 0; + } } if (scene == GameScenes.FLIGHT) From 057c43080596c30e7281887be9687d593f5d3578 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Tue, 19 May 2015 14:39:37 +0100 Subject: [PATCH 14/19] Now handle OnEditorRestart as well. --- .../InfernalRobotics/Command/ServoController.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/InfernalRobotics/InfernalRobotics/Command/ServoController.cs b/InfernalRobotics/InfernalRobotics/Command/ServoController.cs index 2b70eda0..24818c24 100644 --- a/InfernalRobotics/InfernalRobotics/Command/ServoController.cs +++ b/InfernalRobotics/InfernalRobotics/Command/ServoController.cs @@ -241,6 +241,8 @@ private void Awake() GameEvents.onPartAttach.Add(OnPartAttach); GameEvents.onPartRemove.Add(OnPartRemove); GameEvents.onEditorShipModified.Add(OnEditorShipModified); + GameEvents.onEditorLoad.Add(OnEditorLoad); + GameEvents.onEditorRestart.Add(OnEditorRestart); ControllerInstance = this; } else @@ -251,6 +253,18 @@ private void Awake() 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); + } + private void OnVesselWasModified(Vessel v) { if (v == FlightGlobals.ActiveVessel) @@ -270,7 +284,8 @@ private void OnDestroy() GameEvents.onPartRemove.Remove(OnPartRemove); GameEvents.onVesselWasModified.Remove(OnVesselWasModified); GameEvents.onEditorShipModified.Remove(OnEditorShipModified); - + GameEvents.onEditorLoad.Remove(OnEditorLoad); + GameEvents.onEditorRestart.Remove(OnEditorRestart); Logger.Log("[ServoController] OnDestroy finished successfully", Logger.Level.Debug); } From e4691b09e11440c3cf4efc8be94136cb61d072dc Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Wed, 20 May 2015 10:35:45 +0100 Subject: [PATCH 15/19] IR API implementation fix for ServoGroups Does not affect the public API --- .../InfernalRobotics/API/IRWrapper.cs | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/InfernalRobotics/InfernalRobotics/API/IRWrapper.cs b/InfernalRobotics/InfernalRobotics/API/IRWrapper.cs index 27877953..665f8951 100644 --- a/InfernalRobotics/InfernalRobotics/API/IRWrapper.cs +++ b/InfernalRobotics/InfernalRobotics/API/IRWrapper.cs @@ -120,7 +120,7 @@ internal static bool InitWrapper() } LogFormatted("Got Instance, Creating Wrapper Objects"); - IRController = new InfernalRoboticsAPI(ActualServoController); + IRController = new InfernalRoboticsAPI(); isWrapped = true; return true; } @@ -132,22 +132,24 @@ private class InfernalRoboticsAPI : IRAPI private PropertyInfo apiReady; private object actualServoGroups; - public InfernalRoboticsAPI(object irServoController) + public InfernalRoboticsAPI() { DetermineReady(); - BuildServoGroups(irServoController); + BuildServoGroups(); } - private void BuildServoGroups(object irServoController) + private void BuildServoGroups() { - LogFormatted("Getting ServoGroups Object"); var servoGroupsField = IRServoControllerType.GetField("ServoGroups"); if (servoGroupsField == null) LogFormatted("Failed Getting ServoGroups fieldinfo"); + else if (IRWrapper.ActualServoController == null) + { + LogFormatted("ServoController Instance not found"); + } else { - actualServoGroups = servoGroupsField.GetValue(irServoController); - LogFormatted("Success: " + (actualServoGroups != null)); + actualServoGroups = servoGroupsField.GetValue(IRWrapper.ActualServoController); } } @@ -162,7 +164,7 @@ public bool Ready { get { - if (apiReady == null) + if (apiReady == null || actualServoGroups == null) return false; return (bool)apiReady.GetValue(null, null); @@ -173,6 +175,7 @@ public IList ServoGroups { get { + BuildServoGroups (); return ExtractServoGroups(actualServoGroups); } } @@ -181,7 +184,7 @@ private IList ExtractServoGroups(object servoGroups) { var listToReturn = new List(); - if(servoGroups == null) + if (servoGroups == null) return listToReturn; try From 54942a08ae24d87f67acc235a8ff27718e00d9c7 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Wed, 20 May 2015 14:07:33 +0100 Subject: [PATCH 16/19] Additional Null checks --- InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs index 3565c8aa..f7982821 100644 --- a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs +++ b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs @@ -902,13 +902,14 @@ protected void DoRotation() (invertSymmetry ? ((IsSymmMaster() || (part.symmetryCounterparts.Count != 1)) ? 1 : -1) : 1)* (rotation - rotationDelta), rotateAxis); } - else if (transform != null) + else if (RotateModelTransform != null) { Quaternion curRot = Quaternion.AngleAxis( (invertSymmetry ? ((IsSymmMaster() || (part.symmetryCounterparts.Count != 1)) ? 1 : -1) : 1)* rotation, rotateAxis); - transform.FindChild("model").FindChild(rotateModel).localRotation = curRot; + RotateModelTransform.localRotation = curRot; + //transform.FindChild("model").FindChild(rotateModel).localRotation = curRot; } } From f93cd3180538b393c92e5199e1c76bcb24ac0181 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Sat, 23 May 2015 20:43:02 +0100 Subject: [PATCH 17/19] Small bugfixes for issues #58 and #59 --- .../InfernalRobotics/Control/Servo/ServoPreset.cs | 8 ++++++++ InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/InfernalRobotics/InfernalRobotics/Control/Servo/ServoPreset.cs b/InfernalRobotics/InfernalRobotics/Control/Servo/ServoPreset.cs index 40720f49..c359120a 100644 --- a/InfernalRobotics/InfernalRobotics/Control/Servo/ServoPreset.cs +++ b/InfernalRobotics/InfernalRobotics/Control/Servo/ServoPreset.cs @@ -132,6 +132,14 @@ public void GetNearestPresets(out int floor, out int ceiling) floor = rawServo.PresetPositions.FindLastIndex(p => p < rawServo.Position); if (floor == -1) floor = 0; + + if(rawServo.invertAxis) + { + //if axis is inverted swap two nearest presets + var tmp = ceiling; + ceiling = floor; + floor = tmp; + } } public void RemoveAt(int presetIndex) diff --git a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs index f7982821..64b873d4 100644 --- a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs +++ b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs @@ -1277,7 +1277,7 @@ public void MoveCenterAction(KSPActionParam param) switch (param.type) { case KSPActionType.Activate: - Translator.Move(Translator.ToExternalPos(0f), customSpeed * speedTweak); + Translator.Move(Translator.ToExternalPos(defaultPosition), customSpeed * speedTweak); break; case KSPActionType.Deactivate: Translator.Stop (); From 687d9df3ac2056cd2c60d1912c5ed9f81466cbf5 Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Tue, 2 Jun 2015 10:40:35 +0100 Subject: [PATCH 18/19] More null checks (due to KAS/KIS interaction) --- .../InfernalRobotics/Module/MuMechToggle.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs index 64b873d4..f27225f5 100644 --- a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs +++ b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs @@ -587,8 +587,15 @@ protected void ReparentFriction(Transform obj) public void BuildAttachments() { - if (part.findAttachNodeByPart(part.parent).id.Contains(bottomNode) - || part.attachMode == AttachModes.SRF_ATTACH) + if (part.parent == null) + { + Logger.Log ("BuildAttachments: part.parent is null", Logger.Level.Warning); + return; + } + var node = part.findAttachNodeByPart (part.parent); + if (node != null && + (node.id.Contains(bottomNode) + || part.attachMode == AttachModes.SRF_ATTACH)) { if (fixedMesh != "") { From e6b6c03d9c4c4b0658a5ab316045f69b654b51ca Mon Sep 17 00:00:00 2001 From: ZiwKerman Date: Tue, 2 Jun 2015 11:16:07 +0100 Subject: [PATCH 19/19] KAS/KIS compatibility fix If the IR part was not inited properly it will lock itself and will try to re-init on unlock. --- .../InfernalRobotics/Module/MuMechToggle.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs index f27225f5..9b8d434b 100644 --- a/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs +++ b/InfernalRobotics/InfernalRobotics/Module/MuMechToggle.cs @@ -130,6 +130,7 @@ public class MuMechToggle : PartModule, IRescalable [KSPField(isPersistant = false)] public string translateModel = "on"; private SoundSource motorSound; + private bool failedAttachment = false; public MuMechToggle() { @@ -590,6 +591,9 @@ public void BuildAttachments() if (part.parent == null) { Logger.Log ("BuildAttachments: part.parent is null", Logger.Level.Warning); + if(!isMotionLock) + SetLock (true); + failedAttachment = true; return; } var node = part.findAttachNodeByPart (part.parent); @@ -620,6 +624,7 @@ public void BuildAttachments() translateAxis *= -1; } ReparentFriction(part.transform); + failedAttachment = false; } protected void FindTransforms() @@ -1145,6 +1150,16 @@ public override void OnInactive() public void SetLock(bool isLocked) { + if(!isLocked && failedAttachment) + { + BuildAttachments (); + if (failedAttachment) + { + Logger.Log ("Failed rebuilding attachments, try again", Logger.Level.Debug); + return; + } + + } isMotionLock = isLocked; Events["MotionLockToggle"].guiName = isMotionLock ? "Disengage Lock" : "Engage Lock";