51
51
from digitalio import DigitalInOut
52
52
import displayio
53
53
import adafruit_touchscreen
54
+ from adafruit_cursorcontrol .cursorcontrol import Cursor
55
+ from adafruit_cursorcontrol .cursorcontrol_cursormanager import CursorManager
54
56
import audioio
55
57
from adafruit_display_text .label import Label
56
- from adafruit_bitmap_font import bitmap_font
57
58
from adafruit_button import Button
59
+ import terminalio
58
60
59
61
__version__ = "0.0.0-auto.0"
60
62
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PYOA.git"
@@ -74,20 +76,31 @@ def __init__(self):
74
76
75
77
self ._speaker_enable = DigitalInOut (board .SPEAKER_ENABLE )
76
78
self ._speaker_enable .switch_to_output (False )
77
- self .audio = audioio .AudioOut (board .AUDIO_OUT )
79
+ if hasattr (board , 'AUDIO_OUT' ):
80
+ self .audio = audioio .AudioOut (board .AUDIO_OUT )
81
+ elif hasattr (board , 'SPEAKER' ):
82
+ self .audio = audioio .AudioOut (board .SPEAKER )
83
+ else :
84
+ raise AttributeError ('Board does not have an audio output!' )
78
85
79
86
self ._background_file = None
80
87
self ._wavfile = None
81
88
82
89
board .DISPLAY .auto_brightness = False
83
90
self .backlight_fade (0 )
84
91
board .DISPLAY .show (self .root_group )
85
-
86
- self .touchscreen = adafruit_touchscreen .Touchscreen (board .TOUCH_XL , board .TOUCH_XR ,
87
- board .TOUCH_YD , board .TOUCH_YU ,
88
- calibration = ((5200 , 59000 ),
89
- (5800 , 57000 )),
90
- size = (320 , 240 ))
92
+ self .touchscreen = None
93
+ if hasattr (board , 'TOUCH_XL' ):
94
+ self .touchscreen = adafruit_touchscreen .Touchscreen (board .TOUCH_XL , board .TOUCH_XR ,
95
+ board .TOUCH_YD , board .TOUCH_YU ,
96
+ calibration = ((5200 , 59000 ),
97
+ (5800 , 57000 )),
98
+ size = (320 , 240 ))
99
+ elif hasattr (board , 'BUTTON_CLOCK' ):
100
+ self .mouse_cursor = Cursor (board .DISPLAY , display_group = self .root_group , cursor_speed = 8 )
101
+ self .cursor = CursorManager (self .mouse_cursor )
102
+ else :
103
+ raise AttributeError ('PYOA requires a touchscreen or cursor.' )
91
104
self ._gamedirectory = None
92
105
self ._gamefilename = None
93
106
self ._game = None
@@ -103,30 +116,32 @@ def load_game(self, game_directory):
103
116
"""Load a game.
104
117
105
118
:param game_directory: where the game files are stored
106
-
107
119
"""
108
120
self ._gamedirectory = game_directory
109
-
110
- self ._text_font = bitmap_font .load_font (game_directory + "/fonts/Arial-Bold-12.bdf" )
111
- #self._text_font = fontio.BuiltinFont
112
- try :
113
- glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. "\' ?!'
114
- print ("Preloading font glyphs:" , glyphs )
115
- self ._text_font .load_glyphs (glyphs )
116
- except AttributeError :
117
- pass # normal for built in font
118
-
119
- self ._left_button = Button (x = 10 , y = 195 , width = 120 , height = 40 ,
121
+ self ._text_font = terminalio .FONT
122
+ # Button Attributes
123
+ btn_left = 10
124
+ btn_right = btn_left + 180
125
+ btn_mid = btn_left + 90
126
+ button_y = 195
127
+ button_width = 120
128
+ button_height = 40
129
+ if board .DISPLAY .height < 200 :
130
+ button_y /= 2
131
+ button_y += 10
132
+ button_width /= 2
133
+ button_height /= 2
134
+ btn_right /= 2
135
+ btn_mid /= 2
136
+ self ._left_button = Button (x = int (btn_left ), y = int (button_y ), width = int (button_width ), height = int (button_height ),
120
137
label = "Left" , label_font = self ._text_font ,
121
138
style = Button .SHADOWROUNDRECT )
122
- self ._right_button = Button (x = 190 , y = 195 , width = 120 , height = 40 ,
139
+ self ._right_button = Button (x = int ( btn_right ) , y = int ( button_y ) , width = int ( button_width ) , height = int ( button_height ) ,
123
140
label = "Right" , label_font = self ._text_font ,
124
141
style = Button .SHADOWROUNDRECT )
125
- self ._middle_button = Button (x = 100 , y = 195 , width = 120 , height = 40 ,
142
+ self ._middle_button = Button (x = int ( btn_mid ) , y = int ( button_y ) , width = int ( button_width ) , height = int ( button_height ) ,
126
143
label = "Middle" , label_font = self ._text_font ,
127
144
style = Button .SHADOWROUNDRECT )
128
-
129
-
130
145
self ._gamefilename = game_directory + "/cyoa.json"
131
146
try :
132
147
game_file = open (self ._gamefilename , "r" )
@@ -137,18 +152,19 @@ def load_game(self, game_directory):
137
152
138
153
def _fade_to_black (self ):
139
154
"""Turn down the lights."""
155
+ self .mouse_cursor .is_hidden = True
140
156
self .backlight_fade (0 )
141
157
# turn off background so we can render the text
142
158
self .set_background (None , with_fade = False )
143
159
self .set_text (None , None )
144
160
for _ in range (len (self ._button_group )):
145
161
self ._button_group .pop ()
162
+ self .mouse_cursor .is_hidden = False
146
163
147
164
def _display_buttons (self , card ):
148
165
"""Display the buttons of a card.
149
166
150
167
:param card: The active card
151
-
152
168
"""
153
169
button01_text = card .get ('button01_text' , None )
154
170
button02_text = card .get ('button02_text' , None )
@@ -166,15 +182,13 @@ def _display_background_for(self, card):
166
182
"""If there's a background on card, display it.
167
183
168
184
:param card: The active card
169
-
170
185
"""
171
186
self .set_background (card .get ('background_image' , None ), with_fade = False )
172
187
173
188
def _display_text_for (self , card ):
174
189
"""Display the main text of a card.
175
190
176
191
:param card: The active card
177
-
178
192
"""
179
193
text = card .get ('text' , None )
180
194
text_color = card .get ('text_color' , 0x0 ) # default to black
@@ -189,7 +203,6 @@ def _play_sound_for(self, card):
189
203
"""If there's a sound, start playing it.
190
204
191
205
:param card: The active card
192
-
193
206
"""
194
207
sound = card .get ('sound' , None )
195
208
loop = card .get ('sound_repeat' , False )
@@ -207,9 +220,15 @@ def _wait_for_press(self, card):
207
220
"""
208
221
button01_text = card .get ('button01_text' , None )
209
222
button02_text = card .get ('button02_text' , None )
223
+ point_touched = None
210
224
while True :
211
- point_touched = self .touchscreen .touch_point
212
- if point_touched :
225
+ if self .touchscreen is not None :
226
+ point_touched = self .touchscreen .touch_point
227
+ else :
228
+ self .cursor .update ()
229
+ if self .cursor .is_clicked is True :
230
+ point_touched = self .mouse_cursor .x , self .mouse_cursor .y
231
+ if point_touched is not None :
213
232
print ("touch: " , point_touched )
214
233
if button01_text and not button02_text :
215
234
# showing only middle button
@@ -223,12 +242,12 @@ def _wait_for_press(self, card):
223
242
if self ._right_button .contains (point_touched ):
224
243
print ("Right button" )
225
244
return card .get ('button02_goto_card_id' , None )
245
+ time .sleep (0.1 )
226
246
227
247
def display_card (self , card_num ):
228
248
"""Display and handle input on a card.
229
249
230
250
:param card_num: the index of the card to process
231
-
232
251
"""
233
252
card = self ._game [card_num ]
234
253
print (card )
@@ -269,7 +288,6 @@ def play_sound(self, filename, *, wait_to_finish=True, loop=False):
269
288
:param filename: The filename of the sound to play
270
289
:param wait_to_finish: Whether playing the sound should block
271
290
:param loop: Whether the sound should loop
272
-
273
291
"""
274
292
self ._speaker_enable .value = False
275
293
self .audio .stop ()
@@ -309,21 +327,28 @@ def set_text(self, text, color):
309
327
self ._text_group .pop ()
310
328
if not text or not color :
311
329
return # nothing to do!
312
- text = self .wrap_nicely (text , 37 )
330
+ text_wrap = 37
331
+ if board .DISPLAY .height < 130 :
332
+ text_wrap = 25
333
+ text = self .wrap_nicely (text , text_wrap )
313
334
text = '\n ' .join (text )
314
335
print ("Set text to" , text , "with color" , hex (color ))
336
+ text_x = 8
337
+ text_y = 95
338
+ if board .DISPLAY .height < 130 :
339
+ text_y = 38
340
+ text_x = 3
315
341
if text :
316
342
self ._text = Label (self ._text_font , text = str (text ))
317
- self ._text .x = 10
318
- self ._text .y = 100
343
+ self ._text .x = text_x
344
+ self ._text .y = text_y
319
345
self ._text .color = color
320
346
self ._text_group .append (self ._text )
321
347
322
348
def set_background (self , filename , * , with_fade = True ):
323
349
"""The background image to a bitmap file.
324
350
325
351
:param filename: The filename of the chosen background
326
-
327
352
"""
328
353
print ("Set background to" , filename )
329
354
if with_fade :
0 commit comments