Skip to content

Commit

Permalink
Address 2. review for TreeSearch
Browse files Browse the repository at this point in the history
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
  • Loading branch information
monxa committed Oct 13, 2024
1 parent 6776319 commit 98da0db
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 47 deletions.
17 changes: 13 additions & 4 deletions editor/limbo_ai_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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());
}
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -1066,13 +1064,24 @@ 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]];
PRINT_LINE(search_info_opened_tab.search_mask, history[idx_history]->get_path(), history[idx_history]);
}

task_tree->tree_search_set_search_info(search_info_opened_tab);
_update_tabs();
}

Expand Down
76 changes: 35 additions & 41 deletions editor/tree_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}

Expand All @@ -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();
}

Expand Down
3 changes: 1 addition & 2 deletions editor/tree_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class TreeSearch : public RefCounted {
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);
Expand Down Expand Up @@ -140,7 +140,6 @@ class TreeSearchPanel : public HFlowContainer {
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);
Expand Down

0 comments on commit 98da0db

Please sign in to comment.