From fae28006cfb47bdaf4272ff8cc2c30cacb469c7c Mon Sep 17 00:00:00 2001 From: Leroy Hopson Date: Thu, 8 Jul 2021 20:10:34 +0700 Subject: [PATCH] Move scroll handling to GDScript Just expose the underlying libtsm methods from gdnative, but write all the logic in gdscript. Makes it much easier to customize and update. --- addons/godot_xterm/native/src/terminal.cpp | 48 +++++-------------- addons/godot_xterm/native/src/terminal.h | 3 ++ addons/godot_xterm/nodes/terminal/terminal.gd | 28 +++++++++++ 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/addons/godot_xterm/native/src/terminal.cpp b/addons/godot_xterm/native/src/terminal.cpp index 32fbc82..44486f3 100644 --- a/addons/godot_xterm/native/src/terminal.cpp +++ b/addons/godot_xterm/native/src/terminal.cpp @@ -1,11 +1,5 @@ // Copyright (c) 2021, Leroy Hopson (MIT License). -// This file contains snippets of code taken from Godot's TextEdit node. -// The snippet code is copyright of its authors and released under the MIT -// license: -// - Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur (MIT License). -// - Copyright (c) 2014-2021 Godot Engine contributors (MIT License). - #include "terminal.h" #include #include @@ -289,6 +283,9 @@ void Terminal::_register_methods() { register_method("write", &Terminal::write); + register_method("sb_up", &Terminal::sb_up); + register_method("sb_down", &Terminal::sb_down); + register_method("_update_theme", &Terminal::update_theme); register_method("_update_size", &Terminal::update_theme); @@ -366,35 +363,6 @@ void Terminal::_gui_input(Variant event) { tsm_vte_handle_keyboard(vte, keysym, ascii, mods, unicode ? unicode : TSM_VTE_INVALID); } - - Ref mb = event; - - if (mb.is_valid()) { - Vector2 mpos = mb->get_position(); - - if (mb->is_pressed()) { - if (mb->get_button_index() == GLOBAL_CONSTANT(BUTTON_WHEEL_UP)) { - if (mb->get_alt()) { - // Scroll 5 times as fast as normal (like in TextEdit). - tsm_screen_sb_up(screen, 15 * mb->get_factor()); - } else { - // Scroll 3 lines. - tsm_screen_sb_up(screen, 3 * mb->get_factor()); - } - update(); - } - if (mb->get_button_index() == GLOBAL_CONSTANT(BUTTON_WHEEL_DOWN)) { - if (mb->get_alt()) { - // Scroll 5 times as fast as normal (like in TextEdit). - tsm_screen_sb_down(screen, 15 * mb->get_factor()); - } else { - // Scroll 3 lines. - tsm_screen_sb_down(screen, 3 * mb->get_factor()); - } - update(); - } - } - } } void Terminal::_draw() { @@ -600,3 +568,13 @@ void Terminal::write(String data) { tsm_vte_input(vte, u8, len); } + +void Terminal::sb_up(int num) { + tsm_screen_sb_up(screen, num); + update(); +} + +void Terminal::sb_down(int num) { + tsm_screen_sb_down(screen, num); + update(); +} \ No newline at end of file diff --git a/addons/godot_xterm/native/src/terminal.h b/addons/godot_xterm/native/src/terminal.h index 3df9c35..c4dbb9e 100644 --- a/addons/godot_xterm/native/src/terminal.h +++ b/addons/godot_xterm/native/src/terminal.h @@ -56,6 +56,9 @@ public: void write(String data); + void sb_up(int num); + void sb_down(int num); + enum UpdateMode { DISABLED, AUTO, diff --git a/addons/godot_xterm/nodes/terminal/terminal.gd b/addons/godot_xterm/nodes/terminal/terminal.gd index 2b69edf..90d42ae 100644 --- a/addons/godot_xterm/nodes/terminal/terminal.gd +++ b/addons/godot_xterm/nodes/terminal/terminal.gd @@ -1,3 +1,9 @@ +# Copyright (c) 2021, Leroy Hopson (MIT License). +# +# This file contains snippets of code derived from Godot's TextEdit node. +# These snippets are copyright of their authors and released under the MIT license: +# - Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur (MIT License). +# - Copyright (c) 2014-2021 Godot Engine contributors (MIT License). tool extends Control @@ -87,6 +93,28 @@ func _refresh(): func _gui_input(event): _native_terminal._gui_input(event) + _handle_mouse_wheel(event) + + +func _handle_mouse_wheel(event: InputEventMouseButton): + if not event or not event.is_pressed(): + return + + if event.button_index == BUTTON_WHEEL_UP: + if event.alt: + # Scroll 5 times as fast as normal (like TextEdit). + _native_terminal.sb_up(15 * event.factor) + else: + # Scroll 3 lines. + _native_terminal.sb_up(3 * event.factor) + + if event.button_index == BUTTON_WHEEL_DOWN: + if event.alt: + # Scroll 5 times as fast as normal (like TextEdit). + _native_terminal.sb_down(15 * event.factor) + else: + # Scroll 3 lines. + _native_terminal.sb_down(3 * event.factor) func _notification(what: int) -> void: