From 0d39586c2de85c53bd99ed8ab49c4a2792e5e8e9 Mon Sep 17 00:00:00 2001 From: Aloshi Date: Sun, 18 May 2014 20:15:15 -0500 Subject: [PATCH] Adjusted ScrollableContainer autoscroll. Scroll delay and speed are now hardcoded instead of arguments. Changed resetAutoscroll() to just reset(), which now also resets cursor position. Autoscroll now automatically returns to the top (via reset()) after 10 seconds. --- src/components/ScraperSearchComponent.cpp | 5 +- src/components/ScrollableContainer.cpp | 83 +++++++++++++-------- src/components/ScrollableContainer.h | 20 ++--- src/views/gamelist/DetailedGameListView.cpp | 5 +- 4 files changed, 65 insertions(+), 48 deletions(-) diff --git a/src/components/ScraperSearchComponent.cpp b/src/components/ScraperSearchComponent.cpp index a097550f2..4fed43c8f 100644 --- a/src/components/ScraperSearchComponent.cpp +++ b/src/components/ScraperSearchComponent.cpp @@ -38,7 +38,7 @@ ScraperSearchComponent::ScraperSearchComponent(Window* window, SearchType type) mDescContainer = std::make_shared(mWindow); mResultDesc = std::make_shared(mWindow, "Result desc", Font::get(FONT_SIZE_SMALL), 0x777777FF); mDescContainer->addChild(mResultDesc.get()); - mDescContainer->setAutoScroll(2200, 0.015f); + mDescContainer->setAutoScroll(true); // metadata auto font = Font::get(FONT_SIZE_SMALL); // this gets replaced in onSizeChanged() so its just a placeholder @@ -300,8 +300,7 @@ void ScraperSearchComponent::updateInfoPane() ScraperSearchResult& res = mScraperResults.at(i); mResultName->setText(strToUpper(res.mdl.get("name"))); mResultDesc->setText(strToUpper(res.mdl.get("desc"))); - mDescContainer->setScrollPos(Eigen::Vector2d(0, 0)); - mDescContainer->resetAutoScrollTimer(); + mDescContainer->reset(); mResultThumbnail->setImage(""); const std::string& thumb = res.thumbnailUrl.empty() ? res.imageUrl : res.thumbnailUrl; diff --git a/src/components/ScrollableContainer.cpp b/src/components/ScrollableContainer.cpp index 87e948c78..f6b41072e 100644 --- a/src/components/ScrollableContainer.cpp +++ b/src/components/ScrollableContainer.cpp @@ -2,8 +2,12 @@ #include "../Renderer.h" #include "../Log.h" +#define AUTO_SCROLL_RESET_DELAY 10000 // ms to reset to top after we reach the bottom +#define AUTO_SCROLL_DELAY 8000 // ms to wait before we start to scroll +#define AUTO_SCROLL_SPEED 50 // ms between scrolls + ScrollableContainer::ScrollableContainer(Window* window) : GuiComponent(window), - mAutoScrollDelay(0), mAutoScrollSpeed(0), mAutoScrollTimer(0), mScrollPos(0, 0), mScrollDir(0, 0) + mAutoScrollDelay(0), mAutoScrollSpeed(0), mAutoScrollAccumulator(0), mScrollPos(0, 0), mScrollDir(0, 0), mAutoScrollResetAccumulator(0) { } @@ -18,7 +22,7 @@ void ScrollableContainer::render(const Eigen::Affine3f& parentTrans) Renderer::pushClipRect(clipPos, clipDim); - trans.translate(Eigen::Vector3f((float)-mScrollPos.x(), (float)-mScrollPos.y(), 0)); + trans.translate(-Eigen::Vector3f(mScrollPos.x(), mScrollPos.y(), 0)); Renderer::setMatrix(trans); GuiComponent::renderChildren(trans); @@ -26,65 +30,75 @@ void ScrollableContainer::render(const Eigen::Affine3f& parentTrans) Renderer::popClipRect(); } -void ScrollableContainer::setAutoScroll(int delay, double speed) +void ScrollableContainer::setAutoScroll(bool autoScroll) { - mAutoScrollDelay = delay; - mAutoScrollSpeed = speed; - mAutoScrollTimer = 0; + if(autoScroll) + { + mScrollDir << 0, 1; + mAutoScrollDelay = AUTO_SCROLL_DELAY; + mAutoScrollSpeed = AUTO_SCROLL_SPEED; + reset(); + }else{ + mScrollDir << 0, 0; + mAutoScrollDelay = 0; + mAutoScrollSpeed = 0; + mAutoScrollAccumulator = 0; + } } -Eigen::Vector2d ScrollableContainer::getScrollPos() const +Eigen::Vector2f ScrollableContainer::getScrollPos() const { return mScrollPos; } -void ScrollableContainer::setScrollPos(const Eigen::Vector2d& pos) +void ScrollableContainer::setScrollPos(const Eigen::Vector2f& pos) { mScrollPos = pos; } void ScrollableContainer::update(int deltaTime) { - double scrollAmt = (double)deltaTime; - if(mAutoScrollSpeed != 0) { - mAutoScrollTimer += deltaTime; + mAutoScrollAccumulator += deltaTime; - scrollAmt = (float)(mAutoScrollTimer - mAutoScrollDelay); - - if(scrollAmt > 0) + //scale speed by our width! more text per line = slower scrolling + const float widthMod = (680.0f / getSize().x()); + while(mAutoScrollAccumulator >= mAutoScrollSpeed) { - //scroll the amount of time left over from the delay - mAutoScrollTimer = mAutoScrollDelay; - - //scale speed by our width! more text per line = slower scrolling - const double widthMod = (680.0 / getSize().x()); - mScrollDir = Eigen::Vector2d(0, mAutoScrollSpeed * widthMod); - }else{ - //not enough to pass the delay, do nothing - scrollAmt = 0; + mScrollPos += mScrollDir; + mAutoScrollAccumulator -= mAutoScrollSpeed; } } - Eigen::Vector2d scroll = mScrollDir * scrollAmt; - mScrollPos += scroll; - //clip scrolling within bounds if(mScrollPos.x() < 0) mScrollPos[0] = 0; if(mScrollPos.y() < 0) mScrollPos[1] = 0; - - Eigen::Vector2f contentSize = getContentSize(); + const Eigen::Vector2f contentSize = getContentSize(); if(mScrollPos.x() + getSize().x() > contentSize.x()) - mScrollPos[0] = (double)contentSize.x() - getSize().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] = (double)contentSize.y() - getSize().y(); + }else if(mScrollPos.y() + getSize().y() > contentSize.y()) + { + mScrollPos[1] = contentSize.y() - getSize().y(); + mAtEnd = true; + } + + if(mAtEnd) + { + mAutoScrollResetAccumulator += deltaTime; + if(mAutoScrollResetAccumulator >= AUTO_SCROLL_RESET_DELAY) + reset(); + } GuiComponent::update(deltaTime); } @@ -106,7 +120,10 @@ Eigen::Vector2f ScrollableContainer::getContentSize() return max; } -void ScrollableContainer::resetAutoScrollTimer() +void ScrollableContainer::reset() { - mAutoScrollTimer = 0; + mScrollPos << 0, 0; + mAutoScrollResetAccumulator = 0; + mAutoScrollAccumulator = -mAutoScrollDelay + mAutoScrollSpeed; + mAtEnd = false; } diff --git a/src/components/ScrollableContainer.h b/src/components/ScrollableContainer.h index eea48888e..486a64595 100644 --- a/src/components/ScrollableContainer.h +++ b/src/components/ScrollableContainer.h @@ -7,10 +7,10 @@ class ScrollableContainer : public GuiComponent public: ScrollableContainer(Window* window); - Eigen::Vector2d getScrollPos() const; - void setScrollPos(const Eigen::Vector2d& pos); - void setAutoScroll(int delay, double speed); //Use 0 for speed to disable. - void resetAutoScrollTimer(); + Eigen::Vector2f getScrollPos() const; + void setScrollPos(const Eigen::Vector2f& pos); + void setAutoScroll(bool autoScroll); + void reset(); void update(int deltaTime) override; void render(const Eigen::Affine3f& parentTrans) override; @@ -18,9 +18,11 @@ public: private: Eigen::Vector2f getContentSize(); - Eigen::Vector2d mScrollPos; - Eigen::Vector2d mScrollDir; - int mAutoScrollDelay; - double mAutoScrollSpeed; - int mAutoScrollTimer; + Eigen::Vector2f mScrollPos; + Eigen::Vector2f mScrollDir; + int mAutoScrollDelay; // ms to wait before starting to autoscroll + int mAutoScrollSpeed; // ms to wait before scrolling down by mScrollDir + int mAutoScrollAccumulator; + bool mAtEnd; + int mAutoScrollResetAccumulator; }; diff --git a/src/views/gamelist/DetailedGameListView.cpp b/src/views/gamelist/DetailedGameListView.cpp index a87492a7c..72eaaa511 100644 --- a/src/views/gamelist/DetailedGameListView.cpp +++ b/src/views/gamelist/DetailedGameListView.cpp @@ -58,7 +58,7 @@ DetailedGameListView::DetailedGameListView(Window* window, FileData* root) : mDescContainer.setPosition(mSize.x() * padding, mSize.y() * 0.65f); mDescContainer.setSize(mSize.x() * (0.50f - 2*padding), mSize.y() - mDescContainer.getPosition().y()); - mDescContainer.setAutoScroll((int)(1600 + mDescContainer.getSize().x()), 0.025f); + mDescContainer.setAutoScroll(true); addChild(&mDescContainer); mDescription.setFont(Font::get(FONT_SIZE_SMALL)); @@ -199,8 +199,7 @@ void DetailedGameListView::updateInfoPanel() mPlayCount.setValue(file->metadata.get("playcount")); mDescription.setText(file->metadata.get("desc")); - mDescContainer.resetAutoScrollTimer(); - mDescContainer.setScrollPos(Eigen::Vector2d(0, 0)); + mDescContainer.reset(); fadingOut = false; }