diff --git a/CMakeLists.txt b/CMakeLists.txt index 749529d0e..706ba1604 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -164,6 +164,7 @@ set(ES_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/src/components/DateTimeComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/NinePatchComponent.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/components/OptionListComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/RatingComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ScrollableContainer.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/SliderComponent.h @@ -220,6 +221,7 @@ set(ES_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/components/DateTimeComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/NinePatchComponent.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/components/OptionListComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/RatingComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ScrollableContainer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/SliderComponent.cpp diff --git a/src/components/OptionListComponent.cpp b/src/components/OptionListComponent.cpp new file mode 100644 index 000000000..76a071da2 --- /dev/null +++ b/src/components/OptionListComponent.cpp @@ -0,0 +1,55 @@ +#include "OptionListComponent.h" + +OptionListComponent::OptionListComponent(Window* window) : GuiComponent(window), + mClosedCallback(nullptr) +{ +} + +void OptionListComponent::setClosedCallback(std::function)> callback) +{ + mClosedCallback = callback; +} + +bool OptionListComponent::input(InputConfig* config, Input input) +{ + return GuiComponent::input(config, input); +} + +void OptionListComponent::render(const Eigen::Affine3f& parentTrans) +{ + Eigen::Affine3f trans = parentTrans * getTransform(); + + renderChildren(trans); +} + +template +void OptionListComponent::populate(const std::vector& vec, std::function selector) +{ + for(auto it = vec.begin(); it != vec.end(); it++) + { + ListEntry e = selector(*it); + if(!e.name.empty()) + mEntries.push_back(e); + } +} + +std::vector OptionListComponent::getSelected() +{ + std::vector ret; + for(auto it = mEntries.begin(); it != mEntries.end(); it++) + { + if((*it).selected) + ret.push_back(&(*it)); + } + + return ret; +} + +void OptionListComponent::close() +{ + if(mClosedCallback) + mClosedCallback(getSelected()); + + delete this; +} + diff --git a/src/components/OptionListComponent.h b/src/components/OptionListComponent.h new file mode 100644 index 000000000..68cb614d9 --- /dev/null +++ b/src/components/OptionListComponent.h @@ -0,0 +1,38 @@ +#pragma once + +#include "../GuiComponent.h" +#include +#include + +//Used to display a list of options. +//Can select one or multiple options. + +struct ListEntry +{ + std::string name; + unsigned int color; + bool selected; +}; + +class OptionListComponent : public GuiComponent +{ +public: + OptionListComponent(Window* window); + + bool input(InputConfig* config, Input input); + void render(const Eigen::Affine3f& trans); + + void setClosedCallback(std::function)> callback); + + template + void populate(const std::vector& vec, std::function selector); + + std::vector getSelected(); +private: + void close(); + + std::function)> mClosedCallback; + + std::vector mEntries; +}; +