Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/inspector/add_metadata_dialog.cpp
9896 views
1
/**************************************************************************/
2
/* add_metadata_dialog.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "add_metadata_dialog.h"
32
33
#include "editor/gui/editor_validation_panel.h"
34
#include "editor/gui/editor_variant_type_selectors.h"
35
#include "editor/themes/editor_scale.h"
36
#include "scene/gui/line_edit.h"
37
38
AddMetadataDialog::AddMetadataDialog() {
39
VBoxContainer *vbc = memnew(VBoxContainer);
40
add_child(vbc);
41
42
HBoxContainer *hbc = memnew(HBoxContainer);
43
vbc->add_child(hbc);
44
hbc->add_child(memnew(Label(TTR("Name:"))));
45
46
add_meta_name = memnew(LineEdit);
47
add_meta_name->set_accessibility_name(TTRC("Name:"));
48
add_meta_name->set_custom_minimum_size(Size2(200 * EDSCALE, 1));
49
hbc->add_child(add_meta_name);
50
hbc->add_child(memnew(Label(TTR("Type:"))));
51
52
add_meta_type = memnew(EditorVariantTypeOptionButton);
53
add_meta_type->set_accessibility_name(TTRC("Type:"));
54
55
hbc->add_child(add_meta_type);
56
57
Control *spacing = memnew(Control);
58
vbc->add_child(spacing);
59
spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
60
61
set_ok_button_text(TTR("Add"));
62
register_text_enter(add_meta_name);
63
64
validation_panel = memnew(EditorValidationPanel);
65
vbc->add_child(validation_panel);
66
validation_panel->add_line(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Metadata name is valid."));
67
validation_panel->set_update_callback(callable_mp(this, &AddMetadataDialog::_check_meta_name));
68
validation_panel->set_accept_button(get_ok_button());
69
70
add_meta_name->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
71
}
72
73
void AddMetadataDialog::_complete_init(const StringName &p_title) {
74
add_meta_name->set_text("");
75
validation_panel->update();
76
77
set_title(vformat(TTR("Add Metadata Property for \"%s\""), p_title));
78
79
if (add_meta_type->get_item_count() == 0) {
80
add_meta_type->populate({ Variant::NIL }, { { Variant::OBJECT, "Resource" } });
81
}
82
}
83
84
void AddMetadataDialog::open(const StringName p_title, List<StringName> &p_existing_metas) {
85
this->_existing_metas = p_existing_metas;
86
_complete_init(p_title);
87
popup_centered();
88
add_meta_name->grab_focus();
89
}
90
91
StringName AddMetadataDialog::get_meta_name() {
92
return add_meta_name->get_text();
93
}
94
95
Variant AddMetadataDialog::get_meta_defval() {
96
Variant defval;
97
Callable::CallError ce;
98
Variant::construct(add_meta_type->get_selected_type(), defval, nullptr, 0, ce);
99
return defval;
100
}
101
102
void AddMetadataDialog::_check_meta_name() {
103
const String meta_name = add_meta_name->get_text();
104
105
if (meta_name.is_empty()) {
106
validation_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()) {
108
validation_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)) {
110
validation_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] == '_') {
112
validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Names starting with _ are reserved for editor-only metadata."), EditorValidationPanel::MSG_ERROR);
113
}
114
}
115
116