From ed05061476142842680948b582bfed3000a51678 Mon Sep 17 00:00:00 2001 From: "jeremy.mouyon" Date: Fri, 31 May 2024 11:33:36 +0200 Subject: [PATCH] work. --- Qwirkle/QwirkleClassLibrary/Games/Game.cs | 1 + Qwirkle/QwirkleClassLibrary/Players/Player.cs | 16 ++++++- Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs | 27 +++++++++-- Qwirkle/QwirkleViews/Pages/Gameboard.xaml | 47 +++++++++---------- 4 files changed, 58 insertions(+), 33 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index 5664acd..0615ff5 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -30,6 +30,7 @@ namespace QwirkleClassLibrary.Games [DataMember] private Board board = new Board(15, 12); + public Board Board => board; public ObservableCollection GetCellsInBoard => new ObservableCollection(board!.GetCells()); diff --git a/Qwirkle/QwirkleClassLibrary/Players/Player.cs b/Qwirkle/QwirkleClassLibrary/Players/Player.cs index 2c7411b..e9c4997 100644 --- a/Qwirkle/QwirkleClassLibrary/Players/Player.cs +++ b/Qwirkle/QwirkleClassLibrary/Players/Player.cs @@ -1,18 +1,24 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.ComponentModel; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using QwirkleClassLibrary.Tiles; namespace QwirkleClassLibrary.Players { - public class Player + public class Player : INotifyPropertyChanged { public ReadOnlyCollection Tiles => playerTiles.AsReadOnly(); private readonly List playerTiles = new(); + public event PropertyChangedEventHandler? PropertyChanged; + void OnPropertyChanged([CallerMemberName] string? propertyName = null) + => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + /// /// Creates a player with a name /// @@ -39,6 +45,7 @@ namespace QwirkleClassLibrary.Players public void AddTileToPlayer(Tile tile) { playerTiles.Add(tile); + OnPropertyChanged(); } /// @@ -48,7 +55,12 @@ namespace QwirkleClassLibrary.Players /// bool public bool RemoveTileToPlayer(Tile tile) { - return playerTiles.Remove(tile); + if (playerTiles.Remove(tile)) + { + OnPropertyChanged(); + return true; + } + return false; } } } diff --git a/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs b/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs index f933643..9e4cbd4 100644 --- a/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs +++ b/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs @@ -9,24 +9,34 @@ using System.ComponentModel; using Cell = QwirkleClassLibrary.Boards.Cell; namespace Qwirkle.Pages; -public partial class Gameboard : ContentPage +public partial class Gameboard : ContentPage, INotifyPropertyChanged { - public ICommand OnDrop => new Command(OnDropTile); - private Game game = ((App)Application.Current!).Game; private Tile? tiledrag; + + private ObservableCollection playerCells1; + public ObservableCollection PlayerCells1 + { + get => playerCells1; + set + { + playerCells1 = value; + OnPropertyChanged(nameof(PlayerCells1)); + } + } public Gameboard() { InitializeComponent(); BindingContext = game; - DisplayAlert("List", ((App)Application.Current!).Game.PlayerList[0].Tiles[0].ToString(), "chut"); + PlayerCells1 = new ObservableCollection(game.PlayerList[0].Tiles.ToList()); + + DisplayAlert("List", playerCells1[0].ToString(), "chut"); } - public List PlayerCells1 { get; set; } = ((App)Application.Current!).Game.PlayerList[0].Tiles.ToList(); private void OnDragOver(object sender, DragEventArgs e) @@ -59,4 +69,11 @@ public partial class Gameboard : ContentPage { DisplayAlert("Tile place notified", args.Reason, "<3"); } + + public event PropertyChangedEventHandler PropertyChanged; + + protected virtual void OnPropertyChanged(string propertyName) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } } \ No newline at end of file diff --git a/Qwirkle/QwirkleViews/Pages/Gameboard.xaml b/Qwirkle/QwirkleViews/Pages/Gameboard.xaml index a67ee1f..7dafa8f 100644 --- a/Qwirkle/QwirkleViews/Pages/Gameboard.xaml +++ b/Qwirkle/QwirkleViews/Pages/Gameboard.xaml @@ -10,43 +10,39 @@ - - - - - - - + HorizontalOptions="Center" + VerticalOptions="Center"> + + + + + + BackgroundColor="WhiteSmoke" + Margin="0"> - - + - - + + - + - + BackgroundColor="WhiteSmoke" + Margin="0"> - - + + - - \ No newline at end of file +