Insert a more solid abstraction between Qt and Imgui

This commit is contained in:
Scott Mansell 2023-02-03 19:11:31 +13:00
parent b2a31103b4
commit 9c1fe59cc9
9 changed files with 80 additions and 38 deletions

View File

@ -675,6 +675,7 @@
<ClInclude Include="VideoCommon\NetPlayGolfUI.h" /> <ClInclude Include="VideoCommon\NetPlayGolfUI.h" />
<ClInclude Include="VideoCommon\OnScreenDisplay.h" /> <ClInclude Include="VideoCommon\OnScreenDisplay.h" />
<ClInclude Include="VideoCommon\OnScreenUI.h" /> <ClInclude Include="VideoCommon\OnScreenUI.h" />
<ClInclude Include="VideoCommon\OnScreenUIKeyMap.h" />
<ClInclude Include="VideoCommon\OpcodeDecoding.h" /> <ClInclude Include="VideoCommon\OpcodeDecoding.h" />
<ClInclude Include="VideoCommon\PerfQueryBase.h" /> <ClInclude Include="VideoCommon\PerfQueryBase.h" />
<ClInclude Include="VideoCommon\PerformanceMetrics.h" /> <ClInclude Include="VideoCommon\PerformanceMetrics.h" />

View File

@ -19,8 +19,6 @@
#include <QTimer> #include <QTimer>
#include <QWindow> #include <QWindow>
#include <imgui.h>
#include "Core/Config/MainSettings.h" #include "Core/Config/MainSettings.h"
#include "Core/Core.h" #include "Core/Core.h"
#include "Core/State.h" #include "Core/State.h"
@ -32,6 +30,7 @@
#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "VideoCommon/OnScreenUI.h"
#include "VideoCommon/Present.h" #include "VideoCommon/Present.h"
#include "VideoCommon/VideoConfig.h" #include "VideoCommon/VideoConfig.h"
@ -62,7 +61,7 @@ RenderWidget::RenderWidget(QWidget* parent) : QWidget(parent)
connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this](Core::State state) { connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this](Core::State state) {
if (state == Core::State::Running) if (state == Core::State::Running)
SetImGuiKeyMap(); SetPresenterKeyMap();
}); });
// We have to use Qt::DirectConnection here because we don't want those signals to get queued // We have to use Qt::DirectConnection here because we don't want those signals to get queued
@ -338,7 +337,7 @@ void RenderWidget::SetWaitingForMessageBox(bool waiting_for_message_box)
bool RenderWidget::event(QEvent* event) bool RenderWidget::event(QEvent* event)
{ {
PassEventToImGui(event); PassEventToPresenter(event);
switch (event->type()) switch (event->type())
{ {
@ -470,7 +469,7 @@ bool RenderWidget::event(QEvent* event)
return QWidget::event(event); return QWidget::event(event);
} }
void RenderWidget::PassEventToImGui(const QEvent* event) void RenderWidget::PassEventToPresenter(const QEvent* event)
{ {
if (!Core::IsRunningAndStarted()) if (!Core::IsRunningAndStarted())
return; return;
@ -498,7 +497,7 @@ void RenderWidget::PassEventToImGui(const QEvent* event)
chars = utf8.constData(); chars = utf8.constData();
} }
// Pass the key onto ImGui // Pass the key onto Presenter (for the imgui UI)
g_presenter->SetKey(key, is_down, chars); g_presenter->SetKey(key, is_down, chars);
} }
break; break;
@ -529,31 +528,16 @@ void RenderWidget::PassEventToImGui(const QEvent* event)
} }
} }
void RenderWidget::SetImGuiKeyMap() void RenderWidget::SetPresenterKeyMap()
{ {
static std::array<std::array<int, 2>, 21> key_map{{ static constexpr DolphinKeyMap key_map = {
{ImGuiKey_Tab, Qt::Key_Tab}, Qt::Key_Tab, Qt::Key_Left, Qt::Key_Right, Qt::Key_Up, Qt::Key_Down,
{ImGuiKey_LeftArrow, Qt::Key_Left}, Qt::Key_PageUp, Qt::Key_PageDown, Qt::Key_Home, Qt::Key_End, Qt::Key_Insert,
{ImGuiKey_RightArrow, Qt::Key_Right}, Qt::Key_Delete, Qt::Key_Backspace, Qt::Key_Space, Qt::Key_Return, Qt::Key_Escape,
{ImGuiKey_UpArrow, Qt::Key_Up}, Qt::Key_Enter, // Keypad enter
{ImGuiKey_DownArrow, Qt::Key_Down}, Qt::Key_A, Qt::Key_C, Qt::Key_V, Qt::Key_X, Qt::Key_Y,
{ImGuiKey_PageUp, Qt::Key_PageUp}, Qt::Key_Z,
{ImGuiKey_PageDown, Qt::Key_PageDown}, };
{ImGuiKey_Home, Qt::Key_Home},
{ImGuiKey_End, Qt::Key_End},
{ImGuiKey_Insert, Qt::Key_Insert},
{ImGuiKey_Delete, Qt::Key_Delete},
{ImGuiKey_Backspace, Qt::Key_Backspace},
{ImGuiKey_Space, Qt::Key_Space},
{ImGuiKey_Enter, Qt::Key_Return},
{ImGuiKey_Escape, Qt::Key_Escape},
{ImGuiKey_A, Qt::Key_A},
{ImGuiKey_C, Qt::Key_C},
{ImGuiKey_V, Qt::Key_V},
{ImGuiKey_X, Qt::Key_X},
{ImGuiKey_Y, Qt::Key_Y},
{ImGuiKey_Z, Qt::Key_Z},
}};
g_presenter->SetKeyMap(key_map); g_presenter->SetKeyMap(key_map);
} }

