InputManager: Export a few functions

This commit is contained in:
Connor McLaughlin 2022-09-22 23:11:38 +10:00
parent d25a9a22e3
commit 583b8cd955
2 changed files with 74 additions and 43 deletions

View file

@ -36,14 +36,6 @@ enum : u32
LAST_EXTERNAL_INPUT_SOURCE = static_cast<u32>(InputSourceType::Count), LAST_EXTERNAL_INPUT_SOURCE = static_cast<u32>(InputSourceType::Count),
}; };
// ------------------------------------------------------------------------
// Event Handler Type
// ------------------------------------------------------------------------
// This class acts as an adapter to convert from normalized values to
// binary values when the callback is a binary/button handler. That way
// you don't need to convert float->bool in your callbacks.
using InputEventHandler = std::variant<InputAxisEventHandler, InputButtonEventHandler>;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Binding Type // Binding Type
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -107,7 +99,6 @@ static std::optional<InputBindingKey> ParseSensorKey(const std::string_view& sou
static std::vector<std::string_view> SplitChord(const std::string_view& binding); 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 bool SplitBinding(const std::string_view& binding, std::string_view* source, std::string_view* sub_binding);
static void AddBindings(const std::vector<std::string>& bindings, 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);
static bool IsAxisHandler(const InputEventHandler& handler); static bool IsAxisHandler(const InputEventHandler& handler);
@ -341,7 +332,11 @@ std::string InputManager::ConvertInputBindingKeysToString(const InputBindingKey*
void InputManager::AddBindings(const std::vector<std::string>& bindings, const InputEventHandler& handler) void InputManager::AddBindings(const std::vector<std::string>& bindings, const InputEventHandler& handler)
{ {
for (const std::string& binding : bindings) for (const std::string& binding : bindings)
{ AddBinding(binding, handler);
}
void InputManager::AddBinding(const std::string_view& binding, const InputEventHandler& handler)
{
std::shared_ptr<InputBinding> ibinding; std::shared_ptr<InputBinding> ibinding;
const std::vector<std::string_view> chord_bindings(SplitChord(binding)); const std::vector<std::string_view> chord_bindings(SplitChord(binding));
@ -350,7 +345,7 @@ void InputManager::AddBindings(const std::vector<std::string>& bindings, const I
std::optional<InputBindingKey> key = ParseInputBindingKey(chord_binding); std::optional<InputBindingKey> key = ParseInputBindingKey(chord_binding);
if (!key.has_value()) if (!key.has_value())
{ {
Log_ErrorPrintf("Invalid binding: '%s'", binding.c_str()); Log_ErrorPrintf("Invalid binding: '%.*s'", static_cast<int>(binding.size()), binding.data());
ibinding.reset(); ibinding.reset();
break; break;
} }
@ -363,7 +358,8 @@ void InputManager::AddBindings(const std::vector<std::string>& bindings, const I
if (ibinding->num_keys == MAX_KEYS_PER_BINDING) if (ibinding->num_keys == MAX_KEYS_PER_BINDING)
{ {
Log_ErrorPrintf("Too many chord parts, max is %u (%s)", MAX_KEYS_PER_BINDING, binding.c_str()); Log_ErrorPrintf("Too many chord parts, max is %u (%.*s)", MAX_KEYS_PER_BINDING, static_cast<int>(binding.size()),
binding.data());
ibinding.reset(); ibinding.reset();
break; break;
} }
@ -374,12 +370,30 @@ void InputManager::AddBindings(const std::vector<std::string>& bindings, const I
} }
if (!ibinding) if (!ibinding)
continue; return;
// plop it in the input map for all the keys // plop it in the input map for all the keys
for (u32 i = 0; i < ibinding->num_keys; i++) for (u32 i = 0; i < ibinding->num_keys; i++)
s_binding_map.emplace(ibinding->keys[i].MaskDirection(), ibinding); s_binding_map.emplace(ibinding->keys[i].MaskDirection(), ibinding);
}
void InputManager::AddVibrationBinding(u32 pad_index, const InputBindingKey* motor_0_binding,
InputSource* motor_0_source, const InputBindingKey* motor_1_binding,
InputSource* motor_1_source)
{
PadVibrationBinding vib;
vib.pad_index = pad_index;
if (motor_0_binding)
{
vib.motors[0].binding = *motor_0_binding;
vib.motors[0].source = motor_0_source;
} }
if (motor_1_binding)
{
vib.motors[1].binding = *motor_1_binding;
vib.motors[1].source = motor_1_source;
}
s_pad_vibration_array.push_back(std::move(vib));
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -1356,10 +1370,9 @@ void InputManager::ReloadBindings(SettingsInterface& si, SettingsInterface& bind
if (!cinfo || cinfo->type == ControllerType::None) if (!cinfo || cinfo->type == ControllerType::None)
continue; continue;
// NOTE: Macros can be overridden per-game.
const std::string section(Controller::GetSettingsSection(pad)); const std::string section(Controller::GetSettingsSection(pad));
AddPadBindings(binding_si, section, pad, cinfo); AddPadBindings(binding_si, section, pad, cinfo);
LoadMacroButtonConfig(si, section, pad, cinfo); LoadMacroButtonConfig(binding_si, section, pad, cinfo);
} }
for (u32 axis = 0; axis < static_cast<u32>(InputPointerAxis::Count); axis++) for (u32 axis = 0; axis < static_cast<u32>(InputPointerAxis::Count); axis++)

View file

@ -91,6 +91,14 @@ using InputButtonEventHandler = std::function<void(s32 value)>;
/// Callback types for a normalized event. Usually used for pads. /// Callback types for a normalized event. Usually used for pads.
using InputAxisEventHandler = std::function<void(float value)>; using InputAxisEventHandler = std::function<void(float value)>;
/// ------------------------------------------------------------------------
/// Event Handler Type
/// ------------------------------------------------------------------------
/// This class acts as an adapter to convert from normalized values to
/// binary values when the callback is a binary/button handler. That way
/// you don't need to convert float->bool in your callbacks.
using InputEventHandler = std::variant<InputAxisEventHandler, InputButtonEventHandler>;
/// Input monitoring for external access. /// Input monitoring for external access.
struct InputInterceptHook struct InputInterceptHook
{ {
@ -226,6 +234,16 @@ bool HasAnyBindingsForKey(InputBindingKey key);
/// Can be safely called on another thread. /// Can be safely called on another thread.
bool HasAnyBindingsForSource(InputBindingKey key); bool HasAnyBindingsForSource(InputBindingKey key);
/// Parses a string binding into its components. Use with external AddBinding().
bool ParseBindingAndGetSource(const std::string_view& binding, InputBindingKey* key, InputSource** source);
/// Externally adds a fixed binding. Be sure to call *after* ReloadBindings() otherwise it will be lost.
void AddBinding(const std::string_view& binding, const InputEventHandler& handler);
/// Adds an external vibration binding.
void AddVibrationBinding(u32 pad_index, const InputBindingKey* motor_0_binding, InputSource* motor_0_source,
const InputBindingKey* motor_1_binding, InputSource* motor_1_source);
/// Updates internal state for any binds for this key, and fires callbacks as needed. /// Updates internal state for any binds for this key, and fires callbacks as needed.
/// Returns true if anything was bound to this key, otherwise false. /// Returns true if anything was bound to this key, otherwise false.
bool InvokeEvents(InputBindingKey key, float value, GenericInputBinding generic_key); bool InvokeEvents(InputBindingKey key, float value, GenericInputBinding generic_key);