From 3bed9e7b0f39d6ccecfea19dbc932a9ad030e50e Mon Sep 17 00:00:00 2001 From: Leroy Hopson Date: Thu, 15 Feb 2024 18:54:20 +1300 Subject: [PATCH] Add scroll support --- addons/godot_xterm/native/src/terminal.cpp | 26 ++++++++++++++++++++++ addons/godot_xterm/native/src/terminal.h | 3 +++ 2 files changed, 29 insertions(+) diff --git a/addons/godot_xterm/native/src/terminal.cpp b/addons/godot_xterm/native/src/terminal.cpp index 71597b5..b96c248 100644 --- a/addons/godot_xterm/native/src/terminal.cpp +++ b/addons/godot_xterm/native/src/terminal.cpp @@ -176,6 +176,7 @@ String Terminal::write(const Variant data) void Terminal::_gui_input(const Ref &event) { _handle_key_input(event); _handle_selection(event); + _handle_mouse_wheel(event); } void Terminal::_notification(int what) @@ -658,6 +659,31 @@ void Terminal::_handle_key_input(Ref event) { accept_event(); } +void Terminal::_handle_mouse_wheel(Ref event) { + if (!event.is_valid() || !event->is_pressed()) + return; + + void (*scroll_func)(tsm_screen *, unsigned int) = nullptr; + + switch (event->get_button_index()) { + case MOUSE_BUTTON_WHEEL_UP: + scroll_func = &tsm_screen_sb_up; + break; + case MOUSE_BUTTON_WHEEL_DOWN: + scroll_func = &tsm_screen_sb_down; + break; + }; + + if (scroll_func != nullptr) { + // Scroll 5 times as fast as normal if alt is pressed (like TextEdit). + // Otherwise, just scroll 3 lines. + int speed = event->is_alt_pressed() ? 15 : 3; + double factor = event->get_factor(); + (*scroll_func)(screen, speed * factor); + queue_redraw(); + } +} + void Terminal::_handle_selection(Ref event) { if (!event.is_valid()) return; diff --git a/addons/godot_xterm/native/src/terminal.h b/addons/godot_xterm/native/src/terminal.h index 2b2a73e..85f8160 100644 --- a/addons/godot_xterm/native/src/terminal.h +++ b/addons/godot_xterm/native/src/terminal.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -154,6 +155,8 @@ namespace godot void initialize_input(); void _handle_key_input(Ref event); + void _handle_mouse_wheel(Ref event); + enum SelectionMode { NONE, POINTER }; bool selecting = false; SelectionMode selection_mode = SelectionMode::NONE;