public static GetGameShape ( |
||
game | ||
리턴 |
/// <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> /// Event handler for the card deck 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 CardDeckChanged(object sender, EventArgs e) { //Get Decks Shapes GameShape gameShape = GameShape.GetGameShape(this.Card.Deck.Game); DeckShape oldDeck = (DeckShape)((Canvas)this.Parent).Parent; DeckShape newDeck = gameShape.GetDeckShape(this.Card.Deck); //Get the animation positions in relation to the world (background) double startX = Canvas.GetLeft(oldDeck) + Canvas.GetLeft(this); double startY = Canvas.GetTop(oldDeck) + Canvas.GetTop(this); double endX = Canvas.GetLeft(newDeck); double endY = Canvas.GetTop(newDeck); //Change the card parent ((Canvas)this.Parent).Children.Remove(this); newDeck.LayoutRoot.Children.Add(this); //Maintain the same card position relative to the new parent Canvas.SetLeft(this, (double.IsNaN(startX) ? Convert.ToDouble(0) : startX) - endX); Canvas.SetTop(this, (double.IsNaN(startY) ? Convert.ToDouble(0) : startY) - endY); //Reorder decks oldDeck.UpdateCardShapes(); newDeck.UpdateCardShapes(); }
/// <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.2)); 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(); }