Added a function to set scrolling parameters for ScrollableContainer.

Also tuned the scrolling speed a bit.
This commit is contained in:
Leon Styhre 2021-01-05 12:52:21 +01:00
parent 4d2e267c47
commit a28f174960
3 changed files with 79 additions and 53 deletions

View file

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

View file

@ -14,11 +14,6 @@
#include "renderers/Renderer.h"
#include "Window.h"
#define AUTO_SCROLL_RESET_DELAY 4000.0f // Time before resetting to top after we reach the bottom.
#define AUTO_SCROLL_DELAY 2600.0f // Time to wait before we start to scroll.
#define AUTO_SCROLL_SPEED 42 // Relative scrolling speed (lower is faster).
#define AUTO_WIDTH_MOD 350.0f // Line width modifier to use to calculate scrolling speed.
ScrollableContainer::ScrollableContainer(
Window* window)
: GuiComponent(window),
@ -32,45 +27,11 @@ ScrollableContainer::ScrollableContainer(
// Set the modifier to get equivalent scrolling speed regardless of screen resolution.
// 1080p is the reference.
mResolutionModifier = static_cast<float>(Renderer::getScreenWidth()) / 1920.0f;
}
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();
}
void ScrollableContainer::setAutoScroll(bool autoScroll)
{
if (autoScroll) {
mScrollDir = Vector2f(0, 1);
mAutoScrollDelay = static_cast<int>(AUTO_SCROLL_DELAY * mResolutionModifier);
mAutoScrollSpeed = AUTO_SCROLL_SPEED;
reset();
}
else {
mScrollDir = Vector2f(0, 0);
mAutoScrollDelay = 0;
mAutoScrollSpeed = 0;
mAutoScrollAccumulator = 0;
}
mAutoScrollResetDelayConstant = AUTO_SCROLL_RESET_DELAY;
mAutoScrollDelayConstant = AUTO_SCROLL_DELAY;
mAutoScrollSpeedConstant = AUTO_SCROLL_SPEED;
mAutoWidthModConstant = AUTO_WIDTH_MOD;
}
Vector2f ScrollableContainer::getScrollPos() const
@ -83,6 +44,39 @@ void ScrollableContainer::setScrollPos(const Vector2f& pos)
mScrollPos = pos;
}
void ScrollableContainer::setAutoScroll(bool autoScroll)
{
if (autoScroll) {
mScrollDir = Vector2f(0, 1);
mAutoScrollDelay = static_cast<int>(mAutoScrollDelayConstant * mResolutionModifier);
mAutoScrollSpeed = mAutoScrollSpeedConstant;
reset();
}
else {
mScrollDir = Vector2f(0, 0);
mAutoScrollDelay = 0;
mAutoScrollSpeed = 0;
mAutoScrollAccumulator = 0;
}
}
void ScrollableContainer::setScrollParameters(float autoScrollResetDelayConstant,
float autoScrollDelayConstant, int autoScrollSpeedConstant, float autoWidthModConstant)
{
mAutoScrollResetDelayConstant = autoScrollResetDelayConstant;
mAutoScrollDelayConstant = autoScrollDelayConstant;
mAutoScrollSpeedConstant = autoScrollSpeedConstant;
mAutoWidthModConstant = autoWidthModConstant;
}
void ScrollableContainer::reset()
{
mScrollPos = Vector2f(0, 0);
mAutoScrollResetAccumulator = 0;
mAutoScrollAccumulator = -mAutoScrollDelay + mAutoScrollSpeed;
mAtEnd = false;
}
void ScrollableContainer::update(int deltaTime)
{
// Don't scroll if the screensaver is active or text scrolling is disabled;
@ -95,7 +89,7 @@ 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() / AUTO_WIDTH_MOD / mResolutionModifier;
const float widthMod = contentSize.x() / mAutoWidthModConstant / mResolutionModifier;
// Adjust delta time by text width and screen resolution.
int adjustedDeltaTime =
@ -131,7 +125,8 @@ void ScrollableContainer::update(int deltaTime)
if (mAtEnd) {
mAutoScrollResetAccumulator += deltaTime;
if (mAutoScrollResetAccumulator >= static_cast<int>(AUTO_SCROLL_RESET_DELAY * widthMod)) {
if (mAutoScrollResetAccumulator >=
static_cast<int>(mAutoScrollResetDelayConstant * widthMod)) {
// 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));
@ -147,6 +142,29 @@ void ScrollableContainer::update(int deltaTime)
GuiComponent::update(deltaTime);
}
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();
}
Vector2f ScrollableContainer::getContentSize()
{
Vector2f max(0, 0);
@ -161,11 +179,3 @@ Vector2f ScrollableContainer::getContentSize()
return max;
}
void ScrollableContainer::reset()
{
mScrollPos = Vector2f(0, 0);
mAutoScrollResetAccumulator = 0;
mAutoScrollAccumulator = -mAutoScrollDelay + mAutoScrollSpeed;
mAtEnd = false;
}

View file

@ -10,6 +10,11 @@
#ifndef ES_CORE_COMPONENTS_SCROLLABLE_CONTAINER_H
#define ES_CORE_COMPONENTS_SCROLLABLE_CONTAINER_H
#define AUTO_SCROLL_RESET_DELAY 3500.0f // Time before resetting to top after we reach the bottom.
#define AUTO_SCROLL_DELAY 2000.0f // Time to wait before we start to scroll.
#define AUTO_SCROLL_SPEED 38 // Relative scrolling speed (lower is faster).
#define AUTO_WIDTH_MOD 350.0f // Line width modifier to use to calculate scrolling speed.
#include "GuiComponent.h"
class ScrollableContainer : public GuiComponent
@ -20,6 +25,8 @@ public:
Vector2f getScrollPos() const;
void setScrollPos(const Vector2f& pos);
void setAutoScroll(bool autoScroll);
void setScrollParameters(float autoScrollResetDelayConstant, float autoScrollDelayConstant,
int autoScrollSpeedConstant, float autoWidthModConstant) override;
void reset();
void update(int deltaTime) override;
@ -30,6 +37,12 @@ private:
Vector2f mScrollPos;
Vector2f mScrollDir;
float mAutoScrollResetDelayConstant;
float mAutoScrollDelayConstant;
int mAutoScrollSpeedConstant;
float mAutoWidthModConstant;
float mResolutionModifier;
int mAutoScrollDelay;
int mAutoScrollSpeed;