editor_layouts_dialog.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*************************************************************************/
  2. /* editor_layouts_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "editor_layouts_dialog.h"
  31. #include "core/io/config_file.h"
  32. #include "core/object/class_db.h"
  33. #include "core/os/keyboard.h"
  34. #include "editor/editor_settings.h"
  35. #include "scene/gui/item_list.h"
  36. #include "scene/gui/line_edit.h"
  37. void EditorLayoutsDialog::_line_gui_input(const Ref<InputEvent> &p_event) {
  38. Ref<InputEventKey> k = p_event;
  39. if (k.is_valid()) {
  40. if (!k->is_pressed()) {
  41. return;
  42. }
  43. switch (k->get_keycode()) {
  44. case KEY_KP_ENTER:
  45. case KEY_ENTER: {
  46. if (get_hide_on_ok()) {
  47. hide();
  48. }
  49. ok_pressed();
  50. set_input_as_handled();
  51. } break;
  52. case KEY_ESCAPE: {
  53. hide();
  54. set_input_as_handled();
  55. } break;
  56. default:
  57. break;
  58. }
  59. }
  60. }
  61. void EditorLayoutsDialog::_bind_methods() {
  62. ADD_SIGNAL(MethodInfo("name_confirmed", PropertyInfo(Variant::STRING, "name")));
  63. }
  64. void EditorLayoutsDialog::ok_pressed() {
  65. if (layout_names->is_anything_selected()) {
  66. Vector<int> const selected_items = layout_names->get_selected_items();
  67. for (int i = 0; i < selected_items.size(); ++i) {
  68. emit_signal(SNAME("name_confirmed"), layout_names->get_item_text(selected_items[i]));
  69. }
  70. } else if (name->is_visible() && name->get_text() != "") {
  71. emit_signal(SNAME("name_confirmed"), name->get_text());
  72. }
  73. }
  74. void EditorLayoutsDialog::_post_popup() {
  75. ConfirmationDialog::_post_popup();
  76. name->clear();
  77. layout_names->clear();
  78. Ref<ConfigFile> config;
  79. config.instantiate();
  80. Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config());
  81. if (err != OK) {
  82. return;
  83. }
  84. List<String> layouts;
  85. config.ptr()->get_sections(&layouts);
  86. for (const String &E : layouts) {
  87. layout_names->add_item(E);
  88. }
  89. }
  90. EditorLayoutsDialog::EditorLayoutsDialog() {
  91. makevb = memnew(VBoxContainer);
  92. add_child(makevb);
  93. makevb->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 5);
  94. makevb->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -5);
  95. layout_names = memnew(ItemList);
  96. makevb->add_child(layout_names);
  97. layout_names->set_visible(true);
  98. layout_names->set_offset(SIDE_TOP, 5);
  99. layout_names->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 5);
  100. layout_names->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -5);
  101. layout_names->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  102. layout_names->set_select_mode(ItemList::SELECT_MULTI);
  103. layout_names->set_allow_rmb_select(true);
  104. name = memnew(LineEdit);
  105. makevb->add_child(name);
  106. name->set_offset(SIDE_TOP, 5);
  107. name->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 5);
  108. name->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -5);
  109. name->connect("gui_input", callable_mp(this, &EditorLayoutsDialog::_line_gui_input));
  110. name->connect("focus_entered", callable_mp(layout_names, &ItemList::deselect_all));
  111. }
  112. void EditorLayoutsDialog::set_name_line_enabled(bool p_enabled) {
  113. name->set_visible(p_enabled);
  114. }