Path: blob/master/editor/inspector/add_metadata_dialog.cpp
21317 views
/**************************************************************************/1/* add_metadata_dialog.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "add_metadata_dialog.h"3132#include "editor/gui/editor_validation_panel.h"33#include "editor/gui/editor_variant_type_selectors.h"34#include "editor/themes/editor_scale.h"35#include "scene/gui/line_edit.h"3637AddMetadataDialog::AddMetadataDialog() {38VBoxContainer *vbc = memnew(VBoxContainer);39add_child(vbc);4041HBoxContainer *hbc = memnew(HBoxContainer);42vbc->add_child(hbc);43hbc->add_child(memnew(Label(TTR("Name:"))));4445add_meta_name = memnew(LineEdit);46add_meta_name->set_accessibility_name(TTRC("Name:"));47add_meta_name->set_custom_minimum_size(Size2(200 * EDSCALE, 1));48add_meta_name->set_h_size_flags(Control::SIZE_EXPAND_FILL);49hbc->add_child(add_meta_name);50hbc->add_child(memnew(Label(TTR("Type:"))));5152add_meta_type = memnew(EditorVariantTypeOptionButton);53add_meta_type->set_accessibility_name(TTRC("Type:"));5455hbc->add_child(add_meta_type);5657Control *spacing = memnew(Control);58vbc->add_child(spacing);59spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));6061set_ok_button_text(TTR("Add"));62register_text_enter(add_meta_name);6364validation_panel = memnew(EditorValidationPanel);65vbc->add_child(validation_panel);66validation_panel->add_line(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Metadata name is valid."));67validation_panel->set_update_callback(callable_mp(this, &AddMetadataDialog::_check_meta_name));68validation_panel->set_accept_button(get_ok_button());6970add_meta_name->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));71}7273void AddMetadataDialog::_complete_init(const StringName &p_title) {74add_meta_name->set_text("");75validation_panel->update();7677set_title(vformat(TTR("Add Metadata Property for \"%s\""), p_title));7879if (add_meta_type->get_item_count() == 0) {80add_meta_type->populate({ Variant::NIL }, { { Variant::OBJECT, "Resource" } });81}82}8384void AddMetadataDialog::open(const StringName p_title, List<StringName> &p_existing_metas) {85this->_existing_metas = p_existing_metas;86_complete_init(p_title);87popup_centered();88add_meta_name->grab_focus();89}9091StringName AddMetadataDialog::get_meta_name() {92return add_meta_name->get_text();93}9495Variant AddMetadataDialog::get_meta_defval() {96Variant defval;97Callable::CallError ce;98Variant::construct(add_meta_type->get_selected_type(), defval, nullptr, 0, ce);99return defval;100}101102void AddMetadataDialog::_check_meta_name() {103const String meta_name = add_meta_name->get_text();104105if (meta_name.is_empty()) {106validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Metadata name can't be empty."), EditorValidationPanel::MSG_ERROR);107} else if (!meta_name.is_valid_ascii_identifier()) {108validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Metadata name must be a valid identifier."), EditorValidationPanel::MSG_ERROR);109} else if (_existing_metas.find(meta_name)) {110validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, vformat(TTR("Metadata with name \"%s\" already exists."), meta_name), EditorValidationPanel::MSG_ERROR);111} else if (meta_name[0] == '_') {112validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Names starting with _ are reserved for editor-only metadata."), EditorValidationPanel::MSG_ERROR);113}114}115116117