display_server_javascript.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*************************************************************************/
  2. /* display_server_javascript.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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_JAVASCRIPT_H
  31. #define DISPLAY_SERVER_JAVASCRIPT_H
  32. #include "servers/display_server.h"
  33. #include <emscripten.h>
  34. #include <emscripten/html5.h>
  35. class DisplayServerJavaScript : public DisplayServer {
  36. //int video_driver_index;
  37. Vector2 windowed_size;
  38. ObjectID window_attached_instance_id = {};
  39. Ref<InputEventKey> deferred_key_event;
  40. CursorShape cursor_shape = CURSOR_ARROW;
  41. String cursors[CURSOR_MAX];
  42. Map<CursorShape, Vector<Variant>> cursors_cache;
  43. Point2 touches[32];
  44. Point2i last_click_pos = Point2(-100, -100); // TODO check this again.
  45. double last_click_ms = 0;
  46. int last_click_button_index = -1;
  47. static EM_BOOL fullscreen_change_callback(int p_event_type, const EmscriptenFullscreenChangeEvent *p_event, void *p_user_data);
  48. static EM_BOOL keydown_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
  49. static EM_BOOL keypress_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
  50. static EM_BOOL keyup_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data);
  51. static EM_BOOL mousemove_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data);
  52. static EM_BOOL mouse_button_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data);
  53. static EM_BOOL wheel_callback(int p_event_type, const EmscriptenWheelEvent *p_event, void *p_user_data);
  54. static EM_BOOL touch_press_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data);
  55. static EM_BOOL touchmove_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data);
  56. static EM_BOOL gamepad_change_callback(int p_event_type, const EmscriptenGamepadEvent *p_event, void *p_user_data);
  57. void process_joypads();
  58. static Vector<String> get_rendering_drivers_func();
  59. static DisplayServer *create_func(const String &p_rendering_driver, DisplayServer::WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error);
  60. static void _dispatch_input_event(const Ref<InputEvent> &p_event);
  61. protected:
  62. virtual int get_current_video_driver() const;
  63. public:
  64. // Override return type to make writing static callbacks less tedious.
  65. static DisplayServerJavaScript *get_singleton();
  66. WindowMode window_mode = WINDOW_MODE_WINDOWED;
  67. String clipboard;
  68. String canvas_id;
  69. Callable window_event_callback;
  70. Callable input_event_callback;
  71. Callable input_text_callback;
  72. // from DisplayServer
  73. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  74. virtual bool has_feature(Feature p_feature) const;
  75. virtual String get_name() const;
  76. // cursor
  77. virtual void cursor_set_shape(CursorShape p_shape);
  78. virtual CursorShape cursor_get_shape() const;
  79. virtual void cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape = CURSOR_ARROW, const Vector2 &p_hotspot = Vector2());
  80. // mouse
  81. virtual void mouse_set_mode(MouseMode p_mode);
  82. virtual MouseMode mouse_get_mode() const;
  83. // touch
  84. virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  85. // clipboard
  86. virtual void clipboard_set(const String &p_text);
  87. virtual String clipboard_get() const;
  88. // screen
  89. virtual int get_screen_count() const;
  90. virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  91. virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  92. virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  93. virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  94. // windows
  95. virtual Vector<DisplayServer::WindowID> get_window_list() const;
  96. virtual WindowID get_window_at_screen_position(const Point2i &p_position) const;
  97. virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID);
  98. virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const;
  99. virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  100. virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  101. virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  102. virtual void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  103. virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  104. virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID);
  105. virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const;
  106. virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID);
  107. virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const;
  108. virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID);
  109. virtual void window_set_transient(WindowID p_window, WindowID p_parent);
  110. virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID);
  111. virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const;
  112. virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID);
  113. virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const;
  114. virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID);
  115. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const;
  116. virtual Size2i window_get_real_size(WindowID p_window = MAIN_WINDOW_ID) const; // FIXME: Find clearer name for this.
  117. virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID);
  118. virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const;
  119. virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const;
  120. virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID);
  121. virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const;
  122. virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID);
  123. virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID);
  124. virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const;
  125. virtual bool can_any_window_draw() const;
  126. // events
  127. virtual void process_events();
  128. // icon
  129. virtual void set_icon(const Ref<Image> &p_icon);
  130. // others
  131. virtual void swap_buffers();
  132. static void register_javascript_driver();
  133. DisplayServerJavaScript(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error);
  134. ~DisplayServerJavaScript();
  135. };
  136. #endif // DISPLAY_SERVER_JAVASCRIPT_H