diff --git a/blackboard/bb_param/bb_variant.h b/blackboard/bb_param/bb_variant.h index d8664bd..1eb95bc 100644 --- a/blackboard/bb_param/bb_variant.h +++ b/blackboard/bb_param/bb_variant.h @@ -25,10 +25,10 @@ private: protected: static void _bind_methods(); +public: virtual Variant::Type get_type() const override; void set_type(Variant::Type p_type); -public: BBVariant(); }; diff --git a/editor/editor_property_bb_param.cpp b/editor/editor_property_bb_param.cpp index 75898fc..5cf8064 100644 --- a/editor/editor_property_bb_param.cpp +++ b/editor/editor_property_bb_param.cpp @@ -11,11 +11,14 @@ #include "editor_property_bb_param.h" +#include "core/variant/variant.h" #include "modules/limboai/blackboard/bb_param/bb_param.h" +#include "modules/limboai/blackboard/bb_param/bb_variant.h" #include "modules/limboai/editor/mode_switch_button.h" #include "core/error/error_macros.h" #include "core/object/class_db.h" +#include "core/object/ref_counted.h" #include "core/os/memory.h" #include "core/string/print_string.h" #include "editor/editor_inspector.h" @@ -27,6 +30,7 @@ #include "scene/gui/box_container.h" #include "scene/gui/button.h" #include "scene/gui/line_edit.h" +#include "scene/gui/menu_button.h" Ref EditorPropertyBBParam::_get_edited_param() { Ref param = get_edited_property_value(); @@ -47,6 +51,9 @@ void EditorPropertyBBParam::_create_value_editor(Variant::Type p_type) { } switch (p_type) { + case Variant::NIL: { + value_editor = memnew(EditorPropertyNil); + } break; case Variant::BOOL: { value_editor = memnew(EditorPropertyCheck); } break; @@ -186,7 +193,7 @@ void EditorPropertyBBParam::_create_value_editor(Variant::Type p_type) { } } value_editor->set_name_split_ratio(0.0); - hbox->add_child(value_editor); + editor_hbox->add_child(value_editor); value_editor->set_h_size_flags(SIZE_EXPAND_FILL); value_editor->set_meta(SNAME("_param_type"), p_type); value_editor->connect(SNAME("property_changed"), callable_mp(this, &EditorPropertyBBParam::_value_edited)); @@ -194,7 +201,7 @@ void EditorPropertyBBParam::_create_value_editor(Variant::Type p_type) { void EditorPropertyBBParam::_remove_value_editor() { if (value_editor) { - hbox->remove_child(value_editor); + editor_hbox->remove_child(value_editor); value_editor->queue_free(); value_editor = nullptr; } @@ -209,6 +216,16 @@ void EditorPropertyBBParam::_mode_changed() { update_property(); } +void EditorPropertyBBParam::_type_selected(int p_index) { + Ref param = _get_edited_param(); + ERR_FAIL_COND(param.is_null()); + Variant::Type t = Variant::Type(p_index); + param->set_type(t); + String type_name = Variant::get_type_name(t); + type_choice->set_icon(get_editor_theme_icon(type_name)); + update_property(); +} + void EditorPropertyBBParam::_variable_edited(const String &p_text) { _get_edited_param()->set_variable(p_text); } @@ -240,12 +257,32 @@ void EditorPropertyBBParam::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: case NOTIFICATION_THEME_CHANGED: { - int id = mode_button->get_mode(); + { + String type = Variant::get_type_name(_get_edited_param()->get_type()); + type_choice->set_icon(get_editor_theme_icon(type)); + } + + // Initialize mode button. mode_button->clear(); mode_button->add_mode(Mode::SPECIFY_VALUE, get_editor_theme_icon(SNAME("LimboSpecifyValue")), TTR("Mode: Specify value.\nClick to switch mode.")); mode_button->add_mode(Mode::BIND_VAR, get_editor_theme_icon(SNAME("BTSetVar")), TTR("Mode: Bind blackboard variable.\nClick to switch mode.")); - if (id >= 0) { - mode_button->set_mode(id); + mode_button->set_mode(_get_edited_param()->get_value_source() == BBParam::BLACKBOARD_VAR ? Mode::BIND_VAR : Mode::SPECIFY_VALUE); + + Ref variant_param = _get_edited_param(); + bool is_variant_param = variant_param.is_valid(); + if (is_variant_param) { + // Initialize type choice. + PopupMenu *type_menu = type_choice->get_popup(); + type_menu->clear(); + for (int i = 0; i < Variant::VARIANT_MAX; i++) { + if (i == Variant::RID || i == Variant::CALLABLE || i == Variant::OBJECT || i == Variant::SIGNAL) { + continue; + } + String type = Variant::get_type_name(Variant::Type(i)); + type_menu->add_icon_item(get_editor_theme_icon(type), type, i); + } + } else { // Not a variant param. + type_choice->hide(); } } break; } @@ -261,12 +298,21 @@ EditorPropertyBBParam::EditorPropertyBBParam() { mode_button->set_focus_mode(FOCUS_NONE); mode_button->connect(SNAME("mode_changed"), callable_mp(this, &EditorPropertyBBParam::_mode_changed)); + editor_hbox = memnew(HBoxContainer); + hbox->add_child(editor_hbox); + editor_hbox->set_h_size_flags(SIZE_EXPAND_FILL); + editor_hbox->add_theme_constant_override(SNAME("separation"), 0); + variable_edit = memnew(LineEdit); - hbox->add_child(variable_edit); + editor_hbox->add_child(variable_edit); variable_edit->set_placeholder(TTR("Variable")); variable_edit->set_h_size_flags(SIZE_EXPAND_FILL); variable_edit->connect(SNAME("text_changed"), callable_mp(this, &EditorPropertyBBParam::_variable_edited)); + type_choice = memnew(MenuButton); + hbox->add_child(type_choice); + type_choice->get_popup()->connect(SNAME("id_pressed"), callable_mp(this, &EditorPropertyBBParam::_type_selected)); + param_type = SNAME("BBString"); } diff --git a/editor/editor_property_bb_param.h b/editor/editor_property_bb_param.h index 85c2e86..e1c2d40 100644 --- a/editor/editor_property_bb_param.h +++ b/editor/editor_property_bb_param.h @@ -12,12 +12,14 @@ #ifndef EDITOR_PROPERTY_BB_PARAM_H #define EDITOR_PROPERTY_BB_PARAM_H -#include "core/object/object.h" #include "editor/editor_inspector.h" #include "modules/limboai/blackboard/bb_param/bb_param.h" #include "modules/limboai/editor/mode_switch_button.h" +#include "scene/gui/box_container.h" +#include "scene/gui/menu_button.h" + class EditorPropertyBBParam : public EditorProperty { GDCLASS(EditorPropertyBBParam, EditorProperty); @@ -32,9 +34,11 @@ private: Mode mode = Mode::SPECIFY_VALUE; HBoxContainer *hbox = nullptr; + HBoxContainer *editor_hbox = nullptr; ModeSwitchButton *mode_button = nullptr; EditorProperty *value_editor = nullptr; LineEdit *variable_edit = nullptr; + MenuButton *type_choice = nullptr; Ref _get_edited_param(); @@ -44,6 +48,7 @@ private: void _value_edited(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false); void _variable_edited(const String &p_text); void _mode_changed(); + void _type_selected(int p_index); protected: void _notification(int p_what); diff --git a/editor/mode_switch_button.cpp b/editor/mode_switch_button.cpp index e0051c5..b50fa6e 100644 --- a/editor/mode_switch_button.cpp +++ b/editor/mode_switch_button.cpp @@ -32,7 +32,7 @@ void ModeSwitchButton::add_mode(int p_id, const Ref &p_icon, const St modes.append(mode); if (current_mode_index == -1) { - _set_mode(0); + _set_mode_by_index(0); } } @@ -48,7 +48,7 @@ void ModeSwitchButton::set_mode(int p_id, bool p_no_signal) { } ERR_FAIL_COND_MSG(idx == -1, "Button state not found with such id: " + itos(p_id)); - _set_mode(p_id); + _set_mode_by_index(idx); if (!p_no_signal) { emit_signal(SNAME("mode_changed")); } diff --git a/editor/mode_switch_button.h b/editor/mode_switch_button.h index e4830b4..b27e92e 100644 --- a/editor/mode_switch_button.h +++ b/editor/mode_switch_button.h @@ -30,8 +30,8 @@ private: Vector modes; - _FORCE_INLINE_ void _set_mode(int p_id) { - current_mode_index = p_id; + _FORCE_INLINE_ void _set_mode_by_index(int p_index) { + current_mode_index = p_index; set_icon(modes[current_mode_index].icon); if (!modes[current_mode_index].tooltip.is_empty()) { set_tooltip_text(modes[current_mode_index].tooltip);