Repository navigation
Expand file tree
/
Copy pathModPy.py
More file actions
346 lines (263 loc) · 11.7 KB
/
Copy pathModPy.py
File metadata and controls
346 lines (263 loc) · 11.7 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
from functools import partial
from tkinter import DISABLED, Tk, Button, Label, Frame
from tkinter.messagebox import askokcancel, WARNING
from PIL import ImageTk, Image
import os
import shutil
import time
from threading import Thread
from dark_title_bar import *
import settings as s
import progress_window as progressw
import modpack_actions as modpack_act
root = Tk()
root.title("ModPy Beta 1.1")
root.geometry("350x520") #Resolution of the window
root.resizable(False, False) #Make it so you can't resize it
s.get_user_settings()
mods_folder = s.settings["mods_folder"]
theme = s.settings["theme"]
if theme == "light":
print("Theme set to \"light\"")
bg_color = "#f0f0f0"
button_bg_color = "#c3c3c3"
fg_color = "#000000"
button_active_bg_color = button_bg_color
button_active_foreground_color = fg_color
label_bg_color = "#e4e4e4"
frame_bg_color = bg_color
highlightbackground_color = "#afafaf"
settings_cog = ImageTk.PhotoImage(Image.open("images/settings_cog_light.png"))
#virtualPixel is used to make the buttons completely resizable
virtualPixel = ImageTk.PhotoImage(Image.open("images/virtualPixel_light.png"))
if theme == "discord dark":
print("Theme set to \"discord dark\"")
try:
dark_title_bar(root)
except: print("Dark title bar is not supported by this computer")
bg_color = "#36393f"
button_bg_color = bg_color
fg_color = "#d2d3d5"
button_active_bg_color = button_bg_color
button_active_foreground_color = fg_color
label_bg_color = "#40444b"
frame_bg_color = "#2f3136"
highlightbackground_color = "#8a8a8a"
settings_cog = ImageTk.PhotoImage(Image.open("images/settings_cog_discord_dark.png"))
#virtualPixel is used to make the buttons completely resizable
virtualPixel = ImageTk.PhotoImage(Image.open("images/virtualPixel_discord_dark.png"))
root.configure(bg = bg_color)
#Frame offset variables are used to separate the frames from the edges of the window
widget_offset_x = 6
widget_offset_y = widget_offset_x
def enable_closing():
root.protocol("WM_DELETE_WINDOW", root.destroy)
def disable_closing():
root.protocol("WM_DELETE_WINDOW", lambda: print("Error: Not allowed to close the window"))
def create_button_panel_widgets():
button_panel_frame = Frame(root, width = 350 - widget_offset_x * 2, height = 60, bg = frame_bg_color, highlightbackground = "#afafaf", highlightthickness=1)
button_panel_frame.place(in_ = root, x = widget_offset_x, y = widget_offset_y)
import_modpack_button = Button(button_panel_frame,
text = "Import",
font = ("Arial", 14),
bg = button_bg_color,
fg = fg_color,
activebackground = button_active_bg_color,
activeforeground = button_active_foreground_color,
image = virtualPixel, compound = "c",
width = 103 - widget_offset_x * 8 + 2,
height = 60 - widget_offset_x * 5 + 2,
command = import_modpack)
import_modpack_button.place(in_ = button_panel_frame, x = widget_offset_x, y = widget_offset_y)
create_modpy_button = Button(
text = "Create modpy file",
font = ("Arial", 14),
bg = button_bg_color,
fg = fg_color,
activebackground = button_active_bg_color,
activeforeground = button_active_foreground_color,
image = virtualPixel, compound = "c",
width = 193 - widget_offset_x * 8 + 2,
height = 60 - widget_offset_x * 5 + 2,
state = DISABLED,
command = print)
create_modpy_button.place(in_ = button_panel_frame, x = 101, y = widget_offset_y)
settings_button = Button(
relief = "flat",
bg = frame_bg_color,
activebackground = button_active_bg_color,
image = settings_cog,
command = open_settings)
settings_button.place(in_ = button_panel_frame, x = 283, y = widget_offset_y - 2)
def create_modpack_list_structure():
global modpack_list_frame
modpack_list_frame = Frame(root, width = 350 - widget_offset_x * 2, height = 442, bg = frame_bg_color, highlightbackground = "#afafaf", highlightthickness=1)
modpack_list_frame.place(in_ = root, x = 6, y = 72)
root.bind("<MouseWheel>", modpack_scroll) #NOTE: In Linux, <MouseWheel> is <Button-4> and <Button-5>
def open_settings():
global settings
destroy_modpack_buttons()
s.set_up_window(theme)
settings = s.get_user_settings
#This list holds the name of every modpack that has been saved by the user
modpacks_saved = []
#This list holds every label objects that are used to show the modpacks
modpack_labels = []
#This list holds every button created when clicking a modpack. It is used to delete every created button
#when the user clicks a modpack, so there are not multiple sets of buttons at the same time in different modpacks.
created_modpack_buttons = []
def load_modpacks():
#This function will save every modpack that the user has saved in the modpacks_saved list.
#At the moment, this is just a placeholder so I can continue working on the program.
#This functionality will be implemented in the future.
for directory in os.listdir("modpacks"):
modpacks_saved.append(directory)
def show_modpacks():
#TODO
#Shows every modpack saved by the user in modpack_list_frame, creating buttons to install, see options and delete them.
global modpack_list_frame
for modpack in modpacks_saved:
current_position = modpacks_saved.index(modpack) #Current modpack position in the list (index)
#Add a label to the modpack_labels list
modpack_labels.append(
Label(modpack_list_frame,
text = modpack,
font = ("Arial", 12, "bold"),
anchor = "w",
bg = label_bg_color,
fg = fg_color,
image = virtualPixel, compound = "c",
width = 318,
height = 22))
current_label = modpack_labels[current_position] #Current label, the one we have to pack and bind a key to
current_label.pack(padx = widget_offset_x, pady = widget_offset_y / 2)
#We have to use partial() to call the function with arguments and not lambda because with lambda it won't work well,
#it will use the last value of the arguments and not the one in the loop where the label is created
current_label.bind("<Button-1>", partial(create_modpack_buttons, current_label, current_position))
def refresh_modpack_list():
modpacks_saved.clear()
for i in modpack_labels:
i.destroy()
modpack_labels.clear()
created_modpack_buttons.clear()
load_modpacks()
show_modpacks()
def create_modpack_buttons(label, modpack_index, x): #We use x as an argument because partial() needs an extra argument
print(f"Modpack {modpack_index}") #DEBUG LINE
#Delete every other button created next to a modpack name
destroy_modpack_buttons()
install_button = Button(label,
text = "Install",
font = ("Arial", 10, "bold"),
bg = button_bg_color,
fg = fg_color,
activebackground = button_active_bg_color,
activeforeground = button_active_foreground_color,
image = virtualPixel, compound = "c",
width = 20,
height = 6,
command = lambda: modpack_act.install_modpack(modpack_index, theme, root, modpacks_saved))
install_button.place(in_ = label, x = 162, y = 1)
options_button = Button(label,
text = "Options",
font = ("Arial", 10, "bold"),
bg = button_bg_color,
fg = fg_color,
activebackground = button_active_bg_color,
activeforeground = button_active_foreground_color,
image = virtualPixel, compound = "c",
width = 28,
height = 6,
state = DISABLED,
command = lambda: print(f"{modpack_index} - options"))
options_button.place(in_ = label, x = 218, y = 1)
delete_button = Button(label,
text = "✖",
font = ("Arial", 11),
bg = button_bg_color,
fg = "#ff0000",
activebackground = button_active_bg_color,
activeforeground = "#ff0000",
image = virtualPixel, compound = "c",
width = 6,
height = 6,
command = lambda: delete_modpack(modpack_index))
delete_button.place(in_ = label, x = 282, y = 1)
created_modpack_buttons.append(install_button)
created_modpack_buttons.append(options_button)
created_modpack_buttons.append(delete_button)
def destroy_modpack_buttons(x = 0):
for button in created_modpack_buttons:
button.destroy()
def modpack_scroll(event):
#If there are less than 14 modpacks, do not scroll to avoid bugs
if len(modpacks_saved) < 14:
return
scroll_sensibility = 1
for i in modpack_labels:
current_x = i.winfo_x() - 1 #For some reason, if we don't add -1, the labels will go right
#I think it is because the method returns an extra pixel or
#the place method doesn't count the border or smth
current_y = i.winfo_y()
first_label = modpack_labels[0]
last_label = modpack_labels[-1]
if event.delta > 0:
y_increment = 25 * scroll_sensibility
#Top limit for the labels:
#If the first label will go over the top when we add the scroll value, move it to the top and not more
if first_label.winfo_y() + y_increment > 0:
y_increment = -first_label.winfo_y() + 3 #The 3 (removed) is for the spacing between the frame and the label
elif event.delta < 0:
y_increment = -25 * scroll_sensibility
#Bottom limit for the labels:
#If the last label will go down the bottom when we add the scroll value, move it to the bottom and not more
if last_label.winfo_y() + y_increment < 412:
y_increment = 417 - last_label.winfo_y()
i.place_forget()
i.place(x = current_x, y = current_y + y_increment)
def import_modpack():
#Create another file that will create a window to import from a folder, .zip or .modpy
#The latter one is a file that contains the links where the mods need to be downloaded from
destroy_modpack_buttons()
modpack_act.open_import_window(theme, root)
thread = Thread(target = import_modpack_check_for_refresh)
thread.start()
def import_modpack_check_for_refresh():
while True:
if modpack_act.finished_importing == True:
refresh_modpack_list()
break
time.sleep(0.1)
def delete_modpack(modpack_index):
if int(s.settings["delete_confirmation"]) == 1:
user_has_confirmed = askokcancel("Modpack deletion",
"You are about to delete a modpack.\nDo you want to continue?",
icon = WARNING)
if not user_has_confirmed:
return
#Start progress window
progressw.open_window("Deleting...", theme, root, modpacks_saved[modpack_index])
#Create a thread to execute the actions over the progress window
#If we don't use a thread, the window will not open until all the actions are finished
thread = Thread(target = _delete_actions, args = (modpack_index,))
thread.start()
def _delete_actions(modpack_index):
modpack_route = f"modpacks/{modpacks_saved[modpack_index]}/"
#Disable closing
disable_closing()
#Delete the modpack folder
progressw.change_info_text("Removing modpack...")
shutil.rmtree(modpack_route)
progressw.change_progress(100)
#End progress window
progressw.change_info_text("Finished deleting modpack")
progressw.end()
#Enable closing
enable_closing()
#Refresh the modpack list (TODO)
refresh_modpack_list()
create_button_panel_widgets()
create_modpack_list_structure()
load_modpacks()
show_modpacks()
root.mainloop()