48
48
from adafruit_button import Button
49
49
import terminalio
50
50
51
+ try :
52
+ from typing import Dict , Optional , List
53
+ except ImportError :
54
+ pass
55
+
51
56
__version__ = "0.0.0-auto.0"
52
57
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PYOA.git"
53
58
@@ -56,7 +61,7 @@ class PYOA_Graphics:
56
61
# pylint: disable=too-many-instance-attributes
57
62
"""A choose your own adventure game framework."""
58
63
59
- def __init__ (self ):
64
+ def __init__ (self ) -> None :
60
65
self .root_group = displayio .Group ()
61
66
self ._display = board .DISPLAY
62
67
self ._background_group = displayio .Group ()
@@ -113,7 +118,7 @@ def __init__(self):
113
118
self ._right_button = None
114
119
self ._middle_button = None
115
120
116
- def load_game (self , game_directory ) :
121
+ def load_game (self , game_directory : str ) -> None :
117
122
"""Load a game.
118
123
119
124
:param str game_directory: where the game files are stored
@@ -183,7 +188,7 @@ def load_game(self, game_directory):
183
188
except OSError as err :
184
189
raise OSError ("Could not open game file " + self ._gamefilename ) from err
185
190
186
- def _fade_to_black (self ):
191
+ def _fade_to_black (self ) -> None :
187
192
"""Turn down the lights."""
188
193
if self .mouse_cursor :
189
194
self .mouse_cursor .is_hidden = True
@@ -196,10 +201,11 @@ def _fade_to_black(self):
196
201
if self .mouse_cursor :
197
202
self .mouse_cursor .is_hidden = False
198
203
199
- def _display_buttons (self , card ) :
204
+ def _display_buttons (self , card : Dict [ str , str ]) -> None :
200
205
"""Display the buttons of a card.
201
206
202
207
:param card: The active card
208
+ :type card: dict(str, str)
203
209
"""
204
210
button01_text = card .get ("button01_text" , None )
205
211
button02_text = card .get ("button02_text" , None )
@@ -213,17 +219,19 @@ def _display_buttons(self, card):
213
219
self ._button_group .append (self ._right_button )
214
220
self ._button_group .append (self ._left_button )
215
221
216
- def _display_background_for (self , card ) :
222
+ def _display_background_for (self , card : Dict [ str , str ]) -> None :
217
223
"""If there's a background on card, display it.
218
224
219
225
:param card: The active card
226
+ :type card: dict(str, str)
220
227
"""
221
228
self .set_background (card .get ("background_image" , None ), with_fade = False )
222
229
223
- def _display_text_for (self , card ) :
230
+ def _display_text_for (self , card : Dict [ str , str ]) -> None :
224
231
"""Display the main text of a card.
225
232
226
233
:param card: The active card
234
+ :type card: dict(str, str)
227
235
"""
228
236
text = card .get ("text" , None )
229
237
text_color = card .get ("text_color" , 0x0 ) # default to black
@@ -245,10 +253,11 @@ def _display_text_for(self, card):
245
253
246
254
self .set_text (text , text_color , background_color = text_background_color )
247
255
248
- def _play_sound_for (self , card ) :
256
+ def _play_sound_for (self , card : Dict [ str , str ]) -> None :
249
257
"""If there's a sound, start playing it.
250
258
251
259
:param card: The active card
260
+ :type card: dict(str, str)
252
261
"""
253
262
sound = card .get ("sound" , None )
254
263
loop = card .get ("sound_repeat" , False )
@@ -257,12 +266,13 @@ def _play_sound_for(self, card):
257
266
print ("Loop:" , loop )
258
267
self .play_sound (sound , wait_to_finish = False , loop = loop )
259
268
260
- def _wait_for_press (self , card ) :
269
+ def _wait_for_press (self , card : Dict [ str , str ]) -> str :
261
270
"""Wait for a button to be pressed.
262
271
263
272
:param card: The active card
264
-
265
- Return the id of the destination card.
273
+ :type card: dict(str, str)
274
+ :return: The id of the destination card
275
+ :rtype: str
266
276
"""
267
277
button01_text = card .get ("button01_text" , None )
268
278
button02_text = card .get ("button02_text" , None )
@@ -294,10 +304,12 @@ def _wait_for_press(self, card):
294
304
return card .get ("button02_goto_card_id" , None )
295
305
time .sleep (0.1 )
296
306
297
- def display_card (self , card_num ) :
307
+ def display_card (self , card_num : int ) -> int :
298
308
"""Display and handle input on a card.
299
309
300
310
:param int card_num: the index of the card to process
311
+ :return: the card number of the selected card
312
+ :rtype: int
301
313
"""
302
314
card = self ._game [card_num ]
303
315
print (card )
@@ -332,11 +344,18 @@ def display_card(self, card_num):
332
344
"Could not find card with matching 'card_id': " , destination_card_id
333
345
)
334
346
335
- def play_sound (self , filename , * , wait_to_finish = True , loop = False ):
347
+ def play_sound (
348
+ self ,
349
+ filename : Optional [str ],
350
+ * ,
351
+ wait_to_finish : bool = True ,
352
+ loop : bool = False
353
+ ) -> None :
336
354
"""Play a sound
337
355
338
- :param Union(None,str) filename: The filename of the sound to play. Use `None` to stop
356
+ :param filename: The filename of the sound to play. Use `None` to stop
339
357
playing anything.
358
+ :type filename: str or None
340
359
:param bool wait_to_finish: Whether playing the sound should block
341
360
:param bool loop: Whether the sound should loop
342
361
"""
@@ -367,12 +386,20 @@ def play_sound(self, filename, *, wait_to_finish=True, loop=False):
367
386
self ._wavfile = None
368
387
self ._speaker_enable .value = False
369
388
370
- def set_text (self , text , color , background_color = None ):
389
+ def set_text (
390
+ self ,
391
+ text : Optional [str ],
392
+ color : Optional [str ],
393
+ background_color : Optional [int ] = None ,
394
+ ) -> None :
371
395
"""Display the test for a card.
372
396
373
- :param Union(None,str) text: the text to display
374
- :param Union(None,int) color: the text color
375
-
397
+ :param text: the text to display
398
+ :type text: str or None
399
+ :param color: the text color
400
+ :type color: str or None
401
+ :param background_color: the background color of the text
402
+ :type background_color: int or None
376
403
"""
377
404
if self ._text_group :
378
405
self ._text_group .pop ()
@@ -400,10 +427,13 @@ def set_text(self, text, color, background_color=None):
400
427
self ._text .background_color = background_color
401
428
self ._text_group .append (self ._text )
402
429
403
- def set_background (self , filename , * , with_fade = True ):
430
+ def set_background (
431
+ self , filename : Optional [str ], * , with_fade : bool = True
432
+ ) -> None :
404
433
"""The background image to a bitmap file.
405
434
406
- :param Union(None,str) filename: The filename of the chosen background
435
+ :param filename: The filename of the chosen background
436
+ :type filename: str or None
407
437
:param bool with_fade: If `True` fade out the backlight while loading the new background
408
438
and fade in the backlight once completed.
409
439
"""
@@ -424,7 +454,7 @@ def set_background(self, filename, *, with_fade=True):
424
454
self ._display .refresh (target_frames_per_second = 60 )
425
455
self .backlight_fade (1.0 )
426
456
427
- def backlight_fade (self , to_light ) :
457
+ def backlight_fade (self , to_light : float ) -> None :
428
458
"""
429
459
Adjust the TFT backlight. Fade from the current value to the ``to_light`` value
430
460
@@ -444,12 +474,13 @@ def backlight_fade(self, to_light):
444
474
445
475
# return a list of lines with wordwrapping
446
476
@staticmethod
447
- def wrap_nicely (string , max_chars ) :
477
+ def wrap_nicely (string : str , max_chars : int ) -> List [ str ] :
448
478
"""A helper that will return a list of lines with word-break wrapping.
449
479
450
480
:param str string: The text to be wrapped.
451
481
:param int max_chars: The maximum number of characters on a line before wrapping.
452
-
482
+ :return: The list of lines
483
+ :rtype: list(str)
453
484
"""
454
485
# string = string.replace('\n', '').replace('\r', '') # strip confusing newlines
455
486
words = string .split (" " )
0 commit comments