Skip to content

Commit 31485d3

Browse files
WhalesStatekitbdev
andcommitted
Fix TextEdit styles and disable clipping.
Co-authored-by: kit <[email protected]>
1 parent de463e0 commit 31485d3

File tree

6 files changed

+194
-174
lines changed

6 files changed

+194
-174
lines changed

doc/classes/TextEdit.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,6 @@
13091309
<member name="caret_type" type="int" setter="set_caret_type" getter="get_caret_type" enum="TextEdit.CaretType" default="0">
13101310
Set the type of caret to draw.
13111311
</member>
1312-
<member name="clip_contents" type="bool" setter="set_clip_contents" getter="is_clipping_contents" overrides="Control" default="true" />
13131312
<member name="context_menu_enabled" type="bool" setter="set_context_menu_enabled" getter="is_context_menu_enabled" default="true">
13141313
If [code]true[/code], a right-click displays the context menu.
13151314
</member>

editor/script/script_text_editor.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,12 @@ void ScriptTextEditor::_inline_object_draw(const Dictionary &p_info, const Rect2
516516
if (color_alpha_texture.is_null()) {
517517
color_alpha_texture = inline_color_picker->get_theme_icon("sample_bg", "ColorPicker");
518518
}
519-
code_editor->get_text_editor()->draw_texture_rect(color_alpha_texture, col_rect, false);
520-
code_editor->get_text_editor()->draw_rect(col_rect, Color(p_info["color"]));
521-
code_editor->get_text_editor()->draw_rect(col_rect, Color(1, 1, 1), false, 1);
519+
RID text_ci = code_editor->get_text_editor()->get_text_canvas_item();
520+
RenderingServer *rs = RenderingServer::get_singleton();
521+
522+
rs->canvas_item_add_rect(text_ci, p_rect.grow(-3), Color(1, 1, 1));
523+
rs->canvas_item_add_texture_rect(text_ci, col_rect, color_alpha_texture->get_rid());
524+
rs->canvas_item_add_rect(text_ci, col_rect, Color(p_info["color"]));
522525
}
523526
}
524527

scene/gui/code_edit.cpp

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,15 @@ void CodeEdit::_notification(int p_what) {
6969
} break;
7070

7171
case NOTIFICATION_DRAW: {
72-
RID ci = get_canvas_item();
72+
RID ci = get_text_canvas_item();
73+
RenderingServer *rs = RenderingServer::get_singleton();
7374
const bool caret_visible = is_caret_visible();
7475
const bool rtl = is_layout_rtl();
7576
const int row_height = get_line_height();
7677

78+
rs->canvas_item_clear(code_completion_ci);
79+
rs->canvas_item_set_visibility_layer(code_completion_ci, get_visibility_layer());
80+
7781
if (caret_visible) {
7882
const bool draw_code_completion = code_completion_active && !code_completion_options.is_empty();
7983
const bool draw_code_hint = !code_hint.is_empty();
@@ -103,7 +107,7 @@ void CodeEdit::_notification(int p_what) {
103107
hint_ofs.y -= (code_hint_minsize.y + row_height) - theme_cache.line_spacing;
104108
}
105109

106-
draw_style_box(theme_cache.code_hint_style, Rect2(hint_ofs, code_hint_minsize));
110+
theme_cache.code_hint_style->draw(ci, Rect2(hint_ofs, code_hint_minsize));
107111

108112
int yofs = 0;
109113
for (int i = 0; i < line_count; i++) {
@@ -118,17 +122,17 @@ void CodeEdit::_notification(int p_what) {
118122

119123
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);
120124
round_ofs = round_ofs.round();
121-
draw_string(theme_cache.font, round_ofs, line.remove_char(0xFFFF), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size, theme_cache.code_hint_color);
125+
theme_cache.font->draw_string(ci, round_ofs, line.remove_char(0xFFFF), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size, theme_cache.code_hint_color);
122126
if (end > 0) {
123127
// Draw an underline for the currently edited function parameter.
124128
const Vector2 b = hint_ofs + theme_cache.code_hint_style->get_offset() + Vector2(begin, font_height + font_height * i + yofs);
125-
draw_line(b, b + Vector2(end - begin, 0), theme_cache.code_hint_color, 2);
129+
rs->canvas_item_add_line(ci, b, b + Vector2(end - begin, 0), theme_cache.code_hint_color, 2);
126130

127131
// Draw a translucent text highlight as well.
128132
const Rect2 highlight_rect = Rect2(
129133
b - Vector2(0, font_height),
130134
Vector2(end - begin, font_height));
131-
draw_rect(highlight_rect, theme_cache.code_hint_color * Color(1, 1, 1, 0.2));
135+
rs->canvas_item_add_rect(ci, highlight_rect, theme_cache.code_hint_color * Color(1, 1, 1, 0.2));
132136
}
133137
yofs += theme_cache.line_spacing;
134138
}
@@ -177,16 +181,16 @@ void CodeEdit::_notification(int p_what) {
177181
code_completion_rect.position.x = caret_pos.x - code_completion_base_width;
178182
}
179183

180-
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)));
184+
theme_cache.code_completion_style->draw(code_completion_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)));
181185
if (theme_cache.code_completion_background_color.a > 0.01) {
182-
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);
186+
rs->canvas_item_add_rect(code_completion_ci, Rect2(code_completion_rect.position, code_completion_rect.size + Size2(scroll_width, 0)), theme_cache.code_completion_background_color);
183187
}
184188

