Imgui: Implement keyboard input, fix #1797

This commit is contained in:
Vojtech Kral 2019-02-15 15:35:32 +01:00
parent 3901ac47d1
commit 2de814d478
5 changed files with 128 additions and 10 deletions

View file

@ -10,6 +10,7 @@
#include <wx/string.h>
#include <wx/event.h>
#include <wx/clipbrd.h>
#include <wx/debug.h>
#include <GL/glew.h>
@ -44,6 +45,7 @@ bool ImGuiWrapper::init()
ImGui::CreateContext();
init_default_font(m_style_scaling);
init_input();
ImGui::GetIO().IniFilename = nullptr;
@ -109,6 +111,28 @@ bool ImGuiWrapper::update_mouse_data(wxMouseEvent& evt)
return res;
}
bool ImGuiWrapper::update_key_data(wxKeyEvent &evt)
{
ImGuiIO& io = ImGui::GetIO();
if (evt.GetEventType() == wxEVT_CHAR) {
// Char event
io.AddInputCharacter(evt.GetUnicodeKey());
} else {
// Key up/down event
int key = evt.GetKeyCode();
wxCHECK_MSG(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown), false, "Received invalid key code");
io.KeysDown[key] = evt.GetEventType() == wxEVT_KEY_DOWN;
io.KeyShift = evt.ShiftDown();
io.KeyCtrl = evt.ControlDown();
io.KeyAlt = evt.AltDown();
io.KeySuper = evt.MetaDown();
}
return true;
}
void ImGuiWrapper::new_frame()
{
if (m_font_texture == 0)
@ -307,6 +331,39 @@ void ImGuiWrapper::create_fonts_texture()
glBindTexture(GL_TEXTURE_2D, last_texture);
}
void ImGuiWrapper::init_input()
{
ImGuiIO& io = ImGui::GetIO();
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
io.KeyMap[ImGuiKey_Tab] = WXK_TAB;
io.KeyMap[ImGuiKey_LeftArrow] = WXK_LEFT;
io.KeyMap[ImGuiKey_RightArrow] = WXK_RIGHT;
io.KeyMap[ImGuiKey_UpArrow] = WXK_UP;
io.KeyMap[ImGuiKey_DownArrow] = WXK_DOWN;
io.KeyMap[ImGuiKey_PageUp] = WXK_PAGEUP;
io.KeyMap[ImGuiKey_PageDown] = WXK_PAGEDOWN;
io.KeyMap[ImGuiKey_Home] = WXK_HOME;
io.KeyMap[ImGuiKey_End] = WXK_END;
io.KeyMap[ImGuiKey_Insert] = WXK_INSERT;
io.KeyMap[ImGuiKey_Delete] = WXK_DELETE;
io.KeyMap[ImGuiKey_Backspace] = WXK_BACK;
io.KeyMap[ImGuiKey_Space] = WXK_SPACE;
io.KeyMap[ImGuiKey_Enter] = WXK_RETURN;
io.KeyMap[ImGuiKey_Escape] = WXK_ESCAPE;
io.KeyMap[ImGuiKey_A] = 'A';
io.KeyMap[ImGuiKey_C] = 'C';
io.KeyMap[ImGuiKey_V] = 'V';
io.KeyMap[ImGuiKey_X] = 'X';
io.KeyMap[ImGuiKey_Y] = 'Y';
io.KeyMap[ImGuiKey_Z] = 'Z';
// Setup clipboard interaction callbacks
io.SetClipboardTextFn = clipboard_set;
io.GetClipboardTextFn = clipboard_get;
io.ClipboardUserData = this;
}
void ImGuiWrapper::render_draw_data(ImDrawData *draw_data)
{
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
@ -420,6 +477,37 @@ void ImGuiWrapper::destroy_fonts_texture()
}
}
const char* ImGuiWrapper::clipboard_get(void* user_data)
{
ImGuiWrapper *self = reinterpret_cast<ImGuiWrapper*>(user_data);
const char* res = "";
if (wxTheClipboard->Open()) {
if (wxTheClipboard->IsSupported(wxDF_TEXT)) {
wxTextDataObject data;
wxTheClipboard->GetData(data);
if (data.GetTextLength() > 0) {
self->clipboard_text = into_u8(data.GetText());
res = self->clipboard_text.c_str();
}
}
wxTheClipboard->Close();
}
return res;
}
void ImGuiWrapper::clipboard_set(void* /* user_data */, const char* text)
{
if (wxTheClipboard->Open()) {
wxTheClipboard->SetData(new wxTextDataObject(wxString::FromUTF8(text))); // object owned by the clipboard
wxTheClipboard->Close();
}
}
} // namespace GUI
} // namespace Slic3r