Exemplo n.º 1
0
        protected void drawServoHingeControls()
        {
            float setAngle;
            float currentAngle = 0;
            float targetAngle  = 0;
            bool  lockServo    = true;
            bool  soundPlaying = false;

            if (hingeServo != null)
            {
                drawResourceConsumption(hingeServo);
                currentAngle = hingeServo.currentAngle;
                targetAngle  = hingeServo.targetAngle;
            }
            else if (rotationServo != null)
            {
                drawResourceConsumption(rotationServo);
                currentAngle = rotationServo.currentAngle;
                targetAngle  = rotationServo.targetAngle;
            }
            GUILayout.Label(string.Format("<color=white><b>Angle (cur/tgt): </b>{0:f2}/{1:f2}</color>", currentAngle, targetAngle));

            //Rotation speed
            GUILayout.BeginHorizontal();
            GUILayout.Label("<color=white>Rotation Rate:</color>");

            float rotationDegPerSec = 0;

            if (hingeServo != null)
            {
                rotationDegPerSec = hingeServo.traverseVelocity;
            }
            else if (rotationServo != null)
            {
                rotationDegPerSec = rotationServo.traverseVelocity;
            }
            unitsText = rotationDegPerSec.ToString();
            unitsText = GUILayout.TextField(unitsText);
            Vector2 softMinMaxAngles = Vector2.zero;

            if (float.TryParse(unitsText, out rotationDegPerSec))
            {
                if (hingeServo != null)
                {
                    softMinMaxAngles = hingeServo.softMinMaxAngles;

                    if (rotationDegPerSec >= 1f && rotationDegPerSec <= 180f)
                    {
                        hingeServo.traverseVelocity = rotationDegPerSec;
                    }
                }
                else if (rotationServo != null)
                {
                    softMinMaxAngles = rotationServo.softMinMaxAngles;

                    if (rotationDegPerSec >= 1f && rotationDegPerSec <= 180f)
                    {
                        rotationServo.traverseVelocity = rotationDegPerSec;
                    }
                }
            }

            GUILayout.Label("<color=white>deg/s</color>");
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();

            //Minimum rotation
            if (GUILayout.Button(WBIServoGUI.minIcon, WBIServoGUI.buttonOptions))
            {
                lockServo = false;
                if (hingeServo != null)
                {
                    hingeServo.SetMinimumAngle();
                }
                else if (rotationServo != null)
                {
                    rotationServo.SetMinimumAngle();
                }
            }

            ///Rotate down
            if (GUILayout.RepeatButton(WBIServoGUI.counterClockwiseIcon, WBIServoGUI.buttonOptions))
            {
                currentAngle -= rotationDegPerSec * TimeWarp.fixedDeltaTime;
                if (currentAngle < softMinMaxAngles.x)
                {
                    currentAngle = softMinMaxAngles.x;
                }

                if (hingeServo != null)
                {
                    SetServoLock(false);
                    lockServo = false;
                    hingeServo.Fields["targetAngle"].SetValue(currentAngle, hingeServo);
                    hingeServo.currentAngle = currentAngle;
                }
                else if (rotationServo != null)
                {
                    SetServoLock(false);
                    lockServo = false;
                    rotationServo.Fields["targetAngle"].SetValue(currentAngle, rotationServo);
                    rotationServo.currentAngle = currentAngle;
                }
                this.part.Effect(servoEffectName, 1.0f);
                soundPlaying = true;
            }

            //Rotate up
            if (GUILayout.RepeatButton(WBIServoGUI.clockwiseIcon, WBIServoGUI.buttonOptions))
            {
                currentAngle += rotationDegPerSec * TimeWarp.fixedDeltaTime;
                if (currentAngle > softMinMaxAngles.y)
                {
                    currentAngle = softMinMaxAngles.y;
                }

                if (hingeServo != null)
                {
                    SetServoLock(false);
                    lockServo = false;
                    hingeServo.Fields["targetAngle"].SetValue(currentAngle, hingeServo);
                    hingeServo.currentAngle = currentAngle;
                }
                else if (rotationServo != null)
                {
                    SetServoLock(false);
                    lockServo = false;
                    rotationServo.Fields["targetAngle"].SetValue(currentAngle, rotationServo);
                    rotationServo.currentAngle = currentAngle;
                }
                this.part.Effect(servoEffectName, 1.0f);
                soundPlaying = true;
            }

            //Max rotation
            if (GUILayout.Button(WBIServoGUI.maxIcon, WBIServoGUI.buttonOptions))
            {
                lockServo = false;
                SetServoLock(false);
                if (hingeServo != null)
                {
                    hingeServo.SetMaximumAngle();
                }
                else if (rotationServo != null)
                {
                    rotationServo.SetMaximumAngle();
                }
            }

            //Manual lock buttons
            if (!autoLock)
            {
                if (GUILayout.Button(WBIServoGUI.lockIcon, WBIServoGUI.buttonOptions))
                {
                    lockServo = false;
                    SetServoLock(true);
                }
                if (GUILayout.Button(WBIServoGUI.unlockIcon, WBIServoGUI.buttonOptions))
                {
                    lockServo = false;
                    SetServoLock(false);
                }
            }

            GUILayout.EndHorizontal();

            //Specific target angle
            GUILayout.BeginHorizontal();

            GUILayout.Label("<color=white>Target Angle:</color>");
            targetValueText = GUILayout.TextField(targetValueText);

            //Make sure we're in bounds
            if (float.TryParse(targetValueText, out setAngle))
            {
                if (setAngle < softMinMaxAngles.x)
                {
                    setAngle = softMinMaxAngles.x;
                }
                else if (setAngle > softMinMaxAngles.y)
                {
                    setAngle = softMinMaxAngles.y;
                }
            }
            else
            {
                setAngle = currentAngle;
            }

            if (GUILayout.Button("Set"))
            {
                if (hingeServo != null)
                {
                    lockServo = false;
                    SetServoLock(false);
                    hingeServo.Fields["targetAngle"].SetValue(setAngle, hingeServo);
                    hingeServo.currentAngle = setAngle;
                }
                else if (rotationServo != null)
                {
                    lockServo = false;
                    SetServoLock(false);
                    rotationServo.Fields["targetAngle"].SetValue(setAngle, rotationServo);
                    rotationServo.currentAngle = setAngle;
                }
            }

            // Lock servo
            if (lockServo && autoLock && !isPlayingSnapshot && HighLogic.LoadedSceneIsFlight)
            {
                SetServoLock(true);
            }

            // Mute sound
            if (!soundPlaying && !isPlayingSnapshot && HighLogic.LoadedSceneIsFlight)
            {
                this.part.Effect(servoEffectName, -1.0f);
            }

            GUILayout.EndHorizontal();
        }
        protected void drawServoHingeControls()
        {
            float setAngle;
            float currentAngle = 0;

            if (hingeServo != null)
            {
                drawResourceConsumption(hingeServo);
                currentAngle = hingeServo.currentAngle;
            }
            else if (rotationServo != null)
            {
                drawResourceConsumption(rotationServo);
                currentAngle = rotationServo.currentAngle;
            }
            GUILayout.Label(string.Format("<color=white><b>Current Angle: </b>{0:f2}</color>", currentAngle));

            //Rotation speed
            GUILayout.BeginHorizontal();
            GUILayout.Label("<color=white>Rotation Rate:</color>");

            float rotationDegPerSec = 0;

            if (hingeServo != null)
            {
                rotationDegPerSec = hingeServo.traverseVelocity;
            }
            else if (rotationServo != null)
            {
                rotationDegPerSec = rotationServo.traverseVelocity;
            }
            unitsText = rotationDegPerSec.ToString();
            unitsText = GUILayout.TextField(unitsText);
            Vector2 softMinMaxAngles = Vector2.zero;

            if (float.TryParse(unitsText, out rotationDegPerSec))
            {
                if (hingeServo != null)
                {
                    softMinMaxAngles = hingeServo.softMinMaxAngles;

                    if (rotationDegPerSec >= 1f && rotationDegPerSec <= 180f)
                    {
                        hingeServo.traverseVelocity = rotationDegPerSec;
                    }
                }
                else if (rotationServo != null)
                {
                    softMinMaxAngles = rotationServo.softMinMaxAngles;

                    if (rotationDegPerSec >= 1f && rotationDegPerSec <= 180f)
                    {
                        rotationServo.traverseVelocity = rotationDegPerSec;
                    }
                }
            }

            GUILayout.Label("<color=white>deg/s</color>");
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();

            //Minimum rotation
            if (GUILayout.Button(WBIServoGUI.minIcon, WBIServoGUI.buttonOptions))
            {
                if (hingeServo != null)
                {
                    hingeServo.SetMinimumAngle();
                }
                else if (rotationServo != null)
                {
                    rotationServo.SetMinimumAngle();
                }
            }

            ///Rotate down
            if (GUILayout.RepeatButton(WBIServoGUI.backIcon, WBIServoGUI.buttonOptions))
            {
                currentAngle -= rotationDegPerSec * TimeWarp.fixedDeltaTime;
                if (currentAngle < softMinMaxAngles.x)
                {
                    currentAngle = softMinMaxAngles.x;
                }

                if (hingeServo != null)
                {
                    hingeServo.Fields["targetAngle"].SetValue(currentAngle, hingeServo);
                    hingeServo.currentAngle = currentAngle;
                }
                else if (rotationServo != null)
                {
                    rotationServo.Fields["targetAngle"].SetValue(currentAngle, rotationServo);
                    rotationServo.currentAngle = currentAngle;
                }
            }

            //Rotate up
            if (GUILayout.RepeatButton(WBIServoGUI.forwardIcon, WBIServoGUI.buttonOptions))
            {
                currentAngle += rotationDegPerSec * TimeWarp.fixedDeltaTime;
                if (currentAngle > softMinMaxAngles.y)
                {
                    currentAngle = softMinMaxAngles.y;
                }

                if (hingeServo != null)
                {
                    hingeServo.Fields["targetAngle"].SetValue(currentAngle, hingeServo);
                    hingeServo.currentAngle = currentAngle;
                }
                else if (rotationServo != null)
                {
                    rotationServo.Fields["targetAngle"].SetValue(currentAngle, rotationServo);
                    rotationServo.currentAngle = currentAngle;
                }
            }

            //Max rotation
            if (GUILayout.Button(WBIServoGUI.maxIcon, WBIServoGUI.buttonOptions))
            {
                if (hingeServo != null)
                {
                    hingeServo.SetMaximumAngle();
                }
                else if (rotationServo != null)
                {
                    rotationServo.SetMaximumAngle();
                }
            }

            GUILayout.EndHorizontal();

            //Specific target angle
            GUILayout.BeginHorizontal();

            GUILayout.Label("<color=white>Target Angle:</color>");
            targetValueText = GUILayout.TextField(targetValueText);

            //Make sure we're in bounds
            if (float.TryParse(targetValueText, out setAngle))
            {
                if (setAngle < softMinMaxAngles.x)
                {
                    setAngle = softMinMaxAngles.x;
                }
                else if (setAngle > softMinMaxAngles.y)
                {
                    setAngle = softMinMaxAngles.y;
                }
            }
            else
            {
                setAngle = currentAngle;
            }

            if (GUILayout.Button("Set"))
            {
                if (hingeServo != null)
                {
                    hingeServo.Fields["targetAngle"].SetValue(setAngle, hingeServo);
                    hingeServo.currentAngle = setAngle;
                }
                else if (rotationServo != null)
                {
                    rotationServo.Fields["targetAngle"].SetValue(setAngle, rotationServo);
                    rotationServo.currentAngle = setAngle;
                }
            }

            GUILayout.EndHorizontal();
        }