diff --git a/CHANGELOG.md b/CHANGELOG.md index 02d4667..fa16c9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed +- Set a default theme if no theme property has been set. + ### Fixed - Don't swap red and blue channels of theme colors. - Use "Light Cyan" color from theme. Previously ignored. diff --git a/addons/godot_xterm/native/src/terminal.cpp b/addons/godot_xterm/native/src/terminal.cpp index 3f08529..a96c419 100644 --- a/addons/godot_xterm/native/src/terminal.cpp +++ b/addons/godot_xterm/native/src/terminal.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -251,6 +252,7 @@ void Terminal::_register_methods() register_method("_ready", &Terminal::_ready); register_method("_gui_input", &Terminal::_gui_input); register_method("_draw", &Terminal::_draw); + register_method("_notification", &Terminal::_notification); register_method("write", &Terminal::write); register_method("update_size", &Terminal::update_size); @@ -286,15 +288,7 @@ void Terminal::_init() ERR_PRINT("Error creating new tsm vte"); } - update_color_palette(); - if (tsm_vte_set_custom_palette(vte, color_palette)) - { - ERR_PRINT("Error setting custom palette"); - } - if (tsm_vte_set_palette(vte, "custom")) - { - ERR_PRINT("Error setting palette"); - } + update_theme(); } void Terminal::_ready() @@ -310,6 +304,9 @@ void Terminal::_notification(int what) case NOTIFICATION_RESIZED: update_size(); break; + case NOTIFICATION_THEME_CHANGED: + update_theme(); + break; } } @@ -350,7 +347,9 @@ void Terminal::_draw() return; /* Draw the full terminal rect background */ - draw_rect(Rect2(Vector2(0, 0), get_rect().size), get_color("Background", "Terminal")); + Color background_color = palette[TSM_COLOR_BACKGROUND]; + + draw_rect(Rect2(Vector2(0, 0), get_rect().size), background_color); for (int row = 0; row < rows; row++) { @@ -364,39 +363,80 @@ void Terminal::_draw() } } -void Terminal::update_color_palette() +void Terminal::update_theme() { /* Generate color palette based on theme */ // Converts a color from the Control's theme to one that can // be used in a tsm color palette. - auto set_pallete_color = [this](tsm_vte_color color, String theme_color) -> void { - Color c = get_color(theme_color, "Terminal"); + auto set_pallete_color = [this](tsm_vte_color color, String theme_color, int default_r, int default_g, int default_b) -> void { + Color c; + + if (has_color(theme_color, "Terminal")) { + c = get_color(theme_color, "Terminal"); + } else { + int r = default_r; + int g = default_g; + int b = default_b; + c = Color((float)r / 255.0, (float)g / 255.0, (float)b / 255.0); + } color_palette[color][0] = c.get_r8(); color_palette[color][1] = c.get_g8(); color_palette[color][2] = c.get_b8(); + + palette.erase(color) = c; }; - set_pallete_color(TSM_COLOR_BLACK, "Black"); - set_pallete_color(TSM_COLOR_RED, "Red"); - set_pallete_color(TSM_COLOR_GREEN, "Green"); - set_pallete_color(TSM_COLOR_YELLOW, "Yellow"); - set_pallete_color(TSM_COLOR_BLUE, "Blue"); - set_pallete_color(TSM_COLOR_MAGENTA, "Magenta"); - set_pallete_color(TSM_COLOR_CYAN, "Cyan"); - set_pallete_color(TSM_COLOR_LIGHT_GREY, "Light Grey"); - set_pallete_color(TSM_COLOR_DARK_GREY, "Dark Grey"); - set_pallete_color(TSM_COLOR_LIGHT_RED, "Light Red"); - set_pallete_color(TSM_COLOR_LIGHT_GREEN, "Light Green"); - set_pallete_color(TSM_COLOR_LIGHT_YELLOW, "Light Yellow"); - set_pallete_color(TSM_COLOR_LIGHT_BLUE, "Light Blue"); - set_pallete_color(TSM_COLOR_LIGHT_MAGENTA, "Light Magenta"); - set_pallete_color(TSM_COLOR_LIGHT_CYAN, "Light Cyan"); - set_pallete_color(TSM_COLOR_WHITE, "White"); + set_pallete_color(TSM_COLOR_BLACK, "Black", 0, 0, 0); + set_pallete_color(TSM_COLOR_RED, "Red", 205, 0, 0); + set_pallete_color(TSM_COLOR_GREEN, "Green", 0, 205, 0); + set_pallete_color(TSM_COLOR_YELLOW, "Yellow", 205, 205, 0); + set_pallete_color(TSM_COLOR_BLUE, "Blue", 0, 0, 238); + set_pallete_color(TSM_COLOR_MAGENTA, "Magenta", 205, 0, 205); + set_pallete_color(TSM_COLOR_CYAN, "Cyan", 0, 205, 205); + set_pallete_color(TSM_COLOR_LIGHT_GREY, "Light Grey", 229, 229, 229); + set_pallete_color(TSM_COLOR_DARK_GREY, "Dark Grey", 127, 127, 127); + set_pallete_color(TSM_COLOR_LIGHT_RED, "Light Red", 255, 0, 0); + set_pallete_color(TSM_COLOR_LIGHT_GREEN, "Light Green", 0, 255, 0); + set_pallete_color(TSM_COLOR_LIGHT_YELLOW, "Light Yellow", 255, 255, 0); + set_pallete_color(TSM_COLOR_LIGHT_BLUE, "Light Blue", 0, 0, 255); + set_pallete_color(TSM_COLOR_LIGHT_MAGENTA, "Light Magenta", 255, 0, 255); + set_pallete_color(TSM_COLOR_LIGHT_CYAN, "Light Cyan", 0, 255, 255); + set_pallete_color(TSM_COLOR_WHITE, "White", 255, 255, 255); - set_pallete_color(TSM_COLOR_BACKGROUND, "Background"); - set_pallete_color(TSM_COLOR_FOREGROUND, "Foreground"); + set_pallete_color(TSM_COLOR_BACKGROUND, "Background", 255, 255, 255); + set_pallete_color(TSM_COLOR_FOREGROUND, "Foreground", 0, 0, 0); + + if (tsm_vte_set_custom_palette(vte, color_palette)) + { + ERR_PRINT("Error setting custom palette"); + } + if (tsm_vte_set_palette(vte, "custom")) + { + ERR_PRINT("Error setting palette"); + } + + + /* Load fonts into the fontmap from theme */ + + auto set_font = [this](String font_style, String default_font_path) -> void { + Ref fontref; + ResourceLoader* rl = ResourceLoader::get_singleton(); + + if (has_font(font_style, "Terminal")) { + fontref = get_font(font_style, "Terminal"); + } else { + fontref = rl->load(default_font_path); + } + + fontmap.insert(std::pair>(font_style, fontref)); + }; + + set_font("Bold Italic", "res://addons/godot_xterm/themes/fonts/cousine/cousine_bold_italic.tres"); + set_font("Bold", "res://addons/godot_xterm/themes/fonts/cousine/cousine_bold.tres"); + set_font("Italic", "res://addons/godot_xterm/themes/fonts/cousine/cousine_italic.tres"); + set_font("Regular", "res://addons/godot_xterm/themes/fonts/cousine/cousine_regular.tres"); } void Terminal::draw_background(int row, int col, Color bgcolor) @@ -422,19 +462,19 @@ void Terminal::draw_foreground(int row, int col, Color fgcolor) if (cell.attr.bold && cell.attr.italic) { - fontref = get_font("Bold Italic", "Terminal"); + fontref = fontmap["Bold Italic"]; } else if (cell.attr.bold) { - fontref = get_font("Bold", "Terminal"); + fontref = fontmap["Bold"]; } else if (cell.attr.italic) { - fontref = get_font("Italic", "Terminal"); + fontref = fontmap["Italic"]; } else { - fontref = get_font("Regular", "Terminal"); + fontref = fontmap["Regular"]; } /* Draw the foreground */ @@ -454,8 +494,7 @@ std::pair Terminal::get_cell_colors(int row, int col) { struct cell cell = cells[row][col]; Color fgcol, bgcol; - float fr = 1, fg = 1, fb = 1, br = 0, bg = 0, bb = 0; - Ref fontref = get_font(""); + float fr = 0, fg = 0, fb = 0, br = 1, bg = 1, bb = 1; /* Get foreground color */ @@ -506,7 +545,8 @@ void Terminal::update_size() { sleep = true; - cell_size = get_font("Regular", "Terminal").ptr()->get_string_size("W"); + Ref fontref = fontmap.count("Regular") ? fontmap["Regular"] : has_font("Regular", "Terminal") ? get_font("Regular", "Terminal") : get_font(""); + cell_size = fontref->get_string_size("W"); rows = std::max(2, (int)floor(get_rect().size.y / cell_size.y)); cols = std::max(1, (int)floor(get_rect().size.x / cell_size.x)); diff --git a/addons/godot_xterm/native/src/terminal.h b/addons/godot_xterm/native/src/terminal.h index da83137..a7764f1 100644 --- a/addons/godot_xterm/native/src/terminal.h +++ b/addons/godot_xterm/native/src/terminal.h @@ -41,10 +41,11 @@ namespace godot Vector2 cell_size; std::map palette = {}; + std::map> fontmap = {}; void update_size(); - void update_color_palette(); + void update_theme(); std::pair get_cell_colors(int row, int col); void draw_background(int row, int col, Color bgcol); void draw_foreground(int row, int col, Color fgcol); @@ -74,4 +75,4 @@ namespace godot }; } // namespace godot -#endif // TERMINAL_H \ No newline at end of file +#endif // TERMINAL_H diff --git a/addons/godot_xterm/nodes/terminal/README.md b/addons/godot_xterm/nodes/terminal/README.md index 28808dc..4ad9acb 100644 --- a/addons/godot_xterm/nodes/terminal/README.md +++ b/addons/godot_xterm/nodes/terminal/README.md @@ -9,8 +9,8 @@ Terminal emulator. -- If you are not seeing anything in the terminal check that a theme has been set. If there is no theme, everything will be drawn in black by default. A default theme can be found in the [themes directory](../../themes). - If the terminal isn't responding to keyboard or mouse input check that `focus_mode` is set to `All`, otherwise `_gui_input()` won't be called so no input will be processed. +- If you want to customize the colors and font of the terminal, be sure to set the theme property. A default theme that can be found in the [themes directory](../../themes) and can be used as a template for creating a custom theme. ## Description diff --git a/addons/godot_xterm/themes/fonts/cousine/cousine_bold.tres b/addons/godot_xterm/themes/fonts/cousine/cousine_bold.tres new file mode 100644 index 0000000..438559a --- /dev/null +++ b/addons/godot_xterm/themes/fonts/cousine/cousine_bold.tres @@ -0,0 +1,6 @@ +[gd_resource type="DynamicFont" load_steps=2 format=2] + +[ext_resource path="res://addons/godot_xterm/themes/fonts/cousine/Cousine-Bold.ttf" type="DynamicFontData" id=1] + +[resource] +font_data = ExtResource( 1 ) diff --git a/addons/godot_xterm/themes/fonts/cousine/cousine_bold_italic.tres b/addons/godot_xterm/themes/fonts/cousine/cousine_bold_italic.tres new file mode 100644 index 0000000..2a29ff1 --- /dev/null +++ b/addons/godot_xterm/themes/fonts/cousine/cousine_bold_italic.tres @@ -0,0 +1,6 @@ +[gd_resource type="DynamicFont" load_steps=2 format=2] + +[ext_resource path="res://addons/godot_xterm/themes/fonts/cousine/Cousine-BoldItalic.ttf" type="DynamicFontData" id=1] + +[resource] +font_data = ExtResource( 1 ) diff --git a/addons/godot_xterm/themes/fonts/cousine/cousine_italic.tres b/addons/godot_xterm/themes/fonts/cousine/cousine_italic.tres new file mode 100644 index 0000000..b05f614 --- /dev/null +++ b/addons/godot_xterm/themes/fonts/cousine/cousine_italic.tres @@ -0,0 +1,6 @@ +[gd_resource type="DynamicFont" load_steps=2 format=2] + +[ext_resource path="res://addons/godot_xterm/themes/fonts/cousine/Cousine-Italic.ttf" type="DynamicFontData" id=1] + +[resource] +font_data = ExtResource( 1 ) diff --git a/addons/godot_xterm/themes/fonts/cousine/cousine_regular.tres b/addons/godot_xterm/themes/fonts/cousine/cousine_regular.tres new file mode 100644 index 0000000..421b4db --- /dev/null +++ b/addons/godot_xterm/themes/fonts/cousine/cousine_regular.tres @@ -0,0 +1,6 @@ +[gd_resource type="DynamicFont" load_steps=2 format=2] + +[ext_resource path="res://addons/godot_xterm/themes/fonts/cousine/Cousine-Regular.ttf" type="DynamicFontData" id=1] + +[resource] +font_data = ExtResource( 1 )