Use EnumFormatter to generate names for EXI devices

This simplifies the code in GameCubePane, and allows us to use the EXIDeviceType enum in error messages.
This commit is contained in:
Pokechu22 2022-01-11 17:10:13 -08:00
parent d354163fbe
commit fefde0481d
3 changed files with 67 additions and 20 deletions

View File

@ -95,7 +95,6 @@ protected:
constexpr explicit EnumFormatter(const array_type names) : m_names(std::move(names)) {}
private:
const array_type m_names;
char format_type = 'u';
};

View File

@ -4,7 +4,10 @@
#pragma once
#include <memory>
#include "Common/Common.h"
#include "Common/CommonTypes.h"
#include "Common/EnumFormatter.h"
class PointerWrap;
@ -75,3 +78,50 @@ private:
std::unique_ptr<IEXIDevice> EXIDevice_Create(EXIDeviceType device_type, int channel_num,
const Memcard::HeaderData& memcard_header_data);
} // namespace ExpansionInterface
template <>
struct fmt::formatter<ExpansionInterface::EXIDeviceType>
: EnumFormatter<ExpansionInterface::EXIDeviceType::EthernetTapServer>
{
static constexpr array_type names = {
_trans("Dummy"),
_trans("Memory Card"),
_trans("Mask ROM"),
// i18n: A mysterious debugging/diagnostics peripheral for the GameCube.
_trans("AD16"),
_trans("Microphone"),
_trans("Broadband Adapter (TAP)"),
_trans("Triforce AM Baseboard"),
_trans("USB Gecko"),
_trans("GCI Folder"),
_trans("Advance Game Port"),
_trans("Broadband Adapter (XLink Kai)"),
_trans("Broadband Adapter (tapserver)"),
};
constexpr formatter() : EnumFormatter(names) {}
template <typename FormatContext>
auto format(const ExpansionInterface::EXIDeviceType& e, FormatContext& ctx)
{
if (e != ExpansionInterface::EXIDeviceType::None)
{
return EnumFormatter::format(e, ctx);
}
else
{
// Special-case None since it has a fixed ID (0xff) that is much larger than the rest; we
// don't need 200 nullptr entries in names. We also want to format it specially in the UI.
switch (format_type)
{
default:
case 'u':
return fmt::format_to(ctx.out(), "None");
case 's':
return fmt::format_to(ctx.out(), "0xffu /* None */");
case 'n':
return fmt::format_to(ctx.out(), _trans("<Nothing>"));
}
}
}
};

View File

@ -16,6 +16,7 @@
#include <QPushButton>
#include <QVBoxLayout>
#include <array>
#include <utility>
#include "Common/CommonPaths.h"
@ -97,31 +98,28 @@ void GameCubePane::CreateWidgets()
}
// Add slot devices
for (const auto& entry : {std::make_pair(tr("<Nothing>"), EXIDeviceType::None),
std::make_pair(tr("Dummy"), EXIDeviceType::Dummy),
std::make_pair(tr("Memory Card"), EXIDeviceType::MemoryCard),
std::make_pair(tr("GCI Folder"), EXIDeviceType::MemoryCardFolder),
std::make_pair(tr("USB Gecko"), EXIDeviceType::Gecko),
std::make_pair(tr("Advance Game Port"), EXIDeviceType::AGP),
std::make_pair(tr("Microphone"), EXIDeviceType::Microphone)})
for (const auto device : {EXIDeviceType::None, EXIDeviceType::Dummy, EXIDeviceType::MemoryCard,
EXIDeviceType::MemoryCardFolder, EXIDeviceType::Gecko,
EXIDeviceType::AGP, EXIDeviceType::Microphone})
{
m_slot_combos[0]->addItem(entry.first, static_cast<int>(entry.second));
m_slot_combos[1]->addItem(entry.first, static_cast<int>(entry.second));
const QString name = tr(fmt::format("{:n}", device).c_str());
const int value = static_cast<int>(device);
m_slot_combos[0]->addItem(name, value);
m_slot_combos[1]->addItem(name, value);
}
// Add SP1 devices
std::vector<std::pair<QString, EXIDeviceType>> sp1Entries{
std::make_pair(tr("<Nothing>"), EXIDeviceType::None),
std::make_pair(tr("Dummy"), EXIDeviceType::Dummy),
std::make_pair(tr("Broadband Adapter (TAP)"), EXIDeviceType::Ethernet),
std::make_pair(tr("Broadband Adapter (XLink Kai)"), EXIDeviceType::EthernetXLink)};
#if defined(__APPLE__)
sp1Entries.emplace_back(std::make_pair(tr("Broadband Adapter (tapserver)"),
ExpansionInterface::EXIDeviceType::EthernetTapServer));
for (const auto device : {
EXIDeviceType::None,
EXIDeviceType::Dummy,
EXIDeviceType::Ethernet,
EXIDeviceType::EthernetXLink,
#ifdef __APPLE__
EXIDeviceType::EthernetTapServer,
#endif
for (const auto& entry : sp1Entries)
})
{
m_slot_combos[2]->addItem(entry.first, static_cast<int>(entry.second));
m_slot_combos[2]->addItem(tr(fmt::format("{:n}", device).c_str()), static_cast<int>(device));
}
device_layout->addWidget(new QLabel(tr("Slot A:")), 0, 0);