185189
code_completion_scroll_rect.position = code_completion_rect.position + Vector2(code_completion_rect.size.width, 0);
186190
code_completion_scroll_rect.size = Vector2(scroll_width, code_completion_rect.size.height);
187191

188192
code_completion_line_ofs = CLAMP((code_completion_force_item_center < 0 ? code_completion_current_selected : code_completion_force_item_center) - lines / 2, 0, code_completion_options_count - lines);
189-
RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(code_completion_rect.position.x, code_completion_rect.position.y + (code_completion_current_selected - code_completion_line_ofs) * row_height), Size2(code_completion_rect.size.width, row_height)), theme_cache.code_completion_selected_color);
193+
rs->canvas_item_add_rect(code_completion_ci, Rect2(Point2(code_completion_rect.position.x, code_completion_rect.position.y + (code_completion_current_selected - code_completion_line_ofs) * row_height), Size2(code_completion_rect.size.width, row_height)), theme_cache.code_completion_selected_color);
190194

191195
for (int i = 0; i < lines; i++) {
192196
int l = code_completion_line_ofs + i;
@@ -204,25 +208,25 @@ void CodeEdit::_notification(int p_what) {
204208
Rect2 icon_area(code_completion_rect.position.x, code_completion_rect.position.y + i * row_height, icon_area_size.width, icon_area_size.height);
205209
if (icon.is_valid()) {
206210
Size2 icon_size = icon_area.size * 0.7;
207-
icon->draw_rect(ci, Rect2(icon_area.position + (icon_area.size - icon_size) / 2, icon_size));
211+
icon->draw_rect(code_completion_ci, Rect2(icon_area.position + (icon_area.size - icon_size) / 2, icon_size));
208212
}
209213
title_pos.x = icon_area.position.x + icon_area.size.width + theme_cache.code_completion_icon_separation;
210214

211215
tl->set_width(code_completion_rect.size.width - (icon_area_size.x + theme_cache.code_completion_icon_separation));
212216
if (rtl) {
213217
if (code_completion_options[l].default_value.get_type() == Variant::COLOR) {
214-
draw_rect(Rect2(Point2(code_completion_rect.position.x, icon_area.position.y), icon_area_size), (Color)code_completion_options[l].default_value);
218+
rs->canvas_item_add_rect(code_completion_ci, Rect2(Point2(code_completion_rect.position.x, icon_area.position.y), icon_area_size), (Color)code_completion_options[l].default_value);
215219
}
216220
tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
217221
} else {
218222
if (code_completion_options[l].default_value.get_type() == Variant::COLOR) {
219223
const Color color = code_completion_options[l].default_value;
220224
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);
221225
if (color.a < 1.0) {
222-
draw_texture_rect(theme_cache.completion_color_bg, rect, true);
226+
rs->canvas_item_add_texture_rect(code_completion_ci, rect, theme_cache.completion_color_bg->get_rid(), true);
223227
}
224228

225-
draw_rect(rect, color);
229+
rs->canvas_item_add_rect(code_completion_ci, rect, color);
226230
}
227231
tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT);
228232
}
@@ -234,9 +238,9 @@ void CodeEdit::_notification(int p_what) {
234238
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;
235239
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;
236240

237-
draw_rect(Rect2(match_pos + Point2(match_offset, 0), Size2(match_len, row_height)), theme_cache.code_completion_existing_color);
241+
rs->canvas_item_add_rect(code_completion_ci, Rect2(match_pos + Point2(match_offset, 0), Size2(match_len, row_height)), theme_cache.code_completion_existing_color);
238242
}
239-
tl->draw(ci, title_pos, code_completion_options[l].font_color);
243+
tl->draw(code_completion_ci, title_pos, code_completion_options[l].font_color);
240244
}
241245

