Remember SearchInfo for each tab. Focus tree when *user* closes SearchPanel

This commit is contained in:
Alexander Montag 2024-09-30 16:13:36 +02:00
parent 20be87904a
commit 10c8f58ca9
No known key found for this signature in database
GPG Key ID: 07FE50B35A2B7524
6 changed files with 81 additions and 17 deletions

View File

@ -261,6 +261,10 @@ void LimboAIEditor::edit_bt(const Ref<BehaviorTree> &p_behavior_tree, bool p_for
p_behavior_tree->editor_set_section_unfold("blackboard_plan", true); p_behavior_tree->editor_set_section_unfold("blackboard_plan", true);
p_behavior_tree->notify_property_list_changed(); p_behavior_tree->notify_property_list_changed();
#endif // LIMBOAI_MODULE #endif // LIMBOAI_MODULE
// Remember current search info.
if (idx_history >= 0 && idx_history < history.size()) {
tab_search_context.insert(history[idx_history], task_tree->tree_search_get_search_info());
}
task_tree->load_bt(p_behavior_tree); task_tree->load_bt(p_behavior_tree);
@ -280,6 +284,18 @@ void LimboAIEditor::edit_bt(const Ref<BehaviorTree> &p_behavior_tree, bool p_for
task_tree->show(); task_tree->show();
task_palette->show(); task_palette->show();
// Restore search info from [tab_search_context].
if (idx_history >= 0 && idx_history < history.size()) {
// info for BehaviorTree available. Restore!
if (tab_search_context.has(history[idx_history])) {
task_tree->tree_search_set_search_info(tab_search_context[history[idx_history]]);
}
// new SearchContext.
else {
task_tree->tree_search_set_search_info(TreeSearch::SearchInfo());
}
}
_update_tabs(); _update_tabs();
} }

View File

@ -48,6 +48,7 @@
#ifdef LIMBOAI_GDEXTENSION #ifdef LIMBOAI_GDEXTENSION
#include "godot_cpp/classes/accept_dialog.hpp" #include "godot_cpp/classes/accept_dialog.hpp"
#include <godot_cpp/classes/config_file.hpp>
#include <godot_cpp/classes/control.hpp> #include <godot_cpp/classes/control.hpp>
#include <godot_cpp/classes/editor_plugin.hpp> #include <godot_cpp/classes/editor_plugin.hpp>
#include <godot_cpp/classes/editor_spin_slider.hpp> #include <godot_cpp/classes/editor_spin_slider.hpp>
@ -64,7 +65,6 @@
#include <godot_cpp/classes/texture2d.hpp> #include <godot_cpp/classes/texture2d.hpp>
#include <godot_cpp/variant/packed_string_array.hpp> #include <godot_cpp/variant/packed_string_array.hpp>
#include <godot_cpp/variant/variant.hpp> #include <godot_cpp/variant/variant.hpp>
#include <godot_cpp/classes/config_file.hpp>
using namespace godot; using namespace godot;
@ -143,6 +143,7 @@ private:
EditorLayout editor_layout; EditorLayout editor_layout;
Vector<Ref<BehaviorTree>> history; Vector<Ref<BehaviorTree>> history;
int idx_history; int idx_history;
HashMap<Ref<BehaviorTree>, TreeSearch::SearchInfo> tab_search_context;
bool updating_tabs = false; bool updating_tabs = false;
bool request_update_tabs = false; bool request_update_tabs = false;
HashSet<Ref<BehaviorTree>> dirty; HashSet<Ref<BehaviorTree>> dirty;

View File

