Edit bt via inspector and add usage panel

This commit is contained in:
Serhii Snitsaruk 2022-09-05 21:57:24 +02:00
parent 7eff8d5488
commit 507dc8d850
2 changed files with 47 additions and 10 deletions

View File

@ -24,6 +24,7 @@
#include "core/ustring.h" #include "core/ustring.h"
#include "core/variant.h" #include "core/variant.h"
#include "core/vector.h" #include "core/vector.h"
#include "editor/editor_inspector.h"
#include "editor/editor_node.h" #include "editor/editor_node.h"
#include "editor/editor_plugin.h" #include "editor/editor_plugin.h"
#include "editor/editor_scale.h" #include "editor/editor_scale.h"
@ -465,6 +466,7 @@ TaskPanel::~TaskPanel() {
void LimboAIEditor::_add_task(const Ref<BTTask> &p_task) { void LimboAIEditor::_add_task(const Ref<BTTask> &p_task) {
ERR_FAIL_COND(p_task.is_null()); ERR_FAIL_COND(p_task.is_null());
ERR_FAIL_COND(task_tree->get_bt().is_null());
Ref<BTTask> parent = task_tree->get_selected(); Ref<BTTask> parent = task_tree->get_selected();
if (parent.is_null()) { if (parent.is_null()) {
parent = task_tree->get_bt()->get_root_task(); parent = task_tree->get_bt()->get_root_task();
@ -498,7 +500,7 @@ void LimboAIEditor::_update_history_buttons() {
void LimboAIEditor::_new_bt() { void LimboAIEditor::_new_bt() {
BehaviorTree *bt = memnew(BehaviorTree); BehaviorTree *bt = memnew(BehaviorTree);
bt->set_root_task(memnew(BTSelector)); bt->set_root_task(memnew(BTSelector));
_edit_bt(bt); editor->edit_resource(bt);
} }
void LimboAIEditor::_save_bt(String p_path) { void LimboAIEditor::_save_bt(String p_path) {
@ -519,11 +521,16 @@ void LimboAIEditor::_load_bt(String p_path) {
history.push_back(bt); history.push_back(bt);
} }
_edit_bt(bt); editor->edit_resource(bt);
} }
void LimboAIEditor::_edit_bt(Ref<BehaviorTree> p_behavior_tree) { void LimboAIEditor::edit_bt(Ref<BehaviorTree> p_behavior_tree) {
ERR_FAIL_COND_MSG(p_behavior_tree.is_null(), "p_behavior_tree is null"); ERR_FAIL_COND_MSG(p_behavior_tree.is_null(), "p_behavior_tree is null");
if (task_tree->get_bt() == p_behavior_tree) {
return;
}
task_tree->load_bt(p_behavior_tree); task_tree->load_bt(p_behavior_tree);
int idx = history.find(p_behavior_tree); int idx = history.find(p_behavior_tree);
@ -534,6 +541,10 @@ void LimboAIEditor::_edit_bt(Ref<BehaviorTree> p_behavior_tree) {
idx_history = history.size() - 1; idx_history = history.size() - 1;
} }
usage_hint->hide();
task_tree->show();
task_panel->show();
_update_history_buttons(); _update_history_buttons();
_update_header(); _update_header();
} }
@ -656,7 +667,7 @@ void LimboAIEditor::_on_visibility_changed() const {
Ref<BTTask> sel = task_tree->get_selected(); Ref<BTTask> sel = task_tree->get_selected();
if (sel.is_valid()) { if (sel.is_valid()) {
editor->edit_resource(sel); editor->edit_resource(sel);
} else { } else if (task_tree->get_bt().is_valid() && editor->get_inspector()->get_edited_object() != task_tree->get_bt().ptr()) {
editor->edit_resource(task_tree->get_bt()); editor->edit_resource(task_tree->get_bt());
} }
@ -680,12 +691,12 @@ void LimboAIEditor::_on_save_pressed() {
void LimboAIEditor::_on_history_back() { void LimboAIEditor::_on_history_back() {
idx_history = MAX(idx_history - 1, 0); idx_history = MAX(idx_history - 1, 0);
_edit_bt(history[idx_history]); editor->edit_resource(history[idx_history]);
} }
void LimboAIEditor::_on_history_forward() { void LimboAIEditor::_on_history_forward() {
idx_history = MIN(idx_history + 1, history.size() - 1); idx_history = MIN(idx_history + 1, history.size() - 1);
_edit_bt(history[idx_history]); editor->edit_resource(history[idx_history]);
} }
void LimboAIEditor::_on_task_dragged(Ref<BTTask> p_task, Ref<BTTask> p_to_task, int p_type) { void LimboAIEditor::_on_task_dragged(Ref<BTTask> p_task, Ref<BTTask> p_to_task, int p_type) {
@ -765,7 +776,7 @@ void LimboAIEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("_new_bt"), &LimboAIEditor::_new_bt); 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("_save_bt", "p_path"), &LimboAIEditor::_save_bt);
ClassDB::bind_method(D_METHOD("_load_bt", "p_path"), &LimboAIEditor::_load_bt); ClassDB::bind_method(D_METHOD("_load_bt", "p_path"), &LimboAIEditor::_load_bt);
ClassDB::bind_method(D_METHOD("_edit_bt", "p_behavior_tree"), &LimboAIEditor::_edit_bt); ClassDB::bind_method(D_METHOD("edit_bt", "p_behavior_tree"), &LimboAIEditor::edit_bt);
} }
LimboAIEditor::LimboAIEditor(EditorNode *p_editor) { LimboAIEditor::LimboAIEditor(EditorNode *p_editor) {
@ -898,19 +909,31 @@ LimboAIEditor::LimboAIEditor(EditorNode *p_editor) {
task_tree->connect("task_selected", this, "_on_tree_task_selected"); task_tree->connect("task_selected", this, "_on_tree_task_selected");
task_tree->connect("visibility_changed", this, "_on_visibility_changed"); task_tree->connect("visibility_changed", this, "_on_visibility_changed");
task_tree->connect("task_dragged", this, "_on_task_dragged"); task_tree->connect("task_dragged", this, "_on_task_dragged");
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);
usage_label->set_anchor(MARGIN_RIGHT, 1);
usage_label->set_anchor(MARGIN_BOTTOM, 1);
usage_label->set_align(Label::ALIGN_CENTER);
usage_label->set_valign(Label::VALIGN_CENTER);
usage_label->set_text(TTR("Create a new or load an existing behavior tree."));
usage_hint->add_child(usage_label);
task_panel = memnew(TaskPanel(p_editor)); task_panel = memnew(TaskPanel(p_editor));
hsc->add_child(task_panel); hsc->add_child(task_panel);
hsc->set_split_offset(-300); hsc->set_split_offset(-300);
task_panel->connect("task_selected", this, "_on_panel_task_selected"); task_panel->connect("task_selected", this, "_on_panel_task_selected");
task_panel->hide();
menu = memnew(PopupMenu); menu = memnew(PopupMenu);
add_child(menu); add_child(menu);
menu->connect("id_pressed", this, "_on_action_selected"); menu->connect("id_pressed", this, "_on_action_selected");
menu->set_hide_on_window_lose_focus(true); menu->set_hide_on_window_lose_focus(true);
_new_bt();
GLOBAL_DEF("limbo_ai/behavior_tree/behavior_tree_default_dir", "res://ai/trees"); 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", 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)); PropertyInfo(Variant::STRING, "limbo_ai/behavior_tree/behavior_tree_default_dir", PROPERTY_HINT_DIR));
@ -954,6 +977,16 @@ void LimboAIEditorPlugin::make_visible(bool p_visible) {
limbo_ai_editor->set_visible(p_visible); limbo_ai_editor->set_visible(p_visible);
} }
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 {
return p_object->is_class("BehaviorTree");
}
LimboAIEditorPlugin::LimboAIEditorPlugin(EditorNode *p_editor) { LimboAIEditorPlugin::LimboAIEditorPlugin(EditorNode *p_editor) {
editor = p_editor; editor = p_editor;
limbo_ai_editor = memnew(LimboAIEditor(p_editor)); limbo_ai_editor = memnew(LimboAIEditor(p_editor));

View File

@ -117,6 +117,7 @@ private:
Button *header; Button *header;
TaskTree *task_tree; TaskTree *task_tree;
Panel *usage_hint;
PopupMenu *menu; PopupMenu *menu;
FileDialog *save_dialog; FileDialog *save_dialog;
FileDialog *load_dialog; FileDialog *load_dialog;
@ -131,7 +132,6 @@ private:
void _new_bt(); void _new_bt();
void _save_bt(String p_path); void _save_bt(String p_path);
void _load_bt(String p_path); void _load_bt(String p_path);
void _edit_bt(Ref<BehaviorTree> p_behavior_tree);
void _mark_as_dirty(bool p_dirty); void _mark_as_dirty(bool p_dirty);
void _on_tree_rmb(const Vector2 &p_menu_pos); void _on_tree_rmb(const Vector2 &p_menu_pos);
@ -151,6 +151,8 @@ protected:
public: public:
static Ref<Texture> get_task_icon(String p_script_path_or_class); static Ref<Texture> get_task_icon(String p_script_path_or_class);
void edit_bt(Ref<BehaviorTree> p_behavior_tree);
void apply_changes(); void apply_changes();
LimboAIEditor(EditorNode *p_editor); LimboAIEditor(EditorNode *p_editor);
@ -173,6 +175,8 @@ public:
bool has_main_screen() const { return true; } bool has_main_screen() const { return true; }
virtual void make_visible(bool p_visible); virtual void make_visible(bool p_visible);
virtual void apply_changes(); virtual void apply_changes();
virtual void edit(Object *p_object);
virtual bool handles(Object *p_object) const;
LimboAIEditorPlugin(EditorNode *p_editor); LimboAIEditorPlugin(EditorNode *p_editor);
~LimboAIEditorPlugin(); ~LimboAIEditorPlugin();