Inheritance: Rectangular
        private ConsoleBitmapDiffFrame PrepareDiffFrame(ConsoleBitmap bitmap)
        {
            ConsoleBitmapDiffFrame diff = new ConsoleBitmapDiffFrame();

            diff.Diffs = new List <ConsoleBitmapPixelDiff>();
            int changes = 0;

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    var pixel = bitmap.GetPixel(x, y);
                    if (pixel.HasChanged)
                    {
                        changes++;
                        if (pixel.Value.HasValue)
                        {
                            diff.Diffs.Add(new ConsoleBitmapPixelDiff()
                            {
                                X     = x,
                                Y     = y,
                                Value = pixel.Value.Value
                            });
                        }
                    }
                }
            }

            return(diff);
        }
Beispiel #2
0
        private void RenderDataPoint(ConsoleBitmap context, DataSeries series, DataPoint p, bool focused)
        {
            var x = ConvertXValueToPixel(p.X);
            var y = ConvertYValueToPixel(p.Y);

            if (focused)
            {
                context.Pen = new ConsoleCharacter(series.PlotCharacter, Application.Theme.FocusColor, Application.Theme.FocusColor);
            }
            else if (series.Threshold == null || series.Threshold.IsActive(p.Y) == false)
            {
                context.Pen = new ConsoleCharacter(series.PlotCharacter, series.PlotColor, Background);
            }
            else
            {
                context.Pen = new ConsoleCharacter(series.PlotCharacter, series.Threshold.ActiveColor, Background);
            }

            context.DrawPoint(x, y);

            while (series.ShowAreaUnderEachDataPoint && ++y <= YAxisBottom)
            {
                context.DrawPoint(x, y);
            }
        }
 public void Clear()
 {
     Buffer = new ConsoleBitmap(0, 0, this.BufferWidth, this.WindowHeight);
     this.BufferWidth = Buffer.Width;
     this.CursorLeft = 0;
     this.CursorTop = 0;
 }
Beispiel #4
0
 protected override void OnPaint(ConsoleBitmap context)
 {
     context.Pen = new ConsoleCharacter(' ', null, myFocusStackDepth == Application.FocusManager.StackDepth ? Theme.DefaultTheme.H1Color : Theme.DefaultTheme.DisabledColor);
     context.DrawLine(0, 0, Width, 0);
     context.DrawLine(0, Height - 1, Width, Height - 1);
     base.OnPaint(context);
 }
Beispiel #5
0
        /// <summary>
        /// Creates a new console app given a set of boundaries
        /// </summary>
        /// <param name="w">The width of the app</param>
        /// <param name="h">The height of the app</param>
        public ConsoleApp(int w, int h)
        {
            this.Name              = GetType().Name;
            this.console           = ConsoleProvider.Current;
            this.lastConsoleWidth  = this.console.BufferWidth;
            this.lastConsoleHeight = this.console.WindowHeight;
            this.observable        = new ObservableObject(this);

            cycleRateMeter = new FrameRateMeter();

            this.StartOfCycle.SubscribeForLifetime(Cycle, this);

            SetFocusOnStart = true;
            Bitmap          = new ConsoleBitmap(w, h);
            LayoutRoot      = new ConsolePanel {
                Width = w, Height = h
            };
            FocusManager           = new FocusManager();
            LayoutRoot.Application = this;
            isFullScreen           = false;
            FocusManager.SubscribeForLifetime(nameof(FocusManager.FocusedControl), () => Paint(), this);
            LayoutRoot.Controls.BeforeAdded.SubscribeForLifetime((c) => { c.Application = this; c.BeforeAddedToVisualTreeInternal(); }, this);
            LayoutRoot.Controls.BeforeRemoved.SubscribeForLifetime((c) => { c.BeforeRemovedFromVisualTreeInternal(); }, this);
            LayoutRoot.Controls.Added.SubscribeForLifetime(ControlAddedToVisualTree, this);
            LayoutRoot.Controls.Removed.SubscribeForLifetime(ControlRemovedFromVisualTree, this);
            WindowResized.SubscribeForLifetime(HandleDebouncedResize, this);
            this.LoopStarted.SubscribeOnce(() => _current = this);
            this.EndOfCycle.SubscribeForLifetime(DrainPaints, this);
        }
Beispiel #6
0
        protected override void OnPaint(ConsoleBitmap context)
        {
            ScrollableContent.Paint();

            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    int scrollX = x + HorizontalScrollUnits;
                    int scrollY = y + VerticalScrollUnits;

                    if (scrollX >= ScrollableContent.Width || scrollY >= ScrollableContent.Height)
                    {
                        continue;
                    }

                    var scrolledPixel = ScrollableContent.Bitmap.GetPixel(scrollX, scrollY);
                    context.DrawPoint(scrolledPixel, x, y);
                }
            }

            verticalScrollbar.Paint();
            horizontalScrollbar.Paint();
            DrawScrollbar(verticalScrollbar, context);
            DrawScrollbar(horizontalScrollbar, context);
        }
