private static void EditKerbalViewer()
        {
            GUILayout.Label(SelectedKerbal.IsNew ? "Create a Kerbal" : "Edit a Kerbal");
              if (SMSettings.EnableKerbalRename)
              {
            GUILayout.BeginHorizontal();
            SelectedKerbal.Name = GUILayout.TextField(SelectedKerbal.Name, GUILayout.MaxWidth(300));
            GUILayout.Label(" - (" + SelectedKerbal.Kerbal.experienceTrait.Title + ")");
            GUILayout.EndHorizontal();
              }
              else
            GUILayout.Label(SelectedKerbal.Name + " - (" + SelectedKerbal.Trait + ")", SMStyle.LabelStyleBold,
              GUILayout.MaxWidth(300));

              if (!String.IsNullOrEmpty(SMAddon.SaveMessage))
              {
            GUILayout.Label(SMAddon.SaveMessage, SMStyle.ErrorLabelRedStyle);
              }
              if (SMSettings.EnableKerbalRename && SMSettings.EnableChangeProfession)
              {
            DisplaySelectProfession();
              }
              bool isMale = ProtoCrewMember.Gender.Male == SelectedKerbal.Gender;
              GUILayout.BeginHorizontal();
              GUILayout.Label("Gender");
              isMale = GUILayout.Toggle(isMale, ProtoCrewMember.Gender.Male.ToString(), GUILayout.Width(90));
              isMale = GUILayout.Toggle(!isMale, ProtoCrewMember.Gender.Female.ToString());
              SelectedKerbal.Gender = isMale ? ProtoCrewMember.Gender.Female : ProtoCrewMember.Gender.Male;
              GUILayout.EndHorizontal();

              GUILayout.Label("Courage");
              SelectedKerbal.Courage = GUILayout.HorizontalSlider(SelectedKerbal.Courage, 0, 1, GUILayout.MaxWidth(300));

              GUILayout.Label("Stupidity");
              SelectedKerbal.Stupidity = GUILayout.HorizontalSlider(SelectedKerbal.Stupidity, 0, 1, GUILayout.MaxWidth(300));

              SelectedKerbal.Badass = GUILayout.Toggle(SelectedKerbal.Badass, "Badass");

              GUILayout.BeginHorizontal();
              if (GUILayout.Button("Cancel", GUILayout.MaxWidth(50)))
              {
            SelectedKerbal = null;
              }
              string label = "Apply";
              string toolTip =
            "Applies the changes made to this Kerbal.\r\nDesired Name and Profession will be Retained after save.";
              if (GUILayout.Button(new GUIContent(label, toolTip), GUILayout.MaxWidth(50)))
              {
            if (SMSettings.EnableKerbalRename && SMSettings.EnableChangeProfession)
            {
              if (SelectedKerbal != null) SelectedKerbal.Trait = KerbalProfession.ToString();
            }
            if (SelectedKerbal != null)
            {
              SMAddon.SaveMessage = SelectedKerbal.SubmitChanges();
              if (String.IsNullOrEmpty(SMAddon.SaveMessage))
            SelectedKerbal = null;
            }
              }
              Rect rect = GUILayoutUtility.GetLastRect();
              if (Event.current.type == EventType.Repaint && ShowToolTips)
            ToolTip = SMToolTips.SetActiveToolTip(rect, GUI.tooltip, ref ToolTipActive, 10);
              GUILayout.EndHorizontal();
        }
 private static void CreateKerbalViewer()
 {
     DisplaySelectProfession();
       GUILayout.BeginHorizontal();
       GUIContent guilabel = new GUIContent("Create",
     "Creates a Kerbal with profession selected above.\r\nAdds him/her to the Roster.");
       if (GUILayout.Button(guilabel, GUILayout.MaxWidth(80), GUILayout.Height(20)))
       {
     bool kerbalFound = false;
     while (!kerbalFound)
     {
       SelectedKerbal = ModKerbal.CreateKerbal();
       if (SelectedKerbal.Trait == KerbalProfession.ToString())
     kerbalFound = true;
     }
     OnCreate = false;
       }
       Rect rect = GUILayoutUtility.GetLastRect();
       if (Event.current.type == EventType.Repaint && ShowToolTips)
     ToolTip = SMToolTips.SetActiveToolTip(rect, GUI.tooltip, ref ToolTipActive, 10);
       guilabel = new GUIContent("Cancel", "Cancels current creation and exit editor.");
       if (GUILayout.Button(guilabel, GUILayout.MaxWidth(80), GUILayout.Height(20)))
       {
     OnCreate = false;
     SelectedKerbal = null;
       }
       rect = GUILayoutUtility.GetLastRect();
       if (Event.current.type == EventType.Repaint && ShowToolTips)
     ToolTip = SMToolTips.SetActiveToolTip(rect, GUI.tooltip, ref ToolTipActive, 10);
       GUILayout.EndHorizontal();
 }
        private static void DisplayRosterListViewer()
        {
            try
              {
            GUILayout.BeginVertical();
            // Roster List Header...
            GUILayout.BeginHorizontal();
            //GUILayout.Label("", GUILayout.Width(5));
            GUILayout.Label("Name", GUILayout.Width(140));
            GUILayout.Label("Gender", GUILayout.Width(50));
            GUILayout.Label("Profession", GUILayout.Width(70));
            GUILayout.Label("Skill", GUILayout.Width(30));
            GUILayout.Label("Status", GUILayout.Width(220));
            GUILayout.Label("Edit", GUILayout.Width(55));
            GUILayout.Label("Action", GUILayout.Width(65));
            GUILayout.EndHorizontal();

            _scrollViewerPosition = GUILayout.BeginScrollView(_scrollViewerPosition, SMStyle.ScrollStyle,
              GUILayout.Height(230), GUILayout.Width(680));

            // vars for acton to occurs after button press
            bool isAction = false;
            Part actionPart = null;
            string actionText = "";
            ProtoCrewMember actionKerbal = null;

            List<ProtoCrewMember>.Enumerator kerbals = RosterList.GetEnumerator();
            while (kerbals.MoveNext())
            {
              if (kerbals.Current == null) continue;
              if (!CanDisplayKerbal(kerbals.Current)) continue;
              GUIStyle labelStyle;
              if (kerbals.Current.rosterStatus == ProtoCrewMember.RosterStatus.Dead ||
              kerbals.Current.rosterStatus == ProtoCrewMember.RosterStatus.Missing)
            labelStyle = SMStyle.LabelStyleRed;
              else if (kerbals.Current.rosterStatus == ProtoCrewMember.RosterStatus.Assigned)
            labelStyle = SMStyle.LabelStyleYellow;
              else
            labelStyle = SMStyle.LabelStyle;

              // What vessel is this Kerbal Assigned to?
              string rosterDetails = "";
              if (kerbals.Current.rosterStatus == ProtoCrewMember.RosterStatus.Assigned)
              {
            List<Vessel>.Enumerator theseVessels = FlightGlobals.Vessels.GetEnumerator();
            while (theseVessels.MoveNext())
            {
              if (theseVessels.Current == null) continue;
              List<ProtoCrewMember> crew = theseVessels.Current.GetVesselCrew();
              if (crew.Any(crewMember => crewMember == kerbals.Current))
              {
                rosterDetails = "Assigned - " + theseVessels.Current.GetName().Replace("(unloaded)", "");
              }
            }
              }
              else if (InstalledMods.IsDfInstalled && DFWrapper.APIReady && kerbals.Current.type == ProtoCrewMember.KerbalType.Unowned)
              {
            // This kerbal could be frozen.  Lets find out...
            rosterDetails = GetFrozenKerbalDetails(kerbals.Current);
            labelStyle = SMStyle.LabelStyleCyan;
              }
              else
              {
            // Since the kerbal has no vessel assignment, lets show what their status...
            rosterDetails = kerbals.Current.rosterStatus.ToString();
              }
              string buttonText;
              string buttonToolTip;
              GUILayout.BeginHorizontal();
              GUILayout.Label(kerbals.Current.name, labelStyle, GUILayout.Width(140), GUILayout.Height(20));
              GUILayout.Label(kerbals.Current.gender.ToString(), labelStyle, GUILayout.Width(50));
              GUILayout.Label(kerbals.Current.experienceTrait.Title, labelStyle, GUILayout.Width(70));
              GUILayout.Label(kerbals.Current.experienceLevel.ToString(), labelStyle, GUILayout.Width(30));
              GUILayout.Label(rosterDetails, labelStyle, GUILayout.Width(215));

              SetupEditButton(kerbals.Current, out buttonText, out buttonToolTip);
              if (GUILayout.Button(new GUIContent(buttonText, buttonToolTip), GUILayout.Width(55), GUILayout.Height(20),
            GUILayout.Height(20)))
              {
            if (SelectedKerbal == null || SelectedKerbal.Kerbal != kerbals.Current)
            {
              SelectedKerbal = new ModKerbal(kerbals.Current, false);
              SetProfessionFlag();
            }
            else
            {
              SelectedKerbal = null;
            }
              }
              Rect rect = GUILayoutUtility.GetLastRect();
              if (Event.current.type == EventType.Repaint && ShowToolTips)
            ToolTip = SMToolTips.SetActiveToolTip(rect, GUI.tooltip, ref ToolTipActive, XOffset);

              // Setup buttons with gui state, button text and tooltip.
              SetupActionButton(kerbals.Current, out buttonText, out buttonToolTip);

              if (GUILayout.Button(new GUIContent(buttonText, buttonToolTip), GUILayout.Width(65), GUILayout.Height(20)))
              {
            actionPart = SMAddon.SmVessel.FindPartByKerbal(kerbals.Current);
            actionKerbal = kerbals.Current;
            actionText = buttonText;
            isAction = true;
              }
              Rect rect2 = GUILayoutUtility.GetLastRect();
              if (Event.current.type == EventType.Repaint && ShowToolTips)
            ToolTip = SMToolTips.SetActiveToolTip(rect2, GUI.tooltip, ref ToolTipActive, XOffset);
              GUILayout.EndHorizontal();
              GUI.enabled = true;
            }
            GUILayout.EndVertical();
            GUILayout.EndScrollView();

            // perform action from button press.
            if (!isAction) return;
            if (actionText == "Add")
              TransferCrew.AddCrewMember(actionKerbal, SMAddon.SmVessel.SelectedPartsSource[0]);
            else if (actionText == "Respawn")
              RespawnKerbal(actionKerbal);
            else if (actionText == "Thaw")
              ThawKerbal(actionKerbal.name);
            else if (actionText == "Freeze")
              FreezeKerbal(actionKerbal);
            else if (actionText == "Remove")
              TransferCrew.RemoveCrewMember(actionKerbal, actionPart);
              }
              catch (Exception ex)
              {
            if (!SMAddon.FrameErrTripped)
            {
              Utilities.LogMessage(string.Format(" in RosterListViewer.  Error:  {0} \r\n\r\n{1}", ex.Message, ex.StackTrace), Utilities.LogType.Error, true);
            }
              }
        }
        internal static void Display(int windowId)
        {
            // Reset Tooltip active flag...
              ToolTipActive = false;

              Rect rect = new Rect(Position.width - 20, 4, 16, 16);
              if (GUI.Button(rect, new GUIContent("", "Close Window")))
              {
            OnCreate = false;
            SelectedKerbal = null;
            ToolTip = "";
            if (HighLogic.LoadedScene == GameScenes.SPACECENTER)
              SMAddon.OnSmRosterClicked();
            else
              ShowWindow = false;
              }

              if (Event.current.type == EventType.Repaint && ShowToolTips)
            ToolTip = SMToolTips.SetActiveToolTip(rect, GUI.tooltip, ref ToolTipActive, 10);
              try
              {
            GUILayout.BeginVertical();
            DisplayRosterFilter();

            DisplayRosterListViewer();

            if (OnCreate)
              CreateKerbalViewer();
            else if (SelectedKerbal != null)
            {
              EditKerbalViewer();
            }
            else
            {
              GUILayout.BeginHorizontal();
              GUIContent guilabel = new GUIContent("Create Kerbal", "Opens the Kerbal creation editor.");
              if (GUILayout.Button(guilabel, GUILayout.MaxWidth(120), GUILayout.Height(20)))
              {
            OnCreate = true;
              }
              rect = GUILayoutUtility.GetLastRect();
              if (Event.current.type == EventType.Repaint && ShowToolTips)
            ToolTip = SMToolTips.SetActiveToolTip(rect, GUI.tooltip, ref ToolTipActive, 10);
              GUILayout.EndHorizontal();
            }

            GUILayout.EndVertical();
            GUI.DragWindow(new Rect(0, 0, Screen.width, 30));
            SMAddon.RepositionWindow(ref Position);
              }
              catch (Exception ex)
              {
            Utilities.LogMessage(String.Format(" in Roster Window.  Error:  {0} \r\n\r\n{1}", ex.Message, ex.StackTrace),
              Utilities.LogType.Error, true);
              }
        }