From 772148900ee0ce8bad927d3b1c7392bcf816ae59 Mon Sep 17 00:00:00 2001 From: jacob1 Date: Sun, 3 Mar 2019 23:45:33 -0500 Subject: [PATCH] Fix stamp and local save thumbnails being stretched --- src/client/requestbroker/RequestBroker.cpp | 6 ++-- src/client/requestbroker/RequestBroker.h | 2 +- .../requestbroker/ThumbRenderRequest.cpp | 32 +++++++++++++------ src/client/requestbroker/ThumbRenderRequest.h | 6 ++-- src/graphics/Graphics.h | 4 +-- src/gui/interface/SaveButton.cpp | 20 ++++++------ src/gui/interface/SaveButton.h | 1 + src/gui/save/LocalSaveActivity.cpp | 2 +- src/gui/save/ServerSaveActivity.cpp | 2 +- 9 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/client/requestbroker/RequestBroker.cpp b/src/client/requestbroker/RequestBroker.cpp index 5a446a754..2258f7f9b 100644 --- a/src/client/requestbroker/RequestBroker.cpp +++ b/src/client/requestbroker/RequestBroker.cpp @@ -79,14 +79,14 @@ void RequestBroker::Shutdown() void RequestBroker::RenderThumbnail(GameSave * gameSave, int width, int height, RequestListener * tListener) { - RenderThumbnail(gameSave, true, true, width, height, tListener); + RenderThumbnail(gameSave, true, true, width, height, false, tListener); } -void RequestBroker::RenderThumbnail(GameSave * gameSave, bool decorations, bool fire, int width, int height, RequestListener * tListener) +void RequestBroker::RenderThumbnail(GameSave * gameSave, bool decorations, bool fire, int width, int height, bool autoRescale, RequestListener * tListener) { ListenerHandle handle = AttachRequestListener(tListener); - ThumbRenderRequest * r = new ThumbRenderRequest(new GameSave(*gameSave), decorations, fire, width, height, handle); + ThumbRenderRequest * r = new ThumbRenderRequest(new GameSave(*gameSave), decorations, fire, width, height, autoRescale, handle); pthread_mutex_lock(&requestQueueMutex); requestQueue.push_back(r); diff --git a/src/client/requestbroker/RequestBroker.h b/src/client/requestbroker/RequestBroker.h index 6cee30852..d898e870e 100644 --- a/src/client/requestbroker/RequestBroker.h +++ b/src/client/requestbroker/RequestBroker.h @@ -52,7 +52,7 @@ public: void FlushThumbQueue(); void RetrieveImage(ByteString imageUrl, int width, int height, RequestListener * tListener); - void RenderThumbnail(GameSave * gameSave, bool decorations, bool fire, int width, int height, RequestListener * tListener); + void RenderThumbnail(GameSave * gameSave, bool decorations, bool fire, int width, int height, bool autoRescale, RequestListener * tListener); void RenderThumbnail(GameSave * gameSave, int width, int height, RequestListener * tListener); void RetrieveThumbnail(int saveID, int saveDate, int width, int height, RequestListener * tListener); void RetrieveThumbnail(int saveID, int width, int height, RequestListener * tListener); diff --git a/src/client/requestbroker/ThumbRenderRequest.cpp b/src/client/requestbroker/ThumbRenderRequest.cpp index 756da795b..daf0487d8 100644 --- a/src/client/requestbroker/ThumbRenderRequest.cpp +++ b/src/client/requestbroker/ThumbRenderRequest.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "ThumbRenderRequest.h" @@ -5,14 +6,16 @@ #include "graphics/Graphics.h" #include "simulation/SaveRenderer.h" -ThumbRenderRequest::ThumbRenderRequest(GameSave * save, bool decorations, bool fire, int width, int height, ListenerHandle listener, int identifier): - RequestBroker::Request(ThumbnailRender, listener, identifier) +ThumbRenderRequest::ThumbRenderRequest(GameSave * save, bool decorations, bool fire, int width, int height, bool autoRescale, ListenerHandle listener, int identifier): + RequestBroker::Request(ThumbnailRender, listener, identifier), + Save(save), + Width(width), + Height(height), + Decorations(decorations), + Fire(fire), + autoRescale(autoRescale) { - Save = save; - Width = width; - Height = height; - Decorations = decorations; - Fire = fire; + } RequestBroker::ProcessResponse ThumbRenderRequest::Process(RequestBroker & rb) @@ -22,9 +25,20 @@ RequestBroker::ProcessResponse ThumbRenderRequest::Process(RequestBroker & rb) delete Save; Save = NULL; - if(thumbnail) + if (thumbnail) { - thumbnail->Resize(Width, Height, true); + if (!autoRescale) + thumbnail->Resize(Width, Height, true); + else + { + int scaleX = (int)std::ceil((float)thumbnail->Width / Width); + int scaleY = (int)std::ceil((float)thumbnail->Height / Height); + int scale = scaleX > scaleY ? scaleX : scaleY; + int newWidth = thumbnail->Width / scale, newHeight = thumbnail->Height / scale; + thumbnail->Resize(newWidth, newHeight, true); + Width = newWidth; + Height = newHeight; + } ResultObject = (void*)thumbnail; rb.requestComplete((Request*)this); return RequestBroker::Finished; diff --git a/src/client/requestbroker/ThumbRenderRequest.h b/src/client/requestbroker/ThumbRenderRequest.h index 739b0e7eb..29dfbbaaa 100644 --- a/src/client/requestbroker/ThumbRenderRequest.h +++ b/src/client/requestbroker/ThumbRenderRequest.h @@ -4,11 +4,13 @@ class GameSave; class ThumbRenderRequest: public RequestBroker::Request { public: + GameSave * Save; int Width, Height; bool Decorations; bool Fire; - GameSave * Save; - ThumbRenderRequest(GameSave * save, bool decorations, bool fire, int width, int height, ListenerHandle listener, int identifier = 0); + bool autoRescale; + + ThumbRenderRequest(GameSave * save, bool decorations, bool fire, int width, int height, bool autoRescale, ListenerHandle listener, int identifier = 0); virtual RequestBroker::ProcessResponse Process(RequestBroker & rb); virtual ~ThumbRenderRequest(); virtual void Cleanup(); diff --git a/src/graphics/Graphics.h b/src/graphics/Graphics.h index 3ed16ece5..5d0ab4850 100644 --- a/src/graphics/Graphics.h +++ b/src/graphics/Graphics.h @@ -158,8 +158,8 @@ public: void gradientrect(int x, int y, int width, int height, int r, int g, int b, int a, int r2, int g2, int b2, int a2); void draw_image(pixel *img, int x, int y, int w, int h, int a); - void draw_image(const VideoBuffer & vidBuf, int w, int h, int a); - void draw_image(VideoBuffer * vidBuf, int w, int h, int a); + void draw_image(const VideoBuffer & vidBuf, int x, int y, int a); + void draw_image(VideoBuffer * vidBuf, int x, int y, int a); void draw_rgba_image(const unsigned char *data, int x, int y, float alpha); Graphics(); diff --git a/src/gui/interface/SaveButton.cpp b/src/gui/interface/SaveButton.cpp index ec7947ead..b2eced197 100644 --- a/src/gui/interface/SaveButton.cpp +++ b/src/gui/interface/SaveButton.cpp @@ -128,11 +128,13 @@ SaveButton::~SaveButton() void SaveButton::OnResponseReady(void * imagePtr, int identifier) { VideoBuffer * image = (VideoBuffer*)imagePtr; - if(image) + if (image) { delete thumbnail; thumbnail = image; waitingForThumb = false; + if (file) + thumbSize = ui::Point(thumbnail->Width, thumbnail->Height); } } @@ -158,7 +160,7 @@ void SaveButton::Tick(float dt) else if(file && file->GetGameSave()) { waitingForThumb = true; - RequestBroker::Ref().RenderThumbnail(file->GetGameSave(), true, false, thumbBoxSize.X, thumbBoxSize.Y, this); + RequestBroker::Ref().RenderThumbnail(file->GetGameSave(), true, false, thumbBoxSize.X, thumbBoxSize.Y, true, this); } } } @@ -166,8 +168,8 @@ void SaveButton::Tick(float dt) void SaveButton::Draw(const Point& screenPos) { Graphics * g = GetGraphics(); - float scaleFactor; - ui::Point thumbBoxSize(0, 0); + float scaleFactor = (Size.Y-25)/((float)YRES); + ui::Point thumbBoxSize = ui::Point(((float)XRES)*scaleFactor, ((float)YRES)*scaleFactor); wantsDraw = true; @@ -176,15 +178,13 @@ void SaveButton::Draw(const Point& screenPos) g->fillrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 100, 170, 255, 100); } - scaleFactor = (Size.Y-25)/((float)YRES); - thumbBoxSize = ui::Point(((float)XRES)*scaleFactor, ((float)YRES)*scaleFactor); - if(thumbnail) + if (thumbnail) { //thumbBoxSize = ui::Point(thumbnail->Width, thumbnail->Height); - if(save && save->id) + if (save && save->id) g->draw_image(thumbnail, screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, 255); else - g->draw_image(thumbnail, screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, 255); + g->draw_image(thumbnail, screenPos.X+(Size.X-thumbSize.X)/2, screenPos.Y+(Size.Y-21-thumbSize.Y)/2, 255); } else if (file && !file->GetGameSave()) g->drawtext(screenPos.X+(Size.X-Graphics::textwidth("Error loading save"))/2, screenPos.Y+(Size.Y-28)/2, "Error loading save", 180, 180, 180, 255); @@ -257,6 +257,8 @@ void SaveButton::Draw(const Point& screenPos) g->drawrect(screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 210, 230, 255, 255); else g->drawrect(screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 180, 180, 180, 255); + if (thumbSize.X) + g->xor_rect(screenPos.X+(Size.X-thumbSize.X)/2, screenPos.Y+(Size.Y-21-thumbSize.Y)/2, thumbSize.X, thumbSize.Y); if (isMouseInside) { diff --git a/src/gui/interface/SaveButton.h b/src/gui/interface/SaveButton.h index ac219018b..b2da22096 100644 --- a/src/gui/interface/SaveButton.h +++ b/src/gui/interface/SaveButton.h @@ -28,6 +28,7 @@ class SaveButton : public Component, public RequestListener SaveFile * file; SaveInfo * save; VideoBuffer * thumbnail; + ui::Point thumbSize = ui::Point(0, 0); String name; String votesString; String votesBackground; diff --git a/src/gui/save/LocalSaveActivity.cpp b/src/gui/save/LocalSaveActivity.cpp index e74184167..aa56e28b1 100644 --- a/src/gui/save/LocalSaveActivity.cpp +++ b/src/gui/save/LocalSaveActivity.cpp @@ -71,7 +71,7 @@ LocalSaveActivity::LocalSaveActivity(SaveFile save, FileSavedCallback * callback SetOkayButton(okayButton); if(save.GetGameSave()) - RequestBroker::Ref().RenderThumbnail(save.GetGameSave(), true, false, Size.X-16, -1, this); + RequestBroker::Ref().RenderThumbnail(save.GetGameSave(), true, false, Size.X-16, -1, false, this); } void LocalSaveActivity::Save() diff --git a/src/gui/save/ServerSaveActivity.cpp b/src/gui/save/ServerSaveActivity.cpp index fbbac7d96..1071fc1ca 100644 --- a/src/gui/save/ServerSaveActivity.cpp +++ b/src/gui/save/ServerSaveActivity.cpp @@ -184,7 +184,7 @@ ServerSaveActivity::ServerSaveActivity(SaveInfo save, ServerSaveActivity::SaveUp AddComponent(RulesButton); if(save.GetGameSave()) - RequestBroker::Ref().RenderThumbnail(save.GetGameSave(), false, true, (Size.X/2)-16, -1, this); + RequestBroker::Ref().RenderThumbnail(save.GetGameSave(), false, true, (Size.X/2)-16, -1, false, this); } ServerSaveActivity::ServerSaveActivity(SaveInfo save, bool saveNow, ServerSaveActivity::SaveUploadedCallback * callback) :