Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/input/test_input_event.cpp
23450 views
1
/**************************************************************************/
2
/* test_input_event.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 "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_input_event)
34
35
#include "core/input/input_event.h"
36
#include "core/input/input_map.h"
37
#include "core/math/rect2.h"
38
#include "core/variant/array.h"
39
#include "tests/signal_watcher.h"
40
41
namespace TestInputEvent {
42
43
TEST_CASE("[InputEvent] Signal is emitted when device is changed") {
44
Ref<InputEventKey> input_event;
45
input_event.instantiate();
46
47
SIGNAL_WATCH(*input_event, CoreStringName(changed));
48
49
Array empty_args = { {} };
50
input_event->set_device(1);
51
52
SIGNAL_CHECK("changed", empty_args);
53
CHECK(input_event->get_device() == 1);
54
55
SIGNAL_UNWATCH(*input_event, CoreStringName(changed));
56
}
57
58
TEST_CASE("[InputEvent] Test accumulate") {
59
Ref<InputEventMouseMotion> iemm1, iemm2;
60
Ref<InputEventKey> iek;
61
62
iemm1.instantiate(), iemm2.instantiate();
63
iek.instantiate();
64
65
iemm1->set_button_mask(MouseButtonMask::LEFT);
66
67
CHECK_FALSE(iemm1->accumulate(iemm2));
68
69
iemm2->set_button_mask(MouseButtonMask::LEFT);
70
71
CHECK(iemm1->accumulate(iemm2));
72
73
CHECK_FALSE(iemm1->accumulate(iek));
74
CHECK_FALSE(iemm2->accumulate(iek));
75
}
76
77
TEST_CASE("[InputEvent][SceneTree] Test methods that interact with the InputMap") {
78
const String mock_action = "mock_action";
79
Ref<InputEventJoypadMotion> iejm;
80
iejm.instantiate();
81
82
InputMap::get_singleton()->add_action(mock_action, 0.5);
83
InputMap::get_singleton()->action_add_event(mock_action, iejm);
84
85
CHECK(iejm->is_action_type());
86
CHECK(iejm->is_action(mock_action));
87
88
CHECK(iejm->is_action_released(mock_action));
89
CHECK(Math::is_equal_approx(iejm->get_action_strength(mock_action), 0.0f));
90
91
iejm->set_axis_value(0.8f);
92
// Since deadzone is 0.5, action_strength grows linearly from 0.5 to 1.0.
93
CHECK(Math::is_equal_approx(iejm->get_action_strength(mock_action), 0.6f));
94
CHECK(Math::is_equal_approx(iejm->get_action_raw_strength(mock_action), 0.8f));
95
CHECK(iejm->is_action_pressed(mock_action));
96
97
InputMap::get_singleton()->erase_action(mock_action);
98
}
99
100
TEST_CASE("[InputEvent] Test xformed_by") {
101
Ref<InputEventMouseMotion> iemm1;
102
iemm1.instantiate();
103
104
iemm1->set_position(Vector2(0.0f, 0.0f));
105
Transform2D transform;
106
transform = transform.translated(Vector2(2.0f, 3.0f));
107
108
Ref<InputEventMouseMotion> iemm2 = iemm1->xformed_by(transform);
109
110
CHECK(iemm2->get_position().is_equal_approx(Vector2(2.0f, 3.0f)));
111
}
112
113
} // namespace TestInputEvent
114
115