private void PopQueue()
        {
            while (mQueue.Count > 0 && mQueue.Peek().TimeStamp < RTUtil.GetGameTime())
            {
                DelayedCommand dc = mQueue.Dequeue();
                if (dc.Program != null)
                {
                    LoadProgram(dc.Program);
                }

                if (dc.Numpad != null)
                {
                    mProgcom.Memory[mOffset + INPUT_NUM]     = dc.Numpad ?? 0;
                    mProgcom.Memory[mOffset + INPUT_NUM + 1] = 1;
                }
            }
        }
        public void Upload(String fileName)
        {
            Log("Loading program \"{0}\".", fileName);
            Int32[] newCode;
            try {
                newCode = mProgcom.Assemble(fileName);
            } catch (FormatException) {
                Log("ERROR: Compilation failed.");
                return;
            } catch (Exception) {
                Log("ERROR: Unknown error.");
                return;
            }
            Log("Successfully assembled.");
            Log("Transmitting program (ETA: {0})", RTUtil.FormatDuration(mProgcom.Delay));
            DelayedCommand dc = new DelayedCommand()
            {
                Program = newCode
            };

            dc.TimeStamp += mProgcom.Delay;
            mQueue.Enqueue(dc);
        }
        public void Draw()
        {
            if (Event.current.Equals(Event.KeyboardEvent("return")) &&
                GUI.GetNameOfFocusedControl() == "xd")
            {
                mExtraDelay = Delay.ToString();
                mFlightComputer.ExtraDelay = Delay;
            }
            GUILayout.BeginVertical(GUILayout.Width(250));
            {
                mScrollPosition = GUILayout.BeginScrollView(mScrollPosition,
                                                            GUILayout.ExpandHeight(true));
                {
                    foreach (DelayedCommand dc in mFlightComputer)
                    {
                        GUILayout.BeginHorizontal(GUI.skin.box);
                        {
                            GUILayout.Label(Format(dc));
                            RTUtil.Button("x", () => { mFlightComputer.Enqueue(DelayedCommand.Cancel(dc)); }, GUILayout.Width(21));
                        }
                        GUILayout.EndHorizontal();
                    }
                }
                GUILayout.EndScrollView();

                GUILayout.BeginHorizontal();
                {
                    GUILayout.Label("Artificial delay: ");
                    GUI.SetNextControlName("xd");
                    GUILayout.Label(mFlightComputer.ExtraDelay.ToString("F2"));
                    RTUtil.TextField(ref mExtraDelay, GUILayout.ExpandWidth(true));
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndVertical();
        }
        private String Format(DelayedCommand dc)
        {
            StringBuilder s = new StringBuilder();

            if (dc.AttitudeCommand != null)
            {
                switch (dc.AttitudeCommand.Mode)
                {
                case FlightMode.Off:
                    s.Append("Mode: Off");
                    break;

                case FlightMode.KillRot:
                    s.Append("Mode: Kill rotation");
                    break;

                case FlightMode.AttitudeHold:
                    s.Append("Mode: Hold, ");
                    switch (dc.AttitudeCommand.Attitude)
                    {
                    case FlightAttitude.Prograde:
                        s.Append("Prograde");
                        break;

                    case FlightAttitude.Retrograde:
                        s.Append("Retrograde");
                        break;

                    case FlightAttitude.NormalPlus:
                        s.Append("Normal +");
                        break;

                    case FlightAttitude.NormalMinus:
                        s.Append("Normal -");
                        break;

                    case FlightAttitude.RadialPlus:
                        s.Append("Radial +");
                        break;

                    case FlightAttitude.RadialMinus:
                        s.Append("Radial -");
                        break;

                    case FlightAttitude.Surface:
                        s.Append(dc.AttitudeCommand.Orientation.eulerAngles.x.ToString("F1"));
                        s.Append(", ");
                        s.Append(dc.AttitudeCommand.Orientation.eulerAngles.y.ToString("F1"));
                        s.Append(", ");
                        s.Append(dc.AttitudeCommand.Orientation.eulerAngles.z.ToString("F1"));
                        break;
                    }
                    break;

                case FlightMode.AltitudeHold:
                    s.Append("Mode: Hold, ");
                    s.Append(RTUtil.FormatSI(dc.AttitudeCommand.Altitude, "m"));
                    break;
                }
            }
            else if (dc.ActionGroupCommand != null)
            {
                s.Append("Toggle ");
                s.Append(dc.ActionGroupCommand.ActionGroup.ToString());
            }
            else if (dc.BurnCommand != null)
            {
                s.Append("Burn ");
                s.Append(dc.BurnCommand.Throttle.ToString("P2"));
                if (dc.BurnCommand.Duration != Single.NaN)
                {
                    s.Append(", ");
                    s.Append(dc.BurnCommand.Duration.ToString("F2"));
                    s.Append("s");
                }
                if (dc.BurnCommand.DeltaV != Single.NaN)
                {
                    s.Append(", ");
                    s.Append(dc.BurnCommand.DeltaV.ToString("F2"));
                    s.Append("m/s");
                }
            }
            else if (dc.DriveCommand != null)
            {
                dc.DriveCommand.GetDescription(s);
            }
            else if (dc.Event != null)
            {
                s.Append(dc.Event.BaseEvent.listParent.part.partInfo.title);
                s.Append(": ");
                s.Append(dc.Event.BaseEvent.GUIName);
            }

            double delay = Math.Max(dc.TimeStamp - RTUtil.GetGameTime(), 0);

            if (delay > 0 || dc.ExtraDelay > 0)
            {
                s.AppendLine();
                s.Append("Signal delay: ");
                s.Append(delay.ToString("F2"));
                s.Append("s");
                if (dc.ExtraDelay > 0)
                {
                    s.Append(" (+");
                    s.Append(dc.ExtraDelay.ToString("F2"));
                    s.Append("s)");
                }
            }
            return(s.ToString());
        }
 public int CompareTo(DelayedCommand dc)
 {
     return(TimeStamp.CompareTo(dc.TimeStamp));
 }