Added AchievementProgressWidget to AchievementsWindow

This widget is a tab in the AchievementsWindow that displays the player's current achievement progress: which achievements are locked or unlocked, and the progress of achievements that have progress metrics.
This commit is contained in:
LillyJadeKatrin 2023-06-02 21:25:01 -04:00
parent ebe77f149f
commit 582042de1f
6 changed files with 173 additions and 0 deletions

View File

@ -0,0 +1,124 @@
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#ifdef USE_RETRO_ACHIEVEMENTS
#include "DolphinQt/Achievements/AchievementProgressWidget.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/Config/AchievementSettings.h"
#include "Core/Config/MainSettings.h"
#include "Core/Core.h"
#include "DolphinQt/Config/ControllerInterface/ControllerInterfaceWindow.h"
#include "DolphinQt/QtUtils/ModalMessageBox.h"
#include "DolphinQt/QtUtils/NonDefaultQPushButton.h"
#include "DolphinQt/QtUtils/SignalBlocking.h"
#include "DolphinQt/Settings.h"
AchievementProgressWidget::AchievementProgressWidget(QWidget* parent) : QWidget(parent)
{
m_common_box = new QGroupBox();
m_common_layout = new QVBoxLayout();
UpdateData();
m_common_box->setLayout(m_common_layout);
auto* layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
layout->setAlignment(Qt::AlignTop);
layout->addWidget(m_common_box);
setLayout(layout);
}
QGroupBox*
AchievementProgressWidget::CreateAchievementBox(const rc_api_achievement_definition_t* achievement)
{
QLabel* a_title = new QLabel(QString::fromUtf8(achievement->title, strlen(achievement->title)));
QLabel* a_description =
new QLabel(QString::fromUtf8(achievement->description, strlen(achievement->description)));
QLabel* a_points = new QLabel(tr("%1 points").arg(achievement->points));
QLabel* a_status = new QLabel(GetStatusString(achievement->id));
QProgressBar* a_progress_bar = new QProgressBar();
unsigned int value = 0;
unsigned int target = 0;
AchievementManager::GetInstance()->GetAchievementProgress(achievement->id, &value, &target);
if (target > 0)
{
a_progress_bar->setRange(0, target);
a_progress_bar->setValue(value);
}
else
{
a_progress_bar->setVisible(false);
}
QVBoxLayout* a_col_right = new QVBoxLayout();
a_col_right->addWidget(a_title);
a_col_right->addWidget(a_description);
a_col_right->addWidget(a_points);
a_col_right->addWidget(a_status);
a_col_right->addWidget(a_progress_bar);
QHBoxLayout* a_total = new QHBoxLayout();
// TODO: achievement badge goes here
a_total->addLayout(a_col_right);
QGroupBox* a_group_box = new QGroupBox();
a_group_box->setLayout(a_total);
return a_group_box;
}
void AchievementProgressWidget::UpdateData()
{
QLayoutItem* item;
while ((item = m_common_layout->layout()->takeAt(0)) != nullptr)
{
delete item->widget();
delete item;
}
const auto* game_data = AchievementManager::GetInstance()->GetGameData();
for (u32 ix = 0; ix < game_data->num_achievements; ix++)
{
m_common_layout->addWidget(CreateAchievementBox(game_data->achievements + ix));
}
}
QString AchievementProgressWidget::GetStatusString(u32 achievement_id) const
{
const auto unlock_status = AchievementManager::GetInstance()->GetUnlockStatus(achievement_id);
if (unlock_status.session_unlock_count > 0)
{
if (Config::Get(Config::RA_ENCORE_ENABLED))
{
return tr("Unlocked %1 times this session").arg(unlock_status.session_unlock_count);
}
return tr("Unlocked this session");
}
switch (unlock_status.remote_unlock_status)
{
case AchievementManager::UnlockStatus::UnlockType::LOCKED:
return tr("Locked");
case AchievementManager::UnlockStatus::UnlockType::SOFTCORE:
return tr("Unlocked (Casual)");
case AchievementManager::UnlockStatus::UnlockType::HARDCORE:
return tr("Unlocked");
}
return {};
}
#endif // USE_RETRO_ACHIEVEMENTS

View File

