private async Task <ServerInfo> CollectServerInfoFromUser()
        {
            var serverInfo = new ServerInfo {
                Server = "192.168.1.16", Port = 8080
            };
            Task dialogTask = null;

            await QueueAction(() =>
            {
                var panel = new ConsolePanel()
                {
                    Height = 10
                };
                var form     = panel.Add(new Form(FormOptions.FromObject(serverInfo))).Fill(padding: new Thickness(1, 1, 1, 2));
                var okButton = panel.Add(new Button()
                {
                    X = 1, Text = "OK".ToConsoleString(), Shortcut = new KeyboardShortcut(ConsoleKey.Enter)
                }).DockToBottom(padding: 1);

                okButton.Pressed.SubscribeOnce(Dialog.Dismiss);
                dialogTask = Dialog.Show(new ControlDialogOptions()
                {
                    Content = panel, MaxHeight = 8
                }).AsAwaitable();
            }).AsAwaitable();

            await dialogTask;

            return(serverInfo);
        }
Esempio n. 2
0
        public void ConsoleAppLifecycleTestBasic()
        {
            ConsoleProvider.Current = new CliUnitTestConsole();
            ConsoleApp app = new ConsoleApp(0, 0, 80, 10);

            int addCounter = 0, removeCounter = 0;

            app.ControlAdded.SubscribeForLifetime((c) => { addCounter++; }, app);
            app.ControlRemoved.SubscribeForLifetime((c) => { removeCounter++; }, app);
            app.LayoutRoot.Id = "LayoutRoot";
            ConsolePanel panel = app.LayoutRoot.Add(new ConsolePanel()
            {
                Id = "First panel"
            });

            // direct child
            Assert.AreEqual(1, addCounter);
            Assert.AreEqual(0, removeCounter);

            var button = panel.Add(new Button()
            {
                Id = "Button on first panel"
            });

            // grandchild
            Assert.AreEqual(2, addCounter);
            Assert.AreEqual(0, removeCounter);

            var innerPanel = new ConsolePanel()
            {
                Id = "InnerPanel"
            };
            var innerInnerPanel = innerPanel.Add(new ConsolePanel()
            {
                Id = "Inner Inner Panel"
            });

            // no change since not added to the app yet
            Assert.AreEqual(2, addCounter);
            Assert.AreEqual(0, removeCounter);

            panel.Add(innerPanel);

            // both child and grandchild found on add
            Assert.AreEqual(4, addCounter);
            Assert.AreEqual(0, removeCounter);

            // remove a nested child
            innerPanel.Controls.Remove(innerInnerPanel);
            Assert.AreEqual(4, addCounter);
            Assert.AreEqual(1, removeCounter);

            app.LayoutRoot.Controls.Clear();
            Assert.AreEqual(4, addCounter);
            Assert.AreEqual(4, removeCounter);
        }
Esempio n. 3
0
 private void InitNavKeys()
 {
     FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.Escape, null, () =>
     {
         if (currentContent != null && FocusManager.FocusedControl != null &&
             (FocusManager.FocusedControl == currentContent ||
              (currentContent is ConsolePanel && (currentContent as ConsolePanel).Descendents.Contains(FocusManager.FocusedControl)))
             )
         {
             currentNavButton?.TryFocus();
         }
         else
         {
             AnimatedDialog.Show((dialogHandle) =>
             {
                 var contentBg    = RGB.Yellow;
                 var bgCompliment = contentBg.GetCompliment();
                 var textColor    = RGB.Black.CalculateDistanceTo(bgCompliment) < RGB.MaxDistance * .75f ? RGB.Black : bgCompliment;
                 var panel        = new ConsolePanel()
                 {
                     Height = 11, Width = (int)Math.Round(LayoutRoot.Width * .5f), Background = contentBg
                 };
                 var label = panel.Add(new Label()
                 {
                     Text = "Press enter to quit or escape to resume".ToConsoleString(textColor, contentBg)
                 }).CenterBoth();
                 FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.Enter, null, Stop, panel);
                 FocusManager.GlobalKeyHandlers.PushForLifetime(ConsoleKey.Escape, null, dialogHandle.CloseDialog, panel);
                 return(panel);
             });
         }
     }, this);
 }
