Skip to content

Commit

Permalink
Fix most of TextEdit visual issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
WhalesState committed Sep 28, 2024
1 parent e4e024a commit 950add1
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 162 deletions.
1 change: 0 additions & 1 deletion doc/classes/TextEdit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,6 @@
<member name="caret_type" type="int" setter="set_caret_type" getter="get_caret_type" enum="TextEdit.CaretType" default="0">
Set the type of caret to draw.
</member>
<member name="clip_contents" type="bool" setter="set_clip_contents" getter="is_clipping_contents" overrides="Control" default="true" />
<member name="context_menu_enabled" type="bool" setter="set_context_menu_enabled" getter="is_context_menu_enabled" default="true">
If [code]true[/code], a right-click displays the context menu.
</member>
Expand Down
43 changes: 23 additions & 20 deletions scene/gui/code_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ void CodeEdit::_notification(int p_what) {
} break;

case NOTIFICATION_DRAW: {
RID ci = get_canvas_item();
RID ci = _get_text_canvas_item();
const Size2 size = get_size();
const bool caret_visible = is_caret_visible();
const bool rtl = is_layout_rtl();
const int row_height = get_line_height();

if (line_length_guideline_columns.size() > 0) {
HScrollBar *v_scroll_bar = get_v_scroll_bar();
const int xmargin_beg = theme_cache.style_normal->get_margin(SIDE_LEFT) + get_total_gutter_width();
const int xmargin_end = size.width - theme_cache.style_normal->get_margin(SIDE_RIGHT) - (is_drawing_minimap() ? get_minimap_width() : 0);
const int xmargin_end = size.width - theme_cache.style_normal->get_margin(SIDE_RIGHT) - (is_drawing_minimap() ? get_minimap_width() : 0) - (v_scroll_bar->is_visible_in_tree() ? v_scroll_bar->get_combined_minimum_size().width : 0);
const float char_size = theme_cache.font->get_char_size('0', theme_cache.font_size).width;

for (int i = 0; i < line_length_guideline_columns.size(); i++) {
Expand Down Expand Up @@ -108,7 +109,7 @@ void CodeEdit::_notification(int p_what) {
hint_ofs.y -= (code_hint_minsize.y + row_height) - theme_cache.line_spacing;
}

draw_style_box(theme_cache.code_hint_style, Rect2(hint_ofs, code_hint_minsize));
theme_cache.code_hint_style->draw(ci, Rect2(hint_ofs, code_hint_minsize));

int yofs = 0;
for (int i = 0; i < line_count; i++) {
Expand All @@ -123,17 +124,17 @@ void CodeEdit::_notification(int p_what) {

Point2 round_ofs = hint_ofs + theme_cache.code_hint_style->get_offset() + Vector2(0, theme_cache.font->get_ascent(theme_cache.font_size) + font_height * i + yofs);
round_ofs = round_ofs.round();
draw_string(theme_cache.font, round_ofs, line.replace(String::chr(0xFFFF), ""), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size, theme_cache.code_hint_color);
theme_cache.font->draw_string(ci, round_ofs, line.replace(String::chr(0xFFFF), ""), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size, theme_cache.code_hint_color);
if (end > 0) {
// Draw an underline for the currently edited function parameter.
const Vector2 b = hint_ofs + theme_cache.code_hint_style->get_offset() + Vector2(begin, font_height + font_height * i + yofs);
draw_line(b, b + Vector2(end - begin, 0), theme_cache.code_hint_color, 2);
RenderingServer::get_singleton()->canvas_item_add_line(ci, b, b + Vector2(end - begin, 0), theme_cache.code_hint_color, 2);

// Draw a translucent text highlight as well.
const Rect2 highlight_rect = Rect2(
b - Vector2(0, font_height),
Vector2(end - begin, font_height));
draw_rect(highlight_rect, theme_cache.code_hint_color * Color(1, 1, 1, 0.2));
RenderingServer::get_singleton()->canvas_item_add_rect(ci, highlight_rect, theme_cache.code_hint_color * Color(1, 1, 1, 0.2));
}
yofs += theme_cache.line_spacing;
}
Expand Down Expand Up @@ -182,7 +183,7 @@ void CodeEdit::_notification(int p_what) {
code_completion_rect.position.x = caret_pos.x - code_completion_base_width;
}

draw_style_box(theme_cache.code_completion_style, Rect2(code_completion_rect.position - theme_cache.code_completion_style->get_offset(), code_completion_rect.size + theme_cache.code_completion_style->get_minimum_size() + Size2(scroll_width, 0)));
theme_cache.code_completion_style->draw(ci, Rect2(code_completion_rect.position - theme_cache.code_completion_style->get_offset(), code_completion_rect.size + theme_cache.code_completion_style->get_minimum_size() + Size2(scroll_width, 0)));
if (theme_cache.code_completion_background_color.a > 0.01) {
RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(code_completion_rect.position, code_completion_rect.size + Size2(scroll_width, 0)), theme_cache.code_completion_background_color);
}
Expand Down Expand Up @@ -216,18 +217,18 @@ void CodeEdit::_notification(int p_what) {
tl->set_width(code_completion_rect.size.width - (icon_area_size.x + theme_cache.code_completion_icon_separation));
if (rtl) {
if (code_completion_options[l].default_value.get_type() == Variant::COLOR) {
draw_rect(Rect2(Point2(code_completion_rect.position.x, icon_area.position.y), icon_area_size), (Color)code_completion_options[l].default_value);
RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(code_completion_rect.position.x, icon_area.position.y), icon_area_size), (Color)code_completion_options[l].default_value);
}
tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
} else {
if (code_completion_options[l].default_value.get_type() == Variant::COLOR) {
const Color color = code_completion_options[l].default_value;
const Rect2 rect = Rect2(Point2(code_completion_rect.position.x + code_completion_rect.size.width - icon_area_size.x, icon_area.position.y), icon_area_size);
if (color.a < 1.0) {
draw_texture_rect(theme_cache.completion_color_bg, rect, true);
RenderingServer::get_singleton()->canvas_item_add_texture_rect(ci, rect, theme_cache.completion_color_bg->get_rid(), true);
}

draw_rect(rect, color);
RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, color);
}
tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT);
}
Expand All @@ -239,7 +240,7 @@ void CodeEdit::_notification(int p_what) {
int match_offset = theme_cache.font->get_string_size(code_completion_options[l].display.substr(0, match_segment.first), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width;
int match_len = theme_cache.font->get_string_size(code_completion_options[l].display.substr(match_segment.first, match_segment.second), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width;

draw_rect(Rect2(match_pos + Point2(match_offset, 0), Size2(match_len, row_height)), theme_cache.code_completion_existing_color);
RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(match_pos + Point2(match_offset, 0), Size2(match_len, row_height)), theme_cache.code_completion_existing_color);
}
tl->draw(ci, title_pos, code_completion_options[l].font_color);
}
Expand All @@ -250,7 +251,7 @@ void CodeEdit::_notification(int p_what) {

float r = (float)theme_cache.code_completion_max_lines / code_completion_options_count;
float o = (float)code_completion_line_ofs / code_completion_options_count;
draw_rect(Rect2(code_completion_rect.position.x + code_completion_rect.size.width, code_completion_rect.position.y + o * code_completion_rect.size.y, scroll_width, code_completion_rect.size.y * r), scroll_color);
RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(code_completion_rect.position.x + code_completion_rect.size.width, code_completion_rect.position.y + o * code_completion_rect.size.y, scroll_width, code_completion_rect.size.y * r), scroll_color);
}
}
}
Expand Down Expand Up @@ -1298,6 +1299,7 @@ bool CodeEdit::is_drawing_executing_lines_gutter() const {

void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
bool hovering = get_hovered_gutter() == Vector2i(main_gutter, p_line);
RID ci = _get_text_canvas_item();
if (draw_breakpoints && theme_cache.breakpoint_icon.is_valid()) {
bool breakpointed = is_line_breakpointed(p_line);
bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
Expand All @@ -1312,7 +1314,7 @@ void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2
Rect2 icon_region = p_region;
icon_region.position += Point2(padding, padding);
icon_region.size -= Point2(padding, padding) * 2;
theme_cache.breakpoint_icon->draw_rect(get_canvas_item(), icon_region, false, use_color);
theme_cache.breakpoint_icon->draw_rect(ci, icon_region, false, use_color);
}
}

