InputManager: Use TinyString for key-to-string function

pull/3576/head
Stenzek 1 month ago
parent ba0390f6e0
commit 9baa09aace
No known key found for this signature in database

@ -2587,8 +2587,8 @@ void FullscreenUI::InputBindingDialog::Start(SettingsInterface* bsi, InputBindin
other_key.modifier = InputModifier::FullAxis;
SettingsInterface* bsi = GetEditingSettingsInterface(game_settings);
const std::string new_binding(InputManager::ConvertInputBindingKeysToString(
m_binding_type, m_new_bindings.data(), m_new_bindings.size()));
const SmallString new_binding =
InputManager::ConvertInputBindingKeysToString(m_binding_type, m_new_bindings.data(), m_new_bindings.size());
bsi->SetStringValue(m_binding_section.c_str(), m_binding_key.c_str(), new_binding.c_str());
SetSettingsChanged(bsi);
@ -2684,10 +2684,10 @@ void FullscreenUI::BeginVibrationMotorBinding(SettingsInterface* bsi, InputBindi
options.reserve(motors.size() + 1);
for (size_t i = 0; i < motors.size(); i++)
{
std::string text = InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type::Motor, motors[i]);
const TinyString text = InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type::Motor, motors[i]);
const bool this_index = (current_binding.view() == text);
current_index = this_index ? i : current_index;
options.emplace_back(std::move(text), this_index);
options.emplace_back(text, this_index);
}
// empty/no mapping value

@ -221,15 +221,15 @@ void InputBindingDialog::addNewBinding()
if (m_new_bindings.empty())
return;
std::string new_binding =
SmallString new_binding =
InputManager::ConvertInputBindingKeysToString(m_bind_type, m_new_bindings.data(), m_new_bindings.size());
if (!new_binding.empty())
{
if (std::find(m_bindings.begin(), m_bindings.end(), new_binding) != m_bindings.end())
if (std::find(m_bindings.begin(), m_bindings.end(), new_binding.view()) != m_bindings.end())
return;
m_ui.bindingList->addItem(QString::fromStdString(new_binding));
m_bindings.push_back(std::move(new_binding));
m_ui.bindingList->addItem(QtUtils::StringViewToQString(new_binding));
m_bindings.emplace_back(new_binding);
saveListToSettings();
}
}

@ -218,8 +218,8 @@ void InputBindingWidget::setNewBinding()
if (m_new_bindings.empty())
return;
std::string new_binding(
InputManager::ConvertInputBindingKeysToString(m_bind_type, m_new_bindings.data(), m_new_bindings.size()));
const SmallString new_binding =
InputManager::ConvertInputBindingKeysToString(m_bind_type, m_new_bindings.data(), m_new_bindings.size());
if (!new_binding.empty())
{
if (m_sif)
@ -239,7 +239,7 @@ void InputBindingWidget::setNewBinding()
}
m_bindings.clear();
m_bindings.push_back(std::move(new_binding));
m_bindings.emplace_back(new_binding);
}
void InputBindingWidget::clearBinding()

@ -2607,7 +2607,7 @@ void InputDeviceListModel::enumerateDevices()
for (const auto& key : motors)
{
new_motors.push_back(
QString::fromStdString(InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type::Motor, key)));
QtUtils::StringViewToQString(InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type::Motor, key)));
}
QMetaObject::invokeMethod(this, &InputDeviceListModel::resetLists, Qt::QueuedConnection, new_devices, new_motors);
@ -2676,8 +2676,8 @@ void Host::OnInputDeviceConnected(InputBindingKey key, std::string_view identifi
vibration_motor_list.reserve(im_vibration_motor_list.size());
for (const InputBindingKey& motor_key : im_vibration_motor_list)
{
vibration_motor_list.push_back(
QString::fromStdString(InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type::Motor, motor_key)));
vibration_motor_list.push_back(QtUtils::StringViewToQString(
InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type::Motor, motor_key)));
}
}

