From 99666de00fb30cb86473257776504ca70b4469c3 Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Thu, 3 Sep 2020 14:22:16 +0300 Subject: [Complex Text Layouts] Refactor Font class, default themes and controls to use Text Server interface. Implement interface mirroring. Add TextLine and TextParagraph classes. Handle UTF-16 input on macOS and Windows. --- editor/code_editor.cpp | 68 +++++++++++++++++--------------------------------- 1 file changed, 23 insertions(+), 45 deletions(-) (limited to 'editor/code_editor.cpp') diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 3182bca0eb..c4a8b310ec 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -38,7 +38,7 @@ #include "editor_settings.h" #include "scene/gui/margin_container.h" #include "scene/gui/separator.h" -#include "scene/resources/dynamic_font.h" +#include "scene/resources/font.h" void GotoLineDialog::popup_find_line(CodeEdit *p_edit) { text_editor = p_edit; @@ -731,6 +731,7 @@ void CodeTextEditor::_text_editor_gui_input(const Ref &p_event) { Ref magnify_gesture = p_event; if (magnify_gesture.is_valid()) { + /* Ref font = text_editor->get_theme_font("font"); if (font.is_valid()) { @@ -742,6 +743,8 @@ void CodeTextEditor::_text_editor_gui_input(const Ref &p_event) { _add_font_size((int)font_size - font->get_size()); } + */ + //TODO move size to draw functions return; } @@ -764,27 +767,15 @@ void CodeTextEditor::_text_editor_gui_input(const Ref &p_event) { void CodeTextEditor::_zoom_in() { font_resize_val += MAX(EDSCALE, 1.0f); - _zoom_changed(); } void CodeTextEditor::_zoom_out() { font_resize_val -= MAX(EDSCALE, 1.0f); - _zoom_changed(); -} - -void CodeTextEditor::_zoom_changed() { - if (font_resize_timer->get_time_left() == 0) { - font_resize_timer->start(); - } } void CodeTextEditor::_reset_zoom() { - Ref font = text_editor->get_theme_font("font"); // Reset source font size to default. - - if (font.is_valid()) { - EditorSettings::get_singleton()->set("interface/editor/code_font_size", 14); - font->set_size(14); - } + font_resize_val = 1.0f; + //TODO MOVE size to draw functions } void CodeTextEditor::_line_col_changed() { @@ -893,29 +884,6 @@ Ref CodeTextEditor::_get_completion_icon(const ScriptCodeCompletionOp return tex; } -void CodeTextEditor::_font_resize_timeout() { - if (_add_font_size(font_resize_val)) { - font_resize_val = 0; - } -} - -bool CodeTextEditor::_add_font_size(int p_delta) { - Ref font = text_editor->get_theme_font("font"); - - if (font.is_valid()) { - int new_size = CLAMP(font->get_size() + p_delta, 8 * EDSCALE, 96 * EDSCALE); - - if (new_size != font->get_size()) { - EditorSettings::get_singleton()->set("interface/editor/code_font_size", new_size / EDSCALE); - font->set_size(new_size); - } - - return true; - } else { - return false; - } -} - void CodeTextEditor::update_editor_settings() { completion_font_color = EDITOR_GET("text_editor/highlighting/completion_font_color"); completion_string_color = EDITOR_GET("text_editor/highlighting/string_color"); @@ -1486,17 +1454,22 @@ void CodeTextEditor::goto_error() { void CodeTextEditor::_update_font() { text_editor->add_theme_font_override("font", get_theme_font("source", "EditorFonts")); + text_editor->add_theme_font_size_override("font_size", get_theme_font_size("source_size", "EditorFonts")); error->add_theme_font_override("font", get_theme_font("status_source", "EditorFonts")); + error->add_theme_font_size_override("font_size", get_theme_font_size("status_source_size", "EditorFonts")); error->add_theme_color_override("font_color", get_theme_color("error_color", "Editor")); Ref status_bar_font = get_theme_font("status_source", "EditorFonts"); + int status_bar_font_size = get_theme_font_size("status_source_size", "EditorFonts"); error->add_theme_font_override("font", status_bar_font); + error->add_theme_font_size_override("font_size", status_bar_font_size); int count = status_bar->get_child_count(); for (int i = 0; i < count; i++) { Control *n = Object::cast_to(status_bar->get_child(i)); if (n) { n->add_theme_font_override("font", status_bar_font); + n->add_theme_font_size_override("font_size", status_bar_font_size); } } } @@ -1547,7 +1520,11 @@ void CodeTextEditor::_set_show_warnings_panel(bool p_show) { } void CodeTextEditor::_toggle_scripts_pressed() { - toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->toggle_scripts_panel() ? get_theme_icon("Back", "EditorIcons") : get_theme_icon("Forward", "EditorIcons")); + if (is_layout_rtl()) { + toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->toggle_scripts_panel() ? get_theme_icon("Back", "EditorIcons") : get_theme_icon("Forward", "EditorIcons")); + } else { + toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->toggle_scripts_panel() ? get_theme_icon("Forward", "EditorIcons") : get_theme_icon("Back", "EditorIcons")); + } } void CodeTextEditor::_error_pressed(const Ref &p_event) { @@ -1668,7 +1645,11 @@ void CodeTextEditor::show_toggle_scripts_button() { } void CodeTextEditor::update_toggle_scripts_button() { - toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? get_theme_icon("Back", "EditorIcons") : get_theme_icon("Forward", "EditorIcons")); + if (is_layout_rtl()) { + toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? get_theme_icon("Back", "EditorIcons") : get_theme_icon("Forward", "EditorIcons")); + } else { + toggle_scripts_button->set_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? get_theme_icon("Forward", "EditorIcons") : get_theme_icon("Back", "EditorIcons")); + } toggle_scripts_button->set_tooltip(TTR("Toggle Scripts Panel") + " (" + ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text() + ")"); } @@ -1750,6 +1731,7 @@ CodeTextEditor::CodeTextEditor() { warning_count_label->set_tooltip(TTR("Warnings")); warning_count_label->add_theme_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_theme_color("warning_color", "Editor")); warning_count_label->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts")); + warning_count_label->add_theme_font_size_override("font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("status_source_size", "EditorFonts")); warning_count_label->connect("gui_input", callable_mp(this, &CodeTextEditor::_warning_label_gui_input)); is_warnings_panel_opened = false; @@ -1760,6 +1742,7 @@ CodeTextEditor::CodeTextEditor() { status_bar->add_child(line_and_col_txt); line_and_col_txt->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER); line_and_col_txt->add_theme_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("status_source", "EditorFonts")); + line_and_col_txt->add_theme_font_size_override("font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size("status_source_size", "EditorFonts")); line_and_col_txt->set_tooltip(TTR("Line and column numbers.")); line_and_col_txt->set_mouse_filter(MOUSE_FILTER_STOP); @@ -1781,11 +1764,6 @@ CodeTextEditor::CodeTextEditor() { font_resize_val = 0; font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size"); - font_resize_timer = memnew(Timer); - add_child(font_resize_timer); - font_resize_timer->set_one_shot(true); - font_resize_timer->set_wait_time(0.07); - font_resize_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_font_resize_timeout)); EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &CodeTextEditor::_on_settings_change)); } -- cgit v1.2.3 From 3be31c496089b413c87ca1411726dddc5dd0e7d6 Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Fri, 18 Sep 2020 10:36:10 +0300 Subject: [Complex Text Layouts] Refactor TextEdit and CodeEdit controls. --- editor/code_editor.cpp | 101 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 86 insertions(+), 15 deletions(-) (limited to 'editor/code_editor.cpp') diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index c4a8b310ec..8f5b5612fc 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -731,20 +731,10 @@ void CodeTextEditor::_text_editor_gui_input(const Ref &p_event) { Ref magnify_gesture = p_event; if (magnify_gesture.is_valid()) { - /* - Ref font = text_editor->get_theme_font("font"); + font_size = text_editor->get_theme_font_size("font_size"); + font_size *= powf(magnify_gesture->get_factor(), 0.25); - if (font.is_valid()) { - if (font->get_size() != (int)font_size) { - font_size = font->get_size(); - } - - font_size *= powf(magnify_gesture->get_factor(), 0.25); - - _add_font_size((int)font_size - font->get_size()); - } - */ - //TODO move size to draw functions + _add_font_size((int)font_size - text_editor->get_theme_font_size("font_size")); return; } @@ -767,15 +757,23 @@ void CodeTextEditor::_text_editor_gui_input(const Ref &p_event) { void CodeTextEditor::_zoom_in() { font_resize_val += MAX(EDSCALE, 1.0f); + _zoom_changed(); } void CodeTextEditor::_zoom_out() { font_resize_val -= MAX(EDSCALE, 1.0f); + _zoom_changed(); +} + +void CodeTextEditor::_zoom_changed() { + if (font_resize_timer->get_time_left() == 0) { + font_resize_timer->start(); + } } void CodeTextEditor::_reset_zoom() { - font_resize_val = 1.0f; - //TODO MOVE size to draw functions + EditorSettings::get_singleton()->set("interface/editor/code_font_size", 14); + text_editor->add_theme_font_size_override("font_size", 14 * EDSCALE); } void CodeTextEditor::_line_col_changed() { @@ -884,6 +882,24 @@ Ref CodeTextEditor::_get_completion_icon(const ScriptCodeCompletionOp return tex; } +void CodeTextEditor::_font_resize_timeout() { + if (_add_font_size(font_resize_val)) { + font_resize_val = 0; + } +} + +bool CodeTextEditor::_add_font_size(int p_delta) { + int old_size = text_editor->get_theme_font_size("font_size"); + int new_size = CLAMP(old_size + p_delta, 8 * EDSCALE, 96 * EDSCALE); + + if (new_size != old_size) { + EditorSettings::get_singleton()->set("interface/editor/code_font_size", new_size / EDSCALE); + text_editor->add_theme_font_size_override("font_size", new_size); + } + + return true; +} + void CodeTextEditor::update_editor_settings() { completion_font_color = EDITOR_GET("text_editor/highlighting/completion_font_color"); completion_string_color = EDITOR_GET("text_editor/highlighting/string_color"); @@ -1479,6 +1495,31 @@ void CodeTextEditor::_on_settings_change() { font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size"); + int ot_mode = EditorSettings::get_singleton()->get("interface/editor/code_font_contextual_ligatures"); + switch (ot_mode) { + case 1: { // Disable ligatures. + text_editor->clear_opentype_features(); + text_editor->set_opentype_feature("calt", 0); + } break; + case 2: { // Custom. + text_editor->clear_opentype_features(); + Vector subtag = String(EditorSettings::get_singleton()->get("interface/editor/code_font_custom_opentype_features")).split(","); + Dictionary ftrs; + for (int i = 0; i < subtag.size(); i++) { + Vector subtag_a = subtag[i].split("="); + if (subtag_a.size() == 2) { + text_editor->set_opentype_feature(subtag_a[0], subtag_a[1].to_int()); + } else if (subtag_a.size() == 1) { + text_editor->set_opentype_feature(subtag_a[0], 1); + } + } + } break; + default: { // Default. + text_editor->clear_opentype_features(); + text_editor->set_opentype_feature("calt", 1); + } break; + } + // Auto brace completion. text_editor->set_auto_brace_completion( EDITOR_GET("text_editor/completion/auto_brace_complete")); @@ -1663,6 +1704,31 @@ CodeTextEditor::CodeTextEditor() { add_child(text_editor); text_editor->set_v_size_flags(SIZE_EXPAND_FILL); + int ot_mode = EditorSettings::get_singleton()->get("interface/editor/code_font_contextual_ligatures"); + switch (ot_mode) { + case 1: { // Disable ligatures. + text_editor->clear_opentype_features(); + text_editor->set_opentype_feature("calt", 0); + } break; + case 2: { // Custom. + text_editor->clear_opentype_features(); + Vector subtag = String(EditorSettings::get_singleton()->get("interface/editor/code_font_custom_opentype_features")).split(","); + Dictionary ftrs; + for (int i = 0; i < subtag.size(); i++) { + Vector subtag_a = subtag[i].split("="); + if (subtag_a.size() == 2) { + text_editor->set_opentype_feature(subtag_a[0], subtag_a[1].to_int()); + } else if (subtag_a.size() == 1) { + text_editor->set_opentype_feature(subtag_a[0], 1); + } + } + } break; + default: { // Default. + text_editor->clear_opentype_features(); + text_editor->set_opentype_feature("calt", 1); + } break; + } + // Added second so it opens at the bottom, so it won't shift the entire text editor when opening. find_replace_bar = memnew(FindReplaceBar); add_child(find_replace_bar); @@ -1764,6 +1830,11 @@ CodeTextEditor::CodeTextEditor() { font_resize_val = 0; font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size"); + font_resize_timer = memnew(Timer); + add_child(font_resize_timer); + font_resize_timer->set_one_shot(true); + font_resize_timer->set_wait_time(0.07); + font_resize_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_font_resize_timeout)); EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &CodeTextEditor::_on_settings_change)); } -- cgit v1.2.3