@ -546,7 +546,6 @@ void TaskTree::_notification(int p_what) {
_update_tree(); _update_tree();
} break; } break;
} }
} }
void TaskTree::_bind_methods() { void TaskTree::_bind_methods() {
@ -573,11 +572,27 @@ void TaskTree::_bind_methods() {
PropertyInfo(Variant::INT, "type"))); PropertyInfo(Variant::INT, "type")));
} }
// TreeSearch API
void TaskTree::tree_search_show_and_focus() { void TaskTree::tree_search_show_and_focus() {
ERR_FAIL_NULL(tree_search); ERR_FAIL_NULL(tree_search);
tree_search_panel->show_and_focus(); tree_search_panel->set_visible(true);
tree_search_panel->focus_editor();
} }
TreeSearch::SearchInfo TaskTree::tree_search_get_search_info() const {
if (!tree_search.is_valid()) {
return TreeSearch::SearchInfo();
}
return tree_search_panel->get_search_info();
}
void TaskTree::tree_search_set_search_info(const TreeSearch::SearchInfo &p_search_info) {
ERR_FAIL_NULL(tree_search);
tree_search_panel->set_search_info(p_search_info);
}
// TreeSearch Api ^
TaskTree::TaskTree() { TaskTree::TaskTree() {
editable = true; editable = true;
updating_tree = false; updating_tree = false;

View File

@ -103,13 +103,16 @@ public:
Ref<BTTask> get_selected() const; Ref<BTTask> get_selected() const;
Vector<Ref<BTTask>> get_selected_tasks() const; Vector<Ref<BTTask>> get_selected_tasks() const;
void clear_selection(); void clear_selection();
void tree_search_show_and_focus();
Rect2 get_selected_probability_rect() const; Rect2 get_selected_probability_rect() const;
double get_selected_probability_weight() const; double get_selected_probability_weight() const;
double get_selected_probability_percent() const; double get_selected_probability_percent() const;
bool selected_has_probability() const; bool selected_has_probability() const;
// TreeSearch API
void tree_search_show_and_focus();
TreeSearch::SearchInfo tree_search_get_search_info() const;
void tree_search_set_search_info(const TreeSearch::SearchInfo &p_search_info);
virtual bool editor_can_reload_from_file() { return false; } virtual bool editor_can_reload_from_file() { return false; }
TaskTree(); TaskTree();

View File

@ -383,10 +383,11 @@ void TreeSearch::_select_next_match() {
_select_first_match(); _select_first_match();
} }
void TreeSearch::_on_search_panel_visibility_changed() { void TreeSearch::_on_search_panel_closed() {
if (tree_reference && !search_panel->is_visible()) { if (!tree_reference) {
tree_reference->grab_focus(); return;
} }
tree_reference->grab_focus();
} }
template <typename T> template <typename T>
@ -445,7 +446,7 @@ void TreeSearch::update_search(Tree *p_tree) {
TreeSearch::TreeSearch(TreeSearchPanel *p_search_panel) { TreeSearch::TreeSearch(TreeSearchPanel *p_search_panel) {
search_panel = p_search_panel; search_panel = p_search_panel;
search_panel->connect("text_submitted", callable_mp(this, &TreeSearch::_select_next_match)); search_panel->connect("text_submitted", callable_mp(this, &TreeSearch::_select_next_match));
search_panel->connect("visibility_changed", callable_mp(this, &TreeSearch::_on_search_panel_visibility_changed)); search_panel->connect("closed", callable_mp(this, &TreeSearch::_on_search_panel_closed));
} }
/* !TreeSearch */ /* !TreeSearch */
@ -491,8 +492,8 @@ void TreeSearchPanel::_notification(int p_what) {
case NOTIFICATION_READY: { case NOTIFICATION_READY: {
// close callbacks // close callbacks
close_button->connect("pressed", Callable(this, "set_visible").bind(false)); close_button->connect("pressed", Callable(this, "set_visible").bind(false));
close_button->connect("pressed", Callable(this, "emit_signal").bind("closed"));
close_button->set_shortcut(LW_GET_SHORTCUT("limbo_ai/hide_tree_search")); close_button->set_shortcut(LW_GET_SHORTCUT("limbo_ai/hide_tree_search"));
// search callbacks // search callbacks
Callable c_update_requested = Callable(this, "emit_signal").bind("update_requested"); Callable c_update_requested = Callable(this, "emit_signal").bind("update_requested");
Callable c_text_submitted = Callable((Object *)this, "emit_signal").bind("text_submitted"); Callable c_text_submitted = Callable((Object *)this, "emit_signal").bind("text_submitted");
@ -513,6 +514,7 @@ void TreeSearchPanel::_notification(int p_what) {
void TreeSearchPanel::_bind_methods() { void TreeSearchPanel::_bind_methods() {
ADD_SIGNAL(MethodInfo("update_requested")); ADD_SIGNAL(MethodInfo("update_requested"));
ADD_SIGNAL(MethodInfo("text_submitted")); ADD_SIGNAL(MethodInfo("text_submitted"));
ADD_SIGNAL(MethodInfo("closed"));
} }
TreeSearchPanel::TreeSearchPanel() { TreeSearchPanel::TreeSearchPanel() {
@ -534,8 +536,26 @@ String TreeSearchPanel::get_text() const {
return line_edit_search->get_text(); return line_edit_search->get_text();
} }
void TreeSearchPanel::show_and_focus() { TreeSearch::SearchInfo TreeSearchPanel::get_search_info() const {
set_visible(true); TreeSearch::SearchInfo result;
result.search_mask = get_text();
result.search_mode = get_search_mode();
result.visible = is_visible();
return result;
}
void TreeSearchPanel::set_search_info(TreeSearch::SearchInfo p_search_info) {
line_edit_search->set_text(p_search_info.search_mask);
check_button_filter_highlight->set_pressed(p_search_info.search_mode == TreeSearch::TreeSearchMode::FILTER);
set_visible(p_search_info.visible);
emit_signal("update_requested");
}
void TreeSearchPanel::set_text(const String &p_text) {
line_edit_search->set_text(p_text);
}
void TreeSearchPanel::focus_editor() {
line_edit_search->grab_focus(); line_edit_search->grab_focus();
} }

View File

@ -90,7 +90,7 @@ private:
void _select_first_match(); void _select_first_match();
void _select_next_match(); void _select_next_match();
void _on_search_panel_visibility_changed(); void _on_search_panel_closed();
// TODO: make p_vec ref `const` once Vector::bsearch is const. See: https://github.com/godotengine/godot/pull/90341 // TODO: make p_vec ref `const` once Vector::bsearch is const. See: https://github.com/godotengine/godot/pull/90341
template <typename T> template <typename T>
@ -105,6 +105,12 @@ public:
FILTER = 1 FILTER = 1
}; };
struct SearchInfo {
String search_mask;
TreeSearchMode search_mode;
bool visible;
};
void update_search(Tree *p_tree); void update_search(Tree *p_tree);
void notify_item_edited(TreeItem *p_item); void notify_item_edited(TreeItem *p_item);
@ -134,7 +140,10 @@ protected:
public: public:
TreeSearch::TreeSearchMode get_search_mode() const; TreeSearch::TreeSearchMode get_search_mode() const;
String get_text() const; String get_text() const;
void show_and_focus(); TreeSearch::SearchInfo get_search_info() const;
void set_search_info(TreeSearch::SearchInfo p_search_info);
void set_text(const String &p_text);
void focus_editor();
TreeSearchPanel(); TreeSearchPanel();
}; };