/*************************************************************************/ /* property_editor.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "property_editor.h" #include "core/config/project_settings.h" #include "core/input/input.h" #include "core/io/image_loader.h" #include "core/io/marshalls.h" #include "core/io/resource_loader.h" #include "core/math/expression.h" #include "core/object/class_db.h" #include "core/os/keyboard.h" #include "core/string/print_string.h" #include "core/templates/pair.h" #include "editor/array_property_edit.h" #include "editor/create_dialog.h" #include "editor/dictionary_property_edit.h" #include "editor/editor_export.h" #include "editor/editor_file_dialog.h" #include "editor/editor_file_system.h" #include "editor/editor_help.h" #include "editor/editor_node.h" #include "editor/editor_scale.h" #include "editor/editor_settings.h" #include "editor/filesystem_dock.h" #include "editor/multi_node_edit.h" #include "editor/property_selector.h" #include "editor/scene_tree_dock.h" #include "scene/gui/label.h" #include "scene/main/window.h" #include "scene/resources/font.h" #include "scene/resources/packed_scene.h" #include "scene/scene_string_names.h" void EditorResourceConversionPlugin::_bind_methods() { GDVIRTUAL_BIND(_converts_to); GDVIRTUAL_BIND(_handles, "resource"); GDVIRTUAL_BIND(_convert, "resource"); } String EditorResourceConversionPlugin::converts_to() const { String ret; if (GDVIRTUAL_CALL(_converts_to, ret)) { return ret; } return ""; } bool EditorResourceConversionPlugin::handles(const Ref &p_resource) const { bool ret; if (GDVIRTUAL_CALL(_handles, p_resource, ret)) { return ret; } return false; } Ref EditorResourceConversionPlugin::convert(const Ref &p_resource) const { Ref ret; if (GDVIRTUAL_CALL(_convert, p_resource, ret)) { return ret; } return Ref(); } void CustomPropertyEditor::_notification(int p_what) { switch (p_what) { case NOTIFICATION_WM_CLOSE_REQUEST: { hide(); } break; } } void CustomPropertyEditor::_menu_option(int p_which) { switch (type) { case Variant::INT: { if (hint == PROPERTY_HINT_FLAGS) { int val = v; if (val & (1 << p_which)) { val &= ~(1 << p_which); } else { val |= (1 << p_which); } v = val; emit_signal(SNAME("variant_changed")); } else if (hint == PROPERTY_HINT_ENUM) { v = menu->get_item_metadata(p_which); emit_signal(SNAME("variant_changed")); } } break; case Variant::STRING: { if (hint == PROPERTY_HINT_ENUM) { v = hint_text.get_slice(",", p_which); emit_signal(SNAME("variant_changed")); } } break; case Variant::OBJECT: { switch (p_which) { case OBJ_MENU_LOAD: { file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE); String type = (hint == PROPERTY_HINT_RESOURCE_TYPE) ? hint_text : String(); List extensions; for (int i = 0; i < type.get_slice_count(","); i++) { ResourceLoader::get_recognized_extensions_for_type(type.get_slice(",", i), &extensions); } Set valid_extensions; for (const String &E : extensions) { valid_extensions.insert(E); } file->clear_filters(); for (Set::Element *E = valid_extensions.front(); E; E = E->next()) { file->add_filter("*." + E->get() + " ; " + E->get().to_upper()); } file->popup_file_dialog(); } break; case OBJ_MENU_EDIT: { Ref r = v; if (!r.is_null()) { emit_signal(SNAME("resource_edit_request")); hide(); } } break; case OBJ_MENU_CLEAR: { v = Variant(); emit_signal(SNAME("variant_changed")); hide(); } break; case OBJ_MENU_MAKE_UNIQUE: { Ref res_orig = v; if (res_orig.is_null()) { return; } List property_list; res_orig->get_property_list(&property_list); List> propvalues; for (const PropertyInfo &pi : property_list) { Pair p; if (pi.usage & PROPERTY_USAGE_STORAGE) { p.first = pi.name; p.second = res_orig->get(pi.name); } propvalues.push_back(p); } String orig_type = res_orig->get_class(); Object *inst = ClassDB::instantiate(orig_type); Ref res = Ref(Object::cast_to(inst)); ERR_FAIL_COND(res.is_null()); for (const Pair &p : propvalues) { res->set(p.first, p.second); } v = res; emit_signal(SNAME("variant_changed")); hide(); } break; case OBJ_MENU_COPY: { EditorSettings::get_singleton()->set_resource_clipboard(v); } break; case OBJ_MENU_PASTE: { v = EditorSettings::get_singleton()->get_resource_clipboard(); emit_signal(SNAME("variant_changed")); } break; case OBJ_MENU_NEW_SCRIPT: { if (Object::cast_to(owner)) { SceneTreeDock::get_singleton()->open_script_dialog(Object::cast_to(owner), false); } } break; case OBJ_MENU_EXTEND_SCRIPT: { if (Object::cast_to(owner)) { SceneTreeDock::get_singleton()->open_script_dialog(Object::cast_to(owner), true); } } break; case OBJ_MENU_SHOW_IN_FILE_SYSTEM: { Ref r = v; FileSystemDock *file_system_dock = FileSystemDock::get_singleton(); file_system_dock->navigate_to_path(r->get_path()); // Ensure that the FileSystem dock is visible. TabContainer *tab_container = (TabContainer *)file_system_dock->get_parent_control(); tab_container->set_current_tab(tab_container->get_tab_idx_from_control(file_system_dock)); } break; default: { if (p_which >= CONVERT_BASE_ID) { int to_type = p_which - CONVERT_BASE_ID; Vector> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin(Ref(v)); ERR_FAIL_INDEX(to_type, conversions.size()); Ref new_res = conversions[to_type]->convert(v); v = new_res; emit_signal(SNAME("variant_changed")); break; } ERR_FAIL_COND(inheritors_array.is_empty()); String intype = inheritors_array[p_which - TYPE_BASE_ID]; if (intype == "ViewportTexture") { scene_tree->set_title(TTR("Pick a Viewport")); scene_tree->popup_scenetree_dialog(); picking_viewport = true; return; } Variant obj = ClassDB::instantiate(intype); if (!obj) { if (ScriptServer::is_global_class(intype)) { obj = EditorNode::get_editor_data().script_class_instance(intype); } else { obj = EditorNode::get_editor_data().instance_custom_type(intype, "Resource"); } } ERR_BREAK(!obj); Resource *res = Object::cast_to(obj); ERR_BREAK(!res); if (owner && hint == PROPERTY_HINT_RESOURCE_TYPE && hint_text == "Script") { //make visual script the right type res->call("set_instance_base_type", owner->get_class()); } EditorNode::get_editor_data().instantiate_object_properties(obj); v = obj; emit_signal(SNAME("variant_changed")); } break; } } break; default: { } } } void CustomPropertyEditor::hide_menu() { menu->hide(); } Variant CustomPropertyEditor::get_variant() const { return v; } String CustomPropertyEditor::get_name() const { return name; } bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant::Type p_type, const Variant &p_variant, int p_hint, String p_hint_text) { owner = p_owner; updating = true; name = p_name; v = p_variant; field_names.clear(); hint = p_hint; hint_text = p_hint_text; type_button->hide(); if (color_picker) { color_picker->hide(); } texture_preview->hide(); inheritors_array.clear(); text_edit->hide(); easing_draw->hide(); spinbox->hide(); slider->hide(); menu->clear(); menu->reset_size(); for (int i = 0; i < MAX_VALUE_EDITORS; i++) { if (i < MAX_VALUE_EDITORS / 4) { value_hboxes[i]->hide(); } value_editor[i]->hide(); value_label[i]->hide(); if (i < 4) { scroll[i]->hide(); } } for (int i = 0; i < MAX_ACTION_BUTTONS; i++) { action_buttons[i]->hide(); } checks20gc->hide(); for (int i = 0; i < 20; i++) { checks20[i]->hide(); } type = (p_variant.get_type() != Variant::NIL && p_variant.get_type() != Variant::RID && p_type != Variant::OBJECT) ? p_variant.get_type() : p_type; switch (type) { case Variant::BOOL: { checks20gc->show(); CheckBox *c = checks20[0]; c->set_text("True"); checks20gc->set_position(Vector2(4, 4) * EDSCALE); c->set_pressed(v); c->show(); checks20gc->set_size(checks20gc->get_minimum_size()); set_size(checks20gc->get_position() + checks20gc->get_size() + c->get_size() + Vector2(4, 4) * EDSCALE); } break; case Variant::INT: case Variant::FLOAT: { if (hint == PROPERTY_HINT_RANGE) { int c = hint_text.get_slice_count(","); float min = 0, max = 100, step = type == Variant::FLOAT ? .01 : 1; if (c >= 1) { if (!hint_text.get_slice(",", 0).is_empty()) { min = hint_text.get_slice(",", 0).to_float(); } } if (c >= 2) { if (!hint_text.get_slice(",", 1).is_empty()) { max = hint_text.get_slice(",", 1).to_float(); } } if (c >= 3) { if (!hint_text.get_slice(",", 2).is_empty()) { step = hint_text.get_slice(",", 2).to_float(); } } if (c >= 4 && hint_text.get_slice(",", 3) == "slider") { slider->set_min(min); slider->set_max(max); slider->set_step(step); slider->set_value(v); slider->show(); set_size(Size2(110, 30) * EDSCALE); } else { spinbox->set_min(min); spinbox->set_max(max); spinbox->set_step(step); spinbox->set_value(v); spinbox->show(); set_size(Size2(70, 35) * EDSCALE); } } else if (hint == PROPERTY_HINT_ENUM) { Vector options = hint_text.split(","); int current_val = 0; for (int i = 0; i < options.size(); i++) { Vector text_split = options[i].split(":"); if (text_split.size() != 1) { current_val = text_split[1].to_int(); } menu->add_item(text_split[0]); menu->set_item_metadata(i, current_val); current_val += 1; } menu->set_position(get_position()); menu->popup(); hide(); updating = false; return false; } else if (hint == PROPERTY_HINT_LAYERS_2D_PHYSICS || hint == PROPERTY_HINT_LAYERS_2D_RENDER || hint == PROPERTY_HINT_LAYERS_2D_NAVIGATION || hint == PROPERTY_HINT_LAYERS_3D_PHYSICS || hint == PROPERTY_HINT_LAYERS_3D_RENDER || hint == PROPERTY_HINT_LAYERS_3D_NAVIGATION) { String basename; switch (hint) { case PROPERTY_HINT_LAYERS_2D_RENDER: basename = "layer_names/2d_render"; break; case PROPERTY_HINT_LAYERS_2D_PHYSICS: basename = "layer_names/2d_physics"; break; case PROPERTY_HINT_LAYERS_2D_NAVIGATION: basename = "layer_names/2d_navigation"; break; case PROPERTY_HINT_LAYERS_3D_RENDER: basename = "layer_names/3d_render"; break; case PROPERTY_HINT_LAYERS_3D_PHYSICS: basename = "layer_names/3d_physics"; break; case PROPERTY_HINT_LAYERS_3D_NAVIGATION: basename = "layer_names/3d_navigation"; break; } checks20gc->show(); uint32_t flgs = v; for (int i = 0; i < 2; i++) { Point2 ofs(4, 4); ofs.y += 22 * i; for (int j = 0; j < 10; j++) { int idx = i * 10 + j; CheckBox *c = checks20[idx]; c->set_text(ProjectSettings::get_singleton()->get(basename + "/layer_" + itos(idx + 1))); c->set_pressed(flgs & (1 << (i * 10 + j))); c->show(); } } show(); checks20gc->set_position(Vector2(4, 4) * EDSCALE); checks20gc->set_size(checks20gc->get_minimum_size()); set_size(Vector2(4, 4) * EDSCALE + checks20gc->get_position() + checks20gc->get_size()); } else if (hint == PROPERTY_HINT_EXP_EASING) { easing_draw->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 5 * EDSCALE); easing_draw->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -5 * EDSCALE); easing_draw->set_anchor_and_offset(SIDE_TOP, Control::ANCHOR_BEGIN, 5 * EDSCALE); easing_draw->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_END, -30 * EDSCALE); type_button->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 3 * EDSCALE); type_button->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -3 * EDSCALE); type_button->set_anchor_and_offset(SIDE_TOP, Control::ANCHOR_END, -25 * EDSCALE); type_button->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_END, -7 * EDSCALE); type_button->set_text(TTR("Preset...")); type_button->get_popup()->clear(); type_button->get_popup()->add_item(TTR("Linear"), EASING_LINEAR); type_button->get_popup()->add_item(TTR("Ease In"), EASING_EASE_IN); type_button->get_popup()->add_item(TTR("Ease Out"), EASING_EASE_OUT); if (hint_text != "attenuation") { type_button->get_popup()->add_item(TTR("Zero"), EASING_ZERO); type_button->get_popup()->add_item(TTR("Easing In-Out"), EASING_IN_OUT); type_button->get_popup()->add_item(TTR("Easing Out-In"), EASING_OUT_IN); } type_button->show(); easing_draw->show(); set_size(Size2(200, 150) * EDSCALE); } else if (hint == PROPERTY_HINT_FLAGS) { Vector flags = hint_text.split(","); for (int i = 0; i < flags.size(); i++) { String flag = flags[i]; if (flag.is_empty()) { continue; } menu->add_check_item(flag, i); int f = v; if (f & (1 << i)) { menu->set_item_checked(menu->get_item_index(i), true); } } menu->set_position(get_position()); menu->popup(); hide(); updating = false; return false; } else { List names; names.push_back("value:"); config_value_editors(1, 1, 50, names); value_editor[0]->set_text(TS->format_number(String::num(v))); } } break; case Variant::STRING: { if (hint == PROPERTY_HINT_LOCALE_ID) { List names; names.push_back(TTR("Locale...")); names.push_back(TTR("Clear")); config_action_buttons(names); } else if (hint == PROPERTY_HINT_FILE || hint == PROPERTY_HINT_GLOBAL_FILE) { List names; names.push_back(TTR("File...")); names.push_back(TTR("Clear")); config_action_buttons(names); } else if (hint == PROPERTY_HINT_DIR || hint == PROPERTY_HINT_GLOBAL_DIR) { List names; names.push_back(TTR("Dir...")); names.push_back(TTR("Clear")); config_action_buttons(names); } else if (hint == PROPERTY_HINT_ENUM) { Vector options = hint_text.split(","); for (int i = 0; i < options.size(); i++) { menu->add_item(options[i], i); } menu->set_position(get_position()); menu->popup(); hide(); updating = false; return false; } else if (hint == PROPERTY_HINT_MULTILINE_TEXT) { text_edit->show(); text_edit->set_text(v); text_edit->deselect(); int button_margin = text_edit->get_theme_constant(SNAME("button_margin"), SNAME("Dialogs")); int margin = text_edit->get_theme_constant(SNAME("margin"), SNAME("Dialogs")); action_buttons[0]->set_anchor(SIDE_LEFT, Control::ANCHOR_END); action_buttons[0]->set_anchor(SIDE_TOP, Control::ANCHOR_END); action_buttons[0]->set_anchor(SIDE_RIGHT, Control::ANCHOR_END); action_buttons[0]->set_anchor(SIDE_BOTTOM, Control::ANCHOR_END); action_buttons[0]->set_begin(Point2(-70 * EDSCALE, -button_margin + 5 * EDSCALE)); action_buttons[0]->set_end(Point2(-margin, -margin)); action_buttons[0]->set_text(TTR("Close")); action_buttons[0]->show(); } else if (hint == PROPERTY_HINT_TYPE_STRING) { if (!create_dialog) { create_dialog = memnew(CreateDialog); create_dialog->connect("create", callable_mp(this, &CustomPropertyEditor::_create_dialog_callback)); add_child(create_dialog); } if (!hint_text.is_empty()) { create_dialog->set_base_type(hint_text); } else { create_dialog->set_base_type("Object"); } create_dialog->popup_create(false); hide(); updating = false; return false; } else if (hint == PROPERTY_HINT_METHOD_OF_VARIANT_TYPE) { #define MAKE_PROPSELECT \ if (!property_select) { \ property_select = memnew(PropertySelector); \ property_select->connect("selected", callable_mp(this, &CustomPropertyEditor::_create_selected_property)); \ add_child(property_select); \ } \ hide(); MAKE_PROPSELECT; Variant::Type type = Variant::NIL; for (int i = 0; i < Variant::VARIANT_MAX; i++) { if (hint_text == Variant::get_type_name(Variant::Type(i))) { type = Variant::Type(i); } } if (type != Variant::NIL) { property_select->select_method_from_basic_type(type, v); } updating = false; return false; } else if (hint == PROPERTY_HINT_METHOD_OF_BASE_TYPE) { MAKE_PROPSELECT property_select->select_method_from_base_type(hint_text, v); updating = false; return false; } else if (hint == PROPERTY_HINT_METHOD_OF_INSTANCE) { MAKE_PROPSELECT Object *instance = ObjectDB::get_instance(ObjectID(hint_text.to_int())); if (instance) { property_select->select_method_from_instance(instance, v); } updating = false; return false; } else if (hint == PROPERTY_HINT_METHOD_OF_SCRIPT) { MAKE_PROPSELECT Object *obj = ObjectDB::get_instance(ObjectID(hint_text.to_int())); if (Object::cast_to