-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_main.py
executable file
·303 lines (204 loc) · 10.6 KB
/
gui_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
#!/usr/bin/env python3
import customtkinter
import tkinter
from PIL import Image
import tomllib
from GameBrowserFrame import GameBrowserFrame
import game_parser
import os
with open("config.toml", "rb") as f:
config = tomllib.load(f)
class TierFrame(customtkinter.CTkFrame):
def __init__(self, master, **kwargs):
super().__init__(master, **kwargs)
self.guids = []
self.labels = []
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.geometry("1200x1200")
self.minsize(600,600)
self.grid_rowconfigure(0, weight=10)
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure((0), weight=1)
self.tabview = customtkinter.CTkTabview(master=self)
self.tabview.grid(row=0, column=0, padx=20, pady=20, sticky="nsew")
self.tabview.add("Tier list") # add tab at the end
self.tabview.add("Game Search") # add tab at the end
self.tabview.set("Tier list") # set currently visible tab
self.gamebrowserframe = GameBrowserFrame(master=self.tabview.tab("Game Search"))
self.gamebrowserframe.pack(fill=customtkinter.BOTH, expand=1)
self.gamebrowserframe.append_button_all.configure(command=self.append_all_images_button)
self.gamebrowserframe.append_button.configure(command=self.append_selected_image_button)
# creating tiers
self.tiers = config['tiers']
self.tiersColors = config['tiersColors']
self.logo_n_container_tier = []
self.logo_tier = []
self.container_tier = []
for i in range(len(self.tiers)):
self.logo_n_container_tier.append(customtkinter.CTkFrame(master=self.tabview.tab("Tier list")))
self.logo_tier.append(customtkinter.CTkLabel(master=self.logo_n_container_tier[i],
height=170,
width=40,
fg_color=self.tiersColors[i],
text=self.tiers[i])
)
self.logo_tier[i].pack(side=customtkinter.LEFT)
self.container_tier.append(TierFrame(master=self.logo_n_container_tier[i],
height=170,
fg_color='#044536'))
self.container_tier[i].pack(side=customtkinter.LEFT, fill=customtkinter.X, expand=1)
self.logo_n_container_tier[i].pack(side=customtkinter.TOP, fill=customtkinter.X, expand=1)
# self.label_a = customtkinter.CTkLabel(master=self.tabview.tab("Tier list"),
# height=170,
# width=40,
# fg_color='blue',
# text='A')
# self.label_a.pack(side=customtkinter.LEFT)
#self.gamebrowserframe.grid(row=0, column=0, padx=20, pady=20, sticky="nsew")
self.containerFrame = customtkinter.CTkScrollableFrame(master=self, orientation='horizontal', height=120)
self.containerFrame.grid(row=1, column=0, padx=20, pady=20, sticky="ew")
self.contained_guids = []
self.contained_labels = []
self.choosen_guid = ''
self.context_menu = tkinter.Menu(self, tearoff=0)
for t in self.tiers:
self.context_menu.add_command(label=f"Move to tier {t}",
command= lambda arg=t: self.context_move_to_tier(arg))
self.context_menu.add_separator()
self.context_menu.add_command(label="Remove Game", command=self.context_menu_remove)
self.context_menu.add_command(label="Close")
self.tier_context_menu = tkinter.Menu(self, tearoff=0)
self.tier_context_menu.add_command(label="Move to the right <", command=self.context_tier_move_right)
self.tier_context_menu.add_command(label="Move to the left >", command=self.context_tier_move_left)
self.tier_context_menu.add_command(label="Remove Game", command=self.context_tier_remove_game)
self.tier_context_menu.add_command(label="Close")
def append_game_to_container(self, guid):
if guid in self.contained_guids:
print('Game already in container')
return
self.contained_guids.append(guid)
game_data, image_path = game_parser.search_cache_data(guid)
# Load the image
pillow_image = Image.open(image_path)
image_ratio = pillow_image.size[0] / pillow_image.size[1]
container_ratio = 1.0
if container_ratio > image_ratio:
height = 100
width = int(height * image_ratio)
else:
width = 100
height = int(width / image_ratio)
main_image = customtkinter.CTkImage(light_image=pillow_image, size=(width,height))
label = customtkinter.CTkLabel(self.containerFrame, image=main_image, text='')
label.pack(side= customtkinter.RIGHT, padx=5, pady=5)
# source: https://stackoverflow.com/questions/3296893/how-to-pass-an-argument-to-event-handler-in-tkinter
# https://www.youtube.com/watch?v=VnwDPa9biwc
data = {"guid": guid}
label.bind("<Button-1>", lambda event, arg=data: self.clicked_image(event, arg))
label.bind("<Button-3>", lambda event, arg=data: self.show_context_menu(event, arg))
self.contained_labels.append(label)
def append_game_to_tier(self, frame, guid):
if guid in frame.guids:
print('Game already in container')
return
frame.guids.append(guid)
game_data, image_path = game_parser.search_cache_data(guid)
# Load the image
pillow_image = Image.open(image_path)
image_ratio = pillow_image.size[0] / pillow_image.size[1]
container_ratio = 1.0
if container_ratio > image_ratio:
height = 150
width = int(height * image_ratio)
else:
width = 150
height = int(width / image_ratio)
main_image = customtkinter.CTkImage(light_image=pillow_image, size=(width,height))
label = customtkinter.CTkLabel(frame, image=main_image, text='')
#label.pack(side= customtkinter.LEFT, padx=5, pady=5)
frame.labels.append(label)
label.grid(row=0, column=len(frame.labels)-1, padx=5, pady=5)
data = {"guid": guid}
label.bind("<Button-3>", lambda event, arg=data: self.show_tier_context_menu(event, arg))
def show_context_menu(self, event, arg):
self.choosen_guid = arg["guid"]
self.context_menu.post(event.x_root, event.y_root)
def show_tier_context_menu(self, event, arg):
self.choosen_guid = arg["guid"]
self.tier_context_menu.post(event.x_root, event.y_root)
def context_tier_remove_game(self):
current_guid = self.choosen_guid
for tierlist in self.container_tier:
if current_guid in tierlist.guids:
index = tierlist.guids.index(current_guid)
tierlist.guids.pop(index)
tierlist.labels[index].destroy()
tierlist.labels.pop(index)
# rearange grid
for index,label in enumerate(tierlist.labels):
label.grid(row=0, column=index, padx=5, pady=5)
self.append_game_to_container(current_guid)
return
print('guid not found in tierlists!')
def context_tier_move_right(self):
current_guid = self.choosen_guid
for tierlist in self.container_tier:
if current_guid in tierlist.guids:
index = tierlist.guids.index(current_guid)
if index == 0: return
tierlist.guids[index-1], tierlist.guids[index] = tierlist.guids[index], tierlist.guids[index-1]
tierlist.labels[index-1], tierlist.labels[index] = tierlist.labels[index], tierlist.labels[index-1]
tierlist.labels[index].grid(row=0, column=index, padx=5, pady=5)
tierlist.labels[index-1].grid(row=0, column=index-1, padx=5, pady=5)
return
print('guid not found in tierlists!')
def context_tier_move_left(self):
current_guid = self.choosen_guid
for tierlist in self.container_tier:
if current_guid in tierlist.guids:
index = tierlist.guids.index(current_guid)
if len(tierlist.guids) == 1: return
if index == len(tierlist.guids)-1: return
tierlist.guids[index+1], tierlist.guids[index] = tierlist.guids[index], tierlist.guids[index+1]
tierlist.labels[index+1], tierlist.labels[index] = tierlist.labels[index], tierlist.labels[index+1]
tierlist.labels[index].grid(row=0, column=index, padx=5, pady=5)
tierlist.labels[index+1].grid(row=0, column=index+1, padx=5, pady=5)
return
print('guid not found in tierlists!')
def context_menu_remove(self):
self.remove_game_from_container(self.choosen_guid)
def context_move_to_tier(self, arg):
tier = arg
index = config['tiers'].index(tier)
self.append_game_to_tier(self.container_tier[index],self.choosen_guid)
self.remove_game_from_container(self.choosen_guid)
def clicked_image(self, event, arg):
print(f'image was clicked with guid: {arg["guid"]}')
#self.remove_game_from_container(arg["guid"])
#label = self.get_label(arg["guid"])
#self.append_game_to_tier(self.container_tier[0],arg["guid"])
self.gamebrowserframe.load_game_data(arg["guid"])
def get_label(self, guid):
index = self.contained_guids.index(guid)
return self.contained_labels[index]
def remove_game_from_container(self, guid):
if guid not in self.contained_guids:
print('Game is not in container')
return
index = self.contained_guids.index(guid)
self.contained_guids.pop(index)
self.contained_labels[index].destroy()
self.contained_labels.pop(index)
def append_all_images_button(self):
guids = os.listdir(config['CACHE_DIR'])
for g in guids:
self.append_game_to_container(g)
print(self.contained_guids)
def append_selected_image_button(self):
if self.gamebrowserframe.current_guid == '':
return
self.append_game_to_container(self.gamebrowserframe.current_guid)
app = App()
app.mainloop()