-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
385 lines (297 loc) · 13.9 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
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
import os
os.environ["KIVY_IMAGE"] = "pil"
from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0')
Config.set('kivy', 'keyboard_mode', 'system')
Config.set('graphics', 'fullscreen', '1')
Config.set('graphics', 'allow_screensaver', '0')
Config.set('graphics', 'width', '1920')
Config.set('graphics', 'height', '1080')
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.loader import Loader
from kivy import resources
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.behaviors import focus
from kivy.uix.video import Video
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.clock import Clock
from kivy.properties import NumericProperty
from widgets import *
import re
import keyboard
from kivy.core.window import Window
from kivy.cache import Cache
Cache._categories['kv.image']['limit'] = 0
Cache._categories['kv.texture']['limit'] = 0
# from gpiozero import LED
# Retrieve data files
Builder.load_file('./layout.kv')
gif = Loader.image('./timer_frame.gif')
logo = Loader.image('./WHO.png')
check = Loader.image('./Check.png')
check_hover = Loader.image('./Check Hover.png')
class IntroLogo(BgScreen):
def __init__(self, **kwargs):
super(IntroLogo, self).__init__(**kwargs)
self.layout = AnchorLayout(padding=[100])
self.add_widget(self.layout)
# Add starting Logo
self.logo = Image(source='WHO.png',
allow_stretch=True,
keep_ratio=True)
self.layout.add_widget(self.logo)
# Specific function that removes a KeyboardsListener and then switches to next screen
def switch():
keyboard.remove_hotkey(self.hotkey)
sm.switch_to(VideoScreen())
# Add custom KeyBoardListener that triggers the switch-function on enter-key
self.hotkey = keyboard.add_hotkey('enter', switch)
class VideoScreen(Screen):
def __init__(self, **kwargs):
super(VideoScreen, self).__init__(**kwargs)
self.video = Video(source='test.mkv', state='stop')
# Specific function that switches to EscapeRoom screen
def switch(self, kwargs):
sm.switch_to(PasswordScreen())
# Bind a callback to eos event. Eos is when the video ends
self.video.bind(eos=switch)
self.add_widget(self.video)
# When the screen enters view, play the video
def on_enter(self, *args):
self.video.state = 'play'
class PasswordScreen(BgScreen):
def __init__(self, **kwargs):
super(PasswordScreen, self).__init__(**kwargs)
self.layout = BoxLayout(orientation='horizontal')
self.add_widget(self.layout)
self.layout2 = BoxLayout(orientation='vertical', padding=[0, 50])
self.layout.add_widget(self.layout2)
self.layout_count = AnchorLayout(size_hint=(.9, 1))
self.layout.add_widget(self.layout_count)
# Start the global timer and add a Label to EscapeRoom > layout
timer.start()
self.timer_frame = Image(source='timer_frame.gif',
anim_delay=0.05,
allow_stretch=True,
keep_ratio=True)
self.layout_count.add_widget(self.timer_frame)
self.countdown = CountdownText(text=timer.text)
self.layout_count.add_widget(self.countdown)
# Set the countdown-label to be equal to our global timer
def update(dt):
self.countdown.text = timer.text
# Update our ocuntdown-text every 1/15 seconds
Clock.schedule_interval(update, 1.0 / 15.0)
self.img = Image(source='WHO.png',
allow_stretch=True)
self.passwordBox = FloatLayout()
self.layout2.add_widget(self.img)
self.layout2.add_widget(self.passwordBox)
self.textinput = CustomTextInput(hint_text='Password',
multiline=False,
size_hint=(.6, .17),
pos_hint={'center_x': .5, 'y': .55})
self.passwordBox_wrong = Label(text='Incorrect password', color=(1, 0, 0, 0), font_size=20, bold=True,
pos_hint={'center_x': .5, 'y': 0}, valign='top')
# Switch screen if password is correct. Else, show error-label
def check_password(PasswordScreen):
if self.textinput.text == '123':
switch()
else:
self.passwordBox_wrong.color = (1, 0, 0, 1)
# Check button and passwordbox
self.btn = ImageButton(on_press=check_password,
size_hint=(.3, None),
keep_ratio=True,
pos_hint={'center_x': .5, 'center_y': .3})
self.passwordBox.add_widget(self.textinput)
self.passwordBox.add_widget(self.passwordBox_wrong)
self.passwordBox.add_widget(self.btn)
def switch():
keyboard.remove_hotkey(self.enter_hotkey)
sm.switch_to(RapportScreen())
# Hotkey
self.enter_hotkey = keyboard.add_hotkey('enter', check_password, args=[self])
# Set focus on text-input at start of screen
self.textinput.focus = True
class RapportScreen(BgScreen):
def __init__(self, **kwargs):
super(RapportScreen, self).__init__(**kwargs)
self.layout = BoxLayout(orientation='horizontal')
self.add_widget(self.layout)
self.layout2 = BoxLayout(orientation='vertical', padding=[180, 80])
self.layout.add_widget(self.layout2)
self.layout_count = AnchorLayout(size_hint=(.9, 1))
self.layout.add_widget(self.layout_count)
# Start the global timer and add a Label to EscapeRoom > layout
timer.start()
self.timer_frame = Image(source='timer_frame.gif',
anim_delay=0.05,
allow_stretch=True,
keep_ratio=True)
self.layout_count.add_widget(self.timer_frame)
self.countdown = CountdownText(text=timer.text)
self.layout_count.add_widget(self.countdown)
# Set the countdown-label to be equal to our global timer
def update(dt):
self.countdown.text = timer.text
# Update our countdown-text every
Clock.schedule_interval(update, 1.0 / 15.0)
def set_focus_next(RapportScreen):
f = focus.FocusBehavior.get_focus_next(RapportScreen)
f.focus = True
# Adding the layout of the entire left side
self.agent = Label(text='DISEASE AGENT', font_size=20)
self.agent_input = CustomTextInput(multiline=False, write_tab=False)
self.agent_wrong = Label(text="THE DISEASE AGENT WAS INCORRECT", color=(1, 1, 1, 0))
self.zero_name = Label(text='PATIENT ZERO NAME', font_size=20)
self.zero_name_input = CustomTextInput(multiline=False, write_tab=False)
self.zero_name_wrong = Label(text="PATIENT ZERO'S NAME WAS INCORRECT", color=(1, 1, 1, 0))
self.structure = Label(text='MOLECULE STRUCTURE', font_size=40)
self.double_bond = Label(text='DOUBLE BONDS', font_size=20)
self.double_bond_input = CustomTextInput(multiline=False, input_type='number', write_tab=False)
self.double_bond_wrong = Label(text="THE NUMBER OF DOUBLE BONDS IS INCORRECT", color=(1, 1, 1, 0))
self.triple_bond = Label(text='TRIPLE BONDS', font_size=20)
self.triple_bond_input = CustomTextInput(multiline=False, input_type='number', write_tab=False)
self.triple_bond_wrong = Label(text="THE NUMBER OF TRIPLE BONDS IS INCORRECT", color=(1, 1, 1, 0))
self.layout2.add_widget(self.agent)
self.layout2.add_widget(self.agent_input)
self.layout2.add_widget(self.agent_wrong)
self.layout2.add_widget(self.zero_name)
self.layout2.add_widget(self.zero_name_input)
self.layout2.add_widget(self.zero_name_wrong)
self.layout2.add_widget(self.structure)
self.layout2.add_widget(self.double_bond)
self.layout2.add_widget(self.double_bond_input)
self.layout2.add_widget(self.double_bond_wrong)
self.layout2.add_widget(self.triple_bond)
self.layout2.add_widget(self.triple_bond_input)
self.layout2.add_widget(self.triple_bond_wrong)
# Check if info is correct. If yes, add to correct-counter, if no, show specific error message. When done counting, procced to ProgressScreen
def check_info(RapportScreen):
correct_answers = 0
if self.agent_input.text.lower() == '123':
correct_answers += 1
self.agent_wrong.color = (1, 0, 0, 0)
else:
self.agent_wrong.color = (1, 0, 0, 1)
if re.search(r'\b123\b', self.zero_name_input.text) is not None:
correct_answers += 1
self.zero_name_wrong.color = (1, 0, 0, 0)
else:
self.zero_name_wrong.color = (1, 0, 0, 1)
if self.double_bond_input.text == '123':
correct_answers += 1
self.double_bond_wrong.color = (1, 0, 0, 0)
else:
self.double_bond_wrong.color = (1, 0, 0, 1)
if self.triple_bond_input.text == '123':
correct_answers += 1
self.triple_bond_wrong.color = (1, 0, 0, 0)
else:
self.triple_bond_wrong.color = (1, 0, 0, 1)
sm.add_widget(ProgressScreen(name='progress', a=correct_answers))
sm.current = 'progress'
# Add check-button
self.check_button = ImageButton(on_press=check_info)
self.layout2.add_widget(self.check_button)
# Switch focus on enter. If at the end of screen, check info
self.agent_input.bind(on_text_validate=set_focus_next)
self.zero_name_input.bind(on_text_validate=set_focus_next)
self.double_bond_input.bind(on_text_validate=set_focus_next)
self.triple_bond_input.bind(on_text_validate=check_info)
class ProgressScreen(BgScreen):
a = NumericProperty(0)
def __init__(self, **kwargs):
super(ProgressScreen, self).__init__(**kwargs)
self.layout = BoxLayout(orientation='vertical', padding=[350, 300])
self.add_widget(self.layout)
# If NOT all answers are correct, go to previous screen, and remove self. If all ARE correct, stop timer and go to EndVideoScreen.
def switch():
if self.a < 4:
sm.current = sm.previous()
sm.remove_widget(self)
else:
timer.stop()
sm.switch_to(EndVideoScreen())
self.calculating = Label(text='CALCULATING', color=(1, 1, 1, 1), font_size=40,
font_name='./Orbitron/V-GERB(bold).ttf')
self.layout.add_widget(self.calculating)
# Animate calculating text (Calculating -> Calculating. -> Calculating.. -> Calculating...)
def calc_anim(RapportScreen):
t = self.calculating.text
if '...' in t:
self.calculating.text = 'CALCULATING'
else:
self.calculating.text = t + '.'
Clock.schedule_interval(calc_anim, 1.0 / 2.0)
# Add animated progressbar
self.progressBar = CustomProgressBar(switch, a=self.a)
self.layout.add_widget(self.progressBar)
class EndVideoScreen(Screen):
def __init__(self, **kwargs):
super(EndVideoScreen, self).__init__(**kwargs)
self.video = Video(source='test.mkv', state='stop')
# Specific function that switches to EscapeRoom screen
def switch(self, kwargs):
sm.switch_to(WinScreen())
# Bind a callback to eos event. Eos is when the video ends
self.video.bind(eos=switch)
self.add_widget(self.video)
# When the screen enters view, play the video
def on_enter(self, *args):
self.video.state = 'play'
class WinScreen(BgScreen):
def __init__(self, **kwargs):
super(WinScreen, self).__init__(**kwargs)
self.layout = FloatLayout()
self.winText = Label(text='CONGRATULATIONS\n\nTHE CURE IS MIXED\n\nTIME: ' + timer.text, font_size=70,
halign='center')
self.add_widget(self.layout)
self.add_widget(self.winText)
# led.blink(5, 5, 1)
# Reset timer and go to start screen
def restart():
keyboard.remove_hotkey(self.hotkey)
timer.a = 3600
sm.switch_to(IntroLogo())
# Add custom KeyBoardListener that triggers the switch-function on enter-key
self.hotkey = keyboard.add_hotkey('ctrl+shift+r', restart)
class FailScreen(BgScreen):
def __init__(self, **kwargs):
super(FailScreen, self).__init__(**kwargs)
self.layout = FloatLayout()
self.failText = Label(text='YOU LOSE\n\nTIME IS UP\n\n' + timer.text, font_size=70, halign='center')
self.add_widget(self.layout)
self.add_widget(self.failText)
# led.blink(5, 5, 1)
def restart():
keyboard.remove_hotkey(self.hotkey)
timer.a = 3600
sm.switch_to(IntroLogo())
# Add custom KeyBoardListener that triggers the switch-function on enter-key
self.hotkey = keyboard.add_hotkey('ctrl+shift+r', restart)
def fail_switch():
sm.switch_to(FailScreen())
# Setup Raspberry GPIO output
# global led
# led = LED(11)
# Add a global timer, that keeps track of the countdown between Screens
global timer
timer = CountdownTimer(fail_switch)
# Add a screen manager and a starting screen. Remove Transition-animations
sm = ScreenManager()
sm.transition = NoTransition()
sm.add_widget(IntroLogo())
class Main(App):
def build(self):
return sm
if __name__ == "__main__":
Main().run()