diff options
Diffstat (limited to 'platform/javascript')
-rw-r--r-- | platform/javascript/SCsub | 6 | ||||
-rw-r--r-- | platform/javascript/detect.py | 21 | ||||
-rw-r--r-- | platform/javascript/display_server_javascript.cpp | 151 | ||||
-rw-r--r-- | platform/javascript/display_server_javascript.h | 9 | ||||
-rw-r--r-- | platform/javascript/dom_keys.inc | 26 | ||||
-rw-r--r-- | platform/javascript/js/libs/library_godot_input.js | 24 | ||||
-rw-r--r-- | platform/javascript/os_javascript.cpp | 2 | ||||
-rw-r--r-- | platform/javascript/os_javascript.h | 2 |
8 files changed, 130 insertions, 111 deletions
diff --git a/platform/javascript/SCsub b/platform/javascript/SCsub index fa9e6eed15..8d9ba82fd4 100644 --- a/platform/javascript/SCsub +++ b/platform/javascript/SCsub @@ -28,11 +28,11 @@ if env["javascript_eval"]: sys_env.AddJSLibraries(["js/libs/library_godot_javascript_singleton.js"]) for lib in sys_env["JS_LIBS"]: - sys_env.Append(LINKFLAGS=["--js-library", lib]) + sys_env.Append(LINKFLAGS=["--js-library", lib.abspath]) for js in env["JS_PRE"]: - sys_env.Append(LINKFLAGS=["--pre-js", env.File(js).path]) + sys_env.Append(LINKFLAGS=["--pre-js", js.abspath]) for ext in env["JS_EXTERNS"]: - sys_env["ENV"]["EMCC_CLOSURE_ARGS"] += " --externs " + ext.path + sys_env["ENV"]["EMCC_CLOSURE_ARGS"] += " --externs " + ext.abspath build = [] if env["gdnative_enabled"]: diff --git a/platform/javascript/detect.py b/platform/javascript/detect.py index 891ae419bd..b57f3b3f16 100644 --- a/platform/javascript/detect.py +++ b/platform/javascript/detect.py @@ -180,6 +180,13 @@ def configure(env): env.Prepend(CPPPATH=["#platform/javascript"]) env.Append(CPPDEFINES=["JAVASCRIPT_ENABLED", "UNIX_ENABLED"]) + if env["opengl3"]: + env.AppendUnique(CPPDEFINES=["GLES3_ENABLED"]) + # This setting just makes WebGL 2 APIs available, it does NOT disable WebGL 1. + env.Append(LINKFLAGS=["-s", "USE_WEBGL2=1"]) + # Allow use to take control of swapping WebGL buffers. + env.Append(LINKFLAGS=["-s", "OFFSCREEN_FRAMEBUFFER=1"]) + if env["javascript_eval"]: env.Append(CPPDEFINES=["JAVASCRIPT_EVAL_ENABLED"]) @@ -218,25 +225,11 @@ def configure(env): # us since we don't know requirements at compile-time. env.Append(LINKFLAGS=["-s", "ALLOW_MEMORY_GROWTH=1"]) - # This setting just makes WebGL 2 APIs available, it does NOT disable WebGL 1. - env.Append(LINKFLAGS=["-s", "USE_WEBGL2=1"]) - # Do not call main immediately when the support code is ready. env.Append(LINKFLAGS=["-s", "INVOKE_RUN=0"]) - # Allow use to take control of swapping WebGL buffers. - env.Append(LINKFLAGS=["-s", "OFFSCREEN_FRAMEBUFFER=1"]) - # callMain for manual start, cwrap for the mono version. env.Append(LINKFLAGS=["-s", "EXPORTED_RUNTIME_METHODS=['callMain','cwrap']"]) # Add code that allow exiting runtime. env.Append(LINKFLAGS=["-s", "EXIT_RUNTIME=1"]) - - # TODO remove once we have GLES support back (temporary fix undefined symbols due to dead code elimination). - env.Append( - LINKFLAGS=[ - "-s", - "EXPORTED_FUNCTIONS=['_main', '_emscripten_webgl_get_current_context']", - ] - ) diff --git a/platform/javascript/display_server_javascript.cpp b/platform/javascript/display_server_javascript.cpp index c7d68019a1..7648ddaf43 100644 --- a/platform/javascript/display_server_javascript.cpp +++ b/platform/javascript/display_server_javascript.cpp @@ -30,6 +30,9 @@ #include "platform/javascript/display_server_javascript.h" +#ifdef GLES3_ENABLED +#include "drivers/gles3/rasterizer_gles3.h" +#endif #include "platform/javascript/os_javascript.h" #include "servers/rendering/rasterizer_dummy.h" @@ -50,14 +53,6 @@ DisplayServerJavaScript *DisplayServerJavaScript::get_singleton() { } // Window (canvas) -void DisplayServerJavaScript::focus_canvas() { - godot_js_display_canvas_focus(); -} - -bool DisplayServerJavaScript::is_canvas_focused() { - return godot_js_display_canvas_is_focused() != 0; -} - bool DisplayServerJavaScript::check_size_force_redraw() { return godot_js_display_size_update() != 0; } @@ -141,29 +136,30 @@ void DisplayServerJavaScript::key_callback(int p_pressed, int p_repeat, int p_mo int DisplayServerJavaScript::mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers) { DisplayServerJavaScript *ds = get_singleton(); + Point2 pos(p_x, p_y); + Input::get_singleton()->set_mouse_position(pos); Ref<InputEventMouseButton> ev; ev.instantiate(); - ev->set_pressed(p_pressed); - ev->set_position(Point2(p_x, p_y)); - ev->set_global_position(ev->get_position()); + ev->set_position(pos); + ev->set_global_position(pos); ev->set_pressed(p_pressed); dom2godot_mod(ev, p_modifiers); switch (p_button) { case DOM_BUTTON_LEFT: - ev->set_button_index(MOUSE_BUTTON_LEFT); + ev->set_button_index(MouseButton::LEFT); break; case DOM_BUTTON_MIDDLE: - ev->set_button_index(MOUSE_BUTTON_MIDDLE); + ev->set_button_index(MouseButton::MIDDLE); break; case DOM_BUTTON_RIGHT: - ev->set_button_index(MOUSE_BUTTON_RIGHT); + ev->set_button_index(MouseButton::RIGHT); break; case DOM_BUTTON_XBUTTON1: - ev->set_button_index(MOUSE_BUTTON_XBUTTON1); + ev->set_button_index(MouseButton::MB_XBUTTON1); break; case DOM_BUTTON_XBUTTON2: - ev->set_button_index(MOUSE_BUTTON_XBUTTON2); + ev->set_button_index(MouseButton::MB_XBUTTON2); break; default: return false; @@ -176,7 +172,7 @@ int DisplayServerJavaScript::mouse_button_callback(int p_pressed, int p_button, if (diff < 400 && Point2(ds->last_click_pos).distance_to(ev->get_position()) < 5) { ds->last_click_ms = 0; ds->last_click_pos = Point2(-100, -100); - ds->last_click_button_index = -1; + ds->last_click_button_index = MouseButton::NONE; ev->set_double_click(true); } @@ -190,11 +186,11 @@ int DisplayServerJavaScript::mouse_button_callback(int p_pressed, int p_button, } } - int mask = Input::get_singleton()->get_mouse_button_mask(); - int button_flag = 1 << (ev->get_button_index() - 1); + MouseButton mask = Input::get_singleton()->get_mouse_button_mask(); + MouseButton button_flag = mouse_button_to_mask(ev->get_button_index()); if (ev->is_pressed()) { mask |= button_flag; - } else if (mask & button_flag) { + } else if ((mask & button_flag) != MouseButton::NONE) { mask &= ~button_flag; } else { // Received release event, but press was outside the canvas, so ignore. @@ -215,19 +211,22 @@ int DisplayServerJavaScript::mouse_button_callback(int p_pressed, int p_button, } void DisplayServerJavaScript::mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers) { - int input_mask = Input::get_singleton()->get_mouse_button_mask(); + MouseButton 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 behaviour. - if (!get_singleton()->cursor_inside_canvas && !input_mask) + if (!get_singleton()->cursor_inside_canvas && input_mask == MouseButton::NONE) { return; + } + Point2 pos(p_x, p_y); + Input::get_singleton()->set_mouse_position(pos); Ref<InputEventMouseMotion> ev; ev.instantiate(); dom2godot_mod(ev, p_modifiers); ev->set_button_mask(input_mask); - ev->set_position(Point2(p_x, p_y)); - ev->set_global_position(ev->get_position()); + ev->set_position(pos); + ev->set_global_position(pos); ev->set_relative(Vector2(p_rel_x, p_rel_y)); Input::get_singleton()->set_mouse_position(ev->get_position()); @@ -396,6 +395,10 @@ DisplayServer::MouseMode DisplayServerJavaScript::mouse_get_mode() const { return MOUSE_MODE_VISIBLE; } +Point2i DisplayServerJavaScript::mouse_get_position() const { + return Input::get_singleton()->get_mouse_position(); +} + // Wheel int DisplayServerJavaScript::mouse_wheel_callback(double p_delta_x, double p_delta_y) { if (!godot_js_display_canvas_is_focused()) { @@ -412,19 +415,19 @@ int DisplayServerJavaScript::mouse_wheel_callback(double p_delta_x, double p_del ev->set_position(input->get_mouse_position()); ev->set_global_position(ev->get_position()); - ev->set_shift_pressed(input->is_key_pressed(KEY_SHIFT)); - ev->set_alt_pressed(input->is_key_pressed(KEY_ALT)); - ev->set_ctrl_pressed(input->is_key_pressed(KEY_CTRL)); - ev->set_meta_pressed(input->is_key_pressed(KEY_META)); + ev->set_shift_pressed(input->is_key_pressed(Key::SHIFT)); + ev->set_alt_pressed(input->is_key_pressed(Key::ALT)); + ev->set_ctrl_pressed(input->is_key_pressed(Key::CTRL)); + ev->set_meta_pressed(input->is_key_pressed(Key::META)); if (p_delta_y < 0) { - ev->set_button_index(MOUSE_BUTTON_WHEEL_UP); + ev->set_button_index(MouseButton::WHEEL_UP); } else if (p_delta_y > 0) { - ev->set_button_index(MOUSE_BUTTON_WHEEL_DOWN); + ev->set_button_index(MouseButton::WHEEL_DOWN); } else if (p_delta_x > 0) { - ev->set_button_index(MOUSE_BUTTON_WHEEL_LEFT); + ev->set_button_index(MouseButton::WHEEL_LEFT); } else if (p_delta_x < 0) { - ev->set_button_index(MOUSE_BUTTON_WHEEL_RIGHT); + ev->set_button_index(MouseButton::WHEEL_RIGHT); } else { return false; } @@ -432,7 +435,7 @@ int DisplayServerJavaScript::mouse_wheel_callback(double p_delta_x, double p_del // Different browsers give wildly different delta values, and we can't // interpret deltaMode, so use default value for wheel events' factor. - int button_flag = 1 << (ev->get_button_index() - 1); + MouseButton button_flag = mouse_button_to_mask(ev->get_button_index()); ev->set_pressed(true); ev->set_button_mask(input->get_mouse_button_mask() | button_flag); @@ -509,12 +512,12 @@ void DisplayServerJavaScript::vk_input_text_callback(const char *p_text, int p_c k.instantiate(); k->set_pressed(true); k->set_echo(false); - k->set_keycode(KEY_RIGHT); + k->set_keycode(Key::RIGHT); input->parse_input_event(k); k.instantiate(); k->set_pressed(false); k->set_echo(false); - k->set_keycode(KEY_RIGHT); + k->set_keycode(Key::RIGHT); input->parse_input_event(k); } } @@ -557,12 +560,12 @@ void DisplayServerJavaScript::process_joypads() { for (int b = 0; b < s_btns_num; b++) { float value = s_btns[b]; // Buttons 6 and 7 in the standard mapping need to be - // axis to be handled as JOY_AXIS_TRIGGER by Godot. + // axis to be handled as JoyAxis::TRIGGER by Godot. if (s_standard && (b == 6 || b == 7)) { Input::JoyAxisValue joy_axis; joy_axis.min = 0; joy_axis.value = value; - JoyAxis a = b == 6 ? JOY_AXIS_TRIGGER_LEFT : JOY_AXIS_TRIGGER_RIGHT; + JoyAxis a = b == 6 ? JoyAxis::TRIGGER_LEFT : JoyAxis::TRIGGER_RIGHT; input->joy_axis(idx, a, joy_axis); } else { input->joy_button(idx, (JoyButton)b, value); @@ -579,7 +582,9 @@ void DisplayServerJavaScript::process_joypads() { Vector<String> DisplayServerJavaScript::get_rendering_drivers_func() { Vector<String> drivers; - drivers.push_back("dummy"); +#ifdef GLES3_ENABLED + drivers.push_back("opengl3"); +#endif return drivers; } @@ -677,40 +682,34 @@ DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_drive // Expose method for requesting quit. godot_js_os_request_quit_cb(request_quit_callback); - RasterizerDummy::make_current(); // TODO OpenGL in Godot 4.0... or webgpu? -#if 0 - EmscriptenWebGLContextAttributes attributes; - emscripten_webgl_init_context_attributes(&attributes); - attributes.alpha = GLOBAL_GET("display/window/per_pixel_transparency/allowed"); - attributes.antialias = false; - ERR_FAIL_INDEX_V(p_video_driver, VIDEO_DRIVER_MAX, ERR_INVALID_PARAMETER); - - if (p_desired.layered) { - set_window_per_pixel_transparency_enabled(true); - } - - bool gl_initialization_error = false; - - if (RasterizerGLES3::is_viable() == OK) { - attributes.majorVersion = 1; - RasterizerGLES3::register_config(); - RasterizerGLES3::make_current(); - } else { - gl_initialization_error = true; - } - - EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context(canvas_id, &attributes); - if (emscripten_webgl_make_context_current(ctx) != EMSCRIPTEN_RESULT_SUCCESS) { - gl_initialization_error = true; +#ifdef GLES3_ENABLED + // TODO "vulkan" defaults to webgl2 for now. + bool wants_webgl2 = p_rendering_driver == "opengl3" || p_rendering_driver == "vulkan"; + bool webgl2_init_failed = wants_webgl2 && !godot_js_display_has_webgl(2); + if (wants_webgl2 && !webgl2_init_failed) { + EmscriptenWebGLContextAttributes attributes; + emscripten_webgl_init_context_attributes(&attributes); + //attributes.alpha = GLOBAL_GET("display/window/per_pixel_transparency/allowed"); + attributes.alpha = true; + attributes.antialias = false; + attributes.majorVersion = 2; + + webgl_ctx = emscripten_webgl_create_context(canvas_id, &attributes); + if (emscripten_webgl_make_context_current(webgl_ctx) != EMSCRIPTEN_RESULT_SUCCESS) { + webgl2_init_failed = true; + } else { + RasterizerGLES3::make_current(); + } } - - if (gl_initialization_error) { - OS::get_singleton()->alert("Your browser does not seem to support WebGL. Please update your browser version.", + if (webgl2_init_failed) { + OS::get_singleton()->alert("Your browser does not seem to support WebGL2. Please update your browser version.", "Unable to initialize video driver"); - return ERR_UNAVAILABLE; } - - video_driver_index = p_video_driver; + if (!wants_webgl2 || webgl2_init_failed) { + RasterizerDummy::make_current(); + } +#else + RasterizerDummy::make_current(); #endif // JS Input interface (js/libs/library_godot_input.js) @@ -737,8 +736,12 @@ DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_drive } DisplayServerJavaScript::~DisplayServerJavaScript() { - //emscripten_webgl_commit_frame(); - //emscripten_webgl_destroy_context(webgl_ctx); +#ifdef GLES3_ENABLED + if (webgl_ctx) { + emscripten_webgl_commit_frame(); + emscripten_webgl_destroy_context(webgl_ctx); + } +#endif } bool DisplayServerJavaScript::has_feature(Feature p_feature) const { @@ -967,5 +970,9 @@ bool DisplayServerJavaScript::get_swap_cancel_ok() { } void DisplayServerJavaScript::swap_buffers() { - //emscripten_webgl_commit_frame(); +#ifdef GLES3_ENABLED + if (webgl_ctx) { + emscripten_webgl_commit_frame(); + } +#endif } diff --git a/platform/javascript/display_server_javascript.h b/platform/javascript/display_server_javascript.h index 1aa600c421..843bb61984 100644 --- a/platform/javascript/display_server_javascript.h +++ b/platform/javascript/display_server_javascript.h @@ -51,6 +51,10 @@ private: }; JSKeyEvent key_event; +#ifdef GLES3_ENABLED + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE webgl_ctx = 0; +#endif + WindowMode window_mode = WINDOW_MODE_WINDOWED; ObjectID window_attached_instance_id = {}; @@ -67,13 +71,11 @@ private: CursorShape cursor_shape = CURSOR_ARROW; Point2i last_click_pos = Point2(-100, -100); // TODO check this again. uint64_t last_click_ms = 0; - int last_click_button_index = -1; + MouseButton last_click_button_index = MouseButton::NONE; bool swap_cancel_ok = false; // utilities - static void focus_canvas(); - static bool is_canvas_focused(); static void dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod); static const char *godot2dom_cursor(DisplayServer::CursorShape p_shape); @@ -121,6 +123,7 @@ public: // mouse virtual void mouse_set_mode(MouseMode p_mode) override; virtual MouseMode mouse_get_mode() const override; + virtual Point2i mouse_get_position() const override; // touch virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const override; diff --git a/platform/javascript/dom_keys.inc b/platform/javascript/dom_keys.inc index 0e62776923..31589f3f40 100644 --- a/platform/javascript/dom_keys.inc +++ b/platform/javascript/dom_keys.inc @@ -34,7 +34,7 @@ Key dom_code2godot_scancode(EM_UTF8 const p_code[32], EM_UTF8 const p_key[32], bool p_physical) { #define DOM2GODOT(p_str, p_godot_code) \ if (memcmp((const void *)p_str, (void *)p_code, strlen(p_str) + 1) == 0) { \ - return KEY_##p_godot_code; \ + return Key::p_godot_code; \ } // Numpad section. @@ -105,16 +105,16 @@ Key dom_code2godot_scancode(EM_UTF8 const p_code[32], EM_UTF8 const p_key[32], b DOM2GODOT("BracketLeft", BRACKETLEFT); DOM2GODOT("BracketRight", BRACKETRIGHT); DOM2GODOT("Comma", COMMA); - DOM2GODOT("Digit0", 0); - DOM2GODOT("Digit1", 1); - DOM2GODOT("Digit2", 2); - DOM2GODOT("Digit3", 3); - DOM2GODOT("Digit4", 4); - DOM2GODOT("Digit5", 5); - DOM2GODOT("Digit6", 6); - DOM2GODOT("Digit7", 7); - DOM2GODOT("Digit8", 8); - DOM2GODOT("Digit9", 9); + DOM2GODOT("Digit0", KEY_0); + DOM2GODOT("Digit1", KEY_1); + DOM2GODOT("Digit2", KEY_2); + DOM2GODOT("Digit3", KEY_3); + DOM2GODOT("Digit4", KEY_4); + DOM2GODOT("Digit5", KEY_5); + DOM2GODOT("Digit6", KEY_6); + DOM2GODOT("Digit7", KEY_7); + DOM2GODOT("Digit8", KEY_8); + DOM2GODOT("Digit9", KEY_9); DOM2GODOT("Equal", EQUAL); DOM2GODOT("IntlBackslash", BACKSLASH); //DOM2GODOT("IntlRo", UNKNOWN); @@ -170,7 +170,7 @@ Key dom_code2godot_scancode(EM_UTF8 const p_code[32], EM_UTF8 const p_key[32], b DOM2GODOT("Tab", TAB); // ControlPad section. - DOM2GODOT("Delete", DELETE); + DOM2GODOT("Delete", KEY_DELETE); DOM2GODOT("End", END); DOM2GODOT("Help", HELP); DOM2GODOT("Home", HOME); @@ -227,6 +227,6 @@ Key dom_code2godot_scancode(EM_UTF8 const p_code[32], EM_UTF8 const p_key[32], b DOM2GODOT("AudioVolumeMute", VOLUMEMUTE); DOM2GODOT("AudioVolumeUp", VOLUMEUP); //DOM2GODOT("WakeUp", UNKNOWN); - return KEY_UNKNOWN; + return Key::UNKNOWN; #undef DOM2GODOT } diff --git a/platform/javascript/js/libs/library_godot_input.js b/platform/javascript/js/libs/library_godot_input.js index 587c320f35..945dbba902 100644 --- a/platform/javascript/js/libs/library_godot_input.js +++ b/platform/javascript/js/libs/library_godot_input.js @@ -104,10 +104,14 @@ const GodotInputGamepads = { } } GodotEventListeners.add(window, 'gamepadconnected', function (evt) { - add(evt.gamepad); + if (evt.gamepad) { + add(evt.gamepad); + } }, false); GodotEventListeners.add(window, 'gamepaddisconnected', function (evt) { - onchange(evt.gamepad.index, 0); + if (evt.gamepad) { + onchange(evt.gamepad.index, 0); + } }, false); }, @@ -389,6 +393,11 @@ const GodotInput = { const rect = canvas.getBoundingClientRect(); const pos = GodotInput.computePosition(evt, rect); const modifiers = GodotInput.getModifiers(evt); + // Since the event is consumed, focus manually. + // NOTE: The iframe container may not have focus yet, so focus even when already active. + if (p_pressed) { + GodotConfig.canvas.focus(); + } if (func(p_pressed, evt.button, pos[0], pos[1], modifiers)) { evt.preventDefault(); } @@ -405,14 +414,19 @@ const GodotInput = { const func = GodotRuntime.get_func(callback); const canvas = GodotConfig.canvas; function touch_cb(type, evt) { + // Since the event is consumed, focus manually. + // NOTE: The iframe container may not have focus yet, so focus even when already active. + if (type === 0) { + GodotConfig.canvas.focus(); + } const rect = canvas.getBoundingClientRect(); const touches = evt.changedTouches; for (let i = 0; i < touches.length; i++) { const touch = touches[i]; const pos = GodotInput.computePosition(touch, rect); - GodotRuntime.setHeapValue(coords + (i * 2), pos[0], 'double'); - GodotRuntime.setHeapValue(coords + (i * 2 + 8), pos[1], 'double'); - GodotRuntime.setHeapValue(ids + i, touch.identifier, 'i32'); + GodotRuntime.setHeapValue(coords + (i * 2) * 8, pos[0], 'double'); + GodotRuntime.setHeapValue(coords + (i * 2 + 1) * 8, pos[1], 'double'); + GodotRuntime.setHeapValue(ids + i * 4, touch.identifier, 'i32'); } func(type, touches.length); if (evt.cancelable) { diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index 4431bd5f1b..5da9a96a90 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -36,7 +36,7 @@ #include "main/main.h" #include "platform/javascript/display_server_javascript.h" -#include "modules/modules_enabled.gen.h" +#include "modules/modules_enabled.gen.h" // For websocket. #ifdef MODULE_WEBSOCKET_ENABLED #include "modules/websocket/remote_debugger_peer_websocket.h" #endif diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h index d053082d92..fbab95d33b 100644 --- a/platform/javascript/os_javascript.h +++ b/platform/javascript/os_javascript.h @@ -75,6 +75,7 @@ public: Error kill(const ProcessID &p_pid) override; int get_process_id() const override; int get_processor_count() const override; + int get_default_thread_pool_size() const override { return 1; } String get_executable_path() const override; Error shell_open(String p_uri) override; @@ -89,6 +90,7 @@ public: String get_user_data_dir() const override; bool is_userfs_persistent() const override; + bool is_single_window() const override { return true; } void alert(const String &p_alert, const String &p_title = "ALERT!") override; |