Compare commits

...

3 Commits

Author SHA1 Message Date
Alexander Montag ffe344d166 Make TreeSearch independent, remove unnecessary draw bind 2024-09-27 16:07:42 +00:00
Alexander Montag 9a1641e8ab Move TreeSearch::update_tree in TaskTree from update_tree
Catches all cases where the tree is modified. Also those, where for example the tab is switched.
2024-09-27 15:24:14 +00:00
Alexander Montag 47706e9480 Make TreeSearch::search_panel private and fix nulltpr 2024-09-27 17:08:04 +02:00
4 changed files with 44 additions and 25 deletions

View File

@ -47,6 +47,12 @@ TreeItem *TaskTree::_create_tree(const Ref<BTTask> &p_task, TreeItem *p_parent,
_create_tree(p_task->get_child(i), item); _create_tree(p_task->get_child(i), item);
} }
_update_item(item); _update_item(item);
// update TreeSearch if root task was created
if (tree->get_root() == item){
tree_search->update_search(tree);
}
return item; return item;
} }
@ -126,7 +132,6 @@ void TaskTree::_update_tree() {
for (const Ref<BTTask> &task : selection) { for (const Ref<BTTask> &task : selection) {
add_selection(task); add_selection(task);
} }
tree_search->update_search(tree);
} }
TreeItem *TaskTree::_find_item(const Ref<BTTask> &p_task) const { TreeItem *TaskTree::_find_item(const Ref<BTTask> &p_task) const {
@ -533,14 +538,15 @@ void TaskTree::_notification(int p_what) {
tree->connect("multi_selected", callable_mp(this, &TaskTree::_on_item_selected).unbind(3), CONNECT_DEFERRED); tree->connect("multi_selected", callable_mp(this, &TaskTree::_on_item_selected).unbind(3), CONNECT_DEFERRED);
tree->connect("item_activated", callable_mp(this, &TaskTree::_on_item_activated)); tree->connect("item_activated", callable_mp(this, &TaskTree::_on_item_activated));
tree->connect("item_collapsed", callable_mp(this, &TaskTree::_on_item_collapsed)); tree->connect("item_collapsed", callable_mp(this, &TaskTree::_on_item_collapsed));
tree_search->search_panel->connect("update_requested", callable_mp(this, &TaskTree::_update_tree)); tree_search_panel->connect("update_requested", callable_mp(this, &TaskTree::_update_tree));
tree_search->search_panel->connect("visibility_changed", callable_mp(this, &TaskTree::_update_tree)); tree_search_panel->connect("visibility_changed", callable_mp(this, &TaskTree::_update_tree));
} break; } break;
case NOTIFICATION_THEME_CHANGED: { case NOTIFICATION_THEME_CHANGED: {
_do_update_theme_item_cache(); _do_update_theme_item_cache();
_update_tree(); _update_tree();
} break; } break;
} }
} }
void TaskTree::_bind_methods() { void TaskTree::_bind_methods() {
@ -568,7 +574,8 @@ void TaskTree::_bind_methods() {
} }
void TaskTree::tree_search_show_and_focus() { void TaskTree::tree_search_show_and_focus() {
tree_search->search_panel->show_and_focus(); ERR_FAIL_NULL(tree_search);
tree_search_panel->show_and_focus();
} }
TaskTree::TaskTree() { TaskTree::TaskTree() {
@ -595,8 +602,9 @@ TaskTree::TaskTree() {
tree->set_drag_forwarding(callable_mp(this, &TaskTree::_get_drag_data_fw), callable_mp(this, &TaskTree::_can_drop_data_fw), callable_mp(this, &TaskTree::_drop_data_fw)); tree->set_drag_forwarding(callable_mp(this, &TaskTree::_get_drag_data_fw), callable_mp(this, &TaskTree::_can_drop_data_fw), callable_mp(this, &TaskTree::_drop_data_fw));
tree_search.instantiate(); tree_search_panel = memnew(TreeSearchPanel);
vbox_container->add_child(tree_search->search_panel); tree_search = Ref(memnew(TreeSearch(tree_search_panel)));
vbox_container->add_child(tree_search_panel);
} }
TaskTree::~TaskTree() { TaskTree::~TaskTree() {

View File

@ -46,7 +46,9 @@ private:
bool editable; bool editable;
bool updating_tree; bool updating_tree;
HashMap<RECT_CACHE_KEY, Rect2> probability_rect_cache; HashMap<RECT_CACHE_KEY, Rect2> probability_rect_cache;
Ref<TreeSearch> tree_search; Ref<TreeSearch> tree_search;
TreeSearchPanel *tree_search_panel;
struct ThemeCache { struct ThemeCache {
Ref<Font> comment_font; Ref<Font> comment_font;
@ -109,7 +111,6 @@ public:
bool selected_has_probability() const; bool selected_has_probability() const;
virtual bool editor_can_reload_from_file() { return false; } virtual bool editor_can_reload_from_file() { return false; }
TaskTree(); TaskTree();
~TaskTree(); ~TaskTree();

View File

@ -13,7 +13,6 @@
#include "tree_search.h" #include "tree_search.h"
#include "../bt/behavior_tree.h"
#include "../util/limbo_compat.h" // for edscale #include "../util/limbo_compat.h" // for edscale
#include "../util/limbo_string_names.h" #include "../util/limbo_string_names.h"
#include "../util/limbo_utility.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); 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 // -- this is necessary because of the modularity of this implementation
// cache render properties of entry // 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) // custom draw callback for highlighting (bind the parent_drw_method to this)
void TreeSearch::_draw_highlight_item(TreeItem *p_tree_item, Rect2 p_rect, String p_search_mask, Callable p_parent_draw_method) { void TreeSearch::_draw_highlight_item(TreeItem *p_tree_item, Rect2 p_rect, Callable p_parent_draw_method) {
if (!p_tree_item) if (!p_tree_item){
return; return;
}
// call any parent draw methods such as for probability FIRST. // call any parent draw methods such as for probability FIRST.
p_parent_draw_method.call(p_tree_item, p_rect); 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 // substring size
String string_full = p_tree_item->get_text(0); 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); 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); 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<TreeItem *> TreeSearch::_find_matching_entries(TreeItem *p_tree_item, const String &p_search_mask, Vector<TreeItem *> &p_accum) { Vector<TreeItem *> TreeSearch::_find_matching_entries(TreeItem *p_tree_item, const String &p_search_mask, Vector<TreeItem *> &p_accum) {
if (!p_tree_item) if (!p_tree_item)
return p_accum; return p_accum;
@ -367,15 +372,14 @@ TreeSearch::StringSearchIndices TreeSearch::_substring_bounds(const String &p_se
void TreeSearch::_select_item(TreeItem *p_item) { void TreeSearch::_select_item(TreeItem *p_item) {
if (!p_item) if (!p_item)
return; return;
ERR_FAIL_COND(!tree_reference || p_item->get_tree() != tree_reference);
// first unfold ancestors // first unfold ancestors
TreeItem *ancestor = p_item->get_parent(); TreeItem *ancestor = p_item->get_parent();
ancestor = ancestor->get_parent();
while (ancestor) { while (ancestor) {
ancestor->set_collapsed(false); ancestor->set_collapsed(false);
ancestor = ancestor->get_parent(); ancestor = ancestor->get_parent();
} }
//then scroll to [item] //then scroll to [item]
tree_reference->scroll_to_item(p_item); tree_reference->scroll_to_item(p_item);
@ -476,10 +480,11 @@ void TreeSearch::update_search(Tree *p_tree) {
if (search_mode == TreeSearchMode::FILTER) { if (search_mode == TreeSearchMode::FILTER) {
_filter_tree(search_mask); _filter_tree(search_mask);
} }
} }
TreeSearch::TreeSearch() { TreeSearch::TreeSearch(TreeSearchPanel *p_search_panel) {
search_panel = memnew(TreeSearchPanel); search_panel = p_search_panel;
} }
/* !TreeSearch */ /* !TreeSearch */

View File

@ -14,15 +14,13 @@
#ifndef TREE_SEARCH_H #ifndef TREE_SEARCH_H
#define TREE_SEARCH_H #define TREE_SEARCH_H
#include "../bt/tasks/bt_task.h" // for tree item parsing
#ifdef LIMBOAI_GDEXTENSION #ifdef LIMBOAI_GDEXTENSION
#include <godot_cpp/classes/check_box.hpp> #include <godot_cpp/classes/check_box.hpp>
#include <godot_cpp/classes/h_flow_container.hpp> #include <godot_cpp/classes/h_flow_container.hpp>
#include <godot_cpp/classes/label.hpp> #include <godot_cpp/classes/label.hpp>
#include <godot_cpp/classes/line_edit.hpp> #include <godot_cpp/classes/line_edit.hpp>
#include <godot_cpp/classes/tree.hpp> #include <godot_cpp/classes/tree.hpp>
#include <godot_cpp/templates/hash_map.hpp>
#endif // LIMBOAI_GDEXTENSION #endif // LIMBOAI_GDEXTENSION
#ifdef LIMBOAI_MODULE #ifdef LIMBOAI_MODULE
@ -31,6 +29,7 @@
#include "scene/gui/label.h" #include "scene/gui/label.h"
#include "scene/gui/line_edit.h" #include "scene/gui/line_edit.h"
#include "scene/gui/tree.h" #include "scene/gui/tree.h"
#include "core/templates/hash_map.h"
#endif // LIMBOAI_MODULE #endif // LIMBOAI_MODULE
using namespace godot; using namespace godot;
@ -81,21 +80,28 @@ private:
} }
}; };
Tree *tree_reference; TreeSearchPanel *search_panel;
// For TaskTree: These are updated when the tree is updated through TaskTree::_create_tree.
Tree *tree_reference;
Vector<TreeItem *> ordered_tree_items; Vector<TreeItem *> ordered_tree_items;
Vector<TreeItem *> matching_entries; Vector<TreeItem *> matching_entries;
HashMap<TreeItem *, int> number_matches; HashMap<TreeItem *, int> number_matches;
HashMap<TreeItem *, Callable> callable_cache; HashMap<TreeItem *, Callable> callable_cache;
// Update_search() calls these
void _filter_tree(const String &p_search_mask); void _filter_tree(const String &p_search_mask);
void _highlight_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_matching_entries(const String &p_search_mask);
void _update_ordered_tree_items(TreeItem *p_tree_item); void _update_ordered_tree_items(TreeItem *p_tree_item);
void _update_number_matches(); void _update_number_matches();
Vector<TreeItem *> _find_matching_entries(TreeItem *p_tree_item, const String &p_search_mask, Vector<TreeItem *> &p_buffer); Vector<TreeItem *> _find_matching_entries(TreeItem *p_tree_item, const String &p_search_mask, Vector<TreeItem *> &p_buffer);
String _get_search_mask();
StringSearchIndices _substring_bounds(const String &p_searchable, const String &p_search_mask) const; StringSearchIndices _substring_bounds(const String &p_searchable, const String &p_search_mask) const;
void _select_item(TreeItem *p_item); void _select_item(TreeItem *p_item);
@ -109,12 +115,11 @@ protected:
static void _bind_methods() {} static void _bind_methods() {}
public: public:
// we will add everything from TaskTree.h
void update_search(Tree *p_tree); void update_search(Tree *p_tree);
void on_item_edited(TreeItem *p_item); void on_item_edited(TreeItem *p_item);
TreeSearchPanel *search_panel;
TreeSearch(); TreeSearch(){ERR_FAIL_MSG("TreeSearch needs a TreeSearchPanel to work properly");}
TreeSearch(TreeSearchPanel * p_search_panel);
}; };
#endif // TREE_SEARCH_H #endif // TREE_SEARCH_H