From ddc913c13adc507bd78098ffd669332f29d13a64 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Fri, 28 Aug 2020 01:49:07 -0700 Subject: [PATCH] Qt: Refactor GB names and lists into namespace --- src/platform/qt/CMakeLists.txt | 3 +- src/platform/qt/GameBoy.cpp | 94 ++++++++++++++++++++++++++ src/platform/qt/GameBoy.h | 25 +++++++ src/platform/qt/OverrideView.cpp | 78 ++++++++++------------ src/platform/qt/OverrideView.h | 3 - src/platform/qt/OverrideView.ui | 110 ------------------------------- src/platform/qt/SettingsView.cpp | 47 +++++++------ src/platform/qt/SettingsView.h | 1 - src/platform/qt/SettingsView.ui | 60 ----------------- 9 files changed, 183 insertions(+), 238 deletions(-) create mode 100644 src/platform/qt/GameBoy.cpp create mode 100644 src/platform/qt/GameBoy.h diff --git a/src/platform/qt/CMakeLists.txt b/src/platform/qt/CMakeLists.txt index a57a1c316..aacd13a50 100644 --- a/src/platform/qt/CMakeLists.txt +++ b/src/platform/qt/CMakeLists.txt @@ -16,7 +16,7 @@ if(BUILD_SDL) endif() if(POLICY CMP0071) - cmake_policy(SET CMP0071 OLD) + cmake_policy(SET CMP0071 NEW) endif() set(CMAKE_AUTOMOC ON) @@ -154,6 +154,7 @@ set(GBA_SRC GBAOverride.cpp) set(GB_SRC + GameBoy.cpp GBOverride.cpp PrinterView.cpp) diff --git a/src/platform/qt/GameBoy.cpp b/src/platform/qt/GameBoy.cpp new file mode 100644 index 000000000..63f26f1c1 --- /dev/null +++ b/src/platform/qt/GameBoy.cpp @@ -0,0 +1,94 @@ +/* Copyright (c) 2013-2020 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "GameBoy.h" + +#include +#include + +using namespace QGBA; + +static const QList s_gbModelList{ + GB_MODEL_DMG, + GB_MODEL_SGB, + GB_MODEL_CGB, + GB_MODEL_AGB, +}; + +static const QList s_mbcList{ + GB_MBC_NONE, + GB_MBC1, + GB_MBC2, + GB_MBC3, + GB_MBC3_RTC, + GB_MBC5, + GB_MBC5_RUMBLE, + GB_MBC6, + GB_MBC7, + GB_MMM01, + GB_POCKETCAM, + GB_TAMA5, + GB_HuC1, + GB_HuC3, + GB_UNL_WISDOM_TREE, + GB_UNL_BBD, + GB_UNL_HITEK, + GB_UNL_PKJD, +}; + +static QMap s_gbModelNames; +static QMap s_mbcNames; + +static QString tr(const char* str) { + return QCoreApplication::translate("Game Boy", str); +} + +QList GameBoy::modelList() { + return s_gbModelList; +} + +QString GameBoy::modelName(GBModel model) { + if (s_gbModelNames.isEmpty()) { + s_gbModelNames[GB_MODEL_AUTODETECT] = tr("Autodetect"); + s_gbModelNames[GB_MODEL_DMG] = tr("Game Boy (DMG)"); + s_gbModelNames[GB_MODEL_MGB] = tr("Game Boy Pocket (MGB)"); + s_gbModelNames[GB_MODEL_SGB] = tr("Super Game Boy (SGB)"); + s_gbModelNames[GB_MODEL_SGB2] = tr("Super Game Boy 2 (SGB)"); + s_gbModelNames[GB_MODEL_CGB] = tr("Game Boy Color (CGB)"); + s_gbModelNames[GB_MODEL_AGB] = tr("Game Boy Advance (AGB)"); + } + + return s_gbModelNames[model]; +} + +QList GameBoy::mbcList() { + return s_mbcList; +} + +QString GameBoy::mbcName(GBMemoryBankControllerType mbc) { + if (s_mbcNames.isEmpty()) { + s_mbcNames[GB_MBC_AUTODETECT] = tr("Autodetect"); + s_mbcNames[GB_MBC_NONE] = tr("ROM Only"); + s_mbcNames[GB_MBC1] = tr("MBC1"); + s_mbcNames[GB_MBC2] = tr("MBC2"); + s_mbcNames[GB_MBC3] = tr("MBC3"); + s_mbcNames[GB_MBC3_RTC] = tr("MBC3 + RTC"); + s_mbcNames[GB_MBC5] = tr("MBC5"); + s_mbcNames[GB_MBC5_RUMBLE] = tr("MBC5 + Rumble"); + s_mbcNames[GB_MBC6] = tr("MBC6"); + s_mbcNames[GB_MBC7] = tr("MBC7 (Tilt)"); + s_mbcNames[GB_MMM01] = tr("MMM01"); + s_mbcNames[GB_HuC1] = tr("HuC-1"); + s_mbcNames[GB_HuC3] = tr("HuC-3"); + s_mbcNames[GB_POCKETCAM] = tr("Pocket Cam"); + s_mbcNames[GB_TAMA5] = tr("TAMA5"); + s_mbcNames[GB_UNL_WISDOM_TREE] = tr("Wisdom Tree"); + s_mbcNames[GB_UNL_PKJD] = tr("Pokémon Jade/Diamond"); + s_mbcNames[GB_UNL_BBD] = tr("BBD"); + s_mbcNames[GB_UNL_HITEK] = tr("Hitek"); + } + + return s_mbcNames[mbc]; +} diff --git a/src/platform/qt/GameBoy.h b/src/platform/qt/GameBoy.h new file mode 100644 index 000000000..bdb31533f --- /dev/null +++ b/src/platform/qt/GameBoy.h @@ -0,0 +1,25 @@ +/* Copyright (c) 2013-2020 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#pragma once + +#include +#include + +#ifdef M_CORE_GB +#include + +namespace QGBA { + +namespace GameBoy { + QList modelList(); + QString modelName(GBModel); + + QList mbcList(); + QString mbcName(GBMemoryBankControllerType); +} + +} +#endif diff --git a/src/platform/qt/OverrideView.cpp b/src/platform/qt/OverrideView.cpp index b045caecb..d3c5b7654 100644 --- a/src/platform/qt/OverrideView.cpp +++ b/src/platform/qt/OverrideView.cpp @@ -6,6 +6,7 @@ #include "OverrideView.h" #include +#include #include "ConfigController.h" #include "CoreController.h" @@ -16,53 +17,17 @@ #endif #ifdef M_CORE_GB +#include "GameBoy.h" #include "GBOverride.h" #include #endif using namespace QGBA; -#ifdef M_CORE_GB -QList OverrideView::s_gbModelList; -QList OverrideView::s_mbcList; -#endif - OverrideView::OverrideView(ConfigController* config, QWidget* parent) : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint) , m_config(config) { -#ifdef M_CORE_GB - if (s_mbcList.isEmpty()) { - // NB: Keep in sync with OverrideView.ui - s_mbcList.append(GB_MBC_AUTODETECT); - s_mbcList.append(GB_MBC_NONE); - s_mbcList.append(GB_MBC1); - s_mbcList.append(GB_MBC2); - s_mbcList.append(GB_MBC3); - s_mbcList.append(GB_MBC3_RTC); - s_mbcList.append(GB_MBC5); - s_mbcList.append(GB_MBC5_RUMBLE); - s_mbcList.append(GB_MBC6); - s_mbcList.append(GB_MBC7); - s_mbcList.append(GB_MMM01); - s_mbcList.append(GB_POCKETCAM); - s_mbcList.append(GB_TAMA5); - s_mbcList.append(GB_HuC1); - s_mbcList.append(GB_HuC3); - s_mbcList.append(GB_UNL_WISDOM_TREE); - s_mbcList.append(GB_UNL_BBD); - s_mbcList.append(GB_UNL_HITEK); - s_mbcList.append(GB_UNL_PKJD); - } - if (s_gbModelList.isEmpty()) { - // NB: Keep in sync with OverrideView.ui - s_gbModelList.append(GB_MODEL_AUTODETECT); - s_gbModelList.append(GB_MODEL_DMG); - s_gbModelList.append(GB_MODEL_SGB); - s_gbModelList.append(GB_MODEL_CGB); - s_gbModelList.append(GB_MODEL_AGB); - } -#endif m_ui.setupUi(this); connect(m_ui.hwAutodetect, &QAbstractButton::toggled, [this] (bool enabled) { @@ -74,6 +39,33 @@ OverrideView::OverrideView(ConfigController* config, QWidget* parent) }); #ifdef M_CORE_GB + m_ui.gbModel->setItemData(0, GB_MODEL_AUTODETECT); + m_ui.mbc->setItemData(0, GB_MBC_AUTODETECT); + + for (auto model : GameBoy::modelList()) { + m_ui.gbModel->addItem(GameBoy::modelName(model), model); + } + + QStandardItemModel* model = static_cast(m_ui.mbc->model()); + int bitsSeen = 0; + for (auto mbc : GameBoy::mbcList()) { + int mbcValue = static_cast(mbc); + if ((mbcValue & ~bitsSeen) & 0x001) { + m_ui.mbc->addItem(tr("Official MBCs"), -2); + model->item(m_ui.mbc->count() - 1)->setFlags(Qt::NoItemFlags); + } + if ((mbcValue & ~bitsSeen) & 0x010) { + m_ui.mbc->addItem(tr("Licensed MBCs"), -3); + model->item(m_ui.mbc->count() - 1)->setFlags(Qt::NoItemFlags); + } + if ((mbcValue & ~bitsSeen) & 0x200) { + m_ui.mbc->addItem(tr("Unlicensed MBCs"), -4); + model->item(m_ui.mbc->count() - 1)->setFlags(Qt::NoItemFlags); + } + bitsSeen |= mbcValue; + m_ui.mbc->addItem(GameBoy::mbcName(mbc), mbc); + } + m_colorPickers[0] = ColorPicker(m_ui.color0, QColor(0xF8, 0xF8, 0xF8)); m_colorPickers[1] = ColorPicker(m_ui.color1, QColor(0xA8, 0xA8, 0xA8)); m_colorPickers[2] = ColorPicker(m_ui.color2, QColor(0x50, 0x50, 0x50)); @@ -190,8 +182,8 @@ void OverrideView::updateOverrides() { #ifdef M_CORE_GB if (m_ui.tabWidget->currentWidget() == m_ui.tabGB) { std::unique_ptr gb(new GBOverride); - gb->override.mbc = s_mbcList[m_ui.mbc->currentIndex()]; - gb->override.model = s_gbModelList[m_ui.gbModel->currentIndex()]; + gb->override.mbc = static_cast(m_ui.mbc->currentData().toInt()); + gb->override.model = static_cast(m_ui.gbModel->currentData().toInt()); bool hasColor = false; for (int i = 0; i < 12; ++i) { gb->override.gbColors[i] = m_gbColors[i]; @@ -240,13 +232,13 @@ void OverrideView::gameStarted() { case PLATFORM_GB: { m_ui.tabWidget->setCurrentWidget(m_ui.tabGB); GB* gb = static_cast(thread->core->board); - int mbc = s_mbcList.indexOf(gb->memory.mbcType); - if (mbc >= 0) { - m_ui.mbc->setCurrentIndex(mbc); + int index = m_ui.mbc->findData(gb->memory.mbcType); + if (index >= 0) { + m_ui.mbc->setCurrentIndex(index); } else { m_ui.mbc->setCurrentIndex(0); } - int model = s_gbModelList.indexOf(gb->model); + int model = m_ui.gbModel->findData(gb->model); if (model >= 0) { m_ui.gbModel->setCurrentIndex(model); } else { diff --git a/src/platform/qt/OverrideView.h b/src/platform/qt/OverrideView.h index cbface71c..880bc99bd 100644 --- a/src/platform/qt/OverrideView.h +++ b/src/platform/qt/OverrideView.h @@ -55,9 +55,6 @@ private: #ifdef M_CORE_GB uint32_t m_gbColors[12]{}; ColorPicker m_colorPickers[12]; - - static QList s_gbModelList; - static QList s_mbcList; #endif }; diff --git a/src/platform/qt/OverrideView.ui b/src/platform/qt/OverrideView.ui index 28fb357e2..12bd873f3 100644 --- a/src/platform/qt/OverrideView.ui +++ b/src/platform/qt/OverrideView.ui @@ -248,26 +248,6 @@ Autodetect - - - Game Boy (DMG) - - - - - Super Game Boy (SGB) - - - - - Game Boy Color (CGB) - - - - - Game Boy Advance (AGB) - - @@ -284,96 +264,6 @@ Autodetect - - - None - - - - - MBC1 - - - - - MBC2 - - - - - MBC3 - - - - - MBC3 + RTC - - - - - MBC5 - - - - - MBC5 + Rumble - - - - - MBC6 - - - - - MBC7 - - - - - MMM01 - - - - - Pocket Cam - - - - - TAMA5 - - - - - HuC-1 - - - - - HuC-3 - - - - - Wisdom Tree (Unlicensed) - - - - - BBD (Unlicensed) - - - - - Hitek (Unlicensed) - - - - - Pokémon Jade/Diamond (Unlicensed) - - diff --git a/src/platform/qt/SettingsView.cpp b/src/platform/qt/SettingsView.cpp index b0416133a..f6d47b165 100644 --- a/src/platform/qt/SettingsView.cpp +++ b/src/platform/qt/SettingsView.cpp @@ -15,16 +15,16 @@ #include "ShaderSelector.h" #include "ShortcutView.h" +#ifdef M_CORE_GB +#include "GameBoy.h" +#endif + #include #include #include using namespace QGBA; -#ifdef M_CORE_GB -QList SettingsView::s_gbModelList; -#endif - SettingsView::SettingsView(ConfigController* controller, InputController* inputController, ShortcutController* shortcutController, LogController* logController, QWidget* parent) : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint) , m_controller(controller) @@ -33,13 +33,14 @@ SettingsView::SettingsView(ConfigController* controller, InputController* inputC m_ui.setupUi(this); #ifdef M_CORE_GB - if (s_gbModelList.isEmpty()) { - // NB: Keep in sync with SettingsView.ui - s_gbModelList.append(GB_MODEL_AUTODETECT); - s_gbModelList.append(GB_MODEL_DMG); - s_gbModelList.append(GB_MODEL_SGB); - s_gbModelList.append(GB_MODEL_CGB); - s_gbModelList.append(GB_MODEL_AGB); + m_ui.gbModel->setItemData(0, GB_MODEL_AUTODETECT); + m_ui.sgbModel->setItemData(0, GB_MODEL_AUTODETECT); + m_ui.cgbModel->setItemData(0, GB_MODEL_AUTODETECT); + + for (auto model : GameBoy::modelList()) { + m_ui.gbModel->addItem(GameBoy::modelName(model), model); + m_ui.sgbModel->addItem(GameBoy::modelName(model), model); + m_ui.cgbModel->addItem(GameBoy::modelName(model), model); } #endif @@ -511,14 +512,20 @@ void SettingsView::updateConfig() { m_logModel.logger()->logToStdout(m_ui.logToStdout->isChecked()); #ifdef M_CORE_GB - GBModel modelGB = s_gbModelList[m_ui.gbModel->currentIndex()]; - m_controller->setOption("gb.model", GBModelToName(modelGB)); + QVariant modelGB = m_ui.gbModel->currentData(); + if (modelGB.isValid()) { + m_controller->setOption("gb.model", GBModelToName(static_cast(modelGB.toInt()))); + } - GBModel modelSGB = s_gbModelList[m_ui.sgbModel->currentIndex()]; - m_controller->setOption("sgb.model", GBModelToName(modelSGB)); + QVariant modelSGB = m_ui.sgbModel->currentData(); + if (modelSGB.isValid()) { + m_controller->setOption("sgb.model", GBModelToName(static_cast(modelSGB.toInt()))); + } - GBModel modelCGB = s_gbModelList[m_ui.cgbModel->currentIndex()]; - m_controller->setOption("cgb.model", GBModelToName(modelCGB)); + QVariant modelCGB = m_ui.cgbModel->currentData(); + if (modelCGB.isValid()) { + m_controller->setOption("cgb.model", GBModelToName(static_cast(modelCGB.toInt()))); + } for (int colorId = 0; colorId < 12; ++colorId) { if (!(m_gbColors[colorId] & 0xFF000000)) { @@ -641,21 +648,21 @@ void SettingsView::reloadConfig() { QString modelGB = m_controller->getOption("gb.model"); if (!modelGB.isNull()) { GBModel model = GBNameToModel(modelGB.toUtf8().constData()); - int index = s_gbModelList.indexOf(model); + int index = m_ui.gbModel->findData(model); m_ui.gbModel->setCurrentIndex(index >= 0 ? index : 0); } QString modelSGB = m_controller->getOption("sgb.model"); if (!modelSGB.isNull()) { GBModel model = GBNameToModel(modelSGB.toUtf8().constData()); - int index = s_gbModelList.indexOf(model); + int index = m_ui.sgbModel->findData(model); m_ui.sgbModel->setCurrentIndex(index >= 0 ? index : 0); } QString modelCGB = m_controller->getOption("cgb.model"); if (!modelCGB.isNull()) { GBModel model = GBNameToModel(modelCGB.toUtf8().constData()); - int index = s_gbModelList.indexOf(model); + int index = m_ui.cgbModel->findData(model); m_ui.cgbModel->setCurrentIndex(index >= 0 ? index : 0); } #endif diff --git a/src/platform/qt/SettingsView.h b/src/platform/qt/SettingsView.h index 23a8c18ff..5d43b9e1e 100644 --- a/src/platform/qt/SettingsView.h +++ b/src/platform/qt/SettingsView.h @@ -61,7 +61,6 @@ private: #ifdef M_CORE_GB uint32_t m_gbColors[12]{}; ColorPicker m_colorPickers[12]; - static QList s_gbModelList; #endif void saveSetting(const char* key, const QAbstractButton*); diff --git a/src/platform/qt/SettingsView.ui b/src/platform/qt/SettingsView.ui index 0dfb884d2..54519f346 100644 --- a/src/platform/qt/SettingsView.ui +++ b/src/platform/qt/SettingsView.ui @@ -1476,26 +1476,6 @@ Autodetect - - - Game Boy (DMG) - - - - - Super Game Boy (SGB) - - - - - Game Boy Color (CGB) - - - - - Game Boy Advance (AGB) - - @@ -1512,26 +1492,6 @@ Autodetect - - - Game Boy (DMG) - - - - - Super Game Boy (SGB) - - - - - Game Boy Color (CGB) - - - - - Game Boy Advance (AGB) - - @@ -1548,26 +1508,6 @@ Autodetect - - - Game Boy (DMG) - - - - - Super Game Boy (SGB) - - - - - Game Boy Color (CGB) - - - - - Game Boy Advance (AGB) - -