ES-DE/src/components/OptionListComponent.h

178 lines
3.4 KiB
C
Raw Normal View History

2013-10-01 21:52:30 +00:00
#pragma once
#include "../GuiComponent.h"
#include "../resources/Font.h"
2013-10-03 20:58:09 +00:00
#include "../Renderer.h"
2013-10-18 19:53:14 +00:00
#include "../Window.h"
2014-03-06 01:49:32 +00:00
#include "TextComponent.h"
#include "MenuComponent.h"
#include <sstream>
2013-10-01 21:52:30 +00:00
//Used to display a list of options.
//Can select one or multiple options.
2014-03-06 01:49:32 +00:00
// if !multiSelect
// * <- curEntry ->
// always
// * press a -> open full list
2013-10-03 20:58:09 +00:00
template<typename T>
2013-10-01 21:52:30 +00:00
class OptionListComponent : public GuiComponent
{
2014-03-06 01:49:32 +00:00
private:
struct OptionListData
2013-10-03 20:58:09 +00:00
{
2014-03-06 01:49:32 +00:00
std::string name;
2013-10-03 20:58:09 +00:00
T object;
2014-03-06 01:49:32 +00:00
bool selected;
2013-10-03 20:58:09 +00:00
};
2014-03-06 01:49:32 +00:00
class OptionListPopup : public GuiComponent
2013-10-03 20:58:09 +00:00
{
2014-03-06 01:49:32 +00:00
private:
MenuComponent mMenu;
OptionListComponent<T>* mParent;
2014-03-06 01:49:32 +00:00
public:
OptionListPopup(Window* window, OptionListComponent<T>* parent) : GuiComponent(window),
mMenu(window, "optionlist"), mParent(parent)
{
2014-03-06 01:49:32 +00:00
auto font = Font::get(FONT_SIZE_MEDIUM);
ComponentListRow row;
for(auto it = mParent->mEntries.begin(); it != mParent->mEntries.end(); it++)
{
2014-03-06 01:49:32 +00:00
row.elements.clear();
row.addElement(std::make_shared<TextComponent>(mWindow, it->name, font, 0x777777FF), true);
mMenu.addRow(row);
}
2013-10-01 21:52:30 +00:00
2014-03-06 01:49:32 +00:00
mMenu.setPosition((Renderer::getScreenWidth() - mMenu.getSize().x()) / 2, (Renderer::getScreenHeight() - mMenu.getSize().y()) / 2);
addChild(&mMenu);
}
2013-10-01 21:52:30 +00:00
2014-03-06 01:49:32 +00:00
bool input(InputConfig* config, Input input) override
{
if(config->isMappedTo("b", input) && input.value != 0)
{
2014-03-06 01:49:32 +00:00
delete this;
return true;
}
2014-03-06 01:49:32 +00:00
return GuiComponent::input(config, input);
}
2014-03-06 01:49:32 +00:00
virtual ~OptionListPopup()
{
// commit changes
}
};
2014-03-06 01:49:32 +00:00
public:
OptionListComponent(Window* window, bool multiSelect = false) : GuiComponent(window), mText(window), mMultiSelect(multiSelect)
2013-10-03 20:58:09 +00:00
{
2014-03-06 01:49:32 +00:00
auto font = Font::get(FONT_SIZE_MEDIUM);
mText.setFont(font);
mText.setColor(0x777777FF);
mText.setCentered(true);
addChild(&mText);
2013-10-03 20:58:09 +00:00
2014-03-06 01:49:32 +00:00
setSize(Renderer::getScreenWidth() * 0.2f, (float)font->getHeight());
2013-10-03 20:58:09 +00:00
}
2014-03-06 01:49:32 +00:00
void onSizeChanged() override
2013-10-03 20:58:09 +00:00
{
2014-03-06 01:49:32 +00:00
mText.setSize(mSize);
2013-10-03 20:58:09 +00:00
}
2014-03-06 01:49:32 +00:00
bool input(InputConfig* config, Input input) override
2013-10-03 20:58:09 +00:00
{
2014-03-06 01:49:32 +00:00
if(input.value != 0)
2013-10-03 20:58:09 +00:00
{
2014-03-06 01:49:32 +00:00
if(config->isMappedTo("a", input))
{
open();
return true;
}
if(!mMultiSelect)
{
if(config->isMappedTo("left", input))
{
// move selection to previous
}else if(config->isMappedTo("right", input))
{
// move selection to next
}
}
2013-10-03 20:58:09 +00:00
}
2014-03-06 01:49:32 +00:00
return GuiComponent::input(config, input);
2013-10-03 20:58:09 +00:00
}
2013-10-11 00:55:57 +00:00
std::vector<T> getSelectedObjects()
{
std::vector<T> ret;
for(auto it = mEntries.begin(); it != mEntries.end(); it++)
{
2014-03-06 01:49:32 +00:00
if(it->selected)
2013-10-11 00:55:57 +00:00
ret.push_back(it->object);
}
return ret;
}
2014-03-06 01:49:32 +00:00
T getSelected()
{
2014-03-06 01:49:32 +00:00
assert(mMultiSelect == false);
auto selected = getSelectedObjects();
assert(selected.size() == 1);
return selected.at(0);
}
2014-03-06 01:49:32 +00:00
void add(const std::string& name, const T& obj, bool selected)
2013-10-03 20:58:09 +00:00
{
2014-03-06 01:49:32 +00:00
OptionListData e;
e.name = name;
e.object = obj;
e.selected = selected;
2014-03-06 01:49:32 +00:00
mEntries.push_back(e);
onSelectedChanged();
2013-10-03 20:58:09 +00:00
}
2014-03-06 01:49:32 +00:00
private:
void open()
2013-10-03 20:58:09 +00:00
{
2014-03-06 01:49:32 +00:00
mWindow->pushGui(new OptionListPopup(mWindow, this));
}
2014-03-06 01:49:32 +00:00
void onSelectedChanged()
2013-10-03 20:58:09 +00:00
{
2014-03-06 01:49:32 +00:00
if(mMultiSelect)
{
2014-03-06 01:49:32 +00:00
// display # selected
std::stringstream ss;
ss << getSelectedObjects().size() << " selected";
mText.setText(ss.str());
}else{
// display currently selected + l/r cursors
for(auto it = mEntries.begin(); it != mEntries.end(); it++)
{
2014-03-06 01:49:32 +00:00
if(it->selected)
{
2014-03-06 01:49:32 +00:00
mText.setText(it->name);
break;
}
}
}
2014-03-06 01:49:32 +00:00
}
2013-10-01 21:52:30 +00:00
2014-03-06 01:49:32 +00:00
bool mMultiSelect;
2014-03-06 01:49:32 +00:00
TextComponent mText;
2014-03-06 01:49:32 +00:00
std::vector<OptionListData> mEntries;
2013-10-01 21:52:30 +00:00
};
2014-03-06 01:49:32 +00:00