示例#1
0
 /// <summary>
 /// Creates a new pixel control
 /// </summary>
 public PixelControl()
 {
     Width = 1;
     Height = 1;
     SubscribeForLifetime(nameof(Bounds), EnsureNoResize, this.LifetimeManager);
     Value = new ConsoleCharacter(' ', Foreground, Background);
 }
示例#2
0
 private void AdvanceFrame()
 {
     if (IsSpinning)
     {
         currentFrameIndex = currentFrameIndex == frames.Length - 1 ? 0 : currentFrameIndex + 1;
         var val = frames[currentFrameIndex];
         Value = new ConsoleCharacter(val, Foreground, Background);
     }
 }
示例#3
0
 /// <summary>
 /// Creates a new progress bar given a width
 /// </summary>
 /// <param name="initialMessage">an initial message to display in the progress bar</param>
 /// <param name="width">the width to use or null to use the default width which is one third of the console buffer width</param>
 public CliProgressBar(ConsoleString initialMessage = null, int? width = null)
 {
     Console = ConsoleProvider.Current;
     Message = initialMessage;
     Width = width.HasValue ? width.Value : Console.BufferWidth / 3;
     BorderPen = new ConsoleCharacter(' ', null, ConsoleColor.DarkGray);
     FillColor = ConsoleColor.Green;
     MessageFillColor = ConsoleColor.Black;
     indeterminateHighlightIndex = -1;
 }
示例#4
0
        public ConsoleBitmap DrawPointUnsafe(ConsoleCharacter character, int x, int y)
        {
            var oldPen = this.Pen;

            try
            {
                this.Pen = character;
                DrawPointUnsafe(x, y);
            }
            finally
            {
                this.Pen = oldPen;
            }

            return(this);
        }
示例#5
0
        /// <summary>
        /// Draws a filled rectangle at the given coordinates using the specified character
        /// as a temporary pen
        /// </summary>
        /// <param name="character">the temporary character</param>
        /// <param name="x">the left of the rectangle</param>
        /// <param name="y">the top of the rectangle</param>
        /// <param name="w">the width of the rectangle</param>
        /// <param name="h">the height of the rectangle</param>
        /// <returns>this ConsoleBitmap</returns>
        public ConsoleBitmap FillRect(ConsoleCharacter character, int x = 0, int y = 0, int w = -1, int h = -1)
        {
            var oldPen = this.Pen;

            try
            {
                w        = w < 0 ? Width : w;
                h        = h < 0 ? Height : h;
                this.Pen = character;
                FillRect(x, y, w, h);
            }
            finally
            {
                this.Pen = oldPen;
            }
            return(this);
        }
示例#6
0
    private bool IsRestOfLineWhitespaceWithDefaultBackground(int xStart, int y)
    {
        var defaultBg = new ConsoleCharacter(' ').BackgroundColor;

        for (var x = xStart; x < this.Width; x++)
        {
            if (char.IsWhiteSpace(this.GetPixel(x, y).Value) && this.GetPixel(x, y).BackgroundColor == defaultBg)
            {
                // this is whitespace
            }
            else
            {
                return(false);
            }
        }

        return(true);
    }
示例#7
0
 /// <summary>
 /// Creates a new ConsoleBitmap
 /// </summary>
 /// <param name="bounds">the area of the image</param>
 public ConsoleBitmap(Size bounds)
 {
     this.Width           = bounds.Width;
     this.Height          = bounds.Height;
     this.Console         = ConsoleProvider.Current;
     this.lastBufferWidth = this.Console.BufferWidth;
     Pixels          = new ConsoleCharacter[this.Width][];
     lastDrawnPixels = new ConsoleCharacter[this.Width][];
     for (int x = 0; x < this.Width; x++)
     {
         Pixels[x]          = new ConsoleCharacter[this.Height];
         lastDrawnPixels[x] = new ConsoleCharacter[this.Height];
         for (int y = 0; y < Pixels[x].Length; y++)
         {
             Pixels[x][y]          = new ConsoleCharacter(' ');
             lastDrawnPixels[x][y] = new ConsoleCharacter(' ');
         }
     }
 }
