Add scroll support

This commit is contained in:
Leroy Hopson 2024-02-15 18:54:20 +13:00
parent d59a925ca1
commit 3bed9e7b0f
No known key found for this signature in database
GPG key ID: D2747312A6DB51AA
2 changed files with 29 additions and 0 deletions

View file

@ -176,6 +176,7 @@ String Terminal::write(const Variant data)
void Terminal::_gui_input(const Ref<InputEvent> &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<InputEventKey> event) {
accept_event();
}
void Terminal::_handle_mouse_wheel(Ref<InputEventMouseButton> 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<InputEventMouse> event) {
if (!event.is_valid())
return;

View file

@ -9,6 +9,7 @@
#include <godot_cpp/classes/image_texture.hpp>
#include <godot_cpp/classes/input_event_key.hpp>
#include <godot_cpp/classes/input_event_mouse.hpp>
#include <godot_cpp/classes/input_event_mouse_button.hpp>
#include <godot_cpp/classes/rendering_server.hpp>
#include <godot_cpp/classes/shader_material.hpp>
#include <godot_cpp/classes/timer.hpp>
@ -154,6 +155,8 @@ namespace godot
void initialize_input();
void _handle_key_input(Ref<InputEventKey> event);
void _handle_mouse_wheel(Ref<InputEventMouseButton> event);
enum SelectionMode { NONE, POINTER };
bool selecting = false;
SelectionMode selection_mode = SelectionMode::NONE;