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) { SidewaysStack stack = (SidewaysStack)o; CardCollection collection = (CardCollection)args.NewValue; if (collection.IsEmpty) { var _oldCards = stack.InternalChildren.OfType<UIElement>().Where(child => !(child is PilePlaceHolder)).ToList(); foreach (var item in _oldCards) { stack.Children.Remove(item); } return; } else { foreach (var groups in collection.Where(card => card.CardType == ColorettoCardTypes.Color).GroupBy(card => card.Color)) { int exist = stack.Children.OfType<PlayerHandCard>().Where(phc => phc.CardColor == groups.Key).Count(); int different = groups.Count() - exist; if (different > 0) { PlayerHandCard card = new PlayerHandCard { CardColor = groups.Key }; PlayerHand.SetIsNew(card, true); stack.Children.Add(card); return; } else if (different < 0) { Debug.Fail("A color was removed?"); } else { // no change in the number of this color } } foreach (var group in collection.Where(card => card.CardType != ColorettoCardTypes.Color).OrderBy(card => card.CardType).GroupBy(card => card.CardType)) { if (group.Key == ColorettoCardTypes.Wild) { int exist = stack.Children.OfType<PlayerHandWildCard>().Count(); int differnet = group.Count() - exist; if (differnet > 0) { PlayerHandWildCard card = new PlayerHandWildCard(); PlayerHand.SetIsNew(card, true); stack.Children.Add(card); return; } else if (differnet < 0) { Debug.Fail("A wild card was removed?"); } else { // no change in number of wild cards } } else if (group.Key == ColorettoCardTypes.Plus2) { int exist = stack.Children.OfType<PlayerHandPlusTwoCard>().Count(); int differnet = group.Count() - exist; if (differnet > 0) { PlayerHandPlusTwoCard card = new PlayerHandPlusTwoCard(); PlayerHand.SetIsNew(card, true); stack.Children.Add(card); return; } else if (differnet < 0) { Debug.Fail("A wild card was removed?"); } else { // no change in number of wild cards } } } } Debug.Fail("No card was added and the piles weren't cleared."); }