Skip to content

Commit

Permalink
fix changing keybinds while mod is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
apple1417 committed Mar 30, 2024
1 parent 1ebaf96 commit 05891ad
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
18 changes: 18 additions & 0 deletions src/keybinds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

@wraps(KeybindType.enable)
def enable_keybind(self: KeybindType) -> None:
self.is_enabled = True

if self.key is None or self.callback is None:
return

Expand Down Expand Up @@ -54,6 +56,8 @@ def enable_keybind(self: KeybindType) -> None:

@wraps(KeybindType.disable)
def disable_keybind(self: KeybindType) -> None:
self.is_enabled = False

handle = getattr(self, "_kb_handle", None)
if handle is None:
return
Expand All @@ -65,6 +69,20 @@ def disable_keybind(self: KeybindType) -> None:
KeybindType.disable = disable_keybind


@wraps(KeybindType._rebind) # pyright: ignore[reportPrivateUsage]
def rebind_keybind(self: KeybindType, new_key: str | None) -> None:
handle = getattr(self, "_kb_handle", None)
if handle is not None:
deregister_keybind(handle)

if self.is_enabled:
self.key = new_key
enable_keybind(self)


KeybindType._rebind = rebind_keybind # pyright: ignore[reportPrivateUsage]


@wraps(RawKeybind.enable)
def enable_raw_keybind(self: RawKeybind) -> None:
# Even more redundancy for type checking
Expand Down
40 changes: 31 additions & 9 deletions src/mods_base/keybinds.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@ class KeybindType:
event_filter: If not None, only runs the callback when the given event fires.
Extra Attributes:
is_enabled: True if this keybind has been enabled.
default_key: What the key was originally when registered. Does not update on rebind.
"""

identifier: str
key: str | None

# Putting this up here for a more convenient repr
is_enabled: bool = field(init=False, default=False)

# If `event_filter` is None, `callback` should be `KeybindCallback_Event | None`
# If `event_filter` is not None, `callback` should be `KeybindCallback_NoArgs | None`
# The decorator uses overloads to enforce this
Expand All @@ -81,16 +85,38 @@ def __post_init__(self) -> None:

self.default_key = self.key

# These two functions should get replaced by the keybind implementation
def __setattr__(self, name: str, value: Any) -> None:
# Simpler to use `__setattr__` than a property to detect key changes
if name == "key" and not hasattr(self, "_rebind_recursion_guard"):
self._rebind_recursion_guard = True
self._rebind(value)
del self._rebind_recursion_guard

super().__setattr__(name, value)

# These three functions should get replaced by the keybind implementation
# The initialization script should make sure to load it before any mods, to make sure they don't
# end up with references to these functions
# If writing a new implementation, make sure to still set `is_enabled` correctly
def enable(self) -> None:
"""Enables this keybind."""
logging.error("No keybind implementation loaded, unable to enable binds")
self.is_enabled = True

def disable(self) -> None:
"""Disables this keybind."""
logging.error("No keybind implementation loaded, unable to disable binds")
self.is_enabled = False

def _rebind(self, new_key: str | None) -> None:
"""
Called whenever this keybind is rebound, before the `key` attribute is updated.
May be used by the keybind implementation to rebind keys if needed.
Args:
new_key: The new key this keybind is being rebound to.
"""


@overload
Expand All @@ -105,8 +131,7 @@ def keybind(
is_hidden: bool = False,
is_rebindable: bool = True,
event_filter: EInputEvent = EInputEvent.IE_Pressed,
) -> KeybindType:
...
) -> KeybindType: ...


@overload
Expand All @@ -121,8 +146,7 @@ def keybind(
is_hidden: bool = False,
is_rebindable: bool = True,
event_filter: EInputEvent = EInputEvent.IE_Pressed,
) -> Callable[[KeybindCallback_NoArgs], KeybindType]:
...
) -> Callable[[KeybindCallback_NoArgs], KeybindType]: ...


@overload
Expand All @@ -137,8 +161,7 @@ def keybind(
is_hidden: bool = False,
is_rebindable: bool = True,
event_filter: None = None,
) -> KeybindType:
...
) -> KeybindType: ...


@overload
Expand All @@ -153,8 +176,7 @@ def keybind(
is_hidden: bool = False,
is_rebindable: bool = True,
event_filter: None = None,
) -> Callable[[KeybindCallback_Event], KeybindType]:
...
) -> Callable[[KeybindCallback_Event], KeybindType]: ...


def keybind(
Expand Down

0 comments on commit 05891ad

Please sign in to comment.