Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename block_inputs() #6

Merged
merged 3 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/asyncpygame/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__all__ = (
'DEFAULT_PRIORITY', 'run', 'quit', 'Clock', 'SDLEvent', 'PriorityExecutor',
'capture_current_frame', 'block_inputs',
'capture_current_frame', 'block_input_events',
)

from asyncgui import *
Expand All @@ -9,4 +9,4 @@
from .constants import DEFAULT_PRIORITY
from ._sdlevent import SDLEvent
from ._priority_executor import PriorityExecutor
from ._utils import capture_current_frame, block_inputs
from ._utils import capture_current_frame, block_input_events
10 changes: 5 additions & 5 deletions src/asyncpygame/_sdlevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ class Subscriber:
sub.callback = another_function
'''

event_types: Container = None
topics: Container = None
'''
The types of :class:`pygame.event.Event` that the subscriber is interested in.
You can change it by simply assigning to this attribute.

.. code-block::

sub = sdl_event.subscribe(...)
sub.event_types = (FINGERMOTION, FINGERUP, )
sub.topics = (FINGERMOTION, FINGERUP, )
'''

_cancelled: bool = False
Expand Down Expand Up @@ -133,19 +133,19 @@ def dispatch(self, event: Event):
if sub._cancelled:
continue
subs2_append(sub)
if event_type in sub.event_types and sub.callback(event):
if event_type in sub.topics and sub.callback(event):
subs2.extend(sub_iter)
return
finally:
subs.clear()
self._subs = subs2
self._subs_2 = subs

def subscribe(self, event_types, callback, priority=DEFAULT_PRIORITY) -> Subscriber:
def subscribe(self, topics, callback, priority=DEFAULT_PRIORITY) -> Subscriber:
'''
async型APIの礎となっているコールバック型API。直接触るべきではない。
'''
sub = Subscriber(priority, callback, event_types)
sub = Subscriber(priority, callback, topics)
self._subs_to_be_added.append(sub)
return sub

Expand Down
6 changes: 3 additions & 3 deletions src/asyncpygame/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__all__ = (
'capture_current_frame', 'block_inputs',
'capture_current_frame', 'block_input_events',
)

from typing import Awaitable, ContextManager
Expand All @@ -24,13 +24,13 @@ async def capture_current_frame(executor: PriorityExecutor, priority, source: Su
return source.copy()


def block_inputs(sdlevent: SDLEvent, priority) -> ContextManager[Subscriber]:
def block_input_events(sdlevent: SDLEvent, priority) -> ContextManager[Subscriber]:
'''
Returns a context manager that blocks input events.

.. code-block::

with block_inputs(sdlevent, priority):
with block_input_events(sdlevent, priority):
...

.. warning::
Expand Down
2 changes: 2 additions & 0 deletions src/asyncpygame/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
C.MOUSEMOTION, C.MOUSEBUTTONDOWN, C.MOUSEBUTTONUP, C.MOUSEWHEEL,
C.FINGERDOWN, C.FINGERUP, C.FINGERMOTION,
C.JOYAXISMOTION, C.JOYBALLMOTION, C.JOYHATMOTION, C.JOYBUTTONDOWN, C.JOYBUTTONUP,
C.TEXTEDITING, C.TEXTINPUT,
C.MULTIGESTURE,
}
4 changes: 2 additions & 2 deletions src/asyncpygame/scene_switcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import asyncgui as ag
from pygame.math import Vector2

from ._utils import capture_current_frame, block_inputs
from ._utils import capture_current_frame, block_input_events


Transition: TypeAlias = Callable[..., AsyncGenerator[None, None]]
Expand Down Expand Up @@ -87,7 +87,7 @@ async def run(self, first_scene, *, userdata: Any=None, priority, sdlevent, **kw
next_scene, transition = (await self._next_scene_request.wait())[0]
agen = transition(priority=priority, **common_params)
try:
with block_inputs(sdlevent, priority):
with block_input_events(sdlevent, priority):
await agen.asend(None)
task.cancel()
await agen.asend(None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_lower_priority():

received = []
sdlevent.subscribe((KEYDOWN, ), received.append, priority=20)
with ap.block_inputs(sdlevent, priority=10):
with ap.block_input_events(sdlevent, priority=10):
sdlevent.dispatch(Event(KEYDOWN))
assert len(received) == 1
sdlevent.dispatch(Event(KEYDOWN))
Expand All @@ -21,7 +21,7 @@ def test_higher_priority():

received = []
sdlevent.subscribe((KEYDOWN, ), received.append, priority=0)
with ap.block_inputs(sdlevent, priority=10):
with ap.block_input_events(sdlevent, priority=10):
sdlevent.dispatch(Event(KEYDOWN))
assert len(received) == 0
sdlevent.dispatch(Event(KEYDOWN))
Expand Down
Loading