Settings: Fix double source of truth for controller names

This commit is contained in:
Stenzek
2024-04-27 13:21:11 +10:00
parent 5477f2eae2
commit 3e99084770
12 changed files with 93 additions and 128 deletions

View File

@ -63,14 +63,18 @@ void ControllerBindingWidget::populateControllerTypes()
if (!cinfo)
continue;
m_ui.controllerType->addItem(qApp->translate("ControllerType", cinfo->display_name), QVariant(static_cast<int>(i)));
m_ui.controllerType->addItem(QString::fromUtf8(cinfo->GetDisplayName()), QVariant(static_cast<int>(i)));
}
const std::string controller_type_name(
m_controller_info = Controller::GetControllerInfo(
m_dialog->getStringValue(m_config_section.c_str(), "Type", Controller::GetDefaultPadType(m_port_number)));
m_controller_type = Settings::ParseControllerTypeName(controller_type_name.c_str()).value_or(ControllerType::None);
if (!m_controller_info)
{
m_controller_info = Controller::GetControllerInfo(m_port_number == 0 ? Settings::DEFAULT_CONTROLLER_1_TYPE :
Settings::DEFAULT_CONTROLLER_2_TYPE);
}
const int index = m_ui.controllerType->findData(QVariant(static_cast<int>(m_controller_type)));
const int index = m_ui.controllerType->findData(QVariant(static_cast<int>(m_controller_info->type)));
if (index >= 0 && index != m_ui.controllerType->currentIndex())
{
QSignalBlocker sb(m_ui.controllerType);
@ -100,14 +104,13 @@ void ControllerBindingWidget::populateWidgets()
m_macros_widget = nullptr;
}
const Controller::ControllerInfo* cinfo = Controller::GetControllerInfo(m_controller_type);
const bool has_settings = (cinfo && !cinfo->settings.empty());
const bool has_macros = (cinfo && !cinfo->bindings.empty());
const bool has_settings = !m_controller_info->settings.empty();
const bool has_macros = !m_controller_info->bindings.empty();
m_ui.settings->setEnabled(has_settings);
m_ui.macros->setEnabled(has_macros);
m_bindings_widget = new QWidget(this);
switch (m_controller_type)
switch (m_controller_info->type)
{
case ControllerType::AnalogController:
{
@ -224,19 +227,19 @@ void ControllerBindingWidget::onTypeChanged()
if (!ok || index < 0 || index >= static_cast<int>(ControllerType::Count))
return;
m_controller_type = static_cast<ControllerType>(index);
m_controller_info = Controller::GetControllerInfo(static_cast<ControllerType>(index));
DebugAssert(m_controller_info);
SettingsInterface* sif = m_dialog->getProfileSettingsInterface();
if (sif)
{
sif->SetStringValue(m_config_section.c_str(), "Type", Settings::GetControllerTypeName(m_controller_type));
sif->SetStringValue(m_config_section.c_str(), "Type", m_controller_info->name);
QtHost::SaveGameSettings(sif, false);
g_emu_thread->reloadGameSettings();
}
else
{
Host::SetBaseStringSettingValue(m_config_section.c_str(), "Type",
Settings::GetControllerTypeName(m_controller_type));
Host::SetBaseStringSettingValue(m_config_section.c_str(), "Type", m_controller_info->name);
Host::CommitBaseSettingChanges();
g_emu_thread->applySettings();
}
@ -356,10 +359,7 @@ void ControllerBindingWidget::saveAndRefresh()
void ControllerBindingWidget::createBindingWidgets(QWidget* parent)
{
SettingsInterface* sif = getDialog()->getProfileSettingsInterface();
const ControllerType type = getControllerType();
const Controller::ControllerInfo* cinfo = Controller::GetControllerInfo(type);
if (!cinfo)
return;
DebugAssert(m_controller_info);
QGroupBox* axis_gbox = nullptr;
QGridLayout* axis_layout = nullptr;
@ -377,7 +377,7 @@ void ControllerBindingWidget::createBindingWidgets(QWidget* parent)
constexpr int NUM_AXIS_COLUMNS = 2;
int column = 0;
int row = 0;
for (const Controller::ControllerBindingInfo& bi : cinfo->bindings)
for (const Controller::ControllerBindingInfo& bi : m_controller_info->bindings)
{
if (bi.type == InputBindingInfo::Type::Axis || bi.type == InputBindingInfo::Type::HalfAxis ||
bi.type == InputBindingInfo::Type::Pointer)
@ -406,7 +406,7 @@ void ControllerBindingWidget::createBindingWidgets(QWidget* parent)
const int num_button_columns = axis_layout ? 2 : 4;
row = 0;
column = 0;
for (const Controller::ControllerBindingInfo& bi : cinfo->bindings)
for (const Controller::ControllerBindingInfo& bi : m_controller_info->bindings)
{
if (bi.type == InputBindingInfo::Type::Button)
{
@ -452,13 +452,10 @@ void ControllerBindingWidget::createBindingWidgets(QWidget* parent)
void ControllerBindingWidget::bindBindingWidgets(QWidget* parent)
{
SettingsInterface* sif = getDialog()->getProfileSettingsInterface();
const ControllerType type = getControllerType();
const Controller::ControllerInfo* cinfo = Controller::GetControllerInfo(type);
if (!cinfo)
return;
DebugAssert(m_controller_info);
const std::string& config_section = getConfigSection();
for (const Controller::ControllerBindingInfo& bi : cinfo->bindings)
for (const Controller::ControllerBindingInfo& bi : m_controller_info->bindings)
{
if (bi.type == InputBindingInfo::Type::Axis || bi.type == InputBindingInfo::Type::HalfAxis ||
bi.type == InputBindingInfo::Type::Button || bi.type == InputBindingInfo::Type::Pointer)
@ -466,7 +463,7 @@ void ControllerBindingWidget::bindBindingWidgets(QWidget* parent)
InputBindingWidget* widget = parent->findChild<InputBindingWidget*>(QString::fromUtf8(bi.name));
if (!widget)
{
Log_ErrorPrintf("No widget found for '%s' (%s)", bi.name, cinfo->name);
Log_ErrorPrintf("No widget found for '%s' (%s)", bi.name, m_controller_info->name);
continue;
}
@ -474,7 +471,7 @@ void ControllerBindingWidget::bindBindingWidgets(QWidget* parent)
}
}
switch (cinfo->vibration_caps)
switch (m_controller_info->vibration_caps)
{
case Controller::VibrationCapabilities::LargeSmallMotors:
{
@ -549,12 +546,8 @@ ControllerMacroEditWidget::ControllerMacroEditWidget(ControllerMacroWidget* pare
ControllerSettingsWindow* dialog = m_bwidget->getDialog();
const std::string& section = m_bwidget->getConfigSection();
const Controller::ControllerInfo* cinfo = Controller::GetControllerInfo(m_bwidget->getControllerType());
if (!cinfo)
{
// Shouldn't ever happen.
return;
}
const Controller::ControllerInfo* cinfo = m_bwidget->getControllerInfo();
DebugAssert(cinfo);
// load binds (single string joined by &)
const std::string binds_string(
@ -652,9 +645,8 @@ void ControllerMacroEditWidget::updateFrequencyText()
void ControllerMacroEditWidget::updateBinds()
{
ControllerSettingsWindow* dialog = m_bwidget->getDialog();
const Controller::ControllerInfo* cinfo = Controller::GetControllerInfo(m_bwidget->getControllerType());
if (!cinfo)
return;
const Controller::ControllerInfo* cinfo = m_bwidget->getControllerInfo();
DebugAssert(cinfo);
std::vector<const Controller::ControllerBindingInfo*> new_binds;
u32 bind_index = 0;
@ -703,8 +695,9 @@ void ControllerMacroEditWidget::updateBinds()
ControllerCustomSettingsWidget::ControllerCustomSettingsWidget(ControllerBindingWidget* parent)
: QWidget(parent), m_parent(parent)
{
const Controller::ControllerInfo* cinfo = Controller::GetControllerInfo(parent->getControllerType());
if (!cinfo || cinfo->settings.empty())
const Controller::ControllerInfo* cinfo = parent->getControllerInfo();
DebugAssert(cinfo);
if (cinfo->settings.empty())
return;
QScrollArea* sarea = new QScrollArea(this);
@ -865,8 +858,9 @@ void ControllerCustomSettingsWidget::createSettingWidgets(ControllerBindingWidge
void ControllerCustomSettingsWidget::restoreDefaults()
{
const Controller::ControllerInfo* cinfo = Controller::GetControllerInfo(m_parent->getControllerType());
if (!cinfo || cinfo->settings.empty())
const Controller::ControllerInfo* cinfo = m_parent->getControllerInfo();
DebugAssert(cinfo);
if (cinfo->settings.empty())
return;
for (const SettingInfo& si : cinfo->settings)

View File

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once
@ -33,7 +33,7 @@ public:
ALWAYS_INLINE ControllerSettingsWindow* getDialog() const { return m_dialog; }
ALWAYS_INLINE const std::string& getConfigSection() const { return m_config_section; }
ALWAYS_INLINE ControllerType getControllerType() const { return m_controller_type; }
ALWAYS_INLINE const Controller::ControllerInfo* getControllerInfo() const { return m_controller_info; }
ALWAYS_INLINE u32 getPortNumber() const { return m_port_number; }
ALWAYS_INLINE const QIcon& getIcon() { return m_icon; }
@ -59,7 +59,7 @@ private:
ControllerSettingsWindow* m_dialog;
std::string m_config_section;
ControllerType m_controller_type;
const Controller::ControllerInfo* m_controller_info;
u32 m_port_number;
QIcon m_icon;

View File

@ -390,9 +390,7 @@ void ControllerSettingsWindow::createWidgets()
m_port_bindings[global_slot] = new ControllerBindingWidget(m_ui.settingsContainer, this, global_slot);
m_ui.settingsContainer->addWidget(m_port_bindings[global_slot]);
const Controller::ControllerInfo* ci =
Controller::GetControllerInfo(m_port_bindings[global_slot]->getControllerType());
const QString display_name(ci ? qApp->translate("ControllerType", ci->display_name) : QStringLiteral("Unknown"));
const QString display_name(QString::fromUtf8(m_port_bindings[global_slot]->getControllerInfo()->GetDisplayName()));
QListWidgetItem* item = new QListWidgetItem();
item->setText(mtap_enabled[port] ?
@ -431,8 +429,7 @@ void ControllerSettingsWindow::updateListDescription(u32 global_slot, Controller
const std::array<bool, 2> mtap_enabled = getEnabledMultitaps();
const auto [port, slot] = Controller::ConvertPadToPortAndSlot(global_slot);
const Controller::ControllerInfo* ci = Controller::GetControllerInfo(widget->getControllerType());
const QString display_name(ci ? qApp->translate("ControllerType", ci->display_name) : QStringLiteral("Unknown"));
const QString display_name = QString::fromUtf8(widget->getControllerInfo()->GetDisplayName());
item->setText(mtap_enabled[port] ?
(tr("Controller Port %1%2\n%3").arg(port + 1).arg(s_mtap_slot_names[slot]).arg(display_name)) :

View File

@ -6,6 +6,7 @@
#include "qtprogresscallback.h"
#include "settingswindow.h"
#include "core/controller.h"
#include "core/game_database.h"
#include "core/game_list.h"
@ -113,7 +114,7 @@ void GameSummaryWidget::populateUi(const std::string& path, const std::string& s
{
if (!controllers.isEmpty())
controllers.append(", ");
controllers.append(Settings::GetControllerTypeDisplayName(static_cast<ControllerType>(i)));
controllers.append(Controller::GetControllerInfo(static_cast<ControllerType>(i))->GetDisplayName());
}
}
}