public void Render(ICanvas canvas, int width, int height, IPixelMapper pixelMapper)
        {
            var strings = new List <string>();

            float maxWidth = 0;

            foreach ((string name, IStat stat) in InstrumentationBag.Stats)
            {
                if (stat.ShouldShow())
                {
                    string line = name + ": " + stat.GetDescription();
                    strings.Add(line);
                    maxWidth = Math.Max(maxWidth, canvas.MeasureText(line, Brushes.Label));
                }
            }

            var lineGap     = 3;
            var lineHeight  = Brushes.Label.TextSize.GetValueOrDefault();
            var panelHeight = strings.Count * (lineHeight + lineGap);

            canvas.Translate(10, height - panelHeight - 40);

            canvas.DrawRect(0, 0, maxWidth, panelHeight, Brushes.PanelBackground);
            foreach (string?line in strings)
            {
                canvas.DrawText(line, 0, lineHeight, Brushes.Label);
                canvas.Translate(0, lineGap + lineHeight);
            }
        }
 public TrackLayoutRenderer(IGameBoard gameBoard, ITrackRenderer trackRenderer, IPixelMapper pixelMapper, ITrackParameters parameters)
 {
     _gameBoard     = gameBoard;
     _trackRenderer = trackRenderer;
     _pixelMapper   = pixelMapper;
     _parameters    = parameters;
 }
示例#3
0
 public InteractionManager(IEnumerable <IInteractionHandler> handlers, IGame game, IPixelMapper pixelMapper, IGameManager gameManager)
 {
     _handler     = handlers.Reverse().ToArray();
     _game        = game;
     _pixelMapper = pixelMapper;
     _gameManager = gameManager;
 }
示例#4
0
        public void Render(ICanvas canvas, int width, int height, IPixelMapper pixelMapper)
        {
            canvas.DrawRect(0, 0, pixelMapper.ViewPortWidth, pixelMapper.ViewPortHeight, new PaintBrush {
                Style = PaintStyle.Fill, Color = TerrainColourLookup.DefaultColour
            });

            if (_terrainMap.IsEmpty())
            {
                return;
            }

            // Draw any non-grass cells
            foreach (Terrain terrain in _terrainMap)
            {
                (int x, int y, bool onScreen) = pixelMapper.CoordsToViewPortPixels(terrain.Column, terrain.Row);

                if (!onScreen)
                {
                    continue;
                }

                Color colour = TerrainColourLookup.GetTerrainColour(terrain);

                if (colour == TerrainColourLookup.DefaultColour)
                {
                    continue;
                }

                canvas.DrawRect(x, y, pixelMapper.CellSize, pixelMapper.CellSize, new PaintBrush {
                    Style = PaintStyle.Fill, Color = colour
                });
            }

            _dirty = false;
        }
示例#5
0
        public void Render(ICanvas canvas, int width, int height, IPixelMapper pixelMapper)
        {
            if (_terrainMap.IsEmpty())
            {
                canvas.DrawRect(0, 0, pixelMapper.ViewPortWidth, pixelMapper.ViewPortHeight, new PaintBrush {
                    Style = PaintStyle.Fill, Color = TerrainColourLookup.DefaultColour
                });
                return;
            }

            // Draw any non-grass cells
            foreach (Terrain terrain in _terrainMap)
            {
                Color colour = TerrainColourLookup.GetTerrainColour(terrain);

                (int x, int y) = pixelMapper.CoordsToViewPortPixels(terrain.Column, terrain.Row);
                canvas.DrawRect(x, y, _gameParameters.CellSize, _gameParameters.CellSize, new PaintBrush {
                    Style = PaintStyle.Fill, Color = colour
                });
                // Debug, this draws coord and height onto cells
                //canvas.DrawText($"{terrain.Column},{terrain.Row}", x + 2, y + 0.3f * _gameParameters.CellSize, new PaintBrush { Style = PaintStyle.Fill, Color = Colors.Black });
                //canvas.DrawText($"{terrain.Height}", x + 2, y + 0.7f * _gameParameters.CellSize, new PaintBrush { Style = PaintStyle.Fill, Color = Colors.Black });
            }

            _dirty = false;
        }
