summaryrefslogtreecommitdiff
path: root/platform/javascript
diff options
context:
space:
mode:
Diffstat (limited to 'platform/javascript')
-rw-r--r--platform/javascript/export/export.cpp2
-rw-r--r--platform/javascript/javascript_main.cpp148
-rw-r--r--platform/javascript/os_javascript.cpp553
-rw-r--r--platform/javascript/os_javascript.h29
4 files changed, 341 insertions, 391 deletions
diff --git a/platform/javascript/export/export.cpp b/platform/javascript/export/export.cpp
index 1a3c5f3e8f..ea388072c5 100644
--- a/platform/javascript/export/export.cpp
+++ b/platform/javascript/export/export.cpp
@@ -316,7 +316,7 @@ Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_prese
EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() {
- Image img(_javascript_logo);
+ Ref<Image> img = memnew(Image(_javascript_logo));
logo.instance();
logo->create_from_image(img);
}
diff --git a/platform/javascript/javascript_main.cpp b/platform/javascript/javascript_main.cpp
index ff29c21b1c..6b1d574496 100644
--- a/platform/javascript/javascript_main.cpp
+++ b/platform/javascript/javascript_main.cpp
@@ -31,166 +31,50 @@
#include "io/resource_loader.h"
#include "main/main.h"
#include "os_javascript.h"
-#include <GL/glut.h>
-#include <string.h>
OS_JavaScript *os = NULL;
-static void _gfx_init(void *ud, bool gl2, int w, int h, bool fs) {
+static void main_loop() {
- glutInitWindowSize(w, h);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutCreateWindow("godot");
+ os->main_loop_iterate();
}
-static uint32_t _mouse_button_mask = 0;
+extern "C" void main_after_fs_sync() {
-static void _glut_mouse_button(int button, int state, int x, int y) {
-
- InputEvent ev;
- ev.type = InputEvent::MOUSE_BUTTON;
- switch (button) {
- case GLUT_LEFT_BUTTON: ev.mouse_button.button_index = BUTTON_LEFT; break;
- case GLUT_MIDDLE_BUTTON: ev.mouse_button.button_index = BUTTON_MIDDLE; break;
- case GLUT_RIGHT_BUTTON: ev.mouse_button.button_index = BUTTON_RIGHT; break;
- case 3: ev.mouse_button.button_index = BUTTON_WHEEL_UP; break;
- case 4: ev.mouse_button.button_index = BUTTON_WHEEL_DOWN; break;
- }
-
- ev.mouse_button.pressed = state == GLUT_DOWN;
- ev.mouse_button.x = x;
- ev.mouse_button.y = y;
- ev.mouse_button.global_x = x;
- ev.mouse_button.global_y = y;
-
- if (ev.mouse_button.button_index < 4) {
- if (ev.mouse_button.pressed) {
- _mouse_button_mask |= 1 << (ev.mouse_button.button_index - 1);
- } else {
- _mouse_button_mask &= ~(1 << (ev.mouse_button.button_index - 1));
- }
- }
- ev.mouse_button.button_mask = _mouse_button_mask;
-
- uint32_t m = glutGetModifiers();
- ev.mouse_button.mod.alt = (m & GLUT_ACTIVE_ALT) != 0;
- ev.mouse_button.mod.shift = (m & GLUT_ACTIVE_SHIFT) != 0;
- ev.mouse_button.mod.control = (m & GLUT_ACTIVE_CTRL) != 0;
-
- os->push_input(ev);
-
- if (ev.mouse_button.button_index == BUTTON_WHEEL_UP || ev.mouse_button.button_index == BUTTON_WHEEL_DOWN) {
- // GLUT doesn't send release events for mouse wheel, so send manually
- ev.mouse_button.pressed = false;
- os->push_input(ev);
- }
-}
-
-static int _glut_prev_x = 0;
-static int _glut_prev_y = 0;
-
-static void _glut_mouse_motion(int x, int y) {
-
- InputEvent ev;
- ev.type = InputEvent::MOUSE_MOTION;
- ev.mouse_motion.button_mask = _mouse_button_mask;
- ev.mouse_motion.x = x;
- ev.mouse_motion.y = y;
- ev.mouse_motion.global_x = x;
- ev.mouse_motion.global_y = y;
- ev.mouse_motion.relative_x = x - _glut_prev_x;
- ev.mouse_motion.relative_y = y - _glut_prev_y;
- _glut_prev_x = x;
- _glut_prev_y = y;
-
- uint32_t m = glutGetModifiers();
- ev.mouse_motion.mod.alt = (m & GLUT_ACTIVE_ALT) != 0;
- ev.mouse_motion.mod.shift = (m & GLUT_ACTIVE_SHIFT) != 0;
- ev.mouse_motion.mod.control = (m & GLUT_ACTIVE_CTRL) != 0;
-
- os->push_input(ev);
-}
-
-static void _gfx_idle() {
-
- glutPostRedisplay();
-}
-
-int start_step = 0;
-
-static void _godot_draw(void) {
-
- if (start_step == 1) {
- start_step = 2;
- Main::start();
- os->main_loop_begin();
- }
-
- if (start_step == 2) {
- os->main_loop_iterate();
- }
-
- glutSwapBuffers();
-}
-
-extern "C" {
-
-void main_after_fs_sync() {
-
- start_step = 1;
-}
+ // Ease up compatibility
+ ResourceLoader::set_abort_on_missing_resources(false);
+ Main::start();
+ os->main_loop_begin();
+ emscripten_set_main_loop(main_loop, 0, false);
}
int main(int argc, char *argv[]) {
- /* Initialize the window */
printf("let it go dude!\n");
- glutInit(&argc, argv);
- os = new OS_JavaScript(argv[0], _gfx_init, NULL, NULL);
- Error err = Main::setup(argv[0], argc - 1, &argv[1]);
-
- ResourceLoader::set_abort_on_missing_resources(false); //ease up compatibility
-
- glutMouseFunc(_glut_mouse_button);
- glutMotionFunc(_glut_mouse_motion);
- glutPassiveMotionFunc(_glut_mouse_motion);
-
- /* Set up glut callback functions */
- glutIdleFunc(_gfx_idle);
- // glutReshapeFunc(gears_reshape);
- glutDisplayFunc(_godot_draw);
- //glutSpecialFunc(gears_special);
-
- //mount persistent file system
+ // sync from persistent state into memory and then
+ // run the 'main_after_fs_sync' function
/* clang-format off */
EM_ASM(
+ Module.noExitRuntime = true;
FS.mkdir('/userfs');
FS.mount(IDBFS, {}, '/userfs');
-
- // sync from persistent state into memory and then
- // run the 'main_after_fs_sync' function
FS.syncfs(true, function(err) {
-
if (err) {
Module.setStatus('Failed to load persistent data\nPlease allow (third-party) cookies');
Module.printErr('Failed to populate IDB file system: ' + err.message);
- Module.exit();
+ Module.noExitRuntime = false;
} else {
Module.print('Successfully populated IDB file system');
- ccall('main_after_fs_sync', 'void', []);
+ ccall('main_after_fs_sync', null);
}
});
);
/* clang-format on */
- glutMainLoop();
+ os = new OS_JavaScript(argv[0], NULL);
+ Error err = Main::setup(argv[0], argc - 1, &argv[1]);
return 0;
+ // continued async in main_after_fs_sync() from syncfs() callback
}
-
-/*
- *
- *09] <azakai|2__> reduz: yes, define TOTAL_MEMORY on Module. for example var Module = { TOTAL_MEMORY: 12345.. }; before the main
- *
- */
diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp
index 71754502cb..9df26f1471 100644
--- a/platform/javascript/os_javascript.cpp
+++ b/platform/javascript/os_javascript.cpp
@@ -41,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;
@@ -130,16 +143,192 @@ static EM_BOOL _fullscreen_change_callback(int event_type, const EmscriptenFulls
return false;
}
-static InputEvent _setup_key_event(const EmscriptenKeyboardEvent *emscripten_event) {
+static InputDefault *_input;
+
+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())
+ mask |= 1 << ev->get_button_index();
+ else
+ mask &= ~(1 << ev->get_button_index());
+ ev->set_button_mask(mask >> 1);
+
+ _input->parse_input_event(ev);
+ 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);
+
+ Ref<InputEventMouseMotion> ev;
+ ev.instance();
+ dom2godot_mod(mouse_event, ev);
+ ev->set_button_mask(_input->get_mouse_button_mask() >> 1);
+
+ ev->set_position(Point2(mouse_event->canvasX, mouse_event->canvasY));
+ 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);
+ return true;
+}
+
+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);
- 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<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) {
+
+ 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`)
@@ -148,26 +337,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;
}
@@ -175,8 +364,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;
}
@@ -184,10 +373,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) {
@@ -202,14 +391,18 @@ void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, i
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];
@@ -257,25 +450,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("#canvas", mousemove, _mousemove_callback)
+ SET_EM_CALLBACK("#canvas", mousedown, _mousebutton_callback)
+ SET_EM_CALLBACK("#canvas", mouseup, _mousebutton_callback)
+ SET_EM_CALLBACK("#canvas", wheel, _wheel_callback)
+ SET_EM_CALLBACK("#canvas", touchstart, _touchpress_callback)
+ SET_EM_CALLBACK("#canvas", touchmove, _touchmove_callback)
+ SET_EM_CALLBACK("#canvas", touchend, _touchpress_callback)
+ SET_EM_CALLBACK("#canvas", touchcancel, _touchpress_callback)
+ SET_EM_CALLBACK(NULL, keydown, _keydown_callback)
+ SET_EM_CALLBACK(NULL, keypress, _keypress_callback)
+ SET_EM_CALLBACK(NULL, 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)
@@ -316,20 +518,87 @@ 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";
+ }
+}
- //javascript has no mouse...
+void OS_JavaScript::set_css_cursor(const char *p_cursor) {
+
+ /* 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_position() const {
@@ -339,7 +608,7 @@ Point2 OS_JavaScript::get_mouse_position() const {
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) {
@@ -463,7 +732,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() {
@@ -518,197 +791,6 @@ void OS_JavaScript::main_loop_focusin() {
//audio_driver_javascript.set_pause(false);
}
-void OS_JavaScript::push_input(const InputEvent &p_ev) {
-
- InputEvent ev = p_ev;
- if (ev.type == InputEvent::MOUSE_MOTION) {
- input->set_mouse_position(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.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.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.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.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.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_position(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.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.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.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.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.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);
@@ -829,11 +911,8 @@ int OS_JavaScript::get_power_percent_left() {
return power_manager->get_power_percent_left();
}
-OS_JavaScript::OS_JavaScript(const char *p_execpath, GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func) {
+OS_JavaScript::OS_JavaScript(const char *p_execpath, GetDataDirFunc p_get_data_dir_func) {
set_cmdline(p_execpath, get_cmdline_args());
- gfx_init_func = p_gfx_init_func;
- gfx_init_ud = p_gfx_init_ud;
- last_button_mask = 0;
main_loop = NULL;
gl_extensions = NULL;
window_maximized = false;
diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h
index ea906c560f..65269148ec 100644
--- a/platform/javascript/os_javascript.h
+++ b/platform/javascript/os_javascript.h
@@ -45,24 +45,9 @@
#include <emscripten/html5.h>
-typedef void (*GFXInitFunc)(void *ud, bool gl2, int w, int h, bool fs);
typedef String (*GetDataDirFunc)();
class OS_JavaScript : public OS_Unix {
-public:
- struct TouchPos {
- int id;
- Point2 pos;
- };
-
-private:
- Vector<TouchPos> touch;
- Point2 last_mouse;
- int last_button_mask;
- GFXInitFunc gfx_init_func;
- void *gfx_init_ud;
-
- bool use_gl2;
int64_t time_to_save_sync;
int64_t last_sync_time;
@@ -76,6 +61,7 @@ private:
InputDefault *input;
bool window_maximized;
VideoMode video_mode;
+ CursorShape cursor_shape;
MainLoop *main_loop;
GetDataDirFunc get_data_dir_func;
@@ -90,6 +76,9 @@ private:
void process_joypads();
+ void set_css_cursor(const char *);
+ const char *get_css_cursor() const;
+
public:
// functions used by main to initialize/deintialize the OS
virtual int get_video_driver_count() const;
@@ -119,9 +108,8 @@ public:
virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
- virtual void set_mouse_show(bool p_show);
- virtual void set_mouse_grab(bool p_grab);
- virtual bool is_mouse_grab_enabled() const;
+ virtual void set_mouse_mode(MouseMode p_mode);
+ virtual MouseMode get_mouse_mode() const;
virtual Point2 get_mouse_position() const;
virtual int get_mouse_button_state() const;
virtual void set_window_title(const String &p_title);
@@ -166,8 +154,7 @@ public:
virtual String get_resource_dir() const;
void process_accelerometer(const Vector3 &p_accelerometer);
- void process_touch(int p_what, int p_pointer, const Vector<TouchPos> &p_points);
- void push_input(const InputEvent &p_ev);
+ void push_input(const Ref<InputEvent> &p_ev);
virtual bool is_joy_known(int p_device);
virtual String get_joy_guid(int p_device) const;
@@ -177,7 +164,7 @@ public:
virtual int get_power_seconds_left();
virtual int get_power_percent_left();
- OS_JavaScript(const char *p_execpath, GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func);
+ OS_JavaScript(const char *p_execpath, GetDataDirFunc p_get_data_dir_func);
~OS_JavaScript();
};