limboai/editor/tree_search.h

126 lines
3.2 KiB
C
Raw Normal View History

2024-09-19 21:35:14 +00:00
/**
* tree_search.h
* =============================================================================
* Copyright 2021-2024 Serhii Snitsaruk
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
#ifdef TOOLS_ENABLED
#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>
2024-09-25 16:15:17 +00:00
#include <godot_cpp/classes/h_flow_container.hpp>
2024-09-25 15:32:03 +00:00
#include <godot_cpp/classes/label.hpp>
2024-09-19 21:35:14 +00:00
#include <godot_cpp/classes/line_edit.hpp>
#include <godot_cpp/classes/tree.hpp>
2024-09-25 15:32:03 +00:00
2024-09-19 21:35:14 +00:00
#endif // LIMBOAI_GDEXTENSION
#ifdef LIMBOAI_MODULE
#include "scene/gui/check_box.h"
2024-09-25 16:15:17 +00:00
#include "scene/gui/flow_container.h"
2024-09-25 15:32:03 +00:00
#include "scene/gui/label.h"
#include "scene/gui/line_edit.h"
#include "scene/gui/tree.h"
2024-09-19 21:35:14 +00:00
#endif // LIMBOAI_MODULE
using namespace godot;
enum TreeSearchMode {
2024-09-25 15:32:03 +00:00
HIGHLIGHT = 0,
FILTER = 1
2024-09-19 21:35:14 +00:00
};
2024-09-25 16:15:17 +00:00
class TreeSearchPanel : public HFlowContainer {
GDCLASS(TreeSearchPanel, HFlowContainer)
2024-09-19 21:35:14 +00:00
private:
Button *toggle_button_filter_highlight;
Button *close_button;
2024-09-25 15:32:03 +00:00
Label *label_filter;
2024-09-19 21:35:14 +00:00
LineEdit *line_edit_search;
CheckBox *check_button_filter_highlight;
2024-09-19 21:35:14 +00:00
void _initialize_controls();
2024-09-25 15:32:03 +00:00
void _initialize_close_callbacks();
void _add_spacer(float width_multiplier = 1.f);
2024-09-19 21:35:14 +00:00
2024-09-25 15:32:03 +00:00
void _on_draw_highlight(TreeItem *item, Rect2 rect);
void _emit_text_changed(const String &text);
void _emit_text_submitted(const String &p_text);
2024-09-25 16:54:33 +00:00
void _emit_filter_toggled();
2024-09-25 15:32:03 +00:00
void _notification(int p_what);
2024-09-19 21:35:14 +00:00
protected:
2024-09-25 15:32:03 +00:00
static void _bind_methods();
2024-09-19 21:35:14 +00:00
public:
TreeSearchMode get_search_mode();
String get_text();
2024-09-25 15:32:03 +00:00
void show_and_focus();
2024-09-19 21:35:14 +00:00
TreeSearchPanel();
2024-09-25 15:32:03 +00:00
bool has_focus();
2024-09-19 21:35:14 +00:00
};
2024-09-25 15:32:03 +00:00
class TreeSearch : public RefCounted {
GDCLASS(TreeSearch, RefCounted)
2024-09-19 21:35:14 +00:00
private:
2024-09-25 15:32:03 +00:00
struct StringSearchIndices {
// initialize to opposite bounds.
int lower = -1;
int upper = -1;
bool hit() {
return 0 <= lower && lower < upper;
}
};
2024-09-25 16:17:44 +00:00
Tree *tree_reference;
2024-09-25 15:32:03 +00:00
Vector<TreeItem *> ordered_tree_items;
Vector<TreeItem *> matching_entries;
HashMap<TreeItem *, int> number_matches;
HashMap<TreeItem *, Callable> callable_cache;
2024-09-25 16:17:44 +00:00
2024-09-25 16:54:33 +00:00
void _filter_tree(const String &p_search_mask);
2024-09-25 15:32:03 +00:00
void _highlight_tree(const String &p_search_mask);
void _draw_highlight_item(TreeItem *tree_item, Rect2 rect, String search_mask, Callable parent_draw_method);
void _update_matching_entries(const String &search_mask);
void _update_ordered_tree_items(TreeItem *tree_item);
void _update_number_matches();
2024-09-25 16:54:33 +00:00
2024-09-25 15:32:03 +00:00
Vector<TreeItem *> _find_matching_entries(TreeItem *tree_item, const String &search_mask, Vector<TreeItem *> &buffer);
StringSearchIndices _substring_bounds(const String &searchable, const String &search_mask) const;
2024-09-25 16:17:44 +00:00
void _select_item(TreeItem *item);
2024-09-25 15:32:03 +00:00
void _select_first_match();
void _select_next_match();
2024-09-19 21:35:14 +00:00
2024-09-25 16:54:33 +00:00
template<typename T>
bool _vector_has_bsearch(Vector<T*> p_vec, T* element);
2024-09-25 15:32:03 +00:00
protected:
2024-09-25 16:17:44 +00:00
static void _bind_methods() {}
2024-09-19 21:35:14 +00:00
public:
// we will add everything from TaskTree.h
2024-09-25 15:32:03 +00:00
void update_search(Tree *tree);
void on_item_edited(TreeItem *item);
2024-09-19 21:35:14 +00:00
TreeSearchPanel *search_panel;
TreeSearch();
~TreeSearch();
};
#endif // TREE_SEARCH_H
2024-09-25 16:54:33 +00:00
#endif // ! TOOLS_ENABLED