示例#6
0
 public PointerTool(ITrainController gameState, IGameBoard gameBoard, IPixelMapper pixelMapper, ILayout <Track> trackLayout)
 {
     _gameState   = gameState;
     _gameBoard   = gameBoard;
     _pixelMapper = pixelMapper;
     _trackLayout = trackLayout;
 }
示例#7
0
 public PointerTool(ITrainManager trainManager, IGameBoard gameBoard, IPixelMapper pixelMapper, ILayout <Track> trackLayout)
 {
     _trainManager = trainManager;
     _gameBoard    = gameBoard;
     _pixelMapper  = pixelMapper;
     _trackLayout  = trackLayout;
 }
示例#8
0
        public void Snapshot_PostSnapshot_SnapshotShoudntChange()
        {
            int   width     = 200;
            int   height    = 200;
            int   viewportX = 30;
            int   viewportY = 30;
            float gameScale = 2.0f;

            IPixelMapper pixelMapper = new PixelMapper();

            pixelMapper.SetViewPortSize(width, height);
            pixelMapper.AdjustGameScale(gameScale);
            pixelMapper.SetViewPort(viewportX, viewportY);

            IPixelMapper actual = pixelMapper.Snapshot();

            pixelMapper.AdjustGameScale(0.5f);
            pixelMapper.SetViewPort(0, 0);

            Assert.Equal(-viewportX, actual.ViewPortX);
            Assert.Equal(-viewportY, actual.ViewPortY);
            Assert.Equal(gameScale, actual.GameScale);

            Assert.Equal(width, actual.ViewPortWidth);
            Assert.Equal(height, actual.ViewPortHeight);
        }
示例#9
0
        public MainPage(IGame game,
                        IPixelMapper pixelMapper,
                        OrderedList <ITool> tools,
                        OrderedList <ILayerRenderer> layers,
                        OrderedList <ICommand> commands,
                        ITrainController trainControls,
                        ITrackParameters trackParameters,
                        ITrackLayout trackLayout,
                        IGameStorage gameStorage)
        {
            this.Title("Trains - " + ThisAssembly.AssemblyInformationalVersion);

            var controlDelegate = new TrainsDelegate(game, pixelMapper);

            _miniMapDelegate = new MiniMapDelegate(trackLayout, trackParameters, pixelMapper);

            this.Body = () =>
            {
                return(new HStack()
                {
                    new VStack()
                    {
                        new ToggleButton("Configuration", _configurationShown, () => _configurationShown.Value = !_configurationShown.Value),
                        new Spacer(),
                        _configurationShown ?
                        CreateConfigurationControls(layers) :
                        CreateToolsControls(tools, controlDelegate),
                        new Spacer(),
                        _configurationShown ? null :
                        CreateCommandControls(commands),
                        new Spacer(),
                        new DrawableControl(_miniMapDelegate).Frame(height: 100)
                    }.Frame(100, alignment: Alignment.Top),
                    new VStack()
                    {
                        new TrainControllerPanel(trainControls),
                        new DrawableControl(controlDelegate)
                    }
                }.FillHorizontal());
            };

            _timer          = new GameTimer();
            _timer.Interval = 16;
            _timer.Elapsed += (s, e) =>
            {
                game.AdjustViewPortIfNecessary();

                ThreadHelper.Run(async() =>
                {
                    await ThreadHelper.SwitchToMainThreadAsync();

                    controlDelegate.Invalidate();
                    _miniMapDelegate.Invalidate();
                });
            };
            _timer.Start();
            _trackLayout = trackLayout;
            _gameStorage = gameStorage;
        }
示例#10
0
        public Game(IGameBoard gameBoard, OrderedList <ILayerRenderer> boardRenderers, IPixelMapper pixelMapper)
        {
            _gameBoard      = gameBoard;
            _boardRenderers = boardRenderers;
            _pixelMapper    = pixelMapper;

            _renderLayerDrawTimes = _boardRenderers.ToDictionary(x => x, x => InstrumentationBag.Add <ElapsedMillisecondsTimedStat>(x.Name.Replace(" ", "") + "DrawTime"));
        }
