Example #1
0
        public void Render(ICanvas canvas, int width, int height)
        {
            canvas.Save();

            canvas.Translate(100, height - _pixelMapper.Rows - 100);

            if (_terrainMapRenderer.TryGetTerrainImage(out IImage? terrainImage))
            {
                canvas.DrawImage(terrainImage, 0, 0);
            }
            else
            {
                canvas.Clear(TerrainColourLookup.DefaultColour);
            }

            foreach (Track track in _trackLayout)
            {
                canvas.DrawRect(track.Column, track.Row, 1, 1, _paint);
            }

            (int left, int top)     = _pixelMapper.ViewPortPixelsToCoords(0, 0);
            (int right, int bottom) = _pixelMapper.ViewPortPixelsToCoords(_pixelMapper.ViewPortWidth, _pixelMapper.ViewPortHeight);

            canvas.DrawRect(left, top, right - left, bottom - top, _viewPortPaint);

            canvas.DrawRect(0, 0, _pixelMapper.Columns, _pixelMapper.Rows, _border);

            canvas.Restore();
        }
        private static (Rectangle Source, Rectangle Destination) GetSourceAndDestinationRectangles(IPixelMapper pixelMapper)
        {
            (int topLeftColumn, int topLeftRow)         = pixelMapper.ViewPortPixelsToCoords(0, 0);
            (int bottomRightColumn, int bottomRightRow) = pixelMapper.ViewPortPixelsToCoords(pixelMapper.ViewPortWidth, pixelMapper.ViewPortHeight);

            bottomRightColumn += 1;
            bottomRightRow    += 1;

            Rectangle source = new(topLeftColumn, topLeftRow, bottomRightColumn, bottomRightRow);

            (int destinationTopLeftX, int destinationTopLeftY, _)         = pixelMapper.CoordsToViewPortPixels(topLeftColumn, topLeftRow);
            (int destinationBottomRightX, int destinationBottomRightY, _) = pixelMapper.CoordsToViewPortPixels(bottomRightColumn, bottomRightRow);

            Rectangle destination = new(destinationTopLeftX, destinationTopLeftY, destinationBottomRightX, destinationBottomRightY);

            return(source, destination);
        }
        public void Render(ICanvas canvas, int width, int height, IPixelMapper pixelMapper)
        {
            (int topLeftColumn, int topLeftRow)         = pixelMapper.ViewPortPixelsToCoords(0, 0);
            (int bottomRightColumn, int bottomRightRow) = pixelMapper.ViewPortPixelsToCoords(pixelMapper.ViewPortWidth, pixelMapper.ViewPortHeight);

            bottomRightColumn += 1;
            bottomRightRow    += 1;

            var source = new Rectangle(topLeftColumn, topLeftRow, bottomRightColumn, bottomRightRow);

            (int destinationTopLeftX, int destinationTopLeftY, _)         = pixelMapper.CoordsToViewPortPixels(topLeftColumn, topLeftRow);
            (int destinationBottomRightX, int destinationBottomRightY, _) = pixelMapper.CoordsToViewPortPixels(bottomRightColumn, bottomRightRow);

            var destination = new Rectangle(destinationTopLeftX, destinationTopLeftY, destinationBottomRightX, destinationBottomRightY);

            canvas.DrawImage(_terrainMapRenderer.GetTerrainImage(), source, destination);
        }
Example #4
0
        public void ViewPortPixelsToCoords_DefaultScale_DefaultPosition(int x, int y, int expectedCol, int expectedRow)
        {
            (int actualCol, int actualRow) = _pixelMapper.ViewPortPixelsToCoords(x, y);

            Assert.Equal(expectedCol, actualCol);
            Assert.Equal(expectedRow, actualRow);
        }
Example #5
0
        public void Render(ICanvas canvas, int width, int height)
        {
            if (!this.Enabled)
            {
                return;
            }

            canvas.Translate(width - _pixelMapper.Columns - 50, height - _pixelMapper.Rows - 50);

            canvas.DrawImage(_terrainMapRenderer.GetTerrainImage(), 0, 0);

            foreach (Track track in _trackLayout)
            {
                canvas.DrawRect(track.Column, track.Row, 1, 1, _paint);
            }

            (int left, int top)     = _pixelMapper.ViewPortPixelsToCoords(0, 0);
            (int right, int bottom) = _pixelMapper.ViewPortPixelsToCoords(_pixelMapper.ViewPortWidth, _pixelMapper.ViewPortHeight);

            canvas.DrawRect(left, top, right - left, bottom - top, _viewPortPaint);

            canvas.DrawRect(0, 0, _pixelMapper.Columns, _pixelMapper.Rows, _border);
        }
