Compare commits

..

1 Commits

Author SHA1 Message Date
monxa 3278aaf5f7
Merge b53e470c60 into 760af804c0 2024-10-01 21:22:38 +00:00
2 changed files with 48 additions and 120 deletions

View File

@ -43,9 +43,8 @@
void TreeSearch::_clean_callable_cache() {
ERR_FAIL_COND(!tree_reference);
HashMap<TreeItem *, Callable> new_callable_cache;
new_callable_cache.reserve(callable_cache.size());
new_callable_cache.reserve(callable_cache.size()); // Efficiency
for (int i = 0; i < ordered_tree_items.size(); i++) {
TreeItem *cur_item = ordered_tree_items[i];
@ -58,17 +57,15 @@ void TreeSearch::_clean_callable_cache() {
void TreeSearch::_filter_tree() {
ERR_FAIL_COND(!tree_reference);
if (matching_entries.is_empty()) {
return;
}
_filter_tree(tree_reference->get_root(), false);
}
void TreeSearch::_filter_tree(TreeItem *p_item, bool p_parent_matching) {
bool visible = number_matches.has(p_item) | p_parent_matching;
p_item->set_visible(visible);
if (!visible) {
p_item->set_visible(visible);
}
bool is_matching = _vector_has_bsearch(matching_entries, p_item);
for (int i = 0; i < p_item->get_child_count(); i++) {
_filter_tree(p_item->get_child(i), is_matching | p_parent_matching);
@ -91,7 +88,7 @@ void TreeSearch::_clear_filter() {
void TreeSearch::_highlight_tree() {
ERR_FAIL_COND(!tree_reference);
// This might not be the prettiest, but it is the most efficient solution probably.
for (HashMap<TreeItem *, int>::Iterator it = number_matches.begin(); it != number_matches.end(); ++it) {
TreeItem *tree_item = it->key;
_highlight_tree_item(tree_item);
@ -106,48 +103,48 @@ void TreeSearch::_highlight_tree_item(TreeItem *p_tree_item) {
return;
}
// Make sure to also call any draw method already defined.
// make sure to also call any draw method already defined.
Callable parent_draw_method;
if (p_tree_item->get_cell_mode(0) == TreeItem::CELL_MODE_CUSTOM) {
parent_draw_method = p_tree_item->get_custom_draw_callback(0);
}
// If the cached draw method is already applied, do nothing.
// if the cached draw method is already applied, do nothing.
if (callable_cache.has(p_tree_item) && parent_draw_method == callable_cache.get(p_tree_item)) {
return;
}
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.
// -- this is necessary because of the modularity of this implementation
// cache render properties of entry
String cached_text = p_tree_item->get_text(0);
Ref<Texture2D> cached_icon = p_tree_item->get_icon(0);
int cached_max_width = p_tree_item->get_icon_max_width(0);
callable_cache[p_tree_item] = draw_callback;
// This removes render properties in entry.
// this removes render properties in entry
p_tree_item->set_custom_draw_callback(0, draw_callback);
p_tree_item->set_cell_mode(0, TreeItem::CELL_MODE_CUSTOM);
// Restore render properties.
// restore render properties
p_tree_item->set_text(0, cached_text);
p_tree_item->set_icon(0, cached_icon);
p_tree_item->set_icon_max_width(0, cached_max_width);
}
// Custom draw callback for highlighting (bind the parent_drw_method to this)
// custom draw callback for highlighting (bind the parent_drw_method to this)
void TreeSearch::_draw_highlight_item(TreeItem *p_tree_item, const Rect2 p_rect, const Callable p_parent_draw_method) {
if (!p_tree_item) {
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);
// First part: outline
// first part: outline
if (matching_entries.has(p_tree_item)) {
// Font info
// font info
Ref<Font> font = p_tree_item->get_custom_font(0);
if (font.is_null()) {
font = p_tree_item->get_tree()->get_theme_font(LW_NAME(font));
@ -158,7 +155,7 @@ void TreeSearch::_draw_highlight_item(TreeItem *p_tree_item, const Rect2 p_rect,
font_size = p_tree_item->get_tree()->get_theme_font_size(LW_NAME(font));
}
// Substring size
// substring size
String string_full = p_tree_item->get_text(0);
StringSearchIndices substring_idx = _substring_bounds(string_full, _get_search_mask());
@ -168,14 +165,14 @@ void TreeSearch::_draw_highlight_item(TreeItem *p_tree_item, const Rect2 p_rect,
String substring_before = string_full.substr(0, substring_idx.lower);
Vector2 substring_before_size = font->get_string_size(substring_before, HORIZONTAL_ALIGNMENT_LEFT, -1.f, font_size);
// Stylebox
// stylebox
Ref<StyleBox> stylebox = p_tree_item->get_tree()->get_theme_stylebox(LW_NAME(Focus));
ERR_FAIL_NULL(stylebox);
// Extract separation
// extract separation
float h_sep = p_tree_item->get_tree()->get_theme_constant(LW_NAME(h_separation));
// Compose draw rect
// compose draw rect
const Vector2 PADDING = Vector2(4., 2.);
Rect2 draw_rect = p_rect;
@ -187,11 +184,11 @@ void TreeSearch::_draw_highlight_item(TreeItem *p_tree_item, const Rect2 p_rect,
draw_rect.position += rect_offset - PADDING / 2;
draw_rect.size = substring_match_size + PADDING;
// Draw
// draw
stylebox->draw(p_tree_item->get_tree()->get_canvas_item(), draw_rect);
}
// Second part: draw number
// second part: draw number
int num_mat = number_matches.has(p_tree_item) ? number_matches.get(p_tree_item) : 0;
if (num_mat > 0) {
float h_sep = p_tree_item->get_tree()->get_theme_constant(LW_NAME(h_separation));
@ -215,7 +212,7 @@ void TreeSearch::_update_matching_entries(const String &p_search_mask) {
matching_entries = accum;
}
/* Linaerizes the tree into [ordered_tree_items] like so:
/* this linearizes the tree into [ordered_tree_items] like so:
- i1
- i2
- i3
@ -227,10 +224,10 @@ void TreeSearch::_update_ordered_tree_items(TreeItem *p_tree_item) {
if (p_tree_item == p_tree_item->get_tree()->get_root()) {
ordered_tree_items.clear();
}
// Add the current item to the list.
// Add the current item to the list
ordered_tree_items.push_back(p_tree_item);
// Recursively collect items from the first child.
// Recursively collect items from the first child
TreeItem *child = p_tree_item->get_first_child();
while (child) {
_update_ordered_tree_items(child);
@ -268,7 +265,7 @@ void TreeSearch::_find_matching_entries(TreeItem *p_tree_item, const String &p_s
_find_matching_entries(child, p_search_mask, p_accum);
}
// Sort the result if we are at the root.
// sort the result if we are at the root
if (p_tree_item == p_tree_item->get_tree()->get_root()) {
p_accum.sort();
}
@ -323,13 +320,13 @@ void TreeSearch::_select_item(TreeItem *p_item) {
return;
ERR_FAIL_COND(!tree_reference || p_item->get_tree() != tree_reference);
// First unfold ancestors
// first unfold ancestors
TreeItem *ancestor = p_item->get_parent();
while (ancestor) {
ancestor->set_collapsed(false);
ancestor = ancestor->get_parent();
}
// Then scroll to [item]
//then scroll to [item]
tree_reference->scroll_to_item(p_item);
// ...and select it
@ -351,63 +348,18 @@ void TreeSearch::_select_first_match() {
}
}
void TreeSearch::_select_last_match() {
if (matching_entries.size() == 0) {
return;
}
for (int i = ordered_tree_items.size() - 1; i >= 0; i--) {
TreeItem *item = ordered_tree_items[i];
if (!_vector_has_bsearch(matching_entries, item)) {
continue;
}
_select_item(item);
return;
}
}
void TreeSearch::_select_previous_match() {
if (matching_entries.size() == 0) {
return;
}
TreeItem *selected = tree_reference->get_selected();
if (!selected) {
_select_last_match();
return;
}
// Find [selected_idx] among ordered_tree_items.
int selected_idx = 0;
for (int i = ordered_tree_items.size() - 1; i >= 0; i--) {
if (ordered_tree_items[i] == selected) {
selected_idx = i;
break;
}
}
// Find first entry before [selected_idx].
for (int i = MIN(ordered_tree_items.size() - 1, selected_idx) - 1; i >= 0; i--) {
TreeItem *item = ordered_tree_items[i];
if (_vector_has_bsearch(matching_entries, item)) {
_select_item(item);
return;
}
}
// Wrap around.
_select_last_match();
}
void TreeSearch::_select_next_match() {
if (matching_entries.size() == 0) {
return;
}
TreeItem *selected = tree_reference->get_selected();
if (!selected) {
_select_first_match();
return;
}
// Find [selected_idx] among ordered_tree_items
int selected_idx = 0;
// find [selected_idx] among ordered_tree_items
int selected_idx = -1;
for (int i = 0; i < ordered_tree_items.size(); i++) {
if (ordered_tree_items[i] == selected) {
selected_idx = i;
@ -415,7 +367,7 @@ void TreeSearch::_select_next_match() {
}
}
// Find first entry after [selected_idx].
// find first entry after [selected_idx].
for (int i = MAX(0, selected_idx) + 1; i < ordered_tree_items.size(); i++) {
TreeItem *item = ordered_tree_items[i];
if (_vector_has_bsearch(matching_entries, item)) {
@ -423,7 +375,7 @@ void TreeSearch::_select_next_match() {
return;
}
}
// Wrap around.
// wrap around.
_select_first_match();
}
@ -449,14 +401,14 @@ void TreeSearch::notify_item_edited(TreeItem *item) {
_highlight_tree_item(item);
}
// Called as a post-processing step for the already constructed tree.
// Call this as a post-processing step for the already constructed tree.
void TreeSearch::update_search(Tree *p_tree) {
ERR_FAIL_COND(!search_panel || !p_tree);
tree_reference = p_tree;
if (!search_panel->is_visible() || search_panel->get_text().length() == 0) {
// Clear and redraw if search was active recently.
// clear and redraw if search was active recently.
if (was_searched_recently) {
_clear_filter();
number_matches.clear();
@ -475,6 +427,7 @@ void TreeSearch::update_search(Tree *p_tree) {
_update_matching_entries(search_mask);
_update_number_matches();
_clear_filter();
_highlight_tree();
if (search_mode == TreeSearchMode::FILTER) {
_filter_tree();
@ -490,7 +443,6 @@ TreeSearch::TreeSearch(TreeSearchPanel *p_search_panel) {
search_panel = p_search_panel;
search_panel->connect(LW_NAME(text_submitted), callable_mp(this, &TreeSearch::_select_next_match));
search_panel->connect(LW_NAME(Close), callable_mp(this, &TreeSearch::_on_search_panel_closed));
search_panel->connect("select_previous_match", callable_mp(this, &TreeSearch::_select_previous_match));
}
/* !TreeSearch */
@ -501,30 +453,20 @@ 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));
close_button->set_tooltip_text("Hide");
find_next_button->set_tooltip_text("Next Match");
find_prev_button->set_tooltip_text("Previous Match");
// Positioning and sizing
// 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_spacer(0.25); // 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);
@ -532,6 +474,7 @@ void TreeSearchPanel::_initialize_controls() {
_add_spacer(0.25);
add_child(close_button);
_add_spacer(0.25);
}
void TreeSearchPanel::_add_spacer(float p_width_multiplier) {
@ -543,16 +486,13 @@ void TreeSearchPanel::_add_spacer(float p_width_multiplier) {
void TreeSearchPanel::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_READY: {
// Close callbacks
// close callbacks
close_button->connect(LW_NAME(pressed), Callable(this, LW_NAME(set_visible)).bind(false));
close_button->connect(LW_NAME(pressed), Callable(this, LW_NAME(emit_signal)).bind(LW_NAME(Close)));
close_button->set_shortcut(LW_GET_SHORTCUT("limbo_ai/hide_tree_search")); // TODO: use internal shortcut.
// Search callbacks
close_button->set_shortcut(LW_GET_SHORTCUT("limbo_ai/hide_tree_search"));
// search callbacks
Callable c_update_requested = Callable(this, LW_NAME(emit_signal)).bind("update_requested");
Callable c_text_submitted = Callable(this, LW_NAME(emit_signal)).bind(LW_NAME(text_submitted));
Callable c_select_previous_match = Callable(this, LW_NAME(emit_signal)).bind("select_previous_match");
find_next_button->connect(LW_NAME(pressed), c_text_submitted);
find_prev_button->connect(LW_NAME(pressed), c_select_previous_match);
Callable c_text_submitted = Callable((Object *)this, LW_NAME(emit_signal)).bind(LW_NAME(text_submitted));
line_edit_search->connect(LW_NAME(text_changed), c_update_requested.unbind(1));
check_button_filter_highlight->connect(LW_NAME(pressed), c_update_requested);
@ -561,8 +501,6 @@ void TreeSearchPanel::_notification(int p_what) {
}
case NOTIFICATION_THEME_CHANGED: {
BUTTON_SET_ICON(close_button, get_theme_icon(LW_NAME(Close), LW_NAME(EditorIcons)));
BUTTON_SET_ICON(find_prev_button, get_theme_icon("MoveUp", LW_NAME(EditorIcons)));
BUTTON_SET_ICON(find_next_button, get_theme_icon("MoveDown", LW_NAME(EditorIcons)));
label_filter->set_text(TTR("Filter"));
break;
}
@ -572,7 +510,6 @@ void TreeSearchPanel::_notification(int p_what) {
void TreeSearchPanel::_bind_methods() {
ADD_SIGNAL(MethodInfo("update_requested"));
ADD_SIGNAL(MethodInfo(LW_NAME(text_submitted)));
ADD_SIGNAL(MethodInfo("select_previous_match"));
ADD_SIGNAL(MethodInfo(LW_NAME(Close)));
}

View File

@ -53,14 +53,14 @@ private:
// For TaskTree: These are updated when the tree is updated through TaskTree::_create_tree.
Tree *tree_reference;
// Linearized ordering of tree items.
// linearized ordering of tree items.
Vector<TreeItem *> ordered_tree_items;
// Entires that match the search mask.
// entires that match the search mask.
// TODO: Decide if this can be removed. It can be implicitly inferred from number_matches.
Vector<TreeItem *> matching_entries;
// Number of descendant matches for each tree item.
// number of descendant matches for each tree item.
HashMap<TreeItem *, int> number_matches;
// Custom draw-callbacks for each tree item.
// custom draw-callbacks for each tree item.
HashMap<TreeItem *, Callable> callable_cache;
bool was_searched_recently = false; // Performance
@ -68,7 +68,7 @@ private:
void _clean_callable_cache();
// update_search() calls these
// Update_search() calls these
void _filter_tree();
void _filter_tree(TreeItem *item, bool p_parent_matching);
void _clear_filter();
@ -76,7 +76,7 @@ private:
void _highlight_tree();
void _highlight_tree_item(TreeItem *p_tree_item);
// custom draw-Callback (bind inherited Callable).
// 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 _update_matching_entries(const String &p_search_mask);
@ -89,15 +89,11 @@ private:
void _select_item(TreeItem *p_item);
void _select_first_match();
void _select_last_match();
void _select_previous_match();
void _select_next_match();
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>
bool _vector_has_bsearch(Vector<T *> &p_vec, T *element) const;
@ -116,10 +112,7 @@ public:
bool visible;
};
// Called as a post-processing step for the already constructed tree.
void update_search(Tree *p_tree);
// This restores the highlight-drawing if a single item got edited.
void notify_item_edited(TreeItem *p_item);
TreeSearch() { ERR_FAIL_MSG("TreeSearch needs a TreeSearchPanel to work properly."); }
@ -134,8 +127,6 @@ class TreeSearchPanel : public HFlowContainer {
private:
Button *toggle_button_filter_highlight;
Button *close_button;
Button *find_next_button;
Button *find_prev_button;
Label *label_filter;
LineEdit *line_edit_search;
CheckBox *check_button_filter_highlight;