2022-02-03 16:24:36 +01:00
|
|
|
#include "wups/config/WUPSConfigItemIntegerRange.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
|
|
|
|
|
|
|
int32_t WUPSConfigItemIntegerRange_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) {
|
|
|
|
auto *item = (ConfigItemIntegerRange *) context;
|
|
|
|
snprintf(out_buf, out_size, "%d", item->value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WUPSConfigItemIntegerRange_callCallback(void *context) {
|
|
|
|
auto *item = (ConfigItemIntegerRange *) context;
|
|
|
|
if (item->callback != nullptr) {
|
|
|
|
((IntegerRangeValueChangedCallback) item->callback)(item, item->value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WUPSConfigItemIntegerRange_onButtonPressed(void *context, WUPSConfigButtons buttons) {
|
|
|
|
auto *item = (ConfigItemIntegerRange *) context;
|
|
|
|
if (buttons & WUPS_CONFIG_BUTTON_LEFT) {
|
|
|
|
item->value--;
|
|
|
|
} else if ((buttons & WUPS_CONFIG_BUTTON_RIGHT)) {
|
|
|
|
item->value++;
|
|
|
|
} else if ((buttons & WUPS_CONFIG_BUTTON_L)) {
|
|
|
|
item->value = item->value - 50;
|
|
|
|
} else if ((buttons & WUPS_CONFIG_BUTTON_R)) {
|
|
|
|
item->value = item->value + 50;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->value < item->minValue) {
|
|
|
|
item->value = item->minValue;
|
|
|
|
} else if (item->value > item->maxValue) {
|
|
|
|
item->value = item->maxValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WUPSConfigItemIntegerRange_isMovementAllowed(void *context) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t WUPSConfigItemIntegerRange_getCurrentValueSelectedDisplay(void *context, char *out_buf, int32_t out_size) {
|
|
|
|
auto *item = (ConfigItemIntegerRange *) context;
|
|
|
|
if (item->value == item->minValue) {
|
|
|
|
snprintf(out_buf, out_size, " %d >", item->value);
|
|
|
|
} else if (item->value == item->maxValue) {
|
|
|
|
snprintf(out_buf, out_size, "< %d ", item->value);
|
|
|
|
} else {
|
|
|
|
snprintf(out_buf, out_size, "< %d >", item->value);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WUPSConfigItemIntegerRange_restoreDefault(void *context) {
|
2022-02-03 16:24:36 +01:00
|
|
|
auto *item = (ConfigItemIntegerRange *) context;
|
2021-09-24 19:57:15 +02:00
|
|
|
item->value = item->defaultValue;
|
|
|
|
}
|
|
|
|
|
2023-11-17 13:50:27 +01:00
|
|
|
void WUPSConfigItemIntegerRange_onSelected(void *context, bool isSelected) {
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WUPSConfigItemIntegerRange_Cleanup(ConfigItemIntegerRange *item) {
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-24 17:41:20 +02:00
|
|
|
free(item->configId);
|
2021-09-24 19:57:15 +02:00
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
|
2023-11-17 13:50:27 +01:00
|
|
|
void WUPSConfigItemIntegerRange_onDelete(void *context) {
|
|
|
|
WUPSConfigItemIntegerRange_Cleanup((ConfigItemIntegerRange *) context);
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-24 17:41:20 +02:00
|
|
|
extern "C" bool WUPSConfigItemIntegerRange_AddToCategory(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, int32_t defaultValue, int32_t minValue, int32_t maxValue,
|
2021-09-24 19:57:15 +02:00
|
|
|
IntegerRangeValueChangedCallback callback) {
|
|
|
|
if (cat == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto *item = (ConfigItemIntegerRange *) malloc(sizeof(ConfigItemIntegerRange));
|
|
|
|
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->minValue = minValue;
|
|
|
|
item->maxValue = maxValue;
|
|
|
|
item->callback = (void *) callback;
|
2021-09-24 19:57:15 +02:00
|
|
|
|
2023-10-01 13:00:55 +02:00
|
|
|
WUPSConfigAPIItemCallbacksV1 callbacks = {
|
2022-02-03 16:24:36 +01:00
|
|
|
.getCurrentValueDisplay = &WUPSConfigItemIntegerRange_getCurrentValueDisplay,
|
2021-09-24 19:57:15 +02:00
|
|
|
.getCurrentValueSelectedDisplay = &WUPSConfigItemIntegerRange_getCurrentValueSelectedDisplay,
|
2022-02-03 16:24:36 +01:00
|
|
|
.onSelected = &WUPSConfigItemIntegerRange_onSelected,
|
|
|
|
.restoreDefault = &WUPSConfigItemIntegerRange_restoreDefault,
|
|
|
|
.isMovementAllowed = &WUPSConfigItemIntegerRange_isMovementAllowed,
|
|
|
|
.callCallback = &WUPSConfigItemIntegerRange_callCallback,
|
|
|
|
.onButtonPressed = &WUPSConfigItemIntegerRange_onButtonPressed,
|
|
|
|
.onDelete = &WUPSConfigItemIntegerRange_onDelete};
|
2021-09-24 19:57:15 +02:00
|
|
|
|
2023-10-01 13:00:55 +02:00
|
|
|
WUPSConfigAPIItemOptionsV1 options = {
|
|
|
|
.displayName = displayName,
|
|
|
|
.context = item,
|
|
|
|
.callbacks = callbacks,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (WUPSConfigAPI_Item_Create(options, &(item->handle)) != WUPSCONFIG_API_RESULT_SUCCESS) {
|
2023-11-17 13:50:27 +01:00
|
|
|
WUPSConfigItemIntegerRange_Cleanup(item);
|
2021-09-24 19:57:15 +02:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2023-10-01 13:00:55 +02:00
|
|
|
if (WUPSConfigAPI_Category_AddItem(cat, item->handle) != WUPSCONFIG_API_RESULT_SUCCESS) {
|
|
|
|
WUPSConfigAPI_Item_Destroy(item->handle);
|
2021-09-24 19:57:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|