From 57aed28a0ecfbbdefea2b1d27f935bf17f257e18 Mon Sep 17 00:00:00 2001 From: Leroy Hopson Date: Fri, 25 Sep 2020 13:12:31 +0700 Subject: [PATCH] Change write method to accept both String and PoolByteArray Will print a warning if neither of these types is used as an argument. --- CHANGELOG.md | 1 + addons/godot_xterm/native/src/terminal.cpp | 30 ++++++++++++++++++++-- addons/godot_xterm/native/src/terminal.h | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f05b14f..c0c0013 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Implementation of Terminal node from GDScript to GDNative using [Aetf's patched version of libtsm](https://github.com/Aetf/libtsm). - Move input handling to the Terminal node itself, rather than handling it in a seperate Control node. +- The Terminal `write()` method now accepts both String and PoolByteArray. diff --git a/addons/godot_xterm/native/src/terminal.cpp b/addons/godot_xterm/native/src/terminal.cpp index 30d75ee..2a101e5 100644 --- a/addons/godot_xterm/native/src/terminal.cpp +++ b/addons/godot_xterm/native/src/terminal.cpp @@ -566,8 +566,34 @@ void Terminal::update_size() update(); } -void Terminal::write(PoolByteArray data) +void Terminal::write(Variant data) { - tsm_vte_input(vte, (char *)data.read().ptr(), data.size()); + + const char *u8; + size_t len; + + switch (data.get_type()) + { + case Variant::Type::POOL_BYTE_ARRAY: + { + PoolByteArray bytes = data; + u8 = (char *)bytes.read().ptr(); + len = bytes.size(); + break; + } + case Variant::Type::STRING: + { + String string = data; + CharString utf8 = string.utf8(); + u8 = utf8.get_data(); + len = utf8.length(); + break; + } + default: + WARN_PRINT("Method expected a String or PoolByteArray"); + return; + } + + tsm_vte_input(vte, u8, len); framebuffer_age = tsm_screen_draw(screen, text_draw_cb, this); } \ 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 ac79ef8..a6dec7e 100644 --- a/addons/godot_xterm/native/src/terminal.h +++ b/addons/godot_xterm/native/src/terminal.h @@ -58,7 +58,7 @@ namespace godot void _gui_input(Variant event); void _draw(); - void write(PoolByteArray bytes); + void write(Variant data); int rows; int cols;