Exemple #1
0
        public SpacetimePanel(int w, int h, SpaceTime time = null)
        {
            this.Width  = w;
            this.Height = h;
            Background  = ConsoleColor.White;
            renderers   = new Dictionary <SpacialElement, SpacialElementRenderer>();
            thingBinder = new SpacialElementBinder();

            this.SpaceTime = time ?? new SpaceTime(w, h, increment: TimeSpan.FromSeconds(.05));
            this.SpaceTime.QueueAction(() =>
            {
                RealTimeViewing = new RealTimeViewingFunction(this.SpaceTime)
                {
                    Enabled = true
                };
                this.SpaceTime.ChangeTrackingEnabled = true;
                this.SpaceTime.AfterTick.SubscribeForLifetime(() => UpdateView(false), this.LifetimeManager);

                RealTimeViewing.Behind.SubscribeForLifetime((isBehind) =>
                {
                    Application?.QueueAction(() =>
                    {
                        Background = isBehind ? ConsoleColor.DarkYellow : ConsoleColor.White;
                    });
                }, LifetimeManager);
            });

            this.AddedToVisualTree.SubscribeForLifetime(() =>
            {
                LifetimeManager.Manage(Application.SetInterval(() =>
                {
                    RealTimeViewing?.Evaluate();
                }, TimeSpan.FromSeconds(.1)));
            }, this.LifetimeManager);

            this.SpaceTime.UnhandledException.SubscribeForLifetime((ex) =>
            {
                Application?.QueueAction(() =>
                {
                    throw new AggregateException(ex);
                });
            }, this.LifetimeManager);

            this.SubscribeForLifetime(nameof(Bounds), () => { resizedSinceLastRender = false; }, this.LifetimeManager);
        }
        public LevelBuilder()
        {
            Cursor    = new Cursor();
            UndoStack = new UndoRedoStack();
            var topPanel = LayoutRoot.Add(new ConsolePanel()
            {
                Background = System.ConsoleColor.Black
            }).Fill(padding: new Thickness(0, 0, 0, 6));
            var botPanel = LayoutRoot.Add(new ConsolePanel()
            {
                Height = 6, Background = System.ConsoleColor.DarkRed
            }).DockToBottom().FillHoriontally();

            var borderPanel = topPanel.Add(new ConsolePanel()
            {
                Background = ConsoleColor.DarkGray, Width = LevelDefinition.Width + 2, Height = LevelDefinition.Height + 2
            }).CenterHorizontally().CenterVertically();

            ScenePanel = borderPanel.Add(new ScenePanel(LevelDefinition.Width, LevelDefinition.Height)).Fill(padding: new Thickness(1, 1, 1, 1));

            var sceneFPSLabel = LayoutRoot.Add(new Label()
            {
                Text = "".ToConsoleString()
            }).FillHoriontally();
            var renderFPSLabel = LayoutRoot.Add(new Label()
            {
                Y = 1, Text = "".ToConsoleString()
            }).FillHoriontally();
            var paintFPSLabel = LayoutRoot.Add(new Label()
            {
                Y = 2, Text = "".ToConsoleString()
            }).FillHoriontally();

            LifetimeManager.Manage(SetInterval(() =>
            {
                sceneFPSLabel.Text  = $"{ScenePanel.Scene.FPS} scene frames per second".ToCyan();
                renderFPSLabel.Text = $"{FPS} render frames per second".ToCyan();
                paintFPSLabel.Text  = $"{PPS} paint frames per second".ToCyan();
            }, TimeSpan.FromSeconds(1)));



            QueueAction(() =>
            {
                if (this.LevelId != null)
                {
                    LoadLevel(this.LevelId);
                }
                else
                {
                    CurrentLevelDefinition = new LevelDefinition();
                }

                PreviewScene.Start();
                SetupCursorKeyInput();
                SetupDropKeyInput();
                SetupSaveKeyInput();
            });

            PreviewScene.QueueAction(() =>
            {
                Cursor.Bounds.Resize(ScenePanel.PixelSize);
                PreviewScene.Add(Cursor);
            });
        }