44
44
LOADING_IMAGE = "loading.png"
45
45
BUTTON_BACK_IMAGE = "button_back.png"
46
46
BUTTON_NEXT_IMAGE = "button_next.png"
47
+ BUTTON_NEW_IMAGE = "button_new.png"
47
48
48
49
# Asset Paths
49
50
IMAGES_PATH = os .path .dirname (sys .argv [0 ]) + "images/"
@@ -167,10 +168,9 @@ def __init__(self, rotation=0):
167
168
self .rotation = rotation
168
169
self .images = {}
169
170
self .fonts = {}
171
+ self .buttons = {}
170
172
self .width = 0
171
173
self .height = 0
172
- self .back_button = None
173
- self .next_button = None
174
174
self .textarea = None
175
175
self .screen = None
176
176
self .saved_screen = None
@@ -224,23 +224,37 @@ def start(self):
224
224
# Add buttons
225
225
back_button_image = pygame .image .load (IMAGES_PATH + BUTTON_BACK_IMAGE )
226
226
next_button_image = pygame .image .load (IMAGES_PATH + BUTTON_NEXT_IMAGE )
227
+ new_button_image = pygame .image .load (IMAGES_PATH + BUTTON_NEW_IMAGE )
227
228
button_spacing = (
228
- self .width - (back_button_image .get_width () + next_button_image .get_width ())
229
- ) // 3
229
+ self .width - (back_button_image .get_width () + next_button_image .get_width () + new_button_image . get_width () )
230
+ ) // 4
230
231
button_ypos = (
231
232
self .height
232
233
- PAGE_NAV_HEIGHT
233
234
+ (PAGE_NAV_HEIGHT - next_button_image .get_height ()) // 2
234
235
)
235
- self .back_button = Button (
236
+
237
+ self ._load_button (
238
+ "back" ,
236
239
button_spacing ,
237
240
button_ypos ,
238
241
back_button_image ,
239
242
self .previous_page ,
240
243
self ._display_surface ,
241
244
)
242
- self .next_button = Button (
243
- self .width - button_spacing - next_button_image .get_width (),
245
+
246
+ self ._load_button (
247
+ "new" ,
248
+ button_spacing * 2 + back_button_image .get_width (),
249
+ button_ypos ,
250
+ new_button_image ,
251
+ self .new_story ,
252
+ self ._display_surface ,
253
+ )
254
+
255
+ self ._load_button (
256
+ "next" ,
257
+ button_spacing * 3 + back_button_image .get_width () + new_button_image .get_width (),
244
258
button_ypos ,
245
259
next_button_image ,
246
260
self .next_page ,
@@ -255,8 +269,6 @@ def start(self):
255
269
self .height - PAGE_NAV_HEIGHT - PAGE_TOP_MARGIN - PAGE_BOTTOM_MARGIN ,
256
270
)
257
271
258
- self .load_settings ()
259
-
260
272
# Start the sleep check thread after everything is initialized
261
273
self ._sleep_check_thread = threading .Thread (target = self ._handle_sleep )
262
274
self ._sleep_check_thread .start ()
@@ -309,7 +321,7 @@ def _handle_mousedown_event(self, event):
309
321
if event .button == 1 :
310
322
# If button pressed while visible, trigger action
311
323
coords = self ._rotate_mouse_pos (event .pos )
312
- for button in [ self .back_button , self . next_button ] :
324
+ for button in self .buttons . values () :
313
325
if button .visible and button .is_in_bounds (coords ):
314
326
button .action ()
315
327
@@ -334,6 +346,9 @@ def _load_image(self, name, filename):
334
346
except pygame .error :
335
347
pass
336
348
349
+ def _load_button (self , name , x , y , image , action , display_surface ):
350
+ self .buttons [name ] = Button (x , y , image , action , display_surface )
351
+
337
352
def _load_font (self , name , details ):
338
353
self .fonts [name ] = pygame .font .Font (details [0 ], details [1 ])
339
354
@@ -369,6 +384,7 @@ def display_current_page(self):
369
384
self ._display_surface (self .images ["background" ], 0 , 0 )
370
385
pygame .display .update ()
371
386
387
+ print (f"Loading page { self .page } of { len (self .pages )} " )
372
388
page_data = self .pages [self .page ]
373
389
374
390
# Display the title
@@ -385,8 +401,9 @@ def display_current_page(self):
385
401
386
402
# Display the navigation buttons
387
403
if self .page > 0 or self .story > 0 :
388
- self .back_button .show ()
389
- self .next_button .show ()
404
+ self .buttons ["back" ].show ()
405
+ self .buttons ["next" ].show ()
406
+ self .buttons ["new" ].show ()
390
407
pygame .display .update ()
391
408
self ._busy = False
392
409
@@ -463,7 +480,11 @@ def next_page(self):
463
480
self .load_story (self .stories [self .story ])
464
481
self .page = 0
465
482
else :
466
- self .new_story ()
483
+ self .generate_new_story ()
484
+ self .display_current_page ()
485
+
486
+ def new_story (self ):
487
+ self .generate_new_story ()
467
488
self .display_current_page ()
468
489
469
490
def display_loading (self ):
@@ -485,10 +506,13 @@ def load_story(self, story):
485
506
# Parse out the title and story and render into pages
486
507
self ._busy = True
487
508
self .pages = []
488
- title = story .split ("Title: " )[1 ].split ("\n \n " )[0 ]
509
+ if not story .startswith ("Title: " ):
510
+ print ("Unexpected story format from ChatGPT. Missing Title." )
511
+ title = "A Story"
512
+ else :
513
+ title = story .split ("Title: " )[1 ].split ("\n \n " )[0 ]
489
514
page = self ._add_page (title )
490
515
paragraphs = story .split ("\n \n " )[1 :]
491
- paragraphs .append ("The End." )
492
516
for paragraph in paragraphs :
493
517
lines = self ._wrap_text (paragraph , self .fonts ["text" ], self .textarea .width )
494
518
for line in lines :
@@ -524,47 +548,9 @@ def _add_page(self, title=None):
524
548
self .pages .append (page )
525
549
return page
526
550
527
- def load_settings (self ):
528
- storydata = {
529
- "history" : [],
530
- "settings" : {
531
- "story" : 0 ,
532
- "page" : 0 ,
533
- },
534
- }
535
- # Load the story data if it exists
536
- if os .path .exists (os .path .dirname (sys .argv [0 ]) + "storydata.bin" ):
537
- print ("Loading previous story data" )
538
- with open (os .path .dirname (sys .argv [0 ]) + "storydata.bin" , "rb" ) as f :
539
- storydata = pickle .load (f )
540
- self .stories = storydata ["history" ]
541
- self .story = storydata ["settings" ]["story" ]
542
-
543
- if storydata ["history" ] and storydata ["settings" ]["story" ] < len (
544
- storydata ["history" ]
545
- ):
546
- # Load the last story
547
- self .load_story (storydata ["history" ][storydata ["settings" ]["story" ]])
548
- self .page = storydata ["settings" ]["page" ]
549
- # If something changed and caused the current page to be too
550
- # large, just go to the last page of the story
551
- if self .page >= len (self .pages ):
552
- self .page = len (self .pages ) - 1
553
-
554
- def save_settings (self ):
555
- storydata = {
556
- "history" : self .stories ,
557
- "settings" : {
558
- "story" : self .story ,
559
- "page" : self .page ,
560
- },
561
- }
562
- with open (os .path .dirname (sys .argv [0 ]) + "storydata.bin" , "wb" ) as f :
563
- pickle .dump (storydata , f )
564
-
565
- def new_story (self ):
551
+ def generate_new_story (self ):
566
552
self ._busy = True
567
- self .display_message ("What story would you like to hear today? " )
553
+ self .display_message ("Speak aloud the story you wish to read. " )
568
554
569
555
if self ._sleep_request :
570
556
self ._busy = False
@@ -604,10 +590,9 @@ def show_waiting():
604
590
self .stories .append (response )
605
591
self .story = len (self .stories ) - 1
606
592
self .page = 0
607
- self .save_settings ()
593
+ self ._busy = False
608
594
609
595
self .load_story (response )
610
- self ._busy = False
611
596
612
597
def _sleep (self ):
613
598
# Set a sleep request flag so that any busy threads know to finish up
@@ -699,19 +684,14 @@ def main(args):
699
684
book = Book (args .rotation )
700
685
try :
701
686
book .start ()
702
-
703
- # If no stories, start a new one
704
- if not book .stories :
705
- book .new_story ()
706
-
687
+ book .generate_new_story ()
707
688
book .display_current_page ()
708
689
709
690
while True :
710
691
book .handle_events ()
711
692
except KeyboardInterrupt :
712
693
pass
713
694
finally :
714
- book .save_settings ()
715
695
book .deinit ()
716
696
pygame .quit ()
717
697
@@ -721,4 +701,4 @@ def main(args):
721
701
722
702
# TODO:
723
703
# * Figure out how to get the script to start on boot
724
- # * Play with prompt parameters
704
+ # * Play with chatgpt prompt parameters
0 commit comments