Skip to content

Commit

Permalink
Allow the ui_menu action to trigger context menus in Tree
Browse files Browse the repository at this point in the history
My right mouse button is dying. Not gonna replace it yet. :)

Added a test for this one feature, though no other Tree tests exist.
  • Loading branch information
eswartz committed Feb 19, 2024
1 parent ab4c5a5 commit 9301911
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions scene/gui/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3534,6 +3534,10 @@ void Tree::gui_input(const Ref<InputEvent> &p_event) {
}
}
accept_event();
} else if (p_event->is_action("ui_menu") && p_event->is_pressed()) {
// Show the context menu.
emit_signal(SNAME("item_mouse_selected"), get_local_mouse_position(), MouseButton::RIGHT);
accept_event();
}

if (allow_search && k.is_valid()) { // Incremental search
Expand Down
72 changes: 72 additions & 0 deletions tests/scene/test_tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**************************************************************************/
/* test_tree.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef TEST_TREE_H
#define TEST_TREE_H

#include "scene/gui/tree.h"

#include "tests/test_macros.h"

namespace TestTree {

TEST_CASE("[SceneTree][Tree] context menu detection") {
Tree *tree = memnew(Tree);
SceneTree::get_singleton()->get_root()->add_child(tree);
tree->grab_focus();

SUBCASE("[Tree] menu activation on empty") {
SIGNAL_WATCH(tree, "item_mouse_selected");

Ref<InputEventKey> event = memnew(InputEventKey);
event->set_keycode(Key::MENU);
event->set_pressed(true);
Input::get_singleton()->parse_input_event(event);

MessageQueue::get_singleton()->flush();

Vector2 mouse_pos = tree->get_local_mouse_position();
Array selection_args;
selection_args.push_back(mouse_pos);
selection_args.push_back(MouseButton::RIGHT);

Array test_args;
test_args.push_back(selection_args);

SIGNAL_CHECK("item_mouse_selected", test_args);

SIGNAL_UNWATCH(tree, "item_mouse_selected");
memdelete(tree);
}
}

} // namespace TestTree

#endif // TEST_TREE_H
1 change: 1 addition & 0 deletions tests/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
#include "tests/scene/test_path_2d.h"
#include "tests/scene/test_primitives.h"
#include "tests/scene/test_sprite_frames.h"
#include "tests/scene/test_tree.h"
#include "tests/scene/test_text_edit.h"
#include "tests/scene/test_theme.h"
#include "tests/scene/test_viewport.h"
Expand Down

0 comments on commit 9301911

Please sign in to comment.