2022-08-28 10:54:34 +00:00
|
|
|
/* limbo_utility.cpp */
|
|
|
|
|
|
|
|
#include "limbo_utility.h"
|
2022-11-01 13:03:20 +00:00
|
|
|
#include "bt/bt_task.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/variant/variant.h"
|
2023-04-13 07:29:45 +00:00
|
|
|
#include "editor/editor_node.h"
|
|
|
|
#include "scene/resources/texture.h"
|
2022-08-28 10:54:34 +00:00
|
|
|
|
2022-09-21 21:56:04 +00:00
|
|
|
LimboUtility *LimboUtility::singleton = nullptr;
|
|
|
|
|
|
|
|
LimboUtility *LimboUtility::get_singleton() {
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
2022-11-01 13:03:20 +00:00
|
|
|
String LimboUtility::decorate_var(String p_variable) const {
|
2022-09-21 21:56:04 +00:00
|
|
|
String var = p_variable.trim_prefix("$").trim_prefix("\"").trim_suffix("\"");
|
2022-12-15 07:26:52 +00:00
|
|
|
if (var.find(" ") == -1 and not var.is_empty()) {
|
2022-09-21 21:56:04 +00:00
|
|
|
return vformat("$%s", var);
|
|
|
|
} else {
|
|
|
|
return vformat("$\"%s\"", var);
|
2022-08-28 10:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 13:03:20 +00:00
|
|
|
String LimboUtility::get_status_name(int p_status) const {
|
|
|
|
switch (p_status) {
|
|
|
|
case BTTask::FRESH:
|
|
|
|
return "FRESH";
|
|
|
|
case BTTask::RUNNING:
|
|
|
|
return "RUNNING";
|
|
|
|
case BTTask::FAILURE:
|
|
|
|
return "FAILURE";
|
|
|
|
case BTTask::SUCCESS:
|
|
|
|
return "SUCCESS";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 07:29:45 +00:00
|
|
|
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.");
|
|
|
|
|
|
|
|
if (p_class_or_script_path.begins_with("res:")) {
|
|
|
|
Ref<Script> script = ResourceLoader::load(p_class_or_script_path, "Script");
|
2023-04-16 06:26:47 +00:00
|
|
|
return EditorNode::get_singleton()->get_object_icon(script.ptr(), "BTTask");
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
|
|
|
// TODO: Walk inheritance tree until icon is found.
|
2023-04-16 06:26:47 +00:00
|
|
|
return EditorNode::get_singleton()->get_class_icon(p_class_or_script_path, "BTTask");
|
2023-04-13 07:29:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 10:54:34 +00:00
|
|
|
void LimboUtility::_bind_methods() {
|
2022-09-21 21:56:04 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("decorate_var", "p_variable"), &LimboUtility::decorate_var);
|
2022-11-01 13:03:20 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_status_name", "p_status"), &LimboUtility::get_status_name);
|
2023-04-13 07:29:45 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_task_icon"), &LimboUtility::get_task_icon);
|
2022-09-21 21:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LimboUtility::LimboUtility() {
|
|
|
|
singleton = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
LimboUtility::~LimboUtility() {
|
|
|
|
singleton = nullptr;
|
2022-08-28 10:54:34 +00:00
|
|
|
}
|