/// <summary>
        /// Initialize the screen.
        /// </summary>
        /// <param name="mgr">Game instance manager object for getting hte Instances</param>
        /// <param name="first">If true, this is the first screen after the splash, so Ctrl+Q exits, else Esc exits</param>
        public GameInstanceListScreen(GameInstanceManager mgr, bool first = false)
        {
            manager = mgr;

            AddObject(new ConsoleLabel(
                          1, 2, -1,
                          () => "Select or add a game instance:"
                          ));

            instanceList = new ConsoleListBox <GameInstance>(
                1, 4, -1, -2,
                manager.Instances.Values,
                new List <ConsoleListBoxColumn <GameInstance> >()
            {
                new ConsoleListBoxColumn <GameInstance>()
                {
                    Header   = "Default",
                    Width    = 7,
                    Renderer = StatusSymbol
                }, new ConsoleListBoxColumn <GameInstance>()
                {
                    Header   = "Name",
                    Width    = 20,
                    Renderer = k => k.Name
                }, new ConsoleListBoxColumn <GameInstance>()
                {
                    Header   = "Game",
                    Width    = 5,
                    Renderer = k => k.game.ShortName
                }, new ConsoleListBoxColumn <GameInstance>()
                {
                    Header   = "Version",
                    Width    = 12,
                    Renderer = k => k.Version()?.ToString() ?? noVersion,
                    Comparer = (a, b) => a.Version()?.CompareTo(b.Version() ?? GameVersion.Any) ?? 1
                }, new ConsoleListBoxColumn <GameInstance>()
                {
                    Header   = "Path",
                    Width    = 70,
                    Renderer = k => k.GameDir()
                }
            },
                1, 0, ListSortDirection.Descending
                );

            if (first)
            {
                AddTip("Ctrl+Q", "Quit");
                AddBinding(Keys.AltX, (object sender, ConsoleTheme theme) => false);
                AddBinding(Keys.CtrlQ, (object sender, ConsoleTheme theme) => false);
            }
            else
            {
                AddTip("Esc", "Quit");
                AddBinding(Keys.Escape, (object sender, ConsoleTheme theme) => false);
            }

            AddTip("Enter", "Select");
            AddBinding(Keys.Enter, (object sender, ConsoleTheme theme) => {
                ConsoleMessageDialog d = new ConsoleMessageDialog(
                    $"Loading instance {instanceList.Selection.Name}...",
                    new List <string>()
                    );

                if (TryGetInstance(theme, instanceList.Selection, (ConsoleTheme th) => { d.Run(th, (ConsoleTheme thm) => {}); }))
                {
                    try {
                        manager.SetCurrentInstance(instanceList.Selection.Name);
                    } catch (Exception ex) {
                        // This can throw if the previous current instance had an error,
                        // since it gets destructed when it's replaced.
                        RaiseError(ex.Message);
                    }
                    return(false);
                }
                else
                {
                    return(true);
                }
            });

            instanceList.AddTip("A", "Add");
            instanceList.AddBinding(Keys.A, (object sender, ConsoleTheme theme) => {
                LaunchSubScreen(theme, new GameInstanceAddScreen(manager));
                instanceList.SetData(manager.Instances.Values);
                return(true);
            });
            instanceList.AddTip("R", "Remove");
            instanceList.AddBinding(Keys.R, (object sender, ConsoleTheme theme) => {
                manager.RemoveInstance(instanceList.Selection.Name);
                instanceList.SetData(manager.Instances.Values);
                return(true);
            });
            instanceList.AddTip("E", "Edit");
            instanceList.AddBinding(Keys.E, (object sender, ConsoleTheme theme) => {
                ConsoleMessageDialog d = new ConsoleMessageDialog(
                    $"Loading instance {instanceList.Selection.Name}...",
                    new List <string>()
                    );
                TryGetInstance(theme, instanceList.Selection, (ConsoleTheme th) => { d.Run(theme, (ConsoleTheme thm) => {}); });
                // Still launch the screen even if the load fails,
                // because you need to be able to fix the name/path.
                LaunchSubScreen(theme, new GameInstanceEditScreen(manager, instanceList.Selection));

                return(true);
            });

            instanceList.AddTip("D", "Default");
            instanceList.AddBinding(Keys.D, (object sender, ConsoleTheme theme) => {
                string name = instanceList.Selection.Name;
                if (name == manager.AutoStartInstance)
                {
                    manager.ClearAutoStart();
                }
                else
                {
                    try {
                        manager.SetAutoStart(name);
                    } catch (NotKSPDirKraken k) {
                        ConsoleMessageDialog errd = new ConsoleMessageDialog(
                            $"Error loading {k.path}:\n{k.Message}",
                            new List <string>()
                        {
                            "OK"
                        }
                            );
                        errd.Run(theme);
                    }
                }
                return(true);
            });

            AddObject(instanceList);
            mainMenu = instanceList.SortMenu();
        }
Beispiel #2
0
 public void RemoveInstance_HasInstanceReturnsFalse()
 {
     manager.RemoveInstance(nameInReg);
     Assert.False(manager.HasInstance(nameInReg));
 }