Beispiel #7
0
        /// <summary>
        /// paints the text box
        /// </summary>
        /// <param name="context"></param>
        protected override void OnPaint(ConsoleBitmap context)
        {
            var toPaint       = textState.CurrentValue;
            var bgTransformed = new List <ConsoleCharacter>();

            foreach (var c in toPaint)
            {
                if (c.BackgroundColor == ConsoleString.DefaultBackgroundColor && Background != ConsoleString.DefaultBackgroundColor)
                {
                    bgTransformed.Add(new ConsoleCharacter(c.Value, Foreground, Background));
                }
                else
                {
                    bgTransformed.Add(c);
                }
            }

            context.DrawString(new ConsoleString(bgTransformed), 0, 0);

            if (blinkState)
            {
                char blinkChar = textState.CursorPosition >= toPaint.Length ? ' ' : toPaint[textState.CursorPosition].Value;
                context.Pen = new ConsoleCharacter(blinkChar, Application.Theme.FocusContrastColor, Application.Theme.FocusColor);
                context.DrawPoint(textState.CursorPosition, 0);
            }
        }
Beispiel #8
0
 private void RenderThresholds(ConsoleBitmap context)
 {
     foreach (var series in ViewModel.DataSeriesCollection.OrderBy(s => s == ViewModel.FocusedDataSeries ? 1 : 0))
     {
         RenderThreshold(context, series);
     }
 }
        protected override void OnPaint(ConsoleBitmap context)
        {
            var fullSize = ScrollableContentSize;
            ConsoleBitmap fullyPaintedPanel = new ConsoleBitmap(0, 0, fullSize.Width, fullSize.Height);
            ScrollableContent.PaintTo(fullyPaintedPanel);

            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    int scrollX = x + HorizontalScrollUnits;
                    int scrollY = y + VerticalScrollUnits;

                    if (scrollX >= fullyPaintedPanel.Width || scrollY >= fullyPaintedPanel.Height)
                    {
                        continue;
                    }

                    var scrolledPixel = fullyPaintedPanel.GetPixel(scrollX, scrollY);

                    if (scrolledPixel.Value.HasValue)
                    {
                        context.Pen = scrolledPixel.Value.Value;
                    }
                    else
                    {
                        context.Pen = new ConsoleCharacter(' ', backgroundColor: Background);
                    }

                    context.DrawPoint(x, y);
                }
            }

            base.OnPaint(context);
        }
Beispiel #10
0
        private void RenderTitle(ConsoleBitmap context)
        {
            int yOffset = 0;

            foreach (var series in ViewModel.DataSeriesCollection)
            {
                var title = new ConsoleString(series.Title, series.PlotColor, Background);

                if (HasFocus && ViewModel.FocusedDataPointIndex >= 0 && ViewModel.FocusedDataPointIndex < series.DataPoints.Count && ViewModel.FocusedDataSeries == series)
                {
                    var xValue = XAxisValueFormatter(series.DataPoints[ViewModel.FocusedDataPointIndex].X);
                    var yValue = YAxisValueFormatter(series.DataPoints[ViewModel.FocusedDataPointIndex].Y);
                    title += new ConsoleString(" ( " + xValue + ", " + yValue + " )", Application.Theme.FocusColor);
                }

                if (title.Length > MaxTitleLength)
                {
                    title = title.Substring(0, MaxTitleLength) + ("_");
                }

                var titleLeft = XAxisLeft + ((XAxisWidth / 2) - (title.Length / 2));
                context.DrawString(title, titleLeft, YAxisTop + 1 + yOffset);
                yOffset++;
            }
        }
Beispiel #11
0
        /// <summary>
        /// paints the text box
        /// </summary>
        /// <param name="context"></param>
        protected override void OnPaint(ConsoleBitmap context)
        {
            var toPaint = textState.CurrentValue;

            var offset = 0;

            if (toPaint.Length >= Width && textState.CursorPosition > Width - 1)
            {
                offset  = (textState.CursorPosition + 1) - Width;
                toPaint = toPaint.Substring(offset);
            }

            var bgTransformed = new List <ConsoleCharacter>();

            foreach (var c in toPaint)
            {
                if (c.BackgroundColor == ConsoleString.DefaultBackgroundColor && Background != ConsoleString.DefaultBackgroundColor)
                {
                    bgTransformed.Add(new ConsoleCharacter(c.Value, Foreground, Background));
                }
                else
                {
                    bgTransformed.Add(c);
                }
            }

            context.DrawString(new ConsoleString(bgTransformed), 0, 0);

            if (blinkState && BlinkEnabled)
            {
                char blinkChar = textState.CursorPosition >= toPaint.Length ? ' ' : toPaint[textState.CursorPosition].Value;
                var  pen       = new ConsoleCharacter(blinkChar, DefaultColors.FocusContrastColor, DefaultColors.FocusColor);
                context.DrawPoint(pen, textState.CursorPosition - offset, 0);
            }
        }
