Skip to content

Commit

Permalink
Passing modifiers to mouse events for drag handling (#47)
Browse files Browse the repository at this point in the history
+ Add click_count for double-click handling
  • Loading branch information
eliopark-nsuslab committed Mar 15, 2024
1 parent fd2c216 commit cfd3e9b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
37 changes: 36 additions & 1 deletion addons/gdcef/gdcef/src/browser_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "gdbrowser.hpp"
#include "helper_files.hpp"
#include <godot_cpp/core/math.hpp>

//------------------------------------------------------------------------------
void GDBrowserView::leftClick()
Expand Down Expand Up @@ -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<milliseconds>(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);
}

//------------------------------------------------------------------------------
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions addons/gdcef/gdcef/src/gdbrowser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@

# include <iostream>
# include <array>
# include <chrono>

// ****************************************************************************
//! \brief Class wrapping the CefBrowser class and export methods for Godot
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit cfd3e9b

Please sign in to comment.