示例#8
0
        private void DrawPixel(int x, int y, ConsolePixel pixel, ConsoleCharacter value)
        {
            x = Left + x;
            y = Top + y;

            if (x >= lastBufferWidth)
            {
                return;
            }
            try
            {
                if (Console.CursorLeft != x)
                {
                    Console.CursorLeft = x;
                }

                if (Console.CursorTop != y)
                {
                    Console.CursorTop = y;
                }

                if (Console.ForegroundColor != value.ForegroundColor)
                {
                    Console.ForegroundColor = value.ForegroundColor;
                }

                if (Console.BackgroundColor != value.BackgroundColor)
                {
                    Console.BackgroundColor = value.BackgroundColor;
                }

                Console.Write(value.Value);
            }catch (ArgumentOutOfRangeException)
            {
            }

            if (pixel != null)
            {
                pixel.Sync();
            }
        }
示例#9
0
        // Bresenhams Triangle Algorithm

        /// <summary> Draws a Triangle and fills it. </summary>
        /// <param name="a">Point A.</param>
        /// <param name="b">Point B.</param>
        /// <param name="c">Point C.</param>
        /// <param name="col">Color to draw with.</param>
        /// <param name="character">Character to use.</param>
        public void FillTriangle(Point a, Point b, Point c, int col, ConsoleCharacter character = ConsoleCharacter.Full)
        {
            Point min = new Point(Math.Min(Math.Min(a.X, b.X), c.X), Math.Min(Math.Min(a.Y, b.Y), c.Y));
            Point max = new Point(Math.Max(Math.Max(a.X, b.X), c.X), Math.Max(Math.Max(a.Y, b.Y), c.Y));

            Point p = new Point();

            for (p.Y = min.Y; p.Y < max.Y; p.Y++)
            {
                for (p.X = min.X; p.X < max.X; p.X++)
                {
                    int w0 = Orient(b, c, p);
                    int w1 = Orient(c, a, p);
                    int w2 = Orient(a, b, p);

                    if (w0 >= 0 && w1 >= 0 && w2 >= 0)
                    {
                        SetPixel(p, col, character);
                    }
                }
            }
        }
示例#10
0
        public void TestDrawRect()
        {
            var bitmap = new ConsoleBitmap(80, 30);
            var app    = new CliTestHarness(TestContext, bitmap.Width, bitmap.Height, true);

            app.InvokeNextCycle(async() =>
            {
                app.LayoutRoot.Add(new BitmapControl()
                {
                    Bitmap = bitmap
                }).Fill();
                var pen = new ConsoleCharacter('X', ConsoleColor.Green);
                for (var i = 0; i < 500000; i++)
                {
                    bitmap.DrawRect(pen, 0, 0, bitmap.Width, bitmap.Height);
                }
                await app.PaintAndRecordKeyFrameAsync();
                app.Stop();
            });

            app.Start().Wait();
            app.AssertThisTestMatchesLKG();
        }
示例#11
0
        public ConsoleBitmap(Rectangle bounds, ConsoleCharacter? bg = null)
        {
            _syncLock = new object();
            this.Top = bounds.Y;
            this.Left = bounds.X;

            bounds = new Rectangle(0, 0, bounds.Width, bounds.Height);

            this.Bounds = bounds;
            this.scope = bounds;
            this.Console = ConsoleProvider.Current;
            this.Background = bg.HasValue ? bg.Value : new ConsoleCharacter(' ');
            this.Pen = new ConsoleCharacter('*');
            pixels = new ConsolePixel[this.Width][];
            for (int x = 0; x < this.Width; x++)
            {
                pixels[x] = new ConsolePixel[this.Height];
                for (int y = 0; y < pixels[x].Length; y++)
                {
                    pixels[x][y] = new ConsolePixel() { Value = bg };
                }
            }
        }
