Started on OptionListComponent.

This commit is contained in:
Aloshi 2013-10-01 16:52:30 -05:00
parent e7135d869c
commit 6956211ff0
3 changed files with 95 additions and 0 deletions

View file

@ -164,6 +164,7 @@ set(ES_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/src/components/DateTimeComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/DateTimeComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/NinePatchComponent.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/RatingComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ScrollableContainer.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ScrollableContainer.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/SliderComponent.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/DateTimeComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/NinePatchComponent.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/RatingComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ScrollableContainer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ScrollableContainer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/SliderComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/SliderComponent.cpp

View file

@ -0,0 +1,55 @@
#include "OptionListComponent.h"
OptionListComponent::OptionListComponent(Window* window) : GuiComponent(window),
mClosedCallback(nullptr)
{
}
void OptionListComponent::setClosedCallback(std::function<void(std::vector<const ListEntry*>)> 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 <typename T>
void OptionListComponent::populate(const std::vector<T>& vec, std::function<ListEntry(const T&)> selector)
{
for(auto it = vec.begin(); it != vec.end(); it++)
{
ListEntry e = selector(*it);
if(!e.name.empty())
mEntries.push_back(e);
}
}
std::vector<const ListEntry*> OptionListComponent::getSelected()
{
std::vector<const ListEntry*> 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;
}

View file

@ -0,0 +1,38 @@
#pragma once
#include "../GuiComponent.h"
#include <vector>
#include <functional>
//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<void(std::vector<const ListEntry*>)> callback);
template<typename T>
void populate(const std::vector<T>& vec, std::function<ListEntry(const T&)> selector);
std::vector<const ListEntry*> getSelected();
private:
void close();
std::function<void(std::vector<const ListEntry*>)> mClosedCallback;
std::vector<ListEntry> mEntries;
};