242246
/* Draw a small scroll rectangle to show a position in the options. */
@@ -245,7 +249,7 @@ void CodeEdit::_notification(int p_what) {
245249

246250
float r = (float)theme_cache.code_completion_max_lines / code_completion_options_count;
247251
float o = (float)code_completion_line_ofs / code_completion_options_count;
248-
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);
252+
rs->canvas_item_add_rect(code_completion_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);
249253
}
250254
}
251255
}
@@ -268,21 +272,22 @@ void CodeEdit::_draw_guidelines() {
268272

269273
RID ci = get_canvas_item();
270274
const Size2 size = get_size();
271-
const bool rtl = is_layout_rtl();
275+
RenderingServer *rs = RenderingServer::get_singleton();
272276

273-
const int xmargin_beg = theme_cache.style_normal->get_margin(SIDE_LEFT) + get_total_gutter_width();
274-
const int xmargin_end = size.width - theme_cache.style_normal->get_margin(SIDE_RIGHT) - (is_drawing_minimap() ? get_minimap_width() : 0);
277+
Ref<StyleBox> style = is_editable() ? theme_cache.style_normal : theme_cache.style_readonly;
278+
const int xmargin_beg = style->get_margin(SIDE_LEFT) + get_total_gutter_width();
279+
const int xmargin_end = size.width - style->get_margin(SIDE_RIGHT) - (is_drawing_minimap() ? get_minimap_width() : 0);
275280

276281
for (int i = 0; i < line_length_guideline_columns.size(); i++) {
277282
const int column_pos = theme_cache.font->get_string_size(String("0").repeat((int)line_length_guideline_columns[i]), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
278283
const int xoffset = xmargin_beg + column_pos - get_h_scroll();
279284
if (xoffset > xmargin_beg && xoffset < xmargin_end) {
280285
Color guideline_color = (i == 0) ? theme_cache.line_length_guideline_color : theme_cache.line_length_guideline_color * Color(1, 1, 1, 0.5);
281-
if (rtl) {
282-
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(size.width - xoffset, 0), Point2(size.width - xoffset, size.height), guideline_color);
286+
if (is_layout_rtl()) {
287+
rs->canvas_item_add_line(ci, Point2(size.width - xoffset, 0), Point2(size.width - xoffset, size.height), guideline_color);
283288
continue;
284289
}
285-
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(xoffset, 0), Point2(xoffset, size.height), guideline_color);
290+
rs->canvas_item_add_line(ci, Point2(xoffset, 0), Point2(xoffset, size.height), guideline_color);
286291
}
287292
}
288293
}
@@ -1341,6 +1346,7 @@ bool CodeEdit::is_drawing_executing_lines_gutter() const {
13411346

13421347
void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
13431348
bool hovering = get_hovered_gutter() == Vector2i(main_gutter, p_line);
1349+
RID ci = get_text_canvas_item();
13441350
if (draw_breakpoints && theme_cache.breakpoint_icon.is_valid()) {
13451351
bool breakpointed = is_line_breakpointed(p_line);
13461352
bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
@@ -1355,7 +1361,7 @@ void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2
13551361
Rect2 icon_region = p_region;
13561362
icon_region.position += Point2(padding, padding);
13571363
icon_region.size -= Point2(padding, padding) * 2;
1358-
theme_cache.breakpoint_icon->draw_rect(get_canvas_item(), icon_region, false, use_color);
1364+
theme_cache.breakpoint_icon->draw_rect(ci, icon_region, false, use_color);
13591365
}
13601366
}
13611367

@@ -1374,7 +1380,7 @@ void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2
13741380
Rect2 icon_region = p_region;
13751381
icon_region.position += Point2(horizontal_padding, 0);
13761382
icon_region.size -= Point2(horizontal_padding * 1.1, vertical_padding);
1377-
theme_cache.bookmark_icon->draw_rect(get_canvas_item(), icon_region, false, use_color);
1383+
theme_cache.bookmark_icon->draw_rect(ci, icon_region, false, use_color);
13781384
}
13791385
}
13801386

