Fix support for user script class icons

This commit is contained in:
Serhii Snitsaruk 2023-04-16 08:26:47 +02:00
parent 5c0c359832
commit 2156a2e31c
6 changed files with 28 additions and 31 deletions

View File

@ -21,13 +21,20 @@ BehaviorTreeData::BehaviorTreeData(const Ref<BTTask> &p_instance, const NodePath
stack.push_front(task->get_child(num_children - 1 - i)); stack.push_front(task->get_child(num_children - 1 - i));
} }
String script_path;
if (task->get_script()) {
Ref<Script> script = task->get_script();
script_path = script->get_path();
}
tasks.push_back(TaskData( tasks.push_back(TaskData(
id, id,
task->get_task_name(), task->get_task_name(),
num_children, num_children,
task->get_status(), task->get_status(),
task->get_elapsed_time(), task->get_elapsed_time(),
task->get_class())); task->get_class(),
script_path));
id += 1; id += 1;
} }
} }
@ -41,24 +48,28 @@ void BehaviorTreeData::serialize(Array &p_arr) {
p_arr.push_back(td.status); p_arr.push_back(td.status);
p_arr.push_back(td.elapsed_time); p_arr.push_back(td.elapsed_time);
p_arr.push_back(td.type_name); p_arr.push_back(td.type_name);
p_arr.push_back(td.script_path);
} }
} }
void BehaviorTreeData::deserialize(const Array &p_arr) { void BehaviorTreeData::deserialize(const Array &p_arr) {
ERR_FAIL_COND(tasks.size() != 0); ERR_FAIL_COND(tasks.size() != 0);
ERR_FAIL_COND(p_arr.size() < 1); ERR_FAIL_COND(p_arr.size() < 1);
ERR_FAIL_COND(p_arr[0].get_type() != Variant::NODE_PATH); ERR_FAIL_COND(p_arr[0].get_type() != Variant::NODE_PATH);
bt_player_path = p_arr[0]; bt_player_path = p_arr[0];
int idx = 1; int idx = 1;
while (p_arr.size() > idx + 1) { while (p_arr.size() > idx + 1) {
ERR_FAIL_COND(p_arr.size() < idx + 6); ERR_FAIL_COND(p_arr.size() < idx + 7);
ERR_FAIL_COND(p_arr[idx].get_type() != Variant::INT); 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 + 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::INT);
ERR_FAIL_COND(p_arr[idx + 3].get_type() != Variant::INT); 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 + 4].get_type() != Variant::FLOAT);
ERR_FAIL_COND(p_arr[idx + 5].get_type() != Variant::STRING); ERR_FAIL_COND(p_arr[idx + 5].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])); ERR_FAIL_COND(p_arr[idx + 6].get_type() != Variant::STRING);
idx += 6; 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;
} }
} }

View File

@ -15,16 +15,16 @@ public:
int status = 0; int status = 0;
double elapsed_time = 0.0; double elapsed_time = 0.0;
String type_name; String type_name;
// String script_path; String script_path;
// String resource_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) { 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) {
id = p_id; id = p_id;
name = p_name; name = p_name;
num_children = p_num_children; num_children = p_num_children;
status = p_status; status = p_status;
elapsed_time = p_elapsed_time; elapsed_time = p_elapsed_time;
type_name = p_type_name; type_name = p_type_name;
script_path = p_script_path;
} }
TaskData() {} TaskData() {}

View File

