Path: blob/master/editor/inspector/add_metadata_dialog.cpp
9896 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));48hbc->add_child(add_meta_name);49hbc->add_child(memnew(Label(TTR("Type:"))));5051add_meta_type = memnew(EditorVariantTypeOptionButton);52add_meta_type->set_accessibility_name(TTRC("Type:"));5354hbc->add_child(add_meta_type);5556Control *spacing = memnew(Control);57vbc->add_child(spacing);58spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));5960set_ok_button_text(TTR("Add"));61register_text_enter(add_meta_name);6263validation_panel = memnew(EditorValidationPanel);64vbc->add_child(validation_panel);65validation_panel->add_line(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Metadata name is valid."));66validation_panel->set_update_callback(callable_mp(this, &AddMetadataDialog::_check_meta_name));67validation_panel->set_accept_button(get_ok_button());6869add_meta_name->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));70}7172void AddMetadataDialog::_complete_init(const StringName &p_title) {73add_meta_name->set_text("");74validation_panel->update();7576set_title(vformat(TTR("Add Metadata Property for \"%s\""), p_title));7778if (add_meta_type->get_item_count() == 0) {79add_meta_type->populate({ Variant::NIL }, { { Variant::OBJECT, "Resource" } });80}81}8283void AddMetadataDialog::open(const StringName p_title, List<StringName> &p_existing_metas) {84this->_existing_metas = p_existing_metas;85_complete_init(p_title);86popup_centered();87add_meta_name->grab_focus();88}8990StringName AddMetadataDialog::get_meta_name() {91return add_meta_name->get_text();92}9394Variant AddMetadataDialog::get_meta_defval() {95Variant defval;96Callable::CallError ce;97Variant::construct(add_meta_type->get_selected_type(), defval, nullptr, 0, ce);98return defval;99}100101void AddMetadataDialog::_check_meta_name() {102const String meta_name = add_meta_name->get_text();103104if (meta_name.is_empty()) {105validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Metadata name can't be empty."), EditorValidationPanel::MSG_ERROR);106} else if (!meta_name.is_valid_ascii_identifier()) {107validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Metadata name must be a valid identifier."), EditorValidationPanel::MSG_ERROR);108} else if (_existing_metas.find(meta_name)) {109validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, vformat(TTR("Metadata with name \"%s\" already exists."), meta_name), EditorValidationPanel::MSG_ERROR);110} else if (meta_name[0] == '_') {111validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Names starting with _ are reserved for editor-only metadata."), EditorValidationPanel::MSG_ERROR);112}113}114115116