@@ -1385,7 +1391,7 @@ void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2
13851391
Rect2 icon_region = p_region;
13861392
icon_region.position += Point2(horizontal_padding, vertical_padding);
13871393
icon_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
1388-
theme_cache.executing_line_icon->draw_rect(get_canvas_item(), icon_region, false, theme_cache.executing_line_color);
1394+
theme_cache.executing_line_icon->draw_rect(ci, icon_region, false, theme_cache.executing_line_color);
13891395
}
13901396
}
13911397

@@ -1546,7 +1552,7 @@ void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2
15461552
number_color = theme_cache.line_number_color;
15471553
}
15481554

1549-
TS->shaped_text_draw(text_rid, get_canvas_item(), ofs, -1, -1, number_color);
1555+
TS->shaped_text_draw(text_rid, get_text_canvas_item(), ofs, -1, -1, number_color);
15501556
}
15511557

15521558
void CodeEdit::_clear_line_number_text_cache() {
@@ -1575,6 +1581,7 @@ void CodeEdit::_fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_regi
15751581
return;
15761582
}
15771583
set_line_gutter_clickable(p_line, fold_gutter, true);
1584+
RID ci = get_text_canvas_item();
15781585

15791586
int horizontal_padding = p_region.size.x / 10;
15801587
int vertical_padding = p_region.size.y / 6;
@@ -1588,17 +1595,17 @@ void CodeEdit::_fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_regi
15881595
Color region_icon_color = theme_cache.folded_code_region_color;
15891596
region_icon_color.a = MAX(region_icon_color.a, 0.4f);
15901597
if (can_fold) {
1591-
theme_cache.can_fold_code_region_icon->draw_rect(get_canvas_item(), p_region, false, region_icon_color);
1598+
theme_cache.can_fold_code_region_icon->draw_rect(ci, p_region, false, region_icon_color);
15921599
} else {
1593-
theme_cache.folded_code_region_icon->draw_rect(get_canvas_item(), p_region, false, region_icon_color);
1600+
theme_cache.folded_code_region_icon->draw_rect(ci, p_region, false, region_icon_color);
15941601
}
15951602
return;
15961603
}
15971604
if (can_fold) {
1598-
theme_cache.can_fold_icon->draw_rect(get_canvas_item(), p_region, false, theme_cache.code_folding_color);
1605+
theme_cache.can_fold_icon->draw_rect(ci, p_region, false, theme_cache.code_folding_color);
15991606
return;
16001607
}
1601-
theme_cache.folded_icon->draw_rect(get_canvas_item(), p_region, false, theme_cache.code_folding_color);
1608+
theme_cache.folded_icon->draw_rect(ci, p_region, false, theme_cache.code_folding_color);
16021609
}
16031610

16041611
/* Line Folding */
@@ -3008,6 +3015,7 @@ void CodeEdit::_bind_methods() {
30083015

30093016
/* Other visuals */
30103017
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CodeEdit, style_normal, "normal");
3018+
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CodeEdit, style_readonly, "read_only");
30113019

30123020
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, brace_mismatch_color);
30133021

@@ -3838,6 +3846,11 @@ void CodeEdit::_text_changed() {
38383846
}
38393847

38403848
CodeEdit::CodeEdit() {
3849+
/* Code Completion */
3850+
code_completion_ci = RenderingServer::get_singleton()->canvas_item_create();
3851+
RenderingServer::get_singleton()->canvas_item_set_parent(code_completion_ci, get_canvas_item());
3852+
RenderingServer::get_singleton()->canvas_item_set_use_parent_material(code_completion_ci, true);
3853+
38413854
/* Indent management */
38423855
auto_indent_prefixes.insert(':');
38433856
auto_indent_prefixes.insert('{');
@@ -3909,6 +3922,7 @@ CodeEdit::CodeEdit() {
39093922

39103923
CodeEdit::~CodeEdit() {
39113924
_clear_line_number_text_cache();
3925+
RenderingServer::get_singleton()->free(code_completion_ci);
39123926
}
39133927

39143928
// Return true if l should come before r

scene/gui/code_edit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ class CodeEdit : public TextEdit {
204204
int code_hint_xpos = -0xFFFF;
205205

206206
/* Code Completion */
207+
RID code_completion_ci;
207208
bool code_completion_enabled = false;
208209
bool code_completion_forced = false;
209210

@@ -290,6 +291,7 @@ class CodeEdit : public TextEdit {
290291

291292
/* Other visuals */
292293
Ref<StyleBox> style_normal;
294+
Ref<StyleBox> style_readonly;
293295

294296
Color brace_mismatch_color;
295297

0 commit comments

Comments
 (0)