Esempio n. 1
0
        public Task Render(ICommandDto input, char[] buffer, Rectangle screen, CancellationToken cancellationToken)
        {
            var square = input as RectCommandDto;

            if (square != null)
            {
                // we consider that a new canvas will need to reset anything that was before
                for (int i = square.Y1; i <= square.Y2; i++)
                {
                    for (int j = square.X1; j <= square.X2; j++)
                    {
                        // index inside the buffer
                        var idx = i * screen.Width + j;

                        // protect against buffer overflow
                        if (idx >= buffer.Length)
                        {
                            break;
                        }

                        // is within the expected bounds
                        switch (square)
                        {
                        case var c when(j <= c.X2) && (i == c.Y1 || i == c.Y2):
                        case var x when(j == x.X1 || j == x.X2) && (i <= x.Y2): {
                            buffer[idx] = LINE;
                        }
                        break;
                        }
                    }
                }
            }

            return(Task.FromResult(0));
        }
Esempio n. 2
0
        public async Task Render(ICommandDto input, char[] buffer, Rectangle screen, CancellationToken cancellationToken)
        {
            var fill = input as FillCommandDto;

            if (fill != null)
            {
                int sWidth  = screen.Width;
                int sHeight = screen.Height;

                await CheckAndFill(fill.X, fill.Y, fill.Color, buffer, screen, cancellationToken).ConfigureAwait(false);
            }
        }
Esempio n. 3
0
        public Task Render(ICommandDto input, char[] buffer, Rectangle screen, CancellationToken cancellationToken)
        {
            var canvas = input as CanvasCommandDto;

            if (canvas != null)
            {
                int sWidth  = screen.Width;
                int sHeight = screen.Height;
                var cWidth  = canvas.Width + 1;
                var cHeigth = canvas.Height + 1;

                // we consider that a new canvas will need to reset anything that was before
                for (int y = 0; y < sHeight; y++)
                {
                    for (int x = 0; x < sWidth; x++)
                    {
                        // index inside the buffer
                        var idx = y * sWidth + x;

                        // protect against buffer overflow
                        if (idx >= buffer.Length)
                        {
                            break;
                        }

                        // reset prev value
                        buffer[idx] = char.MinValue;

                        // is within the expected bounds
                        if ((x <= cWidth) && (y <= cHeigth))
                        {
                            switch (canvas)
                            {
                            case var c when(x <= cWidth) && (y == 0 || y == cHeigth): {
                                buffer[idx] = LINE_HORIZONTAL;
                            }
                            break;

                            case var c when(x == 0 || x == cWidth) && (y <= cHeigth): {
                                buffer[idx] = LINE_VERTICAL;
                            }
                            break;
                            }
                        }
                    }
                }
            }

            return(Task.FromResult(0));
        }
Esempio n. 4
0
        public Task Render(ICommandDto input, char[] buffer, Rectangle screen, CancellationToken cancellationToken)
        {
            var line = input as LineCommandDto;

            if (line != null)
            {
                var mx = (line.X1 - line.X2);
                var my = (line.Y1 - line.Y2);

                // slope
                var m = mx == 0 || my == 0 ? 0 : my / mx;
                if (mx == 0)
                {
                    // is vertical
                    for (int y = line.Y1; y <= line.Y2; y++)
                    {
                        var x   = line.X1;
                        var idx = y * screen.Width + x;
                        buffer[idx] = LINE;
                    }
                }
                else if (my == 0)
                {
                    // is horizontal
                    for (int x = line.X1; x <= line.X2; x++)
                    {
                        var y   = line.Y1;
                        var idx = y * screen.Width + x;
                        buffer[idx] = LINE;
                    }
                }
                else
                {
                    // we have a slope
                    for (int x = line.X1; x <= line.X2; x++)
                    {
                        var y   = GetY(x, m, line.X1, line.Y1);
                        var idx = y * screen.Width + x;
                        buffer[idx] = LINE;
                    }
                }
            }

            return(Task.FromResult(0));
        }