editor_layouts_dialog.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**************************************************************************/
  2. /* editor_layouts_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 "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_scale.h"
  35. #include "editor/editor_settings.h"
  36. #include "scene/gui/item_list.h"
  37. #include "scene/gui/line_edit.h"
  38. void EditorLayoutsDialog::_line_gui_input(const Ref<InputEvent> &p_event) {
  39. Ref<InputEventKey> k = p_event;
  40. if (k.is_valid()) {
  41. if (k->is_action_pressed(SNAME("ui_text_submit"), false, true)) {
  42. if (get_hide_on_ok()) {
  43. hide();
  44. }
  45. ok_pressed();
  46. set_input_as_handled();
  47. } else if (k->is_action_pressed(SNAME("ui_cancel"), false, true)) {
  48. hide();
  49. set_input_as_handled();
  50. }
  51. }
  52. }
  53. void EditorLayoutsDialog::_update_ok_disable_state() {
  54. if (layout_names->is_anything_selected()) {
  55. get_ok_button()->set_disabled(false);
  56. } else {
  57. get_ok_button()->set_disabled(!name->is_visible() || name->get_text().is_empty());
  58. }
  59. }
  60. void EditorLayoutsDialog::_deselect_layout_names() {
  61. // The deselect method does not emit any signal, therefore we need update the disable state as well.
  62. layout_names->deselect_all();
  63. _update_ok_disable_state();
  64. }
  65. void EditorLayoutsDialog::_bind_methods() {
  66. ADD_SIGNAL(MethodInfo("name_confirmed", PropertyInfo(Variant::STRING, "name")));
  67. }
  68. void EditorLayoutsDialog::ok_pressed() {
  69. if (layout_names->is_anything_selected()) {
  70. Vector<int> const selected_items = layout_names->get_selected_items();
  71. for (int i = 0; i < selected_items.size(); ++i) {
  72. emit_signal(SNAME("name_confirmed"), layout_names->get_item_text(selected_items[i]));
  73. }
  74. } else if (name->is_visible() && !name->get_text().is_empty()) {
  75. emit_signal(SNAME("name_confirmed"), name->get_text());
  76. }
  77. }
  78. void EditorLayoutsDialog::_post_popup() {
  79. ConfirmationDialog::_post_popup();
  80. layout_names->clear();
  81. name->clear();
  82. Ref<ConfigFile> config;
  83. config.instantiate();
  84. Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config());
  85. if (err != OK) {
  86. return;
  87. }
  88. List<String> layouts;
  89. config.ptr()->get_sections(&layouts);
  90. for (const String &E : layouts) {
  91. layout_names->add_item(E);
  92. }
  93. if (name->is_visible()) {
  94. name->grab_focus();
  95. } else {
  96. layout_names->grab_focus();
  97. }
  98. }
  99. EditorLayoutsDialog::EditorLayoutsDialog() {
  100. makevb = memnew(VBoxContainer);
  101. add_child(makevb);
  102. layout_names = memnew(ItemList);
  103. layout_names->set_auto_height(true);
  104. layout_names->set_custom_minimum_size(Size2(300 * EDSCALE, 50 * EDSCALE));
  105. layout_names->set_visible(true);
  106. layout_names->set_offset(SIDE_TOP, 5);
  107. layout_names->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  108. layout_names->set_select_mode(ItemList::SELECT_MULTI);
  109. layout_names->set_allow_rmb_select(true);
  110. layout_names->connect("multi_selected", callable_mp(this, &EditorLayoutsDialog::_update_ok_disable_state).unbind(2));
  111. MarginContainer *mc = makevb->add_margin_child(TTR("Select existing layout:"), layout_names);
  112. mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  113. name = memnew(LineEdit);
  114. makevb->add_child(name);
  115. name->set_placeholder(TTR("Or enter new layout name"));
  116. name->set_offset(SIDE_TOP, 5);
  117. name->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 5);
  118. name->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -5);
  119. name->connect("gui_input", callable_mp(this, &EditorLayoutsDialog::_line_gui_input));
  120. name->connect("focus_entered", callable_mp(this, &EditorLayoutsDialog::_deselect_layout_names));
  121. name->connect("text_changed", callable_mp(this, &EditorLayoutsDialog::_update_ok_disable_state).unbind(1));
  122. }
  123. void EditorLayoutsDialog::set_name_line_enabled(bool p_enabled) {
  124. name->set_visible(p_enabled);
  125. }