View File

@ -39,8 +39,8 @@ private:
void OnLockCursorChanged(); void OnLockCursorChanged();
void OnKeepOnTopChanged(bool top); void OnKeepOnTopChanged(bool top);
void UpdateCursor(); void UpdateCursor();
void PassEventToImGui(const QEvent* event); void PassEventToPresenter(const QEvent* event);
void SetImGuiKeyMap(); void SetPresenterKeyMap();
void dragEnterEvent(QDragEnterEvent* event) override; void dragEnterEvent(QDragEnterEvent* event) override;
void dropEvent(QDropEvent* event) override; void dropEvent(QDropEvent* event) override;

View File

@ -89,6 +89,7 @@ add_library(videocommon
OnScreenDisplay.h OnScreenDisplay.h
OnScreenUI.cpp OnScreenUI.cpp
OnScreenUI.h OnScreenUI.h
OnScreenUIKeyMap.h
OpcodeDecoding.cpp OpcodeDecoding.cpp
OpcodeDecoding.h OpcodeDecoding.h
PerfQueryBase.cpp PerfQueryBase.cpp

View File

@ -3,6 +3,7 @@
#include "VideoCommon/OnScreenUI.h" #include "VideoCommon/OnScreenUI.h"
#include "Common/EnumMap.h"
#include "Common/Profiler.h" #include "Common/Profiler.h"
#include "Common/Timer.h" #include "Common/Timer.h"
@ -346,15 +347,30 @@ void OnScreenUI::SetScale(float backbuffer_scale)
m_backbuffer_scale = backbuffer_scale; m_backbuffer_scale = backbuffer_scale;
} }
void OnScreenUI::SetKeyMap(std::span<const std::array<int, 2>> key_map) void OnScreenUI::SetKeyMap(const DolphinKeyMap& key_map)
{ {
// Right now this is a 1:1 mapping. But might not be true later
static constexpr DolphinKeyMap dolphin_to_imgui_map = {
ImGuiKey_Tab, ImGuiKey_LeftArrow, ImGuiKey_RightArrow, ImGuiKey_UpArrow,
ImGuiKey_DownArrow, ImGuiKey_PageUp, ImGuiKey_PageDown, ImGuiKey_Home,
ImGuiKey_End, ImGuiKey_Insert, ImGuiKey_Delete, ImGuiKey_Backspace,
ImGuiKey_Space, ImGuiKey_Enter, ImGuiKey_Escape, ImGuiKey_KeyPadEnter,
ImGuiKey_A, ImGuiKey_C, ImGuiKey_V, ImGuiKey_X,
ImGuiKey_Y, ImGuiKey_Z,
};
static_assert(dolphin_to_imgui_map.size() == ImGuiKey_COUNT); // Fail if ImGui adds keys
auto lock = GetImGuiLock(); auto lock = GetImGuiLock();
if (!ImGui::GetCurrentContext()) if (!ImGui::GetCurrentContext())
return; return;
for (auto [imgui_key, qt_key] : key_map) for (int dolphin_key = 0; dolphin_key <= static_cast<int>(DolphinKey::Z); dolphin_key++)
ImGui::GetIO().KeyMap[imgui_key] = (qt_key & 0x1FF); {
int imgui_key = dolphin_to_imgui_map[DolphinKey(dolphin_key)];
if (imgui_key >= 0)
ImGui::GetIO().KeyMap[imgui_key] = (key_map[DolphinKey(dolphin_key)] & 0x1FF);
}
} }
void OnScreenUI::SetKey(u32 key, bool is_down, const char* chars) void OnScreenUI::SetKey(u32 key, bool is_down, const char* chars)

View File

@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "VideoCommon/OnScreenUIKeyMap.h"
class NativeVertexFormat; class NativeVertexFormat;
class AbstractTexture; class AbstractTexture;
@ -52,7 +53,7 @@ public:
void Finalize(); void Finalize();
// Receive keyboard and mouse from QT // Receive keyboard and mouse from QT
void SetKeyMap(std::span<const std::array<int, 2>> key_map); void SetKeyMap(const DolphinKeyMap& key_map);
void SetKey(u32 key, bool is_down, const char* chars); void SetKey(u32 key, bool is_down, const char* chars);
void SetMousePos(float x, float y); void SetMousePos(float x, float y);
void SetMousePress(u32 button_mask); void SetMousePress(u32 button_mask);

View File

@ -0,0 +1,37 @@
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "Common/EnumMap.h"
// The main point of this is to allow other parts of dolphin to set ImGui's key map without
// having to import ImGui headers.
// But the idea is that it can be expanded in the future with more keys to support more things.
enum class DolphinKey
{
Tab,
LeftArrow,
RightArrow,
UpArrow,
DownArrow,
PageUp,
PageDown,
Home,
End,
Insert,
Delete,
Backspace,
Space,
Enter,
Escape,
KeyPadEnter,
A, // for text edit CTRL+A: select all
C, // for text edit CTRL+C: copy
V, // for text edit CTRL+V: paste
X, // for text edit CTRL+X: cut
Y, // for text edit CTRL+Y: redo
Z, // for text edit CTRL+Z: undo
};
using DolphinKeyMap = Common::EnumMap<int, DolphinKey::Z>;

View File

@ -571,7 +571,7 @@ void Presenter::Present()
g_gfx->EndUtilityDrawing(); g_gfx->EndUtilityDrawing();
} }
void Presenter::SetKeyMap(std::span<std::array<int, 2>> key_map) void Presenter::SetKeyMap(const DolphinKeyMap& key_map)
{ {
if (m_onscreen_ui) if (m_onscreen_ui)
m_onscreen_ui->SetKeyMap(key_map); m_onscreen_ui->SetKeyMap(key_map);

View File

@ -6,6 +6,7 @@
#include "Common/Flag.h" #include "Common/Flag.h"
#include "Common/MathUtil.h" #include "Common/MathUtil.h"
#include "VideoCommon/OnScreenUIKeyMap.h"
#include "VideoCommon/TextureCacheBase.h" #include "VideoCommon/TextureCacheBase.h"
#include "VideoCommon/TextureConfig.h" #include "VideoCommon/TextureConfig.h"
#include "VideoCommon/VideoCommon.h" #include "VideoCommon/VideoCommon.h"
@ -18,6 +19,7 @@
class AbstractTexture; class AbstractTexture;
struct SurfaceInfo; struct SurfaceInfo;
enum class DolphinKey;
namespace VideoCommon namespace VideoCommon
{ {
@ -82,7 +84,7 @@ public:
bool SurfaceChangedTestAndClear() { return m_surface_changed.TestAndClear(); } bool SurfaceChangedTestAndClear() { return m_surface_changed.TestAndClear(); }
void* GetNewSurfaceHandle(); void* GetNewSurfaceHandle();
void SetKeyMap(std::span<std::array<int, 2>> key_map); void SetKeyMap(const DolphinKeyMap& key_map);
void SetKey(u32 key, bool is_down, const char* chars); void SetKey(u32 key, bool is_down, const char* chars);
void SetMousePos(float x, float y); void SetMousePos(float x, float y);