/// <summary> /// Event handler for the image card mouse left button up. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param> private void ImgCardMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (isDrag) { imgCard.ReleaseMouseCapture(); isDrag = false; //Get which deck this card was dropped into GameShape gameShape = GameShape.GetGameShape(this.Card.Deck.Game); DeckShape oldDeckShape = gameShape.GetDeckShape(this.Card.Deck); DeckShape nearestDeckShape = null; double nearestDistance = double.MaxValue; foreach (var deck in gameShape.DeckShapes) { if (deck.Deck.Enabled) { //double dx = Canvas.GetLeft(deck) - (Canvas.GetLeft(this) + Canvas.GetLeft((UIElement)((Canvas)this.Parent).Parent)); //double dy = Canvas.GetTop(deck) - (Canvas.GetTop(this) + Canvas.GetTop((UIElement)((Canvas)this.Parent).Parent)); var offset = VisualTreeHelper.GetOffset(deck); var dx = offset.X - e.GetPosition((UIElement)((Canvas)this.Parent).Parent).X; var dy = offset.Y - e.GetPosition((UIElement)((Canvas)this.Parent).Parent).Y; double distance = Math.Sqrt(dx * dx + dy * dy); if (distance < nearestDistance) { nearestDistance = distance; nearestDeckShape = deck; } } } if ((nearestDeckShape != null) && (CardDrag != null)) { CardDrag(this, gameShape.GetDeckShape(this.Card.Deck), nearestDeckShape); } gameShape.GetDeckShape(this.Card.Deck).UpdateCardShapes(); Canvas.SetZIndex(oldDeckShape, 0); } if (CardMouseLeftButtonUp != null) { CardMouseLeftButtonUp(this, e); } }
/// <summary> /// Event handler for the card visible property changed event. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void CardVisibleChanged(object sender, EventArgs e) { // set left and top values var gameShape = GameShape.GetGameShape(this.Card.Deck.Game); var cardShape = gameShape.GetCardShape((Model.Card)sender); if (double.IsNaN(Canvas.GetLeft(cardShape))) { cardShape.SetValue(Canvas.LeftProperty, Convert.ToDouble(0)); } if (double.IsNaN(Canvas.GetTop(cardShape))) { cardShape.SetValue(Canvas.TopProperty, Convert.ToDouble(0)); } _aniFlipStart.Begin(); }
/// <summary> /// Recalculate all the card positions and animate them to the new positions /// Should be called when the deck change its cards order or count /// </summary> public void UpdateCardShapes() { GameShape game = GameShape.GetGameShape(Deck.Game); NextCardX = 0; NextCardY = 0; double localCardSpacerX = CardSpacerX; double localCardSpacerY = CardSpacerY; if ((MaxCardsSpace > 0) && (Deck.Cards.Count > MaxCardsSpace)) { //override the spacers values to squeez cards localCardSpacerX = (CardSpacerX * MaxCardsSpace) / Deck.Cards.Count; localCardSpacerY = (CardSpacerY * MaxCardsSpace) / Deck.Cards.Count; } ////Create the animation to move the card from one deck to the other Duration duration = new Duration(TimeSpan.FromSeconds(0)); Storyboard sb = new Storyboard(); sb.Duration = duration; //Loop on the Deck Cards (not playing cards) for (int i = 0; i < Deck.Cards.Count; i++) { //Get the card shape CardShape cardShape = game.GetCardShape(Deck.Cards[i]); if (cardShape.Parent != this.LayoutRoot) { LayoutRoot.Children.Add(cardShape); } // set left and top values if (double.IsNaN(Canvas.GetLeft(cardShape))) { cardShape.SetValue(Canvas.LeftProperty, Convert.ToDouble(0)); } if (double.IsNaN(Canvas.GetTop(cardShape))) { cardShape.SetValue(Canvas.TopProperty, Convert.ToDouble(0)); } //Animate card to its correct position DoubleAnimation xAnim = new DoubleAnimation(); xAnim.Duration = duration; sb.Children.Add(xAnim); Storyboard.SetTarget(xAnim, cardShape); Storyboard.SetTargetProperty(xAnim, new PropertyPath("(Canvas.Left)")); xAnim.To = NextCardX; DoubleAnimation yAnim = new DoubleAnimation(); yAnim.Duration = duration; sb.Children.Add(yAnim); Storyboard.SetTarget(yAnim, cardShape); Storyboard.SetTargetProperty(yAnim, new PropertyPath("(Canvas.Top)")); yAnim.To = NextCardY; Canvas.SetZIndex(cardShape, i); //Increment the next card position NextCardX += localCardSpacerX; NextCardY += localCardSpacerY; } if (LayoutRoot.Resources.Contains("sb")) { LayoutRoot.Resources.Remove("sb"); } LayoutRoot.Resources.Add("sb", sb); sb.Begin(); }