Beispiel #12
0
        protected override void OnPaint(ConsoleBitmap context)
        {
            ScrollableContent.Paint();

            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    int scrollX = x + HorizontalScrollUnits;
                    int scrollY = y + VerticalScrollUnits;

                    if (scrollX >= ScrollableContent.Width || scrollY >= ScrollableContent.Height)
                    {
                        continue;
                    }

                    var scrolledPixel = ScrollableContent.Bitmap.GetPixel(scrollX, scrollY);

                    if (scrolledPixel.Value.HasValue)
                    {
                        context.Pen = scrolledPixel.Value.Value;
                    }
                    else
                    {
                        context.Pen = new ConsoleCharacter(' ', backgroundColor: Background);
                    }

                    context.DrawPoint(x, y);
                }
            }
        }
Beispiel #13
0
 protected override void OnPaint(ConsoleBitmap context)
 {
     base.OnPaint(context);
     if (HasFocus)
     {
         context.FillRectUnsafe(new ConsoleCharacter(' ', backgroundColor: DefaultColors.FocusColor), 0, 0, Width, Height);
     }
 }
Beispiel #14
0
 /// <summary>
 /// Paints the diff on top of the given image which, unlike with raw frames, cannot be null,
 /// since a diff frame can only be applied to an existing image
 /// </summary>
 /// <param name="bitmap">the image to apply the diff to</param>
 /// <returns>the same image reference you passed in, updated with the diff</returns>
 public override ConsoleBitmap Paint(ref ConsoleBitmap bitmap)
 {
     foreach (var diff in Diffs)
     {
         bitmap.Pixels[diff.X][diff.Y] = diff.Value;
     }
     return(bitmap);
 }
Beispiel #15
0
        protected override void OnPaint(ConsoleBitmap context)
        {
            RenderTitle(context);
            RenderXAxis(context);
            RenderYAxis(context);

            RenderThresholds(context);
            RenderDataPoints(context);
        }
 /// <summary>
 /// Paints the diff on top of the given image which, unlike with raw frames, cannot be null,
 /// since a diff frame can only be applied to an existing image
 /// </summary>
 /// <param name="bitmap">the image to apply the diff to</param>
 /// <returns>the same image reference you passed in, updated with the diff</returns>
 public override ConsoleBitmap Paint(ref ConsoleBitmap bitmap)
 {
     foreach (var diff in Diffs)
     {
         bitmap.Pen = diff.Value;
         bitmap.DrawPoint(diff.X, diff.Y);
     }
     return(bitmap);
 }
Beispiel #17
0
 protected override void OnPaint(ConsoleBitmap context)
 {
     base.OnPaint(context);
     if (HasFocus)
     {
         context.Pen = new ConsoleCharacter(' ', backgroundColor: Application.Theme.FocusColor);
         context.FillRect(0, 0, Width, Height);
     }
 }
Beispiel #18
0
 private void DrawScrollbar(Scrollbar bar, ConsoleBitmap context)
 {
     for (int x = 0; x < bar.Width; x++)
     {
         for (int y = 0; y < bar.Height; y++)
         {
             context.DrawPoint(bar.Bitmap.GetPixel(x, y).Value.Value, x + bar.X, y + bar.Y);
         }
     }
 }
Beispiel #19
0
        internal override void OnPaint(ConsoleBitmap context)
        {
            context.Pen = Foreground;
            RenderTitle(context);
            RenderXAxis(context);
            RenderYAxis(context);

            RenderThresholds(context);
            RenderDataPoints(context);
        }
Beispiel #20
0
 /// <summary>
 /// Paints this control
 /// </summary>
 /// <param name="context">the drawing surface</param>
 protected override void OnPaint(ConsoleBitmap context)
 {
     foreach (var control in GetPaintOrderedControls())
     {
         if (control.Width > 0 && control.Height > 0 && control.IsVisible)
         {
             Compose(control);
         }
     }
 }
Beispiel #21
0
 public void CloneTo(ConsoleBitmap ret)
 {
     for (var x = 0; x < Width; x++)
     {
         for (var y = 0; y < Height; y++)
         {
             ret.Pen = this.GetPixel(x, y).Value.HasValue ? this.GetPixel(x, y).Value.Value : new ConsoleCharacter(' ');
             ret.DrawPoint(x, y);
         }
     }
 }
Beispiel #22
0
        protected override void OnPaint(ConsoleBitmap context)
        {
#if PROFILING
            using (new TimeProfiler("Grid.OnPaint"))
            {
#endif
            PaintInternal(context);
#if PROFILING
        }
#endif
        }
Beispiel #23
0
 protected override void OnPaint(ConsoleBitmap context)
 {
     #if PROFILING
     using (new TimeProfiler("Grid.OnPaint"))
     {
     #endif
         PaintInternal(context);
     #if PROFILING
     }
     #endif
 }
