protected void cardShape_CardDrag(CardShape cardShape, DeckShape oldDeckShape, DeckShape newDeckShape) { if (CardDrag != null) { CardDrag(cardShape, oldDeckShape, newDeckShape); } }
public CardShape GetCardShape(Card card) { for (int i = 0; i < CardShapes.Count; i++) { if (CardShapes[i].Card == card) { return(CardShapes[i]); } } //If not found, create a new card shape CardShape cardShape = new CardShape(); cardShape.Card = card; CardShapes.Add(cardShape); cardShape.CardMouseLeftButtonDown += new MouseButtonEventHandler(cardShape_MouseLeftButtonDown); cardShape.CardMouseLeftButtonUp += new MouseButtonEventHandler(cardShape_MouseLeftButtonUp); cardShape.CardMouseEnter += new MouseEventHandler(cardShape_MouseEnter); cardShape.CardMouseLeave += new MouseEventHandler(cardShape_MouseLeave); cardShape.CardMouseMove += new MouseEventHandler(cardShape_MouseMove); cardShape.CardDrag += new CardDragEventHandler(cardShape_CardDrag); return(cardShape); }
/// <summary> /// Event handler for the image card mouse move event. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param> private void ImgCardMouseMove(object sender, MouseEventArgs e) { if (isDrag) { Point newMousePos = e.GetPosition(LayoutRoot); double dx = newMousePos.X - oldMousePos.X; double dy = newMousePos.Y - oldMousePos.Y; GameShape gameShape = GameShape.GetGameShape(this.Card.Deck.Game); for (int i = this.Card.Deck.Cards.IndexOf(this.Card); i < this.Card.Deck.Cards.Count; i++) { CardShape cardShape = gameShape.GetCardShape(this.Card.Deck.Cards[i]); Canvas.SetLeft(cardShape, Canvas.GetLeft(cardShape) + dx); Canvas.SetTop(cardShape, Canvas.GetTop(cardShape) + dy); Canvas.SetZIndex(gameShape.GetDeckShape(this.Card.Deck), 100); } } if (CardMouseMove != null) { CardMouseMove(this, e); } }
/// <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(); }