Exemple #1
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;
        }
Exemple #2
0
        private static View CreateToolsControls(IEnumerable <ITool> tools, TrainsDelegate controlDelegate)
        {
            var controlsGroup = new RadioGroup(Orientation.Vertical);

            foreach (ITool tool in tools)
            {
                controlsGroup.Add(new RadioButton(() => tool.Name, () => controlDelegate.CurrentTool.Value == tool, () => controlDelegate.CurrentTool.Value = tool));
            }

            return(controlsGroup);
        }
Exemple #3
0
        private static View CreateToolsControls(IEnumerable <ITool> tools, TrainsDelegate controlDelegate, bool buildMode)
        {
            var controlsGroup = new RadioGroup(Orientation.Vertical);

            foreach (ITool?tool in tools.Where(t => ShouldShowTool(buildMode, t)))
            {
                if (controlDelegate.CurrentTool.Value == null)
                {
                    controlDelegate.CurrentTool.Value = tool;
                }

                controlsGroup.Add(new RadioButton(() => tool.Name, () => controlDelegate.CurrentTool.Value == tool, () => controlDelegate.CurrentTool.Value = tool));
            }

            return(controlsGroup);
        }
Exemple #4
0
        public MainPage(IGame game,
                        IPixelMapper pixelMapper,
                        ITrackParameters trackParameters,
                        OrderedList <ITool> tools,
                        OrderedList <ILayerRenderer> layers,
                        OrderedList <ICommand> commands,
                        ITrainController trainControls)
        {
            this.Title("Train.NET - " + ThisAssembly.AssemblyInformationalVersion);

            var controlDelegate = new TrainsDelegate(game, pixelMapper);

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

            _timer = new Timer((state) =>
            {
                ThreadHelper.Run(async() =>
                {
                    await ThreadHelper.SwitchToMainThreadAsync();

                    controlDelegate.Invalidate();
                });
            }, null, 0, 16);
        }
Exemple #5
0
        public MainPage(IGame game, IPixelMapper pixelMapper, IEnumerable <ITool> tools)
        {
            HotReloadHelper.Register(this, game);

            this.Title("Trains.NET");

            this.Body = () =>
            {
                var controlsPanel = new VStack();

                var controlDelegate = new TrainsDelegate(game, pixelMapper);

                foreach (ITool tool in tools)
                {
                    controlsPanel.Add(new Button(tool.Name, () => controlDelegate.CurrentTool = tool));
                }

                return(new HStack()
                {
                    controlsPanel.Frame(100),
                    new DrawableControl(controlDelegate).FillVertical()
                }.FillHorizontal());
            };
        }
Exemple #6
0
        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;
        }
Exemple #7
0
        public MainPage(IGame game,
                        IPixelMapper pixelMapper,
                        OrderedList <ITool> tools,
                        OrderedList <ILayerRenderer> layers,
                        OrderedList <ICommand> commands,
                        ITrainController trainControls,
                        ITrackParameters trackParameters,
                        ITrackLayout trackLayout,
                        IGameStorage gameStorage,
                        ITimer gameTimer,
                        Factory <IToolPreviewer> previewerFactory)
        {
            this.Title("Trains - " + ThisAssembly.AssemblyInformationalVersion);

            var controlDelegate = new TrainsDelegate(game, pixelMapper, previewerFactory);

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

            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 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());
            };

            _gameTimer          = gameTimer;
            _gameTimer.Elapsed += (s, e) =>
            {
                game.AdjustViewPortIfNecessary();

                controlDelegate.FlagDraw();
                _miniMapDelegate.FlagDraw();

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

                    controlDelegate.Invalidate();
                    _miniMapDelegate.Invalidate();
                });
            };
            _trackLayout = trackLayout;
            _gameStorage = gameStorage;

            void SwitchGameMode()
            {
                trainControls.ToggleBuildMode();

                if (controlDelegate == null)
                {
                    return;
                }

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