From cfd3e9b34923efda094824691a0c23faf9d9ea95 Mon Sep 17 00:00:00 2001 From: eliopark-nsuslab <150650541+eliopark-nsuslab@users.noreply.github.com> Date: Fri, 15 Mar 2024 16:17:18 -0700 Subject: [PATCH] Passing modifiers to mouse events for drag handling (#47) + Add click_count for double-click handling --- addons/gdcef/gdcef/src/browser_io.cpp | 37 ++++++++++++++++++++++++++- addons/gdcef/gdcef/src/gdbrowser.hpp | 8 ++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/addons/gdcef/gdcef/src/browser_io.cpp b/addons/gdcef/gdcef/src/browser_io.cpp index 9642114..5b9c07f 100644 --- a/addons/gdcef/gdcef/src/browser_io.cpp +++ b/addons/gdcef/gdcef/src/browser_io.cpp @@ -25,6 +25,7 @@ #include "gdbrowser.hpp" #include "helper_files.hpp" +#include //------------------------------------------------------------------------------ void GDBrowserView::leftClick() @@ -53,12 +54,28 @@ void GDBrowserView::leftMouseDown() if (!m_browser) return; + // increase click count but max == 3 + // double-click to select a word. + // triple-click to select a paragraph. + // more than triple-click keep paragraph selection. + m_left_click_count = godot::Math::clamp(m_left_click_count + 1, 1, 3); + + using namespace std::chrono; + system_clock::time_point now = system_clock::now(); + int64_t click_interval_ms = duration_cast(now - m_last_left_down).count(); + m_last_left_down = now; + if (click_interval_ms > 500) + m_left_click_count = 1; + + m_mouse_event_modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON; + CefBrowserHost::MouseButtonType btn = CefBrowserHost::MouseButtonType::MBT_LEFT; CefMouseEvent evt; evt.x = m_mouse_x; evt.y = m_mouse_y; + evt.modifiers = m_mouse_event_modifiers; - m_browser->GetHost()->SendMouseClickEvent(evt, btn, false, 1); + m_browser->GetHost()->SendMouseClickEvent(evt, btn, false, m_left_click_count); } //------------------------------------------------------------------------------ @@ -67,10 +84,13 @@ void GDBrowserView::rightMouseDown() if (!m_browser) return; + m_mouse_event_modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON; + CefBrowserHost::MouseButtonType btn = CefBrowserHost::MouseButtonType::MBT_RIGHT; CefMouseEvent evt; evt.x = m_mouse_x; evt.y = m_mouse_y; + evt.modifiers = m_mouse_event_modifiers; m_browser->GetHost()->SendMouseClickEvent(evt, btn, false, 1); } @@ -81,10 +101,13 @@ void GDBrowserView::leftMouseUp() if (!m_browser) return; + m_mouse_event_modifiers &= ~EVENTFLAG_LEFT_MOUSE_BUTTON; + CefBrowserHost::MouseButtonType btn = CefBrowserHost::MouseButtonType::MBT_LEFT; CefMouseEvent evt; evt.x = m_mouse_x; evt.y = m_mouse_y; + evt.modifiers = m_mouse_event_modifiers; m_browser->GetHost()->SendMouseClickEvent(evt, btn, true, 1); } @@ -95,10 +118,13 @@ void GDBrowserView::rightMouseUp() if (!m_browser) return; + m_mouse_event_modifiers &= ~EVENTFLAG_RIGHT_MOUSE_BUTTON; + CefBrowserHost::MouseButtonType btn = CefBrowserHost::MouseButtonType::MBT_RIGHT; CefMouseEvent evt; evt.x = m_mouse_x; evt.y = m_mouse_y; + evt.modifiers = m_mouse_event_modifiers; m_browser->GetHost()->SendMouseClickEvent(evt, btn, true, 1); } @@ -109,10 +135,13 @@ void GDBrowserView::middleMouseDown() if (!m_browser) return; + m_mouse_event_modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON; + CefBrowserHost::MouseButtonType btn = CefBrowserHost::MouseButtonType::MBT_MIDDLE; CefMouseEvent evt; evt.x = m_mouse_x; evt.y = m_mouse_y; + evt.modifiers = m_mouse_event_modifiers; m_browser->GetHost()->SendMouseClickEvent(evt, btn, false, 1); } @@ -123,10 +152,13 @@ void GDBrowserView::middleMouseUp() if (!m_browser) return; + m_mouse_event_modifiers &= ~EVENTFLAG_MIDDLE_MOUSE_BUTTON; + CefBrowserHost::MouseButtonType btn = CefBrowserHost::MouseButtonType::MBT_MIDDLE; CefMouseEvent evt; evt.x = m_mouse_x; evt.y = m_mouse_y; + evt.modifiers = m_mouse_event_modifiers; m_browser->GetHost()->SendMouseClickEvent(evt, btn, true, 1); } @@ -143,6 +175,7 @@ void GDBrowserView::mouseMove(int x, int y) CefMouseEvent evt; evt.x = x; evt.y = y; + evt.modifiers = m_mouse_event_modifiers; bool mouse_leave = false; // TODO // AD - Adding focus just like what's done in BLUI @@ -160,6 +193,7 @@ void GDBrowserView::mouseWheelVertical(const int wDelta) CefMouseEvent evt; evt.x = m_mouse_x; evt.y = m_mouse_y; + evt.modifiers = m_mouse_event_modifiers; m_browser->GetHost()->SendMouseWheelEvent(evt, 0, wDelta * 10); } @@ -173,6 +207,7 @@ void GDBrowserView::mouseWheelHorizontal(const int wDelta) CefMouseEvent evt; evt.x = m_mouse_x; evt.y = m_mouse_y; + evt.modifiers = m_mouse_event_modifiers; m_browser->GetHost()->SendMouseWheelEvent(evt, wDelta * 10, 0); } diff --git a/addons/gdcef/gdcef/src/gdbrowser.hpp b/addons/gdcef/gdcef/src/gdbrowser.hpp index 4e6e755..63ec562 100644 --- a/addons/gdcef/gdcef/src/gdbrowser.hpp +++ b/addons/gdcef/gdcef/src/gdbrowser.hpp @@ -72,6 +72,7 @@ # include # include +# include // **************************************************************************** //! \brief Class wrapping the CefBrowser class and export methods for Godot @@ -591,6 +592,13 @@ class GDBrowserView : public godot::Node int m_mouse_x = 0; int m_mouse_y = 0; + //! \brief Mouse button modifiers on the mouse event + uint32_t m_mouse_event_modifiers = 0; + + //! \brief Left mouse button click counting for double-click and more + int m_left_click_count = 1; + std::chrono::system_clock::time_point m_last_left_down; + //! \brief Browser's view dimension. //! Initial browser's view size. We expose it to Godot which can set the //! desired size depending on its viewport size.