all repos — m12-patcher @ b84de75f35171c4c5072063225f2e8270f9dfa49

Gui.py (view raw)

 1#!/usr/bin/python3
 2import tkinter as tk
 3from tkinter.filedialog import askopenfilename
 4import pathlib
 5import pygubu
 6import Constants
 7from Functions import check_rom, show_warning, start_patching
 8
 9PROJECT_PATH = pathlib.Path(__file__).parent
10PROJECT_UI = PROJECT_PATH / "patcher.ui"
11
12class PatcherApp:
13    def __init__(self, master=None, baserom=None):
14        self.builder = builder = pygubu.Builder()
15        builder.add_resource_path(PROJECT_PATH)
16        builder.add_from_file(PROJECT_UI)
17        # Main widget
18        self.mainwindow = builder.get_object("main_frame", master)
19
20        self.browse_path = None
21        self.preset = None
22        self.font = None
23        self.places = None
24        self.sprites = None
25        self.palette = None
26        self.skip_m1 = None
27        self.progress = None
28        self.progress_text = None
29        builder.import_variables(
30            self,
31            [
32                "browse_path",
33                "preset",
34                "font",
35                "places",
36                "sprites",
37                "palette",
38                "skip_m1",
39                "progress",
40                "progress_text",
41            ],
42        )
43        self.font.set(1)
44        self.sprites.set(3)
45        self.places.set(6)
46        self.palette.set(8)
47        self.skip_m1.set(12)
48        self.progress_text.set(Constants.STATUS_START)
49        
50        self.apply_button = builder.get_object("apply_button")
51        
52        if baserom is not None:
53            self.browse_path.set(baserom)
54        
55        builder.connect_callbacks(self)
56    
57    def run(self):
58        self.mainwindow.mainloop()
59
60    def on_browse_button(self):
61        fn = askopenfilename(filetypes=[(Constants.VAR_FILEPICKER, '*.gba')])
62        self.browse_path.set(fn)
63
64    def on_change_preset(self, option):
65        new_vars = Constants.PRESETS[option]
66        self.set_progress(0, Constants.STATUS_PRESET)
67        
68        for key in new_vars.keys():
69            getattr(self, key).set(new_vars[key])
70
71    def on_apply_button(self):
72        self.apply_button['state'] = 'disabled'
73        
74        baserom_temp = self.browse_path.get()
75        
76        if not check_rom(baserom_temp):
77            show_warning(Constants.WARNING_MD5_MISMATCH)
78            self.apply_button['state'] = 'normal'
79            return
80        self.baserom = baserom_temp
81        start_patching(self)