InputManager: Split AddBinding() and AddBindings()

This commit is contained in:
Connor McLaughlin 2022-09-12 23:27:37 +10:00 committed by refractionpcsx2
parent 564c81575f
commit f8d9e6eeb8
1 changed files with 39 additions and 35 deletions

View File

@ -99,6 +99,7 @@ namespace InputManager
static std::vector<std::string_view> SplitChord(const std::string_view& binding);
static bool SplitBinding(const std::string_view& binding, std::string_view* source, std::string_view* sub_binding);
static void AddBinding(const std::string_view& binding, const InputEventHandler& handler);
static void AddBindings(const std::vector<std::string>& bindings, const InputEventHandler& handler);
static bool ParseBindingAndGetSource(const std::string_view& binding, InputBindingKey* key, InputSource** source);
@ -304,10 +305,8 @@ std::string InputManager::ConvertInputBindingKeysToString(const InputBindingKey*
return ss.str();
}
void InputManager::AddBindings(const std::vector<std::string>& bindings, const InputEventHandler& handler)
void InputManager::AddBinding(const std::string_view& binding, const InputEventHandler& handler)
{
for (const std::string& binding : bindings)
{
std::shared_ptr<InputBinding> ibinding;
const std::vector<std::string_view> chord_bindings(SplitChord(binding));
@ -316,7 +315,7 @@ void InputManager::AddBindings(const std::vector<std::string>& bindings, const I
std::optional<InputBindingKey> key = ParseInputBindingKey(chord_binding);
if (!key.has_value())
{
Console.WriteLn("Invalid binding: '%s'", binding.c_str());
Console.WriteLn(fmt::format("Invalid binding: '{}'", binding));
ibinding.reset();
break;
}
@ -329,7 +328,7 @@ void InputManager::AddBindings(const std::vector<std::string>& bindings, const I
if (ibinding->num_keys == MAX_KEYS_PER_BINDING)
{
Console.WriteLn("Too many chord parts, max is %u (%s)", MAX_KEYS_PER_BINDING, binding.c_str());
Console.WriteLn(fmt::format("Too many chord parts, max is {} ({})", MAX_KEYS_PER_BINDING, binding));
ibinding.reset();
break;
}
@ -340,12 +339,17 @@ void InputManager::AddBindings(const std::vector<std::string>& bindings, const I
}
if (!ibinding)
continue;
return;
// plop it in the input map for all the keys
for (u32 i = 0; i < ibinding->num_keys; i++)
s_binding_map.emplace(ibinding->keys[i].MaskDirection(), ibinding);
}
}
void InputManager::AddBindings(const std::vector<std::string>& bindings, const InputEventHandler& handler)
{
for (const std::string& binding : bindings)
AddBinding(binding, handler);
}
// ------------------------------------------------------------------------
@ -559,7 +563,7 @@ void InputManager::AddPadBindings(SettingsInterface& si, u32 pad_index, const ch
if (!bindings.empty())
{
// we use axes for all pad bindings to simplify things, and because they are pressure sensitive
AddBindings(bindings, InputAxisEventHandler{[pad_index, bind_index, bind_names](
AddBindings(bindings, InputAxisEventHandler{[pad_index, bind_index](
float value) { PAD::SetControllerState(pad_index, bind_index, value); }});
}
}