editor_scene_tabs.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /**************************************************************************/
  2. /* editor_scene_tabs.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_scene_tabs.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_resource_preview.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/editor_string_names.h"
  36. #include "editor/editor_undo_redo_manager.h"
  37. #include "editor/inspector_dock.h"
  38. #include "scene/gui/box_container.h"
  39. #include "scene/gui/button.h"
  40. #include "scene/gui/panel.h"
  41. #include "scene/gui/panel_container.h"
  42. #include "scene/gui/popup_menu.h"
  43. #include "scene/gui/tab_bar.h"
  44. #include "scene/gui/texture_rect.h"
  45. EditorSceneTabs *EditorSceneTabs::singleton = nullptr;
  46. void EditorSceneTabs::_notification(int p_what) {
  47. switch (p_what) {
  48. case NOTIFICATION_THEME_CHANGED: {
  49. tabbar_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("tabbar_background"), SNAME("TabContainer")));
  50. scene_tabs->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
  51. scene_tab_add->set_icon(get_editor_theme_icon(SNAME("Add")));
  52. scene_tab_add->add_theme_color_override("icon_normal_color", Color(0.6f, 0.6f, 0.6f, 0.8f));
  53. scene_tab_add_ph->set_custom_minimum_size(scene_tab_add->get_minimum_size());
  54. } break;
  55. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  56. scene_tabs->set_tab_close_display_policy((TabBar::CloseButtonDisplayPolicy)EDITOR_GET("interface/scene_tabs/display_close_button").operator int());
  57. scene_tabs->set_max_tab_width(int(EDITOR_GET("interface/scene_tabs/maximum_width")) * EDSCALE);
  58. } break;
  59. }
  60. }
  61. void EditorSceneTabs::_scene_tab_changed(int p_tab) {
  62. tab_preview_panel->hide();
  63. emit_signal("tab_changed", p_tab);
  64. }
  65. void EditorSceneTabs::_scene_tab_script_edited(int p_tab) {
  66. Ref<Script> scr = EditorNode::get_editor_data().get_scene_root_script(p_tab);
  67. if (scr.is_valid()) {
  68. InspectorDock::get_singleton()->edit_resource(scr);
  69. }
  70. }
  71. void EditorSceneTabs::_scene_tab_closed(int p_tab) {
  72. emit_signal("tab_closed", p_tab);
  73. }
  74. void EditorSceneTabs::_scene_tab_hovered(int p_tab) {
  75. if (!bool(EDITOR_GET("interface/scene_tabs/show_thumbnail_on_hover"))) {
  76. return;
  77. }
  78. int current_tab = scene_tabs->get_current_tab();
  79. if (p_tab == current_tab || p_tab < 0) {
  80. tab_preview_panel->hide();
  81. } else {
  82. String path = EditorNode::get_editor_data().get_scene_path(p_tab);
  83. if (!path.is_empty()) {
  84. EditorResourcePreview::get_singleton()->queue_resource_preview(path, this, "_tab_preview_done", p_tab);
  85. }
  86. }
  87. }
  88. void EditorSceneTabs::_scene_tab_exit() {
  89. tab_preview_panel->hide();
  90. }
  91. void EditorSceneTabs::_scene_tab_input(const Ref<InputEvent> &p_input) {
  92. int tab_id = scene_tabs->get_hovered_tab();
  93. Ref<InputEventMouseButton> mb = p_input;
  94. if (mb.is_valid()) {
  95. if (tab_id >= 0) {
  96. if (mb->get_button_index() == MouseButton::MIDDLE && mb->is_pressed()) {
  97. _scene_tab_closed(tab_id);
  98. }
  99. } else if (mb->get_button_index() == MouseButton::LEFT && mb->is_double_click()) {
  100. int tab_buttons = 0;
  101. if (scene_tabs->get_offset_buttons_visible()) {
  102. tab_buttons = get_theme_icon(SNAME("increment"), SNAME("TabBar"))->get_width() + get_theme_icon(SNAME("decrement"), SNAME("TabBar"))->get_width();
  103. }
  104. if ((is_layout_rtl() && mb->get_position().x > tab_buttons) || (!is_layout_rtl() && mb->get_position().x < scene_tabs->get_size().width - tab_buttons)) {
  105. EditorNode::get_singleton()->trigger_menu_option(EditorNode::FILE_NEW_SCENE, true);
  106. }
  107. }
  108. if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  109. // Context menu.
  110. _update_context_menu();
  111. scene_tabs_context_menu->set_position(scene_tabs->get_screen_position() + mb->get_position());
  112. scene_tabs_context_menu->reset_size();
  113. scene_tabs_context_menu->popup();
  114. }
  115. }
  116. }
  117. void EditorSceneTabs::_reposition_active_tab(int p_to_index) {
  118. EditorNode::get_editor_data().move_edited_scene_to_index(p_to_index);
  119. update_scene_tabs();
  120. }
  121. void EditorSceneTabs::_update_context_menu() {
  122. scene_tabs_context_menu->clear();
  123. scene_tabs_context_menu->reset_size();
  124. int tab_id = scene_tabs->get_hovered_tab();
  125. bool no_root_node = !EditorNode::get_editor_data().get_edited_scene_root(tab_id);
  126. scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/new_scene"), EditorNode::FILE_NEW_SCENE);
  127. if (tab_id >= 0) {
  128. scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_scene"), EditorNode::FILE_SAVE_SCENE);
  129. _disable_menu_option_if(EditorNode::FILE_SAVE_SCENE, no_root_node);
  130. scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_scene_as"), EditorNode::FILE_SAVE_AS_SCENE);
  131. _disable_menu_option_if(EditorNode::FILE_SAVE_AS_SCENE, no_root_node);
  132. }
  133. scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_all_scenes"), EditorNode::FILE_SAVE_ALL_SCENES);
  134. bool can_save_all_scenes = false;
  135. for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) {
  136. if (!EditorNode::get_editor_data().get_scene_path(i).is_empty() && EditorNode::get_editor_data().get_edited_scene_root(i)) {
  137. can_save_all_scenes = true;
  138. break;
  139. }
  140. }
  141. _disable_menu_option_if(EditorNode::FILE_SAVE_ALL_SCENES, !can_save_all_scenes);
  142. if (tab_id >= 0) {
  143. scene_tabs_context_menu->add_separator();
  144. scene_tabs_context_menu->add_item(TTR("Show in FileSystem"), EditorNode::FILE_SHOW_IN_FILESYSTEM);
  145. _disable_menu_option_if(EditorNode::FILE_SHOW_IN_FILESYSTEM, !ResourceLoader::exists(EditorNode::get_editor_data().get_scene_path(tab_id)));
  146. scene_tabs_context_menu->add_item(TTR("Play This Scene"), EditorNode::FILE_RUN_SCENE);
  147. _disable_menu_option_if(EditorNode::FILE_RUN_SCENE, no_root_node);
  148. scene_tabs_context_menu->add_separator();
  149. scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/close_scene"), EditorNode::FILE_CLOSE);
  150. scene_tabs_context_menu->set_item_text(scene_tabs_context_menu->get_item_index(EditorNode::FILE_CLOSE), TTR("Close Tab"));
  151. scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/reopen_closed_scene"), EditorNode::FILE_OPEN_PREV);
  152. scene_tabs_context_menu->set_item_text(scene_tabs_context_menu->get_item_index(EditorNode::FILE_OPEN_PREV), TTR("Undo Close Tab"));
  153. _disable_menu_option_if(EditorNode::FILE_OPEN_PREV, !EditorNode::get_singleton()->has_previous_scenes());
  154. scene_tabs_context_menu->add_item(TTR("Close Other Tabs"), EditorNode::FILE_CLOSE_OTHERS);
  155. _disable_menu_option_if(EditorNode::FILE_CLOSE_OTHERS, EditorNode::get_editor_data().get_edited_scene_count() <= 1);
  156. scene_tabs_context_menu->add_item(TTR("Close Tabs to the Right"), EditorNode::FILE_CLOSE_RIGHT);
  157. _disable_menu_option_if(EditorNode::FILE_CLOSE_RIGHT, EditorNode::get_editor_data().get_edited_scene_count() == tab_id + 1);
  158. scene_tabs_context_menu->add_item(TTR("Close All Tabs"), EditorNode::FILE_CLOSE_ALL);
  159. }
  160. }
  161. void EditorSceneTabs::_disable_menu_option_if(int p_option, bool p_condition) {
  162. if (p_condition) {
  163. scene_tabs_context_menu->set_item_disabled(scene_tabs_context_menu->get_item_index(p_option), true);
  164. }
  165. }
  166. // TODO: This REALLY should be done in a better way than replacing all tabs after almost EVERY action.
  167. void EditorSceneTabs::update_scene_tabs() {
  168. tab_preview_panel->hide();
  169. bool show_rb = EDITOR_GET("interface/scene_tabs/show_script_button");
  170. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_GLOBAL_MENU)) {
  171. DisplayServer::get_singleton()->global_menu_clear("_dock");
  172. }
  173. // Get all scene names, which may be ambiguous.
  174. Vector<String> disambiguated_scene_names;
  175. Vector<String> full_path_names;
  176. for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) {
  177. disambiguated_scene_names.append(EditorNode::get_editor_data().get_scene_title(i));
  178. full_path_names.append(EditorNode::get_editor_data().get_scene_path(i));
  179. }
  180. EditorNode::disambiguate_filenames(full_path_names, disambiguated_scene_names);
  181. // Workaround to ignore the tab_changed signal from the first added tab.
  182. scene_tabs->disconnect("tab_changed", callable_mp(this, &EditorSceneTabs::_scene_tab_changed));
  183. scene_tabs->clear_tabs();
  184. Ref<Texture2D> script_icon = get_editor_theme_icon(SNAME("Script"));
  185. for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) {
  186. Node *type_node = EditorNode::get_editor_data().get_edited_scene_root(i);
  187. Ref<Texture2D> icon;
  188. if (type_node) {
  189. icon = EditorNode::get_singleton()->get_object_icon(type_node, "Node");
  190. }
  191. bool unsaved = EditorUndoRedoManager::get_singleton()->is_history_unsaved(EditorNode::get_editor_data().get_scene_history_id(i));
  192. scene_tabs->add_tab(disambiguated_scene_names[i] + (unsaved ? "(*)" : ""), icon);
  193. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_GLOBAL_MENU)) {
  194. DisplayServer::get_singleton()->global_menu_add_item("_dock", EditorNode::get_editor_data().get_scene_title(i) + (unsaved ? "(*)" : ""), callable_mp(this, &EditorSceneTabs::_global_menu_scene), Callable(), i);
  195. }
  196. if (show_rb && EditorNode::get_editor_data().get_scene_root_script(i).is_valid()) {
  197. scene_tabs->set_tab_button_icon(i, script_icon);
  198. }
  199. }
  200. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_GLOBAL_MENU)) {
  201. DisplayServer::get_singleton()->global_menu_add_separator("_dock");
  202. DisplayServer::get_singleton()->global_menu_add_item("_dock", TTR("New Window"), callable_mp(this, &EditorSceneTabs::_global_menu_new_window));
  203. }
  204. if (scene_tabs->get_tab_count() > 0) {
  205. scene_tabs->set_current_tab(EditorNode::get_editor_data().get_edited_scene());
  206. }
  207. const Size2 add_button_size = Size2(scene_tab_add->get_size().x, scene_tabs->get_size().y);
  208. if (scene_tabs->get_offset_buttons_visible()) {
  209. // Move the add button to a fixed position.
  210. if (scene_tab_add->get_parent() == scene_tabs) {
  211. scene_tabs->remove_child(scene_tab_add);
  212. scene_tab_add_ph->add_child(scene_tab_add);
  213. scene_tab_add->set_rect(Rect2(Point2(), add_button_size));
  214. }
  215. } else {
  216. // Move the add button to be after the last tab.
  217. if (scene_tab_add->get_parent() == scene_tab_add_ph) {
  218. scene_tab_add_ph->remove_child(scene_tab_add);
  219. scene_tabs->add_child(scene_tab_add);
  220. }
  221. if (scene_tabs->get_tab_count() == 0) {
  222. scene_tab_add->set_rect(Rect2(Point2(), add_button_size));
  223. return;
  224. }
  225. Rect2 last_tab = scene_tabs->get_tab_rect(scene_tabs->get_tab_count() - 1);
  226. int hsep = scene_tabs->get_theme_constant(SNAME("h_separation"));
  227. if (scene_tabs->is_layout_rtl()) {
  228. scene_tab_add->set_rect(Rect2(Point2(last_tab.position.x - add_button_size.x - hsep, last_tab.position.y), add_button_size));
  229. } else {
  230. scene_tab_add->set_rect(Rect2(Point2(last_tab.position.x + last_tab.size.width + hsep, last_tab.position.y), add_button_size));
  231. }
  232. }
  233. // Reconnect after everything is done.
  234. scene_tabs->connect("tab_changed", callable_mp(this, &EditorSceneTabs::_scene_tab_changed));
  235. }
  236. void EditorSceneTabs::_tab_preview_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata) {
  237. int p_tab = p_udata;
  238. if (p_preview.is_valid()) {
  239. tab_preview->set_texture(p_preview);
  240. Rect2 rect = scene_tabs->get_tab_rect(p_tab);
  241. rect.position += scene_tabs->get_global_position();
  242. tab_preview_panel->set_global_position(rect.position + Vector2(0, rect.size.height));
  243. tab_preview_panel->show();
  244. }
  245. }
  246. void EditorSceneTabs::_global_menu_scene(const Variant &p_tag) {
  247. int idx = (int)p_tag;
  248. scene_tabs->set_current_tab(idx);
  249. }
  250. void EditorSceneTabs::_global_menu_new_window(const Variant &p_tag) {
  251. if (OS::get_singleton()->get_main_loop()) {
  252. List<String> args;
  253. args.push_back("-p");
  254. OS::get_singleton()->create_instance(args);
  255. }
  256. }
  257. void EditorSceneTabs::shortcut_input(const Ref<InputEvent> &p_event) {
  258. ERR_FAIL_COND(p_event.is_null());
  259. Ref<InputEventKey> k = p_event;
  260. if ((k.is_valid() && k->is_pressed() && !k->is_echo()) || Object::cast_to<InputEventShortcut>(*p_event)) {
  261. if (ED_IS_SHORTCUT("editor/next_tab", p_event)) {
  262. int next_tab = EditorNode::get_editor_data().get_edited_scene() + 1;
  263. next_tab %= EditorNode::get_editor_data().get_edited_scene_count();
  264. _scene_tab_changed(next_tab);
  265. }
  266. if (ED_IS_SHORTCUT("editor/prev_tab", p_event)) {
  267. int next_tab = EditorNode::get_editor_data().get_edited_scene() - 1;
  268. next_tab = next_tab >= 0 ? next_tab : EditorNode::get_editor_data().get_edited_scene_count() - 1;
  269. _scene_tab_changed(next_tab);
  270. }
  271. }
  272. }
  273. void EditorSceneTabs::add_extra_button(Button *p_button) {
  274. tabbar_container->add_child(p_button);
  275. }
  276. void EditorSceneTabs::set_current_tab(int p_tab) {
  277. scene_tabs->set_current_tab(p_tab);
  278. }
  279. int EditorSceneTabs::get_current_tab() const {
  280. return scene_tabs->get_current_tab();
  281. }
  282. void EditorSceneTabs::_bind_methods() {
  283. ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab_index")));
  284. ADD_SIGNAL(MethodInfo("tab_closed", PropertyInfo(Variant::INT, "tab_index")));
  285. ClassDB::bind_method("_tab_preview_done", &EditorSceneTabs::_tab_preview_done);
  286. }
  287. EditorSceneTabs::EditorSceneTabs() {
  288. singleton = this;
  289. set_process_shortcut_input(true);
  290. tabbar_panel = memnew(PanelContainer);
  291. add_child(tabbar_panel);
  292. tabbar_container = memnew(HBoxContainer);
  293. tabbar_panel->add_child(tabbar_container);
  294. scene_tabs = memnew(TabBar);
  295. scene_tabs->set_select_with_rmb(true);
  296. scene_tabs->add_tab("unsaved");
  297. scene_tabs->set_tab_close_display_policy((TabBar::CloseButtonDisplayPolicy)EDITOR_GET("interface/scene_tabs/display_close_button").operator int());
  298. scene_tabs->set_max_tab_width(int(EDITOR_GET("interface/scene_tabs/maximum_width")) * EDSCALE);
  299. scene_tabs->set_drag_to_rearrange_enabled(true);
  300. scene_tabs->set_auto_translate(false);
  301. scene_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  302. tabbar_container->add_child(scene_tabs);
  303. scene_tabs->connect("tab_changed", callable_mp(this, &EditorSceneTabs::_scene_tab_changed));
  304. scene_tabs->connect("tab_button_pressed", callable_mp(this, &EditorSceneTabs::_scene_tab_script_edited));
  305. scene_tabs->connect("tab_close_pressed", callable_mp(this, &EditorSceneTabs::_scene_tab_closed));
  306. scene_tabs->connect("tab_hovered", callable_mp(this, &EditorSceneTabs::_scene_tab_hovered));
  307. scene_tabs->connect("mouse_exited", callable_mp(this, &EditorSceneTabs::_scene_tab_exit));
  308. scene_tabs->connect("gui_input", callable_mp(this, &EditorSceneTabs::_scene_tab_input));
  309. scene_tabs->connect("active_tab_rearranged", callable_mp(this, &EditorSceneTabs::_reposition_active_tab));
  310. scene_tabs->connect("resized", callable_mp(this, &EditorSceneTabs::update_scene_tabs));
  311. scene_tabs_context_menu = memnew(PopupMenu);
  312. tabbar_container->add_child(scene_tabs_context_menu);
  313. scene_tabs_context_menu->connect("id_pressed", callable_mp(EditorNode::get_singleton(), &EditorNode::trigger_menu_option).bind(false));
  314. scene_tab_add = memnew(Button);
  315. scene_tab_add->set_flat(true);
  316. scene_tab_add->set_tooltip_text(TTR("Add a new scene."));
  317. scene_tabs->add_child(scene_tab_add);
  318. scene_tab_add->connect("pressed", callable_mp(EditorNode::get_singleton(), &EditorNode::trigger_menu_option).bind(EditorNode::FILE_NEW_SCENE, false));
  319. scene_tab_add_ph = memnew(Control);
  320. scene_tab_add_ph->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  321. scene_tab_add_ph->set_custom_minimum_size(scene_tab_add->get_minimum_size());
  322. tabbar_container->add_child(scene_tab_add_ph);
  323. // On-hover tab preview.
  324. Control *tab_preview_anchor = memnew(Control);
  325. tab_preview_anchor->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  326. add_child(tab_preview_anchor);
  327. tab_preview_panel = memnew(Panel);
  328. tab_preview_panel->set_size(Size2(100, 100) * EDSCALE);
  329. tab_preview_panel->hide();
  330. tab_preview_panel->set_self_modulate(Color(1, 1, 1, 0.7));
  331. tab_preview_anchor->add_child(tab_preview_panel);
  332. tab_preview = memnew(TextureRect);
  333. tab_preview->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  334. tab_preview->set_size(Size2(96, 96) * EDSCALE);
  335. tab_preview->set_position(Point2(2, 2) * EDSCALE);
  336. tab_preview_panel->add_child(tab_preview);
  337. }