2018-01-01 20:01:58 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-01-01 20:01:58 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/ARCodeWidget.h"
|
2024-03-31 19:54:47 +00:00
|
|
|
#include "DolphinQt/QtUtils/WrapInScrollArea.h"
|
2018-01-01 20:01:58 +00:00
|
|
|
|
2023-04-26 08:53:29 +00:00
|
|
|
#include <algorithm>
|
2021-06-06 19:18:02 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2019-06-21 07:28:32 +00:00
|
|
|
#include <QCursor>
|
2018-01-01 20:01:58 +00:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QListWidget>
|
2019-06-21 07:28:32 +00:00
|
|
|
#include <QMenu>
|
2018-01-01 20:01:58 +00:00
|
|
|
#include <QPushButton>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/IniFile.h"
|
2018-05-28 01:48:04 +00:00
|
|
|
|
2018-01-01 20:01:58 +00:00
|
|
|
#include "Core/ActionReplay.h"
|
|
|
|
#include "Core/ConfigManager.h"
|
2018-05-28 01:48:04 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/CheatCodeEditor.h"
|
|
|
|
#include "DolphinQt/Config/CheatWarningWidget.h"
|
2023-06-08 01:53:38 +00:00
|
|
|
#include "DolphinQt/Config/HardcoreWarningWidget.h"
|
2022-03-08 07:51:29 +00:00
|
|
|
#include "DolphinQt/QtUtils/NonDefaultQPushButton.h"
|
2023-07-30 22:42:15 +00:00
|
|
|
#include "DolphinQt/QtUtils/SetWindowDecorations.h"
|
2018-05-28 01:48:04 +00:00
|
|
|
|
2017-12-31 19:33:36 +00:00
|
|
|
#include "UICommon/GameFile.h"
|
2018-01-01 20:01:58 +00:00
|
|
|
|
2021-06-06 19:18:02 +00:00
|
|
|
ARCodeWidget::ARCodeWidget(std::string game_id, u16 game_revision, bool restart_required)
|
|
|
|
: m_game_id(std::move(game_id)), m_game_revision(game_revision),
|
2018-03-26 02:17:47 +00:00
|
|
|
m_restart_required(restart_required)
|
2018-01-01 20:01:58 +00:00
|
|
|
{
|
|
|
|
CreateWidgets();
|
|
|
|
ConnectWidgets();
|
|
|
|
|
2021-09-16 05:12:27 +00:00
|
|
|
if (!m_game_id.empty())
|
|
|
|
{
|
2023-04-13 13:38:09 +00:00
|
|
|
Common::IniFile game_ini_local;
|
2018-01-01 20:01:58 +00:00
|
|
|
|
2021-09-16 05:12:27 +00:00
|
|
|
// We don't use LoadLocalGameIni() here because user cheat codes that are installed via the UI
|
|
|
|
// will always be stored in GS/${GAMEID}.ini
|
|
|
|
game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini");
|
2018-01-01 20:01:58 +00:00
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
const Common::IniFile game_ini_default =
|
|
|
|
SConfig::LoadDefaultGameIni(m_game_id, m_game_revision);
|
2021-09-16 05:12:27 +00:00
|
|
|
m_ar_codes = ActionReplay::LoadCodes(game_ini_default, game_ini_local);
|
|
|
|
}
|
2018-01-01 20:01:58 +00:00
|
|
|
|
|
|
|
UpdateList();
|
|
|
|
OnSelectionChanged();
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:44:16 +00:00
|
|
|
ARCodeWidget::~ARCodeWidget() = default;
|
|
|
|
|
2018-01-01 20:01:58 +00:00
|
|
|
void ARCodeWidget::CreateWidgets()
|
|
|
|
{
|
2018-05-18 05:17:30 +00:00
|
|
|
m_warning = new CheatWarningWidget(m_game_id, m_restart_required, this);
|
2023-06-08 01:53:38 +00:00
|
|
|
#ifdef USE_RETRO_ACHIEVEMENTS
|
|
|
|
m_hc_warning = new HardcoreWarningWidget(this);
|
|
|
|
#endif // USE_RETRO_ACHIEVEMENTS
|
2018-01-01 20:01:58 +00:00
|
|
|
m_code_list = new QListWidget;
|
2022-03-08 07:51:29 +00:00
|
|
|
m_code_add = new NonDefaultQPushButton(tr("&Add New Code..."));
|
|
|
|
m_code_edit = new NonDefaultQPushButton(tr("&Edit Code..."));
|
|
|
|
m_code_remove = new NonDefaultQPushButton(tr("&Remove Code"));
|
2018-01-01 20:01:58 +00:00
|
|
|
|
2021-09-16 05:12:27 +00:00
|
|
|
m_code_list->setEnabled(!m_game_id.empty());
|
|
|
|
m_code_add->setEnabled(!m_game_id.empty());
|
|
|
|
m_code_edit->setEnabled(!m_game_id.empty());
|
|
|
|
m_code_remove->setEnabled(!m_game_id.empty());
|
|
|
|
|
2019-06-21 07:28:32 +00:00
|
|
|
m_code_list->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
2018-01-01 20:01:58 +00:00
|
|
|
auto* button_layout = new QHBoxLayout;
|
|
|
|
|
|
|
|
button_layout->addWidget(m_code_add);
|
|
|
|
button_layout->addWidget(m_code_edit);
|
|
|
|
button_layout->addWidget(m_code_remove);
|
|
|
|
|
|
|
|
QVBoxLayout* layout = new QVBoxLayout;
|
|
|
|
|
|
|
|
layout->addWidget(m_warning);
|
2023-06-08 01:53:38 +00:00
|
|
|
#ifdef USE_RETRO_ACHIEVEMENTS
|
|
|
|
layout->addWidget(m_hc_warning);
|
|
|
|
#endif // USE_RETRO_ACHIEVEMENTS
|
2018-01-01 20:01:58 +00:00
|
|
|
layout->addWidget(m_code_list);
|
|
|
|
layout->addLayout(button_layout);
|
|
|
|
|
2024-03-31 19:54:47 +00:00
|
|
|
WrapInScrollArea(this, layout);
|
2018-01-01 20:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARCodeWidget::ConnectWidgets()
|
|
|
|
{
|
|
|
|
connect(m_warning, &CheatWarningWidget::OpenCheatEnableSettings, this,
|
|
|
|
&ARCodeWidget::OpenGeneralSettings);
|
2023-06-08 01:53:38 +00:00
|
|
|
#ifdef USE_RETRO_ACHIEVEMENTS
|
|
|
|
connect(m_hc_warning, &HardcoreWarningWidget::OpenAchievementSettings, this,
|
|
|
|
&ARCodeWidget::OpenAchievementSettings);
|
|
|
|
#endif // USE_RETRO_ACHIEVEMENTS
|
2019-06-21 07:28:32 +00:00
|
|
|
|
2018-01-01 20:01:58 +00:00
|
|
|
connect(m_code_list, &QListWidget::itemChanged, this, &ARCodeWidget::OnItemChanged);
|
|
|
|
connect(m_code_list, &QListWidget::itemSelectionChanged, this, &ARCodeWidget::OnSelectionChanged);
|
2019-06-21 00:25:30 +00:00
|
|
|
connect(m_code_list->model(), &QAbstractItemModel::rowsMoved, this,
|
|
|
|
&ARCodeWidget::OnListReordered);
|
2019-06-21 07:28:32 +00:00
|
|
|
connect(m_code_list, &QListWidget::customContextMenuRequested, this,
|
|
|
|
&ARCodeWidget::OnContextMenuRequested);
|
2018-01-01 20:01:58 +00:00
|
|
|
|
2019-07-23 22:18:58 +00:00
|
|
|
connect(m_code_add, &QPushButton::clicked, this, &ARCodeWidget::OnCodeAddClicked);
|
|
|
|
connect(m_code_edit, &QPushButton::clicked, this, &ARCodeWidget::OnCodeEditClicked);
|
|
|
|
connect(m_code_remove, &QPushButton::clicked, this, &ARCodeWidget::OnCodeRemoveClicked);
|
2018-01-01 20:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARCodeWidget::OnItemChanged(QListWidgetItem* item)
|
|
|
|
{
|
2020-12-10 11:58:27 +00:00
|
|
|
m_ar_codes[m_code_list->row(item)].enabled = (item->checkState() == Qt::Checked);
|
2018-03-26 02:17:47 +00:00
|
|
|
|
|
|
|
if (!m_restart_required)
|
|
|
|
ActionReplay::ApplyCodes(m_ar_codes);
|
|
|
|
|
2019-06-21 00:25:30 +00:00
|
|
|
UpdateList();
|
|
|
|
SaveCodes();
|
|
|
|
}
|
|
|
|
|
2019-06-21 07:28:32 +00:00
|
|
|
void ARCodeWidget::OnContextMenuRequested()
|
|
|
|
{
|
|
|
|
QMenu menu;
|
|
|
|
|
|
|
|
menu.addAction(tr("Sort Alphabetically"), this, &ARCodeWidget::SortAlphabetically);
|
2023-04-26 08:53:29 +00:00
|
|
|
menu.addAction(tr("Show Enabled Codes First"), this, &ARCodeWidget::SortEnabledCodesFirst);
|
|
|
|
menu.addAction(tr("Show Disabled Codes First"), this, &ARCodeWidget::SortDisabledCodesFirst);
|
2019-06-21 07:28:32 +00:00
|
|
|
|
|
|
|
menu.exec(QCursor::pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ARCodeWidget::SortAlphabetically()
|
|
|
|
{
|
|
|
|
m_code_list->sortItems();
|
|
|
|
OnListReordered();
|
|
|
|
}
|
|
|
|
|
2023-04-26 08:53:29 +00:00
|
|
|
void ARCodeWidget::SortEnabledCodesFirst()
|
|
|
|
{
|
|
|
|
std::stable_sort(m_ar_codes.begin(), m_ar_codes.end(), [](const auto& a, const auto& b) {
|
|
|
|
return a.enabled && a.enabled != b.enabled;
|
|
|
|
});
|
|
|
|
|
|
|
|
UpdateList();
|
|
|
|
SaveCodes();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ARCodeWidget::SortDisabledCodesFirst()
|
|
|
|
{
|
|
|
|
std::stable_sort(m_ar_codes.begin(), m_ar_codes.end(), [](const auto& a, const auto& b) {
|
|
|
|
return !a.enabled && a.enabled != b.enabled;
|
|
|
|
});
|
|
|
|
|
|
|
|
UpdateList();
|
|
|
|
SaveCodes();
|
|
|
|
}
|
|
|
|
|
2019-06-21 00:25:30 +00:00
|
|
|
void ARCodeWidget::OnListReordered()
|
|
|
|
{
|
|
|
|
// Reorder codes based on the indices of table item
|
|
|
|
std::vector<ActionReplay::ARCode> codes;
|
|
|
|
codes.reserve(m_ar_codes.size());
|
|
|
|
|
|
|
|
for (int i = 0; i < m_code_list->count(); i++)
|
|
|
|
{
|
|
|
|
const int index = m_code_list->item(i)->data(Qt::UserRole).toInt();
|
|
|
|
|
|
|
|
codes.push_back(std::move(m_ar_codes[index]));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ar_codes = std::move(codes);
|
|
|
|
|
2018-01-01 20:01:58 +00:00
|
|
|
SaveCodes();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ARCodeWidget::OnSelectionChanged()
|
|
|
|
{
|
|
|
|
auto items = m_code_list->selectedItems();
|
|
|
|
|
|
|
|
if (items.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto* selected = items[0];
|
|
|
|
|
|
|
|
bool user_defined = m_ar_codes[m_code_list->row(selected)].user_defined;
|
|
|
|
|
|
|
|
m_code_remove->setEnabled(user_defined);
|
2018-01-24 19:38:41 +00:00
|
|
|
m_code_edit->setText(user_defined ? tr("&Edit Code...") : tr("Clone and &Edit Code..."));
|
2018-01-01 20:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARCodeWidget::UpdateList()
|
|
|
|
{
|
|
|
|
m_code_list->clear();
|
|
|
|
|
2019-06-21 00:25:30 +00:00
|
|
|
for (size_t i = 0; i < m_ar_codes.size(); i++)
|
2018-01-01 20:01:58 +00:00
|
|
|
{
|
2019-06-21 00:25:30 +00:00
|
|
|
const auto& ar = m_ar_codes[i];
|
2018-01-01 20:01:58 +00:00
|
|
|
auto* item = new QListWidgetItem(QString::fromStdString(ar.name)
|
2019-07-30 11:57:06 +00:00
|
|
|
.replace(QStringLiteral("<"), QChar::fromLatin1('<'))
|
|
|
|
.replace(QStringLiteral(">"), QChar::fromLatin1('>')));
|
2018-01-01 20:01:58 +00:00
|
|
|
|
2019-06-21 00:25:30 +00:00
|
|
|
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable |
|
|
|
|
Qt::ItemIsDragEnabled);
|
2020-12-10 11:58:27 +00:00
|
|
|
item->setCheckState(ar.enabled ? Qt::Checked : Qt::Unchecked);
|
2019-06-21 00:25:30 +00:00
|
|
|
item->setData(Qt::UserRole, static_cast<int>(i));
|
2018-01-01 20:01:58 +00:00
|
|
|
|
|
|
|
m_code_list->addItem(item);
|
|
|
|
}
|
2019-06-21 00:25:30 +00:00
|
|
|
|
|
|
|
m_code_list->setDragDropMode(QAbstractItemView::InternalMove);
|
2018-01-01 20:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARCodeWidget::SaveCodes()
|
|
|
|
{
|
2021-09-16 05:12:27 +00:00
|
|
|
if (m_game_id.empty())
|
|
|
|
return;
|
|
|
|
|
2019-07-31 12:35:57 +00:00
|
|
|
const auto ini_path =
|
|
|
|
std::string(File::GetUserPath(D_GAMESETTINGS_IDX)).append(m_game_id).append(".ini");
|
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
Common::IniFile game_ini_local;
|
2019-07-31 12:35:57 +00:00
|
|
|
game_ini_local.Load(ini_path);
|
2018-01-01 20:01:58 +00:00
|
|
|
ActionReplay::SaveCodes(&game_ini_local, m_ar_codes);
|
2019-07-31 12:35:57 +00:00
|
|
|
game_ini_local.Save(ini_path);
|
2018-03-26 02:17:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARCodeWidget::AddCode(ActionReplay::ARCode code)
|
|
|
|
{
|
|
|
|
m_ar_codes.push_back(std::move(code));
|
|
|
|
|
|
|
|
UpdateList();
|
|
|
|
SaveCodes();
|
2018-01-01 20:01:58 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 22:18:58 +00:00
|
|
|
void ARCodeWidget::OnCodeAddClicked()
|
2018-01-01 20:01:58 +00:00
|
|
|
{
|
|
|
|
ActionReplay::ARCode ar;
|
2020-12-10 11:58:27 +00:00
|
|
|
ar.enabled = true;
|
2018-01-01 20:01:58 +00:00
|
|
|
|
2018-05-06 16:02:39 +00:00
|
|
|
CheatCodeEditor ed(this);
|
2018-01-24 12:35:44 +00:00
|
|
|
ed.SetARCode(&ar);
|
2023-07-30 22:42:15 +00:00
|
|
|
SetQWidgetWindowDecorations(&ed);
|
2019-07-31 13:01:35 +00:00
|
|
|
if (ed.exec() == QDialog::Rejected)
|
|
|
|
return;
|
2018-01-01 20:01:58 +00:00
|
|
|
|
2019-07-31 13:01:35 +00:00
|
|
|
m_ar_codes.push_back(std::move(ar));
|
2018-01-01 20:01:58 +00:00
|
|
|
|
2019-07-31 13:01:35 +00:00
|
|
|
UpdateList();
|
|
|
|
SaveCodes();
|
2018-01-01 20:01:58 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 22:18:58 +00:00
|
|
|
void ARCodeWidget::OnCodeEditClicked()
|
2018-01-01 20:01:58 +00:00
|
|
|
{
|
2019-07-31 13:01:35 +00:00
|
|
|
const auto items = m_code_list->selectedItems();
|
2018-01-01 20:01:58 +00:00
|
|
|
if (items.empty())
|
|
|
|
return;
|
|
|
|
|
2019-07-31 13:01:35 +00:00
|
|
|
const auto* const selected = items[0];
|
2018-01-01 20:01:58 +00:00
|
|
|
auto& current_ar = m_ar_codes[m_code_list->row(selected)];
|
|
|
|
|
2018-05-06 16:02:39 +00:00
|
|
|
CheatCodeEditor ed(this);
|
2019-07-31 13:01:35 +00:00
|
|
|
if (current_ar.user_defined)
|
|
|
|
{
|
|
|
|
ed.SetARCode(¤t_ar);
|
2018-01-01 20:01:58 +00:00
|
|
|
|
2023-07-30 22:42:15 +00:00
|
|
|
SetQWidgetWindowDecorations(&ed);
|
2019-07-31 13:01:35 +00:00
|
|
|
if (ed.exec() == QDialog::Rejected)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ActionReplay::ARCode ar = current_ar;
|
|
|
|
ed.SetARCode(&ar);
|
|
|
|
|
2023-07-30 22:42:15 +00:00
|
|
|
SetQWidgetWindowDecorations(&ed);
|
2019-07-31 13:01:35 +00:00
|
|
|
if (ed.exec() == QDialog::Rejected)
|
|
|
|
return;
|
2018-01-01 20:01:58 +00:00
|
|
|
|
2019-07-31 13:01:35 +00:00
|
|
|
m_ar_codes.push_back(std::move(ar));
|
|
|
|
}
|
2018-01-01 20:01:58 +00:00
|
|
|
|
|
|
|
SaveCodes();
|
|
|
|
UpdateList();
|
|
|
|
}
|
|
|
|
|
2019-07-23 22:18:58 +00:00
|
|
|
void ARCodeWidget::OnCodeRemoveClicked()
|
2018-01-01 20:01:58 +00:00
|
|
|
{
|
|
|
|
auto items = m_code_list->selectedItems();
|
|
|
|
|
|
|
|
if (items.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto* selected = items[0];
|
|
|
|
|
|
|
|
m_ar_codes.erase(m_ar_codes.begin() + m_code_list->row(selected));
|
|
|
|
|
|
|
|
SaveCodes();
|
|
|
|
UpdateList();
|
|
|
|
|
|
|
|
m_code_remove->setEnabled(false);
|
|
|
|
}
|