示例#12
0
        private void ComposeBlendBackground(ConsoleControl control)
        {
            var minX = Math.Max(control.X, 0);
            var minY = Math.Max(control.Y, 0);
            var maxX = Math.Min(Width, control.X + control.Width);
            var maxY = Math.Min(Height, control.Y + control.Height);

            for (var x = minX; x < maxX; x++)
            {
                for (var y = minY; y < maxY; y++)
                {
                    var controlPixel = control.Bitmap.GetPixel(x - control.X, y - control.Y).Value;

                    if (controlPixel?.BackgroundColor != ConsoleString.DefaultBackgroundColor)
                    {
                        Bitmap.Pen = controlPixel.Value;
                        Bitmap.DrawPointUnsafe(x, y);
                    }
                    else
                    {
                        var myPixel = Bitmap.GetPixel(x, y).Value;
                        if (myPixel.HasValue && myPixel.Value.BackgroundColor != ConsoleString.DefaultBackgroundColor)
                        {
                            var composedValue = new ConsoleCharacter(controlPixel.Value.Value, controlPixel.Value.ForegroundColor, myPixel.Value.BackgroundColor);
                            Bitmap.Pen = composedValue;
                            Bitmap.DrawPointUnsafe(x, y);
                        }
                        else
                        {
                            Bitmap.Pen = controlPixel.Value;
                            Bitmap.DrawPointUnsafe(x, y);
                        }
                    }
                }
            }
        }
示例#13
0
        private void SetupDropKeyInput()
        {
            BrokerToScene(ConsoleKey.W, () => { UndoStack.Do(new DropWallAction()
                {
                    Context = this
                }); });
            BrokerToScene(ConsoleKey.C, () => { UndoStack.Do(new DropAutoCeilingAction()
                {
                    Context = this
                }); });
            BrokerToScene(ConsoleKey.T, () => { UndoStack.Do(new DropTurretAction()
                {
                    Context = this
                }); });

            BrokerToScene(ConsoleKey.M, () => { UndoStack.Do(new DropMainCharacterAction()
                {
                    Context = this
                }); });

            BrokerToScene(ConsoleKey.Z, () => { UndoStack.Do(new DropZombieAction()
                {
                    Context = this
                }); });
            PushAppHandler(ConsoleKey.A, () => { UndoStack.Do(new DropAmmoAction()
                {
                    Context = this
                }); });
            PushAppHandler(ConsoleKey.P, () => { UndoStack.Do(new DropPortalAction()
                {
                    Context = this
                }); });
            BrokerToScene(ConsoleKey.D, () =>
            {
                if (PositionDoorAction.IsReadyForDrop(this) == false)
                {
                    UndoStack.Do(new PositionDoorAction(false)
                    {
                        Context = this
                    });
                }
                else
                {
                    UndoStack.Do(new DropDoorAction()
                    {
                        Context = this
                    });
                }
            });

            BrokerToScene(ConsoleKey.D, () =>
            {
                if (PositionDoorAction.IsReadyForDrop(this) == false)
                {
                    UndoStack.Do(new PositionDoorAction(true)
                    {
                        Context = this
                    });
                }
                else
                {
                    UndoStack.Do(new DropDoorAction()
                    {
                        Context = this
                    });
                }
            }, ConsoleModifiers.Shift);


            BrokerToScene(ConsoleKey.Delete, () => { UndoStack.Do(new DeleteAction()
                {
                    Context = this
                }); });

            BrokerToScene(ConsoleKey.U, () => { UndoStack.Undo(); });
            BrokerToScene(ConsoleKey.R, () => { UndoStack.Redo(); });

            PushAppHandler(ConsoleKey.T, () =>
            {
                Dialog.PickFromEnum <ConsoleColor>("Choose a background color".ToConsoleString())
                .Then((val) =>
                {
                    if (val.HasValue == false)
                    {
                        return;
                    }

                    this.WallPen = new ConsoleCharacter(this.WallPen.Value, this.WallPen.ForegroundColor, val.Value);
                }).Then((val) =>
                {
                    Dialog.ShowTextInput("Pick a character".ToConsoleString(), (result) =>
                    {
                        this.WallPen = new ConsoleCharacter(result.Length == 0 ? ' ' : result[0].Value, WallPen.ForegroundColor, WallPen.BackgroundColor);

                        Dialog.ShowTextInput("Pick HP".ToConsoleString(), (hpResult) =>
                        {
                            float hpVal;
                            if (float.TryParse(hpResult.ToString(), out hpVal) == false)
                            {
                                hpVal = 10;
                            }
                            this.WallPenHP = hpVal;
                        });
                    });
                });
            });
        }
