2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-07 18:09:02 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-28 16:39:18 +00:00
|
|
|
// OptionListComponent.h
|
2020-06-07 18:09:02 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// Provides a list of option components.
|
2020-06-28 16:39:18 +00:00
|
|
|
// Supports various types using templates.
|
2020-06-07 18:09:02 +00:00
|
|
|
//
|
|
|
|
|
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
|
|
|
|
2021-10-08 17:13:37 +00:00
|
|
|
#define OPTIONLIST_REPEAT_START_DELAY 650
|
|
|
|
#define OPTIONLIST_REPEAT_SPEED 250 // Lower is faster.
|
|
|
|
|
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-08-24 16:51:55 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2020-06-07 18:09:02 +00:00
|
|
|
// Used to display a list of options.
|
|
|
|
// Can select one or multiple options.
|
2021-07-07 18:31:46 +00:00
|
|
|
template <typename T> class OptionListComponent : public GuiComponent
|
2013-10-01 21:52:30 +00:00
|
|
|
{
|
2014-03-06 01:49:32 +00:00
|
|
|
public:
|
2022-01-19 17:01:54 +00:00
|
|
|
OptionListComponent(const HelpStyle& helpstyle,
|
2021-07-07 18:31:46 +00:00
|
|
|
const std::string& name,
|
2021-09-25 08:47:59 +00:00
|
|
|
bool multiSelect = false,
|
|
|
|
bool multiExclusiveSelect = false,
|
|
|
|
bool multiShowTotal = false)
|
2022-01-19 17:01:54 +00:00
|
|
|
: mHelpStyle {helpstyle}
|
2022-01-16 11:09:55 +00:00
|
|
|
, mMultiSelect {multiSelect}
|
|
|
|
, mMultiExclusiveSelect {multiExclusiveSelect}
|
|
|
|
, mMultiShowTotal {multiShowTotal}
|
|
|
|
, mKeyRepeat {false}
|
|
|
|
, mKeyRepeatDir {0}
|
|
|
|
, mKeyRepeatTimer {0}
|
|
|
|
, mKeyRepeatStartDelay {OPTIONLIST_REPEAT_START_DELAY}
|
|
|
|
, mKeyRepeatSpeed {OPTIONLIST_REPEAT_SPEED}
|
|
|
|
, mName {name}
|
2020-06-28 16:39:18 +00:00
|
|
|
{
|
2022-01-16 11:09:55 +00:00
|
|
|
auto font {Font::get(FONT_SIZE_MEDIUM, FONT_PATH_LIGHT)};
|
2020-06-28 16:39:18 +00:00
|
|
|
mText.setFont(font);
|
|
|
|
mText.setColor(0x777777FF);
|
|
|
|
mText.setHorizontalAlignment(ALIGN_CENTER);
|
|
|
|
addChild(&mText);
|
|
|
|
|
|
|
|
mLeftArrow.setResize(0, mText.getFont()->getLetterHeight());
|
|
|
|
mRightArrow.setResize(0, mText.getFont()->getLetterHeight());
|
|
|
|
|
2020-07-13 18:58:25 +00:00
|
|
|
if (mMultiSelect) {
|
2020-06-28 16:39:18 +00:00
|
|
|
mRightArrow.setImage(":/graphics/arrow.svg");
|
|
|
|
addChild(&mRightArrow);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mLeftArrow.setImage(":/graphics/option_arrow.svg");
|
|
|
|
mLeftArrow.setFlipX(true);
|
|
|
|
addChild(&mLeftArrow);
|
|
|
|
|
|
|
|
mRightArrow.setImage(":/graphics/option_arrow.svg");
|
|
|
|
addChild(&mRightArrow);
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
setSize(mLeftArrow.getSize().x + mRightArrow.getSize().x, font->getHeight());
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handles positioning/resizing of text and arrows.
|
|
|
|
void onSizeChanged() override
|
|
|
|
{
|
2021-10-25 16:39:58 +00:00
|
|
|
if (mText.getFont()->getLetterHeight() != mLeftArrow.getSize().y ||
|
|
|
|
mLeftArrow.getTexture()->getPendingRasterization())
|
|
|
|
mLeftArrow.setResize(0, mText.getFont()->getLetterHeight());
|
|
|
|
if (mText.getFont()->getLetterHeight() != mRightArrow.getSize().y ||
|
|
|
|
mRightArrow.getTexture()->getPendingRasterization())
|
|
|
|
mRightArrow.setResize(0, mText.getFont()->getLetterHeight());
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
if (mSize.x < (mLeftArrow.getSize().x + mRightArrow.getSize().x)) {
|
2021-01-14 17:24:41 +00:00
|
|
|
LOG(LogWarning) << "OptionListComponent too narrow";
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
mText.setSize(mSize.x - mLeftArrow.getSize().x - mRightArrow.getSize().x,
|
2021-07-07 18:31:46 +00:00
|
|
|
mText.getFont()->getHeight());
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
// Position.
|
2021-10-29 17:45:46 +00:00
|
|
|
mLeftArrow.setPosition(0.0f, std::round((mSize.y - mLeftArrow.getSize().y) / 2.0f));
|
2021-08-16 16:25:01 +00:00
|
|
|
mText.setPosition(mLeftArrow.getPosition().x + mLeftArrow.getSize().x,
|
|
|
|
(mSize.y - mText.getSize().y) / 2.0f);
|
|
|
|
mRightArrow.setPosition(mText.getPosition().x + mText.getSize().x,
|
2021-10-29 17:45:46 +00:00
|
|
|
std::round((mSize.y - mRightArrow.getSize().y) / 2.0f));
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool input(InputConfig* config, Input input) override
|
|
|
|
{
|
2021-10-08 17:13:37 +00:00
|
|
|
if (config->isMappedTo("a", input) && input.value) {
|
|
|
|
// Ignore input if the component has been disabled.
|
|
|
|
if (!mEnabled)
|
2020-06-28 16:39:18 +00:00
|
|
|
return true;
|
2021-10-08 17:13:37 +00:00
|
|
|
mKeyRepeatDir = 0;
|
|
|
|
open();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!mMultiSelect) {
|
|
|
|
if (config->isMappedLike("left", input)) {
|
|
|
|
if (input.value) {
|
|
|
|
if (mKeyRepeat) {
|
|
|
|
mKeyRepeatDir = -1;
|
|
|
|
mKeyRepeatTimer = -(mKeyRepeatStartDelay - mKeyRepeatSpeed);
|
|
|
|
}
|
2020-08-05 17:31:59 +00:00
|
|
|
// Ignore input if the component has been disabled.
|
|
|
|
if (!mEnabled)
|
|
|
|
return true;
|
2020-06-28 16:39:18 +00:00
|
|
|
// Move selection to previous.
|
2022-01-17 19:47:30 +00:00
|
|
|
unsigned int i {getSelectedId()};
|
|
|
|
int next {static_cast<int>(i) - 1};
|
2020-07-13 18:58:25 +00:00
|
|
|
if (next < 0)
|
2020-11-17 22:06:54 +00:00
|
|
|
next += static_cast<int>(mEntries.size());
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
mEntries.at(i).selected = false;
|
|
|
|
mEntries.at(next).selected = true;
|
|
|
|
onSelectedChanged();
|
|
|
|
return true;
|
|
|
|
}
|
2021-10-08 17:13:37 +00:00
|
|
|
else {
|
|
|
|
mKeyRepeatDir = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (config->isMappedLike("right", input)) {
|
|
|
|
if (input.value) {
|
|
|
|
if (mKeyRepeat) {
|
|
|
|
mKeyRepeatDir = 1;
|
|
|
|
mKeyRepeatTimer = -(mKeyRepeatStartDelay - mKeyRepeatSpeed);
|
|
|
|
}
|
2020-08-05 17:31:59 +00:00
|
|
|
// Ignore input if the component has been disabled.
|
|
|
|
if (!mEnabled)
|
|
|
|
return true;
|
2020-06-28 16:39:18 +00:00
|
|
|
// Move selection to next.
|
2022-01-17 19:47:30 +00:00
|
|
|
unsigned int i {getSelectedId()};
|
2020-06-28 16:39:18 +00:00
|
|
|
int next = (i + 1) % mEntries.size();
|
|
|
|
mEntries.at(i).selected = false;
|
|
|
|
mEntries.at(next).selected = true;
|
|
|
|
onSelectedChanged();
|
|
|
|
return true;
|
|
|
|
}
|
2021-10-08 17:13:37 +00:00
|
|
|
else {
|
|
|
|
mKeyRepeatDir = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (input.value) {
|
|
|
|
mKeyRepeatDir = 0;
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return GuiComponent::input(config, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<T> getSelectedObjects()
|
|
|
|
{
|
|
|
|
std::vector<T> ret;
|
2021-11-17 16:48:49 +00:00
|
|
|
for (auto it = mEntries.cbegin(); it != mEntries.cend(); ++it) {
|
2020-07-13 18:58:25 +00:00
|
|
|
if (it->selected)
|
2020-06-28 16:39:18 +00:00
|
|
|
ret.push_back(it->object);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
T getSelected()
|
|
|
|
{
|
|
|
|
assert(mMultiSelect == false);
|
|
|
|
auto selected = getSelectedObjects();
|
|
|
|
assert(selected.size() == 1);
|
|
|
|
return selected.at(0);
|
|
|
|
}
|
|
|
|
|
2021-10-09 11:06:04 +00:00
|
|
|
void add(const std::string& name, const T& obj, bool selected, float maxNameLength = 0.0f)
|
2020-06-28 16:39:18 +00:00
|
|
|
{
|
|
|
|
OptionListData e;
|
|
|
|
e.name = name;
|
|
|
|
e.object = obj;
|
|
|
|
e.selected = selected;
|
2021-10-09 11:06:04 +00:00
|
|
|
e.maxNameLength = maxNameLength;
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
mEntries.push_back(e);
|
2021-09-28 19:46:45 +00:00
|
|
|
|
2021-10-08 20:50:37 +00:00
|
|
|
onSelectedChanged();
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool selectEntry(unsigned int entry)
|
|
|
|
{
|
2021-09-28 19:46:45 +00:00
|
|
|
if (mEntries.empty() || entry > mEntries.size()) {
|
2020-06-28 16:39:18 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void selectAll()
|
|
|
|
{
|
2021-11-17 16:48:49 +00:00
|
|
|
for (unsigned int i = 0; i < mEntries.size(); ++i)
|
2020-06-28 16:39:18 +00:00
|
|
|
mEntries.at(i).selected = true;
|
|
|
|
onSelectedChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void selectNone()
|
|
|
|
{
|
2021-11-17 16:48:49 +00:00
|
|
|
for (unsigned int i = 0; i < mEntries.size(); ++i)
|
2020-06-28 16:39:18 +00:00
|
|
|
mEntries.at(i).selected = false;
|
|
|
|
onSelectedChanged();
|
|
|
|
}
|
|
|
|
|
2020-08-24 16:51:55 +00:00
|
|
|
void sortEntriesByName()
|
|
|
|
{
|
|
|
|
std::sort(std::begin(mEntries), std::end(mEntries),
|
2021-07-07 18:31:46 +00:00
|
|
|
[](OptionListData a, OptionListData b) { return a.name < b.name; });
|
2020-08-24 16:51:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
unsigned int getSelectedId()
|
|
|
|
{
|
|
|
|
assert(mMultiSelect == false);
|
2021-11-17 16:48:49 +00:00
|
|
|
for (unsigned int i = 0; i < mEntries.size(); ++i) {
|
2020-07-13 18:58:25 +00:00
|
|
|
if (mEntries.at(i).selected)
|
2020-06-28 16:39:18 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(LogWarning) << "OptionListComponent::getSelectedId() - "
|
2021-07-07 18:31:46 +00:00
|
|
|
"no selected element found, defaulting to 0";
|
2020-06-28 16:39:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-25 08:47:59 +00:00
|
|
|
void setOverrideMultiText(const std::string& text) { mOverrideMultiText = text; }
|
2021-10-08 19:11:38 +00:00
|
|
|
|
|
|
|
void setKeyRepeat(bool state,
|
|
|
|
int delay = OPTIONLIST_REPEAT_START_DELAY,
|
|
|
|
int speed = OPTIONLIST_REPEAT_SPEED)
|
|
|
|
{
|
|
|
|
mKeyRepeat = state;
|
|
|
|
mKeyRepeatStartDelay = delay;
|
|
|
|
mKeyRepeatSpeed = speed;
|
|
|
|
}
|
2021-10-08 17:13:37 +00:00
|
|
|
|
|
|
|
void update(int deltaTime) override
|
|
|
|
{
|
|
|
|
if (mKeyRepeat && mKeyRepeatDir != 0) {
|
|
|
|
mKeyRepeatTimer += deltaTime;
|
|
|
|
while (mKeyRepeatTimer >= mKeyRepeatSpeed) {
|
|
|
|
if (mKeyRepeatDir == -1) {
|
|
|
|
// Move selection to previous.
|
|
|
|
unsigned int i = getSelectedId();
|
|
|
|
int next = static_cast<int>(i) - 1;
|
|
|
|
if (next < 0)
|
|
|
|
next += static_cast<int>(mEntries.size());
|
|
|
|
mEntries.at(i).selected = false;
|
|
|
|
mEntries.at(next).selected = true;
|
|
|
|
onSelectedChanged();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
mKeyRepeatTimer -= mKeyRepeatSpeed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GuiComponent::update(deltaTime);
|
|
|
|
}
|
2021-09-25 08:47:59 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
HelpStyle getHelpStyle() override { return mHelpStyle; }
|
2020-12-16 16:35:23 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct OptionListData {
|
|
|
|
std::string name;
|
|
|
|
T object;
|
|
|
|
bool selected;
|
2021-10-09 11:06:04 +00:00
|
|
|
float maxNameLength;
|
2020-12-16 16:35:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
HelpStyle mHelpStyle;
|
|
|
|
|
2021-08-22 13:26:38 +00:00
|
|
|
void open()
|
|
|
|
{
|
|
|
|
// Open the list popup.
|
2022-01-19 17:01:54 +00:00
|
|
|
mWindow->pushGui(new OptionListPopup(getHelpStyle(), this, mName));
|
2021-08-22 13:26:38 +00:00
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
void onSelectedChanged()
|
|
|
|
{
|
2020-07-13 18:58:25 +00:00
|
|
|
if (mMultiSelect) {
|
2021-01-14 17:24:41 +00:00
|
|
|
// Display the selected entry.
|
2020-06-28 16:39:18 +00:00
|
|
|
std::stringstream ss;
|
2021-09-25 08:47:59 +00:00
|
|
|
|
|
|
|
// For special situations, allow the "selected" text to be overridden to a custom value.
|
|
|
|
if (mOverrideMultiText != "")
|
|
|
|
ss << mOverrideMultiText;
|
|
|
|
else if (mMultiShowTotal)
|
|
|
|
ss << getSelectedObjects().size() << " (OF " << mEntries.size() << ") SELECTED";
|
|
|
|
else
|
|
|
|
ss << getSelectedObjects().size() << " SELECTED";
|
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
mText.setText(ss.str());
|
2021-08-16 16:25:01 +00:00
|
|
|
mText.setSize(0, mText.getSize().y);
|
|
|
|
setSize(mText.getSize().x + mRightArrow.getSize().x +
|
2021-11-11 18:54:10 +00:00
|
|
|
std::round(Font::get(FONT_SIZE_MEDIUM)->getLetterHeight() * 0.68f),
|
2021-08-16 16:25:01 +00:00
|
|
|
mText.getSize().y);
|
2021-01-14 17:24:41 +00:00
|
|
|
if (mParent) // Hack since there's no "on child size changed" callback.
|
2020-06-28 16:39:18 +00:00
|
|
|
mParent->onSizeChanged();
|
|
|
|
}
|
|
|
|
else {
|
2021-01-14 17:24:41 +00:00
|
|
|
// Display the selected entry and left/right option arrows.
|
2021-11-17 16:48:49 +00:00
|
|
|
for (auto it = mEntries.cbegin(); it != mEntries.cend(); ++it) {
|
2020-07-13 18:58:25 +00:00
|
|
|
if (it->selected) {
|
2021-10-09 11:06:04 +00:00
|
|
|
if (it->maxNameLength > 0.0f &&
|
|
|
|
Font::get(FONT_SIZE_MEDIUM)->sizeText(Utils::String::toUpper(it->name)).x >
|
|
|
|
it->maxNameLength) {
|
|
|
|
// A maximum length parameter has been passed and the "name" size surpasses
|
|
|
|
// this value, so abbreviate the string inside the arrows.
|
|
|
|
auto font = Font::get(FONT_SIZE_MEDIUM);
|
|
|
|
// Calculate with an extra dot to give some leeway.
|
|
|
|
float dotsSize = font->sizeText("....").x;
|
|
|
|
std::string abbreviatedString = font->getTextMaxWidth(
|
|
|
|
Utils::String::toUpper(it->name), it->maxNameLength);
|
|
|
|
float sizeDifference = font->sizeText(Utils::String::toUpper(it->name)).x -
|
|
|
|
font->sizeText(abbreviatedString).x;
|
|
|
|
if (sizeDifference > 0.0f) {
|
|
|
|
// It doesn't make sense to abbreviate if the number of pixels removed
|
|
|
|
// by the abbreviation is less or equal to the size of the three dots
|
|
|
|
// that would be appended to the string.
|
|
|
|
if (sizeDifference <= dotsSize) {
|
|
|
|
abbreviatedString = it->name;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (abbreviatedString.back() == ' ')
|
|
|
|
abbreviatedString.pop_back();
|
|
|
|
abbreviatedString += "...";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mText.setText(Utils::String::toUpper(abbreviatedString));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mText.setText(Utils::String::toUpper(it->name));
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
mText.setSize(0.0f, mText.getSize().y);
|
|
|
|
setSize(mText.getSize().x + mLeftArrow.getSize().x + mRightArrow.getSize().x +
|
2021-11-11 18:54:10 +00:00
|
|
|
std::round(Font::get(FONT_SIZE_MEDIUM)->getLetterHeight() * 0.68f),
|
2021-08-16 16:25:01 +00:00
|
|
|
mText.getSize().y);
|
2021-01-14 17:24:41 +00:00
|
|
|
if (mParent) // Hack since there's no "on child size changed" callback.
|
2020-06-28 16:39:18 +00:00
|
|
|
mParent->onSizeChanged();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 20:50:37 +00:00
|
|
|
onSizeChanged();
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<HelpPrompt> getHelpPrompts() override
|
|
|
|
{
|
|
|
|
std::vector<HelpPrompt> prompts;
|
2020-07-13 18:58:25 +00:00
|
|
|
if (!mMultiSelect)
|
2020-06-28 16:39:18 +00:00
|
|
|
prompts.push_back(HelpPrompt("left/right", "change value"));
|
|
|
|
|
|
|
|
prompts.push_back(HelpPrompt("a", "select"));
|
|
|
|
return prompts;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mMultiSelect;
|
2021-09-25 08:47:59 +00:00
|
|
|
bool mMultiExclusiveSelect;
|
|
|
|
bool mMultiShowTotal;
|
2021-10-08 17:13:37 +00:00
|
|
|
bool mKeyRepeat;
|
|
|
|
|
|
|
|
int mKeyRepeatDir;
|
|
|
|
int mKeyRepeatTimer;
|
|
|
|
int mKeyRepeatStartDelay;
|
|
|
|
int mKeyRepeatSpeed;
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2021-10-08 17:13:37 +00:00
|
|
|
std::string mOverrideMultiText;
|
2020-06-28 16:39:18 +00:00
|
|
|
std::string mName;
|
2021-10-08 17:13:37 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
TextComponent mText;
|
|
|
|
ImageComponent mLeftArrow;
|
|
|
|
ImageComponent mRightArrow;
|
|
|
|
|
|
|
|
std::vector<OptionListData> mEntries;
|
|
|
|
|
|
|
|
// Subclass to OptionListComponent.
|
|
|
|
class OptionListPopup : public GuiComponent
|
|
|
|
{
|
|
|
|
public:
|
2022-01-19 17:01:54 +00:00
|
|
|
OptionListPopup(const HelpStyle& helpstyle,
|
2021-07-07 18:31:46 +00:00
|
|
|
OptionListComponent<T>* parent,
|
|
|
|
const std::string& title)
|
2022-01-19 17:01:54 +00:00
|
|
|
: mMenu(title.c_str())
|
2021-07-07 18:31:46 +00:00
|
|
|
, mParent(parent)
|
2021-09-18 07:53:26 +00:00
|
|
|
, mHelpStyle(helpstyle)
|
2020-06-28 16:39:18 +00:00
|
|
|
{
|
|
|
|
auto font = Font::get(FONT_SIZE_MEDIUM);
|
|
|
|
ComponentListRow row;
|
|
|
|
|
2022-01-17 19:47:30 +00:00
|
|
|
bool hasSelectedRow {false};
|
2021-09-25 08:47:59 +00:00
|
|
|
|
|
|
|
// If the exclusive selection flag has been set, i.e. only a single row can be selected
|
|
|
|
// at a time, then make sure to gray out and disable any non-selected rows.
|
|
|
|
if (mParent->mMultiExclusiveSelect) {
|
|
|
|
for (auto entry : mParent->mEntries) {
|
|
|
|
if (entry.selected == true) {
|
|
|
|
hasSelectedRow = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 17:24:41 +00:00
|
|
|
// For selecting all/none.
|
2021-09-25 08:47:59 +00:00
|
|
|
std::vector<ImageComponent*> checkBoxes;
|
|
|
|
std::vector<TextComponent*> textEntries;
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2021-11-17 16:48:49 +00:00
|
|
|
for (auto it = mParent->mEntries.begin(); it != mParent->mEntries.end(); ++it) {
|
2020-06-28 16:39:18 +00:00
|
|
|
row.elements.clear();
|
2021-09-25 08:47:59 +00:00
|
|
|
auto textComponent = std::make_shared<TextComponent>(
|
2022-01-19 17:01:54 +00:00
|
|
|
Utils::String::toUpper(it->name), font, 0x777777FF);
|
2021-09-25 08:47:59 +00:00
|
|
|
row.addElement(textComponent, true);
|
|
|
|
|
|
|
|
if (mParent->mMultiExclusiveSelect && hasSelectedRow && !(*it).selected) {
|
|
|
|
textComponent.get()->setOpacity(DISABLED_OPACITY);
|
|
|
|
textComponent.get()->setEnabled(false);
|
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2022-01-17 19:47:30 +00:00
|
|
|
OptionListData& e {*it};
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2020-07-13 18:58:25 +00:00
|
|
|
if (mParent->mMultiSelect) {
|
2020-06-28 16:39:18 +00:00
|
|
|
// Add checkbox.
|
2022-01-19 17:01:54 +00:00
|
|
|
auto checkbox = std::make_shared<ImageComponent>();
|
2020-06-28 16:39:18 +00:00
|
|
|
checkbox->setImage(it->selected ? CHECKED_PATH : UNCHECKED_PATH);
|
|
|
|
checkbox->setResize(0, font->getLetterHeight());
|
|
|
|
row.addElement(checkbox, false);
|
|
|
|
|
2021-09-25 08:47:59 +00:00
|
|
|
if (mParent->mMultiExclusiveSelect && hasSelectedRow && !(*it).selected)
|
|
|
|
checkbox.get()->setOpacity(DISABLED_OPACITY);
|
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
// Input handler.
|
2021-01-14 17:24:41 +00:00
|
|
|
// Update checkbox state and selected value.
|
2020-06-28 16:39:18 +00:00
|
|
|
row.makeAcceptInputHandler([this, &e, checkbox] {
|
2021-09-25 08:47:59 +00:00
|
|
|
auto list = mMenu.getList();
|
2022-01-17 19:47:30 +00:00
|
|
|
int cursorId {list->getCursorId()};
|
|
|
|
bool isEnabled {list->getChild(cursorId * 2)->getEnabled()};
|
2021-09-25 08:47:59 +00:00
|
|
|
|
|
|
|
if (mParent->mMultiExclusiveSelect && !isEnabled)
|
|
|
|
return;
|
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
e.selected = !e.selected;
|
|
|
|
checkbox->setImage(e.selected ? CHECKED_PATH : UNCHECKED_PATH);
|
|
|
|
mParent->onSelectedChanged();
|
2021-09-25 08:47:59 +00:00
|
|
|
|
|
|
|
// When selecting a row and the exclusive selection flag has been set,
|
|
|
|
// gray out and disable all other rows.
|
|
|
|
if (mParent->mMultiExclusiveSelect) {
|
2021-11-17 16:48:49 +00:00
|
|
|
for (unsigned int i = 0; i < mParent->mEntries.size(); ++i) {
|
2021-09-25 08:47:59 +00:00
|
|
|
|
2022-01-17 19:47:30 +00:00
|
|
|
bool isSelected {mParent->mEntries[cursorId].selected};
|
2021-09-25 08:47:59 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < list->getChildCount(); i += 2) {
|
|
|
|
if (i == static_cast<unsigned int>(cursorId) * 2)
|
|
|
|
continue;
|
|
|
|
if (isSelected) {
|
|
|
|
mEnabled = false;
|
|
|
|
list->getChild(i)->setEnabled(false);
|
|
|
|
list->getChild(i)->setOpacity(DISABLED_OPACITY);
|
|
|
|
list->getChild(i + 1)->setOpacity(DISABLED_OPACITY);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mEnabled = true;
|
|
|
|
list->getChild(i)->setEnabled(true);
|
|
|
|
list->getChild(i)->setOpacity(255);
|
|
|
|
list->getChild(i + 1)->setOpacity(255);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
});
|
|
|
|
|
2021-01-14 17:24:41 +00:00
|
|
|
// For selecting all/none.
|
2021-09-25 08:47:59 +00:00
|
|
|
checkBoxes.push_back(checkbox.get());
|
|
|
|
textEntries.push_back(textComponent.get());
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
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.
|
2021-09-25 17:22:59 +00:00
|
|
|
mMenu.addRow(row, (!mParent->mMultiSelect && it->selected), false);
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mMenu.addButton("BACK", "back", [this] { delete this; });
|
|
|
|
|
2020-07-13 18:58:25 +00:00
|
|
|
if (mParent->mMultiSelect) {
|
2021-09-25 08:47:59 +00:00
|
|
|
if (!mParent->mMultiExclusiveSelect) {
|
|
|
|
mMenu.addButton("SELECT ALL", "select all", [this, checkBoxes] {
|
2021-11-17 16:48:49 +00:00
|
|
|
for (unsigned int i = 0; i < mParent->mEntries.size(); ++i) {
|
2021-09-25 08:47:59 +00:00
|
|
|
mParent->mEntries.at(i).selected = true;
|
|
|
|
checkBoxes.at(i)->setImage(CHECKED_PATH);
|
|
|
|
}
|
|
|
|
mParent->onSelectedChanged();
|
|
|
|
});
|
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2021-09-25 08:47:59 +00:00
|
|
|
mMenu.addButton("SELECT NONE", "select none", [this, checkBoxes, textEntries] {
|
2021-11-17 16:48:49 +00:00
|
|
|
for (unsigned int i = 0; i < mParent->mEntries.size(); ++i) {
|
2020-06-28 16:39:18 +00:00
|
|
|
mParent->mEntries.at(i).selected = false;
|
2021-09-25 08:47:59 +00:00
|
|
|
checkBoxes.at(i)->setImage(UNCHECKED_PATH);
|
|
|
|
if (mParent->mMultiExclusiveSelect) {
|
|
|
|
checkBoxes.at(i)->setOpacity(255);
|
|
|
|
textEntries.at(i)->setOpacity(255);
|
|
|
|
textEntries.at(i)->setEnabled(true);
|
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
mParent->onSelectedChanged();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
mMenu.setPosition((Renderer::getScreenWidth() - mMenu.getSize().x) / 2.0f,
|
2021-07-07 18:31:46 +00:00
|
|
|
Renderer::getScreenHeight() * 0.13f);
|
2020-06-28 16:39:18 +00:00
|
|
|
addChild(&mMenu);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool input(InputConfig* config, Input input) override
|
|
|
|
{
|
2020-07-13 18:58:25 +00:00
|
|
|
if (config->isMappedTo("b", input) && input.value != 0) {
|
2020-06-28 16:39:18 +00:00
|
|
|
delete this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GuiComponent::input(config, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<HelpPrompt> getHelpPrompts() override
|
|
|
|
{
|
|
|
|
auto prompts = mMenu.getHelpPrompts();
|
|
|
|
prompts.push_back(HelpPrompt("a", "select"));
|
|
|
|
prompts.push_back(HelpPrompt("b", "back"));
|
|
|
|
return prompts;
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
HelpStyle getHelpStyle() override { return mHelpStyle; }
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
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
|