diff options
Diffstat (limited to 'platform/javascript/os_javascript.cpp')
-rw-r--r-- | platform/javascript/os_javascript.cpp | 796 |
1 files changed, 475 insertions, 321 deletions
diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index 83072c30aa..d339baf024 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -6,6 +6,7 @@ /* http://www.godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 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 */ @@ -28,8 +29,8 @@ /*************************************************************************/ #include "os_javascript.h" -#include "core/global_config.h" #include "core/io/file_access_buffered_fa.h" +#include "core/project_settings.h" #include "dom_keys.h" #include "drivers/gles3/rasterizer_gles3.h" #include "drivers/unix/dir_access_unix.h" @@ -40,6 +41,19 @@ #include <emscripten.h> #include <stdlib.h> +#define DOM_BUTTON_LEFT 0 +#define DOM_BUTTON_MIDDLE 1 +#define DOM_BUTTON_RIGHT 2 + +template <typename T> +static void dom2godot_mod(T emscripten_event_ptr, Ref<InputEventWithModifiers> godot_event) { + + godot_event->set_shift(emscripten_event_ptr->shiftKey); + godot_event->set_alt(emscripten_event_ptr->altKey); + godot_event->set_control(emscripten_event_ptr->ctrlKey); + godot_event->set_metakey(emscripten_event_ptr->metaKey); +} + int OS_JavaScript::get_video_driver_count() const { return 1; @@ -82,30 +96,18 @@ static EM_BOOL _browser_resize_callback(int event_type, const EmscriptenUiEvent ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_RESIZE, false); OS_JavaScript *os = static_cast<OS_JavaScript *>(user_data); - - // the order in which _browser_resize_callback and - // _fullscreen_change_callback are called is browser-dependent, - // so try adjusting for fullscreen in both - if (os->is_window_fullscreen() || os->is_window_maximized()) { - - OS::VideoMode vm = os->get_video_mode(); - vm.width = ui_event->windowInnerWidth; - vm.height = ui_event->windowInnerHeight; - os->set_video_mode(vm); - emscripten_set_canvas_size(ui_event->windowInnerWidth, ui_event->windowInnerHeight); - } + // The order of the fullscreen change event and the window size change + // event varies, even within just one browser, so defer handling + os->request_canvas_size_adjustment(); return false; } -static Size2 _windowed_size; - static EM_BOOL _fullscreen_change_callback(int event_type, const EmscriptenFullscreenChangeEvent *event, void *user_data) { ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_FULLSCREENCHANGE, false); OS_JavaScript *os = static_cast<OS_JavaScript *>(user_data); String id = String::utf8(event->id); - // empty id is canvas if (id.empty() || id == "canvas") { @@ -113,32 +115,247 @@ static EM_BOOL _fullscreen_change_callback(int event_type, const EmscriptenFulls // this event property is the only reliable information on // browser fullscreen state vm.fullscreen = event->isFullscreen; + os->set_video_mode(vm); + os->request_canvas_size_adjustment(); + } + return false; +} - if (event->isFullscreen) { - vm.width = event->screenWidth; - vm.height = event->screenHeight; - os->set_video_mode(vm); - emscripten_set_canvas_size(vm.width, vm.height); - } else { - os->set_video_mode(vm); - if (!os->is_window_maximized()) { - os->set_window_size(_windowed_size); - } +static InputDefault *_input; + +static bool is_canvas_focused() { + + /* clang-format off */ + return EM_ASM_INT_V( + return document.activeElement == Module.canvas; + ); + /* clang-format on */ +} + +static void focus_canvas() { + + /* clang-format off */ + EM_ASM( + Module.canvas.focus(); + ); + /* clang-format on */ +} + +static bool _cursor_inside_canvas = true; + +static bool is_cursor_inside_canvas() { + + return _cursor_inside_canvas; +} + +static EM_BOOL _mousebutton_callback(int event_type, const EmscriptenMouseEvent *mouse_event, void *user_data) { + + ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_MOUSEDOWN && event_type != EMSCRIPTEN_EVENT_MOUSEUP, false); + + Ref<InputEventMouseButton> ev; + ev.instance(); + ev->set_pressed(event_type == EMSCRIPTEN_EVENT_MOUSEDOWN); + ev->set_position(Point2(mouse_event->canvasX, mouse_event->canvasY)); + ev->set_global_position(ev->get_position()); + dom2godot_mod(mouse_event, ev); + + switch (mouse_event->button) { + case DOM_BUTTON_LEFT: ev->set_button_index(BUTTON_LEFT); break; + case DOM_BUTTON_MIDDLE: ev->set_button_index(BUTTON_MIDDLE); break; + case DOM_BUTTON_RIGHT: ev->set_button_index(BUTTON_RIGHT); break; + default: return false; + } + + int mask = _input->get_mouse_button_mask(); + if (ev->is_pressed()) { + // since the event is consumed, focus manually + if (!is_canvas_focused()) { + focus_canvas(); } + mask |= 1 << ev->get_button_index(); + } else if (mask & (1 << ev->get_button_index())) { + mask &= ~(1 << ev->get_button_index()); + } else { + // release event, but press was outside the canvas, so ignore + return false; } + ev->set_button_mask(mask >> 1); + + _input->parse_input_event(ev); + // prevent selection dragging + return true; +} + +static EM_BOOL _mousemove_callback(int event_type, const EmscriptenMouseEvent *mouse_event, void *user_data) { + + ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_MOUSEMOVE, false); + OS_JavaScript *os = static_cast<OS_JavaScript *>(user_data); + int input_mask = _input->get_mouse_button_mask(); + Point2 pos = Point2(mouse_event->canvasX, mouse_event->canvasY); + // outside the canvas, only read mouse movement if dragging started inside + // the canvas; imitating desktop app behaviour + if (!is_cursor_inside_canvas() && !input_mask) + return false; + + Ref<InputEventMouseMotion> ev; + ev.instance(); + dom2godot_mod(mouse_event, ev); + ev->set_button_mask(input_mask >> 1); + + ev->set_position(pos); + ev->set_global_position(ev->get_position()); + + ev->set_relative(_input->get_mouse_position() - ev->get_position()); + _input->set_mouse_position(ev->get_position()); + ev->set_speed(_input->get_last_mouse_speed()); + + _input->parse_input_event(ev); + // don't suppress mouseover/leave events return false; } -static InputEvent _setup_key_event(const EmscriptenKeyboardEvent *emscripten_event) { +static EM_BOOL _wheel_callback(int event_type, const EmscriptenWheelEvent *wheel_event, void *user_data) { + + ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_WHEEL, false); + if (!is_canvas_focused()) { + if (is_cursor_inside_canvas()) { + focus_canvas(); + } else { + return false; + } + } + + Ref<InputEventMouseButton> ev; + ev.instance(); + ev->set_button_mask(_input->get_mouse_button_mask() >> 1); + ev->set_position(_input->get_mouse_position()); + ev->set_global_position(ev->get_position()); + + ev->set_shift(_input->is_key_pressed(KEY_SHIFT)); + ev->set_alt(_input->is_key_pressed(KEY_ALT)); + ev->set_control(_input->is_key_pressed(KEY_CONTROL)); + ev->set_metakey(_input->is_key_pressed(KEY_META)); + + if (wheel_event->deltaY < 0) + ev->set_button_index(BUTTON_WHEEL_UP); + else if (wheel_event->deltaY > 0) + ev->set_button_index(BUTTON_WHEEL_DOWN); + else if (wheel_event->deltaX > 0) + ev->set_button_index(BUTTON_WHEEL_LEFT); + else if (wheel_event->deltaX < 0) + ev->set_button_index(BUTTON_WHEEL_RIGHT); + else + return false; + + // Different browsers give wildly different delta values, and we can't + // interpret deltaMode, so use default value for wheel events' factor + + ev->set_pressed(true); + _input->parse_input_event(ev); + + ev->set_pressed(false); + _input->parse_input_event(ev); + + return true; +} + +static Point2 _prev_touches[32]; + +static EM_BOOL _touchpress_callback(int event_type, const EmscriptenTouchEvent *touch_event, void *user_data) { + + ERR_FAIL_COND_V( + event_type != EMSCRIPTEN_EVENT_TOUCHSTART && + event_type != EMSCRIPTEN_EVENT_TOUCHEND && + event_type != EMSCRIPTEN_EVENT_TOUCHCANCEL, + false); + + Ref<InputEventScreenTouch> ev; + ev.instance(); + int lowest_id_index = -1; + for (int i = 0; i < touch_event->numTouches; ++i) { + + const EmscriptenTouchPoint &touch = touch_event->touches[i]; + if (lowest_id_index == -1 || touch.identifier < touch_event->touches[lowest_id_index].identifier) + lowest_id_index = i; + if (!touch.isChanged) + continue; + ev->set_index(touch.identifier); + ev->set_position(Point2(touch.canvasX, touch.canvasY)); + _prev_touches[i] = ev->get_position(); + ev->set_pressed(event_type == EMSCRIPTEN_EVENT_TOUCHSTART); + + _input->parse_input_event(ev); + } + + if (touch_event->touches[lowest_id_index].isChanged) { + + Ref<InputEventMouseButton> ev_mouse; + ev_mouse.instance(); + ev_mouse->set_button_mask(_input->get_mouse_button_mask() >> 1); + dom2godot_mod(touch_event, ev_mouse); + + const EmscriptenTouchPoint &first_touch = touch_event->touches[lowest_id_index]; + ev_mouse->set_position(Point2(first_touch.canvasX, first_touch.canvasY)); + ev_mouse->set_global_position(ev_mouse->get_position()); + + ev_mouse->set_button_index(BUTTON_LEFT); + ev_mouse->set_pressed(event_type == EMSCRIPTEN_EVENT_TOUCHSTART); + + _input->parse_input_event(ev_mouse); + } + return true; +} + +static EM_BOOL _touchmove_callback(int event_type, const EmscriptenTouchEvent *touch_event, void *user_data) { + + ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_TOUCHMOVE, false); + + Ref<InputEventScreenDrag> ev; + ev.instance(); + int lowest_id_index = -1; + for (int i = 0; i < touch_event->numTouches; ++i) { + + const EmscriptenTouchPoint &touch = touch_event->touches[i]; + if (lowest_id_index == -1 || touch.identifier < touch_event->touches[lowest_id_index].identifier) + lowest_id_index = i; + if (!touch.isChanged) + continue; + ev->set_index(touch.identifier); + ev->set_position(Point2(touch.canvasX, touch.canvasY)); + Point2 &prev = _prev_touches[i]; + ev->set_relative(ev->get_position() - prev); + prev = ev->get_position(); + + _input->parse_input_event(ev); + } + + if (touch_event->touches[lowest_id_index].isChanged) { + + Ref<InputEventMouseMotion> ev_mouse; + ev_mouse.instance(); + dom2godot_mod(touch_event, ev_mouse); + ev_mouse->set_button_mask(_input->get_mouse_button_mask() >> 1); + + const EmscriptenTouchPoint &first_touch = touch_event->touches[lowest_id_index]; + ev_mouse->set_position(Point2(first_touch.canvasX, first_touch.canvasY)); + ev_mouse->set_global_position(ev_mouse->get_position()); + + ev_mouse->set_relative(_input->get_mouse_position() - ev_mouse->get_position()); + _input->set_mouse_position(ev_mouse->get_position()); + ev_mouse->set_speed(_input->get_last_mouse_speed()); + + _input->parse_input_event(ev_mouse); + } + return true; +} + +static Ref<InputEventKey> _setup_key_event(const EmscriptenKeyboardEvent *emscripten_event) { - InputEvent ev; - ev.type = InputEvent::KEY; - ev.key.echo = emscripten_event->repeat; - ev.key.mod.alt = emscripten_event->altKey; - ev.key.mod.shift = emscripten_event->shiftKey; - ev.key.mod.control = emscripten_event->ctrlKey; - ev.key.mod.meta = emscripten_event->metaKey; - ev.key.scancode = dom2godot_scancode(emscripten_event->keyCode); + Ref<InputEventKey> ev; + ev.instance(); + ev->set_echo(emscripten_event->repeat); + dom2godot_mod(emscripten_event, ev); + ev->set_scancode(dom2godot_scancode(emscripten_event->keyCode)); String unicode = String::utf8(emscripten_event->key); // check if empty or multi-character (e.g. `CapsLock`) @@ -147,26 +364,26 @@ static InputEvent _setup_key_event(const EmscriptenKeyboardEvent *emscripten_eve unicode = String::utf8(emscripten_event->charValue); } if (unicode.length() == 1) { - ev.key.unicode = unicode[0]; + ev->set_unicode(unicode[0]); } return ev; } -static InputEvent deferred_key_event; +static Ref<InputEventKey> deferred_key_event; static EM_BOOL _keydown_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) { ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYDOWN, false); - InputEvent ev = _setup_key_event(key_event); - ev.key.pressed = true; - if (ev.key.unicode == 0 && keycode_has_unicode(ev.key.scancode)) { + Ref<InputEventKey> ev = _setup_key_event(key_event); + ev->set_pressed(true); + if (ev->get_unicode() == 0 && keycode_has_unicode(ev->get_scancode())) { // defer to keypress event for legacy unicode retrieval deferred_key_event = ev; return false; // do not suppress keypress event } - static_cast<OS_JavaScript *>(user_data)->push_input(ev); + _input->parse_input_event(ev); return true; } @@ -174,8 +391,8 @@ static EM_BOOL _keypress_callback(int event_type, const EmscriptenKeyboardEvent ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYPRESS, false); - deferred_key_event.key.unicode = key_event->charCode; - static_cast<OS_JavaScript *>(user_data)->push_input(deferred_key_event); + deferred_key_event->set_unicode(key_event->charCode); + _input->parse_input_event(deferred_key_event); return true; } @@ -183,10 +400,10 @@ static EM_BOOL _keyup_callback(int event_type, const EmscriptenKeyboardEvent *ke ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYUP, false); - InputEvent ev = _setup_key_event(key_event); - ev.key.pressed = false; - static_cast<OS_JavaScript *>(user_data)->push_input(ev); - return ev.key.scancode != KEY_UNKNOWN && ev.key.scancode != 0; + Ref<InputEventKey> ev = _setup_key_event(key_event); + ev->set_pressed(false); + _input->parse_input_event(ev); + return ev->get_scancode() != KEY_UNKNOWN && ev->get_scancode() != 0; } static EM_BOOL joy_callback_func(int p_type, const EmscriptenGamepadEvent *p_event, void *p_user) { @@ -197,18 +414,31 @@ static EM_BOOL joy_callback_func(int p_type, const EmscriptenGamepadEvent *p_eve return false; } +extern "C" { +void send_notification(int notif) { + if (notif == MainLoop::NOTIFICATION_WM_MOUSE_ENTER || notif == MainLoop::NOTIFICATION_WM_MOUSE_EXIT) { + _cursor_inside_canvas = notif == MainLoop::NOTIFICATION_WM_MOUSE_ENTER; + } + OS_JavaScript::get_singleton()->get_main_loop()->notification(notif); +} +} + void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { print_line("Init OS"); - if (gfx_init_func) - gfx_init_func(gfx_init_ud, use_gl2, p_desired.width, p_desired.height, p_desired.fullscreen); + EmscriptenWebGLContextAttributes attributes; + emscripten_webgl_init_context_attributes(&attributes); + attributes.alpha = false; + attributes.antialias = false; + attributes.majorVersion = 2; + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context(NULL, &attributes); + ERR_FAIL_COND(emscripten_webgl_make_context_current(ctx) != EMSCRIPTEN_RESULT_SUCCESS); - // nothing to do here, can't fulfil fullscreen request due to - // browser security, window size is already set from HTML video_mode = p_desired; + // can't fulfil fullscreen request due to browser security video_mode.fullscreen = false; - _windowed_size = get_window_size(); + set_window_size(Size2(p_desired.width, p_desired.height)); // find locale, emscripten only sets "C" char locale_ptr[16]; @@ -246,7 +476,7 @@ void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, i print_line("Init VS"); visual_server = memnew(VisualServerRaster()); - visual_server->cursor_set_visible(false, 0); + // visual_server->cursor_set_visible(false, 0); print_line("Init Physicsserver"); @@ -256,25 +486,34 @@ void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, i physics_2d_server->init(); input = memnew(InputDefault); + _input = input; power_manager = memnew(PowerJavascript); #define EM_CHECK(ev) \ if (result != EMSCRIPTEN_RESULT_SUCCESS) \ ERR_PRINTS("Error while setting " #ev " callback: Code " + itos(result)) -#define SET_EM_CALLBACK(ev, cb) \ - result = emscripten_set_##ev##_callback(NULL, this, true, &cb); \ +#define SET_EM_CALLBACK(target, ev, cb) \ + result = emscripten_set_##ev##_callback(target, this, true, &cb); \ EM_CHECK(ev) #define SET_EM_CALLBACK_NODATA(ev, cb) \ result = emscripten_set_##ev##_callback(NULL, true, &cb); \ EM_CHECK(ev) EMSCRIPTEN_RESULT result; - SET_EM_CALLBACK(keydown, _keydown_callback) - SET_EM_CALLBACK(keypress, _keypress_callback) - SET_EM_CALLBACK(keyup, _keyup_callback) - SET_EM_CALLBACK(resize, _browser_resize_callback) - SET_EM_CALLBACK(fullscreenchange, _fullscreen_change_callback) + SET_EM_CALLBACK("#window", mousemove, _mousemove_callback) + SET_EM_CALLBACK("#canvas", mousedown, _mousebutton_callback) + SET_EM_CALLBACK("#window", mouseup, _mousebutton_callback) + SET_EM_CALLBACK("#window", wheel, _wheel_callback) + SET_EM_CALLBACK("#window", touchstart, _touchpress_callback) + SET_EM_CALLBACK("#window", touchmove, _touchmove_callback) + SET_EM_CALLBACK("#window", touchend, _touchpress_callback) + SET_EM_CALLBACK("#window", touchcancel, _touchpress_callback) + SET_EM_CALLBACK("#canvas", keydown, _keydown_callback) + SET_EM_CALLBACK("#canvas", keypress, _keypress_callback) + SET_EM_CALLBACK("#canvas", keyup, _keyup_callback) + SET_EM_CALLBACK(NULL, resize, _browser_resize_callback) + SET_EM_CALLBACK(NULL, fullscreenchange, _fullscreen_change_callback) SET_EM_CALLBACK_NODATA(gamepadconnected, joy_callback_func) SET_EM_CALLBACK_NODATA(gamepaddisconnected, joy_callback_func) @@ -282,9 +521,24 @@ void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, i #undef SET_EM_CALLBACK #undef EM_CHECK + /* clang-format off */ + EM_ASM_ARGS({ + const send_notification = Module.cwrap('send_notification', null, ['number']); + const notifs = arguments; + (['mouseover', 'mouseleave', 'focus', 'blur']).forEach(function(event, i) { + Module.canvas.addEventListener(event, send_notification.bind(this, notifs[i])); + }); + }, + MainLoop::NOTIFICATION_WM_MOUSE_ENTER, + MainLoop::NOTIFICATION_WM_MOUSE_EXIT, + MainLoop::NOTIFICATION_WM_FOCUS_IN, + MainLoop::NOTIFICATION_WM_FOCUS_OUT + ); +/* clang-format on */ + #ifdef JAVASCRIPT_EVAL_ENABLED javascript_eval = memnew(JavaScript); - GlobalConfig::get_singleton()->add_singleton(GlobalConfig::Singleton("JavaScript", javascript_eval)); + ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("JavaScript", javascript_eval)); #endif visual_server->init(); @@ -315,30 +569,97 @@ void OS_JavaScript::alert(const String &p_alert, const String &p_title) { /* clang-format on */ } -void OS_JavaScript::set_mouse_show(bool p_show) { +static const char *godot2dom_cursor(OS::CursorShape p_shape) { + + switch (p_shape) { + case OS::CURSOR_ARROW: + default: + return "auto"; + case OS::CURSOR_IBEAM: return "text"; + case OS::CURSOR_POINTING_HAND: return "pointer"; + case OS::CURSOR_CROSS: return "crosshair"; + case OS::CURSOR_WAIT: return "progress"; + case OS::CURSOR_BUSY: return "wait"; + case OS::CURSOR_DRAG: return "grab"; + case OS::CURSOR_CAN_DROP: return "grabbing"; + case OS::CURSOR_FORBIDDEN: return "no-drop"; + case OS::CURSOR_VSIZE: return "ns-resize"; + case OS::CURSOR_HSIZE: return "ew-resize"; + case OS::CURSOR_BDIAGSIZE: return "nesw-resize"; + case OS::CURSOR_FDIAGSIZE: return "nwse-resize"; + case OS::CURSOR_MOVE: return "move"; + case OS::CURSOR_VSPLIT: return "row-resize"; + case OS::CURSOR_HSPLIT: return "col-resize"; + case OS::CURSOR_HELP: return "help"; + } +} + +void OS_JavaScript::set_css_cursor(const char *p_cursor) { - //javascript has no mouse... + /* clang-format off */ + EM_ASM_({ + Module.canvas.style.cursor = Module.UTF8ToString($0); + }, p_cursor); + /* clang-format on */ } -void OS_JavaScript::set_mouse_grab(bool p_grab) { +const char *OS_JavaScript::get_css_cursor() const { - //it really has no mouse...! + char cursor[16]; + /* clang-format off */ + EM_ASM_INT({ + Module.stringToUTF8(Module.canvas.style.cursor ? Module.canvas.style.cursor : 'auto', $0, 16); + }, cursor); + /* clang-format on */ + return cursor; } -bool OS_JavaScript::is_mouse_grab_enabled() const { +void OS_JavaScript::set_mouse_mode(OS::MouseMode p_mode) { - //*sigh* technology has evolved so much since i was a kid.. - return false; + ERR_FAIL_INDEX(p_mode, MOUSE_MODE_CONFINED + 1); + ERR_EXPLAIN("MOUSE_MODE_CONFINED is not supported for the HTML5 platform"); + ERR_FAIL_COND(p_mode == MOUSE_MODE_CONFINED); + if (p_mode == get_mouse_mode()) + return; + + if (p_mode == MOUSE_MODE_VISIBLE) { + + set_css_cursor(godot2dom_cursor(cursor_shape)); + emscripten_exit_pointerlock(); + + } else if (p_mode == MOUSE_MODE_HIDDEN) { + + set_css_cursor("none"); + emscripten_exit_pointerlock(); + + } else if (p_mode == MOUSE_MODE_CAPTURED) { + + EMSCRIPTEN_RESULT result = emscripten_request_pointerlock("canvas", false); + ERR_EXPLAIN("MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback"); + ERR_FAIL_COND(result == EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED); + ERR_FAIL_COND(result != EMSCRIPTEN_RESULT_SUCCESS); + set_css_cursor(godot2dom_cursor(cursor_shape)); + } +} + +OS::MouseMode OS_JavaScript::get_mouse_mode() const { + + if (!strcmp(get_css_cursor(), "none")) + return MOUSE_MODE_HIDDEN; + + EmscriptenPointerlockChangeEvent ev; + emscripten_get_pointerlock_status(&ev); + return ev.isActive && (strcmp(ev.id, "canvas") == 0) ? MOUSE_MODE_CAPTURED : MOUSE_MODE_VISIBLE; } -Point2 OS_JavaScript::get_mouse_pos() const { +Point2 OS_JavaScript::get_mouse_position() const { - return input->get_mouse_pos(); + return input->get_mouse_position(); } int OS_JavaScript::get_mouse_button_state() const { - return last_button_mask; + return input->get_mouse_button_mask(); } void OS_JavaScript::set_window_title(const String &p_title) { @@ -376,14 +697,17 @@ Size2 OS_JavaScript::get_screen_size(int p_screen) const { void OS_JavaScript::set_window_size(const Size2 p_size) { - window_maximized = false; + windowed_size = p_size; if (is_window_fullscreen()) { + window_maximized = false; set_window_fullscreen(false); + } else if (is_window_maximized()) { + set_window_maximized(false); + } else { + video_mode.width = p_size.x; + video_mode.height = p_size.y; + emscripten_set_canvas_size(p_size.x, p_size.y); } - _windowed_size = p_size; - video_mode.width = p_size.x; - video_mode.height = p_size.y; - emscripten_set_canvas_size(p_size.x, p_size.y); } Size2 OS_JavaScript::get_window_size() const { @@ -396,20 +720,30 @@ Size2 OS_JavaScript::get_window_size() const { void OS_JavaScript::set_window_maximized(bool p_enabled) { window_maximized = p_enabled; - if (p_enabled) { - - if (is_window_fullscreen()) { - // _browser_resize callback will set canvas size - set_window_fullscreen(false); - } else { - /* clang-format off */ - video_mode.width = EM_ASM_INT_V(return window.innerWidth); - video_mode.height = EM_ASM_INT_V(return window.innerHeight); - /* clang-format on */ - emscripten_set_canvas_size(video_mode.width, video_mode.height); - } - } else { - set_window_size(_windowed_size); + if (is_window_fullscreen()) { + set_window_fullscreen(false); + return; + } + // Calling emscripten_enter_soft_fullscreen mutltiple times hides all + // page elements except the canvas permanently, so track state + if (p_enabled && !soft_fs_enabled) { + + EmscriptenFullscreenStrategy strategy; + strategy.scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH; + strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF; + strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT; + strategy.canvasResizedCallback = NULL; + emscripten_enter_soft_fullscreen(NULL, &strategy); + soft_fs_enabled = true; + video_mode.width = get_window_size().width; + video_mode.height = get_window_size().height; + } else if (!p_enabled) { + + emscripten_exit_soft_fullscreen(); + soft_fs_enabled = false; + video_mode.width = windowed_size.width; + video_mode.height = windowed_size.height; + emscripten_set_canvas_size(video_mode.width, video_mode.height); } } @@ -423,9 +757,17 @@ void OS_JavaScript::set_window_fullscreen(bool p_enable) { // _browser_resize_callback or _fullscreen_change_callback EMSCRIPTEN_RESULT result; if (p_enable) { - /* clang-format off */ - EM_ASM(Module.requestFullscreen(false, false);); - /* clang-format on */ + if (window_maximized) { + // soft fs during real fs can cause issues + set_window_maximized(false); + window_maximized = true; + } + EmscriptenFullscreenStrategy strategy; + strategy.scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH; + strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF; + strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT; + strategy.canvasResizedCallback = NULL; + emscripten_request_fullscreen_strategy(NULL, false, &strategy); } else { result = emscripten_exit_fullscreen(); if (result != EMSCRIPTEN_RESULT_SUCCESS) { @@ -439,6 +781,11 @@ bool OS_JavaScript::is_window_fullscreen() const { return video_mode.fullscreen; } +void OS_JavaScript::request_canvas_size_adjustment() { + + canvas_size_adjustment_requested = true; +} + void OS_JavaScript::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const { Size2 screen = get_screen_size(); @@ -462,7 +809,11 @@ bool OS_JavaScript::can_draw() const { void OS_JavaScript::set_cursor_shape(CursorShape p_shape) { - //javascript really really really has no mouse.. how amazing.. + ERR_FAIL_INDEX(p_shape, CURSOR_MAX); + + cursor_shape = p_shape; + if (get_mouse_mode() != MOUSE_MODE_HIDDEN) + set_css_cursor(godot2dom_cursor(cursor_shape)); } void OS_JavaScript::main_loop_begin() { @@ -494,6 +845,17 @@ bool OS_JavaScript::main_loop_iterate() { } } process_joypads(); + if (canvas_size_adjustment_requested) { + + if (video_mode.fullscreen || window_maximized) { + video_mode.width = get_window_size().width; + video_mode.height = get_window_size().height; + } + if (!video_mode.fullscreen) { + set_window_maximized(window_maximized); + } + canvas_size_adjustment_requested = false; + } return Main::iteration(); } @@ -503,222 +865,6 @@ void OS_JavaScript::main_loop_end() { main_loop->finish(); } -void OS_JavaScript::main_loop_focusout() { - - if (main_loop) - main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT); - //audio_driver_javascript.set_pause(true); -} - -void OS_JavaScript::main_loop_focusin() { - - if (main_loop) - main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN); - //audio_driver_javascript.set_pause(false); -} - -void OS_JavaScript::push_input(const InputEvent &p_ev) { - - InputEvent ev = p_ev; - ev.ID = last_id++; - if (ev.type == InputEvent::MOUSE_MOTION) { - input->set_mouse_pos(Point2(ev.mouse_motion.x, ev.mouse_motion.y)); - } else if (ev.type == InputEvent::MOUSE_BUTTON) { - last_button_mask = ev.mouse_button.button_mask; - } - input->parse_input_event(p_ev); -} - -void OS_JavaScript::process_touch(int p_what, int p_pointer, const Vector<TouchPos> &p_points) { - - //print_line("ev: "+itos(p_what)+" pnt: "+itos(p_pointer)+" pointc: "+itos(p_points.size())); - - switch (p_what) { - case 0: { //gesture begin - - if (touch.size()) { - //end all if exist - InputEvent ev; - ev.type = InputEvent::MOUSE_BUTTON; - ev.ID = last_id++; - ev.mouse_button.button_index = BUTTON_LEFT; - ev.mouse_button.button_mask = BUTTON_MASK_LEFT; - ev.mouse_button.pressed = false; - ev.mouse_button.x = touch[0].pos.x; - ev.mouse_button.y = touch[0].pos.y; - ev.mouse_button.global_x = touch[0].pos.x; - ev.mouse_button.global_y = touch[0].pos.y; - input->parse_input_event(ev); - - for (int i = 0; i < touch.size(); i++) { - - InputEvent ev; - ev.type = InputEvent::SCREEN_TOUCH; - ev.ID = last_id++; - ev.screen_touch.index = touch[i].id; - ev.screen_touch.pressed = false; - ev.screen_touch.x = touch[i].pos.x; - ev.screen_touch.y = touch[i].pos.y; - input->parse_input_event(ev); - } - } - - touch.resize(p_points.size()); - for (int i = 0; i < p_points.size(); i++) { - touch[i].id = p_points[i].id; - touch[i].pos = p_points[i].pos; - } - - { - //send mouse - InputEvent ev; - ev.type = InputEvent::MOUSE_BUTTON; - ev.ID = last_id++; - ev.mouse_button.button_index = BUTTON_LEFT; - ev.mouse_button.button_mask = BUTTON_MASK_LEFT; - ev.mouse_button.pressed = true; - ev.mouse_button.x = touch[0].pos.x; - ev.mouse_button.y = touch[0].pos.y; - ev.mouse_button.global_x = touch[0].pos.x; - ev.mouse_button.global_y = touch[0].pos.y; - last_mouse = touch[0].pos; - input->parse_input_event(ev); - } - - //send touch - for (int i = 0; i < touch.size(); i++) { - - InputEvent ev; - ev.type = InputEvent::SCREEN_TOUCH; - ev.ID = last_id++; - ev.screen_touch.index = touch[i].id; - ev.screen_touch.pressed = true; - ev.screen_touch.x = touch[i].pos.x; - ev.screen_touch.y = touch[i].pos.y; - input->parse_input_event(ev); - } - - } break; - case 1: { //motion - - if (p_points.size()) { - //send mouse, should look for point 0? - InputEvent ev; - ev.type = InputEvent::MOUSE_MOTION; - ev.ID = last_id++; - ev.mouse_motion.button_mask = BUTTON_MASK_LEFT; - ev.mouse_motion.x = p_points[0].pos.x; - ev.mouse_motion.y = p_points[0].pos.y; - input->set_mouse_pos(Point2(ev.mouse_motion.x, ev.mouse_motion.y)); - ev.mouse_motion.speed_x = input->get_last_mouse_speed().x; - ev.mouse_motion.speed_y = input->get_last_mouse_speed().y; - ev.mouse_motion.relative_x = p_points[0].pos.x - last_mouse.x; - ev.mouse_motion.relative_y = p_points[0].pos.y - last_mouse.y; - last_mouse = p_points[0].pos; - input->parse_input_event(ev); - } - - ERR_FAIL_COND(touch.size() != p_points.size()); - - for (int i = 0; i < touch.size(); i++) { - - int idx = -1; - for (int j = 0; j < p_points.size(); j++) { - - if (touch[i].id == p_points[j].id) { - idx = j; - break; - } - } - - ERR_CONTINUE(idx == -1); - - if (touch[i].pos == p_points[idx].pos) - continue; //no move unncesearily - - InputEvent ev; - ev.type = InputEvent::SCREEN_DRAG; - ev.ID = last_id++; - ev.screen_drag.index = touch[i].id; - ev.screen_drag.x = p_points[idx].pos.x; - ev.screen_drag.y = p_points[idx].pos.y; - ev.screen_drag.relative_x = p_points[idx].pos.x - touch[i].pos.x; - ev.screen_drag.relative_y = p_points[idx].pos.y - touch[i].pos.y; - input->parse_input_event(ev); - touch[i].pos = p_points[idx].pos; - } - - } break; - case 2: { //release - - if (touch.size()) { - //end all if exist - InputEvent ev; - ev.type = InputEvent::MOUSE_BUTTON; - ev.ID = last_id++; - ev.mouse_button.button_index = BUTTON_LEFT; - ev.mouse_button.button_mask = BUTTON_MASK_LEFT; - ev.mouse_button.pressed = false; - ev.mouse_button.x = touch[0].pos.x; - ev.mouse_button.y = touch[0].pos.y; - ev.mouse_button.global_x = touch[0].pos.x; - ev.mouse_button.global_y = touch[0].pos.y; - input->parse_input_event(ev); - - for (int i = 0; i < touch.size(); i++) { - - InputEvent ev; - ev.type = InputEvent::SCREEN_TOUCH; - ev.ID = last_id++; - ev.screen_touch.index = touch[i].id; - ev.screen_touch.pressed = false; - ev.screen_touch.x = touch[i].pos.x; - ev.screen_touch.y = touch[i].pos.y; - input->parse_input_event(ev); - } - touch.clear(); - } - - } break; - case 3: { // add tuchi - - ERR_FAIL_INDEX(p_pointer, p_points.size()); - - TouchPos tp = p_points[p_pointer]; - touch.push_back(tp); - - InputEvent ev; - ev.type = InputEvent::SCREEN_TOUCH; - ev.ID = last_id++; - ev.screen_touch.index = tp.id; - ev.screen_touch.pressed = true; - ev.screen_touch.x = tp.pos.x; - ev.screen_touch.y = tp.pos.y; - input->parse_input_event(ev); - - } break; - case 4: { - - for (int i = 0; i < touch.size(); i++) { - if (touch[i].id == p_pointer) { - - InputEvent ev; - ev.type = InputEvent::SCREEN_TOUCH; - ev.ID = last_id++; - ev.screen_touch.index = touch[i].id; - ev.screen_touch.pressed = false; - ev.screen_touch.x = touch[i].pos.x; - ev.screen_touch.y = touch[i].pos.y; - input->parse_input_event(ev); - touch.remove(i); - i--; - } - } - - } break; - } -} - void OS_JavaScript::process_accelerometer(const Vector3 &p_accelerometer) { input->set_accelerometer(p_accelerometer); @@ -726,7 +872,11 @@ void OS_JavaScript::process_accelerometer(const Vector3 &p_accelerometer) { bool OS_JavaScript::has_touchscreen_ui_hint() const { - return false; //??? + /* clang-format off */ + return EM_ASM_INT_V( + return 'ontouchstart' in window; + ); + /* clang-format on */ } void OS_JavaScript::main_loop_request_quit() { @@ -756,12 +906,12 @@ String OS_JavaScript::get_data_dir() const { return get_data_dir_func(); */ return "/userfs"; - //return GlobalConfig::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir"); + //return ProjectSettings::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir"); }; String OS_JavaScript::get_executable_path() const { - return String(); + return OS::get_executable_path(); } void OS_JavaScript::_close_notification_funcs(const String &p_file, int p_flags) { @@ -790,9 +940,9 @@ void OS_JavaScript::process_joypads() { InputDefault::JoyAxis jx; jx.min = 0; jx.value = value; - last_id = input->joy_axis(last_id, i, j, jx); + input->joy_axis(i, j, jx); } else { - last_id = input->joy_button(last_id, i, j, value); + input->joy_button(i, j, value); } } for (int j = 0; j < num_axes; j++) { @@ -800,7 +950,7 @@ void OS_JavaScript::process_joypads() { InputDefault::JoyAxis jx; jx.min = -1; jx.value = state.axis[j]; - last_id = input->joy_axis(last_id, i, j, jx); + input->joy_axis(i, j, jx); } } } @@ -839,14 +989,18 @@ int OS_JavaScript::get_power_percent_left() { return power_manager->get_power_percent_left(); } -OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func) { - gfx_init_func = p_gfx_init_func; - gfx_init_ud = p_gfx_init_ud; - last_button_mask = 0; +bool OS_JavaScript::_check_internal_feature_support(const String &p_feature) { + + return p_feature == "web" || p_feature == "s3tc"; // TODO check for these features really being available +} + +OS_JavaScript::OS_JavaScript(const char *p_execpath, GetDataDirFunc p_get_data_dir_func) { + set_cmdline(p_execpath, get_cmdline_args()); main_loop = NULL; - last_id = 1; gl_extensions = NULL; window_maximized = false; + soft_fs_enabled = false; + canvas_size_adjustment_requested = false; get_data_dir_func = p_get_data_dir_func; FileAccessUnix::close_notification_func = _close_notification_funcs; |