コード例 #1
0
        public override void Draw(SKCanvas canvas, RectangleF dirtyRect)
        {
            if (dirtyRect.IsEmpty)
            {
                return;
            }
            if (!_redraw)
            {
                return;
            }

            const int maxGridSize = PixelMapper.MaxGridSize;

            using var bitmap = new SKBitmap(maxGridSize, maxGridSize);

            using var tempCanvas = new SKCanvas(bitmap);
            tempCanvas.Clear(SKColor.Parse(Colors.VeryLightGray.HexCode));
            using var canvasWrapper = new SKCanvasWrapper(tempCanvas);

            foreach (Track track in _trackLayout)
            {
                (int x, int y) = _pixelMapper.CoordsToWorldPixels(track.Column, track.Row);
                tempCanvas.DrawRect(new SKRect(x, y, _trackParameters.CellSize + x, _trackParameters.CellSize + y), _paint);
            }

            tempCanvas.DrawRect(new SKRect(_pixelMapper.ViewPortX * -1, _pixelMapper.ViewPortY * -1, Math.Abs(_pixelMapper.ViewPortX) + _pixelMapper.ViewPortWidth, Math.Abs(_pixelMapper.ViewPortY) + _pixelMapper.ViewPortHeight), _viewPortPaint);

            canvas.DrawBitmap(bitmap, new SKRect(0, 0, maxGridSize, maxGridSize), new SKRect(0, 0, 100, 100));

            _redraw = false;
        }
コード例 #2
0
        public override void Draw(SKCanvas canvas, RectangleF dirtyRect)
        {
            if (dirtyRect.IsEmpty)
            {
                return;
            }
            if (!_redraw)
            {
                return;
            }

            const int maxGridSize = PixelMapper.MaxGridSize;

            using var bitmap = new SKBitmap(maxGridSize, maxGridSize, SKImageInfo.PlatformColorType, SKAlphaType.Premul);

            using var tempCanvas = new SKCanvas(bitmap);
            tempCanvas.Clear(TerrainColourLookup.DefaultColour.ToSkia());
            using var canvasWrapper = new SKCanvasWrapper(tempCanvas);

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

            foreach (Terrain terrain in _terrainMap)
            {
                paint.Color    = TerrainColourLookup.GetTerrainColour(terrain).ToSkia();
                (int x, int y) = _pixelMapper.CoordsToWorldPixels(terrain.Column, terrain.Row);
                tempCanvas.DrawRect(new SKRect(x, y, _gameParameters.CellSize + x, _gameParameters.CellSize + y), paint);
            }

            foreach (Track track in _trackLayout.OfType <Track>())
            {
                (int x, int y) = _pixelMapper.CoordsToWorldPixels(track.Column, track.Row);
                tempCanvas.DrawRect(new SKRect(x, y, _gameParameters.CellSize + x, _gameParameters.CellSize + y), _paint);
            }

            tempCanvas.DrawRect(new SKRect(_pixelMapper.ViewPortX * -1, _pixelMapper.ViewPortY * -1, Math.Abs(_pixelMapper.ViewPortX) + _pixelMapper.ViewPortWidth, Math.Abs(_pixelMapper.ViewPortY) + _pixelMapper.ViewPortHeight), _viewPortPaint);

            canvas.DrawBitmap(bitmap, new SKRect(0, 0, maxGridSize, maxGridSize), new SKRect(0, 0, 100, 100));

            _redraw = false;
        }
コード例 #3
0
        public void Execute()
        {
            var pixelMapper = _pixelMapper.Snapshot();

            (int width, int height) = (pixelMapper.ViewPortWidth, pixelMapper.ViewPortHeight);
            using var bitmap        = new SKBitmap(width, height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);
            using var skCanvas      = new SKCanvas(bitmap);
            using (ICanvas canvas = new SKCanvasWrapper(skCanvas))
            {
                foreach (var renderer in _renderers)
                {
                    if (!renderer.Enabled)
                    {
                        continue;
                    }
                    using var _ = canvas.Scope();
                    renderer.Render(canvas, width, height, pixelMapper);
                }
            }
            Clipboard.SetImage(bitmap.ToWriteableBitmap());
        }
コード例 #4
0
ファイル: MainPage.cs プロジェクト: mortizzle/Trains.NET
        public MainPage(IGame game,
                        IEnumerable <ITool> tools,
                        IEnumerable <ILayerRenderer> layers,
                        IEnumerable <ICommand> commands,
                        ITrainController trainControls,
                        ILayout trackLayout,
                        IGameStorage gameStorage,
                        ITerrainMap terrainMap,
                        MiniMapDelegate miniMapDelegate,
                        TrainsDelegate trainsDelegate)
        {
            this.Title("Trains - " + ThisAssembly.AssemblyInformationalVersion);

            _game            = game;
            _controlDelegate = trainsDelegate;
            _miniMapDelegate = miniMapDelegate;

            this.Body = () =>
            {
                return(new HStack()
                {
                    new VStack()
                    {
                        _configurationShown?null:
                        new Button(trainControls.BuildMode ? "Building" : "Playing", () => SwitchGameMode()),
                        new Spacer(),
                        _configurationShown ?
                        CreateConfigurationControls(layers) :
                        CreateToolsControls(tools, _controlDelegate, trainControls.BuildMode.Value),
                        new Spacer(),
                        _configurationShown || !trainControls.BuildMode ? null :
                        CreateCommandControls(commands),
                        new Spacer(),
                        new HStack()
                        {
                            new Button(" - ", () => _game.Zoom(-1))
                            .Frame(40),
                            new Spacer(),
                            new Button(" + ", () => _game.Zoom(1))
                            .Frame(40),
                        },
                        new Spacer(),
                        new Button("Snapshot", () => Snapshot()),
                        new Button("Configuration", () => _configurationShown.Value = !_configurationShown.Value),
                        new DrawableControl(_miniMapDelegate).Frame(height: 100)
                    }.Frame(100, alignment: Alignment.Top),
                    new VStack()
                    {
                        new TrainControllerPanel(trainControls),
                        new DrawableControl(_controlDelegate)
                    }
                }.FillHorizontal());
            };

            _trackLayout = trackLayout;
            _gameStorage = gameStorage;

            _ = PresentLoop();

            void SwitchGameMode()
            {
                trainControls.ToggleBuildMode();

                if (_controlDelegate == null)
                {
                    return;
                }

                _controlDelegate.CurrentTool.Value = tools.FirstOrDefault(t => ShouldShowTool(trainControls.BuildMode, t));
            }

            void Snapshot()
            {
                (int width, int height) = _game.GetSize();
                using var bitmap        = new SKBitmap(width, height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);
                using var skCanvas      = new SKCanvas(bitmap);
                using (ICanvas canvas = new SKCanvasWrapper(skCanvas))
                {
                    canvas.Save();
                    _game.Render(canvas);
                    canvas.Restore();
                }
                Clipboard.SetImage(bitmap.ToWriteableBitmap());
            }

            _terrainMap = terrainMap;
        }
コード例 #5
0
ファイル: MainForm.cs プロジェクト: ScottHolden/Trains.NET
 private void SKControl_PaintSurface(object sender, SkiaSharp.Views.Desktop.SKPaintSurfaceEventArgs e)
 {
     using var canvas = new SKCanvasWrapper(e.Surface.Canvas);
     _game.Render(canvas);
 }