@ -362,84 +362,88 @@ bool InputManager::ParseBindingAndGetSource(std::string_view binding, InputBindi
return false;
}
std::string InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type binding_type, InputBindingKey key)
TinyString InputManager::ConvertInputBindingKeyToString(InputBindingInfo::Type binding_type, InputBindingKey key)
{
TinyString ret;
if (binding_type == InputBindingInfo::Type::Pointer || binding_type == InputBindingInfo::Type::RelativePointer ||
binding_type == InputBindingInfo::Type::Device)
{
// pointer and device bindings don't have a data part
if (key.source_type == InputSourceType::Pointer)
{
return GetPointerDeviceName(key.source_index);
ret = GetPointerDeviceName(key.source_index);
}
else if (key.source_type < InputSourceType::Count && s_state.input_sources[static_cast<u32>(key.source_type)])
{
// This assumes that it always follows the Type/Binding form.
std::string keystr(s_state.input_sources[static_cast<u32>(key.source_type)]->ConvertKeyToString(key));
std::string::size_type pos = keystr.find('/');
if (pos != std::string::npos)
keystr.erase(pos);
return keystr;
ret = s_state.input_sources[static_cast<size_t>(key.source_type)]->ConvertKeyToString(key);
if (const s32 pos = ret.find('/'); pos > 0)
ret.erase(pos);
}
}
else
{
if (key.source_type == InputSourceType::Keyboard)
{
const std::optional<std::string> str(ConvertHostKeyboardCodeToString(key.data));
if (str.has_value() && !str->empty())
return fmt::format("Keyboard/{}", str->c_str());
if (const char* key_code_string = ConvertHostKeyboardCodeToString(key.data))
ret.format("Keyboard/{}", key_code_string);
}
else if (key.source_type == InputSourceType::Pointer)
{
if (key.source_subtype == InputSubclass::PointerButton)
{
if (key.data < s_pointer_button_names.size())
return fmt::format("Pointer-{}/{}", u32{key.source_index}, s_pointer_button_names[key.data]);
ret.format("Pointer-{}/{}", u32{key.source_index}, s_pointer_button_names[key.data]);
else
return fmt::format("Pointer-{}/Button{}", u32{key.source_index}, key.data);
ret.format("Pointer-{}/Button{}", u32{key.source_index}, key.data);
}
else if (key.source_subtype == InputSubclass::PointerAxis)
{
return fmt::format("Pointer-{}/{}{:c}", u32{key.source_index}, s_pointer_axis_names[key.data],
key.modifier == InputModifier::Negate ? '-' : '+');
ret.format("Pointer-{}/{}{:c}", u32{key.source_index}, s_pointer_axis_names[key.data],
key.modifier == InputModifier::Negate ? '-' : '+');
}
}
else if (key.source_type < InputSourceType::Count && s_state.input_sources[static_cast<u32>(key.source_type)])
{
return std::string(s_state.input_sources[static_cast<u32>(key.source_type)]->ConvertKeyToString(key));
ret = s_state.input_sources[static_cast<size_t>(key.source_type)]->ConvertKeyToString(key);
}
}
return {};
return ret;
}
std::string InputManager::ConvertInputBindingKeysToString(InputBindingInfo::Type binding_type,
SmallString InputManager::ConvertInputBindingKeysToString(InputBindingInfo::Type binding_type,
const InputBindingKey* keys, size_t num_keys)
{
SmallString ret;
// can't have a chord of devices/pointers
if (binding_type == InputBindingInfo::Type::Pointer || binding_type == InputBindingInfo::Type::RelativePointer ||
binding_type == InputBindingInfo::Type::Device)
{
// so only take the first
if (num_keys > 0)
return ConvertInputBindingKeyToString(binding_type, keys[0]);
{
ret = ConvertInputBindingKeyToString(binding_type, keys[0]);
return ret;
}
}
std::stringstream ss;
for (size_t i = 0; i < num_keys; i++)
{
const std::string keystr(ConvertInputBindingKeyToString(binding_type, keys[i]));
const TinyString keystr = ConvertInputBindingKeyToString(binding_type, keys[i]);
if (keystr.empty())
return std::string();
return ret;
if (i > 0)
ss << " & ";
ret.append(" & ");
ss << keystr;
ret.append(keystr);
}
return std::move(ss).str();
return ret;
}
bool InputManager::PrettifyInputBinding(SmallStringBase& binding, BindingIconMappingFunction mapper /*= nullptr*/)
@ -683,15 +687,10 @@ std::optional<u32> InputManager::ConvertHostKeyboardStringToCode(std::string_vie
return std::nullopt;
}
std::optional<std::string> InputManager::ConvertHostKeyboardCodeToString(u32 code)
const char* InputManager::ConvertHostKeyboardCodeToString(u32 code)
{
std::optional<std::string> ret;
const KeyCodeData* key_data = FindKeyCodeData(code);
if (key_data)
ret.emplace(key_data->name);
return ret;
return key_data ? key_data->name : nullptr;
}
const InputManager::KeyCodeData* InputManager::FindKeyCodeData(u32 usb_code)
@ -874,9 +873,9 @@ std::optional<u32> InputManager::GetIndexFromPointerBinding(std::string_view sou
return static_cast<u32>(pointer_index.value());
}
std::string InputManager::GetPointerDeviceName(u32 pointer_index)
TinyString InputManager::GetPointerDeviceName(u32 pointer_index)
{
return fmt::format("Pointer-{}", pointer_index);
return TinyString::from_format("Pointer-{}", pointer_index);
}
std::optional<InputBindingKey> InputManager::ParseSensorKey(std::string_view source, std::string_view sub_binding)

@ -226,13 +226,13 @@ std::optional<InputSourceType> ParseInputSourceString(std::string_view str);
std::optional<u32> GetIndexFromPointerBinding(std::string_view str);
/// Returns the device name for a pointer index (e.g. Pointer-0).
std::string GetPointerDeviceName(u32 pointer_index);
TinyString GetPointerDeviceName(u32 pointer_index);
/// Converts a key code from a human-readable string to an identifier.
std::optional<u32> ConvertHostKeyboardStringToCode(std::string_view str);
/// Converts a key code from an identifier to a human-readable string.
std::optional<std::string> ConvertHostKeyboardCodeToString(u32 code);
const char* ConvertHostKeyboardCodeToString(u32 code);
/// Converts a key code from an identifier to an icon which can be drawn.
const char* ConvertHostKeyboardCodeToIcon(u32 code);
@ -257,10 +257,10 @@ InputBindingKey MakeSensorAxisKey(InputSubclass sensor, u32 axis);
std::optional<InputBindingKey> ParseInputBindingKey(std::string_view binding);
/// Converts a input key to a string.
std::string ConvertInputBindingKeyToString(InputBindingInfo::Type binding_type, InputBindingKey key);
TinyString ConvertInputBindingKeyToString(InputBindingInfo::Type binding_type, InputBindingKey key);
/// Converts a chord of binding keys to a string.
std::string ConvertInputBindingKeysToString(InputBindingInfo::Type binding_type, const InputBindingKey* keys,
SmallString ConvertInputBindingKeysToString(InputBindingInfo::Type binding_type, const InputBindingKey* keys,
size_t num_keys);
/// Represents a binding with icon fonts, if available.

Loading…
Cancel
Save