Beispiel #24
0
        internal override void OnPaint(ConsoleBitmap context)
        {
            base.OnPaint(context);

            int y = 0;
            foreach(var item in ViewModel.Items)
            {
                var displayString = this.HasFocus && y == ViewModel.SelectedIndex && ViewModel.IsSelectionEnabled ? new ConsoleString(item.DisplayText, this.FocusForeground.ForegroundColor) : item.RichDisplayText;
                context.DrawString(displayString, 0, y++);
            }
        }
Beispiel #25
0
        /// <summary>
        /// Writes the given bitmap image as a frame to the stream.  If this is the first image or more than half of the pixels have
        /// changed then a raw frame will be written.   Otherwise, a diff frame will be written.
        ///
        /// This method uses the system's wall clock to determine the timestamp for this frame. The timestamp will be
        /// relative to the wall clock time when the first frame was written.
        /// </summary>
        /// <param name="bitmap">the image to write</param>
        /// <param name="desiredFrameTime">if provided, sstamp the frame with this time, otherwise stamp it with the wall clock delta from the first frame time</param>
        /// <param name="force">if true, writes the frame even if there are no changes</param>
        /// <returns>the same bitmap that was passed in</returns>
        public ConsoleBitmap WriteFrame(ConsoleBitmap bitmap, bool force = false, TimeSpan?desiredFrameTime = null)
        {
            if (pausedAt.HasValue)
            {
                return(bitmap);
            }

            var rawFrame = GetRawFrame(bitmap);

            var now = DateTime.UtcNow - TotalPauseTime;

            if (firstFrameTime.HasValue == false)
            {
                rawFrame.Timestamp = TimeSpan.Zero;
                firstFrameTime     = now;
            }
            else
            {
                rawFrame.Timestamp = desiredFrameTime.HasValue ? desiredFrameTime.Value : now - firstFrameTime.Value;
            }

            if (lastFrame == null)
            {
                StreamHeader(bitmap);
                writer.Write(serializer.SerializeFrame(rawFrame));
                FramesWritten++;
            }
            else
            {
                if (GetEffectiveWidth(bitmap) != lastFrame.Pixels.Length || GetEffectiveHeight(bitmap) != lastFrame.Pixels[0].Length)
                {
                    throw new InvalidOperationException("Video frame has changed size");
                }

                var diff = PrepareDiffFrame(lastFrame, bitmap);
                diff.Timestamp = rawFrame.Timestamp;

                var numPixels = GetEffectiveWidth(bitmap) * GetEffectiveHeight(bitmap);
                if (force || diff.Diffs.Count > numPixels / 2)
                {
                    writer.Write(serializer.SerializeFrame(rawFrame));
                    FramesWritten++;
                }
                else if (diff.Diffs.Count > 0)
                {
                    writer.Write(serializer.SerializeFrame(diff));
                    FramesWritten++;
                }
            }

            lastFrame = rawFrame;
            return(bitmap);
        }
Beispiel #26
0
 /// <summary>
 /// Paints the entire frame onto the given bitmap.  If the given bitmap is null then
 /// a new bitmap of the correct size will be created and assigned to the reference you
 /// have provided.  The normal usage pattern is to pass null when reading the first frame,
 /// which will always be a raw frame.  You can then pass this same bitmap to subsequent calls
 /// to Paint, and it will work whether the subsequent frames are raw frames or diff frames.
 ///
 /// </summary>
 /// <param name="bitmap">The bitmap to paint on or null to create a new bitmap from the raw frame</param>
 /// <returns>the same bitmap you passed in or one that was created for you</returns>
 public override ConsoleBitmap Paint(ref ConsoleBitmap bitmap)
 {
     bitmap = bitmap ?? new ConsoleBitmap(Pixels.Length, Pixels[0].Length);
     for (var x = 0; x < Pixels.Length; x++)
     {
         for (var y = 0; y < Pixels[0].Length; y++)
         {
             bitmap.Pixels[x][y] = Pixels[x][y];
         }
     }
     return(bitmap);
 }
Beispiel #27
0
        internal override void OnPaint(ConsoleBitmap context)
        {
            base.OnPaint(context);

            int y = 0;

            foreach (var item in ViewModel.Items)
            {
                var displayString = this.HasFocus && y == ViewModel.SelectedIndex && ViewModel.IsSelectionEnabled ? new ConsoleString(item.DisplayText, this.FocusForeground.ForegroundColor) : item.RichDisplayText;
                context.DrawString(displayString, 0, y++);
            }
        }
Beispiel #28
0
 public void Do()
 {
     myTime   = studio.CurrentFrame.FrameTime;
     myBitmap = studio.CurrentBitmap.Clone();
     studio.animation.Frames.Insert(myIndex, new InMemoryConsoleBitmapFrame()
     {
         Bitmap = myBitmap, FrameTime = myTime
     });
     studio.Refresh();
     studio.CurrentFrameIndex          = myIndex + 1;
     studio.frameList.SelectedRowIndex = myIndex + 1;
 }
Beispiel #29
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         // free managed resources
         if (bitmap != null)
         {
             bitmap.Unlock();
             bitmap = null;
         }
     }
 }
