22# A concrete example exploiting the possibilities of Visual-dialog.
33
44import curses
5+ from functools import partial
56
67from visualdialog import DialogBox
78
89
9- PASS_KEYS = (" " , "\n " )
10- HEIGHT , WIDTH = 35 , 5
10+ pass_keys = (" " , "\n " )
11+ height , width = 35 , 5
1112
1213
1314# It is preferable to create its own class derived from DialogBox for
14- # complex applications.
15- class CustomDialogBox (DialogBox ):
16-
17- def __init__ (self ,
18- win ,
19- pos_x : int ,
20- pos_y : int ,
21- title : str ,
22- title_colors_pair_nb : int ,
23- ** kwargs ):
24- DialogBox .__init__ (self ,
25- pos_x ,
26- pos_y ,
27- HEIGHT ,
28- WIDTH ,
29- title ,
30- title_colors_pair_nb ,
31- global_win = win ,
32- # Use a default window to display text.
33- # Setting this parameter allows to avoid passing
34- # `win` parameter to `char_by_char` and
35- # `word_by_word` methods. Useful when dealing with
36- # many `DialogBox` methods calls.
37- ** kwargs )
38-
39- # Definition of accepted key codes to pass a dialog.
40- self .confirm_keys = PASS_KEYS
15+ # complex applications (or an instance factory like here).
16+ def box_factory (win ,
17+ x : int ,
18+ y : int ,
19+ title : int ,
20+ title_colors_pair_nb : int ,
21+ ** kwargs ) -> DialogBox :
22+ box = DialogBox (x , y ,
23+ height , width ,
24+ title , title_colors_pair_nb ,
25+ global_win = win ,
26+ # Use a default window to display text.
27+ # Setting this parameter allows to avoid passing `win`
28+ # parameter to `char_by_char` and `word_by_word` methods.
29+ # Useful when dealing with many `DialogBox` methods calls.
30+ ** kwargs )
31+
32+ # Definition of accepted key codes to pass a dialog.
33+ box .confirm_keys = pass_keys
34+ # Definition of a partial objet to reduce verbosity and accelerate
35+ # the time it takes to write the text on the screen.
36+ box .char_by_char = partial (box .char_by_char , delay = 30 )
37+
38+ return box
4139
4240
4341def main (win ):
@@ -52,24 +50,24 @@ def main(win):
5250 max_y , max_x = win .getmaxyx () # Get height and width of the window.
5351
5452 left_x = 2 # Left alignment.
55- right_x = max_x - HEIGHT - 4 # Calculation of right alignment.
56- center_x = max_x // 2 - HEIGHT // 2 # Calculation of center alignment.
57- bottom_y = max_y - WIDTH - 4 # Calculation of bottom alignment.
58-
59- phoenix_wright = CustomDialogBox (win ,
60- left_x , bottom_y ,
61- "Phoenix" , # Title of dialog box.
62- 1 ) # Color pair used to colored title.
63-
64- april_may = CustomDialogBox (win ,
65- center_x , bottom_y ,
66- "April" ,
67- 2 )
68-
69- miles_edgeworth = CustomDialogBox (win ,
70- right_x , bottom_y ,
71- "Edgeworth" ,
72- 3 )
53+ right_x = max_x - height - 4 # Calculation of right alignment.
54+ center_x = max_x // 2 - height // 2 # Calculation of center alignment.
55+ bottom_y = max_y - width - 4 # Calculation of bottom alignment.
56+
57+ phoenix_wright = box_factory (win ,
58+ left_x , bottom_y ,
59+ "Phoenix" , # Title of dialog box.
60+ 1 ) # Color pair used to colored title.
61+
62+ april_may = box_factory (win ,
63+ center_x , bottom_y ,
64+ "April" ,
65+ 2 )
66+
67+ miles_edgeworth = box_factory (win ,
68+ right_x , bottom_y ,
69+ "Edgeworth" ,
70+ 3 )
7371
7472 phoenix_wright .char_by_char ("This testimony is a pure invention !" ,
7573 delay = 30 )
0 commit comments