Skip to content

Commit

Permalink
Merge pull request #20609 from YeldhamDev/menu_hover_explicit
Browse files Browse the repository at this point in the history
Make opening menus with the same parent on mouse focus explicit
  • Loading branch information
akien-mga authored Dec 9, 2018
2 parents 3a312db + da7d3c5 commit bf59b73
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 10 deletions.
5 changes: 5 additions & 0 deletions doc/classes/MenuButton.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
</description>
</method>
</methods>
<members>
<member name="switch_on_hover" type="bool" setter="set_switch_on_hover" getter="is_switch_on_hover">
If [code]true[/code], when the cursor hovers above another MenuButton within the same parent which also has [code]switch_on_hover[/code] enabled, it will close the current MenuButton and open the other one.
</member>
</members>
<signals>
<signal name="about_to_show">
<description>
Expand Down
5 changes: 5 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5189,6 +5189,7 @@ EditorNode::EditorNode() {

file_menu = memnew(MenuButton);
file_menu->set_flat(false);
file_menu->set_switch_on_hover(true);
file_menu->set_text(TTR("Scene"));
file_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
left_menu_hb->add_child(file_menu);
Expand Down Expand Up @@ -5280,6 +5281,7 @@ EditorNode::EditorNode() {

project_menu = memnew(MenuButton);
project_menu->set_flat(false);
project_menu->set_switch_on_hover(true);
project_menu->set_tooltip(TTR("Miscellaneous project or scene-wide tools."));
project_menu->set_text(TTR("Project"));
project_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
Expand Down Expand Up @@ -5321,6 +5323,7 @@ EditorNode::EditorNode() {

debug_menu = memnew(MenuButton);
debug_menu->set_flat(false);
debug_menu->set_switch_on_hover(true);
debug_menu->set_text(TTR("Debug"));
debug_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
left_menu_hb->add_child(debug_menu);
Expand Down Expand Up @@ -5351,6 +5354,7 @@ EditorNode::EditorNode() {

settings_menu = memnew(MenuButton);
settings_menu->set_flat(false);
settings_menu->set_switch_on_hover(true);
settings_menu->set_text(TTR("Editor"));
settings_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
left_menu_hb->add_child(settings_menu);
Expand Down Expand Up @@ -5387,6 +5391,7 @@ EditorNode::EditorNode() {
// Help Menu
help_menu = memnew(MenuButton);
help_menu->set_flat(false);
help_menu->set_switch_on_hover(true);
help_menu->set_text(TTR("Help"));
help_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
left_menu_hb->add_child(help_menu);
Expand Down
3 changes: 3 additions & 0 deletions editor/plugins/script_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2977,6 +2977,7 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) {
file_menu = memnew(MenuButton);
menu_hb->add_child(file_menu);
file_menu->set_text(TTR("File"));
file_menu->set_switch_on_hover(true);
file_menu->get_popup()->set_hide_on_window_lose_focus(true);
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new", TTR("New Script...")), FILE_NEW);
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new_textfile", TTR("New TextFile...")), FILE_NEW_TEXTFILE);
Expand Down Expand Up @@ -3029,6 +3030,7 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) {
script_search_menu = memnew(MenuButton);
menu_hb->add_child(script_search_menu);
script_search_menu->set_text(TTR("Search"));
script_search_menu->set_switch_on_hover(true);
script_search_menu->get_popup()->set_hide_on_window_lose_focus(true);
script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find..."), KEY_MASK_CMD | KEY_F), HELP_SEARCH_FIND);
script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), KEY_F3), HELP_SEARCH_FIND_NEXT);
Expand All @@ -3038,6 +3040,7 @@ ScriptEditor::ScriptEditor(EditorNode *p_editor) {
debug_menu = memnew(MenuButton);
menu_hb->add_child(debug_menu);
debug_menu->set_text(TTR("Debug"));
debug_menu->set_switch_on_hover(true);
debug_menu->get_popup()->set_hide_on_window_lose_focus(true);
debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_over", TTR("Step Over"), KEY_F10), DEBUG_NEXT);
debug_menu->get_popup()->add_shortcut(ED_SHORTCUT("debugger/step_into", TTR("Step Into"), KEY_F11), DEBUG_STEP);
Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/script_text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,7 @@ ScriptTextEditor::ScriptTextEditor() {

edit_menu = memnew(MenuButton);
edit_menu->set_text(TTR("Edit"));
edit_menu->set_switch_on_hover(true);
edit_menu->get_popup()->set_hide_on_window_lose_focus(true);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
Expand Down Expand Up @@ -1522,6 +1523,7 @@ ScriptTextEditor::ScriptTextEditor() {
search_menu = memnew(MenuButton);
edit_hb->add_child(search_menu);
search_menu->set_text(TTR("Search"));
search_menu->set_switch_on_hover(true);
search_menu->get_popup()->set_hide_on_window_lose_focus(true);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
Expand Down
6 changes: 2 additions & 4 deletions editor/plugins/shader_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,8 @@ ShaderEditor::ShaderEditor(EditorNode *p_node) {
HBoxContainer *hbc = memnew(HBoxContainer);

edit_menu = memnew(MenuButton);
//edit_menu->set_position(Point2(5, -1));
edit_menu->set_text(TTR("Edit"));

edit_menu->set_switch_on_hover(true);
edit_menu->get_popup()->set_hide_on_window_lose_focus(true);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
Expand All @@ -556,12 +555,11 @@ ShaderEditor::ShaderEditor(EditorNode *p_node) {
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);

edit_menu->get_popup()->connect("id_pressed", this, "_menu_option");

search_menu = memnew(MenuButton);
//search_menu->set_position(Point2(38, -1));
search_menu->set_text(TTR("Search"));
search_menu->set_switch_on_hover(true);
search_menu->get_popup()->set_hide_on_window_lose_focus(true);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/spatial_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5475,6 +5475,7 @@ SpatialEditor::SpatialEditor(EditorNode *p_editor) {

transform_menu = memnew(MenuButton);
transform_menu->set_text(TTR("Transform"));
transform_menu->set_switch_on_hover(true);
hbc_menu->add_child(transform_menu);

p = transform_menu->get_popup();
Expand All @@ -5487,6 +5488,7 @@ SpatialEditor::SpatialEditor(EditorNode *p_editor) {

view_menu = memnew(MenuButton);
view_menu->set_text(TTR("View"));
view_menu->set_switch_on_hover(true);
hbc_menu->add_child(view_menu);

p = view_menu->get_popup();
Expand Down
1 change: 1 addition & 0 deletions modules/visual_script/visual_script_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3491,6 +3491,7 @@ VisualScriptEditor::VisualScriptEditor() {

edit_menu = memnew(MenuButton);
edit_menu->set_text(TTR("Edit"));
edit_menu->set_switch_on_hover(true);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/delete_selected"), EDIT_DELETE_NODES);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/toggle_breakpoint"), EDIT_TOGGLE_BREAKPOINT);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/find_node_type"), EDIT_FIND_NODE_TYPE);
Expand Down
19 changes: 17 additions & 2 deletions scene/gui/menu_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,24 @@ PopupMenu *MenuButton::get_popup() const {
return popup;
}

void MenuButton::_set_items(const Array &p_items) {

popup->set("items", p_items);
}

Array MenuButton::_get_items() const {

return popup->get("items");
}
void MenuButton::_set_items(const Array &p_items) {

popup->set("items", p_items);
void MenuButton::set_switch_on_hover(bool p_enabled) {

switch_on_hover = p_enabled;
}

bool MenuButton::is_switch_on_hover() {

return switch_on_hover;
}

void MenuButton::_bind_methods() {
Expand All @@ -86,9 +97,12 @@ void MenuButton::_bind_methods() {
ClassDB::bind_method(D_METHOD("_unhandled_key_input"), &MenuButton::_unhandled_key_input);
ClassDB::bind_method(D_METHOD("_set_items"), &MenuButton::_set_items);
ClassDB::bind_method(D_METHOD("_get_items"), &MenuButton::_get_items);
ClassDB::bind_method(D_METHOD("set_switch_on_hover", "enable"), &MenuButton::set_switch_on_hover);
ClassDB::bind_method(D_METHOD("is_switch_on_hover"), &MenuButton::is_switch_on_hover);
ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &MenuButton::set_disable_shortcuts);

ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_items", "_get_items");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_hover"), "set_switch_on_hover", "is_switch_on_hover");

ADD_SIGNAL(MethodInfo("about_to_show"));
}
Expand All @@ -100,6 +114,7 @@ void MenuButton::set_disable_shortcuts(bool p_disabled) {

MenuButton::MenuButton() {

switch_on_hover = false;
set_flat(true);
set_disable_shortcuts(false);
set_enabled_focus_mode(FOCUS_NONE);
Expand Down
3 changes: 3 additions & 0 deletions scene/gui/menu_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class MenuButton : public Button {
GDCLASS(MenuButton, Button);

bool clicked;
bool switch_on_hover;
bool disable_shortcuts;
PopupMenu *popup;

Expand All @@ -57,6 +58,8 @@ class MenuButton : public Button {
virtual void pressed();

PopupMenu *get_popup() const;
void set_switch_on_hover(bool p_enabled);
bool is_switch_on_hover();
void set_disable_shortcuts(bool p_disabled);

MenuButton();
Expand Down
7 changes: 3 additions & 4 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1955,10 +1955,9 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) {

// If the mouse is over a menu button, this menu will open automatically
// if there is already a pop-up menu open at the same hierarchical level.
if (popup_menu_parent && menu_button &&
popup_menu_parent->get_icon().is_null() &&
menu_button->get_icon().is_null() &&
(popup_menu->get_parent()->get_parent()->is_a_parent_of(menu_button) ||
if (popup_menu_parent && menu_button && popup_menu_parent->is_switch_on_hover() &&
!menu_button->is_disabled() && menu_button->is_switch_on_hover() &&
(popup_menu_parent->get_parent()->is_a_parent_of(menu_button) ||
menu_button->get_parent()->is_a_parent_of(popup_menu))) {

popup_menu->notification(Control::NOTIFICATION_MODAL_CLOSE);
Expand Down

0 comments on commit bf59b73

Please sign in to comment.