示例#14
0
 private void Compose(int x, int y, ConsoleCharacter pen)
 {
     pixels[x][y].Value = pen;
 }
 /// <summary>
 /// Writes the given character to the console
 /// </summary>
 /// <param name="consoleCharacter">the character to write</param>
 public void Write(ConsoleCharacter consoleCharacter)
 {
     var existing = ConsoleString.ConsoleProvider;
     try
     {
         ConsoleString.ConsoleProvider = this;
         consoleCharacter.Write();
     }
     finally
     {
         ConsoleString.ConsoleProvider = existing;
     }
 }
示例#16
0
 public void Write(ConsoleCharacter consoleCharacter)
 {
     throw new NotImplementedException();
 }
示例#17
0
 public AimLineSegment(int z, ConsoleCharacter pen)
 {
     this.Pen = pen;
     this.ResizeTo(1, 1);
     this.MoveTo(0, 0, z);
 }
示例#18
0
            public void Write(ConsoleCharacter consoleCharacter)
            {
                buffer.Pen = consoleCharacter;
                buffer.DrawPoint(CursorLeft, CursorTop);

                if (CursorLeft == BufferWidth - 1)
                {
                    CursorLeft = 0;
                    CursorTop++;
                }
                else
                {
                    CursorLeft++;
                }
            }
示例#19
0
 /// <summary> Draws a Triangle. </summary>
 /// <param name="a">Point A.</param>
 /// <param name="b">Point B.</param>
 /// <param name="c">Point C.</param>
 /// <param name="col">Color to draw with.</param>
 /// <param name="character">Character to use.</param>
 public void Triangle(Point a, Point b, Point c, int col, ConsoleCharacter character = ConsoleCharacter.Full)
 {
     Line(a, b, col, character);
     Line(b, c, col, character);
     Line(c, a, col, character);
 }
示例#20
0
 public ConsoleBitmap(int x, int y, int w, int h, ConsoleCharacter? bg = null)
     : this(new Rectangle(x,y,w,h), bg)
 {
 }
 public void Write(ConsoleCharacter consoleCharacter)
 {
     Write(consoleCharacter.ToString());
 }
示例#22
0
        /// <summary> Draws a filled Arc. </summary>
        /// <param name="pos">Center of Arc.</param>
        /// <param name="radius">Radius of Arc.</param>
        /// <param name="start">Start angle in degrees.</param>
        /// <param name="arc">End angle in degrees.</param>
        /// <param name="fgColor">Specified color index.</param>
        /// <param name="bgColor">Specified background color index.</param>
        /// <param name="c">Character to use.</param>
        public void SemiCircle(Point pos, int radius, int start, int arc, int fgColor, int bgColor, ConsoleCharacter c = ConsoleCharacter.Full)
        {
            for (int a = start; a > -arc + start; a--)
            {
                for (int r = 0; r < radius + 1; r++)
                {
                    int x = (int)(r * Math.Cos((float)a / 57.29577f));
                    int y = (int)(r * Math.Sin((float)a / 57.29577f));

                    Point v = new Point(pos.X + x, pos.Y + y);
                    SetPixel(v, fgColor, bgColor, c);
                }
            }
        }
示例#23
0
 /// <summary> Draws a filled Arc, calls new method with Background as the bgColor </summary>
 /// <param name="pos">Center of Arc.</param>
 /// <param name="radius">Radius of Arc.</param>
 /// <param name="start">Start angle in degrees.</param>
 /// <param name="arc">End angle in degrees.</param>
 /// <param name="color">Specified color index.</param>
 /// <param name="c">Character to use.</param>
 public void SemiCircle(Point pos, int radius, int start, int arc, int color, ConsoleCharacter c = ConsoleCharacter.Full)
 {
     SemiCircle(pos, radius, start, arc, color, Background, c);
 }
示例#24
0
        /// <summary> Draws an Arc. </summary>
        /// <param name="pos">Center of Arc.</param>
        /// <param name="radius">Radius of Arc.</param>
        /// <param name="fgColor">Specified color index.</param>
        /// <param name="bgColor">Specified background color index.</param>
        /// <param name="arc">angle in degrees, 360 if not specified.</param>
        /// <param name="c">Character to use.</param>
        public void Arc(Point pos, int radius, int fgColor, int bgColor, int arc = 360, ConsoleCharacter c = ConsoleCharacter.Full)
        {
            for (int a = 0; a < arc; a++)
            {
                int x = (int)(radius * Math.Cos((float)a / 57.29577f));
                int y = (int)(radius * Math.Sin((float)a / 57.29577f));

                Point v = new Point(pos.X + x, pos.Y + y);
                SetPixel(v, fgColor, bgColor, ConsoleCharacter.Full);
            }
        }
