|
@@ -92,38 +92,17 @@ void OS_JavaScript::send_notification_callback(int p_notification) {
|
|
|
|
|
|
// Window (canvas)
|
|
// Window (canvas)
|
|
|
|
|
|
-Point2 OS_JavaScript::compute_position_in_canvas(int p_x, int p_y) {
|
|
|
|
- int point[2];
|
|
|
|
- godot_js_display_compute_position(p_x, p_y, point, point + 1);
|
|
|
|
- return Point2(point[0], point[1]);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
bool OS_JavaScript::check_size_force_redraw() {
|
|
bool OS_JavaScript::check_size_force_redraw() {
|
|
return godot_js_display_size_update() != 0;
|
|
return godot_js_display_size_update() != 0;
|
|
}
|
|
}
|
|
|
|
|
|
-EM_BOOL OS_JavaScript::fullscreen_change_callback(int p_event_type, const EmscriptenFullscreenChangeEvent *p_event, void *p_user_data) {
|
|
|
|
|
|
+void OS_JavaScript::fullscreen_change_callback(int p_fullscreen) {
|
|
OS_JavaScript *os = get_singleton();
|
|
OS_JavaScript *os = get_singleton();
|
|
- // Empty ID is canvas.
|
|
|
|
- String target_id = String::utf8(p_event->id);
|
|
|
|
- if (target_id.empty() || target_id == String::utf8(&(os->canvas_id[1]))) {
|
|
|
|
- // This event property is the only reliable data on
|
|
|
|
- // browser fullscreen state.
|
|
|
|
- os->video_mode.fullscreen = p_event->isFullscreen;
|
|
|
|
- if (os->video_mode.fullscreen) {
|
|
|
|
- os->entering_fullscreen = false;
|
|
|
|
- } else {
|
|
|
|
- // Restoring maximized window now will cause issues,
|
|
|
|
- // so delay until main_loop_iterate.
|
|
|
|
- os->just_exited_fullscreen = true;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return false;
|
|
|
|
|
|
+ os->video_mode.fullscreen = p_fullscreen;
|
|
}
|
|
}
|
|
|
|
|
|
-EM_BOOL OS_JavaScript::blur_callback(int p_event_type, const EmscriptenFocusEvent *p_event, void *p_user_data) {
|
|
|
|
|
|
+void OS_JavaScript::window_blur_callback() {
|
|
get_singleton()->input->release_pressed_events();
|
|
get_singleton()->input->release_pressed_events();
|
|
- return false;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
void OS_JavaScript::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
|
|
void OS_JavaScript::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
|
|
@@ -142,15 +121,9 @@ Size2 OS_JavaScript::get_screen_size(int p_screen) const {
|
|
|
|
|
|
void OS_JavaScript::set_window_size(const Size2 p_size) {
|
|
void OS_JavaScript::set_window_size(const Size2 p_size) {
|
|
if (video_mode.fullscreen) {
|
|
if (video_mode.fullscreen) {
|
|
- window_maximized = false;
|
|
|
|
set_window_fullscreen(false);
|
|
set_window_fullscreen(false);
|
|
- } else {
|
|
|
|
- if (window_maximized) {
|
|
|
|
- emscripten_exit_soft_fullscreen();
|
|
|
|
- window_maximized = false;
|
|
|
|
- }
|
|
|
|
- godot_js_display_desired_size_set(p_size.x, p_size.y);
|
|
|
|
}
|
|
}
|
|
|
|
+ godot_js_display_desired_size_set(p_size.x, p_size.y);
|
|
}
|
|
}
|
|
|
|
|
|
Size2 OS_JavaScript::get_window_size() const {
|
|
Size2 OS_JavaScript::get_window_size() const {
|
|
@@ -160,29 +133,11 @@ Size2 OS_JavaScript::get_window_size() const {
|
|
}
|
|
}
|
|
|
|
|
|
void OS_JavaScript::set_window_maximized(bool p_enabled) {
|
|
void OS_JavaScript::set_window_maximized(bool p_enabled) {
|
|
-#ifndef TOOLS_ENABLED
|
|
|
|
- if (video_mode.fullscreen) {
|
|
|
|
- window_maximized = p_enabled;
|
|
|
|
- set_window_fullscreen(false);
|
|
|
|
- } else if (!p_enabled) {
|
|
|
|
- emscripten_exit_soft_fullscreen();
|
|
|
|
- window_maximized = false;
|
|
|
|
- } else if (!window_maximized) {
|
|
|
|
- // Prevent calling emscripten_enter_soft_fullscreen mutltiple times,
|
|
|
|
- // this would hide page elements permanently.
|
|
|
|
- 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(canvas_id, &strategy);
|
|
|
|
- window_maximized = p_enabled;
|
|
|
|
- }
|
|
|
|
-#endif
|
|
|
|
|
|
+ WARN_PRINT_ONCE("Maximizing windows is not supported for the HTML5 platform.");
|
|
}
|
|
}
|
|
|
|
|
|
bool OS_JavaScript::is_window_maximized() const {
|
|
bool OS_JavaScript::is_window_maximized() const {
|
|
- return window_maximized;
|
|
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
|
|
|
|
void OS_JavaScript::set_window_fullscreen(bool p_enabled) {
|
|
void OS_JavaScript::set_window_fullscreen(bool p_enabled) {
|
|
@@ -193,14 +148,8 @@ void OS_JavaScript::set_window_fullscreen(bool p_enabled) {
|
|
// Just request changes here, if successful, logic continues in
|
|
// Just request changes here, if successful, logic continues in
|
|
// fullscreen_change_callback.
|
|
// fullscreen_change_callback.
|
|
if (p_enabled) {
|
|
if (p_enabled) {
|
|
- if (window_maximized) {
|
|
|
|
- // Soft fullsreen during real fullscreen can cause issues, so exit.
|
|
|
|
- // This must be called before requesting full screen.
|
|
|
|
- emscripten_exit_soft_fullscreen();
|
|
|
|
- }
|
|
|
|
int result = godot_js_display_fullscreen_request();
|
|
int result = godot_js_display_fullscreen_request();
|
|
ERR_FAIL_COND_MSG(result, "The request was denied. Remember that enabling fullscreen is only possible from an input callback for the HTML5 platform.");
|
|
ERR_FAIL_COND_MSG(result, "The request was denied. Remember that enabling fullscreen is only possible from an input callback for the HTML5 platform.");
|
|
- entering_fullscreen = true;
|
|
|
|
} else {
|
|
} else {
|
|
// No logic allowed here, since exiting w/ ESC key won't use this function.
|
|
// No logic allowed here, since exiting w/ ESC key won't use this function.
|
|
ERR_FAIL_COND(godot_js_display_fullscreen_exit());
|
|
ERR_FAIL_COND(godot_js_display_fullscreen_exit());
|
|
@@ -232,76 +181,35 @@ void OS_JavaScript::set_window_per_pixel_transparency_enabled(bool p_enabled) {
|
|
|
|
|
|
// Keys
|
|
// Keys
|
|
|
|
|
|
-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);
|
|
|
|
|
|
+static void dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod) {
|
|
|
|
+ ev->set_shift(p_mod & 1);
|
|
|
|
+ ev->set_alt(p_mod & 2);
|
|
|
|
+ ev->set_control(p_mod & 4);
|
|
|
|
+ ev->set_metakey(p_mod & 8);
|
|
}
|
|
}
|
|
|
|
|
|
-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(dom_code2godot_scancode(emscripten_event->code, emscripten_event->key, false));
|
|
|
|
- ev->set_physical_scancode(dom_code2godot_scancode(emscripten_event->code, emscripten_event->key, true));
|
|
|
|
-
|
|
|
|
- String unicode = String::utf8(emscripten_event->key);
|
|
|
|
- // Check if empty or multi-character (e.g. `CapsLock`).
|
|
|
|
- if (unicode.length() != 1) {
|
|
|
|
- // Might be empty as well, but better than nonsense.
|
|
|
|
- unicode = String::utf8(emscripten_event->charValue);
|
|
|
|
- }
|
|
|
|
- if (unicode.length() == 1) {
|
|
|
|
- ev->set_unicode(unicode[0]);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return ev;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-EM_BOOL OS_JavaScript::keydown_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data) {
|
|
|
|
|
|
+void OS_JavaScript::key_callback(int p_pressed, int p_repeat, int p_modifiers) {
|
|
OS_JavaScript *os = get_singleton();
|
|
OS_JavaScript *os = get_singleton();
|
|
- Ref<InputEventKey> ev = setup_key_event(p_event);
|
|
|
|
- ev->set_pressed(true);
|
|
|
|
- if (ev->get_unicode() == 0 && keycode_has_unicode(ev->get_scancode())) {
|
|
|
|
- // Defer to keypress event for legacy unicode retrieval.
|
|
|
|
- os->deferred_key_event = ev;
|
|
|
|
- // Do not suppress keypress event.
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- os->input->parse_input_event(ev);
|
|
|
|
-
|
|
|
|
- // Make sure to flush all events so we can call restricted APIs inside the event.
|
|
|
|
- os->input->flush_buffered_events();
|
|
|
|
-
|
|
|
|
|
|
+ JSKeyEvent &key_event = os->key_event;
|
|
// Resume audio context after input in case autoplay was denied.
|
|
// Resume audio context after input in case autoplay was denied.
|
|
os->resume_audio();
|
|
os->resume_audio();
|
|
- return true;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-EM_BOOL OS_JavaScript::keypress_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data) {
|
|
|
|
- OS_JavaScript *os = get_singleton();
|
|
|
|
- os->deferred_key_event->set_unicode(p_event->charCode);
|
|
|
|
- os->input->parse_input_event(os->deferred_key_event);
|
|
|
|
|
|
|
|
- // Make sure to flush all events so we can call restricted APIs inside the event.
|
|
|
|
- os->input->flush_buffered_events();
|
|
|
|
-
|
|
|
|
- return true;
|
|
|
|
-}
|
|
|
|
|
|
+ Ref<InputEventKey> ev;
|
|
|
|
+ ev.instance();
|
|
|
|
+ ev->set_echo(p_repeat);
|
|
|
|
+ ev->set_scancode(dom_code2godot_scancode(key_event.code, key_event.key, false));
|
|
|
|
+ ev->set_physical_scancode(dom_code2godot_scancode(key_event.code, key_event.key, true));
|
|
|
|
+ ev->set_pressed(p_pressed);
|
|
|
|
+ dom2godot_mod(ev, p_modifiers);
|
|
|
|
|
|
-EM_BOOL OS_JavaScript::keyup_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data) {
|
|
|
|
- OS_JavaScript *os = get_singleton();
|
|
|
|
- Ref<InputEventKey> ev = setup_key_event(p_event);
|
|
|
|
- ev->set_pressed(false);
|
|
|
|
|
|
+ String unicode = String::utf8(key_event.key);
|
|
|
|
+ if (unicode.length() == 1) {
|
|
|
|
+ ev->set_unicode(unicode[0]);
|
|
|
|
+ }
|
|
os->input->parse_input_event(ev);
|
|
os->input->parse_input_event(ev);
|
|
|
|
|
|
// Make sure to flush all events so we can call restricted APIs inside the event.
|
|
// Make sure to flush all events so we can call restricted APIs inside the event.
|
|
os->input->flush_buffered_events();
|
|
os->input->flush_buffered_events();
|
|
-
|
|
|
|
- return ev->get_scancode() != KEY_UNKNOWN && ev->get_scancode() != 0;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// Mouse
|
|
// Mouse
|
|
@@ -314,17 +222,18 @@ int OS_JavaScript::get_mouse_button_state() const {
|
|
return input->get_mouse_button_mask();
|
|
return input->get_mouse_button_mask();
|
|
}
|
|
}
|
|
|
|
|
|
-EM_BOOL OS_JavaScript::mouse_button_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data) {
|
|
|
|
|
|
+int OS_JavaScript::mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers) {
|
|
OS_JavaScript *os = get_singleton();
|
|
OS_JavaScript *os = get_singleton();
|
|
|
|
|
|
Ref<InputEventMouseButton> ev;
|
|
Ref<InputEventMouseButton> ev;
|
|
ev.instance();
|
|
ev.instance();
|
|
- ev->set_pressed(p_event_type == EMSCRIPTEN_EVENT_MOUSEDOWN);
|
|
|
|
- ev->set_position(compute_position_in_canvas(p_event->clientX, p_event->clientY));
|
|
|
|
|
|
+ ev->set_pressed(p_pressed);
|
|
|
|
+ ev->set_position(Point2(p_x, p_y));
|
|
ev->set_global_position(ev->get_position());
|
|
ev->set_global_position(ev->get_position());
|
|
- dom2godot_mod(p_event, ev);
|
|
|
|
|
|
+ ev->set_pressed(p_pressed);
|
|
|
|
+ dom2godot_mod(ev, p_modifiers);
|
|
|
|
|
|
- switch (p_event->button) {
|
|
|
|
|
|
+ switch (p_button) {
|
|
case DOM_BUTTON_LEFT:
|
|
case DOM_BUTTON_LEFT:
|
|
ev->set_button_index(BUTTON_LEFT);
|
|
ev->set_button_index(BUTTON_LEFT);
|
|
break;
|
|
break;
|
|
@@ -344,8 +253,8 @@ EM_BOOL OS_JavaScript::mouse_button_callback(int p_event_type, const EmscriptenM
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
- if (ev->is_pressed()) {
|
|
|
|
- double diff = emscripten_get_now() - os->last_click_ms;
|
|
|
|
|
|
+ if (p_pressed) {
|
|
|
|
+ uint64_t diff = (OS::get_singleton()->get_ticks_usec() / 1000) - os->last_click_ms;
|
|
|
|
|
|
if (ev->get_button_index() == os->last_click_button_index) {
|
|
if (ev->get_button_index() == os->last_click_button_index) {
|
|
if (diff < 400 && Point2(os->last_click_pos).distance_to(ev->get_position()) < 5) {
|
|
if (diff < 400 && Point2(os->last_click_pos).distance_to(ev->get_position()) < 5) {
|
|
@@ -368,9 +277,6 @@ EM_BOOL OS_JavaScript::mouse_button_callback(int p_event_type, const EmscriptenM
|
|
int mask = os->input->get_mouse_button_mask();
|
|
int mask = os->input->get_mouse_button_mask();
|
|
int button_flag = 1 << (ev->get_button_index() - 1);
|
|
int button_flag = 1 << (ev->get_button_index() - 1);
|
|
if (ev->is_pressed()) {
|
|
if (ev->is_pressed()) {
|
|
- // Since the event is consumed, focus manually. The containing iframe,
|
|
|
|
- // if exists, may not have focus yet, so focus even if already focused.
|
|
|
|
- godot_js_display_canvas_focus();
|
|
|
|
mask |= button_flag;
|
|
mask |= button_flag;
|
|
} else if (mask & button_flag) {
|
|
} else if (mask & button_flag) {
|
|
mask &= ~button_flag;
|
|
mask &= ~button_flag;
|
|
@@ -381,43 +287,39 @@ EM_BOOL OS_JavaScript::mouse_button_callback(int p_event_type, const EmscriptenM
|
|
ev->set_button_mask(mask);
|
|
ev->set_button_mask(mask);
|
|
|
|
|
|
os->input->parse_input_event(ev);
|
|
os->input->parse_input_event(ev);
|
|
|
|
+ // Resume audio context after input in case autoplay was denied.
|
|
|
|
+ os->resume_audio();
|
|
|
|
|
|
// Make sure to flush all events so we can call restricted APIs inside the event.
|
|
// Make sure to flush all events so we can call restricted APIs inside the event.
|
|
os->input->flush_buffered_events();
|
|
os->input->flush_buffered_events();
|
|
|
|
|
|
- // Resume audio context after input in case autoplay was denied.
|
|
|
|
- os->resume_audio();
|
|
|
|
-
|
|
|
|
// Prevent multi-click text selection and wheel-click scrolling anchor.
|
|
// Prevent multi-click text selection and wheel-click scrolling anchor.
|
|
// Context menu is prevented through contextmenu event.
|
|
// Context menu is prevented through contextmenu event.
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
-EM_BOOL OS_JavaScript::mousemove_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data) {
|
|
|
|
|
|
+void OS_JavaScript::mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers) {
|
|
OS_JavaScript *os = get_singleton();
|
|
OS_JavaScript *os = get_singleton();
|
|
|
|
|
|
int input_mask = os->input->get_mouse_button_mask();
|
|
int input_mask = os->input->get_mouse_button_mask();
|
|
- Point2 pos = compute_position_in_canvas(p_event->clientX, p_event->clientY);
|
|
|
|
// For motion outside the canvas, only read mouse movement if dragging
|
|
// For motion outside the canvas, only read mouse movement if dragging
|
|
// started inside the canvas; imitating desktop app behaviour.
|
|
// started inside the canvas; imitating desktop app behaviour.
|
|
if (!os->cursor_inside_canvas && !input_mask)
|
|
if (!os->cursor_inside_canvas && !input_mask)
|
|
- return false;
|
|
|
|
|
|
+ return;
|
|
|
|
|
|
Ref<InputEventMouseMotion> ev;
|
|
Ref<InputEventMouseMotion> ev;
|
|
ev.instance();
|
|
ev.instance();
|
|
- dom2godot_mod(p_event, ev);
|
|
|
|
|
|
+ dom2godot_mod(ev, p_modifiers);
|
|
ev->set_button_mask(input_mask);
|
|
ev->set_button_mask(input_mask);
|
|
|
|
|
|
- ev->set_position(pos);
|
|
|
|
|
|
+ ev->set_position(Point2(p_x, p_y));
|
|
ev->set_global_position(ev->get_position());
|
|
ev->set_global_position(ev->get_position());
|
|
|
|
|
|
- ev->set_relative(Vector2(p_event->movementX, p_event->movementY));
|
|
|
|
|
|
+ ev->set_relative(Vector2(p_rel_x, p_rel_y));
|
|
os->input->set_mouse_position(ev->get_position());
|
|
os->input->set_mouse_position(ev->get_position());
|
|
ev->set_speed(os->input->get_last_mouse_speed());
|
|
ev->set_speed(os->input->get_last_mouse_speed());
|
|
|
|
|
|
os->input->parse_input_event(ev);
|
|
os->input->parse_input_event(ev);
|
|
- // Don't suppress mouseover/-leave events.
|
|
|
|
- return false;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
static const char *godot2dom_cursor(OS::CursorShape p_shape) {
|
|
static const char *godot2dom_cursor(OS::CursorShape p_shape) {
|
|
@@ -554,34 +456,33 @@ void OS_JavaScript::set_mouse_mode(OS::MouseMode p_mode) {
|
|
|
|
|
|
if (p_mode == MOUSE_MODE_VISIBLE) {
|
|
if (p_mode == MOUSE_MODE_VISIBLE) {
|
|
godot_js_display_cursor_set_visible(1);
|
|
godot_js_display_cursor_set_visible(1);
|
|
- emscripten_exit_pointerlock();
|
|
|
|
|
|
+ godot_js_display_cursor_lock_set(false);
|
|
|
|
|
|
} else if (p_mode == MOUSE_MODE_HIDDEN) {
|
|
} else if (p_mode == MOUSE_MODE_HIDDEN) {
|
|
godot_js_display_cursor_set_visible(0);
|
|
godot_js_display_cursor_set_visible(0);
|
|
- emscripten_exit_pointerlock();
|
|
|
|
|
|
+ godot_js_display_cursor_lock_set(false);
|
|
|
|
|
|
} else if (p_mode == MOUSE_MODE_CAPTURED) {
|
|
} else if (p_mode == MOUSE_MODE_CAPTURED) {
|
|
godot_js_display_cursor_set_visible(1);
|
|
godot_js_display_cursor_set_visible(1);
|
|
- EMSCRIPTEN_RESULT result = emscripten_request_pointerlock(canvas_id, false);
|
|
|
|
- ERR_FAIL_COND_MSG(result == EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED, "MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback.");
|
|
|
|
- ERR_FAIL_COND_MSG(result != EMSCRIPTEN_RESULT_SUCCESS, "MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback.");
|
|
|
|
|
|
+ godot_js_display_cursor_lock_set(true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
OS::MouseMode OS_JavaScript::get_mouse_mode() const {
|
|
OS::MouseMode OS_JavaScript::get_mouse_mode() const {
|
|
- if (godot_js_display_cursor_is_hidden())
|
|
|
|
|
|
+ if (godot_js_display_cursor_is_hidden()) {
|
|
return MOUSE_MODE_HIDDEN;
|
|
return MOUSE_MODE_HIDDEN;
|
|
-
|
|
|
|
- EmscriptenPointerlockChangeEvent ev;
|
|
|
|
- emscripten_get_pointerlock_status(&ev);
|
|
|
|
- return (ev.isActive && String::utf8(ev.id) == String::utf8(&canvas_id[1])) ? MOUSE_MODE_CAPTURED : MOUSE_MODE_VISIBLE;
|
|
|
|
|
|
+ }
|
|
|
|
+ if (godot_js_display_cursor_is_locked()) {
|
|
|
|
+ return MOUSE_MODE_CAPTURED;
|
|
|
|
+ }
|
|
|
|
+ return MOUSE_MODE_VISIBLE;
|
|
}
|
|
}
|
|
|
|
|
|
// Wheel
|
|
// Wheel
|
|
|
|
|
|
-EM_BOOL OS_JavaScript::wheel_callback(int p_event_type, const EmscriptenWheelEvent *p_event, void *p_user_data) {
|
|
|
|
- ERR_FAIL_COND_V(p_event_type != EMSCRIPTEN_EVENT_WHEEL, false);
|
|
|
|
|
|
+int OS_JavaScript::mouse_wheel_callback(double p_delta_x, double p_delta_y) {
|
|
OS_JavaScript *os = get_singleton();
|
|
OS_JavaScript *os = get_singleton();
|
|
|
|
+
|
|
if (!godot_js_display_canvas_is_focused()) {
|
|
if (!godot_js_display_canvas_is_focused()) {
|
|
if (os->cursor_inside_canvas) {
|
|
if (os->cursor_inside_canvas) {
|
|
godot_js_display_canvas_focus();
|
|
godot_js_display_canvas_focus();
|
|
@@ -601,16 +502,17 @@ EM_BOOL OS_JavaScript::wheel_callback(int p_event_type, const EmscriptenWheelEve
|
|
ev->set_control(input->is_key_pressed(KEY_CONTROL));
|
|
ev->set_control(input->is_key_pressed(KEY_CONTROL));
|
|
ev->set_metakey(input->is_key_pressed(KEY_META));
|
|
ev->set_metakey(input->is_key_pressed(KEY_META));
|
|
|
|
|
|
- if (p_event->deltaY < 0)
|
|
|
|
|
|
+ if (p_delta_y < 0) {
|
|
ev->set_button_index(BUTTON_WHEEL_UP);
|
|
ev->set_button_index(BUTTON_WHEEL_UP);
|
|
- else if (p_event->deltaY > 0)
|
|
|
|
|
|
+ } else if (p_delta_y > 0) {
|
|
ev->set_button_index(BUTTON_WHEEL_DOWN);
|
|
ev->set_button_index(BUTTON_WHEEL_DOWN);
|
|
- else if (p_event->deltaX > 0)
|
|
|
|
|
|
+ } else if (p_delta_x > 0) {
|
|
ev->set_button_index(BUTTON_WHEEL_LEFT);
|
|
ev->set_button_index(BUTTON_WHEEL_LEFT);
|
|
- else if (p_event->deltaX < 0)
|
|
|
|
|
|
+ } else if (p_delta_x < 0) {
|
|
ev->set_button_index(BUTTON_WHEEL_RIGHT);
|
|
ev->set_button_index(BUTTON_WHEEL_RIGHT);
|
|
- else
|
|
|
|
|
|
+ } else {
|
|
return false;
|
|
return false;
|
|
|
|
+ }
|
|
|
|
|
|
// Different browsers give wildly different delta values, and we can't
|
|
// Different browsers give wildly different delta values, and we can't
|
|
// interpret deltaMode, so use default value for wheel events' factor.
|
|
// interpret deltaMode, so use default value for wheel events' factor.
|
|
@@ -635,53 +537,41 @@ bool OS_JavaScript::has_touchscreen_ui_hint() const {
|
|
return godot_js_display_touchscreen_is_available();
|
|
return godot_js_display_touchscreen_is_available();
|
|
}
|
|
}
|
|
|
|
|
|
-EM_BOOL OS_JavaScript::touch_press_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data) {
|
|
|
|
|
|
+void OS_JavaScript::touch_callback(int p_type, int p_count) {
|
|
OS_JavaScript *os = get_singleton();
|
|
OS_JavaScript *os = get_singleton();
|
|
- Ref<InputEventScreenTouch> ev;
|
|
|
|
- int lowest_id_index = -1;
|
|
|
|
- for (int i = 0; i < p_event->numTouches; ++i) {
|
|
|
|
- const EmscriptenTouchPoint &touch = p_event->touches[i];
|
|
|
|
- if (lowest_id_index == -1 || touch.identifier < p_event->touches[lowest_id_index].identifier)
|
|
|
|
- lowest_id_index = i;
|
|
|
|
- if (!touch.isChanged)
|
|
|
|
- continue;
|
|
|
|
- ev.instance();
|
|
|
|
- ev->set_index(touch.identifier);
|
|
|
|
- ev->set_position(compute_position_in_canvas(touch.clientX, touch.clientY));
|
|
|
|
- os->touches[i] = ev->get_position();
|
|
|
|
- ev->set_pressed(p_event_type == EMSCRIPTEN_EVENT_TOUCHSTART);
|
|
|
|
-
|
|
|
|
- os->input->parse_input_event(ev);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // Make sure to flush all events so we can call restricted APIs inside the event.
|
|
|
|
- os->input->flush_buffered_events();
|
|
|
|
-
|
|
|
|
// Resume audio context after input in case autoplay was denied.
|
|
// Resume audio context after input in case autoplay was denied.
|
|
os->resume_audio();
|
|
os->resume_audio();
|
|
- return true;
|
|
|
|
-}
|
|
|
|
|
|
|
|
-EM_BOOL OS_JavaScript::touchmove_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data) {
|
|
|
|
- OS_JavaScript *os = get_singleton();
|
|
|
|
- Ref<InputEventScreenDrag> ev;
|
|
|
|
- int lowest_id_index = -1;
|
|
|
|
- for (int i = 0; i < p_event->numTouches; ++i) {
|
|
|
|
- const EmscriptenTouchPoint &touch = p_event->touches[i];
|
|
|
|
- if (lowest_id_index == -1 || touch.identifier < p_event->touches[lowest_id_index].identifier)
|
|
|
|
- lowest_id_index = i;
|
|
|
|
- if (!touch.isChanged)
|
|
|
|
- continue;
|
|
|
|
- ev.instance();
|
|
|
|
- ev->set_index(touch.identifier);
|
|
|
|
- ev->set_position(compute_position_in_canvas(touch.clientX, touch.clientY));
|
|
|
|
- Point2 &prev = os->touches[i];
|
|
|
|
- ev->set_relative(ev->get_position() - prev);
|
|
|
|
- prev = ev->get_position();
|
|
|
|
-
|
|
|
|
- os->input->parse_input_event(ev);
|
|
|
|
|
|
+ const JSTouchEvent &touch_event = os->touch_event;
|
|
|
|
+ for (int i = 0; i < p_count; i++) {
|
|
|
|
+ Point2 point(touch_event.coords[i * 2], touch_event.coords[i * 2 + 1]);
|
|
|
|
+ if (p_type == 2) {
|
|
|
|
+ // touchmove
|
|
|
|
+ Ref<InputEventScreenDrag> ev;
|
|
|
|
+ ev.instance();
|
|
|
|
+ ev->set_index(touch_event.identifier[i]);
|
|
|
|
+ ev->set_position(point);
|
|
|
|
+
|
|
|
|
+ Point2 &prev = os->touches[i];
|
|
|
|
+ ev->set_relative(ev->get_position() - prev);
|
|
|
|
+ prev = ev->get_position();
|
|
|
|
+
|
|
|
|
+ os->input->parse_input_event(ev);
|
|
|
|
+ } else {
|
|
|
|
+ // touchstart/touchend
|
|
|
|
+ Ref<InputEventScreenTouch> ev;
|
|
|
|
+ ev.instance();
|
|
|
|
+ ev->set_index(touch_event.identifier[i]);
|
|
|
|
+ ev->set_position(point);
|
|
|
|
+ ev->set_pressed(p_type == 0);
|
|
|
|
+ os->touches[i] = point;
|
|
|
|
+
|
|
|
|
+ os->input->parse_input_event(ev);
|
|
|
|
+
|
|
|
|
+ // Make sure to flush all events so we can call restricted APIs inside the event.
|
|
|
|
+ os->input->flush_buffered_events();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- return true;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// Gamepad
|
|
// Gamepad
|
|
@@ -695,14 +585,14 @@ void OS_JavaScript::gamepad_callback(int p_index, int p_connected, const char *p
|
|
}
|
|
}
|
|
|
|
|
|
void OS_JavaScript::process_joypads() {
|
|
void OS_JavaScript::process_joypads() {
|
|
- int32_t pads = godot_js_display_gamepad_sample_count();
|
|
|
|
|
|
+ int32_t pads = godot_js_input_gamepad_sample_count();
|
|
int32_t s_btns_num = 0;
|
|
int32_t s_btns_num = 0;
|
|
int32_t s_axes_num = 0;
|
|
int32_t s_axes_num = 0;
|
|
int32_t s_standard = 0;
|
|
int32_t s_standard = 0;
|
|
float s_btns[16];
|
|
float s_btns[16];
|
|
float s_axes[10];
|
|
float s_axes[10];
|
|
for (int idx = 0; idx < pads; idx++) {
|
|
for (int idx = 0; idx < pads; idx++) {
|
|
- int err = godot_js_display_gamepad_sample_get(idx, s_btns, &s_btns_num, s_axes, &s_axes_num, &s_standard);
|
|
|
|
|
|
+ int err = godot_js_input_gamepad_sample_get(idx, s_btns, &s_btns_num, s_axes, &s_axes_num, &s_standard);
|
|
if (err) {
|
|
if (err) {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
@@ -874,44 +764,24 @@ Error OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver,
|
|
#endif
|
|
#endif
|
|
input = memnew(InputDefault);
|
|
input = memnew(InputDefault);
|
|
|
|
|
|
- EMSCRIPTEN_RESULT result;
|
|
|
|
-#define EM_CHECK(ev) \
|
|
|
|
- if (result != EMSCRIPTEN_RESULT_SUCCESS) \
|
|
|
|
- ERR_PRINT("Error while setting " #ev " callback: Code " + itos(result));
|
|
|
|
-#define SET_EM_CALLBACK(target, ev, cb) \
|
|
|
|
- result = emscripten_set_##ev##_callback(target, NULL, true, &cb); \
|
|
|
|
- EM_CHECK(ev)
|
|
|
|
-#define SET_EM_WINDOW_CALLBACK(ev, cb) \
|
|
|
|
- result = emscripten_set_##ev##_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, false, &cb); \
|
|
|
|
- EM_CHECK(ev)
|
|
|
|
- // These callbacks from Emscripten's html5.h suffice to access most
|
|
|
|
- // JavaScript APIs.
|
|
|
|
- SET_EM_CALLBACK(canvas_id, mousedown, mouse_button_callback)
|
|
|
|
- SET_EM_WINDOW_CALLBACK(mousemove, mousemove_callback)
|
|
|
|
- SET_EM_WINDOW_CALLBACK(mouseup, mouse_button_callback)
|
|
|
|
- SET_EM_WINDOW_CALLBACK(blur, blur_callback)
|
|
|
|
- SET_EM_CALLBACK(canvas_id, wheel, wheel_callback)
|
|
|
|
- SET_EM_CALLBACK(canvas_id, touchstart, touch_press_callback)
|
|
|
|
- SET_EM_CALLBACK(canvas_id, touchmove, touchmove_callback)
|
|
|
|
- SET_EM_CALLBACK(canvas_id, touchend, touch_press_callback)
|
|
|
|
- SET_EM_CALLBACK(canvas_id, touchcancel, touch_press_callback)
|
|
|
|
- SET_EM_CALLBACK(canvas_id, keydown, keydown_callback)
|
|
|
|
- SET_EM_CALLBACK(canvas_id, keypress, keypress_callback)
|
|
|
|
- SET_EM_CALLBACK(canvas_id, keyup, keyup_callback)
|
|
|
|
- SET_EM_CALLBACK(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, fullscreenchange, fullscreen_change_callback)
|
|
|
|
-#undef SET_EM_CALLBACK
|
|
|
|
-#undef EM_CHECK
|
|
|
|
-
|
|
|
|
- // For APIs that are not (sufficiently) exposed, a
|
|
|
|
- // library is used below (implemented in library_godot_display.js).
|
|
|
|
|
|
+ // JS Input interface (js/libs/library_godot_input.js)
|
|
|
|
+ godot_js_input_mouse_button_cb(&OS_JavaScript::mouse_button_callback);
|
|
|
|
+ godot_js_input_mouse_move_cb(&OS_JavaScript::mouse_move_callback);
|
|
|
|
+ godot_js_input_mouse_wheel_cb(&OS_JavaScript::mouse_wheel_callback);
|
|
|
|
+ godot_js_input_touch_cb(&OS_JavaScript::touch_callback, touch_event.identifier, touch_event.coords);
|
|
|
|
+ godot_js_input_key_cb(&OS_JavaScript::key_callback, key_event.code, key_event.key);
|
|
|
|
+ godot_js_input_gamepad_cb(&OS_JavaScript::gamepad_callback);
|
|
|
|
+ godot_js_input_paste_cb(&OS_JavaScript::update_clipboard_callback);
|
|
|
|
+ godot_js_input_drop_files_cb(&OS_JavaScript::drop_files_callback);
|
|
|
|
+
|
|
|
|
+ // JS Display interface (js/libs/library_godot_display.js)
|
|
|
|
+ godot_js_display_fullscreen_cb(&OS_JavaScript::fullscreen_change_callback);
|
|
|
|
+ godot_js_display_window_blur_cb(&window_blur_callback);
|
|
godot_js_display_notification_cb(&OS_JavaScript::send_notification_callback,
|
|
godot_js_display_notification_cb(&OS_JavaScript::send_notification_callback,
|
|
MainLoop::NOTIFICATION_WM_MOUSE_ENTER,
|
|
MainLoop::NOTIFICATION_WM_MOUSE_ENTER,
|
|
MainLoop::NOTIFICATION_WM_MOUSE_EXIT,
|
|
MainLoop::NOTIFICATION_WM_MOUSE_EXIT,
|
|
MainLoop::NOTIFICATION_WM_FOCUS_IN,
|
|
MainLoop::NOTIFICATION_WM_FOCUS_IN,
|
|
MainLoop::NOTIFICATION_WM_FOCUS_OUT);
|
|
MainLoop::NOTIFICATION_WM_FOCUS_OUT);
|
|
- godot_js_display_paste_cb(&OS_JavaScript::update_clipboard_callback);
|
|
|
|
- godot_js_display_drop_files_cb(&OS_JavaScript::drop_files_callback);
|
|
|
|
- godot_js_display_gamepad_cb(&OS_JavaScript::gamepad_callback);
|
|
|
|
godot_js_display_vk_cb(&input_text_callback);
|
|
godot_js_display_vk_cb(&input_text_callback);
|
|
|
|
|
|
visual_server->init();
|
|
visual_server->init();
|
|
@@ -986,22 +856,10 @@ bool OS_JavaScript::main_loop_iterate() {
|
|
|
|
|
|
input->flush_buffered_events();
|
|
input->flush_buffered_events();
|
|
|
|
|
|
- if (godot_js_display_gamepad_sample() == OK)
|
|
|
|
|
|
+ if (godot_js_input_gamepad_sample() == OK) {
|
|
process_joypads();
|
|
process_joypads();
|
|
-
|
|
|
|
- if (just_exited_fullscreen) {
|
|
|
|
- if (window_maximized) {
|
|
|
|
- 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(canvas_id, &strategy);
|
|
|
|
- } else {
|
|
|
|
- godot_js_display_size_update();
|
|
|
|
- }
|
|
|
|
- just_exited_fullscreen = false;
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
return Main::iteration();
|
|
return Main::iteration();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1218,9 +1076,6 @@ OS_JavaScript::OS_JavaScript() {
|
|
last_click_ms = 0;
|
|
last_click_ms = 0;
|
|
last_click_pos = Point2(-100, -100);
|
|
last_click_pos = Point2(-100, -100);
|
|
|
|
|
|
- window_maximized = false;
|
|
|
|
- entering_fullscreen = false;
|
|
|
|
- just_exited_fullscreen = false;
|
|
|
|
transparency_enabled = false;
|
|
transparency_enabled = false;
|
|
|
|
|
|
main_loop = NULL;
|
|
main_loop = NULL;
|