Skip to content

Commit abe3d3f

Browse files
committed
Continue working on the checkmark menu
1 parent dab9d0a commit abe3d3f

File tree

5 files changed

+155
-14
lines changed

5 files changed

+155
-14
lines changed

Python/gui/checkbox_button_menu.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import PySimpleGUI as sg
2+
3+
4+
from Python import shared_globals as cfg
5+
6+
7+
def handle_button_press(window, buttonmenu_name, selected_button_name, user_settings_button_mappings):
8+
buttonmenu = get_button_menu(window, buttonmenu_name)
9+
menu_definition = get_menu_definition(buttonmenu)
10+
11+
selected_button_index = get_user_settings_button_index(user_settings_button_mappings, selected_button_name)
12+
13+
buttons = get_button_menu_buttons(menu_definition)
14+
toggle_button(user_settings_button_mappings, selected_button_name, buttons, selected_button_index)
15+
16+
buttonmenu.update(menu_definition)
17+
18+
19+
def get_button_menu(window, buttonmenu_name):
20+
return window[buttonmenu_name]
21+
22+
23+
def get_menu_definition(buttonmenu):
24+
return buttonmenu.MenuDefinition
25+
26+
27+
def get_button_menu_buttons(menu_definition):
28+
return menu_definition[1]
29+
30+
31+
def toggle_button(user_settings_button_mappings, selected_button_name, buttons, selected_button_index):
32+
toggle_button_user_entry_setting(user_settings_button_mappings, selected_button_name)
33+
toggle_button_visually(buttons, selected_button_index)
34+
35+
36+
def toggle_button_visually(buttons, selected_button_index):
37+
current_button_string = buttons[selected_button_index]
38+
39+
if current_button_string[0] == cfg.CHECKMARK:
40+
buttons[selected_button_index] = cfg.NO_CHECKMARK + current_button_string[1:]
41+
else:
42+
buttons[selected_button_index] = cfg.CHECKMARK + current_button_string[1:]
43+
44+
45+
def toggle_button_user_entry_setting(user_settings_button_mappings, selected_button_name):
46+
user_settings_button_key = get_user_settings_button_value(user_settings_button_mappings, selected_button_name)
47+
48+
current_user_settings_button_state = sg.user_settings_get_entry(user_settings_button_key)
49+
sg.user_settings_set_entry(user_settings_button_key, not current_user_settings_button_state)
50+
51+
52+
def get_user_settings_button_index(user_settings_button_mappings, selected_button_name):
53+
""" We can't do a simple lookup due to the checkmark that may be in selected_button_name, so we need to use the in operator """
54+
for i, button_name in enumerate(user_settings_button_mappings.keys()):
55+
if button_name in selected_button_name:
56+
return i
57+
58+
59+
def get_user_settings_button_value(user_settings_button_mappings, selected_button_name):
60+
""" We can't do a simple lookup due to the checkmark that may be in selected_button_name, so we need to use the in operator """
61+
for k, v in user_settings_button_mappings.items():
62+
if k in selected_button_name:
63+
return v
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import PySimpleGUI as sg
2+
3+
def find_sub_menu(widget, position):
4+
length = widget.index("end")+1
5+
for i in range(length):
6+
label = widget.entrycget(i, 'label')
7+
sub_menu = widget.nametowidget(widget.entrycget(i, 'menu'))
8+
if label == position[0]:
9+
if len(position) == 1:
10+
return sub_menu
11+
else:
12+
return find_sub_menu(sub_menu, position[1:])
13+
14+
def insert_menu_checkbutton(menu, position, indexes, labels):
15+
widget = menu.Widget
16+
sub_menu = find_sub_menu(widget, position)
17+
if sub_menu:
18+
for index, label in zip(indexes, labels):
19+
var = sg.tk.BooleanVar()
20+
sub_menu.insert_checkbutton(index, label=label, variable=var,
21+
command=lambda label=label, var=var: menu._MenuItemChosenCallback(f'{label} {var.get()}'))
22+
else:
23+
print("Postion not found !")
24+
25+
sg.theme("DarkBlue3")
26+
sg.set_options(font=("Courier New", 20))
27+
28+
menu_def = [['&File', ['&Open', '&Save', '&Properties', 'E&xit']]]
29+
30+
layout = [
31+
[sg.Menu(menu_def, key='-MENU-')],
32+
[sg.Button("Add Checkbox in Menu")],
33+
]
34+
window = sg.Window('Title', layout, finalize=True)
35+
variables = []
36+
while True:
37+
38+
event, values = window.read()
39+
if event == sg.WINDOW_CLOSED:
40+
break
41+
elif event == "Add Checkbox in Menu":
42+
43+
menu = window['-MENU-']
44+
45+
position = ("File",)
46+
indexes = (0, 2, 4)
47+
labels = ("Confirmation", "Alarm", "Status")
48+
insert_menu_checkbutton(menu, position, indexes, labels)
49+
50+
print(event, values)
51+
52+
window.close()

