Skip to content

Commit

Permalink
add "expand_to_text_height" to textedit
Browse files Browse the repository at this point in the history
  • Loading branch information
Valla-Chan committed Mar 31, 2024
1 parent f5341f2 commit ff62c8d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 13 deletions.
26 changes: 14 additions & 12 deletions editor/plugins/canvas_item_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6199,18 +6199,6 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) {

main_menu_hbox->add_child(memnew(VSeparator));

menu_spacer = memnew(Control);
menu_spacer->set_h_size_flags(Control::SIZE_EXPAND_FILL);
main_menu_hbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
main_menu_hbox->add_child(menu_spacer);


label_gridsize = memnew(Label);
label_gridsize->set_tooltip("Grid Size");
main_menu_hbox->add_child(memnew(VSeparator));
main_menu_hbox->add_child(label_gridsize);
//label_gridsize->set_text("HELLO TEST");

context_menu_panel = memnew(PanelContainer);
context_menu_hbox = memnew(HBoxContainer);
context_menu_panel->add_child(context_menu_hbox);
Expand Down Expand Up @@ -6321,6 +6309,20 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) {
divide_grid_step_shortcut = ED_SHORTCUT("canvas_item_editor/divide_grid_step", TTR("Divide grid step by 2"), KEY_KP_DIVIDE);
pan_view_shortcut = ED_SHORTCUT("canvas_item_editor/pan_view", TTR("Pan View"), KEY_SPACE);


// Grid size preview

menu_spacer = memnew(Control);
menu_spacer->set_h_size_flags(Control::SIZE_EXPAND_FILL);
main_menu_hbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
main_menu_hbox->add_child(menu_spacer);

label_gridsize = memnew(Label);
label_gridsize->set_tooltip("Grid Size");
main_menu_hbox->add_child(memnew(VSeparator));
main_menu_hbox->add_child(label_gridsize);


skeleton_menu->get_popup()->set_item_checked(skeleton_menu->get_popup()->get_item_index(SKELETON_SHOW_BONES), true);
singleton = this;

Expand Down
53 changes: 52 additions & 1 deletion scene/gui/text_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4441,7 +4441,30 @@ int TextEdit::get_char_count() {
}

Size2 TextEdit::get_minimum_size() const {
return cache.style_normal->get_minimum_size();
if (!expand_to_text_height) {
return cache.style_normal->get_minimum_size();
} else {
Ref<StyleBox> style = get_stylebox("normal");
Ref<Font> font = get_font("font");

Size2 min_size;

// Minimum size of text.
int space_size = font->get_char_size(' ').x;
min_size.width = get_constant("minimum_spaces") * space_size;

// DO NOT SET WIDTH.

//if (expand_to_text_length) {
// Add a space because some fonts are too exact, and because cursor needs a bit more when at the end.
// min_size.width = MAX(min_size.width, get_line_width(0));
//}

min_size.height = get_line_height() * get_total_visible_rows();
// TODO: handle wrapped text lines. loop through each line and test if wrapped and add that to the count before multiplying.

return style->get_minimum_size() + min_size;
}
}

int TextEdit::_get_control_height() const {
Expand Down Expand Up @@ -5245,6 +5268,10 @@ void TextEdit::set_text(String p_text) {
cursor_set_line(0);
cursor_set_column(0);

if (expand_to_text_height) {
minimum_size_changed();
}

update();
setting_text = false;
};
Expand Down Expand Up @@ -6507,6 +6534,11 @@ void TextEdit::undo() {
cursor_set_line(undo_stack_pos->get().from_line, false);
cursor_set_column(undo_stack_pos->get().from_column);
}

if (expand_to_text_height) {
minimum_size_changed();
}

update();
}

Expand Down Expand Up @@ -6539,6 +6571,11 @@ void TextEdit::redo() {
cursor_set_line(undo_stack_pos->get().to_line, false);
cursor_set_column(undo_stack_pos->get().to_column);
undo_stack_pos = undo_stack_pos->next();

if (expand_to_text_height) {
minimum_size_changed();
}

update();
}

Expand Down Expand Up @@ -7411,6 +7448,15 @@ int TextEdit::get_line_height() const {
return get_row_height();
}

void TextEdit::set_expand_to_text_height(bool p_enabled) {
expand_to_text_height = p_enabled;
minimum_size_changed();
}

bool TextEdit::get_expand_to_text_height() const {
return expand_to_text_height;
}

void TextEdit::_bind_methods() {
ClassDB::bind_method(D_METHOD("_gui_input"), &TextEdit::_gui_input);
ClassDB::bind_method(D_METHOD("_scroll_moved"), &TextEdit::_scroll_moved);
Expand Down Expand Up @@ -7453,6 +7499,9 @@ void TextEdit::_bind_methods() {
ClassDB::bind_method(D_METHOD("cursor_set_column", "column", "adjust_viewport"), &TextEdit::cursor_set_column, DEFVAL(true));
ClassDB::bind_method(D_METHOD("cursor_set_line", "line", "adjust_viewport", "can_be_hidden", "wrap_index"), &TextEdit::cursor_set_line, DEFVAL(true), DEFVAL(true), DEFVAL(0));

ClassDB::bind_method(D_METHOD("set_expand_to_text_height", "enabled"), &TextEdit::set_expand_to_text_height);
ClassDB::bind_method(D_METHOD("get_expand_to_text_height"), &TextEdit::get_expand_to_text_height);

ClassDB::bind_method(D_METHOD("cursor_get_column"), &TextEdit::cursor_get_column);
ClassDB::bind_method(D_METHOD("cursor_get_line"), &TextEdit::cursor_get_line);
ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enable"), &TextEdit::cursor_set_blink_enabled);
Expand Down Expand Up @@ -7610,6 +7659,7 @@ void TextEdit::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "wrap_enabled"), "set_wrap_enabled", "is_wrap_enabled");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "scroll_vertical"), "set_v_scroll", "get_v_scroll");
ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal"), "set_h_scroll", "get_h_scroll");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_to_text_height"), "set_expand_to_text_height", "get_expand_to_text_height");

ADD_GROUP("Minimap", "minimap_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_draw"), "draw_minimap", "is_drawing_minimap");
Expand Down Expand Up @@ -7665,6 +7715,7 @@ TextEdit::TextEdit() {
fold_gutter_width = 0;
info_gutter_width = 0;
cache.info_gutter_width = 0;
expand_to_text_height = false;
set_default_cursor_shape(CURSOR_IBEAM);

indent_size = 4;
Expand Down
5 changes: 5 additions & 0 deletions scene/gui/text_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ class TextEdit : public Control {
void _text_changed_emit();
void _line_edited_from(int p_line);

bool expand_to_text_height;

void _push_current_op();

/* Line and character position. */
Expand Down Expand Up @@ -617,6 +619,9 @@ class TextEdit : public Control {
void insert_text_at_cursor(const String &p_text);
void insert_at(const String &p_text, int at);

void set_expand_to_text_height(bool p_enabled);
bool get_expand_to_text_height() const;

int get_line_count() const;

int get_line_width(int p_line, int p_wrap_index = -1) const;
Expand Down

0 comments on commit ff62c8d

Please sign in to comment.