示例#25
0
 /// <summary> Draws an Arc, calls new method with Background as the bgColor. </summary>
 /// <param name="pos">Center of Arc.</param>
 /// <param name="radius">Radius of Arc.</param>
 /// <param name="color">Specified color index.</param>
 /// <param name="arc">angle in degrees, 360 if not specified.</param>
 /// <param name="c">Character to use.</param>
 public void Arc(Point pos, int radius, int color, int arc = 360, ConsoleCharacter c = ConsoleCharacter.Full)
 {
     Arc(pos, radius, color, Background, arc, c);
 }
示例#26
0
 /// <summary> Overloaded Method Draws a single pixel to the screenbuffer with custom bgColor. </summary>
 /// <param name="v">The Point that should be drawn to.</param>
 /// <param name="fgColor">The foreground color index.</param>
 /// <param name="bgColor">The background color index.</param>
 /// <param name="c">The character that should be drawn with.</param>
 public void SetPixel(Point v, int fgColor, int bgColor, ConsoleCharacter c = ConsoleCharacter.Full)
 {
     SetPixel(v, fgColor, bgColor, (char)c);
 }
示例#27
0
 /// <summary> Draws a single pixel to the screenbuffer. </summary>
 /// <param name="v">The Point that should be drawn to.</param>
 /// <param name="color">The color index.</param>
 /// <param name="c">The character that should be drawn with.</param>
 public void SetPixel(Point v, int color, ConsoleCharacter c = ConsoleCharacter.Full)
 {
     SetPixel(v, color, (char)c);
 }
示例#28
0
        // Bresenhams Line Algorithm
        // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
        /// <summary> Draws a line from start to end. (Bresenhams Line) </summary>
        /// <param name="start">Point to draw line from.</param>
        /// <param name="end">Point to end line at.</param>
        /// <param name="col">Color to draw with.</param>
        /// <param name="c">Character to use.</param>
        public void Line(Point start, Point end, int col, ConsoleCharacter c = ConsoleCharacter.Full)
        {
            Point delta = end - start;
            Point da = Point.Zero, db = Point.Zero;

            if (delta.X < 0)
            {
                da.X = -1;
            }
            else if (delta.X > 0)
            {
                da.X = 1;
            }
            if (delta.Y < 0)
            {
                da.Y = -1;
            }
            else if (delta.Y > 0)
            {
                da.Y = 1;
            }
            if (delta.X < 0)
            {
                db.X = -1;
            }
            else if (delta.X > 0)
            {
                db.X = 1;
            }
            int longest  = Math.Abs(delta.X);
            int shortest = Math.Abs(delta.Y);

            if (!(longest > shortest))
            {
                longest  = Math.Abs(delta.Y);
                shortest = Math.Abs(delta.X);
                if (delta.Y < 0)
                {
                    db.Y = -1;
                }
                else if (delta.Y > 0)
                {
                    db.Y = 1;
                }
                db.X = 0;
            }

            int   numerator = longest >> 1;
            Point p         = new Point(start.X, start.Y);

            for (int i = 0; i <= longest; i++)
            {
                SetPixel(p, col, c);
                numerator += shortest;
                if (!(numerator < longest))
                {
                    numerator -= longest;
                    p         += da;
                }
                else
                {
                    p += db;
                }
            }
        }
示例#29
0
        private void DrawPixel(int x, int y, ConsolePixel pixel, ConsoleCharacter value)
        {
            x = Left + x;
            y = Top + y;

            if (Console.CursorLeft != x)
            {
                Console.CursorLeft = x;
            }

            if (Console.CursorTop != y)
            {
                Console.CursorTop = y;
            }

            if (Console.ForegroundColor != value.ForegroundColor)
            {
                Console.ForegroundColor = value.ForegroundColor;
            }

            if (Console.BackgroundColor != value.BackgroundColor)
            {
                Console.BackgroundColor = value.BackgroundColor;
            }

            Console.Write(value.Value);

            if(pixel != null)
            {
                pixel.Sync();
            }
        }
