implement vinicius suggestions

This commit is contained in:
CrazyBloo 2024-09-30 20:08:04 -04:00
parent 358e63d5e6
commit 2e2bfe8db4
4 changed files with 77 additions and 78 deletions

View file

@ -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);
}
}

View file

@ -4,14 +4,25 @@
#include <chrono>
#include <imgui.h>
#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<TrophyUI> current_trophy_ui;
std::queue<TrophyInfo> 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<std::chrono::seconds>(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

View file

@ -5,32 +5,38 @@
#include <string>
#include <variant>
#include <vector>
#include <queue>
#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<TrophyInfo> 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

View file

@ -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");