Metadata-Version: 2.4
Name: kivy-chess-board
Version: 0.2.2
Summary: Provides a chess board as a kivy widget.
Keywords: chess
Author: Moritz R. Schäfer
Author-email: Moritz R. Schäfer <moritz.schaefer-f91@rub.de>
License-Expression: GPL-3.0-or-later
Requires-Dist: chess>=1.11.1
Requires-Dist: kivy>=2.3.1
Requires-Dist: pdoc ; extra == 'docs'
Maintainer: Moritz R. Schäfer
Maintainer-email: Moritz R. Schäfer <moritz.schaefer-f91@rub.de>
Requires-Python: >=3.10, <3.14
Project-URL: Documentation, https://kivy-chess-board-ebce1f.gitlab.io/
Project-URL: Issues, https://gitlab.com/M0M097/kivy-chess-board/-/issues
Project-URL: Repository, https://gitlab.com/M0M097/kivy-chess-board
Provides-Extra: docs
Description-Content-Type: text/markdown

# ChessBoard - a Kivy Widget to interact with a Chess Board

This package provides a chess board as a kivy widget. It allows users to
interact with the chess board by dragging and dropping pieces, and it can be
integrated into Kivy applications.

The widget displays the current state of a `chess.Board` from the
`python-chess` library. It supports highlighting specific moves and selecting
squares. By design, it does not implement any game logic or enforce chess rules;
it simply reflects the state of the provided `chess.Board` and notifies the host
application of user interactions.

## Exmaple Usage

See the [documentation](https://kivy-chess-board-ebce1f.gitlab.io/) for more
details and full API reference.

```python
import chess
from kivy.app import App
from kivy.config import Config as KivyConfig
from kivy.logger import LOG_LEVELS, Logger
from kivy.uix.floatlayout import FloatLayout
from kivy.utils import platform

from kivy_chess_board import ChessBoard


class MyApp(App):
    def build(self):
        root = FloatLayout()
        cb = ChessBoard()
        cb.highlight_hover_when_dragged = True
        root.add_widget(cb)

        # At any time we can set the board to a specific position and highlight
        # moves.
        cb.update(
            chess.Board(),
            moves_to_be_highlighted=[
                chess.Move.from_uci("e2e4"),
                chess.Move.from_uci("d2d4"),
            ],
            move_selector=0,  # e2e4 move appears "selected"
        )

        cb.process_square_selection = process_square
        cb.process_move_selection = process_move
        return root


def process_square(cb: ChessBoard, pos: chess.Square) -> None:
    """Callback to process square selections (clicks as well as drag-and-drop)."""
    cb.squares_to_be_highlighted = [pos]
    square_name = chess.square_name(pos)
    Logger.debug(f"App: Square selected: {square_name}")


def process_move(cb: ChessBoard, move: chess.Move) -> None:
    """Callback to process move selection."""
    if move not in cb.board.legal_moves:
        Logger.warning("App: Illegal move")
        return

    cb.board.push(move)
    cb.update()

    # flip board after each move, this will only be slow the first time when
    # the assets for the inverted board are loaded
    cb.inverted = not cb.inverted

    cb.moves_to_be_highlighted = [move]  # highlight last move
    cb.squares_to_be_highlighted = []


def adjust_kivy_config() -> None:
    """Remove weird touchpad behavior on Linux.

    This is a bug in Kivy itself, not specific to this progream."""
    if platform != "linux":
        return

    if KivyConfig is None or not KivyConfig.has_section("input"):
        return

    for opt in list(KivyConfig.options("input")):
        if KivyConfig.get("input", opt) == "probesysfs":
            KivyConfig.remove_option("input", opt)


Logger.setLevel(LOG_LEVELS["debug"])
adjust_kivy_config()
MyApp().run()
```
