BlackboardPlan: Auto-fill type info when adding a missing variable for a mapping

This commit is contained in:
Serhii Snitsaruk 2024-05-14 22:55:25 +02:00
parent ef1c1e5192
commit c30c5a4d7a
No known key found for this signature in database
GPG Key ID: A965EF8799FFEC2D
5 changed files with 41 additions and 20 deletions

View File

@ -54,8 +54,9 @@ void BlackboardPlanEditor::_add_var() {
var_name = String(default_var_name) + itos(suffix);
}
BBVariable var(Variant::Type::FLOAT);
BBVariable var(default_type, default_hint, default_hint_string);
plan->add_var(var_name, var);
reset_defaults();
_refresh();
}
@ -127,10 +128,18 @@ void BlackboardPlanEditor::edit_plan(const Ref<BlackboardPlan> &p_plan) {
_refresh();
}
void BlackboardPlanEditor::set_next_var_name(const StringName &p_name) {
if (String(p_name).is_valid_identifier()) {
default_var_name = p_name;
void BlackboardPlanEditor::set_defaults(const StringName &p_var_name, Variant::Type p_type, PropertyHint p_hint, String p_hint_string) {
default_var_name = p_var_name;
default_type = p_type;
default_hint = p_hint;
default_hint_string = p_hint_string;
}
void BlackboardPlanEditor::reset_defaults() {
default_var_name = "var";
default_type = Variant::FLOAT;
default_hint = PROPERTY_HINT_NONE;
default_hint_string = "";
}
void BlackboardPlanEditor::_show_button_popup(Button *p_button, PopupMenu *p_popup, int p_index) {
@ -233,7 +242,7 @@ void BlackboardPlanEditor::_drag_button_gui_input(const Ref<InputEvent> &p_event
void BlackboardPlanEditor::_visibility_changed() {
if (!is_visible() && plan.is_valid()) {
plan->notify_property_list_changed();
default_var_name = "var";
reset_defaults();
}
}
@ -367,7 +376,7 @@ void BlackboardPlanEditor::_notification(int p_what) {
}
BlackboardPlanEditor::BlackboardPlanEditor() {
default_var_name = "var";
reset_defaults();
set_title(TTR("Manage Blackboard Plan"));

View File

@ -59,6 +59,9 @@ private:
Ref<BlackboardPlan> plan;
StringName default_var_name;
Variant::Type default_type = Variant::NIL;
PropertyHint default_hint = PROPERTY_HINT_NONE;
String default_hint_string;
VBoxContainer *rows_vbox;
Button *add_var_tool;
@ -99,7 +102,8 @@ public:
_FORCE_INLINE_ static BlackboardPlanEditor *get_singleton() { return singleton; }
void edit_plan(const Ref<BlackboardPlan> &p_plan);
void set_next_var_name(const StringName &p_name);
void set_defaults(const StringName &p_name, Variant::Type p_type = Variant::FLOAT, PropertyHint p_hint = PROPERTY_HINT_NONE, String p_hint_string = "");
void reset_defaults();
BlackboardPlanEditor();
};

View File

@ -300,7 +300,7 @@ void EditorPropertyBBParam::update_property() {
void EditorPropertyBBParam::setup(PropertyHint p_hint, const String &p_hint_text, const Ref<BlackboardPlan> &p_plan) {
param_type = p_hint_text;
property_hint = p_hint;
variable_editor->setup(p_plan);
variable_editor->setup(p_plan, false);
variable_editor->set_name_split_ratio(0.0);
}

View File

@ -76,7 +76,7 @@ void EditorPropertyVariableName::_update_status() {
BUTTON_SET_ICON(status_btn, theme_cache.var_empty_icon);
status_btn->set_tooltip_text(TTR("Variable name not specified.\nClick to open the blackboard plan."));
} else if (plan->has_var(var_name)) {
if (type_hint == Variant::NIL || plan->get_var(var_name).get_type() == type_hint) {
if (expected_type == Variant::NIL || plan->get_var(var_name).get_type() == expected_type) {
BUTTON_SET_ICON(status_btn, theme_cache.var_exists_icon);
status_btn->set_tooltip_text(TTR("This variable is present in the blackboard plan.\nClick to open the blackboard plan."));
} else {
@ -84,7 +84,7 @@ void EditorPropertyVariableName::_update_status() {
status_btn->set_tooltip_text(TTR(vformat(
"The %s variable in the blackboard plan is not of the same type as this variable (expected %s).\nClick to open the blackboard plan and fix the variable type.",
LimboUtility::get_singleton()->decorate_var(var_name),
Variant::get_type_name(type_hint))));
Variant::get_type_name(expected_type))));
}
} else if (name_edit->get_text().begins_with("_")) {
BUTTON_SET_ICON(status_btn, theme_cache.var_private_icon);
@ -98,7 +98,7 @@ void EditorPropertyVariableName::_update_status() {
void EditorPropertyVariableName::_status_pressed() {
ERR_FAIL_NULL(plan);
if (!plan->has_var(name_edit->get_text())) {
BlackboardPlanEditor::get_singleton()->set_next_var_name(name_edit->get_text());
BlackboardPlanEditor::get_singleton()->set_defaults(name_edit->get_text(), expected_type, expected_hint, expected_hint_string);
}
BlackboardPlanEditor::get_singleton()->edit_plan(plan);
BlackboardPlanEditor::get_singleton()->popup_centered();
@ -131,10 +131,12 @@ void EditorPropertyVariableName::_update_property() {
_update_status();
}
void EditorPropertyVariableName::setup(const Ref<BlackboardPlan> &p_plan, bool p_allow_empty, Variant::Type p_type_hint) {
void EditorPropertyVariableName::setup(const Ref<BlackboardPlan> &p_plan, bool p_allow_empty, Variant::Type p_type, PropertyHint p_hint, String p_hint_string) {
plan = p_plan;
allow_empty = p_allow_empty;
type_hint = p_type_hint;
expected_type = p_type;
expected_hint = p_hint;
expected_hint_string = p_hint_string;
_update_status();
}
@ -226,14 +228,18 @@ bool EditorInspectorPluginVariableName::_parse_property(Object *p_object, const
}
Ref<BlackboardPlan> plan;
Variant::Type type_hint = Variant::NIL;
Variant::Type expected_type = Variant::NIL;
PropertyHint expected_hint = PROPERTY_HINT_NONE;
String expected_hint_string;
if (is_mapping) {
plan.reference_ptr(Object::cast_to<BlackboardPlan>(p_object));
ERR_FAIL_NULL_V(plan, false);
String var_name = p_path.trim_prefix("mapping/");
if (plan->has_var(var_name)) {
BBVariable variable = plan->get_var(var_name);
type_hint = variable.get_type();
expected_type = variable.get_type();
expected_hint = variable.get_hint();
expected_hint_string = variable.get_hint_string();
}
plan = plan->get_parent_scope_plan();
ERR_FAIL_NULL_V(plan, false);
@ -242,8 +248,8 @@ bool EditorInspectorPluginVariableName::_parse_property(Object *p_object, const
}
EditorPropertyVariableName *ed = memnew(EditorPropertyVariableName);
ed->setup(plan, is_mapping, type_hint);
add_property_editor(p_path, ed, type_hint);
ed->setup(plan, is_mapping, expected_type, expected_hint, expected_hint_string);
add_property_editor(p_path, ed, expected_type);
return true;
}

View File

@ -45,7 +45,9 @@ private:
Ref<BlackboardPlan> plan;
bool allow_empty = false;
Variant::Type type_hint = Variant::NIL;
Variant::Type expected_type = Variant::NIL;
PropertyHint expected_hint = PROPERTY_HINT_NONE;
String expected_hint_string;
LineEdit *name_edit;
Button *drop_btn;
@ -73,7 +75,7 @@ public:
virtual void _update_property() override;
#endif
void setup(const Ref<BlackboardPlan> &p_plan, bool p_allow_empty = false, Variant::Type p_type_hint = Variant::NIL);
void setup(const Ref<BlackboardPlan> &p_plan, bool p_allow_empty, Variant::Type p_type = Variant::FLOAT, PropertyHint p_hint = PROPERTY_HINT_NONE, String p_hint_string = "");
EditorPropertyVariableName();
};