diff --git a/scene/gui/code_edit.cpp b/scene/gui/code_edit.cpp index 5cfd74d07b6..5ce97d93b20 100644 --- a/scene/gui/code_edit.cpp +++ b/scene/gui/code_edit.cpp @@ -270,8 +270,9 @@ void CodeEdit::_draw_guidelines() { const Size2 size = get_size(); const bool rtl = is_layout_rtl(); - 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); + Ref style = is_editable() ? theme_cache.style_normal : theme_cache.style_readonly; + const int xmargin_beg = style->get_margin(SIDE_LEFT) + get_total_gutter_width(); + const int xmargin_end = size.width - style->get_margin(SIDE_RIGHT) - (is_drawing_minimap() ? get_minimap_width() : 0); for (int i = 0; i < line_length_guideline_columns.size(); i++) { 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; @@ -3010,6 +3011,7 @@ void CodeEdit::_bind_methods() { /* Other visuals */ BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CodeEdit, style_normal, "normal"); + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CodeEdit, style_readonly, "read_only"); BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, brace_mismatch_color); diff --git a/scene/gui/code_edit.h b/scene/gui/code_edit.h index 72e48f25ed4..86d6dbc14f3 100644 --- a/scene/gui/code_edit.h +++ b/scene/gui/code_edit.h @@ -290,6 +290,7 @@ private: /* Other visuals */ Ref style_normal; + Ref style_readonly; Color brace_mismatch_color; diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index 9dd0cce910d..ca1c189b116 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -730,6 +730,10 @@ void TextEdit::_accessibility_action_scroll_into_view(const Variant &p_data, int } } +Ref TextEdit::_get_current_stylebox() const { + return editable ? theme_cache.style_normal : theme_cache.style_readonly; +} + void TextEdit::_notification(int p_what) { switch (p_what) { case NOTIFICATION_EXIT_TREE: @@ -763,7 +767,8 @@ void TextEdit::_notification(int p_what) { int first_vis_line = get_first_visible_line(); int row_height = get_line_height(); - int xmargin_beg = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding; + Ref style = _get_current_stylebox(); + int xmargin_beg = Math::ceil(style->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding; Size2 size = get_size(); bool rtl = is_layout_rtl(); int lines_drawn = 0; @@ -782,15 +787,7 @@ void TextEdit::_notification(int p_what) { indent_ofs = MIN(text.get_indent_offset(i, rtl), wrap_at_column * 0.6); } for (int j = 0; j < text_aes.size(); j++) { - float text_off_x = 0.0; - float text_off_y = 0.0; - if (!editable) { - text_off_x = theme_cache.style_readonly->get_offset().x / 2; - text_off_x -= theme_cache.style_normal->get_offset().x / 2; - text_off_y = theme_cache.style_readonly->get_offset().y / 2; - } else { - text_off_y = theme_cache.style_normal->get_offset().y / 2; - } + float text_off_y = style->get_margin(SIDE_TOP); text_off_y += (lines_drawn + j) * row_height + theme_cache.line_spacing / 2; text_off_y -= (first_vis_line + first_visible_line_wrap_ofs) * row_height; @@ -806,7 +803,7 @@ void TextEdit::_notification(int p_what) { DisplayServer::get_singleton()->accessibility_update_set_flag(text_aes[j], DisplayServer::AccessibilityFlags::FLAG_HIDDEN, _is_line_hidden(i)); Transform2D tr; - tr.set_origin(Point2(char_margin + text_off_x, text_off_y)); + tr.set_origin(Point2(char_margin, text_off_y)); DisplayServer::get_singleton()->accessibility_update_set_transform(text_aes[j], tr); DisplayServer::get_singleton()->accessibility_update_set_name(text_aes[j], vformat(RTR("Line %d"), i)); DisplayServer::get_singleton()->accessibility_element_set_meta(text_aes[j], Vector2i(i, j)); @@ -917,6 +914,12 @@ void TextEdit::_notification(int p_what) { first_draw = false; } + RID ci = get_canvas_item(); + Ref style = _get_current_stylebox(); + + // Draw normal/read_only style. + style->draw(ci, Rect2(Point2(), get_size())); + /* Prevent the resource getting lost between the editor and game. */ if (Engine::get_singleton()->is_editor_hint()) { if (syntax_highlighter.is_valid() && syntax_highlighter->get_text_edit() != this) { @@ -938,18 +941,14 @@ void TextEdit::_notification(int p_what) { RS::get_singleton()->canvas_item_set_visibility_layer(text_ci, get_visibility_layer()); RS::get_singleton()->canvas_item_set_default_texture_filter(text_ci, RS::CanvasItemTextureFilter(get_texture_filter_in_tree())); - RID ci = get_canvas_item(); + int left_margin = Math::ceil(style->get_margin(SIDE_LEFT)); + int xmargin_beg = left_margin + gutters_width + gutter_padding; - int xmargin_beg = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding; - - int xmargin_end = size.width - Math::ceil(theme_cache.style_normal->get_margin(SIDE_RIGHT)); + int xmargin_end = size.width - Math::ceil(style->get_margin(SIDE_RIGHT)); if (draw_minimap) { xmargin_end -= minimap_width; } - // Let's do it easy for now. - theme_cache.style_normal->draw(ci, Rect2(Point2(), size)); if (!editable) { - theme_cache.style_readonly->draw(ci, Rect2(Point2(), size)); draw_caret = is_drawing_caret_when_editable_disabled(); } if (has_focus(true)) { @@ -1318,15 +1317,8 @@ void TextEdit::_notification(int p_what) { } } - int top_limit_y = 0; - int bottom_limit_y = get_size().height; - if (!editable) { - top_limit_y += theme_cache.style_readonly->get_margin(SIDE_TOP); - bottom_limit_y -= theme_cache.style_readonly->get_margin(SIDE_BOTTOM); - } else { - top_limit_y += theme_cache.style_normal->get_margin(SIDE_TOP); - bottom_limit_y -= theme_cache.style_normal->get_margin(SIDE_BOTTOM); - } + int top_limit_y = style->get_margin(SIDE_TOP); + int bottom_limit_y = get_size().height - style->get_margin(SIDE_BOTTOM); // Draw guidelines. _draw_guidelines(); @@ -1385,15 +1377,7 @@ void TextEdit::_notification(int p_what) { const String &str = wrap_rows[line_wrap_index]; int char_margin = xmargin_beg - first_visible_col; - int ofs_x = 0; - int ofs_y = 0; - if (!editable) { - ofs_x = theme_cache.style_readonly->get_offset().x / 2; - ofs_x -= theme_cache.style_normal->get_offset().x / 2; - ofs_y = theme_cache.style_readonly->get_offset().y / 2; - } else { - ofs_y = theme_cache.style_normal->get_offset().y / 2; - } + int ofs_y = style->get_margin(SIDE_TOP); ofs_y += i * row_height + theme_cache.line_spacing / 2; ofs_y -= first_visible_line_wrap_ofs * row_height; @@ -1414,18 +1398,18 @@ void TextEdit::_notification(int p_what) { if (text.get_line_background_color(line).a > 0.0) { if (rtl) { - RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(size.width - ofs_x - xmargin_end, ofs_y, xmargin_end - xmargin_beg, row_height), text.get_line_background_color(line)); + RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(size.width - xmargin_end, ofs_y, xmargin_end - xmargin_beg, row_height), text.get_line_background_color(line)); } else { - RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(xmargin_beg + ofs_x, ofs_y, xmargin_end - xmargin_beg, row_height), text.get_line_background_color(line)); + RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(xmargin_beg, ofs_y, xmargin_end - xmargin_beg, row_height), text.get_line_background_color(line)); } } // Draw current line highlight. if (highlight_current_line && highlighted_lines.has(Pair(line, line_wrap_index))) { if (rtl) { - RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(size.width - ofs_x - xmargin_end, ofs_y, xmargin_end, row_height), theme_cache.current_line_color); + RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(size.width - xmargin_end, ofs_y, xmargin_end - xmargin_beg, row_height), theme_cache.current_line_color); } else { - RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(ofs_x, ofs_y, xmargin_end, row_height), theme_cache.current_line_color); + RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(xmargin_beg, ofs_y, xmargin_end - xmargin_beg, row_height), theme_cache.current_line_color); } } @@ -1434,7 +1418,7 @@ void TextEdit::_notification(int p_what) { cache_entry.y_offset = ofs_y; - int gutter_offset = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)); + int gutter_offset = left_margin; for (int g = 0; g < gutters.size(); g++) { const GutterInfo &gutter = gutters[g]; @@ -1455,9 +1439,9 @@ void TextEdit::_notification(int p_what) { int yofs = ofs_y + (row_height - tl->get_size().y) / 2; if (theme_cache.outline_size > 0 && theme_cache.outline_color.a > 0) { - tl->draw_outline(text_ci, Point2(gutter_offset + ofs_x, yofs), theme_cache.outline_size, theme_cache.outline_color); + tl->draw_outline(text_ci, Point2(gutter_offset, yofs), theme_cache.outline_size, theme_cache.outline_color); } - tl->draw(text_ci, Point2(gutter_offset + ofs_x, yofs), get_line_gutter_item_color(line, g)); + tl->draw(text_ci, Point2(gutter_offset, yofs), get_line_gutter_item_color(line, g)); } break; case GUTTER_TYPE_ICON: { const Ref icon = get_line_gutter_icon(line, g); @@ -1551,7 +1535,7 @@ void TextEdit::_notification(int p_what) { } for (int j = 0; j < sel.size(); j++) { - Rect2 rect = Rect2(Math::ceil(sel[j].x) + char_margin + ofs_x, ofs_y, Math::ceil(sel[j].y) - Math::ceil(sel[j].x), row_height); + Rect2 rect = Rect2(Math::ceil(sel[j].x) + char_margin, ofs_y, Math::ceil(sel[j].y) - Math::ceil(sel[j].x), row_height); if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) { continue; } @@ -1574,7 +1558,7 @@ void TextEdit::_notification(int p_what) { while (search_text_col != -1) { const Vector sel = TS->shaped_text_get_selection(rid, search_text_col + start, search_text_col + search_text_len + start); for (int j = 0; j < sel.size(); j++) { - Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height); + Rect2 rect = Rect2(sel[j].x + char_margin, ofs_y, sel[j].y - sel[j].x, row_height); if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) { continue; } @@ -1598,7 +1582,7 @@ void TextEdit::_notification(int p_what) { while (highlighted_text_col != -1) { const Vector sel = TS->shaped_text_get_selection(rid, highlighted_text_col + start, highlighted_text_col + highlighted_text_len + start); for (int j = 0; j < sel.size(); j++) { - Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height); + Rect2 rect = Rect2(sel[j].x + char_margin, ofs_y, sel[j].y - sel[j].x, row_height); if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) { continue; } @@ -1623,7 +1607,7 @@ void TextEdit::_notification(int p_what) { while (lookup_symbol_word_col != -1) { const Vector sel = TS->shaped_text_get_selection(rid, lookup_symbol_word_col + start, lookup_symbol_word_col + lookup_symbol_word_len + start); for (int j = 0; j < sel.size(); j++) { - Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y + (theme_cache.line_spacing / 2), sel[j].y - sel[j].x, row_height); + Rect2 rect = Rect2(sel[j].x + char_margin, ofs_y + (theme_cache.line_spacing / 2), sel[j].y - sel[j].x, row_height); if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) { continue; } @@ -1659,7 +1643,7 @@ void TextEdit::_notification(int p_what) { for (int k = 0; k < glyphs[j].repeat; k++) { if ((char_ofs + char_margin) >= xmargin_beg && (char_ofs + glyphs[j].advance + char_margin) <= xmargin_end) { if (glyphs[j].font_rid != RID()) { - TS->font_draw_glyph_outline(glyphs[j].font_rid, text_ci, glyphs[j].font_size, theme_cache.outline_size, Vector2(char_margin + char_ofs + ofs_x + glyphs[j].x_off, ofs_y + glyphs[j].y_off), glyphs[j].index, theme_cache.outline_color); + TS->font_draw_glyph_outline(glyphs[j].font_rid, text_ci, glyphs[j].font_size, theme_cache.outline_size, Vector2(char_margin + char_ofs + glyphs[j].x_off, ofs_y + glyphs[j].y_off), glyphs[j].index, theme_cache.outline_color); } } char_ofs += glyphs[j].advance; @@ -1674,7 +1658,7 @@ void TextEdit::_notification(int p_what) { // Draw inline objects. for (Dictionary k : object_keys) { Rect2 col_rect = TS->shaped_text_get_object_rect(rid, k); - col_rect.position += Vector2(char_margin + ofs_x, ofs_y); + col_rect.position += Vector2(char_margin, ofs_y); if (!clipped && (col_rect.position.x) >= xmargin_beg && (col_rect.position.x + col_rect.size.x) <= xmargin_end) { inline_object_drawer.call(k, col_rect); } @@ -1704,7 +1688,7 @@ void TextEdit::_notification(int p_what) { } } - float char_pos = char_ofs + char_margin + ofs_x; + float char_pos = char_ofs + char_margin; if (char_pos >= xmargin_beg) { if (highlight_matching_braces_enabled) { for (int c = 0; c < get_caret_count(); c++) { @@ -1743,10 +1727,10 @@ void TextEdit::_notification(int p_what) { for (int k = 0; k < glyphs[j].repeat; k++) { if (!clipped && (char_ofs + char_margin) >= xmargin_beg && (char_ofs + glyphs[j].advance + char_margin) <= xmargin_end) { if (glyphs[j].font_rid != RID()) { - TS->font_draw_glyph(glyphs[j].font_rid, text_ci, glyphs[j].font_size, Vector2(char_margin + char_ofs + ofs_x + glyphs[j].x_off, ofs_y + glyphs[j].y_off), glyphs[j].index, gl_color); + TS->font_draw_glyph(glyphs[j].font_rid, text_ci, glyphs[j].font_size, Vector2(char_margin + char_ofs + glyphs[j].x_off, ofs_y + glyphs[j].y_off), glyphs[j].index, gl_color); had_glyphs_drawn = true; } else if (((glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) && ((glyphs[j].flags & TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) != TextServer::GRAPHEME_IS_EMBEDDED_OBJECT)) { - TS->draw_hex_code_box(text_ci, glyphs[j].font_size, Vector2(char_margin + char_ofs + ofs_x + glyphs[j].x_off, ofs_y + glyphs[j].y_off), glyphs[j].index, gl_color); + TS->draw_hex_code_box(text_ci, glyphs[j].font_size, Vector2(char_margin + char_ofs + glyphs[j].x_off, ofs_y + glyphs[j].y_off), glyphs[j].index, gl_color); had_glyphs_drawn = true; } } @@ -1772,7 +1756,7 @@ void TextEdit::_notification(int p_what) { // is_line_folded if (line_wrap_index == line_wrap_amount && line < text.size() - 1 && _is_line_hidden(line + 1)) { - int xofs = char_ofs + char_margin + ofs_x + (_get_folded_eol_icon()->get_width() / 2); + int xofs = char_ofs + char_margin + (_get_folded_eol_icon()->get_width() / 2); if (xofs >= xmargin_beg && xofs < xmargin_end) { int yofs = (text_height - _get_folded_eol_icon()->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index); Color eol_color = _get_code_folding_color(); @@ -1808,9 +1792,9 @@ void TextEdit::_notification(int p_what) { } if ((ts_caret.l_caret != Rect2() && (ts_caret.l_dir == TextServer::DIRECTION_AUTO || ts_caret.l_dir == (TextServer::Direction)input_direction)) || (ts_caret.t_caret == Rect2())) { - carets.write[c].draw_pos.x = char_margin + ofs_x + ts_caret.l_caret.position.x; + carets.write[c].draw_pos.x = char_margin + ts_caret.l_caret.position.x; } else { - carets.write[c].draw_pos.x = char_margin + ofs_x + ts_caret.t_caret.position.x; + carets.write[c].draw_pos.x = char_margin + ts_caret.t_caret.position.x; } if (get_caret_draw_pos(c).x >= xmargin_beg && get_caret_draw_pos(c).x <= xmargin_end) { @@ -1828,7 +1812,7 @@ void TextEdit::_notification(int p_what) { ts_caret.t_caret.position.y = -TS->shaped_text_get_ascent(rid); ts_caret.t_caret.size.y = h; } - ts_caret.t_caret.position += Vector2(char_margin + ofs_x, ofs_y); + ts_caret.t_caret.position += Vector2(char_margin, ofs_y); if (overtype_mode) { RS::get_singleton()->canvas_item_add_rect(text_ci, ts_caret.t_caret, theme_cache.caret_color); } else { @@ -1837,7 +1821,7 @@ void TextEdit::_notification(int p_what) { if (ts_caret.l_caret != Rect2() && ts_caret.l_dir != ts_caret.t_dir) { // Draw split caret (leading part). - ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y); + ts_caret.l_caret.position += Vector2(char_margin, ofs_y); ts_caret.l_caret.size.x = caret_width; RS::get_singleton()->canvas_item_add_rect(text_ci, ts_caret.l_caret, theme_cache.caret_color); // Draw extra direction marker on top of split caret. @@ -1864,7 +1848,7 @@ void TextEdit::_notification(int p_what) { } else { ts_caret.l_caret.size.x = 3 * caret_width; } - ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y); + ts_caret.l_caret.position += Vector2(char_margin, ofs_y); if (ts_caret.l_dir == TextServer::DIRECTION_RTL) { ts_caret.l_caret.position.x -= ts_caret.l_caret.size.x; } @@ -1879,26 +1863,26 @@ void TextEdit::_notification(int p_what) { if (ts_caret.l_caret != Rect2() && ts_caret.l_dir == TextServer::DIRECTION_AUTO) { // Draw extra marker on top of mid caret. Rect2 trect = Rect2(ts_caret.l_caret.position.x - 2.5 * caret_width, ts_caret.l_caret.position.y, 6 * caret_width, caret_width); - trect.position += Vector2(char_margin + ofs_x, ofs_y); + trect.position += Vector2(char_margin, ofs_y); RS::get_singleton()->canvas_item_add_rect(text_ci, trect, theme_cache.caret_color); } else if (ts_caret.l_caret != Rect2() && ts_caret.t_caret != Rect2() && ts_caret.l_dir != ts_caret.t_dir) { // Draw extra direction marker on top of split caret. float d = (ts_caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3; Rect2 trect = Rect2(ts_caret.l_caret.position.x + d * caret_width, ts_caret.l_caret.position.y + ts_caret.l_caret.size.y - caret_width, 3 * caret_width, caret_width); - trect.position += Vector2(char_margin + ofs_x, ofs_y); + trect.position += Vector2(char_margin, ofs_y); RS::get_singleton()->canvas_item_add_rect(text_ci, trect, theme_cache.caret_color); d = (ts_caret.t_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3; trect = Rect2(ts_caret.t_caret.position.x + d * caret_width, ts_caret.t_caret.position.y, 3 * caret_width, caret_width); - trect.position += Vector2(char_margin + ofs_x, ofs_y); + trect.position += Vector2(char_margin, ofs_y); RS::get_singleton()->canvas_item_add_rect(text_ci, trect, theme_cache.caret_color); } - ts_caret.l_caret.position += Vector2(char_margin + ofs_x, ofs_y); + ts_caret.l_caret.position += Vector2(char_margin, ofs_y); ts_caret.l_caret.size.x = caret_width; RS::get_singleton()->canvas_item_add_rect(text_ci, ts_caret.l_caret, theme_cache.caret_color); - ts_caret.t_caret.position += Vector2(char_margin + ofs_x, ofs_y); + ts_caret.t_caret.position += Vector2(char_margin, ofs_y); ts_caret.t_caret.size.x = caret_width; RS::get_singleton()->canvas_item_add_rect(text_ci, ts_caret.t_caret, theme_cache.caret_color); @@ -1911,7 +1895,7 @@ void TextEdit::_notification(int p_what) { // IME Intermediate text range. const Vector sel = TS->shaped_text_get_selection(rid, get_caret_column(c), get_caret_column(c) + ime_text.length()); for (int j = 0; j < sel.size(); j++) { - Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, text_height); + Rect2 rect = Rect2(sel[j].x + char_margin, ofs_y, sel[j].y - sel[j].x, text_height); if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) { continue; } @@ -1930,7 +1914,7 @@ void TextEdit::_notification(int p_what) { // IME caret. const Vector sel = TS->shaped_text_get_selection(rid, get_caret_column(c) + ime_selection.x, get_caret_column(c) + ime_selection.x + ime_selection.y); for (int j = 0; j < sel.size(); j++) { - Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, text_height); + Rect2 rect = Rect2(sel[j].x + char_margin, ofs_y, sel[j].y - sel[j].x, text_height); if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) { continue; } @@ -2312,7 +2296,7 @@ void TextEdit::gui_input(const Ref &p_gui_input) { emit_signal(SNAME("gutter_clicked"), hovered_gutter.y, hovered_gutter.x); return; } - int left_margin = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)); + int left_margin = Math::ceil(_get_current_stylebox()->get_margin(SIDE_LEFT)); if (mpos.x < left_margin + gutters_width + gutter_padding) { return; } @@ -2413,7 +2397,7 @@ void TextEdit::gui_input(const Ref &p_gui_input) { // Click inline objects. if (inline_object_click_handler.is_valid()) { - int xmargin_beg = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding; + int xmargin_beg = left_margin + gutters_width + gutter_padding; int wrap_i = get_line_wrap_index_at_column(pos.y, pos.x); const float wrap_indent = _get_wrap_indent_offset(pos.y, wrap_i, is_layout_rtl()); @@ -3513,14 +3497,14 @@ void TextEdit::_show_virtual_keyboard() { /* General overrides. */ Size2 TextEdit::get_minimum_size() const { - Size2 size = theme_cache.style_normal->get_minimum_size(); + Size2 ms = _get_current_stylebox()->get_minimum_size(); if (fit_content_height) { - size.y += content_size_cache.y; + ms.height += content_size_cache.height; } if (fit_content_width) { - size.x += content_size_cache.x; + ms.width += content_size_cache.width; } - return size; + return ms; } bool TextEdit::is_text_field() const { @@ -3624,12 +3608,13 @@ Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const { return CURSOR_ARROW; } } - int left_margin = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)); + Ref style = _get_current_stylebox(); + int left_margin = Math::ceil(style->get_margin(SIDE_LEFT)); if (p_pos.x < left_margin + gutters_width + gutter_padding) { return CURSOR_ARROW; } - int xmargin_end = get_size().width - Math::ceil(theme_cache.style_normal->get_margin(SIDE_RIGHT)); + int xmargin_end = get_size().width - Math::ceil(style->get_margin(SIDE_RIGHT)); if (draw_minimap && p_pos.x > xmargin_end - minimap_width && p_pos.x <= xmargin_end) { return CURSOR_ARROW; } @@ -3637,7 +3622,7 @@ Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const { // Hover inline objects. if (inline_object_click_handler.is_valid()) { Point2i pos = get_line_column_at_pos(p_pos); - int xmargin_beg = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding; + int xmargin_beg = left_margin + gutters_width + gutter_padding; int wrap_i = get_line_wrap_index_at_column(pos.y, pos.x); const float wrap_indent = _get_wrap_indent_offset(pos.y, wrap_i, is_layout_rtl()); @@ -3736,6 +3721,7 @@ void TextEdit::set_editable(bool p_editable) { editable = p_editable; queue_accessibility_update(); queue_redraw(); + update_minimum_size(); } bool TextEdit::is_editable() const { @@ -4994,11 +4980,8 @@ String TextEdit::get_word(int p_line, int p_column) const { } Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_clamp_line, bool p_clamp_column) const { - float rows = p_pos.y - theme_cache.style_normal->get_margin(SIDE_TOP); - if (!editable) { - rows -= theme_cache.style_readonly->get_offset().y / 2; - rows += theme_cache.style_normal->get_offset().y / 2; - } + Ref style = _get_current_stylebox(); + float rows = p_pos.y - (style->get_margin(SIDE_TOP) + (theme_cache.line_spacing / 2)); rows /= get_line_height(); rows += _get_v_scroll_offset(); int first_vis_line = get_first_visible_line(); @@ -5025,13 +5008,8 @@ Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_clamp_line } return Point2i(-1, -1); } - - int colx = p_pos.x - (Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding); + int colx = p_pos.x - (Math::ceil(style->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding); colx += first_visible_col; - if (!editable) { - colx -= theme_cache.style_readonly->get_offset().x / 2; - colx += theme_cache.style_normal->get_offset().x / 2; - } RID text_rid = text.get_line_data(row)->get_line_rid(wrap_index); @@ -5097,7 +5075,7 @@ Rect2i TextEdit::get_rect_at_line_column(int p_line, int p_column) const { Point2i pos, size; pos.y = cache_entry.y_offset + get_line_height() * wrap_index; - pos.x = get_total_gutter_width() + Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)) + wrap_indent - get_h_scroll(); + pos.x = get_total_gutter_width() + Math::ceil(_get_current_stylebox()->get_margin(SIDE_LEFT)) + wrap_indent - get_h_scroll(); RID text_rid = text.get_line_data(p_line)->get_line_rid(wrap_index); Vector2 col_bounds = TS->shaped_text_get_grapheme_bounds(text_rid, p_column); @@ -5110,8 +5088,7 @@ Rect2i TextEdit::get_rect_at_line_column(int p_line, int p_column) const { } int TextEdit::get_minimap_line_at_pos(const Point2i &p_pos) const { - float rows = p_pos.y; - rows -= theme_cache.style_normal->get_margin(SIDE_TOP); + float rows = p_pos.y - _get_current_stylebox()->get_margin(SIDE_TOP); rows /= (minimap_char_size.y + minimap_line_spacing); rows += _get_v_scroll_offset(); @@ -8406,9 +8383,7 @@ void TextEdit::_click_selection_held() { } void TextEdit::_update_selection_mode_pointer(bool p_initial) { - Point2 mp = get_local_mouse_pos(); - - Point2i pos = get_line_column_at_pos(mp); + Point2i pos = get_line_column_at_pos(get_local_mouse_pos()); int line = pos.y; int column = pos.x; int caret_index = get_caret_count() - 1; @@ -8435,9 +8410,8 @@ void TextEdit::_update_selection_mode_pointer(bool p_initial) { void TextEdit::_update_selection_mode_word(bool p_initial) { dragging_selection = true; - Point2 mp = get_local_mouse_pos(); - Point2i pos = get_line_column_at_pos(mp); + Point2i pos = get_line_column_at_pos(get_local_mouse_pos()); int line = pos.y; int column = pos.x; int caret_index = get_caret_count() - 1; @@ -8483,9 +8457,8 @@ void TextEdit::_update_selection_mode_word(bool p_initial) { void TextEdit::_update_selection_mode_line(bool p_initial) { dragging_selection = true; - Point2 mp = get_local_mouse_pos(); - Point2i pos = get_line_column_at_pos(mp); + Point2i pos = get_line_column_at_pos(get_local_mouse_pos()); int line = pos.y; int caret_index = get_caret_count() - 1; @@ -8538,7 +8511,7 @@ bool TextEdit::_selection_contains(int p_caret, int p_line, int p_column, bool p /* Line Wrapping */ void TextEdit::_update_wrap_at_column(bool p_force) { - int new_wrap_at = get_size().width - theme_cache.style_normal->get_minimum_size().width - gutters_width - gutter_padding; + int new_wrap_at = get_size().width - _get_current_stylebox()->get_minimum_size().width - gutters_width - gutter_padding; if (draw_minimap) { new_wrap_at -= minimap_width; } @@ -8593,8 +8566,9 @@ void TextEdit::_update_scrollbars() { Size2 hmin = h_scroll->get_combined_minimum_size(); Size2 vmin = v_scroll->get_combined_minimum_size(); - v_scroll->set_begin(Point2(size.width - vmin.width, theme_cache.style_normal->get_margin(SIDE_TOP))); - v_scroll->set_end(Point2(size.width, size.height - theme_cache.style_normal->get_margin(SIDE_TOP) - theme_cache.style_normal->get_margin(SIDE_BOTTOM))); + Ref style = _get_current_stylebox(); + v_scroll->set_begin(Point2(size.width - vmin.width, style->get_margin(SIDE_TOP))); + v_scroll->set_end(Point2(size.width, size.height - style->get_margin(SIDE_TOP) - style->get_margin(SIDE_BOTTOM))); h_scroll->set_begin(Point2(0, size.height - hmin.height)); h_scroll->set_end(Point2(size.width - vmin.width, size.height)); @@ -8607,7 +8581,7 @@ void TextEdit::_update_scrollbars() { total_rows += visible_rows - 1; } - int visible_width = size.width - theme_cache.style_normal->get_minimum_size().width; + int visible_width = size.width - style->get_minimum_size().width; int total_width = (draw_placeholder ? placeholder_max_width : text.get_max_width()) + gutters_width + gutter_padding; if (draw_minimap) { @@ -8656,8 +8630,7 @@ void TextEdit::_update_scrollbars() { } int TextEdit::_get_control_height() const { - int control_height = get_size().height; - control_height -= theme_cache.style_normal->get_minimum_size().height; + int control_height = get_size().height - _get_current_stylebox()->get_minimum_size().height; if (h_scroll->is_visible_in_tree()) { control_height -= h_scroll->get_size().height; } @@ -8826,7 +8799,7 @@ void TextEdit::_adjust_viewport_to_caret_horizontally(int p_caret, bool p_maximi return; } - int visible_width = get_size().width - theme_cache.style_normal->get_minimum_size().width - gutters_width - gutter_padding; + int visible_width = get_size().width - _get_current_stylebox()->get_minimum_size().width - gutters_width - gutter_padding; if (draw_minimap) { visible_width -= minimap_width; } @@ -8888,7 +8861,7 @@ void TextEdit::_adjust_viewport_to_caret_horizontally(int p_caret, bool p_maximi void TextEdit::_update_minimap_hover() { const Point2 mp = get_local_mouse_pos(); - const int xmargin_end = get_size().width - Math::ceil(theme_cache.style_normal->get_margin(SIDE_RIGHT)); + const int xmargin_end = get_size().width - Math::ceil(_get_current_stylebox()->get_margin(SIDE_RIGHT)); bool hovering_sidebar = mp.x > xmargin_end - minimap_width && mp.x < xmargin_end; if (!hovering_sidebar) { @@ -8915,7 +8888,7 @@ void TextEdit::_update_minimap_hover() { void TextEdit::_update_minimap_click() { Point2 mp = get_local_mouse_pos(); - int xmargin_end = get_size().width - Math::ceil(theme_cache.style_normal->get_margin(SIDE_RIGHT)); + int xmargin_end = get_size().width - Math::ceil(_get_current_stylebox()->get_margin(SIDE_RIGHT)); if (!dragging_minimap && (mp.x < xmargin_end - minimap_width || mp.x > xmargin_end)) { minimap_clicked = false; return; @@ -8978,7 +8951,7 @@ void TextEdit::_update_gutter_width() { } Vector2i TextEdit::_get_hovered_gutter(const Point2 &p_mouse_pos) const { - int left_margin = Math::ceil(theme_cache.style_normal->get_margin(SIDE_LEFT)); + int left_margin = Math::ceil(_get_current_stylebox()->get_margin(SIDE_LEFT)); if (p_mouse_pos.x > left_margin + gutters_width + gutter_padding) { return Vector2i(-1, -1); } diff --git a/scene/gui/text_edit.h b/scene/gui/text_edit.h index 0cabfae0f03..a0504cb6c1e 100644 --- a/scene/gui/text_edit.h +++ b/scene/gui/text_edit.h @@ -661,6 +661,9 @@ private: // FIXME: Helper method to draw unfilled rects, should be moved to RenderingServer. void _draw_rect_unfilled(RID p_canvas_item, const Rect2 &p_rect, const Color &p_color, real_t p_width = -1.0, bool p_antialiased = false) const; + /* Theme. */ + Ref _get_current_stylebox() const; + /*** Super internal Core API. Everything builds on it. ***/ bool text_changed_dirty = false; void _text_changed();