Skip to content

Commit

Permalink
Merge pull request #23 from limbonaut/remember-collapsed
Browse files Browse the repository at this point in the history
Editor: Remember collapsed items in the task tree
  • Loading branch information
limbonaut authored Feb 2, 2024
2 parents b0e4c15 + 3e32051 commit 5bd8a90
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bt/tasks/bt_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ void BTTask::_set_children(Array p_children) {
}
}

void BTTask::set_display_collapsed(bool p_display_collapsed) {
data.display_collapsed = p_display_collapsed;
}

bool BTTask::is_displayed_collapsed() const {
return data.display_collapsed;
}

String BTTask::get_task_name() {
if (data.custom_name.is_empty()) {
#ifdef LIMBOAI_MODULE
Expand Down
4 changes: 4 additions & 0 deletions bt/tasks/bt_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class BTTask : public BT {
Vector<Ref<BTTask>> children;
Status status = FRESH;
double elapsed = 0.0;
bool display_collapsed = false;
} data;

Array _get_children() const;
Expand Down Expand Up @@ -115,6 +116,9 @@ class BTTask : public BT {
_FORCE_INLINE_ Node *get_agent() const { return data.agent; }
void set_agent(Node *p_agent) { data.agent = p_agent; }

void set_display_collapsed(bool p_display_collapsed);
bool is_displayed_collapsed() const;

String get_custom_name() const { return data.custom_name; }
void set_custom_name(const String &p_name);
String get_task_name();
Expand Down
22 changes: 22 additions & 0 deletions editor/task_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void TaskTree::_update_item(TreeItem *p_item) {
p_item->set_icon(0, LimboUtility::get_singleton()->get_task_icon(type_arg));
p_item->set_icon_max_width(0, 16 * EDSCALE);
p_item->set_editable(0, false);
p_item->set_collapsed(task->is_displayed_collapsed());

for (int i = 0; i < p_item->get_button_count(0); i++) {
p_item->erase_button(0, i);
Expand Down Expand Up @@ -111,7 +112,9 @@ void TaskTree::_update_tree() {
}

if (bt->get_root_task().is_valid()) {
updating_tree = true;
_create_tree(bt->get_root_task(), nullptr);
updating_tree = false;
}

TreeItem *item = _find_item(sel);
Expand Down Expand Up @@ -167,6 +170,21 @@ void TaskTree::_on_item_activated() {
emit_signal(LW_NAME(task_activated));
}

void TaskTree::_on_item_collapsed(Object *p_obj) {
if (updating_tree) {
return;
}

TreeItem *item = Object::cast_to<TreeItem>(p_obj);
if (!item) {
return;
}

Ref<BTTask> task = item->get_metadata(0);
ERR_FAIL_NULL(task);
task->set_display_collapsed(item->is_collapsed());
}

void TaskTree::_on_task_changed() {
_update_item(tree->get_selected());
}
Expand All @@ -183,7 +201,9 @@ void TaskTree::load_bt(const Ref<BehaviorTree> &p_behavior_tree) {
tree->clear();
probability_rect_cache.clear();
if (bt->get_root_task().is_valid()) {
updating_tree = true;
_create_tree(bt->get_root_task(), nullptr);
updating_tree = false;
}
}

Expand Down Expand Up @@ -366,6 +386,7 @@ void TaskTree::_notification(int p_what) {
tree->connect("item_mouse_selected", callable_mp(this, &TaskTree::_on_item_mouse_selected));
tree->connect("item_selected", callable_mp(this, &TaskTree::_on_item_selected));
tree->connect("item_activated", callable_mp(this, &TaskTree::_on_item_activated));
tree->connect("item_collapsed", callable_mp(this, &TaskTree::_on_item_collapsed));
} break;
case NOTIFICATION_THEME_CHANGED: {
_do_update_theme_item_cache();
Expand Down Expand Up @@ -399,6 +420,7 @@ void TaskTree::_bind_methods() {

TaskTree::TaskTree() {
editable = true;
updating_tree = false;

tree = memnew(Tree);
add_child(tree);
Expand Down
2 changes: 2 additions & 0 deletions editor/task_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class TaskTree : public Control {
Ref<BehaviorTree> bt;
Ref<BTTask> last_selected;
bool editable;
bool updating_tree;
HashMap<RECT_CACHE_KEY, Rect2> probability_rect_cache;

struct ThemeCache {
Expand Down Expand Up @@ -67,6 +68,7 @@ class TaskTree : public Control {

void _on_item_selected();
void _on_item_activated();
void _on_item_collapsed(Object *p_obj);
void _on_item_mouse_selected(const Vector2 &p_pos, MouseButton p_button_index);
void _on_task_changed();

Expand Down

0 comments on commit 5bd8a90

Please sign in to comment.