2022-09-01 22:20:37 +00:00
|
|
|
/* limbo_ai_editor_plugin.cpp */
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
|
|
|
#include "limbo_ai_editor_plugin.h"
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/config/project_settings.h"
|
|
|
|
#include "core/error/error_list.h"
|
|
|
|
#include "core/error/error_macros.h"
|
2022-09-06 11:28:25 +00:00
|
|
|
#include "core/io/config_file.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/io/dir_access.h"
|
2022-09-05 14:39:40 +00:00
|
|
|
#include "core/io/image_loader.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/io/resource.h"
|
2022-09-01 22:20:37 +00:00
|
|
|
#include "core/io/resource_loader.h"
|
|
|
|
#include "core/io/resource_saver.h"
|
|
|
|
#include "core/math/math_defs.h"
|
2022-11-23 15:25:32 +00:00
|
|
|
#include "core/math/vector2.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/object/callable_method_pointer.h"
|
|
|
|
#include "core/object/class_db.h"
|
|
|
|
#include "core/object/object.h"
|
|
|
|
#include "core/object/script_language.h"
|
2022-09-01 22:20:37 +00:00
|
|
|
#include "core/os/memory.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/string/print_string.h"
|
|
|
|
#include "core/string/string_name.h"
|
|
|
|
#include "core/string/ustring.h"
|
|
|
|
#include "core/templates/list.h"
|
|
|
|
#include "core/templates/vector.h"
|
2022-09-02 13:43:54 +00:00
|
|
|
#include "core/typedefs.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/variant/array.h"
|
|
|
|
#include "core/variant/dictionary.h"
|
|
|
|
#include "core/variant/variant.h"
|
|
|
|
#include "editor/editor_file_system.h"
|
2022-09-05 19:57:24 +00:00
|
|
|
#include "editor/editor_inspector.h"
|
2022-09-01 22:20:37 +00:00
|
|
|
#include "editor/editor_node.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "editor/editor_paths.h"
|
2022-09-01 22:20:37 +00:00
|
|
|
#include "editor/editor_plugin.h"
|
2022-09-05 14:39:40 +00:00
|
|
|
#include "editor/editor_scale.h"
|
2022-09-06 11:28:25 +00:00
|
|
|
#include "editor/editor_settings.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "editor/inspector_dock.h"
|
|
|
|
#include "editor/plugins/script_editor_plugin.h"
|
2022-09-02 22:10:22 +00:00
|
|
|
#include "modules/limboai/bt/actions/bt_action.h"
|
2022-09-02 13:43:54 +00:00
|
|
|
#include "modules/limboai/bt/behavior_tree.h"
|
2022-09-02 22:10:22 +00:00
|
|
|
#include "modules/limboai/bt/bt_task.h"
|
|
|
|
#include "modules/limboai/bt/composites/bt_parallel.h"
|
|
|
|
#include "modules/limboai/bt/composites/bt_selector.h"
|
|
|
|
#include "modules/limboai/bt/composites/bt_sequence.h"
|
2022-09-01 22:20:37 +00:00
|
|
|
#include "scene/gui/box_container.h"
|
2022-09-02 12:25:03 +00:00
|
|
|
#include "scene/gui/button.h"
|
2022-09-05 11:31:38 +00:00
|
|
|
#include "scene/gui/control.h"
|
2022-09-01 22:20:37 +00:00
|
|
|
#include "scene/gui/file_dialog.h"
|
2022-09-02 12:25:03 +00:00
|
|
|
#include "scene/gui/flow_container.h"
|
2022-09-01 22:20:37 +00:00
|
|
|
#include "scene/gui/label.h"
|
2022-09-02 12:25:03 +00:00
|
|
|
#include "scene/gui/line_edit.h"
|
2022-09-01 22:20:37 +00:00
|
|
|
#include "scene/gui/popup_menu.h"
|
2022-09-02 12:25:03 +00:00
|
|
|
#include "scene/gui/scroll_container.h"
|
2022-09-01 22:20:37 +00:00
|
|
|
#include "scene/gui/separator.h"
|
2022-09-02 12:25:03 +00:00
|
|
|
#include "scene/gui/split_container.h"
|
2022-09-01 22:20:37 +00:00
|
|
|
#include "scene/gui/tree.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "servers/display_server.h"
|
2022-09-01 22:20:37 +00:00
|
|
|
|
2022-09-03 15:00:20 +00:00
|
|
|
////////////////////////////// TaskTree //////////////////////////////////////
|
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
TreeItem *TaskTree::_create_tree(const Ref<BTTask> &p_task, TreeItem *p_parent, int p_idx) {
|
|
|
|
ERR_FAIL_COND_V(p_task.is_null(), nullptr);
|
|
|
|
TreeItem *item = tree->create_item(p_parent, p_idx);
|
|
|
|
item->set_metadata(0, p_task);
|
|
|
|
// p_task->connect("changed"...)
|
|
|
|
for (int i = 0; i < p_task->get_child_count(); i++) {
|
|
|
|
_create_tree(p_task->get_child(i), item);
|
|
|
|
}
|
|
|
|
_update_item(item);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskTree::_update_item(TreeItem *p_item) {
|
|
|
|
ERR_FAIL_COND_MSG(p_item == nullptr, "Argument \"p_item\" is null.");
|
|
|
|
Ref<BTTask> task = p_item->get_metadata(0);
|
|
|
|
ERR_FAIL_COND_MSG(!task.is_valid(), "Invalid task reference in metadata.");
|
|
|
|
p_item->set_text(0, task->get_task_name());
|
2022-12-15 07:26:52 +00:00
|
|
|
if (task->get_script_instance() && !task->get_script_instance()->get_script()->get_path().is_empty()) {
|
2022-09-05 14:39:40 +00:00
|
|
|
p_item->set_icon(0, LimboAIEditor::get_task_icon(task->get_script_instance()->get_script()->get_path()));
|
|
|
|
} else {
|
|
|
|
p_item->set_icon(0, LimboAIEditor::get_task_icon(task->get_class()));
|
|
|
|
}
|
2022-09-01 22:20:37 +00:00
|
|
|
p_item->set_editable(0, false);
|
|
|
|
|
2022-09-03 11:11:50 +00:00
|
|
|
for (int i = 0; i < p_item->get_button_count(0); i++) {
|
|
|
|
p_item->erase_button(0, i);
|
|
|
|
}
|
|
|
|
String warning = task->get_configuration_warning();
|
2022-12-15 07:26:52 +00:00
|
|
|
if (!warning.is_empty()) {
|
|
|
|
p_item->add_button(0, get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons")), 0, false, warning);
|
2022-09-03 11:11:50 +00:00
|
|
|
}
|
2022-09-01 22:20:37 +00:00
|
|
|
|
|
|
|
// TODO: Update probabilities.
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskTree::_update_tree() {
|
|
|
|
Ref<BTTask> sel;
|
|
|
|
if (tree->get_selected()) {
|
|
|
|
sel = tree->get_selected()->get_metadata(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
tree->clear();
|
|
|
|
if (bt->get_root_task().is_valid()) {
|
|
|
|
_create_tree(bt->get_root_task(), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeItem *item = _find_item(sel);
|
|
|
|
if (item) {
|
|
|
|
item->select(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeItem *TaskTree::_find_item(const Ref<BTTask> &p_task) const {
|
|
|
|
if (p_task.is_null()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
TreeItem *item = tree->get_root();
|
|
|
|
List<TreeItem *> stack;
|
|
|
|
while (item && item->get_metadata(0) != p_task) {
|
2022-12-15 07:26:52 +00:00
|
|
|
if (item->get_child_count() > 0) {
|
|
|
|
stack.push_back(item->get_first_child());
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
item = item->get_next();
|
2022-12-15 07:26:52 +00:00
|
|
|
if (item == nullptr && !stack.is_empty()) {
|
2022-09-01 22:20:37 +00:00
|
|
|
item = stack.front()->get();
|
|
|
|
stack.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
void TaskTree::_on_item_mouse_selected(const Vector2 &p_pos, int p_button_index) {
|
|
|
|
if (p_button_index == 2) {
|
2022-12-15 18:14:36 +00:00
|
|
|
emit_signal(SNAME("rmb_pressed"), get_screen_position() + p_pos);
|
2022-12-15 07:26:52 +00:00
|
|
|
}
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TaskTree::_on_item_selected() {
|
|
|
|
if (last_selected.is_valid()) {
|
|
|
|
update_task(last_selected);
|
2022-12-15 07:26:52 +00:00
|
|
|
if (last_selected->is_connected("changed", callable_mp(this, &TaskTree::_on_task_changed))) {
|
|
|
|
last_selected->disconnect("changed", callable_mp(this, &TaskTree::_on_task_changed));
|
2022-09-03 15:13:15 +00:00
|
|
|
}
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
last_selected = get_selected();
|
2022-12-15 07:26:52 +00:00
|
|
|
last_selected->connect("changed", callable_mp(this, &TaskTree::_on_task_changed));
|
2022-12-15 18:14:36 +00:00
|
|
|
emit_signal(SNAME("task_selected"), last_selected);
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 15:25:32 +00:00
|
|
|
void TaskTree::_on_item_double_clicked() {
|
2022-12-15 18:14:36 +00:00
|
|
|
emit_signal(SNAME("task_double_clicked"));
|
2022-11-23 15:25:32 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 15:13:15 +00:00
|
|
|
void TaskTree::_on_task_changed() {
|
|
|
|
_update_item(tree->get_selected());
|
|
|
|
}
|
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
void TaskTree::load_bt(const Ref<BehaviorTree> &p_behavior_tree) {
|
|
|
|
ERR_FAIL_COND_MSG(p_behavior_tree.is_null(), "Tried to load a null tree.");
|
2022-09-03 15:13:15 +00:00
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
if (last_selected.is_valid() and last_selected->is_connected("changed", callable_mp(this, &TaskTree::_on_task_changed))) {
|
|
|
|
last_selected->disconnect("changed", callable_mp(this, &TaskTree::_on_task_changed));
|
2022-09-03 15:13:15 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
bt = p_behavior_tree;
|
|
|
|
tree->clear();
|
|
|
|
if (bt->get_root_task().is_valid()) {
|
|
|
|
_create_tree(bt->get_root_task(), nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 09:29:46 +00:00
|
|
|
void TaskTree::unload() {
|
|
|
|
bt->unreference();
|
|
|
|
tree->clear();
|
|
|
|
}
|
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
void TaskTree::update_task(const Ref<BTTask> &p_task) {
|
|
|
|
ERR_FAIL_COND(p_task.is_null());
|
|
|
|
TreeItem *item = _find_item(p_task);
|
|
|
|
if (item) {
|
|
|
|
_update_item(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<BTTask> TaskTree::get_selected() const {
|
|
|
|
if (tree->get_selected()) {
|
|
|
|
return tree->get_selected()->get_metadata(0);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskTree::deselect() {
|
|
|
|
TreeItem *sel = tree->get_selected();
|
|
|
|
if (sel) {
|
|
|
|
sel->deselect(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
Variant TaskTree::_get_drag_data_fw(const Point2 &p_point, Control *p_from) {
|
2022-09-03 15:00:20 +00:00
|
|
|
if (editable && tree->get_item_at_position(p_point)) {
|
|
|
|
Dictionary drag_data;
|
|
|
|
drag_data["type"] = "task";
|
|
|
|
drag_data["task"] = tree->get_item_at_position(p_point)->get_metadata(0);
|
|
|
|
tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
|
|
|
|
return drag_data;
|
|
|
|
}
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
bool TaskTree::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
|
2022-09-03 15:00:20 +00:00
|
|
|
if (!editable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary d = p_data;
|
|
|
|
if (!d.has("type") || !d.has("task")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int section = tree->get_drop_section_at_position(p_point);
|
|
|
|
TreeItem *item = tree->get_item_at_position(p_point);
|
|
|
|
if (!item || section < -1 || (section == -1 && !item->get_parent())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (String(d["type"]) == "task") {
|
|
|
|
Ref<BTTask> task = d["task"];
|
|
|
|
const Ref<BTTask> to_task = item->get_metadata(0);
|
|
|
|
if (task != to_task && !to_task->is_descendant_of(task)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
void TaskTree::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
|
2022-09-03 15:00:20 +00:00
|
|
|
Dictionary d = p_data;
|
|
|
|
TreeItem *item = tree->get_item_at_position(p_point);
|
|
|
|
if (item && d.has("task")) {
|
|
|
|
Ref<BTTask> task = d["task"];
|
2022-12-15 18:14:36 +00:00
|
|
|
emit_signal(SNAME("task_dragged"), task, item->get_metadata(0), tree->get_drop_section_at_position(p_point));
|
2022-09-03 15:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
void TaskTree::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("load_bt", "p_behavior_tree"), &TaskTree::load_bt);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bt"), &TaskTree::get_bt);
|
|
|
|
ClassDB::bind_method(D_METHOD("update_tree"), &TaskTree::update_tree);
|
|
|
|
ClassDB::bind_method(D_METHOD("update_task", "p_task"), &TaskTree::update_task);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_selected"), &TaskTree::get_selected);
|
|
|
|
ClassDB::bind_method(D_METHOD("deselect"), &TaskTree::deselect);
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_get_drag_data_fw"), &TaskTree::_get_drag_data_fw);
|
|
|
|
ClassDB::bind_method(D_METHOD("_can_drop_data_fw"), &TaskTree::_can_drop_data_fw);
|
|
|
|
ClassDB::bind_method(D_METHOD("_drop_data_fw"), &TaskTree::_drop_data_fw);
|
2022-09-03 15:00:20 +00:00
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("rmb_pressed"));
|
|
|
|
ADD_SIGNAL(MethodInfo("task_selected"));
|
2022-11-23 15:25:32 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("task_double_clicked"));
|
2022-09-03 15:00:20 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("task_dragged",
|
|
|
|
PropertyInfo(Variant::OBJECT, "p_task", PROPERTY_HINT_RESOURCE_TYPE, "BTTask"),
|
|
|
|
PropertyInfo(Variant::OBJECT, "p_to_task", PROPERTY_HINT_RESOURCE_TYPE, "BTTask"),
|
|
|
|
PropertyInfo(Variant::INT, "p_type")));
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TaskTree::TaskTree() {
|
2022-09-03 15:00:20 +00:00
|
|
|
editable = true;
|
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
tree = memnew(Tree);
|
|
|
|
add_child(tree);
|
|
|
|
tree->set_columns(2);
|
|
|
|
tree->set_column_expand(0, true);
|
|
|
|
tree->set_column_expand(1, false);
|
2022-12-15 07:26:52 +00:00
|
|
|
tree->set_column_custom_minimum_width(1, 64);
|
|
|
|
tree->set_anchor(SIDE_RIGHT, ANCHOR_END);
|
|
|
|
tree->set_anchor(SIDE_BOTTOM, ANCHOR_END);
|
2022-09-01 22:20:37 +00:00
|
|
|
tree->set_allow_rmb_select(true);
|
2022-12-15 07:26:52 +00:00
|
|
|
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_double_clicked));
|
|
|
|
|
2022-09-03 15:00:20 +00:00
|
|
|
tree->set_drag_forwarding(this);
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TaskTree::~TaskTree() {
|
2022-12-15 07:26:52 +00:00
|
|
|
if (last_selected.is_valid() and last_selected->is_connected("changed", callable_mp(this, &TaskTree::_on_task_changed))) {
|
|
|
|
last_selected->disconnect("changed", callable_mp(this, &TaskTree::_on_task_changed));
|
2022-09-03 15:13:15 +00:00
|
|
|
}
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 15:00:20 +00:00
|
|
|
////////////////////////////// TaskTree //////////////////////////////////////
|
|
|
|
|
|
|
|
////////////////////////////// TaskSection ////////////////////////////////////
|
2022-09-01 22:20:37 +00:00
|
|
|
|
2022-09-02 12:25:03 +00:00
|
|
|
void TaskSection::_on_task_button_pressed(const StringName &p_task) {
|
2022-12-15 18:14:36 +00:00
|
|
|
emit_signal(SNAME("task_button_pressed"), p_task);
|
2022-09-02 12:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TaskSection::_on_header_pressed() {
|
|
|
|
tasks_container->set_visible(!tasks_container->is_visible());
|
2022-12-15 07:26:52 +00:00
|
|
|
section_header->set_icon(tasks_container->is_visible() ? get_theme_icon(SNAME("GuiTreeArrowDown"), SNAME("EditorIcons")) : get_theme_icon(SNAME("GuiTreeArrowRight"), SNAME("EditorIcons")));
|
2022-09-02 12:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TaskSection::set_filter(String p_filter_text) {
|
|
|
|
int num_hidden = 0;
|
2022-12-15 07:26:52 +00:00
|
|
|
if (p_filter_text.is_empty()) {
|
2022-09-02 12:25:03 +00:00
|
|
|
for (int i = 0; i < tasks_container->get_child_count(); i++) {
|
|
|
|
Object::cast_to<Button>(tasks_container->get_child(i))->show();
|
|
|
|
}
|
|
|
|
set_visible(tasks_container->get_child_count() > 0);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < tasks_container->get_child_count(); i++) {
|
|
|
|
Button *btn = Object::cast_to<Button>(tasks_container->get_child(i));
|
|
|
|
btn->set_visible(btn->get_text().findn(p_filter_text) != -1);
|
|
|
|
num_hidden += !btn->is_visible();
|
|
|
|
}
|
|
|
|
set_visible(num_hidden < tasks_container->get_child_count());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 17:30:32 +00:00
|
|
|
void TaskSection::add_task_button(String p_name, const Ref<Texture> &icon, Variant p_meta) {
|
|
|
|
Button *btn = memnew(Button);
|
|
|
|
btn->set_text(p_name);
|
|
|
|
btn->set_icon(icon);
|
2022-12-15 07:26:52 +00:00
|
|
|
btn->connect("pressed", callable_mp(this, &TaskSection::_on_task_button_pressed).bind(p_meta));
|
2022-09-02 17:30:32 +00:00
|
|
|
tasks_container->add_child(btn);
|
|
|
|
}
|
|
|
|
|
2022-11-23 00:04:35 +00:00
|
|
|
void TaskSection::set_collapsed(bool p_collapsed) {
|
|
|
|
tasks_container->set_visible(!p_collapsed);
|
2022-12-15 07:26:52 +00:00
|
|
|
section_header->set_icon(p_collapsed ? get_theme_icon(SNAME("GuiTreeArrowRight"), SNAME("EditorIcons")) : get_theme_icon(SNAME("GuiTreeArrowDown"), SNAME("EditorIcons")));
|
2022-11-23 00:04:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TaskSection::is_collapsed() const {
|
|
|
|
return !tasks_container->is_visible();
|
|
|
|
}
|
|
|
|
|
2022-09-02 12:25:03 +00:00
|
|
|
void TaskSection::_bind_methods() {
|
|
|
|
ADD_SIGNAL(MethodInfo("task_button_pressed"));
|
|
|
|
}
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
TaskSection::TaskSection(String p_category_name) {
|
2022-09-02 12:25:03 +00:00
|
|
|
section_header = memnew(Button);
|
|
|
|
add_child(section_header);
|
2022-09-02 17:30:32 +00:00
|
|
|
section_header->set_text(p_category_name);
|
2022-12-15 07:26:52 +00:00
|
|
|
section_header->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("GuiTreeArrowDown"), SNAME("EditorIcons")));
|
2022-09-02 12:25:03 +00:00
|
|
|
section_header->set_focus_mode(FOCUS_NONE);
|
2022-12-15 18:14:36 +00:00
|
|
|
section_header->add_theme_font_override(SNAME("font"), get_theme_font(SNAME("bold"), SNAME("EditorFonts")));
|
2022-12-15 07:26:52 +00:00
|
|
|
section_header->connect("pressed", callable_mp(this, &TaskSection::_on_header_pressed));
|
2022-09-02 12:25:03 +00:00
|
|
|
|
|
|
|
tasks_container = memnew(HFlowContainer);
|
|
|
|
add_child(tasks_container);
|
|
|
|
}
|
|
|
|
|
|
|
|
TaskSection::~TaskSection() {
|
|
|
|
}
|
|
|
|
|
2022-09-03 15:00:20 +00:00
|
|
|
////////////////////////////// TaskSection ////////////////////////////////////
|
|
|
|
|
|
|
|
////////////////////////////// TaskPanel /////////////////////////////////////
|
2022-09-02 12:25:03 +00:00
|
|
|
|
|
|
|
void TaskPanel::_on_task_button_pressed(const StringName &p_task) {
|
2022-12-15 18:14:36 +00:00
|
|
|
emit_signal(SNAME("task_selected"), p_task);
|
2022-09-02 12:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TaskPanel::_on_filter_text_changed(String p_text) {
|
|
|
|
for (int i = 0; i < sections->get_child_count(); i++) {
|
|
|
|
TaskSection *sec = Object::cast_to<TaskSection>(sections->get_child(i));
|
|
|
|
sec->set_filter(p_text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 15:56:38 +00:00
|
|
|
void TaskPanel::refresh() {
|
2022-12-15 07:26:52 +00:00
|
|
|
filter_edit->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
|
2022-09-02 17:30:32 +00:00
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
HashSet<String> collapsed_sections;
|
2022-11-23 00:04:35 +00:00
|
|
|
if (sections->get_child_count() == 0) {
|
|
|
|
// Restore collapsed state from config.
|
|
|
|
ConfigFile conf;
|
2022-12-15 07:26:52 +00:00
|
|
|
String conf_path = EditorPaths::get_singleton()->get_project_settings_dir().path_join("limbo_ai.cfg");
|
2022-11-23 00:04:35 +00:00
|
|
|
if (conf.load(conf_path) == OK) {
|
|
|
|
Variant value = conf.get_value("bt_editor", "collapsed_sections", Array());
|
|
|
|
if (value.is_array()) {
|
|
|
|
Array arr = value;
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
if (arr[i].get_type() == Variant::STRING) {
|
|
|
|
collapsed_sections.insert(arr[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < sections->get_child_count(); i++) {
|
|
|
|
TaskSection *sec = Object::cast_to<TaskSection>(sections->get_child(i));
|
|
|
|
if (sec->is_collapsed()) {
|
|
|
|
collapsed_sections.insert(sec->get_category_name());
|
|
|
|
}
|
2022-12-15 07:26:52 +00:00
|
|
|
sections->get_child(i)->queue_free();
|
2022-11-23 00:04:35 +00:00
|
|
|
}
|
2022-09-05 15:56:38 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 20:18:11 +00:00
|
|
|
HashMap<String, List<String>> categorized_tasks;
|
2022-09-02 17:30:32 +00:00
|
|
|
|
2022-12-17 20:18:11 +00:00
|
|
|
categorized_tasks["Composites"] = List<String>();
|
|
|
|
_populate_core_tasks_from_class("BTComposite", &categorized_tasks["Composites"]);
|
2022-09-02 17:30:32 +00:00
|
|
|
|
2022-12-17 20:18:11 +00:00
|
|
|
categorized_tasks["Actions"] = List<String>();
|
|
|
|
_populate_core_tasks_from_class("BTAction", &categorized_tasks["Actions"]);
|
2022-09-02 17:30:32 +00:00
|
|
|
|
2022-12-17 20:18:11 +00:00
|
|
|
categorized_tasks["Decorators"] = List<String>();
|
|
|
|
_populate_core_tasks_from_class("BTDecorator", &categorized_tasks["Decorators"]);
|
2022-09-02 17:30:32 +00:00
|
|
|
|
2022-12-17 20:18:11 +00:00
|
|
|
categorized_tasks["Conditions"] = List<String>();
|
|
|
|
_populate_core_tasks_from_class("BTCondition", &categorized_tasks["Conditions"]);
|
2022-09-02 17:30:32 +00:00
|
|
|
|
2022-12-17 20:18:11 +00:00
|
|
|
categorized_tasks["User"] = List<String>();
|
2022-09-03 10:59:11 +00:00
|
|
|
|
2022-09-05 11:11:47 +00:00
|
|
|
String dir1 = GLOBAL_GET("limbo_ai/behavior_tree/user_task_dir_1");
|
2022-12-17 20:18:11 +00:00
|
|
|
_populate_from_user_dir(dir1, &categorized_tasks);
|
2022-10-28 23:50:47 +00:00
|
|
|
|
2022-09-05 11:11:47 +00:00
|
|
|
String dir2 = GLOBAL_GET("limbo_ai/behavior_tree/user_task_dir_2");
|
2022-12-17 20:18:11 +00:00
|
|
|
_populate_from_user_dir(dir2, &categorized_tasks);
|
2022-10-28 23:50:47 +00:00
|
|
|
|
2022-09-05 11:11:47 +00:00
|
|
|
String dir3 = GLOBAL_GET("limbo_ai/behavior_tree/user_task_dir_3");
|
2022-12-17 20:18:11 +00:00
|
|
|
_populate_from_user_dir(dir3, &categorized_tasks);
|
2022-09-02 17:30:32 +00:00
|
|
|
|
2022-12-17 20:18:11 +00:00
|
|
|
List<String> categories;
|
|
|
|
for (const KeyValue<String, List<String>> &K : categorized_tasks) {
|
|
|
|
categories.push_back(K.key);
|
2022-12-15 07:26:52 +00:00
|
|
|
}
|
2022-12-17 20:18:11 +00:00
|
|
|
categories.sort();
|
|
|
|
for (String cat : categories) {
|
|
|
|
List<String> tasks = categorized_tasks.get(cat);
|
2022-10-28 23:50:47 +00:00
|
|
|
|
2022-12-17 20:18:11 +00:00
|
|
|
if (tasks.size() == 0) {
|
2022-10-28 23:50:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
TaskSection *sec = memnew(TaskSection(cat));
|
2022-12-17 20:18:11 +00:00
|
|
|
for (String task_meta : tasks) {
|
2022-09-02 22:10:22 +00:00
|
|
|
String tname;
|
|
|
|
Ref<Texture> icon;
|
2022-12-17 20:18:11 +00:00
|
|
|
icon = LimboAIEditor::get_task_icon(task_meta);
|
|
|
|
if (task_meta.begins_with("res:")) {
|
|
|
|
tname = task_meta.get_file().get_basename().trim_prefix("BT").to_pascal_case();
|
|
|
|
} else {
|
|
|
|
tname = task_meta.trim_prefix("BT");
|
|
|
|
}
|
|
|
|
sec->add_task_button(tname, icon, task_meta);
|
2022-09-02 17:30:32 +00:00
|
|
|
}
|
|
|
|
sec->set_filter("");
|
2022-12-15 07:26:52 +00:00
|
|
|
sec->connect("task_button_pressed", callable_mp(this, &TaskPanel::_on_task_button_pressed));
|
2022-09-02 17:30:32 +00:00
|
|
|
sections->add_child(sec);
|
2022-11-23 00:04:35 +00:00
|
|
|
sec->set_collapsed(collapsed_sections.has(cat));
|
2022-09-02 17:30:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskPanel::_populate_core_tasks_from_class(const StringName &p_base_class, List<String> *p_task_classes) {
|
|
|
|
List<StringName> inheriters;
|
|
|
|
ClassDB::get_inheriters_from_class(p_base_class, &inheriters);
|
|
|
|
|
2022-12-17 20:18:11 +00:00
|
|
|
for (StringName cl : inheriters) {
|
|
|
|
p_task_classes->push_back(cl);
|
2022-09-02 17:30:32 +00:00
|
|
|
}
|
2022-09-02 12:25:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 23:50:47 +00:00
|
|
|
void TaskPanel::_populate_from_user_dir(String p_path, HashMap<String, List<String>> *p_categories) {
|
2022-12-15 07:26:52 +00:00
|
|
|
if (p_path.is_empty()) {
|
2022-10-28 23:50:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-12-15 07:26:52 +00:00
|
|
|
Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_RESOURCES);
|
2022-10-28 23:50:47 +00:00
|
|
|
if (dir->change_dir(p_path) == OK) {
|
|
|
|
dir->list_dir_begin();
|
|
|
|
String fn = dir->get_next();
|
2022-12-15 07:26:52 +00:00
|
|
|
while (!fn.is_empty()) {
|
2022-11-01 13:03:20 +00:00
|
|
|
if (dir->current_is_dir() && fn != "..") {
|
2022-10-30 11:33:38 +00:00
|
|
|
String full_path;
|
|
|
|
String category;
|
|
|
|
if (fn == ".") {
|
|
|
|
full_path = p_path;
|
|
|
|
category = "User";
|
|
|
|
} else {
|
2022-12-15 07:26:52 +00:00
|
|
|
full_path = p_path.path_join(fn);
|
2022-10-30 11:33:38 +00:00
|
|
|
category = fn.capitalize();
|
|
|
|
}
|
|
|
|
|
2022-10-28 23:50:47 +00:00
|
|
|
if (!p_categories->has(category)) {
|
2022-12-15 07:26:52 +00:00
|
|
|
p_categories->insert(category, List<String>());
|
2022-10-28 23:50:47 +00:00
|
|
|
}
|
2022-10-30 11:33:38 +00:00
|
|
|
|
2022-10-28 23:50:47 +00:00
|
|
|
_populate_scripted_tasks_from_dir(full_path, &p_categories->get(category));
|
|
|
|
}
|
|
|
|
fn = dir->get_next();
|
|
|
|
}
|
|
|
|
dir->list_dir_end();
|
|
|
|
} else {
|
|
|
|
ERR_FAIL_MSG(vformat("Failed to list \"%s\" directory.", p_path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 22:10:22 +00:00
|
|
|
void TaskPanel::_populate_scripted_tasks_from_dir(String p_path, List<String> *p_task_classes) {
|
2022-12-15 07:26:52 +00:00
|
|
|
if (p_path.is_empty()) {
|
2022-09-03 10:59:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-12-15 07:26:52 +00:00
|
|
|
Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_RESOURCES);
|
2022-09-02 22:10:22 +00:00
|
|
|
if (dir->change_dir(p_path) == OK) {
|
|
|
|
dir->list_dir_begin();
|
|
|
|
String fn = dir->get_next();
|
2022-12-15 07:26:52 +00:00
|
|
|
while (!fn.is_empty()) {
|
2022-09-02 22:10:22 +00:00
|
|
|
if (fn.ends_with(".gd")) {
|
2022-12-15 07:26:52 +00:00
|
|
|
String full_path = p_path.path_join(fn);
|
2022-09-02 22:10:22 +00:00
|
|
|
p_task_classes->push_back(full_path);
|
|
|
|
}
|
|
|
|
fn = dir->get_next();
|
|
|
|
}
|
|
|
|
dir->list_dir_end();
|
|
|
|
} else {
|
|
|
|
ERR_FAIL_MSG(vformat("Failed to list \"%s\" directory.", p_path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 00:04:35 +00:00
|
|
|
void TaskPanel::_notification(int p_what) {
|
|
|
|
if (p_what == NOTIFICATION_EXIT_TREE) {
|
|
|
|
if (sections->get_child_count() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Array collapsed_sections;
|
|
|
|
for (int i = 0; i < sections->get_child_count(); i++) {
|
|
|
|
TaskSection *sec = Object::cast_to<TaskSection>(sections->get_child(i));
|
|
|
|
if (sec->is_collapsed()) {
|
|
|
|
collapsed_sections.push_back(sec->get_category_name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ConfigFile conf;
|
2022-12-15 07:26:52 +00:00
|
|
|
String conf_path = EditorPaths::get_singleton()->get_project_settings_dir().path_join("limbo_ai.cfg");
|
2022-11-23 00:04:35 +00:00
|
|
|
conf.load(conf_path);
|
|
|
|
conf.set_value("bt_editor", "collapsed_sections", collapsed_sections);
|
|
|
|
conf.save(conf_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 12:25:03 +00:00
|
|
|
void TaskPanel::_bind_methods() {
|
2022-09-05 15:56:38 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("refresh"), &TaskPanel::refresh);
|
2022-09-02 12:25:03 +00:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("task_selected"));
|
|
|
|
}
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
TaskPanel::TaskPanel() {
|
2022-09-02 12:25:03 +00:00
|
|
|
VBoxContainer *vb = memnew(VBoxContainer);
|
|
|
|
add_child(vb);
|
|
|
|
|
|
|
|
filter_edit = memnew(LineEdit);
|
|
|
|
vb->add_child(filter_edit);
|
|
|
|
filter_edit->set_clear_button_enabled(true);
|
2022-12-15 17:44:42 +00:00
|
|
|
filter_edit->set_placeholder(TTR("Filter tasks"));
|
2022-12-15 07:26:52 +00:00
|
|
|
filter_edit->connect("text_changed", callable_mp(this, &TaskPanel::_on_filter_text_changed));
|
2022-09-02 12:25:03 +00:00
|
|
|
|
|
|
|
ScrollContainer *sc = memnew(ScrollContainer);
|
|
|
|
vb->add_child(sc);
|
|
|
|
sc->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
sc->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
|
|
|
|
sections = memnew(VBoxContainer);
|
|
|
|
sc->add_child(sections);
|
|
|
|
sections->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
sections->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
}
|
|
|
|
|
|
|
|
TaskPanel::~TaskPanel() {
|
|
|
|
}
|
|
|
|
|
2022-09-03 15:00:20 +00:00
|
|
|
////////////////////////////// TaskPanel /////////////////////////////////////
|
|
|
|
|
|
|
|
//////////////////////////// LimboAIEditor ///////////////////////////////////
|
2022-09-02 12:25:03 +00:00
|
|
|
|
2022-09-03 12:11:47 +00:00
|
|
|
void LimboAIEditor::_add_task(const Ref<BTTask> &p_task) {
|
|
|
|
ERR_FAIL_COND(p_task.is_null());
|
2022-09-05 19:57:24 +00:00
|
|
|
ERR_FAIL_COND(task_tree->get_bt().is_null());
|
2022-09-01 22:20:37 +00:00
|
|
|
Ref<BTTask> parent = task_tree->get_selected();
|
|
|
|
if (parent.is_null()) {
|
|
|
|
parent = task_tree->get_bt()->get_root_task();
|
|
|
|
}
|
|
|
|
if (parent.is_null()) {
|
2022-09-03 12:11:47 +00:00
|
|
|
task_tree->get_bt()->set_root_task(p_task);
|
2022-09-01 22:20:37 +00:00
|
|
|
} else {
|
2022-09-03 12:11:47 +00:00
|
|
|
parent->add_child(p_task);
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
2022-09-03 12:01:13 +00:00
|
|
|
_mark_as_dirty(true);
|
2022-09-01 22:20:37 +00:00
|
|
|
task_tree->update_tree();
|
|
|
|
}
|
|
|
|
|
2022-11-22 21:58:53 +00:00
|
|
|
void LimboAIEditor::_update_header() const {
|
2022-09-01 22:20:37 +00:00
|
|
|
String text = task_tree->get_bt()->get_path();
|
2022-12-15 07:26:52 +00:00
|
|
|
if (text.is_empty()) {
|
2022-09-01 22:20:37 +00:00
|
|
|
text = TTR("New Behavior Tree");
|
2022-09-03 12:01:13 +00:00
|
|
|
} else if (dirty.has(task_tree->get_bt())) {
|
|
|
|
text += "(*)";
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
2022-09-03 12:01:13 +00:00
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
header->set_text(text);
|
2022-12-15 07:26:52 +00:00
|
|
|
header->set_icon(EditorNode::get_singleton()->get_object_icon(task_tree->get_bt().ptr(), "BehaviorTree"));
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 13:43:54 +00:00
|
|
|
void LimboAIEditor::_update_history_buttons() {
|
|
|
|
history_back->set_disabled(idx_history == 0);
|
|
|
|
history_forward->set_disabled(idx_history == (history.size() - 1));
|
|
|
|
}
|
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
void LimboAIEditor::_new_bt() {
|
|
|
|
BehaviorTree *bt = memnew(BehaviorTree);
|
|
|
|
bt->set_root_task(memnew(BTSelector));
|
2022-12-15 07:26:52 +00:00
|
|
|
EditorNode::get_singleton()->edit_resource(bt);
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LimboAIEditor::_save_bt(String p_path) {
|
2022-12-15 07:26:52 +00:00
|
|
|
ERR_FAIL_COND_MSG(p_path.is_empty(), "Empty p_path");
|
2022-09-01 22:20:37 +00:00
|
|
|
ERR_FAIL_COND_MSG(task_tree->get_bt().is_null(), "Behavior Tree is null.");
|
|
|
|
task_tree->get_bt()->set_path(p_path, true);
|
2022-12-15 07:26:52 +00:00
|
|
|
ResourceSaver::save(task_tree->get_bt(), p_path, ResourceSaver::FLAG_CHANGE_PATH);
|
2022-09-01 22:20:37 +00:00
|
|
|
_update_header();
|
2022-09-03 12:01:13 +00:00
|
|
|
_mark_as_dirty(false);
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LimboAIEditor::_load_bt(String p_path) {
|
2022-12-15 07:26:52 +00:00
|
|
|
ERR_FAIL_COND_MSG(p_path.is_empty(), "Empty p_path");
|
2022-09-02 13:43:54 +00:00
|
|
|
Ref<BehaviorTree> bt = ResourceLoader::load(p_path, "BehaviorTree");
|
|
|
|
|
|
|
|
if (history.find(bt) != -1) {
|
|
|
|
history.erase(bt);
|
|
|
|
history.push_back(bt);
|
|
|
|
}
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
EditorNode::get_singleton()->edit_resource(bt);
|
2022-09-02 13:43:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 20:00:08 +00:00
|
|
|
void LimboAIEditor::edit_bt(Ref<BehaviorTree> p_behavior_tree, bool p_force_refresh) {
|
2022-09-02 13:43:54 +00:00
|
|
|
ERR_FAIL_COND_MSG(p_behavior_tree.is_null(), "p_behavior_tree is null");
|
2022-09-05 19:57:24 +00:00
|
|
|
|
2022-11-22 20:00:08 +00:00
|
|
|
if (!p_force_refresh && task_tree->get_bt() == p_behavior_tree) {
|
2022-09-05 19:57:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-02 13:43:54 +00:00
|
|
|
task_tree->load_bt(p_behavior_tree);
|
|
|
|
|
|
|
|
int idx = history.find(p_behavior_tree);
|
|
|
|
if (idx != -1) {
|
|
|
|
idx_history = idx;
|
|
|
|
} else {
|
|
|
|
history.push_back(p_behavior_tree);
|
|
|
|
idx_history = history.size() - 1;
|
|
|
|
}
|
|
|
|
|
2022-09-05 19:57:24 +00:00
|
|
|
usage_hint->hide();
|
|
|
|
task_tree->show();
|
|
|
|
task_panel->show();
|
|
|
|
|
2022-09-02 13:43:54 +00:00
|
|
|
_update_history_buttons();
|
2022-09-01 22:20:37 +00:00
|
|
|
_update_header();
|
|
|
|
}
|
|
|
|
|
2022-09-03 12:01:13 +00:00
|
|
|
void LimboAIEditor::_mark_as_dirty(bool p_dirty) {
|
|
|
|
Ref<BehaviorTree> bt = task_tree->get_bt();
|
|
|
|
if (p_dirty && !dirty.has(bt)) {
|
|
|
|
dirty.insert(bt);
|
|
|
|
_update_header();
|
|
|
|
} else if (p_dirty == false && dirty.has(bt)) {
|
|
|
|
dirty.erase(bt);
|
|
|
|
_update_header();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
void LimboAIEditor::_on_tree_rmb(const Vector2 &p_menu_pos) {
|
|
|
|
menu->clear();
|
2022-12-15 07:26:52 +00:00
|
|
|
menu->add_icon_item(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), TTR("Remove"), ACTION_REMOVE);
|
2022-09-01 22:20:37 +00:00
|
|
|
menu->add_separator();
|
2022-12-15 07:26:52 +00:00
|
|
|
menu->add_icon_item(get_theme_icon(SNAME("MoveUp"), SNAME("EditorIcons")), TTR("Move Up"), ACTION_MOVE_UP);
|
|
|
|
menu->add_icon_item(get_theme_icon(SNAME("MoveDown"), SNAME("EditorIcons")), TTR("Move Down"), ACTION_MOVE_DOWN);
|
|
|
|
menu->add_icon_item(get_theme_icon(SNAME("Duplicate"), SNAME("EditorIcons")), TTR("Duplicate"), ACTION_DUPLICATE);
|
|
|
|
menu->add_icon_item(get_theme_icon(SNAME("NewRoot"), SNAME("EditorIcons")), TTR("Make Root"), ACTION_MAKE_ROOT);
|
2022-09-01 22:20:37 +00:00
|
|
|
|
2022-12-15 17:29:22 +00:00
|
|
|
menu->reset_size();
|
|
|
|
menu->set_position(p_menu_pos);
|
2022-09-01 22:20:37 +00:00
|
|
|
menu->popup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LimboAIEditor::_on_action_selected(int p_id) {
|
|
|
|
switch (p_id) {
|
|
|
|
case ACTION_REMOVE: {
|
|
|
|
Ref<BTTask> sel = task_tree->get_selected();
|
|
|
|
if (sel.is_valid()) {
|
|
|
|
if (sel->get_parent().is_null()) {
|
|
|
|
task_tree->get_bt()->set_root_task(nullptr);
|
|
|
|
} else {
|
|
|
|
sel->get_parent()->remove_child(sel);
|
|
|
|
}
|
|
|
|
task_tree->update_tree();
|
2022-12-15 07:26:52 +00:00
|
|
|
EditorNode::get_singleton()->edit_node(nullptr);
|
2022-09-03 12:01:13 +00:00
|
|
|
_mark_as_dirty(true);
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case ACTION_MOVE_UP: {
|
|
|
|
Ref<BTTask> sel = task_tree->get_selected();
|
|
|
|
if (sel.is_valid() && sel->get_parent().is_valid()) {
|
|
|
|
Ref<BTTask> parent = sel->get_parent();
|
|
|
|
int idx = parent->get_child_index(sel);
|
|
|
|
if (idx > 0 && idx < parent->get_child_count()) {
|
|
|
|
parent->remove_child(sel);
|
|
|
|
parent->add_child_at_index(sel, idx - 1);
|
|
|
|
task_tree->update_tree();
|
2022-09-03 12:01:13 +00:00
|
|
|
_mark_as_dirty(true);
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case ACTION_MOVE_DOWN: {
|
|
|
|
Ref<BTTask> sel = task_tree->get_selected();
|
|
|
|
if (sel.is_valid() && sel->get_parent().is_valid()) {
|
|
|
|
Ref<BTTask> parent = sel->get_parent();
|
|
|
|
int idx = parent->get_child_index(sel);
|
|
|
|
if (idx >= 0 && idx < (parent->get_child_count() - 1)) {
|
|
|
|
parent->remove_child(sel);
|
|
|
|
parent->add_child_at_index(sel, idx + 1);
|
|
|
|
task_tree->update_tree();
|
2022-09-03 12:01:13 +00:00
|
|
|
_mark_as_dirty(true);
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case ACTION_DUPLICATE: {
|
|
|
|
Ref<BTTask> sel = task_tree->get_selected();
|
|
|
|
if (sel.is_valid()) {
|
|
|
|
Ref<BTTask> parent = sel->get_parent();
|
|
|
|
if (parent.is_null()) {
|
|
|
|
parent = sel;
|
|
|
|
}
|
2022-09-03 12:11:03 +00:00
|
|
|
parent->add_child_at_index(sel->clone(), parent->get_child_index(sel) + 1);
|
2022-09-01 22:20:37 +00:00
|
|
|
task_tree->update_tree();
|
2022-09-03 12:01:13 +00:00
|
|
|
_mark_as_dirty(true);
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case ACTION_MAKE_ROOT: {
|
|
|
|
Ref<BTTask> sel = task_tree->get_selected();
|
|
|
|
if (sel.is_valid() && task_tree->get_bt()->get_root_task() != sel) {
|
|
|
|
Ref<BTTask> parent = sel->get_parent();
|
|
|
|
ERR_FAIL_COND(parent.is_null());
|
|
|
|
parent->remove_child(sel);
|
|
|
|
Ref<BTTask> old_root = task_tree->get_bt()->get_root_task();
|
|
|
|
task_tree->get_bt()->set_root_task(sel);
|
|
|
|
sel->add_child(old_root);
|
|
|
|
task_tree->update_tree();
|
2022-09-03 12:01:13 +00:00
|
|
|
_mark_as_dirty(true);
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
void LimboAIEditor::_on_tree_task_selected(const Ref<BTTask> &p_task) {
|
|
|
|
EditorNode::get_singleton()->edit_resource(p_task);
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 15:25:32 +00:00
|
|
|
void LimboAIEditor::_on_tree_task_double_clicked() {
|
|
|
|
if (!task_tree->get_selected().is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rename_dialog->popup_centered();
|
|
|
|
rename_edit->set_text(task_tree->get_selected()->get_custom_name());
|
|
|
|
rename_edit->select_all();
|
|
|
|
rename_edit->grab_focus();
|
|
|
|
}
|
|
|
|
|
2022-09-02 22:10:22 +00:00
|
|
|
void LimboAIEditor::_on_panel_task_selected(String p_task) {
|
|
|
|
if (p_task.begins_with("res:")) {
|
|
|
|
Ref<Script> script = ResourceLoader::load(p_task, "Script");
|
2022-10-30 11:33:38 +00:00
|
|
|
ERR_FAIL_COND_MSG(script.is_null() || !script->is_valid(), vformat("LimboAI: Failed to instance task. Bad script: %s", p_task));
|
2022-12-15 07:26:52 +00:00
|
|
|
Variant inst = ClassDB::instantiate(script->get_instance_base_type());
|
2022-10-28 23:50:47 +00:00
|
|
|
ERR_FAIL_COND_MSG(inst.is_zero(), vformat("LimboAI: Failed to instance base type \"%s\".", script->get_instance_base_type()));
|
|
|
|
|
|
|
|
if (unlikely(!((Object *)inst)->is_class("BTTask"))) {
|
2022-12-15 07:26:52 +00:00
|
|
|
if (!inst.is_ref_counted()) {
|
2022-10-28 23:50:47 +00:00
|
|
|
memdelete((Object *)inst);
|
|
|
|
}
|
|
|
|
ERR_PRINT(vformat("LimboAI: Failed to instance task. Script is not a BTTask: %s", p_task));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-02 22:10:22 +00:00
|
|
|
if (inst && script.is_valid()) {
|
2022-12-15 07:26:52 +00:00
|
|
|
((Object *)inst)->set_script(script);
|
2022-09-02 22:10:22 +00:00
|
|
|
_add_task(Variant(inst));
|
|
|
|
}
|
|
|
|
} else {
|
2022-12-15 07:26:52 +00:00
|
|
|
_add_task(Ref<BTTask>(ClassDB::instantiate(p_task)));
|
2022-09-02 22:10:22 +00:00
|
|
|
}
|
2022-09-02 12:25:03 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
void LimboAIEditor::_on_visibility_changed() {
|
|
|
|
if (task_tree->is_visible()) {
|
2022-09-01 22:20:37 +00:00
|
|
|
Ref<BTTask> sel = task_tree->get_selected();
|
|
|
|
if (sel.is_valid()) {
|
2022-12-15 07:26:52 +00:00
|
|
|
EditorNode::get_singleton()->edit_resource(sel);
|
|
|
|
} else if (task_tree->get_bt().is_valid() && InspectorDock::get_inspector_singleton()->get_edited_object() != task_tree->get_bt().ptr()) {
|
|
|
|
EditorNode::get_singleton()->edit_resource(task_tree->get_bt());
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
2022-09-05 15:56:38 +00:00
|
|
|
|
|
|
|
task_panel->refresh();
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
void LimboAIEditor::_on_header_pressed() {
|
2022-11-22 21:58:53 +00:00
|
|
|
_update_header();
|
2022-09-01 22:20:37 +00:00
|
|
|
task_tree->deselect();
|
2022-12-15 07:26:52 +00:00
|
|
|
EditorNode::get_singleton()->edit_resource(task_tree->get_bt());
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LimboAIEditor::_on_save_pressed() {
|
2022-09-06 11:08:27 +00:00
|
|
|
if (task_tree->get_bt().is_null()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-01 22:20:37 +00:00
|
|
|
String path = task_tree->get_bt()->get_path();
|
2022-12-15 07:26:52 +00:00
|
|
|
if (path.is_empty()) {
|
2022-09-01 22:20:37 +00:00
|
|
|
save_dialog->popup_centered_ratio();
|
|
|
|
} else {
|
|
|
|
_save_bt(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 13:43:54 +00:00
|
|
|
void LimboAIEditor::_on_history_back() {
|
|
|
|
idx_history = MAX(idx_history - 1, 0);
|
2022-12-15 07:26:52 +00:00
|
|
|
EditorNode::get_singleton()->edit_resource(history[idx_history]);
|
2022-09-02 13:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LimboAIEditor::_on_history_forward() {
|
|
|
|
idx_history = MIN(idx_history + 1, history.size() - 1);
|
2022-12-15 07:26:52 +00:00
|
|
|
EditorNode::get_singleton()->edit_resource(history[idx_history]);
|
2022-09-02 13:43:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 15:00:20 +00:00
|
|
|
void LimboAIEditor::_on_task_dragged(Ref<BTTask> p_task, Ref<BTTask> p_to_task, int p_type) {
|
|
|
|
if (p_task == p_to_task) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (p_type == 0) {
|
|
|
|
p_task->get_parent()->remove_child(p_task);
|
|
|
|
p_to_task->add_child(p_task);
|
|
|
|
task_tree->update_tree();
|
|
|
|
_mark_as_dirty(true);
|
|
|
|
} else if (p_type == -1 && p_to_task->get_parent().is_valid()) {
|
|
|
|
p_task->get_parent()->remove_child(p_task);
|
|
|
|
p_to_task->get_parent()->add_child_at_index(p_task, p_to_task->get_parent()->get_child_index(p_to_task));
|
|
|
|
task_tree->update_tree();
|
|
|
|
_mark_as_dirty(true);
|
|
|
|
} else if (p_type == 1 && p_to_task->get_parent().is_valid()) {
|
|
|
|
p_task->get_parent()->remove_child(p_task);
|
|
|
|
p_to_task->get_parent()->add_child_at_index(p_task, p_to_task->get_parent()->get_child_index(p_to_task) + 1);
|
|
|
|
task_tree->update_tree();
|
|
|
|
_mark_as_dirty(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 20:00:08 +00:00
|
|
|
void LimboAIEditor::_on_resources_reload(const Vector<String> &p_resources) {
|
2022-12-19 09:29:46 +00:00
|
|
|
for (const String &res : p_resources) {
|
|
|
|
if (!ResourceCache::has(res)) {
|
2022-11-22 20:00:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-12-19 09:29:46 +00:00
|
|
|
String res_type = ResourceLoader::get_resource_type(res);
|
2022-11-22 20:00:08 +00:00
|
|
|
if (res_type == "BehaviorTree") {
|
2022-12-19 09:29:46 +00:00
|
|
|
disk_changed_files.insert(res);
|
2022-11-22 20:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disk_changed_files.size() > 0) {
|
|
|
|
disk_changed_list->clear();
|
|
|
|
disk_changed_list->set_hide_root(true);
|
|
|
|
disk_changed_list->create_item();
|
2022-12-15 07:26:52 +00:00
|
|
|
for (const String &fn : disk_changed_files) {
|
2022-11-22 20:00:08 +00:00
|
|
|
TreeItem *ti = disk_changed_list->create_item();
|
2022-12-15 07:26:52 +00:00
|
|
|
ti->set_text(0, fn);
|
2022-11-22 20:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_visible()) {
|
|
|
|
EditorNode::get_singleton()->select_editor_by_name("LimboAI");
|
|
|
|
}
|
|
|
|
disk_changed->call_deferred("popup_centered_ratio", 0.5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LimboAIEditor::_reload_modified() {
|
2022-12-15 07:26:52 +00:00
|
|
|
for (const String &fn : disk_changed_files) {
|
2022-12-19 09:29:46 +00:00
|
|
|
Ref<Resource> res = ResourceCache::get_ref(fn);
|
|
|
|
if (res.is_valid()) {
|
|
|
|
ERR_FAIL_COND(!res->is_class("BehaviorTree"));
|
|
|
|
res->reload_from_file();
|
|
|
|
if (idx_history >= 0 && history.get(idx_history) == res) {
|
|
|
|
edit_bt(res, true);
|
2022-11-22 20:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
disk_changed_files.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LimboAIEditor::_resave_modified(String _str) {
|
2022-12-15 07:26:52 +00:00
|
|
|
for (const String &fn : disk_changed_files) {
|
2022-11-22 20:00:08 +00:00
|
|
|
for (int j = 0; j < history.size(); j++) {
|
2022-12-15 07:26:52 +00:00
|
|
|
if (history.get(j)->get_path() == fn) {
|
|
|
|
ResourceSaver::save(history.get(j), history.get(j)->get_path());
|
2022-11-22 20:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
disk_changed->hide();
|
|
|
|
disk_changed_files.clear();
|
|
|
|
}
|
|
|
|
|
2022-12-17 12:39:37 +00:00
|
|
|
void LimboAIEditor::_rename_task_confirmed() {
|
2022-11-23 15:25:32 +00:00
|
|
|
ERR_FAIL_COND(!task_tree->get_selected().is_valid());
|
|
|
|
task_tree->get_selected()->set_custom_name(rename_edit->get_text());
|
|
|
|
rename_dialog->hide();
|
2022-12-15 07:26:52 +00:00
|
|
|
EditorNode::get_singleton()->edit_resource(task_tree->get_selected());
|
2022-11-23 15:25:32 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 12:01:13 +00:00
|
|
|
void LimboAIEditor::apply_changes() {
|
|
|
|
for (int i = 0; i < history.size(); i++) {
|
|
|
|
Ref<BehaviorTree> bt = history.get(i);
|
|
|
|
String path = bt->get_path();
|
|
|
|
if (ResourceLoader::exists(path)) {
|
2022-12-15 07:26:52 +00:00
|
|
|
ResourceSaver::save(bt, path);
|
2022-09-03 12:01:13 +00:00
|
|
|
}
|
|
|
|
dirty.clear();
|
|
|
|
_update_header();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:39:40 +00:00
|
|
|
Ref<Texture> LimboAIEditor::get_task_icon(String p_script_path_or_class) {
|
|
|
|
// TODO: Implement caching.
|
|
|
|
String base_type = p_script_path_or_class;
|
|
|
|
if (p_script_path_or_class.begins_with("res:")) {
|
|
|
|
Ref<Script> script = ResourceLoader::load(p_script_path_or_class, "Script");
|
|
|
|
Ref<Script> base_script = script;
|
|
|
|
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);
|
2022-12-15 07:26:52 +00:00
|
|
|
if (!icon_path.is_empty()) {
|
2022-09-05 14:39:40 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-10-28 23:50:47 +00:00
|
|
|
// TODO: Walk inheritance tree until icon is found.
|
2022-09-05 14:39:40 +00:00
|
|
|
return EditorNode::get_singleton()->get_class_icon(base_type, "BTTask");
|
|
|
|
}
|
|
|
|
|
2022-09-06 11:28:25 +00:00
|
|
|
void LimboAIEditor::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
|
|
|
ConfigFile conf;
|
2022-12-15 07:26:52 +00:00
|
|
|
String conf_path = EditorPaths::get_singleton()->get_project_settings_dir().path_join("limbo_ai.cfg");
|
2022-09-06 11:28:25 +00:00
|
|
|
if (conf.load(conf_path) == OK) {
|
|
|
|
hsc->set_split_offset(conf.get_value("bt_editor", "bteditor_hsplit", hsc->get_split_offset()));
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
ConfigFile conf;
|
2022-12-15 07:26:52 +00:00
|
|
|
String conf_path = EditorPaths::get_singleton()->get_project_settings_dir().path_join("limbo_ai.cfg");
|
2022-09-06 11:28:25 +00:00
|
|
|
conf.load(conf_path);
|
|
|
|
conf.set_value("bt_editor", "bteditor_hsplit", hsc->get_split_offset());
|
|
|
|
conf.save(conf_path);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
void LimboAIEditor::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("_add_task", "p_task"), &LimboAIEditor::_add_task);
|
2022-09-03 12:11:47 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_add_task_with_prototype", "p_prototype"), &LimboAIEditor::_add_task_with_prototype);
|
2022-09-01 22:20:37 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_new_bt"), &LimboAIEditor::_new_bt);
|
|
|
|
ClassDB::bind_method(D_METHOD("_save_bt", "p_path"), &LimboAIEditor::_save_bt);
|
|
|
|
ClassDB::bind_method(D_METHOD("_load_bt", "p_path"), &LimboAIEditor::_load_bt);
|
2022-11-22 20:00:08 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("edit_bt", "p_behavior_tree", "p_force_refresh"), &LimboAIEditor::edit_bt, Variant(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("_reload_modified"), &LimboAIEditor::_reload_modified);
|
|
|
|
ClassDB::bind_method(D_METHOD("_resave_modified"), &LimboAIEditor::_resave_modified);
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
LimboAIEditor::LimboAIEditor() {
|
|
|
|
idx_history = 0;
|
2022-09-01 22:20:37 +00:00
|
|
|
|
|
|
|
save_dialog = memnew(FileDialog);
|
|
|
|
add_child(save_dialog);
|
2022-12-15 07:26:52 +00:00
|
|
|
save_dialog->set_file_mode(FileDialog::FILE_MODE_SAVE_FILE);
|
2022-09-01 22:20:37 +00:00
|
|
|
save_dialog->set_title("Save Behavior Tree");
|
|
|
|
save_dialog->add_filter("*.tres");
|
2022-12-15 07:26:52 +00:00
|
|
|
save_dialog->connect("file_selected", callable_mp(this, &LimboAIEditor::_save_bt));
|
2022-09-01 22:20:37 +00:00
|
|
|
save_dialog->hide();
|
|
|
|
|
|
|
|
load_dialog = memnew(FileDialog);
|
|
|
|
add_child(load_dialog);
|
2022-12-15 07:26:52 +00:00
|
|
|
load_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE);
|
2022-09-01 22:20:37 +00:00
|
|
|
load_dialog->set_title("Load Behavior Tree");
|
|
|
|
load_dialog->add_filter("*.tres");
|
2022-12-15 07:26:52 +00:00
|
|
|
load_dialog->connect("file_selected", callable_mp(this, &LimboAIEditor::_load_bt));
|
2022-09-01 22:20:37 +00:00
|
|
|
load_dialog->hide();
|
|
|
|
|
2022-09-02 12:25:03 +00:00
|
|
|
VBoxContainer *vb = memnew(VBoxContainer);
|
2022-12-15 07:26:52 +00:00
|
|
|
vb->set_anchor(SIDE_RIGHT, ANCHOR_END);
|
|
|
|
vb->set_anchor(SIDE_BOTTOM, ANCHOR_END);
|
2022-09-02 12:25:03 +00:00
|
|
|
add_child(vb);
|
2022-09-01 22:20:37 +00:00
|
|
|
|
|
|
|
HBoxContainer *panel = memnew(HBoxContainer);
|
2022-09-02 12:25:03 +00:00
|
|
|
vb->add_child(panel);
|
2022-09-01 22:20:37 +00:00
|
|
|
|
|
|
|
Button *selector_btn = memnew(Button);
|
|
|
|
selector_btn->set_text(TTR("Selector"));
|
2022-12-15 07:26:52 +00:00
|
|
|
selector_btn->set_tooltip_text(TTR("Add Selector task."));
|
|
|
|
selector_btn->set_icon(EditorNode::get_singleton()->get_class_icon("BTSelector"));
|
2022-09-01 22:20:37 +00:00
|
|
|
selector_btn->set_flat(true);
|
|
|
|
selector_btn->set_focus_mode(Control::FOCUS_NONE);
|
2022-12-15 07:26:52 +00:00
|
|
|
selector_btn->connect("pressed", callable_mp(this, &LimboAIEditor::_add_task_with_prototype).bind(Ref<BTTask>(memnew(BTSelector))));
|
2022-09-01 22:20:37 +00:00
|
|
|
panel->add_child(selector_btn);
|
|
|
|
|
|
|
|
Button *sequence_btn = memnew(Button);
|
|
|
|
sequence_btn->set_text(TTR("Sequence"));
|
2022-12-15 07:26:52 +00:00
|
|
|
sequence_btn->set_tooltip_text(TTR("Add Sequence task."));
|
|
|
|
sequence_btn->set_icon(EditorNode::get_singleton()->get_class_icon("BTSequence"));
|
2022-09-01 22:20:37 +00:00
|
|
|
sequence_btn->set_flat(true);
|
|
|
|
sequence_btn->set_focus_mode(Control::FOCUS_NONE);
|
2022-12-15 07:26:52 +00:00
|
|
|
sequence_btn->connect("pressed", callable_mp(this, &LimboAIEditor::_add_task_with_prototype).bind(Ref<BTTask>(memnew(BTSequence))));
|
2022-09-01 22:20:37 +00:00
|
|
|
panel->add_child(sequence_btn);
|
|
|
|
|
|
|
|
Button *parallel_btn = memnew(Button);
|
|
|
|
parallel_btn->set_text(TTR("Parallel"));
|
2022-12-15 07:26:52 +00:00
|
|
|
parallel_btn->set_tooltip_text(TTR("Add Parallel task."));
|
|
|
|
parallel_btn->set_icon(EditorNode::get_singleton()->get_class_icon("BTParallel"));
|
2022-09-01 22:20:37 +00:00
|
|
|
parallel_btn->set_flat(true);
|
|
|
|
parallel_btn->set_focus_mode(Control::FOCUS_NONE);
|
2022-12-15 07:26:52 +00:00
|
|
|
parallel_btn->connect("pressed", callable_mp(this, &LimboAIEditor::_add_task_with_prototype).bind(Ref<BTTask>(memnew(BTParallel))));
|
2022-09-01 22:20:37 +00:00
|
|
|
panel->add_child(parallel_btn);
|
|
|
|
|
|
|
|
panel->add_child(memnew(VSeparator));
|
|
|
|
|
|
|
|
Button *new_btn = memnew(Button);
|
|
|
|
panel->add_child(new_btn);
|
|
|
|
new_btn->set_text(TTR("New"));
|
2022-12-15 07:26:52 +00:00
|
|
|
new_btn->set_tooltip_text(TTR("Create new behavior tree."));
|
|
|
|
new_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("New"), SNAME("EditorIcons")));
|
2022-09-01 22:20:37 +00:00
|
|
|
new_btn->set_flat(true);
|
|
|
|
new_btn->set_focus_mode(Control::FOCUS_NONE);
|
2022-12-15 07:26:52 +00:00
|
|
|
new_btn->connect("pressed", callable_mp(this, &LimboAIEditor::_new_bt));
|
2022-09-01 22:20:37 +00:00
|
|
|
|
|
|
|
Button *load_btn = memnew(Button);
|
|
|
|
panel->add_child(load_btn);
|
|
|
|
load_btn->set_text(TTR("Load"));
|
2022-12-15 07:26:52 +00:00
|
|
|
load_btn->set_tooltip_text(TTR("Load behavior tree."));
|
|
|
|
load_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Load"), SNAME("EditorIcons")));
|
2022-09-01 22:20:37 +00:00
|
|
|
load_btn->set_flat(true);
|
|
|
|
load_btn->set_focus_mode(Control::FOCUS_NONE);
|
2022-12-15 07:26:52 +00:00
|
|
|
load_btn->connect("pressed", callable_mp(load_dialog, &FileDialog::popup_file_dialog));
|
2022-09-01 22:20:37 +00:00
|
|
|
|
|
|
|
Button *save_btn = memnew(Button);
|
|
|
|
panel->add_child(save_btn);
|
|
|
|
save_btn->set_text(TTR("Save"));
|
2022-12-15 07:26:52 +00:00
|
|
|
save_btn->set_tooltip_text(TTR("Save current behavior tree."));
|
|
|
|
save_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Save"), SNAME("EditorIcons")));
|
2022-09-01 22:20:37 +00:00
|
|
|
save_btn->set_flat(true);
|
|
|
|
save_btn->set_focus_mode(Control::FOCUS_NONE);
|
2022-12-15 07:26:52 +00:00
|
|
|
save_btn->connect("pressed", callable_mp(this, &LimboAIEditor::_on_save_pressed));
|
2022-09-01 22:20:37 +00:00
|
|
|
|
|
|
|
panel->add_child(memnew(VSeparator));
|
|
|
|
|
2022-09-05 11:31:38 +00:00
|
|
|
Button *new_script_btn = memnew(Button);
|
|
|
|
panel->add_child(new_script_btn);
|
|
|
|
new_script_btn->set_text(TTR("New Task"));
|
2022-12-15 07:26:52 +00:00
|
|
|
new_script_btn->set_tooltip_text(TTR("Create new task script and edit it."));
|
|
|
|
new_script_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("ScriptCreate"), SNAME("EditorIcons")));
|
2022-09-05 11:31:38 +00:00
|
|
|
new_script_btn->set_flat(true);
|
|
|
|
new_script_btn->set_focus_mode(Control::FOCUS_NONE);
|
|
|
|
|
2022-09-02 13:43:54 +00:00
|
|
|
HBoxContainer *nav = memnew(HBoxContainer);
|
|
|
|
panel->add_child(nav);
|
|
|
|
nav->set_h_size_flags(SIZE_EXPAND | SIZE_SHRINK_END);
|
|
|
|
|
|
|
|
history_back = memnew(Button);
|
2022-12-15 07:26:52 +00:00
|
|
|
history_back->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Back"), SNAME("EditorIcons")));
|
2022-09-02 13:43:54 +00:00
|
|
|
history_back->set_flat(true);
|
|
|
|
history_back->set_focus_mode(FOCUS_NONE);
|
2022-12-15 07:26:52 +00:00
|
|
|
history_back->connect("pressed", callable_mp(this, &LimboAIEditor::_on_history_back));
|
2022-09-02 13:43:54 +00:00
|
|
|
nav->add_child(history_back);
|
|
|
|
|
|
|
|
history_forward = memnew(Button);
|
2022-12-15 07:26:52 +00:00
|
|
|
history_forward->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Forward"), SNAME("EditorIcons")));
|
2022-09-02 13:43:54 +00:00
|
|
|
history_forward->set_flat(true);
|
|
|
|
history_forward->set_focus_mode(FOCUS_NONE);
|
2022-12-15 07:26:52 +00:00
|
|
|
history_forward->connect("pressed", callable_mp(this, &LimboAIEditor::_on_history_forward));
|
2022-09-02 13:43:54 +00:00
|
|
|
nav->add_child(history_forward);
|
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
header = memnew(Button);
|
2022-09-02 12:25:03 +00:00
|
|
|
vb->add_child(header);
|
2022-12-15 07:26:52 +00:00
|
|
|
header->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
|
|
|
|
header->add_theme_constant_override("hseparation", 8);
|
|
|
|
header->connect("pressed", callable_mp(this, &LimboAIEditor::_on_header_pressed));
|
2022-09-01 22:20:37 +00:00
|
|
|
|
2022-09-06 11:28:25 +00:00
|
|
|
hsc = memnew(HSplitContainer);
|
2022-09-02 12:25:03 +00:00
|
|
|
vb->add_child(hsc);
|
|
|
|
hsc->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
hsc->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
task_tree = memnew(TaskTree);
|
2022-09-02 12:25:03 +00:00
|
|
|
hsc->add_child(task_tree);
|
2022-09-01 22:20:37 +00:00
|
|
|
task_tree->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
task_tree->set_h_size_flags(SIZE_EXPAND_FILL);
|
2022-12-15 07:26:52 +00:00
|
|
|
task_tree->connect("rmb_pressed", callable_mp(this, &LimboAIEditor::_on_tree_rmb));
|
|
|
|
task_tree->connect("task_selected", callable_mp(this, &LimboAIEditor::_on_tree_task_selected));
|
|
|
|
task_tree->connect("visibility_changed", callable_mp(this, &LimboAIEditor::_on_visibility_changed));
|
|
|
|
task_tree->connect("task_dragged", callable_mp(this, &LimboAIEditor::_on_task_dragged));
|
|
|
|
task_tree->connect("task_double_clicked", callable_mp(this, &LimboAIEditor::_on_tree_task_double_clicked));
|
2022-09-05 19:57:24 +00:00
|
|
|
task_tree->hide();
|
|
|
|
|
|
|
|
usage_hint = memnew(Panel);
|
|
|
|
usage_hint->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
usage_hint->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
hsc->add_child(usage_hint);
|
|
|
|
Label *usage_label = memnew(Label);
|
2022-12-15 07:26:52 +00:00
|
|
|
usage_label->set_anchor(SIDE_RIGHT, 1);
|
|
|
|
usage_label->set_anchor(SIDE_BOTTOM, 1);
|
|
|
|
usage_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
|
|
|
|
usage_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
|
2022-09-05 19:57:24 +00:00
|
|
|
usage_label->set_text(TTR("Create a new or load an existing behavior tree."));
|
|
|
|
usage_hint->add_child(usage_label);
|
2022-09-02 12:25:03 +00:00
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
task_panel = memnew(TaskPanel());
|
2022-09-02 12:25:03 +00:00
|
|
|
hsc->add_child(task_panel);
|
2022-09-02 13:43:54 +00:00
|
|
|
hsc->set_split_offset(-300);
|
2022-12-15 07:26:52 +00:00
|
|
|
task_panel->connect("task_selected", callable_mp(this, &LimboAIEditor::_on_panel_task_selected));
|
2022-09-05 19:57:24 +00:00
|
|
|
task_panel->hide();
|
2022-09-01 22:20:37 +00:00
|
|
|
|
|
|
|
menu = memnew(PopupMenu);
|
|
|
|
add_child(menu);
|
2022-12-15 07:26:52 +00:00
|
|
|
menu->connect("id_pressed", callable_mp(this, &LimboAIEditor::_on_action_selected));
|
2022-09-01 22:20:37 +00:00
|
|
|
|
2022-11-23 15:25:32 +00:00
|
|
|
rename_dialog = memnew(ConfirmationDialog);
|
|
|
|
{
|
|
|
|
rename_dialog->set_title("Rename Task");
|
|
|
|
|
|
|
|
VBoxContainer *vbc = memnew(VBoxContainer);
|
|
|
|
rename_dialog->add_child(vbc);
|
|
|
|
|
|
|
|
rename_edit = memnew(LineEdit);
|
|
|
|
vbc->add_child(rename_edit);
|
2022-11-23 20:19:23 +00:00
|
|
|
rename_edit->set_placeholder("Custom Name");
|
2022-11-23 15:25:32 +00:00
|
|
|
rename_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
|
|
|
|
rename_edit->set_custom_minimum_size(Size2(350.0, 0.0));
|
|
|
|
|
2022-12-17 12:39:37 +00:00
|
|
|
rename_dialog->register_text_enter(rename_edit);
|
2022-12-15 07:26:52 +00:00
|
|
|
rename_dialog->get_ok_button()->set_text(TTR("Rename"));
|
2022-12-17 12:39:37 +00:00
|
|
|
rename_dialog->connect("confirmed", callable_mp(this, &LimboAIEditor::_rename_task_confirmed));
|
2022-11-23 15:25:32 +00:00
|
|
|
}
|
|
|
|
add_child(rename_dialog);
|
|
|
|
|
2022-11-22 20:00:08 +00:00
|
|
|
disk_changed = memnew(ConfirmationDialog);
|
|
|
|
{
|
|
|
|
VBoxContainer *vbc = memnew(VBoxContainer);
|
|
|
|
disk_changed->add_child(vbc);
|
|
|
|
|
|
|
|
Label *dl = memnew(Label);
|
|
|
|
dl->set_text(TTR("The following BehaviorTree resources are newer on disk.\nWhat action should be taken?"));
|
|
|
|
vbc->add_child(dl);
|
|
|
|
|
|
|
|
disk_changed_list = memnew(Tree);
|
|
|
|
vbc->add_child(disk_changed_list);
|
|
|
|
disk_changed_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
disk_changed->get_ok_button()->set_text(TTR("Reload"));
|
|
|
|
disk_changed->connect("confirmed", callable_mp(this, &LimboAIEditor::_reload_modified));
|
2022-11-22 20:00:08 +00:00
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
disk_changed->add_button(TTR("Resave"), !DisplayServer::get_singleton()->get_swap_cancel_ok(), "resave");
|
|
|
|
disk_changed->connect("custom_action", callable_mp(this, &LimboAIEditor::_resave_modified));
|
2022-11-22 20:00:08 +00:00
|
|
|
}
|
2022-12-15 07:26:52 +00:00
|
|
|
EditorNode::get_singleton()->get_gui_base()->add_child(disk_changed);
|
2022-11-22 20:00:08 +00:00
|
|
|
|
2022-09-05 11:11:47 +00:00
|
|
|
GLOBAL_DEF("limbo_ai/behavior_tree/behavior_tree_default_dir", "res://ai/trees");
|
|
|
|
ProjectSettings::get_singleton()->set_custom_property_info("limbo_ai/behavior_tree/behavior_tree_default_dir",
|
|
|
|
PropertyInfo(Variant::STRING, "limbo_ai/behavior_tree/behavior_tree_default_dir", PROPERTY_HINT_DIR));
|
|
|
|
GLOBAL_DEF("limbo_ai/behavior_tree/user_task_dir_1", "res://ai/tasks");
|
|
|
|
ProjectSettings::get_singleton()->set_custom_property_info("limbo_ai/behavior_tree/user_task_dir_1",
|
|
|
|
PropertyInfo(Variant::STRING, "limbo_ai/behavior_tree/user_task_dir_1", PROPERTY_HINT_DIR));
|
|
|
|
GLOBAL_DEF("limbo_ai/behavior_tree/user_task_dir_2", "");
|
|
|
|
ProjectSettings::get_singleton()->set_custom_property_info("limbo_ai/behavior_tree/user_task_dir_2",
|
|
|
|
PropertyInfo(Variant::STRING, "limbo_ai/behavior_tree/user_task_dir_2", PROPERTY_HINT_DIR));
|
|
|
|
GLOBAL_DEF("limbo_ai/behavior_tree/user_task_dir_3", "");
|
|
|
|
ProjectSettings::get_singleton()->set_custom_property_info("limbo_ai/behavior_tree/user_task_dir_3",
|
|
|
|
PropertyInfo(Variant::STRING, "limbo_ai/behavior_tree/user_task_dir_3", PROPERTY_HINT_DIR));
|
|
|
|
|
|
|
|
save_dialog->set_current_dir(GLOBAL_GET("limbo_ai/behavior_tree/behavior_tree_default_dir"));
|
|
|
|
load_dialog->set_current_dir(GLOBAL_GET("limbo_ai/behavior_tree/behavior_tree_default_dir"));
|
2022-12-15 07:26:52 +00:00
|
|
|
new_script_btn->connect("pressed", callable_mp(ScriptEditor::get_singleton(), &ScriptEditor::open_script_create_dialog).bind("BTAction", String(GLOBAL_GET("limbo_ai/behavior_tree/user_task_dir_1")).path_join("new_task")));
|
2022-11-22 20:00:08 +00:00
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
EditorFileSystem::get_singleton()->connect("resources_reload", callable_mp(this, &LimboAIEditor::_on_resources_reload));
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LimboAIEditor::~LimboAIEditor() {
|
|
|
|
}
|
|
|
|
|
2022-09-03 15:00:20 +00:00
|
|
|
//////////////////////////// LimboAIEditor ///////////////////////////////////
|
|
|
|
|
|
|
|
///////////////////////// LimboAIEditorPlugin ////////////////////////////////
|
2022-09-01 22:20:37 +00:00
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
const Ref<Texture2D> LimboAIEditorPlugin::get_icon() const {
|
|
|
|
return EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("LimboAIEditor"), SNAME("EditorIcons"));
|
2022-09-01 22:20:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 12:01:13 +00:00
|
|
|
void LimboAIEditorPlugin::apply_changes() {
|
|
|
|
limbo_ai_editor->apply_changes();
|
|
|
|
}
|
|
|
|
|
2022-09-01 22:20:37 +00:00
|
|
|
void LimboAIEditorPlugin::_notification(int p_notification) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void LimboAIEditorPlugin::make_visible(bool p_visible) {
|
|
|
|
limbo_ai_editor->set_visible(p_visible);
|
|
|
|
}
|
|
|
|
|
2022-09-05 19:57:24 +00:00
|
|
|
void LimboAIEditorPlugin::edit(Object *p_object) {
|
|
|
|
if (Object::cast_to<BehaviorTree>(p_object)) {
|
|
|
|
limbo_ai_editor->edit_bt(Object::cast_to<BehaviorTree>(p_object));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LimboAIEditorPlugin::handles(Object *p_object) const {
|
2022-12-15 17:29:22 +00:00
|
|
|
if (Object::cast_to<BehaviorTree>(p_object)) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-12-15 18:14:36 +00:00
|
|
|
return false;
|
2022-09-05 19:57:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
LimboAIEditorPlugin::LimboAIEditorPlugin() {
|
|
|
|
limbo_ai_editor = memnew(LimboAIEditor());
|
2022-09-01 22:20:37 +00:00
|
|
|
limbo_ai_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
|
2022-12-15 07:26:52 +00:00
|
|
|
EditorNode::get_singleton()->get_main_screen_control()->add_child(limbo_ai_editor);
|
2022-09-01 22:20:37 +00:00
|
|
|
limbo_ai_editor->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
LimboAIEditorPlugin::~LimboAIEditorPlugin() {
|
|
|
|
}
|
|
|
|
|
2022-12-15 17:29:22 +00:00
|
|
|
#endif // TOOLS_ENABLED
|