示例#11
0
        public void Snapshot_NoChanges()
        {
            IPixelMapper pixelMapper = new PixelMapper();

            IPixelMapper actual = pixelMapper.Snapshot();

            AssertSnapshotsSame(pixelMapper, actual);
        }
示例#12
0
 public TrainLookaheadRenderer(IGameBoard gameBoard, IPixelMapper pixelMapper, ITrackParameters parameters, ITrainPainter painter, ITimer gameTimer)
 {
     _gameBoard   = gameBoard;
     _pixelMapper = pixelMapper;
     _parameters  = parameters;
     _painter     = painter;
     _gameTimer   = gameTimer;
 }
示例#13
0
        public TrackLayoutRenderer(ITrackLayout trackLayout, ITrackRenderer trackRenderer, IPixelMapper pixelMapper, ITrackParameters parameters)
        {
            _trackLayout   = trackLayout;
            _trackRenderer = trackRenderer;
            _pixelMapper   = pixelMapper;
            _parameters    = parameters;

            _trackLayout.TracksChanged += (s, e) => _dirty = true;
        }
        public TerrainMapRenderer(ITerrainMap terrainMap, IImageFactory imageFactory, IImageCache imageCache, IPixelMapper pixelMapper)
        {
            _terrainMap   = terrainMap;
            _imageFactory = imageFactory;
            _imageCache   = imageCache;
            _pixelMapper  = pixelMapper;

            _terrainMap.CollectionChanged += (s, e) => _imageCache.SetDirty(this);
        }
示例#15
0
        public MiniMapScreen(ITerrainMapRenderer terrainMapRenderer, ILayout <Track> trackLayout, IPixelMapper pixelMapper)
        {
            _terrainMapRenderer = terrainMapRenderer;
            _trackLayout        = trackLayout;
            _pixelMapper        = pixelMapper;

            _trackLayout.CollectionChanged += (s, e) => Changed?.Invoke(this, EventArgs.Empty);
            _pixelMapper.ViewPortChanged   += (s, e) => Changed?.Invoke(this, EventArgs.Empty);
        }
示例#16
0
        public MiniMapDelegate(IGameBoard gameBoard, ITrackParameters trackParameters, IPixelMapper pixelMapper)
        {
            _gameBoard       = gameBoard;
            _trackParameters = trackParameters;
            _pixelMapper     = pixelMapper;

            _pixelMapper.ViewPortChanged += (s, e) => _redraw = true;
            _gameBoard.TracksChanged     += (s, e) => _redraw = true;
        }
示例#17
0
 public MiniMapDelegate(ILayout trackLayout, IPixelMapper pixelMapper, ITerrainMap terrainMap)
 {
     _trackLayout = trackLayout;
     _pixelMapper = pixelMapper;
     _terrainMap  = terrainMap;
     _pixelMapper.ViewPortChanged   += (s, e) => _redraw = true;
     _trackLayout.CollectionChanged += (s, e) => _redraw = true;
     _terrainMap.CollectionChanged  += (s, e) => _redraw = true;
 }
