limboai/util/limbo_utility.cpp

405 lines
12 KiB
C++
Raw Permalink Normal View History

/**
* limbo_utility.cpp
* =============================================================================
* Copyright 2021-2023 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.
* =============================================================================
*/
2022-08-28 10:54:34 +00:00
#include "limbo_utility.h"
2023-07-20 16:35:36 +00:00
2024-01-13 16:10:42 +00:00
#include "../bt/tasks/bt_task.h"
#include "../util/limbo_compat.h"
2023-07-20 16:35:36 +00:00
2024-01-13 16:10:42 +00:00
#ifdef LIMBOAI_MODULE
#include "core/error/error_macros.h"
2023-10-28 11:09:03 +00:00
#include "core/object/script_language.h"
2024-01-13 21:40:27 +00:00
#include "core/os/os.h"
#include "core/variant/variant.h"
#include "scene/resources/texture.h"
2022-08-28 10:54:34 +00:00
2023-04-26 12:45:03 +00:00
#ifdef TOOLS_ENABLED
#include "editor/editor_node.h"
#endif // TOOLS_ENABLED
2024-01-13 16:10:42 +00:00
#endif // ! LIMBOAI_MODULE
#ifdef LIMBOAI_GDEXTENSION
2024-01-09 12:34:24 +00:00
#include "godot_cpp/classes/input_event_key.hpp"
#include "godot_cpp/classes/project_settings.hpp"
#include "godot_cpp/variant/dictionary.hpp"
2024-01-09 12:34:24 +00:00
#include "godot_cpp/variant/utility_functions.hpp"
2024-01-13 21:40:27 +00:00
#include <godot_cpp/classes/os.hpp>
2024-01-07 05:51:34 +00:00
#include <godot_cpp/classes/resource_loader.hpp>
#include <godot_cpp/classes/script.hpp>
2024-01-10 00:17:09 +00:00
#include <godot_cpp/classes/texture2d.hpp>
2024-01-07 05:51:34 +00:00
#include <godot_cpp/classes/theme.hpp>
#include <godot_cpp/core/error_macros.hpp>
2024-01-13 16:10:42 +00:00
#endif // ! LIMBOAI_GDEXTENSION
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("\"");
2023-07-28 22:35:47 +00:00
if (var.find(" ") == -1 && !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 "";
}
}
Ref<Texture2D> LimboUtility::get_task_icon(String p_class_or_script_path) const {
2024-01-07 05:51:34 +00:00
#if defined(TOOLS_ENABLED) && defined(LIMBOAI_MODULE)
ERR_FAIL_COND_V_MSG(p_class_or_script_path.is_empty(), Variant(), "BTTask: script path or class cannot be empty.");
Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
ERR_FAIL_COND_V(theme.is_null(), nullptr);
if (p_class_or_script_path.begins_with("res:")) {
2023-07-20 18:10:02 +00:00
Ref<Script> s = ResourceLoader::load(p_class_or_script_path, "Script");
if (s.is_null()) {
return theme->get_icon(SNAME("FileBroken"), SNAME("EditorIcons"));
}
EditorData &ed = EditorNode::get_editor_data();
Ref<Texture2D> script_icon = ed.get_script_icon(s);
if (script_icon.is_valid()) {
return script_icon;
}
StringName base_type = s->get_instance_base_type();
if (theme->has_icon(base_type, SNAME("EditorIcons"))) {
return theme->get_icon(base_type, SNAME("EditorIcons"));
}
}
2023-08-09 13:38:24 +00:00
if (theme->has_icon(p_class_or_script_path, SNAME("EditorIcons"))) {
return theme->get_icon(p_class_or_script_path, SNAME("EditorIcons"));
2023-08-09 13:38:24 +00:00
}
// Use an icon of one of the base classes: look up max 3 parents.
StringName class_name = p_class_or_script_path;
for (int i = 0; i < 3; i++) {
class_name = ClassDB::get_parent_class(class_name);
if (theme->has_icon(class_name, SNAME("EditorIcons"))) {
return theme->get_icon(class_name, SNAME("EditorIcons"));
2023-08-09 13:38:24 +00:00
}
}
// Return generic resource icon as a fallback.
return theme->get_icon(SNAME("Resource"), SNAME("EditorIcons"));
2024-01-10 00:17:09 +00:00
#endif // ! TOOLS_ENABLED && LIMBOAI_MODULE
#ifdef LIMBOAI_GDEXTENSION
String path;
if (p_class_or_script_path.begins_with("res://")) {
TypedArray<Dictionary> classes = ProjectSettings::get_singleton()->get_global_class_list();
for (int i = 0; i < classes.size(); i++) {
if (classes[i].get("path") == p_class_or_script_path) {
path = classes[i].get("icon");
break;
}
}
if (path.is_empty()) {
Ref<Script> sc = RESOURCE_LOAD(p_class_or_script_path, "Script");
if (sc.is_valid()) {
path = "res://addons/limboai/icons/" + sc->get_instance_base_type() + ".svg";
}
}
2024-01-10 00:17:09 +00:00
} else {
// Assuming global class.
2024-01-10 00:17:09 +00:00
path = "res://addons/limboai/icons/" + p_class_or_script_path + ".svg";
}
Ref<Texture2D> icon = RESOURCE_LOAD(path, "Texture2D");
return icon;
#endif // LIMBOAI_GDEXTENSION
2023-08-09 13:38:24 +00:00
2024-01-07 12:36:59 +00:00
// TODO: GDExtension needs the icons too.
2023-08-09 13:38:24 +00:00
// * Class icons are not available at runtime as they are part of the editor theme.
2023-04-26 12:45:03 +00:00
return nullptr;
}
String LimboUtility::get_check_operator_string(CheckType p_check_type) const {
2023-08-10 11:04:53 +00:00
switch (p_check_type) {
case LimboUtility::CheckType::CHECK_EQUAL: {
return "==";
} break;
case LimboUtility::CheckType::CHECK_LESS_THAN: {
return "<";
} break;
case LimboUtility::CheckType::CHECK_LESS_THAN_OR_EQUAL: {
return "<=";
} break;
case LimboUtility::CheckType::CHECK_GREATER_THAN: {
return ">";
} break;
case LimboUtility::CheckType::CHECK_GREATER_THAN_OR_EQUAL: {
return ">=";
} break;
case LimboUtility::CheckType::CHECK_NOT_EQUAL: {
return "!=";
} break;
default: {
return "?";
} break;
}
}
bool LimboUtility::perform_check(CheckType p_check_type, const Variant &left_value, const Variant &right_value) {
Variant ret;
2023-08-10 11:04:53 +00:00
switch (p_check_type) {
case LimboUtility::CheckType::CHECK_EQUAL: {
VARIANT_EVALUATE(Variant::OP_EQUAL, left_value, right_value, ret);
2023-08-10 11:04:53 +00:00
} break;
case LimboUtility::CheckType::CHECK_LESS_THAN: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_LESS, left_value, right_value, ret);
2023-08-10 11:04:53 +00:00
} break;
case LimboUtility::CheckType::CHECK_LESS_THAN_OR_EQUAL: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_LESS_EQUAL, left_value, right_value, ret);
2023-08-10 11:04:53 +00:00
} break;
case LimboUtility::CheckType::CHECK_GREATER_THAN: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_GREATER, left_value, right_value, ret);
2023-08-10 11:04:53 +00:00
} break;
case LimboUtility::CheckType::CHECK_GREATER_THAN_OR_EQUAL: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_GREATER_EQUAL, left_value, right_value, ret);
2023-08-10 11:04:53 +00:00
} break;
case LimboUtility::CheckType::CHECK_NOT_EQUAL: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_NOT_EQUAL, left_value, right_value, ret);
2023-08-10 11:04:53 +00:00
} break;
default: {
return false;
} break;
}
return ret;
2023-08-10 11:04:53 +00:00
}
String LimboUtility::get_operation_string(Operation p_operation) const {
switch (p_operation) {
case OPERATION_NONE: {
return "";
} break;
case OPERATION_ADDITION: {
return "+";
} break;
case OPERATION_SUBTRACTION: {
return "-";
} break;
case OPERATION_MULTIPLICATION: {
return "*";
} break;
case OPERATION_DIVISION: {
return "/";
} break;
case OPERATION_MODULO: {
return "%";
} break;
case OPERATION_POWER: {
return "**";
} break;
case OPERATION_BIT_SHIFT_LEFT: {
return "<<";
} break;
case OPERATION_BIT_SHIFT_RIGHT: {
return ">>";
} break;
case OPERATION_BIT_AND: {
return "&";
} break;
case OPERATION_BIT_OR: {
return "|";
} break;
case OPERATION_BIT_XOR: {
return "^";
} break;
}
2023-11-19 15:26:00 +00:00
return "";
}
Variant LimboUtility::perform_operation(Operation p_operation, const Variant &left_value, const Variant &right_value) {
Variant ret;
switch (p_operation) {
case OPERATION_NONE: {
ret = right_value;
} break;
case OPERATION_ADDITION: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_ADD, left_value, right_value, ret);
} break;
case OPERATION_SUBTRACTION: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_SUBTRACT, left_value, right_value, ret);
} break;
case OPERATION_MULTIPLICATION: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_MULTIPLY, left_value, right_value, ret);
} break;
case OPERATION_DIVISION: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_DIVIDE, left_value, right_value, ret);
} break;
case OPERATION_MODULO: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_MODULE, left_value, right_value, ret);
} break;
case OPERATION_POWER: {
2024-01-10 22:26:24 +00:00
// TODO: Fix when godot-cpp https://github.com/godotengine/godot-cpp/issues/1348 is resolved.
#ifdef LIMBOAI_MODULE
VARIANT_EVALUATE(Variant::OP_POWER, left_value, right_value, ret);
2024-01-13 16:10:42 +00:00
#elif LIMBOAI_GDEXTENSION
2024-01-09 12:34:24 +00:00
ERR_PRINT("LimboUtility: Operation POWER is not available due to https://github.com/godotengine/godot-cpp/issues/1348");
2024-01-10 22:26:24 +00:00
ret = left_value;
2024-01-13 16:10:42 +00:00
#endif
} break;
case OPERATION_BIT_SHIFT_LEFT: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_SHIFT_LEFT, left_value, right_value, ret);
} break;
case OPERATION_BIT_SHIFT_RIGHT: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_SHIFT_RIGHT, left_value, right_value, ret);
} break;
case OPERATION_BIT_AND: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_BIT_AND, left_value, right_value, ret);
} break;
case OPERATION_BIT_OR: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_BIT_OR, left_value, right_value, ret);
} break;
case OPERATION_BIT_XOR: {
2024-01-09 12:34:24 +00:00
VARIANT_EVALUATE(Variant::OP_BIT_XOR, left_value, right_value, ret);
} break;
}
2024-01-10 22:26:24 +00:00
return ret;
2024-01-09 12:34:24 +00:00
}
#ifdef TOOLS_ENABLED
2024-01-09 12:34:24 +00:00
Ref<Shortcut> LimboUtility::add_shortcut(const String &p_path, const String &p_name, Key p_keycode) {
Ref<Shortcut> sc = memnew(Shortcut);
sc->set_name(p_name);
2024-01-09 12:34:24 +00:00
Array events;
2024-01-14 20:48:15 +00:00
Key keycode = p_keycode;
2024-01-09 12:34:24 +00:00
Ref<InputEventKey> ev = memnew(InputEventKey);
2024-01-14 20:48:15 +00:00
if (((int)LW_KEY_MASK(CMD_OR_CTRL) & (int)keycode) == (int)LW_KEY_MASK(CMD_OR_CTRL)) {
keycode = (Key)((int)keycode & (~((int)LW_KEY_MASK(CMD_OR_CTRL))));
ev->set_ctrl_pressed(true);
}
if (((int)LW_KEY_MASK(ALT) & (int)keycode) == (int)LW_KEY_MASK(ALT)) {
keycode = (Key)((int)keycode & (~((int)LW_KEY_MASK(ALT))));
ev->set_alt_pressed(true);
}
if (((int)LW_KEY_MASK(SHIFT) & (int)keycode) == (int)LW_KEY_MASK(SHIFT)) {
keycode = (Key)((int)keycode & (~((int)LW_KEY_MASK(SHIFT))));
ev->set_shift_pressed(true);
}
ev->set_keycode(keycode);
ev->set_pressed(true);
2024-01-09 12:34:24 +00:00
events.append(ev);
sc->set_events(events);
shortcuts[p_path] = sc;
return sc;
}
bool LimboUtility::is_shortcut(const String &p_path, const Ref<InputEvent> &p_event) const {
HashMap<String, Ref<Shortcut>>::ConstIterator E = shortcuts.find(p_path);
ERR_FAIL_COND_V_MSG(!E, false, vformat("LimboUtility: Shortcut not found: %s.", p_path));
return E->value->matches_event(p_event);
}
Ref<Shortcut> LimboUtility::get_shortcut(const String &p_path) const {
HashMap<String, Ref<Shortcut>>::ConstIterator SC = shortcuts.find(p_path);
if (SC) {
return SC->value;
}
return nullptr;
}
2024-01-13 21:40:27 +00:00
void LimboUtility::open_doc_introduction() {
OS::get_singleton()->shell_open(vformat("https://limboai.readthedocs.io/en/%s/getting-started/introduction.html",
2024-01-13 21:40:27 +00:00
LIMBO_DOC_VERSION));
}
void LimboUtility::open_doc_online() {
OS::get_singleton()->shell_open(vformat("https://limboai.readthedocs.io/en/%s/index.html",
LIMBO_DOC_VERSION));
}
void LimboUtility::open_doc_gdextension_limitations() {
OS::get_singleton()->shell_open(vformat("https://limboai.readthedocs.io/en/%s/getting-started/gdextension.html#limitations-of-the-gdextension-version",
LIMBO_DOC_VERSION));
}
void LimboUtility::open_doc_custom_tasks() {
OS::get_singleton()->shell_open(vformat("https://limboai.readthedocs.io/en/%s/getting-started/custom-tasks.html",
LIMBO_DOC_VERSION));
}
2024-01-13 21:40:27 +00:00
void LimboUtility::open_doc_class(const String &p_class_name) {
if (p_class_name.begins_with("res://")) {
SHOW_DOC(vformat("class_name:\"%s\"", p_class_name));
return;
}
#ifdef LIMBOAI_MODULE
SHOW_DOC("class_name:" + p_class_name);
#elif LIMBOAI_GDEXTENSION
OS::get_singleton()->shell_open(vformat("https://limboai.readthedocs.io/en/%s/classes/class_%s.html",
LIMBO_DOC_VERSION, p_class_name.to_lower()));
#endif
}
#endif // ! TOOLS_ENABLED
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-07-20 20:15:30 +00:00
ClassDB::bind_method(D_METHOD("get_task_icon", "p_class_or_script_path"), &LimboUtility::get_task_icon);
2023-08-10 11:04:53 +00:00
BIND_ENUM_CONSTANT(CHECK_EQUAL);
BIND_ENUM_CONSTANT(CHECK_LESS_THAN);
BIND_ENUM_CONSTANT(CHECK_LESS_THAN_OR_EQUAL);
BIND_ENUM_CONSTANT(CHECK_GREATER_THAN);
BIND_ENUM_CONSTANT(CHECK_GREATER_THAN_OR_EQUAL);
BIND_ENUM_CONSTANT(CHECK_NOT_EQUAL);
BIND_ENUM_CONSTANT(OPERATION_NONE);
BIND_ENUM_CONSTANT(OPERATION_ADDITION);
BIND_ENUM_CONSTANT(OPERATION_SUBTRACTION);
BIND_ENUM_CONSTANT(OPERATION_MULTIPLICATION);
BIND_ENUM_CONSTANT(OPERATION_DIVISION);
BIND_ENUM_CONSTANT(OPERATION_MODULO);
BIND_ENUM_CONSTANT(OPERATION_POWER);
BIND_ENUM_CONSTANT(OPERATION_BIT_SHIFT_LEFT);
BIND_ENUM_CONSTANT(OPERATION_BIT_SHIFT_RIGHT);
BIND_ENUM_CONSTANT(OPERATION_BIT_AND);
BIND_ENUM_CONSTANT(OPERATION_BIT_OR);
BIND_ENUM_CONSTANT(OPERATION_BIT_XOR);
2022-09-21 21:56:04 +00:00
}
LimboUtility::LimboUtility() {
singleton = this;
}
LimboUtility::~LimboUtility() {
singleton = nullptr;
2022-08-28 10:54:34 +00:00
}