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