diff --git a/es-core/src/GuiComponent.h b/es-core/src/GuiComponent.h index 26e3a2ce4..703b10618 100644 --- a/es-core/src/GuiComponent.h +++ b/es-core/src/GuiComponent.h @@ -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() {}; diff --git a/es-core/src/components/ScrollableContainer.cpp b/es-core/src/components/ScrollableContainer.cpp index 980d87906..a58dc4b4e 100644 --- a/es-core/src/components/ScrollableContainer.cpp +++ b/es-core/src/components/ScrollableContainer.cpp @@ -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(Renderer::getScreenWidth()) / 1920.0f; -} -void ScrollableContainer::render(const Transform4x4f& parentTrans) -{ - if (!isVisible()) - return; - - Transform4x4f trans = parentTrans * getTransform(); - - Vector2i clipPos(static_cast(trans.translation().x()), - static_cast(trans.translation().y())); - - Vector3f dimScaled = trans * Vector3f(mSize.x(), mSize.y(), 0); - Vector2i clipDim(static_cast((dimScaled.x()) - trans.translation().x()), - static_cast((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(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(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(AUTO_SCROLL_RESET_DELAY * widthMod)) { + if (mAutoScrollResetAccumulator >= + static_cast(mAutoScrollResetDelayConstant * widthMod)) { // Fade in the text as it resets to the start position. auto func = [this](float t) { this->setOpacity(static_cast(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(trans.translation().x()), + static_cast(trans.translation().y())); + + Vector3f dimScaled = trans * Vector3f(mSize.x(), mSize.y(), 0); + Vector2i clipDim(static_cast((dimScaled.x()) - trans.translation().x()), + static_cast((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; -} diff --git a/es-core/src/components/ScrollableContainer.h b/es-core/src/components/ScrollableContainer.h index 0a023869f..109c03fcc 100644 --- a/es-core/src/components/ScrollableContainer.h +++ b/es-core/src/components/ScrollableContainer.h @@ -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;