From 4ec11baf9b761dbb0c859bb7f3918a62b8dde9c7 Mon Sep 17 00:00:00 2001 From: Mateusz Piesta Date: Thu, 22 Jul 2021 14:15:33 +0200 Subject: [PATCH] [BH-675] Add TextSpinner Added TextSpinner widget --- module-gui/gui/widgets/CMakeLists.txt | 2 + module-gui/gui/widgets/TextSpinner.cpp | 106 +++++++++++++++++++++++++ module-gui/gui/widgets/TextSpinner.hpp | 47 +++++++++++ 3 files changed, 155 insertions(+) create mode 100644 module-gui/gui/widgets/TextSpinner.cpp create mode 100644 module-gui/gui/widgets/TextSpinner.hpp diff --git a/module-gui/gui/widgets/CMakeLists.txt b/module-gui/gui/widgets/CMakeLists.txt index c590d60f86f1924240597bc6fbf6c3063bb8d837..1fe8fd4217623122b167e6c05cd9ef4466f0cda7 100644 --- a/module-gui/gui/widgets/CMakeLists.txt +++ b/module-gui/gui/widgets/CMakeLists.txt @@ -29,6 +29,7 @@ target_sources( ${PROJECT_NAME} "${CMAKE_CURRENT_LIST_DIR}/SideListItem.cpp" "${CMAKE_CURRENT_LIST_DIR}/SideListView.cpp" "${CMAKE_CURRENT_LIST_DIR}/Spinner.cpp" + "${CMAKE_CURRENT_LIST_DIR}/TextSpinner.cpp" "${CMAKE_CURRENT_LIST_DIR}/StatusBar.cpp" "${CMAKE_CURRENT_LIST_DIR}/status-bar/SIM.cpp" "${CMAKE_CURRENT_LIST_DIR}/status-bar/BatteryBase.cpp" @@ -84,6 +85,7 @@ target_sources( ${PROJECT_NAME} "${CMAKE_CURRENT_LIST_DIR}/SideListItem.hpp" "${CMAKE_CURRENT_LIST_DIR}/SideListView.hpp" "${CMAKE_CURRENT_LIST_DIR}/Spinner.hpp" + "${CMAKE_CURRENT_LIST_DIR}/TextSpinner.hpp" "${CMAKE_CURRENT_LIST_DIR}/StatusBar.hpp" "${CMAKE_CURRENT_LIST_DIR}/Text.hpp" "${CMAKE_CURRENT_LIST_DIR}/header/Header.hpp" diff --git a/module-gui/gui/widgets/TextSpinner.cpp b/module-gui/gui/widgets/TextSpinner.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8e1d2406f40ebac2d3a6d3fa0ae9f1b17050b7c --- /dev/null +++ b/module-gui/gui/widgets/TextSpinner.cpp @@ -0,0 +1,106 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "TextSpinner.hpp" + +namespace gui +{ + TextSpinner::TextSpinner(const TextSpinner::TextRange &range, Boundaries boundaries) + : textRange{range}, boundaries(boundaries) + { + setEditMode(EditMode::Browse); + update(); + } + void TextSpinner::setTextRange(const TextSpinner::TextRange &range) + { + textRange = range; + update(); + } + void TextSpinner::setCurrentPosition(Position pos) + { + if ((pos >= getRangeDownLimit()) && (pos <= getRangeUpLimit())) { + currentPosition = pos; + } + update(); + } + + std::string TextSpinner::getCurrentText() const noexcept + { + return textRange[currentPosition]; + } + + void TextSpinner::stepUp() + { + if (currentPosition >= getRangeUpLimit()) { + if (boundaries == Boundaries::Continuous) { + currentPosition = getRangeDownLimit(); + } + else { + currentPosition = getRangeUpLimit(); + } + } + else { + currentPosition++; + } + update(); + } + void TextSpinner::stepDown() + { + if (currentPosition <= getRangeDownLimit()) { + if (boundaries == Boundaries::Continuous) { + currentPosition = getRangeUpLimit(); + } + else { + currentPosition = getRangeDownLimit(); + } + } + else { + currentPosition--; + } + update(); + } + void TextSpinner::update() + { + setText(textRange[currentPosition]); + } + bool TextSpinner::onInput(const InputEvent &inputEvent) + { + if (inputEvent.isShortRelease()) { + switch (inputEvent.getKeyCode()) { + case KeyCode::KEY_UP: + stepUp(); + return true; + case KeyCode::KEY_DOWN: + stepDown(); + return true; + default: + break; + } + } + return false; + } + bool TextSpinner::onFocus(bool state) + { + if (focus) { + setEdges(RectangleEdge::Top | RectangleEdge::Bottom); + } + else { + setEdges(RectangleEdge::None); + } + showCursor(state); + return true; + } + TextSpinner::Position TextSpinner::getRangeUpLimit() const + { + return static_cast(textRange.size() - 1); + } + TextSpinner::Position TextSpinner::getRangeDownLimit() const + { + return 0; + } + TextSpinner::Range TextSpinner::getValidRange() const noexcept + { + return Range{getRangeDownLimit(), getRangeUpLimit()}; + } + +} // namespace gui diff --git a/module-gui/gui/widgets/TextSpinner.hpp b/module-gui/gui/widgets/TextSpinner.hpp new file mode 100644 index 0000000000000000000000000000000000000000..a6fab2b7aa2ab9bf699dbace6ac8688646c8713d --- /dev/null +++ b/module-gui/gui/widgets/TextSpinner.hpp @@ -0,0 +1,47 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#pragma once + +#include "Text.hpp" + +#include +#include + +namespace gui +{ + class TextSpinner : public Text + { + public: + using TextRange = std::vector; + using Position = std::uint32_t; + using Range = std::pair; + + TextSpinner() = delete; + TextSpinner(const TextRange &range, Boundaries boundaries); + + /// Sets range of strings to spin over + void setTextRange(const TextRange &range); + /// Sets current position. Must be within valid @ref Range that can be checked by @ref getValidRange + void setCurrentPosition(Position pos); + + [[nodiscard]] std::string getCurrentText() const noexcept; + [[nodiscard]] Range getValidRange() const noexcept; + + void stepUp(); + void stepDown(); + + // virtual methods from Item + bool onInput(const InputEvent &inputEvent) override; + bool onFocus(bool state) override; + + private: + TextRange textRange; + Position currentPosition = 0; + Boundaries boundaries = Boundaries::Continuous; + + void update(); + Position getRangeUpLimit() const; + Position getRangeDownLimit() const; + }; +} // namespace gui