Octgn.Play.Gui.CardControl.DragCardStarted C# (CSharp) Method

DragCardStarted() protected method

protected DragCardStarted ( ) : void
return void
        protected void DragCardStarted()
        {
            DraggedCards.Clear();
            // in theory draggedCards should already be empty, but it's better to recover if there was a problem during last DnD						
            if (!Selection.IsEmpty())
                DraggedCards.AddRange(Selection.Cards);
            else if (_isOverCount)
                DraggedCards.AddRange(MultipleCards.Cast<Card>());
            else if (Card != null)
                DraggedCards.Add(Card);
            else
                return;
            // Fix: Card == null can occur, e.g. hold the mouse down but don't move, dismiss the card with a keyboard shortcut (e.g. Delete) and move the mouse after that (with the left button still down).

            _isDragging = true;
            mouseClickHandler.AutoFireNext();

            // Keep control of the card and the group it's in
            foreach (Card c in DraggedCards) c.KeepControl();
            Card.Group.KeepControl();

            // Hides the card view
            RaiseEvent(new CardEventArgs(CardHoveredEvent, this));

            // Starts the drag-and-drop            
            ScaleFactor = TransformToAncestor(_mainWin).TransformBounds(new Rect(0, 0, 1, 1)).Size;
            //bool rot90 = (Card.Orientation & CardOrientation.Rot90) != 0;
            var cs = Card == null ? Program.GameEngine.Definition.CardSize : Card.Size;
            if (IsUp)
            {
                _mouseOffset = new Vector(_mousePt.X * cs.Width / ActualWidth, _mousePt.Y * cs.Height / ActualHeight);
            }
            else
            {
                _mouseOffset = new Vector(_mousePt.X * cs.BackWidth / ActualWidth, _mousePt.Y * cs.BackHeight / ActualHeight);
            }

            // Create adorners
            var mwn = _mainWin.Content as Visual;
            AdornerLayer layer = null;
            if (mwn != null)
                layer = AdornerLayer.GetAdornerLayer(mwn);
            double offset = 0;
            double step = ActualWidth * 1.05;
            // HACK: if the selected card is in HandControl, its ContentPresenter has a RenderTransform, 
            // which we must account for
            if (GroupControl is HandControl)
            {
                var parent = VisualTreeHelper.GetParent(this) as ContentPresenter;
                if (parent != null)
                    step = 1.05 * parent.RenderTransform.TransformBounds(new Rect(0, 0, ActualWidth, 0)).Width;
            }
            foreach (CardControl cardCtrl in Selection.GetCardControls(GroupControl, this))
            {
                // Create an adorner                
                if (Card.Group is Table)
                {
                    var overlay = new CardDragAdorner(cardCtrl, _mouseOffset);
                    OverlayElements.Add(overlay);
                    if (layer != null) layer.Add(overlay);
                }
                else
                {
                    // If there are multiple cards but they don't come from the table, 
                    // layout the adorners properly
                    var overlay = new CardDragAdorner(this, cardCtrl, _mouseOffset);
                    OverlayElements.Add(overlay);
                    if (cardCtrl != this)
                    {
                        offset += step;
                        overlay.OffsetBy(offset, 0);
                        overlay.CollapseTo(0, 0, false);
                    }
                    if (layer != null) layer.Add(overlay);
                }

                // Make the card translucent
                cardCtrl.Opacity = 0.4;
            }
            if (!Selection.IsEmpty() || !_isOverCount) return;
            // Create additional fake adorners when dragging from the count label
            offset = 0;
            foreach (Card c in MultipleCards.Skip(1))
            {
                offset += step;
                var overlay = new CardDragAdorner(this, _mouseOffset);
                overlay.OffsetBy(offset, 0);
                overlay.CollapseTo(0, 0, false);
                OverlayElements.Add(overlay);
                if (layer != null) layer.Add(overlay);
            }
        }