| 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 | |
| 31 | #include "editor_scene_tabs.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "editor/docks/inspector_dock.h" |
| 35 | #include "editor/editor_main_screen.h" |
| 36 | #include "editor/editor_node.h" |
| 37 | #include "editor/editor_string_names.h" |
| 38 | #include "editor/editor_undo_redo_manager.h" |
| 39 | #include "editor/inspector/editor_context_menu_plugin.h" |
| 40 | #include "editor/inspector/editor_resource_preview.h" |
| 41 | #include "editor/run/editor_run_bar.h" |
| 42 | #include "editor/settings/editor_settings.h" |
| 43 | #include "editor/themes/editor_scale.h" |
| 44 | #include "scene/gui/box_container.h" |
| 45 | #include "scene/gui/button.h" |
| 46 | #include "scene/gui/menu_button.h" |
| 47 | #include "scene/gui/panel.h" |
| 48 | #include "scene/gui/panel_container.h" |
| 49 | #include "scene/gui/popup_menu.h" |
| 50 | #include "scene/gui/tab_bar.h" |
| 51 | #include "scene/gui/texture_rect.h" |
| 52 | |
| 53 | void EditorSceneTabs::_notification(int p_what) { |
| 54 | switch (p_what) { |
| 55 | case NOTIFICATION_THEME_CHANGED: { |
| 56 | tabbar_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("tabbar_background"), SNAME("TabContainer"))); |
| 57 | scene_tabs->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor))); |
| 58 | |
| 59 | scene_list->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl"))); |
| 60 | |
| 61 | scene_tab_add->set_button_icon(get_editor_theme_icon(SNAME("Add"))); |
| 62 | scene_tab_add->add_theme_color_override("icon_normal_color", Color(0.6f, 0.6f, 0.6f, 0.8f)); |
| 63 | |
| 64 | scene_tab_add_ph->set_custom_minimum_size(scene_tab_add->get_minimum_size()); |
| 65 | } break; |
| 66 | |
| 67 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
| 68 | if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/scene_tabs")) { |
| 69 | scene_tabs->set_tab_close_display_policy((TabBar::CloseButtonDisplayPolicy)EDITOR_GET("interface/scene_tabs/display_close_button").operator int()); |
| 70 | scene_tabs->set_max_tab_width(int(EDITOR_GET("interface/scene_tabs/maximum_width")) * EDSCALE); |
| 71 | _scene_tabs_resized(); |
| 72 | } |
| 73 | } break; |
| 74 | |
| 75 | case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: |
| 76 | case NOTIFICATION_TRANSLATION_CHANGED: { |
| 77 | _scene_tabs_resized(); |
| 78 | } break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void EditorSceneTabs::_scene_tab_changed(int p_tab) { |
| 83 | tab_preview_panel->hide(); |
| 84 | |
| 85 | emit_signal("tab_changed", p_tab); |
| 86 | } |
| 87 | |
| 88 | void EditorSceneTabs::_scene_tab_script_edited(int p_tab) { |
| 89 | Ref<Script> scr = EditorNode::get_editor_data().get_scene_root_script(p_tab); |
| 90 | if (scr.is_valid()) { |
| 91 | InspectorDock::get_singleton()->edit_resource(scr); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void EditorSceneTabs::_scene_tab_closed(int p_tab) { |
| 96 | emit_signal("tab_closed", p_tab); |
| 97 | } |
| 98 | |
| 99 | void EditorSceneTabs::_scene_tab_hovered(int p_tab) { |
| 100 | if (!bool(EDITOR_GET("interface/scene_tabs/show_thumbnail_on_hover"))) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | // Currently the tab previews are displayed under the running game process when embed. |
| 105 | // Right now, the easiest technique to fix that is to prevent displaying the tab preview |
| 106 | // when the user is in the Game View. |
| 107 | if (EditorNode::get_singleton()->get_editor_main_screen()->get_selected_index() == EditorMainScreen::EDITOR_GAME && EditorRunBar::get_singleton()->is_playing()) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | int current_tab = scene_tabs->get_current_tab(); |
| 112 | |
| 113 | if (p_tab == current_tab || p_tab < 0) { |
| 114 | tab_preview_panel->hide(); |
| 115 | } else { |
| 116 | String path = EditorNode::get_editor_data().get_scene_path(p_tab); |
| 117 | if (!path.is_empty()) { |
| 118 | EditorResourcePreview::get_singleton()->queue_resource_preview(path, callable_mp(this, &EditorSceneTabs::_tab_preview_done).bind(p_tab)); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void EditorSceneTabs::_scene_tab_exit() { |
| 124 | tab_preview_panel->hide(); |
| 125 | } |
| 126 | |
| 127 | void EditorSceneTabs::_scene_tab_input(const Ref<InputEvent> &p_input) { |
| 128 | Ref<InputEventMouseButton> mb = p_input; |
| 129 | |
| 130 | if (mb.is_valid()) { |
| 131 | if (scene_tabs->get_hovered_tab() < 0 && mb->get_button_index() == MouseButton::LEFT && mb->is_double_click()) { |
| 132 | int tab_buttons = 0; |
| 133 | if (scene_tabs->get_offset_buttons_visible()) { |
| 134 | tab_buttons = get_theme_icon(SNAME("increment"), SNAME("TabBar"))->get_width() + get_theme_icon(SNAME("decrement"), SNAME("TabBar"))->get_width(); |
| 135 | } |
| 136 | |
| 137 | if ((is_layout_rtl() && mb->get_position().x > tab_buttons) || (!is_layout_rtl() && mb->get_position().x < scene_tabs->get_size().width - tab_buttons)) { |
| 138 | EditorNode::get_singleton()->trigger_menu_option(EditorNode::SCENE_NEW_SCENE, true); |
| 139 | } |
| 140 | } else if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) { |
| 141 | // Context menu. |
| 142 | _update_context_menu(); |
| 143 | |
| 144 | scene_tabs_context_menu->set_position(scene_tabs->get_screen_position() + mb->get_position()); |
| 145 | scene_tabs_context_menu->reset_size(); |
| 146 | scene_tabs_context_menu->popup(); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void EditorSceneTabs::unhandled_key_input(const Ref<InputEvent> &p_event) { |
| 152 | if (!tab_preview_panel->is_visible()) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | Ref<InputEventKey> k = p_event; |
| 157 | if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true)) { |
| 158 | tab_preview_panel->hide(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void EditorSceneTabs::_reposition_active_tab(int p_to_index) { |
| 163 | EditorNode::get_editor_data().move_edited_scene_to_index(p_to_index); |
| 164 | update_scene_tabs(); |
| 165 | } |
| 166 | |
| 167 | void EditorSceneTabs::_update_context_menu() { |
| 168 | #define DISABLE_LAST_OPTION_IF(m_condition) \ |
| 169 | if (m_condition) { \ |
| 170 | scene_tabs_context_menu->set_item_disabled(-1, true); \ |
| 171 | } |
| 172 | |
| 173 | scene_tabs_context_menu->clear(); |
| 174 | scene_tabs_context_menu->reset_size(); |
| 175 | |
| 176 | int tab_id = scene_tabs->get_hovered_tab(); |
| 177 | bool no_root_node = !EditorNode::get_editor_data().get_edited_scene_root(tab_id); |
| 178 | |
| 179 | scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/new_scene"), EditorNode::SCENE_NEW_SCENE); |
| 180 | if (tab_id >= 0) { |
| 181 | scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_scene"), EditorNode::SCENE_SAVE_SCENE); |
| 182 | DISABLE_LAST_OPTION_IF(no_root_node); |
| 183 | scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_scene_as"), EditorNode::SCENE_SAVE_AS_SCENE); |
| 184 | DISABLE_LAST_OPTION_IF(no_root_node); |
| 185 | } |
| 186 | |
| 187 | bool can_save_all_scenes = false; |
| 188 | for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) { |
| 189 | if (!EditorNode::get_editor_data().get_scene_path(i).is_empty() && EditorNode::get_editor_data().get_edited_scene_root(i)) { |
| 190 | can_save_all_scenes = true; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_all_scenes"), EditorNode::SCENE_SAVE_ALL_SCENES); |
| 195 | DISABLE_LAST_OPTION_IF(!can_save_all_scenes); |
| 196 | |
| 197 | if (tab_id >= 0) { |
| 198 | const String scene_path = EditorNode::get_editor_data().get_scene_path(tab_id); |
| 199 | const String main_scene_path = GLOBAL_GET("application/run/main_scene"); |
| 200 | |
| 201 | scene_tabs_context_menu->add_separator(); |
| 202 | scene_tabs_context_menu->add_item(TTR("Show in FileSystem"), SCENE_SHOW_IN_FILESYSTEM); |
| 203 | DISABLE_LAST_OPTION_IF(!ResourceLoader::exists(scene_path)); |
| 204 | scene_tabs_context_menu->add_item(TTR("Play This Scene"), SCENE_RUN); |
| 205 | DISABLE_LAST_OPTION_IF(no_root_node); |
| 206 | scene_tabs_context_menu->add_item(TTR("Set as Main Scene"), EditorNode::SCENE_TAB_SET_AS_MAIN_SCENE); |
| 207 | DISABLE_LAST_OPTION_IF(no_root_node || (!main_scene_path.is_empty() && ResourceUID::ensure_path(main_scene_path) == scene_path)); |
| 208 | |
| 209 | scene_tabs_context_menu->add_separator(); |
| 210 | scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/close_scene"), EditorNode::SCENE_CLOSE); |
| 211 | scene_tabs_context_menu->set_item_text(-1, TTR("Close Tab")); |
| 212 | scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/reopen_closed_scene"), EditorNode::SCENE_OPEN_PREV); |
| 213 | scene_tabs_context_menu->set_item_text(-1, TTR("Undo Close Tab")); |
| 214 | DISABLE_LAST_OPTION_IF(!EditorNode::get_singleton()->has_previous_closed_scenes()); |
| 215 | scene_tabs_context_menu->add_item(TTR("Close Other Tabs"), SCENE_CLOSE_OTHERS); |
| 216 | DISABLE_LAST_OPTION_IF(EditorNode::get_editor_data().get_edited_scene_count() <= 1); |
| 217 | scene_tabs_context_menu->add_item(TTR("Close Tabs to the Right"), SCENE_CLOSE_RIGHT); |
| 218 | DISABLE_LAST_OPTION_IF(EditorNode::get_editor_data().get_edited_scene_count() == tab_id + 1); |
| 219 | scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/close_all_scenes"), EditorNode::SCENE_CLOSE_ALL); |
| 220 | scene_tabs_context_menu->set_item_text(-1, TTRC("Close All Tabs")); |
| 221 | |
| 222 | const PackedStringArray paths = { EditorNode::get_editor_data().get_scene_path(tab_id) }; |
| 223 | EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(scene_tabs_context_menu, EditorContextMenuPlugin::CONTEXT_SLOT_SCENE_TABS, paths); |
| 224 | } else { |
| 225 | scene_tabs_context_menu->add_separator(); |
| 226 | scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/reopen_closed_scene"), EditorNode::SCENE_OPEN_PREV); |
| 227 | scene_tabs_context_menu->set_item_text(-1, TTRC("Undo Close Tab")); |
| 228 | DISABLE_LAST_OPTION_IF(!EditorNode::get_singleton()->has_previous_closed_scenes()); |
| 229 | scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/close_all_scenes"), EditorNode::SCENE_CLOSE_ALL); |
| 230 | scene_tabs_context_menu->set_item_text(-1, TTRC("Close All Tabs")); |
| 231 | |
| 232 | EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(scene_tabs_context_menu, EditorContextMenuPlugin::CONTEXT_SLOT_SCENE_TABS, {}); |
| 233 | } |
| 234 | #undef DISABLE_LAST_OPTION_IF |
| 235 | |
| 236 | last_hovered_tab = tab_id; |
| 237 | } |
| 238 | |
| 239 | void EditorSceneTabs::_custom_menu_option(int p_option) { |
| 240 | if (p_option >= EditorContextMenuPlugin::BASE_ID) { |
| 241 | EditorContextMenuPluginManager::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()); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | void EditorSceneTabs::_update_scene_list() { |
| 246 | PopupMenu *popup = scene_list->get_popup(); |
| 247 | popup->clear(); |
| 248 | |
| 249 | for (int i = 0; i < scene_tabs->get_tab_count(); i++) { |
| 250 | popup->add_item(scene_tabs->get_tab_title(i), i); |
| 251 | popup->set_item_icon(i, scene_tabs->get_tab_icon(i)); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void EditorSceneTabs::update_scene_tabs() { |
| 256 | static bool menu_initialized = false; |
| 257 | tab_preview_panel->hide(); |
| 258 | |
| 259 | if (menu_initialized && scene_tabs->get_tab_count() == EditorNode::get_editor_data().get_edited_scene_count()) { |
| 260 | _update_tab_titles(); |
| 261 | return; |
| 262 | } |
| 263 | menu_initialized = true; |
| 264 | |
| 265 | if (NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)) { |
| 266 | RID dock_rid = NativeMenu::get_singleton()->get_system_menu(NativeMenu::DOCK_MENU_ID); |
| 267 | NativeMenu::get_singleton()->clear(dock_rid); |
| 268 | } |
| 269 | |
| 270 | scene_tabs->set_block_signals(true); |
| 271 | scene_tabs->set_tab_count(EditorNode::get_editor_data().get_edited_scene_count()); |
| 272 | scene_tabs->set_block_signals(false); |
| 273 | |
| 274 | if (NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)) { |
| 275 | RID dock_rid = NativeMenu::get_singleton()->get_system_menu(NativeMenu::DOCK_MENU_ID); |
| 276 | for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) { |
| 277 | int 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); |
| 278 | scene_tabs->set_tab_metadata(i, global_menu_index); |
| 279 | } |
| 280 | NativeMenu::get_singleton()->add_separator(dock_rid); |
| 281 | NativeMenu::get_singleton()->add_item(dock_rid, TTR("New Window"), callable_mp(this, &EditorSceneTabs::_global_menu_new_window)); |
| 282 | } |
| 283 | |
| 284 | _update_tab_titles(); |
| 285 | } |
| 286 | |
| 287 | void EditorSceneTabs::_update_tab_titles() { |
| 288 | bool show_rb = EDITOR_GET("interface/scene_tabs/show_script_button"); |
| 289 | |
| 290 | // Get all scene names, which may be ambiguous. |
| 291 | Vector<String> disambiguated_scene_names; |
| 292 | Vector<String> full_path_names; |
| 293 | for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) { |
| 294 | disambiguated_scene_names.append(EditorNode::get_editor_data().get_scene_title(i)); |
| 295 | full_path_names.append(EditorNode::get_editor_data().get_scene_path(i)); |
| 296 | } |
| 297 | EditorNode::disambiguate_filenames(full_path_names, disambiguated_scene_names); |
| 298 | |
| 299 | Ref<Texture2D> script_icon = get_editor_theme_icon(SNAME("Script")); |
| 300 | for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) { |
| 301 | Node *type_node = EditorNode::get_editor_data().get_edited_scene_root(i); |
| 302 | Ref<Texture2D> icon; |
| 303 | if (type_node) { |
| 304 | icon = EditorNode::get_singleton()->get_object_icon(type_node); |
| 305 | } |
| 306 | scene_tabs->set_tab_icon(i, icon); |
| 307 | |
| 308 | bool unsaved = EditorUndoRedoManager::get_singleton()->is_history_unsaved(EditorNode::get_editor_data().get_scene_history_id(i)); |
| 309 | scene_tabs->set_tab_title(i, disambiguated_scene_names[i] + (unsaved ? "(*)" : "")); |
| 310 | |
| 311 | if (NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)) { |
| 312 | RID dock_rid = NativeMenu::get_singleton()->get_system_menu(NativeMenu::DOCK_MENU_ID); |
| 313 | int global_menu_index = scene_tabs->get_tab_metadata(i); |
| 314 | NativeMenu::get_singleton()->set_item_text(dock_rid, global_menu_index, EditorNode::get_editor_data().get_scene_title(i) + (unsaved ? "(*)" : "")); |
| 315 | NativeMenu::get_singleton()->set_item_tag(dock_rid, global_menu_index, i); |
| 316 | } |
| 317 | |
| 318 | if (show_rb && EditorNode::get_editor_data().get_scene_root_script(i).is_valid()) { |
| 319 | scene_tabs->set_tab_button_icon(i, script_icon); |
| 320 | } else { |
| 321 | scene_tabs->set_tab_button_icon(i, nullptr); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | int current_tab = EditorNode::get_editor_data().get_edited_scene(); |
| 326 | if (scene_tabs->get_tab_count() > 0 && scene_tabs->get_current_tab() != current_tab) { |
| 327 | scene_tabs->set_block_signals(true); |
| 328 | scene_tabs->set_current_tab(current_tab); |
| 329 | scene_tabs->set_block_signals(false); |
| 330 | } |
| 331 | |
| 332 | _scene_tabs_resized(); |
| 333 | } |
| 334 | |
| 335 | void EditorSceneTabs::_scene_tabs_resized() { |
| 336 | const Size2 add_button_size = Size2(scene_tab_add->get_size().x, scene_tabs->get_size().y); |
| 337 | if (scene_tabs->get_offset_buttons_visible()) { |
| 338 | // Move the add button to a fixed position. |
| 339 | if (scene_tab_add->get_parent() == scene_tabs) { |
| 340 | scene_tabs->remove_child(scene_tab_add); |
| 341 | scene_tab_add_ph->add_child(scene_tab_add); |
| 342 | scene_tab_add->set_rect(Rect2(Point2(), add_button_size)); |
| 343 | } |
| 344 | } else { |
| 345 | // Move the add button to be after the last tab. |
| 346 | if (scene_tab_add->get_parent() == scene_tab_add_ph) { |
| 347 | scene_tab_add_ph->remove_child(scene_tab_add); |
| 348 | scene_tabs->add_child(scene_tab_add); |
| 349 | } |
| 350 | |
| 351 | if (scene_tabs->get_tab_count() == 0) { |
| 352 | scene_tab_add->set_rect(Rect2(Point2(), add_button_size)); |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | Rect2 last_tab = scene_tabs->get_tab_rect(scene_tabs->get_tab_count() - 1); |
| 357 | int hsep = scene_tabs->get_theme_constant(SNAME("h_separation")); |
| 358 | if (scene_tabs->is_layout_rtl()) { |
| 359 | scene_tab_add->set_rect(Rect2(Point2(last_tab.position.x - add_button_size.x - hsep, last_tab.position.y), add_button_size)); |
| 360 | } else { |
| 361 | scene_tab_add->set_rect(Rect2(Point2(last_tab.position.x + last_tab.size.width + hsep, last_tab.position.y), add_button_size)); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | void EditorSceneTabs::_tab_preview_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, int p_tab) { |
| 367 | if (p_preview.is_valid()) { |
| 368 | tab_preview->set_texture(p_preview); |
| 369 | |
| 370 | Rect2 rect = scene_tabs->get_tab_rect(p_tab); |
| 371 | rect.position += scene_tabs->get_global_position(); |
| 372 | tab_preview_panel->set_global_position(rect.position + Vector2(0, rect.size.height)); |
| 373 | tab_preview_panel->show(); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | void EditorSceneTabs::_global_menu_scene(const Variant &p_tag) { |
| 378 | int idx = (int)p_tag; |
| 379 | scene_tabs->set_current_tab(idx); |
| 380 | } |
| 381 | |
| 382 | void EditorSceneTabs::_global_menu_new_window(const Variant &p_tag) { |
| 383 | if (OS::get_singleton()->get_main_loop()) { |
| 384 | List<String> args; |
| 385 | args.push_back("-p"); |
| 386 | OS::get_singleton()->create_instance(args); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | void EditorSceneTabs::shortcut_input(const Ref<InputEvent> &p_event) { |
| 391 | ERR_FAIL_COND(p_event.is_null()); |
| 392 | |
| 393 | Ref<InputEventKey> k = p_event; |
| 394 | if ((k.is_valid() && k->is_pressed() && !k->is_echo()) || Object::cast_to<InputEventShortcut>(*p_event)) { |
| 395 | if (ED_IS_SHORTCUT("editor/next_tab", p_event)) { |
| 396 | int next_tab = EditorNode::get_editor_data().get_edited_scene() + 1; |
| 397 | next_tab %= EditorNode::get_editor_data().get_edited_scene_count(); |
| 398 | _scene_tab_changed(next_tab); |
| 399 | } |
| 400 | if (ED_IS_SHORTCUT("editor/prev_tab", p_event)) { |
| 401 | int next_tab = EditorNode::get_editor_data().get_edited_scene() - 1; |
| 402 | next_tab = next_tab >= 0 ? next_tab : EditorNode::get_editor_data().get_edited_scene_count() - 1; |
| 403 | _scene_tab_changed(next_tab); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | void EditorSceneTabs::add_extra_button(Button *p_button) { |
| 409 | tabbar_container->add_child(p_button); |
| 410 | } |
| 411 | |
| 412 | void EditorSceneTabs::set_current_tab(int p_tab) { |
| 413 | scene_tabs->set_current_tab(p_tab); |
| 414 | } |
| 415 | |
| 416 | int EditorSceneTabs::get_current_tab() const { |
| 417 | return scene_tabs->get_current_tab(); |
| 418 | } |
| 419 | |
| 420 | void EditorSceneTabs::_bind_methods() { |
| 421 | ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab_index"))); |
| 422 | ADD_SIGNAL(MethodInfo("tab_closed", PropertyInfo(Variant::INT, "tab_index"))); |
| 423 | } |
| 424 | |
| 425 | EditorSceneTabs::EditorSceneTabs() { |
| 426 | singleton = this; |
| 427 | |
| 428 | set_process_shortcut_input(true); |
| 429 | set_process_unhandled_key_input(true); |
| 430 | |
| 431 | tabbar_panel = memnew(PanelContainer); |
| 432 | add_child(tabbar_panel); |
| 433 | tabbar_container = memnew(HBoxContainer); |
| 434 | tabbar_panel->add_child(tabbar_container); |
| 435 | |
| 436 | scene_tabs = memnew(TabBar); |
| 437 | scene_tabs->set_select_with_rmb(true); |
| 438 | scene_tabs->add_tab("unsaved"); |
| 439 | scene_tabs->set_tab_close_display_policy((TabBar::CloseButtonDisplayPolicy)EDITOR_GET("interface/scene_tabs/display_close_button").operator int()); |
| 440 | scene_tabs->set_max_tab_width(int(EDITOR_GET("interface/scene_tabs/maximum_width")) * EDSCALE); |
| 441 | scene_tabs->set_drag_to_rearrange_enabled(true); |
| 442 | scene_tabs->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 443 | scene_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 444 | tabbar_container->add_child(scene_tabs); |
| 445 | |
| 446 | scene_tabs->connect("tab_changed", callable_mp(this, &EditorSceneTabs::_scene_tab_changed)); |
| 447 | scene_tabs->connect("tab_button_pressed", callable_mp(this, &EditorSceneTabs::_scene_tab_script_edited)); |
| 448 | scene_tabs->connect("tab_close_pressed", callable_mp(this, &EditorSceneTabs::_scene_tab_closed)); |
| 449 | scene_tabs->connect("tab_hovered", callable_mp(this, &EditorSceneTabs::_scene_tab_hovered)); |
| 450 | scene_tabs->connect(SceneStringName(mouse_exited), callable_mp(this, &EditorSceneTabs::_scene_tab_exit)); |
| 451 | scene_tabs->connect(SceneStringName(gui_input), callable_mp(this, &EditorSceneTabs::_scene_tab_input)); |
| 452 | scene_tabs->connect("active_tab_rearranged", callable_mp(this, &EditorSceneTabs::_reposition_active_tab)); |
| 453 | scene_tabs->connect(SceneStringName(resized), callable_mp(this, &EditorSceneTabs::_scene_tabs_resized), CONNECT_DEFERRED); |
| 454 | |
| 455 | scene_tabs_context_menu = memnew(PopupMenu); |
| 456 | tabbar_container->add_child(scene_tabs_context_menu); |
| 457 | scene_tabs_context_menu->connect(SceneStringName(id_pressed), callable_mp(EditorNode::get_singleton(), &EditorNode::trigger_menu_option).bind(false)); |
| 458 | scene_tabs_context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorSceneTabs::_custom_menu_option)); |
| 459 | |
| 460 | scene_tab_add = memnew(Button); |
| 461 | scene_tab_add->set_flat(true); |
| 462 | scene_tab_add->set_tooltip_text(TTR("Add a new scene.")); |
| 463 | scene_tabs->add_child(scene_tab_add); |
| 464 | scene_tab_add->connect(SceneStringName(pressed), callable_mp(EditorNode::get_singleton(), &EditorNode::trigger_menu_option).bind(EditorNode::SCENE_NEW_SCENE, false)); |
| 465 | |
| 466 | scene_tab_add_ph = memnew(Control); |
| 467 | scene_tab_add_ph->set_mouse_filter(Control::MOUSE_FILTER_IGNORE); |
| 468 | scene_tab_add_ph->set_custom_minimum_size(scene_tab_add->get_minimum_size()); |
| 469 | tabbar_container->add_child(scene_tab_add_ph); |
| 470 | |
| 471 | scene_list = memnew(MenuButton); |
| 472 | scene_list->set_flat(false); |
| 473 | scene_list->set_theme_type_variation("FlatMenuButton"); |
| 474 | scene_list->set_accessibility_name(TTRC("Show Opened Scenes List")); |
| 475 | scene_list->set_shortcut(ED_SHORTCUT("editor/show_opened_scenes_list", TTRC("Show Opened Scenes List"), KeyModifierMask::ALT | Key::T)); |
| 476 | scene_list->get_popup()->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 477 | scene_list->get_popup()->connect("about_to_popup", callable_mp(this, &EditorSceneTabs::_update_scene_list)); |
| 478 | scene_list->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditorSceneTabs::_scene_tab_changed)); |
| 479 | tabbar_container->add_child(scene_list); |
| 480 | |
| 481 | // On-hover tab preview. |
| 482 | |
| 483 | Control *tab_preview_anchor = memnew(Control); |
| 484 | tab_preview_anchor->set_mouse_filter(Control::MOUSE_FILTER_IGNORE); |
| 485 | add_child(tab_preview_anchor); |
| 486 | |
| 487 | tab_preview_panel = memnew(Panel); |
| 488 | tab_preview_panel->set_size(Size2(100, 100) * EDSCALE); |
| 489 | tab_preview_panel->hide(); |
| 490 | tab_preview_panel->set_self_modulate(Color(1, 1, 1, 0.7)); |
| 491 | tab_preview_anchor->add_child(tab_preview_panel); |
| 492 | |
| 493 | tab_preview = memnew(TextureRect); |
| 494 | tab_preview->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED); |
| 495 | tab_preview->set_size(Size2(96, 96) * EDSCALE); |
| 496 | tab_preview->set_position(Point2(2, 2) * EDSCALE); |
| 497 | tab_preview_panel->add_child(tab_preview); |
| 498 | } |