From ebe77f149f4db747347811f769cbb1866639bf51 Mon Sep 17 00:00:00 2001 From: LillyJadeKatrin Date: Fri, 2 Jun 2023 21:20:42 -0400 Subject: [PATCH] Added AchievementHeaderWidget to AchievementsWindow This widget displays a header on the AchievementsWindow dialog above the tabs that shows the currently logged in user (if there is one) and the game they are playing (if there is one). --- .../Achievements/AchievementHeaderWidget.cpp | 140 ++++++++++++++++++ .../Achievements/AchievementHeaderWidget.h | 42 ++++++ .../Achievements/AchievementsWindow.cpp | 5 + .../Achievements/AchievementsWindow.h | 2 + Source/Core/DolphinQt/CMakeLists.txt | 2 + Source/Core/DolphinQt/DolphinQt.vcxproj | 2 + 6 files changed, 193 insertions(+) create mode 100644 Source/Core/DolphinQt/Achievements/AchievementHeaderWidget.cpp create mode 100644 Source/Core/DolphinQt/Achievements/AchievementHeaderWidget.h diff --git a/Source/Core/DolphinQt/Achievements/AchievementHeaderWidget.cpp b/Source/Core/DolphinQt/Achievements/AchievementHeaderWidget.cpp new file mode 100644 index 0000000000..4dc3e66538 --- /dev/null +++ b/Source/Core/DolphinQt/Achievements/AchievementHeaderWidget.cpp @@ -0,0 +1,140 @@ +// Copyright 2023 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifdef USE_RETRO_ACHIEVEMENTS +#include "DolphinQt/Achievements/AchievementHeaderWidget.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include "Core/AchievementManager.h" +#include "Core/Core.h" + +#include "DolphinQt/QtUtils/ModalMessageBox.h" +#include "DolphinQt/QtUtils/NonDefaultQPushButton.h" +#include "DolphinQt/QtUtils/SignalBlocking.h" +#include "DolphinQt/Settings.h" + +AchievementHeaderWidget::AchievementHeaderWidget(QWidget* parent) : QWidget(parent) +{ + m_user_name = new QLabel(); + m_user_points = new QLabel(); + m_game_name = new QLabel(); + m_game_points = new QLabel(); + m_game_progress_hard = new QProgressBar(); + m_game_progress_soft = new QProgressBar(); + m_rich_presence = new QLabel(); + + QVBoxLayout* m_user_right_col = new QVBoxLayout(); + m_user_right_col->addWidget(m_user_name); + m_user_right_col->addWidget(m_user_points); + QHBoxLayout* m_user_layout = new QHBoxLayout(); + // TODO: player badge goes here + m_user_layout->addLayout(m_user_right_col); + m_user_box = new QGroupBox(); + m_user_box->setLayout(m_user_layout); + + QVBoxLayout* m_game_right_col = new QVBoxLayout(); + m_game_right_col->addWidget(m_game_name); + m_game_right_col->addWidget(m_game_points); + m_game_right_col->addWidget(m_game_progress_hard); + m_game_right_col->addWidget(m_game_progress_soft); + QHBoxLayout* m_game_upper_row = new QHBoxLayout(); + // TODO: player badge and game badge go here + m_game_upper_row->addLayout(m_game_right_col); + QVBoxLayout* m_game_layout = new QVBoxLayout(); + m_game_layout->addLayout(m_game_upper_row); + m_game_layout->addWidget(m_rich_presence); + m_game_box = new QGroupBox(); + m_game_box->setLayout(m_game_layout); + + QVBoxLayout* m_total = new QVBoxLayout(); + m_total->addWidget(m_user_box); + m_total->addWidget(m_game_box); + + UpdateData(); + + m_total->setContentsMargins(0, 0, 0, 0); + m_total->setAlignment(Qt::AlignTop); + setLayout(m_total); +} + +void AchievementHeaderWidget::UpdateData() +{ + if (!AchievementManager::GetInstance()->IsLoggedIn()) + { + m_user_box->setVisible(false); + m_game_box->setVisible(false); + return; + } + + QString user_name = + QString::fromStdString(AchievementManager::GetInstance()->GetPlayerDisplayName()); + m_user_name->setText(user_name); + m_user_points->setText(tr("%1 points").arg(AchievementManager::GetInstance()->GetPlayerScore())); + + if (!AchievementManager::GetInstance()->IsGameLoaded()) + { + m_user_box->setVisible(true); + m_game_box->setVisible(false); + return; + } + + AchievementManager::PointSpread point_spread = AchievementManager::GetInstance()->TallyScore(); + m_game_name->setText( + QString::fromStdString(AchievementManager::GetInstance()->GetGameDisplayName())); + m_game_points->setText(GetPointsString(user_name, point_spread)); + m_game_progress_hard = new QProgressBar(); + m_game_progress_hard->setRange(0, point_spread.total_count); + m_game_progress_soft->setValue(point_spread.hard_unlocks); + m_game_progress_soft->setRange(0, point_spread.total_count); + m_game_progress_soft->setValue(point_spread.hard_unlocks + point_spread.soft_unlocks); + // TODO: RP needs a minor refactor to work here, will be a future PR + // m_rich_presence->setText(QString::fromStdString(AchievementManager::GetInstance()->GenerateRichPresence())); + // m_rich_presence->setVisible(Config::Get(Config::RA_RICH_PRESENCE_ENABLED)); + m_rich_presence->setText(QString{}); + m_rich_presence->setVisible(false); + + m_user_box->setVisible(false); + m_game_box->setVisible(true); +} + +QString +AchievementHeaderWidget::GetPointsString(const QString& user_name, + const AchievementManager::PointSpread& point_spread) const +{ + if (point_spread.soft_points > 0) + { + return tr("%1 has unlocked %2/%3 achievements (%4 hardcore) worth %5/%6 points (%7 hardcore)") + .arg(user_name) + .arg(point_spread.hard_unlocks + point_spread.soft_unlocks) + .arg(point_spread.total_count) + .arg(point_spread.hard_unlocks) + .arg(point_spread.hard_points + point_spread.soft_points) + .arg(point_spread.total_points) + .arg(point_spread.hard_points); + } + else + { + return tr("%1 has unlocked %2/%3 achievements worth %4/%5 points") + .arg(user_name) + .arg(point_spread.hard_unlocks) + .arg(point_spread.total_count) + .arg(point_spread.hard_points) + .arg(point_spread.total_points); + } +} + +#endif // USE_RETRO_ACHIEVEMENTS diff --git a/Source/Core/DolphinQt/Achievements/AchievementHeaderWidget.h b/Source/Core/DolphinQt/Achievements/AchievementHeaderWidget.h new file mode 100644 index 0000000000..b99457f2f1 --- /dev/null +++ b/Source/Core/DolphinQt/Achievements/AchievementHeaderWidget.h @@ -0,0 +1,42 @@ +// Copyright 2023 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#ifdef USE_RETRO_ACHIEVEMENTS +#include + +#include "Core/AchievementManager.h" + +class QGroupBox; +class QLabel; +class QProgressBar; +class QVBoxLayout; + +class AchievementHeaderWidget final : public QWidget +{ + Q_OBJECT +public: + explicit AchievementHeaderWidget(QWidget* parent); + void UpdateData(); + +private: + QString GetPointsString(const QString& user_name, + const AchievementManager::PointSpread& point_spread) const; + + QGroupBox* m_common_box; + QVBoxLayout* m_common_layout; + + QLabel* m_user_name; + QLabel* m_user_points; + QLabel* m_game_name; + QLabel* m_game_points; + QProgressBar* m_game_progress_hard; + QProgressBar* m_game_progress_soft; + QLabel* m_rich_presence; + + QGroupBox* m_user_box; + QGroupBox* m_game_box; +}; + +#endif // USE_RETRO_ACHIEVEMENTS diff --git a/Source/Core/DolphinQt/Achievements/AchievementsWindow.cpp b/Source/Core/DolphinQt/Achievements/AchievementsWindow.cpp index b23843f75b..1d08ede223 100644 --- a/Source/Core/DolphinQt/Achievements/AchievementsWindow.cpp +++ b/Source/Core/DolphinQt/Achievements/AchievementsWindow.cpp @@ -8,6 +8,7 @@ #include #include +#include "DolphinQt/Achievements/AchievementHeaderWidget.h" #include "DolphinQt/Achievements/AchievementSettingsWidget.h" #include "DolphinQt/QtUtils/WrapInScrollArea.h" @@ -30,6 +31,7 @@ void AchievementsWindow::CreateMainLayout() { auto* layout = new QVBoxLayout(); + m_header_widget = new AchievementHeaderWidget(this); m_tab_widget = new QTabWidget(); m_tab_widget->addTab( GetWrappedWidget(new AchievementSettingsWidget(m_tab_widget, this), this, 125, 100), @@ -37,6 +39,7 @@ void AchievementsWindow::CreateMainLayout() m_button_box = new QDialogButtonBox(QDialogButtonBox::Close); + layout->addWidget(m_header_widget); layout->addWidget(m_tab_widget); layout->addWidget(m_button_box); @@ -50,6 +53,8 @@ void AchievementsWindow::ConnectWidgets() void AchievementsWindow::UpdateData() { + m_header_widget->UpdateData(); + m_header_widget->setVisible(AchievementManager::GetInstance()->IsLoggedIn()); update(); } diff --git a/Source/Core/DolphinQt/Achievements/AchievementsWindow.h b/Source/Core/DolphinQt/Achievements/AchievementsWindow.h index 9570e8127f..0e9a8bfdbe 100644 --- a/Source/Core/DolphinQt/Achievements/AchievementsWindow.h +++ b/Source/Core/DolphinQt/Achievements/AchievementsWindow.h @@ -6,6 +6,7 @@ #ifdef USE_RETRO_ACHIEVEMENTS #include +class AchievementHeaderWidget; class QTabWidget; class QDialogButtonBox; @@ -21,6 +22,7 @@ private: void showEvent(QShowEvent* event); void ConnectWidgets(); + AchievementHeaderWidget* m_header_widget; QTabWidget* m_tab_widget; QDialogButtonBox* m_button_box; }; diff --git a/Source/Core/DolphinQt/CMakeLists.txt b/Source/Core/DolphinQt/CMakeLists.txt index e302287677..6ce4591cc6 100644 --- a/Source/Core/DolphinQt/CMakeLists.txt +++ b/Source/Core/DolphinQt/CMakeLists.txt @@ -27,6 +27,8 @@ add_executable(dolphin-emu CheatSearchWidget.h CheatsManager.cpp CheatsManager.h + Achievements/AchievementHeaderWidget.cpp + Achievements/AchievementHeaderWidget.h Achievements/AchievementSettingsWidget.cpp Achievements/AchievementSettingsWidget.h Achievements/AchievementsWindow.cpp diff --git a/Source/Core/DolphinQt/DolphinQt.vcxproj b/Source/Core/DolphinQt/DolphinQt.vcxproj index 79b05b372b..1d6512692e 100644 --- a/Source/Core/DolphinQt/DolphinQt.vcxproj +++ b/Source/Core/DolphinQt/DolphinQt.vcxproj @@ -50,6 +50,7 @@ + @@ -256,6 +257,7 @@ +