Beispiel #30
0
        protected override void OnPaint(ConsoleBitmap context)
        {
            var drawState = new ConsoleString();

            drawState = "[".ToConsoleString(CanFocus ? Application.Theme.H1Color : Application.Theme.DisabledColor, Background = Background);
            if (Text != null)
            {
                ConsoleColor fg, bg;

                if (HasFocus)
                {
                    fg = Application.Theme.FocusContrastColor;
                    bg = Application.Theme.FocusColor;
                }
                else if (CanFocus)
                {
                    fg = Foreground;
                    bg = Background;
                }
                else
                {
                    fg = Application.Theme.DisabledColor;
                    bg = Background;
                }

                drawState += new ConsoleString(Text, fg, bg);

                if (Shortcut != null)
                {
                    if (Shortcut.Modifier.HasValue && Shortcut.Modifier == ConsoleModifiers.Alt)
                    {
                        drawState += new ConsoleString($" (ALT+{Shortcut.Key})", CanFocus ? Application.Theme.H1Color : Application.Theme.DisabledColor);
                    }
                    else if (Shortcut.Modifier.HasValue && Shortcut.Modifier == ConsoleModifiers.Shift)
                    {
                        drawState += new ConsoleString($" (SHIFT+{Shortcut.Key})", CanFocus ? Application.Theme.H1Color : Application.Theme.DisabledColor);
                    }
                    else if (Shortcut.Modifier.HasValue && Shortcut.Modifier == ConsoleModifiers.Control)
                    {
                        drawState += new ConsoleString($" (CTL+{Shortcut.Key})", CanFocus ? Application.Theme.H1Color : Application.Theme.DisabledColor);
                    }
                    else
                    {
                        drawState += new ConsoleString($" ({Shortcut.Key})", CanFocus ? Application.Theme.H1Color : Application.Theme.DisabledColor);
                    }
                }
            }

            drawState += "]".ToConsoleString(CanFocus ? Application.Theme.H1Color : Application.Theme.DisabledColor, Background);
            Width      = drawState.Length;
            context.DrawString(drawState, 0, 0);
        }
Beispiel #31
0
        /// <summary>
        /// Loads a video from a given stream
        /// </summary>
        /// <param name="videoStream">the video stream</param>
        public void Load(Stream videoStream)
        {
            if (Application == null)
            {
                throw new InvalidOperationException("Can't load until the control has been added to an application");
            }

            Task.Factory.StartNew(() =>
            {
                try
                {
                    var reader = new ConsoleBitmapStreamReader(videoStream);
                    reader.ReadToEnd((videoWithProgressInfo) =>
                    {
                        inMemoryVideo = inMemoryVideo ?? videoWithProgressInfo;
                        this.duration = videoWithProgressInfo.Duration;
                        Application.QueueAction(() =>
                        {
                            if (this.CurrentFrame == null)
                            {
                                this.CurrentFrame = videoWithProgressInfo.Frames[0].Bitmap;
                                playerProgressBar.ShowPlayCursor = true;
                                playButton.CanFocus            = true;
                                seekToBeginningButton.CanFocus = true;
                                seekBack10SButton.CanFocus     = true;
                                seekForward10SButton.CanFocus  = true;
                                seekToEndButton.CanFocus       = true;
                                State = PlayerState.Stopped;
                                if (Application.FocusManager.FocusedControl == null)
                                {
                                    Application.FocusManager.TrySetFocus(playButton);
                                }
                            }

                            playerProgressBar.LoadProgressPosition = inMemoryVideo.LoadProgress;
                        });
                        if (AfterFrameLoadDelay.HasValue)
                        {
                            Thread.Sleep(AfterFrameLoadDelay.Value);
                        }
                    });
                }
                catch (Exception ex)
                {
                    Application.QueueAction(() =>
                    {
                        failedMessage = ex.Message;
                        State         = PlayerState.Failed;
                    });
                }
            });
        }
Beispiel #32
0
        /// <summary>
        /// Makes a copy of this bitmap
        /// </summary>
        /// <returns>a copy of this bitmap</returns>
        public ConsoleBitmap Clone()
        {
            var ret = new ConsoleBitmap(X, Y, Width, Height);

            for (var x = 0; x < Width; x++)
            {
                for (var y = 0; y < Height; y++)
                {
                    ret.Pen = this.GetPixel(x, y).Value.HasValue ? this.GetPixel(x, y).Value.Value : new ConsoleCharacter(' ');
                    ret.DrawPoint(x, y);
                }
            }
            return(ret);
        }
