add_metadata_dialog.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include "add_metadata_dialog.h"
  31. #include "editor/gui/editor_validation_panel.h"
  32. #include "editor/themes/editor_scale.h"
  33. #include "scene/gui/line_edit.h"
  34. #include "scene/gui/option_button.h"
  35. AddMetadataDialog::AddMetadataDialog() {
  36. VBoxContainer *vbc = memnew(VBoxContainer);
  37. add_child(vbc);
  38. HBoxContainer *hbc = memnew(HBoxContainer);
  39. vbc->add_child(hbc);
  40. hbc->add_child(memnew(Label(TTR("Name:"))));
  41. add_meta_name = memnew(LineEdit);
  42. add_meta_name->set_accessibility_name(TTRC("Name:"));
  43. add_meta_name->set_custom_minimum_size(Size2(200 * EDSCALE, 1));
  44. hbc->add_child(add_meta_name);
  45. hbc->add_child(memnew(Label(TTR("Type:"))));
  46. add_meta_type = memnew(OptionButton);
  47. add_meta_type->set_accessibility_name(TTRC("Type:"));
  48. hbc->add_child(add_meta_type);
  49. Control *spacing = memnew(Control);
  50. vbc->add_child(spacing);
  51. spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
  52. set_ok_button_text(TTR("Add"));
  53. register_text_enter(add_meta_name);
  54. validation_panel = memnew(EditorValidationPanel);
  55. vbc->add_child(validation_panel);
  56. validation_panel->add_line(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Metadata name is valid."));
  57. validation_panel->set_update_callback(callable_mp(this, &AddMetadataDialog::_check_meta_name));
  58. validation_panel->set_accept_button(get_ok_button());
  59. add_meta_name->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
  60. }
  61. void AddMetadataDialog::_complete_init(const StringName &p_title) {
  62. add_meta_name->set_text("");
  63. validation_panel->update();
  64. set_title(vformat(TTR("Add Metadata Property for \"%s\""), p_title));
  65. // Skip if we already completed the initialization.
  66. if (add_meta_type->get_item_count()) {
  67. return;
  68. }
  69. // Theme icons can be retrieved only the Window has been initialized.
  70. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  71. if (i == Variant::NIL || i == Variant::RID || i == Variant::CALLABLE || i == Variant::SIGNAL) {
  72. continue; //not editable by inspector.
  73. }
  74. String type = i == Variant::OBJECT ? String("Resource") : Variant::get_type_name(Variant::Type(i));
  75. add_meta_type->add_icon_item(get_editor_theme_icon(type), type, i);
  76. }
  77. }
  78. void AddMetadataDialog::open(const StringName p_title, List<StringName> &p_existing_metas) {
  79. this->_existing_metas = p_existing_metas;
  80. _complete_init(p_title);
  81. popup_centered();
  82. add_meta_name->grab_focus();
  83. }
  84. StringName AddMetadataDialog::get_meta_name() {
  85. return add_meta_name->get_text();
  86. }
  87. Variant AddMetadataDialog::get_meta_defval() {
  88. Variant defval;
  89. Callable::CallError ce;
  90. Variant::construct(Variant::Type(add_meta_type->get_selected_id()), defval, nullptr, 0, ce);
  91. return defval;
  92. }
  93. void AddMetadataDialog::_check_meta_name() {
  94. const String meta_name = add_meta_name->get_text();
  95. if (meta_name.is_empty()) {
  96. validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Metadata name can't be empty."), EditorValidationPanel::MSG_ERROR);
  97. } else if (!meta_name.is_valid_ascii_identifier()) {
  98. validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Metadata name must be a valid identifier."), EditorValidationPanel::MSG_ERROR);
  99. } else if (_existing_metas.find(meta_name)) {
  100. validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, vformat(TTR("Metadata with name \"%s\" already exists."), meta_name), EditorValidationPanel::MSG_ERROR);
  101. } else if (meta_name[0] == '_') {
  102. validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Names starting with _ are reserved for editor-only metadata."), EditorValidationPanel::MSG_ERROR);
  103. }
  104. }