From a717a3ed8f2fb2c3bd058ff050909780212fe7ff Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Fri, 18 Aug 2023 21:38:28 +0200 Subject: [PATCH] Add BTComment task --- bt/tasks/bt_comment.cpp | 19 +++++++++++++++++++ bt/tasks/bt_comment.h | 26 ++++++++++++++++++++++++++ bt/tasks/bt_task.cpp | 13 +++++++++++-- config.py | 1 + editor/limbo_ai_editor_plugin.cpp | 16 ++++++++++++++-- editor/limbo_ai_editor_plugin.h | 1 + register_types.cpp | 3 +++ 7 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 bt/tasks/bt_comment.cpp create mode 100644 bt/tasks/bt_comment.h diff --git a/bt/tasks/bt_comment.cpp b/bt/tasks/bt_comment.cpp new file mode 100644 index 0000000..aab4f4d --- /dev/null +++ b/bt/tasks/bt_comment.cpp @@ -0,0 +1,19 @@ +/** + * bt_comment.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. + * ============================================================================= + */ + +#include "bt_comment.h" + +Ref BTComment::clone() const { + if (Engine::get_singleton()->is_editor_hint()) { + return BTTask::clone(); + } + return nullptr; +} diff --git a/bt/tasks/bt_comment.h b/bt/tasks/bt_comment.h new file mode 100644 index 0000000..7299415 --- /dev/null +++ b/bt/tasks/bt_comment.h @@ -0,0 +1,26 @@ +/** + * bt_comment.h + * ============================================================================= + * 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. + * ============================================================================= + */ +/* bt_comment.h */ + +#ifndef BT_COMMENT_H +#define BT_COMMENT_H + +#include "bt_task.h" + +class BTComment : public BTTask { + GDCLASS(BTComment, BTTask); + +private: +public: + virtual Ref clone() const override; +}; + +#endif // BT_COMMENT \ No newline at end of file diff --git a/bt/tasks/bt_task.cpp b/bt/tasks/bt_task.cpp index f3cc2fe..ec24ce5 100644 --- a/bt/tasks/bt_task.cpp +++ b/bt/tasks/bt_task.cpp @@ -104,10 +104,19 @@ Ref BTTask::clone() const { inst->data.parent = nullptr; inst->data.agent = nullptr; inst->data.blackboard.unref(); + int num_null = 0; for (int i = 0; i < data.children.size(); i++) { Ref c = get_child(i)->clone(); - c->data.parent = inst.ptr(); - inst->data.children.set(i, c); + if (c.is_valid()) { + c->data.parent = inst.ptr(); + inst->data.children.set(i - num_null, c); + } else { + num_null += 1; + } + } + if (num_null > 0) { + // * BTComment tasks return nullptr at runtime - we remove those. + inst->data.children.resize(data.children.size() - num_null); } // Make BBParam properties unique. diff --git a/config.py b/config.py index ef9e5dd..3c1b166 100644 --- a/config.py +++ b/config.py @@ -65,6 +65,7 @@ def get_doc_classes(): "BTCheckAgentProperty", "BTCheckTrigger", "BTCheckVar", + "BTComment", "BTComposite", "BTCondition", "BTConsolePrint", diff --git a/editor/limbo_ai_editor_plugin.cpp b/editor/limbo_ai_editor_plugin.cpp index 935672a..e2178d2 100644 --- a/editor/limbo_ai_editor_plugin.cpp +++ b/editor/limbo_ai_editor_plugin.cpp @@ -94,9 +94,12 @@ void TaskTree::_update_item(TreeItem *p_item) { Ref 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()); - if (task->get_custom_name().is_empty()) { + if (task->is_class("BTComment")) { + p_item->set_custom_font(0, (get_theme_font(SNAME("doc_italic"), SNAME("EditorFonts")))); + p_item->set_custom_color(0, get_theme_color(SNAME("disabled_font_color"), SNAME("Editor"))); + } else if (task->get_custom_name().is_empty()) { p_item->set_custom_font(0, nullptr); - // p_item->clear_custom_color(0); + p_item->clear_custom_color(0); } else { p_item->set_custom_font(0, (get_theme_font(SNAME("bold"), SNAME("EditorFonts")))); // p_item->set_custom_color(0, get_theme_color(SNAME("warning_color"), SNAME("Editor"))); @@ -1333,6 +1336,15 @@ LimboAIEditor::LimboAIEditor() { fav_tasks_hbox = memnew(HBoxContainer); toolbar->add_child(fav_tasks_hbox); + comment_btn = memnew(Button); + comment_btn->set_text(TTR("Comment")); + comment_btn->set_icon(LimboUtility::get_singleton()->get_task_icon("BTComment")); + comment_btn->set_tooltip_text(TTR("Add a BTComment task.")); + comment_btn->set_flat(true); + comment_btn->set_focus_mode(Control::FOCUS_NONE); + comment_btn->connect("pressed", callable_mp(this, &LimboAIEditor::_add_task_by_class_or_path).bind("BTComment")); + toolbar->add_child(comment_btn); + toolbar->add_child(memnew(VSeparator)); new_btn = memnew(Button); diff --git a/editor/limbo_ai_editor_plugin.h b/editor/limbo_ai_editor_plugin.h index 0076a8b..2321ebb 100644 --- a/editor/limbo_ai_editor_plugin.h +++ b/editor/limbo_ai_editor_plugin.h @@ -171,6 +171,7 @@ private: TaskPanel *task_panel; HBoxContainer *fav_tasks_hbox; + Button *comment_btn; Button *new_btn; Button *load_btn; Button *save_btn; diff --git a/register_types.cpp b/register_types.cpp index 84743a2..6502698 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -60,6 +60,7 @@ #include "bt/tasks/actions/bt_wait.h" #include "bt/tasks/actions/bt_wait_ticks.h" #include "bt/tasks/bt_action.h" +#include "bt/tasks/bt_comment.h" #include "bt/tasks/bt_composite.h" #include "bt/tasks/bt_condition.h" #include "bt/tasks/bt_decorator.h" @@ -120,6 +121,8 @@ void initialize_limboai_module(ModuleInitializationLevel p_level) { GDREGISTER_CLASS(BTPlayer); GDREGISTER_CLASS(BTState); + GDREGISTER_CLASS(BTComment); + GDREGISTER_CLASS(BTComposite); GDREGISTER_CLASS(BTSequence); GDREGISTER_CLASS(BTSelector);