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

TableKeyDown() private method

private TableKeyDown ( object source, TableKeyEventArgs te ) : void
source object
te TableKeyEventArgs
return void
        private void TableKeyDown(object source, TableKeyEventArgs te)
        {
            try
            {
                // Fix: keyboard shortcuts are forbidden during a DnD
                if (_isDragging)
                {
                    te.Handled = te.KeyEventArgs.Handled = true;
                    return;
                }

                KeyEventArgs e = te.KeyEventArgs;
                switch (e.Key)
                {
                    case Key.PageUp:
                        Program.GameEngine.Table.BringToFront(Card);
                        e.Handled = te.Handled = true;
                        break;
                    case Key.PageDown:
                        Program.GameEngine.Table.SendToBack(Card);
                        e.Handled = te.Handled = true;
                        break;
                    case Key.P:
                        if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) && !Card.FaceUp)
                        {
                            if (Card != null)
                                Card.Peek();
                            break;
                        }
                        goto default;
                    default:
                        // Look for a custom shortcut in the game definition
                        ActionShortcut[] shortcuts = Card.Group.CardShortcuts;
                        ActionShortcut match =
                            shortcuts.FirstOrDefault(shortcut => shortcut.Key.Matches(this, te.KeyEventArgs));
                        if (match != null && Card.Group.CanManipulate())
                        {
                            // Look for cards to execute it upon, shortcuts are applied to selection first
                            IEnumerable<Card> targets;
                            if (!Selection.IsEmpty())
                                targets = Selection.Cards;
                            else if (Card.CanManipulate())
                                targets = Selection.ExtendToSelection(Card);
                            else
                                break;
                            // If the card is on the table, extract the cursor position
                            Point? pos = GroupControl is TableControl
                                             ? ((TableControl)GroupControl).MousePosition()
                                             : (Point?)null;
                            if (match.ActionDef.AsAction().Execute != null)
                                ScriptEngine.ExecuteOnCards(match.ActionDef.AsAction().Execute, targets, pos);
                            else if (match.ActionDef.AsAction().BatchExecute != null)
                                ScriptEngine.ExecuteOnBatch(match.ActionDef.AsAction().BatchExecute, targets, pos);
                            e.Handled = te.Handled = true;
                            break;
                        }

                        // Look for a "Move to" shortcut
                        Group group =
                            Player.LocalPlayer.Groups.FirstOrDefault(
                                g => g.MoveToShortcut != null && g.MoveToShortcut.Matches(this, te.KeyEventArgs));
                        bool toBottom = false;
                        // If no group is found, try to match a shortcut with "Alt" and use it as "Move to bottom"
                        if (group == null)
                        {
                            group =
                                Player.LocalPlayer.Groups.FirstOrDefault(
                                    g =>
                                    g.MoveToShortcut != null &&
                                    new KeyGesture(g.MoveToShortcut.Key, g.MoveToShortcut.Modifiers | ModifierKeys.Alt).
                                        Matches(this, te.KeyEventArgs));
                            if (group is Pile) toBottom = true;
                        }
                        if (group != null && group.CanManipulate())
                        {
                            Action<Card> moveAction = toBottom
                                                          ? (c => c.MoveTo(@group, true, @group.Count, false))
                                                          : new Action<Card>(c => c.MoveTo(group, true, false));
                            if (!Selection.IsEmpty())
                                Selection.ForEachModifiable(moveAction);
                            else if (count.IsMouseOver)
                            {
                                for (int i = MultipleCards.Count - 1; i >= 0; --i)
                                {
                                    var c = (Card)MultipleCards[i];
                                    if (c.CanManipulate()) moveAction(c);
                                }
                            }
                            else if (Card.CanManipulate())
                                moveAction(Card);
                            else
                                break;
                            e.Handled = te.Handled = true;
                            break;
                        }
                        break;
                }
            }
            catch (Exception e)
            {
                Log.Warn("TableKeyDown Error", e);
            }
        }