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