Added support for key repeat to OptionListComponent.

This commit is contained in:
Leon Styhre 2021-10-08 19:13:37 +02:00
parent e12eb5a908
commit a4d2b87ede

View file

@ -10,6 +10,9 @@
#ifndef ES_CORE_COMPONENTS_OPTION_LIST_COMPONENT_H #ifndef ES_CORE_COMPONENTS_OPTION_LIST_COMPONENT_H
#define ES_CORE_COMPONENTS_OPTION_LIST_COMPONENT_H #define ES_CORE_COMPONENTS_OPTION_LIST_COMPONENT_H
#define OPTIONLIST_REPEAT_START_DELAY 650
#define OPTIONLIST_REPEAT_SPEED 250 // Lower is faster.
#define CHECKED_PATH ":/graphics/checkbox_checked.svg" #define CHECKED_PATH ":/graphics/checkbox_checked.svg"
#define UNCHECKED_PATH ":/graphics/checkbox_unchecked.svg" #define UNCHECKED_PATH ":/graphics/checkbox_unchecked.svg"
@ -30,17 +33,22 @@ public:
bool multiSelect = false, bool multiSelect = false,
bool multiExclusiveSelect = false, bool multiExclusiveSelect = false,
bool multiShowTotal = false) bool multiShowTotal = false)
: GuiComponent(window) : GuiComponent{window}
, mHelpStyle(helpstyle) , mHelpStyle{helpstyle}
, mMultiSelect(multiSelect) , mMultiSelect{multiSelect}
, mMultiExclusiveSelect(multiExclusiveSelect) , mMultiExclusiveSelect{multiExclusiveSelect}
, mMultiShowTotal(multiShowTotal) , mMultiShowTotal{multiShowTotal}
, mName(name) , mKeyRepeat{false}
, mText(window) , mKeyRepeatDir{0}
, mLeftArrow(window) , mKeyRepeatTimer{0}
, mRightArrow(window) , mKeyRepeatStartDelay{OPTIONLIST_REPEAT_START_DELAY}
, mKeyRepeatSpeed{OPTIONLIST_REPEAT_SPEED}
, mName{name}
, mText{window}
, mLeftArrow{window}
, mRightArrow{window}
{ {
auto font = Font::get(FONT_SIZE_MEDIUM, FONT_PATH_LIGHT); auto font{Font::get(FONT_SIZE_MEDIUM, FONT_PATH_LIGHT)};
mText.setFont(font); mText.setFont(font);
mText.setColor(0x777777FF); mText.setColor(0x777777FF);
mText.setHorizontalAlignment(ALIGN_CENTER); mText.setHorizontalAlignment(ALIGN_CENTER);
@ -88,16 +96,21 @@ public:
bool input(InputConfig* config, Input input) override bool input(InputConfig* config, Input input) override
{ {
if (input.value != 0) { if (config->isMappedTo("a", input) && input.value) {
if (config->isMappedTo("a", input)) {
// Ignore input if the component has been disabled. // Ignore input if the component has been disabled.
if (!mEnabled) if (!mEnabled)
return true; return true;
mKeyRepeatDir = 0;
open(); open();
return true; return true;
} }
if (!mMultiSelect) { if (!mMultiSelect) {
if (config->isMappedLike("left", input)) { if (config->isMappedLike("left", input)) {
if (input.value) {
if (mKeyRepeat) {
mKeyRepeatDir = -1;
mKeyRepeatTimer = -(mKeyRepeatStartDelay - mKeyRepeatSpeed);
}
// Ignore input if the component has been disabled. // Ignore input if the component has been disabled.
if (!mEnabled) if (!mEnabled)
return true; return true;
@ -112,7 +125,16 @@ public:
onSelectedChanged(); onSelectedChanged();
return true; return true;
} }
else {
mKeyRepeatDir = 0;
}
}
else if (config->isMappedLike("right", input)) { else if (config->isMappedLike("right", input)) {
if (input.value) {
if (mKeyRepeat) {
mKeyRepeatDir = 1;
mKeyRepeatTimer = -(mKeyRepeatStartDelay - mKeyRepeatSpeed);
}
// Ignore input if the component has been disabled. // Ignore input if the component has been disabled.
if (!mEnabled) if (!mEnabled)
return true; return true;
@ -124,6 +146,12 @@ public:
onSelectedChanged(); onSelectedChanged();
return true; return true;
} }
else {
mKeyRepeatDir = 0;
}
}
else if (input.value) {
mKeyRepeatDir = 0;
} }
} }
return GuiComponent::input(config, input); return GuiComponent::input(config, input);
@ -219,6 +247,37 @@ public:
} }
void setOverrideMultiText(const std::string& text) { mOverrideMultiText = text; } void setOverrideMultiText(const std::string& text) { mOverrideMultiText = text; }
void setKeyRepeat(bool state) { mKeyRepeat = state; }
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);
}
HelpStyle getHelpStyle() override { return mHelpStyle; } HelpStyle getHelpStyle() override { return mHelpStyle; }
@ -292,9 +351,16 @@ private:
bool mMultiSelect; bool mMultiSelect;
bool mMultiExclusiveSelect; bool mMultiExclusiveSelect;
bool mMultiShowTotal; bool mMultiShowTotal;
std::string mOverrideMultiText; bool mKeyRepeat;
int mKeyRepeatDir;
int mKeyRepeatTimer;
int mKeyRepeatStartDelay;
int mKeyRepeatSpeed;
std::string mOverrideMultiText;
std::string mName; std::string mName;
TextComponent mText; TextComponent mText;
ImageComponent mLeftArrow; ImageComponent mLeftArrow;
ImageComponent mRightArrow; ImageComponent mRightArrow;