-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
527 lines (437 loc) · 21.8 KB
/
main.py
File metadata and controls
527 lines (437 loc) · 21.8 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
import sys
from PySide6.QtGui import QCursor
import autostart
from checklist import MenuScroll
from saveload import load_user, save_user
import audio_player
from event import load_event_list
from pathretriever import R
from PySide6 import QtWidgets, QtCore, QtGui
from PySide6.QtGui import QFont, QFontDatabase, QPixmap, QAction, QIcon, QMovie
from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QMenu, QPushButton, QSystemTrayIcon, QWidget, QScrollArea, QVBoxLayout, QSizePolicy
from user import run_at_midnight
SCROLL_WIDTH = 250
DEFAULT_DATA_FILE = 'data/default_data.json'
USER_DATA_FILE = 'data/user.json'
class EventSpeechBubble(QLabel):
clicked = QtCore.Signal()
def __init__(self, script, parent=None):
super().__init__(parent)
self.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor)) # Optional: hand cursor
self.script = script
self.curr_index = 0
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.MouseButton.LeftButton:
self.clicked.emit()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.hidden = False
self.setMouseTracking(True)
self.user = load_user(R(USER_DATA_FILE))
print(self.user.streaks)
# AUDIO
self.sfx_player = audio_player.AudioPlayer()
self.bgm_player = audio_player.AudioPlayer()
# Load sprout image
# Load and resize sprout image
original_pixmap = QPixmap(R("assets/Base_Bg.png"))
scaled_width = 300 # change this as needed
scaled_pixmap = original_pixmap.scaledToWidth(scaled_width, QtCore.Qt.TransformationMode.SmoothTransformation)
self.pixmap = scaled_pixmap
self.image_label = QLabel(self)
self.set_background()
# Window appearance
self.setWindowFlags(
QtCore.Qt.WindowType.Window |
QtCore.Qt.WindowType.FramelessWindowHint |
QtCore.Qt.WindowType.WindowStaysOnTopHint |
QtCore.Qt.WindowType.Tool
)
self.setAttribute(QtCore.Qt.WidgetAttribute.WA_TranslucentBackground)
# Resize and position
self.resize(self.pixmap.size())
self.move_to_bottom_right_above_taskbar()
# System tray icon
self.tray_icon = QSystemTrayIcon(QIcon(R("assets/Base_Bg_Wide.png")), self)
tray_menu = QMenu()
restore_action = QAction("Restore", self)
restore_action.triggered.connect(self.showNormal)
tray_menu.addAction(restore_action)
quit_action = QAction("Quit", self)
quit_action.triggered.connect(QApplication.quit)
tray_menu.addAction(quit_action)
self.tray_icon.setContextMenu(tray_menu)
self.tray_icon.setToolTip("Sprout App")
self.tray_icon.show()
icon_pixmap = QPixmap(R("assets/scroll_closed.png")).scaledToWidth(SCROLL_WIDTH,
QtCore.Qt.SmoothTransformation)
icon_width = icon_pixmap.width()
icon_height = icon_pixmap.height()
# Center horizontally, align to bottom (10px above the edge)
x = (self.pixmap.width() - icon_width) // 2
y = self.pixmap.height() - icon_height
self.menu_scroll = MenuScroll(self.user, x, y, icon_width, icon_height, self)
run_at_midnight(self.midnight_update)
self.menu_scroll.setGeometry(x, y, icon_width, icon_height)
self.menu_scroll.setPixmap(icon_pixmap)
self.menu_scroll.setScaledContents(True)
self.menu_scroll.clicked.connect(self.change_background_on_toggle)
# Play GIF animation on top of main window at startup, matching background size and position
gif_x = self.image_label.x()
gif_y = self.image_label.y()
gif_width = self.image_label.width()
gif_height = self.image_label.height()
Event = load_event_list()
if self.user.event_index < len(Event):
self.begin_event(gif_x, gif_y, gif_width, gif_height, Event[self.user.event_index])
def left_arrow_key_pressed_event(self, event):
boundaries = [2, 4, 7, 14, 20]
for i in range(len(boundaries)):
if self.user.streaks <= boundaries[i]:
self.user.streaks = boundaries[(i - 1) % len(boundaries)]
self.set_background()
print(f"Streaks updated to: {self.user.streaks}")
self.menu_scroll.update_subtitle()
save_user(self.user, R(USER_DATA_FILE))
return
self.user.streaks = 14
self.set_background()
print(f"Streaks updated to: {self.user.streaks}")
self.menu_scroll.update_subtitle()
save_user(self.user, R(USER_DATA_FILE))
def right_arrow_key_pressed_event(self, event):
boundaries = [2, 4, 7, 14, 20]
for i in range(len(boundaries)):
if self.user.streaks <= boundaries[i]:
self.user.streaks = boundaries[(i + 1) % len(boundaries)]
self.set_background()
print(f"Streaks updated to: {self.user.streaks}")
self.menu_scroll.update_subtitle()
save_user(self.user, R(USER_DATA_FILE))
return
self.user.streaks = 2
self.set_background()
print(f"Streaks updated to: {self.user.streaks}")
self.menu_scroll.update_subtitle()
save_user(self.user, R(USER_DATA_FILE))
def midnight_update(self):
self.menu_scroll.update_subtitle()
self.user.check_streak()
def change_background_on_toggle(self):
self.set_background()
self.menu_scroll.toggle_scroll()
self.sfx_player.play_sfx(R("assets/audio/sfx/scroll.mp3"))
def choose_background(self):
if self.user.streaks >= 20:
return 'Base_Bg5.png'
elif self.user.streaks >= 14:
return 'Base_Bg4.png'
elif self.user.streaks >= 7:
return 'Base_Bg3.png'
elif self.user.streaks >= 4:
return 'Base_Bg2.png'
elif self.user.streaks >= 2:
return 'Base_Bg1.png'
else:
return 'Base_Bg.png'
def set_background(self):
original_pixmap = QPixmap(R(f"assets/{self.choose_background()}"))
scaled_width = 300
scaled_pixmap = original_pixmap.scaledToWidth(scaled_width,
QtCore.Qt.TransformationMode.SmoothTransformation)
self.pixmap = scaled_pixmap
self.image_label.setPixmap(self.pixmap)
self.image_label.setFixedSize(self.pixmap.size())
self.image_label.lower()
def move_to_bottom_right_above_taskbar(self):
"""Move window to bottom-right corner, above taskbar."""
screen = QApplication.primaryScreen().availableGeometry()
window_rect = self.geometry()
x = screen.x() + screen.width() - window_rect.width()
y = screen.y() + screen.height() - window_rect.height()
self.move(x, y)
def contextMenuEvent(self, event):
menu = QMenu(self)
hide_action = QAction("Hide to Tray", self)
hide_action.triggered.connect(self.hide)
self.hidden = True
relocate_action = QAction("Relocate", self)
relocate_action.triggered.connect(self.relocate)
quit_action = QAction("Quit", self)
quit_action.triggered.connect(QApplication.quit)
reset_action = QAction("Reset Data", self)
reset_action.triggered.connect(self.reset_data)
menu.addAction(hide_action)
menu.addAction(relocate_action)
menu.addAction(quit_action)
menu.addAction(reset_action)
menu.exec(event.globalPos())
def relocate(self):
class EnterFilter(QtCore.QObject):
enter_pressed = QtCore.Signal()
def eventFilter(self, obj, ev):
if ev.type() == QtCore.QEvent.Type.KeyPress:
if ev.key() in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
self.enter_pressed.emit()
return True
return False
filter = EnterFilter(self)
app = QApplication.instance()
app.installEventFilter(filter)
self.running_relocate = True
filter.enter_pressed.connect(self.end_running_relocate)
while self.running_relocate:
pos = QCursor.pos()
self.move(pos.x() - self.width() // 2, pos.y() - self.height() // 2)
QtCore.QCoreApplication.processEvents()
QtCore.QThread.msleep(10)
if not self.running_relocate:
break
app.removeEventFilter(filter)
def end_running_relocate(self):
self.running_relocate = False
def reset_data(self):
"""Reset user data to default."""
self.user = load_user(DEFAULT_DATA_FILE)
save_user(self.user, R(USER_DATA_FILE))
self.menu_scroll.update_menu()
self.menu_scroll.update_subtitle()
self.set_background()
print("Data reset to default.")
def closeEvent(self, event):
event.ignore()
self.hide()
def resizeEvent(self, event):
super().resizeEvent(event)
def on_button_click(self, index):
print(f"Button {index + 1} clicked!")
def setup_speech_bubble(self, messages):
bubble_pixmap = QPixmap(R("assets/speech_bubble.png")).scaledToWidth(SCROLL_WIDTH,
QtCore.Qt.SmoothTransformation)
self.speech_bubble = EventSpeechBubble(messages, self)
bubble_width = bubble_pixmap.width()
bubble_height = bubble_pixmap.height()
# Center horizontally, align to bottom (10px above the edge)
x = (self.pixmap.width() - bubble_width) // 2
y = self.pixmap.height() - bubble_height
self.speech_bubble.setGeometry(x, y, bubble_width, bubble_height)
self.speech_bubble.setPixmap(bubble_pixmap)
self.speech_bubble.setScaledContents(True)
self.speech_text = QLabel(self.speech_bubble)
self.speech_text.setText(self.speech_bubble.script[0])
self.speech_text.setWordWrap(True)
self.speech_text.setAlignment(QtCore.Qt.AlignmentFlag.AlignTop | QtCore.Qt.AlignmentFlag.AlignHCenter)
self.speech_text.setGeometry(bubble_width // 2, bubble_height // 2, self.menu_scroll.width(), self.menu_scroll.height())
self.speech_text.setStyleSheet("""
background-color: transparent;
color: white;
padding: 4px 10px;
border-radius: 6px;
font-size: 12px;
""")
self.speech_bubble.clicked.connect(self.on_speech_bubble_clicked)
def on_speech_bubble_clicked(self, event):
if self.speech_bubble.curr_index < len(self.speech_bubble.script) - 1:
self.speech_bubble.curr_index += 1
self.speech_text.setText(self.speech_bubble.script[self.speech_bubble.curr_index])
else:
self.speech_bubble.hide()
def begin_event(self, x, y, width, height, messages):
self.user.event_index += 1
self.setup_speech_bubble(messages)
self.speech_bubble.hide()
# Overlay the GIF label on top of the main window, matching background
if hasattr(self, 'gif_label'):
self.gif_label.deleteLater()
self.gif_label = QLabel(self)
self.gif_label.setGeometry(x, y, width, height)
self.gif_label.setAttribute(QtCore.Qt.WidgetAttribute.WA_TranslucentBackground)
self.gif_label.setStyleSheet('background: transparent;')
self.movie = QMovie(R(f"assets/walk_in.gif"))
if not self.movie.isValid():
print(f"GIF not found or invalid: assets/walk_in.gif")
self.gif_label.setMovie(self.movie)
self.gif_label.setScaledContents(True)
self.gif_label.show()
self.gif_label.raise_() # Ensure it's on top
self._gif_target_frame = 2 # 0-based index, so frame 3 is index 2
self.movie.frameChanged.connect(self._on_gif_frame_changed)
self.movie.start()
def set_stand_still_png(self):
if hasattr(self, 'gif_label'):
self.gif_label.setMovie(None)
standstill_movie = QMovie(R("assets/stand_still.gif"))
if not standstill_movie.isValid():
print("stand_still.gif not found or invalid!")
return
self.gif_label.setMovie(standstill_movie)
self.gif_label.setScaledContents(True)
standstill_movie.start()
# Show text bubble after stationary gif begins playing
# wait 0.15 seconds before showing text bubble
QtCore.QTimer.singleShot(200, lambda: self.show_text_bubble())
def _on_gif_frame_changed(self, frame_number):
if hasattr(self, '_gif_target_frame') and hasattr(self, 'movie') and frame_number == self._gif_target_frame:
self.movie.stop()
self.set_stand_still_png()
def show_text_bubble(self):
# Always show the first message in the sequence
if hasattr(self, 'speech_bubble'):
self.speech_bubble.curr_index = 0
text = self.speech_bubble.script[0]
else:
text = ""
# If a text bubble already exists, just update the text
if hasattr(self, 'text_bubble_label') and self.text_bubble_label.isVisible():
if hasattr(self, 'text_bubble_text'):
self.text_bubble_text.setText("")
self._start_typewriter_animation(text)
else:
# Fallback: recreate text label if missing
bubble_width = self.text_bubble_label.width()
bubble_height = self.text_bubble_label.height()
margin_x = int(bubble_width * 0.18)
margin_y = 20
self.text_bubble_text = QLabel(self.text_bubble_label)
self.text_bubble_text.setText("")
self.text_bubble_text.setWordWrap(True)
self.text_bubble_text.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.text_bubble_text.setGeometry(margin_x, margin_y, bubble_width - 2 * margin_x, bubble_height - 2 * margin_y)
self.text_bubble_text.setStyleSheet("background: transparent; color: black; font-size: 16px; padding: 4px;")
self.text_bubble_text.show()
self._start_typewriter_animation(text)
return
# Otherwise, create a new text bubble
if hasattr(self, 'text_bubble_label'):
self.text_bubble_label.deleteLater()
bubble_pixmap = QPixmap(R("assets/text_bubble.png"))
bubble_width = int(self.width() // 1.7)
bubble_height = int(self.height() // 1.7)
bubble_x = 0
bubble_y = 80
self.text_bubble_label = QLabel(self)
self.text_bubble_label.setPixmap(bubble_pixmap.scaled(bubble_width, bubble_height, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation))
self.text_bubble_label.setGeometry(bubble_x, bubble_y, bubble_width, bubble_height)
self.text_bubble_label.setScaledContents(True)
self.text_bubble_label.setAttribute(QtCore.Qt.WidgetAttribute.WA_TranslucentBackground)
self.text_bubble_label.setStyleSheet('background: transparent;')
self.text_bubble_label.show()
# Add a text label on top of the bubble
if hasattr(self, 'text_bubble_text'):
self.text_bubble_text.deleteLater()
self.text_bubble_text = QLabel(self.text_bubble_label)
self.text_bubble_text.setText("")
self.text_bubble_text.setWordWrap(True)
self.text_bubble_text.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
margin_x = int(bubble_width * 0.18)
margin_y = 20
self.text_bubble_text.setGeometry(margin_x, margin_y, bubble_width - 2 * margin_x, bubble_height - 2 * margin_y)
self.text_bubble_text.setStyleSheet("background: transparent; color: black; font-size: 16px; padding: 4px;")
self.text_bubble_text.show()
# Make bubble clickable for next message
self.text_bubble_label.mousePressEvent = self._on_text_bubble_clicked
self._start_typewriter_animation(text)
def _on_text_bubble_clicked(self, event):
# If animation is running, finish instantly
if hasattr(self, '_typewriter_timer') and self._typewriter_timer.isActive() and self._typewriter_index <= len(self._typewriter_text):
self._typewriter_timer.stop()
self.sfx_player.stop()
self.text_bubble_text.setText(self._typewriter_text)
else:
# Advance to next message or hide if at the end
if self.speech_bubble.curr_index < len(self.speech_bubble.script) - 1:
self.speech_bubble.curr_index += 1
next_text = self.speech_bubble.script[self.speech_bubble.curr_index]
self.text_bubble_text.setText("")
self._start_typewriter_animation(next_text)
else:
self.speech_bubble.hide()
self.text_bubble_label.hide()
# After bubble is done, wait 0.3s then play walking_out.gif
QtCore.QTimer.singleShot(300, self.play_walking_out_gif)
def _start_typewriter_animation(self, full_text):
# Cancel any previous animation
if hasattr(self, '_typewriter_timer') and self._typewriter_timer is not None:
self._typewriter_timer.stop()
self._typewriter_timer.deleteLater()
self._typewriter_index = 0
self._typewriter_text = full_text
self._typewriter_timer = QtCore.QTimer(self)
self._typewriter_timer.timeout.connect(self._update_typewriter_text)
self._typewriter_timer.start(25) # Adjust speed here (ms per character)
self.sfx_player.play_sfx(R("assets/audio/sfx/irene.mp3")) # Play sound effect for 2 seconds
def _update_typewriter_text(self):
if self._typewriter_index <= len(self._typewriter_text):
self.text_bubble_text.setText(self._typewriter_text[:self._typewriter_index])
self._typewriter_index += 1
else:
self.sfx_player.stop()
self._typewriter_timer.stop()
def play_walking_out_gif(self):
# Play walking_out.gif at the same position as the sprout
gif_x = self.image_label.x()
gif_y = self.image_label.y()
gif_width = self.image_label.width()
gif_height = self.image_label.height()
if hasattr(self, 'gif_label'):
self.gif_label.deleteLater()
self.gif_label = QLabel(self)
self.gif_label.setGeometry(gif_x, gif_y, gif_width, gif_height)
self.gif_label.setAttribute(QtCore.Qt.WidgetAttribute.WA_TranslucentBackground)
self.gif_label.setStyleSheet('background: transparent;')
self.movie = QMovie(R("assets/walking_out.gif"))
if not self.movie.isValid():
print("GIF not found or invalid: assets/walking_out.gif")
self.gif_label.setMovie(self.movie)
self.gif_label.setScaledContents(True)
self.gif_label.show()
self.gif_label.raise_()
self.movie.frameChanged.connect(self.walking_out_frame_changed)
self.movie.start()
self._gif_target_frame = 2 # 0-based index, so frame 3 is index 2
def walking_out_frame_changed(self, frame_number):
if hasattr(self, 'movie') and frame_number == self._gif_target_frame:
# wait 0.3 seconds
QtCore.QTimer.singleShot(300, self._on_walking_out_finished)
def _on_walking_out_finished(self):
if hasattr(self, 'gif_label'):
save_user(self.user, R(USER_DATA_FILE))
self.movie.stop()
self.gif_label.deleteLater()
# App resumes normal operation (no further action needed)
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Z:
self.left_arrow_key_pressed_event(event)
print("z pressed")
elif event.key() == QtCore.Qt.Key_X:
self.right_arrow_key_pressed_event(event)
print("x pressed")
else:
super().keyPressEvent(event)
if __name__ == "__main__":
autostart.add_to_startup()
app = QApplication(sys.argv)
font_id = QFontDatabase.addApplicationFont(R("assets/font/Atlantistextregular-qZv0.ttf"))
if font_id == -1:
print("Failed to load font")
else:
font_family = QFontDatabase.applicationFontFamilies(font_id)[0]
app.setFont(QFont(font_family, 14)) # Set globally
app.setQuitOnLastWindowClosed(False)
window = MainWindow()
bgm_player = audio_player.AudioPlayer()
if window.user.streaks >= 22:
bgm_player.play_bg_track(R("assets/audio/bgm/bigbig_tree_track.mp3"))
elif window.user.streaks >= 15:
bgm_player.play_bg_track(R("assets/audio/bgm/big_tree.mp3"))
elif window.user.streaks >= 9:
bgm_player.play_bg_track(R("assets/audio/bgm/mid_tree.mp3"))
elif window.user.streaks >= 5:
bgm_player.play_bg_track(R("assets/audio/bgm/tree_teen.mp3"))
elif window.user.streaks >= 3:
bgm_player.play_bg_track(R("assets/audio/bgm/sprout.mp3"))
else:
bgm_player.play_bg_track(R("assets/audio/bgm/brown_noise.mp3"))
window.show()
sys.exit(app.exec())