1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-19 14:31:59 +00:00

Merge pull request #40140 from hinlopen/tree-scroll-center

This commit is contained in:
Rémi Verschelde
2022-02-08 13:43:19 +01:00
committed by GitHub
5 changed files with 24 additions and 15 deletions

View File

@@ -240,6 +240,7 @@
<method name="scroll_to_item"> <method name="scroll_to_item">
<return type="void" /> <return type="void" />
<argument index="0" name="item" type="TreeItem" /> <argument index="0" name="item" type="TreeItem" />
<argument index="1" name="center_on_item" type="bool" default="false" />
<description> <description>
Causes the [Tree] to jump to the specified [TreeItem]. Causes the [Tree] to jump to the specified [TreeItem].
</description> </description>

View File

@@ -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)) { if (!search_options_types.has(p_type)) {
return; return;
} }
TreeItem *to_select = search_options_types[p_type]; TreeItem *to_select = search_options_types[p_type];
to_select->select(0); 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()) { 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 // 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() { void CreateDialog::_item_selected() {
String name = get_selected_type(); String name = get_selected_type();
select_type(name); select_type(name, false);
} }
void CreateDialog::_hide_requested() { void CreateDialog::_hide_requested() {

View File

@@ -79,7 +79,7 @@ class CreateDialog : public ConfirmationDialog {
void _sbox_input(const Ref<InputEvent> &p_ie); void _sbox_input(const Ref<InputEvent> &p_ie);
void _text_changed(const String &p_newtext); 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 _item_selected();
void _hide_requested(); void _hide_requested();

View File

@@ -4409,21 +4409,29 @@ Point2 Tree::get_scroll() const {
return ofs; 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()) { if (!is_visible_in_tree()) {
// hack to work around crash in get_item_rect() if Tree is not in tree. return; // Hack to work around crash in get_item_rect() if Tree is not in tree.
return;
} }
// make sure the scrollbar min and max are up to date with latest changes.
update_scrollbars(); 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()) { if (p_center_on_item) {
v_scroll->set_value(r.position.y); v_scroll->set_value(item_y - (tree_height - item_height) / 2.0f);
} else if (r.position.y + r.size.y + 2 * cache.vseparation > v_scroll->get_value() + get_size().y) { } else {
v_scroll->set_value(r.position.y + r.size.y + 2 * cache.vseparation - get_size().y); 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_column_title_language", "column"), &Tree::get_column_title_language);
ClassDB::bind_method(D_METHOD("get_scroll"), &Tree::get_scroll); 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("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); ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &Tree::is_h_scroll_enabled);

View File

@@ -682,7 +682,7 @@ public:
TreeItem *get_item_with_text(const String &p_find) const; TreeItem *get_item_with_text(const String &p_find) const;
Point2 get_scroll() 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); void set_h_scroll_enabled(bool p_enable);
bool is_h_scroll_enabled() const; bool is_h_scroll_enabled() const;
void set_v_scroll_enabled(bool p_enable); void set_v_scroll_enabled(bool p_enable);