Last round of cleanup for settings for now.

This commit is contained in:
2026-07-07 14:56:08 -05:00
parent 38c5080f9f
commit 860c797c9c
7 changed files with 59 additions and 85 deletions
+15 -31
View File
@@ -20,20 +20,10 @@
#include "locale/localemanager.h"
#include "asset/loader/locale/assetlocaleloader.h"
#define UI_SETTINGS_DISCARD_TEXT_MAX 128
uisettings_t UI_SETTINGS;
typedef struct {
const char_t *localeId;
errorret_t (*init)(void);
uimenu_t *menu;
void (*load)(void);
void (*apply)(void);
bool_t (*hasChanges)(void);
bool_t pendingDiscardConfirm;
} uisettingstabdef_t;
static uisettingstabdef_t UI_SETTINGS_TAB_DEFS[UI_SETTINGS_TAB_COUNT] = {
[UI_SETTINGS_TAB_GAMEPLAY] = {
uisettingstabdef_t UI_SETTINGS_TAB_DEFS[UI_SETTINGS_TAB_COUNT] = {
{
.localeId = "ui.settings.tabs.gameplay",
.init = uiSettingsGameplayInit,
.menu = &UI_SETTINGS_GAMEPLAY.menu,
@@ -42,7 +32,7 @@ static uisettingstabdef_t UI_SETTINGS_TAB_DEFS[UI_SETTINGS_TAB_COUNT] = {
.hasChanges = uiSettingsGameplayHasChanges
},
[UI_SETTINGS_TAB_INPUT] = {
{
.localeId = "ui.settings.tabs.input",
.init = uiSettingsInputInit,
.menu = &UI_SETTINGS_INPUT.menu,
@@ -51,7 +41,7 @@ static uisettingstabdef_t UI_SETTINGS_TAB_DEFS[UI_SETTINGS_TAB_COUNT] = {
.hasChanges = uiSettingsInputHasChanges
},
[UI_SETTINGS_TAB_DISPLAY] = {
{
.localeId = "ui.settings.tabs.display",
.init = uiSettingsDisplayInit,
.menu = &UI_SETTINGS_DISPLAY.menu,
@@ -60,7 +50,7 @@ static uisettingstabdef_t UI_SETTINGS_TAB_DEFS[UI_SETTINGS_TAB_COUNT] = {
.hasChanges = uiSettingsDisplayHasChanges
},
[UI_SETTINGS_TAB_AUDIO] = {
{
.localeId = "ui.settings.tabs.audio",
.init = uiSettingsAudioInit,
.menu = &UI_SETTINGS_AUDIO.menu,
@@ -70,16 +60,13 @@ static uisettingstabdef_t UI_SETTINGS_TAB_DEFS[UI_SETTINGS_TAB_COUNT] = {
}
};
uisettings_t UI_SETTINGS;
static uisettingstabdef_t UI_SETTINGS_TOP_DEF = {
.menu = &UI_SETTINGS.tabsMenu,
.load = uiSettingsLoad,
.hasChanges = uiSettingsHasChanges
};
errorret_t uiSettingsDiscardUpdate(uisettingstabdef_t *def);
void uiSettingsDiscardConfirmResult(const bool_t result, void *user);
static uisettingstabdef_t *UI_SETTINGS_PENDING_DISCARD = NULL;
void uiSettingsTabSelected(
const uimenu_t *menu,
@@ -172,11 +159,12 @@ errorret_t uiSettingsDraw(void) {
}
errorret_t uiSettingsUpdate(void) {
for(uint8_t i = 0; i < UI_SETTINGS_TAB_COUNT; i++) {
errorChain(uiSettingsDiscardUpdate(&UI_SETTINGS_TAB_DEFS[i]));
}
if(!UI_SETTINGS_PENDING_DISCARD) errorOk();
return uiSettingsDiscardUpdate(&UI_SETTINGS_TOP_DEF);
uisettingstabdef_t *def = UI_SETTINGS_PENDING_DISCARD;
UI_SETTINGS_PENDING_DISCARD = NULL;
return uiSettingsDiscardUpdate(def);
}
bool_t uiSettingsIsOpen(void) {
@@ -217,13 +205,11 @@ bool_t uiSettingsHasChanges(void) {
void uiSettingsDiscardMenuClosed(const uimenu_t *menu) {
uisettingstabdef_t *def = (uisettingstabdef_t *)menu->user;
if(def->hasChanges()) def->pendingDiscardConfirm = true;
if(def->hasChanges()) UI_SETTINGS_PENDING_DISCARD = def;
}
errorret_t uiSettingsDiscardUpdate(uisettingstabdef_t *def) {
if(!def->pendingDiscardConfirm) errorOk();
def->pendingDiscardConfirm = false;
#define UI_SETTINGS_DISCARD_TEXT_MAX 128
char_t text[UI_SETTINGS_DISCARD_TEXT_MAX];
errorChain(assetLocaleGetString(
&LOCALE.entry->data.locale,
@@ -232,6 +218,7 @@ errorret_t uiSettingsDiscardUpdate(uisettingstabdef_t *def) {
text,
UI_SETTINGS_DISCARD_TEXT_MAX
));
#undef UI_SETTINGS_DISCARD_TEXT_MAX
uiConfirmOpen(text, uiSettingsDiscardConfirmResult, def);
@@ -242,12 +229,9 @@ void uiSettingsDiscardConfirmResult(const bool_t result, void *user) {
uisettingstabdef_t *def = (uisettingstabdef_t *)user;
if(!result) {
// Cancel means "keep editing" - reopen without reloading so the
// unsaved widget values the user had are preserved.
uiMenuOpen(def->menu);
return;
}
// Confirmed - reload the widgets back to the actual engine values.
def->load();
}
+33 -5
View File
@@ -9,10 +9,6 @@
#include "error/error.h"
#include "ui/widget/uimenu.h"
#define UI_SETTINGS_TAB_GAMEPLAY 0
#define UI_SETTINGS_TAB_INPUT 1
#define UI_SETTINGS_TAB_DISPLAY 2
#define UI_SETTINGS_TAB_AUDIO 3
#define UI_SETTINGS_TAB_COUNT 4
#define UI_SETTINGS_TAB_LABEL_MAX 32
#define UI_SETTINGS_APPLY_LABEL_MAX 32
@@ -24,18 +20,50 @@ typedef struct {
char_t applyLabel[UI_SETTINGS_APPLY_LABEL_MAX];
} uisettings_t;
typedef struct {
const char_t *localeId;
errorret_t (*init)(void);
uimenu_t *menu;
void (*load)(void);
void (*apply)(void);
bool_t (*hasChanges)(void);
} uisettingstabdef_t;
extern uisettings_t UI_SETTINGS;
extern uisettingstabdef_t UI_SETTINGS_TAB_DEFS[UI_SETTINGS_TAB_COUNT];
/**
* Menu closed callback that flags a pending discard-changes
* confirmation if the menu's settings page/panel reports unsaved
* changes. Pass this directly as a settings page menu's closed
* callback.
* callback. The pending def is picked up by uiSettingsUpdate on the
* next tick.
*
* @param menu The menu that was just closed.
*/
void uiSettingsDiscardMenuClosed(const uimenu_t *menu);
/**
* Shows the discard-changes confirmation dialog for def. Called once
* by uiSettingsUpdate when uiSettingsDiscardMenuClosed has flagged a
* pending def.
*
* @param def The tab/panel def to show the confirmation for.
* @return Any error that occurs.
*/
errorret_t uiSettingsDiscardUpdate(uisettingstabdef_t *def);
/**
* Callback passed to uiConfirmOpen by uiSettingsDiscardUpdate. Reopens
* the menu if the user chose to keep editing, otherwise reloads the
* widgets back to the actual engine values.
*
* @param result True if the user confirmed discarding changes, false
* to keep editing.
* @param user The uisettingstabdef_t* this confirmation belongs to.
*/
void uiSettingsDiscardConfirmResult(const bool_t result, void *user);
/**
* Initializes the settings panel and all of its category pages.
*
+1 -1
View File
@@ -18,7 +18,6 @@ void uiSettingsAudioSelected(
) {
if(index != UI_SETTINGS_AUDIO_INDEX_APPLY) return;
uiSettingsAudioApply();
uiMenuClose(&UI_SETTINGS_AUDIO.menu);
}
errorret_t uiSettingsAudioInit(void) {
@@ -40,6 +39,7 @@ void uiSettingsAudioLoad(void) {
}
void uiSettingsAudioApply(void) {
uiMenuClose(&UI_SETTINGS_AUDIO.menu);
}
bool_t uiSettingsAudioHasChanges(void) {
@@ -18,7 +18,6 @@ void uiSettingsDisplaySelected(
) {
if(index != UI_SETTINGS_DISPLAY_INDEX_APPLY) return;
uiSettingsDisplayApply();
uiMenuClose(&UI_SETTINGS_DISPLAY.menu);
}
errorret_t uiSettingsDisplayInit(void) {
@@ -40,6 +39,7 @@ void uiSettingsDisplayLoad(void) {
}
void uiSettingsDisplayApply(void) {
uiMenuClose(&UI_SETTINGS_DISPLAY.menu);
}
bool_t uiSettingsDisplayHasChanges(void) {
@@ -13,26 +13,18 @@
uisettingsgameplay_t UI_SETTINGS_GAMEPLAY;
void uiSettingsGameplayShowConfirmResult(const bool_t result, void *user) {
consolePrint("Confirm dialog result: %s", result ? "confirmed" : "cancelled");
}
void uiSettingsGameplaySelected(
const uimenu_t *menu,
const uint8_t index,
const uimenuitem_t *item
) {
if(index == UI_SETTINGS_GAMEPLAY_INDEX_SHOW_CONFIRM) {
uiConfirmOpen(
"Are you sure?", uiSettingsGameplayShowConfirmResult, NULL
);
return;
}
switch(index) {
case UI_SETTINGS_GAMEPLAY_INDEX_APPLY:
uiSettingsGameplayApply();
break;
if(index == UI_SETTINGS_GAMEPLAY_INDEX_APPLY) {
uiSettingsGameplayApply();
uiMenuClose(&UI_SETTINGS_GAMEPLAY.menu);
return;
default:
break;
}
}
@@ -43,48 +35,20 @@ errorret_t uiSettingsGameplayInit(void) {
&UI_SETTINGS_GAMEPLAY.menu, UI_SETTINGS_GAMEPLAY.items,
uiSettingsGameplaySelected, uiSettingsDiscardMenuClosed, NULL
);
MENU_BUTTON("Do thing");
MENU_BUTTON("Do other thing");
MENU_CHECKBOX("Enable thing?");
MENU_SLIDER_FLOAT("test", 1.0f, 1.0f, 4.0f, 0.5f);
MENU_BUTTON("Show confirm");
MENU_BUTTON(UI_SETTINGS.applyLabel);
MENU_BUTTON(UI_SETTINGS.applyLabel);
MENU_END(UI_SETTINGS_GAMEPLAY.items, 1);
errorOk();
}
void uiSettingsGameplayLoad(void) {
UI_SETTINGS_GAMEPLAY.loadedEnableThing = uiCheckboxIsChecked(
&UI_SETTINGS_GAMEPLAY.items[UI_SETTINGS_GAMEPLAY_INDEX_ENABLE_THING].checkbox
);
UI_SETTINGS_GAMEPLAY.loadedTestValue = uiSliderGetFloat(
&UI_SETTINGS_GAMEPLAY.items[UI_SETTINGS_GAMEPLAY_INDEX_TEST_SLIDER].slider
);
}
void uiSettingsGameplayApply(void) {
UI_SETTINGS_GAMEPLAY.loadedEnableThing = uiCheckboxIsChecked(
&UI_SETTINGS_GAMEPLAY.items[UI_SETTINGS_GAMEPLAY_INDEX_ENABLE_THING].checkbox
);
UI_SETTINGS_GAMEPLAY.loadedTestValue = uiSliderGetFloat(
&UI_SETTINGS_GAMEPLAY.items[UI_SETTINGS_GAMEPLAY_INDEX_TEST_SLIDER].slider
);
uiMenuClose(&UI_SETTINGS_GAMEPLAY.menu);
}
bool_t uiSettingsGameplayHasChanges(void) {
if(
uiCheckboxIsChecked(
&UI_SETTINGS_GAMEPLAY.items[UI_SETTINGS_GAMEPLAY_INDEX_ENABLE_THING].checkbox
) != UI_SETTINGS_GAMEPLAY.loadedEnableThing
) return true;
if(
uiSliderGetFloat(
&UI_SETTINGS_GAMEPLAY.items[UI_SETTINGS_GAMEPLAY_INDEX_TEST_SLIDER].slider
) != UI_SETTINGS_GAMEPLAY.loadedTestValue
) return true;
return false;
}
@@ -19,8 +19,6 @@
typedef struct {
uimenu_t menu;
uimenuitem_t items[UI_SETTINGS_GAMEPLAY_ITEM_COUNT];
bool_t loadedEnableThing;
float_t loadedTestValue;
} uisettingsgameplay_t;
extern uisettingsgameplay_t UI_SETTINGS_GAMEPLAY;
+1 -1
View File
@@ -21,7 +21,6 @@ void uiSettingsInputSelected(
) {
if(index != UI_SETTINGS_INPUT_INDEX_APPLY) return;
uiSettingsInputApply();
uiMenuClose(&UI_SETTINGS_INPUT.menu);
}
errorret_t uiSettingsInputInit(void) {
@@ -67,6 +66,7 @@ void uiSettingsInputApply(void) {
&UI_SETTINGS_INPUT.items[UI_SETTINGS_INPUT_INDEX_DEADZONE].slider
);
#endif
uiMenuClose(&UI_SETTINGS_INPUT.menu);
}
bool_t uiSettingsInputHasChanges(void) {