editor_main_screen.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**************************************************************************/
  2. /* editor_main_screen.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_main_screen.h"
  31. #include "core/io/config_file.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/plugins/editor_plugin.h"
  36. #include "scene/gui/box_container.h"
  37. #include "scene/gui/button.h"
  38. void EditorMainScreen::_notification(int p_what) {
  39. switch (p_what) {
  40. case NOTIFICATION_READY: {
  41. if (EDITOR_3D < buttons.size() && buttons[EDITOR_3D]->is_visible()) {
  42. // If the 3D editor is enabled, use this as the default.
  43. select(EDITOR_3D);
  44. return;
  45. }
  46. // Switch to the first main screen plugin that is enabled. Usually this is
  47. // 2D, but may be subsequent ones if 2D is disabled in the feature profile.
  48. for (int i = 0; i < buttons.size(); i++) {
  49. Button *editor_button = buttons[i];
  50. if (editor_button->is_visible()) {
  51. select(i);
  52. return;
  53. }
  54. }
  55. select(-1);
  56. } break;
  57. case NOTIFICATION_THEME_CHANGED: {
  58. for (int i = 0; i < buttons.size(); i++) {
  59. Button *tb = buttons[i];
  60. EditorPlugin *p_editor = editor_table[i];
  61. Ref<Texture2D> icon = p_editor->get_icon();
  62. if (icon.is_valid()) {
  63. tb->set_button_icon(icon);
  64. } else if (has_theme_icon(p_editor->get_name(), EditorStringName(EditorIcons))) {
  65. tb->set_button_icon(get_theme_icon(p_editor->get_name(), EditorStringName(EditorIcons)));
  66. }
  67. }
  68. } break;
  69. }
  70. }
  71. void EditorMainScreen::set_button_container(HBoxContainer *p_button_hb) {
  72. button_hb = p_button_hb;
  73. }
  74. void EditorMainScreen::save_layout_to_config(Ref<ConfigFile> p_config_file, const String &p_section) const {
  75. int selected_main_editor_idx = -1;
  76. for (int i = 0; i < buttons.size(); i++) {
  77. if (buttons[i]->is_pressed()) {
  78. selected_main_editor_idx = i;
  79. break;
  80. }
  81. }
  82. if (selected_main_editor_idx != -1) {
  83. p_config_file->set_value(p_section, "selected_main_editor_idx", selected_main_editor_idx);
  84. } else {
  85. p_config_file->set_value(p_section, "selected_main_editor_idx", Variant());
  86. }
  87. }
  88. void EditorMainScreen::load_layout_from_config(Ref<ConfigFile> p_config_file, const String &p_section) {
  89. int selected_main_editor_idx = p_config_file->get_value(p_section, "selected_main_editor_idx", -1);
  90. if (selected_main_editor_idx >= 0 && selected_main_editor_idx < buttons.size()) {
  91. callable_mp(this, &EditorMainScreen::select).call_deferred(selected_main_editor_idx);
  92. }
  93. }
  94. void EditorMainScreen::set_button_enabled(int p_index, bool p_enabled) {
  95. ERR_FAIL_INDEX(p_index, buttons.size());
  96. buttons[p_index]->set_visible(p_enabled);
  97. if (!p_enabled && buttons[p_index]->is_pressed()) {
  98. select(EDITOR_2D);
  99. }
  100. }
  101. bool EditorMainScreen::is_button_enabled(int p_index) const {
  102. ERR_FAIL_INDEX_V(p_index, buttons.size(), false);
  103. return buttons[p_index]->is_visible();
  104. }
  105. int EditorMainScreen::_get_current_main_editor() const {
  106. for (int i = 0; i < editor_table.size(); i++) {
  107. if (editor_table[i] == selected_plugin) {
  108. return i;
  109. }
  110. }
  111. return 0;
  112. }
  113. void EditorMainScreen::select_next() {
  114. int editor = _get_current_main_editor();
  115. do {
  116. if (editor == editor_table.size() - 1) {
  117. editor = 0;
  118. } else {
  119. editor++;
  120. }
  121. } while (!buttons[editor]->is_visible());
  122. select(editor);
  123. }
  124. void EditorMainScreen::select_prev() {
  125. int editor = _get_current_main_editor();
  126. do {
  127. if (editor == 0) {
  128. editor = editor_table.size() - 1;
  129. } else {
  130. editor--;
  131. }
  132. } while (!buttons[editor]->is_visible());
  133. select(editor);
  134. }
  135. void EditorMainScreen::select_by_name(const String &p_name) {
  136. ERR_FAIL_COND(p_name.is_empty());
  137. for (int i = 0; i < buttons.size(); i++) {
  138. if (buttons[i]->get_text() == p_name) {
  139. select(i);
  140. return;
  141. }
  142. }
  143. ERR_FAIL_MSG("The editor name '" + p_name + "' was not found.");
  144. }
  145. void EditorMainScreen::select(int p_index) {
  146. if (EditorNode::get_singleton()->is_changing_scene()) {
  147. return;
  148. }
  149. ERR_FAIL_INDEX(p_index, editor_table.size());
  150. if (!buttons[p_index]->is_visible()) { // Button hidden, no editor.
  151. return;
  152. }
  153. for (int i = 0; i < buttons.size(); i++) {
  154. buttons[i]->set_pressed_no_signal(i == p_index);
  155. }
  156. EditorPlugin *new_editor = editor_table[p_index];
  157. ERR_FAIL_NULL(new_editor);
  158. if (selected_plugin == new_editor) {
  159. return;
  160. }
  161. if (selected_plugin) {
  162. selected_plugin->make_visible(false);
  163. }
  164. selected_plugin = new_editor;
  165. selected_plugin->make_visible(true);
  166. selected_plugin->selected_notify();
  167. EditorData &editor_data = EditorNode::get_editor_data();
  168. int plugin_count = editor_data.get_editor_plugin_count();
  169. for (int i = 0; i < plugin_count; i++) {
  170. editor_data.get_editor_plugin(i)->notify_main_screen_changed(selected_plugin->get_name());
  171. }
  172. EditorNode::get_singleton()->update_distraction_free_mode();
  173. }
  174. int EditorMainScreen::get_selected_index() const {
  175. for (int i = 0; i < editor_table.size(); i++) {
  176. if (selected_plugin == editor_table[i]) {
  177. return i;
  178. }
  179. }
  180. return -1;
  181. }
  182. int EditorMainScreen::get_plugin_index(EditorPlugin *p_editor) const {
  183. int screen = -1;
  184. for (int i = 0; i < editor_table.size(); i++) {
  185. if (p_editor == editor_table[i]) {
  186. screen = i;
  187. break;
  188. }
  189. }
  190. return screen;
  191. }
  192. EditorPlugin *EditorMainScreen::get_selected_plugin() const {
  193. return selected_plugin;
  194. }
  195. VBoxContainer *EditorMainScreen::get_control() const {
  196. return main_screen_vbox;
  197. }
  198. void EditorMainScreen::add_main_plugin(EditorPlugin *p_editor) {
  199. Button *tb = memnew(Button);
  200. tb->set_toggle_mode(true);
  201. tb->set_theme_type_variation("MainScreenButton");
  202. tb->set_name(p_editor->get_name());
  203. tb->set_text(p_editor->get_name());
  204. Ref<Texture2D> icon = p_editor->get_icon();
  205. if (icon.is_null() && has_theme_icon(p_editor->get_name(), EditorStringName(EditorIcons))) {
  206. icon = get_editor_theme_icon(p_editor->get_name());
  207. }
  208. if (icon.is_valid()) {
  209. tb->set_button_icon(icon);
  210. // Make sure the control is updated if the icon is reimported.
  211. icon->connect_changed(callable_mp((Control *)tb, &Control::update_minimum_size));
  212. }
  213. tb->connect(SceneStringName(pressed), callable_mp(this, &EditorMainScreen::select).bind(buttons.size()));
  214. buttons.push_back(tb);
  215. button_hb->add_child(tb);
  216. editor_table.push_back(p_editor);
  217. }
  218. void EditorMainScreen::remove_main_plugin(EditorPlugin *p_editor) {
  219. // Remove the main editor button and update the bindings of
  220. // all buttons behind it to point to the correct main window.
  221. for (int i = buttons.size() - 1; i >= 0; i--) {
  222. if (p_editor->get_name() == buttons[i]->get_text()) {
  223. if (buttons[i]->is_pressed()) {
  224. select(EDITOR_SCRIPT);
  225. }
  226. memdelete(buttons[i]);
  227. buttons.remove_at(i);
  228. break;
  229. } else {
  230. buttons[i]->disconnect(SceneStringName(pressed), callable_mp(this, &EditorMainScreen::select));
  231. buttons[i]->connect(SceneStringName(pressed), callable_mp(this, &EditorMainScreen::select).bind(i - 1));
  232. }
  233. }
  234. if (selected_plugin == p_editor) {
  235. selected_plugin = nullptr;
  236. }
  237. editor_table.erase(p_editor);
  238. }
  239. EditorMainScreen::EditorMainScreen() {
  240. main_screen_vbox = memnew(VBoxContainer);
  241. main_screen_vbox->set_name("MainScreen");
  242. main_screen_vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  243. main_screen_vbox->add_theme_constant_override("separation", 0);
  244. add_child(main_screen_vbox);
  245. }