示例#18
0
 public void Render(ICanvas canvas, int width, int height, IPixelMapper pixelMapper)
 {
     foreach (Train train in _gameBoard.GetMovables())
     {
         var _paint = new PaintBrush
         {
             Color = _painter.GetPalette(train).FrontSectionEndColor with {
                 A = 200
             },
示例#19
0
 public Game(IGameBoard gameBoard, OrderedList <ILayerRenderer> boardRenderers, IPixelMapper pixelMapper, IBitmapFactory bitmapFactory)
 {
     _gameBoard                    = gameBoard;
     _boardRenderers               = boardRenderers;
     _pixelMapper                  = pixelMapper;
     _bitmapFactory                = bitmapFactory;
     _renderLayerDrawTimes         = _boardRenderers.ToDictionary(x => x, x => InstrumentationBag.Add <ElapsedMillisecondsTimedStat>(x.Name.Replace(" ", "") + "DrawTime"));
     _pixelMapper.ViewPortChanged += (s, e) => _needsBufferReset = true;
 }
        public void Render(ICanvas canvas, int width, int height, IPixelMapper pixelMapper)
        {
            if (_terrainMapRenderer.TryGetTerrainImage(out IImage? terrainImage))
            {
                (Rectangle source, Rectangle destination) = GetSourceAndDestinationRectangles(pixelMapper);

                canvas.DrawImage(terrainImage, source, destination);
            }
        }
示例#21
0
        public MiniMapDelegate(ITrackLayout trackLayout, ITrackParameters trackParameters, IPixelMapper pixelMapper)
        {
            _trackLayout     = trackLayout;
            _trackParameters = trackParameters;
            _pixelMapper     = pixelMapper;

            _pixelMapper.ViewPortChanged += (s, e) => _redraw = true;
            _trackLayout.TracksChanged   += (s, e) => _redraw = true;
        }
示例#22
0
        public void Snapshot_CompareTwoSnapshotsFromSameMapper()
        {
            IPixelMapper pixelMapper = new PixelMapper();

            IPixelMapper expected = pixelMapper.Snapshot();
            IPixelMapper actual   = pixelMapper.Snapshot();

            AssertSnapshotsSame(expected, actual);
        }
示例#23
0
        public void Render(ICanvas canvas, int width, int height, IPixelMapper pixelMapper)
        {
            if (pixelMapper.CellSize != _lastCellSize)
            {
                _cache.Clear();
                _lastCellSize = pixelMapper.CellSize;
            }

            foreach (T entity in _layout)
            {
                (int x, int y, bool onScreen) = pixelMapper.CoordsToViewPortPixels(entity.Column, entity.Row);

                if (!onScreen)
                {
                    continue;
                }

                canvas.Save();

                canvas.Translate(x, y);

                if (_renderer is ICachableRenderer <T> cachableRenderer)
                {
                    canvas.ClipRect(new Rectangle(0, 0, pixelMapper.CellSize, pixelMapper.CellSize), false);

                    string key = cachableRenderer.GetCacheKey(entity);

                    if (!_cache.TryGetValue(key, out IImage cachedImage))
                    {
                        using IImageCanvas imageCanvas = _imageFactory.CreateImageCanvas(pixelMapper.CellSize, pixelMapper.CellSize);

                        float scale = pixelMapper.CellSize / 100.0f;

                        imageCanvas.Canvas.Scale(scale, scale);

                        _renderer.Render(imageCanvas.Canvas, entity);

                        cachedImage = imageCanvas.Render();

                        _cache[key] = cachedImage;
                    }

                    canvas.DrawImage(cachedImage, 0, 0);
                }
                else
                {
                    float scale = pixelMapper.CellSize / 100.0f;

                    canvas.Scale(scale, scale);

                    _renderer.Render(canvas, entity);
                }

                canvas.Restore();
            }
        }
示例#24
0
        public AdjustGameScaleTests(ITestOutputHelper output)
        {
            _pixelMapper = new PixelMapper();
            _pixelMapper.SetViewPortSize(ScreenSize, ScreenSize);
            int centerViewportOffsetX = _pixelMapper.MaxGridWidth / 2 - ScreenSize / 2;
            int centerViewportOffsetY = _pixelMapper.MaxGridHeight / 2 - ScreenSize / 2;

            _pixelMapper.SetViewPort(centerViewportOffsetX, centerViewportOffsetY);
            _output = output;
        }
示例#25
0
 private static void AssertSnapshotsSame(IPixelMapper expected, IPixelMapper actual)
 {
     Assert.Equal(expected.CellSize, actual.CellSize);
     Assert.Equal(expected.GameScale, actual.GameScale);
     Assert.Equal(expected.MaxGridWidth, actual.MaxGridWidth);
     Assert.Equal(expected.MaxGridHeight, actual.MaxGridHeight);
     Assert.Equal(expected.ViewPortHeight, actual.ViewPortHeight);
     Assert.Equal(expected.ViewPortWidth, actual.ViewPortWidth);
     Assert.Equal(expected.ViewPortX, actual.ViewPortX);
     Assert.Equal(expected.ViewPortY, actual.ViewPortY);
 }
示例#26
0
        public TerrainRenderer(ITerrainMap terrainMap, IPixelMapper pixelMapper, ITrackParameters trackParameters)
        {
            _terrainMap      = terrainMap;
            _pixelMapper     = pixelMapper;
            _trackParameters = trackParameters;

            _paintBrush = new PaintBrush
            {
                Color       = Colors.LightGray,
                StrokeWidth = 1,
                Style       = PaintStyle.Stroke
            };
        }
示例#27
0
        public CoordPixelConverstionTests(ITestOutputHelper output)
        {
            _output = output;

            _pixelMapper = new PixelMapper();
            _pixelMapper.SetViewPortSize(ScreenSize, ScreenSize);
            _pixelMapper.LogData(output);

            if (DefaultCellSize != _pixelMapper.CellSize)
            {
                throw new Exception("Cell size is different than this test expects, these tests assume the DefaultCellSize is " + DefaultCellSize);
            }
        }
示例#28
0
        public void Render(ICanvas canvas, int width, int height, IPixelMapper pixelMapper)
        {
            var tunnelRoofColour = BuildModeAwareColour(Colors.LightGray);
            var firstMountain    = new Terrain()
            {
                Height = Terrain.FirstMountainHeight
            };
            var tunnelBaseColour    = BuildModeAwareColour(TerrainMapRenderer.GetTerrainColour(firstMountain));
            var entranceColourArray = new[] { tunnelBaseColour, tunnelRoofColour, tunnelBaseColour };

            Dictionary <(int column, int row), Tunnel> entrances = new();

            foreach (Track track in _trackLayout)
            {
                var terrain = _terrainMap.Get(track.Column, track.Row);
                if (!terrain.IsMountain)
                {
                    continue;
                }

                (int x, int y, _) = pixelMapper.CoordsToViewPortPixels(track.Column, track.Row);

                // Paint over the tracks with the colour of the terrain. Would be awesome to remove this in future somehow
                var terrainColour = BuildModeAwareColour(TerrainMapRenderer.GetTerrainColour(terrain));
                canvas.DrawRect(x, y, pixelMapper.CellSize, pixelMapper.CellSize,
                                new PaintBrush
                {
                    Style = PaintStyle.Fill,
                    Color = terrainColour,
                });

                TrackNeighbors trackNeighbours = track.GetConnectedNeighbors();

                BuildEntrances(trackNeighbours.Up, Tunnel.Bottom, entrances);
                BuildEntrances(trackNeighbours.Right, Tunnel.Left, entrances);
                BuildEntrances(trackNeighbours.Down, Tunnel.Top, entrances);
                BuildEntrances(trackNeighbours.Left, Tunnel.Right, entrances);

                var currentCellTunnels = (IsEntrance(trackNeighbours.Up) ? Tunnel.Top : Tunnel.NoTunnels) |
                                         (IsEntrance(trackNeighbours.Right) ? Tunnel.Right : Tunnel.NoTunnels) |
                                         (IsEntrance(trackNeighbours.Down) ? Tunnel.Bottom : Tunnel.NoTunnels) |
                                         (IsEntrance(trackNeighbours.Left) ? Tunnel.Left : Tunnel.NoTunnels);

                DrawTunnel(canvas, pixelMapper, tunnelRoofColour, tunnelBaseColour, x, y, currentCellTunnels);
            }

            foreach (var(col, row, tunnels) in entrances)
            {
                DrawEntrance(canvas, pixelMapper, entranceColourArray, col, row, tunnels);
            }
        }
示例#29
0
        public void Render(ICanvas canvas, int width, int height, IPixelMapper pixelMapper)
        {
            foreach (Track track in _trackLayout)
            {
                if (!track.Happy)
                {
                    continue;
                }

                (int x, int y) = pixelMapper.CoordsToViewPortPixels(track.Column, track.Row);

                canvas.DrawRect(x, y, _gameParameters.CellSize, _gameParameters.CellSize, _paint);
            }
        }
示例#30
0
        public Task <IPixelMapper> GetMapper(BitmapPixelFormat format)
        {
            IPixelMapper returnValue = null;

            if (format == BitmapPixelFormat.Bgra8)
            {
                returnValue = new Bgra8Pixelmapper();
            }
            else
            {
                throw new NotSupportedException($"The pixel format {format} is not supported.");
            }

            return(Task.FromResult(returnValue));
        }