Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/display_server_mock.h
9887 views
1
/**************************************************************************/
2
/* display_server_mock.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 "servers/display_server_headless.h"
34
35
#include "servers/rendering/dummy/rasterizer_dummy.h"
36
37
// Specialized DisplayServer for unittests based on DisplayServerHeadless, that
38
// additionally supports things like mouse enter/exit events and clipboard.
39
class DisplayServerMock : public DisplayServerHeadless {
40
GDSOFTCLASS(DisplayServerMock, DisplayServerHeadless);
41
42
private:
43
friend class DisplayServer;
44
45
Point2i mouse_position = Point2i(-1, -1); // Outside of Window.
46
CursorShape cursor_shape = CursorShape::CURSOR_ARROW;
47
bool window_over = false;
48
Callable event_callback;
49
50
String clipboard_text;
51
String primary_clipboard_text;
52
53
static Vector<String> get_rendering_drivers_func() {
54
Vector<String> drivers;
55
drivers.push_back("dummy");
56
return drivers;
57
}
58
59
static DisplayServer *create_func(const String &p_rendering_driver, DisplayServer::WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) {
60
r_error = OK;
61
RasterizerDummy::make_current();
62
return memnew(DisplayServerMock());
63
}
64
65
void _set_mouse_position(const Point2i &p_position) {
66
if (mouse_position == p_position) {
67
return;
68
}
69
mouse_position = p_position;
70
_set_window_over(Rect2i(Point2i(0, 0), window_get_size()).has_point(p_position));
71
}
72
73
void _set_window_over(bool p_over) {
74
if (p_over == window_over) {
75
return;
76
}
77
window_over = p_over;
78
_send_window_event(p_over ? WINDOW_EVENT_MOUSE_ENTER : WINDOW_EVENT_MOUSE_EXIT);
79
}
80
81
void _send_window_event(WindowEvent p_event) {
82
if (event_callback.is_valid()) {
83
Variant event = int(p_event);
84
event_callback.call(event);
85
}
86
}
87
88
public:
89
bool has_feature(Feature p_feature) const override {
90
switch (p_feature) {
91
case FEATURE_MOUSE:
92
case FEATURE_CURSOR_SHAPE:
93
case FEATURE_CLIPBOARD:
94
case FEATURE_CLIPBOARD_PRIMARY:
95
return true;
96
default: {
97
}
98
}
99
return false;
100
}
101
102
String get_name() const override { return "mock"; }
103
104
// You can simulate DisplayServer-events by calling this function.
105
// The events will be delivered to Godot's Input-system.
106
// Mouse-events (Button & Motion) will additionally update the DisplayServer's mouse position.
107
// For Mouse motion events, the `relative`-property is set based on the distance to the previous mouse position.
108
void simulate_event(Ref<InputEvent> p_event) {
109
Ref<InputEvent> event = p_event;
110
Ref<InputEventMouse> me = p_event;
111
if (me.is_valid()) {
112
Ref<InputEventMouseMotion> mm = p_event;
113
if (mm.is_valid()) {
114
mm->set_relative(mm->get_position() - mouse_position);
115
event = mm;
116
}
117
_set_mouse_position(me->get_position());
118
}
119
Input::get_singleton()->parse_input_event(event);
120
}
121
122
// Returns the current cursor shape.
123
CursorShape get_cursor_shape() {
124
return cursor_shape;
125
}
126
127
virtual Point2i mouse_get_position() const override { return mouse_position; }
128
129
virtual void clipboard_set(const String &p_text) override { clipboard_text = p_text; }
130
virtual String clipboard_get() const override { return clipboard_text; }
131
virtual void clipboard_set_primary(const String &p_text) override { primary_clipboard_text = p_text; }
132
virtual String clipboard_get_primary() const override { return primary_clipboard_text; }
133
134
virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override {
135
return Size2i(1920, 1080);
136
}
137
138
virtual void cursor_set_shape(CursorShape p_shape) override {
139
cursor_shape = p_shape;
140
}
141
142
virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override {
143
event_callback = p_callable;
144
}
145
146
static void register_mock_driver() {
147
register_create_function("mock", create_func, get_rendering_drivers_func);
148
}
149
};
150
151