Use variable property editor within the param property editor

This commit is contained in:
Serhii Snitsaruk 2024-01-28 19:24:59 +01:00
parent b8bf8e07e9
commit 7ac1bc2dec
4 changed files with 41 additions and 23 deletions

View File

@ -17,7 +17,9 @@
#include "modules/limboai/blackboard/bb_param/bb_param.h" #include "modules/limboai/blackboard/bb_param/bb_param.h"
#include "modules/limboai/blackboard/bb_param/bb_variant.h" #include "modules/limboai/blackboard/bb_param/bb_variant.h"
#include "modules/limboai/editor/editor_property_variable_name.h"
#include "modules/limboai/editor/mode_switch_button.h" #include "modules/limboai/editor/mode_switch_button.h"
#include "modules/limboai/util/limbo_string_names.h"
#include "core/error/error_macros.h" #include "core/error/error_macros.h"
#include "core/io/marshalls.h" #include "core/io/marshalls.h"
@ -263,8 +265,8 @@ void EditorPropertyBBParam::_type_selected(int p_index) {
update_property(); update_property();
} }
void EditorPropertyBBParam::_variable_edited(const String &p_text) { void EditorPropertyBBParam::_variable_edited(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
_get_edited_param()->set_variable(p_text); _get_edited_param()->set_variable(p_value);
} }
void EditorPropertyBBParam::update_property() { void EditorPropertyBBParam::update_property() {
@ -273,14 +275,14 @@ void EditorPropertyBBParam::update_property() {
if (param->get_value_source() == BBParam::BLACKBOARD_VAR) { if (param->get_value_source() == BBParam::BLACKBOARD_VAR) {
_remove_value_editor(); _remove_value_editor();
variable_edit->set_text(param->get_variable()); variable_editor->set_object_and_property(param.ptr(), SNAME("variable"));
variable_edit->set_editable(true); variable_editor->update_property();
variable_edit->show(); variable_editor->show();
mode_button->call_deferred(SNAME("set_mode"), Mode::BIND_VAR, true); mode_button->call_deferred(SNAME("set_mode"), Mode::BIND_VAR, true);
type_choice->hide(); type_choice->hide();
} else { } else {
_create_value_editor(param->get_type()); _create_value_editor(param->get_type());
variable_edit->hide(); variable_editor->hide();
value_editor->show(); value_editor->show();
value_editor->set_object_and_property(param.ptr(), SNAME("saved_value")); value_editor->set_object_and_property(param.ptr(), SNAME("saved_value"));
value_editor->update_property(); value_editor->update_property();
@ -295,9 +297,11 @@ void EditorPropertyBBParam::update_property() {
} }
} }
void EditorPropertyBBParam::setup(PropertyHint p_hint, const String &p_hint_text) { void EditorPropertyBBParam::setup(PropertyHint p_hint, const String &p_hint_text, const Ref<BlackboardPlan> &p_plan) {
param_type = p_hint_text; param_type = p_hint_text;
property_hint = p_hint; property_hint = p_hint;
variable_editor->setup(p_plan);
variable_editor->set_name_split_ratio(0.0);
} }
void EditorPropertyBBParam::_notification(int p_what) { void EditorPropertyBBParam::_notification(int p_what) {
@ -337,7 +341,7 @@ void EditorPropertyBBParam::_notification(int p_what) {
EditorPropertyBBParam::EditorPropertyBBParam() { EditorPropertyBBParam::EditorPropertyBBParam() {
hbox = memnew(HBoxContainer); hbox = memnew(HBoxContainer);
add_child(hbox); add_child(hbox);
hbox->add_theme_constant_override(SNAME("separation"), 0); hbox->add_theme_constant_override(LW_NAME(separation), 0);
bottom_container = memnew(MarginContainer); bottom_container = memnew(MarginContainer);
bottom_container->set_theme_type_variation("MarginContainer4px"); bottom_container->set_theme_type_variation("MarginContainer4px");
@ -346,24 +350,23 @@ EditorPropertyBBParam::EditorPropertyBBParam() {
mode_button = memnew(ModeSwitchButton); mode_button = memnew(ModeSwitchButton);
hbox->add_child(mode_button); hbox->add_child(mode_button);
mode_button->set_focus_mode(FOCUS_NONE); mode_button->set_focus_mode(FOCUS_NONE);
mode_button->connect(SNAME("mode_changed"), callable_mp(this, &EditorPropertyBBParam::_mode_changed)); mode_button->connect(LW_NAME(mode_changed), callable_mp(this, &EditorPropertyBBParam::_mode_changed));
type_choice = memnew(MenuButton); type_choice = memnew(MenuButton);
hbox->add_child(type_choice); hbox->add_child(type_choice);
type_choice->get_popup()->connect(SNAME("id_pressed"), callable_mp(this, &EditorPropertyBBParam::_type_selected)); type_choice->get_popup()->connect(LW_NAME(id_pressed), callable_mp(this, &EditorPropertyBBParam::_type_selected));
type_choice->set_tooltip_text(TTR("Click to choose type")); type_choice->set_tooltip_text(TTR("Click to choose type"));
type_choice->set_flat(false); type_choice->set_flat(false);
editor_hbox = memnew(HBoxContainer); editor_hbox = memnew(HBoxContainer);
hbox->add_child(editor_hbox); hbox->add_child(editor_hbox);
editor_hbox->set_h_size_flags(SIZE_EXPAND_FILL); editor_hbox->set_h_size_flags(SIZE_EXPAND_FILL);
editor_hbox->add_theme_constant_override(SNAME("separation"), 0); editor_hbox->add_theme_constant_override(LW_NAME(separation), 0);
variable_edit = memnew(LineEdit); variable_editor = memnew(EditorPropertyVariableName);
editor_hbox->add_child(variable_edit); editor_hbox->add_child(variable_editor);
variable_edit->set_placeholder(TTR("Variable")); variable_editor->set_h_size_flags(SIZE_EXPAND_FILL);
variable_edit->set_h_size_flags(SIZE_EXPAND_FILL); variable_editor->connect(SNAME("property_changed"), callable_mp(this, &EditorPropertyBBParam::_variable_edited));
variable_edit->connect(SNAME("text_changed"), callable_mp(this, &EditorPropertyBBParam::_variable_edited));
param_type = SNAME("BBString"); param_type = SNAME("BBString");
} }
@ -378,7 +381,7 @@ bool EditorInspectorPluginBBParam::parse_property(Object *p_object, const Varian
if (p_hint == PROPERTY_HINT_RESOURCE_TYPE && p_hint_text.begins_with("BB")) { if (p_hint == PROPERTY_HINT_RESOURCE_TYPE && p_hint_text.begins_with("BB")) {
// TODO: Add more rigid hint check. // TODO: Add more rigid hint check.
EditorPropertyBBParam *editor = memnew(EditorPropertyBBParam()); EditorPropertyBBParam *editor = memnew(EditorPropertyBBParam());
editor->setup(p_hint, p_hint_text); editor->setup(p_hint, p_hint_text, plan_getter.call());
add_property_editor(p_path, editor); add_property_editor(p_path, editor);
return true; return true;
} }

View File

@ -19,12 +19,15 @@
#include "editor/editor_inspector.h" #include "editor/editor_inspector.h"
#include "modules/limboai/blackboard/bb_param/bb_param.h" #include "modules/limboai/blackboard/bb_param/bb_param.h"
#include "modules/limboai/blackboard/blackboard_plan.h"
#include "modules/limboai/editor/mode_switch_button.h" #include "modules/limboai/editor/mode_switch_button.h"
#include "scene/gui/box_container.h" #include "scene/gui/box_container.h"
#include "scene/gui/margin_container.h" #include "scene/gui/margin_container.h"
#include "scene/gui/menu_button.h" #include "scene/gui/menu_button.h"
class EditorPropertyVariableName;
class EditorPropertyBBParam : public EditorProperty { class EditorPropertyBBParam : public EditorProperty {
GDCLASS(EditorPropertyBBParam, EditorProperty); GDCLASS(EditorPropertyBBParam, EditorProperty);
@ -43,7 +46,7 @@ private:
HBoxContainer *editor_hbox = nullptr; HBoxContainer *editor_hbox = nullptr;
ModeSwitchButton *mode_button = nullptr; ModeSwitchButton *mode_button = nullptr;
EditorProperty *value_editor = nullptr; EditorProperty *value_editor = nullptr;
LineEdit *variable_edit = nullptr; EditorPropertyVariableName *variable_editor = nullptr;
MenuButton *type_choice = nullptr; MenuButton *type_choice = nullptr;
Ref<BBParam> _get_edited_param(); Ref<BBParam> _get_edited_param();
@ -52,7 +55,7 @@ private:
void _remove_value_editor(); void _remove_value_editor();
void _value_edited(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false); 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 _variable_edited(const String &p_property, Variant p_value, const String &p_name = "", bool p_changing = false);
void _mode_changed(); void _mode_changed();
void _type_selected(int p_index); void _type_selected(int p_index);
@ -61,7 +64,7 @@ protected:
public: public:
virtual void update_property() override; virtual void update_property() override;
void setup(PropertyHint p_hint, const String &p_hint_text); void setup(PropertyHint p_hint, const String &p_hint_text, const Ref<BlackboardPlan> &p_plan);
EditorPropertyBBParam(); EditorPropertyBBParam();
}; };
@ -69,9 +72,14 @@ public:
class EditorInspectorPluginBBParam : public EditorInspectorPlugin { class EditorInspectorPluginBBParam : public EditorInspectorPlugin {
GDCLASS(EditorInspectorPluginBBParam, EditorInspectorPlugin); GDCLASS(EditorInspectorPluginBBParam, EditorInspectorPlugin);
private:
Callable plan_getter;
public: public:
virtual bool can_handle(Object *p_object) override; virtual bool can_handle(Object *p_object) override;
virtual bool parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide = false) override; virtual bool parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide = false) override;
void set_plan_getter(const Callable &p_getter) { plan_getter = p_getter; }
}; };
#endif // ! LIMBOAI_MODULE #endif // ! LIMBOAI_MODULE

View File

@ -60,11 +60,15 @@ void EditorPropertyVariableName::_name_changed(const String &p_new_name) {
} }
void EditorPropertyVariableName::_variable_selected(int p_id) { void EditorPropertyVariableName::_variable_selected(int p_id) {
_name_changed(plan->get_var_by_index(p_id).first); String var_name = plan->get_var_by_index(p_id).first;
name_edit->set_text(var_name);
_name_changed(var_name);
} }
void EditorPropertyVariableName::_update_status() { void EditorPropertyVariableName::_update_status() {
ERR_FAIL_NULL(plan); if (plan.is_null()) {
return;
}
if (plan->has_var(name_edit->get_text())) { if (plan->has_var(name_edit->get_text())) {
BUTTON_SET_ICON(status_btn, theme_cache.var_exists_icon); BUTTON_SET_ICON(status_btn, theme_cache.var_exists_icon);
status_btn->set_tooltip_text(TTR("This variable exists in the blackboard plan.\n\nClick to open blackboard plan.")); status_btn->set_tooltip_text(TTR("This variable exists in the blackboard plan.\n\nClick to open blackboard plan."));
@ -108,6 +112,7 @@ void EditorPropertyVariableName::_update_property() {
void EditorPropertyVariableName::setup(const Ref<BlackboardPlan> &p_plan) { void EditorPropertyVariableName::setup(const Ref<BlackboardPlan> &p_plan) {
plan = p_plan; plan = p_plan;
_update_status();
} }
void EditorPropertyVariableName::_notification(int p_what) { void EditorPropertyVariableName::_notification(int p_what) {

View File

@ -1431,7 +1431,9 @@ void LimboAIEditorPlugin::_notification(int p_notification) {
add_inspector_plugin(var_plugin); add_inspector_plugin(var_plugin);
#ifdef LIMBOAI_MODULE #ifdef LIMBOAI_MODULE
// ! Only used in the module version. // ! Only used in the module version.
add_inspector_plugin(memnew(EditorInspectorPluginBBParam)); EditorInspectorPluginBBParam *param_plugin = memnew(EditorInspectorPluginBBParam);
param_plugin->set_plan_getter(Callable(limbo_ai_editor, "get_edited_blackboard_plan"));
add_inspector_plugin(param_plugin);
#endif // LIMBOAI_MODULE #endif // LIMBOAI_MODULE
} break; } break;
case NOTIFICATION_ENTER_TREE: { case NOTIFICATION_ENTER_TREE: {