-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppController.py
177 lines (155 loc) · 6.92 KB
/
AppController.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
import time
from AppGUI import *
from AppModel import *
from tkinter import filedialog
class AppController():
def __init__(self, view: AppGUI, model: AppModel):
self.view = view
self.model = model
self.init_view()
def init_view(self):
self.view.set_save_button_listener(self.save)
self.view.set_load_button_listener(self.load)
self.view.set_new_game_button_listener(self.new_game)
self.view.set_send_button_listener(self.send)
self.view.set_image_prev_button_listener(self.image_prev)
self.view.set_image_next_button_listener(self.image_next)
self.view.set_music_prev_button_listener(self.music_prev)
self.view.set_music_next_button_listener(self.music_next)
self.view.set_music_play_button_listener(self.music_play)
self.view.update_function = self.music_progress_bar_update
def save(self):
self.view.disable_all_buttons()
self.model.save()
self.view.enable_all_buttons()
def load(self):
self.view.disable_all_buttons()
filetypes = (
('Save files', '*.sav'),
('All files', '*.*')
)
file_path = filedialog.askopenfilename(title="Load",
initialdir=self.view.save_path,
filetypes=filetypes)
if file_path != "" and self.model.load(file_path):
self.view.replace_main_text(self.model.main_text)
self.view.replace_image(self.model.get_last_image())
self.music_first()
self.view.enable_all_buttons()
def new_game(self):
self.view.disable_all_buttons()
dialog = NewGameDialog(self.view)
dialog.mainloop()
if dialog.confirm:
information = dialog.get_entered_information()
threading.Thread(target=self._new_game_helper, args=(information,)).start()
else:
self.view.enable_all_buttons()
dialog.destroy()
def _new_game_helper(self, information):
# universe -> first incident
texts = ""
for text in self.model.new_game(*information):
self._sync_main_text()
texts += text + "\n"
# create image
threading.Thread(target=self._create_and_replace_image, args=(texts,)).start()
# create sound
threading.Thread(target=self._create_and_save_music, args=(text,)).start()
self._sync_main_text()
self._enable_all_buttons()
def send(self):
self.view.disable_all_buttons()
user_prompt = self.view.user_textbox.get("0.0", "end").strip()
print(user_prompt)
if user_prompt != "":
threading.Thread(target=self._send_helper, args=(user_prompt,)).start()
else:
self.view.enable_all_buttons()
def _send_helper(self, user_prompt):
if self.model.waiting_user_input:
self._empty_user_textbox()
self._sync_main_text()
answer = self.model.process_user_text(user_prompt)
if answer is not None:
# create image
threading.Thread(target=self._create_and_replace_image, args=(self.model.main_text,)).start()
# create sound
threading.Thread(target=self._create_and_save_music, args=(answer,)).start()
self._sync_main_text()
self._enable_all_buttons()
def _create_and_replace_image(self, context, delay_seconds=0):
time.sleep(delay_seconds)
index = self.model.create_and_save_image(context)
self.view.update_queue.put({"function": self.view.replace_image,
"arg": self.model.get_image(index)})
def music_progress_bar_update(self):
# progress bar update
progress = 0 if self.model.mixer.get_pos() == -1 or self.model.music_length == 0 else (self.model.mixer.get_pos() / 1000.0 / self.model.music_length)
self.view.update_queue.put({"function": self.view.change_progress_bar_value, "arg": progress})
# autoplay next sound
for event in pygame.event.get():
if event.type == self.model.MUSIC_END:
if self.view.music_keep_playing_toggle.get():
print('auto play next music')
success = self.music_next()
if success:
self.music_play()
else:
self._change_music_play_button_label("▶")
def _create_and_save_music(self, text):
self.model.create_and_save_music(text)
def _sync_main_text(self):
self.view.update_queue.put({"function": self.view.replace_main_text, "arg": self.model.main_text})
def _empty_user_textbox(self):
self.view.update_queue.put({"function": self.view.empty_user_textbox})
def _enable_all_buttons(self):
self.view.update_queue.put({"function": self.view.enable_all_buttons})
def _change_music_label(self, text):
self.view.update_queue.put({"function": self.view.change_music_label, "arg": text})
def _change_music_play_button_label(self, text):
self.view.update_queue.put({"function": self.view.change_music_play_button_label, "arg": text})
def image_prev(self):
self.view.disable_all_buttons()
image = self.model.get_prev_image()
if image is not None:
self.view.replace_image(image)
self.view.enable_all_buttons()
def image_next(self):
self.view.disable_all_buttons()
image = self.model.get_next_image()
if image is not None:
self.view.replace_image(image)
self.view.enable_all_buttons()
def music_prev(self):
self.view.disable_all_buttons()
success, index = self.model.load_prev_music()
if success:
self._change_music_label(f"Status: Playing {index}.wav")
self._change_music_play_button_label("▶")
self.view.enable_all_buttons()
def music_play(self):
self.view.disable_all_buttons()
flag, index = self.model.play_music()
if flag == "load":
self._change_music_label(f"Status: Playing {index}.wav")
self._change_music_play_button_label("=")
elif flag == "unpause":
self._change_music_play_button_label("=")
elif flag == "pause":
self._change_music_play_button_label("▶")
self.view.enable_all_buttons()
def music_next(self):
self.view.disable_all_buttons()
success, index = self.model.load_next_music()
if success:
self._change_music_label(f"Status: Playing {index}.wav")
self._change_music_play_button_label("▶")
self.view.enable_all_buttons()
return success
def music_first(self):
self.view.disable_all_buttons()
success, index = self.model.load_first_music()
if success:
self._change_music_label(f"Status: Playing {index}.wav")
self.view.enable_all_buttons()