Qt: Forward text input to ImGui

This commit is contained in:
Connor McLaughlin 2022-08-20 22:08:15 +10:00 committed by refractionpcsx2
parent a19045f9d6
commit 9012afc534
3 changed files with 38 additions and 0 deletions

View File

@ -17,6 +17,8 @@
#include "common/Assertions.h"
#include "pcsx2/Frontend/ImGuiManager.h"
#include "DisplayWidget.h"
#include "EmuThread.h"
#include "MainWindow.h"
@ -224,6 +226,15 @@ bool DisplayWidget::event(QEvent* event)
case QEvent::KeyRelease:
{
const QKeyEvent* key_event = static_cast<QKeyEvent*>(event);
// Forward text input to imgui.
if (ImGuiManager::WantsTextInput() && key_event->type() == QEvent::KeyPress)
{
const QString text(key_event->text());
if (!text.isEmpty())
ImGuiManager::AddTextInput(text.toStdString());
}
if (key_event->isAutoRepeat())
return true;

View File

@ -778,6 +778,27 @@ ImFont* ImGuiManager::GetFixedFont()
#ifdef PCSX2_CORE
bool ImGuiManager::WantsTextInput()
{
return s_imgui_wants_keyboard.load(std::memory_order_acquire);
}
void ImGuiManager::AddTextInput(std::string str)
{
if (!s_imgui_wants_keyboard.load(std::memory_order_acquire))
return;
// Has to go through the CPU -> GS thread :(
Host::RunOnCPUThread([str = std::move(str)]() {
GetMTGS().RunOnGSThread([str = std::move(str)]() {
if (!ImGui::GetCurrentContext())
return;
ImGui::GetIO().AddInputCharactersUTF8(str.c_str());
});
});
}
void ImGuiManager::UpdateMousePosition(float x, float y)
{
if (!ImGui::GetCurrentContext())

View File

@ -50,6 +50,12 @@ namespace ImGuiManager
ImFont* GetFixedFont();
#ifdef PCSX2_CORE
/// Returns true if imgui wants to intercept text input.
bool WantsTextInput();
/// Called on the UI or CPU thread in response to a key press. String is UTF-8.
void AddTextInput(std::string str);
/// Called on the UI or CPU thread in response to mouse movement.
void UpdateMousePosition(float x, float y);