Esempio n. 4
0
        private void InitMenu()
        {
            var menuStack = layout.Add(new StackPanel()
            {
                Orientation = Orientation.Vertical
            }, 0, 0);

            menuStack.Add(new Label()
            {
                Text = "Samples Menu".ToYellow(underlined: true)
            });
            menuStack.Add(new Label()
            {
                Text = ConsoleString.Empty
            });
            var overviewButton = menuStack.Add(new Button()
            {
                Tag = MenuTag, Shortcut = new KeyboardShortcut(ConsoleKey.O, null), Text = "Overview".ToWhite()
            });

            SetupMenuItem(overviewButton, () =>
            {
                var panel = new ConsolePanel();
                var label = panel.Add(new Label()
                {
                    Text = "Welcome to the PowerArgs sample app.".ToGreen()
                }).CenterBoth();
                return(panel);
            });

            var calculatorButton = menuStack.Add(new Button()
            {
                Tag = MenuTag, Shortcut = new KeyboardShortcut(ConsoleKey.C, null), Text = "Calculator program".ToWhite()
            });

            SetupMenuItem(calculatorButton, () =>
            {
                var panel   = new ConsolePanel();
                var console = panel.Add(new SampleConsole(() => new CommandLineArgumentsDefinition(typeof(CalculatorProgram)))).Fill();
                return(panel);
            });

            var args = new PerfTestArgs()
            {
                Test = TestCase.FallingChars
            };
            var perfButton = menuStack.Add(new Button()
            {
                Tag = MenuTag, Shortcut = new KeyboardShortcut(ConsoleKey.P, null), Text = "Perf Test".ToWhite()
            });

            SetupMenuItem(perfButton, () =>
            {
                var panel = new StackPanel()
                {
                    Height = 3, Orientation = Orientation.Vertical
                };
                panel.Add(new Form(FormOptions.FromObject(args))
                {
                    Height = 2
                }).FillHorizontally();
                var runButton = panel.Add(new Button()
                {
                    Text = "Run".ToWhite(), Shortcut = new KeyboardShortcut(ConsoleKey.R)
                });

                runButton.Pressed.SubscribeOnce(() =>
                {
                    panel.Controls.Clear();
                    var console = panel.Add(new PerfTest(args)).Fill();
                });

                QueueAction(() => panel.Descendents.Where(d => d.CanFocus).FirstOrDefault()?.TryFocus());

                return(panel);
            });

            var colorArgs = new ColorTestArgs {
                From = ConsoleColor.Black, To = ConsoleColor.Green, Mode = ConsoleMode.VirtualTerminal
            };
            var colorButton = menuStack.Add(new Button()
            {
                Tag = MenuTag, Shortcut = new KeyboardShortcut(ConsoleKey.R, null), Text = "RGB Test".ToWhite()
            });

            SetupMenuItem(colorButton, () =>
            {
                var panel = new ConsolePanel()
                {
                    Height = 4
                };
                panel.Add(new Form(FormOptions.FromObject(colorArgs))
                {
                    Height = 3
                }).FillHorizontally();
                var runButton = panel.Add(new Button()
                {
                    Y = 3, Text = "Run".ToWhite(), Shortcut = new KeyboardShortcut(ConsoleKey.R)
                });

                runButton.Pressed.SubscribeOnce(() =>
                {
                    panel.Controls.Clear();

                    if (colorArgs.Mode == ConsoleMode.VirtualTerminal)
                    {
                        ConsoleProvider.Fancy = true;
                    }

                    if (colorArgs.Mode == ConsoleMode.Console)
                    {
                        ConsoleProvider.Fancy = false;
                    }

                    var toColor = panel.Add(new ConsolePanel()
                    {
                        Width = 20, Height = 3, Background = colorArgs.From
                    }).CenterBoth();
                    var label = toColor.Add(new Label()
                    {
                        Text = toColor.Background.ToRGBString().ToWhite(toColor.Background, underlined: true)
                    }).CenterBoth();
                    RGB.AnimateAsync(new RGBAnimationOptions()
                    {
                        Transitions = new System.Collections.Generic.List <System.Collections.Generic.KeyValuePair <RGB, RGB> >()
                        {
                            new System.Collections.Generic.KeyValuePair <RGB, RGB>(colorArgs.From, colorArgs.To),
                        }
                        ,
                        Duration         = 1500,
                        EasingFunction   = Animator.EaseInOut,
                        AutoReverse      = true,
                        Loop             = this,
                        AutoReverseDelay = 500,
                        OnColorsChanged  = (c) =>
                        {
                            toColor.Background = c[0];
                            label.Text         = toColor.Background.ToRGBString().ToWhite(toColor.Background, underlined: true);
                        }
                    });
                });

                QueueAction(() => panel.Descendents.Where(d => d.CanFocus).FirstOrDefault()?.TryFocus());

                return(panel);
            });

            overviewButton.Pressed.Fire();
        }