Beispiel #33
0
 /// <summary>
 /// Draws the bitmap
 /// </summary>
 /// <param name="context">the pain context</param>
 protected override void OnPaint(ConsoleBitmap context)
 {
     if (Bitmap == null)
     {
         return;
     }
     for (var x = 0; x < Bitmap.Width && x < this.Width; x++)
     {
         for (var y = 0; y < Bitmap.Height && y < this.Height; y++)
         {
             ref var pixel = ref Bitmap.GetPixel(x, y);
             context.DrawPoint(pixel, x, y);
         }
     }
Beispiel #34
0
        public ConsoleApp(int x, int y, int w, int h)
        {
            Bitmap     = new ConsoleBitmap(x, y, w, h);
            LayoutRoot = new ConsolePanel {
                Width = w, Height = h
            };
            LayoutRoot.Application     = this;
            focusableControls          = new List <ConsoleControl>();
            focusIndex                 = -1;
            ExitOnEscapeCharacter      = true;
            LayoutRoot.Controls.Added += (c) =>
            {
                c.Application = this;
                if (c.CanFocus)
                {
                    focusableControls.Add(c);
                }

                if (c is ConsolePanel)
                {
                    var children = TraverseControlTree(c as ConsolePanel);
                    focusableControls.AddRange(children.Where(child => child.CanFocus));
                    focusableControls = focusableControls.Distinct().ToList();
                    foreach (var child in children)
                    {
                        child.Application = this;
                    }
                }
            };

            LayoutRoot.Controls.Removed += (c) =>
            {
                c.Application = null;
                if (c.CanFocus)
                {
                    focusableControls.Remove(c);
                }

                if (c is ConsolePanel)
                {
                    var children = TraverseControlTree(c as ConsolePanel);
                    foreach (var child in children)
                    {
                        focusableControls.Remove(child);
                        child.Application = null;
                    }
                }
            };
        }
Beispiel #35
0
 internal override void OnPaint(ConsoleBitmap context)
 {
     foreach (var control in Controls)
     {
         Rectangle scope = context.GetScope();
         try
         {
             context.Rescope(control.X, control.Y, control.Width, control.Height);
             control.Paint(context);
         }
         finally
         {
             context.Scope(scope);
         }
     }
 }
Beispiel #36
0
 /// <summary>
 /// Creates a new console app given a set of boundaries
 /// </summary>
 /// <param name="x">The left position on the target console to bound this app</param>
 /// <param name="y">The right position on the target console to bound this app</param>
 /// <param name="w">The width of the app</param>
 /// <param name="h">The height of the app</param>
 public ConsoleApp(int x, int y, int w, int h)
     : base(ConsoleProvider.Current)
 {
     SetFocusOnStart = true;
     Theme = new Theme();
     Bitmap = new ConsoleBitmap(x, y, w, h);
     LayoutRoot = new ConsolePanel { Width = w, Height = h };
     FocusManager = new FocusManager();
     LayoutRoot.Application = this;
     AutoFillOnConsoleResize = false;
     FocusManager.SubscribeForLifetime(nameof(FocusManager.FocusedControl), Paint, LifetimeManager);
     LayoutRoot.Controls.BeforeAdded.SubscribeForLifetime((c) => { c.Application = this; c.BeforeAddedToVisualTreeInternal(); }, LifetimeManager);
     LayoutRoot.Controls.BeforeRemoved.SubscribeForLifetime((c) => { c.BeforeRemovedFromVisualTreeInternal(); }, LifetimeManager);
     LayoutRoot.Controls.Added.SubscribeForLifetime(ControlAddedToVisualTree, LifetimeManager);
     LayoutRoot.Controls.Removed.SubscribeForLifetime(ControlRemovedFromVisualTree, LifetimeManager);
     WindowResized.SubscribeForLifetime(HandleDebouncedResize, LifetimeManager);
 }
Beispiel #37
0
        public ConsoleApp(int x, int y, int w, int h)
        {
            Bitmap = new ConsoleBitmap(x,y, w, h);
            LayoutRoot = new ConsolePanel { Width = w, Height = h };
            LayoutRoot.Application = this;
            focusableControls = new List<ConsoleControl>();
            focusIndex = -1;
            ExitOnEscapeCharacter = true;
            LayoutRoot.Controls.Added += (c) =>
            {
                c.Application = this;
                if (c.CanFocus) focusableControls.Add(c);

                if (c is ConsolePanel)
                {
                    var children = TraverseControlTree(c as ConsolePanel);
                    focusableControls.AddRange(children.Where(child => child.CanFocus));
                    focusableControls = focusableControls.Distinct().ToList();
                    foreach (var child in children) child.Application = this;
                }
            };

            LayoutRoot.Controls.Removed += (c) =>
            {
                c.Application = null;
                if (c.CanFocus) focusableControls.Remove(c);

                if (c is ConsolePanel)
                {
                    var children = TraverseControlTree(c as ConsolePanel);
                    foreach (var child in children)
                    {
                        focusableControls.Remove(child);
                        child.Application = null;
                    }
                }
            };
        }
Beispiel #38
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         // free managed resources
         if (bitmap != null)
         {
             bitmap.Unlock();
             bitmap = null;
         }
     }
 }
Beispiel #39
0
 public PaintOnceContext(ConsoleBitmap bitmap)
 {
     this.bitmap = bitmap;
     bitmap.Lock();
 }
