diff options
-rw-r--r-- | doc/classes/PopupMenu.xml | 7 | ||||
-rw-r--r-- | doc/classes/Tree.xml | 1 | ||||
-rw-r--r-- | editor/create_dialog.cpp | 6 | ||||
-rw-r--r-- | editor/create_dialog.h | 2 | ||||
-rw-r--r-- | editor/plugins/text_control_editor_plugin.cpp | 144 | ||||
-rw-r--r-- | modules/SCsub | 45 | ||||
-rw-r--r-- | modules/modules_builders.py | 7 | ||||
-rw-r--r-- | scene/gui/option_button.cpp | 44 | ||||
-rw-r--r-- | scene/gui/popup_menu.cpp | 50 | ||||
-rw-r--r-- | scene/gui/popup_menu.h | 3 | ||||
-rw-r--r-- | scene/gui/tree.cpp | 28 | ||||
-rw-r--r-- | scene/gui/tree.h | 2 |
12 files changed, 184 insertions, 155 deletions
diff --git a/doc/classes/PopupMenu.xml b/doc/classes/PopupMenu.xml index eb1b0aada7..b316f822f0 100644 --- a/doc/classes/PopupMenu.xml +++ b/doc/classes/PopupMenu.xml @@ -331,6 +331,13 @@ [b]Note:[/b] The indices of items after the removed item will be shifted by one. </description> </method> + <method name="scroll_to_item"> + <return type="void" /> + <argument index="0" name="index" type="int" /> + <description> + Moves the scroll view to make the item at the given [code]index[/code] visible. + </description> + </method> <method name="set_current_index"> <return type="void" /> <argument index="0" name="index" type="int" /> diff --git a/doc/classes/Tree.xml b/doc/classes/Tree.xml index 766c740a2c..4b051c4938 100644 --- a/doc/classes/Tree.xml +++ b/doc/classes/Tree.xml @@ -240,6 +240,7 @@ <method name="scroll_to_item"> <return type="void" /> <argument index="0" name="item" type="TreeItem" /> + <argument index="1" name="center_on_item" type="bool" default="false" /> <description> Causes the [Tree] to jump to the specified [TreeItem]. </description> diff --git a/editor/create_dialog.cpp b/editor/create_dialog.cpp index 33e5100cb4..61ec8abacf 100644 --- a/editor/create_dialog.cpp +++ b/editor/create_dialog.cpp @@ -446,14 +446,14 @@ void CreateDialog::_notification(int p_what) { } } -void CreateDialog::select_type(const String &p_type) { +void CreateDialog::select_type(const String &p_type, bool p_center_on_item) { if (!search_options_types.has(p_type)) { return; } TreeItem *to_select = search_options_types[p_type]; to_select->select(0); - search_options->scroll_to_item(to_select); + search_options->scroll_to_item(to_select, p_center_on_item); if (EditorHelp::get_doc_data()->class_list.has(p_type) && !DTR(EditorHelp::get_doc_data()->class_list[p_type].brief_description).is_empty()) { // Display both class name and description, since the help bit may be displayed @@ -510,7 +510,7 @@ Variant CreateDialog::instance_selected() { void CreateDialog::_item_selected() { String name = get_selected_type(); - select_type(name); + select_type(name, false); } void CreateDialog::_hide_requested() { diff --git a/editor/create_dialog.h b/editor/create_dialog.h index f905160df3..a82c4db191 100644 --- a/editor/create_dialog.h +++ b/editor/create_dialog.h @@ -79,7 +79,7 @@ class CreateDialog : public ConfirmationDialog { void _sbox_input(const Ref<InputEvent> &p_ie); void _text_changed(const String &p_newtext); - void select_type(const String &p_type); + void select_type(const String &p_type, bool p_center_on_item = true); void _item_selected(); void _hide_requested(); diff --git a/editor/plugins/text_control_editor_plugin.cpp b/editor/plugins/text_control_editor_plugin.cpp index 12f0a298d3..4ce94176e5 100644 --- a/editor/plugins/text_control_editor_plugin.cpp +++ b/editor/plugins/text_control_editor_plugin.cpp @@ -110,7 +110,7 @@ void TextControlEditor::_update_styles_menu() { for (Map<String, String>::Element *E = fonts[name].front(); E; E = E->next()) { font_style_list->add_item(E->key()); } - } else { + } else if (font_list->get_selected() >= 0) { font_style_list->add_item("Default"); } @@ -123,9 +123,9 @@ void TextControlEditor::_update_styles_menu() { void TextControlEditor::_update_control() { if (!edited_controls.is_empty()) { - int font_selected = 0; + String font_selected; bool same_font = true; - int style_selected = 0; + String style_selected; bool same_style = true; int font_size = 0; bool same_font_size = true; @@ -136,26 +136,23 @@ void TextControlEditor::_update_control() { Color outline_color = Color{ 1.0f, 1.0f, 1.0f }; bool same_outline_color = true; - _update_fonts_menu(); - _update_styles_menu(); - int count = edited_controls.size(); for (int i = 0; i < count; ++i) { Control *edited_control = edited_controls[i]; - String edited_color; - String edited_font; - String edited_font_size; + StringName edited_color; + StringName edited_font; + StringName edited_font_size; // Get override names. - if (edited_control->is_class("RichTextLabel")) { - edited_color = "default_color"; - edited_font = "normal_font"; - edited_font_size = "normal_font_size"; + if (Object::cast_to<RichTextLabel>(edited_control)) { + edited_color = SNAME("default_color"); + edited_font = SNAME("normal_font"); + edited_font_size = SNAME("normal_font_size"); } else { - edited_color = "font_color"; - edited_font = "font"; - edited_font_size = "font_size"; + edited_color = SNAME("font_color"); + edited_font = SNAME("font"); + edited_font_size = SNAME("font_size"); } // Get font override. @@ -166,57 +163,40 @@ void TextControlEditor::_update_control() { if (font.is_valid()) { if (font->get_data_count() != 1) { - custom_font = font; if (i > 0) { - same_font = same_font && (font_selected == FONT_INFO_USER_CUSTOM); - same_style = same_style && (style_selected == 0); + same_font = same_font && (custom_font == font); } + custom_font = font; - font_selected = FONT_INFO_USER_CUSTOM; - style_selected = 0; + font_selected = TTR("[Custom Font]"); + same_style = false; } else { String name = font->get_data(0)->get_font_name(); String style = font->get_data(0)->get_font_style_name(); if (fonts.has(name) && fonts[name].has(style)) { - for (int j = 0; j < font_list->get_item_count(); j++) { - if (font_list->get_item_text(j) == name) { - if (i > 0) { - same_font = same_font && (j == font_selected); - } - - font_selected = j; - break; - } - } - for (int j = 0; j < font_style_list->get_item_count(); j++) { - if (font_style_list->get_item_text(j) == style) { - if (i > 0) { - same_style = same_style && (j == style_selected); - } - - style_selected = j; - break; - } + if (i > 0) { + same_font = same_font && (name == font_selected); + same_style = same_style && (style == style_selected); } + font_selected = name; + style_selected = style; } else { - custom_font = font; if (i > 0) { - same_font = same_font && (font_selected == FONT_INFO_USER_CUSTOM); - same_style = same_style && (style_selected == 0); + same_font = same_font && (custom_font == font); } + custom_font = font; - font_selected = FONT_INFO_USER_CUSTOM; - style_selected = 0; + font_selected = TTR("[Custom Font]"); + same_style = false; } } } else { if (i > 0) { - same_font = same_font && (font_selected == FONT_INFO_THEME_DEFAULT); - same_style = same_style && (style_selected == 0); + same_font = same_font && (font_selected == TTR("[Theme Default]")); } - font_selected = FONT_INFO_THEME_DEFAULT; - style_selected = 0; + font_selected = TTR("[Theme Default]"); + same_style = false; } int current_font_size = edited_control->get_theme_font_size(edited_font_size); @@ -235,19 +215,29 @@ void TextControlEditor::_update_control() { font_color = current_font_color; outline_color = current_outline_color; } - _update_fonts_menu(); if (same_font) { - font_list->select(font_selected); + for (int j = 0; j < font_list->get_item_count(); j++) { + if (font_list->get_item_text(j) == font_selected) { + font_list->select(j); + break; + } + } } else { + custom_font = Ref<Font>(); font_list->select(-1); } _update_styles_menu(); if (same_style) { - font_style_list->select(style_selected); + for (int j = 0; j < font_style_list->get_item_count(); j++) { + if (font_style_list->get_item_text(j) == style_selected) { + font_style_list->select(j); + break; + } + } } else { - font_list->select(-1); + font_style_list->select(-1); } // Get other theme overrides. @@ -282,6 +272,7 @@ void TextControlEditor::_update_control() { } void TextControlEditor::_font_selected(int p_id) { + _update_styles_menu(); _set_font(); } @@ -301,11 +292,11 @@ void TextControlEditor::_set_font() { for (int i = 0; i < count; ++i) { Control *edited_control = edited_controls[i]; - String edited_font; - if (edited_control->is_class("RichTextLabel")) { - edited_font = "normal_font"; + StringName edited_font; + if (Object::cast_to<RichTextLabel>(edited_control)) { + edited_font = SNAME("normal_font"); } else { - edited_font = "font"; + edited_font = SNAME("font"); } if (font_list->get_selected_id() == FONT_INFO_THEME_DEFAULT) { @@ -314,14 +305,13 @@ void TextControlEditor::_set_font() { } else if (font_list->get_selected_id() == FONT_INFO_USER_CUSTOM) { // Restore "custom_font". ur->add_do_method(edited_control, "add_theme_font_override", edited_font, custom_font); - } else { + } else if (font_list->get_selected() >= 0) { // Load new font resource using selected name and style. String name = font_list->get_item_text(font_list->get_selected()); String style = font_style_list->get_item_text(font_style_list->get_selected()); if (style.is_empty()) { style = "Default"; } - if (fonts.has(name)) { Ref<FontData> fd = ResourceLoader::load(fonts[name][style]); if (fd.is_valid()) { @@ -358,11 +348,11 @@ void TextControlEditor::_font_size_selected(double p_size) { for (int i = 0; i < count; ++i) { Control *edited_control = edited_controls[i]; - String edited_font_size; - if (edited_control->is_class("RichTextLabel")) { - edited_font_size = "normal_font_size"; + StringName edited_font_size; + if (Object::cast_to<RichTextLabel>(edited_control)) { + edited_font_size = SNAME("normal_font_size"); } else { - edited_font_size = "font_size"; + edited_font_size = SNAME("font_size"); } ur->add_do_method(edited_control, "add_theme_font_size_override", edited_font_size, p_size); @@ -417,11 +407,11 @@ void TextControlEditor::_font_color_changed(const Color &p_color) { for (int i = 0; i < count; ++i) { Control *edited_control = edited_controls[i]; - String edited_color; - if (edited_control->is_class("RichTextLabel")) { - edited_color = "default_color"; + StringName edited_color; + if (Object::cast_to<RichTextLabel>(edited_control)) { + edited_color = SNAME("default_color"); } else { - edited_color = "font_color"; + edited_color = SNAME("font_color"); } ur->add_do_method(edited_control, "add_theme_color_override", edited_color, p_color); @@ -476,19 +466,19 @@ void TextControlEditor::_clear_formatting() { for (int i = 0; i < count; ++i) { Control *edited_control = edited_controls[i]; - String edited_color; - String edited_font; - String edited_font_size; + StringName edited_color; + StringName edited_font; + StringName edited_font_size; // Get override names. - if (edited_control->is_class("RichTextLabel")) { - edited_color = "default_color"; - edited_font = "normal_font"; - edited_font_size = "normal_font_size"; + if (Object::cast_to<RichTextLabel>(edited_control)) { + edited_color = SNAME("default_color"); + edited_font = SNAME("normal_font"); + edited_font_size = SNAME("normal_font_size"); } else { - edited_color = "font_color"; - edited_font = "font"; - edited_font_size = "font_size"; + edited_color = SNAME("font_color"); + edited_font = SNAME("font"); + edited_font_size = SNAME("font_size"); } ur->add_do_method(edited_control, "begin_bulk_theme_override"); diff --git a/modules/SCsub b/modules/SCsub index 5ff4623743..fcc01e2c1b 100644 --- a/modules/SCsub +++ b/modules/SCsub @@ -22,36 +22,45 @@ env.CommandNoCache( ), ) -# Header to be included in `tests/test_main.cpp` to run module-specific tests. -if env["tests"]: - env.Depends("modules_tests.gen.h", Value(env.module_list)) - env.CommandNoCache( - "modules_tests.gen.h", - Value(env.module_list), - env.Run( - modules_builders.generate_modules_tests, - "Generating modules tests header.", - # NOTE: No need to run in subprocess since this is still executed serially. - subprocess=False, - ), - ) - env.AlwaysBuild("modules_tests.gen.h") vs_sources = [] +test_headers = [] # libmodule_<name>.a for each active module. for name, path in env.module_list.items(): env.modules_sources = [] - if not os.path.isabs(path): - SConscript(name + "/SCsub") # Built-in. - else: - SConscript(path + "/SCsub") # Custom. + # Name for built-in modules, (absolute) path for custom ones. + base_path = path if os.path.isabs(path) else name + SConscript(base_path + "/SCsub") lib = env_modules.add_library("module_%s" % name, env.modules_sources) env.Prepend(LIBS=[lib]) if env["vsproj"]: vs_sources += env.modules_sources + if env["tests"]: + # Lookup potential headers in `tests` subfolder. + import glob + + module_tests = sorted(glob.glob(os.path.join(base_path, "tests", "*.h"))) + if module_tests != []: + test_headers += module_tests + + +# Generate header to be included in `tests/test_main.cpp` to run module-specific tests. +if env["tests"]: + env.Depends("modules_tests.gen.h", test_headers) + env.CommandNoCache( + "modules_tests.gen.h", + test_headers, + env.Run( + modules_builders.generate_modules_tests, + "Generating modules tests header.", + # NOTE: No need to run in subprocess since this is still executed serially. + subprocess=False, + ), + ) + # libmodules.a with only register_module_types. # Must be last so that all libmodule_<name>.a libraries are on the right side # in the linker command. diff --git a/modules/modules_builders.py b/modules/modules_builders.py index 2243162555..13d5a2075a 100644 --- a/modules/modules_builders.py +++ b/modules/modules_builders.py @@ -14,13 +14,10 @@ def generate_modules_enabled(target, source, env): def generate_modules_tests(target, source, env): import os - import glob with open(target[0].path, "w") as f: - for name, path in env.module_list.items(): - headers = glob.glob(os.path.join(path, "tests", "*.h")) - for h in headers: - f.write('#include "%s"\n' % (os.path.normpath(h))) + for header in source: + f.write('#include "%s"\n' % (os.path.normpath(header.path))) if __name__ == "__main__": diff --git a/scene/gui/option_button.cpp b/scene/gui/option_button.cpp index 31f3b306b7..ad2434cd8b 100644 --- a/scene/gui/option_button.cpp +++ b/scene/gui/option_button.cpp @@ -118,6 +118,11 @@ void OptionButton::_notification(int p_what) { bool OptionButton::_set(const StringName &p_name, const Variant &p_value) { Vector<String> components = String(p_name).split("/", true, 2); if (components.size() >= 2 && components[0] == "popup") { + String property = components[2]; + if (property != "text" && property != "icon" && property != "id" && property != "disabled" && property != "separator") { + return false; + } + bool valid; popup->set(String(p_name).trim_prefix("popup/"), p_value, &valid); @@ -136,6 +141,11 @@ bool OptionButton::_set(const StringName &p_name, const Variant &p_value) { bool OptionButton::_get(const StringName &p_name, Variant &r_ret) const { Vector<String> components = String(p_name).split("/", true, 2); if (components.size() >= 2 && components[0] == "popup") { + String property = components[2]; + if (property != "text" && property != "icon" && property != "id" && property != "disabled" && property != "separator") { + return false; + } + bool valid; r_ret = popup->get(String(p_name).trim_prefix("popup/"), &valid); return valid; @@ -151,14 +161,6 @@ void OptionButton::_get_property_list(List<PropertyInfo> *p_list) const { pi.usage &= ~(popup->get_item_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0); p_list->push_back(pi); - pi = PropertyInfo(Variant::INT, vformat("popup/item_%d/checkable", i), PROPERTY_HINT_ENUM, "No,As checkbox,As radio button"); - pi.usage &= ~(!popup->is_item_checkable(i) ? PROPERTY_USAGE_STORAGE : 0); - p_list->push_back(pi); - - pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/checked", i)); - pi.usage &= ~(!popup->is_item_checked(i) ? PROPERTY_USAGE_STORAGE : 0); - p_list->push_back(pi); - pi = PropertyInfo(Variant::INT, vformat("popup/item_%d/id", i), PROPERTY_HINT_RANGE, "0,10,1,or_greater"); p_list->push_back(pi); @@ -186,10 +188,13 @@ void OptionButton::pressed() { popup->set_size(Size2(size.width, 0)); // If not triggered by the mouse, start the popup with the checked item selected. - if (popup->get_item_count() > 0 && - ((get_action_mode() == ActionMode::ACTION_MODE_BUTTON_PRESS && Input::get_singleton()->is_action_just_pressed("ui_accept")) || - (get_action_mode() == ActionMode::ACTION_MODE_BUTTON_RELEASE && Input::get_singleton()->is_action_just_released("ui_accept")))) { - popup->set_current_index(current > -1 ? current : 0); + if (popup->get_item_count() > 0) { + if ((get_action_mode() == ActionMode::ACTION_MODE_BUTTON_PRESS && Input::get_singleton()->is_action_just_pressed("ui_accept")) || + (get_action_mode() == ActionMode::ACTION_MODE_BUTTON_RELEASE && Input::get_singleton()->is_action_just_released("ui_accept"))) { + popup->set_current_index(current > -1 ? current : 0); + } else { + popup->scroll_to_item(current > -1 ? current : 0); + } } popup->popup(); @@ -267,7 +272,20 @@ bool OptionButton::is_item_disabled(int p_idx) const { void OptionButton::set_item_count(int p_count) { ERR_FAIL_COND(p_count < 0); + + int count_old = get_item_count(); + if (p_count == count_old) { + return; + } + popup->set_item_count(p_count); + + if (p_count > count_old) { + for (int i = count_old; i < p_count; i++) { + popup->set_item_as_radio_checkable(i, true); + } + } + notify_property_list_changed(); } @@ -297,7 +315,7 @@ void OptionButton::_select(int p_which, bool p_emit) { current = NONE_SELECTED; set_text(""); - set_icon(NULL); + set_icon(nullptr); } else { ERR_FAIL_INDEX(p_which, popup->get_item_count()); diff --git a/scene/gui/popup_menu.cpp b/scene/gui/popup_menu.cpp index 6d43bbdcf3..6e0f1c5198 100644 --- a/scene/gui/popup_menu.cpp +++ b/scene/gui/popup_menu.cpp @@ -108,7 +108,6 @@ Size2 PopupMenu::_get_contents_minimum_size() const { int PopupMenu::_get_item_height(int p_item) const { ERR_FAIL_INDEX_V(p_item, items.size(), 0); - ERR_FAIL_COND_V(p_item < 0, 0); int icon_height = items[p_item].get_icon_size().height; if (items[p_item].checkable_type && !items[p_item].separator) { @@ -141,23 +140,6 @@ int PopupMenu::_get_items_total_height() const { return items_total_height - vsep; } -void PopupMenu::_scroll_to_item(int p_item) { - ERR_FAIL_INDEX(p_item, items.size()); - ERR_FAIL_COND(p_item < 0); - - // Scroll item into view (upwards) - if (items[p_item]._ofs_cache < -control->get_position().y) { - int amnt_over = items[p_item]._ofs_cache + control->get_position().y; - scroll_container->set_v_scroll(scroll_container->get_v_scroll() + amnt_over); - } - - // Scroll item into view (downwards) - if (items[p_item]._ofs_cache + items[p_item]._height_cache > -control->get_position().y + scroll_container->get_size().height) { - int amnt_over = items[p_item]._ofs_cache + items[p_item]._height_cache + control->get_position().y - scroll_container->get_size().height; - scroll_container->set_v_scroll(scroll_container->get_v_scroll() + amnt_over); - } -} - int PopupMenu::_get_mouse_over(const Point2 &p_over) const { if (p_over.x < 0 || p_over.x >= get_size().width) { return -1; @@ -276,7 +258,7 @@ void PopupMenu::gui_input(const Ref<InputEvent> &p_event) { if (!items[i].separator && !items[i].disabled) { mouse_over = i; emit_signal(SNAME("id_focused"), i); - _scroll_to_item(i); + scroll_to_item(i); control->update(); set_input_as_handled(); match_found = true; @@ -290,7 +272,7 @@ void PopupMenu::gui_input(const Ref<InputEvent> &p_event) { if (!items[i].separator && !items[i].disabled) { mouse_over = i; emit_signal(SNAME("id_focused"), i); - _scroll_to_item(i); + scroll_to_item(i); control->update(); set_input_as_handled(); break; @@ -308,7 +290,7 @@ void PopupMenu::gui_input(const Ref<InputEvent> &p_event) { if (!items[i].separator && !items[i].disabled) { mouse_over = i; emit_signal(SNAME("id_focused"), i); - _scroll_to_item(i); + scroll_to_item(i); control->update(); set_input_as_handled(); match_found = true; @@ -322,7 +304,7 @@ void PopupMenu::gui_input(const Ref<InputEvent> &p_event) { if (!items[i].separator && !items[i].disabled) { mouse_over = i; emit_signal(SNAME("id_focused"), i); - _scroll_to_item(i); + scroll_to_item(i); control->update(); set_input_as_handled(); break; @@ -472,7 +454,7 @@ void PopupMenu::gui_input(const Ref<InputEvent> &p_event) { if (items[i].text.findn(search_string) == 0) { mouse_over = i; emit_signal(SNAME("id_focused"), i); - _scroll_to_item(i); + scroll_to_item(i); control->update(); set_input_as_handled(); break; @@ -1324,7 +1306,7 @@ bool PopupMenu::is_item_shortcut_disabled(int p_idx) const { void PopupMenu::set_current_index(int p_idx) { ERR_FAIL_INDEX(p_idx, items.size()); mouse_over = p_idx; - _scroll_to_item(mouse_over); + scroll_to_item(mouse_over); control->update(); } @@ -1352,6 +1334,20 @@ int PopupMenu::get_item_count() const { return items.size(); } +void PopupMenu::scroll_to_item(int p_item) { + ERR_FAIL_INDEX(p_item, items.size()); + + // Scroll item into view (upwards). + if (items[p_item]._ofs_cache - scroll_container->get_v_scroll() < -control->get_position().y) { + scroll_container->set_v_scroll(items[p_item]._ofs_cache + control->get_position().y); + } + + // Scroll item into view (downwards). + if (items[p_item]._ofs_cache + items[p_item]._height_cache - scroll_container->get_v_scroll() > -control->get_position().y + scroll_container->get_size().height) { + scroll_container->set_v_scroll(items[p_item]._ofs_cache + items[p_item]._height_cache + control->get_position().y); + } +} + bool PopupMenu::activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only) { Key code = Key::NONE; Ref<InputEventKey> k = p_event; @@ -1625,7 +1621,7 @@ bool PopupMenu::_set(const StringName &p_name, const Variant &p_value) { } else if (property == "id") { set_item_id(item_index, p_value); return true; - } else if (components[1] == "disabled") { + } else if (property == "disabled") { set_item_disabled(item_index, p_value); return true; } else if (property == "separator") { @@ -1698,7 +1694,7 @@ bool PopupMenu::_get(const StringName &p_name, Variant &r_ret) const { } else if (property == "id") { r_ret = get_item_id(item_index); return true; - } else if (components[1] == "disabled") { + } else if (property == "disabled") { r_ret = is_item_disabled(item_index); return true; } else if (property == "separator") { @@ -1804,6 +1800,8 @@ void PopupMenu::_bind_methods() { ClassDB::bind_method(D_METHOD("set_item_count", "count"), &PopupMenu::set_item_count); ClassDB::bind_method(D_METHOD("get_item_count"), &PopupMenu::get_item_count); + ClassDB::bind_method(D_METHOD("scroll_to_item", "index"), &PopupMenu::scroll_to_item); + ClassDB::bind_method(D_METHOD("remove_item", "index"), &PopupMenu::remove_item); ClassDB::bind_method(D_METHOD("add_separator", "label", "id"), &PopupMenu::add_separator, DEFVAL(String()), DEFVAL(-1)); diff --git a/scene/gui/popup_menu.h b/scene/gui/popup_menu.h index 7c2212d82d..5ce55209d4 100644 --- a/scene/gui/popup_menu.h +++ b/scene/gui/popup_menu.h @@ -103,7 +103,6 @@ class PopupMenu : public Popup { int _get_item_height(int p_item) const; int _get_items_total_height() const; - void _scroll_to_item(int p_item); void _shape_item(int p_item); @@ -218,6 +217,8 @@ public: void set_item_count(int p_count); int get_item_count() const; + void scroll_to_item(int p_item); + bool activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only = false); void activate_item(int p_item); diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp index e33ce0b017..9a6c87276f 100644 --- a/scene/gui/tree.cpp +++ b/scene/gui/tree.cpp @@ -4409,21 +4409,29 @@ Point2 Tree::get_scroll() const { return ofs; } -void Tree::scroll_to_item(TreeItem *p_item) { +void Tree::scroll_to_item(TreeItem *p_item, bool p_center_on_item) { if (!is_visible_in_tree()) { - // hack to work around crash in get_item_rect() if Tree is not in tree. - return; + return; // Hack to work around crash in get_item_rect() if Tree is not in tree. } - // make sure the scrollbar min and max are up to date with latest changes. update_scrollbars(); - const Rect2 r = get_item_rect(p_item); + const real_t tree_height = get_size().y; + const Rect2 item_rect = get_item_rect(p_item); + const real_t item_y = item_rect.position.y; + const real_t item_height = item_rect.size.y + cache.vseparation; - if (r.position.y <= v_scroll->get_value()) { - v_scroll->set_value(r.position.y); - } else if (r.position.y + r.size.y + 2 * cache.vseparation > v_scroll->get_value() + get_size().y) { - v_scroll->set_value(r.position.y + r.size.y + 2 * cache.vseparation - get_size().y); + if (p_center_on_item) { + v_scroll->set_value(item_y - (tree_height - item_height) / 2.0f); + } else { + if (item_y < v_scroll->get_value()) { + v_scroll->set_value(item_y); + } else { + const real_t new_position = item_y + item_height - tree_height; + if (new_position > v_scroll->get_value()) { + v_scroll->set_value(new_position); + } + } } } @@ -4868,7 +4876,7 @@ void Tree::_bind_methods() { ClassDB::bind_method(D_METHOD("get_column_title_language", "column"), &Tree::get_column_title_language); ClassDB::bind_method(D_METHOD("get_scroll"), &Tree::get_scroll); - ClassDB::bind_method(D_METHOD("scroll_to_item", "item"), &Tree::scroll_to_item); + ClassDB::bind_method(D_METHOD("scroll_to_item", "item", "center_on_item"), &Tree::scroll_to_item, DEFVAL(false)); ClassDB::bind_method(D_METHOD("set_h_scroll_enabled", "h_scroll"), &Tree::set_h_scroll_enabled); ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &Tree::is_h_scroll_enabled); diff --git a/scene/gui/tree.h b/scene/gui/tree.h index c24763a0e4..255a4f0576 100644 --- a/scene/gui/tree.h +++ b/scene/gui/tree.h @@ -682,7 +682,7 @@ public: TreeItem *get_item_with_text(const String &p_find) const; Point2 get_scroll() const; - void scroll_to_item(TreeItem *p_item); + void scroll_to_item(TreeItem *p_item, bool p_center_on_item = false); void set_h_scroll_enabled(bool p_enable); bool is_h_scroll_enabled() const; void set_v_scroll_enabled(bool p_enable); |