diff --git a/CMakeLists.txt b/CMakeLists.txt index 9912bc314..7eaa90576 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/components/AsyncReqComponent.cpp b/src/components/AsyncReqComponent.cpp new file mode 100644 index 000000000..2f7f3a9ea --- /dev/null +++ b/src/components/AsyncReqComponent.cpp @@ -0,0 +1,44 @@ +#include "AsyncReqComponent.h" +#include "../Renderer.h" + +AsyncReqComponent::AsyncReqComponent(Window* window, std::shared_ptr req, std::function)> onSuccess, std::function 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); +} diff --git a/src/components/AsyncReqComponent.h b/src/components/AsyncReqComponent.h new file mode 100644 index 000000000..47b43fdfb --- /dev/null +++ b/src/components/AsyncReqComponent.h @@ -0,0 +1,39 @@ +#pragma once + +#include "../GuiComponent.h" +#include "../HttpReq.h" +#include + +/* Usage example: + std::shared_ptr httpreq = std::make_shared("cdn.garcya.us", "/wp-content/uploads/2010/04/TD250.jpg"); + AsyncReqComponent* req = new AsyncReqComponent(mWindow, httpreq, + [] (std::shared_ptr 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 req, std::function)> onSuccess, std::function onCancel = NULL); + + bool input(InputConfig* config, Input input) override; + void update(int deltaTime) override; + void render(const Eigen::Affine3f& parentTrans) override; + +private: + std::function)> mSuccessFunc; + std::function mCancelFunc; + + unsigned int mTime; + std::shared_ptr mRequest; +}; diff --git a/src/components/GuiMetaDataEd.cpp b/src/components/GuiMetaDataEd.cpp index 3f4e632bd..13323f307 100644 --- a/src/components/GuiMetaDataEd.cpp +++ b/src/components/GuiMetaDataEd.cpp @@ -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; }); @@ -72,7 +74,7 @@ void GuiMetaDataEd::populateList(const std::vector& mdd) //fetch button mList.setEntry(Vector2i(1, y), Vector2i(1, 1), &mFetchButton, true, ComponentListComponent::AlignCenter); - + y++; for(auto iter = mdd.begin(); iter != mdd.end(); iter++) @@ -105,3 +107,8 @@ void GuiMetaDataEd::save() if(mSavedCallback) mSavedCallback(); } + +void GuiMetaDataEd::fetch() +{ + +} diff --git a/src/components/GuiMetaDataEd.h b/src/components/GuiMetaDataEd.h index bdad64344..624c75154 100644 --- a/src/components/GuiMetaDataEd.h +++ b/src/components/GuiMetaDataEd.h @@ -18,6 +18,7 @@ public: private: void save(); + void fetch(); void populateList(const std::vector& mdd);