Skip to content

Commit

Permalink
Refactor disabling scene tab context menu options
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi committed Sep 1, 2023
1 parent 80bfca2 commit a949f1b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
33 changes: 24 additions & 9 deletions editor/gui/editor_scene_tabs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,38 +142,53 @@ void EditorSceneTabs::_update_context_menu() {
scene_tabs_context_menu->reset_size();

int tab_id = scene_tabs->get_hovered_tab();
bool no_root_node = !EditorNode::get_editor_data().get_edited_scene_root(tab_id);

scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/new_scene"), EditorNode::FILE_NEW_SCENE);
if (tab_id >= 0) {
scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_scene"), EditorNode::FILE_SAVE_SCENE);
_disable_menu_option_if(EditorNode::FILE_SAVE_SCENE, no_root_node);
scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_scene_as"), EditorNode::FILE_SAVE_AS_SCENE);
_disable_menu_option_if(EditorNode::FILE_SAVE_AS_SCENE, no_root_node);
}

scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_all_scenes"), EditorNode::FILE_SAVE_ALL_SCENES);
bool can_save_all_scenes = false;
for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); i++) {
if (!EditorNode::get_editor_data().get_scene_path(i).is_empty() && EditorNode::get_editor_data().get_edited_scene_root(i)) {
can_save_all_scenes = true;
break;
}
}
_disable_menu_option_if(EditorNode::FILE_SAVE_ALL_SCENES, !can_save_all_scenes);

if (tab_id >= 0) {
scene_tabs_context_menu->add_separator();
scene_tabs_context_menu->add_item(TTR("Show in FileSystem"), EditorNode::FILE_SHOW_IN_FILESYSTEM);
_disable_menu_option_if(EditorNode::FILE_SHOW_IN_FILESYSTEM, !ResourceLoader::exists(EditorNode::get_editor_data().get_scene_path(tab_id)));
scene_tabs_context_menu->add_item(TTR("Play This Scene"), EditorNode::FILE_RUN_SCENE);
_disable_menu_option_if(EditorNode::FILE_RUN_SCENE, no_root_node);

scene_tabs_context_menu->add_separator();
scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/close_scene"), EditorNode::FILE_CLOSE);
scene_tabs_context_menu->set_item_text(scene_tabs_context_menu->get_item_index(EditorNode::FILE_CLOSE), TTR("Close Tab"));
scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/reopen_closed_scene"), EditorNode::FILE_OPEN_PREV);
scene_tabs_context_menu->set_item_text(scene_tabs_context_menu->get_item_index(EditorNode::FILE_OPEN_PREV), TTR("Undo Close Tab"));
if (!EditorNode::get_singleton()->has_previous_scenes()) {
scene_tabs_context_menu->set_item_disabled(scene_tabs_context_menu->get_item_index(EditorNode::FILE_OPEN_PREV), true);
}
_disable_menu_option_if(EditorNode::FILE_OPEN_PREV, !EditorNode::get_singleton()->has_previous_scenes());
scene_tabs_context_menu->add_item(TTR("Close Other Tabs"), EditorNode::FILE_CLOSE_OTHERS);
if (EditorNode::get_editor_data().get_edited_scene_count() <= 1) {
scene_tabs_context_menu->set_item_disabled(scene_tabs_context_menu->get_item_index(EditorNode::FILE_CLOSE_OTHERS), true);
}
_disable_menu_option_if(EditorNode::FILE_CLOSE_OTHERS, EditorNode::get_editor_data().get_edited_scene_count() <= 1);
scene_tabs_context_menu->add_item(TTR("Close Tabs to the Right"), EditorNode::FILE_CLOSE_RIGHT);
if (EditorNode::get_editor_data().get_edited_scene_count() == tab_id + 1) {
scene_tabs_context_menu->set_item_disabled(scene_tabs_context_menu->get_item_index(EditorNode::FILE_CLOSE_RIGHT), true);
}
_disable_menu_option_if(EditorNode::FILE_CLOSE_RIGHT, EditorNode::get_editor_data().get_edited_scene_count() == tab_id + 1);
scene_tabs_context_menu->add_item(TTR("Close All Tabs"), EditorNode::FILE_CLOSE_ALL);
}
}

void EditorSceneTabs::_disable_menu_option_if(int p_option, bool p_condition) {
if (p_condition) {
scene_tabs_context_menu->set_item_disabled(scene_tabs_context_menu->get_item_index(p_option), true);
}
}

// TODO: This REALLY should be done in a better way than replacing all tabs after almost EVERY action.
void EditorSceneTabs::update_scene_tabs() {
tab_preview_panel->hide();
Expand Down
1 change: 1 addition & 0 deletions editor/gui/editor_scene_tabs.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class EditorSceneTabs : public MarginContainer {

void _reposition_active_tab(int p_to_index);
void _update_context_menu();
void _disable_menu_option_if(int p_option, bool p_condition);

void _tab_preview_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata);

Expand Down

0 comments on commit a949f1b

Please sign in to comment.