Example #6
0
        public bool PointerRelease(int x, int y)
        {
            (int column, int row) = _pixelMapper.ViewPortPixelsToCoords(x, y);

            if (_capturedHandler is null &&
                !_hasDragged &&
                _gameManager.CurrentTool is not null &&
                _gameManager.CurrentTool.IsValid(column, row))
            {
                _gameManager.CurrentTool.Execute(column, row, false);
            }

            _hasDragged     = false;
            _lastToolColumn = -1;
            _lastToolRow    = -1;
            if (_capturedHandler != null || _capturedTool != null)
            {
                _capturedTool    = null;
                _capturedHandler = null;
                return(true);
            }
            return(false);
        }
Example #7
0
        public override void Draw(SKCanvas canvas, RectangleF dirtyRect)
        {
            if (dirtyRect.IsEmpty)
            {
                return;
            }
            if (!_redraw)
            {
                return;
            }

            canvas.Clear(TerrainColourLookup.DefaultColour.ToSkia());

            using var paint = new SKPaint
                  {
                      Style = SKPaintStyle.Fill,
                  };

            foreach (Terrain terrain in _terrainMap)
            {
                paint.Color = TerrainColourLookup.GetTerrainColour(terrain).ToSkia();
                canvas.DrawRect(terrain.Column, terrain.Row, 1, 1, paint);
            }

            foreach (Track track in _trackLayout.OfType <Track>())
            {
                canvas.DrawRect(track.Column, track.Row, 1, 1, _paint);
            }

            (int left, int top)     = _pixelMapper.ViewPortPixelsToCoords(0, 0);
            (int right, int bottom) = _pixelMapper.ViewPortPixelsToCoords(_pixelMapper.ViewPortWidth, _pixelMapper.ViewPortHeight);

            canvas.DrawRect(left, top, right - left, bottom - top, _viewPortPaint);

            _redraw = false;
        }
Example #8
0
        public void SetSize(int width, int height)
        {
            (int columns, int rows) = _pixelMapper.ViewPortPixelsToCoords(width, height);
            columns = Math.Max(columns, 1);
            rows    = Math.Max(rows, 1);

            (_width, _height) = _pixelMapper.CoordsToViewPortPixels(columns, rows);

            _pixelMapper.SetViewPortSize(_width, _height);

            _gameBoard.Columns = columns;
            _gameBoard.Rows    = rows;

            ResetBuffers();
        }
Example #9
0
        private bool ExecuteTool(ITool tool, int x, int y, PointerAction action)
        {
            (int column, int row) = _pixelMapper.ViewPortPixelsToCoords(x, y);

            var inSameCell = (column == _lastToolColumn && row == _lastToolRow);

            if (action is PointerAction.Click or PointerAction.Drag)
            {
                _lastToolColumn = column;
                _lastToolRow    = row;
            }

            if (!inSameCell && tool.IsValid(column, row) && action is PointerAction.Click or PointerAction.Drag)
            {
                tool.Execute(column, row);
                return(true);
            }
        private bool HandleInteraction(int x, int y, MouseAction action)
        {
            (int width, int height) = _game.GetScreenSize();

            if (_capturedScreen != null)
            {
                _capturedScreen.HandleInteraction(x, y, width, height, action);
                return(true);
            }

            foreach (IScreen screen in _screens)
            {
                if (screen.HandleInteraction(x, y, width, height, action))
                {
                    _capturedScreen = screen;
                    return(true);
                }
            }


            if (this.CurrentTool is not null)
            {
                (int column, int row) = _pixelMapper.ViewPortPixelsToCoords(x, y);

                if (this.CurrentTool.IsValid(column, row) && action != MouseAction.Move)
                {
                    this.CurrentTool.Execute(column, row);
                    return(true);
                }
                else if (this.CurrentTool is IDraggableTool draggableTool)
                {
                    if (action == MouseAction.Click)
                    {
                        draggableTool.StartDrag(x, y);
                        return(true);
                    }
                    else if (action == MouseAction.Drag)
                    {
                        draggableTool.ContinueDrag(x, y);
                        return(true);
                    }
                }
            }

            return(false);
        }
Example #11
0
        public void SetSize(int width, int height)
        {
            _screenWidth  = width;
            _screenHeight = height;

            _imageCache.SetDirtyAll(_screens);

            (int columns, int rows) = _pixelMapper.ViewPortPixelsToCoords(width, height);
            columns = Math.Max(columns, 1);
            rows    = Math.Max(rows, 1);

            (width, height, _) = _pixelMapper.CoordsToViewPortPixels(columns + 1, rows + 1);

            if (_width != width || _height != height)
            {
                _width  = width;
                _height = height;
                _pixelMapper.SetViewPortSize(_width, _height);

                // strictly speaking this only needs to clear renderers, but we already cleared screens
                // so its easier just to call clear
                _imageCache.Clear();
            }
        }