From 6d4288f245347f4ebc075ed5a83a314b04aa34dd Mon Sep 17 00:00:00 2001
From: Aloshi <allofquist@yahoo.com>
Date: Fri, 18 Apr 2014 19:46:55 -0500
Subject: [PATCH] Added busy animation + "WORKING..." to the
 ScraperSearchComponent.

---
 src/components/AnimatedImageComponent.cpp | 10 ++++---
 src/components/AnimatedImageComponent.h   |  4 ++-
 src/components/ScraperSearchComponent.cpp | 32 ++++++++++++++++++++++-
 src/components/ScraperSearchComponent.h   |  6 ++++-
 4 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/src/components/AnimatedImageComponent.cpp b/src/components/AnimatedImageComponent.cpp
index 665045df7..deb3d584a 100644
--- a/src/components/AnimatedImageComponent.cpp
+++ b/src/components/AnimatedImageComponent.cpp
@@ -12,10 +12,8 @@ AnimationFrame BUSY_ANIMATION_FRAMES[] = {
 const AnimationDef BUSY_ANIMATION_DEF = { BUSY_ANIMATION_FRAMES, 4, true };
 
 
-AnimatedImageComponent::AnimatedImageComponent(Window* window, const AnimationDef* def) : GuiComponent(window), mEnabled(false)
+AnimatedImageComponent::AnimatedImageComponent(Window* window) : GuiComponent(window), mEnabled(false)
 {
-	if(def)
-		load(def);
 }
 
 void AnimatedImageComponent::load(const AnimationDef* def)
@@ -46,6 +44,12 @@ void AnimatedImageComponent::load(const AnimationDef* def)
 	mEnabled = true;
 }
 
+void AnimatedImageComponent::reset()
+{
+	mCurrentFrame = 0;
+	mFrameAccumulator = 0;
+}
+
 void AnimatedImageComponent::onSizeChanged()
 {
 	for(auto it = mFrames.begin(); it != mFrames.end(); it++)
diff --git a/src/components/AnimatedImageComponent.h b/src/components/AnimatedImageComponent.h
index a49bc0804..fc44034e0 100644
--- a/src/components/AnimatedImageComponent.h
+++ b/src/components/AnimatedImageComponent.h
@@ -18,10 +18,12 @@ struct AnimationDef
 class AnimatedImageComponent : public GuiComponent
 {
 public:
-	AnimatedImageComponent(Window* window, const AnimationDef* def = NULL);
+	AnimatedImageComponent(Window* window);
 	
 	void load(const AnimationDef* def); // no reference to def is kept after loading is complete
 
+	void reset(); // set to frame 0
+
 	void update(int deltaTime) override;
 	void render(const Eigen::Affine3f& trans) override;
 
diff --git a/src/components/ScraperSearchComponent.cpp b/src/components/ScraperSearchComponent.cpp
index 22cb648f7..794b59a3e 100644
--- a/src/components/ScraperSearchComponent.cpp
+++ b/src/components/ScraperSearchComponent.cpp
@@ -6,6 +6,7 @@
 #include "ImageComponent.h"
 #include "RatingComponent.h"
 #include "DateTimeComponent.h"
+#include "AnimatedImageComponent.h"
 #include "ComponentList.h"
 #include "../HttpReq.h"
 #include "../Settings.h"
@@ -14,7 +15,7 @@
 #include "../guis/GuiTextEditPopup.h"
 
 ScraperSearchComponent::ScraperSearchComponent(Window* window, SearchType type) : GuiComponent(window),
-	mGrid(window, Eigen::Vector2i(4, 3)),
+	mGrid(window, Eigen::Vector2i(4, 3)), mBusyGrid(window, Eigen::Vector2i(3, 1)), 
 	mSearchType(type)
 {
 	addChild(&mGrid);
@@ -73,6 +74,14 @@ ScraperSearchComponent::ScraperSearchComponent(Window* window, SearchType type)
 	mResultList = std::make_shared<ComponentList>(mWindow);
 	mResultList->setCursorChangedCallback([this](CursorState state) { if(state == CURSOR_STOPPED) updateInfoPane(); });
 
+	mBusyAnimation = std::make_shared<AnimatedImageComponent>(mWindow);
+	mBusyAnimation->load(&BUSY_ANIMATION_DEF);
+	mBusyText = std::make_shared<TextComponent>(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();
 }
 
@@ -143,6 +152,20 @@ 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();
 }
 
@@ -330,6 +353,8 @@ void ScraperSearchComponent::render(const Eigen::Affine3f& parentTrans)
 		Renderer::setMatrix(trans);
 		Renderer::drawRect((int)mResultList->getPosition().x(), (int)mResultList->getPosition().y(),
 			(int)mResultList->getSize().x(), (int)mResultList->getSize().y(), 0x00000011);
+
+		mBusyGrid.render(trans);
 	}
 }
 
@@ -351,6 +376,11 @@ void ScraperSearchComponent::update(int deltaTime)
 {
 	GuiComponent::update(deltaTime);
 
+	if(mBlockAccept)
+	{
+		mBusyAnimation->update(deltaTime);
+	}
+
 	if(mThumbnailReq && mThumbnailReq->status() != HttpReq::REQ_IN_PROGRESS)
 	{
 		updateThumbnail();
diff --git a/src/components/ScraperSearchComponent.h b/src/components/ScraperSearchComponent.h
index e1260acac..a4cfb9b74 100644
--- a/src/components/ScraperSearchComponent.h
+++ b/src/components/ScraperSearchComponent.h
@@ -14,7 +14,7 @@ class TextComponent;
 class DateTimeComponent;
 class ScrollableContainer;
 class HttpReq;
-
+class AnimatedImageComponent;
 
 class ScraperSearchComponent : public GuiComponent
 {
@@ -97,4 +97,8 @@ private:
 	std::unique_ptr<MDResolveHandle> mMDResolveHandle;
 	std::vector<ScraperSearchResult> mScraperResults;
 	std::unique_ptr<HttpReq> mThumbnailReq;
+
+	ComponentGrid mBusyGrid;
+	std::shared_ptr<AnimatedImageComponent> mBusyAnimation;
+	std::shared_ptr<TextComponent> mBusyText;
 };