2022-02-03 16:24:36 +01:00
|
|
|
#include "wups/config/WUPSConfigItemBoolean.h"
|
2021-09-24 19:57:15 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2022-09-24 17:41:20 +02:00
|
|
|
#include <cstring>
|
2022-02-03 16:24:36 +01:00
|
|
|
#include <wups.h>
|
2021-09-24 19:57:15 +02:00
|
|
|
|
|
|
|
void WUPSConfigItemBoolean_onDelete(void *context);
|
|
|
|
|
|
|
|
int32_t WUPSConfigItemBoolean_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) {
|
|
|
|
auto *item = (ConfigItemBoolean *) context;
|
|
|
|
snprintf(out_buf, out_size, " %s", item->value ? item->trueValue : item->falseValue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void toggleValue(ConfigItemBoolean *item) {
|
|
|
|
item->value = !item->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WUPSConfigItemBoolean_callCallback(void *context) {
|
|
|
|
auto *item = (ConfigItemBoolean *) context;
|
|
|
|
if (item->callback != nullptr) {
|
|
|
|
((BooleanValueChangedCallback) (item->callback))(item, item->value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WUPSConfigItemBoolean_onButtonPressed(void *context, WUPSConfigButtons buttons) {
|
|
|
|
auto *item = (ConfigItemBoolean *) context;
|
|
|
|
if ((buttons & WUPS_CONFIG_BUTTON_A) == WUPS_CONFIG_BUTTON_A) {
|
|
|
|
toggleValue(item);
|
|
|
|
} else if (buttons & WUPS_CONFIG_BUTTON_LEFT && !item->value) {
|
|
|
|
toggleValue(item);
|
|
|
|
} else if ((buttons & WUPS_CONFIG_BUTTON_RIGHT) && item->value) {
|
|
|
|
toggleValue(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WUPSConfigItemBoolean_isMovementAllowed(void *context) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t WUPSConfigItemBoolean_getCurrentValueSelectedDisplay(void *context, char *out_buf, int32_t out_size) {
|
|
|
|
auto *item = (ConfigItemBoolean *) context;
|
|
|
|
if (item->value) {
|
|
|
|
snprintf(out_buf, out_size, " %s >", item->trueValue);
|
|
|
|
} else {
|
|
|
|
snprintf(out_buf, out_size, "< %s ", item->falseValue);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WUPSConfigItemBoolean_restoreDefault(void *context) {
|
2022-02-03 16:24:36 +01:00
|
|
|
auto *item = (ConfigItemBoolean *) context;
|
2021-09-24 19:57:15 +02:00
|
|
|
item->value = item->defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WUPSConfigItemBoolean_onSelected(void *context, bool isSelected) {
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" bool
|
2022-09-24 17:41:20 +02:00
|
|
|
WUPSConfigItemBoolean_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, bool defaultValue, BooleanValueChangedCallback callback, const char *trueValue,
|
2021-09-24 19:57:15 +02:00
|
|
|
const char *falseValue) {
|
|
|
|
if (cat == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto *item = (ConfigItemBoolean *) malloc(sizeof(ConfigItemBoolean));
|
|
|
|
if (item == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-24 17:41:20 +02:00
|
|
|
if (configId != nullptr) {
|
|
|
|
item->configId = strdup(configId);
|
|
|
|
} else {
|
|
|
|
item->configId = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-24 19:57:15 +02:00
|
|
|
item->defaultValue = defaultValue;
|
2022-02-03 16:24:36 +01:00
|
|
|
item->value = defaultValue;
|
|
|
|
item->callback = (void *) callback;
|
2021-09-24 19:57:15 +02:00
|
|
|
snprintf(item->trueValue, sizeof(item->trueValue), "%s", trueValue);
|
|
|
|
snprintf(item->falseValue, sizeof(item->falseValue), "%s", falseValue);
|
|
|
|
|
|
|
|
WUPSConfigCallbacks_t callbacks = {
|
2022-02-03 16:24:36 +01:00
|
|
|
.getCurrentValueDisplay = &WUPSConfigItemBoolean_getCurrentValueDisplay,
|
2021-09-24 19:57:15 +02:00
|
|
|
.getCurrentValueSelectedDisplay = &WUPSConfigItemBoolean_getCurrentValueSelectedDisplay,
|
2022-02-03 16:24:36 +01:00
|
|
|
.onSelected = &WUPSConfigItemBoolean_onSelected,
|
|
|
|
.restoreDefault = &WUPSConfigItemBoolean_restoreDefault,
|
|
|
|
.isMovementAllowed = &WUPSConfigItemBoolean_isMovementAllowed,
|
|
|
|
.callCallback = &WUPSConfigItemBoolean_callCallback,
|
|
|
|
.onButtonPressed = &WUPSConfigItemBoolean_onButtonPressed,
|
|
|
|
.onDelete = &WUPSConfigItemBoolean_onDelete};
|
2021-09-24 19:57:15 +02:00
|
|
|
|
2022-09-24 17:41:20 +02:00
|
|
|
if (WUPSConfigItem_Create(&item->handle, configId, displayName, callbacks, item) < 0) {
|
2021-09-24 19:57:15 +02:00
|
|
|
free(item);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WUPSConfigCategory_AddItem(cat, item->handle) < 0) {
|
2022-09-18 22:05:44 +02:00
|
|
|
WUPSConfigItem_Destroy(item->handle);
|
2021-09-24 19:57:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WUPSConfigItemBoolean_onDelete(void *context) {
|
|
|
|
auto *item = (ConfigItemBoolean *) context;
|
2022-09-24 17:41:20 +02:00
|
|
|
free(item->configId);
|
2021-09-24 19:57:15 +02:00
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" bool WUPSConfigItemBoolean_AddToCategory(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName, bool defaultValue, BooleanValueChangedCallback callback) {
|
|
|
|
return WUPSConfigItemBoolean_AddToCategoryEx(cat, configID, displayName, defaultValue, callback, "true", "false");
|
|
|
|
}
|