Python/gui/gui.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from Python import shared_globals as cfg
77
from Python import convert
88
from Python import warnings
9+
910
from Python.gui import gui_layout
11+
from Python.gui import checkbox_button_menu
1012

1113

1214
def init_window_theme():
@@ -25,27 +27,34 @@ def init_window():
2527
if not os.path.isfile(sg.user_settings_filename()):
2628
sg.Popup("This is a tool that allows you to convert legacy (old) mods to the latest version of CCCP. You can get more information from the GitHub repo or the Discord server by clicking the corresponding icons.", title="Welcome screen", custom_text=" OK ")
2729

28-
# if not sg.user_settings_get_entry("cccp_folder"):
29-
# sg.user_settings_set_entry("cccp_folder", "Input")
30-
31-
play_finish_sound_setting = sg.user_settings_get_entry("play_finish_sound")
32-
sg.user_settings_set_entry("play_finish_sound", True if play_finish_sound_setting == None else play_finish_sound_setting)
30+
# set_default_settings(window)
3331

3432
cfg.sg = sg
33+
3534
warnings.load_conversion_and_warning_rules() # TODO: Why is this called in this GUI function?
3635

3736
window = sg.Window(
3837
f"Legacy Mod Converter {cfg.CONVERTER_VERSION} for CCCP {cfg.GAME_VERSION}",
3938
gui_layout.get_layout(),
4039
icon=utils.path("Media/legacy-mod-converter.ico"),
41-
font=("Helvetica", 16)
40+
font=("Helvetica", 16),
41+
finalize=True
4242
)
43+
44+
set_default_settings(window)
45+
4346
cfg.progress_bar = window["PROGRESS_BAR"]
44-
window.finalize()
47+
48+
# window.finalize()
4549

4650
return window
4751

4852

53+
def set_default_settings(window):
54+
if sg.user_settings_get_entry("play_finish_sound") == None:
55+
checkbox_button_menu.handle_button_press(window, "SETTINGS", "Play finish sound", cfg.USER_SETTINGS_BUTTON_MAPPINGS)
56+
57+
4958
def run_window(window):
5059
valid_cccp_path = True if sg.user_settings_get_entry("cccp_folder") else False
5160

@@ -72,12 +81,9 @@ def run_window(window):
7281
valid_cccp_path = False
7382
window[event](background_color = cfg.NO_PATH_SET_COLOR)
7483

75-
elif event == "OUTPUT_ZIPS":
76-
sg.user_settings_set_entry("output_zips", values[event])
77-
elif event == "PLAY_FINISH_SOUND":
78-
sg.user_settings_set_entry("play_finish_sound", values[event])
79-
elif event == "SKIP_CONVERSION":
80-
sg.user_settings_set_entry("skip_conversion", values[event])
84+
elif event == "SETTINGS":
85+
selected_button_name = values[event]
86+
checkbox_button_menu.handle_button_press(window, event, selected_button_name, cfg.USER_SETTINGS_BUTTON_MAPPINGS)
8187

8288
elif event == "CONVERT":
8389
if valid_cccp_path:

Python/gui/gui_layout.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,18 @@ def get_layout():
3030
)
3131
],
3232
[
33-
sg.ButtonMenu('Options', ['_', ['Edit Me', 'Exit']]),
33+
sg.ButtonMenu(
34+
"Settings", [
35+
"",
36+
[
37+
f"{cfg.NO_CHECKMARK}Skip conversion",
38+
f"{cfg.NO_CHECKMARK}Output zips",
39+
f"{cfg.NO_CHECKMARK}Play finish sound",
40+
]
41+
],
42+
key="SETTINGS",
43+
background_color="#2a3948"
44+
),
3445
sg.Button(
3546
"Convert",
3647
key="CONVERT",

Python/shared_globals.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
CONVERTER_FOLDER_NAME = "LegacyModConverter" + VERSION_STRING
77
WARNINGS_MOD_NAME_SEPARATOR = "-" * 50
88

9+
NO_CHECKMARK = " "
10+
CHECKMARK = "✓"
11+
12+
USER_SETTINGS_BUTTON_MAPPINGS = {
13+
"Skip conversion": "skip_conversion",
14+
"Output zips": "output_zips",
15+
"Play finish sound": "play_finish_sound"
16+
}
17+
918
NOT_RELEASE = True # This enables tests when set to True, as the release version shouldn't run tests.
1019

1120
sg = None

0 commit comments

Comments
 (0)