diff --git a/source/Common.py b/source/Common.py index d86abb3..4c4f5b8 100644 --- a/source/Common.py +++ b/source/Common.py @@ -3,6 +3,7 @@ from source.Option import Option from source.Game import Game from source.Gui.Main import Main from source.Gui.TrackConfiguration import TrackConfiguration +from source.Gui.SelectPack import SelectPack from source.Translation import Translator from source.Error import ErrorLogger @@ -27,4 +28,5 @@ class Common: self.gui_main = Main(common=self) def show_gui_track_configuration(self): TrackConfiguration(common=self) + def show_gui_add_pack(self): SelectPack(common=self) def mainloop(self): self.gui_main.mainloop() diff --git a/source/Gui/Main.py b/source/Gui/Main.py index 66775d1..eb05526 100644 --- a/source/Gui/Main.py +++ b/source/Gui/Main.py @@ -29,11 +29,11 @@ class Main: self.common.translate("Error"), self.common.translate("There is no pack in the ./Pack/ directory.") ) - self.quit() + #self.quit() self.is_dev_version = False # Is this installer version a dev ? self.is_track_configuration_edited = False - self.stringvar_ctconfig = StringVar(value=self.available_packs[0]) + self.stringvar_ctconfig = StringVar(value=self.available_packs[0] if self.available_packs else None) self.stringvar_language = StringVar(value=self.common.option.language) self.stringvar_game_format = StringVar(value=self.common.option.format) self.boolvar_dont_check_for_update = BooleanVar(value=self.common.option.dont_check_for_update) @@ -60,9 +60,17 @@ class Main: textvariable=self.stringvar_ctconfig, width=30 ) - self.combobox_ctconfig_path.grid(row=1, column=1, sticky="NEWS", columnspan=2) + self.combobox_ctconfig_path.grid(row=1, column=1, sticky="NEWS") self.combobox_ctconfig_path.bind("<>", lambda x=None: self.reload_ctconfig()) - self.reload_ctconfig() + + self.button_add_mod = Button( + self.frame_ctconfig, + text="+", + relief=RIDGE, + command=self.common.show_gui_add_pack, + width=2 + ) + self.button_add_mod.grid(row=1, column=2) # Jeu self.frame_game_path = LabelFrame(self.root, text=self.common.translate("Original game")) @@ -249,10 +257,19 @@ class Main: self.menu_help.add_command(label="Github Wiki", command=lambda: webbrowser.open(GITHUB_HELP_PAGE_URL)) self.menu_help.add_command(label="Discord", command=lambda: webbrowser.open(DISCORD_URL)) + self.reload_ctconfig() + def reload_ctconfig(self) -> None: - self.common.ct_config.load_ctconfig_file( - ctconfig_file=self.get_ctconfig_path_pack(self.stringvar_ctconfig.get()) - ) + self.available_packs = self.get_available_packs() + if self.available_packs: self.state_button(enable=True) + else: self.state_button(enable=False) + + self.combobox_ctconfig_path.configure(values=self.available_packs) + + if self.stringvar_ctconfig.get(): + self.common.ct_config.load_ctconfig_file( + ctconfig_file=self.get_ctconfig_path_pack(self.stringvar_ctconfig.get()) + ) @staticmethod def get_available_packs() -> list: @@ -352,9 +369,9 @@ class Main: :param enable: are the button enabled ? """ button = [ - self.button_do_everything, - self.button_select_path, self.combobox_ctconfig_path, + self.button_select_path, + self.button_do_everything ] for widget in button: if enable: widget.config(state=NORMAL) diff --git a/source/Gui/SelectPack.py b/source/Gui/SelectPack.py index 0bcbf24..3b6a7c1 100644 --- a/source/Gui/SelectPack.py +++ b/source/Gui/SelectPack.py @@ -1,3 +1,77 @@ +from tkinter import * +from tkinter import ttk +from tkinter import filedialog +from tkinter import messagebox +import zipfile +import os + + class SelectPack: - def __init__(self): - pass \ No newline at end of file + def __init__(self, common): + self.common = common + + self.root = Toplevel(self.common.gui_main.root) + self.root.title(self.common.translate("Add a pack")) + self.root.iconbitmap("./icon.ico") + self.root.resizable(False, False) + self.root.grab_set() + + self.entry_modpack_path = Entry(self.root, width=50) + self.entry_modpack_path.grid(row=1, column=1, sticky="NEWS") + + def select_path(): + path = filedialog.askopenfilename( + filetypes=((self.common.translate("MKW Pack"), r"*.mkwf.pack"),) + ) + if os.path.exists(path): + self.entry_modpack_path.delete(0, END) + self.entry_modpack_path.insert(0, path) + + self.button_select_path = Button( + self.root, + text="...", + relief=RIDGE, + command=lambda: self.root.after(0, select_path) + ) + self.button_select_path.grid(row=1, column=2) + + def extract_pack(): + self.progressbar_extract.grid(row=3, column=1, columnspan=2, sticky="NEWS") + try: + path = self.entry_modpack_path.get() + *packname, _, _ = os.path.basename(path).split(".") + packname = ".".join(packname) + + with zipfile.ZipFile(path) as zip_pack: + zip_pack.extractall(f"./Pack/{packname}/") + + self.common.gui_main.reload_ctconfig() + + messagebox.showinfo( + self.common.translate("Extraction"), + self.common.translate("The mod have been extracted !") + ) + self.root.destroy() + + except Exception as e: + self.progressbar_extract.grid_forget() + raise e + + self.button_extract_modpack = Button( + self.root, + text=self.common.translate("Extract the modpack"), + relief=RIDGE, + command=extract_pack + ) + self.button_extract_modpack.grid(row=2, column=1, columnspan=2, sticky="NEWS") + + self.progressbar_extract = ttk.Progressbar(self.root) + self.progressbar_extract.configure(mode="indeterminate") + self.progressbar_extract.start(50) + + def state_button(self, enable=True): + for button in [ + self.button_extract_modpack + ]: + if enable: button.config(state=NORMAL) + else: button.config(state=DISABLED) diff --git a/translation.json b/translation.json index 2eea30a..0c4bc6a 100644 --- a/translation.json +++ b/translation.json @@ -110,6 +110,11 @@ "value2": "valeur2", "Save track configuration": "Sauvegarder la configuration de course", "track configuration": "configuration de course", - "Load track configuration": "Charger la configuration de course" + "Load track configuration": "Charger la configuration de course", + "Add a pack": "Ajouter un pack", + "Extract the modpack": "Extraire le modpack", + "MKW Pack": "Pack MKW", + "Extraction": "Extraction", + "The mod have been extracted !": "Le mod à bien été extrait !" } } \ No newline at end of file