display_server_mock.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. if (input_event_callback.is_valid()) {
  59. input_event_callback.call(p_event);
  60. }
  61. }
  62. void _set_mouse_position(const Point2i &p_position) {
  63. if (mouse_position == p_position) {
  64. return;
  65. }
  66. mouse_position = p_position;
  67. _set_window_over(Rect2i(Point2i(0, 0), window_get_size()).has_point(p_position));
  68. }
  69. void _set_window_over(bool p_over) {
  70. if (p_over == window_over) {
  71. return;
  72. }
  73. window_over = p_over;
  74. _send_window_event(p_over ? WINDOW_EVENT_MOUSE_ENTER : WINDOW_EVENT_MOUSE_EXIT);
  75. }
  76. void _send_window_event(WindowEvent p_event) {
  77. if (!event_callback.is_null()) {
  78. Variant event = int(p_event);
  79. event_callback.call(event);
  80. }
  81. }
  82. public:
  83. bool has_feature(Feature p_feature) const override {
  84. switch (p_feature) {
  85. case FEATURE_MOUSE:
  86. case FEATURE_CURSOR_SHAPE:
  87. return true;
  88. default: {
  89. }
  90. }
  91. return false;
  92. }
  93. String get_name() const override { return "mock"; }
  94. // You can simulate DisplayServer-events by calling this function.
  95. // The events will be deliverd to Godot's Input-system.
  96. // Mouse-events (Button & Motion) will additionally update the DisplayServer's mouse position.
  97. // For Mouse motion events, the `relative`-property is set based on the distance to the previous mouse position.
  98. void simulate_event(Ref<InputEvent> p_event) {
  99. Ref<InputEvent> event = p_event;
  100. Ref<InputEventMouse> me = p_event;
  101. if (me.is_valid()) {
  102. Ref<InputEventMouseMotion> mm = p_event;
  103. if (mm.is_valid()) {
  104. mm->set_relative(mm->get_position() - mouse_position);
  105. event = mm;
  106. }
  107. _set_mouse_position(me->get_position());
  108. }
  109. Input::get_singleton()->parse_input_event(event);
  110. }
  111. // Returns the current cursor shape.
  112. CursorShape get_cursor_shape() {
  113. return cursor_shape;
  114. }
  115. virtual Point2i mouse_get_position() const override { return mouse_position; }
  116. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override {
  117. return Size2i(1920, 1080);
  118. }
  119. virtual void cursor_set_shape(CursorShape p_shape) override {
  120. cursor_shape = p_shape;
  121. }
  122. virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override {
  123. event_callback = p_callable;
  124. }
  125. virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override {
  126. input_event_callback = p_callable;
  127. }
  128. static void register_mock_driver() {
  129. register_create_function("mock", create_func, get_rendering_drivers_func);
  130. }
  131. DisplayServerMock() {
  132. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
  133. }
  134. ~DisplayServerMock() {}
  135. };
  136. #endif // DISPLAY_SERVER_MOCK_H