Skip to content

Commit

Permalink
[Complex Text Layouts] Refactor TextEdit and CodeEdit controls.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruvzg committed Nov 26, 2020
1 parent d66eb77 commit 3be31c4
Show file tree
Hide file tree
Showing 7 changed files with 1,271 additions and 763 deletions.
1 change: 1 addition & 0 deletions doc/classes/CodeEdit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<brief_description>
</brief_description>
<description>
[b]Note[/b]: By default [CodeEdit] always use left-to-right text direction to correcly display source code.
</description>
<tutorials>
</tutorials>
Expand Down
101 changes: 86 additions & 15 deletions editor/code_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,20 +731,10 @@ void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {

Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
if (magnify_gesture.is_valid()) {
/*
Ref<DynamicFont> 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;
}

Expand All @@ -767,15 +757,23 @@ void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &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() {
Expand Down Expand Up @@ -884,6 +882,24 @@ Ref<Texture2D> 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");
Expand Down Expand Up @@ -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<String> 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<String> 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"));
Expand Down Expand Up @@ -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<String> 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<String> 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);
Expand Down Expand Up @@ -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));
}
4 changes: 4 additions & 0 deletions editor/code_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class CodeTextEditor : public VBoxContainer {
Timer *idle;
Timer *code_complete_timer;

Timer *font_resize_timer;
int font_resize_val;
real_t font_size;

Expand All @@ -163,11 +164,14 @@ class CodeTextEditor : public VBoxContainer {
void _update_font();
void _complete_request();
Ref<Texture2D> _get_completion_icon(const ScriptCodeCompletionOption &p_option);
void _font_resize_timeout();
bool _add_font_size(int p_delta);

void _input(const Ref<InputEvent> &event);
void _text_editor_gui_input(const Ref<InputEvent> &p_event);
void _zoom_in();
void _zoom_out();
void _zoom_changed();
void _reset_zoom();

Color completion_font_color;
Expand Down
4 changes: 3 additions & 1 deletion editor/plugins/script_text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@ void ScriptTextEditor::update_toggle_scripts_button() {

void ScriptTextEditor::_update_connected_methods() {
CodeEdit *text_edit = code_editor->get_text_editor();
text_edit->set_gutter_width(connection_gutter, text_edit->get_row_height());
for (int i = 0; i < text_edit->get_line_count(); i++) {
if (text_edit->get_line_gutter_metadata(i, connection_gutter) == "") {
continue;
Expand Down Expand Up @@ -1352,7 +1353,8 @@ void ScriptTextEditor::_change_syntax_highlighter(int p_idx) {

void ScriptTextEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_THEME_CHANGED: {
case NOTIFICATION_THEME_CHANGED:
case NOTIFICATION_ENTER_TREE: {
code_editor->get_text_editor()->set_gutter_width(connection_gutter, code_editor->get_text_editor()->get_row_height());
} break;
default:
Expand Down
22 changes: 14 additions & 8 deletions scene/gui/code_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ void CodeEdit::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_THEME_CHANGED:
case NOTIFICATION_ENTER_TREE: {
set_gutter_width(main_gutter, cache.row_height);
set_gutter_width(line_number_gutter, (line_number_digits + 1) * cache.font->get_char_size('0').width);
set_gutter_width(fold_gutter, cache.row_height / 1.2);
set_gutter_width(main_gutter, get_row_height());
set_gutter_width(line_number_gutter, (line_number_digits + 1) * cache.font->get_char_size('0', 0, cache.font_size).width);
set_gutter_width(fold_gutter, get_row_height() / 1.2);

breakpoint_color = get_theme_color("breakpoint_color");
breakpoint_icon = get_theme_icon("breakpoint");
Expand Down Expand Up @@ -234,14 +234,16 @@ bool CodeEdit::is_line_numbers_zero_padded() const {
}

void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
String fc = String::num(p_line + 1).lpad(line_number_digits, line_number_padding);

int yofs = p_region.position.y + (cache.row_height - cache.font->get_height()) / 2;
String fc = TS->format_number(String::num(p_line + 1).lpad(line_number_digits, line_number_padding));
Ref<TextLine> tl;
tl.instance();
tl->add_string(fc, cache.font, cache.font_size);
int yofs = p_region.position.y + (get_row_height() - tl->get_size().y) / 2;
Color number_color = get_line_gutter_item_color(p_line, line_number_gutter);
if (number_color == Color(1, 1, 1)) {
number_color = line_number_color;
}
cache.font->draw_string(get_canvas_item(), Point2(p_region.position.x, yofs + cache.font->get_ascent()), fc, HALIGN_LEFT, -1, cache.font_size, number_color);
tl->draw(get_canvas_item(), Point2(p_region.position.x, yofs), number_color);
}

/* Fold Gutter */
Expand Down Expand Up @@ -368,7 +370,7 @@ void CodeEdit::_lines_edited_from(int p_from_line, int p_to_line) {
while (lc /= 10) {
line_number_digits++;
}
set_gutter_width(line_number_gutter, (line_number_digits + 1) * cache.font->get_char_size('0').width);
set_gutter_width(line_number_gutter, (line_number_digits + 1) * cache.font->get_char_size('0', 0, cache.font_size).width);

int from_line = MIN(p_from_line, p_to_line);
int line_count = (p_to_line - p_from_line);
Expand Down Expand Up @@ -410,6 +412,10 @@ void CodeEdit::_update_gutter_indexes() {
}

CodeEdit::CodeEdit() {
/* Text Direction */
set_layout_direction(LAYOUT_DIRECTION_LTR);
set_text_direction(TEXT_DIRECTION_LTR);

/* Gutters */
int gutter_idx = 0;

Expand Down
Loading

0 comments on commit 3be31c4

Please sign in to comment.