Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to add submenus without using names #85477

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions doc/classes/PopupMenu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
If [param allow_echo] is [code]true[/code], the shortcut can be activated with echo events.
</description>
</method>
<method name="add_submenu_item">
<method name="add_submenu_item" deprecated="Prefer using [method add_submenu_node_item] instead.">
<return type="void" />
<param index="0" name="label" type="String" />
<param index="1" name="submenu" type="String" />
Expand All @@ -207,6 +207,17 @@
An [param id] can optionally be provided. If no [param id] is provided, one will be created from the index.
</description>
</method>
<method name="add_submenu_node_item">
<return type="void" />
<param index="0" name="label" type="String" />
<param index="1" name="submenu" type="PopupMenu" />
<param index="2" name="id" type="int" default="-1" />
<description>
Adds an item that will act as a submenu of the parent [PopupMenu] node when clicked. This submenu will be shown when the item is clicked, hovered for long enough, or activated using the [code]ui_select[/code] or [code]ui_right[/code] input actions.
[param submenu] must be either child of this [PopupMenu] or has no parent node (in which case it will be automatically added as a child). If the [param submenu] popup has another parent, this method will fail.
KoBeWi marked this conversation as resolved.
Show resolved Hide resolved
An [param id] can optionally be provided. If no [param id] is provided, one will be created from the index.
</description>
</method>
<method name="clear">
<return type="void" />
<param index="0" name="free_submenus" type="bool" default="false" />
Expand Down Expand Up @@ -304,13 +315,20 @@
Returns the [Shortcut] associated with the item at the given [param index].
</description>
</method>
<method name="get_item_submenu" qualifiers="const">
<method name="get_item_submenu" qualifiers="const" deprecated="Prefer using [method get_item_submenu_node] instead.">
<return type="String" />
<param index="0" name="index" type="int" />
<description>
Returns the submenu name of the item at the given [param index]. See [method add_submenu_item] for more info on how to add a submenu.
</description>
</method>
<method name="get_item_submenu_node" qualifiers="const">
<return type="PopupMenu" />
<param index="0" name="index" type="int" />
<description>
Returns the submenu of the item at the given [param index], or [code]null[/code] if no submenu was added. See [method add_submenu_node_item] for more info on how to add a submenu.
</description>
</method>
<method name="get_item_text" qualifiers="const">
<return type="String" />
<param index="0" name="index" type="int" />
Expand Down Expand Up @@ -545,14 +563,22 @@
Disables the [Shortcut] of the item at the given [param index].
</description>
</method>
<method name="set_item_submenu">
<method name="set_item_submenu" deprecated="Prefer using [method set_item_submenu_node] instead.">
<return type="void" />
<param index="0" name="index" type="int" />
<param index="1" name="submenu" type="String" />
<description>
Sets the submenu of the item at the given [param index]. The submenu is the name of a child [PopupMenu] node that would be shown when the item is clicked.
</description>
</method>
<method name="set_item_submenu_node">
<return type="void" />
<param index="0" name="index" type="int" />
<param index="1" name="submenu" type="PopupMenu" />
<description>
Sets the submenu of the item at the given [param index]. The submenu is a [PopupMenu] node that would be shown when the item is clicked. It must either be a child of this [PopupMenu] or has no parent (in which case it will be automatically added as a child). If the [param submenu] popup has another parent, this method will fail.
</description>
</method>
<method name="set_item_text">
<return type="void" />
<param index="0" name="index" type="int" />
Expand Down
30 changes: 9 additions & 21 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5501,9 +5501,7 @@ void EditorNode::add_tool_menu_item(const String &p_name, const Callable &p_call
void EditorNode::add_tool_submenu_item(const String &p_name, PopupMenu *p_submenu) {
ERR_FAIL_NULL(p_submenu);
ERR_FAIL_COND(p_submenu->get_parent() != nullptr);

tool_menu->add_child(p_submenu);
tool_menu->add_submenu_item(p_name, p_submenu->get_name(), TOOLS_CUSTOM);
tool_menu->add_submenu_node_item(p_name, p_submenu, TOOLS_CUSTOM);
}

void EditorNode::remove_tool_menu_item(const String &p_name) {
Expand Down Expand Up @@ -6807,7 +6805,10 @@ EditorNode::EditorNode() {
file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/new_inherited_scene", TTR("New Inherited Scene..."), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::SHIFT + Key::N), FILE_NEW_INHERITED_SCENE);
file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/open_scene", TTR("Open Scene..."), KeyModifierMask::CMD_OR_CTRL + Key::O), FILE_OPEN_SCENE);
file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/reopen_closed_scene", TTR("Reopen Closed Scene"), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::SHIFT + Key::T), FILE_OPEN_PREV);
file_menu->add_submenu_item(TTR("Open Recent"), "RecentScenes", FILE_OPEN_RECENT);

recent_scenes = memnew(PopupMenu);
file_menu->add_submenu_node_item(TTR("Open Recent"), recent_scenes, FILE_OPEN_RECENT);
recent_scenes->connect("id_pressed", callable_mp(this, &EditorNode::_open_recent_scene));

file_menu->add_separator();
file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/save_scene", TTR("Save Scene"), KeyModifierMask::CMD_OR_CTRL + Key::S), FILE_SAVE_SCENE);
Expand All @@ -6823,9 +6824,7 @@ EditorNode::EditorNode() {

file_menu->add_separator();
export_as_menu = memnew(PopupMenu);
export_as_menu->set_name("Export");
file_menu->add_child(export_as_menu);
file_menu->add_submenu_item(TTR("Export As..."), "Export");
file_menu->add_submenu_node_item(TTR("Export As..."), export_as_menu);
export_as_menu->add_shortcut(ED_SHORTCUT("editor/export_as_mesh_library", TTR("MeshLibrary...")), FILE_EXPORT_MESH_LIBRARY);
export_as_menu->connect("index_pressed", callable_mp(this, &EditorNode::_export_as_menu_option));

Expand All @@ -6837,11 +6836,6 @@ EditorNode::EditorNode() {
file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/reload_saved_scene", TTR("Reload Saved Scene")), EDIT_RELOAD_SAVED_SCENE);
file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/close_scene", TTR("Close Scene"), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::SHIFT + Key::W), FILE_CLOSE);

recent_scenes = memnew(PopupMenu);
recent_scenes->set_name("RecentScenes");
file_menu->add_child(recent_scenes);
recent_scenes->connect("id_pressed", callable_mp(this, &EditorNode::_open_recent_scene));

if (!global_menu || !OS::get_singleton()->has_feature("macos")) {
// On macOS "Quit" and "About" options are in the "app" menu.
file_menu->add_separator();
Expand Down Expand Up @@ -6884,10 +6878,8 @@ EditorNode::EditorNode() {
project_menu->add_separator();

tool_menu = memnew(PopupMenu);
tool_menu->set_name("Tools");
tool_menu->connect("index_pressed", callable_mp(this, &EditorNode::_tool_menu_option));
project_menu->add_child(tool_menu);
project_menu->add_submenu_item(TTR("Tools"), "Tools");
project_menu->add_submenu_node_item(TTR("Tools"), tool_menu);
tool_menu->add_item(TTR("Orphan Resource Explorer..."), TOOLS_ORPHAN_RESOURCES);
tool_menu->add_item(TTR("Upgrade Mesh Surfaces..."), TOOLS_SURFACE_UPGRADE);

Expand Down Expand Up @@ -6938,11 +6930,9 @@ EditorNode::EditorNode() {
settings_menu->add_separator();

editor_layouts = memnew(PopupMenu);
editor_layouts->set_name("Layouts");
editor_layouts->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
settings_menu->add_child(editor_layouts);
settings_menu->add_submenu_node_item(TTR("Editor Layout"), editor_layouts);
editor_layouts->connect("id_pressed", callable_mp(this, &EditorNode::_layout_menu_option));
settings_menu->add_submenu_item(TTR("Editor Layout"), "Layouts");
settings_menu->add_separator();

ED_SHORTCUT_AND_COMMAND("editor/take_screenshot", TTR("Take Screenshot"), KeyModifierMask::CTRL | Key::F12);
Expand Down Expand Up @@ -7370,12 +7360,10 @@ EditorNode::EditorNode() {
add_editor_plugin(VersionControlEditorPlugin::get_singleton());

vcs_actions_menu = VersionControlEditorPlugin::get_singleton()->get_version_control_actions_panel();
vcs_actions_menu->set_name("Version Control");
vcs_actions_menu->connect("index_pressed", callable_mp(this, &EditorNode::_version_control_menu_option));
vcs_actions_menu->add_item(TTR("Create Version Control Metadata..."), RUN_VCS_METADATA);
vcs_actions_menu->add_item(TTR("Version Control Settings..."), RUN_VCS_SETTINGS);
project_menu->add_child(vcs_actions_menu);
project_menu->set_item_submenu(project_menu->get_item_index(VCS_MENU), "Version Control");
project_menu->set_item_submenu_node(project_menu->get_item_index(VCS_MENU), vcs_actions_menu);

add_editor_plugin(memnew(AudioBusesEditorPlugin(audio_bus_editor)));

Expand Down
8 changes: 2 additions & 6 deletions editor/filesystem_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3052,11 +3052,9 @@ void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, Vector<Str

if (p_paths.size() == 1 && p_display_path_dependent_options) {
PopupMenu *new_menu = memnew(PopupMenu);
new_menu->set_name("New");
new_menu->connect("id_pressed", callable_mp(this, &FileSystemDock::_tree_rmb_option));

p_popup->add_child(new_menu);
p_popup->add_submenu_item(TTR("Create New"), "New", FILE_NEW);
p_popup->add_submenu_node_item(TTR("Create New"), new_menu, FILE_NEW);
p_popup->set_item_icon(p_popup->get_item_index(FILE_NEW), get_editor_theme_icon(SNAME("Add")));

new_menu->add_icon_item(get_editor_theme_icon(SNAME("Folder")), TTR("Folder..."), FILE_NEW_FOLDER);
Expand All @@ -3079,11 +3077,9 @@ void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, Vector<Str

if (p_paths[0] != "res://") {
PopupMenu *folder_colors_menu = memnew(PopupMenu);
folder_colors_menu->set_name("FolderColor");
folder_colors_menu->connect("id_pressed", callable_mp(this, &FileSystemDock::_folder_color_index_pressed).bind(folder_colors_menu));

p_popup->add_child(folder_colors_menu);
p_popup->add_submenu_item(TTR("Set Folder Color..."), "FolderColor");
p_popup->add_submenu_node_item(TTR("Set Folder Color..."), folder_colors_menu);
p_popup->set_item_icon(-1, get_editor_theme_icon(SNAME("Paint")));

folder_colors_menu->add_icon_item(get_editor_theme_icon(SNAME("Folder")), TTR("Default (Reset)"));
Expand Down
5 changes: 2 additions & 3 deletions editor/plugins/animation_blend_space_1d_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void AnimationNodeBlendSpace1DEditor::_blend_space_gui_input(const Ref<InputEven
ClassDB::get_inheriters_from_class("AnimationRootNode", &classes);
classes.sort_custom<StringName::AlphCompare>();

menu->add_submenu_item(TTR("Add Animation"), "AddAnimations");
menu->add_submenu_node_item(TTR("Add Animation"), animations_menu);

List<StringName> names;
tree->get_animation_list(&names);
Expand Down Expand Up @@ -802,9 +802,8 @@ AnimationNodeBlendSpace1DEditor::AnimationNodeBlendSpace1DEditor() {
menu->connect("id_pressed", callable_mp(this, &AnimationNodeBlendSpace1DEditor::_add_menu_type));

animations_menu = memnew(PopupMenu);
menu->add_child(animations_menu);
animations_menu->set_name("AddAnimations");
animations_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
menu->add_child(animations_menu);
animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeBlendSpace1DEditor::_add_animation_type));

open_file = memnew(EditorFileDialog);
Expand Down
5 changes: 2 additions & 3 deletions editor/plugins/animation_blend_space_2d_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void AnimationNodeBlendSpace2DEditor::_blend_space_gui_input(const Ref<InputEven
classes.sort_custom<StringName::AlphCompare>();

ClassDB::get_inheriters_from_class("AnimationRootNode", &classes);
menu->add_submenu_item(TTR("Add Animation"), "AddAnimations");
menu->add_submenu_node_item(TTR("Add Animation"), animations_menu);

List<StringName> names;
tree->get_animation_list(&names);
Expand Down Expand Up @@ -1081,9 +1081,8 @@ AnimationNodeBlendSpace2DEditor::AnimationNodeBlendSpace2DEditor() {
menu->connect("id_pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_add_menu_type));

animations_menu = memnew(PopupMenu);
menu->add_child(animations_menu);
animations_menu->set_name("AddAnimations");
animations_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
menu->add_child(animations_menu);
animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeBlendSpace2DEditor::_add_animation_type));

open_file = memnew(EditorFileDialog);
Expand Down
5 changes: 2 additions & 3 deletions editor/plugins/animation_state_machine_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ void AnimationNodeStateMachineEditor::_open_menu(const Vector2 &p_position) {

List<StringName> animation_names;
tree->get_animation_list(&animation_names);
menu->add_submenu_item(TTR("Add Animation"), "AddAnimations");
menu->add_submenu_node_item(TTR("Add Animation"), animations_menu);
if (animation_names.is_empty()) {
menu->set_item_disabled(menu->get_item_idx_from_text(TTR("Add Animation")), true);
} else {
Expand Down Expand Up @@ -1777,9 +1777,8 @@ AnimationNodeStateMachineEditor::AnimationNodeStateMachineEditor() {
menu->connect("popup_hide", callable_mp(this, &AnimationNodeStateMachineEditor::_stop_connecting));

animations_menu = memnew(PopupMenu);
menu->add_child(animations_menu);
animations_menu->set_name("AddAnimations");
animations_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
menu->add_child(animations_menu);
animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_add_animation_type));

connect_menu = memnew(PopupMenu);
Expand Down
18 changes: 6 additions & 12 deletions editor/plugins/canvas_item_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5319,14 +5319,8 @@ CanvasItemEditor::CanvasItemEditor() {
p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/use_scale_snap", TTR("Use Scale Snap")), SNAP_USE_SCALE);
p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_relative", TTR("Snap Relative")), SNAP_RELATIVE);
p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/use_pixel_snap", TTR("Use Pixel Snap")), SNAP_USE_PIXEL);
p->add_submenu_item(TTR("Smart Snapping"), "SmartSnapping");

p->add_separator();
p->add_shortcut(ED_SHORTCUT("canvas_item_editor/configure_snap", TTR("Configure Snap...")), SNAP_CONFIGURE);

smartsnap_config_popup = memnew(PopupMenu);
p->add_child(smartsnap_config_popup);
smartsnap_config_popup->set_name("SmartSnapping");
smartsnap_config_popup->connect("id_pressed", callable_mp(this, &CanvasItemEditor::_popup_callback));
smartsnap_config_popup->set_hide_on_checkable_item_selection(false);
smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_node_parent", TTR("Snap to Parent")), SNAP_USE_NODE_PARENT);
Expand All @@ -5335,6 +5329,10 @@ CanvasItemEditor::CanvasItemEditor() {
smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_node_center", TTR("Snap to Node Center")), SNAP_USE_NODE_CENTER);
smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_other_nodes", TTR("Snap to Other Nodes")), SNAP_USE_OTHER_NODES);
smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_guides", TTR("Snap to Guides")), SNAP_USE_GUIDES);
p->add_submenu_node_item(TTR("Smart Snapping"), smartsnap_config_popup);

p->add_separator();
p->add_shortcut(ED_SHORTCUT("canvas_item_editor/configure_snap", TTR("Configure Snap...")), SNAP_CONFIGURE);

main_menu_hbox->add_child(memnew(VSeparator));

Expand Down Expand Up @@ -5416,14 +5414,12 @@ CanvasItemEditor::CanvasItemEditor() {
grid_menu = memnew(PopupMenu);
grid_menu->connect("about_to_popup", callable_mp(this, &CanvasItemEditor::_prepare_grid_menu));
grid_menu->connect("id_pressed", callable_mp(this, &CanvasItemEditor::_on_grid_menu_id_pressed));
grid_menu->set_name("GridMenu");
grid_menu->add_radio_check_item(TTR("Show"), GRID_VISIBILITY_SHOW);
grid_menu->add_radio_check_item(TTR("Show When Snapping"), GRID_VISIBILITY_SHOW_WHEN_SNAPPING);
grid_menu->add_radio_check_item(TTR("Hide"), GRID_VISIBILITY_HIDE);
grid_menu->add_separator();
grid_menu->add_shortcut(ED_SHORTCUT("canvas_item_editor/toggle_grid", TTR("Toggle Grid"), KeyModifierMask::CMD_OR_CTRL | Key::APOSTROPHE));
p->add_child(grid_menu);
p->add_submenu_item(TTR("Grid"), "GridMenu");
p->add_submenu_node_item(TTR("Grid"), grid_menu);

p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_helpers", TTR("Show Helpers"), Key::H), SHOW_HELPERS);
p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_rulers", TTR("Show Rulers")), SHOW_RULERS);
Expand Down Expand Up @@ -5452,12 +5448,10 @@ CanvasItemEditor::CanvasItemEditor() {

theme_menu = memnew(PopupMenu);
theme_menu->connect("id_pressed", callable_mp(this, &CanvasItemEditor::_switch_theme_preview));
theme_menu->set_name("ThemeMenu");
theme_menu->add_radio_check_item(TTR("Project theme"), THEME_PREVIEW_PROJECT);
theme_menu->add_radio_check_item(TTR("Editor theme"), THEME_PREVIEW_EDITOR);
theme_menu->add_radio_check_item(TTR("Default theme"), THEME_PREVIEW_DEFAULT);
p->add_child(theme_menu);
p->add_submenu_item(TTR("Preview Theme"), "ThemeMenu");
p->add_submenu_node_item(TTR("Preview Theme"), theme_menu);

theme_preview = (ThemePreviewMode)(int)EditorSettings::get_singleton()->get_project_metadata("2d_editor", "theme_preview", THEME_PREVIEW_PROJECT);
for (int i = 0; i < THEME_PREVIEW_MAX; i++) {
Expand Down
3 changes: 1 addition & 2 deletions editor/plugins/font_config_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ void EditorPropertyOTFeatures::update_property() {
}
for (int i = 0; i < FGRP_MAX; i++) {
if (have_sub[i]) {
menu->add_submenu_item(RTR(group_names[i]), "FTRMenu_" + itos(i));
menu->add_submenu_node_item(RTR(group_names[i]), menu_sub[i]);
}
}

Expand Down Expand Up @@ -851,7 +851,6 @@ EditorPropertyOTFeatures::EditorPropertyOTFeatures() {

for (int i = 0; i < FGRP_MAX; i++) {
menu_sub[i] = memnew(PopupMenu);
menu_sub[i]->set_name("FTRMenu_" + itos(i));
menu->add_child(menu_sub[i]);
menu_sub[i]->connect("id_pressed", callable_mp(this, &EditorPropertyOTFeatures::_add_feature));
}
Expand Down
Loading
Loading