示例#30
0
        /// <summary>
        /// Display the ASCII art file if it exists
        /// </summary>
        /// <param name="file">file with the ascii art</param>
        /// <param name="color">ConsoleColor</param>
        /// <param name="animation">animate the art</param>
        /// <returns>Task</returns>
        public static async Task DisplayAsciiArt(string file, ConsoleColor color, Animation animation = Animation.None)
        {
            try
            {
                if (File.Exists(file))
                {
                    string txt = File.ReadAllText(file);

                    if (animation == Animation.None)
                    {
                        DisplayArt(txt, color);
                        Console.ResetColor();

                        return;
                    }

                    string[] lines = File.ReadAllLines(file);

                    Console.CursorVisible = false;

                    // scroll the window
                    for (int i = 0; i < Console.WindowHeight; i++)
                    {
                        Console.WriteLine();
                    }

                    // determine top of screen
                    int top = Console.CursorTop - Console.WindowHeight;
                    int row = top + Console.WindowHeight - lines.Length - 5;

                    int    key = 0;
                    Random rnd = new Random(DateTime.Now.Millisecond);

                    SortedList <int, ConsoleCharacter> lrandom = new SortedList <int, ConsoleCharacter>();
                    List <ConsoleCharacter>            list    = new List <ConsoleCharacter>();

                    // create the random list
                    for (int r = 0; r < lines.Length; r++)
                    {
                        string line = lines[r];

                        for (int c = 0; c < line.Length; c++)
                        {
                            if (!char.IsWhiteSpace(line[c]))
                            {
                                while (key < 1 || lrandom.ContainsKey(key))
                                {
                                    key = rnd.Next(1, int.MaxValue);
                                }

                                ConsoleCharacter l = new ConsoleCharacter
                                {
                                    Color = color,
                                    Row   = top + r,
                                    Col   = c,
                                    Value = line[c],
                                };

                                // add to random list and in-order list
                                list.Add(l);
                                lrandom.Add(key, l);
                            }
                        }
                    }

                    switch (animation)
                    {
                    case Animation.None:
                        break;

                    case Animation.Dissolve:
                        await Dissolve(lrandom).ConfigureAwait(false);

                        break;

                    case Animation.Fade:
                        await Fade(txt, lines, top).ConfigureAwait(false);

                        break;

                    case Animation.Scroll:
                        await Scroll(txt, color, lines, top).ConfigureAwait(false);

                        break;

                    case Animation.TwoColor:
                        await TwoColor(list).ConfigureAwait(false);

                        break;

                    default:
                        break;
                    }

                    Console.SetCursorPosition(0, top + lines.Length + 1);
                    Console.CursorVisible = true;
                    Console.ResetColor();
                }
            }
            catch
            {
                // ignore any errors
            }
        }
示例#31
0
 /// <summary> Draws a Triangle. </summary>
 /// <param name="a">Point A.</param>
 /// <param name="b">Point B.</param>
 /// <param name="c">Point C.</param>
 /// <param name="fgColor">Color to draw with.</param>
 /// <param name="bgColor">Color to draw to the background with.</param>
 /// <param name="character">Character to use.</param>
 public void Triangle(Point a, Point b, Point c, int fgColor, int bgColor, ConsoleCharacter character = ConsoleCharacter.Full)
 {
     Line(a, b, fgColor, bgColor, character);
     Line(b, c, fgColor, bgColor, character);
     Line(c, a, fgColor, bgColor, character);
 }
示例#32
0
 public Triangle(object n)
 {
     p     = new Vector3[3];
     color = 0;
     c     = ConsoleCharacter.Null;
 }
示例#33
0
        // Bresenhams Triangle Algorithm

        /// <summary> Draws a Triangle and fills it, calls overloaded method with background as bgColor </summary>
        /// <param name="a">Point A.</param>
        /// <param name="b">Point B.</param>
        /// <param name="c">Point C.</param>
        /// <param name="color">Color to draw with.</param>
        /// <param name="character">Character to use.</param>
        public void FillTriangle(Point a, Point b, Point c, int color, ConsoleCharacter character = ConsoleCharacter.Full)
        {
            FillTriangle(a, b, c, color, Background, character);
        }
