diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index ca53f92667..3c47e2c264 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -5162,7 +5162,8 @@ void GLCanvas3D::bind_event_handlers() m_canvas->Bind(wxEVT_SIZE, &GLCanvas3D::on_size, this); m_canvas->Bind(wxEVT_IDLE, &GLCanvas3D::on_idle, this); m_canvas->Bind(wxEVT_CHAR, &GLCanvas3D::on_char, this); - m_canvas->Bind(wxEVT_KEY_UP, &GLCanvas3D::on_key_up, this); + m_canvas->Bind(wxEVT_KEY_DOWN, &GLCanvas3D::on_key, this); + m_canvas->Bind(wxEVT_KEY_UP, &GLCanvas3D::on_key, this); m_canvas->Bind(wxEVT_MOUSEWHEEL, &GLCanvas3D::on_mouse_wheel, this); m_canvas->Bind(wxEVT_TIMER, &GLCanvas3D::on_timer, this); m_canvas->Bind(wxEVT_LEFT_DOWN, &GLCanvas3D::on_mouse, this); @@ -5188,7 +5189,8 @@ void GLCanvas3D::unbind_event_handlers() m_canvas->Unbind(wxEVT_SIZE, &GLCanvas3D::on_size, this); m_canvas->Unbind(wxEVT_IDLE, &GLCanvas3D::on_idle, this); m_canvas->Unbind(wxEVT_CHAR, &GLCanvas3D::on_char, this); - m_canvas->Unbind(wxEVT_KEY_UP, &GLCanvas3D::on_key_up, this); + m_canvas->Unbind(wxEVT_KEY_DOWN, &GLCanvas3D::on_key, this); + m_canvas->Unbind(wxEVT_KEY_UP, &GLCanvas3D::on_key, this); m_canvas->Unbind(wxEVT_MOUSEWHEEL, &GLCanvas3D::on_mouse_wheel, this); m_canvas->Unbind(wxEVT_TIMER, &GLCanvas3D::on_timer, this); m_canvas->Unbind(wxEVT_LEFT_DOWN, &GLCanvas3D::on_mouse, this); @@ -5225,6 +5227,15 @@ void GLCanvas3D::on_char(wxKeyEvent& evt) // see include/wx/defs.h enum wxKeyCode int keyCode = evt.GetKeyCode(); int ctrlMask = wxMOD_CONTROL; + +#if ENABLE_IMGUI + auto imgui = wxGetApp().imgui(); + if (imgui->update_key_data(evt)) { + return; + render(); + } +#endif // ENABLE_IMGUI + //#ifdef __APPLE__ // ctrlMask |= wxMOD_RAW_CONTROL; //#endif /* __APPLE__ */ @@ -5300,14 +5311,23 @@ void GLCanvas3D::on_char(wxKeyEvent& evt) } } -void GLCanvas3D::on_key_up(wxKeyEvent& evt) +void GLCanvas3D::on_key(wxKeyEvent& evt) { - // see include/wx/defs.h enum wxKeyCode - int keyCode = evt.GetKeyCode(); +#if ENABLE_IMGUI + auto imgui = wxGetApp().imgui(); + if (imgui->update_key_data(evt)) { + render(); + } else +#endif // ENABLE_IMGUI + if (evt.GetEventType() == wxEVT_KEY_UP) { + const int keyCode = evt.GetKeyCode(); + + // shift has been just released - SLA gizmo might want to close rectangular selection. + if (m_gizmos.get_current_type() == Gizmos::SlaSupports && keyCode == WXK_SHIFT && m_gizmos.mouse_event(SLAGizmoEventType::ShiftUp)) + m_dirty = true; + } - // shift has been just released - SLA gizmo might want to close rectangular selection. - if (m_gizmos.get_current_type() == Gizmos::SlaSupports && keyCode == WXK_SHIFT && m_gizmos.mouse_event(SLAGizmoEventType::ShiftUp)) - m_dirty = true; + evt.Skip(); // Needed to have EVT_CHAR generated as well } void GLCanvas3D::on_mouse_wheel(wxMouseEvent& evt) @@ -5364,9 +5384,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) auto imgui = wxGetApp().imgui(); if (imgui->update_mouse_data(evt)) { render(); - if (imgui->want_any_input()) { - return; - } + return; } #endif // ENABLE_IMGUI diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index 3317c6378e..3d44aa13f9 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -1075,7 +1075,7 @@ public: void on_size(wxSizeEvent& evt); void on_idle(wxIdleEvent& evt); void on_char(wxKeyEvent& evt); - void on_key_up(wxKeyEvent& evt); + void on_key(wxKeyEvent& evt); void on_mouse_wheel(wxMouseEvent& evt); void on_timer(wxTimerEvent& evt); void on_mouse(wxMouseEvent& evt); diff --git a/src/slic3r/GUI/GLCanvas3DManager.cpp b/src/slic3r/GUI/GLCanvas3DManager.cpp index 8f2e5b219c..71299f777d 100644 --- a/src/slic3r/GUI/GLCanvas3DManager.cpp +++ b/src/slic3r/GUI/GLCanvas3DManager.cpp @@ -233,7 +233,7 @@ wxGLCanvas* GLCanvas3DManager::create_wxglcanvas(wxWindow *parent) attribList[4] = 0; } - return new wxGLCanvas(parent, wxID_ANY, attribList); + return new wxGLCanvas(parent, wxID_ANY, attribList, wxDefaultPosition, wxDefaultSize, wxWANTS_CHARS); } GLCanvas3DManager::CanvasesMap::iterator GLCanvas3DManager::_get_canvas(wxGLCanvas* canvas) diff --git a/src/slic3r/GUI/ImGuiWrapper.cpp b/src/slic3r/GUI/ImGuiWrapper.cpp index 63fa09ae2c..d8d8089c23 100644 --- a/src/slic3r/GUI/ImGuiWrapper.cpp +++ b/src/slic3r/GUI/ImGuiWrapper.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -30,6 +31,7 @@ ImGuiWrapper::ImGuiWrapper() , m_style_scaling(1.0) , m_mouse_buttons(0) , m_disabled(false) + , m_new_frame_open(false) { } @@ -44,6 +46,8 @@ bool ImGuiWrapper::init() ImGui::CreateContext(); init_default_font(m_style_scaling); + init_input(); + init_style(); ImGui::GetIO().IniFilename = nullptr; @@ -104,23 +108,59 @@ bool ImGuiWrapper::update_mouse_data(wxMouseEvent& evt) io.MouseDown[2] = evt.MiddleDown(); unsigned buttons = (evt.LeftDown() ? 1 : 0) | (evt.RightDown() ? 2 : 0) | (evt.MiddleDown() ? 4 : 0); - bool res = buttons != m_mouse_buttons; m_mouse_buttons = buttons; - return res; + + new_frame(); + return want_mouse(); +} + +bool ImGuiWrapper::update_key_data(wxKeyEvent &evt) +{ + ImGuiIO& io = ImGui::GetIO(); + + if (evt.GetEventType() == wxEVT_CHAR) { + // Char event + const auto key = evt.GetUnicodeKey(); + if (key != 0) { + io.AddInputCharacter(key); + } + } 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(); + } + + // XXX: Unfortunatelly this seems broken due to some interference with wxWidgets, + // we have to return true always (perform re-render). + // new_frame(); + // return want_keyboard() || want_text_input(); + return true; } void ImGuiWrapper::new_frame() { + if (m_new_frame_open) { + return; + } + if (m_font_texture == 0) create_device_objects(); ImGui::NewFrame(); + m_new_frame_open = true; } void ImGuiWrapper::render() { ImGui::Render(); render_draw_data(ImGui::GetDrawData()); + m_new_frame_open = false; } void ImGuiWrapper::set_next_window_pos(float x, float y, int flag) @@ -307,6 +347,82 @@ 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'; + + // Don't let imgui special-case Mac, wxWidgets already do that + io.ConfigMacOSXBehaviors = false; + + // Setup clipboard interaction callbacks + io.SetClipboardTextFn = clipboard_set; + io.GetClipboardTextFn = clipboard_get; + io.ClipboardUserData = this; +} + +void ImGuiWrapper::init_style() +{ + ImGuiStyle &style = ImGui::GetStyle(); + + auto set_color = [&](ImGuiCol_ col, unsigned hex_color) { + style.Colors[col] = ImVec4( + ((hex_color >> 24) & 0xff) / 255.0f, + ((hex_color >> 16) & 0xff) / 255.0f, + ((hex_color >> 8) & 0xff) / 255.0f, + (hex_color & 0xff) / 255.0f); + }; + + static const unsigned COL_GREY_DARK = 0x444444ff; + static const unsigned COL_GREY_LIGHT = 0x666666ff; + static const unsigned COL_ORANGE_DARK = 0xba5418ff; + static const unsigned COL_ORANGE_LIGHT = 0xff6f22ff; + + // Generics + set_color(ImGuiCol_TitleBgActive, COL_ORANGE_DARK); + set_color(ImGuiCol_FrameBg, COL_GREY_DARK); + set_color(ImGuiCol_FrameBgHovered, COL_GREY_LIGHT); + set_color(ImGuiCol_FrameBgActive, COL_GREY_LIGHT); + + // Text selection + set_color(ImGuiCol_TextSelectedBg, COL_ORANGE_DARK); + + // Buttons + set_color(ImGuiCol_Button, COL_ORANGE_DARK); + set_color(ImGuiCol_ButtonHovered, COL_ORANGE_LIGHT); + set_color(ImGuiCol_ButtonActive, COL_ORANGE_LIGHT); + + // Checkbox + set_color(ImGuiCol_CheckMark, COL_ORANGE_LIGHT); + + // ComboBox items + set_color(ImGuiCol_Header, COL_ORANGE_DARK); + set_color(ImGuiCol_HeaderHovered, COL_ORANGE_LIGHT); + set_color(ImGuiCol_HeaderActive, COL_ORANGE_LIGHT); +} + void ImGuiWrapper::render_draw_data(ImDrawData *draw_data) { // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) @@ -420,6 +536,37 @@ void ImGuiWrapper::destroy_fonts_texture() } } +const char* ImGuiWrapper::clipboard_get(void* user_data) +{ + ImGuiWrapper *self = reinterpret_cast(user_data); + + const char* res = ""; + + if (wxTheClipboard->Open()) { + if (wxTheClipboard->IsSupported(wxDF_TEXT)) { + wxTextDataObject data; + wxTheClipboard->GetData(data); + + if (data.GetTextLength() > 0) { + self->m_clipboard_text = into_u8(data.GetText()); + res = self->m_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 diff --git a/src/slic3r/GUI/ImGuiWrapper.hpp b/src/slic3r/GUI/ImGuiWrapper.hpp index a4f5a89762..e8755718b3 100644 --- a/src/slic3r/GUI/ImGuiWrapper.hpp +++ b/src/slic3r/GUI/ImGuiWrapper.hpp @@ -10,6 +10,7 @@ class wxString; class wxMouseEvent; +class wxKeyEvent; namespace Slic3r { @@ -25,6 +26,8 @@ class ImGuiWrapper float m_style_scaling; unsigned m_mouse_buttons; bool m_disabled; + bool m_new_frame_open; + std::string m_clipboard_text; public: ImGuiWrapper(); @@ -37,6 +40,7 @@ public: void set_display_size(float w, float h); void set_style_scaling(float scaling); bool update_mouse_data(wxMouseEvent &evt); + bool update_key_data(wxKeyEvent &evt); void new_frame(); void render(); @@ -68,9 +72,14 @@ private: void init_default_font(float scaling); void create_device_objects(); void create_fonts_texture(); + void init_input(); + void init_style(); void render_draw_data(ImDrawData *draw_data); void destroy_device_objects(); void destroy_fonts_texture(); + + static const char* clipboard_get(void* user_data); + static void clipboard_set(void* user_data, const char* text); };