display_server_mock.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #ifndef DISPLAY_SERVER_MOCK_H
  31. #define DISPLAY_SERVER_MOCK_H
  32. #include "servers/display_server_headless.h"
  33. #include "servers/rendering/dummy/rasterizer_dummy.h"
  34. // Specialized DisplayServer for unittests based on DisplayServerHeadless, that
  35. // additionally supports rudimentary InputEvent handling and mouse position.
  36. class DisplayServerMock : public DisplayServerHeadless {
  37. private:
  38. friend class DisplayServer;
  39. Point2i mouse_position = Point2i(-1, -1); // Outside of Window.
  40. CursorShape cursor_shape = CursorShape::CURSOR_ARROW;
  41. bool window_over = false;
  42. Callable event_callback;
  43. Callable input_event_callback;
  44. static Vector<String> get_rendering_drivers_func() {
  45. Vector<String> drivers;
  46. drivers.push_back("dummy");
  47. return drivers;
  48. }
  49. 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, Error &r_error) {
  50. r_error = OK;
  51. RasterizerDummy::make_current();
  52. return memnew(DisplayServerMock());
  53. }
  54. static void _dispatch_input_events(const Ref<InputEvent> &p_event) {
  55. static_cast<DisplayServerMock *>(get_singleton())->_dispatch_input_event(p_event);
  56. }
  57. void _dispatch_input_event(const Ref<InputEvent> &p_event) {
  58. Variant ev = p_event;
  59. Variant *evp = &ev;
  60. Variant ret;
  61. Callable::CallError ce;
  62. if (input_event_callback.is_valid()) {
  63. input_event_callback.callp((const Variant **)&evp, 1, ret, ce);
  64. }
  65. }
  66. void _set_mouse_position(const Point2i &p_position) {
  67. if (mouse_position == p_position) {
  68. return;
  69. }
  70. mouse_position = p_position;
  71. _set_window_over(Rect2i(Point2i(0, 0), window_get_size()).has_point(p_position));
  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. void _send_window_event(WindowEvent p_event) {
  81. if (!event_callback.is_null()) {
  82. Variant event = int(p_event);
  83. Variant *eventp = &event;
  84. Variant ret;
  85. Callable::CallError ce;
  86. event_callback.callp((const Variant **)&eventp, 1, ret, ce);
  87. }
  88. }
  89. public:
  90. bool has_feature(Feature p_feature) const override {
  91. switch (p_feature) {
  92. case FEATURE_MOUSE:
  93. case FEATURE_CURSOR_SHAPE:
  94. return true;
  95. default: {
  96. }
  97. }
  98. return false;
  99. }
  100. String get_name() const override { return "mock"; }
  101. // You can simulate DisplayServer-events by calling this function.
  102. // The events will be deliverd to Godot's Input-system.
  103. // Mouse-events (Button & Motion) will additionally update the DisplayServer's mouse position.
  104. // For Mouse motion events, the `relative`-property is set based on the distance to the previous mouse position.
  105. void simulate_event(Ref<InputEvent> p_event) {
  106. Ref<InputEvent> event = p_event;
  107. Ref<InputEventMouse> me = p_event;
  108. if (me.is_valid()) {
  109. Ref<InputEventMouseMotion> mm = p_event;
  110. if (mm.is_valid()) {
  111. mm->set_relative(mm->get_position() - mouse_position);
  112. event = mm;
  113. }
  114. _set_mouse_position(me->get_position());
  115. }
  116. Input::get_singleton()->parse_input_event(event);
  117. }
  118. // Returns the current cursor shape.
  119. CursorShape get_cursor_shape() {
  120. return cursor_shape;
  121. }
  122. virtual Point2i mouse_get_position() const override { return mouse_position; }
  123. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override {
  124. return Size2i(1920, 1080);
  125. }
  126. virtual void cursor_set_shape(CursorShape p_shape) override {
  127. cursor_shape = p_shape;
  128. }
  129. virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override {
  130. event_callback = p_callable;
  131. }
  132. virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override {
  133. input_event_callback = p_callable;
  134. }
  135. static void register_mock_driver() {
  136. register_create_function("mock", create_func, get_rendering_drivers_func);
  137. }
  138. DisplayServerMock() {
  139. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
  140. }
  141. ~DisplayServerMock() {}
  142. };
  143. #endif // DISPLAY_SERVER_MOCK_H