display_server_mock.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 things like mouse enter/exit events and clipboard.
  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. String clipboard_text;
  44. String primary_clipboard_text;
  45. static Vector<String> get_rendering_drivers_func() {
  46. Vector<String> drivers;
  47. drivers.push_back("dummy");
  48. return drivers;
  49. }
  50. 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, Error &r_error) {
  51. r_error = OK;
  52. RasterizerDummy::make_current();
  53. return memnew(DisplayServerMock());
  54. }
  55. void _set_mouse_position(const Point2i &p_position) {
  56. if (mouse_position == p_position) {
  57. return;
  58. }
  59. mouse_position = p_position;
  60. _set_window_over(Rect2i(Point2i(0, 0), window_get_size()).has_point(p_position));
  61. }
  62. void _set_window_over(bool p_over) {
  63. if (p_over == window_over) {
  64. return;
  65. }
  66. window_over = p_over;
  67. _send_window_event(p_over ? WINDOW_EVENT_MOUSE_ENTER : WINDOW_EVENT_MOUSE_EXIT);
  68. }
  69. void _send_window_event(WindowEvent p_event) {
  70. if (event_callback.is_valid()) {
  71. Variant event = int(p_event);
  72. event_callback.call(event);
  73. }
  74. }
  75. public:
  76. bool has_feature(Feature p_feature) const override {
  77. switch (p_feature) {
  78. case FEATURE_MOUSE:
  79. case FEATURE_CURSOR_SHAPE:
  80. case FEATURE_CLIPBOARD:
  81. case FEATURE_CLIPBOARD_PRIMARY:
  82. return true;
  83. default: {
  84. }
  85. }
  86. return false;
  87. }
  88. String get_name() const override { return "mock"; }
  89. // You can simulate DisplayServer-events by calling this function.
  90. // The events will be deliverd to Godot's Input-system.
  91. // Mouse-events (Button & Motion) will additionally update the DisplayServer's mouse position.
  92. // For Mouse motion events, the `relative`-property is set based on the distance to the previous mouse position.
  93. void simulate_event(Ref<InputEvent> p_event) {
  94. Ref<InputEvent> event = p_event;
  95. Ref<InputEventMouse> me = p_event;
  96. if (me.is_valid()) {
  97. Ref<InputEventMouseMotion> mm = p_event;
  98. if (mm.is_valid()) {
  99. mm->set_relative(mm->get_position() - mouse_position);
  100. event = mm;
  101. }
  102. _set_mouse_position(me->get_position());
  103. }
  104. Input::get_singleton()->parse_input_event(event);
  105. }
  106. // Returns the current cursor shape.
  107. CursorShape get_cursor_shape() {
  108. return cursor_shape;
  109. }
  110. virtual Point2i mouse_get_position() const override { return mouse_position; }
  111. virtual void clipboard_set(const String &p_text) override { clipboard_text = p_text; }
  112. virtual String clipboard_get() const override { return clipboard_text; }
  113. virtual void clipboard_set_primary(const String &p_text) override { primary_clipboard_text = p_text; }
  114. virtual String clipboard_get_primary() const override { return primary_clipboard_text; }
  115. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override {
  116. return Size2i(1920, 1080);
  117. }
  118. virtual void cursor_set_shape(CursorShape p_shape) override {
  119. cursor_shape = p_shape;
  120. }
  121. virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override {
  122. event_callback = p_callable;
  123. }
  124. static void register_mock_driver() {
  125. register_create_function("mock", create_func, get_rendering_drivers_func);
  126. }
  127. };
  128. #endif // DISPLAY_SERVER_MOCK_H