From b29787896ce8e5ae35d360ded8cbf6394736f6d5 Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 7 Feb 2019 13:03:48 +0100 Subject: [PATCH] Add new ConfigItem type to a set integers in a given range. --- src/config/WUPSConfigItemIntegerRange.cpp | 100 ++++++++++++++++++ .../wups/config/WUPSConfigItemIntegerRange.h | 61 +++++++++++ 2 files changed, 161 insertions(+) create mode 100644 src/config/WUPSConfigItemIntegerRange.cpp create mode 100644 wups_include/wups/config/WUPSConfigItemIntegerRange.h diff --git a/src/config/WUPSConfigItemIntegerRange.cpp b/src/config/WUPSConfigItemIntegerRange.cpp new file mode 100644 index 0000000..1f754b9 --- /dev/null +++ b/src/config/WUPSConfigItemIntegerRange.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** + * Copyright (C) 2019 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 . + ****************************************************************************/ + +#include +#include + +WUPSConfigItemIntegerRange::WUPSConfigItemIntegerRange(std::string configID, std::string displayName, int32_t defaultValue, int32_t _minValue, int32_t _maxValue, IntegerRangeValueChangedCallback callback) : WUPSConfigItem(configID,displayName) { + this->defaultValue = defaultValue; + this->value = defaultValue; + this->minValue = _minValue; + this->maxValue = _maxValue; + this->callback = callback; +} + +WUPSConfigItemIntegerRange::~WUPSConfigItemIntegerRange() { + +} + +std::string WUPSConfigItemIntegerRange::getCurrentValueDisplay() { + return " " + std::to_string(this->value); +} + +std::string WUPSConfigItemIntegerRange::getCurrentValueSelectedDisplay() { + if(value == this->minValue) { + return " " + std::to_string(this->value) + " >"; + } else if(value == this->maxValue){ + return "< " + std::to_string(this->value); + } + return "< " + std::to_string(this->value) + " >"; +} + +void WUPSConfigItemIntegerRange::onSelected(bool isSelected) { + +} + +void WUPSConfigItemIntegerRange::onButtonPressed(WUPSConfigButtons buttons) { + if(buttons & WUPS_CONFIG_BUTTON_LEFT){ + this->value--; + }else if((buttons & WUPS_CONFIG_BUTTON_RIGHT)){ + this->value++; + }else if((buttons & WUPS_CONFIG_BUTTON_L)){ + this->value = this->value - 50; + }else if((buttons & WUPS_CONFIG_BUTTON_R)){ + this->value = this->value + 50; + } + + if(this->value < this->minValue){ + this->value = this->minValue; + }else if(this->value > this->maxValue){ + this->value = this->maxValue; + } +} + +bool WUPSConfigItemIntegerRange::isMovementAllowed() { + return true; +} + +std::string WUPSConfigItemIntegerRange::persistValue() { + return std::to_string(this->value); +} + +void WUPSConfigItemIntegerRange::loadValue(std::string persistedValue) { + // Note: std::stoi would throw an exception on error. atoi leads to an undefined behavior, but we + // check if the result is in range anyway. + int32_t newValue = atoi(persistedValue.c_str()); + + if(newValue < this->minValue){ + newValue = this->minValue; + }else if(newValue > this->maxValue){ + newValue = this->maxValue; + } + + this->value = newValue; +} + +void WUPSConfigItemIntegerRange::restoreDefault() { + this->value = this->defaultValue; +} + +bool WUPSConfigItemIntegerRange::callCallback(){ + if(callback != NULL) { + callback(this, this->value); + return true; + } + return false; +} diff --git a/wups_include/wups/config/WUPSConfigItemIntegerRange.h b/wups_include/wups/config/WUPSConfigItemIntegerRange.h new file mode 100644 index 0000000..08b9b6b --- /dev/null +++ b/wups_include/wups/config/WUPSConfigItemIntegerRange.h @@ -0,0 +1,61 @@ +/**************************************************************************** + * Copyright (C) 2018-2019 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 . + ****************************************************************************/ + +#ifndef _WUPS_CONFIG_ITEM_INTEGER_RANGE_H_ +#define _WUPS_CONFIG_ITEM_INTEGER_RANGE_H_ + +#include +#include +#include + +class WUPSConfigItemIntegerRange; + +typedef void (*IntegerRangeValueChangedCallback)(WUPSConfigItemIntegerRange *, int32_t); + +class WUPSConfigItemIntegerRange : public WUPSConfigItem { +public: + WUPSConfigItemIntegerRange(std::string configID, std::string displayName, int32_t defaultValue, int32_t minValue, int32_t maxValue, IntegerRangeValueChangedCallback callback); + + virtual ~WUPSConfigItemIntegerRange(); + + virtual std::string getCurrentValueDisplay(); + + virtual std::string getCurrentValueSelectedDisplay(); + + virtual void onSelected(bool isSelected); + + virtual void onButtonPressed(WUPSConfigButtons buttons); + + virtual bool isMovementAllowed(); + + virtual std::string persistValue(); + + virtual void loadValue(std::string persistedValue); + + virtual void restoreDefault(); + + virtual bool callCallback(); + +private: + IntegerRangeValueChangedCallback callback = NULL; + int32_t value; + int32_t defaultValue; + int32_t minValue; + int32_t maxValue; +}; + +#endif