@ -69,9 +69,12 @@ void BehaviorTreeView::update_tree(const BehaviorTreeData &p_data) {
item->set_metadata(0, task_data.id); item->set_metadata(0, task_data.id);
item->set_text(0, task_data.name); item->set_text(0, task_data.name);
item->set_icon(0, LimboUtility::get_singleton()->get_task_icon(task_data.type_name));
item->set_text(2, rtos(Math::snapped(task_data.elapsed_time, 0.01)).pad_decimals(2)); 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;
item->set_icon(0, LimboUtility::get_singleton()->get_task_icon(cors));
item->set_icon_max_width(0, 16 * EDSCALE); // Force user icon size.
if (task_data.status == BTTask::SUCCESS) { if (task_data.status == BTTask::SUCCESS) {
item->set_custom_draw(0, this, SNAME("_draw_success_status")); item->set_custom_draw(0, this, SNAME("_draw_success_status"));
item->set_icon(1, icon_success); item->set_icon(1, icon_success);

View File

@ -91,6 +91,7 @@ void TaskTree::_update_item(TreeItem *p_item) {
type_arg = task->get_class(); type_arg = task->get_class();
} }
p_item->set_icon(0, LimboUtility::get_singleton()->get_task_icon(type_arg)); 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_editable(0, false);
for (int i = 0; i < p_item->get_button_count(0); i++) { for (int i = 0; i < p_item->get_button_count(0); i++) {
@ -341,6 +342,7 @@ void TaskSection::add_task_button(String p_name, const Ref<Texture> &icon, Varia
Button *btn = memnew(Button); Button *btn = memnew(Button);
btn->set_text(p_name); btn->set_text(p_name);
btn->set_icon(icon); btn->set_icon(icon);
btn->add_theme_constant_override(SNAME("icon_max_width"), 16 * EDSCALE); // Force user icons to be of the proper size.
btn->connect("pressed", callable_mp(this, &TaskSection::_on_task_button_pressed).bind(p_meta)); btn->connect("pressed", callable_mp(this, &TaskSection::_on_task_button_pressed).bind(p_meta));
tasks_container->add_child(btn); tasks_container->add_child(btn);
} }
@ -459,7 +461,7 @@ void TaskPanel::refresh() {
TaskSection *sec = memnew(TaskSection(cat)); TaskSection *sec = memnew(TaskSection(cat));
for (String task_meta : tasks) { for (String task_meta : tasks) {
Ref<Texture> icon = LimboUtility::get_singleton()->get_task_icon(task_meta); Ref<Texture2D> icon = LimboUtility::get_singleton()->get_task_icon(task_meta);
String tname; String tname;
if (task_meta.begins_with("res:")) { if (task_meta.begins_with("res:")) {
tname = task_meta.get_file().get_basename().trim_prefix("BT").to_pascal_case(); tname = task_meta.get_file().get_basename().trim_prefix("BT").to_pascal_case();

View File

@ -2,10 +2,8 @@
#include "limbo_utility.h" #include "limbo_utility.h"
#include "bt/bt_task.h" #include "bt/bt_task.h"
#include "core/io/image_loader.h"
#include "core/variant/variant.h" #include "core/variant/variant.h"
#include "editor/editor_node.h" #include "editor/editor_node.h"
#include "editor/editor_scale.h"
#include "scene/resources/texture.h" #include "scene/resources/texture.h"
LimboUtility *LimboUtility::singleton = nullptr; LimboUtility *LimboUtility::singleton = nullptr;
@ -41,30 +39,12 @@ String LimboUtility::get_status_name(int p_status) const {
Ref<Texture2D> LimboUtility::get_task_icon(String p_class_or_script_path) const { Ref<Texture2D> LimboUtility::get_task_icon(String p_class_or_script_path) const {
ERR_FAIL_COND_V_MSG(p_class_or_script_path.is_empty(), Variant(), "BTTask: script path or class cannot be empty."); ERR_FAIL_COND_V_MSG(p_class_or_script_path.is_empty(), Variant(), "BTTask: script path or class cannot be empty.");
String base_type = p_class_or_script_path;
if (p_class_or_script_path.begins_with("res:")) { if (p_class_or_script_path.begins_with("res:")) {
Ref<Script> script = ResourceLoader::load(p_class_or_script_path, "Script"); Ref<Script> script = ResourceLoader::load(p_class_or_script_path, "Script");
Ref<Script> base_script = script; return EditorNode::get_singleton()->get_object_icon(script.ptr(), "BTTask");
while (base_script.is_valid()) {
StringName name = EditorNode::get_editor_data().script_class_get_name(base_script->get_path());
String icon_path = EditorNode::get_editor_data().script_class_get_icon_path(name);
if (!icon_path.is_empty()) {
Ref<Image> img = memnew(Image);
Error err = ImageLoader::load_image(icon_path, img);
if (err == OK) {
Ref<ImageTexture> icon = memnew(ImageTexture);
img->resize(16 * EDSCALE, 16 * EDSCALE, Image::INTERPOLATE_LANCZOS);
icon->create_from_image(img);
return icon;
} }
}
base_script = base_script->get_base_script();
}
base_type = script->get_instance_base_type();
}
// TODO: Walk inheritance tree until icon is found. // TODO: Walk inheritance tree until icon is found.
return EditorNode::get_singleton()->get_class_icon(base_type, "BTTask"); return EditorNode::get_singleton()->get_class_icon(p_class_or_script_path, "BTTask");
} }
void LimboUtility::_bind_methods() { void LimboUtility::_bind_methods() {

View File

@ -1,5 +1,6 @@
@tool @tool
@icon("res://icon.png") @icon("res://icon.png")
class_name ArrivePos
extends BTAction extends BTAction
@export var target_position_var := "target_position" @export var target_position_var := "target_position"