2021-04-07 00:23:23 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (C) 2018 Maschell
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "utils/StringTools.h"
|
|
|
|
#include "utils/logger.h"
|
2022-02-04 16:25:44 +01:00
|
|
|
#include <wups/config.h>
|
2021-04-07 00:23:23 +02:00
|
|
|
|
|
|
|
class WUPSConfigItem {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
Sets the display name of this WUPSConfigItem
|
|
|
|
This is the value which will be shown in the configuration menu.
|
|
|
|
**/
|
2023-11-04 15:32:45 +01:00
|
|
|
virtual void setDisplayName(std::string_view _displayName) {
|
2021-04-07 00:23:23 +02:00
|
|
|
this->displayName = _displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\return Returns the display name of this WUPSConfigItem
|
|
|
|
**/
|
|
|
|
virtual const std::string &getDisplayName() {
|
|
|
|
return this->displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Sets the config ID name of this WUPSConfigItem.
|
|
|
|
This config ID is used to persist the configuration values and needs
|
|
|
|
to be unique in the context of this WUPSConfig.
|
|
|
|
Items in different categories are NOT allowed to have the config ID.
|
|
|
|
**/
|
2023-11-04 15:32:45 +01:00
|
|
|
virtual void setConfigID(std::string_view _configID) {
|
2021-04-07 00:23:23 +02:00
|
|
|
this->configID = _configID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\return Returns the configID of this WUPSConfigItem.
|
|
|
|
**/
|
|
|
|
[[nodiscard]] virtual const std::string &getConfigID() const {
|
|
|
|
return this->configID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns a string that displays the current value.
|
|
|
|
This string is shown next to the display name when the cursor is NOT on this item
|
|
|
|
**/
|
|
|
|
[[nodiscard]] std::string getCurrentValueDisplay() const {
|
|
|
|
if (this->callbacks.getCurrentValueDisplay != nullptr) {
|
2022-09-18 21:46:30 +02:00
|
|
|
char buf[80];
|
2021-04-07 00:23:23 +02:00
|
|
|
int res = this->callbacks.getCurrentValueDisplay(context, buf, sizeof(buf));
|
|
|
|
if (res == 0) {
|
|
|
|
return buf;
|
|
|
|
} else {
|
2022-05-14 14:00:20 +02:00
|
|
|
return string_format("[ERROR %d]", res);
|
2021-04-07 00:23:23 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("NOT IMPLEMENTED");
|
2021-04-07 00:23:23 +02:00
|
|
|
return "NOT_IMPLEMENTED";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns a string that displays the current value when selected.
|
|
|
|
This string is shown next to the display name when the cursor IS on this item
|
|
|
|
**/
|
|
|
|
[[nodiscard]] std::string getCurrentValueSelectedDisplay() const {
|
|
|
|
if (this->callbacks.getCurrentValueSelectedDisplay != nullptr) {
|
2022-09-18 21:46:30 +02:00
|
|
|
char buf[80];
|
2021-04-07 00:23:23 +02:00
|
|
|
int res = this->callbacks.getCurrentValueSelectedDisplay(context, buf, sizeof(buf));
|
|
|
|
if (res == 0) {
|
|
|
|
return buf;
|
|
|
|
} else {
|
2022-05-14 14:00:20 +02:00
|
|
|
return string_format("[ERROR %d]", res);
|
2021-04-07 00:23:23 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("NOT IMPLEMENTED");
|
2021-04-07 00:23:23 +02:00
|
|
|
return "NOT_IMPLEMENTED";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Is called when the cursor enters or leaves the item.
|
|
|
|
When the cursor enters the item, "isSelected" will be true.
|
|
|
|
When the cursor leaves the item, "isSelected" will be false.
|
|
|
|
**/
|
|
|
|
void onSelected(bool isSelected) const {
|
|
|
|
if (this->callbacks.onSelected != nullptr) {
|
|
|
|
this->callbacks.onSelected(context, isSelected);
|
2022-04-22 22:55:53 +02:00
|
|
|
return;
|
2021-04-07 00:23:23 +02:00
|
|
|
}
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("NOT IMPLEMENTED");
|
2021-04-07 00:23:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Is called when a button is pressed while the cursor on this item.
|
|
|
|
See the WUPSConfigButtons enum for possible values.
|
|
|
|
**/
|
|
|
|
void onButtonPressed(WUPSConfigButtons buttons) const {
|
|
|
|
if (this->callbacks.onButtonPressed != nullptr) {
|
|
|
|
this->callbacks.onButtonPressed(context, buttons);
|
2022-04-22 22:55:53 +02:00
|
|
|
return;
|
2021-04-07 00:23:23 +02:00
|
|
|
}
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("NOT IMPLEMENTED");
|
2021-04-07 00:23:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
When the cursor is on this item, the configuration menu asks this item
|
|
|
|
if it's allowed to leave it.
|
|
|
|
If it returns true, the item can be leaved.
|
|
|
|
It it returns false, leaves is not allowed.
|
|
|
|
**/
|
|
|
|
[[nodiscard]] bool isMovementAllowed() const {
|
|
|
|
if (this->callbacks.isMovementAllowed != nullptr) {
|
|
|
|
return this->callbacks.isMovementAllowed(context);
|
|
|
|
}
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("NOT IMPLEMENTED");
|
2021-04-07 00:23:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Restores the default value
|
|
|
|
**/
|
|
|
|
void restoreDefault() {
|
|
|
|
if (this->callbacks.restoreDefault != nullptr) {
|
|
|
|
this->callbacks.restoreDefault(context);
|
2022-04-22 22:55:53 +02:00
|
|
|
return;
|
2021-04-07 00:23:23 +02:00
|
|
|
}
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("NOT IMPLEMENTED");
|
2021-04-07 00:23:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Call callback with with current value.
|
|
|
|
This function will be called whenever this item should call it's (optional) given
|
|
|
|
callback with the current value.
|
|
|
|
Returns true if a valid callback could be called
|
2021-09-25 14:26:18 +02:00
|
|
|
Returns false if no callback was called (e.g. callback was nullptr)
|
2021-04-07 00:23:23 +02:00
|
|
|
**/
|
|
|
|
bool callCallback() {
|
|
|
|
if (this->callbacks.callCallback != nullptr) {
|
|
|
|
return this->callbacks.callCallback(context);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDirty() {
|
|
|
|
return defaultValue != getCurrentValueDisplay();
|
|
|
|
}
|
|
|
|
|
2023-11-04 15:32:45 +01:00
|
|
|
WUPSConfigItem(std::string_view _configID, std::string_view _displayName, WUPSConfigCallbacks_t callbacks, void *_context) {
|
2022-02-04 16:25:44 +01:00
|
|
|
this->configID = _configID;
|
|
|
|
this->displayName = _displayName;
|
|
|
|
this->context = _context;
|
|
|
|
this->callbacks = callbacks;
|
2021-04-07 00:23:23 +02:00
|
|
|
this->defaultValue = getCurrentValueDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~WUPSConfigItem() {
|
|
|
|
if (this->callbacks.onDelete != nullptr) {
|
|
|
|
this->callbacks.onDelete(context);
|
2022-04-22 22:55:53 +02:00
|
|
|
return;
|
2021-04-07 00:23:23 +02:00
|
|
|
}
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("NOT IMPLEMENTED");
|
2021-04-07 00:23:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
void *context;
|
|
|
|
std::string displayName;
|
|
|
|
std::string configID;
|
|
|
|
std::string defaultValue;
|
|
|
|
WUPSConfigCallbacks_t callbacks{};
|
|
|
|
};
|