Add RequestMonitor
This commit is contained in:
parent
66c49203f2
commit
af4d022087
56
src/client/RequestMonitor.h
Normal file
56
src/client/RequestMonitor.h
Normal file
@ -0,0 +1,56 @@
|
||||
#ifndef REQUESTMONITOR_H
|
||||
#define REQUESTMONITOR_H
|
||||
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
namespace http
|
||||
{
|
||||
template<class R>
|
||||
class RequestMonitor
|
||||
{
|
||||
R *request;
|
||||
|
||||
protected:
|
||||
RequestMonitor() :
|
||||
request(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~RequestMonitor()
|
||||
{
|
||||
if (request)
|
||||
{
|
||||
request->Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
void RequestPoll()
|
||||
{
|
||||
if (request && request->CheckDone())
|
||||
{
|
||||
OnResponse(request->Finish());
|
||||
request = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
template<class... Args>
|
||||
void RequestSetup(Args&&... args)
|
||||
{
|
||||
assert(!request);
|
||||
request = new R(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
void RequestStart()
|
||||
{
|
||||
assert(request);
|
||||
request->Start();
|
||||
}
|
||||
|
||||
virtual void OnResponse(typename std::result_of<decltype(&R::Finish)(R)>::type v) = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // REQUESTMONITOR_H
|
||||
|
||||
|
@ -4,7 +4,6 @@
|
||||
#include "AvatarButton.h"
|
||||
#include "Format.h"
|
||||
#include "client/Client.h"
|
||||
#include "client/AvatarRequest.h"
|
||||
#include "graphics/Graphics.h"
|
||||
#include "ContextMenu.h"
|
||||
#include "Keys.h"
|
||||
@ -14,7 +13,6 @@ namespace ui {
|
||||
|
||||
AvatarButton::AvatarButton(Point position, Point size, ByteString username):
|
||||
Component(position, size),
|
||||
avatarRequest(nullptr),
|
||||
name(username),
|
||||
tried(false),
|
||||
actionCallback(NULL)
|
||||
@ -27,20 +25,21 @@ AvatarButton::~AvatarButton()
|
||||
delete actionCallback;
|
||||
}
|
||||
|
||||
void AvatarButton::OnResponse(std::unique_ptr<VideoBuffer> Avatar)
|
||||
{
|
||||
avatar = std::move(Avatar);
|
||||
}
|
||||
|
||||
void AvatarButton::Tick(float dt)
|
||||
{
|
||||
if(!avatar && !tried && name.size() > 0)
|
||||
{
|
||||
tried = true;
|
||||
avatarRequest = new http::AvatarRequest(name, Size.X, Size.Y);
|
||||
avatarRequest->Start();
|
||||
RequestSetup(name, Size.X, Size.Y);
|
||||
RequestStart();
|
||||
}
|
||||
|
||||
if (avatarRequest && avatarRequest->CheckDone())
|
||||
{
|
||||
avatar = avatarRequest->Finish();
|
||||
avatarRequest = nullptr;
|
||||
}
|
||||
RequestPoll();
|
||||
}
|
||||
|
||||
void AvatarButton::Draw(const Point& screenPos)
|
||||
|
@ -6,13 +6,11 @@
|
||||
#include "Component.h"
|
||||
#include "graphics/Graphics.h"
|
||||
#include "gui/interface/Colour.h"
|
||||
#include "client/AvatarRequest.h"
|
||||
#include "client/RequestMonitor.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace http
|
||||
{
|
||||
class AvatarRequest;
|
||||
}
|
||||
namespace ui
|
||||
{
|
||||
class AvatarButton;
|
||||
@ -23,10 +21,9 @@ public:
|
||||
virtual ~AvatarButtonAction() {}
|
||||
};
|
||||
|
||||
class AvatarButton : public Component
|
||||
class AvatarButton : public Component, public http::RequestMonitor<http::AvatarRequest>
|
||||
{
|
||||
std::unique_ptr<VideoBuffer> avatar;
|
||||
http::AvatarRequest *avatarRequest;
|
||||
ByteString name;
|
||||
bool tried;
|
||||
public:
|
||||
@ -44,6 +41,8 @@ public:
|
||||
void Draw(const Point& screenPos) override;
|
||||
void Tick(float dt) override;
|
||||
|
||||
void OnResponse(std::unique_ptr<VideoBuffer> avatar) override;
|
||||
|
||||
void DoAction();
|
||||
|
||||
void SetUsername(ByteString username) { name = username; }
|
||||
|
@ -8,11 +8,10 @@
|
||||
#include "SaveButton.h"
|
||||
#include "client/Client.h"
|
||||
#include "client/SaveInfo.h"
|
||||
#include "client/ThumbnailRequest.h"
|
||||
#include "client/ThumbnailRenderer.h"
|
||||
#include "graphics/Graphics.h"
|
||||
#include "simulation/SaveRenderer.h"
|
||||
#include "client/GameSave.h"
|
||||
#include "simulation/SaveRenderer.h"
|
||||
|
||||
namespace ui {
|
||||
|
||||
@ -24,7 +23,6 @@ SaveButton::SaveButton(Point position, Point size, SaveInfo * save):
|
||||
isMouseInsideAuthor(false),
|
||||
isMouseInsideHistory(false),
|
||||
showVotes(false),
|
||||
thumbnailRequest(nullptr),
|
||||
isButtonDown(false),
|
||||
isMouseInside(false),
|
||||
selected(false),
|
||||
@ -98,7 +96,6 @@ SaveButton::SaveButton(Point position, Point size, SaveFile * file):
|
||||
isMouseInsideAuthor(false),
|
||||
isMouseInsideHistory(false),
|
||||
showVotes(false),
|
||||
thumbnailRequest(nullptr),
|
||||
isButtonDown(false),
|
||||
isMouseInside(false),
|
||||
selected(false),
|
||||
@ -119,16 +116,16 @@ SaveButton::SaveButton(Point position, Point size, SaveFile * file):
|
||||
|
||||
SaveButton::~SaveButton()
|
||||
{
|
||||
if (thumbnailRequest)
|
||||
{
|
||||
thumbnailRequest->Cancel();
|
||||
}
|
||||
|
||||
delete actionCallback;
|
||||
delete save;
|
||||
delete file;
|
||||
}
|
||||
|
||||
void SaveButton::OnResponse(std::unique_ptr<VideoBuffer> Thumbnail)
|
||||
{
|
||||
thumbnail = std::move(Thumbnail);
|
||||
}
|
||||
|
||||
void SaveButton::Tick(float dt)
|
||||
{
|
||||
if (!thumbnail)
|
||||
@ -147,8 +144,8 @@ void SaveButton::Tick(float dt)
|
||||
}
|
||||
else if (save->GetID())
|
||||
{
|
||||
thumbnailRequest = new http::ThumbnailRequest(save->GetID(), save->GetVersion(), thumbBoxSize.X, thumbBoxSize.Y);
|
||||
thumbnailRequest->Start();
|
||||
RequestSetup(save->GetID(), save->GetVersion(), thumbBoxSize.X, thumbBoxSize.Y);
|
||||
RequestStart();
|
||||
triedThumbnail = true;
|
||||
}
|
||||
}
|
||||
@ -160,11 +157,7 @@ void SaveButton::Tick(float dt)
|
||||
}
|
||||
}
|
||||
|
||||
if (thumbnailRequest && thumbnailRequest->CheckDone())
|
||||
{
|
||||
thumbnail = thumbnailRequest->Finish();
|
||||
thumbnailRequest = nullptr;
|
||||
}
|
||||
RequestPoll();
|
||||
|
||||
if (thumbnailRenderer)
|
||||
{
|
||||
|
@ -8,14 +8,13 @@
|
||||
#include "client/SaveInfo.h"
|
||||
#include "graphics/Graphics.h"
|
||||
#include "gui/interface/Colour.h"
|
||||
#include "client/ThumbnailRequest.h"
|
||||
#include "client/RequestMonitor.h"
|
||||
#include "graphics/Graphics.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class ThumbnailRendererTask;
|
||||
namespace http
|
||||
{
|
||||
class ThumbnailRequest;
|
||||
}
|
||||
namespace ui
|
||||
{
|
||||
class SaveButton;
|
||||
@ -29,7 +28,7 @@ public:
|
||||
virtual ~SaveButtonAction() {}
|
||||
};
|
||||
|
||||
class SaveButton : public Component
|
||||
class SaveButton : public Component, public http::RequestMonitor<http::ThumbnailRequest>
|
||||
{
|
||||
SaveFile * file;
|
||||
SaveInfo * save;
|
||||
@ -47,7 +46,6 @@ class SaveButton : public Component
|
||||
bool isMouseInsideHistory;
|
||||
bool showVotes;
|
||||
std::unique_ptr<ThumbnailRendererTask> thumbnailRenderer;
|
||||
http::ThumbnailRequest *thumbnailRequest;
|
||||
public:
|
||||
SaveButton(Point position, Point size, SaveInfo * save);
|
||||
SaveButton(Point position, Point size, SaveFile * file);
|
||||
@ -67,6 +65,8 @@ public:
|
||||
void Draw(const Point& screenPos) override;
|
||||
void Tick(float dt) override;
|
||||
|
||||
void OnResponse(std::unique_ptr<VideoBuffer> thumbnail) override;
|
||||
|
||||
void SetSelected(bool selected_) { selected = selected_; }
|
||||
bool GetSelected() { return selected; }
|
||||
void SetSelectable(bool selectable_) { selectable = selectable_; }
|
||||
|
Reference in New Issue
Block a user