feat(term): add select method

Adds select() method to Terminal. Method behaves the same way as
TextEdit's select method.
This commit is contained in:
Leroy Hopson 2024-04-26 13:58:49 +12:00 committed by Leroy Hopson
parent 43303a51bf
commit dd118d72f3
5 changed files with 112 additions and 8 deletions

View file

@ -3,6 +3,7 @@
#include "terminal.h"
#include <algorithm>
#include <godot_cpp/classes/control.hpp>
#include <godot_cpp/classes/font.hpp>
#include <godot_cpp/classes/image_texture.hpp>
@ -70,6 +71,9 @@ void Terminal::_bind_methods()
ClassDB::bind_method(D_METHOD("set_blink_off_time", "time"), &Terminal::set_blink_off_time);
ClassDB::add_property("Terminal", PropertyInfo(Variant::FLOAT, "blink_off_time"), "set_blink_off_time", "get_blink_off_time");
// Selection.
ClassDB::bind_method(D_METHOD("select", "from_line", "from_column", "to_line", "to_column"), &Terminal::select);
// Copying.
ClassDB::bind_method(D_METHOD("copy_all"), &Terminal::copy_all);
ClassDB::bind_method(D_METHOD("copy_selection"), &Terminal::copy_selection);
@ -657,6 +661,26 @@ String Terminal::_copy_screen(ScreenCopyFunction func) {
return data.get_string_from_utf8();
}
void Terminal::select(const int p_from_line, const int p_from_column, const int p_to_line, const int p_to_column) {
int from_line = std::clamp((int)p_from_line, 0, (int)rows);
int from_column = std::clamp((int)p_from_column, 0, (int)cols);
int to_line = std::clamp((int)p_to_line, 0, (int)rows);
int to_column = std::clamp((int)p_to_column, 0, (int)cols);
if (from_line > to_line) {
std::swap(to_line, from_line);
std::swap(to_column, from_column);
} else if ((from_line == to_line) && (from_column > to_column)) {
std::swap(to_column, from_column);
}
to_column -= 1;
tsm_screen_selection_reset(screen);
tsm_screen_selection_start(screen, from_column, from_line);
tsm_screen_selection_target(screen, to_column, to_line);
}
String Terminal::copy_all() {
return _copy_screen(&tsm_screen_copy_all);
}

View file

@ -74,6 +74,8 @@ namespace godot
void clear();
void select(const int p_from_line, const int p_from_column, const int p_to_line, const int p_to_column);
String copy_all();
String copy_selection();
void set_copy_on_selection(const bool p_enable);