[ImGui] Use new key API

This commit is contained in:
Joel Linn 2022-11-20 19:45:29 +01:00 committed by Rick Gibbed
parent b641e39c4d
commit da9c90835b
2 changed files with 34 additions and 28 deletions

View File

@ -184,24 +184,6 @@ void ImGuiDrawer::Initialize() {
style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 1.00f, 0.00f, 0.21f); style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 1.00f, 0.00f, 0.21f);
style.Colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); style.Colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);
io.KeyMap[ImGuiKey_Tab] = int(ui::VirtualKey::kTab);
io.KeyMap[ImGuiKey_LeftArrow] = int(ui::VirtualKey::kLeft);
io.KeyMap[ImGuiKey_RightArrow] = int(ui::VirtualKey::kRight);
io.KeyMap[ImGuiKey_UpArrow] = int(ui::VirtualKey::kUp);
io.KeyMap[ImGuiKey_DownArrow] = int(ui::VirtualKey::kDown);
io.KeyMap[ImGuiKey_Home] = int(ui::VirtualKey::kHome);
io.KeyMap[ImGuiKey_End] = int(ui::VirtualKey::kEnd);
io.KeyMap[ImGuiKey_Delete] = int(ui::VirtualKey::kDelete);
io.KeyMap[ImGuiKey_Backspace] = int(ui::VirtualKey::kBack);
io.KeyMap[ImGuiKey_Enter] = int(ui::VirtualKey::kReturn);
io.KeyMap[ImGuiKey_Escape] = int(ui::VirtualKey::kEscape);
io.KeyMap[ImGuiKey_A] = int(ui::VirtualKey::kA);
io.KeyMap[ImGuiKey_C] = int(ui::VirtualKey::kC);
io.KeyMap[ImGuiKey_V] = int(ui::VirtualKey::kV);
io.KeyMap[ImGuiKey_X] = int(ui::VirtualKey::kX);
io.KeyMap[ImGuiKey_Y] = int(ui::VirtualKey::kY);
io.KeyMap[ImGuiKey_Z] = int(ui::VirtualKey::kZ);
frame_time_tick_frequency_ = double(Clock::QueryHostTickFrequency()); frame_time_tick_frequency_ = double(Clock::QueryHostTickFrequency());
last_frame_time_ticks_ = Clock::QueryHostTickCount(); last_frame_time_ticks_ = Clock::QueryHostTickCount();
@ -209,6 +191,33 @@ void ImGuiDrawer::Initialize() {
reset_mouse_position_after_next_frame_ = false; reset_mouse_position_after_next_frame_ = false;
} }
std::optional<ImGuiKey> ImGuiDrawer::VirtualKeyToImGuiKey(VirtualKey vkey) {
static const std::map<VirtualKey, ImGuiKey> map = {
{ui::VirtualKey::kTab, ImGuiKey_Tab},
{ui::VirtualKey::kLeft, ImGuiKey_LeftArrow},
{ui::VirtualKey::kRight, ImGuiKey_RightArrow},
{ui::VirtualKey::kUp, ImGuiKey_UpArrow},
{ui::VirtualKey::kDown, ImGuiKey_DownArrow},
{ui::VirtualKey::kHome, ImGuiKey_Home},
{ui::VirtualKey::kEnd, ImGuiKey_End},
{ui::VirtualKey::kDelete, ImGuiKey_Delete},
{ui::VirtualKey::kBack, ImGuiKey_Backspace},
{ui::VirtualKey::kReturn, ImGuiKey_Enter},
{ui::VirtualKey::kEscape, ImGuiKey_Escape},
{ui::VirtualKey::kA, ImGuiKey_A},
{ui::VirtualKey::kC, ImGuiKey_C},
{ui::VirtualKey::kV, ImGuiKey_V},
{ui::VirtualKey::kX, ImGuiKey_X},
{ui::VirtualKey::kY, ImGuiKey_Y},
{ui::VirtualKey::kZ, ImGuiKey_Z},
};
if (auto search = map.find(vkey); search != map.end()) {
return search->second;
} else {
return std::nullopt;
}
}
void ImGuiDrawer::SetupFontTexture() { void ImGuiDrawer::SetupFontTexture() {
if (font_texture_ || !immediate_drawer_) { if (font_texture_ || !immediate_drawer_) {
return; return;
@ -484,13 +493,7 @@ void ImGuiDrawer::ClearInput() {
} }
io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
std::memset(io.MouseDown, 0, sizeof(io.MouseDown)); std::memset(io.MouseDown, 0, sizeof(io.MouseDown));
io.MouseWheel = 0.0f; io.ClearInputKeys();
io.MouseWheelH = 0.0f;
io.KeyCtrl = false;
io.KeyShift = false;
io.KeyAlt = false;
io.KeySuper = false;
std::memset(io.KeysDown, 0, sizeof(io.KeysDown));
io.ClearInputCharacters(); io.ClearInputCharacters();
touch_pointer_id_ = TouchEvent::kPointerIDNone; touch_pointer_id_ = TouchEvent::kPointerIDNone;
reset_mouse_position_after_next_frame_ = false; reset_mouse_position_after_next_frame_ = false;
@ -498,9 +501,9 @@ void ImGuiDrawer::ClearInput() {
void ImGuiDrawer::OnKey(KeyEvent& e, bool is_down) { void ImGuiDrawer::OnKey(KeyEvent& e, bool is_down) {
auto& io = GetIO(); auto& io = GetIO();
VirtualKey virtual_key = e.virtual_key(); const VirtualKey virtual_key = e.virtual_key();
if (size_t(virtual_key) < xe::countof(io.KeysDown)) { if (auto imGuiKey = VirtualKeyToImGuiKey(virtual_key); imGuiKey) {
io.KeysDown[size_t(virtual_key)] = is_down; io.AddKeyEvent(*imGuiKey, is_down);
} }
switch (virtual_key) { switch (virtual_key) {
case VirtualKey::kShift: case VirtualKey::kShift:

View File

@ -23,6 +23,7 @@
struct ImDrawData; struct ImDrawData;
struct ImGuiContext; struct ImGuiContext;
struct ImGuiIO; struct ImGuiIO;
enum ImGuiKey : int;
namespace xe { namespace xe {
namespace ui { namespace ui {
@ -77,6 +78,8 @@ class ImGuiDrawer : public WindowInputListener, public UIDrawer {
bool IsDrawingDialogs() const { return dialog_loop_next_index_ != SIZE_MAX; } bool IsDrawingDialogs() const { return dialog_loop_next_index_ != SIZE_MAX; }
void DetachIfLastDialogRemoved(); void DetachIfLastDialogRemoved();
std::optional<ImGuiKey> VirtualKeyToImGuiKey(VirtualKey vkey);
Window* window_; Window* window_;
size_t z_order_; size_t z_order_;