Assign "bold" font to tasks with custom name set in BT debugger

Also, fixes resources used without Ref in debugger view.
This commit is contained in:
Serhii Snitsaruk 2023-08-29 10:45:00 +02:00
parent feab3c1ba9
commit 4ff1b874f3
4 changed files with 67 additions and 46 deletions

View File

@ -40,6 +40,7 @@ BehaviorTreeData::BehaviorTreeData(const Ref<BTTask> &p_instance, const NodePath
tasks.push_back(TaskData(
id,
task->get_task_name(),
!task->get_custom_name().is_empty(),
num_children,
task->get_status(),
task->get_elapsed_time(),
@ -54,6 +55,7 @@ void BehaviorTreeData::serialize(Array &p_arr) {
for (const TaskData &td : tasks) {
p_arr.push_back(td.id);
p_arr.push_back(td.name);
p_arr.push_back(td.is_custom_name);
p_arr.push_back(td.num_children);
p_arr.push_back(td.status);
p_arr.push_back(td.elapsed_time);
@ -74,12 +76,13 @@ void BehaviorTreeData::deserialize(const Array &p_arr) {
ERR_FAIL_COND(p_arr.size() < idx + 7);
ERR_FAIL_COND(p_arr[idx].get_type() != Variant::INT);
ERR_FAIL_COND(p_arr[idx + 1].get_type() != Variant::STRING);
ERR_FAIL_COND(p_arr[idx + 2].get_type() != Variant::INT);
ERR_FAIL_COND(p_arr[idx + 2].get_type() != Variant::BOOL);
ERR_FAIL_COND(p_arr[idx + 3].get_type() != Variant::INT);
ERR_FAIL_COND(p_arr[idx + 4].get_type() != Variant::FLOAT);
ERR_FAIL_COND(p_arr[idx + 5].get_type() != Variant::STRING);
ERR_FAIL_COND(p_arr[idx + 4].get_type() != Variant::INT);
ERR_FAIL_COND(p_arr[idx + 5].get_type() != Variant::FLOAT);
ERR_FAIL_COND(p_arr[idx + 6].get_type() != Variant::STRING);
tasks.push_back(TaskData(p_arr[idx], p_arr[idx + 1], p_arr[idx + 2], p_arr[idx + 3], p_arr[idx + 4], p_arr[idx + 5], p_arr[idx + 6]));
idx += 7;
ERR_FAIL_COND(p_arr[idx + 7].get_type() != Variant::STRING);
tasks.push_back(TaskData(p_arr[idx], p_arr[idx + 1], p_arr[idx + 2], p_arr[idx + 3], p_arr[idx + 4], p_arr[idx + 5], p_arr[idx + 6], p_arr[idx + 7]));
idx += 8;
}
}

View File

@ -19,15 +19,17 @@ public:
struct TaskData {
int id = 0;
String name;
bool is_custom_name = false;
int num_children = 0;
int status = 0;
double elapsed_time = 0.0;
String type_name;
String script_path;
TaskData(int p_id, const String &p_name, int p_num_children, int p_status, double p_elapsed_time, const String &p_type_name, const String &p_script_path) {
TaskData(int p_id, const String &p_name, bool p_is_custom_name, int p_num_children, int p_status, double p_elapsed_time, const String &p_type_name, const String &p_script_path) {
id = p_id;
name = p_name;
is_custom_name = p_is_custom_name;
num_children = p_num_children;
status = p_status;
elapsed_time = p_elapsed_time;

View File

@ -26,17 +26,17 @@
void BehaviorTreeView::_draw_running_status(Object *p_obj, Rect2 p_rect) {
p_rect = p_rect.grow_side(SIDE_LEFT, p_rect.get_position().x);
sbf_running.draw(tree->get_canvas_item(), p_rect);
theme_cache.sbf_running->draw(tree->get_canvas_item(), p_rect);
}
void BehaviorTreeView::_draw_success_status(Object *p_obj, Rect2 p_rect) {
p_rect = p_rect.grow_side(SIDE_LEFT, p_rect.get_position().x);
sbf_success.draw(tree->get_canvas_item(), p_rect);
theme_cache.sbf_success->draw(tree->get_canvas_item(), p_rect);
}
void BehaviorTreeView::_draw_failure_status(Object *p_obj, Rect2 p_rect) {
p_rect = p_rect.grow_side(SIDE_LEFT, p_rect.get_position().x);
sbf_failure.draw(tree->get_canvas_item(), p_rect);
theme_cache.sbf_failure->draw(tree->get_canvas_item(), p_rect);
}
void BehaviorTreeView::_item_collapsed(Object *p_obj) {
@ -81,7 +81,12 @@ void BehaviorTreeView::update_tree(const BehaviorTreeData &p_data) {
item->set_cell_mode(1, TreeItem::CELL_MODE_ICON);
item->set_metadata(0, task_data.id);
item->set_text(0, task_data.name);
if (task_data.is_custom_name) {
item->set_custom_font(0, theme_cache.font_custom_name);
}
item->set_text(2, rtos(Math::snapped(task_data.elapsed_time, 0.01)).pad_decimals(2));
String cors = (task_data.script_path.is_empty()) ? task_data.type_name : task_data.script_path;
@ -90,13 +95,13 @@ void BehaviorTreeView::update_tree(const BehaviorTreeData &p_data) {
if (task_data.status == BTTask::SUCCESS) {
item->set_custom_draw(0, this, SNAME("_draw_success_status"));
item->set_icon(1, icon_success);
item->set_icon(1, theme_cache.icon_success);
} else if (task_data.status == BTTask::FAILURE) {
item->set_custom_draw(0, this, SNAME("_draw_failure_status"));
item->set_icon(1, icon_failure);
item->set_icon(1, theme_cache.icon_failure);
} else if (task_data.status == BTTask::RUNNING) {
item->set_custom_draw(0, this, SNAME("_draw_running_status"));
item->set_icon(1, icon_running);
item->set_icon(1, theme_cache.icon_running);
}
if (task_data.id == selected_id) {
@ -119,34 +124,39 @@ void BehaviorTreeView::clear() {
collapsed_ids.clear();
}
void BehaviorTreeView::_notification(int p_notification) {
if (p_notification == NOTIFICATION_THEME_CHANGED) {
icon_running = get_theme_icon(SNAME("LimboExtraClock"), SNAME("EditorIcons"));
icon_success = get_theme_icon(SNAME("BTAlwaysSucceed"), SNAME("EditorIcons"));
icon_failure = get_theme_icon(SNAME("BTAlwaysFail"), SNAME("EditorIcons"));
void BehaviorTreeView::_update_theme_item_cache() {
Control::_update_theme_item_cache();
Color running_border = Color::html("#fea900");
Color running_fill = Color(running_border, 0.1);
Color success_border = Color::html("#2fa139");
Color success_fill = Color(success_border, 0.1);
Color failure_border = Color::html("#cd3838");
Color failure_fill = Color(failure_border, 0.1);
theme_cache.icon_running = get_theme_icon(SNAME("LimboExtraClock"), SNAME("EditorIcons"));
theme_cache.icon_success = get_theme_icon(SNAME("BTAlwaysSucceed"), SNAME("EditorIcons"));
theme_cache.icon_failure = get_theme_icon(SNAME("BTAlwaysFail"), SNAME("EditorIcons"));
sbf_running.set_border_color(running_border);
sbf_running.set_bg_color(running_fill);
sbf_running.set_border_width(SIDE_LEFT, 4.0);
sbf_running.set_border_width(SIDE_RIGHT, 4.0);
theme_cache.font_custom_name = get_theme_font(SNAME("bold"), SNAME("EditorFonts"));
sbf_success.set_border_color(success_border);
sbf_success.set_bg_color(success_fill);
sbf_success.set_border_width(SIDE_LEFT, 4.0);
sbf_success.set_border_width(SIDE_RIGHT, 4.0);
Color running_border = Color::html("#fea900");
Color running_fill = Color(running_border, 0.1);
Color success_border = Color::html("#2fa139");
Color success_fill = Color(success_border, 0.1);
Color failure_border = Color::html("#cd3838");
Color failure_fill = Color(failure_border, 0.1);
sbf_failure.set_border_color(failure_border);
sbf_failure.set_bg_color(failure_fill);
sbf_failure.set_border_width(SIDE_LEFT, 4.0);
sbf_failure.set_border_width(SIDE_RIGHT, 4.0);
}
theme_cache.sbf_running.instantiate();
theme_cache.sbf_running->set_border_color(running_border);
theme_cache.sbf_running->set_bg_color(running_fill);
theme_cache.sbf_running->set_border_width(SIDE_LEFT, 4.0);
theme_cache.sbf_running->set_border_width(SIDE_RIGHT, 4.0);
theme_cache.sbf_success.instantiate();
theme_cache.sbf_success->set_border_color(success_border);
theme_cache.sbf_success->set_bg_color(success_fill);
theme_cache.sbf_success->set_border_width(SIDE_LEFT, 4.0);
theme_cache.sbf_success->set_border_width(SIDE_RIGHT, 4.0);
theme_cache.sbf_failure.instantiate();
theme_cache.sbf_failure->set_border_color(failure_border);
theme_cache.sbf_failure->set_bg_color(failure_fill);
theme_cache.sbf_failure->set_border_width(SIDE_LEFT, 4.0);
theme_cache.sbf_failure->set_border_width(SIDE_RIGHT, 4.0);
}
void BehaviorTreeView::_bind_methods() {
@ -171,4 +181,4 @@ BehaviorTreeView::BehaviorTreeView() {
tree->connect(SNAME("item_collapsed"), callable_mp(this, &BehaviorTreeView::_item_collapsed));
}
#endif // TOOLS_ENABLED
#endif // TOOLS_ENABLED

View File

@ -28,14 +28,20 @@ class BehaviorTreeView : public Control {
private:
Tree *tree;
StyleBoxFlat sbf_running;
StyleBoxFlat sbf_success;
StyleBoxFlat sbf_failure;
Vector<int> collapsed_ids;
Ref<Texture2D> icon_running;
Ref<Texture2D> icon_success;
Ref<Texture2D> icon_failure;
struct ThemeCache {
Ref<StyleBoxFlat> sbf_running;
Ref<StyleBoxFlat> sbf_success;
Ref<StyleBoxFlat> sbf_failure;
Ref<Texture2D> icon_running;
Ref<Texture2D> icon_success;
Ref<Texture2D> icon_failure;
Ref<Font> font_custom_name;
} theme_cache;
Vector<int> collapsed_ids;
void _draw_success_status(Object *p_obj, Rect2 p_rect);
void _draw_running_status(Object *p_obj, Rect2 p_rect);
@ -43,9 +49,9 @@ private:
void _item_collapsed(Object *p_obj);
protected:
static void _bind_methods();
virtual void _update_theme_item_cache() override;
void _notification(int p_notification);
static void _bind_methods();
public:
void update_tree(const BehaviorTreeData &p_data);