示例#34
0
        private void WriteCharacterForPressedKey(ConsoleKeyInfo key)
        {
            var c = new ConsoleCharacter(key.KeyChar);
            if (CursorPosition == Context.Buffer.Count)
            {
                Context.Buffer.Add(c);
            }
            else
            {
                Context.Buffer.Insert(CursorPosition, c);
            }

            CursorPosition++;
        }
 /// <summary>
 /// Takes the value of KeyPressed.KeyChar and writes it to the console in the current buffer position.
 /// </summary>
 public void WriteCharacterForPressedKey()
 {
     var c = new ConsoleCharacter(KeyPressed.KeyChar);
     if (BufferPosition == Buffer.Count)
     {
         Buffer.Add(c);
         this.Console.Write(c);
     }
     else
     {
         Buffer.Insert(BufferPosition, c);
         RefreshConsole(1, 0);
     }
 }
示例#36
0
 // Bresenhams Line Algorithm
 // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
 /// <summary> Draws a line from start to end. (Bresenhams Line), calls overloaded method with background as bgColor </summary>
 /// <param name="start">Point to draw line from.</param>
 /// <param name="end">Point to end line at.</param>
 /// <param name="color">Color to draw with.</param>
 /// <param name="c">Character to use.</param>
 public void Line(Point start, Point end, int color, ConsoleCharacter c = ConsoleCharacter.Full)
 {
     Line(start, end, color, Background, c);
 }
示例#37
0
        public override void Update()
        {
            if (Engine.GetKeyDown(ConsoleKey.Y))
            {
                drawWireframe = !drawWireframe;
            }
            if (Engine.GetKeyDown(ConsoleKey.H))
            {
                drawSolid = !drawSolid;
            }

            if (Engine.GetMouseLeft())
            {
                Point delta = Engine.GetMousePos() - lastMousePos;
                yRot += delta.X / 80f;
                xRot += delta.Y / 80f;
            }

            lastMousePos = Engine.GetMousePos();

            // matriserar
            Matrix transformMat, rotationMat;

            rotationMat  = Matrix.RotationMatrixY(yRot);
            rotationMat *= Matrix.RotationMatrixX(xRot);
            transformMat = Matrix.Translation(modelPosition);

            // generera trianglar
            for (int i = 0; i < mesh.Triangles.Length; i++)
            {
                Triangle vertex  = mesh.Triangles[i];
                Triangle rotated = vertex.MatMul(rotationMat);

                Triangle transformed = rotated.MatMul(transformMat);

                // beräknar kryssprodukt av line1 och line2 för att hitta normal.
                Vec3D normal, line1, line2;
                line1 = transformed.p[1] - transformed.p[0];
                line2 = transformed.p[2] - transformed.p[0];

                normal = Vec3D.Cross(line1, line2);
                normal.Normalize();

                // testar ifall vi kan se ytan
                if (Vec3D.Dot(normal, transformed.p[0] - camera) < 0.0f)
                {
                    // beräknar ljus
                    float            l         = Vec3D.Dot(lightDirection, normal);
                    ConsoleCharacter character = ConsoleCharacter.Light;
                    if (l > 0.4)
                    {
                        character = ConsoleCharacter.Medium;
                    }
                    if (l > 0.7)
                    {
                        character = ConsoleCharacter.Dark;
                    }
                    if (l > 1)
                    {
                        character = ConsoleCharacter.Full;
                    }

                    // projekterar från 3D -> 2D
                    Triangle projected = new Triangle(null);
                    projected = transformed.MatMul(projectionMatrix);

                    // transformerar och skalar projektionen
                    Vec3D offsetView = new Vec3D(1, 1, 0);
                    projected.p[0] += offsetView;
                    projected.p[1] += offsetView;
                    projected.p[2] += offsetView;

                    projected.p[0].x *= 0.5f * consoleWidth; projected.p[0].y *= 0.5f * consoleHeight;
                    projected.p[1].x *= 0.5f * consoleWidth; projected.p[1].y *= 0.5f * consoleHeight;
                    projected.p[2].x *= 0.5f * consoleWidth; projected.p[2].y *= 0.5f * consoleHeight;

                    projected.c = character;
                    trianglesToRaster.Add(projected);
                }
            }

            // sortera
            trianglesToRaster.Sort((t1, t2) => ((t2.p[0].z + t2.p[1].z + t2.p[2].z).CompareTo((t1.p[0].z + t1.p[1].z + t1.p[2].z))));
        }
