private static void CardsPropertyChanged(object o, DependencyPropertyChangedEventArgs args) { PlayerHand playerHand = (PlayerHand)o; // TODO: Considering doing a diff or something less bulky. playerHand.Children.Clear(); CardCollection cards = args.NewValue as CardCollection; CardCollection oldCards = args.OldValue as CardCollection; List<ColorettoCard> newCards = new List<ColorettoCard>(3); if (cards == null) { return; } else if (oldCards != null && cards.Count == oldCards.Count) { return; } else { // In order to highlight new cards we need to determine which ones are the new ones. for (int i = 0, j = 0; i < cards.Count; i++) { if (j == oldCards.Count || cards[i] != oldCards[j]) newCards.Add(cards[i]); else j++; } } var colorGroups = cards.Where(c => c.CardType == ColorettoCardTypes.Color).GroupBy(c => c.Color).OrderByDescending(g => g.Count()); foreach (IGrouping<ColorettoCardColors, ColorettoCard> colorGroup in colorGroups) { PlayerHandPile pile = new PlayerHandPile(); foreach (var card in colorGroup) { PlayerHandCard handCard = new PlayerHandCard { CardColor = card.Color }; pile.Children.Add(handCard); } int numberThatAreNew = newCards.Where(c => c.Color == colorGroup.Key).Count(); for (int i = pile.Children.Count - 1; numberThatAreNew > 0 && i >= 0; i--, numberThatAreNew--) { SetIsNew(pile.Children[i], true); } playerHand.Children.Add(pile); } var specialGroups = cards.Where(c => c.CardType != ColorettoCardTypes.Color).GroupBy(c => c.CardType).OrderBy(g => g.Key); foreach (IGrouping<ColorettoCardTypes, ColorettoCard> cardGroup in specialGroups) { Debug.Assert(cardGroup.Key != ColorettoCardTypes.Unknown || cardGroup.Key != ColorettoCardTypes.LastCycle, "LastCycle and unknown card types should not be in the player's hand"); PlayerHandPile pile = new PlayerHandPile(); foreach (var card in cardGroup) { UIElement specialCard = null; if (cardGroup.Key == ColorettoCardTypes.Plus2) specialCard = new PlayerHandPlusTwoCard(); else if (cardGroup.Key == ColorettoCardTypes.Wild) specialCard = new PlayerHandWildCard(); pile.Children.Add(specialCard); } int numberThatAreNew = newCards.Where(c => c.CardType == cardGroup.Key).Count(); for (int i = pile.Children.Count - 1; numberThatAreNew > 0 && i >= 0; i--, numberThatAreNew--) { SetIsNew(pile.Children[i], true); } playerHand.Children.Add(pile); } }
private static void CardsPropertyChanged(object o, DependencyPropertyChangedEventArgs args) { PlayerHand playerHand = (PlayerHand)o; // TODO: Considering doing a diff or something less bulky. playerHand.Children.Clear(); CardCollection cards = args.NewValue as CardCollection; if (cards == null) return; var colorGroups = cards.GroupBy(c => c.Color).OrderByDescending(g => g.Count()); foreach (IGrouping<ColorettoCardColors, ColorettoCard> colorGroup in colorGroups) { if (colorGroup.Key != ColorettoCardColors.None) { PlayerHandPile pile = new PlayerHandPile(); foreach (var card in colorGroup) { Brush brush = Brushes.Gold; switch (card.Color) { case ColorettoCardColors.Blue: brush = Brushes.Blue; break; case ColorettoCardColors.Brown: brush = Brushes.Brown; break; case ColorettoCardColors.Gray: brush = Brushes.Gray; break; case ColorettoCardColors.Green: brush = Brushes.Green; break; case ColorettoCardColors.Orange: brush = Brushes.Orange; break; case ColorettoCardColors.Pink: brush = Brushes.Pink; break; case ColorettoCardColors.Yellow: brush = Brushes.Yellow; break; case ColorettoCardColors.None: default: break; } pile.Children.Add(new PlayerHandCard { CardColor = brush }); } playerHand.Children.Add(pile); } } }