-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
121 lines (109 loc) · 4 KB
/
main.py
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
# import the pygame module, so you can use it
import pygame
import os
from time import sleep
SCREEN_WIDTH = 128
SCREEN_HEIGHT = 64
black = (0,0,0)
white = (255,255,255)
HOME = ["LOAD SCHEDULE", "NEW SCHEDULE"]
def init_screen():
pygame.init()
# create a surface on screen
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
screen.fill(black)
pygame.draw.rect(screen, white, (0,0,SCREEN_WIDTH,SCREEN_HEIGHT), 1) # screen border
pygame.display.update()
return screen
def select_mode(screen, selections, selected):
#selections = ["LOAD SCHEDULE", "NEW SCHEDULE"]
init_screen()
main_font = pygame.font.SysFont("menlo", 10)
font_height = main_font.get_height()
text_start = [2, 2]
max_lines = (SCREEN_HEIGHT - text_start[1])//font_height
print("max lines: " + str(max_lines))
text = []
for item in selections:
if selected == selections.index(item):
text.append(main_font.render(item, 1, black, white))
else:
text.append(main_font.render(item, 1, white, black))
if (selected + 1) > max_lines:
start_index = (selected + 1) - max_lines
max_index = max_lines
else:
start_index = 0
max_index = len(selections) - 1
if max_index + 1 > max_lines:
max_index = start_index + (max_lines - 1)
for txt in range(start_index,max_index+1):
screen.blit(text[txt], text_start)
text_start = [text_start[0], text_start[1] + font_height]
pygame.display.update()
def get_new_list(mode, selected):
if mode == "list_strings":
if selected == "LOAD SCHEDULE":
new_list = dir_schedules()
new_list.append('Back')
new_mode = "list_files"
elif mode == "list_files" and selected == "Back":
new_list = HOME
new_mode = "list_strings"
elif mode == "list_files" and selected != "Back":
new_list = read_schedule("schedules/" + selected)
new_list.append('Start')
new_list.append('Back')
new_mode = "list_display"
elif mode == "list_display" and selected == "Back":
new_list = dir_schedules()
new_list.append('Back')
new_mode = "list_files"
elif mode == "list_display" and selected == "Start":
new_list = ["starting"]
new_mode = "list_strings"
return (new_mode, new_list)
def dir_schedules():
schedules = os.listdir('schedules')
return schedules
def read_schedule(schedule_file):
lines = []
with open(schedule_file, "r") as f:
for line in f:
lines.append(line.rstrip())
return lines
def main():
running = True
screen = init_screen()
mode = "list_strings"
selected = 0
item_list = HOME
select_mode(screen, item_list, selected)
clock = pygame.time.Clock()
# main loop
while running:
time_delta = clock.tick(60)/1000.0
# event handling, gets all event from the eventqueue
for event in pygame.event.get():
# only do something if the event is of type QUIT
#print(mode, event)
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
if selected < len(item_list) - 1:
selected = selected + 1
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
if selected > 0 :
selected = selected - 1
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
screen = init_screen()
(mode, item_list) = get_new_list(mode, item_list[selected])
selected = 0
select_mode(screen, item_list, selected)
if (event.type == pygame.QUIT):
# change the value to False, to exit the main loop
running = False
sleep(time_delta)
# run the main function only if this module is executed as the main script
# (if you import this as a module then nothing is executed)
if __name__=="__main__":
# call the main function
main()