2020-09-17 20:00:07 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-28 16:39:18 +00:00
|
|
|
//
|
2020-09-17 20:00:07 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-28 16:39:18 +00:00
|
|
|
// ScrollableContainer.cpp
|
|
|
|
//
|
2021-09-30 18:11:56 +00:00
|
|
|
// Component containing scrollable information, used for the game
|
|
|
|
// description text in the scraper and gamelist views.
|
2020-06-28 16:39:18 +00:00
|
|
|
//
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/ScrollableContainer.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "Window.h"
|
2021-01-02 20:17:23 +00:00
|
|
|
#include "animations/LambdaAnimation.h"
|
2019-08-08 20:16:11 +00:00
|
|
|
#include "renderers/Renderer.h"
|
2021-01-17 21:02:22 +00:00
|
|
|
#include "resources/Font.h"
|
2013-07-03 01:01:58 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
ScrollableContainer::ScrollableContainer(Window* window)
|
2021-09-30 18:11:56 +00:00
|
|
|
: GuiComponent{window}
|
|
|
|
, mScrollPos{0.0f, 0.0f}
|
|
|
|
, mScrollDir{0.0f, 0.0f}
|
|
|
|
, mAutoScrollDelay{0}
|
|
|
|
, mAutoScrollSpeed{0}
|
|
|
|
, mAutoScrollAccumulator{0}
|
|
|
|
, mAutoScrollResetAccumulator{0}
|
|
|
|
, mAdjustedAutoScrollSpeed{0}
|
|
|
|
, mUpdatedSize{false}
|
2013-07-03 01:01:58 +00:00
|
|
|
{
|
2021-01-02 20:17:23 +00:00
|
|
|
// Set the modifier to get equivalent scrolling speed regardless of screen resolution.
|
2021-01-18 23:11:02 +00:00
|
|
|
mResolutionModifier = Renderer::getScreenHeightModifier();
|
2021-01-05 11:52:21 +00:00
|
|
|
|
|
|
|
mAutoScrollResetDelayConstant = AUTO_SCROLL_RESET_DELAY;
|
|
|
|
mAutoScrollDelayConstant = AUTO_SCROLL_DELAY;
|
|
|
|
mAutoScrollSpeedConstant = AUTO_SCROLL_SPEED;
|
2013-07-03 01:01:58 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 01:15:15 +00:00
|
|
|
void ScrollableContainer::setAutoScroll(bool autoScroll)
|
2013-07-03 01:01:58 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
if (autoScroll) {
|
2021-08-17 16:41:45 +00:00
|
|
|
mScrollDir = glm::vec2{0.0f, 1.0f};
|
2021-01-18 23:11:02 +00:00
|
|
|
mAutoScrollDelay = static_cast<int>(mAutoScrollDelayConstant);
|
2020-06-28 16:39:18 +00:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
else {
|
2021-08-17 16:41:45 +00:00
|
|
|
mScrollDir = glm::vec2{};
|
2020-06-28 16:39:18 +00:00
|
|
|
mAutoScrollDelay = 0;
|
|
|
|
mAutoScrollSpeed = 0;
|
|
|
|
mAutoScrollAccumulator = 0;
|
|
|
|
}
|
2013-07-03 01:01:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 09:17:41 +00:00
|
|
|
void ScrollableContainer::setScrollParameters(float autoScrollDelayConstant,
|
2021-07-07 18:31:46 +00:00
|
|
|
float autoScrollResetDelayConstant,
|
2021-09-30 18:11:56 +00:00
|
|
|
float autoScrollSpeedConstant)
|
2013-07-03 01:01:58 +00:00
|
|
|
{
|
2021-09-30 18:11:56 +00:00
|
|
|
mAutoScrollResetDelayConstant = glm::clamp(autoScrollResetDelayConstant, 1000.0f, 10000.0f);
|
|
|
|
mAutoScrollDelayConstant = glm::clamp(autoScrollDelayConstant, 1000.0f, 10000.0f);
|
|
|
|
mAutoScrollSpeedConstant = AUTO_SCROLL_SPEED / glm::clamp(autoScrollSpeedConstant, 0.1f, 10.0f);
|
2021-01-17 21:02:22 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 11:52:21 +00:00
|
|
|
void ScrollableContainer::reset()
|
2013-07-03 01:01:58 +00:00
|
|
|
{
|
2021-08-17 16:41:45 +00:00
|
|
|
mScrollPos = glm::vec2{};
|
2021-01-05 11:52:21 +00:00
|
|
|
mAutoScrollResetAccumulator = 0;
|
|
|
|
mAutoScrollAccumulator = -mAutoScrollDelay + mAutoScrollSpeed;
|
|
|
|
mAtEnd = false;
|
2013-07-03 01:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollableContainer::update(int deltaTime)
|
|
|
|
{
|
2021-05-16 11:12:31 +00:00
|
|
|
// Don't scroll if the media viewer or screensaver is active or if text scrolling is disabled;
|
|
|
|
if (mWindow->isMediaViewerActive() || mWindow->isScreensaverActive() ||
|
2021-07-07 18:31:46 +00:00
|
|
|
!mWindow->getAllowTextScrolling()) {
|
2021-08-17 16:41:45 +00:00
|
|
|
if (mScrollPos != glm::vec2{} && !mWindow->isLaunchScreenDisplayed())
|
2020-09-17 20:00:07 +00:00
|
|
|
reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-30 18:11:56 +00:00
|
|
|
const glm::vec2 contentSize{mChildren.front()->getSize()};
|
|
|
|
float rowModifier{1.0f};
|
|
|
|
|
|
|
|
float lineSpacing{mChildren.front()->getLineSpacing()};
|
|
|
|
float combinedHeight{mChildren.front()->getFont()->getHeight(lineSpacing)};
|
|
|
|
|
|
|
|
// Resize container to font height boundary to avoid rendering a fraction of the last line.
|
|
|
|
if (!mUpdatedSize && contentSize.y > mSize.y) {
|
|
|
|
float numLines{mSize.y / combinedHeight};
|
|
|
|
mSize.y = floorf(numLines) * combinedHeight;
|
|
|
|
mUpdatedSize = true;
|
|
|
|
}
|
|
|
|
else if (mUpdatedSize) {
|
|
|
|
// If there are less than 8 lines of text, accelerate the scrolling further.
|
|
|
|
float lines{mSize.y / combinedHeight};
|
|
|
|
if (lines < 8.0f)
|
|
|
|
rowModifier = lines / 8.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mAdjustedAutoScrollSpeed) {
|
|
|
|
float fontSize{static_cast<float>(mChildren.front()->getFont()->getSize())};
|
|
|
|
float width{contentSize.x / (fontSize * 1.3f)};
|
2021-01-18 23:11:02 +00:00
|
|
|
|
2021-09-30 18:11:56 +00:00
|
|
|
// Keep speed adjustments within reason.
|
|
|
|
float speedModifier{glm::clamp(width, 10.0f, 40.0f)};
|
2021-01-02 20:17:23 +00:00
|
|
|
|
2021-09-30 18:11:56 +00:00
|
|
|
speedModifier *= mAutoScrollSpeedConstant;
|
|
|
|
speedModifier /= mResolutionModifier;
|
2021-09-30 18:18:15 +00:00
|
|
|
mAdjustedAutoScrollSpeed = static_cast<int>(speedModifier);
|
2021-09-30 18:11:56 +00:00
|
|
|
}
|
2020-10-17 10:16:58 +00:00
|
|
|
|
2021-09-30 18:11:56 +00:00
|
|
|
if (mAdjustedAutoScrollSpeed < 0)
|
|
|
|
mAdjustedAutoScrollSpeed = 1;
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2021-09-30 18:11:56 +00:00
|
|
|
if (mAdjustedAutoScrollSpeed != 0) {
|
2021-01-18 23:11:02 +00:00
|
|
|
mAutoScrollAccumulator += deltaTime;
|
2021-09-30 18:11:56 +00:00
|
|
|
while (mAutoScrollAccumulator >=
|
|
|
|
static_cast<int>(rowModifier * static_cast<float>(mAdjustedAutoScrollSpeed))) {
|
2021-08-16 16:25:01 +00:00
|
|
|
if (contentSize.y > mSize.y)
|
2021-03-14 08:49:26 +00:00
|
|
|
mScrollPos += mScrollDir;
|
2021-09-30 18:11:56 +00:00
|
|
|
mAutoScrollAccumulator -=
|
|
|
|
static_cast<int>(rowModifier * static_cast<float>(mAdjustedAutoScrollSpeed));
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clip scrolling within bounds.
|
2021-08-16 16:25:01 +00:00
|
|
|
if (mScrollPos.x < 0.0f)
|
|
|
|
mScrollPos.x = 0.0f;
|
|
|
|
if (mScrollPos.y < 0.0f)
|
|
|
|
mScrollPos.y = 0.0f;
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
if (mScrollPos.x + getSize().x > contentSize.x) {
|
|
|
|
mScrollPos.x = contentSize.x - getSize().x;
|
2020-06-28 16:39:18 +00:00
|
|
|
mAtEnd = true;
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
if (contentSize.y < getSize().y) {
|
|
|
|
mScrollPos.y = 0.0f;
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
2021-08-16 16:25:01 +00:00
|
|
|
else if (mScrollPos.y + getSize().y > contentSize.y) {
|
|
|
|
mScrollPos.y = contentSize.y - getSize().y;
|
2020-06-28 16:39:18 +00:00
|
|
|
mAtEnd = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mAtEnd) {
|
|
|
|
mAutoScrollResetAccumulator += deltaTime;
|
2021-01-17 21:02:22 +00:00
|
|
|
if (mAutoScrollResetAccumulator >= static_cast<int>(mAutoScrollResetDelayConstant)) {
|
2021-01-02 20:17:23 +00:00
|
|
|
// Fade in the text as it resets to the start position.
|
|
|
|
auto func = [this](float t) {
|
2021-08-17 18:55:29 +00:00
|
|
|
this->setOpacity(static_cast<unsigned char>(glm::mix(0.0f, 1.0f, t) * 255));
|
2021-08-17 16:41:45 +00:00
|
|
|
mScrollPos = glm::vec2{};
|
2021-01-02 20:17:23 +00:00
|
|
|
mAutoScrollResetAccumulator = 0;
|
|
|
|
mAutoScrollAccumulator = -mAutoScrollDelay + mAutoScrollSpeed;
|
|
|
|
mAtEnd = false;
|
|
|
|
};
|
|
|
|
this->setAnimation(new LambdaAnimation(func, 300), 0, nullptr, false);
|
|
|
|
}
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GuiComponent::update(deltaTime);
|
2013-07-03 01:01:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 17:30:31 +00:00
|
|
|
void ScrollableContainer::render(const glm::mat4& parentTrans)
|
2021-01-05 11:52:21 +00:00
|
|
|
{
|
|
|
|
if (!isVisible())
|
|
|
|
return;
|
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::mat4 trans{parentTrans * getTransform()};
|
2021-01-05 11:52:21 +00:00
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::ivec2 clipPos{static_cast<int>(trans[3].x), static_cast<int>(trans[3].y)};
|
2021-08-15 17:30:31 +00:00
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::vec3 dimScaled{};
|
2021-08-16 16:25:01 +00:00
|
|
|
dimScaled.x = std::fabs(trans[3].x + mSize.x);
|
|
|
|
dimScaled.y = std::fabs(trans[3].y + mSize.y);
|
2021-08-15 17:30:31 +00:00
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::ivec2 clipDim{static_cast<int>(dimScaled.x - trans[3].x),
|
|
|
|
static_cast<int>(dimScaled.y - trans[3].y)};
|
2021-01-05 11:52:21 +00:00
|
|
|
|
|
|
|
Renderer::pushClipRect(clipPos, clipDim);
|
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
trans = glm::translate(trans, -glm::vec3{mScrollPos.x, mScrollPos.y, 0.0f});
|
2021-01-05 11:52:21 +00:00
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
|
|
|
|
GuiComponent::renderChildren(trans);
|
|
|
|
Renderer::popClipRect();
|
|
|
|
}
|