Compare commits

...

6 Commits

Author SHA1 Message Date
monxa 1e73b6d653
Merge ffe344d166 into 60a767032e 2024-09-27 16:07:51 +00:00
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
Serhii Snitsaruk 60a767032e
Merge pull request #226 from limbonaut/fix-hsm-exit-crash
Fix invalid access errors on exit in LimboHSM
2024-09-26 16:54:31 +02:00
Serhii Snitsaruk 60142b191d
Fix invalid access crash on exit in LimboHSM
Since #131, `LimboState::_exit()` became a source of potential crashes
if object references are used without a validity check. It's too easy
to miss this, which can lead to game crashing during runtime.

This fix reverts #131 change and proposes alternative approach of
re-activating root HSM upon tree entering if it was previously active.
Note that it's not an ideal solution, as some state will be lost upon
re-parenting: HSM exits and then re-activates and enters its initial state.
2024-09-22 13:57:15 +02:00
7 changed files with 60 additions and 30 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);
}
_update_item(item);
// update TreeSearch if root task was created
if (tree->get_root() == item){
tree_search->update_search(tree);
}
return item;
}
@ -126,7 +132,6 @@ void TaskTree::_update_tree() {
for (const Ref<BTTask> &task : selection) {
add_selection(task);
}
tree_search->update_search(tree);
}
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("item_activated", callable_mp(this, &TaskTree::_on_item_activated));
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->search_panel->connect("visibility_changed", callable_mp(this, &TaskTree::_update_tree));
tree_search_panel->connect("update_requested", callable_mp(this, &TaskTree::_update_tree));
tree_search_panel->connect("visibility_changed", callable_mp(this, &TaskTree::_update_tree));
} break;
case NOTIFICATION_THEME_CHANGED: {
_do_update_theme_item_cache();
_update_tree();
} break;
}
}
void TaskTree::_bind_methods() {
@ -568,7 +574,8 @@ void TaskTree::_bind_methods() {
}
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() {
@ -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_search.instantiate();
vbox_container->add_child(tree_search->search_panel);
tree_search_panel = memnew(TreeSearchPanel);
tree_search = Ref(memnew(TreeSearch(tree_search_panel)));
vbox_container->add_child(tree_search_panel);
}
TaskTree::~TaskTree() {

View File

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

View File

@ -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<TreeItem *> TreeSearch::_find_matching_entries(TreeItem *p_tree_item, const String &p_search_mask, Vector<TreeItem *> &p_accum) {
if (!p_tree_item)
return p_accum;
@ -367,15 +372,14 @@ TreeSearch::StringSearchIndices TreeSearch::_substring_bounds(const String &p_se
void TreeSearch::_select_item(TreeItem *p_item) {
if (!p_item)
return;
ERR_FAIL_COND(!tree_reference || p_item->get_tree() != tree_reference);
// first unfold ancestors
TreeItem *ancestor = p_item->get_parent();
ancestor = ancestor->get_parent();
while (ancestor) {
ancestor->set_collapsed(false);
ancestor = ancestor->get_parent();
}
//then scroll to [item]
tree_reference->scroll_to_item(p_item);
@ -476,10 +480,11 @@ void TreeSearch::update_search(Tree *p_tree) {
if (search_mode == TreeSearchMode::FILTER) {
_filter_tree(search_mask);
}
}
TreeSearch::TreeSearch() {
search_panel = memnew(TreeSearchPanel);
TreeSearch::TreeSearch(TreeSearchPanel *p_search_panel) {
search_panel = p_search_panel;
}
/* !TreeSearch */

View File

@ -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 <godot_cpp/classes/check_box.hpp>
#include <godot_cpp/classes/h_flow_container.hpp>
#include <godot_cpp/classes/label.hpp>
#include <godot_cpp/classes/line_edit.hpp>
#include <godot_cpp/classes/tree.hpp>
#include <godot_cpp/templates/hash_map.hpp>
#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;
@ -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 *> matching_entries;
HashMap<TreeItem *, int> number_matches;
HashMap<TreeItem *, Callable> 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<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;
void _select_item(TreeItem *p_item);
@ -109,12 +115,11 @@ 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);
TreeSearchPanel *search_panel;
TreeSearch();
TreeSearch(){ERR_FAIL_MSG("TreeSearch needs a TreeSearchPanel to work properly");}
TreeSearch(TreeSearchPanel * p_search_panel);
};
#endif // TREE_SEARCH_H

View File

@ -263,6 +263,21 @@ void LimboHSM::_validate_property(PropertyInfo &p_property) const {
void LimboHSM::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_POST_ENTER_TREE: {
if (was_active && is_root()) {
// Re-activate the root HSM if it was previously active.
// Typically, this happens when the node is re-entered scene repeatedly (e.g., re-parenting, pooling).
set_active(true);
}
} break;
case NOTIFICATION_EXIT_TREE: {
if (is_root()) {
// Remember active status for re-parenting and exit state machine
// to release resources and signal connections if active.
was_active = active;
if (is_active()) {
_exit();
}
}
} break;
case NOTIFICATION_PROCESS: {
_update(get_process_delta_time());

View File

@ -55,6 +55,7 @@ private:
LimboState *previous_active;
LimboState *next_active;
bool updating = false;
bool was_active = false;
HashMap<TransitionKey, Transition, TransitionKeyHasher> transitions;

View File

@ -190,11 +190,6 @@ void LimboState::_notification(int p_what) {
_update_blackboard_plan();
}
} break;
case NOTIFICATION_PREDELETE: {
if (is_active()) {
_exit();
}
} break;
}
}