Skip to content

Commit

Permalink
[gui] track settings selected by user
Browse files Browse the repository at this point in the history
  • Loading branch information
mike8699 committed Jan 21, 2024
1 parent 7d0ef93 commit 44704dc
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion ph_rando/ui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
class RandomizerUi(QWidget):
rom_path: Path | None
seed: str | None
settings: dict[str, bool | str | list[str]]

def __init__(self) -> None:
super().__init__()

self.rom_path = None
self.seed = None
self.settings = {}

self.setWindowTitle('Phantom Hourglass Randomizer')
layout = QFormLayout()
Expand Down Expand Up @@ -103,13 +105,26 @@ def render_file_open_ui(self) -> None:
vbox.addWidget(self._get_seed_widget())

def render_settings(self) -> None:
SETTINGS_INTERNAL_TO_HUMAN_READABLE = {
setting_name: inflection.titleize(setting_name) for setting_name in RANDOMIZER_SETTINGS
} | {
choice: inflection.titleize(choice)
for setting in filter(lambda s: hasattr(s, 'choices'), RANDOMIZER_SETTINGS.values())
for choice in setting.choices
}
SETTINGS_HUMAN_READABLE_TO_INTERNAL = {
v: k for k, v in SETTINGS_INTERNAL_TO_HUMAN_READABLE.items()
}

groupbox = QGroupBox('Randomizer Settings')
self.layout().addWidget(groupbox)

hbox = QHBoxLayout()
groupbox.setLayout(hbox)

for i, setting in enumerate(RANDOMIZER_SETTINGS.values()):
self.settings[setting.name] = setting.default

if i % 6 == 0:
current_widget = QWidget()
vbox = QVBoxLayout()
Expand All @@ -120,9 +135,18 @@ def render_settings(self) -> None:
internal_hbox = QHBoxLayout()
internal_widget.setLayout(internal_hbox)
if setting.type == 'flag':
chbox = QCheckBox(inflection.titleize(setting.name))
chbox = QCheckBox(SETTINGS_INTERNAL_TO_HUMAN_READABLE[setting.name])
chbox.setEnabled(setting.supported)
chbox.setChecked(setting.default)
internal_hbox.addWidget(chbox)

def _on_change(checkbox: QCheckBox, setting: str) -> None:
self.settings[setting] = not self.settings[setting]
checkbox.setChecked(self.settings[setting])

chbox.clicked.connect(
lambda chbox=chbox, setting=setting.name: _on_change(chbox, setting)
)
else:
assert (
setting.choices is not None
Expand All @@ -134,6 +158,16 @@ def render_settings(self) -> None:
comboxbox.setEnabled(setting.supported)
internal_hbox.addWidget(comboxbox)
internal_hbox.addWidget(comboxbox_label)

def _on_change(cbox: QComboBox, setting: str) -> None:
self.settings[setting] = SETTINGS_HUMAN_READABLE_TO_INTERNAL[cbox.currentText()]

comboxbox.currentTextChanged.connect(
lambda _, comboxbox=comboxbox, setting=setting.name: _on_change(
comboxbox, setting
)
)

vbox.addWidget(internal_widget)
if setting.description:
internal_widget.setToolTip(setting.description)
Expand Down

0 comments on commit 44704dc

Please sign in to comment.