2020-06-07 18:09:02 +00:00
|
|
|
//
|
|
|
|
// OptionListComponent.h
|
|
|
|
//
|
|
|
|
// Provides a list of options.
|
|
|
|
// Supports various types using templates.
|
|
|
|
//
|
|
|
|
|
2013-10-01 21:52:30 +00:00
|
|
|
#pragma once
|
2017-10-31 17:12:50 +00:00
|
|
|
#ifndef ES_CORE_COMPONENTS_OPTION_LIST_COMPONENT_H
|
|
|
|
#define ES_CORE_COMPONENTS_OPTION_LIST_COMPONENT_H
|
2013-10-01 21:52:30 +00:00
|
|
|
|
2020-06-21 17:35:43 +00:00
|
|
|
#define CHECKED_PATH ":/graphics/checkbox_checked.svg"
|
|
|
|
#define UNCHECKED_PATH ":/graphics/checkbox_unchecked.svg"
|
2020-06-07 18:09:02 +00:00
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "GuiComponent.h"
|
|
|
|
#include "Log.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "Window.h"
|
2013-10-01 21:52:30 +00:00
|
|
|
|
2020-06-07 18:09:02 +00:00
|
|
|
// Used to display a list of options.
|
|
|
|
// Can select one or multiple options.
|
2013-10-01 21:52:30 +00:00
|
|
|
|
2014-03-06 01:49:32 +00:00
|
|
|
// if !multiSelect
|
|
|
|
// * <- curEntry ->
|
2020-06-07 18:09:02 +00:00
|
|
|
// Always
|
|
|
|
// * press a -> open full list.
|
2014-05-31 19:20:14 +00:00
|
|
|
|
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
|
|
|
public:
|
2020-06-07 18:09:02 +00:00
|
|
|
OptionListComponent(
|
|
|
|
Window* window,
|
|
|
|
const HelpStyle& helpstyle,
|
|
|
|
const std::string& name,
|
|
|
|
bool multiSelect = false)
|
|
|
|
: GuiComponent(window),
|
|
|
|
mHelpStyle(helpstyle),
|
|
|
|
mMultiSelect(multiSelect),
|
|
|
|
mName(name),
|
|
|
|
mText(window),
|
|
|
|
mLeftArrow(window),
|
|
|
|
mRightArrow(window)
|
2013-10-03 20:58:09 +00:00
|
|
|
{
|
2014-03-17 00:52:15 +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);
|
2017-08-15 02:34:34 +00:00
|
|
|
mText.setHorizontalAlignment(ALIGN_CENTER);
|
2014-03-06 01:49:32 +00:00
|
|
|
addChild(&mText);
|
2013-10-03 20:58:09 +00:00
|
|
|
|
2018-06-12 14:42:30 +00:00
|
|
|
mLeftArrow.setResize(0, mText.getFont()->getLetterHeight());
|
|
|
|
mRightArrow.setResize(0, mText.getFont()->getLetterHeight());
|
|
|
|
|
2020-06-07 18:09:02 +00:00
|
|
|
if(mMultiSelect) {
|
2020-06-21 17:35:43 +00:00
|
|
|
mRightArrow.setImage(":/graphics/arrow.svg");
|
2014-03-06 19:45:03 +00:00
|
|
|
addChild(&mRightArrow);
|
2020-06-07 18:09:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-06-21 17:35:43 +00:00
|
|
|
mLeftArrow.setImage(":/graphics/option_arrow.svg");
|
2014-03-06 19:45:03 +00:00
|
|
|
mLeftArrow.setFlipX(true);
|
|
|
|
addChild(&mLeftArrow);
|
|
|
|
|
2020-06-21 17:35:43 +00:00
|
|
|
mRightArrow.setImage(":/graphics/option_arrow.svg");
|
2014-03-06 19:45:03 +00:00
|
|
|
addChild(&mRightArrow);
|
|
|
|
}
|
|
|
|
|
2014-03-22 18:04:14 +00:00
|
|
|
setSize(mLeftArrow.getSize().x() + mRightArrow.getSize().x(), font->getHeight());
|
2013-10-03 20:58:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 18:09:02 +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
|
|
|
{
|
2014-03-22 21:02:25 +00:00
|
|
|
mLeftArrow.setResize(0, mText.getFont()->getLetterHeight());
|
|
|
|
mRightArrow.setResize(0, mText.getFont()->getLetterHeight());
|
2014-03-06 19:45:03 +00:00
|
|
|
|
2020-06-25 17:52:38 +00:00
|
|
|
if(mSize.x() < (mLeftArrow.getSize().x() + mRightArrow.getSize().x())) {
|
2014-03-06 19:45:03 +00:00
|
|
|
LOG(LogWarning) << "OptionListComponent too narrow!";
|
2020-06-25 17:52:38 +00:00
|
|
|
}
|
2014-03-06 19:45:03 +00:00
|
|
|
|
2020-06-07 18:09:02 +00:00
|
|
|
mText.setSize(mSize.x() - mLeftArrow.getSize().x() -
|
|
|
|
mRightArrow.getSize().x(), mText.getFont()->getHeight());
|
2014-03-06 19:45:03 +00:00
|
|
|
|
2020-06-07 18:09:02 +00:00
|
|
|
// Position.
|
2014-03-06 19:45:03 +00:00
|
|
|
mLeftArrow.setPosition(0, (mSize.y() - mLeftArrow.getSize().y()) / 2);
|
2020-06-07 18:09:02 +00:00
|
|
|
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
|
|
|
{
|
2020-06-07 18:09:02 +00:00
|
|
|
if(input.value != 0) {
|
|
|
|
if(config->isMappedTo("a", input)) {
|
2014-03-06 01:49:32 +00:00
|
|
|
open();
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-07 18:09:02 +00:00
|
|
|
if(!mMultiSelect) {
|
|
|
|
if(config->isMappedLike("left", input)) {
|
|
|
|
// Move selection to previous.
|
2014-03-06 19:45:03 +00:00
|
|
|
unsigned int i = getSelectedId();
|
|
|
|
int next = (int)i - 1;
|
|
|
|
if(next < 0)
|
2017-11-17 14:58:52 +00:00
|
|
|
next += (int)mEntries.size();
|
2014-03-06 19:45:03 +00:00
|
|
|
|
|
|
|
mEntries.at(i).selected = false;
|
|
|
|
mEntries.at(next).selected = true;
|
|
|
|
onSelectedChanged();
|
|
|
|
return true;
|
|
|
|
|
2020-06-07 18:09:02 +00:00
|
|
|
}
|
|
|
|
else if(config->isMappedLike("right", input)) {
|
|
|
|
// Move selection to next.
|
2014-03-06 19:45:03 +00:00
|
|
|
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;
|
2020-06-07 18:09:02 +00:00
|
|
|
for(auto it = mEntries.cbegin(); it != mEntries.cend(); 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()
|
2013-10-12 19:03:32 +00:00
|
|
|
{
|
2014-03-06 01:49:32 +00:00
|
|
|
assert(mMultiSelect == false);
|
|
|
|
auto selected = getSelectedObjects();
|
|
|
|
assert(selected.size() == 1);
|
|
|
|
return selected.at(0);
|
2013-10-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2013-10-06 02:56:06 +00:00
|
|
|
|
2014-03-06 01:49:32 +00:00
|
|
|
mEntries.push_back(e);
|
|
|
|
onSelectedChanged();
|
2013-10-03 20:58:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 11:10:33 +00:00
|
|
|
bool selectEntry(unsigned int entry)
|
|
|
|
{
|
|
|
|
if (entry > mEntries.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mEntries.at(entry).selected = true;
|
|
|
|
onSelectedChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool unselectEntry(unsigned int entry)
|
|
|
|
{
|
|
|
|
if (entry > mEntries.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mEntries.at(entry).selected = false;
|
|
|
|
onSelectedChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-18 10:16:57 +00:00
|
|
|
void selectAll()
|
2017-03-18 17:54:39 +00:00
|
|
|
{
|
|
|
|
for(unsigned int i = 0; i < mEntries.size(); i++)
|
2017-05-18 10:16:57 +00:00
|
|
|
mEntries.at(i).selected = true;
|
2017-03-18 17:54:39 +00:00
|
|
|
onSelectedChanged();
|
|
|
|
}
|
|
|
|
|
2017-05-18 10:16:57 +00:00
|
|
|
void selectNone()
|
2017-03-18 17:54:39 +00:00
|
|
|
{
|
|
|
|
for(unsigned int i = 0; i < mEntries.size(); i++)
|
2017-05-18 10:16:57 +00:00
|
|
|
mEntries.at(i).selected = false;
|
2017-03-18 17:54:39 +00:00
|
|
|
onSelectedChanged();
|
|
|
|
}
|
|
|
|
|
2020-06-07 18:09:02 +00:00
|
|
|
HelpStyle getHelpStyle() override { return mHelpStyle; };
|
|
|
|
|
2014-03-06 01:49:32 +00:00
|
|
|
private:
|
2020-06-07 18:09:02 +00:00
|
|
|
struct OptionListData {
|
|
|
|
std::string name;
|
|
|
|
T object;
|
|
|
|
bool selected;
|
|
|
|
};
|
|
|
|
|
|
|
|
HelpStyle mHelpStyle;
|
|
|
|
|
2014-03-06 19:45:03 +00:00
|
|
|
unsigned int getSelectedId()
|
|
|
|
{
|
|
|
|
assert(mMultiSelect == false);
|
2020-06-07 18:09:02 +00:00
|
|
|
for(unsigned int i = 0; i < mEntries.size(); i++) {
|
2014-03-06 19:45:03 +00:00
|
|
|
if(mEntries.at(i).selected)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2020-06-07 18:09:02 +00:00
|
|
|
LOG(LogWarning) << "OptionListComponent::getSelectedId() - "
|
|
|
|
"no selected element found, defaulting to 0";
|
2014-03-06 19:45:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-06 01:49:32 +00:00
|
|
|
void open()
|
2013-10-03 20:58:09 +00:00
|
|
|
{
|
2020-06-07 18:09:02 +00:00
|
|
|
mWindow->pushGui(new OptionListPopup(mWindow, getHelpStyle(), this, mName));
|
2013-10-06 02:56:06 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 01:49:32 +00:00
|
|
|
void onSelectedChanged()
|
2013-10-03 20:58:09 +00:00
|
|
|
{
|
2020-06-07 18:09:02 +00:00
|
|
|
if(mMultiSelect) {
|
|
|
|
// Display # selected.
|
2014-03-06 01:49:32 +00:00
|
|
|
std::stringstream ss;
|
2014-03-15 22:06:16 +00:00
|
|
|
ss << getSelectedObjects().size() << " SELECTED";
|
2014-03-06 01:49:32 +00:00
|
|
|
mText.setText(ss.str());
|
2014-03-13 19:09:50 +00:00
|
|
|
mText.setSize(0, mText.getSize().y());
|
2014-03-19 00:55:37 +00:00
|
|
|
setSize(mText.getSize().x() + mRightArrow.getSize().x() + 24, mText.getSize().y());
|
2020-06-07 18:09:02 +00:00
|
|
|
if(mParent) // Hack since theres no "on child size changed" callback atm...
|
2014-03-13 19:09:50 +00:00
|
|
|
mParent->onSizeChanged();
|
2020-06-07 18:09:02 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Display currently selected + l/r cursors.
|
|
|
|
for(auto it = mEntries.cbegin(); it != mEntries.cend(); it++) {
|
|
|
|
if(it->selected) {
|
2018-01-27 17:04:28 +00:00
|
|
|
mText.setText(Utils::String::toUpper(it->name));
|
2014-03-13 19:09:50 +00:00
|
|
|
mText.setSize(0, mText.getSize().y());
|
2020-06-07 18:09:02 +00:00
|
|
|
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...
|
2014-03-13 19:09:50 +00:00
|
|
|
mParent->onSizeChanged();
|
2014-03-06 01:49:32 +00:00
|
|
|
break;
|
2013-10-26 20:57:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-06 01:49:32 +00:00
|
|
|
}
|
2013-10-01 21:52:30 +00:00
|
|
|
|
2014-03-13 19:09:50 +00:00
|
|
|
std::vector<HelpPrompt> getHelpPrompts() override
|
|
|
|
{
|
|
|
|
std::vector<HelpPrompt> prompts;
|
|
|
|
if(!mMultiSelect)
|
2020-06-09 18:03:31 +00:00
|
|
|
prompts.push_back(HelpPrompt("left/right", "change value"));
|
2017-05-18 10:16:57 +00:00
|
|
|
|
2014-05-16 21:21:33 +00:00
|
|
|
prompts.push_back(HelpPrompt("a", "select"));
|
2014-03-13 19:09:50 +00:00
|
|
|
return prompts;
|
|
|
|
}
|
|
|
|
|
2014-03-06 01:49:32 +00:00
|
|
|
bool mMultiSelect;
|
2013-10-12 19:03:32 +00:00
|
|
|
|
2014-03-13 19:09:50 +00:00
|
|
|
std::string mName;
|
2014-03-06 01:49:32 +00:00
|
|
|
TextComponent mText;
|
2014-03-06 19:45:03 +00:00
|
|
|
ImageComponent mLeftArrow;
|
|
|
|
ImageComponent mRightArrow;
|
2013-10-12 19:03:32 +00:00
|
|
|
|
2014-03-06 01:49:32 +00:00
|
|
|
std::vector<OptionListData> mEntries;
|
2020-06-07 18:09:02 +00:00
|
|
|
|
|
|
|
// Subclass to OptionListComponent.
|
|
|
|
class OptionListPopup : public GuiComponent
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OptionListPopup(
|
|
|
|
Window* window,
|
|
|
|
const HelpStyle& helpstyle,
|
|
|
|
OptionListComponent<T>* parent,
|
|
|
|
const std::string& title)
|
|
|
|
: GuiComponent(window),
|
|
|
|
mHelpStyle(helpstyle),
|
|
|
|
mMenu(window, title.c_str()),
|
|
|
|
mParent(parent)
|
|
|
|
{
|
|
|
|
auto font = Font::get(FONT_SIZE_MEDIUM);
|
|
|
|
ComponentListRow row;
|
|
|
|
|
|
|
|
// For select all/none.
|
|
|
|
std::vector<ImageComponent*> checkboxes;
|
|
|
|
|
|
|
|
for(auto it = mParent->mEntries.begin(); it != mParent->mEntries.end(); it++) {
|
|
|
|
row.elements.clear();
|
|
|
|
row.addElement(std::make_shared<TextComponent>
|
|
|
|
(mWindow, Utils::String::toUpper(it->name), font, 0x777777FF), true);
|
|
|
|
|
|
|
|
OptionListData& e = *it;
|
|
|
|
|
|
|
|
if(mParent->mMultiSelect) {
|
|
|
|
// Add checkbox.
|
|
|
|
auto checkbox = std::make_shared<ImageComponent>(mWindow);
|
|
|
|
checkbox->setImage(it->selected ? CHECKED_PATH : UNCHECKED_PATH);
|
|
|
|
checkbox->setResize(0, font->getLetterHeight());
|
|
|
|
row.addElement(checkbox, false);
|
|
|
|
|
|
|
|
// Input handler.
|
|
|
|
// Update checkbox state & selected value.
|
|
|
|
row.makeAcceptInputHandler([this, &e, checkbox] {
|
|
|
|
e.selected = !e.selected;
|
|
|
|
checkbox->setImage(e.selected ? CHECKED_PATH : UNCHECKED_PATH);
|
|
|
|
mParent->onSelectedChanged();
|
|
|
|
});
|
|
|
|
|
|
|
|
// For select all/none.
|
|
|
|
checkboxes.push_back(checkbox.get());
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2020-06-09 18:03:31 +00:00
|
|
|
mMenu.addButton("BACK", "back", [this] { delete this; });
|
2020-06-07 18:09:02 +00:00
|
|
|
|
|
|
|
if(mParent->mMultiSelect) {
|
|
|
|
mMenu.addButton("SELECT ALL", "select all", [this, checkboxes] {
|
|
|
|
for(unsigned int i = 0; i < mParent->mEntries.size(); i++) {
|
|
|
|
mParent->mEntries.at(i).selected = true;
|
|
|
|
checkboxes.at(i)->setImage(CHECKED_PATH);
|
|
|
|
}
|
|
|
|
mParent->onSelectedChanged();
|
|
|
|
});
|
|
|
|
|
|
|
|
mMenu.addButton("SELECT NONE", "select none", [this, checkboxes] {
|
|
|
|
for(unsigned int i = 0; i < mParent->mEntries.size(); i++) {
|
|
|
|
mParent->mEntries.at(i).selected = false;
|
|
|
|
checkboxes.at(i)->setImage(UNCHECKED_PATH);
|
|
|
|
}
|
|
|
|
mParent->onSelectedChanged();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
mMenu.setPosition((Renderer::getScreenWidth() - mMenu.getSize().x()) / 2,
|
|
|
|
Renderer::getScreenHeight() * 0.15f);
|
|
|
|
addChild(&mMenu);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool input(InputConfig* config, Input input) override
|
|
|
|
{
|
|
|
|
if(config->isMappedTo("b", input) && input.value != 0) {
|
|
|
|
delete this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GuiComponent::input(config, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<HelpPrompt> getHelpPrompts() override
|
|
|
|
{
|
|
|
|
auto prompts = mMenu.getHelpPrompts();
|
2020-06-09 18:03:31 +00:00
|
|
|
prompts.push_back(HelpPrompt("a", "select"));
|
2020-06-07 18:09:02 +00:00
|
|
|
prompts.push_back(HelpPrompt("b", "back"));
|
|
|
|
return prompts;
|
|
|
|
}
|
|
|
|
|
|
|
|
HelpStyle getHelpStyle() override { return mHelpStyle; };
|
|
|
|
|
|
|
|
private:
|
|
|
|
MenuComponent mMenu;
|
|
|
|
OptionListComponent<T>* mParent;
|
|
|
|
HelpStyle mHelpStyle;
|
|
|
|
};
|
2013-10-01 21:52:30 +00:00
|
|
|
};
|
2017-10-31 17:12:50 +00:00
|
|
|
|
|
|
|
#endif // ES_CORE_COMPONENTS_OPTION_LIST_COMPONENT_H
|