Path: blob/master/editor/scene/editor_scene_tabs.cpp
10278 views
/**************************************************************************/1/* editor_scene_tabs.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_scene_tabs.h"3132#include "editor/docks/inspector_dock.h"33#include "editor/editor_main_screen.h"34#include "editor/editor_node.h"35#include "editor/editor_string_names.h"36#include "editor/editor_undo_redo_manager.h"37#include "editor/inspector/editor_context_menu_plugin.h"38#include "editor/inspector/editor_resource_preview.h"39#include "editor/run/editor_run_bar.h"40#include "editor/settings/editor_settings.h"41#include "editor/themes/editor_scale.h"42#include "scene/gui/box_container.h"43#include "scene/gui/button.h"44#include "scene/gui/panel.h"45#include "scene/gui/panel_container.h"46#include "scene/gui/popup_menu.h"47#include "scene/gui/tab_bar.h"48#include "scene/gui/texture_rect.h"4950void EditorSceneTabs::_notification(int p_what) {51switch (p_what) {52case NOTIFICATION_THEME_CHANGED: {53tabbar_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("tabbar_background"), SNAME("TabContainer")));54scene_tabs->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));5556scene_tab_add->set_button_icon(get_editor_theme_icon(SNAME("Add")));57scene_tab_add->add_theme_color_override("icon_normal_color", Color(0.6f, 0.6f, 0.6f, 0.8f));5859scene_tab_add_ph->set_custom_minimum_size(scene_tab_add->get_minimum_size());60} break;6162case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {63if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/scene_tabs")) {64scene_tabs->set_tab_close_display_policy((TabBar::CloseButtonDisplayPolicy)EDITOR_GET("interface/scene_tabs/display_close_button").operator int());65scene_tabs->set_max_tab_width(int(EDITOR_GET("interface/scene_tabs/maximum_width")) * EDSCALE);66_scene_tabs_resized();67}68} break;6970case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:71case NOTIFICATION_TRANSLATION_CHANGED: {72_scene_tabs_resized();73} break;74}75}7677void EditorSceneTabs::_scene_tab_changed(int p_tab) {78tab_preview_panel->hide();7980emit_signal("tab_changed", p_tab);81}8283void EditorSceneTabs::_scene_tab_script_edited(int p_tab) {84Ref<Script> scr = EditorNode::get_editor_data().get_scene_root_script(p_tab);85if (scr.is_valid()) {86InspectorDock::get_singleton()->edit_resource(scr);87}88}8990void EditorSceneTabs::_scene_tab_closed(int p_tab) {91emit_signal("tab_closed", p_tab);92}9394void EditorSceneTabs::_scene_tab_hovered(int p_tab) {95if (!bool(EDITOR_GET("interface/scene_tabs/show_thumbnail_on_hover"))) {96return;97}9899// Currently the tab previews are displayed under the running game process when embed.100// Right now, the easiest technique to fix that is to prevent displaying the tab preview101// when the user is in the Game View.102if (EditorNode::get_singleton()->get_editor_main_screen()->get_selected_index() == EditorMainScreen::EDITOR_GAME && EditorRunBar::get_singleton()->is_playing()) {103return;104}105106int current_tab = scene_tabs->get_current_tab();107108if (p_tab == current_tab || p_tab < 0) {109tab_preview_panel->hide();110} else {111String path = EditorNode::get_editor_data().get_scene_path(p_tab);112if (!path.is_empty()) {113EditorResourcePreview::get_singleton()->queue_resource_preview(path, this, "_tab_preview_done", p_tab);114}115}116}117118void EditorSceneTabs::_scene_tab_exit() {119tab_preview_panel->hide();120}121122void EditorSceneTabs::_scene_tab_input(const Ref<InputEvent> &p_input) {123Ref<InputEventMouseButton> mb = p_input;124125if (mb.is_valid()) {126if (mb->get_button_index() == MouseButton::LEFT && mb->is_double_click()) {127int tab_buttons = 0;128if (scene_tabs->get_offset_buttons_visible()) {129tab_buttons = get_theme_icon(SNAME("increment"), SNAME("TabBar"))->get_width() + get_theme_icon(SNAME("decrement"), SNAME("TabBar"))->get_width();130}131132if ((is_layout_rtl() && mb->get_position().x > tab_buttons) || (!is_layout_rtl() && mb->get_position().x < scene_tabs->get_size().width - tab_buttons)) {133EditorNode::get_singleton()->trigger_menu_option(EditorNode::SCENE_NEW_SCENE, true);134}135}136if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {137// Context menu.138_update_context_menu();139140scene_tabs_context_menu->set_position(scene_tabs->get_screen_position() + mb->get_position());141scene_tabs_context_menu->reset_size();142scene_tabs_context_menu->popup();143}144}145}146147void EditorSceneTabs::unhandled_key_input(const Ref<InputEvent> &p_event) {148if (!tab_preview_panel->is_visible()) {149return;150}151152Ref<InputEventKey> k = p_event;153if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true)) {154tab_preview_panel->hide();155}156}157158void EditorSceneTabs::_reposition_active_tab(int p_to_index) {159EditorNode::get_editor_data().move_edited_scene_to_index(p_to_index);160update_scene_tabs();161}162163void EditorSceneTabs::_update_context_menu() {164#define DISABLE_LAST_OPTION_IF(m_condition) \165if (m_condition) { \166scene_tabs_context_menu->set_item_disabled(-1, true); \167}168169scene_tabs_context_menu->clear();170scene_tabs_context_menu->reset_size();171172int tab_id = scene_tabs->get_hovered_tab();173bool no_root_node = !EditorNode::get_editor_data().get_edited_scene_root(tab_id);174175scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/new_scene"), EditorNode::SCENE_NEW_SCENE);176if (tab_id >= 0) {177scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_scene"), EditorNode::SCENE_SAVE_SCENE);178DISABLE_LAST_OPTION_IF(no_root_node);179scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_scene_as"), EditorNode::SCENE_SAVE_AS_SCENE);180DISABLE_LAST_OPTION_IF(no_root_node);181}182183bool can_save_all_scenes = false;184for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) {185if (!EditorNode::get_editor_data().get_scene_path(i).is_empty() && EditorNode::get_editor_data().get_edited_scene_root(i)) {186can_save_all_scenes = true;187break;188}189}190scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_all_scenes"), EditorNode::SCENE_SAVE_ALL_SCENES);191DISABLE_LAST_OPTION_IF(!can_save_all_scenes);192193if (tab_id >= 0) {194scene_tabs_context_menu->add_separator();195scene_tabs_context_menu->add_item(TTR("Show in FileSystem"), SCENE_SHOW_IN_FILESYSTEM);196DISABLE_LAST_OPTION_IF(!ResourceLoader::exists(EditorNode::get_editor_data().get_scene_path(tab_id)));197scene_tabs_context_menu->add_item(TTR("Play This Scene"), SCENE_RUN);198DISABLE_LAST_OPTION_IF(no_root_node);199200scene_tabs_context_menu->add_separator();201scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/close_scene"), EditorNode::SCENE_CLOSE);202scene_tabs_context_menu->set_item_text(-1, TTR("Close Tab"));203scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/reopen_closed_scene"), EditorNode::SCENE_OPEN_PREV);204scene_tabs_context_menu->set_item_text(-1, TTR("Undo Close Tab"));205DISABLE_LAST_OPTION_IF(!EditorNode::get_singleton()->has_previous_closed_scenes());206scene_tabs_context_menu->add_item(TTR("Close Other Tabs"), SCENE_CLOSE_OTHERS);207DISABLE_LAST_OPTION_IF(EditorNode::get_editor_data().get_edited_scene_count() <= 1);208scene_tabs_context_menu->add_item(TTR("Close Tabs to the Right"), SCENE_CLOSE_RIGHT);209DISABLE_LAST_OPTION_IF(EditorNode::get_editor_data().get_edited_scene_count() == tab_id + 1);210scene_tabs_context_menu->add_item(TTR("Close All Tabs"), SCENE_CLOSE_ALL);211212const PackedStringArray paths = { EditorNode::get_editor_data().get_scene_path(tab_id) };213EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(scene_tabs_context_menu, EditorContextMenuPlugin::CONTEXT_SLOT_SCENE_TABS, paths);214} else {215EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(scene_tabs_context_menu, EditorContextMenuPlugin::CONTEXT_SLOT_SCENE_TABS, {});216}217#undef DISABLE_LAST_OPTION_IF218219last_hovered_tab = tab_id;220}221222void EditorSceneTabs::_custom_menu_option(int p_option) {223if (p_option >= EditorContextMenuPlugin::BASE_ID) {224EditorContextMenuPluginManager::get_singleton()->activate_custom_option(EditorContextMenuPlugin::CONTEXT_SLOT_SCENE_TABS, p_option, last_hovered_tab >= 0 ? EditorNode::get_editor_data().get_scene_path(last_hovered_tab) : String());225}226}227228void EditorSceneTabs::update_scene_tabs() {229static bool menu_initialized = false;230tab_preview_panel->hide();231232if (menu_initialized && scene_tabs->get_tab_count() == EditorNode::get_editor_data().get_edited_scene_count()) {233_update_tab_titles();234return;235}236menu_initialized = true;237238if (NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)) {239RID dock_rid = NativeMenu::get_singleton()->get_system_menu(NativeMenu::DOCK_MENU_ID);240NativeMenu::get_singleton()->clear(dock_rid);241}242243scene_tabs->set_block_signals(true);244scene_tabs->set_tab_count(EditorNode::get_editor_data().get_edited_scene_count());245scene_tabs->set_block_signals(false);246247if (NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)) {248RID dock_rid = NativeMenu::get_singleton()->get_system_menu(NativeMenu::DOCK_MENU_ID);249for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) {250int global_menu_index = NativeMenu::get_singleton()->add_item(dock_rid, EditorNode::get_editor_data().get_scene_title(i), callable_mp(this, &EditorSceneTabs::_global_menu_scene), Callable(), i);251scene_tabs->set_tab_metadata(i, global_menu_index);252}253NativeMenu::get_singleton()->add_separator(dock_rid);254NativeMenu::get_singleton()->add_item(dock_rid, TTR("New Window"), callable_mp(this, &EditorSceneTabs::_global_menu_new_window));255}256257_update_tab_titles();258}259260void EditorSceneTabs::_update_tab_titles() {261bool show_rb = EDITOR_GET("interface/scene_tabs/show_script_button");262263// Get all scene names, which may be ambiguous.264Vector<String> disambiguated_scene_names;265Vector<String> full_path_names;266for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) {267disambiguated_scene_names.append(EditorNode::get_editor_data().get_scene_title(i));268full_path_names.append(EditorNode::get_editor_data().get_scene_path(i));269}270EditorNode::disambiguate_filenames(full_path_names, disambiguated_scene_names);271272Ref<Texture2D> script_icon = get_editor_theme_icon(SNAME("Script"));273for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) {274Node *type_node = EditorNode::get_editor_data().get_edited_scene_root(i);275Ref<Texture2D> icon;276if (type_node) {277icon = EditorNode::get_singleton()->get_object_icon(type_node, "Node");278}279scene_tabs->set_tab_icon(i, icon);280281bool unsaved = EditorUndoRedoManager::get_singleton()->is_history_unsaved(EditorNode::get_editor_data().get_scene_history_id(i));282scene_tabs->set_tab_title(i, disambiguated_scene_names[i] + (unsaved ? "(*)" : ""));283284if (NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)) {285RID dock_rid = NativeMenu::get_singleton()->get_system_menu(NativeMenu::DOCK_MENU_ID);286int global_menu_index = scene_tabs->get_tab_metadata(i);287NativeMenu::get_singleton()->set_item_text(dock_rid, global_menu_index, EditorNode::get_editor_data().get_scene_title(i) + (unsaved ? "(*)" : ""));288NativeMenu::get_singleton()->set_item_tag(dock_rid, global_menu_index, i);289}290291if (show_rb && EditorNode::get_editor_data().get_scene_root_script(i).is_valid()) {292scene_tabs->set_tab_button_icon(i, script_icon);293} else {294scene_tabs->set_tab_button_icon(i, nullptr);295}296}297298int current_tab = EditorNode::get_editor_data().get_edited_scene();299if (scene_tabs->get_tab_count() > 0 && scene_tabs->get_current_tab() != current_tab) {300scene_tabs->set_block_signals(true);301scene_tabs->set_current_tab(current_tab);302scene_tabs->set_block_signals(false);303}304305_scene_tabs_resized();306}307308void EditorSceneTabs::_scene_tabs_resized() {309const Size2 add_button_size = Size2(scene_tab_add->get_size().x, scene_tabs->get_size().y);310if (scene_tabs->get_offset_buttons_visible()) {311// Move the add button to a fixed position.312if (scene_tab_add->get_parent() == scene_tabs) {313scene_tabs->remove_child(scene_tab_add);314scene_tab_add_ph->add_child(scene_tab_add);315scene_tab_add->set_rect(Rect2(Point2(), add_button_size));316}317} else {318// Move the add button to be after the last tab.319if (scene_tab_add->get_parent() == scene_tab_add_ph) {320scene_tab_add_ph->remove_child(scene_tab_add);321scene_tabs->add_child(scene_tab_add);322}323324if (scene_tabs->get_tab_count() == 0) {325scene_tab_add->set_rect(Rect2(Point2(), add_button_size));326return;327}328329Rect2 last_tab = scene_tabs->get_tab_rect(scene_tabs->get_tab_count() - 1);330int hsep = scene_tabs->get_theme_constant(SNAME("h_separation"));331if (scene_tabs->is_layout_rtl()) {332scene_tab_add->set_rect(Rect2(Point2(last_tab.position.x - add_button_size.x - hsep, last_tab.position.y), add_button_size));333} else {334scene_tab_add->set_rect(Rect2(Point2(last_tab.position.x + last_tab.size.width + hsep, last_tab.position.y), add_button_size));335}336}337}338339void EditorSceneTabs::_tab_preview_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata) {340int p_tab = p_udata;341if (p_preview.is_valid()) {342tab_preview->set_texture(p_preview);343344Rect2 rect = scene_tabs->get_tab_rect(p_tab);345rect.position += scene_tabs->get_global_position();346tab_preview_panel->set_global_position(rect.position + Vector2(0, rect.size.height));347tab_preview_panel->show();348}349}350351void EditorSceneTabs::_global_menu_scene(const Variant &p_tag) {352int idx = (int)p_tag;353scene_tabs->set_current_tab(idx);354}355356void EditorSceneTabs::_global_menu_new_window(const Variant &p_tag) {357if (OS::get_singleton()->get_main_loop()) {358List<String> args;359args.push_back("-p");360OS::get_singleton()->create_instance(args);361}362}363364void EditorSceneTabs::shortcut_input(const Ref<InputEvent> &p_event) {365ERR_FAIL_COND(p_event.is_null());366367Ref<InputEventKey> k = p_event;368if ((k.is_valid() && k->is_pressed() && !k->is_echo()) || Object::cast_to<InputEventShortcut>(*p_event)) {369if (ED_IS_SHORTCUT("editor/next_tab", p_event)) {370int next_tab = EditorNode::get_editor_data().get_edited_scene() + 1;371next_tab %= EditorNode::get_editor_data().get_edited_scene_count();372_scene_tab_changed(next_tab);373}374if (ED_IS_SHORTCUT("editor/prev_tab", p_event)) {375int next_tab = EditorNode::get_editor_data().get_edited_scene() - 1;376next_tab = next_tab >= 0 ? next_tab : EditorNode::get_editor_data().get_edited_scene_count() - 1;377_scene_tab_changed(next_tab);378}379}380}381382void EditorSceneTabs::add_extra_button(Button *p_button) {383tabbar_container->add_child(p_button);384}385386void EditorSceneTabs::set_current_tab(int p_tab) {387scene_tabs->set_current_tab(p_tab);388}389390int EditorSceneTabs::get_current_tab() const {391return scene_tabs->get_current_tab();392}393394void EditorSceneTabs::_bind_methods() {395ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab_index")));396ADD_SIGNAL(MethodInfo("tab_closed", PropertyInfo(Variant::INT, "tab_index")));397398ClassDB::bind_method("_tab_preview_done", &EditorSceneTabs::_tab_preview_done);399}400401EditorSceneTabs::EditorSceneTabs() {402singleton = this;403404set_process_shortcut_input(true);405set_process_unhandled_key_input(true);406407tabbar_panel = memnew(PanelContainer);408add_child(tabbar_panel);409tabbar_container = memnew(HBoxContainer);410tabbar_panel->add_child(tabbar_container);411412scene_tabs = memnew(TabBar);413scene_tabs->set_select_with_rmb(true);414scene_tabs->add_tab("unsaved");415scene_tabs->set_tab_close_display_policy((TabBar::CloseButtonDisplayPolicy)EDITOR_GET("interface/scene_tabs/display_close_button").operator int());416scene_tabs->set_max_tab_width(int(EDITOR_GET("interface/scene_tabs/maximum_width")) * EDSCALE);417scene_tabs->set_drag_to_rearrange_enabled(true);418scene_tabs->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);419scene_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL);420tabbar_container->add_child(scene_tabs);421422scene_tabs->connect("tab_changed", callable_mp(this, &EditorSceneTabs::_scene_tab_changed));423scene_tabs->connect("tab_button_pressed", callable_mp(this, &EditorSceneTabs::_scene_tab_script_edited));424scene_tabs->connect("tab_close_pressed", callable_mp(this, &EditorSceneTabs::_scene_tab_closed));425scene_tabs->connect("tab_hovered", callable_mp(this, &EditorSceneTabs::_scene_tab_hovered));426scene_tabs->connect(SceneStringName(mouse_exited), callable_mp(this, &EditorSceneTabs::_scene_tab_exit));427scene_tabs->connect(SceneStringName(gui_input), callable_mp(this, &EditorSceneTabs::_scene_tab_input));428scene_tabs->connect("active_tab_rearranged", callable_mp(this, &EditorSceneTabs::_reposition_active_tab));429scene_tabs->connect(SceneStringName(resized), callable_mp(this, &EditorSceneTabs::_scene_tabs_resized), CONNECT_DEFERRED);430431scene_tabs_context_menu = memnew(PopupMenu);432tabbar_container->add_child(scene_tabs_context_menu);433scene_tabs_context_menu->connect(SceneStringName(id_pressed), callable_mp(EditorNode::get_singleton(), &EditorNode::trigger_menu_option).bind(false));434scene_tabs_context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorSceneTabs::_custom_menu_option));435436scene_tab_add = memnew(Button);437scene_tab_add->set_flat(true);438scene_tab_add->set_tooltip_text(TTR("Add a new scene."));439scene_tabs->add_child(scene_tab_add);440scene_tab_add->connect(SceneStringName(pressed), callable_mp(EditorNode::get_singleton(), &EditorNode::trigger_menu_option).bind(EditorNode::SCENE_NEW_SCENE, false));441442scene_tab_add_ph = memnew(Control);443scene_tab_add_ph->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);444scene_tab_add_ph->set_custom_minimum_size(scene_tab_add->get_minimum_size());445tabbar_container->add_child(scene_tab_add_ph);446447// On-hover tab preview.448449Control *tab_preview_anchor = memnew(Control);450tab_preview_anchor->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);451add_child(tab_preview_anchor);452453tab_preview_panel = memnew(Panel);454tab_preview_panel->set_size(Size2(100, 100) * EDSCALE);455tab_preview_panel->hide();456tab_preview_panel->set_self_modulate(Color(1, 1, 1, 0.7));457tab_preview_anchor->add_child(tab_preview_panel);458459tab_preview = memnew(TextureRect);460tab_preview->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);461tab_preview->set_size(Size2(96, 96) * EDSCALE);462tab_preview->set_position(Point2(2, 2) * EDSCALE);463tab_preview_panel->add_child(tab_preview);464}465466467