From 4b835cfc6baccd764856c91d151697035433f043 Mon Sep 17 00:00:00 2001 From: Maschell Date: Tue, 17 Jul 2018 15:27:51 +0200 Subject: [PATCH] Add a config item type for multiple choice configs (e.g. select a resolution) --- src/config/WUPSConfigItemMultipleValues.cpp | 143 ++++++++++++++++++ .../config/WUPSConfigItemMultipleValues.h | 56 +++++++ 2 files changed, 199 insertions(+) create mode 100644 src/config/WUPSConfigItemMultipleValues.cpp create mode 100644 wups_include/wups/config/WUPSConfigItemMultipleValues.h diff --git a/src/config/WUPSConfigItemMultipleValues.cpp b/src/config/WUPSConfigItemMultipleValues.cpp new file mode 100644 index 0000000..0d8eaa2 --- /dev/null +++ b/src/config/WUPSConfigItemMultipleValues.cpp @@ -0,0 +1,143 @@ +/**************************************************************************** + * 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 +#include + +WUPSConfigItemMultipleValues::WUPSConfigItemMultipleValues(std::string configID, std::string displayName, int32_t defaultValue, std::map values_, MultipleValuesChangedCallback callback) : WUPSConfigItem(configID,displayName) { + if(values_.size() == 0) { + this->values[defaultValue] = ""; + } else { + this->values = values_; + } + + if (values.count(defaultValue) > 0) { + this->defaultValue = defaultValue; + valueIndex = 0; + for (auto& kv : values) { + if(defaultValue == kv.first) { + break; + } + valueIndex++; + } + } else { + for (auto& kv : values) { + this->defaultValue = kv.first; + break; + } + valueIndex = 0; + } + + + this->callback = callback; +} + +WUPSConfigItemMultipleValues::~WUPSConfigItemMultipleValues() { + +} + +std::string WUPSConfigItemMultipleValues::getCurrentValueDisplay() { + int32_t index = 0; + for (auto& kv : values) { + if(index == valueIndex) { + return " " + kv.second; + } + index++; + } + return ""; +} + +std::string WUPSConfigItemMultipleValues::getCurrentValueSelectedDisplay() { + int32_t index_max = values.size()-1; + int32_t index = 0; + for (auto& kv : values) { + if(index == valueIndex) { + std::string s; + if(valueIndex != 0) { + s.append("< "); + } else { + s.append(" "); + } + s.append(kv.second); + if(valueIndex != index_max) { + s.append(" >"); + } + return s; + } + index++; + } + return ""; +} + + +void WUPSConfigItemMultipleValues::onSelected(bool isSelected) { + // not needed. +} + +void WUPSConfigItemMultipleValues::onButtonPressed(WUPSConfigButtons buttons) { + int32_t previousValue = valueIndex; + if(buttons & WUPS_CONFIG_BUTTON_LEFT) { + valueIndex--; + if(valueIndex < 0) { + valueIndex = 0; + } + } + if(buttons & WUPS_CONFIG_BUTTON_RIGHT) { + valueIndex++; + if(valueIndex > values.size()-1) { + valueIndex = values.size()-1; + } + } + if(previousValue != valueIndex) { + int32_t index = 0; + for (auto& kv : values) { + if(index == valueIndex) { + callback(kv.first); + break; + } + index++; + } + } +} + +bool WUPSConfigItemMultipleValues::isMovementAllowed() { + return true; +} + +std::string WUPSConfigItemMultipleValues::persistValue() { + return std::to_string(this->valueIndex); +} + +void WUPSConfigItemMultipleValues::loadValue(std::string persistedValue) { + int32_t newValueIndex = std::stoi(persistedValue); + if(newValueIndex != valueIndex) { + int32_t index = 0; + for (auto& kv : values) { + if(index == newValueIndex) { + valueIndex = newValueIndex; + callback(kv.first); + break; + } + index++; + } + } +} + +void WUPSConfigItemMultipleValues::restoreDefault() { + this->valueIndex = 0; +} diff --git a/wups_include/wups/config/WUPSConfigItemMultipleValues.h b/wups_include/wups/config/WUPSConfigItemMultipleValues.h new file mode 100644 index 0000000..40eac2f --- /dev/null +++ b/wups_include/wups/config/WUPSConfigItemMultipleValues.h @@ -0,0 +1,56 @@ +/**************************************************************************** + * 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_MULTIPLE_VALUES_H_ +#define _WUPS_CONFIG_ITEM_MULTIPLE_VALUES_H_ + +#include +#include +#include +#include + +typedef void (*MultipleValuesChangedCallback)(int32_t); + +class WUPSConfigItemMultipleValues : public WUPSConfigItem { +public: + WUPSConfigItemMultipleValues(std::string configID, std::string displayName, int32_t defaultValue, std::map values_, MultipleValuesChangedCallback callback); + + virtual ~WUPSConfigItemMultipleValues(); + + 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: + MultipleValuesChangedCallback callback = NULL; + int32_t defaultValue; + int32_t valueIndex = 0; + std::map values; +}; + +#endif