ScrollableContainer now takes the font size into consideration for adjusting the scrolling speed.

This commit is contained in:
Leon Styhre 2021-01-17 22:02:22 +01:00
parent 7b19b2cc6b
commit e68c8f1db3
7 changed files with 39 additions and 21 deletions

View file

@ -68,9 +68,9 @@ GuiScraperSearch::GuiScraperSearch(
// Adjust the game description text scrolling parameters depending on the search type.
if (mSearchType == NEVER_AUTO_ACCEPT)
mDescContainer->setScrollParameters(1500, 1500, 11, AUTO_WIDTH_MOD);
mDescContainer->setScrollParameters(2500, 3000, 30);
else
mDescContainer->setScrollParameters(4000, 1500, 11, AUTO_WIDTH_MOD);
mDescContainer->setScrollParameters(6000, 3000, 30);
mResultDesc = std::make_shared<TextComponent>(mWindow, "Result desc",
Font::get(FONT_SIZE_SMALL), 0x777777FF);

View file

@ -123,6 +123,7 @@ DetailedGameListView::DetailedGameListView(
mDescription.setFont(Font::get(FONT_SIZE_SMALL));
mDescription.setSize(mDescContainer.getSize().x(), 0);
mDescContainer.addChild(&mDescription);
mDescContainer.setFontSizeSpeedAdjustments(Font::get(FONT_SIZE_SMALL)->getSize());
mGamelistInfo.setOrigin(0.5f, 0.5f);
mGamelistInfo.setFont(Font::get(FONT_SIZE_SMALL));
@ -174,6 +175,7 @@ void DetailedGameListView::onThemeChanged(const std::shared_ptr<ThemeData>& them
mDescription.setSize(mDescContainer.getSize().x(), 0);
mDescription.applyTheme(theme, getName(), "md_description",
ALL ^ (POSITION | ThemeFlags::SIZE | ThemeFlags::ORIGIN | TEXT | ROTATION));
mDescContainer.setFontSizeSpeedAdjustments(mDescription.getFont()->getSize());
mGamelistInfo.applyTheme(theme, getName(), "gamelistInfo", ALL ^ ThemeFlags::TEXT);

View file

@ -117,6 +117,7 @@ GridGameListView::GridGameListView(
mDescription.setFont(Font::get(FONT_SIZE_SMALL));
mDescription.setSize(mDescContainer.getSize().x(), 0);
mDescContainer.addChild(&mDescription);
mDescContainer.setFontSizeSpeedAdjustments(Font::get(FONT_SIZE_SMALL)->getSize());
mMarquee.setOrigin(0.5f, 0.5f);
mMarquee.setPosition(mSize.x() * 0.25f, mSize.y() * 0.10f);
@ -305,6 +306,7 @@ void GridGameListView::onThemeChanged(const std::shared_ptr<ThemeData>& theme)
mDescription.setSize(mDescContainer.getSize().x(), 0);
mDescription.applyTheme(theme, getName(), "md_description",
ALL ^ (POSITION | ThemeFlags::SIZE | ThemeFlags::ORIGIN | TEXT | ROTATION));
mDescContainer.setFontSizeSpeedAdjustments(mDescription.getFont()->getSize());
// Repopulate list in case a new theme is displaying a different image.
// Preserve selection.

View file

@ -141,6 +141,7 @@ VideoGameListView::VideoGameListView(
mDescription.setFont(Font::get(FONT_SIZE_SMALL));
mDescription.setSize(mDescContainer.getSize().x(), 0);
mDescContainer.addChild(&mDescription);
mDescContainer.setFontSizeSpeedAdjustments(Font::get(FONT_SIZE_SMALL)->getSize());
mGamelistInfo.setOrigin(0.5f, 0.5f);
mGamelistInfo.setFont(Font::get(FONT_SIZE_SMALL));
@ -199,6 +200,7 @@ void VideoGameListView::onThemeChanged(const std::shared_ptr<ThemeData>& theme)
mDescription.setSize(mDescContainer.getSize().x(), 0);
mDescription.applyTheme(theme, getName(), "md_description",
ALL ^ (POSITION | ThemeFlags::SIZE | ThemeFlags::ORIGIN | TEXT | ROTATION));
mDescContainer.setFontSizeSpeedAdjustments(mDescription.getFont()->getSize());
mGamelistInfo.applyTheme(theme, getName(), "gamelistInfo", ALL ^ ThemeFlags::TEXT);

View file

@ -169,7 +169,7 @@ public:
virtual void setHiddenValue(const std::string& value);
// Used to set the parameters for ScrollableContainer.
virtual void setScrollParameters(float, float, int, float) {};
virtual void setScrollParameters(float, float, int) {};
virtual void onFocusGained() {};
virtual void onFocusLost() {};

View file

@ -12,6 +12,7 @@
#include "animations/LambdaAnimation.h"
#include "math/Vector2i.h"
#include "renderers/Renderer.h"
#include "resources/Font.h"
#include "Window.h"
ScrollableContainer::ScrollableContainer(
@ -31,7 +32,6 @@ ScrollableContainer::ScrollableContainer(
mAutoScrollResetDelayConstant = AUTO_SCROLL_RESET_DELAY;
mAutoScrollDelayConstant = AUTO_SCROLL_DELAY;
mAutoScrollSpeedConstant = AUTO_SCROLL_SPEED;
mAutoWidthModConstant = AUTO_WIDTH_MOD;
}
Vector2f ScrollableContainer::getScrollPos() const
@ -61,13 +61,27 @@ void ScrollableContainer::setAutoScroll(bool autoScroll)
}
void ScrollableContainer::setScrollParameters(float autoScrollDelayConstant,
float autoScrollResetDelayConstant, int autoScrollSpeedConstant,
float autoWidthModConstant)
float autoScrollResetDelayConstant, int autoScrollSpeedConstant)
{
mAutoScrollResetDelayConstant = autoScrollResetDelayConstant;
mAutoScrollDelayConstant = autoScrollDelayConstant;
mAutoScrollSpeedConstant = autoScrollSpeedConstant;
mAutoWidthModConstant = autoWidthModConstant;
}
void ScrollableContainer::setFontSizeSpeedAdjustments(int size)
{
// Adjust scrolling speed relative to the font size, i.e. a larger size makes it faster.
float fontSizeModifier =
static_cast<float>(Font::get(FONT_SIZE_SMALL)->getSize()) / (static_cast<float>(size));
fontSizeModifier = fontSizeModifier * fontSizeModifier * fontSizeModifier;
mAutoScrollSpeed =
static_cast<int>((static_cast<float>(mAutoScrollSpeed) * fontSizeModifier) / 2.0f);
// Also adjust the speed relative to the width of the text container. This is not perfect
// but at least increases the speed for narrower fields to a reasonable level.
float widthSpeedModifier = getContentSize().x() / Renderer::getScreenWidth();
mAutoScrollSpeed =
static_cast<int>(static_cast<float>(mAutoScrollSpeed) * widthSpeedModifier);
}
void ScrollableContainer::reset()
@ -89,12 +103,8 @@ void ScrollableContainer::update(int deltaTime)
const Vector2f contentSize = getContentSize();
// Scale speed by the text width, more text per line leads to slower scrolling.
const float widthMod = contentSize.x() / mAutoWidthModConstant / mResolutionModifier;
// Adjust delta time by text width and screen resolution.
int adjustedDeltaTime =
static_cast<int>(static_cast<float>(deltaTime) * mResolutionModifier / widthMod);
// Adjust delta time by screen resolution.
int adjustedDeltaTime = static_cast<int>(static_cast<float>(deltaTime) * mResolutionModifier);
if (mAutoScrollSpeed != 0) {
mAutoScrollAccumulator += adjustedDeltaTime;
@ -126,8 +136,7 @@ void ScrollableContainer::update(int deltaTime)
if (mAtEnd) {
mAutoScrollResetAccumulator += deltaTime;
if (mAutoScrollResetAccumulator >=
static_cast<int>(mAutoScrollResetDelayConstant * widthMod)) {
if (mAutoScrollResetAccumulator >= static_cast<int>(mAutoScrollResetDelayConstant)) {
// Fade in the text as it resets to the start position.
auto func = [this](float t) {
this->setOpacity(static_cast<unsigned char>(Math::lerp(0.0f, 1.0f, t) * 255));

View file

@ -10,10 +10,12 @@
#ifndef ES_CORE_COMPONENTS_SCROLLABLE_CONTAINER_H
#define ES_CORE_COMPONENTS_SCROLLABLE_CONTAINER_H
#define AUTO_SCROLL_DELAY 2400.0f // Time to wait before we start to scroll.
#define AUTO_SCROLL_RESET_DELAY 4000.0f // Time before resetting to top after we reach the bottom.
#define AUTO_SCROLL_SPEED 48 // Relative scrolling speed (lower is faster).
#define AUTO_WIDTH_MOD 350.0f // Line width modifier to use to calculate scrolling speed.
// Time in ms to wait before scrolling starts.
#define AUTO_SCROLL_DELAY 4500.0f
// Time in ms before resetting to the top after we reach the bottom.
#define AUTO_SCROLL_RESET_DELAY 7000.0f
// Relative scrolling speed (lower is faster).
#define AUTO_SCROLL_SPEED 420
#include "GuiComponent.h"
@ -26,7 +28,9 @@ public:
void setScrollPos(const Vector2f& pos);
void setAutoScroll(bool autoScroll);
void setScrollParameters(float autoScrollDelayConstant, float autoScrollResetDelayConstant,
int autoScrollSpeedConstant, float autoWidthModConstant) override;
int autoScrollSpeedConstant) override;
// Adjust scrolling speed based on the font size (and text container width).
void setFontSizeSpeedAdjustments(int size);
void reset();
void update(int deltaTime) override;
@ -41,7 +45,6 @@ private:
float mAutoScrollResetDelayConstant;
float mAutoScrollDelayConstant;
int mAutoScrollSpeedConstant;
float mAutoWidthModConstant;
float mResolutionModifier;
int mAutoScrollDelay;