Esempio n. 5
0
        public HeadsUpDisplay(GameApp app)
        {
            this.gameApp = app;
            this.Height  = 7;

            var topPanel = Add(new StackPanel()
            {
                Orientation = Orientation.Horizontal, Height = 6
            }).FillHoriontally();
            var leftPanel = topPanel.Add(new StackPanel()
            {
                Orientation = Orientation.Vertical, Width = 12
            }).FillVertically();
            var middleGrid = topPanel.Add(new Grid(new List <object>()
            {
                new WeaponRow {
                    Weapon = "".ToWhite(), Trigger = "".ToWhite(), Amount = "".ToWhite()
                },
                new WeaponRow {
                    Weapon = "".ToWhite(), Trigger = "".ToWhite(), Amount = "".ToWhite()
                },
            })
            {
                Gutter = 0, ShowEndIfComplete = false, CanFocus = false, Width = 35
            }).FillVertically();

            messagePanel = topPanel.Add(new ConsolePanel()
            {
                IsVisible = false, Width = 31, Background = ConsoleColor.White
            }).FillVertically(padding: new Thickness(0, 0, 1, 1));
            var bottomPanel = Add(new StackPanel()
            {
                Orientation = Orientation.Horizontal, Margin = 2, Height = 1
            }).FillHoriontally().DockToBottom();

            var hpLabel = leftPanel.Add(new Label()
            {
                Text = "HP".ToGray()
            }).FillHoriontally();
            var hpValue = leftPanel.Add(new Label()
            {
                Text = ConsoleString.Empty
            }).FillHoriontally();
            var spacer = leftPanel.Add(new Label()
            {
                Text = ConsoleString.Empty
            });
            var aimLabel = leftPanel.Add(new Label()
            {
                Text = "AIM".ToGray()
            }).FillHoriontally();
            var aimValue = leftPanel.Add(new Label()
            {
                Text = "".ToWhite()
            }).FillHoriontally();

            middleGrid.VisibleColumns[0].ColumnDisplayName = new ConsoleString(middleGrid.VisibleColumns[0].ColumnDisplayName.ToString(), ConsoleColor.Gray);
            middleGrid.VisibleColumns[1].ColumnDisplayName = new ConsoleString(middleGrid.VisibleColumns[1].ColumnDisplayName.ToString(), ConsoleColor.Gray);
            middleGrid.VisibleColumns[2].ColumnDisplayName = new ConsoleString(middleGrid.VisibleColumns[2].ColumnDisplayName.ToString(), ConsoleColor.Gray);

            middleGrid.VisibleColumns[0].OverflowBehavior = new TruncateOverflowBehavior()
            {
                TruncationText = "", ColumnWidth = 15
            };
            middleGrid.VisibleColumns[1].OverflowBehavior = new TruncateOverflowBehavior()
            {
                TruncationText = "", ColumnWidth = 10
            };
            middleGrid.VisibleColumns[2].OverflowBehavior = new TruncateOverflowBehavior()
            {
                TruncationText = "", ColumnWidth = 10
            };
            var menuLabel = bottomPanel.Add(new Label()
            {
                Text = "".ToYellow()
            });
            var pauseLabel = bottomPanel.Add(new Label()
            {
                Text = "".ToYellow()
            });
            var quitLabel = bottomPanel.Add(new Label()
            {
                Text = "Quit [ESC]".ToYellow()
            });

            messageLabel = messagePanel.Add(new Label()
            {
                Mode = LabelRenderMode.MultiLineSmartWrap, Background = ConsoleColor.White, Text = "".ToBlack(bg: ConsoleColor.White)
            }).Fill(padding: new Thickness(1, 1, 1, 1));

            app.SubscribeProxiedForLifetime(app, nameof(app.MainCharacter) + "." + nameof(MainCharacter.HealthPoints), () =>
            {
                var hp       = app.MainCharacter?.HealthPoints;
                hpValue.Text = hp.HasValue ? FormatHPValue(hp.Value) : "unknown".ToRed();
            }, this.LifetimeManager);


            app.SynchronizeProxiedForLifetime(app, nameof(app.InputManager) + "." + nameof(GameInputManager.KeyMap) + "." + nameof(KeyMap.MenuKey), () =>
            {
                menuLabel.Text = $"Menu [{app.InputManager.KeyMap.MenuKey}]".ToYellow();
            }, this.LifetimeManager);

            app.SynchronizeProxiedForLifetime(app, nameof(app.InputManager) + "." + nameof(GameInputManager.KeyMap) + "." + nameof(KeyMap.TogglePauseKey), () =>
            {
                pauseLabel.Text = $"Pause [{app.InputManager.KeyMap.TogglePauseKey}]".ToYellow();
            }, this.LifetimeManager);

            app.SynchronizeProxiedForLifetime(app, nameof(app.InputManager) + "." + nameof(GameInputManager.KeyMap) + "." + ObservableObject.AnyProperty, () =>
            {
                var primaryWeaponRow     = ((WeaponRow)(middleGrid.DataSource as MemoryDataSource).Items[0]);
                primaryWeaponRow.Trigger = $"[{this.gameApp.InputManager.KeyMap.PrimaryWeaponKey}]".ToWhite();

                var explosiveWeaponRow     = ((WeaponRow)(middleGrid.DataSource as MemoryDataSource).Items[1]);
                explosiveWeaponRow.Trigger = $"[{this.gameApp.InputManager.KeyMap.ExplosiveWeaponKey}]".ToWhite();


                aimLabel.Text = $"Aim[{this.gameApp.InputManager.KeyMap.AimToggleKey}]".ToGray();
            }, this.LifetimeManager);

            app.SynchronizeProxiedForLifetime(app, nameof(app.MainCharacter) + "." + nameof(MainCharacter.Inventory) + "." + nameof(Inventory.PrimaryWeapon), () =>
            {
                var row        = ((WeaponRow)(middleGrid.DataSource as MemoryDataSource).Items[0]);
                var weaponName = app.MainCharacter?.Inventory?.PrimaryWeapon?.GetType().Name;
                row.Weapon     = weaponName != null ? weaponName.ToWhite() : "none".ToRed();
                if (weaponName != null)
                {
                    ShowMessage("Equipped ".ToBlack() + weaponName.ToDarkBlue(), TimeSpan.FromSeconds(4));
                }
            }, this.LifetimeManager);

            app.SynchronizeProxiedForLifetime(app,
                                              nameof(app.MainCharacter) + "." +
                                              nameof(MainCharacter.Inventory) + "." +
                                              nameof(Inventory.PrimaryWeapon) + "." +
                                              nameof(Weapon.AmmoAmount), () =>
            {
                var row    = ((WeaponRow)(middleGrid.DataSource as MemoryDataSource).Items[0]);
                var ammo   = app.MainCharacter?.Inventory?.PrimaryWeapon?.AmmoAmount;
                row.Amount = ammo.HasValue ? FormatAmmoAmmount(ammo.Value): "empty".ToRed();
            }, this.LifetimeManager);

            app.SynchronizeProxiedForLifetime(app, nameof(app.MainCharacter) + "." + nameof(MainCharacter.Inventory) + "." + nameof(Inventory.ExplosiveWeapon), () =>
            {
                var row        = ((WeaponRow)(middleGrid.DataSource as MemoryDataSource).Items[1]);
                var weaponName = app.MainCharacter?.Inventory?.ExplosiveWeapon?.GetType().Name;
                row.Weapon     = weaponName != null ? weaponName.ToWhite() : "none".ToRed();
                if (weaponName != null)
                {
                    ShowMessage("Equipped ".ToBlack() + weaponName.ToDarkBlue(), TimeSpan.FromSeconds(4));
                }
            }, this.LifetimeManager);

            app.SynchronizeProxiedForLifetime(app,
                                              nameof(app.MainCharacter) + "." +
                                              nameof(MainCharacter.Inventory) + "." +
                                              nameof(Inventory.ExplosiveWeapon) + "." +
                                              nameof(Weapon.AmmoAmount), () =>
            {
                var row    = ((WeaponRow)(middleGrid.DataSource as MemoryDataSource).Items[1]);
                var ammo   = app.MainCharacter?.Inventory?.ExplosiveWeapon?.AmmoAmount;
                row.Amount = ammo.HasValue ? FormatAmmoAmmount(ammo.Value) : "empty".ToRed();
            }, this.LifetimeManager);



            app.SynchronizeProxiedForLifetime(app, nameof(app.MainCharacter) + "." + nameof(MainCharacter.AimMode), () =>
            {
                var aimMode   = app.MainCharacter?.AimMode;
                aimValue.Text = aimMode.HasValue ? aimMode.Value.ToString().ToConsoleString(aimMode.Value == AimMode.Auto ? ConsoleColor.White : ConsoleColor.Cyan) : "".ToConsoleString();
            }, this.LifetimeManager);
        }