From 7a04c99d86396cb64ea749b4abb2f861171b9985 Mon Sep 17 00:00:00 2001 From: Maschell Date: Tue, 17 Jul 2018 15:27:19 +0200 Subject: [PATCH] Add a config item for boolean values. --- src/config/WUPSConfigItemBoolean.cpp | 100 ++++++++++++++++++ .../wups/config/WUPSConfigItemBoolean.h | 75 +++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 src/config/WUPSConfigItemBoolean.cpp create mode 100644 wups_include/wups/config/WUPSConfigItemBoolean.h diff --git a/src/config/WUPSConfigItemBoolean.cpp b/src/config/WUPSConfigItemBoolean.cpp new file mode 100644 index 0000000..fff3b24 --- /dev/null +++ b/src/config/WUPSConfigItemBoolean.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** + * 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 . + ****************************************************************************/ + +#include +#include + +WUPSConfigItemBoolean::WUPSConfigItemBoolean(std::string configID, std::string displayName, bool defaultValue, BooleanValueChangedCallback callback) : WUPSConfigItem(configID,displayName) { + this->defaultValue = defaultValue; + this->value = defaultValue; + this->callback = callback; +} + +WUPSConfigItemBoolean::~WUPSConfigItemBoolean() { + +} + +void WUPSConfigItemBoolean::setTrueValueName(std::string trueValName) { + this->trueValName = trueValName; +} + +void WUPSConfigItemBoolean::setFalseValueName(std::string falseValName) { + this->falseValName = falseValName; +} + +std::string WUPSConfigItemBoolean::getCurrentValueDisplay() { + if(value) { + return " " + trueValName; + } else { + return " " + falseValName; + } +} + +std::string WUPSConfigItemBoolean::getCurrentValueSelectedDisplay() { + if(value) { + return " " + trueValName + " >"; + } else { + return "< " + falseValName; + } +} + +void WUPSConfigItemBoolean::onSelected(bool isSelected) { + +} + +void WUPSConfigItemBoolean::onButtonPressed(WUPSConfigButtons buttons) { + if(buttons & WUPS_CONFIG_BUTTON_A) { + toggleValue(); + }else if(buttons & WUPS_CONFIG_BUTTON_LEFT && !value){ + toggleValue(); + }else if((buttons & WUPS_CONFIG_BUTTON_RIGHT) && value){ + toggleValue(); + } +} + +bool WUPSConfigItemBoolean::isMovementAllowed() { + return true; +} + +std::string WUPSConfigItemBoolean::persistValue() { + if(value) { + return "1"; + } + return "0"; +} + +void WUPSConfigItemBoolean::loadValue(std::string persistedValue) { + bool newValue = false; + if(persistedValue.compare("1") == 0) { + newValue = true; + } + + if(newValue != value) { + toggleValue(); + } +} + +void WUPSConfigItemBoolean::toggleValue() { + value = !value; + if(callback != NULL) { + callback(value); + } +} + +void WUPSConfigItemBoolean::restoreDefault() { + this->value = this->defaultValue; +} diff --git a/wups_include/wups/config/WUPSConfigItemBoolean.h b/wups_include/wups/config/WUPSConfigItemBoolean.h new file mode 100644 index 0000000..0ac4eba --- /dev/null +++ b/wups_include/wups/config/WUPSConfigItemBoolean.h @@ -0,0 +1,75 @@ +/**************************************************************************** + * 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 . + ****************************************************************************/ + +#ifndef _WUPS_CONFIG_ITEM_BOOLEAN_H_ +#define _WUPS_CONFIG_ITEM_BOOLEAN_H_ + +#include +#include +#include + +typedef void (*BooleanValueChangedCallback)(bool); + +class WUPSConfigItemBoolean : public WUPSConfigItem { +public: + WUPSConfigItemBoolean(std::string configID, std::string displayName, bool defaultValue, BooleanValueChangedCallback callback); + + virtual ~WUPSConfigItemBoolean(); + + /** + Sets the name with will be displayed as "true" value + \param name of the "true" value + **/ + void setTrueValueName(std::string trueValName); + + /** + Sets the name with will be displayed as "false" value + \param name of the "false" value + **/ + void setFalseValueName(std::string falseValName); + + /** + Toggles the value. When it was true, it now false, when it was false, it's now true. + Call the callback with the new value. + **/ + virtual void toggleValue(); + + 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(); + +private: + BooleanValueChangedCallback callback = NULL; + bool value; + bool defaultValue; + std::string trueValName = "on"; + std::string falseValName = "off"; +}; + +#endif