From a9d231587c609c6aa8c5857b95bd07231c6b1d16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Sat, 13 May 2023 12:34:23 +0200 Subject: [PATCH] Show save button thumbnail in the preview if it's available This is good enough for now but also more limited than I'd like because if the thumbnail arrives (gets rendered or downloaded) later than the preview is opened, it's never shown. --- src/gui/game/GameController.cpp | 4 ++-- src/gui/game/GameController.h | 1 + src/gui/interface/SaveButton.cpp | 9 +++++++++ src/gui/interface/SaveButton.h | 5 +++++ src/gui/preview/PreviewController.cpp | 5 +++-- src/gui/preview/PreviewController.h | 3 ++- src/gui/preview/PreviewView.cpp | 9 ++++++--- src/gui/preview/PreviewView.h | 2 +- src/gui/search/SearchController.cpp | 13 ++----------- src/gui/search/SearchController.h | 4 ++-- src/gui/search/SearchView.cpp | 2 +- 11 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index 799801f53..328bfb317 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -1213,7 +1213,7 @@ void GameController::OpenSaveDone() void GameController::OpenSavePreview(int saveID, int saveDate, bool instant) { - activePreview = new PreviewController(saveID, saveDate, instant, [this] { OpenSaveDone(); }); + activePreview = new PreviewController(saveID, saveDate, instant, [this] { OpenSaveDone(); }, nullptr); ui::Engine::Ref().ShowWindow(activePreview->GetView()); } @@ -1221,7 +1221,7 @@ void GameController::OpenSavePreview() { if(gameModel->GetSave()) { - activePreview = new PreviewController(gameModel->GetSave()->GetID(), 0, false, [this] { OpenSaveDone(); }); + activePreview = new PreviewController(gameModel->GetSave()->GetID(), 0, false, [this] { OpenSaveDone(); }, nullptr); ui::Engine::Ref().ShowWindow(activePreview->GetView()); } } diff --git a/src/gui/game/GameController.h b/src/gui/game/GameController.h index 447cf85b7..47f07dd2b 100644 --- a/src/gui/game/GameController.h +++ b/src/gui/game/GameController.h @@ -21,6 +21,7 @@ class SearchController; class PreviewController; class RenderController; class CommandInterface; +class VideoBuffer; class Tool; class Menu; class SaveInfo; diff --git a/src/gui/interface/SaveButton.cpp b/src/gui/interface/SaveButton.cpp index cf4cd6e0f..66b14ea75 100644 --- a/src/gui/interface/SaveButton.cpp +++ b/src/gui/interface/SaveButton.cpp @@ -401,4 +401,13 @@ void SaveButton::DoSelection() actionCallback.selected(); } +std::unique_ptr SaveButton::CloneThumbnail() const +{ + if (thumbnail) + { + return std::make_unique(*thumbnail); + } + return nullptr; +} + } /* namespace ui */ diff --git a/src/gui/interface/SaveButton.h b/src/gui/interface/SaveButton.h index 9b95db19d..ead5504f2 100644 --- a/src/gui/interface/SaveButton.h +++ b/src/gui/interface/SaveButton.h @@ -75,6 +75,11 @@ public: void DoAltAction2(); void DoSelection(); inline void SetActionCallback(SaveButtonAction action) { actionCallback = action; } + + // TODO: clone the request instead because sometimes the user of CloneThumbnail might end up + // with a nullptr even though the thumbnail for the SaveButton will eventually arrive. + std::unique_ptr CloneThumbnail() const; + protected: bool isButtonDown, state, isMouseInside, selected, selectable; }; diff --git a/src/gui/preview/PreviewController.cpp b/src/gui/preview/PreviewController.cpp index 7f43d30ea..58d2d33c9 100644 --- a/src/gui/preview/PreviewController.cpp +++ b/src/gui/preview/PreviewController.cpp @@ -7,19 +7,20 @@ #include "client/SaveInfo.h" #include "client/GameSave.h" #include "common/platform/Platform.h" +#include "graphics/Graphics.h" #include "gui/dialogues/ErrorMessage.h" #include "gui/dialogues/InformationMessage.h" #include "gui/login/LoginController.h" #include "gui/login/LoginView.h" #include "Config.h" -PreviewController::PreviewController(int saveID, int saveDate, bool instant, std::function onDone_): +PreviewController::PreviewController(int saveID, int saveDate, bool instant, std::function onDone_, std::unique_ptr thumbnail): saveId(saveID), loginWindow(NULL), HasExited(false) { previewModel = new PreviewModel(); - previewView = new PreviewView(); + previewView = new PreviewView(std::move(thumbnail)); previewModel->AddObserver(previewView); previewView->AttachController(this); previewModel->SetDoOpen(instant); diff --git a/src/gui/preview/PreviewController.h b/src/gui/preview/PreviewController.h index a6c0fd69d..321a6beaa 100644 --- a/src/gui/preview/PreviewController.h +++ b/src/gui/preview/PreviewController.h @@ -3,6 +3,7 @@ #include #include +class VideoBuffer; class SaveInfo; class LoginController; class PreviewModel; @@ -18,7 +19,7 @@ public: inline int SaveID() { return saveId; } bool HasExited; - PreviewController(int saveID, int saveDate, bool instant, std::function onDone = nullptr); + PreviewController(int saveID, int saveDate, bool instant, std::function onDone, std::unique_ptr thumbnail); void Exit(); void DoOpen(); void OpenInBrowser(); diff --git a/src/gui/preview/PreviewView.cpp b/src/gui/preview/PreviewView.cpp index a1923164c..780e7b5e5 100644 --- a/src/gui/preview/PreviewView.cpp +++ b/src/gui/preview/PreviewView.cpp @@ -33,9 +33,8 @@ # undef GetUserName // dammit windows #endif -PreviewView::PreviewView(): +PreviewView::PreviewView(std::unique_ptr newSavePreview): ui::Window(ui::Point(-1, -1), ui::Point((XRES/2)+210, (YRES/2)+150)), - savePreview(nullptr), submitCommentButton(NULL), addCommentBox(NULL), commentWarningLabel(NULL), @@ -48,6 +47,11 @@ PreviewView::PreviewView(): commentBoxHeight(20), commentHelpText(false) { + if (newSavePreview) + { + newSavePreview->Resize(RES / 2, true); + savePreview = std::move(newSavePreview); + } showAvatars = ui::Engine::Ref().ShowAvatars; favButton = new ui::Button(ui::Point(50, Size.Y-19), ui::Point(51, 19), "Fav"); @@ -416,7 +420,6 @@ void PreviewView::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ct void PreviewView::NotifySaveChanged(PreviewModel * sender) { auto *save = sender->GetSaveInfo(); - savePreview = nullptr; if(save) { votesUp = save->votesUp; diff --git a/src/gui/preview/PreviewView.h b/src/gui/preview/PreviewView.h index e61596cd9..a08835670 100644 --- a/src/gui/preview/PreviewView.h +++ b/src/gui/preview/PreviewView.h @@ -66,7 +66,7 @@ class PreviewView: public ui::Window void CheckComment(); public: void AttachController(PreviewController * controller); - PreviewView(); + PreviewView(std::unique_ptr newSavePreviev); void NotifySaveChanged(PreviewModel * sender); void NotifyCommentsChanged(PreviewModel * sender); void NotifyCommentsPageChanged(PreviewModel * sender); diff --git a/src/gui/search/SearchController.cpp b/src/gui/search/SearchController.cpp index 0e732a558..706c266c2 100644 --- a/src/gui/search/SearchController.cpp +++ b/src/gui/search/SearchController.cpp @@ -204,21 +204,12 @@ void SearchController::OpenSaveDone() } } -void SearchController::OpenSave(int saveID) +void SearchController::OpenSave(int saveID, int saveDate, std::unique_ptr thumbnail) { delete activePreview; Graphics * g = searchView->GetGraphics(); g->BlendFilledRect(RectSized(Vec2{ XRES/3, WINDOWH-20 }, Vec2{ XRES/3, 20 }), 0x000000_rgb .WithAlpha(150)); //dim the "Page X of Y" a little to make the CopyTextButton more noticeable - activePreview = new PreviewController(saveID, 0, instantOpen, [this] { OpenSaveDone(); }); - activePreview->GetView()->MakeActiveWindow(); -} - -void SearchController::OpenSave(int saveID, int saveDate) -{ - delete activePreview; - Graphics * g = searchView->GetGraphics(); - g->BlendFilledRect(RectSized(Vec2{ XRES/3, WINDOWH-20 }, Vec2{ XRES/3, 20 }), 0x000000_rgb .WithAlpha(150)); //dim the "Page X of Y" a little to make the CopyTextButton more noticeable - activePreview = new PreviewController(saveID, saveDate, instantOpen, [this] { OpenSaveDone(); }); + activePreview = new PreviewController(saveID, saveDate, instantOpen, [this] { OpenSaveDone(); }, std::move(thumbnail)); activePreview->GetView()->MakeActiveWindow(); } diff --git a/src/gui/search/SearchController.h b/src/gui/search/SearchController.h index 78cbb297d..cc33fa5e8 100644 --- a/src/gui/search/SearchController.h +++ b/src/gui/search/SearchController.h @@ -8,6 +8,7 @@ class PreviewController; class PreviewController; class SearchView; class SearchModel; +class VideoBuffer; class SearchController { private: @@ -42,8 +43,7 @@ public: void Selected(int saveID, bool selected); void SelectAllSaves(); void InstantOpen(bool instant); - void OpenSave(int saveID); - void OpenSave(int saveID, int saveDate); + void OpenSave(int saveID, int saveDate, std::unique_ptr thumbnail); void Update(); void ClearSelection(); void RemoveSelected(); diff --git a/src/gui/search/SearchView.cpp b/src/gui/search/SearchView.cpp index df9a739cf..8803ba13b 100644 --- a/src/gui/search/SearchView.cpp +++ b/src/gui/search/SearchView.cpp @@ -562,7 +562,7 @@ void SearchView::NotifySaveListChanged(SearchModel * sender) saves[i]); saveButton->AddContextMenu(0); saveButton->SetActionCallback({ - [this, saveButton] { c->OpenSave(saveButton->GetSave()->GetID(), saveButton->GetSave()->GetVersion()); }, + [this, saveButton] { c->OpenSave(saveButton->GetSave()->GetID(), saveButton->GetSave()->GetVersion(), saveButton->CloneThumbnail()); }, [this, saveButton] { Search(String::Build("history:", saveButton->GetSave()->GetID())); }, [this, saveButton] { Search(String::Build("user:", saveButton->GetSave()->GetUserName().FromUtf8())); }, [this, saveButton] { c->Selected(saveButton->GetSave()->GetID(), saveButton->GetSelected()); }