Address 2. review for TreeSearch

Remove redundant comment

Prune tab_search_context

Fix restore tab on `_tab_closed`

Add break statement

Pass callable by reference in _draw_highlight_item

Refactor _initialize_controls into constructor

Remove redundant if (!line_edit_search)-check
This commit is contained in:
Alexander Montag 2024-10-13 03:33:20 +00:00
parent 6776319472
commit 8c557f87f7
No known key found for this signature in database
GPG Key ID: 07FE50B35A2B7524
3 changed files with 47 additions and 47 deletions

View File

@ -262,7 +262,7 @@ void LimboAIEditor::edit_bt(const Ref<BehaviorTree> &p_behavior_tree, bool p_for
p_behavior_tree->notify_property_list_changed();
#endif // LIMBOAI_MODULE
// Remember current search info.
if (idx_history >= 0 && idx_history < history.size()) {
if (idx_history >= 0 && idx_history < history.size() && task_tree->get_bt() == history[idx_history]) {
tab_search_context.insert(history[idx_history], task_tree->tree_search_get_search_info());
}
@ -286,11 +286,9 @@ void LimboAIEditor::edit_bt(const Ref<BehaviorTree> &p_behavior_tree, bool p_for
// 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());
}
@ -819,7 +817,7 @@ void LimboAIEditor::_misc_option_selected(int p_id) {
} break;
case MISC_SEARCH_TREE: {
task_tree->tree_search_show_and_focus();
}
} break;
}
}
@ -1066,13 +1064,22 @@ void LimboAIEditor::_tab_closed(int p_tab) {
if (history_bt.is_valid() && history_bt->is_connected(LW_NAME(changed), callable_mp(this, &LimboAIEditor::_mark_as_dirty))) {
history_bt->disconnect(LW_NAME(changed), callable_mp(this, &LimboAIEditor::_mark_as_dirty));
}
if (tab_search_context.has(history_bt)) {
tab_search_context.erase(history_bt);
}
history.remove_at(p_tab);
idx_history = MIN(idx_history, history.size() - 1);
TreeSearch::SearchInfo search_info_opened_tab;
if (idx_history < 0) {
_disable_editing();
} else {
EDIT_RESOURCE(history[idx_history]);
ERR_FAIL_COND(!tab_search_context.has(history[idx_history]));
search_info_opened_tab = tab_search_context[history[idx_history]];
}
task_tree->tree_search_set_search_info(search_info_opened_tab);
_update_tabs();
}

View File

@ -143,7 +143,7 @@ void TreeSearch::_highlight_tree_item(TreeItem *p_tree_item) {
}
// Custom draw callback for highlighting (bind the parent_draw_method to this)
void TreeSearch::_draw_highlight_item(TreeItem *p_tree_item, const Rect2 p_rect, const Callable p_parent_draw_method) {
void TreeSearch::_draw_highlight_item(TreeItem *p_tree_item, const Rect2 p_rect, const Callable &p_parent_draw_method) {
if (!p_tree_item) {
return;
}
@ -533,42 +533,6 @@ TreeSearch::TreeSearch(TreeSearchPanel *p_search_panel) {
/* ------- TreeSearchPanel ------- */
void TreeSearchPanel::_initialize_controls() {
line_edit_search = memnew(LineEdit);
check_button_filter_highlight = memnew(CheckBox);
close_button = memnew(Button);
find_next_button = memnew(Button);
find_prev_button = memnew(Button);
label_filter = memnew(Label);
line_edit_search->set_placeholder(TTR("Search tree"));
close_button->set_theme_type_variation(LW_NAME(FlatButton));
find_next_button->set_theme_type_variation(LW_NAME(FlatButton));
find_prev_button->set_theme_type_variation(LW_NAME(FlatButton));
find_next_button->set_tooltip_text("Next Match");
find_prev_button->set_tooltip_text("Previous Match");
// Positioning and sizing
set_anchors_and_offsets_preset(LayoutPreset::PRESET_BOTTOM_WIDE);
set_v_size_flags(SIZE_SHRINK_CENTER); // Do not expand vertically
line_edit_search->set_h_size_flags(SIZE_EXPAND_FILL);
_add_spacer(0.1); // -> Otherwise the lineedits expand margin touches the left border.
add_child(line_edit_search);
add_child(find_prev_button);
add_child(find_next_button);
_add_spacer(0.25);
add_child(check_button_filter_highlight);
add_child(label_filter);
_add_spacer(0.25);
add_child(close_button);
}
void TreeSearchPanel::_add_spacer(float p_width_multiplier) {
Control *spacer = memnew(Control);
spacer->set_custom_minimum_size(Vector2(8.0 * EDSCALE * p_width_multiplier, 0.0));
@ -612,7 +576,40 @@ void TreeSearchPanel::_bind_methods() {
}
TreeSearchPanel::TreeSearchPanel() {
_initialize_controls();
line_edit_search = memnew(LineEdit);
check_button_filter_highlight = memnew(CheckBox);
close_button = memnew(Button);
find_next_button = memnew(Button);
find_prev_button = memnew(Button);
label_filter = memnew(Label);
line_edit_search->set_placeholder(TTR("Search tree"));
close_button->set_theme_type_variation(LW_NAME(FlatButton));
find_next_button->set_theme_type_variation(LW_NAME(FlatButton));
find_prev_button->set_theme_type_variation(LW_NAME(FlatButton));
find_next_button->set_tooltip_text("Next Match");
find_prev_button->set_tooltip_text("Previous Match");
// Positioning and sizing
set_anchors_and_offsets_preset(LayoutPreset::PRESET_BOTTOM_WIDE);
set_v_size_flags(SIZE_SHRINK_CENTER); // Do not expand vertically
line_edit_search->set_h_size_flags(SIZE_EXPAND_FILL);
_add_spacer(0.1); // -> Otherwise the lineedits expand margin touches the left border.
add_child(line_edit_search);
add_child(find_prev_button);
add_child(find_next_button);
_add_spacer(0.25);
add_child(check_button_filter_highlight);
add_child(label_filter);
_add_spacer(0.25);
add_child(close_button);
set_visible(false);
}
@ -624,9 +621,6 @@ TreeSearch::TreeSearchMode TreeSearchPanel::get_search_mode() const {
}
String TreeSearchPanel::get_text() const {
if (!line_edit_search) {
return String();
}
return line_edit_search->get_text();
}

View File

@ -77,7 +77,7 @@ private:
void _highlight_tree_item(TreeItem *p_tree_item);
// Custom draw-Callback (bind inherited Callable).
void _draw_highlight_item(TreeItem *p_tree_item, const Rect2 p_rect, const Callable p_parent_draw_method);
void _draw_highlight_item(TreeItem *p_tree_item, const Rect2 p_rect, const Callable &p_parent_draw_method);
void _update_matching_entries(const String &p_search_mask);
void _update_ordered_tree_items(TreeItem *p_tree_item);
@ -140,7 +140,6 @@ private:
Label *label_filter;
LineEdit *line_edit_search;
CheckBox *check_button_filter_highlight;
void _initialize_controls();
void _add_spacer(float width_multiplier = 1.f);
void _notification(int p_what);