Example #1
0
        /// <inheritdoc/>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            // Get all cases for being pressed keys.
            switch (e.Key)
            {
            case var key when key.IsDigit():
            {
                var pt = Mouse.GetPosition(_imageGrid);
                if (IsPointOutOfRange(_imageGrid, pt))
                {
                    e.Handled = true;
                    return;
                }

                // Input or eliminate a digit.
                if (Keyboard.Modifiers == ModifierKeys.Shift)
                {
                    // Eliminate a digit.
                    _puzzle[
                        _pointConverter.GetCellOffset(pt.ToDPointF()),
                        e.Key.IsDigitUpsideAlphabets() ? e.Key - Key.D1 : e.Key - Key.NumPad1] = true;
                }
                else if (Keyboard.Modifiers == ModifierKeys.None)
                {
                    // Input a digit.
                    _puzzle[_pointConverter.GetCellOffset(pt.ToDPointF())] =
                        e.Key.IsDigitUpsideAlphabets() ? e.Key - Key.D1 : e.Key - Key.NumPad1;
                }

                UpdateUndoRedoControls();
                UpdateImageGrid();

                break;
            }

            case var key when key.IsArrow() && _focusedCells.Count == 1:
            {
                // Move the focused cell.
                int cell = _focusedCells.SetAt(0);
                _focusedCells.Clear();
                _focusedCells.Add(
                    e.Key switch
                    {
                        Key.Up => cell - 9 < 0 ? cell + 72 : cell - 9,
                        Key.Down => cell + 9 >= 81 ? cell - 72 : cell + 9,
                        Key.Left => cell - 1 < 0 ? cell + 8 : cell - 1,
                        Key.Right => (cell + 1) % 81,
                        _ => throw Throwings.ImpossibleCase
                    });

                _layerCollection.Add(new FocusLayer(_pointConverter, _focusedCells, Settings.FocusedCellColor));

                UpdateImageGrid();

                break;
            }