2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* os_javascript.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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 "os_javascript.h"
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "core/global_config.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "core/io/file_access_buffered_fa.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "dom_keys.h"
|
|
|
|
#include "drivers/gles3/rasterizer_gles3.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "drivers/unix/dir_access_unix.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "drivers/unix/file_access_unix.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "main/main.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "servers/visual/visual_server_raster.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-16 18:19:45 +00:00
|
|
|
#include <emscripten.h>
|
2017-03-05 15:44:50 +00:00
|
|
|
#include <stdlib.h>
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2017-04-24 19:41:39 +00:00
|
|
|
#define DOM_BUTTON_LEFT 0
|
|
|
|
#define DOM_BUTTON_MIDDLE 1
|
|
|
|
#define DOM_BUTTON_RIGHT 2
|
2017-04-21 03:01:31 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static InputModifierState dom2godot_mod(T emscripten_event_ptr) {
|
|
|
|
|
|
|
|
InputModifierState mod;
|
|
|
|
mod.shift = emscripten_event_ptr->shiftKey;
|
|
|
|
mod.alt = emscripten_event_ptr->altKey;
|
|
|
|
mod.control = emscripten_event_ptr->ctrlKey;
|
|
|
|
mod.meta = emscripten_event_ptr->metaKey;
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int OS_JavaScript::get_video_driver_count() const {
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2017-01-16 18:19:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const char *OS_JavaScript::get_video_driver_name(int p_driver) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-31 02:46:30 +00:00
|
|
|
return "GLES3";
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OS::VideoMode OS_JavaScript::get_default_video_mode() const {
|
|
|
|
|
|
|
|
return OS::VideoMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
int OS_JavaScript::get_audio_driver_count() const {
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const char *OS_JavaScript::get_audio_driver_name(int p_driver) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return "JavaScript";
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::initialize_core() {
|
|
|
|
|
|
|
|
OS_Unix::initialize_core();
|
|
|
|
FileAccess::make_default<FileAccessBufferedFA<FileAccessUnix> >(FileAccess::ACCESS_RESOURCES);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void OS_JavaScript::set_opengl_extensions(const char *p_gl_extensions) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!p_gl_extensions);
|
2017-03-05 15:44:50 +00:00
|
|
|
gl_extensions = p_gl_extensions;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-11-26 12:13:16 +00:00
|
|
|
static EM_BOOL _browser_resize_callback(int event_type, const EmscriptenUiEvent *ui_event, void *user_data) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_RESIZE, false);
|
2016-11-26 12:13:16 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
OS_JavaScript *os = static_cast<OS_JavaScript *>(user_data);
|
2016-11-26 12:13:16 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Size2 _windowed_size;
|
|
|
|
|
|
|
|
static EM_BOOL _fullscreen_change_callback(int event_type, const EmscriptenFullscreenChangeEvent *event, void *user_data) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_FULLSCREENCHANGE, false);
|
2016-11-26 12:13:16 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
OS_JavaScript *os = static_cast<OS_JavaScript *>(user_data);
|
2016-11-26 12:13:16 +00:00
|
|
|
String id = String::utf8(event->id);
|
|
|
|
|
|
|
|
// empty id is canvas
|
2017-03-05 15:44:50 +00:00
|
|
|
if (id.empty() || id == "canvas") {
|
2016-11-26 12:13:16 +00:00
|
|
|
|
|
|
|
OS::VideoMode vm = os->get_video_mode();
|
|
|
|
// this event property is the only reliable information on
|
|
|
|
// browser fullscreen state
|
|
|
|
vm.fullscreen = event->isFullscreen;
|
|
|
|
|
|
|
|
if (event->isFullscreen) {
|
|
|
|
vm.width = event->screenWidth;
|
|
|
|
vm.height = event->screenHeight;
|
|
|
|
os->set_video_mode(vm);
|
|
|
|
emscripten_set_canvas_size(vm.width, vm.height);
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2016-11-26 12:13:16 +00:00
|
|
|
os->set_video_mode(vm);
|
|
|
|
if (!os->is_window_maximized()) {
|
|
|
|
os->set_window_size(_windowed_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-24 19:41:39 +00:00
|
|
|
static InputDefault *_input;
|
2017-04-21 03:01:31 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEvent> ev;
|
|
|
|
ev.type = Ref<InputEvent>::MOUSE_BUTTON;
|
|
|
|
ev->is_pressed() = event_type == EMSCRIPTEN_EVENT_MOUSEDOWN;
|
|
|
|
ev.mouse_button.global_x = ev->get_pos().x = mouse_event->canvasX;
|
|
|
|
ev.mouse_button.global_y = ev->get_pos().y = mouse_event->canvasY;
|
2017-04-21 03:01:31 +00:00
|
|
|
ev.mouse_button.mod = dom2godot_mod(mouse_event);
|
|
|
|
|
|
|
|
switch (mouse_event->button) {
|
2017-05-20 15:38:03 +00:00
|
|
|
case DOM_BUTTON_LEFT: ev->get_button_index() = BUTTON_LEFT; break;
|
|
|
|
case DOM_BUTTON_MIDDLE: ev->get_button_index() = BUTTON_MIDDLE; break;
|
|
|
|
case DOM_BUTTON_RIGHT: ev->get_button_index() = BUTTON_RIGHT; break;
|
2017-04-21 03:01:31 +00:00
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->get_button_mask() = _input->get_mouse_button_mask();
|
|
|
|
if (ev->is_pressed())
|
|
|
|
ev->get_button_mask() |= 1 << ev->get_button_index();
|
2017-04-21 03:01:31 +00:00
|
|
|
else
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->get_button_mask() &= ~(1 << ev->get_button_index());
|
|
|
|
ev->get_button_mask() >>= 1;
|
2017-04-21 03:01:31 +00:00
|
|
|
|
|
|
|
_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);
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEvent> ev;
|
|
|
|
ev.type = Ref<InputEvent>::MOUSE_MOTION;
|
2017-04-21 03:01:31 +00:00
|
|
|
ev.mouse_motion.mod = dom2godot_mod(mouse_event);
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->get_button_mask() = _input->get_mouse_button_mask() >> 1;
|
2017-04-21 03:01:31 +00:00
|
|
|
|
|
|
|
ev.mouse_motion.global_x = ev.mouse_motion.x = mouse_event->canvasX;
|
|
|
|
ev.mouse_motion.global_y = ev.mouse_motion.y = mouse_event->canvasY;
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->get_relative().x = _input->get_mouse_position().x - ev.mouse_motion.x;
|
|
|
|
ev->get_relative().y = _input->get_mouse_position().y - ev.mouse_motion.y;
|
2017-04-21 03:01:31 +00:00
|
|
|
|
|
|
|
_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;
|
|
|
|
|
|
|
|
_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);
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEvent> ev;
|
|
|
|
ev.type = Ref<InputEvent>::MOUSE_BUTTON;
|
|
|
|
ev->get_button_mask() = _input->get_mouse_button_mask() >> 1;
|
|
|
|
ev.mouse_button.global_x = ev->get_pos().x = _input->get_mouse_position().x;
|
|
|
|
ev.mouse_button.global_y = ev->get_pos().y = _input->get_mouse_position().y;
|
|
|
|
ev.mouse_button->get_shift() = _input->is_key_pressed(KEY_SHIFT);
|
|
|
|
ev.mouse_button->get_alt() = _input->is_key_pressed(KEY_ALT);
|
|
|
|
ev.mouse_button->get_control() = _input->is_key_pressed(KEY_CONTROL);
|
|
|
|
ev.mouse_button->get_metakey() = _input->is_key_pressed(KEY_META);
|
2017-04-21 03:01:31 +00:00
|
|
|
|
|
|
|
if (wheel_event->deltaY < 0)
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->get_button_index() = BUTTON_WHEEL_UP;
|
2017-04-21 03:01:31 +00:00
|
|
|
else if (wheel_event->deltaY > 0)
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->get_button_index() = BUTTON_WHEEL_DOWN;
|
2017-04-21 03:01:31 +00:00
|
|
|
else if (wheel_event->deltaX > 0)
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->get_button_index() = BUTTON_WHEEL_LEFT;
|
2017-04-21 03:01:31 +00:00
|
|
|
else if (wheel_event->deltaX < 0)
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->get_button_index() = BUTTON_WHEEL_RIGHT;
|
2017-04-21 03:01:31 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->is_pressed() = true;
|
2017-04-21 03:01:31 +00:00
|
|
|
_input->parse_input_event(ev);
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->is_pressed() = false;
|
2017-04-21 03:01:31 +00:00
|
|
|
_input->parse_input_event(ev);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-24 19:41:39 +00:00
|
|
|
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);
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEvent> ev;
|
|
|
|
ev.type = Ref<InputEvent>::SCREEN_TOUCH;
|
2017-04-24 19:41:39 +00:00
|
|
|
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.screen_touch.index = touch.identifier;
|
|
|
|
_prev_touches[i].x = ev.screen_touch.x = touch.canvasX;
|
|
|
|
_prev_touches[i].y = ev.screen_touch.y = touch.canvasY;
|
2017-05-20 15:38:03 +00:00
|
|
|
ev.screen_touch->is_pressed() = event_type == EMSCRIPTEN_EVENT_TOUCHSTART;
|
2017-04-24 19:41:39 +00:00
|
|
|
|
|
|
|
_input->parse_input_event(ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (touch_event->touches[lowest_id_index].isChanged) {
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
ev.type = Ref<InputEvent>::MOUSE_BUTTON;
|
2017-04-24 19:41:39 +00:00
|
|
|
ev.mouse_button.mod = dom2godot_mod(touch_event);
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->get_button_mask() = _input->get_mouse_button_mask() >> 1;
|
|
|
|
ev.mouse_button.global_x = ev->get_pos().x = touch_event->touches[lowest_id_index].canvasX;
|
|
|
|
ev.mouse_button.global_y = ev->get_pos().y = touch_event->touches[lowest_id_index].canvasY;
|
|
|
|
ev->get_button_index() = BUTTON_LEFT;
|
|
|
|
ev->is_pressed() = event_type == EMSCRIPTEN_EVENT_TOUCHSTART;
|
2017-04-24 19:41:39 +00:00
|
|
|
|
|
|
|
_input->parse_input_event(ev);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEvent> ev;
|
|
|
|
ev.type = Ref<InputEvent>::SCREEN_DRAG;
|
2017-04-24 19:41:39 +00:00
|
|
|
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.screen_drag.index = touch.identifier;
|
|
|
|
ev.screen_drag.x = touch.canvasX;
|
|
|
|
ev.screen_drag.y = touch.canvasY;
|
|
|
|
Point2 &prev = _prev_touches[i];
|
|
|
|
ev.screen_drag.relative_x = touch.canvasX - prev.x;
|
|
|
|
ev.screen_drag.relative_y = touch.canvasY - prev.y;
|
|
|
|
prev.x = ev.screen_drag.x;
|
|
|
|
prev.y = ev.screen_drag.y;
|
|
|
|
|
|
|
|
_input->parse_input_event(ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (touch_event->touches[lowest_id_index].isChanged) {
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
ev.type = Ref<InputEvent>::MOUSE_MOTION;
|
2017-04-24 19:41:39 +00:00
|
|
|
ev.mouse_motion.mod = dom2godot_mod(touch_event);
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->get_button_mask() = _input->get_mouse_button_mask() >> 1;
|
2017-04-24 19:41:39 +00:00
|
|
|
ev.mouse_motion.global_x = ev.mouse_motion.x = touch_event->touches[lowest_id_index].canvasX;
|
|
|
|
ev.mouse_motion.global_y = ev.mouse_motion.y = touch_event->touches[lowest_id_index].canvasY;
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->get_relative().x = _input->get_mouse_position().x - ev.mouse_motion.x;
|
|
|
|
ev->get_relative().y = _input->get_mouse_position().y - ev.mouse_motion.y;
|
2017-04-24 19:41:39 +00:00
|
|
|
|
|
|
|
_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;
|
|
|
|
|
|
|
|
_input->parse_input_event(ev);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
static Ref<InputEvent> _setup_key_event(const EmscriptenKeyboardEvent *emscripten_event) {
|
2016-04-15 22:19:02 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEvent> ev;
|
|
|
|
ev.type = Ref<InputEvent>::KEY;
|
|
|
|
ev->is_echo() = emscripten_event->repeat;
|
2017-04-21 03:01:31 +00:00
|
|
|
ev.key.mod = dom2godot_mod(emscripten_event);
|
2017-05-20 15:38:03 +00:00
|
|
|
ev->get_scancode() = dom2godot_scancode(emscripten_event->keyCode);
|
2016-04-15 22:19:02 +00:00
|
|
|
|
|
|
|
String unicode = String::utf8(emscripten_event->key);
|
2016-11-18 17:52:44 +00:00
|
|
|
// check if empty or multi-character (e.g. `CapsLock`)
|
2017-03-05 15:44:50 +00:00
|
|
|
if (unicode.length() != 1) {
|
2016-11-18 17:52:44 +00:00
|
|
|
// might be empty as well, but better than nonsense
|
2016-04-15 22:19:02 +00:00
|
|
|
unicode = String::utf8(emscripten_event->charValue);
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (unicode.length() == 1) {
|
|
|
|
ev.key.unicode = unicode[0];
|
2016-04-15 22:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ev;
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
static Ref<InputEvent> deferred_key_event;
|
2016-04-19 12:15:20 +00:00
|
|
|
|
2016-04-15 22:19:02 +00:00
|
|
|
static EM_BOOL _keydown_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYDOWN, false);
|
2016-04-15 22:19:02 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEvent> ev = _setup_key_event(key_event);
|
|
|
|
ev->is_pressed() = true;
|
|
|
|
if (ev.key.unicode == 0 && keycode_has_unicode(ev->get_scancode())) {
|
2016-04-19 12:15:20 +00:00
|
|
|
// defer to keypress event for legacy unicode retrieval
|
|
|
|
deferred_key_event = ev;
|
|
|
|
return false; // do not suppress keypress event
|
|
|
|
}
|
2017-04-21 03:01:31 +00:00
|
|
|
_input->parse_input_event(ev);
|
2016-04-19 12:15:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static EM_BOOL _keypress_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYPRESS, false);
|
2016-04-19 12:15:20 +00:00
|
|
|
|
|
|
|
deferred_key_event.key.unicode = key_event->charCode;
|
2017-04-21 03:01:31 +00:00
|
|
|
_input->parse_input_event(deferred_key_event);
|
2016-04-19 12:15:20 +00:00
|
|
|
return true;
|
2016-04-15 22:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static EM_BOOL _keyup_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYUP, false);
|
2016-04-15 22:19:02 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEvent> ev = _setup_key_event(key_event);
|
|
|
|
ev->is_pressed() = false;
|
2017-04-21 03:01:31 +00:00
|
|
|
_input->parse_input_event(ev);
|
2017-05-20 15:38:03 +00:00
|
|
|
return ev->get_scancode() != KEY_UNKNOWN && ev->get_scancode() != 0;
|
2016-04-15 22:19:02 +00:00
|
|
|
}
|
|
|
|
|
2016-01-21 01:23:15 +00:00
|
|
|
static EM_BOOL joy_callback_func(int p_type, const EmscriptenGamepadEvent *p_event, void *p_user) {
|
2017-03-05 15:44:50 +00:00
|
|
|
OS_JavaScript *os = (OS_JavaScript *)OS::get_singleton();
|
2016-01-21 01:23:15 +00:00
|
|
|
if (os) {
|
|
|
|
return os->joy_connection_changed(p_type, p_event);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
print_line("Init OS");
|
|
|
|
|
2017-04-27 13:34:46 +00:00
|
|
|
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);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
video_mode = p_desired;
|
2017-04-27 13:34:46 +00:00
|
|
|
// can't fulfil fullscreen request due to browser security
|
2017-03-05 15:44:50 +00:00
|
|
|
video_mode.fullscreen = false;
|
2017-04-27 13:34:46 +00:00
|
|
|
set_window_size(Size2(p_desired.width, p_desired.height));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-11-18 17:52:44 +00:00
|
|
|
// find locale, emscripten only sets "C"
|
|
|
|
char locale_ptr[16];
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2016-11-18 17:52:44 +00:00
|
|
|
EM_ASM_({
|
|
|
|
var locale = "";
|
|
|
|
if (Module.locale) {
|
|
|
|
// best case: server-side script reads Accept-Language early and
|
|
|
|
// defines the locale to be read here
|
|
|
|
locale = Module.locale;
|
|
|
|
} else {
|
|
|
|
// no luck, use what the JS engine can tell us
|
|
|
|
// if this turns out not compatible enough, add tests for
|
|
|
|
// browserLanguage, systemLanguage and userLanguage
|
|
|
|
locale = navigator.languages ? navigator.languages[0] : navigator.language;
|
|
|
|
}
|
|
|
|
locale = locale.split('.')[0];
|
|
|
|
stringToUTF8(locale, $0, 16);
|
|
|
|
}, locale_ptr);
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format on */
|
2016-11-18 17:52:44 +00:00
|
|
|
setenv("LANG", locale_ptr, true);
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
print_line("Init Audio");
|
|
|
|
|
2017-01-15 19:06:14 +00:00
|
|
|
AudioDriverManager::add_driver(&audio_driver_javascript);
|
2017-01-31 02:46:30 +00:00
|
|
|
audio_driver_javascript.set_singleton();
|
|
|
|
if (audio_driver_javascript.init() != OK) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-31 02:46:30 +00:00
|
|
|
ERR_PRINT("Initializing audio failed.");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 02:46:30 +00:00
|
|
|
RasterizerGLES3::register_config();
|
|
|
|
RasterizerGLES3::make_current();
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
print_line("Init VS");
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
visual_server = memnew(VisualServerRaster());
|
2014-02-10 01:10:30 +00:00
|
|
|
visual_server->cursor_set_visible(false, 0);
|
|
|
|
|
|
|
|
print_line("Init Physicsserver");
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
physics_server = memnew(PhysicsServerSW);
|
2014-02-10 01:10:30 +00:00
|
|
|
physics_server->init();
|
2017-03-05 15:44:50 +00:00
|
|
|
physics_2d_server = memnew(Physics2DServerSW);
|
2014-02-10 01:10:30 +00:00
|
|
|
physics_2d_server->init();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
input = memnew(InputDefault);
|
2017-04-21 03:01:31 +00:00
|
|
|
_input = input;
|
2017-03-05 15:44:50 +00:00
|
|
|
|
|
|
|
power_manager = memnew(PowerJavascript);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#define EM_CHECK(ev) \
|
|
|
|
if (result != EMSCRIPTEN_RESULT_SUCCESS) \
|
2016-11-26 12:13:16 +00:00
|
|
|
ERR_PRINTS("Error while setting " #ev " callback: Code " + itos(result))
|
2017-04-24 19:41:39 +00:00
|
|
|
#define SET_EM_CALLBACK(target, ev, cb) \
|
|
|
|
result = emscripten_set_##ev##_callback(target, this, true, &cb); \
|
2017-03-05 15:44:50 +00:00
|
|
|
EM_CHECK(ev)
|
|
|
|
#define SET_EM_CALLBACK_NODATA(ev, cb) \
|
|
|
|
result = emscripten_set_##ev##_callback(NULL, true, &cb); \
|
|
|
|
EM_CHECK(ev)
|
2016-11-26 12:13:16 +00:00
|
|
|
|
|
|
|
EMSCRIPTEN_RESULT result;
|
2017-04-24 19:41:39 +00:00
|
|
|
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)
|
2017-04-29 02:42:32 +00:00
|
|
|
SET_EM_CALLBACK(NULL, keydown, _keydown_callback)
|
|
|
|
SET_EM_CALLBACK(NULL, keypress, _keypress_callback)
|
|
|
|
SET_EM_CALLBACK(NULL, keyup, _keyup_callback)
|
2017-04-24 19:41:39 +00:00
|
|
|
SET_EM_CALLBACK(NULL, resize, _browser_resize_callback)
|
|
|
|
SET_EM_CALLBACK(NULL, fullscreenchange, _fullscreen_change_callback)
|
2016-11-26 12:13:16 +00:00
|
|
|
SET_EM_CALLBACK_NODATA(gamepadconnected, joy_callback_func)
|
|
|
|
SET_EM_CALLBACK_NODATA(gamepaddisconnected, joy_callback_func)
|
|
|
|
|
|
|
|
#undef SET_EM_CALLBACK_NODATA
|
|
|
|
#undef SET_EM_CALLBACK
|
|
|
|
#undef EM_CHECK
|
2016-04-11 21:22:14 +00:00
|
|
|
|
|
|
|
#ifdef JAVASCRIPT_EVAL_ENABLED
|
|
|
|
javascript_eval = memnew(JavaScript);
|
2017-01-05 12:16:00 +00:00
|
|
|
GlobalConfig::get_singleton()->add_singleton(GlobalConfig::Singleton("JavaScript", javascript_eval));
|
2016-04-11 21:22:14 +00:00
|
|
|
#endif
|
2017-02-22 22:14:21 +00:00
|
|
|
|
|
|
|
visual_server->init();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void OS_JavaScript::set_main_loop(MainLoop *p_main_loop) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
main_loop = p_main_loop;
|
2014-02-10 01:10:30 +00:00
|
|
|
input->set_main_loop(p_main_loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::delete_main_loop() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
memdelete(main_loop);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::finalize() {
|
|
|
|
|
|
|
|
memdelete(input);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void OS_JavaScript::alert(const String &p_alert, const String &p_title) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2016-11-18 17:52:44 +00:00
|
|
|
EM_ASM_({
|
|
|
|
window.alert(UTF8ToString($0));
|
|
|
|
}, p_alert.utf8().get_data());
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format on */
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 19:35:31 +00:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-29 02:54:48 +00:00
|
|
|
void OS_JavaScript::set_css_cursor(const char *p_cursor) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-29 02:54:48 +00:00
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_({
|
|
|
|
Module.canvas.style.cursor = Module.UTF8ToString($0);
|
|
|
|
}, p_cursor);
|
|
|
|
/* clang-format on */
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 02:54:48 +00:00
|
|
|
const char *OS_JavaScript::get_css_cursor() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-29 02:54:48 +00:00
|
|
|
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;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 02:54:48 +00:00
|
|
|
void OS_JavaScript::set_mouse_mode(OS::MouseMode p_mode) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-29 02:54:48 +00:00
|
|
|
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) {
|
|
|
|
|
2017-04-29 19:35:31 +00:00
|
|
|
set_css_cursor(godot2dom_cursor(cursor_shape));
|
2017-04-29 02:54:48 +00:00
|
|
|
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);
|
2017-04-29 19:35:31 +00:00
|
|
|
set_css_cursor(godot2dom_cursor(cursor_shape));
|
2017-04-29 02:54:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-11-23 22:53:38 +00:00
|
|
|
|
2017-03-29 15:29:38 +00:00
|
|
|
Point2 OS_JavaScript::get_mouse_position() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-29 15:29:38 +00:00
|
|
|
return input->get_mouse_position();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-11-23 22:53:38 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int OS_JavaScript::get_mouse_button_state() const {
|
|
|
|
|
2017-04-21 03:01:31 +00:00
|
|
|
return input->get_mouse_button_mask();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void OS_JavaScript::set_window_title(const String &p_title) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2016-11-18 17:52:44 +00:00
|
|
|
EM_ASM_({
|
|
|
|
document.title = UTF8ToString($0);
|
|
|
|
}, p_title.utf8().get_data());
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format on */
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//interesting byt not yet
|
|
|
|
//void set_clipboard(const String& p_text);
|
|
|
|
//String get_clipboard() const;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void OS_JavaScript::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-11-26 12:13:16 +00:00
|
|
|
video_mode = p_video_mode;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OS::VideoMode OS_JavaScript::get_video_mode(int p_screen) const {
|
|
|
|
|
2016-11-26 12:13:16 +00:00
|
|
|
return video_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 OS_JavaScript::get_screen_size(int p_screen) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(p_screen != 0, Size2());
|
2016-11-26 12:13:16 +00:00
|
|
|
|
|
|
|
EmscriptenFullscreenChangeEvent ev;
|
|
|
|
EMSCRIPTEN_RESULT result = emscripten_get_fullscreen_status(&ev);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(result != EMSCRIPTEN_RESULT_SUCCESS, Size2());
|
2016-11-26 12:13:16 +00:00
|
|
|
return Size2(ev.screenWidth, ev.screenHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::set_window_size(const Size2 p_size) {
|
|
|
|
|
|
|
|
window_maximized = false;
|
|
|
|
if (is_window_fullscreen()) {
|
|
|
|
set_window_fullscreen(false);
|
|
|
|
}
|
|
|
|
_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);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2015-04-02 17:54:36 +00:00
|
|
|
|
2015-04-02 18:03:14 +00:00
|
|
|
Size2 OS_JavaScript::get_window_size() const {
|
2015-04-02 17:54:36 +00:00
|
|
|
|
2016-11-26 12:13:16 +00:00
|
|
|
int canvas[3];
|
2017-03-05 15:44:50 +00:00
|
|
|
emscripten_get_canvas_size(canvas, canvas + 1, canvas + 2);
|
2016-11-26 12:13:16 +00:00
|
|
|
return Size2(canvas[0], canvas[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2016-11-26 12:13:16 +00:00
|
|
|
video_mode.width = EM_ASM_INT_V(return window.innerWidth);
|
|
|
|
video_mode.height = EM_ASM_INT_V(return window.innerHeight);
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format on */
|
2016-11-26 12:13:16 +00:00
|
|
|
emscripten_set_canvas_size(video_mode.width, video_mode.height);
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2016-11-26 12:13:16 +00:00
|
|
|
set_window_size(_windowed_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::set_window_fullscreen(bool p_enable) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_enable == is_window_fullscreen()) {
|
2016-11-26 12:13:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only requesting changes here, if successful, canvas is resized in
|
|
|
|
// _browser_resize_callback or _fullscreen_change_callback
|
|
|
|
EMSCRIPTEN_RESULT result;
|
|
|
|
if (p_enable) {
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2016-11-26 12:13:16 +00:00
|
|
|
EM_ASM(Module.requestFullscreen(false, false););
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format on */
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2016-11-26 12:13:16 +00:00
|
|
|
result = emscripten_exit_fullscreen();
|
2017-03-05 15:44:50 +00:00
|
|
|
if (result != EMSCRIPTEN_RESULT_SUCCESS) {
|
2016-11-26 12:13:16 +00:00
|
|
|
ERR_PRINTS("Failed to exit fullscreen: Code " + itos(result));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OS_JavaScript::is_window_fullscreen() const {
|
|
|
|
|
|
|
|
return video_mode.fullscreen;
|
2015-04-02 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void OS_JavaScript::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-11-26 12:13:16 +00:00
|
|
|
Size2 screen = get_screen_size();
|
|
|
|
p_list->push_back(OS::VideoMode(screen.width, screen.height, true));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String OS_JavaScript::get_name() {
|
|
|
|
|
|
|
|
return "HTML5";
|
|
|
|
}
|
|
|
|
|
|
|
|
MainLoop *OS_JavaScript::get_main_loop() const {
|
|
|
|
|
|
|
|
return main_loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OS_JavaScript::can_draw() const {
|
|
|
|
|
|
|
|
return true; //always?
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::set_cursor_shape(CursorShape p_shape) {
|
|
|
|
|
2017-04-29 19:35:31 +00:00
|
|
|
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));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::main_loop_begin() {
|
|
|
|
|
|
|
|
if (main_loop)
|
|
|
|
main_loop->init();
|
|
|
|
}
|
|
|
|
bool OS_JavaScript::main_loop_iterate() {
|
|
|
|
|
|
|
|
if (!main_loop)
|
|
|
|
return false;
|
2015-09-12 13:54:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (time_to_save_sync >= 0) {
|
2015-09-12 13:54:47 +00:00
|
|
|
int64_t newtime = get_ticks_msec();
|
|
|
|
int64_t elapsed = newtime - last_sync_time;
|
2017-03-05 15:44:50 +00:00
|
|
|
last_sync_time = newtime;
|
2015-09-12 13:54:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
time_to_save_sync -= elapsed;
|
2015-09-12 13:54:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (time_to_save_sync < 0) {
|
2015-09-12 13:54:47 +00:00
|
|
|
//time to sync, for real
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2015-09-12 13:54:47 +00:00
|
|
|
EM_ASM(
|
2017-01-12 13:14:40 +00:00
|
|
|
FS.syncfs(function(err) {
|
|
|
|
if (err) { Module.printErr('Failed to save IDB file system: ' + err.message); }
|
|
|
|
});
|
2015-09-12 13:54:47 +00:00
|
|
|
);
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format on */
|
2015-09-12 13:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-08 20:05:51 +00:00
|
|
|
process_joypads();
|
2014-02-10 01:10:30 +00:00
|
|
|
return Main::iteration();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::main_loop_end() {
|
|
|
|
|
|
|
|
if (main_loop)
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void OS_JavaScript::main_loop_focusin() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (main_loop)
|
|
|
|
main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
|
|
|
|
//audio_driver_javascript.set_pause(false);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void OS_JavaScript::process_accelerometer(const Vector3 &p_accelerometer) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
input->set_accelerometer(p_accelerometer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OS_JavaScript::has_touchscreen_ui_hint() const {
|
|
|
|
|
2014-02-13 21:03:28 +00:00
|
|
|
return false; //???
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OS_JavaScript::main_loop_request_quit() {
|
|
|
|
|
|
|
|
if (main_loop)
|
|
|
|
main_loop->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error OS_JavaScript::shell_open(String p_uri) {
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2016-11-18 17:52:44 +00:00
|
|
|
EM_ASM_({
|
|
|
|
window.open(UTF8ToString($0), '_blank');
|
|
|
|
}, p_uri.utf8().get_data());
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format on */
|
2016-11-18 17:52:44 +00:00
|
|
|
return OK;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
String OS_JavaScript::get_resource_dir() const {
|
|
|
|
|
|
|
|
return "/"; //javascript has it's own filesystem for resources inside the APK
|
|
|
|
}
|
|
|
|
|
|
|
|
String OS_JavaScript::get_data_dir() const {
|
|
|
|
|
2017-01-14 11:26:56 +00:00
|
|
|
/*
|
|
|
|
if (get_data_dir_func)
|
|
|
|
return get_data_dir_func();
|
|
|
|
*/
|
2015-09-12 13:54:47 +00:00
|
|
|
return "/userfs";
|
2017-01-05 12:16:00 +00:00
|
|
|
//return GlobalConfig::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir");
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2016-11-18 17:52:44 +00:00
|
|
|
String OS_JavaScript::get_executable_path() const {
|
|
|
|
|
2017-03-28 01:21:21 +00:00
|
|
|
return OS::get_executable_path();
|
2016-11-18 17:52:44 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void OS_JavaScript::_close_notification_funcs(const String &p_file, int p_flags) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
print_line("close " + p_file + " flags " + itos(p_flags));
|
|
|
|
if (p_file.begins_with("/userfs") && p_flags & FileAccess::WRITE) {
|
|
|
|
static_cast<OS_JavaScript *>(get_singleton())->last_sync_time = OS::get_singleton()->get_ticks_msec();
|
|
|
|
static_cast<OS_JavaScript *>(get_singleton())->time_to_save_sync = 5000; //five seconds since last save
|
2015-09-12 13:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-08 20:05:51 +00:00
|
|
|
void OS_JavaScript::process_joypads() {
|
2016-01-21 01:23:15 +00:00
|
|
|
|
|
|
|
int joy_count = emscripten_get_num_gamepads();
|
|
|
|
for (int i = 0; i < joy_count; i++) {
|
|
|
|
EmscriptenGamepadEvent state;
|
|
|
|
emscripten_get_gamepad_status(i, &state);
|
|
|
|
if (state.connected) {
|
|
|
|
|
2016-01-22 21:56:05 +00:00
|
|
|
int num_buttons = MIN(state.numButtons, 18);
|
2016-01-21 01:23:15 +00:00
|
|
|
int num_axes = MIN(state.numAxes, 8);
|
|
|
|
for (int j = 0; j < num_buttons; j++) {
|
|
|
|
|
|
|
|
float value = state.analogButton[j];
|
|
|
|
if (String(state.mapping) == "standard" && (j == 6 || j == 7)) {
|
|
|
|
InputDefault::JoyAxis jx;
|
|
|
|
jx.min = 0;
|
|
|
|
jx.value = value;
|
2017-03-26 13:59:13 +00:00
|
|
|
input->joy_axis(i, j, jx);
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2017-03-26 13:59:13 +00:00
|
|
|
input->joy_button(i, j, value);
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int j = 0; j < num_axes; j++) {
|
|
|
|
|
|
|
|
InputDefault::JoyAxis jx;
|
|
|
|
jx.min = -1;
|
|
|
|
jx.value = state.axis[j];
|
2017-03-26 13:59:13 +00:00
|
|
|
input->joy_axis(i, j, jx);
|
2016-01-21 01:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OS_JavaScript::joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event) {
|
|
|
|
if (p_type == EMSCRIPTEN_EVENT_GAMEPADCONNECTED) {
|
|
|
|
|
|
|
|
String guid = "";
|
|
|
|
if (String(p_event->mapping) == "standard")
|
|
|
|
guid = "Default HTML5 Gamepad";
|
|
|
|
input->joy_connection_changed(p_event->index, true, String(p_event->id), guid);
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2016-01-21 01:23:15 +00:00
|
|
|
input->joy_connection_changed(p_event->index, false, "");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OS_JavaScript::is_joy_known(int p_device) {
|
|
|
|
return input->is_joy_mapped(p_device);
|
|
|
|
}
|
|
|
|
|
|
|
|
String OS_JavaScript::get_joy_guid(int p_device) const {
|
|
|
|
return input->get_joy_guid_remapped(p_device);
|
|
|
|
}
|
|
|
|
|
2016-07-23 11:15:55 +00:00
|
|
|
PowerState OS_JavaScript::get_power_state() {
|
|
|
|
return power_manager->get_power_state();
|
|
|
|
}
|
|
|
|
|
|
|
|
int OS_JavaScript::get_power_seconds_left() {
|
|
|
|
return power_manager->get_power_seconds_left();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-07-23 11:15:55 +00:00
|
|
|
int OS_JavaScript::get_power_percent_left() {
|
|
|
|
return power_manager->get_power_percent_left();
|
|
|
|
}
|
|
|
|
|
2017-04-27 13:34:46 +00:00
|
|
|
OS_JavaScript::OS_JavaScript(const char *p_execpath, GetDataDirFunc p_get_data_dir_func) {
|
2017-03-28 01:21:21 +00:00
|
|
|
set_cmdline(p_execpath, get_cmdline_args());
|
2017-03-05 15:44:50 +00:00
|
|
|
main_loop = NULL;
|
|
|
|
gl_extensions = NULL;
|
|
|
|
window_maximized = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
get_data_dir_func = p_get_data_dir_func;
|
|
|
|
FileAccessUnix::close_notification_func = _close_notification_funcs;
|
2015-09-12 13:54:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
time_to_save_sync = -1;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OS_JavaScript::~OS_JavaScript() {
|
|
|
|
}
|