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).
This commit is contained in:
LillyJadeKatrin 2023-06-02 21:20:42 -04:00
parent f700faf6ab
commit ebe77f149f
6 changed files with 193 additions and 0 deletions

View File

@ -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 <QCheckBox>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QProgressBar>
#include <QPushButton>
#include <QString>
#include <QVBoxLayout>
#include <fmt/format.h>
#include <rcheevos/include/rc_api_runtime.h>
#include <rcheevos/include/rc_api_user.h>
#include <rcheevos/include/rc_runtime.h>
#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

View File

@ -0,0 +1,42 @@
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#ifdef USE_RETRO_ACHIEVEMENTS
#include <QWidget>
#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

View File

@ -8,6 +8,7 @@
#include <QTabWidget>
#include <QVBoxLayout>
#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();
}

View File

@ -6,6 +6,7 @@
#ifdef USE_RETRO_ACHIEVEMENTS
#include <QDialog>
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;
};

View File

@ -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

View File

@ -50,6 +50,7 @@
<ClCompile Include="CheatSearchFactoryWidget.cpp" />
<ClCompile Include="CheatSearchWidget.cpp" />
<ClCompile Include="CheatsManager.cpp" />
<ClCompile Include="Achievements\AchievementHeaderWidget.cpp" />
<ClCompile Include="Achievements\AchievementSettingsWidget.cpp" />
<ClCompile Include="Achievements\AchievementsWindow.cpp" />
<ClCompile Include="Config\ARCodeWidget.cpp" />
@ -256,6 +257,7 @@
<QtMoc Include="CheatSearchFactoryWidget.h" />
<QtMoc Include="CheatSearchWidget.h" />
<QtMoc Include="CheatsManager.h" />
<QtMoc Include="Achievements\AchievementHeaderWidget.h" />
<QtMoc Include="Achievements\AchievementSettingsWidget.h" />
<QtMoc Include="Achievements\AchievementsWindow.h" />
<QtMoc Include="Config\ARCodeWidget.h" />