-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5432a99
commit d91fa24
Showing
14 changed files
with
173 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,6 @@ | ||
__all__ = ( | ||
'DEFAULT_PRIORITY', 'STOP_DISPATCHING', | ||
'Timer', 'sleep', 'move_on_after', | ||
'anim_with_dt', 'anim_with_dt_et', 'anim_with_dt_et_ratio', 'anim_with_et', 'anim_with_ratio', | ||
'PriorityDispatcher', | ||
'sdl_event', 'sdl_event_repeatedly', | ||
'run_in_thread', 'run_in_executor', | ||
# 'PrefilledAPIs', | ||
) | ||
|
||
from asyncgui import * | ||
from .constants import DEFAULT_PRIORITY, STOP_DISPATCHING | ||
from ._timer import Timer, sleep, move_on_after | ||
from ._animation import ( | ||
anim_with_dt, anim_with_dt_et, anim_with_dt_et_ratio, anim_with_et, anim_with_ratio, | ||
) | ||
from ._priority_dispatcher import PriorityDispatcher | ||
from ._sdl_event import sdl_event, sdl_event_repeatedly | ||
from ._threads import run_in_thread, run_in_executor | ||
# from ._pre_filled_apis import PrefilledAPIs |
Empty file.
2 changes: 1 addition & 1 deletion
2
src/asyncpygame/_animation.py → src/asyncpygame/_lowlevel/_animation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from typing import TypeAlias | ||
from collections.abc import Callable | ||
from dataclasses import dataclass | ||
from heapq import merge as heapq_merge | ||
|
||
from pygame.surface import Surface | ||
|
||
from ..constants import DEFAULT_PRIORITY | ||
|
||
|
||
DrawFunc: TypeAlias = Callable[[Surface], None] | ||
|
||
|
||
@dataclass(slots=True) | ||
class Drawer: | ||
_priority: int | ||
|
||
callback: DrawFunc | ||
''' | ||
The callback function registered using the :meth:`PrioritizedDrawing.add_drawer` call that returned this | ||
instance. You can replace it with another one by simply assigning to this attribute. | ||
.. code-block:: | ||
dr = priority_drawing.add_drawer(...) | ||
dr.callback = another_function | ||
''' | ||
|
||
_cancelled: bool = False | ||
|
||
def cancel(self): | ||
self._cancelled = True | ||
|
||
def __eq__(self, other): | ||
return self._priority == other._priority | ||
|
||
def __ne__(self, other): | ||
return self._priority != other._priority | ||
|
||
def __lt__(self, other): | ||
return self._priority < other._priority | ||
|
||
def __le__(self, other): | ||
return self._priority <= other._priority | ||
|
||
def __gt__(self, other): | ||
return self._priority > other._priority | ||
|
||
def __ge__(self, other): | ||
return self._priority >= other._priority | ||
|
||
|
||
class PrioritizedDrawing: | ||
__slots__ = ('_drs', '_drs_2', '_drs_to_be_added', '_drs_to_be_added_2', ) | ||
|
||
def __init__(self): | ||
self._drs: list[Drawer] = [] | ||
self._drs_2: list[Drawer] = [] # double buffering | ||
self._drs_to_be_added: list[Drawer] = [] | ||
self._drs_to_be_added_2: list[Drawer] = [] # double buffering | ||
|
||
def draw(self, draw_target: Surface): | ||
drs = self._drs | ||
drs_tba = self._drs_to_be_added | ||
if drs_tba: | ||
drs_tba.sort() | ||
dr_iter = heapq_merge(drs, drs_tba) | ||
drs_tba2 = self._drs_to_be_added_2 | ||
drs_tba2.clear() | ||
self._drs_to_be_added = drs_tba2 | ||
self._drs_to_be_added_2 = drs_tba | ||
del drs_tba2 | ||
else: | ||
dr_iter = iter(drs) | ||
del drs_tba | ||
|
||
drs2 = self._drs_2 | ||
drs2_append = drs2.append | ||
try: | ||
for dr in dr_iter: | ||
if dr._cancelled: | ||
continue | ||
drs2_append(dr) | ||
dr.callback(draw_target) | ||
finally: | ||
drs.clear() | ||
self._drs = drs2 | ||
self._drs_2 = drs | ||
|
||
def add_drawer(self, func: DrawFunc, priority=DEFAULT_PRIORITY) -> Drawer: | ||
dr = Drawer(priority, func) | ||
self._drs_to_be_added.append(dr) | ||
return dr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.