Skip to content

Commit 88f0dbd

Browse files
edit examples
1 parent 57d4e80 commit 88f0dbd

10 files changed

+224
-309
lines changed

examples/06_full_blown_app_structure.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import asyncpygame as apg
1010
from asyncpygame.scene_switcher import SceneSwitcher, FadeTransition
1111
from _uix.touch_indicator import touch_indicator
12-
from _uix.anchor_layout import AnchorLayout
13-
from _uix.ripple_button import RippleButton
12+
from _uix.anchor_layout import anchor_layout
13+
from _uix.ripple_button import ripple_button
1414
from _uix.modal_dialog import ask_yes_no_question
1515

1616

@@ -47,17 +47,20 @@ async def title_scene(*, scene_switcher, userdata, **kwargs: Unpack[apg.CommonPa
4747
target_rect = draw_target.get_rect()
4848
font = userdata['font']
4949
async with apg.open_nursery() as nursery:
50-
AnchorLayout(
51-
nursery,
50+
e_start = apg.Event()
51+
s = nursery.start
52+
s(anchor_layout(
5253
font.render("<Your App Title>", True, "white", userdata["bgcolor"]).convert(draw_target),
5354
target_rect.scale_by(1.0, 0.5).move_to(y=target_rect.y),
54-
priority=0x100, **kwargs)
55-
start_button = RippleButton(
56-
nursery,
55+
priority=0x100,
56+
**kwargs))
57+
s(ripple_button(
5758
button_image := font.render("Start", True, "white").convert_alpha(),
5859
button_image.get_rect(center=target_rect.scale_by(1.0, 0.5).move_to(bottom=target_rect.bottom).center).inflate(80, 80),
59-
priority=0x100, **kwargs)
60-
await start_button.to_be_clicked()
60+
on_click=e_start.fire,
61+
priority=0x100,
62+
**kwargs))
63+
await e_start.wait()
6164
scene_switcher.switch_to(menu_scene, FadeTransition())
6265
await apg.sleep_forever()
6366

@@ -67,20 +70,20 @@ async def menu_scene(*, scene_switcher, userdata, **kwargs: Unpack[apg.CommonPar
6770
target_rect = draw_target.get_rect()
6871
font = userdata['font']
6972
async with apg.open_nursery() as nursery:
70-
play_button = RippleButton(
71-
nursery,
73+
e_play = apg.Event()
74+
e_back = apg.Event()
75+
s = nursery.start
76+
s(ripple_button(
7277
button_image := font.render("Play Game", True, "white").convert_alpha(),
7378
button_image.get_rect(center=target_rect.scale_by(1.0, 0.5).move_to(y=target_rect.y).center).inflate(80, 80),
74-
priority=0x100, **kwargs)
75-
back_button = RippleButton(
76-
nursery,
79+
on_click=e_play.fire,
80+
priority=0x100, **kwargs))
81+
s(ripple_button(
7782
button_image := font.render("Back to Title", True, "white").convert_alpha(),
7883
button_image.get_rect(center=target_rect.scale_by(1.0, 0.5).move_to(bottom=target_rect.bottom).center).inflate(80, 80),
79-
priority=0x100, **kwargs)
80-
tasks = await apg.wait_any(
81-
play_button.to_be_clicked(),
82-
back_button.to_be_clicked(),
83-
)
84+
on_click=e_back.fire,
85+
priority=0x100, **kwargs))
86+
tasks = await apg.wait_any(e_play.wait(), e_back.wait())
8487
next_scene = title_scene if tasks[1].finished else game_scene
8588
scene_switcher.switch_to(next_scene, FadeTransition())
8689
await apg.sleep_forever()

examples/_uix/anchor_layout.py

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,30 @@
1-
__all__ = ('AnchorLayout', )
1+
__all__ = ('anchor_layout', )
22

3-
from typing import Self
43
from functools import partial
54

6-
from asyncgui import Nursery, sleep_forever
5+
import asyncgui
76
from pygame import Surface, Rect
87

98

10-
class AnchorLayout:
9+
async def anchor_layout(
10+
image: Surface, dest: Rect, priority, *, anchor_image="center", anchor_dest="center",
11+
executor, draw_target, **__):
1112
'''
1213
.. code-block::
1314
1415
async with asyncpygame.open_nursery() as nursery:
1516
image = Surface(...)
1617
dest = Rect(...)
17-
layout = AnchorLayout(nursery, image, dest, **common_params)
1818
19-
# Aligns the right edge of the image with the right edge of the layout.
20-
layout.anchor_src = "right"
21-
layout.anchor_dest = "right"
22-
23-
# Aligns the center of the image with the midtop of the layout.
24-
layout.anchor_src = "center"
25-
layout.anchor_dest = "midtop"
26-
27-
# You can change its image anytime.
28-
layout.image = another_image
29-
30-
# You can move or resize the layout by updating the ``dest``.
31-
dest.right = ...
32-
dest.width = ...
33-
34-
# but you cannot assign another Rect instance to the layout.
35-
layout.dest = another_rect # NOT ALLOWED
19+
nursery.start(anchor_layout(image, dest, priority=..., **common_params))
3620
'''
37-
def __init__(self, owner: Nursery, image: Surface, dest: Rect,
38-
*, anchor_src="center", anchor_dest="center", **common_params):
39-
'''
40-
:param owner: AnchorLayout cannot outlive its owner. When the owner is closed, the sprite is destroyed.
41-
:param anchor_src: This must be any of the ``Rect``s positional attribute names. (e.g. "topleft", "bottomleft", ...)
42-
:param anchor_dest: Same as ``anchor_src``.
43-
'''
44-
self._dest = dest
45-
self.image = image
46-
self.anchor_src = anchor_src
47-
self.anchor_dest = anchor_dest
48-
self._main_task = owner.start(self._main(**common_params), daemon=True)
49-
50-
def kill(self):
51-
self._main_task.cancel()
21+
with executor.register(partial(_draw, draw_target.blit, image, image.get_rect(), dest, anchor_image, anchor_dest), priority):
22+
await asyncgui.sleep_forever()
5223

53-
@property
54-
def dest(self) -> Rect:
55-
return self._dest
5624

57-
async def _main(self, *, priority, draw_target, executor, **unused):
58-
with executor.register(partial(self._draw, draw_target.blit, self._dest, self), priority=priority):
59-
await sleep_forever()
25+
def _draw(getattr, setattr, blit, image, src: Rect, dest: Rect, anchor_image, anchor_dest):
26+
setattr(src, anchor_image, getattr(dest, anchor_dest))
27+
blit(image, src)
6028

61-
def _draw(getattr, blit, dest, self: Self):
62-
image = self.image
63-
blit(image, image.get_rect(**{self.anchor_src: getattr(dest, self.anchor_dest)}))
6429

65-
_draw = partial(_draw, getattr)
30+
_draw = partial(_draw, getattr, setattr)

examples/_uix/modal_dialog.py

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
from functools import partial
66

77
import asyncgui
8-
from pygame import Rect
8+
from pygame.colordict import THECOLORS
9+
from pygame import Rect, Surface
910
from pygame.font import SysFont
1011

1112
from asyncpygame import CommonParams, block_input_events, Clock
12-
from _uix.ripple_button import RippleButton
13-
from _uix.anchor_layout import AnchorLayout
13+
from _uix.ripple_button import ripple_button
14+
from _uix.anchor_layout import anchor_layout
1415

1516

1617
@asynccontextmanager
1718
async def darken(*, priority, **kwargs: Unpack[CommonParams]):
1819
interpolate = kwargs["clock"].interpolate_scalar
1920
draw_target = kwargs["draw_target"]
20-
overlay_surface = draw_target.copy()
21-
overlay_surface.fill("black")
21+
overlay_surface = Surface(draw_target.size)
2222
set_alpha = overlay_surface.set_alpha
2323
with kwargs["executor"].register(partial(draw_target.blit, overlay_surface), priority):
2424
async for v in interpolate(0, 180, duration=200):
@@ -28,22 +28,22 @@ async def darken(*, priority, **kwargs: Unpack[CommonParams]):
2828
set_alpha(v)
2929

3030

31-
async def translate_rects_vertically(clock: Clock, rects, movement, duration):
31+
async def move_rects_vertically(clock: Clock, rects, movement, duration):
3232
org_ys = tuple(rect.y for rect in rects)
3333
async for v in clock.interpolate_scalar(0, movement, duration=duration):
3434
for rect, org_y in zip(rects, org_ys):
3535
rect.y = org_y + v
3636

3737

3838
async def show_messagebox(
39-
message, *, dialog_size: Rect=None, font=None, text_ok='OK',
40-
priority, **kwargs: Unpack[CommonParams]) -> bool:
39+
message, priority, *, dialog_size: Rect=None, font=None, text_ok='OK',
40+
**kwargs: Unpack[CommonParams]) -> bool:
4141
'''
4242
.. code-block::
4343
4444
await show_messagebox("Hello World", priority=0xFFFFFA00, **kwargs)
4545
'''
46-
bgcolor = "grey90"
46+
bgcolor = THECOLORS["grey90"]
4747
clock = kwargs["clock"]
4848
draw_target = kwargs["draw_target"]
4949
if font is None:
@@ -56,33 +56,37 @@ async def show_messagebox(
5656
dialog_size = target_rect.inflate(-100, 0)
5757
dialog_size.height = dialog_size.width // 2
5858
dialog_dest = dialog_size.move_to(bottom=target_rect.top)
59+
e_ok = asyncgui.Event()
5960
with kwargs["executor"].register(partial(draw_target.fill, bgcolor, dialog_dest), priority=priority + 1):
60-
label = AnchorLayout(
61-
nursery,
61+
s = nursery.start
62+
s(anchor_layout(
6263
font.render(message, True, "black", bgcolor).convert(draw_target),
63-
dialog_dest.scale_by(1.0, 0.7).move_to(top=dialog_dest.top).inflate(-10, -10),
64-
priority=priority + 2, **kwargs)
65-
ok_button = RippleButton(
66-
nursery,
64+
label_dest := dialog_dest.scale_by(1.0, 0.7).move_to(top=dialog_dest.top).inflate(-10, -10),
65+
priority + 2,
66+
**kwargs), daemon=True)
67+
s(ripple_button(
6768
font.render(text_ok, True, "white"),
68-
dialog_dest.scale_by(0.5, 0.3).move_to(midbottom=dialog_dest.midbottom).inflate(-20, -20),
69-
priority=priority + 2, **kwargs)
70-
rects = (dialog_dest, label.dest, ok_button.dest, )
69+
button_dest := dialog_dest.scale_by(0.5, 0.3).move_to(midbottom=dialog_dest.midbottom).inflate(-20, -20),
70+
priority + 2,
71+
on_click=e_ok.fire,
72+
**kwargs), daemon=True)
73+
rects = (dialog_dest, label_dest, button_dest, )
7174
y_movement = target_rect.centery - dialog_dest.centery
72-
await translate_rects_vertically(clock, rects, y_movement, duration=200)
73-
await ok_button.to_be_clicked()
74-
await translate_rects_vertically(clock, rects, -y_movement, duration=200)
75+
await move_rects_vertically(clock, rects, y_movement, duration=200)
76+
await e_ok.wait()
77+
await move_rects_vertically(clock, rects, -y_movement, duration=200)
78+
return
7579

7680

7781
async def ask_yes_no_question(
78-
question, *, dialog_size: Rect=None, font=None, text_yes='Yes', text_no='No',
79-
priority, **kwargs: Unpack[CommonParams]) -> bool:
82+
question, priority, *, dialog_size: Rect=None, font=None, text_yes='Yes', text_no='No',
83+
**kwargs: Unpack[CommonParams]) -> bool:
8084
'''
8185
.. code-block::
8286
8387
result = await ask_yes_no_question("Do you like PyGame?", priority=0xFFFFFA00, **kwargs)
8488
'''
85-
bgcolor = "grey90"
89+
bgcolor = THECOLORS["grey90"]
8690
clock = kwargs["clock"]
8791
draw_target = kwargs["draw_target"]
8892
if font is None:
@@ -95,25 +99,30 @@ async def ask_yes_no_question(
9599
dialog_size = target_rect.inflate(-100, 0)
96100
dialog_size.height = dialog_size.width // 2
97101
dialog_dest = dialog_size.move_to(bottom=target_rect.top)
102+
e_yes = asyncgui.Event()
103+
e_no = asyncgui.Event()
98104
with kwargs["executor"].register(partial(draw_target.fill, bgcolor, dialog_dest), priority=priority + 1):
99-
label = AnchorLayout(
100-
nursery,
105+
s = nursery.start
106+
s(anchor_layout(
101107
font.render(question, True, "black", bgcolor).convert(draw_target),
102-
dialog_dest.scale_by(1.0, 0.5).move_to(top=dialog_dest.top).inflate(-10, -10),
103-
priority=priority + 2, **kwargs)
104-
yes_button = RippleButton(
105-
nursery,
108+
label_dest := dialog_dest.scale_by(1.0, 0.5).move_to(top=dialog_dest.top).inflate(-10, -10),
109+
priority + 2,
110+
**kwargs), daemon=True)
111+
s(ripple_button(
106112
font.render(text_yes, True, "white"),
107-
dialog_dest.scale_by(0.5, 0.5).move_to(bottomright=dialog_dest.bottomright).inflate(-20, -20),
108-
priority=priority + 2, **kwargs)
109-
no_button = RippleButton(
110-
nursery,
113+
yes_button_dest := dialog_dest.scale_by(0.5, 0.5).move_to(bottomright=dialog_dest.bottomright).inflate(-20, -20),
114+
priority + 2,
115+
on_click=e_yes.fire,
116+
**kwargs), daemon=True)
117+
s(ripple_button(
111118
font.render(text_no, True, "white"),
112-
dialog_dest.scale_by(0.5, 0.5).move_to(bottomleft=dialog_dest.bottomleft).inflate(-20, -20),
113-
priority=priority + 2, **kwargs)
114-
rects = (dialog_dest, label.dest, yes_button.dest, no_button.dest, )
119+
no_button_dest := dialog_dest.scale_by(0.5, 0.5).move_to(bottomleft=dialog_dest.bottomleft).inflate(-20, -20),
120+
priority + 2,
121+
on_click=e_no.fire,
122+
**kwargs), daemon=True)
123+
rects = (dialog_dest, label_dest, yes_button_dest, no_button_dest, )
115124
y_movement = target_rect.centery - dialog_dest.centery
116-
await translate_rects_vertically(clock, rects, y_movement, duration=200)
117-
tasks = await asyncgui.wait_any(yes_button.to_be_clicked(), no_button.to_be_clicked())
118-
await translate_rects_vertically(clock, rects, -y_movement, duration=200)
125+
await move_rects_vertically(clock, rects, y_movement, duration=200)
126+
tasks = await asyncgui.wait_any(e_yes.wait(), e_no.wait())
127+
await move_rects_vertically(clock, rects, -y_movement, duration=200)
119128
return tasks[0].finished

examples/_uix/progress_spinner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def _draw(pygame_draw_arc, draw_target, color, rect, line_width, self: Self):
2323
_draw = partial(_draw, pygame.draw.arc)
2424

2525

26-
async def progress_spinner(dest: Rect, *, color="white", line_width=20, min_arc_angle=0.3, speed=1.0, priority, **kwargs):
26+
async def progress_spinner(dest: Rect, priority, *, color="white", line_width=20, min_arc_angle=0.3, speed=1.0, **kwargs):
2727
R1 = 0.4
2828
R2 = math.tau - min_arc_angle * 2
2929
next_start = itertools.accumulate(itertools.cycle((R1, R1, R1 + R2, R1, )), initial=0).__next__

0 commit comments

Comments
 (0)