public NextPieceCanvas(NextPieceViewModel nextPieceViewModel, SolidColorBrush[] blockBrushes) { _nextPieceViewModel = nextPieceViewModel; _blockBrushes = blockBrushes; // Update View (NextPieceCanvas) every time model (GameBoard.NextPiece) changes // Soon after, OnRender method is called _nextPieceViewModel.GameBoard.NextPieceChanged += (sender, e) => InvalidateVisual(); }
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) { // GameBoard GameBoard gameBoard = new GameBoard(Rows, Cols); _gameBoardViewModel = new GameBoardViewModel(gameBoard, _statistics); GameBoardCanvas gameBoardCanvas = new GameBoardCanvas(gameBoard, BlockSizeInPixels, BlockBrushes) { Height = Rows * BlockSizeInPixels, // Usually 500px Width = Cols * BlockSizeInPixels // Usually 250px }; Border gameBoardCanvasBorder = BuildBorderForFrameworkElement(gameBoardCanvas, ElementsBorderThickness); gameBoardCanvasBorder.Margin = new Thickness(ElementsSpacing); // NextPiece NextPieceViewModel nextPieceViewModel = new NextPieceViewModel(gameBoard, NextPieceBlockSizeInPixels, NextPieceRows, NextPieceCols); NextPieceCanvas nextPieceCanvas = new NextPieceCanvas(nextPieceViewModel, BlockBrushes) { Height = NextPieceRows * NextPieceBlockSizeInPixels, // Usually 120px Width = NextPieceCols * NextPieceBlockSizeInPixels // Usually 120px }; Border nextPieceCanvasBorder = BuildBorderForFrameworkElement(nextPieceCanvas, ElementsBorderThickness); nextPieceCanvasBorder.Margin = new Thickness(gameBoardCanvasBorder.Width + 2 * ElementsSpacing, ElementsSpacing, ElementsSpacing, ElementsSpacing); // Statistics StatisticsViewModel statisticsViewModel = new StatisticsViewModel(_statistics); StatisticsUserControl statisticsUserControl = new StatisticsUserControl(statisticsViewModel) { Height = 94, Width = nextPieceCanvas.Width // Usually 120px }; Border statisticsCanvasBorder = BuildBorderForFrameworkElement(statisticsUserControl, ElementsBorderThickness); statisticsCanvasBorder.Margin = new Thickness(gameBoardCanvasBorder.Width + 2 * ElementsSpacing, nextPieceCanvasBorder.Height + 2 * ElementsSpacing, ElementsSpacing, ElementsSpacing); // HighScores HighScoresViewModel highScoresViewModel = new HighScoresViewModel(_highScoreList); HighScoresUserControl highScoresUserControl = new HighScoresUserControl(highScoresViewModel) { Height = 135, Width = nextPieceCanvas.Width // Usually 120px }; Border highScoresCanvasBorder = BuildBorderForFrameworkElement(highScoresUserControl, ElementsBorderThickness); highScoresCanvasBorder.Margin = new Thickness(gameBoardCanvasBorder.Width + 2 * ElementsSpacing, nextPieceCanvasBorder.Height + statisticsCanvasBorder.Height + 3 * ElementsSpacing, ElementsSpacing, ElementsSpacing); // ButtonsUserControl _buttonsUserControl = new ButtonsUserControl { Height = gameBoardCanvasBorder.Height - nextPieceCanvasBorder.Height - statisticsCanvasBorder.Height - highScoresCanvasBorder.Height - 3 * ElementsSpacing - 2 * ElementsBorderThickness, Width = nextPieceCanvas.Width // Usually 120px }; Border buttonsUserControlBorder = BuildBorderForFrameworkElement(_buttonsUserControl, ElementsBorderThickness); buttonsUserControlBorder.Margin = new Thickness(gameBoardCanvasBorder.Width + 2 * ElementsSpacing, nextPieceCanvasBorder.Height + statisticsCanvasBorder.Height + highScoresCanvasBorder.Height + 4 * ElementsSpacing, ElementsSpacing, ElementsSpacing); // Make all backgrounds black gameBoardCanvas.Background = Brushes.Black; nextPieceCanvas.Background = Brushes.Black; statisticsUserControl.Background = Brushes.Black; highScoresUserControl.Background = Brushes.Black; _buttonsUserControl.Background = Brushes.Black; // Add controls to grid Grid2.Children.Add(gameBoardCanvasBorder); Grid2.Children.Add(nextPieceCanvasBorder); Grid2.Children.Add(statisticsCanvasBorder); Grid2.Children.Add(highScoresCanvasBorder); Grid2.Children.Add(buttonsUserControlBorder); // Event handling _buttonsUserControl.ButtonNewGame.Click += ButtonsUserControl_ButtonNewGame_Click; _buttonsUserControl.ButtonPauseResume.Click += ButtonsUserControl_ButtonPauseResume_Click; gameBoard.GameOver += GameBoard_GameOver; HighScoreInputUserControl1.ButtonOk.Click += HighScoreInputUserControl1_ButtonOk_Click; GameOverUserControl1.ButtonNewGame.Click += GameOverUserControl1_ButtonNewGame_Click; }