From ffe344d16651307c7c44e1f29897c666a516006d Mon Sep 17 00:00:00 2001 From: Alexander Montag Date: Fri, 27 Sep 2024 16:07:42 +0000 Subject: [PATCH] Make TreeSearch independent, remove unnecessary draw bind --- editor/tree_search.cpp | 17 +++++++++++------ editor/tree_search.h | 15 +++++++++------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/editor/tree_search.cpp b/editor/tree_search.cpp index 4916ad7..3524065 100644 --- a/editor/tree_search.cpp +++ b/editor/tree_search.cpp @@ -13,7 +13,6 @@ #include "tree_search.h" -#include "../bt/behavior_tree.h" #include "../util/limbo_compat.h" // for edscale #include "../util/limbo_string_names.h" #include "../util/limbo_utility.h" @@ -179,7 +178,7 @@ void TreeSearch::_highlight_tree(const String &p_search_mask) { parent_draw_method = entry->get_custom_draw_callback(0); } - Callable draw_callback = callable_mp(this, &TreeSearch::_draw_highlight_item).bind(p_search_mask, parent_draw_method); + Callable draw_callback = callable_mp(this, &TreeSearch::_draw_highlight_item).bind(parent_draw_method); // -- this is necessary because of the modularity of this implementation // cache render properties of entry @@ -199,10 +198,11 @@ void TreeSearch::_highlight_tree(const String &p_search_mask) { } } -// custom draw callback for highlighting (bind 2) -void TreeSearch::_draw_highlight_item(TreeItem *p_tree_item, Rect2 p_rect, String p_search_mask, Callable p_parent_draw_method) { - if (!p_tree_item) +// custom draw callback for highlighting (bind the parent_drw_method to this) +void TreeSearch::_draw_highlight_item(TreeItem *p_tree_item, Rect2 p_rect, Callable p_parent_draw_method) { + if (!p_tree_item){ return; + } // call any parent draw methods such as for probability FIRST. p_parent_draw_method.call(p_tree_item, p_rect); @@ -221,7 +221,7 @@ void TreeSearch::_draw_highlight_item(TreeItem *p_tree_item, Rect2 p_rect, Strin // substring size String string_full = p_tree_item->get_text(0); - StringSearchIndices substring_idx = _substring_bounds(string_full, p_search_mask); + StringSearchIndices substring_idx = _substring_bounds(string_full, _get_search_mask()); String substring_match = string_full.substr(substring_idx.lower, substring_idx.upper - substring_idx.lower); Vector2 substring_match_size = font->get_string_size(substring_match, HORIZONTAL_ALIGNMENT_LEFT, -1.f, font_size); @@ -310,6 +310,11 @@ void TreeSearch::_update_number_matches() { } } +String TreeSearch::_get_search_mask() { + ERR_FAIL_COND_V(!search_panel, ""); + return search_panel->get_text(); +} + Vector TreeSearch::_find_matching_entries(TreeItem *p_tree_item, const String &p_search_mask, Vector &p_accum) { if (!p_tree_item) return p_accum; diff --git a/editor/tree_search.h b/editor/tree_search.h index 24ceae2..830c36c 100644 --- a/editor/tree_search.h +++ b/editor/tree_search.h @@ -14,15 +14,13 @@ #ifndef TREE_SEARCH_H #define TREE_SEARCH_H -#include "../bt/tasks/bt_task.h" // for tree item parsing - #ifdef LIMBOAI_GDEXTENSION #include #include #include #include #include - +#include #endif // LIMBOAI_GDEXTENSION #ifdef LIMBOAI_MODULE @@ -31,6 +29,7 @@ #include "scene/gui/label.h" #include "scene/gui/line_edit.h" #include "scene/gui/tree.h" +#include "core/templates/hash_map.h" #endif // LIMBOAI_MODULE using namespace godot; @@ -83,21 +82,26 @@ private: TreeSearchPanel *search_panel; - // These variables are updated every time the update_search method is called. + // For TaskTree: These are updated when the tree is updated through TaskTree::_create_tree. Tree *tree_reference; Vector ordered_tree_items; Vector matching_entries; HashMap number_matches; HashMap callable_cache; + // Update_search() calls these void _filter_tree(const String &p_search_mask); void _highlight_tree(const String &p_search_mask); - void _draw_highlight_item(TreeItem *p_tree_item, Rect2 p_rect, String p_search_mask, Callable p_parent_draw_method); + + // Custom draw-Callback (bind inherited Callable). + void _draw_highlight_item(TreeItem *p_tree_item, Rect2 p_rect, Callable p_parent_draw_method); + void _update_matching_entries(const String &p_search_mask); void _update_ordered_tree_items(TreeItem *p_tree_item); void _update_number_matches(); Vector _find_matching_entries(TreeItem *p_tree_item, const String &p_search_mask, Vector &p_buffer); + String _get_search_mask(); StringSearchIndices _substring_bounds(const String &p_searchable, const String &p_search_mask) const; void _select_item(TreeItem *p_item); @@ -111,7 +115,6 @@ protected: static void _bind_methods() {} public: - // we will add everything from TaskTree.h void update_search(Tree *p_tree); void on_item_edited(TreeItem *p_item);