From 66e1c4e8976b272c227dd4830a83201d4b080f99 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Tue, 19 Jan 2021 00:11:02 +0100 Subject: [PATCH] Improved the speed consistency for ScrollableContainer. --- es-app/src/guis/GuiScraperSearch.cpp | 4 +- .../views/gamelist/DetailedGameListView.cpp | 2 - .../src/views/gamelist/GridGameListView.cpp | 2 - .../src/views/gamelist/VideoGameListView.cpp | 2 - es-core/src/GuiComponent.h | 2 + .../src/components/DateTimeEditComponent.h | 2 +- .../src/components/ScrollableContainer.cpp | 56 ++++++++++--------- es-core/src/components/ScrollableContainer.h | 7 ++- es-core/src/components/TextComponent.h | 2 +- es-core/src/components/TextEditComponent.h | 2 +- 10 files changed, 41 insertions(+), 40 deletions(-) diff --git a/es-app/src/guis/GuiScraperSearch.cpp b/es-app/src/guis/GuiScraperSearch.cpp index 05ea4a26d..39b2cf105 100644 --- a/es-app/src/guis/GuiScraperSearch.cpp +++ b/es-app/src/guis/GuiScraperSearch.cpp @@ -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(2500, 3000, 30); + mDescContainer->setScrollParameters(2500, 3000, 70); else - mDescContainer->setScrollParameters(6000, 3000, 30); + mDescContainer->setScrollParameters(6000, 3000, 70); mResultDesc = std::make_shared(mWindow, "Result desc", Font::get(FONT_SIZE_SMALL), 0x777777FF); diff --git a/es-app/src/views/gamelist/DetailedGameListView.cpp b/es-app/src/views/gamelist/DetailedGameListView.cpp index ee531883b..f1d0719ef 100644 --- a/es-app/src/views/gamelist/DetailedGameListView.cpp +++ b/es-app/src/views/gamelist/DetailedGameListView.cpp @@ -123,7 +123,6 @@ 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)); @@ -175,7 +174,6 @@ void DetailedGameListView::onThemeChanged(const std::shared_ptr& 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); diff --git a/es-app/src/views/gamelist/GridGameListView.cpp b/es-app/src/views/gamelist/GridGameListView.cpp index 05fc8a53c..fd27554b7 100644 --- a/es-app/src/views/gamelist/GridGameListView.cpp +++ b/es-app/src/views/gamelist/GridGameListView.cpp @@ -117,7 +117,6 @@ 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); @@ -306,7 +305,6 @@ void GridGameListView::onThemeChanged(const std::shared_ptr& 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. diff --git a/es-app/src/views/gamelist/VideoGameListView.cpp b/es-app/src/views/gamelist/VideoGameListView.cpp index aa7e351fa..55f909e00 100644 --- a/es-app/src/views/gamelist/VideoGameListView.cpp +++ b/es-app/src/views/gamelist/VideoGameListView.cpp @@ -141,7 +141,6 @@ 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)); @@ -200,7 +199,6 @@ void VideoGameListView::onThemeChanged(const std::shared_ptr& 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); diff --git a/es-core/src/GuiComponent.h b/es-core/src/GuiComponent.h index dd7c9938a..c900e8db8 100644 --- a/es-core/src/GuiComponent.h +++ b/es-core/src/GuiComponent.h @@ -160,6 +160,8 @@ public: virtual bool getEnabled() { return mEnabled; }; virtual void setEnabled(bool state) { mEnabled = state; }; + virtual std::shared_ptr getFont() const { return nullptr; }; + const Transform4x4f& getTransform(); virtual std::string getValue() const; diff --git a/es-core/src/components/DateTimeEditComponent.h b/es-core/src/components/DateTimeEditComponent.h index 8f918eb71..376ad1c3c 100644 --- a/es-core/src/components/DateTimeEditComponent.h +++ b/es-core/src/components/DateTimeEditComponent.h @@ -61,7 +61,7 @@ public: virtual std::vector getHelpPrompts() override; private: - std::shared_ptr getFont() const; + std::shared_ptr getFont() const override; std::string getDisplayString(DisplayMode mode) const; DisplayMode getCurrentDisplayMode() const; diff --git a/es-core/src/components/ScrollableContainer.cpp b/es-core/src/components/ScrollableContainer.cpp index dc2c93433..68121e33f 100644 --- a/es-core/src/components/ScrollableContainer.cpp +++ b/es-core/src/components/ScrollableContainer.cpp @@ -23,11 +23,19 @@ ScrollableContainer::ScrollableContainer( mAutoScrollAccumulator(0), mScrollPos(0, 0), mScrollDir(0, 0), - mAutoScrollResetAccumulator(0) + mAutoScrollResetAccumulator(0), + mFontSize(0.0f) { // Set the modifier to get equivalent scrolling speed regardless of screen resolution. - // 1080p is the reference. - mResolutionModifier = Renderer::getScreenWidthModifier(); + mResolutionModifier = Renderer::getScreenHeightModifier(); + mSmallFontSize = static_cast(Font::get(FONT_SIZE_SMALL)->getSize()); + + // For narrower aspect ratios than 16:9 there is a need to set an additional compensation + // to get somehow consistent scrolling speeds. + float aspectCompensation = static_cast(Renderer::getScreenHeight()) / + static_cast(Renderer::getScreenWidth()) - 0.5625f; + if (aspectCompensation > 0) + mResolutionModifier += aspectCompensation * 4.0f; mAutoScrollResetDelayConstant = AUTO_SCROLL_RESET_DELAY; mAutoScrollDelayConstant = AUTO_SCROLL_DELAY; @@ -48,8 +56,10 @@ void ScrollableContainer::setAutoScroll(bool autoScroll) { if (autoScroll) { mScrollDir = Vector2f(0, 1); - mAutoScrollDelay = static_cast(mAutoScrollDelayConstant * mResolutionModifier); + mAutoScrollDelay = static_cast(mAutoScrollDelayConstant); mAutoScrollSpeed = mAutoScrollSpeedConstant; + mAutoScrollSpeed = static_cast(static_cast(mAutoScrollSpeedConstant) / + mResolutionModifier); reset(); } else { @@ -68,22 +78,6 @@ void ScrollableContainer::setScrollParameters(float autoScrollDelayConstant, mAutoScrollSpeedConstant = autoScrollSpeedConstant; } -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(Font::get(FONT_SIZE_SMALL)->getSize()) / (static_cast(size)); - fontSizeModifier = fontSizeModifier * fontSizeModifier * fontSizeModifier; - mAutoScrollSpeed = - static_cast((static_cast(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(static_cast(mAutoScrollSpeed) * widthSpeedModifier); -} - void ScrollableContainer::reset() { mScrollPos = Vector2f(0, 0); @@ -102,16 +96,24 @@ void ScrollableContainer::update(int deltaTime) } const Vector2f contentSize = getContentSize(); + int adjustedAutoScrollSpeed = mAutoScrollSpeed; - // Adjust delta time by screen resolution. - int adjustedDeltaTime = static_cast(static_cast(deltaTime) * mResolutionModifier); + // Adjust the scrolling speed based on the width of the container. + float widthModifier = contentSize.x() / static_cast(Renderer::getScreenWidth()); + adjustedAutoScrollSpeed *= widthModifier; - if (mAutoScrollSpeed != 0) { - mAutoScrollAccumulator += adjustedDeltaTime; + // Also adjust the scrolling speed based on the size of the font. + float fontSizeModifier = mSmallFontSize / mFontSize; + adjustedAutoScrollSpeed *= fontSizeModifier * fontSizeModifier; - while (mAutoScrollAccumulator >= mAutoScrollSpeed) { + if (adjustedAutoScrollSpeed < 0) + adjustedAutoScrollSpeed = 1; + + if (adjustedAutoScrollSpeed != 0) { + mAutoScrollAccumulator += deltaTime; + while (mAutoScrollAccumulator >= adjustedAutoScrollSpeed) { mScrollPos += mScrollDir; - mAutoScrollAccumulator -= mAutoScrollSpeed; + mAutoScrollAccumulator -= adjustedAutoScrollSpeed; } } @@ -185,6 +187,8 @@ Vector2f ScrollableContainer::getContentSize() max.x() = bottomRight.x(); if (bottomRight.y() > max.y()) max.y() = bottomRight.y(); + if (!mFontSize) + mFontSize = static_cast(mChildren.at(i)->getFont()->getSize()); } return max; diff --git a/es-core/src/components/ScrollableContainer.h b/es-core/src/components/ScrollableContainer.h index cb4167361..293d4d598 100644 --- a/es-core/src/components/ScrollableContainer.h +++ b/es-core/src/components/ScrollableContainer.h @@ -15,7 +15,7 @@ // 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 +#define AUTO_SCROLL_SPEED 90 #include "GuiComponent.h" @@ -29,8 +29,6 @@ public: void setAutoScroll(bool autoScroll); void setScrollParameters(float autoScrollDelayConstant, float autoScrollResetDelayConstant, 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; @@ -42,6 +40,9 @@ private: Vector2f mScrollPos; Vector2f mScrollDir; + float mFontSize; + float mSmallFontSize; + float mAutoScrollResetDelayConstant; float mAutoScrollDelayConstant; int mAutoScrollSpeedConstant; diff --git a/es-core/src/components/TextComponent.h b/es-core/src/components/TextComponent.h index 2547c222c..a6c55d380 100644 --- a/es-core/src/components/TextComponent.h +++ b/es-core/src/components/TextComponent.h @@ -63,7 +63,7 @@ public: const std::string& element, unsigned int properties) override; unsigned int getColor() const override { return mColor; }; - inline std::shared_ptr getFont() const { return mFont; } + inline std::shared_ptr getFont() const override { return mFont; } protected: virtual void onTextChanged(); diff --git a/es-core/src/components/TextEditComponent.h b/es-core/src/components/TextEditComponent.h index a95905db4..1a97a7bbe 100644 --- a/es-core/src/components/TextEditComponent.h +++ b/es-core/src/components/TextEditComponent.h @@ -38,7 +38,7 @@ public: void stopEditing(); inline bool isEditing() const { return mEditing; }; - inline const std::shared_ptr& getFont() const { return mFont; } + inline std::shared_ptr getFont() const override{ return mFont; } void setCursor(size_t pos);