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>
|
2022-02-03 16:24:36 +01:00
|
|
|
#include <wups.h>
|
2021-09-24 19:57:15 +02:00
|
|
|
|
|
|
|
void WUPSConfigItemMultipleValues_onDelete(void *context);
|
|
|
|
|
|
|
|
int32_t WUPSConfigItemMultipleValues_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WUPSConfigItemMultipleValues_callCallback(void *context) {
|
|
|
|
auto *item = (ConfigItemMultipleValues *) context;
|
|
|
|
if (item->callback != nullptr && item->values && item->valueIndex >= 0 && item->valueIndex < item->valueCount) {
|
|
|
|
((MultipleValuesChangedCallback) (item->callback))(item, item->values[item->valueIndex].value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WUPSConfigItemMultipleValues_onButtonPressed(void *context, WUPSConfigButtons buttons) {
|
|
|
|
auto *item = (ConfigItemMultipleValues *) context;
|
|
|
|
if (buttons & WUPS_CONFIG_BUTTON_LEFT) {
|
|
|
|
item->valueIndex--;
|
|
|
|
} else if (buttons & WUPS_CONFIG_BUTTON_RIGHT) {
|
|
|
|
item->valueIndex++;
|
|
|
|
}
|
|
|
|
if (item->valueIndex < 0) {
|
|
|
|
item->valueIndex = 0;
|
|
|
|
} else if (item->valueIndex >= item->valueCount) {
|
|
|
|
item->valueIndex = item->valueCount - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WUPSConfigItemMultipleValues_isMovementAllowed(void *context) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t WUPSConfigItemMultipleValues_getCurrentValueSelectedDisplay(void *context, char *out_buf, int32_t out_size) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WUPSConfigItemMultipleValues_onSelected(void *context, bool isSelected) {
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" bool
|
|
|
|
WUPSConfigItemMultipleValues_AddToCategory(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName,
|
|
|
|
int32_t defaultValueIndex, ConfigItemMultipleValuesPair *possibleValues,
|
|
|
|
int pairCount, MultipleValuesChangedCallback callback) {
|
|
|
|
if (cat == 0 || displayName == nullptr || possibleValues == nullptr || pairCount < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto *item = (ConfigItemMultipleValues *) malloc(sizeof(ConfigItemMultipleValues));
|
|
|
|
if (item == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *values = (ConfigItemMultipleValuesPair *) malloc(sizeof(ConfigItemMultipleValuesPair) * pairCount);
|
|
|
|
|
|
|
|
for (int i = 0; i < pairCount; ++i) {
|
|
|
|
values[i].value = possibleValues[i].value;
|
|
|
|
if (possibleValues[i].valueName == nullptr) {
|
|
|
|
values[i].valueName = nullptr;
|
|
|
|
continue;
|
|
|
|
}
|
2022-02-03 16:24:36 +01:00
|
|
|
auto bufLen = strlen(possibleValues[i].valueName) + 1;
|
2021-09-24 19:57:15 +02:00
|
|
|
values[i].valueName = (char *) malloc(bufLen);
|
|
|
|
strncpy(values[i].valueName, possibleValues[i].valueName, bufLen);
|
|
|
|
}
|
|
|
|
|
2022-02-03 16:24:36 +01:00
|
|
|
item->valueCount = pairCount;
|
|
|
|
item->values = values;
|
|
|
|
item->valueIndex = defaultValueIndex;
|
2021-09-24 19:57:15 +02:00
|
|
|
item->defaultValueIndex = defaultValueIndex;
|
2022-02-03 16:24:36 +01:00
|
|
|
item->callback = (void *) callback;
|
2021-09-24 19:57:15 +02:00
|
|
|
|
|
|
|
if (configID != nullptr) {
|
|
|
|
auto configIDLen = strlen(configID) + 1;
|
2022-02-03 16:24:36 +01:00
|
|
|
item->configID = (char *) malloc(configIDLen);
|
2021-09-24 19:57:15 +02:00
|
|
|
strncpy(item->configID, configID, configIDLen);
|
|
|
|
} else {
|
|
|
|
item->configID = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
WUPSConfigCallbacks_t callbacks = {
|
2022-02-03 16:24:36 +01:00
|
|
|
.getCurrentValueDisplay = &WUPSConfigItemMultipleValues_getCurrentValueDisplay,
|
2021-09-24 19:57:15 +02:00
|
|
|
.getCurrentValueSelectedDisplay = &WUPSConfigItemMultipleValues_getCurrentValueSelectedDisplay,
|
2022-02-03 16:24:36 +01:00
|
|
|
.onSelected = &WUPSConfigItemMultipleValues_onSelected,
|
|
|
|
.restoreDefault = &WUPSConfigItemMultipleValues_restoreDefault,
|
|
|
|
.isMovementAllowed = &WUPSConfigItemMultipleValues_isMovementAllowed,
|
|
|
|
.callCallback = &WUPSConfigItemMultipleValues_callCallback,
|
|
|
|
.onButtonPressed = &WUPSConfigItemMultipleValues_onButtonPressed,
|
|
|
|
.onDelete = &WUPSConfigItemMultipleValues_onDelete};
|
2021-09-24 19:57:15 +02:00
|
|
|
|
|
|
|
if (WUPSConfigItem_Create(&item->handle, configID, displayName, callbacks, item) < 0) {
|
|
|
|
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 WUPSConfigItemMultipleValues_onDelete(void *context) {
|
|
|
|
auto *item = (ConfigItemMultipleValues *) context;
|
|
|
|
|
|
|
|
for (int i = 0; i < item->valueCount; ++i) {
|
|
|
|
free(item->values[i].valueName);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(item->configID);
|
|
|
|
free(item->values);
|
|
|
|
|
|
|
|
free(item);
|
|
|
|
}
|