Controller: Use std::span

This commit is contained in:
Stenzek 2023-09-20 17:10:41 +10:00
parent 539b406c95
commit 3cd28f433b
12 changed files with 45 additions and 120 deletions

View file

@ -875,9 +875,7 @@ const Controller::ControllerInfo AnalogController::INFO = {ControllerType::Analo
"AnalogController",
TRANSLATE_NOOP("ControllerType", "Analog Controller"),
s_binding_info,
countof(s_binding_info),
s_settings,
countof(s_settings),
Controller::VibrationCapabilities::LargeSmallMotors};
void AnalogController::LoadSettings(SettingsInterface& si, const char* section)

View file

@ -407,9 +407,7 @@ const Controller::ControllerInfo AnalogJoystick::INFO = {ControllerType::AnalogJ
"AnalogJoystick",
TRANSLATE_NOOP("ControllerType", "Analog Joystick"),
s_binding_info,
countof(s_binding_info),
s_settings,
countof(s_settings),
Controller::VibrationCapabilities::NoVibration};
void AnalogJoystick::LoadSettings(SettingsInterface& si, const char* section)

View file

@ -15,10 +15,8 @@
static const Controller::ControllerInfo s_none_info = {ControllerType::None,
"None",
TRANSLATE_NOOP("ControllerType", "Not Connected"),
nullptr,
0,
nullptr,
0,
{},
{},
Controller::VibrationCapabilities::NoVibration};
static const Controller::ControllerInfo* s_controller_info[] = {
@ -144,53 +142,13 @@ std::vector<std::pair<std::string, std::string>> Controller::GetControllerTypeNa
return ret;
}
std::vector<std::string> Controller::GetControllerBinds(const std::string_view& type)
{
std::vector<std::string> ret;
const ControllerInfo* info = GetControllerInfo(type);
if (info)
{
for (u32 i = 0; i < info->num_bindings; i++)
{
const ControllerBindingInfo& bi = info->bindings[i];
if (bi.type == InputBindingInfo::Type::Unknown || bi.type == InputBindingInfo::Type::Motor)
continue;
ret.emplace_back(info->bindings[i].name);
}
}
return ret;
}
std::vector<std::string> Controller::GetControllerBinds(ControllerType type)
{
std::vector<std::string> ret;
const ControllerInfo* info = GetControllerInfo(type);
if (info)
{
for (u32 i = 0; i < info->num_bindings; i++)
{
const ControllerBindingInfo& bi = info->bindings[i];
if (bi.type == InputBindingInfo::Type::Unknown || bi.type == InputBindingInfo::Type::Motor)
continue;
ret.emplace_back(info->bindings[i].name);
}
}
return ret;
}
std::optional<u32> Controller::GetBindIndex(ControllerType type, const std::string_view& bind_name)
{
const ControllerInfo* info = GetControllerInfo(type);
if (!info)
return std::nullopt;
for (u32 i = 0; i < info->num_bindings; i++)
for (u32 i = 0; i < static_cast<u32>(info->bindings.size()); i++)
{
if (bind_name == info->bindings[i].name)
return i;

View file

@ -1,13 +1,17 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once
#include "common/image.h"
#include "input_types.h"
#include "settings.h"
#include "types.h"
#include "common/image.h"
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <tuple>
@ -42,10 +46,8 @@ public:
ControllerType type;
const char* name;
const char* display_name;
const ControllerBindingInfo* bindings;
u32 num_bindings;
const SettingInfo* settings;
u32 num_settings;
std::span<const ControllerBindingInfo> bindings;
std::span<const SettingInfo> settings;
VibrationCapabilities vibration_caps;
};
@ -96,10 +98,6 @@ public:
/// Returns a list of controller type names. Pair of [name, display name].
static std::vector<std::pair<std::string, std::string>> GetControllerTypeNames();
/// Returns the list of binds for the specified controller type.
static std::vector<std::string> GetControllerBinds(const std::string_view& type);
static std::vector<std::string> GetControllerBinds(ControllerType type);
/// Gets the integer code for an axis in the specified controller type.
static std::optional<u32> GetBindIndex(ControllerType type, const std::string_view& bind_name);

View file

@ -178,9 +178,7 @@ const Controller::ControllerInfo DigitalController::INFO = {ControllerType::Digi
"DigitalController",
TRANSLATE_NOOP("ControllerType", "Digital Controller"),
s_binding_info,
countof(s_binding_info),
s_settings,
countof(s_settings),
Controller::VibrationCapabilities::NoVibration};
void DigitalController::LoadSettings(SettingsInterface& si, const char* section)

View file

@ -96,9 +96,9 @@ using ImGuiFullscreen::UIPrimaryLightColor;
using ImGuiFullscreen::UIPrimaryLineColor;
using ImGuiFullscreen::UIPrimaryTextColor;
using ImGuiFullscreen::UISecondaryColor;
using ImGuiFullscreen::UISecondaryWeakColor;
using ImGuiFullscreen::UISecondaryStrongColor;
using ImGuiFullscreen::UISecondaryTextColor;
using ImGuiFullscreen::UISecondaryWeakColor;
using ImGuiFullscreen::UITextHighlightColor;
using ImGuiFullscreen::ActiveButton;
@ -1114,9 +1114,8 @@ void FullscreenUI::DoToggleAnalogMode()
if (!cinfo)
continue;
for (u32 j = 0; j < cinfo->num_bindings; j++)
for (const Controller::ControllerBindingInfo& bi : cinfo->bindings)
{
const Controller::ControllerBindingInfo& bi = cinfo->bindings[j];
if (std::strcmp(bi.name, "Analog") == 0)
{
ctrl->SetBindState(bi.bind_index, 1.0f);
@ -3314,7 +3313,7 @@ void FullscreenUI::DrawControllerSettingsPage()
});
}
if (!ci || ci->num_bindings == 0)
if (!ci || ci->bindings.empty() == 0)
continue;
if (MenuButton(FSUI_ICONSTR(ICON_FA_MAGIC, "Automatic Mapping"),
@ -3323,11 +3322,8 @@ void FullscreenUI::DrawControllerSettingsPage()
StartAutomaticBinding(global_slot);
}
for (u32 i = 0; i < ci->num_bindings; i++)
{
const Controller::ControllerBindingInfo& bi = ci->bindings[i];
for (const Controller::ControllerBindingInfo& bi : ci->bindings)
DrawInputBindingButton(bsi, bi.type, section.c_str(), bi.name, bi.display_name, true);
}
if (mtap_enabled[mtap_port])
{
@ -3354,9 +3350,8 @@ void FullscreenUI::DrawControllerSettingsPage()
{
std::vector<std::string_view> buttons_split(StringUtil::SplitString(binds_string, '&', true));
ImGuiFullscreen::ChoiceDialogOptions options;
for (u32 i = 0; i < ci->num_bindings; i++)
for (const Controller::ControllerBindingInfo& bi : ci->bindings)
{
const Controller::ControllerBindingInfo& bi = ci->bindings[i];
if (bi.type != InputBindingInfo::Type::Button && bi.type != InputBindingInfo::Type::Axis &&
bi.type != InputBindingInfo::Type::HalfAxis)
{
@ -3372,9 +3367,8 @@ void FullscreenUI::DrawControllerSettingsPage()
[game_settings, section, macro_index, ci](s32 index, const std::string& title, bool checked) {
// convert display name back to bind name
std::string_view to_modify;
for (u32 j = 0; j < ci->num_bindings; j++)
for (const Controller::ControllerBindingInfo& bi : ci->bindings)
{
const Controller::ControllerBindingInfo& bi = ci->bindings[j];
if (bi.display_name == title)
{
to_modify = bi.name;
@ -3458,7 +3452,7 @@ void FullscreenUI::DrawControllerSettingsPage()
ImGui::PopFont();
}
if (ci->num_settings > 0)
if (!ci->settings.empty())
{
if (mtap_enabled[mtap_port])
{
@ -3471,9 +3465,8 @@ void FullscreenUI::DrawControllerSettingsPage()
mtap_port + 1));
}
for (u32 i = 0; i < ci->num_settings; i++)
for (const SettingInfo& si : ci->settings)
{
const SettingInfo& si = ci->settings[i];
TinyString title;
title.Fmt(ICON_FA_COG "{}", si.display_name);
switch (si.type)
@ -4715,8 +4708,7 @@ void FullscreenUI::DrawPauseMenu()
subtitle_pos.x -= rp_height;
subtitle_pos.y -= rp_height;
DrawShadowedText(dl, g_medium_font, rp_pos, text_color, rp.data(), rp.data() + rp.size(),
wrap_width);
DrawShadowedText(dl, g_medium_font, rp_pos, text_color, rp.data(), rp.data() + rp.size(), wrap_width);
}
}

View file

@ -304,9 +304,7 @@ const Controller::ControllerInfo GunCon::INFO = {ControllerType::GunCon,
"GunCon",
TRANSLATE_NOOP("ControllerType", "GunCon"),
s_binding_info,
countof(s_binding_info),
s_settings,
countof(s_settings),
Controller::VibrationCapabilities::NoVibration};
void GunCon::LoadSettings(SettingsInterface& si, const char* section)

View file

@ -627,9 +627,8 @@ void ImGuiManager::DrawInputsOverlay()
text.Fmt("P{} |", port + 1u);
for (u32 bind = 0; bind < cinfo->num_bindings; bind++)
for (const Controller::ControllerBindingInfo& bi : cinfo->bindings)
{
const Controller::ControllerBindingInfo& bi = cinfo->bindings[bind];
switch (bi.type)
{
case InputBindingInfo::Type::Axis:

View file

@ -269,9 +269,7 @@ const Controller::ControllerInfo NeGcon::INFO = {ControllerType::NeGcon,
"NeGcon",
TRANSLATE_NOOP("ControllerType", "NeGcon"),
s_binding_info,
countof(s_binding_info),
s_settings,
countof(s_settings),
Controller::VibrationCapabilities::NoVibration};
void NeGcon::LoadSettings(SettingsInterface& si, const char* section)

View file

@ -219,7 +219,5 @@ const Controller::ControllerInfo PlayStationMouse::INFO = {ControllerType::PlayS
"PlayStationMouse",
TRANSLATE_NOOP("ControllerType", "PlayStation Mouse"),
s_binding_info,
countof(s_binding_info),
s_settings,
countof(s_settings),
Controller::VibrationCapabilities::NoVibration};

View file

@ -98,8 +98,8 @@ void ControllerBindingWidget::populateWidgets()
}
const Controller::ControllerInfo* cinfo = Controller::GetControllerInfo(m_controller_type);
const bool has_settings = (cinfo && cinfo->num_settings > 0);
const bool has_macros = (cinfo && cinfo->num_bindings > 0);
const bool has_settings = (cinfo && !cinfo->settings.empty());
const bool has_macros = (cinfo && !cinfo->bindings.empty());
m_ui.settings->setEnabled(has_settings);
m_ui.macros->setEnabled(has_macros);
@ -360,20 +360,19 @@ ControllerMacroEditWidget::ControllerMacroEditWidget(ControllerMacroWidget* pare
for (const std::string_view& button : buttons_split)
{
for (u32 i = 0; i < cinfo->num_bindings; i++)
for (const Controller::ControllerBindingInfo& bi : cinfo->bindings)
{
if (button == cinfo->bindings[i].name)
if (button == bi.name)
{
m_binds.push_back(&cinfo->bindings[i]);
m_binds.push_back(&bi);
break;
}
}
}
// populate list view
for (u32 i = 0; i < cinfo->num_bindings; i++)
for (const Controller::ControllerBindingInfo& bi : cinfo->bindings)
{
const Controller::ControllerBindingInfo& bi = cinfo->bindings[i];
if (bi.type == InputBindingInfo::Type::Motor)
continue;
@ -455,9 +454,9 @@ void ControllerMacroEditWidget::updateBinds()
return;
std::vector<const Controller::ControllerBindingInfo*> new_binds;
for (u32 i = 0, bind_index = 0; i < cinfo->num_bindings; i++)
u32 bind_index = 0;
for (const Controller::ControllerBindingInfo& bi : cinfo->bindings)
{
const Controller::ControllerBindingInfo& bi = cinfo->bindings[i];
if (bi.type == InputBindingInfo::Type::Motor)
continue;
@ -471,7 +470,7 @@ void ControllerMacroEditWidget::updateBinds()
}
if (item->checkState() == Qt::Checked)
new_binds.push_back(&cinfo->bindings[i]);
new_binds.push_back(&bi);
}
if (m_binds == new_binds)
return;
@ -502,7 +501,7 @@ ControllerCustomSettingsWidget::ControllerCustomSettingsWidget(ControllerBinding
: QWidget(parent), m_parent(parent)
{
const Controller::ControllerInfo* cinfo = Controller::GetControllerInfo(parent->getControllerType());
if (!cinfo || cinfo->num_settings == 0)
if (!cinfo || cinfo->settings.empty())
return;
QGroupBox* gbox = new QGroupBox(tr("%1 Settings").arg(qApp->translate("ControllerType", cinfo->display_name)), this);
@ -534,9 +533,8 @@ void ControllerCustomSettingsWidget::createSettingWidgets(ControllerBindingWidge
SettingsInterface* sif = parent->getDialog()->getProfileSettingsInterface();
int current_row = 0;
for (u32 i = 0; i < cinfo->num_settings; i++)
for (const SettingInfo& si : cinfo->settings)
{
const SettingInfo& si = cinfo->settings[i];
std::string key_name = si.name;
switch (si.type)
@ -658,12 +656,11 @@ void ControllerCustomSettingsWidget::createSettingWidgets(ControllerBindingWidge
void ControllerCustomSettingsWidget::restoreDefaults()
{
const Controller::ControllerInfo* cinfo = Controller::GetControllerInfo(m_parent->getControllerType());
if (!cinfo || cinfo->num_settings == 0)
if (!cinfo || cinfo->settings.empty())
return;
for (u32 i = 0; i < cinfo->num_settings; i++)
for (const SettingInfo& si : cinfo->settings)
{
const SettingInfo& si = cinfo->settings[i];
const QString key(QString::fromStdString(si.name));
switch (si.type)
@ -748,9 +745,8 @@ void ControllerBindingWidget_Base::initBindingWidgets()
return;
const std::string& config_section = getConfigSection();
for (u32 i = 0; i < cinfo->num_bindings; i++)
for (const Controller::ControllerBindingInfo& bi : cinfo->bindings)
{
const Controller::ControllerBindingInfo& bi = cinfo->bindings[i];
if (bi.type == InputBindingInfo::Type::Axis || bi.type == InputBindingInfo::Type::HalfAxis ||
bi.type == InputBindingInfo::Type::Button || bi.type == InputBindingInfo::Type::Pointer)
{

View file

@ -698,9 +698,8 @@ void InputManager::AddHotkeyBindings(SettingsInterface& si)
void InputManager::AddPadBindings(SettingsInterface& si, const std::string& section, u32 pad_index,
const Controller::ControllerInfo* cinfo)
{
for (u32 i = 0; i < cinfo->num_bindings; i++)
for (const Controller::ControllerBindingInfo& bi : cinfo->bindings)
{
const Controller::ControllerBindingInfo& bi = cinfo->bindings[i];
const std::vector<std::string> bindings(si.GetStringList(section.c_str(), bi.name));
switch (bi.type)
@ -1170,8 +1169,8 @@ void InputManager::ClearPortBindings(SettingsInterface& si, u32 port)
if (!info)
return;
for (u32 i = 0; i < info->num_bindings; i++)
si.DeleteValue(section.c_str(), info->bindings[i].name);
for (const Controller::ControllerBindingInfo& bi : info->bindings)
si.DeleteValue(section.c_str(), bi.name);
}
void InputManager::CopyConfiguration(SettingsInterface* dest_si, const SettingsInterface& src_si,
@ -1201,11 +1200,8 @@ void InputManager::CopyConfiguration(SettingsInterface* dest_si, const SettingsI
if (copy_pad_bindings)
{
for (u32 i = 0; i < info->num_bindings; i++)
{
const Controller::ControllerBindingInfo& bi = info->bindings[i];
for (const Controller::ControllerBindingInfo& bi : info->bindings)
dest_si->CopyStringListValue(src_si, section.c_str(), bi.name);
}
for (u32 i = 0; i < NUM_MACRO_BUTTONS_PER_CONTROLLER; i++)
{
@ -1217,9 +1213,8 @@ void InputManager::CopyConfiguration(SettingsInterface* dest_si, const SettingsI
if (copy_pad_config)
{
for (u32 i = 0; i < info->num_settings; i++)
for (const SettingInfo& csi : info->settings)
{
const SettingInfo& csi = info->settings[i];
switch (csi.type)
{
case SettingInfo::Type::Boolean:
@ -1289,9 +1284,8 @@ bool InputManager::MapController(SettingsInterface& si, u32 controller,
return false;
u32 num_mappings = 0;
for (u32 i = 0; i < info->num_bindings; i++)
for (const Controller::ControllerBindingInfo& bi : info->bindings)
{
const Controller::ControllerBindingInfo& bi = info->bindings[i];
if (bi.generic_mapping == GenericInputBinding::Unknown)
continue;
@ -1464,7 +1458,7 @@ void InputManager::LoadMacroButtonConfig(SettingsInterface& si, const std::strin
const Controller::ControllerInfo* cinfo)
{
s_macro_buttons[pad] = {};
if (cinfo->num_bindings == 0)
if (cinfo->bindings.empty())
return;
for (u32 i = 0; i < NUM_MACRO_BUTTONS_PER_CONTROLLER; i++)
@ -1483,11 +1477,11 @@ void InputManager::LoadMacroButtonConfig(SettingsInterface& si, const std::strin
for (const std::string_view& button : buttons_split)
{
const Controller::ControllerBindingInfo* binding = nullptr;
for (u32 j = 0; j < cinfo->num_bindings; j++)
for (const Controller::ControllerBindingInfo& bi : cinfo->bindings)
{
if (button == cinfo->bindings[j].name)
if (button == bi.name)
{
binding = &cinfo->bindings[j];
binding = &bi;
break;
}
}