Пример #1
0
        /// <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);
            }
        }
Пример #2
0
        /// <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);
            }
        }
Пример #3
0
        /// <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();
        }