From dedfcfea4c5c69b6b3c4c5e46470650bc61951d4 Mon Sep 17 00:00:00 2001 From: Aloshi Date: Sat, 19 Apr 2014 13:37:10 -0500 Subject: [PATCH] Split busy animation into its own component for reusability. Changed the design a bit. The ScraperSearchComponent now grays out entirely + displays the busy animation in the center. --- CMakeLists.txt | 2 + src/components/AnimatedImageComponent.cpp | 10 ---- src/components/AnimatedImageComponent.h | 4 -- src/components/BusyComponent.cpp | 58 +++++++++++++++++++++++ src/components/BusyComponent.h | 23 +++++++++ src/components/ScraperSearchComponent.cpp | 37 ++++----------- src/components/ScraperSearchComponent.h | 5 +- 7 files changed, 95 insertions(+), 44 deletions(-) create mode 100644 src/components/BusyComponent.cpp create mode 100644 src/components/BusyComponent.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f30355664..b610736c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -164,6 +164,7 @@ set(ES_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/src/components/AnimatedImageComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/AsyncReqComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ButtonComponent.h + ${CMAKE_CURRENT_SOURCE_DIR}/src/components/BusyComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ComponentGrid.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ComponentList.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/DateTimeComponent.h @@ -255,6 +256,7 @@ set(ES_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/components/AnimatedImageComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/AsyncReqComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ButtonComponent.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/components/BusyComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ComponentGrid.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ComponentList.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/DateTimeComponent.cpp diff --git a/src/components/AnimatedImageComponent.cpp b/src/components/AnimatedImageComponent.cpp index deb3d584a..aef223198 100644 --- a/src/components/AnimatedImageComponent.cpp +++ b/src/components/AnimatedImageComponent.cpp @@ -2,16 +2,6 @@ #include "ImageComponent.h" #include "../Log.h" -// animation definitions because there's only one right now and i'm too lazy to make another file -AnimationFrame BUSY_ANIMATION_FRAMES[] = { - {":/busy_0.svg", 500}, - {":/busy_1.svg", 500}, - {":/busy_2.svg", 500}, - {":/busy_3.svg", 500}, -}; -const AnimationDef BUSY_ANIMATION_DEF = { BUSY_ANIMATION_FRAMES, 4, true }; - - AnimatedImageComponent::AnimatedImageComponent(Window* window) : GuiComponent(window), mEnabled(false) { } diff --git a/src/components/AnimatedImageComponent.h b/src/components/AnimatedImageComponent.h index fc44034e0..5d6c988b3 100644 --- a/src/components/AnimatedImageComponent.h +++ b/src/components/AnimatedImageComponent.h @@ -39,7 +39,3 @@ private: int mFrameAccumulator; int mCurrentFrame; }; - - -// animation declarations because there's only one right now and I'm too lazy to make another file -extern const AnimationDef BUSY_ANIMATION_DEF; diff --git a/src/components/BusyComponent.cpp b/src/components/BusyComponent.cpp new file mode 100644 index 000000000..003aa51b8 --- /dev/null +++ b/src/components/BusyComponent.cpp @@ -0,0 +1,58 @@ +#include "BusyComponent.h" + +#include "AnimatedImageComponent.h" +#include "TextComponent.h" +#include "../Renderer.h" + +// animation definition +AnimationFrame BUSY_ANIMATION_FRAMES[] = { + {":/busy_0.svg", 300}, + {":/busy_1.svg", 300}, + {":/busy_2.svg", 300}, + {":/busy_3.svg", 300}, +}; +const AnimationDef BUSY_ANIMATION_DEF = { BUSY_ANIMATION_FRAMES, 4, true }; + +using namespace Eigen; + +BusyComponent::BusyComponent(Window* window) : GuiComponent(window), + mBackground(window, ":/frame.png"), mGrid(window, Vector2i(5, 3)) +{ + mAnimation = std::make_shared(mWindow); + mAnimation->load(&BUSY_ANIMATION_DEF); + mText = std::make_shared(mWindow, "WORKING...", Font::get(FONT_SIZE_MEDIUM), 0x777777FF); + + // col 0 = animation, col 1 = spacer, col 2 = text + mGrid.setEntry(mAnimation, Vector2i(1, 1), false, true); + mGrid.setEntry(mText, Vector2i(3, 1), false, true); + + addChild(&mBackground); + addChild(&mGrid); +} + +void BusyComponent::onSizeChanged() +{ + mGrid.setSize(mSize); + + if(mSize.x() == 0 || mSize.y() == 0) + return; + + const float middleSpacerWidth = 0.01f * Renderer::getScreenWidth(); + const float textHeight = mText->getFont()->getLetterHeight(); + mText->setSize(0, textHeight); + const float textWidth = mText->getSize().x(); + + mGrid.setColWidthPerc(1, textHeight / mSize.x()); // animation is square + mGrid.setColWidthPerc(2, middleSpacerWidth / mSize.x()); + mGrid.setColWidthPerc(3, textWidth / mSize.x()); + + mGrid.setRowHeightPerc(1, textHeight / mSize.y()); + + mBackground.fitTo(Vector2f(mGrid.getColWidth(1) + mGrid.getColWidth(2) + mGrid.getColWidth(3), textHeight + 2), + mAnimation->getPosition(), Vector2f(0, 0)); +} + +void BusyComponent::reset() +{ + mAnimation->reset(); +} diff --git a/src/components/BusyComponent.h b/src/components/BusyComponent.h new file mode 100644 index 000000000..44d6d4d2e --- /dev/null +++ b/src/components/BusyComponent.h @@ -0,0 +1,23 @@ +#include "../GuiComponent.h" +#include "ComponentGrid.h" +#include "NinePatchComponent.h" + +class AnimatedImageComponent; +class TextComponent; + +class BusyComponent : public GuiComponent +{ +public: + BusyComponent(Window* window); + + void onSizeChanged() override; + + void reset(); // reset to frame 0 + +private: + NinePatchComponent mBackground; + ComponentGrid mGrid; + + std::shared_ptr mAnimation; + std::shared_ptr mText; +}; diff --git a/src/components/ScraperSearchComponent.cpp b/src/components/ScraperSearchComponent.cpp index 794b59a3e..ef8c95ad7 100644 --- a/src/components/ScraperSearchComponent.cpp +++ b/src/components/ScraperSearchComponent.cpp @@ -15,7 +15,7 @@ #include "../guis/GuiTextEditPopup.h" ScraperSearchComponent::ScraperSearchComponent(Window* window, SearchType type) : GuiComponent(window), - mGrid(window, Eigen::Vector2i(4, 3)), mBusyGrid(window, Eigen::Vector2i(3, 1)), + mGrid(window, Eigen::Vector2i(4, 3)), mBusyAnim(window), mSearchType(type) { addChild(&mGrid); @@ -74,14 +74,6 @@ ScraperSearchComponent::ScraperSearchComponent(Window* window, SearchType type) mResultList = std::make_shared(mWindow); mResultList->setCursorChangedCallback([this](CursorState state) { if(state == CURSOR_STOPPED) updateInfoPane(); }); - mBusyAnimation = std::make_shared(mWindow); - mBusyAnimation->load(&BUSY_ANIMATION_DEF); - mBusyText = std::make_shared(mWindow, "WORKING...", Font::get(FONT_SIZE_LARGE), 0x777777FF); - - // col 0 = animation, col 1 = spacer, col 2 = text - mBusyGrid.setEntry(mBusyAnimation, Vector2i(0, 0), false, true); - mBusyGrid.setEntry(mBusyText, Vector2i(2, 0), false, true); - updateViewStyle(); } @@ -152,21 +144,11 @@ void ScraperSearchComponent::onSizeChanged() mDescContainer->setSize(mGrid.getColWidth(1)*boxartCellScale + mGrid.getColWidth(2), mResultDesc->getFont()->getHeight() * 3); mResultDesc->setSize(mDescContainer->getSize().x(), 0); // make desc text wrap at edge of container - const float busyGridHeight = mBusyText->getSize().y(); - const float busyGridWidth = (busyGridHeight + mBusyText->getSize().x()) * 1.03f; - if(busyGridWidth > 0 && busyGridHeight > 0) - { - mBusyGrid.setSize(busyGridWidth, busyGridHeight); - - mBusyGrid.setColWidthPerc(0, (busyGridHeight) / busyGridWidth); - mBusyGrid.setColWidthPerc(1, 0.025f); - - // in the far right - mBusyGrid.setPosition(mGrid.getColWidth(0) + mGrid.getColWidth(1) + mGrid.getColWidth(2) + (mGrid.getColWidth(3) - busyGridWidth)/2, - mGrid.getRowHeight(0) + mGrid.getRowHeight(1) + (mGrid.getRowHeight(2) - busyGridHeight) / 2); - } - mGrid.onSizeChanged(); + + mBusyAnim.setSize(mSize); + //mBusyAnim.setPosition(mSearchType == ALWAYS_ACCEPT_FIRST_RESULT ? mDescContainer->getPosition() : mResultList->getPosition()); + //mBusyAnim.setSize(mSearchType == ALWAYS_ACCEPT_FIRST_RESULT ? mDescContainer->getSize() : mResultList->getSize()); } void ScraperSearchComponent::updateViewStyle() @@ -351,10 +333,11 @@ void ScraperSearchComponent::render(const Eigen::Affine3f& parentTrans) if(mBlockAccept) { Renderer::setMatrix(trans); - Renderer::drawRect((int)mResultList->getPosition().x(), (int)mResultList->getPosition().y(), - (int)mResultList->getSize().x(), (int)mResultList->getSize().y(), 0x00000011); + Renderer::drawRect(0.f, 0.f, mSize.x(), mSize.y(), 0x00000011); + //Renderer::drawRect((int)mResultList->getPosition().x(), (int)mResultList->getPosition().y(), + // (int)mResultList->getSize().x(), (int)mResultList->getSize().y(), 0x00000011); - mBusyGrid.render(trans); + mBusyAnim.render(trans); } } @@ -378,7 +361,7 @@ void ScraperSearchComponent::update(int deltaTime) if(mBlockAccept) { - mBusyAnimation->update(deltaTime); + mBusyAnim.update(deltaTime); } if(mThumbnailReq && mThumbnailReq->status() != HttpReq::REQ_IN_PROGRESS) diff --git a/src/components/ScraperSearchComponent.h b/src/components/ScraperSearchComponent.h index a4cfb9b74..c943aa548 100644 --- a/src/components/ScraperSearchComponent.h +++ b/src/components/ScraperSearchComponent.h @@ -3,6 +3,7 @@ #include "../GuiComponent.h" #include "../scrapers/Scraper.h" #include "../components/ComponentGrid.h" +#include "../components/BusyComponent.h" #include #define MAX_SCRAPER_RESULTS 7 @@ -98,7 +99,5 @@ private: std::vector mScraperResults; std::unique_ptr mThumbnailReq; - ComponentGrid mBusyGrid; - std::shared_ptr mBusyAnimation; - std::shared_ptr mBusyText; + BusyComponent mBusyAnim; };