/** * editor_property_variable_name.cpp * ============================================================================= * Copyright 2021-2024 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. * ============================================================================= */ #ifdef TOOLS_ENABLED #include "editor_property_variable_name.h" #include "../blackboard/bb_param/bb_param.h" #include "../bt/tasks/bt_task.h" #include "../util/limbo_compat.h" #include "../util/limbo_string_names.h" #include "../util/limbo_utility.h" #include "blackboard_plan_editor.h" #ifdef LIMBOAI_MODULE #include "scene/gui/box_container.h" #include "scene/gui/button.h" #include "scene/gui/line_edit.h" #include "scene/gui/popup_menu.h" #endif // LIMBOAI_MODULE #ifdef LIMBOAI_GDEXTENSION #include #endif // LIMBOAI_GDEXTENSION //***** EditorPropertyVariableName void EditorPropertyVariableName::_show_variables_popup() { ERR_FAIL_NULL(plan); variables_popup->clear(); variables_popup->reset_size(); int idx = 0; for (String var_name : plan->list_vars()) { variables_popup->add_item(var_name, idx); idx += 1; } Transform2D xform = name_edit->get_screen_transform(); Rect2 rect(xform.get_origin(), xform.get_scale() * name_edit->get_size()); rect.position.y += rect.size.height; rect.size.height = 0; variables_popup->set_size(rect.size); variables_popup->set_position(rect.position); variables_popup->popup(rect); } void EditorPropertyVariableName::_name_changed(const String &p_new_name) { emit_changed(get_edited_property(), p_new_name); _update_status(); } void EditorPropertyVariableName::_variable_selected(int p_id) { 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() { status_btn->set_visible(plan.is_valid()); drop_btn->set_visible(plan.is_valid()); if (plan.is_null()) { return; } String var_name = name_edit->get_text(); if (var_name.is_empty() && allow_empty) { 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) { 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 { BUTTON_SET_ICON(status_btn, theme_cache.var_error_icon); 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)))); } } else if (name_edit->get_text().begins_with("_")) { BUTTON_SET_ICON(status_btn, theme_cache.var_private_icon); status_btn->set_tooltip_text(TTR("This variable is private and is not included in the blackboard plan.\nClick to open the blackboard plan.")); } else { BUTTON_SET_ICON(status_btn, theme_cache.var_not_found_icon); status_btn->set_tooltip_text(TTR("No matching variable found in the blackboard plan!\nClick to open the blackboard plan.")); } } 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()->edit_plan(plan); BlackboardPlanEditor::get_singleton()->popup_centered(); } void EditorPropertyVariableName::_status_mouse_entered() { ERR_FAIL_NULL(plan); if (!plan->has_var(name_edit->get_text())) { BUTTON_SET_ICON(status_btn, theme_cache.var_add_icon); } } void EditorPropertyVariableName::_status_mouse_exited() { ERR_FAIL_NULL(plan); _update_status(); } #ifdef LIMBOAI_MODULE void EditorPropertyVariableName::update_property() { #elif LIMBOAI_GDEXTENSION void EditorPropertyVariableName::_update_property() { #endif // LIMBOAI_GDEXTENSION String s = get_edited_object()->get(get_edited_property()); if (name_edit->get_text() != s) { int caret = name_edit->get_caret_column(); name_edit->set_text(s); name_edit->set_caret_column(caret); } name_edit->set_editable(!is_read_only()); _update_status(); } void EditorPropertyVariableName::setup(const Ref &p_plan, bool p_allow_empty, Variant::Type p_type_hint) { plan = p_plan; allow_empty = p_allow_empty; type_hint = p_type_hint; _update_status(); } void EditorPropertyVariableName::_notification(int p_what) { switch (p_what) { case NOTIFICATION_READY: { name_edit->connect(LW_NAME(text_changed), callable_mp(this, &EditorPropertyVariableName::_name_changed)); variables_popup->connect(LW_NAME(id_pressed), callable_mp(this, &EditorPropertyVariableName::_variable_selected)); drop_btn->connect(LW_NAME(pressed), callable_mp(this, &EditorPropertyVariableName::_show_variables_popup)); status_btn->connect(LW_NAME(pressed), callable_mp(this, &EditorPropertyVariableName::_status_pressed)); status_btn->connect(LW_NAME(mouse_entered), callable_mp(this, &EditorPropertyVariableName::_status_mouse_entered)); status_btn->connect(LW_NAME(mouse_exited), callable_mp(this, &EditorPropertyVariableName::_status_mouse_exited)); } break; case NOTIFICATION_ENTER_TREE: { BlackboardPlanEditor::get_singleton()->connect(LW_NAME(visibility_changed), callable_mp(this, &EditorPropertyVariableName::_update_status)); } break; case NOTIFICATION_EXIT_TREE: { if (BlackboardPlanEditor::get_singleton()) { BlackboardPlanEditor::get_singleton()->disconnect(LW_NAME(visibility_changed), callable_mp(this, &EditorPropertyVariableName::_update_status)); } } break; case NOTIFICATION_THEME_CHANGED: { BUTTON_SET_ICON(drop_btn, get_theme_icon(LW_NAME(GuiOptionArrow), LW_NAME(EditorIcons))); theme_cache.var_add_icon = LimboUtility::get_singleton()->get_task_icon(LW_NAME(LimboVarAdd)); theme_cache.var_exists_icon = LimboUtility::get_singleton()->get_task_icon(LW_NAME(LimboVarExists)); theme_cache.var_not_found_icon = LimboUtility::get_singleton()->get_task_icon(LW_NAME(LimboVarNotFound)); theme_cache.var_private_icon = LimboUtility::get_singleton()->get_task_icon(LW_NAME(LimboVarPrivate)); theme_cache.var_empty_icon = LimboUtility::get_singleton()->get_task_icon(LW_NAME(LimboVarEmpty)); theme_cache.var_error_icon = LimboUtility::get_singleton()->get_task_icon(LW_NAME(LimboVarError)); } break; } } EditorPropertyVariableName::EditorPropertyVariableName() { HBoxContainer *hbox = memnew(HBoxContainer); add_child(hbox); hbox->add_theme_constant_override(LW_NAME(separation), 0); name_edit = memnew(LineEdit); hbox->add_child(name_edit); add_focusable(name_edit); name_edit->set_h_size_flags(SIZE_EXPAND_FILL); name_edit->set_placeholder(TTR("Variable name")); drop_btn = memnew(Button); hbox->add_child(drop_btn); drop_btn->set_flat(true); drop_btn->set_focus_mode(FOCUS_NONE); status_btn = memnew(Button); hbox->add_child(status_btn); status_btn->set_flat(true); status_btn->set_focus_mode(FOCUS_NONE); variables_popup = memnew(PopupMenu); add_child(variables_popup); } //***** EditorInspectorPluginVariableName #ifdef LIMBOAI_MODULE bool EditorInspectorPluginVariableName::can_handle(Object *p_object) { #elif LIMBOAI_GDEXTENSION bool EditorInspectorPluginVariableName::_can_handle(Object *p_object) const { #endif Ref task = Object::cast_to(p_object); if (task.is_valid()) { return true; } Ref param = Object::cast_to(p_object); if (param.is_valid()) { return true; } Ref plan = Object::cast_to(p_object); if (plan.is_valid()) { return true; } return false; } #ifdef LIMBOAI_MODULE bool EditorInspectorPluginVariableName::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 p_usage, const bool p_wide) { #elif LIMBOAI_GDEXTENSION bool EditorInspectorPluginVariableName::_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 p_usage, const bool p_wide) { #endif bool is_mapping = p_path.begins_with("mapping/"); if (!(p_type == Variant::Type::STRING_NAME || p_type == Variant::Type::STRING) || !(is_mapping || p_path.ends_with("_var") || p_path.ends_with("variable"))) { return false; } Ref plan; Variant::Type type_hint = Variant::NIL; if (is_mapping) { plan.reference_ptr(Object::cast_to(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(); } plan = plan->get_parent_scope_plan(); ERR_FAIL_NULL_V(plan, false); } else { plan = plan_getter.call(); } EditorPropertyVariableName *ed = memnew(EditorPropertyVariableName); ed->setup(plan, is_mapping, type_hint); add_property_editor(p_path, ed, type_hint); return true; } #endif // TOOLS_ENABLED