ES-DE/src/components/OptionListComponent.h

301 lines
7.5 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 "ImageComponent.h"
2014-03-06 01:49:32 +00:00
#include "MenuComponent.h"
#include <sstream>
#include "../Log.h"
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, const std::string& title) : GuiComponent(window),
mMenu(window, title.c_str()), mParent(parent)
{
2014-03-06 01:49:32 +00:00
auto font = Font::get(FONT_SIZE_MEDIUM);
ComponentListRow row;
2014-03-06 01:49:32 +00:00
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, strToUpper(it->name), font, 0x777777FF), true);
2014-03-06 01:49:32 +00:00
OptionListData& e = *it;
if(mParent->mMultiSelect)
{
// add checkbox
auto checkbox = std::make_shared<ImageComponent>(mWindow);
checkbox->setImage(it->selected ? ":/checkbox_checked.png" : ":/checkbox_unchecked.png");
if(checkbox->getTextureSize().y() > (int)FONT_SIZE_MEDIUM) // downscale if necessary to match text
checkbox->setResize(0, (float)FONT_SIZE_MEDIUM);
row.addElement(checkbox, false);
// input handler
// update checkbox state & selected value
row.makeAcceptInputHandler([this, &e, checkbox]
{
e.selected = !e.selected;
checkbox->setImage(e.selected ? ":/checkbox_checked.png" : ":/checkbox_unchecked.png");
mParent->onSelectedChanged();
});
}else{
// input handler for non-multiselect
// update selected value and close
row.makeAcceptInputHandler([this, &e]
{
mParent->mEntries.at(mParent->getSelectedId()).selected = false;
e.selected = true;
mParent->onSelectedChanged();
delete this;
});
}
// also set cursor to this row if we're not multi-select and this row is selected
mMenu.addRow(row, (!mParent->mMultiSelect && it->selected));
}
2013-10-01 21:52:30 +00:00
if(mParent->mMultiSelect)
mMenu.addButton("BACK", "accept", [this] { delete this; });
mMenu.setPosition((Renderer::getScreenWidth() - mMenu.getSize().x()) / 2, Renderer::getScreenHeight() * 0.15f);
2014-03-06 01:49:32 +00:00
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);
}
std::vector<HelpPrompt> getHelpPrompts() override
{
return mMenu.getHelpPrompts();
}
2014-03-06 01:49:32 +00:00
};
2014-03-06 01:49:32 +00:00
public:
OptionListComponent(Window* window, const std::string& name, bool multiSelect = false) : GuiComponent(window), mMultiSelect(multiSelect), mName(name),
mText(window), mLeftArrow(window), mRightArrow(window)
2013-10-03 20:58:09 +00:00
{
auto font = Font::get(FONT_SIZE_MEDIUM, FONT_PATH_LIGHT);
2014-03-06 01:49:32 +00:00
mText.setFont(font);
mText.setColor(0x777777FF);
mText.setCentered(true);
addChild(&mText);
2013-10-03 20:58:09 +00:00
if(mMultiSelect)
{
mRightArrow.setImage(":/sq_bracket.png");
addChild(&mRightArrow);
}else{
mLeftArrow.setImage(":/arrow.png");
mLeftArrow.setFlipX(true);
addChild(&mLeftArrow);
mRightArrow.setImage(":/arrow.png");
addChild(&mRightArrow);
}
setSize(mLeftArrow.getSize().x() + mRightArrow.getSize().x(), (float)font->getHeight());
2013-10-03 20:58:09 +00:00
}
// handles positioning/resizing of text and arrows
2014-03-06 01:49:32 +00:00
void onSizeChanged() override
2013-10-03 20:58:09 +00:00
{
// size
if(mLeftArrow.getTextureSize().y() > mSize.y())
mLeftArrow.setResize(0, mSize.y());
else
mLeftArrow.setResize(0, 0);
if(mRightArrow.getTextureSize().y() > mSize.y())
mRightArrow.setResize(0, mSize.y());
else
mRightArrow.setResize(0, 0);
if(mSize.x() < (mLeftArrow.getSize().x() + mRightArrow.getSize().x()))
LOG(LogWarning) << "OptionListComponent too narrow!";
mText.setSize(mSize.x() - mLeftArrow.getSize().x() - mRightArrow.getSize().x(), (float)mText.getFont()->getHeight());
// position
mLeftArrow.setPosition(0, (mSize.y() - mLeftArrow.getSize().y()) / 2);
mText.setPosition(mLeftArrow.getPosition().x() + mLeftArrow.getSize().x(), (mSize.y() - mText.getSize().y()) / 2);
mRightArrow.setPosition(mText.getPosition().x() + mText.getSize().x(), (mSize.y() - mRightArrow.getSize().y()) / 2);
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
unsigned int i = getSelectedId();
int next = (int)i - 1;
if(next < 0)
next += mEntries.size();
mEntries.at(i).selected = false;
mEntries.at(next).selected = true;
onSelectedChanged();
return true;
2014-03-06 01:49:32 +00:00
}else if(config->isMappedTo("right", input))
{
// move selection to next
unsigned int i = getSelectedId();
int next = (i + 1) % mEntries.size();
mEntries.at(i).selected = false;
mEntries.at(next).selected = true;
onSelectedChanged();
return true;
2014-03-06 01:49:32 +00:00
}
}
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:
unsigned int getSelectedId()
{
assert(mMultiSelect == false);
for(unsigned int i = 0; i < mEntries.size(); i++)
{
if(mEntries.at(i).selected)
return i;
}
LOG(LogWarning) << "OptionListComponent::getSelectedId() - no selected element found, defaulting to 0";
return 0;
}
2014-03-06 01:49:32 +00:00
void open()
2013-10-03 20:58:09 +00:00
{
mWindow->pushGui(new OptionListPopup(mWindow, this, mName));
}
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";
2014-03-06 01:49:32 +00:00
mText.setText(ss.str());
mText.setSize(0, mText.getSize().y());
setSize(mText.getSize().x() + mRightArrow.getSize().x() + 24, mText.getSize().y());
if(mParent) // hack since theres no "on child size changed" callback atm...
mParent->onSizeChanged();
2014-03-06 01:49:32 +00:00
}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)
{
mText.setText(strToUpper(it->name));
mText.setSize(0, mText.getSize().y());
setSize(mText.getSize().x() + mLeftArrow.getSize().x() + mRightArrow.getSize().x() + 24, mText.getSize().y());
if(mParent) // hack since theres no "on child size changed" callback atm...
mParent->onSizeChanged();
2014-03-06 01:49:32 +00:00
break;
}
}
}
2014-03-06 01:49:32 +00:00
}
2013-10-01 21:52:30 +00:00
std::vector<HelpPrompt> getHelpPrompts() override
{
std::vector<HelpPrompt> prompts;
if(!mMultiSelect)
prompts.push_back(HelpPrompt("left/right", "change"));
prompts.push_back(HelpPrompt("a", "change"));
return prompts;
}
2014-03-06 01:49:32 +00:00
bool mMultiSelect;
std::string mName;
2014-03-06 01:49:32 +00:00
TextComponent mText;
ImageComponent mLeftArrow;
ImageComponent mRightArrow;
2014-03-06 01:49:32 +00:00
std::vector<OptionListData> mEntries;
2013-10-01 21:52:30 +00:00
};