Beispiel #40
0
        protected override void OnPaint(ConsoleBitmap context)
        {
            for(int y = 0; y < lines.Count; y++)
            {
                if(y >= Height)
                {
                    break;
                }

                var line = lines[y];

                for(int x = 0; x < line.Count && x < Width; x++)
                {
                    context.Pen = HasFocus ? new ConsoleCharacter(line[x].Value, Application.Theme.FocusContrastColor, Application.Theme.FocusColor) : line[x];
                    context.DrawPoint(x, y);
                }
            }
        }
Beispiel #41
0
        /// <summary>
        /// paints the button
        /// </summary>
        /// <param name="context">drawing context</param>
        protected override void OnPaint(ConsoleBitmap context)
        {
            var drawState = new ConsoleString();

            drawState = "[".ToConsoleString(CanFocus ? Application.Theme.H1Color : Application.Theme.DisabledColor, Background = Background);
            if (Text != null)
            {
                ConsoleColor fg, bg;

                if(HasFocus)
                {
                    fg = Application.Theme.FocusContrastColor;
                    bg = Application.Theme.FocusColor;
                }
                else if(CanFocus)
                {
                    fg = Foreground;
                    bg = Background;
                }
                else
                {
                    fg = Application.Theme.DisabledColor;
                    bg = Background;
                }

                drawState += new ConsoleString(Text, fg, bg);

                if(Shortcut != null)
                {
                    if(Shortcut.Modifier.HasValue && Shortcut.Modifier == ConsoleModifiers.Alt)
                    {
                        drawState += new ConsoleString($" (ALT+{Shortcut.Key})", CanFocus ? Application.Theme.H1Color : Application.Theme.DisabledColor);
                    }
                    else if (Shortcut.Modifier.HasValue && Shortcut.Modifier == ConsoleModifiers.Shift)
                    {
                        drawState += new ConsoleString($" (SHIFT+{Shortcut.Key})", CanFocus ? Application.Theme.H1Color : Application.Theme.DisabledColor);
                    }
                    else if (Shortcut.Modifier.HasValue && Shortcut.Modifier == ConsoleModifiers.Control)
                    {
                        drawState += new ConsoleString($" (CTL+{Shortcut.Key})", CanFocus ? Application.Theme.H1Color : Application.Theme.DisabledColor);
                    }
                    else
                    {
                        drawState += new ConsoleString($" ({Shortcut.Key})", CanFocus ? Application.Theme.H1Color : Application.Theme.DisabledColor);
                    }
                }
            }

            drawState += "]".ToConsoleString(CanFocus ? Application.Theme.H1Color : Application.Theme.DisabledColor, Background);
            Width = drawState.Length;
            context.DrawString(drawState, 0, 0);
        }
 internal void PaintTo(ConsoleBitmap context)
 {
     OnPaint(context);
 }
 protected override void OnPaint(ConsoleBitmap context)
 {
     base.OnPaint(context);
 }
 protected override void OnPaint(ConsoleBitmap context)
 {
     base.OnPaint(context);
     if (HasFocus)
     {
         context.Pen = new ConsoleCharacter(' ', backgroundColor: Application.Theme.FocusColor);
         context.FillRect(0, 0, Width, Height);
     }
 }
 internal virtual void OnPaint(ConsoleBitmap context)
 {
 }
        internal void Paint(ConsoleBitmap context)
        {
            Rectangle scope = context.GetScope();

            if (Background != ConsoleControl.TransparantColor)
            {
                context.Pen = Background;
                context.FillRect(0, 0, Width, Height);
            }

            try
            {
                context.Rescope(this.X, this.Y, this.Width, this.Height);
                context.Pen = this.Foreground;
                OnPaint(context);
            }
            finally
            {
                context.Scope(scope);
            }
        }
        internal void Paint(ConsoleBitmap context)
        {
            if (IsVisible == false)
            {
                return;
            }

            if (TransparentBackground == false)
            {
                context.Pen = new ConsoleCharacter(' ', null, Background);
                context.FillRect(0, 0, Width, Height);
            }

            OnPaint(context);
        }
