diff --git a/examples/_uix/touch_indicator.py b/examples/_uix/touch_indicator.py index 175e659..2bfbcb9 100644 --- a/examples/_uix/touch_indicator.py +++ b/examples/_uix/touch_indicator.py @@ -4,35 +4,26 @@ from typing import Unpack import pygame -from pygame import Color, Event +from pygame import Color, Event, Surface import pygame.constants as C import asyncpygame as apg -class Ring: - __slots__ = ('draw', 'pos', ) - - def __init__(self, draw_target: pygame.Surface, color, initial_pos, radius, line_width): - ring_img = pygame.Surface((radius * 2, radius * 2)).convert(draw_target) - color = Color(color) - bgcolor = Color("black") - if color == bgcolor: - bgcolor = Color("white") - ring_img.fill(bgcolor) - ring_img.set_colorkey(bgcolor) - pygame.draw.circle(ring_img, color, (radius, radius), radius, line_width) - - self.draw = partial(self.__class__._draw, self, draw_target.blit, ring_img, ring_img.get_rect()) - self.pos = initial_pos - - def _draw(self, blit, ring_img, ring_dest): - ring_dest.center = self.pos - blit(ring_img, ring_dest) +def generate_ring_image(color, radius, line_width) -> Surface: + ring_img = Surface((radius * 2, radius * 2)) + color = Color(color) + bgcolor = Color("black") + if color == bgcolor: + bgcolor = Color("white") + ring_img.fill(bgcolor) + pygame.draw.circle(ring_img, color, (radius, radius), radius, line_width) + ring_img = ring_img.convert() + ring_img.set_colorkey(bgcolor) + return ring_img async def touch_indicator(*, color="white", radius=60, line_width=4, priority, **kwargs: Unpack[apg.CommonParams]): - color = Color(color) - draw_target = kwargs["draw_target"] + ring_img = generate_ring_image(color, radius, line_width) async with ( apg.open_nursery() as nursery, kwargs["sdlevent"].wait_freq( @@ -40,32 +31,37 @@ async def touch_indicator(*, color="white", radius=60, line_width=4, priority, * filter=lambda e: not getattr(e, 'touch', False) ) as touch_down, ): + funcs = (draw_ring_under_finger, draw_ring_under_mouse_cursor, ) while True: e_down = await touch_down() - if e_down.type == C.MOUSEBUTTONDOWN: - f = draw_ring_under_mouse_cursor - else: - f = draw_ring_under_finger - nursery.start(f(e_down, priority=priority, ring=Ring(draw_target, color, e_down.pos, radius, line_width), **kwargs)) + nursery.start(funcs[e_down.type == C.MOUSEBUTTONDOWN](ring_img, e_down, priority=priority, **kwargs)) + +def on_mouse_motion(dest, e: Event): + dest.center = e.pos -async def draw_ring_under_mouse_cursor(e_down: Event, *, priority, executor, sdlevent, ring, **__): - with executor.register(ring.draw, priority): - async with ( - apg.move_on_when(sdlevent.wait(C.MOUSEBUTTONUP, filter=lambda e: e.button == e_down.button, priority=priority)), - sdlevent.wait_freq(C.MOUSEMOTION, priority=priority) as mouse_motion, - ): - while True: - e = await mouse_motion() - ring.pos = e.pos +async def draw_ring_under_mouse_cursor( + ring_img: Surface, e_down: Event, *, priority, executor, sdlevent, draw_target, **__): + dest = ring_img.get_rect(center=e_down.pos) + with ( + executor.register(partial(draw_target.blit, ring_img, dest), priority), + sdlevent.subscribe((C.MOUSEMOTION, ), partial(on_mouse_motion, dest), priority), -async def draw_ring_under_finger(e_down: Event, *, priority, executor, sdlevent, ring, **__): - with executor.register(ring.draw, priority): - async with ( - apg.move_on_when(sdlevent.wait(C.FINGERUP, filter=lambda e: e.finger_id == e_down.finger_id, priority=priority)), - sdlevent.wait_freq(C.FINGERMOTION, filter=lambda e: e.finger_id == e_down.finger_id, priority=priority) as finger_motion, - ): - while True: - e = await finger_motion() - ring.pos = e.pos + ): + await sdlevent.wait(C.MOUSEBUTTONUP, filter=lambda e: e.button == e_down.button, priority=priority) + + +def on_finger_motion(finger_id, dest, e: Event): + if finger_id == e.finger_id: + dest.center = e.pos + + +async def draw_ring_under_finger( + ring_img: Surface, e_down: Event, *, priority, executor, sdlevent, draw_target, **__): + dest = ring_img.get_rect(center=e_down.pos) + with ( + executor.register(partial(draw_target.blit, ring_img, dest), priority), + sdlevent.subscribe((C.FINGERMOTION, ), partial(on_finger_motion, e_down.finger_id, dest), priority), + ): + await sdlevent.wait(C.FINGERUP, filter=lambda e: e.finger_id == e_down.finger_id, priority=priority) diff --git a/poetry.lock b/poetry.lock index 9bd62d7..6dae9a7 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1327,4 +1327,4 @@ zstd = ["zstandard (>=0.18.0)"] [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "642dbc638f9c9ccb07f2dc29593df1d81273c3b45457b433cee1d8fae03fa327" +content-hash = "1a24153e0faab6f82166da36b5660f4b3d350fc54006bb64d08d770b4ea4406a" diff --git a/pyproject.toml b/pyproject.toml index 0735315..0e4e08a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,6 +44,9 @@ matplotlib = "^3.9.2" [tool.poetry.group.recording.dependencies] numpy = "^2.1.2" +[tool.poetry.group.example.dependencies] +requests = "^2.32.3" + [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api"