public void SetMode(AttitudeMode mode, AttitudeButtonState input)
 {
     this.attitideActive = input.Active;
     if (!input.Active)
     {
         return;
     }
     switch (mode)
     {
         case AttitudeMode.MANEUVERNODE:
             if (input.MN != Quaternion.identity)
             {
                 Target = input.MN;
                 Reference = AttitudeReference.INERTIAL;
             }
             else
             {
                 Target = Quaternion.LookRotation(vessel.ReferenceTransform.up, -vessel.ReferenceTransform.forward);
                 Reference = AttitudeReference.INERTIAL;
             }
             break;
         case AttitudeMode.KILLROT:
             Target = Quaternion.LookRotation(vessel.ReferenceTransform.up, -vessel.ReferenceTransform.forward);
             Reference = AttitudeReference.INERTIAL;
             break;
         case AttitudeMode.PROGRADE:
             Target = Direction(Vector3d.forward, AttitudeReference.ORBIT);
             Reference = AttitudeReference.ORBIT;
             break;
         case AttitudeMode.RETROGRADE:
             Target = Direction(Vector3d.back, AttitudeReference.ORBIT);
             Reference = AttitudeReference.ORBIT;
             break;
         case AttitudeMode.SRF_PROGRADE:
             Target = Direction(Vector3d.forward, AttitudeReference.SURFACE_VELOCITY);
             Reference = AttitudeReference.SURFACE_VELOCITY;
             break;
         case AttitudeMode.SRF_RETROGRADE:
             Target = Direction(Vector3d.back, AttitudeReference.SURFACE_VELOCITY);
             Reference = AttitudeReference.SURFACE_VELOCITY;
             break;
         case AttitudeMode.NORMAL_PLUS:
             Target = Direction(Vector3d.left, AttitudeReference.ORBIT);
             Reference = AttitudeReference.ORBIT;
             break;
         case AttitudeMode.NORMAL_MINUS:
             Target = Direction(Vector3d.right, AttitudeReference.ORBIT);
             Reference = AttitudeReference.ORBIT;
             break;
         case AttitudeMode.RADIAL_PLUS:
             Target = Direction(Vector3d.up, AttitudeReference.ORBIT);
             Reference = AttitudeReference.ORBIT;
             break;
         case AttitudeMode.RADIAL_MINUS:
             Target = Direction(Vector3d.down, AttitudeReference.ORBIT);
             Reference = AttitudeReference.ORBIT;
             break;
         case AttitudeMode.SURFACE:
             if (input.USEROL)
                 Target = Quaternion.AngleAxis(input.HDG, Vector3.up) * Quaternion.AngleAxis(-input.PIT, Vector3.right) * Quaternion.AngleAxis(-input.ROL, Vector3.forward);
             else
                 Target = Direction(Quaternion.AngleAxis(input.HDG, Vector3.up) * Quaternion.AngleAxis(-input.PIT, Vector3.right) * Vector3d.forward, AttitudeReference.SURFACE_NORTH);
             Reference = AttitudeReference.SURFACE_NORTH;
             break;
     }
 }
        public void Update()
        {
            if (on != lastOn && (computer.core.localControl || computer.core.InContact))
            {
                AttitudeButtonState tmp = new AttitudeButtonState();
                tmp.Active = lastOn = on;

                if(burnAt >= (computer.core.localControl ? 0 : computer.core.path.ControlDelay))
                {
                    tmp.ActTime = lastActTime = Planetarium.GetUniversalTime() + burnAt;
                    burnAt = 0;
                    burnAts = "";
                }
                else
                tmp.ActTime = lastActTime = Planetarium.GetUniversalTime() + (computer.core.localControl ? 0 : computer.core.path.ControlDelay);

                tmp.HDG = this.HDG;
                tmp.PIT = this.PIT;
                tmp.ROL = this.ROL;
                tmp.USEROL = this.USEROL;

                if (this.mode == AttitudeMode.MANEUVERNODE)
                {
                    if (FlightGlobals.ActiveVessel.patchedConicSolver.maneuverNodes.Count > 0)
                    {
                        tmp.MN = Quaternion.LookRotation(FlightGlobals.ActiveVessel.patchedConicSolver.maneuverNodes[0].GetBurnVector(FlightGlobals.ActiveVessel.orbit).normalized, -FlightGlobals.ActiveVessel.ReferenceTransform.forward);
                        states.Enqueue(tmp);
                    }
                    else
                    {
                        if (state.Active)
                        {
                            tmp.MN = Quaternion.identity;
                            tmp.Active = false;
                            states.Enqueue(tmp);
                        }
                    }
                }
                else
                {
                    tmp.MN = Quaternion.identity;
                    states.Enqueue(tmp);
                }

            }

            if (sending && states.Peek().ActTime <= Planetarium.GetUniversalTime())
            {
                state = states.Dequeue();
                foreach (AttitudeStateButton b in computer.attitudeButtons)
                {
                    if (b != this)
                    {
                        b.on = b.lastOn = b.state.Active = false;
                    }
                }
                computer.core.computer.SetMode(mode, state);
            }
        }