Beispiel #48
0
        private void PaintInternal(ConsoleBitmap context)
        {
            if (this.Height < 5)
            {
                context.DrawString("Grid can't render in a space this small", 0, 0);
                return;
            }

            if (VisibleColumns.Count == 0)
            {
                context.DrawString(NoVisibleColumnsMessage.ToConsoleString(Application.Theme.H1Color), 0, 0);
                return;
            }

            List<ConsoleString> headers = new List<ConsoleString>();
            List<List<ConsoleString>> rows = new List<List<ConsoleString>>();
            List<ColumnOverflowBehavior> overflowBehaviors = new List<ColumnOverflowBehavior>();

            if (VisibleColumns.Where(c => c.WidthPercentage != 0).Count() == 0)
            {
                foreach (var col in VisibleColumns)
                {
                    col.WidthPercentage = 1.0 / VisibleColumns.Count;
                }
            }

            foreach (var header in VisibleColumns)
            {
                headers.Add(header.ColumnDisplayName);
                var colWidth = (int)(header.WidthPercentage * this.Width);

                if (header.OverflowBehavior is SmartWrapOverflowBehavior)
                {
                    (header.OverflowBehavior as SmartWrapOverflowBehavior).MaxWidthBeforeWrapping = colWidth;
                }
                else if (header.OverflowBehavior is TruncateOverflowBehavior)
                {
                    (header.OverflowBehavior as TruncateOverflowBehavior).ColumnWidth = colWidth;
                }

                overflowBehaviors.Add(header.OverflowBehavior);
            }

            int viewIndex = visibleRowOffset;
            foreach (var item in DataView.Items)
            {
                List<ConsoleString> row = new List<ConsoleString>();
                int columnIndex = 0;
                foreach (var col in VisibleColumns)
                {
                    var value = PropertyResolver(item, col.ColumnName.ToString());
                    var displayValue = value == null ? "<null>".ToConsoleString() : (value is ConsoleString ? (ConsoleString)value : value.ToString().ToConsoleString());

                    if (viewIndex == SelectedIndex)
                    {
                        if (this.SelectionMode == GridSelectionMode.Row || (this.SelectionMode == GridSelectionMode.Cell && columnIndex == selectedColumnIndex))
                        {
                            displayValue = new ConsoleString(displayValue.ToString(), this.Background, HasFocus ? Application.Theme.FocusColor : Application.Theme.SelectedUnfocusedColor);
                        }
                    }

                    row.Add(displayValue);
                    columnIndex++;
                }
                viewIndex++;
                rows.Add(row);
            }

            ConsoleTableBuilder builder = new ConsoleTableBuilder();
            ConsoleString table;
            #if PROFILING
            using (new TimeProfiler("Grid.FormatAsTable"))
            {
            #endif
                table = builder.FormatAsTable(headers, rows, RowPrefix.ToString(), overflowBehaviors, Gutter);
            #if PROFILING
            }
            #endif

            if (FilterText != null)
            {
                table = table.Highlight(FilterText, Application.Theme.HighlightContrastColor, Application.Theme.HighlightColor, StringComparison.InvariantCultureIgnoreCase);
            }

            if (DataView.IsViewComplete == false)
            {
                table += "Loading more rows...".ToConsoleString(Application.Theme.H1Color);
            }
            else if (DataView.IsViewEndOfData && DataView.Items.Count == 0)
            {
                table += NoDataMessage.ToConsoleString(Application.Theme.H1Color);
            }
            else if (DataView.IsViewEndOfData)
            {
                table += EndOfDataMessage.ToConsoleString(Application.Theme.H1Color);
            }
            else
            {
                table += "more data below".ToConsoleString(Application.Theme.H1Color);
            }
            context.DrawString(table, 0, 0);

            if (FilteringEnabled)
            {

            }
        }
 /// <summary>
 /// You should override this method if you are building a custom control, from scratch, and need to control
 /// every detail of the painting process.  If possible, prefer to create your custom control by deriving from
 /// ConsolePanel, which will let you assemble a new control from others.
 /// </summary>
 /// <param name="context">The scoped bitmap that you can paint on</param>
 protected virtual void OnPaint(ConsoleBitmap context)
 {
 }
Beispiel #50
0
 protected override void OnPaint(ConsoleBitmap context)
 {
     context.Pen = new ConsoleCharacter(' ', null, myFocusStackDepth == Application.FocusManager.StackDepth ? Theme.DefaultTheme.H1Color : Theme.DefaultTheme.DisabledColor);
     context.DrawLine(0, 0, Width, 0);
     context.DrawLine(0, Height-1, Width, Height-1);
     base.OnPaint(context);
 }
 protected override void OnPaint(ConsoleBitmap context)
 {
     base.OnPaint(context);
     context.Pen = Value;
     context.DrawPoint(0,0);
 }
Beispiel #52
0
        /// <summary>
        /// paints the text box
        /// </summary>
        /// <param name="context"></param>
        protected override void OnPaint(ConsoleBitmap context)
        {
            var toPaint = textState.CurrentValue;
            var bgTransformed = new List<ConsoleCharacter>();

            foreach(var c in toPaint)
            {
                if(c.BackgroundColor == ConsoleString.DefaultBackgroundColor && Background != ConsoleString.DefaultBackgroundColor)
                {
                    bgTransformed.Add(new ConsoleCharacter(c.Value, Foreground, Background));
                }
                else
                {
                    bgTransformed.Add(c);
                }
            }

            context.DrawString(new ConsoleString(bgTransformed), 0, 0);

            if (blinkState)
            {
                char blinkChar = textState.CursorPosition >= toPaint.Length ? ' ' : toPaint[textState.CursorPosition].Value;
                context.Pen = new ConsoleCharacter(blinkChar, Application.Theme.FocusContrastColor, Application.Theme.FocusColor);
                context.DrawPoint(textState.CursorPosition, 0);
            }
        }