-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathinterface.py
More file actions
1562 lines (1339 loc) · 67.7 KB
/
Copy pathinterface.py
File metadata and controls
1562 lines (1339 loc) · 67.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# interface.py
import pygame
import json
import qrcode
import io
import socket
import os
from settings import *
from helpers import get_cocktail_image_path, get_valid_cocktails, wrap_text, favorite_cocktail, unfavorite_cocktail
from controller import make_drink
import logging
logger = logging.getLogger(__name__)
class CustomDropdown:
"""Custom dropdown implementation to replace pygame_widgets"""
def __init__(self, x, y, width, height, options, current_value="", font_size=18):
self.rect = pygame.Rect(x, y, width, height)
self.options = options
self.current_value = current_value
self.font = pygame.font.SysFont(None, font_size)
self.is_open = False
self.selected_index = 0
self.scroll_offset = 0
self.max_visible_items = 5
self.item_height = 30
# Find current value index
if current_value in options:
self.selected_index = options.index(current_value)
def handle_event(self, event):
"""Handle mouse events for the dropdown"""
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.is_open = not self.is_open
return True
elif self.is_open:
# Check if clicking on dropdown items
dropdown_rect = pygame.Rect(
self.rect.x,
self.rect.y + self.rect.height,
self.rect.width,
min(len(self.options), self.max_visible_items) * self.item_height
)
if dropdown_rect.collidepoint(event.pos):
# Calculate which item was clicked
relative_y = event.pos[1] - dropdown_rect.y
item_index = relative_y // self.item_height + self.scroll_offset
if 0 <= item_index < len(self.options):
self.selected_index = item_index
self.current_value = self.options[item_index]
self.is_open = False
return True
else:
self.is_open = False
elif event.type == pygame.MOUSEWHEEL and self.is_open:
# Handle scrolling in dropdown
dropdown_rect = pygame.Rect(
self.rect.x,
self.rect.y + self.rect.height,
self.rect.width,
min(len(self.options), self.max_visible_items) * self.item_height
)
if dropdown_rect.collidepoint(pygame.mouse.get_pos()):
self.scroll_offset = max(0, min(
len(self.options) - self.max_visible_items,
self.scroll_offset - event.y
))
return True
return False
def draw(self, surface):
"""Draw the dropdown"""
# Draw main dropdown button
pygame.draw.rect(surface, (240, 240, 240), self.rect)
pygame.draw.rect(surface, (100, 100, 100), self.rect, 2)
# Draw current selection text
display_text = self.current_value if self.current_value else "Select..."
if len(display_text) > 20:
display_text = display_text[:17] + "..."
text_surface = self.font.render(display_text, True, (0, 0, 0))
text_rect = text_surface.get_rect(center=(self.rect.centerx, self.rect.centery))
surface.blit(text_surface, text_rect)
# Draw dropdown arrow
arrow_points = [
(self.rect.right - 20, self.rect.centery - 5),
(self.rect.right - 10, self.rect.centery + 5),
(self.rect.right - 30, self.rect.centery + 5)
]
pygame.draw.polygon(surface, (0, 0, 0), arrow_points)
# Draw dropdown list if open
if self.is_open:
visible_items = min(len(self.options), self.max_visible_items)
dropdown_rect = pygame.Rect(
self.rect.x,
self.rect.y + self.rect.height,
self.rect.width,
visible_items * self.item_height
)
# Draw dropdown background
pygame.draw.rect(surface, (255, 255, 255), dropdown_rect)
pygame.draw.rect(surface, (100, 100, 100), dropdown_rect, 2)
# Draw items
for i in range(visible_items):
item_index = i + self.scroll_offset
if item_index >= len(self.options):
break
item_rect = pygame.Rect(
dropdown_rect.x,
dropdown_rect.y + i * self.item_height,
dropdown_rect.width,
self.item_height
)
# Highlight selected item
if item_index == self.selected_index:
pygame.draw.rect(surface, (200, 220, 255), item_rect)
# Highlight hovered item
mouse_pos = pygame.mouse.get_pos()
if item_rect.collidepoint(mouse_pos):
pygame.draw.rect(surface, (230, 240, 255), item_rect)
# Draw item text
item_text = self.options[item_index]
if len(item_text) > 25:
item_text = item_text[:22] + "..."
text_surface = self.font.render(item_text, True, (0, 0, 0))
text_rect = text_surface.get_rect(center=item_rect.center)
surface.blit(text_surface, text_rect)
# Draw separator line
if i < visible_items - 1:
pygame.draw.line(surface, (200, 200, 200),
(item_rect.left, item_rect.bottom),
(item_rect.right, item_rect.bottom))
# Draw scrollbar if needed
if len(self.options) > self.max_visible_items:
scrollbar_rect = pygame.Rect(
dropdown_rect.right - 10,
dropdown_rect.y,
10,
dropdown_rect.height
)
pygame.draw.rect(surface, (200, 200, 200), scrollbar_rect)
# Calculate scrollbar thumb
thumb_height = max(20, dropdown_rect.height * self.max_visible_items // len(self.options))
thumb_y = dropdown_rect.y + (dropdown_rect.height - thumb_height) * self.scroll_offset // (len(self.options) - self.max_visible_items)
thumb_rect = pygame.Rect(scrollbar_rect.x, thumb_y, scrollbar_rect.width, thumb_height)
pygame.draw.rect(surface, (100, 100, 100), thumb_rect)
def get_selected(self):
"""Get the currently selected value"""
return self.current_value
def get_local_ip():
"""Get the local IP address"""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
local_ip = s.getsockname()[0]
s.close()
return local_ip
except:
return "localhost"
def create_qr_code_slide():
"""Create a QR code slide for the Streamlit app access"""
# Get local IP and port
local_ip = get_local_ip()
streamlit_port = 8501
url = f"http://{local_ip}:{streamlit_port}"
# Create QR code
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(url)
qr.make(fit=True)
# Create QR code image
qr_image = qr.make_image(fill_color="black", back_color="white")
# Convert PIL image to pygame surface
img_buffer = io.BytesIO()
qr_image.save(img_buffer, format='PNG')
img_buffer.seek(0)
# Load into pygame
qr_surface = pygame.image.load(img_buffer)
# Scale to fit screen (make it large enough to scan easily)
qr_size = min(screen_width, screen_height) // 2
qr_surface = pygame.transform.scale(qr_surface, (qr_size, qr_size))
# Create a cocktail-like object for the QR code slide
qr_cocktail = {
'normal_name': 'Access App',
'fun_name': 'Scan QR Code',
'qr_surface': qr_surface,
'url': url,
'is_qr_slide': True
}
return qr_cocktail
def get_cocktails_with_qr():
"""Get valid cocktails and add QR code slide at the end"""
cocktails = get_valid_cocktails()
qr_cocktail = create_qr_code_slide()
cocktails.append(qr_cocktail)
return cocktails
def check_for_refresh_signal():
"""Check if there's a signal from the app to refresh cocktails"""
try:
if os.path.exists('interface_signal.json'):
with open('interface_signal.json', 'r') as f:
signal = json.load(f)
# Check if it's a refresh signal
if signal.get('action') == 'refresh_cocktails':
# Remove the signal file after reading
os.remove('interface_signal.json')
logger.info("Received refresh signal from app")
return True
except Exception as e:
logger.error(f"Error checking refresh signal: {e}")
return False
pygame.init()
if FULL_SCREEN:
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
else:
screen = pygame.display.set_mode((720, 720))
screen_size = screen.get_size()
screen_width, screen_height = screen_size
cocktail_image_offset = screen_width * (1.0 - COCKTAIL_IMAGE_SCALE) // 2
pygame.display.set_caption('Cocktail Swipe')
normal_text_size = 72
small_text_size = int(normal_text_size * 0.6)
text_position = (screen_width // 2, int(screen_height * 0.85))
def add_layer(*args, function=screen.blit, key=None):
if key == None:
key = len(layers)
layers[str(key)] = {'function': function, 'args': args}
def remove_layer(key):
try:
del layers[key]
except KeyError:
pass
layers = {}
def draw_frame():
for layer in layers.values():
layer['function'](*layer['args'])
pygame.display.flip()
def animate_logo_click(logo, rect, base_size, target_size, layer_key, duration=150):
"""Animate a logo click (pop effect): grow from base_size to target_size then shrink back."""
clock = pygame.time.Clock()
center = rect.center
# Expand
start_time = pygame.time.get_ticks()
while True:
elapsed = pygame.time.get_ticks() - start_time
progress = min(elapsed / duration, 1.0)
current_size = int(base_size + (target_size - base_size) * progress)
scaled_img = pygame.transform.scale(logo, (current_size, current_size))
new_rect = scaled_img.get_rect(center=center)
add_layer(scaled_img, new_rect, key=layer_key)
draw_frame()
if progress >= 1.0:
break
clock.tick(60)
# Shrink back
start_time = pygame.time.get_ticks()
while True:
elapsed = pygame.time.get_ticks() - start_time
progress = min(elapsed / duration, 1.0)
current_size = int(target_size - (target_size - base_size) * progress)
scaled_img = pygame.transform.scale(logo, (current_size, current_size))
new_rect = scaled_img.get_rect(center=center)
add_layer(scaled_img, new_rect, key=layer_key)
draw_frame()
if progress >= 1.0:
break
clock.tick(60)
def animate_logo_rotate(logo, rect, layer_key, rotation=180):
"""Animate a logo click (rotate effect): rotate the amount of rotation provided"""
angle = 0
while angle < rotation:
angle = (angle + 5) % 360
rotated_loading = pygame.transform.rotate(logo, angle * -1)
rotated_rect = rotated_loading.get_rect(center=rect.center)
# Draw loading image first (under)
add_layer(rotated_loading, rotated_rect, key=layer_key)
draw_frame()
def animate_both_logos_zoom(single_logo, double_logo, single_rect, double_rect, base_size, target_size, duration=300):
"""Animate both logos zooming in together and then shrinking back."""
clock = pygame.time.Clock()
center_single = single_rect.center
center_double = double_rect.center
# Expand
start_time = pygame.time.get_ticks()
while True:
elapsed = pygame.time.get_ticks() - start_time
progress = min(elapsed / duration, 1.0)
current_size = int(base_size + (target_size - base_size) * progress)
scaled_single = pygame.transform.scale(single_logo, (current_size, current_size))
scaled_double = pygame.transform.scale(double_logo, (current_size, current_size))
new_rect_single = scaled_single.get_rect(center=center_single)
new_rect_double = scaled_double.get_rect(center=center_double)
add_layer(scaled_single, new_rect_single, key='single_logo')
add_layer(scaled_double, new_rect_double, key='double_logo')
draw_frame()
if progress >= 1.0:
break
clock.tick(60)
# Contract
start_time = pygame.time.get_ticks()
while True:
elapsed = pygame.time.get_ticks() - start_time
progress = min(elapsed / duration, 1.0)
current_size = int(target_size - (target_size - base_size) * progress)
scaled_single = pygame.transform.scale(single_logo, (current_size, current_size))
scaled_double = pygame.transform.scale(double_logo, (current_size, current_size))
new_rect_single = scaled_single.get_rect(center=center_single)
new_rect_double = scaled_double.get_rect(center=center_double)
add_layer(scaled_single, new_rect_single, key='single_logo')
add_layer(scaled_double, new_rect_double, key='double_logo')
draw_frame()
if progress >= 1.0:
break
clock.tick(60)
def show_pouring_and_loading(watcher):
"""Overlay pouring_img full screen and a spinning loading_img (720x720) drawn underneath."""
try:
pouring_img = pygame.image.load('pouring.png')
pouring_img = pygame.transform.scale(pouring_img, screen_size)
except Exception as e:
logger.exception('Error loading pouring.png')
pouring_img = None
try:
loading_img = pygame.image.load('loading.png')
loading_img = pygame.transform.scale(loading_img, (70, 70))
except Exception as e:
logger.exception('Error loading loading.png')
loading_img = None
try:
checkmark_img = pygame.image.load('checkmark.png')
checkmark_img = pygame.transform.scale(checkmark_img, (30, 30))
except Exception as e:
logger.exception('Error loading loading.png')
checkmark_img = None
angle = 0
# Add a background layer
add_layer(*layers['background']['args'], function=layers['background']['function'], key='pouring_background')
# Then draw pouring image on top
if pouring_img:
add_layer(pouring_img, (0, -150), key='pouring')
pour_layers = []
pouring_line = 0
while not watcher.done():
angle = (angle - 5) % 360
if loading_img:
rotated_loading = pygame.transform.rotate(loading_img, angle)
for index, pour in enumerate(watcher.pours):
layer_key = f'pour_{index}'
logo_layer_key = f'{layer_key}_logo'
x_position = screen_width // 3
y_position = (text_position[1] + small_text_size * pouring_line) - 325
if logo_layer_key not in pour_layers:
font = pygame.font.SysFont(None, small_text_size)
for layer_index, line in enumerate(wrap_text(str(pour), font, screen_width * 0.5)):
line_key = f'{layer_key}_{layer_index}'
text_surface = font.render(line, True, (255, 255, 255))
line_y_position = y_position + small_text_size * layer_index
if layer_index > 0:
line_y_position = line_y_position - 10 * layer_index
text_rect = text_surface.get_rect(topleft=(x_position, line_y_position))
pour_layers.append(line_key)
add_layer(text_surface, text_rect, key=line_key)
pouring_line += 1
pour_layers.append(logo_layer_key)
status_position = layers.get(logo_layer_key, {}).get('args', [None, None])[1]
if status_position:
status_position = status_position.center
else:
status_position = (x_position - small_text_size // 2, y_position - 7 + small_text_size // 2)
if pour.running and loading_img:
rect = rotated_loading.get_rect(center=status_position)
add_layer(rotated_loading, rect, key=logo_layer_key)
else:
if checkmark_img:
rect = checkmark_img.get_rect(center=status_position)
add_layer(checkmark_img, rect, key=logo_layer_key)
else:
remove_layer(logo_layer_key)
draw_frame()
for layer in pour_layers:
remove_layer(layer)
remove_layer('pouring')
remove_layer('pouring_background')
draw_frame()
pygame.event.clear() # Drop all events that happened while pouring
def create_settings_tray():
"""Create the settings tray UI elements"""
tray_height = int(screen_height * 0.4) # 40% of screen height
tray_rect = pygame.Rect(0, screen_height - tray_height, screen_width, tray_height)
# Create a semi-transparent background
overlay = pygame.Surface((screen_width, tray_height))
overlay.set_alpha(200)
overlay.fill((0, 0, 0))
# Settings title
title_font = pygame.font.SysFont(None, 48)
title_text = title_font.render("Settings", True, (255, 255, 255))
title_rect = title_text.get_rect(center=(screen_width // 2, screen_height - tray_height + 40))
# Time per oz slider
slider_width = int(screen_width * 0.6)
slider_height = 20
slider_x = (screen_width - slider_width) // 2
slider_y = screen_height - tray_height + 100
# Slider background
slider_bg_rect = pygame.Rect(slider_x, slider_y, slider_width, slider_height)
# Slider handle position (based on current OZ_COEFFICIENT value)
min_val, max_val = 1.0, 15.0
slider_handle_x = slider_x + (OZ_COEFFICIENT - min_val) / (max_val - min_val) * slider_width
slider_handle_rect = pygame.Rect(slider_handle_x - 10, slider_y - 5, 20, 30)
# Slider label
slider_font = pygame.font.SysFont(None, 32)
slider_label = slider_font.render(f"Time per oz: {OZ_COEFFICIENT:.1f}s", True, (255, 255, 255))
slider_label_rect = slider_label.get_rect(center=(screen_width // 2, slider_y - 30))
# Buttons
button_width = 150
button_height = 50
button_spacing = 20
# Prime pumps button
prime_rect = pygame.Rect(screen_width // 2 - button_width - button_spacing // 2,
screen_height - tray_height + 180, button_width, button_height)
prime_font = pygame.font.SysFont(None, 28)
prime_text = prime_font.render("Prime Pumps", True, (255, 255, 255))
prime_text_rect = prime_text.get_rect(center=prime_rect.center)
# Clean pumps button
clean_rect = pygame.Rect(screen_width // 2 + button_spacing // 2,
screen_height - tray_height + 180, button_width, button_height)
clean_font = pygame.font.SysFont(None, 28)
clean_text = clean_font.render("Clean Pumps", True, (255, 255, 255))
clean_text_rect = clean_text.get_rect(center=clean_rect.center)
# Reverse pump direction toggle switch
switch_width = 60
switch_height = 30
switch_x = screen_width // 2 - switch_width // 2
switch_y = screen_height - tray_height + 250
switch_rect = pygame.Rect(switch_x, switch_y, switch_width, switch_height)
# Switch label
switch_font = pygame.font.SysFont(None, 24)
switch_label = switch_font.render("Reverse Pump Direction", True, (255, 255, 255))
switch_label_rect = switch_label.get_rect(center=(screen_width // 2, switch_y - 20))
# Streamlit app access info
import socket
try:
# Get local IP address
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
local_ip = s.getsockname()[0]
s.close()
except:
local_ip = "localhost"
streamlit_port = 8501 # Default Streamlit port
# Access info label
access_font = pygame.font.SysFont(None, 20)
access_label = access_font.render("Access your app at:", True, (200, 200, 200))
access_label_rect = access_label.get_rect(center=(screen_width // 2, switch_y + 50))
# IP and port info
ip_font = pygame.font.SysFont(None, 24)
ip_text = f"http://{local_ip}:{streamlit_port}"
ip_label = ip_font.render(ip_text, True, (0, 255, 255)) # Cyan color for URL
ip_label_rect = ip_label.get_rect(center=(screen_width // 2, switch_y + 75))
return {
'tray_rect': tray_rect,
'overlay': overlay,
'title_text': title_text,
'title_rect': title_rect,
'slider_bg_rect': slider_bg_rect,
'slider_handle_rect': slider_handle_rect,
'slider_label': slider_label,
'slider_label_rect': slider_label_rect,
'prime_rect': prime_rect,
'prime_text': prime_text,
'prime_text_rect': prime_text_rect,
'clean_rect': clean_rect,
'clean_text': clean_text,
'clean_text_rect': clean_text_rect,
'switch_rect': switch_rect,
'switch_label': switch_label,
'switch_label_rect': switch_label_rect,
'access_label': access_label,
'access_label_rect': access_label_rect,
'ip_label': ip_label,
'ip_label_rect': ip_label_rect,
'slider_x': slider_x,
'slider_width': slider_width,
'min_val': min_val,
'max_val': max_val
}
def draw_settings_tray(settings_ui, is_visible):
"""Draw the settings tray if visible"""
if not is_visible:
return
# Draw overlay
add_layer(settings_ui['overlay'], settings_ui['tray_rect'], key='settings_overlay')
# Draw title
add_layer(settings_ui['title_text'], settings_ui['title_rect'], key='settings_title')
# Create temporary surfaces for slider and buttons
temp_surface = pygame.Surface(screen_size, pygame.SRCALPHA)
# Draw slider background
pygame.draw.rect(temp_surface, (100, 100, 100), settings_ui['slider_bg_rect'])
# Draw slider handle
pygame.draw.rect(temp_surface, (255, 255, 255), settings_ui['slider_handle_rect'])
# Draw buttons
pygame.draw.rect(temp_surface, (50, 150, 50), settings_ui['prime_rect'])
pygame.draw.rect(temp_surface, (150, 50, 50), settings_ui['clean_rect'])
# Draw switch background
switch_color = (0, 200, 0) if INVERT_PUMP_PINS else (100, 100, 100)
pygame.draw.rect(temp_surface, switch_color, settings_ui['switch_rect'])
pygame.draw.rect(temp_surface, (200, 200, 200), settings_ui['switch_rect'], 2)
# Draw switch indicator (circle)
indicator_radius = 12
if INVERT_PUMP_PINS:
# ON position - indicator on the right
indicator_x = settings_ui['switch_rect'].x + settings_ui['switch_rect'].width - indicator_radius - 3
else:
# OFF position - indicator on the left
indicator_x = settings_ui['switch_rect'].x + indicator_radius + 3
indicator_y = settings_ui['switch_rect'].y + settings_ui['switch_rect'].height // 2
pygame.draw.circle(temp_surface, (255, 255, 255), (indicator_x, indicator_y), indicator_radius)
add_layer(temp_surface, (0, 0), key='settings_controls')
# Draw slider label
add_layer(settings_ui['slider_label'], settings_ui['slider_label_rect'], key='slider_label')
# Draw button text
add_layer(settings_ui['prime_text'], settings_ui['prime_text_rect'], key='prime_text')
add_layer(settings_ui['clean_text'], settings_ui['clean_text_rect'], key='clean_text')
# Draw switch label
add_layer(settings_ui['switch_label'], settings_ui['switch_label_rect'], key='switch_label')
# Draw access info
add_layer(settings_ui['access_label'], settings_ui['access_label_rect'], key='access_label')
add_layer(settings_ui['ip_label'], settings_ui['ip_label_rect'], key='ip_label')
def create_settings_tab():
"""Create the small tab at the bottom for accessing settings"""
tab_width = 80
tab_height = 20
tab_x = (screen_width - tab_width) // 2
tab_y = screen_height - tab_height
tab_rect = pygame.Rect(tab_x, tab_y, tab_width, tab_height)
# Create simple tab surface
tab_surface = pygame.Surface((tab_width, tab_height))
tab_surface.fill((60, 60, 60)) # Dark gray
# Add border for definition
pygame.draw.rect(tab_surface, (120, 120, 120), (0, 0, tab_width, tab_height), 2)
return {
'rect': tab_rect,
'surface': tab_surface,
'base_y': tab_y, # Store original position
'width': tab_width,
'height': tab_height
}
def animate_settings_tray(settings_ui, settings_tab, show_tray, duration=300):
"""Animate the settings tray sliding up or down"""
clock = pygame.time.Clock()
start_time = pygame.time.get_ticks()
tray_height = settings_ui['tray_rect'].height
if show_tray:
# Slide up from bottom
start_y = screen_height
end_y = screen_height - tray_height
tab_start_y = settings_tab['base_y']
tab_end_y = screen_height - tray_height - settings_tab['height']
else:
# Slide down to bottom
start_y = screen_height - tray_height
end_y = screen_height
tab_start_y = screen_height - tray_height - settings_tab['height']
tab_end_y = settings_tab['base_y']
while True:
elapsed = pygame.time.get_ticks() - start_time
progress = min(elapsed / duration, 1.0)
current_y = start_y + (end_y - start_y) * progress
settings_ui['tray_rect'].y = current_y
# Update tab position to slide with tray
tab_current_y = tab_start_y + (tab_end_y - tab_start_y) * progress
settings_tab['rect'].y = tab_current_y
# Update all related positions
settings_ui['title_rect'].y = current_y + 40
settings_ui['slider_bg_rect'].y = current_y + 100
settings_ui['slider_handle_rect'].y = current_y + 95
settings_ui['slider_label_rect'].y = current_y + 70
settings_ui['prime_rect'].y = current_y + 180
settings_ui['clean_rect'].y = current_y + 180
settings_ui['prime_text_rect'].center = settings_ui['prime_rect'].center
settings_ui['clean_text_rect'].center = settings_ui['clean_rect'].center
settings_ui['switch_rect'].y = current_y + 250
settings_ui['switch_label_rect'].y = current_y + 230
settings_ui['access_label_rect'].y = current_y + 300
settings_ui['ip_label_rect'].y = current_y + 325
# Update tab layer
remove_layer('settings_tab')
add_layer(settings_tab['surface'], settings_tab['rect'], key='settings_tab')
draw_settings_tray(settings_ui, True)
draw_frame()
if progress >= 1.0:
break
clock.tick(60)
def handle_settings_interaction(settings_ui, event_pos):
"""Handle interactions with settings tray elements"""
# Check if slider is being dragged
if settings_ui['slider_handle_rect'].collidepoint(event_pos):
return 'slider_drag'
# Check if prime button is clicked
if settings_ui['prime_rect'].collidepoint(event_pos):
return 'prime_pumps'
# Check if clean button is clicked
if settings_ui['clean_rect'].collidepoint(event_pos):
return 'clean_pumps'
# Check if switch is clicked
if settings_ui['switch_rect'].collidepoint(event_pos):
return 'toggle_switch'
return None
def update_oz_coefficient(settings_ui, new_value):
"""Update the OZ_COEFFICIENT setting and slider position"""
global OZ_COEFFICIENT
OZ_COEFFICIENT = max(settings_ui['min_val'], min(settings_ui['max_val'], new_value))
# Update slider handle position
slider_handle_x = settings_ui['slider_x'] + (OZ_COEFFICIENT - settings_ui['min_val']) / (settings_ui['max_val'] - settings_ui['min_val']) * settings_ui['slider_width']
settings_ui['slider_handle_rect'].x = slider_handle_x - 10
# Update slider label
slider_font = pygame.font.SysFont(None, 32)
settings_ui['slider_label'] = slider_font.render(f"Time per oz: {OZ_COEFFICIENT:.1f}s", True, (255, 255, 255))
def toggle_pump_direction():
"""Toggle the INVERT_PUMP_PINS setting"""
global INVERT_PUMP_PINS
INVERT_PUMP_PINS = not INVERT_PUMP_PINS
logger.info(f'Pump direction inverted: {INVERT_PUMP_PINS}')
def create_drink_management_tray():
"""Create the drink management tray UI elements"""
tray_height = int(screen_height * 0.8) # 80% of screen height
tray_rect = pygame.Rect(0, -tray_height, screen_width, tray_height)
# Create a gradient background for better aesthetics
overlay = pygame.Surface((screen_width, tray_height))
overlay.set_alpha(220)
# Create gradient effect
for y in range(tray_height):
alpha = int(255 * (1 - y / tray_height) * 0.8)
color = (20, 25, 35)
pygame.draw.line(overlay, color, (0, y), (screen_width, y))
# Header section with better styling
header_height = 80
header_surface = pygame.Surface((screen_width, header_height))
header_surface.set_alpha(180)
header_surface.fill((25, 30, 40))
# Title with shadow effect
title_font = pygame.font.SysFont('Arial', 42, bold=True)
title_shadow = title_font.render("Drink Management", True, (0, 0, 0))
title_text = title_font.render("Drink Management", True, (255, 255, 255))
title_shadow_rect = title_shadow.get_rect(center=(screen_width // 2 + 2, 42))
title_rect = title_text.get_rect(center=(screen_width // 2, 40))
# Load drink options
try:
with open('drink_options.json', 'r') as f:
drink_data = json.load(f)
drink_options = drink_data['drinks']
print(f"Loaded {len(drink_options)} drink options from JSON")
except Exception as e:
print(f"Error loading drink options: {e}")
drink_options = ["", "Vodka", "Gin", "Rum", "Tequila", "Whiskey", "Bourbon", "Scotch", "Brandy", "Cognac"]
print(f"Using fallback options: {len(drink_options)} drinks")
# Load current pump configuration
try:
with open(CONFIG_FILE, 'r') as f:
current_config = json.load(f)
except:
current_config = {}
# Create custom dropdowns for 12 pumps with better layout
dropdowns = []
dropdown_width = 180 # Slightly smaller for better fit
dropdown_height = 45
dropdown_spacing = 25
# Calculate positions for a more organized 6x2 grid
total_width = 6 * dropdown_width + 5 * dropdown_spacing
start_x = (screen_width - total_width) // 2
# Top row (pumps 1-6) - with better spacing
top_row_y = header_height + 40
for i in range(6):
x = start_x + i * (dropdown_width + dropdown_spacing)
y = top_row_y
# Styled pump label
label_font = pygame.font.SysFont('Arial', 20, bold=True)
label_text = label_font.render(f"Pump {i+1}", True, (220, 220, 220))
label_rect = label_text.get_rect(center=(x + dropdown_width // 2, y - 15))
# Current selection
current_drink = current_config.get(f"Pump {i+1}", "")
# Create custom dropdown
dropdown = CustomDropdown(
x, y, dropdown_width, dropdown_height,
drink_options, current_drink, font_size=16
)
print(f"Created dropdown for Pump {i+1} at ({x}, {y}) with {len(drink_options)} options")
dropdowns.append({
'dropdown': dropdown,
'label_text': label_text,
'label_rect': label_rect,
'current_value': current_drink,
'pump_number': i+1,
'rect': pygame.Rect(x, y, dropdown_width, dropdown_height)
})
# Bottom row (pumps 7-12) - with better spacing
bottom_row_y = top_row_y + 120
for i in range(6):
x = start_x + i * (dropdown_width + dropdown_spacing)
y = bottom_row_y
# Styled pump label
label_font = pygame.font.SysFont('Arial', 20, bold=True)
label_text = label_font.render(f"Pump {i+7}", True, (220, 220, 220))
label_rect = label_text.get_rect(center=(x + dropdown_width // 2, y - 15))
# Current selection
current_drink = current_config.get(f"Pump {i+7}", "")
# Create custom dropdown
dropdown = CustomDropdown(
x, y, dropdown_width, dropdown_height,
drink_options, current_drink, font_size=16
)
print(f"Created dropdown for Pump {i+7} at ({x}, {y}) with {len(drink_options)} options")
dropdowns.append({
'dropdown': dropdown,
'label_text': label_text,
'label_rect': label_rect,
'current_value': current_drink,
'pump_number': i+7,
'rect': pygame.Rect(x, y, dropdown_width, dropdown_height)
})
# Styled Generate button at the bottom
generate_button_width = 250
generate_button_height = 55
generate_button_x = (screen_width - generate_button_width) // 2
generate_button_y = bottom_row_y + 150
generate_button_rect = pygame.Rect(generate_button_x, generate_button_y, generate_button_width, generate_button_height)
generate_font = pygame.font.SysFont('Arial', 24, bold=True)
generate_text = generate_font.render("Generate New Menu", True, (255, 255, 255))
generate_text_rect = generate_text.get_rect(center=generate_button_rect.center)
return {
'tray_rect': tray_rect,
'overlay': overlay,
'header_surface': header_surface,
'title_text': title_text,
'title_shadow': title_shadow,
'title_rect': title_rect,
'title_shadow_rect': title_shadow_rect,
'dropdowns': dropdowns,
'generate_button_rect': generate_button_rect,
'generate_text': generate_text,
'generate_text_rect': generate_text_rect
}
def create_drink_management_tab():
"""Create the small tab at the top for accessing drink management"""
tab_width = 80
tab_height = 20
tab_x = (screen_width - tab_width) // 2
tab_y = 0
tab_rect = pygame.Rect(tab_x, tab_y, tab_width, tab_height)
# Create simple tab surface
tab_surface = pygame.Surface((tab_width, tab_height))
tab_surface.fill((60, 60, 60)) # Dark gray
# Add border for definition
pygame.draw.rect(tab_surface, (120, 120, 120), (0, 0, tab_width, tab_height), 2)
return {
'rect': tab_rect,
'surface': tab_surface,
'base_y': tab_y, # Store original position
'width': tab_width,
'height': tab_height
}
def draw_drink_management_tray(drink_ui, is_visible, events=None):
"""Draw the drink management tray if visible"""
if not is_visible:
return
# Draw gradient background overlay
add_layer(drink_ui['overlay'], drink_ui['tray_rect'], key='drink_overlay')
# Draw header section
header_rect = pygame.Rect(drink_ui['tray_rect'].x, drink_ui['tray_rect'].y,
drink_ui['tray_rect'].width, 80)
add_layer(drink_ui['header_surface'], header_rect, key='drink_header')
# Draw title with shadow effect
add_layer(drink_ui['title_shadow'], drink_ui['title_shadow_rect'], key='drink_title_shadow')
add_layer(drink_ui['title_text'], drink_ui['title_rect'], key='drink_title')
# Draw pump labels with better styling
for dropdown in drink_ui['dropdowns']:
add_layer(dropdown['label_text'], dropdown['label_rect'], key=f'label_{dropdown["pump_number"]}')
# Note: Custom dropdowns will be drawn after draw_frame() to ensure they're on top
# Draw styled generate button with gradient and border
button_rect = drink_ui['generate_button_rect']
# Button gradient background
for i in range(button_rect.height):
color_ratio = i / button_rect.height
color = (
int(40 + (80 - 40) * color_ratio), # Dark green to lighter green
int(120 + (160 - 120) * color_ratio),
int(40 + (80 - 40) * color_ratio)
)
pygame.draw.line(screen, color,
(button_rect.left, button_rect.top + i),
(button_rect.right, button_rect.top + i))
# Button border and highlight
pygame.draw.rect(screen, (100, 200, 100), button_rect, 3)
pygame.draw.rect(screen, (150, 220, 150), button_rect, 1)
# Button text
add_layer(drink_ui['generate_text'], drink_ui['generate_text_rect'], key='generate_text')
def animate_drink_management_tray(drink_ui, drink_tab, show_tray, duration=300):
"""Animate the drink management tray sliding down or up"""
clock = pygame.time.Clock()
start_time = pygame.time.get_ticks()
tray_height = drink_ui['tray_rect'].height
if show_tray:
# Slide down from top
start_y = -tray_height
end_y = 0
tab_start_y = drink_tab['base_y']
tab_end_y = tray_height - drink_tab['height']
else:
# Slide up to top
start_y = 0
end_y = -tray_height
tab_start_y = tray_height - drink_tab['height']
tab_end_y = drink_tab['base_y']
while True:
elapsed = pygame.time.get_ticks() - start_time
progress = min(elapsed / duration, 1.0)
current_y = start_y + (end_y - start_y) * progress
drink_ui['tray_rect'].y = current_y
# Update tab position to slide with tray
tab_current_y = tab_start_y + (tab_end_y - tab_start_y) * progress
drink_tab['rect'].y = tab_current_y
# Update all related positions
drink_ui['title_rect'].y = current_y + 40
# Update dropdown positions to match new layout
header_height = 80
for dropdown in drink_ui['dropdowns']:
if dropdown['pump_number'] <= 6:
# Top row
new_y = current_y + header_height + 40
dropdown['rect'].y = new_y
dropdown['label_rect'].y = new_y - 15
# Update custom dropdown position
dropdown['dropdown'].rect.y = new_y
else:
# Bottom row
new_y = current_y + header_height + 40 + 120
dropdown['rect'].y = new_y
dropdown['label_rect'].y = new_y - 15
# Update custom dropdown position
dropdown['dropdown'].rect.y = new_y
# Update generate button position
drink_ui['generate_button_rect'].y = current_y + tray_height - 80
drink_ui['generate_text_rect'].center = drink_ui['generate_button_rect'].center
# Update tab layer
remove_layer('drink_tab')
add_layer(drink_tab['surface'], drink_tab['rect'], key='drink_tab')