2022-02-03 16:24:36 +01:00
|
|
|
#include "wups/config/WUPSConfigItemMultipleValues.h"
|
2021-09-24 19:57:15 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2023-12-16 17:10:45 +01:00
|
|
|
#include <wups/config.h>
|
2021-09-24 19:57:15 +02:00
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
static int32_t WUPSConfigItemMultipleValues_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) {
|
2021-09-24 19:57:15 +02:00
|
|
|
auto *item = (ConfigItemMultipleValues *) context;
|
|
|
|
|
|
|
|
if (item->values && item->valueIndex >= 0 && item->valueIndex < item->valueCount) {
|
|
|
|
if (item->values[item->valueIndex].valueName == nullptr) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
strncpy(out_buf, item->values[item->valueIndex].valueName, out_size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
static void WUPSConfigItemMultipleValues_onCloseCallback(void *context) {
|
2021-09-24 19:57:15 +02:00
|
|
|
auto *item = (ConfigItemMultipleValues *) context;
|
2023-12-16 17:10:45 +01:00
|
|
|
if (item->valueIndexAtCreation != item->valueIndex && item->valueChangedCallback != nullptr) {
|
|
|
|
if (item->values && item->valueIndex >= 0 && item->valueIndex < item->valueCount) {
|
|
|
|
((MultipleValuesChangedCallback) (item->valueChangedCallback))(item, item->values[item->valueIndex].value);
|
|
|
|
}
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
static void WUPSConfigItemMultipleValues_onInput(void *context, WUPSConfigSimplePadData input) {
|
2021-09-24 19:57:15 +02:00
|
|
|
auto *item = (ConfigItemMultipleValues *) context;
|
2023-12-16 17:10:45 +01:00
|
|
|
if (input.buttons_d & WUPS_CONFIG_BUTTON_LEFT) {
|
2021-09-24 19:57:15 +02:00
|
|
|
item->valueIndex--;
|
2023-12-16 17:10:45 +01:00
|
|
|
} else if (input.buttons_d & WUPS_CONFIG_BUTTON_RIGHT) {
|
2021-09-24 19:57:15 +02:00
|
|
|
item->valueIndex++;
|
|
|
|
}
|
|
|
|
if (item->valueIndex < 0) {
|
|
|
|
item->valueIndex = 0;
|
|
|
|
} else if (item->valueIndex >= item->valueCount) {
|
|
|
|
item->valueIndex = item->valueCount - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
static int32_t WUPSConfigItemMultipleValues_getCurrentValueSelectedDisplay(void *context, char *out_buf, int32_t out_size) {
|
2021-09-24 19:57:15 +02:00
|
|
|
auto *item = (ConfigItemMultipleValues *) context;
|
|
|
|
if (item->values && item->valueIndex >= 0 && item->valueIndex < item->valueCount) {
|
|
|
|
if (item->valueCount == 1) {
|
|
|
|
snprintf(out_buf, out_size, " %s ", item->values[item->valueIndex].valueName);
|
|
|
|
} else if (item->valueIndex == 0) {
|
|
|
|
snprintf(out_buf, out_size, " %s >", item->values[item->valueIndex].valueName);
|
|
|
|
} else if (item->valueIndex + 1 == item->valueCount) {
|
|
|
|
snprintf(out_buf, out_size, "< %s ", item->values[item->valueIndex].valueName);
|
|
|
|
} else {
|
|
|
|
snprintf(out_buf, out_size, "< %s >", item->values[item->valueIndex].valueName);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
static void WUPSConfigItemMultipleValues_restoreDefault(void *context) {
|
2022-02-03 16:24:36 +01:00
|
|
|
auto *item = (ConfigItemMultipleValues *) context;
|
2021-09-24 19:57:15 +02:00
|
|
|
item->valueIndex = item->defaultValueIndex;
|
|
|
|
}
|
|
|
|
|
2023-11-17 13:50:27 +01:00
|
|
|
static void WUPSConfigItemMultipleValues_Cleanup(ConfigItemMultipleValues *item) {
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < item->valueCount; ++i) {
|
2023-12-16 17:10:45 +01:00
|
|
|
free((void *) item->values[i].valueName);
|
2023-11-17 13:50:27 +01:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
free((void *) item->identifier);
|
2023-11-17 13:50:27 +01:00
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
free(item->values);
|
2023-11-17 13:50:27 +01:00
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
static void WUPSConfigItemMultipleValues_onDelete(void *context) {
|
2023-11-17 13:50:27 +01:00
|
|
|
auto *item = (ConfigItemMultipleValues *) context;
|
|
|
|
WUPSConfigItemMultipleValues_Cleanup(item);
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
extern "C" WUPSConfigAPIStatus
|
|
|
|
WUPSConfigItemMultipleValues_Create(const char *identifier, const char *displayName,
|
|
|
|
int32_t defaultValueIndex, int currentValueIndex,
|
|
|
|
ConfigItemMultipleValuesPair *possibleValues,
|
|
|
|
int pairCount, MultipleValuesChangedCallback callback,
|
|
|
|
WUPSConfigItemHandle *outHandle) {
|
|
|
|
if (outHandle == nullptr || displayName == nullptr || possibleValues == nullptr || pairCount < 0 ||
|
|
|
|
defaultValueIndex >= pairCount || currentValueIndex >= pairCount) {
|
|
|
|
return WUPSCONFIG_API_RESULT_INVALID_ARGUMENT;
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
2023-12-16 17:10:45 +01:00
|
|
|
*outHandle = {};
|
2021-09-24 19:57:15 +02:00
|
|
|
auto *item = (ConfigItemMultipleValues *) malloc(sizeof(ConfigItemMultipleValues));
|
|
|
|
if (item == nullptr) {
|
2023-12-16 17:10:45 +01:00
|
|
|
return WUPSCONFIG_API_RESULT_OUT_OF_MEMORY;
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
2023-12-16 17:10:45 +01:00
|
|
|
*item = {};
|
2021-09-24 19:57:15 +02:00
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
item->values = (ConfigItemMultipleValuesPair *) malloc(sizeof(ConfigItemMultipleValuesPair) * pairCount);
|
|
|
|
if (!item->values) {
|
|
|
|
WUPSConfigItemMultipleValues_Cleanup(item);
|
|
|
|
return WUPSCONFIG_API_RESULT_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (identifier != nullptr) {
|
|
|
|
item->identifier = strdup(identifier);
|
|
|
|
} else {
|
|
|
|
item->identifier = nullptr;
|
|
|
|
}
|
2021-09-24 19:57:15 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < pairCount; ++i) {
|
2023-12-16 17:10:45 +01:00
|
|
|
item->values[i].value = possibleValues[i].value;
|
2021-09-24 19:57:15 +02:00
|
|
|
if (possibleValues[i].valueName == nullptr) {
|
2023-12-16 17:10:45 +01:00
|
|
|
item->values[i].valueName = nullptr;
|
2021-09-24 19:57:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-12-16 17:10:45 +01:00
|
|
|
item->values[i].valueName = strdup(possibleValues[i].valueName);
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
item->valueCount = pairCount;
|
|
|
|
item->defaultValueIndex = defaultValueIndex;
|
|
|
|
item->valueIndex = currentValueIndex;
|
|
|
|
item->valueIndexAtCreation = currentValueIndex;
|
|
|
|
item->valueChangedCallback = (void *) callback;
|
2021-09-24 19:57:15 +02:00
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
WUPSConfigAPIItemCallbacksV2 callbacks = {
|
2022-02-03 16:24:36 +01:00
|
|
|
.getCurrentValueDisplay = &WUPSConfigItemMultipleValues_getCurrentValueDisplay,
|
2021-09-24 19:57:15 +02:00
|
|
|
.getCurrentValueSelectedDisplay = &WUPSConfigItemMultipleValues_getCurrentValueSelectedDisplay,
|
2023-12-16 17:10:45 +01:00
|
|
|
.onSelected = nullptr,
|
2022-02-03 16:24:36 +01:00
|
|
|
.restoreDefault = &WUPSConfigItemMultipleValues_restoreDefault,
|
2023-12-16 17:10:45 +01:00
|
|
|
.isMovementAllowed = nullptr,
|
|
|
|
.onCloseCallback = &WUPSConfigItemMultipleValues_onCloseCallback,
|
|
|
|
.onInput = &WUPSConfigItemMultipleValues_onInput,
|
|
|
|
.onInputEx = nullptr,
|
2022-02-03 16:24:36 +01:00
|
|
|
.onDelete = &WUPSConfigItemMultipleValues_onDelete};
|
2021-09-24 19:57:15 +02:00
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
WUPSConfigAPIItemOptionsV2 options = {
|
2023-10-01 13:00:55 +02:00
|
|
|
.displayName = displayName,
|
|
|
|
.context = item,
|
|
|
|
.callbacks = callbacks,
|
|
|
|
};
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
WUPSConfigAPIStatus err;
|
|
|
|
if ((err = WUPSConfigAPI_Item_Create(options, &item->handle)) != WUPSCONFIG_API_RESULT_SUCCESS) {
|
2023-11-17 13:50:27 +01:00
|
|
|
WUPSConfigItemMultipleValues_Cleanup(item);
|
2023-12-16 17:10:45 +01:00
|
|
|
return err;
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
2023-12-16 17:10:45 +01:00
|
|
|
*outHandle = item->handle;
|
|
|
|
return WUPSCONFIG_API_RESULT_SUCCESS;
|
|
|
|
}
|
2021-09-24 19:57:15 +02:00
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
extern "C" WUPSConfigAPIStatus
|
|
|
|
WUPSConfigItemMultipleValues_AddToCategory(WUPSConfigCategoryHandle cat, const char *identifier, const char *displayName,
|
|
|
|
int32_t defaultValueIndex, int currentValueIndex,
|
|
|
|
ConfigItemMultipleValuesPair *possibleValues, int pairCount,
|
|
|
|
MultipleValuesChangedCallback callback) {
|
|
|
|
WUPSConfigItemHandle itemHandle;
|
|
|
|
WUPSConfigAPIStatus res;
|
|
|
|
if ((res = WUPSConfigItemMultipleValues_Create(identifier,
|
|
|
|
displayName,
|
|
|
|
defaultValueIndex, currentValueIndex,
|
|
|
|
possibleValues, pairCount,
|
|
|
|
callback,
|
|
|
|
&itemHandle)) != WUPSCONFIG_API_RESULT_SUCCESS) {
|
|
|
|
return res;
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
2023-12-16 17:10:45 +01:00
|
|
|
if ((res = WUPSConfigAPI_Category_AddItem(cat, itemHandle)) != WUPSCONFIG_API_RESULT_SUCCESS) {
|
|
|
|
WUPSConfigAPI_Item_Destroy(itemHandle);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return WUPSCONFIG_API_RESULT_SUCCESS;
|
|
|
|
}
|