-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui_main.py
More file actions
189 lines (143 loc) · 5.22 KB
/
gui_main.py
File metadata and controls
189 lines (143 loc) · 5.22 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
import tkinter as tk
from tkinter import font as tkfont
from multiprocessing import Process, Queue
from PIL import Image, ImageTk
import time
import math
from utils.gif import GifPlayer
from utils.get_path import resource_path
from workers.assistant import assistant_worker
from workers.infoExtracter import infoExtration
ACTIVE_TIMEOUT = 10
class BuddyGUI:
def __init__(self, root):
self.root = root
self.root.geometry("300x300")
self.label = tk.Label(root)
self.label.pack(expand=True, fill="both")
custom_font = ("Comic Sans MS", 8, "bold")
self.heard_label = tk.Label(
root,
text="",
justify="left",
font=custom_font,
bg="black",
fg="white",
anchor="nw"
)
self.heard_label.place(x=0, y=270, width=300)
self.tk_font = tkfont.Font(font=custom_font)
# GIFs
self.idle_gif = GifPlayer(self.label, resource_path("face/Idle.gif"), 300, 300)
self.listen_gif = GifPlayer(self.label, resource_path("face/listening.gif"), 300, 300)
self.thinking_gif = GifPlayer(self.label, resource_path("face/thinking.gif"), 300, 300)
self.talking_gif = GifPlayer(self.label, resource_path("face/Talking1.gif"), 300, 300)
self.set_gif(self.idle_gif)
# ---------------- Multiprocessing setup ----------------
self.queue = Queue()
self.task_queue = Queue()
self.assistant_process = Process(
target=assistant_worker,
args=(self.queue,self.task_queue),
daemon=True
)
self.infoExtract_process = Process(
target=infoExtration,
args=(self.task_queue,),
daemon=True
)
self.assistant_process.start()
self.infoExtract_process.start()
self.root.after(50, self.check_queue)
def update_heard_text(self, text: str):
self.heard_label.config(text=text)
max_width = self.heard_label.winfo_width() or 250
words = text.split(" ")
lines = []
current_line = ""
for word in words:
if self.tk_font.measure(current_line + " " + word) <= max_width:
current_line += " " + word if current_line else word
else:
lines.append(current_line)
current_line = word
if current_line:
lines.append(current_line)
line_height = self.tk_font.metrics("linespace")
new_height = line_height * len(lines) + 3
self.heard_label.place(height=new_height)
def set_gif(self, gif):
if hasattr(self, "current_gif") and self.current_gif == gif:
return
self.idle_gif.stop()
self.listen_gif.stop()
self.thinking_gif.stop()
self.talking_gif.stop()
self.current_gif = gif
gif.play()
def animate_screenshot(self, path):
img = Image.open(path).resize((180, 110))
tk_img = ImageTk.PhotoImage(img)
shot = tk.Label(self.root, image=tk_img, bd=0)
shot.image = tk_img
start_x = -220
center_x = 60
y = 100
shot.place(x=start_x, y=y)
FPS = 60
duration = 0.6 # seconds for movement
hold_time = 2000 # 2 sec wait
# -------- easing functions --------
def ease_out(t):
return 1 - (1 - t) ** 3
def ease_in(t):
return t ** 3
def ease_out_back(t):
c1 = 1.70158
c3 = c1 + 1
return 1 + c3 * (t - 1) ** 3 + c1 * (t - 1) ** 2
def animate(start, end, easing, callback=None):
start_time = time.time()
def step():
elapsed = time.time() - start_time
t = min(elapsed / duration, 1)
value = start + (end - start) * easing(t)
shot.place(x=value, y=y)
if t < 1:
self.root.after(int(1000 / FPS), step)
elif callback:
callback()
step()
# -------- sequence --------
# come in smoothly
def go_center():
animate(start_x, center_x, ease_out, hold)
# wait 2 sec
def hold():
self.root.after(hold_time, go_back)
# bounce back left
def go_back():
animate(center_x, start_x, ease_out_back, shot.destroy)
go_center()
def check_queue(self):
while not self.queue.empty():
msg_type, value = self.queue.get()
if msg_type == "heard":
self.update_heard_text(value)
elif msg_type == "gif":
if value == "idle":
self.set_gif(self.idle_gif)
elif value == "listen":
self.set_gif(self.listen_gif)
elif value == "thinking":
self.set_gif(self.thinking_gif)
elif value == "talking":
self.set_gif(self.talking_gif)
elif msg_type == "screenshot":
self.animate_screenshot(value)
self.root.after(50, self.check_queue)
if __name__ == "__main__":
root = tk.Tk()
root.title("Misaki")
BuddyGUI(root)
root.mainloop()