55from functools import partial
66
77import asyncgui
8- from pygame import Rect
8+ from pygame .colordict import THECOLORS
9+ from pygame import Rect , Surface
910from pygame .font import SysFont
1011
1112from 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
1718async 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
3838async 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
7781async 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
0 commit comments