Source code for simvx.core.input.map

"""InputMap — input action registry. Instance-based with module-level default."""

import contextvars
import logging
from contextlib import contextmanager

from .enums import JoyButton, Key, MouseButton, name_to_keys
from .events import InputBinding

log = logging.getLogger(__name__)

class _InputMap:
    """Maps action names to physical inputs. Create new instances for per-tree isolation."""

    def __init__(self):
        self._actions: dict[str, list[InputBinding]] = {}

    def add_action(self, name: str, bindings: list[InputBinding | Key | MouseButton | JoyButton | str] | None = None):
        """Register a named action with optional initial bindings.

        Convenience: passing bare Key/MouseButton/JoyButton values auto-wraps them.
        """
        if name in self._actions:
            log.warning("Input action %r overwritten (had %s bindings)", name, len(self._actions[name]))
        else:
            self._actions[name] = []
        log.debug("InputMap.add_action(%r, %s)", name, bindings)
        if bindings:
            self._actions[name].extend(self._to_binding(b) for b in bindings)

    def remove_action(self, name: str):
        """Remove a named action and all its bindings."""
        self._actions.pop(name, None)

    def add_binding(self, name: str, binding: InputBinding | Key | MouseButton | JoyButton | str):
        """Add a binding to an existing action. Creates the action if it does not exist."""
        if name not in self._actions:
            self._actions[name] = []
        self._actions[name].append(self._to_binding(binding))

    def remove_binding(self, name: str, binding: InputBinding):
        """Remove a specific binding from an action."""
        if name in self._actions:
            try:
                self._actions[name].remove(binding)
            except ValueError:
                pass

    def get_bindings(self, name: str) -> list[InputBinding]:
        """Return bindings for an action (empty list if unknown)."""
        return self._actions.get(name, [])

    def has_action(self, name: str) -> bool:
        """Check if an action is registered."""
        return name in self._actions

    @property
    def actions(self) -> list[str]:
        """All registered action names."""
        return list(self._actions)

    def clear(self):
        """Remove all actions and bindings."""
        self._actions.clear()

    def _to_binding(self, b: InputBinding | Key | MouseButton | JoyButton | str) -> InputBinding:
        if isinstance(b, InputBinding):
            return b
        if isinstance(b, Key):
            return InputBinding(key=b)
        if isinstance(b, MouseButton):
            return InputBinding(mouse_button=b)
        if isinstance(b, JoyButton):
            return InputBinding(joy_button=b)
        if isinstance(b, str):
            # Try name_to_keys lookup first (handles "space", "escape", etc.)
            keys = name_to_keys(b)
            if keys:
                return InputBinding(key=keys[0])
            # Try enum name lookup: Key, MouseButton, JoyButton
            upper = b.upper()
            for enum_cls, field in ((Key, "key"), (MouseButton, "mouse_button"), (JoyButton, "joy_button")):
                try:
                    return InputBinding(**{field: enum_cls[upper]})
                except KeyError:
                    continue
            raise ValueError(f"Cannot resolve input binding from string {b!r}")
        raise TypeError(f"Cannot create InputBinding from {type(b).__name__}")

_default_input_map = _InputMap()
_active_input_map: contextvars.ContextVar[_InputMap] = contextvars.ContextVar("_active_input_map", default=_default_input_map)

class _InputMapProxy:
    """Proxy that delegates all access to the active _InputMap for the current context."""

    __slots__ = ()

    def __getattr__(self, name: str):
        return getattr(_active_input_map.get(), name)

    def __setattr__(self, name: str, value):
        setattr(_active_input_map.get(), name, value)

    def __repr__(self) -> str:
        return repr(_active_input_map.get())

[docs] @contextmanager def set_active_input_map(instance: _InputMap): """Context manager to set the active InputMap for the current context.""" token = _active_input_map.set(instance) try: yield finally: _active_input_map.reset(token)
InputMap = _InputMapProxy()