CommonHostInterface: Support mouse bindings

This commit is contained in:
Connor McLaughlin 2020-04-26 17:21:49 +10:00
parent 02db665d4a
commit fce35d6dbe
2 changed files with 33 additions and 0 deletions

View file

@ -424,9 +424,20 @@ bool CommonHostInterface::HandleHostKeyEvent(HostKeyCode key, bool pressed)
return true; return true;
} }
bool CommonHostInterface::HandleHostMouseEvent(HostMouseButton button, bool pressed)
{
const auto iter = m_mouse_input_handlers.find(button);
if (iter == m_mouse_input_handlers.end())
return false;
iter->second(pressed);
return true;
}
void CommonHostInterface::UpdateInputMap(SettingsInterface& si) void CommonHostInterface::UpdateInputMap(SettingsInterface& si)
{ {
m_keyboard_input_handlers.clear(); m_keyboard_input_handlers.clear();
m_mouse_input_handlers.clear();
if (m_controller_interface) if (m_controller_interface)
m_controller_interface->ClearBindings(); m_controller_interface->ClearBindings();
@ -601,6 +612,25 @@ bool CommonHostInterface::AddButtonToInputMap(const std::string& binding, const
return true; return true;
} }
if (device == "Mouse")
{
if (StringUtil::StartsWith(button, "Button"))
{
const std::optional<s32> button_index = StringUtil::FromChars<s32>(button.substr(6));
if (!button_index.has_value())
{
Log_WarningPrintf("Invalid button in mouse binding '%s'", binding.c_str());
return false;
}
m_mouse_input_handlers.emplace(static_cast<HostMouseButton>(button_index.value()), std::move(handler));
return true;
}
Log_WarningPrintf("Malformed mouse binding '%s'", binding.c_str());
return false;
}
if (StringUtil::StartsWith(device, "Controller")) if (StringUtil::StartsWith(device, "Controller"))
{ {
if (!m_controller_interface) if (!m_controller_interface)

View file

@ -23,6 +23,7 @@ public:
friend ControllerInterface; friend ControllerInterface;
using HostKeyCode = s32; using HostKeyCode = s32;
using HostMouseButton = s32;
using InputButtonHandler = std::function<void(bool)>; using InputButtonHandler = std::function<void(bool)>;
using InputAxisHandler = std::function<void(float)>; using InputAxisHandler = std::function<void(float)>;
@ -102,6 +103,7 @@ protected:
void RegisterHotkey(String category, String name, String display_name, InputButtonHandler handler); void RegisterHotkey(String category, String name, String display_name, InputButtonHandler handler);
bool HandleHostKeyEvent(HostKeyCode code, bool pressed); bool HandleHostKeyEvent(HostKeyCode code, bool pressed);
bool HandleHostMouseEvent(HostMouseButton button, bool pressed);
void UpdateInputMap(SettingsInterface& si); void UpdateInputMap(SettingsInterface& si);
void AddControllerRumble(u32 controller_index, u32 num_motors, ControllerRumbleCallback callback); void AddControllerRumble(u32 controller_index, u32 num_motors, ControllerRumbleCallback callback);
@ -124,6 +126,7 @@ private:
// input key maps // input key maps
std::map<HostKeyCode, InputButtonHandler> m_keyboard_input_handlers; std::map<HostKeyCode, InputButtonHandler> m_keyboard_input_handlers;
std::map<HostMouseButton, InputButtonHandler> m_mouse_input_handlers;
// controller vibration motors/rumble // controller vibration motors/rumble
struct ControllerRumbleState struct ControllerRumbleState