From 2e2bfe8db4e2ee826efcbcc730c2a4a44b1bb01f Mon Sep 17 00:00:00 2001 From: CrazyBloo Date: Mon, 30 Sep 2024 20:08:04 -0400 Subject: [PATCH] implement vinicius suggestions --- src/core/libraries/np_trophy/np_trophy.cpp | 6 +- src/core/libraries/np_trophy/trophy_ui.cpp | 113 ++++++++++----------- src/core/libraries/np_trophy/trophy_ui.h | 34 ++++--- src/qt_gui/trophy_viewer.cpp | 2 +- 4 files changed, 77 insertions(+), 78 deletions(-) diff --git a/src/core/libraries/np_trophy/np_trophy.cpp b/src/core/libraries/np_trophy/np_trophy.cpp index 2d3e309d4..0641a2c06 100644 --- a/src/core/libraries/np_trophy/np_trophy.cpp +++ b/src/core/libraries/np_trophy/np_trophy.cpp @@ -14,8 +14,6 @@ namespace Libraries::NpTrophy { -static TrophyUI g_trophy_ui; - std::string game_serial; static constexpr auto MaxTrophyHandles = 4u; @@ -927,7 +925,7 @@ int PS4_SYSV_ABI sceNpTrophyUnlockTrophy(OrbisNpTrophyContext context, OrbisNpTr std::filesystem::path current_icon_path = trophy_dir / "trophy00" / "Icons" / trophy_icon_file; - g_trophy_ui.AddTrophyToQueue(current_icon_path, current_trophy_name); + AddTrophyToQueue(current_icon_path, current_trophy_name); } } } @@ -964,7 +962,7 @@ int PS4_SYSV_ABI sceNpTrophyUnlockTrophy(OrbisNpTrophyContext context, OrbisNpTr trophy_dir / "trophy00" / "Icons" / platinum_icon_file; *platinumId = platinum_trophy_id; - g_trophy_ui.AddTrophyToQueue(platinum_icon_path, platinum_trophy_name); + AddTrophyToQueue(platinum_icon_path, platinum_trophy_name); } } diff --git a/src/core/libraries/np_trophy/trophy_ui.cpp b/src/core/libraries/np_trophy/trophy_ui.cpp index 1dbe74815..412e5fe74 100644 --- a/src/core/libraries/np_trophy/trophy_ui.cpp +++ b/src/core/libraries/np_trophy/trophy_ui.cpp @@ -4,14 +4,25 @@ #include #include #include "common/assert.h" +#include "common/singleton.h" #include "imgui/imgui_std.h" -#include "imgui/imgui_texture.h" #include "trophy_ui.h" using namespace ImGui; -using namespace Libraries::NpTrophy; +namespace Libraries::NpTrophy { -TrophyUI::TrophyUI() { +std::optional current_trophy_ui; +std::queue trophy_queue; + +TrophyUI::TrophyUI(std::filesystem::path trophyIconPath, std::string trophyName) + : trophy_icon_path(trophyIconPath), trophy_name(trophyName) { + trophy_start_time = std::chrono::system_clock::now().time_since_epoch().count(); + trophy_time_now = trophy_start_time; + if (std::filesystem::exists(trophy_icon_path)) { + trophy_icon = RefCountedTexture::DecodePngFile(trophy_icon_path); + } else { + LOG_ERROR(Lib_NpTrophy, "Couldnt load trophy icon at {}", trophy_icon_path.string()); + } AddLayer(this); } @@ -19,23 +30,10 @@ TrophyUI::~TrophyUI() { Finish(); } -void Libraries::NpTrophy::TrophyUI::AddTrophyToQueue(std::filesystem::path trophyIconPath, - std::string trophyName) { - TrophyInfo newInfo; - newInfo.trophy_icon_path = trophyIconPath; - newInfo.trophy_name = trophyName; - trophy_queue.push_back(newInfo); -} - void TrophyUI::Finish() { RemoveLayer(this); } -bool displayingTrophy; -std::chrono::steady_clock::time_point trophyStartedTime; -bool iconLoaded = false; -RefCountedTexture trophyIcon; - void TrophyUI::Draw() { const auto& io = GetIO(); @@ -44,51 +42,48 @@ void TrophyUI::Draw() { std::min(io.DisplaySize.y, 70.f), }; - if (trophy_queue.size() != 0) { - if (!displayingTrophy) { - displayingTrophy = true; - trophyStartedTime = std::chrono::steady_clock::now(); + SetNextWindowSize(window_size); + SetNextWindowCollapsed(false); + SetNextWindowPos(ImVec2(io.DisplaySize.x - 250, 50)); + KeepNavHighlight(); + + if (Begin("Trophy Window", nullptr, + ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoSavedSettings | + ImGuiWindowFlags_NoInputs)) { + if (trophy_icon) { + Image(trophy_icon.GetTexture().im_id, ImVec2(50, 50)); + ImGui::SameLine(); + } else { + // placeholder + ImGui::GetWindowDrawList()->AddRectFilled(ImVec2(1040, 60), ImVec2(1090, 110), + GetColorU32(ImVec4{0.7f})); + ImGui::Indent(60); } + TextWrapped("Trophy earned!\n%s", trophy_name.c_str()); + } + End(); - std::chrono::steady_clock::time_point timeNow = std::chrono::steady_clock::now(); - std::chrono::seconds duration = - std::chrono::duration_cast(timeNow - trophyStartedTime); - - if (duration.count() >= 5) { - trophy_queue.erase(trophy_queue.begin()); - displayingTrophy = false; - iconLoaded = false; - } - - if (trophy_queue.size() != 0) { - SetNextWindowSize(window_size); - SetNextWindowCollapsed(false); - SetNextWindowPos(ImVec2(io.DisplaySize.x - 250, 50)); - KeepNavHighlight(); - - TrophyInfo currentTrophyInfo = trophy_queue[0]; - - if (!iconLoaded) { - if (std::filesystem::exists(currentTrophyInfo.trophy_icon_path)) { - trophyIcon = - RefCountedTexture::DecodePngFile(currentTrophyInfo.trophy_icon_path); - iconLoaded = true; - } else { - LOG_ERROR(Lib_NpTrophy, "Couldnt load trophy icon at {}", - currentTrophyInfo.trophy_icon_path.string()); - } - } - - if (Begin("Trophy Window", nullptr, - ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoSavedSettings | - ImGuiWindowFlags_NoInputs)) { - if (iconLoaded) { - Image(trophyIcon.GetTexture().im_id, ImVec2(50, 50)); - ImGui::SameLine(); - } - TextWrapped("Trophy earned!\n%s", currentTrophyInfo.trophy_name.c_str()); - } - End(); + trophy_time_now += io.DeltaTime * 1000; + if (trophy_time_now >= trophy_start_time + 5000) { + if (!trophy_queue.empty()) { + TrophyInfo nextTrophy = trophy_queue.front(); + trophy_queue.pop(); + current_trophy_ui.emplace(nextTrophy.trophy_icon_path, nextTrophy.trophy_name); + } else { + current_trophy_ui.reset(); } } } + +void AddTrophyToQueue(std::filesystem::path trophyIconPath, std::string trophyName) { + if (current_trophy_ui.has_value()) { + TrophyInfo new_trophy; + new_trophy.trophy_icon_path = trophyIconPath; + new_trophy.trophy_name = trophyName; + trophy_queue.push(new_trophy); + } else { + current_trophy_ui.emplace(trophyIconPath, trophyName); + } +} + +} // namespace Libraries::NpTrophy \ No newline at end of file diff --git a/src/core/libraries/np_trophy/trophy_ui.h b/src/core/libraries/np_trophy/trophy_ui.h index 06055c974..60e2fa87b 100644 --- a/src/core/libraries/np_trophy/trophy_ui.h +++ b/src/core/libraries/np_trophy/trophy_ui.h @@ -5,32 +5,38 @@ #include #include -#include +#include #include "common/fixed_value.h" #include "common/types.h" #include "core/libraries/np_trophy/np_trophy.h" #include "imgui/imgui_layer.h" +#include "imgui/imgui_texture.h" namespace Libraries::NpTrophy { +class TrophyUI final : public ImGui::Layer { +public: + TrophyUI(std::filesystem::path trophyIconPath, std::string trophyName); + ~TrophyUI() override; + + void Finish(); + + void Draw() override; + +private: + std::filesystem::path trophy_icon_path; + std::string trophy_name; + double trophy_start_time; + double trophy_time_now; + ImGui::RefCountedTexture trophy_icon; +}; + struct TrophyInfo { std::filesystem::path trophy_icon_path; std::string trophy_name; }; -class TrophyUI final : public ImGui::Layer { - std::vector trophy_queue; - -public: - TrophyUI(); - ~TrophyUI() override; - - void AddTrophyToQueue(std::filesystem::path trophyIconPath, std::string trophyName); - - void Finish(); - - void Draw() override; -}; +void AddTrophyToQueue(std::filesystem::path trophyIconPath, std::string trophyName); }; // namespace Libraries::NpTrophy \ No newline at end of file diff --git a/src/qt_gui/trophy_viewer.cpp b/src/qt_gui/trophy_viewer.cpp index 528beee0d..31ea03b17 100644 --- a/src/qt_gui/trophy_viewer.cpp +++ b/src/qt_gui/trophy_viewer.cpp @@ -79,7 +79,7 @@ void TrophyViewer::PopulateTrophyWidget(QString title) { trpType.append(reader.attributes().value("ttype").toString()); trpPid.append(reader.attributes().value("pid").toString()); if (reader.attributes().hasAttribute("unlockstate")) { - if (reader.attributes().value("unlockstate").toString() == "unlocked") { + if (reader.attributes().value("unlockstate").toString() == "true") { trpUnlocked.append("unlocked"); } else { trpUnlocked.append("locked");