-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
415 lines (316 loc) · 12.3 KB
/
app.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
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
import os, json, sys
import tkinter as tk
from functools import partial
from PIL import Image
from pyo import SfPlayer, Server as SfServer
from pystray import Icon as Tray, MenuItem as TrayCmd
import frames, utils
from form_wids import DialogBox
class ShelfApp(tk.Tk):
def __new__(cls):
if utils.is_already_running():
return cls._already_running_error()
return super().__new__(cls)
def __init__(
self, screenName=None, baseName=None,
className='Tk', useTk=True, sync=False, use=None
):
super().__init__(screenName, baseName, className, useTk, sync, use)
self.init_vars()
self.init_frames()
def mainloop(self, n=0):
self.init_screen()
self.bind_keys()
self.setup_screen()
return super().mainloop(n)
def destroy(self):
self.music_server.stop()
if self.ST and self.icon_path:
if not self.system_tray:
self.start_tray()
self.music_server.start()
return
else:
self.stop_tray()
if self.asset_dir:
for file in os.listdir(self.asset_dir):
if file.endswith('.ttf'):
utils.load_unload_font(
os.path.join(self.asset_dir, file), load=False
)
return super().destroy()
def lift(self):
super().lift()
self.attributes('-topmost', 1)
self.focus_force()
self.after_idle(self.attributes,'-topmost',False)
def init_vars(self):
self.base_dir, self.asset_dir, self.icon_path = None, None, None
self.dialog_box, self.system_tray = None, None
if getattr(sys, 'frozen', False):
self.base_dir = os.path.dirname(sys.executable)
elif __file__:
self.base_dir = os.path.dirname(os.path.abspath(__file__))
if 'Assets' in os.listdir(self.base_dir):
self.asset_dir = os.path.join(self.base_dir, 'Assets')
self.init_config()
self.load_fonts()
self.init_music_server(self.MV)
self.DEFAULT_WIDTH, self.DEFAULT_HEIGHT = 512, 384
self.RES = dict(S=0.5, M=1, L=1.5, XL=2, FS=2.5)
self.RES['FS'] = self.winfo_screenheight()/self.DEFAULT_HEIGHT
def init_music_server(self, MV=100):
self.music_server = SfServer().boot()
self.music_server.start()
self.music_server.setAmp(MV/100)
if not self.asset_dir:
return
for file in os.listdir(self.asset_dir):
if file.endswith('.ogg'):
self.default_music = SfPlayer(
path=os.path.join(self.asset_dir, file), loop=True
)
self.default_music.out()
return
def init_config(self):
self.config_file = os.path.join(self.base_dir, 'config.json')
if "config.json" not in os.listdir(self.base_dir):
config_data = {}
config_data['screenSize'] = self.SS = "M"
config_data['musicVolume'] = self.MV = 100
config_data['gameOrder'] = self.GO = []
config_data['GBEmulator'] = self.GB = ""
config_data['DSEmulator'] = self.DS = ""
config_data['systemTray'] = self.ST = False
config_data['gameList'], self.GD = [], {}
with open(self.config_file, "w") as f:
json.dump(config_data, f, indent=4)
else:
with open(self.config_file, "r") as config_file:
config_data = json.load(config_file)
self.SS = config_data.get("screenSize", "M")
self.MV = config_data.get("musicVolume", 10)
self.GO = config_data.get("gameOrder", [])
self.GB = config_data.get("GBEmulator", "")
self.DS = config_data.get("DSEmulator", "")
self.ST = bool(config_data.get("systemTray", False))
self.GD = { i['oid']: i for i in config_data.get("gameList", []) }
self._check_and_fix_order()
def save_config(self):
self.config_file = os.path.join(self.base_dir, 'config.json')
self._check_and_fix_order()
with open(self.config_file, "w") as f:
json.dump({
"screenSize": getattr(self, "SS", "M"),
"musicVolume": getattr(self, "MV", 100),
"gameOrder": getattr(self, "GO", []),
"GBEmulator": getattr(self, "GB", ""),
"DSEmulator": getattr(self, "DS", ""),
"systemTray": bool(getattr(self, "ST", False)),
"gameList": list(getattr(self, "GD", {}).values()),
}, f, indent=4)
def init_screen(self):
self.title('PokéShelf')
self.resizable(False, False)
self.configure(background='black')
self.init_icon()
self.init_menu()
def init_icon(self):
if not self.asset_dir:
return
for file in os.listdir(self.asset_dir):
if file.endswith('.ico'):
self.icon_path = os.path.join(self.asset_dir, file)
self.iconbitmap(self.icon_path)
return
def init_frames(self):
frames.OptionsFrame(self)
frames.AboutFrame(self)
frames.GameSettingsFrame(self)
frames.ShelfFrame(self)
frames.SplashFrame(self)
def init_menu(self):
menu_bar = tk.Menu(self, name="menu")
game_menu = tk.Menu(menu_bar, tearoff=False, name="game")
game_menu_data = [
{"lbl": "📚 PokéShelf", 'func': 'toggle_frame', 'func_arg': ['shelf'], 'con':'s'}, {'sep': True},
{"lbl": "➕ Add Game", 'func': 'toggle_frame', 'func_arg': ['gamesettings'], 'con':'a'},
{"lbl": "🔧 Edit Game", 'func': 'toggle_frame', 'func_arg': ['gamesettings', {'edit':True}], 'con':'e'},
{"lbl": "➖ Remove Game", 'func': 'remove_game', 'con':'r'},
{'sep': True}, {"lbl": "❌ Close Menu"},
]
for data in game_menu_data:
is_sep, command = data.get('sep', False), data.get('func', None)
if is_sep:
game_menu.add_separator()
continue
command = partial(getattr(self, command), *data.get('func_arg', [])) if command else None
game_menu.add_command(label=data['lbl'], command=command)
if command:
func = partial(self._shortcut_event, menu=game_menu, lbl=data['lbl'], cmd=command)
self.bind(f'<Control-{data["con"].lower()}>', func)
self.bind(f'<Control-{data["con"].upper()}>', func)
menu_bar.add_cascade(label="🎮 Game", menu=game_menu)
label, command = "⚙️ Options", partial(self.toggle_frame, "options")
menu_bar.add_command(label=label, command=command)
self.bind(f'<Alt-s>', partial(self._shortcut_event, menu=menu_bar, lbl=label, cmd=command))
self.bind(f'<Alt-S>', partial(self._shortcut_event, menu=menu_bar, lbl=label, cmd=command))
label, command = "💡 About", partial(self.toggle_frame, "about")
menu_bar.add_command(label=label, command=command)
self.bind(f'<Alt-a>', partial(self._shortcut_event, menu=menu_bar, lbl=label, cmd=command))
self.bind(f'<Alt-A>', partial(self._shortcut_event, menu=menu_bar, lbl=label, cmd=command))
self.config(menu=menu_bar)
def init_tray(self):
self.system_tray = Tray(
name="PokéShelf : Tray", title="PokéShelf",
icon=Image.open(self.icon_path),
menu=(
TrayCmd('PokéShelf', partial(self.stop_tray, fr="splash")),
TrayCmd('Options', partial(self.stop_tray, fr="options")),
TrayCmd('About', partial(self.stop_tray, fr="about")),
TrayCmd('Quit', self.destroy))
)
def set_resolution(self, SS):
width = int(self.DEFAULT_WIDTH * self.RES[SS])
height = int(self.DEFAULT_HEIGHT * self.RES[SS])
self.is_fs, self.SS = bool(SS=='FS'), SS
self.geometry(f'{width}x{height}')
self.attributes('-fullscreen', self.is_fs)
cavas_width = int(
(self.winfo_width() - (self.winfo_height()/0.75))/2
) if self.is_fs else 0
for child in self.children.values():
if getattr(child, 'l_canvas', None):
child.l_canvas.config(width=cavas_width)
if getattr(child, 'r_canvas', None):
child.r_canvas.config(width=cavas_width)
def setup_screen(self, frame_name="splash"):
self.set_resolution(self.SS)
self.toggle_frame(frame_name)
def load_fonts(self):
if not self.asset_dir:
return
for file in os.listdir(self.asset_dir):
if file.endswith('.ttf'):
utils.load_unload_font(os.path.join(self.asset_dir, file))
def create_frame(self, name, pre=None, post=None):
frame = tk.Frame(
self, bg="black", highlightthickness=5, name=str.lower(name),
highlightbackground="black", highlightcolor="black"
)
frame.pack(expand=True, fill='both')
frame.grid_columnconfigure(0, weight=1)
frame.grid_columnconfigure(1, weight=1000)
frame.grid_columnconfigure(2, weight=1)
frame.display_name = name
frame.pre_pack = pre if pre else lambda _: None
frame.post_forget = post if post else lambda _: None
frame.l_canvas = tk.Canvas(frame, width=0, background="black", highlightthickness=0)
frame.l_canvas.grid(column=0, row=0, rowspan=100, sticky=tk.NS)
frame.r_canvas = tk.Canvas(frame, width=0, background="black", highlightthickness=0)
frame.r_canvas.grid(column=2, row=0, rowspan=100, sticky=tk.NS)
return frame
def frame_resize(self, widget, event=None):
for wid in getattr(widget, 'winfo_children', lambda : [])():
resize = getattr(wid, "resize", None)
if resize:
resize(event)
self.frame_resize(wid, event)
def toggle_frame(self, frame_name, pre_pack_args=None):
nxt_fr = self.children[frame_name]
for child in self.winfo_children():
if not isinstance(child, tk.Frame) or nxt_fr == frame_name:
continue
child.pack_forget()
if getattr(self, 'cur_fr', None) and child == self.cur_fr:
child.post_forget(child)
self.frame_resize(nxt_fr)
self.cur_fr = nxt_fr
nxt_fr.pre_pack(nxt_fr, **(pre_pack_args or {}))
nxt_fr.pack(fill='both', expand=True)
display_name, title = self.cur_fr.display_name, 'PokéShelf'
title += f" : {display_name}" if display_name != "Shelf" else ""
self.title(title)
def bind_keys(self):
for i in ['Escape', 'X', 'x']:
self.bind(f"<{i}>", self._escape_event)
controls = {
"prv_op": ["Up", "Shift-Tab"], "nxt_op": ["Down", "Tab"],
"dec_val": ["Left"], "inc_val": ["Right"],
"run_op": ['Return', 'C', 'c', 'Z', 'z', 'space']
}
for func, keys in controls.items():
for key in keys:
self.bind(f"<{key}>", partial(self._interaction_event, func_name=func))
def start_tray(self):
self.withdraw()
self.init_tray()
self.system_tray.run()
self.system_tray = None
def stop_tray(self, tray=None, cmd=None, fr=None):
self.system_tray.stop()
self.system_tray = None
if fr:
self.after(0, lambda: self._win_from_tray(fr))
def _win_from_tray(self, fr):
self.deiconify()
self.lift()
self.toggle_frame(fr)
def add_or_update_game(self, exe_val, img_val, mus_val):
self.cur_game.update({"exe": exe_val, "img": img_val, "mus": mus_val})
oid_val = self.cur_game.get(
'oid', (set(range(1, len(self.GD) + 2)) - set(self.GD)).pop()
)
self.cur_game['oid'] = oid_val
self.GD[oid_val] = self.cur_game
if oid_val not in self.GO:
self.GO.append(oid_val)
def remove_game(self):
self.GO.remove(self.cur_game['oid'])
self.GD.pop(self.cur_game['oid'])
self.cur_game = {}
self.save_config()
DialogBox(self, "Game Removed!")
if self.GD:
self.toggle_frame('shelf')
else:
self.toggle_frame('splash')
def _escape_event(self, event):
if event.widget.dialog_box:
return
if event.widget.cur_fr == event.widget.children["splash"]:
event.widget.destroy()
else:
event.widget.toggle_frame("splash")
def _shortcut_event(self, event, menu, lbl, cmd):
if event.widget.dialog_box:
return
cmd() if menu.entrycget(lbl, 'state') == "normal" else None
def _interaction_event(self, event, func_name):
if event.widget.dialog_box:
return
if func_name:
func = getattr(event.widget.cur_fr, func_name, None)
func() if func else None
def _check_and_fix_order(self):
# uniquify
unique_g_order = []
for x in self.GO:
if x not in unique_g_order:
unique_g_order.append(x)
self.GO = unique_g_order
# maintain 1-to-1 b/w g_order & g_dict
g_order = set(self.GO)
g_dict = set(self.GD)
items_to_be_removed = g_order - g_dict
items_to_be_added = g_dict - g_order
for item in items_to_be_removed:
self.GO.remove(item)
for item in items_to_be_added:
self.GO.append(item)
@classmethod
def _already_running_error(cls):
tk.Tk().withdraw()
tk.messagebox.showerror(
title='PokéShelf : Error', message="PokéShelf Already Running!"
)