Expand All @@ -1331,7 +1333,7 @@ void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2
Rect2 icon_region = p_region;
icon_region.position += Point2(horizontal_padding, 0);
icon_region.size -= Point2(horizontal_padding * 1.1, vertical_padding);
theme_cache.bookmark_icon->draw_rect(get_canvas_item(), icon_region, false, use_color);
theme_cache.bookmark_icon->draw_rect(ci, icon_region, false, use_color);
}
}

Expand All @@ -1342,7 +1344,7 @@ void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2
Rect2 icon_region = p_region;
icon_region.position += Point2(horizontal_padding, vertical_padding);
icon_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
theme_cache.executing_line_icon->draw_rect(get_canvas_item(), icon_region, false, theme_cache.executing_line_color);
theme_cache.executing_line_icon->draw_rect(ci, icon_region, false, theme_cache.executing_line_color);
}
}

Expand Down Expand Up @@ -1503,7 +1505,7 @@ void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2
number_color = theme_cache.line_number_color;
}

TS->shaped_text_draw(text_rid, get_canvas_item(), ofs, -1, -1, number_color);
TS->shaped_text_draw(text_rid, _get_text_canvas_item(), ofs, -1, -1, number_color);
}

void CodeEdit::_clear_line_number_text_cache() {
Expand Down Expand Up @@ -1532,6 +1534,7 @@ void CodeEdit::_fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_regi
return;
}
set_line_gutter_clickable(p_line, fold_gutter, true);
RID ci = _get_text_canvas_item();

int horizontal_padding = p_region.size.x / 10;
int vertical_padding = p_region.size.y / 6;
Expand All @@ -1545,17 +1548,17 @@ void CodeEdit::_fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_regi
Color region_icon_color = theme_cache.folded_code_region_color;
region_icon_color.a = MAX(region_icon_color.a, 0.4f);
if (can_fold) {
theme_cache.can_fold_code_region_icon->draw_rect(get_canvas_item(), p_region, false, region_icon_color);
theme_cache.can_fold_code_region_icon->draw_rect(ci, p_region, false, region_icon_color);
} else {
theme_cache.folded_code_region_icon->draw_rect(get_canvas_item(), p_region, false, region_icon_color);
theme_cache.folded_code_region_icon->draw_rect(ci, p_region, false, region_icon_color);
}
return;
}
if (can_fold) {
theme_cache.can_fold_icon->draw_rect(get_canvas_item(), p_region, false, theme_cache.code_folding_color);
theme_cache.can_fold_icon->draw_rect(ci, p_region, false, theme_cache.code_folding_color);
return;
}
theme_cache.folded_icon->draw_rect(get_canvas_item(), p_region, false, theme_cache.code_folding_color);
theme_cache.folded_icon->draw_rect(ci, p_region, false, theme_cache.code_folding_color);
}

/* Line Folding */
Expand Down
Loading

0 comments on commit 950add1

Please sign in to comment.