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
|
|
|
|
//
|
|
|
|
// Area containing scrollable information, for example the game description
|
|
|
|
// text container in the detailed, video and grid views.
|
|
|
|
//
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/ScrollableContainer.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2021-01-02 20:17:23 +00:00
|
|
|
#include "animations/LambdaAnimation.h"
|
2019-08-08 20:16:11 +00:00
|
|
|
#include "math/Vector2i.h"
|
|
|
|
#include "renderers/Renderer.h"
|
2021-01-17 21:02:22 +00:00
|
|
|
#include "resources/Font.h"
|
2020-09-17 20:00:07 +00:00
|
|
|
#include "Window.h"
|
2013-07-03 01:01:58 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
ScrollableContainer::ScrollableContainer(
|
|
|
|
Window* window)
|
|
|
|
: GuiComponent(window),
|
|
|
|
mAutoScrollDelay(0),
|
|
|
|
mAutoScrollSpeed(0),
|
|
|
|
mAutoScrollAccumulator(0),
|
|
|
|
mScrollPos(0, 0),
|
|
|
|
mScrollDir(0, 0),
|
2021-01-18 23:11:02 +00:00
|
|
|
mAutoScrollResetAccumulator(0),
|
|
|
|
mFontSize(0.0f)
|
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();
|
|
|
|
mSmallFontSize = static_cast<float>(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<float>(Renderer::getScreenHeight()) /
|
|
|
|
static_cast<float>(Renderer::getScreenWidth()) - 0.5625f;
|
|
|
|
if (aspectCompensation > 0)
|
|
|
|
mResolutionModifier += aspectCompensation * 4.0f;
|
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
|
|
|
}
|
|
|
|
|
2021-01-05 11:52:21 +00:00
|
|
|
Vector2f ScrollableContainer::getScrollPos() const
|
2013-07-03 01:01:58 +00:00
|
|
|
{
|
2021-01-05 11:52:21 +00:00
|
|
|
return mScrollPos;
|
|
|
|
}
|
2013-07-03 01:01:58 +00:00
|
|
|
|
2021-01-05 11:52:21 +00:00
|
|
|
void ScrollableContainer::setScrollPos(const Vector2f& pos)
|
|
|
|
{
|
|
|
|
mScrollPos = pos;
|
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) {
|
|
|
|
mScrollDir = Vector2f(0, 1);
|
2021-01-18 23:11:02 +00:00
|
|
|
mAutoScrollDelay = static_cast<int>(mAutoScrollDelayConstant);
|
2021-01-05 11:52:21 +00:00
|
|
|
mAutoScrollSpeed = mAutoScrollSpeedConstant;
|
2021-01-18 23:11:02 +00:00
|
|
|
mAutoScrollSpeed = static_cast<int>(static_cast<float>(mAutoScrollSpeedConstant) /
|
|
|
|
mResolutionModifier);
|
2020-06-28 16:39:18 +00:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mScrollDir = Vector2f(0, 0);
|
|
|
|
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-01-17 21:02:22 +00:00
|
|
|
float autoScrollResetDelayConstant, int autoScrollSpeedConstant)
|
2013-07-03 01:01:58 +00:00
|
|
|
{
|
2021-01-05 11:52:21 +00:00
|
|
|
mAutoScrollResetDelayConstant = autoScrollResetDelayConstant;
|
|
|
|
mAutoScrollDelayConstant = autoScrollDelayConstant;
|
|
|
|
mAutoScrollSpeedConstant = autoScrollSpeedConstant;
|
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-01-05 11:52:21 +00:00
|
|
|
mScrollPos = Vector2f(0, 0);
|
|
|
|
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() ||
|
|
|
|
!mWindow->getAllowTextScrolling()) {
|
2021-06-14 17:15:22 +00:00
|
|
|
if (mScrollPos != 0 && !mWindow->isLaunchScreenDisplayed())
|
2020-09-17 20:00:07 +00:00
|
|
|
reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-17 10:16:58 +00:00
|
|
|
const Vector2f contentSize = getContentSize();
|
2021-01-18 23:11:02 +00:00
|
|
|
int adjustedAutoScrollSpeed = mAutoScrollSpeed;
|
|
|
|
|
|
|
|
// Adjust the scrolling speed based on the width of the container.
|
|
|
|
float widthModifier = contentSize.x() / static_cast<float>(Renderer::getScreenWidth());
|
2021-01-18 23:26:02 +00:00
|
|
|
adjustedAutoScrollSpeed = static_cast<int>(adjustedAutoScrollSpeed * widthModifier);
|
2021-01-02 20:17:23 +00:00
|
|
|
|
2021-01-18 23:11:02 +00:00
|
|
|
// Also adjust the scrolling speed based on the size of the font.
|
|
|
|
float fontSizeModifier = mSmallFontSize / mFontSize;
|
2021-01-18 23:26:02 +00:00
|
|
|
adjustedAutoScrollSpeed = static_cast<int>(adjustedAutoScrollSpeed *
|
|
|
|
fontSizeModifier * fontSizeModifier);
|
2020-10-17 10:16:58 +00:00
|
|
|
|
2021-01-18 23:11:02 +00:00
|
|
|
if (adjustedAutoScrollSpeed < 0)
|
|
|
|
adjustedAutoScrollSpeed = 1;
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2021-01-18 23:11:02 +00:00
|
|
|
if (adjustedAutoScrollSpeed != 0) {
|
|
|
|
mAutoScrollAccumulator += deltaTime;
|
|
|
|
while (mAutoScrollAccumulator >= adjustedAutoScrollSpeed) {
|
2021-03-14 08:49:26 +00:00
|
|
|
if (contentSize.y() > mSize.y())
|
|
|
|
mScrollPos += mScrollDir;
|
2021-01-18 23:11:02 +00:00
|
|
|
mAutoScrollAccumulator -= adjustedAutoScrollSpeed;
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clip scrolling within bounds.
|
|
|
|
if (mScrollPos.x() < 0)
|
|
|
|
mScrollPos[0] = 0;
|
|
|
|
if (mScrollPos.y() < 0)
|
|
|
|
mScrollPos[1] = 0;
|
|
|
|
|
|
|
|
if (mScrollPos.x() + getSize().x() > contentSize.x()) {
|
|
|
|
mScrollPos[0] = contentSize.x() - getSize().x();
|
|
|
|
mAtEnd = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (contentSize.y() < getSize().y()) {
|
|
|
|
mScrollPos[1] = 0;
|
|
|
|
}
|
|
|
|
else if (mScrollPos.y() + getSize().y() > contentSize.y()) {
|
|
|
|
mScrollPos[1] = contentSize.y() - getSize().y();
|
|
|
|
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) {
|
|
|
|
this->setOpacity(static_cast<unsigned char>(Math::lerp(0.0f, 1.0f, t) * 255));
|
|
|
|
mScrollPos = Vector2f(0, 0);
|
|
|
|
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-01-05 11:52:21 +00:00
|
|
|
void ScrollableContainer::render(const Transform4x4f& parentTrans)
|
|
|
|
{
|
|
|
|
if (!isVisible())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Transform4x4f trans = parentTrans * getTransform();
|
|
|
|
|
|
|
|
Vector2i clipPos(static_cast<int>(trans.translation().x()),
|
|
|
|
static_cast<int>(trans.translation().y()));
|
|
|
|
|
|
|
|
Vector3f dimScaled = trans * Vector3f(mSize.x(), mSize.y(), 0);
|
|
|
|
Vector2i clipDim(static_cast<int>((dimScaled.x()) - trans.translation().x()),
|
|
|
|
static_cast<int>((dimScaled.y()) - trans.translation().y()));
|
|
|
|
|
|
|
|
Renderer::pushClipRect(clipPos, clipDim);
|
|
|
|
|
|
|
|
trans.translate(-Vector3f(mScrollPos.x(), mScrollPos.y(), 0));
|
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
|
|
|
|
GuiComponent::renderChildren(trans);
|
|
|
|
Renderer::popClipRect();
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f ScrollableContainer::getContentSize()
|
2013-07-03 01:01:58 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
Vector2f max(0, 0);
|
|
|
|
for (unsigned int i = 0; i < mChildren.size(); i++) {
|
|
|
|
Vector2f pos(mChildren.at(i)->getPosition()[0], mChildren.at(i)->getPosition()[1]);
|
|
|
|
Vector2f bottomRight = mChildren.at(i)->getSize() + pos;
|
|
|
|
if (bottomRight.x() > max.x())
|
|
|
|
max.x() = bottomRight.x();
|
|
|
|
if (bottomRight.y() > max.y())
|
|
|
|
max.y() = bottomRight.y();
|
2021-01-18 23:11:02 +00:00
|
|
|
if (!mFontSize)
|
|
|
|
mFontSize = static_cast<float>(mChildren.at(i)->getFont()->getSize());
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return max;
|
2013-07-03 01:01:58 +00:00
|
|
|
}
|