From 719483864c87cd701bd164d13cb308584fba7053 Mon Sep 17 00:00:00 2001 From: Aloshi Date: Mon, 24 Mar 2014 17:55:36 -0500 Subject: [PATCH] Made ScraperSearchComponent use ComponentList callbacks instead of manually intercepting input. Tweaked IList and ComponentList for this. --- src/components/ComponentList.cpp | 33 +++++++++++++-------- src/components/ComponentList.h | 6 ++++ src/components/IList.h | 13 +++++++- src/components/ScraperSearchComponent.cpp | 36 ++++++++++------------- src/guis/GuiScraperMulti.cpp | 28 ++++++++++++------ src/guis/GuiScraperMulti.h | 1 + 6 files changed, 74 insertions(+), 43 deletions(-) diff --git a/src/components/ComponentList.cpp b/src/components/ComponentList.cpp index eccfcca2f..f5eaeea35 100644 --- a/src/components/ComponentList.cpp +++ b/src/components/ComponentList.cpp @@ -1,5 +1,6 @@ #include "ComponentList.h" #include "../Util.h" +#include "../Log.h" #define TOTAL_HORIZONTAL_PADDING_PX 20 @@ -40,7 +41,7 @@ void ComponentList::onSizeChanged() updateElementPosition(it->data); } - onCursorChanged(mScrollVelocity != 0 ? CURSOR_SCROLLING : CURSOR_STOPPED); + updateCameraOffset(); } void ComponentList::onFocusLost() @@ -107,6 +108,25 @@ void ComponentList::onCursorChanged(const CursorState& state) mSelectorBarOffset += getRowHeight(mEntries.at(i).data); } + updateCameraOffset(); + + // this is terribly inefficient but we don't know what we came from so... + if(size()) + { + for(auto it = mEntries.begin(); it != mEntries.end(); it++) + it->data.elements.back().component->onFocusLost(); + + mEntries.at(mCursor).data.elements.back().component->onFocusGained(); + } + + if(mCursorChangedCallback) + mCursorChangedCallback(state); + + updateHelpPrompts(); +} + +void ComponentList::updateCameraOffset() +{ // move the camera to scroll const float totalHeight = getTotalRowHeight(); if(totalHeight > mSize.y()) @@ -129,17 +149,6 @@ void ComponentList::onCursorChanged(const CursorState& state) }else{ mCameraOffset = 0; } - - // this is terribly inefficient but we don't know what we came from so... - if(size()) - { - for(auto it = mEntries.begin(); it != mEntries.end(); it++) - it->data.elements.back().component->onFocusLost(); - - mEntries.at(mCursor).data.elements.back().component->onFocusGained(); - } - - updateHelpPrompts(); } void ComponentList::render(const Eigen::Affine3f& parentTrans) diff --git a/src/components/ComponentList.h b/src/components/ComponentList.h index 247f3884d..99f06a4ec 100644 --- a/src/components/ComponentList.h +++ b/src/components/ComponentList.h @@ -65,12 +65,16 @@ public: float getTotalRowHeight() const; inline float getRowHeight(int row) const { return getRowHeight(mEntries.at(row).data); } + inline void setCursorChangedCallback(const std::function& callback) { mCursorChangedCallback = callback; }; + inline const std::function& getCursorChangedCallback() const { return mCursorChangedCallback; }; + protected: void onCursorChanged(const CursorState& state) override; private: bool mFocused; + void updateCameraOffset(); void updateElementPosition(const ComponentListRow& row); void updateElementSize(const ComponentListRow& row); @@ -78,4 +82,6 @@ private: float mSelectorBarOffset; float mCameraOffset; + + std::function mCursorChangedCallback; }; diff --git a/src/components/IList.h b/src/components/IList.h index 5b4c7f806..683cae0f3 100644 --- a/src/components/IList.h +++ b/src/components/IList.h @@ -185,6 +185,10 @@ protected: bool listInput(int velocity) // a velocity of 0 = stop scrolling { + // generate an onCursorChanged event in the stopped state when the user lets go of the key + if(velocity == 0 && mScrollVelocity != 0) + onCursorChanged(CURSOR_STOPPED); + mScrollVelocity = velocity; mScrollTier = 0; mScrollTierAccumulator = 0; @@ -268,9 +272,16 @@ protected: mLoopType == LIST_NEVER_LOOP) { if(cursor < 0) + { cursor = 0; - else if(cursor >= size()) + mScrollVelocity = 0; + mScrollTier = 0; + }else if(cursor >= size()) + { cursor = size() - 1; + mScrollVelocity = 0; + mScrollTier = 0; + } }else{ while(cursor < 0) cursor += size(); diff --git a/src/components/ScraperSearchComponent.cpp b/src/components/ScraperSearchComponent.cpp index ccfb60965..d7e109383 100644 --- a/src/components/ScraperSearchComponent.cpp +++ b/src/components/ScraperSearchComponent.cpp @@ -72,7 +72,8 @@ ScraperSearchComponent::ScraperSearchComponent(Window* window, SearchType type) // result list mResultList = std::make_shared(mWindow); - + mResultList->setCursorChangedCallback([this](CursorState state) { if(state == CURSOR_STOPPED) updateInfoPane(); }); + updateViewStyle(); } @@ -183,7 +184,11 @@ void ScraperSearchComponent::onSearchDone(const std::vector if(end == 0) { ComponentListRow row; - row.addElement(std::make_shared(mWindow, "No games found!", font, color), true); + row.addElement(std::make_shared(mWindow, "NO GAMES FOUND - SKIP", font, color), true); + + if(mSkipCallback) + row.makeAcceptInputHandler(mSkipCallback); + mResultList->addRow(row); mGrid.resetCursor(); }else{ @@ -191,7 +196,8 @@ void ScraperSearchComponent::onSearchDone(const std::vector for(int i = 0; i < end; i++) { row.elements.clear(); - row.addElement(std::make_shared(mWindow, results.at(i).mdl.get("name"), font, color), true); + row.addElement(std::make_shared(mWindow, strToUpper(results.at(i).mdl.get("name")), font, color), true); + row.makeAcceptInputHandler([this, i] { returnResult(mScraperResults.at(i)); }); mResultList->addRow(row); } mGrid.resetCursor(); @@ -222,7 +228,7 @@ void ScraperSearchComponent::onSearchError(const std::string& error) int ScraperSearchComponent::getSelectedIndex() { - if(mScraperResults.size() && mGrid.getSelectedComponent() != mResultList) + if(!mScraperResults.size() || mGrid.getSelectedComponent() != mResultList) return -1; return mResultList->getCursorId(); @@ -242,9 +248,11 @@ void ScraperSearchComponent::updateInfoPane() mResultThumbnail->setImage(""); const std::string& thumb = res.thumbnailUrl.empty() ? res.imageUrl : res.thumbnailUrl; if(!thumb.empty()) + { mThumbnailReq = std::unique_ptr(new HttpReq(thumb)); - else + }else{ mThumbnailReq.reset(); + } // metadata mMD_Rating->setValue(strToUpper(res.mdl.get("rating"))); @@ -275,23 +283,9 @@ bool ScraperSearchComponent::input(InputConfig* config, Input input) { if(mBlockAccept) return true; - - //if you're on a result - if(getSelectedIndex() != -1) - { - returnResult(mScraperResults.at(getSelectedIndex())); - return true; - } } - bool ret = GuiComponent::input(config, input); - - if(config->isMappedTo("up", input) || config->isMappedTo("down", input) && input.value != 0) - { - updateInfoPane(); - } - - return ret; + return GuiComponent::input(config, input); } void ScraperSearchComponent::render(const Eigen::Affine3f& parentTrans) @@ -366,13 +360,13 @@ void ScraperSearchComponent::updateThumbnail() { std::string content = mThumbnailReq->getContent(); mResultThumbnail->setImage(content.data(), content.length()); + mGrid.onSizeChanged(); // a hack to fix the thumbnail position since its size changed }else{ LOG(LogWarning) << "thumbnail req failed: " << mThumbnailReq->getErrorMsg(); mResultThumbnail->setImage(""); } mThumbnailReq.reset(); - mGrid.onSizeChanged(); // a hack to fix the thumbnail position since its size changed } void ScraperSearchComponent::openInputScreen(ScraperSearchParams& params) diff --git a/src/guis/GuiScraperMulti.cpp b/src/guis/GuiScraperMulti.cpp index a5017113e..2e7f22a54 100644 --- a/src/guis/GuiScraperMulti.cpp +++ b/src/guis/GuiScraperMulti.cpp @@ -1,5 +1,6 @@ #include "GuiScraperMulti.h" #include "../Renderer.h" +#include "../Log.h" #include "../components/TextComponent.h" #include "../components/ButtonComponent.h" @@ -10,7 +11,7 @@ using namespace Eigen; GuiScraperMulti::GuiScraperMulti(Window* window, const std::queue& searches, bool approveResults) : - GuiComponent(window), mBackground(window, ":/frame.png"), mGrid(window, Vector2i(1, 4)), + GuiComponent(window), mBackground(window, ":/frame.png"), mGrid(window, Vector2i(1, 5)), mSearchQueue(searches) { addChild(&mBackground); @@ -20,17 +21,21 @@ GuiScraperMulti::GuiScraperMulti(Window* window, const std::queue(mWindow, "SCRAPING IN PROGRESS", Font::get(FONT_SIZE_SMALL), 0x777777FF, TextComponent::ALIGN_CENTER); + mTitle = std::make_shared(mWindow, "SCRAPING IN PROGRESS", Font::get(FONT_SIZE_LARGE), 0x777777FF, TextComponent::ALIGN_CENTER); mGrid.setEntry(mTitle, Vector2i(0, 0), false, true); + mSystem = std::make_shared(mWindow, "SYSTEM", Font::get(FONT_SIZE_MEDIUM), 0x777777FF, TextComponent::ALIGN_CENTER); + mGrid.setEntry(mSystem, Vector2i(0, 1), false, true); + mSubtitle = std::make_shared(mWindow, "subtitle text", Font::get(FONT_SIZE_SMALL), 0x888888FF, TextComponent::ALIGN_CENTER); - mGrid.setEntry(mSubtitle, Vector2i(0, 1), false, true); + mGrid.setEntry(mSubtitle, Vector2i(0, 2), false, true); mSearchComp = std::make_shared(mWindow, approveResults ? ScraperSearchComponent::ALWAYS_ACCEPT_MATCHING_CRC : ScraperSearchComponent::ALWAYS_ACCEPT_FIRST_RESULT); mSearchComp->setAcceptCallback(std::bind(&GuiScraperMulti::acceptResult, this, std::placeholders::_1)); + mSearchComp->setSkipCallback(std::bind(&GuiScraperMulti::skip, this)); mSearchComp->setCancelCallback(std::bind(&GuiScraperMulti::finish, this)); - mGrid.setEntry(mSearchComp, Vector2i(0, 2), approveResults, true); + mGrid.setEntry(mSearchComp, Vector2i(0, 3), approveResults, true); std::vector< std::shared_ptr > buttons; buttons.push_back(std::make_shared(mWindow, "INPUT", "search", [&] { @@ -40,7 +45,7 @@ GuiScraperMulti::GuiScraperMulti(Window* window, const std::queue(mWindow, "SKIP", "skip", std::bind(&GuiScraperMulti::skip, this))); buttons.push_back(std::make_shared(mWindow, "STOP", "stop (progress saved)", std::bind(&GuiScraperMulti::finish, this))); mButtonGrid = makeButtonGrid(mWindow, buttons); - mGrid.setEntry(mButtonGrid, Vector2i(0, 3), true, false); + mGrid.setEntry(mButtonGrid, Vector2i(0, 4), true, false); setSize(Renderer::getScreenWidth() * 0.7f, Renderer::getScreenHeight() * 0.65f); setPosition((Renderer::getScreenWidth() - mSize.x()) / 2, (Renderer::getScreenHeight() - mSize.y()) / 2); @@ -54,8 +59,9 @@ void GuiScraperMulti::onSizeChanged() mGrid.setSize(mSize); mGrid.setRowHeightPerc(0, mTitle->getFont()->getHeight() / mGrid.getSize().y()); - mGrid.setRowHeightPerc(1, mSubtitle->getFont()->getHeight() / mGrid.getSize().y()); - mGrid.setRowHeightPerc(3, mButtonGrid->getSize().y() / mGrid.getSize().y()); + mGrid.setRowHeightPerc(1, mSystem->getFont()->getHeight() / mGrid.getSize().y()); + mGrid.setRowHeightPerc(2, mSubtitle->getFont()->getHeight() / mGrid.getSize().y()); + mGrid.setRowHeightPerc(4, mButtonGrid->getSize().y() / mGrid.getSize().y()); } void GuiScraperMulti::doNextSearch() @@ -66,9 +72,13 @@ void GuiScraperMulti::doNextSearch() return; } - // update subtitle + // update title std::stringstream ss; - ss << "GAME " << (mCurrentGame + 1) << " OF " << mTotalGames; + mSystem->setText(strToUpper(mSearchQueue.front().system->getName())); + + // update subtitle + ss.str(""); // clear + ss << "GAME " << (mCurrentGame + 1) << " OF " << mTotalGames << " - " << strToUpper(mSearchQueue.front().game->getPath().filename().string()); mSubtitle->setText(ss.str()); mSearchComp->search(mSearchQueue.front()); diff --git a/src/guis/GuiScraperMulti.h b/src/guis/GuiScraperMulti.h index 1f9919a89..dd2b51c08 100644 --- a/src/guis/GuiScraperMulti.h +++ b/src/guis/GuiScraperMulti.h @@ -33,6 +33,7 @@ private: ComponentGrid mGrid; std::shared_ptr mTitle; + std::shared_ptr mSystem; std::shared_ptr mSubtitle; std::shared_ptr mSearchComp; std::shared_ptr mButtonGrid;