summaryrefslogtreecommitdiff
path: root/platform/web/display_server_web.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'platform/web/display_server_web.cpp')
-rw-r--r--platform/web/display_server_web.cpp139
1 files changed, 81 insertions, 58 deletions
diff --git a/platform/web/display_server_web.cpp b/platform/web/display_server_web.cpp
index d057010c02..e89a79834b 100644
--- a/platform/web/display_server_web.cpp
+++ b/platform/web/display_server_web.cpp
@@ -1,32 +1,32 @@
-/*************************************************************************/
-/* display_server_web.cpp */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
+/**************************************************************************/
+/* display_server_web.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
#include "display_server_web.h"
@@ -108,11 +108,19 @@ void DisplayServerWeb::request_quit_callback() {
// Keys
-void DisplayServerWeb::dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod) {
- ev->set_shift_pressed(p_mod & 1);
- ev->set_alt_pressed(p_mod & 2);
- ev->set_ctrl_pressed(p_mod & 4);
- ev->set_meta_pressed(p_mod & 8);
+void DisplayServerWeb::dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod, Key p_keycode) {
+ if (p_keycode != Key::SHIFT) {
+ ev->set_shift_pressed(p_mod & 1);
+ }
+ if (p_keycode != Key::ALT) {
+ ev->set_alt_pressed(p_mod & 2);
+ }
+ if (p_keycode != Key::CTRL) {
+ ev->set_ctrl_pressed(p_mod & 4);
+ }
+ if (p_keycode != Key::META) {
+ ev->set_meta_pressed(p_mod & 8);
+ }
}
void DisplayServerWeb::key_callback(int p_pressed, int p_repeat, int p_modifiers) {
@@ -121,18 +129,25 @@ void DisplayServerWeb::key_callback(int p_pressed, int p_repeat, int p_modifiers
// Resume audio context after input in case autoplay was denied.
OS_Web::get_singleton()->resume_audio();
+ char32_t c = 0x00;
+ String unicode = String::utf8(key_event.key);
+ if (unicode.length() == 1) {
+ c = unicode[0];
+ }
+
+ Key keycode = dom_code2godot_scancode(key_event.code, key_event.key, false);
+ Key scancode = dom_code2godot_scancode(key_event.code, key_event.key, true);
+
Ref<InputEventKey> ev;
ev.instantiate();
ev->set_echo(p_repeat);
- ev->set_keycode(dom_code2godot_scancode(key_event.code, key_event.key, false));
- ev->set_physical_keycode(dom_code2godot_scancode(key_event.code, key_event.key, true));
+ ev->set_keycode(fix_keycode(c, keycode));
+ ev->set_physical_keycode(scancode);
+ ev->set_key_label(fix_key_label(c, keycode));
+ ev->set_unicode(fix_unicode(c));
ev->set_pressed(p_pressed);
- dom2godot_mod(ev, p_modifiers);
+ dom2godot_mod(ev, p_modifiers, fix_keycode(c, keycode));
- String unicode = String::utf8(key_event.key);
- if (unicode.length() == 1) {
- ev->set_unicode(unicode[0]);
- }
Input::get_singleton()->parse_input_event(ev);
// Make sure to flush all events so we can call restricted APIs inside the event.
@@ -150,7 +165,7 @@ int DisplayServerWeb::mouse_button_callback(int p_pressed, int p_button, double
ev->set_position(pos);
ev->set_global_position(pos);
ev->set_pressed(p_pressed);
- dom2godot_mod(ev, p_modifiers);
+ dom2godot_mod(ev, p_modifiers, Key::NONE);
switch (p_button) {
case DOM_BUTTON_LEFT:
@@ -193,12 +208,12 @@ int DisplayServerWeb::mouse_button_callback(int p_pressed, int p_button, double
}
}
- MouseButton mask = Input::get_singleton()->get_mouse_button_mask();
- MouseButton button_flag = mouse_button_to_mask(ev->get_button_index());
+ BitField<MouseButtonMask> mask = Input::get_singleton()->get_mouse_button_mask();
+ MouseButtonMask button_flag = mouse_button_to_mask(ev->get_button_index());
if (ev->is_pressed()) {
- mask |= button_flag;
- } else if ((mask & button_flag) != MouseButton::NONE) {
- mask &= ~button_flag;
+ mask.set_flag(button_flag);
+ } else if (mask.has_flag(button_flag)) {
+ mask.clear_flag(button_flag);
} else {
// Received release event, but press was outside the canvas, so ignore.
return false;
@@ -218,17 +233,17 @@ int DisplayServerWeb::mouse_button_callback(int p_pressed, int p_button, double
}
void DisplayServerWeb::mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers) {
- MouseButton input_mask = Input::get_singleton()->get_mouse_button_mask();
+ BitField<MouseButtonMask> input_mask = Input::get_singleton()->get_mouse_button_mask();
// For motion outside the canvas, only read mouse movement if dragging
// started inside the canvas; imitating desktop app behavior.
- if (!get_singleton()->cursor_inside_canvas && input_mask == MouseButton::NONE) {
+ if (!get_singleton()->cursor_inside_canvas && input_mask.is_empty()) {
return;
}
Point2 pos(p_x, p_y);
Ref<InputEventMouseMotion> ev;
ev.instantiate();
- dom2godot_mod(ev, p_modifiers);
+ dom2godot_mod(ev, p_modifiers, Key::NONE);
ev->set_button_mask(input_mask);
ev->set_position(pos);
@@ -525,15 +540,17 @@ int DisplayServerWeb::mouse_wheel_callback(double p_delta_x, double p_delta_y) {
// Different browsers give wildly different delta values, and we can't
// interpret deltaMode, so use default value for wheel events' factor.
- MouseButton button_flag = mouse_button_to_mask(ev->get_button_index());
+ MouseButtonMask button_flag = mouse_button_to_mask(ev->get_button_index());
+ BitField<MouseButtonMask> button_mask = input->get_mouse_button_mask();
+ button_mask.set_flag(button_flag);
ev->set_pressed(true);
- ev->set_button_mask(input->get_mouse_button_mask() | button_flag);
+ ev->set_button_mask(button_mask);
input->parse_input_event(ev);
Ref<InputEventMouseButton> release = ev->duplicate();
release->set_pressed(false);
- release->set_button_mask(MouseButton(input->get_mouse_button_mask() & ~button_flag));
+ release->set_button_mask(input->get_mouse_button_mask());
input->parse_input_event(release);
return true;
@@ -746,11 +763,11 @@ void DisplayServerWeb::_dispatch_input_event(const Ref<InputEvent> &p_event) {
}
}
-DisplayServer *DisplayServerWeb::create_func(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Point2i *p_position, const Size2i &p_resolution, Error &r_error) {
- return memnew(DisplayServerWeb(p_rendering_driver, p_window_mode, p_vsync_mode, p_flags, p_position, p_resolution, r_error));
+DisplayServer *DisplayServerWeb::create_func(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Point2i *p_position, const Size2i &p_resolution, int p_screen, Error &r_error) {
+ return memnew(DisplayServerWeb(p_rendering_driver, p_window_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, r_error));
}
-DisplayServerWeb::DisplayServerWeb(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Point2i *p_position, const Size2i &p_resolution, Error &r_error) {
+DisplayServerWeb::DisplayServerWeb(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Point2i *p_position, const Size2i &p_resolution, int p_screen, Error &r_error) {
r_error = OK; // Always succeeds for now.
// Ensure the canvas ID.
@@ -785,8 +802,10 @@ DisplayServerWeb::DisplayServerWeb(const String &p_rendering_driver, WindowMode
RasterizerGLES3::make_current();
} else {
- OS::get_singleton()->alert("Your browser does not seem to support WebGL2. Please update your browser version.",
- "Unable to initialize video driver");
+ OS::get_singleton()->alert(
+ "Your browser seems not to support WebGL 2.\n\n"
+ "If possible, consider updating your browser version and video card drivers.",
+ "Unable to initialize WebGL 2 video driver");
RasterizerDummy::make_current();
}
#else
@@ -864,6 +883,10 @@ int DisplayServerWeb::get_screen_count() const {
return 1;
}
+int DisplayServerWeb::get_primary_screen() const {
+ return 0;
+}
+
Point2i DisplayServerWeb::screen_get_position(int p_screen) const {
return Point2i(); // TODO offsetX/Y?
}