Allow keyboard binding in nogui/fullscreen mode (#1679)

* Allow keyboard binding in nogui/fullscreen mode
This commit is contained in:
Chris 2021-02-24 11:05:33 -05:00 committed by GitHub
parent 0d0a7eac1f
commit dd3d5dbd86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 1 deletions

View File

@ -4,6 +4,7 @@
#include "frontend-common/ini_settings_interface.h" #include "frontend-common/ini_settings_interface.h"
#include "frontend-common/sdl_controller_interface.h" #include "frontend-common/sdl_controller_interface.h"
#include "frontend-common/sdl_initializer.h" #include "frontend-common/sdl_initializer.h"
#include "frontend-common/fullscreen_ui.h"
#include "imgui.h" #include "imgui.h"
#include "imgui_impl_sdl.h" #include "imgui_impl_sdl.h"
#include "scmversion/scmversion.h" #include "scmversion/scmversion.h"
@ -275,7 +276,7 @@ void SDLHostInterface::PollAndUpdate()
} }
ImGui_ImplSDL2_NewFrame(); ImGui_ImplSDL2_NewFrame();
CommonHostInterface::PollAndUpdate(); NoGUIHostInterface::PollAndUpdate();
} }
void SDLHostInterface::HandleSDLEvent(const SDL_Event* event) void SDLHostInterface::HandleSDLEvent(const SDL_Event* event)
@ -305,6 +306,22 @@ void SDLHostInterface::HandleSDLEvent(const SDL_Event* event)
case SDL_KEYDOWN: case SDL_KEYDOWN:
case SDL_KEYUP: case SDL_KEYUP:
{ {
// Binding mode
if (m_fullscreen_ui_enabled && m_controller_interface && m_controller_interface->HasHook() && event->key.repeat == 0)
{
String keyName;
if (!SDLKeyNames::KeyEventToString(event, keyName))
{
break;
}
const bool pressed = (event->type == SDL_KEYDOWN);
if (FullscreenUI::HandleKeyboardBinding(keyName, pressed))
{
break;
}
}
if (!ImGui::GetIO().WantCaptureKeyboard && event->key.repeat == 0) if (!ImGui::GetIO().WantCaptureKeyboard && event->key.repeat == 0)
{ {
const HostKeyCode code = static_cast<HostKeyCode>(SDLKeyNames::KeyEventToInt(event)); const HostKeyCode code = static_cast<HostKeyCode>(SDLKeyNames::KeyEventToInt(event));

View File

@ -36,6 +36,12 @@ void ControllerInterface::ClearHook()
m_event_intercept_callback = {}; m_event_intercept_callback = {};
} }
bool ControllerInterface::HasHook()
{
std::unique_lock<std::mutex> lock(m_event_intercept_mutex);
return (bool)m_event_intercept_callback;
}
bool ControllerInterface::DoEventHook(Hook::Type type, int controller_index, int button_or_axis_number, bool ControllerInterface::DoEventHook(Hook::Type type, int controller_index, int button_or_axis_number,
std::variant<float, std::string_view> value, bool track_history) std::variant<float, std::string_view> value, bool track_history)
{ {

View File

@ -113,6 +113,7 @@ public:
}; };
void SetHook(Hook::Callback callback); void SetHook(Hook::Callback callback);
void ClearHook(); void ClearHook();
bool HasHook();
protected: protected:
bool DoEventHook(Hook::Type type, int controller_index, int button_or_axis_number, bool DoEventHook(Hook::Type type, int controller_index, int button_or_axis_number,

View File

@ -141,6 +141,7 @@ static InputBindingType s_input_binding_type = InputBindingType::None;
static TinyString s_input_binding_section; static TinyString s_input_binding_section;
static TinyString s_input_binding_key; static TinyString s_input_binding_key;
static TinyString s_input_binding_display_name; static TinyString s_input_binding_display_name;
static bool s_input_binding_keyboard_pressed;
static Common::Timer s_input_binding_timer; static Common::Timer s_input_binding_timer;
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -816,6 +817,34 @@ static void ClearInputBindingVariables()
s_input_binding_display_name.Clear(); s_input_binding_display_name.Clear();
} }
bool HandleKeyboardBinding(const char* keyName, bool pressed)
{
if (s_input_binding_type == InputBindingType::None)
return false;
if (pressed)
{
s_input_binding_keyboard_pressed = true;
return true;
}
if (!s_input_binding_keyboard_pressed)
{
return false;
}
TinyString value;
value.Format("Keyboard/%s", keyName);
s_host_interface->GetSettingsInterface()->SetStringValue(s_input_binding_section, s_input_binding_key, value);
s_host_interface->AddFormattedOSDMessage(5.0f, "Set %s binding %s to %s.", s_input_binding_section.GetCharArray(),
s_input_binding_display_name.GetCharArray(), value.GetCharArray());
EndInputBinding();
s_host_interface->RunLater(SaveAndApplySettings);
return true;
}
void BeginInputBinding(InputBindingType type, const std::string_view& section, const std::string_view& key, void BeginInputBinding(InputBindingType type, const std::string_view& section, const std::string_view& key,
const std::string_view& display_name) const std::string_view& display_name)
{ {

View File

@ -47,6 +47,7 @@ void OpenQuickMenu();
void CloseQuickMenu(); void CloseQuickMenu();
void Shutdown(); void Shutdown();
void Render(); void Render();
bool HandleKeyboardBinding(const char* keyName, bool pressed);
bool InvalidateCachedTexture(const std::string& path); bool InvalidateCachedTexture(const std::string& path);