示例#38
0
        public void TestRecordVideoBasic()
        {
            ConsoleBitmap bitmap = new ConsoleBitmap(4, 2), redBitmap = null, greenBitmap = null, magentaPixelBitmap = null;

            using (var sharedStream = new MemoryStream())
            {
                using (var bitmapVideoWriter = new ConsoleBitmapStreamWriter(sharedStream)
                {
                    CloseInnerStream = false
                })
                {
                    bitmap             = new ConsoleBitmap(4, 2);
                    redBitmap          = bitmapVideoWriter.WriteFrame(bitmap.FillRect(ConsoleCharacter.RedBG())).Clone();
                    greenBitmap        = bitmapVideoWriter.WriteFrame(bitmap.FillRect(ConsoleCharacter.GreenBG())).Clone();
                    magentaPixelBitmap = bitmapVideoWriter.WriteFrame(bitmap.DrawPoint(ConsoleCharacter.MagentaBG(), 0, 0)).Clone();
                }

                sharedStream.Position = 0; // rewind the stream to the beginning to read it back

                // create a reader and make sure we can read each frame back exactly as they were written
                var bitmapVideoReader = new ConsoleBitmapStreamReader(sharedStream);
                Assert.AreEqual(redBitmap, bitmapVideoReader.ReadFrame().CurrentBitmap);
                Assert.AreEqual(greenBitmap, bitmapVideoReader.ReadFrame().CurrentBitmap);
                Assert.AreEqual(magentaPixelBitmap, bitmapVideoReader.ReadFrame().CurrentBitmap);
                Assert.IsNull(bitmapVideoReader.ReadFrame().CurrentFrame);
            }
        }
示例#39
0
 /// <summary> Draws a Rectangle, calls overloaded method with background as bgColor  </summary>
 /// <param name="pos">Top Left corner of rectangle.</param>
 /// <param name="end">Bottom Right corner of rectangle.</param>
 /// <param name="color">Color to draw with.</param>
 /// <param name="c">Character to use.</param>
 public void Rectangle(Point pos, Point end, int color, ConsoleCharacter c = ConsoleCharacter.Full)
 {
     Rectangle(pos, end, color, Background, c);
 }
示例#40
0
 /// <summary> Draws a Rectangle and fills it, calls overloaded method with background as bgColor </summary>
 /// <param name="a">Top Left corner of rectangle.</param>
 /// <param name="b">Bottom Right corner of rectangle.</param>
 /// <param name="color">Color to draw with.</param>
 /// <param name="c">Character to use.</param>
 public void Fill(Point a, Point b, int color, ConsoleCharacter c = ConsoleCharacter.Full)
 {
     Fill(a, b, color, Background, c);
 }
示例#41
0
 /// <summary> Draws a grid, calls overloaded method with background as bgColor </summary>
 /// <param name="a">Top Left corner of grid.</param>
 /// <param name="b">Bottom Right corner of grid.</param>
 /// <param name="spacing">the spacing until next line</param>
 /// <param name="color">Color to draw with.</param>
 /// <param name="c">Character to use.</param>
 public void Grid(Point a, Point b, int spacing, int color, ConsoleCharacter c = ConsoleCharacter.Full)
 {
     Grid(a, b, spacing, color, Background, c);
 }
 public void Write(ConsoleCharacter consoleCharacter)
 {
     Write(consoleCharacter.ToString());
 }
示例#43
0
        private void DrawPixel(int x, int y, ConsolePixel pixel, ConsoleCharacter value)
        {
            x = Left + x;
            y = Top + y;

            if(x >= lastBufferWidth)
            {
                return;
            }
            try
            {
                if (Console.CursorLeft != x)
                {
                    Console.CursorLeft = x;
                }

                if (Console.CursorTop != y)
                {
                    Console.CursorTop = y;
                }

                if (Console.ForegroundColor != value.ForegroundColor)
                {
                    Console.ForegroundColor = value.ForegroundColor;
                }

                if (Console.BackgroundColor != value.BackgroundColor)
                {
                    Console.BackgroundColor = value.BackgroundColor;
                }

                Console.Write(value.Value);
            }catch(ArgumentOutOfRangeException)
            {

            }

            if(pixel != null)
            {
                pixel.Sync();
            }
        }