@ -0,0 +1,34 @@
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#ifdef USE_RETRO_ACHIEVEMENTS
#include <QWidget>
#include "Common/CommonTypes.h"
class QCheckBox;
class QGroupBox;
class QLineEdit;
class QPushButton;
class QVBoxLayout;
struct rc_api_achievement_definition_t;
class AchievementProgressWidget final : public QWidget
{
Q_OBJECT
public:
explicit AchievementProgressWidget(QWidget* parent);
void UpdateData();
private:
QGroupBox* CreateAchievementBox(const rc_api_achievement_definition_t* achievement);
QString GetStatusString(u32 achievement_id) const;
QGroupBox* m_common_box;
QVBoxLayout* m_common_layout;
};
#endif // USE_RETRO_ACHIEVEMENTS

View File

@ -9,6 +9,7 @@
#include <QVBoxLayout>
#include "DolphinQt/Achievements/AchievementHeaderWidget.h"
#include "DolphinQt/Achievements/AchievementProgressWidget.h"
#include "DolphinQt/Achievements/AchievementSettingsWidget.h"
#include "DolphinQt/QtUtils/WrapInScrollArea.h"
@ -33,9 +34,12 @@ void AchievementsWindow::CreateMainLayout()
m_header_widget = new AchievementHeaderWidget(this);
m_tab_widget = new QTabWidget();
m_progress_widget = new AchievementProgressWidget(m_tab_widget);
m_tab_widget->addTab(
GetWrappedWidget(new AchievementSettingsWidget(m_tab_widget, this), this, 125, 100),
tr("Settings"));
m_tab_widget->addTab(GetWrappedWidget(m_progress_widget, this, 125, 100), tr("Progress"));
m_tab_widget->setTabVisible(1, AchievementManager::GetInstance()->IsGameLoaded());
m_button_box = new QDialogButtonBox(QDialogButtonBox::Close);
@ -55,6 +59,9 @@ void AchievementsWindow::UpdateData()
{
m_header_widget->UpdateData();
m_header_widget->setVisible(AchievementManager::GetInstance()->IsLoggedIn());
// Settings tab handles its own updates ... indeed, that calls this
m_progress_widget->UpdateData();
m_tab_widget->setTabVisible(1, AchievementManager::GetInstance()->IsGameLoaded());
update();
}

View File

@ -6,7 +6,10 @@
#ifdef USE_RETRO_ACHIEVEMENTS
#include <QDialog>
#include "Core/AchievementManager.h"
class AchievementHeaderWidget;
class AchievementProgressWidget;
class QTabWidget;
class QDialogButtonBox;
@ -24,6 +27,7 @@ private:
AchievementHeaderWidget* m_header_widget;
QTabWidget* m_tab_widget;
AchievementProgressWidget* m_progress_widget;
QDialogButtonBox* m_button_box;
};

View File

@ -29,6 +29,8 @@ add_executable(dolphin-emu
CheatsManager.h
Achievements/AchievementHeaderWidget.cpp
Achievements/AchievementHeaderWidget.h
Achievements/AchievementProgressWidget.cpp
Achievements/AchievementProgressWidget.h
Achievements/AchievementSettingsWidget.cpp
Achievements/AchievementSettingsWidget.h
Achievements/AchievementsWindow.cpp

View File

@ -51,6 +51,7 @@
<ClCompile Include="CheatSearchWidget.cpp" />
<ClCompile Include="CheatsManager.cpp" />
<ClCompile Include="Achievements\AchievementHeaderWidget.cpp" />
<ClCompile Include="Achievements\AchievementProgressWidget.cpp" />
<ClCompile Include="Achievements\AchievementSettingsWidget.cpp" />
<ClCompile Include="Achievements\AchievementsWindow.cpp" />
<ClCompile Include="Config\ARCodeWidget.cpp" />
@ -258,6 +259,7 @@
<QtMoc Include="CheatSearchWidget.h" />
<QtMoc Include="CheatsManager.h" />
<QtMoc Include="Achievements\AchievementHeaderWidget.h" />
<QtMoc Include="Achievements\AchievementProgressWidget.h" />
<QtMoc Include="Achievements\AchievementSettingsWidget.h" />
<QtMoc Include="Achievements\AchievementsWindow.h" />
<QtMoc Include="Config\ARCodeWidget.h" />