Added AsyncReqComponent for easy asynchronous, cancelable HTTP requests with a nice loading icon.

This commit is contained in:
Aloshi 2013-09-15 14:11:39 -05:00
parent c807c98b4a
commit e823592660
5 changed files with 94 additions and 1 deletions

View file

@ -156,6 +156,7 @@ set(ES_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/src/Window.h
${CMAKE_CURRENT_SOURCE_DIR}/src/XMLReader.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/AnimationComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/AsyncReqComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ButtonComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ComponentListComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.h
@ -204,6 +205,7 @@ set(ES_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/Window.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/XMLReader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/AnimationComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/AsyncReqComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ButtonComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ComponentListComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.cpp

View file

@ -0,0 +1,44 @@
#include "AsyncReqComponent.h"
#include "../Renderer.h"
AsyncReqComponent::AsyncReqComponent(Window* window, std::shared_ptr<HttpReq> req, std::function<void(std::shared_ptr<HttpReq>)> onSuccess, std::function<void()> onCancel)
: GuiComponent(window),
mSuccessFunc(onSuccess), mCancelFunc(onCancel), mTime(0), mRequest(req)
{
}
bool AsyncReqComponent::input(InputConfig* config, Input input)
{
if(input.value != 0 && config->isMappedTo("b", input))
{
if(mCancelFunc)
mCancelFunc();
delete this;
}
return true;
}
void AsyncReqComponent::update(int deltaTime)
{
if(mRequest->status() != HttpReq::REQ_IN_PROGRESS)
{
mSuccessFunc(mRequest);
delete this;
return;
}
mTime += deltaTime;
}
void AsyncReqComponent::render(const Eigen::Affine3f& parentTrans)
{
Eigen::Affine3f trans = Eigen::Affine3f::Identity();
trans = trans.translate(Eigen::Vector3f(Renderer::getScreenWidth() / 2.0f, Renderer::getScreenHeight() / 2.0f, 0));
Renderer::setMatrix(trans);
Eigen::Vector3f point(cos(mTime * 0.01f) * 12, sin(mTime * 0.01f) * 12, 0);
Renderer::drawRect((int)point.x(), (int)point.y(), 8, 8, 0x0000FFFF);
}

View file

@ -0,0 +1,39 @@
#pragma once
#include "../GuiComponent.h"
#include "../HttpReq.h"
#include <functional>
/* Usage example:
std::shared_ptr<HttpReq> httpreq = std::make_shared<HttpReq>("cdn.garcya.us", "/wp-content/uploads/2010/04/TD250.jpg");
AsyncReqComponent* req = new AsyncReqComponent(mWindow, httpreq,
[] (std::shared_ptr<HttpReq> r)
{
LOG(LogInfo) << "Request completed";
LOG(LogInfo) << " error, if any: " << r->getErrorMsg();
}, [] ()
{
LOG(LogInfo) << "Request canceled";
});
mWindow->pushGui(req);
//we can forget about req, since it will always delete itself
*/
class AsyncReqComponent : public GuiComponent
{
public:
AsyncReqComponent(Window* window, std::shared_ptr<HttpReq> req, std::function<void(std::shared_ptr<HttpReq>)> onSuccess, std::function<void()> onCancel = NULL);
bool input(InputConfig* config, Input input) override;
void update(int deltaTime) override;
void render(const Eigen::Affine3f& parentTrans) override;
private:
std::function<void(std::shared_ptr<HttpReq>)> mSuccessFunc;
std::function<void()> mCancelFunc;
unsigned int mTime;
std::shared_ptr<HttpReq> mRequest;
};

View file

@ -1,6 +1,7 @@
#include "GuiMetaDataEd.h"
#include "../Renderer.h"
#include "../Log.h"
#include "AsyncReqComponent.h"
#define MDED_RESERVED_ROWS 3
@ -37,6 +38,7 @@ GuiMetaDataEd::GuiMetaDataEd(Window* window, MetaDataList* md, const std::vector
mDeleteButton.setPressedFunc([&] { mDeleteFunc(); delete this; });
mFetchButton.setText("FETCH", 0x555555FF);
mFetchButton.setPressedFunc([&] { fetch(); });
mSaveButton.setText("SAVE", 0x0000FFFF);
mSaveButton.setPressedFunc([&] { save(); delete this; });
@ -105,3 +107,8 @@ void GuiMetaDataEd::save()
if(mSavedCallback)
mSavedCallback();
}
void GuiMetaDataEd::fetch()
{
}

View file

@ -18,6 +18,7 @@ public:
private:
void save();
void fetch();
void populateList(const std::vector<MetaDataDecl>& mdd);