summaryrefslogtreecommitdiff
path: root/platform/javascript/os_javascript.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'platform/javascript/os_javascript.cpp')
-rw-r--r--platform/javascript/os_javascript.cpp230
1 files changed, 130 insertions, 100 deletions
diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp
index 1cd1991608..9df26f1471 100644
--- a/platform/javascript/os_javascript.cpp
+++ b/platform/javascript/os_javascript.cpp
@@ -46,14 +46,12 @@
#define DOM_BUTTON_RIGHT 2
template <typename T>
-static InputModifierState dom2godot_mod(T emscripten_event_ptr) {
+static void dom2godot_mod(T emscripten_event_ptr, Ref<InputEventWithModifiers> godot_event) {
- 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;
+ 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 {
@@ -151,26 +149,26 @@ static EM_BOOL _mousebutton_callback(int event_type, const EmscriptenMouseEvent
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_MOUSEDOWN && event_type != EMSCRIPTEN_EVENT_MOUSEUP, false);
- InputEvent ev;
- ev.type = InputEvent::MOUSE_BUTTON;
- ev.mouse_button.pressed = event_type == EMSCRIPTEN_EVENT_MOUSEDOWN;
- ev.mouse_button.global_x = ev.mouse_button.x = mouse_event->canvasX;
- ev.mouse_button.global_y = ev.mouse_button.y = mouse_event->canvasY;
- ev.mouse_button.mod = dom2godot_mod(mouse_event);
+ 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.mouse_button.button_index = BUTTON_LEFT; break;
- case DOM_BUTTON_MIDDLE: ev.mouse_button.button_index = BUTTON_MIDDLE; break;
- case DOM_BUTTON_RIGHT: ev.mouse_button.button_index = BUTTON_RIGHT; break;
+ 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;
}
- ev.mouse_button.button_mask = _input->get_mouse_button_mask();
- if (ev.mouse_button.pressed)
- ev.mouse_button.button_mask |= 1 << ev.mouse_button.button_index;
+ int mask = _input->get_mouse_button_mask();
+ if (ev->is_pressed())
+ mask |= 1 << ev->get_button_index();
else
- ev.mouse_button.button_mask &= ~(1 << ev.mouse_button.button_index);
- ev.mouse_button.button_mask >>= 1;
+ mask &= ~(1 << ev->get_button_index());
+ ev->set_button_mask(mask >> 1);
_input->parse_input_event(ev);
return true;
@@ -180,20 +178,17 @@ static EM_BOOL _mousemove_callback(int event_type, const EmscriptenMouseEvent *m
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_MOUSEMOVE, false);
- InputEvent ev;
- ev.type = InputEvent::MOUSE_MOTION;
- ev.mouse_motion.mod = dom2godot_mod(mouse_event);
- ev.mouse_motion.button_mask = _input->get_mouse_button_mask() >> 1;
+ Ref<InputEventMouseMotion> ev;
+ ev.instance();
+ dom2godot_mod(mouse_event, ev);
+ ev->set_button_mask(_input->get_mouse_button_mask() >> 1);
- ev.mouse_motion.global_x = ev.mouse_motion.x = mouse_event->canvasX;
- ev.mouse_motion.global_y = ev.mouse_motion.y = mouse_event->canvasY;
+ ev->set_position(Point2(mouse_event->canvasX, mouse_event->canvasY));
+ ev->set_global_position(ev->get_position());
- ev.mouse_motion.relative_x = _input->get_mouse_position().x - ev.mouse_motion.x;
- ev.mouse_motion.relative_y = _input->get_mouse_position().y - ev.mouse_motion.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->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;
@@ -203,31 +198,35 @@ static EM_BOOL _wheel_callback(int event_type, const EmscriptenWheelEvent *wheel
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_WHEEL, false);
- InputEvent ev;
- ev.type = InputEvent::MOUSE_BUTTON;
- ev.mouse_button.button_mask = _input->get_mouse_button_mask() >> 1;
- ev.mouse_button.global_x = ev.mouse_button.x = _input->get_mouse_position().x;
- ev.mouse_button.global_y = ev.mouse_button.y = _input->get_mouse_position().y;
- ev.mouse_button.mod.shift = _input->is_key_pressed(KEY_SHIFT);
- ev.mouse_button.mod.alt = _input->is_key_pressed(KEY_ALT);
- ev.mouse_button.mod.control = _input->is_key_pressed(KEY_CONTROL);
- ev.mouse_button.mod.meta = _input->is_key_pressed(KEY_META);
+ 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.mouse_button.button_index = BUTTON_WHEEL_UP;
+ ev->set_button_index(BUTTON_WHEEL_UP);
else if (wheel_event->deltaY > 0)
- ev.mouse_button.button_index = BUTTON_WHEEL_DOWN;
+ ev->set_button_index(BUTTON_WHEEL_DOWN);
else if (wheel_event->deltaX > 0)
- ev.mouse_button.button_index = BUTTON_WHEEL_LEFT;
+ ev->set_button_index(BUTTON_WHEEL_LEFT);
else if (wheel_event->deltaX < 0)
- ev.mouse_button.button_index = BUTTON_WHEEL_RIGHT;
+ ev->set_button_index(BUTTON_WHEEL_RIGHT);
else
return false;
- ev.mouse_button.pressed = true;
+ // 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.mouse_button.pressed = false;
+ ev->set_pressed(false);
_input->parse_input_event(ev);
return true;
@@ -243,8 +242,8 @@ static EM_BOOL _touchpress_callback(int event_type, const EmscriptenTouchEvent *
event_type != EMSCRIPTEN_EVENT_TOUCHCANCEL,
false);
- InputEvent ev;
- ev.type = InputEvent::SCREEN_TOUCH;
+ Ref<InputEventScreenTouch> ev;
+ ev.instance();
int lowest_id_index = -1;
for (int i = 0; i < touch_event->numTouches; ++i) {
@@ -253,25 +252,29 @@ static EM_BOOL _touchpress_callback(int event_type, const EmscriptenTouchEvent *
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;
- ev.screen_touch.pressed = event_type == EMSCRIPTEN_EVENT_TOUCHSTART;
+ 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) {
- ev.type = InputEvent::MOUSE_BUTTON;
- ev.mouse_button.mod = dom2godot_mod(touch_event);
- ev.mouse_button.button_mask = _input->get_mouse_button_mask() >> 1;
- ev.mouse_button.global_x = ev.mouse_button.x = touch_event->touches[lowest_id_index].canvasX;
- ev.mouse_button.global_y = ev.mouse_button.y = touch_event->touches[lowest_id_index].canvasY;
- ev.mouse_button.button_index = BUTTON_LEFT;
- ev.mouse_button.pressed = event_type == EMSCRIPTEN_EVENT_TOUCHSTART;
+ Ref<InputEventMouseButton> ev_mouse;
+ ev_mouse.instance();
+ ev_mouse->set_button_mask(_input->get_mouse_button_mask() >> 1);
+ dom2godot_mod(touch_event, ev_mouse);
- _input->parse_input_event(ev);
+ 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;
}
@@ -280,8 +283,8 @@ static EM_BOOL _touchmove_callback(int event_type, const EmscriptenTouchEvent *t
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_TOUCHMOVE, false);
- InputEvent ev;
- ev.type = InputEvent::SCREEN_DRAG;
+ Ref<InputEventScreenDrag> ev;
+ ev.instance();
int lowest_id_index = -1;
for (int i = 0; i < touch_event->numTouches; ++i) {
@@ -290,44 +293,42 @@ static EM_BOOL _touchmove_callback(int event_type, const EmscriptenTouchEvent *t
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;
+ ev->set_index(touch.identifier);
+ ev->set_position(Point2(touch.canvasX, 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;
+ ev->set_relative(ev->get_position() - prev);
+ prev = ev->get_position();
_input->parse_input_event(ev);
}
if (touch_event->touches[lowest_id_index].isChanged) {
- ev.type = InputEvent::MOUSE_MOTION;
- ev.mouse_motion.mod = dom2godot_mod(touch_event);
- ev.mouse_motion.button_mask = _input->get_mouse_button_mask() >> 1;
- 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;
- ev.mouse_motion.relative_x = _input->get_mouse_position().x - ev.mouse_motion.x;
- ev.mouse_motion.relative_y = _input->get_mouse_position().y - ev.mouse_motion.y;
+ Ref<InputEventMouseMotion> ev_mouse;
+ ev_mouse.instance();
+ dom2godot_mod(touch_event, ev_mouse);
+ ev_mouse->set_button_mask(_input->get_mouse_button_mask() >> 1);
- _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;
+ 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());
- _input->parse_input_event(ev);
+ 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 InputEvent _setup_key_event(const EmscriptenKeyboardEvent *emscripten_event) {
+static Ref<InputEventKey> _setup_key_event(const EmscriptenKeyboardEvent *emscripten_event) {
- InputEvent ev;
- ev.type = InputEvent::KEY;
- ev.key.echo = emscripten_event->repeat;
- ev.key.mod = dom2godot_mod(emscripten_event);
- ev.key.scancode = dom2godot_scancode(emscripten_event->keyCode);
+ Ref<InputEventKey> ev;
+ ev.instance();
+ ev->set_echo(emscripten_event->repeat);
+ dom2godot_mod(emscripten_event, ev);
+ ev->set_scancode(dom2godot_scancode(emscripten_event->keyCode));
String unicode = String::utf8(emscripten_event->key);
// check if empty or multi-character (e.g. `CapsLock`)
@@ -336,21 +337,21 @@ 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
@@ -363,7 +364,7 @@ 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;
+ deferred_key_event->set_unicode(key_event->charCode);
_input->parse_input_event(deferred_key_event);
return true;
}
@@ -372,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;
+ Ref<InputEventKey> ev = _setup_key_event(key_event);
+ ev->set_pressed(false);
_input->parse_input_event(ev);
- return ev.key.scancode != KEY_UNKNOWN && ev.key.scancode != 0;
+ 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) {
@@ -517,6 +518,31 @@ void OS_JavaScript::alert(const String &p_alert, const String &p_title) {
/* clang-format on */
}
+static const char *godot2dom_cursor(OS::CursorShape p_shape) {
+
+ switch (p_shape) {
+ case OS::CURSOR_ARROW:
+ default:
+ return "auto";
+ case OS::CURSOR_IBEAM: return "text";
+ case OS::CURSOR_POINTING_HAND: return "pointer";
+ case OS::CURSOR_CROSS: return "crosshair";
+ case OS::CURSOR_WAIT: return "progress";
+ case OS::CURSOR_BUSY: return "wait";
+ case OS::CURSOR_DRAG: return "grab";
+ case OS::CURSOR_CAN_DROP: return "grabbing";
+ case OS::CURSOR_FORBIDDEN: return "no-drop";
+ case OS::CURSOR_VSIZE: return "ns-resize";
+ case OS::CURSOR_HSIZE: return "ew-resize";
+ case OS::CURSOR_BDIAGSIZE: return "nesw-resize";
+ case OS::CURSOR_FDIAGSIZE: return "nwse-resize";
+ case OS::CURSOR_MOVE: return "move";
+ case OS::CURSOR_VSPLIT: return "row-resize";
+ case OS::CURSOR_HSPLIT: return "col-resize";
+ case OS::CURSOR_HELP: return "help";
+ }
+}
+
void OS_JavaScript::set_css_cursor(const char *p_cursor) {
/* clang-format off */
@@ -547,7 +573,7 @@ void OS_JavaScript::set_mouse_mode(OS::MouseMode p_mode) {
if (p_mode == MOUSE_MODE_VISIBLE) {
- set_css_cursor("auto");
+ set_css_cursor(godot2dom_cursor(cursor_shape));
emscripten_exit_pointerlock();
} else if (p_mode == MOUSE_MODE_HIDDEN) {
@@ -561,7 +587,7 @@ void OS_JavaScript::set_mouse_mode(OS::MouseMode p_mode) {
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("auto");
+ set_css_cursor(godot2dom_cursor(cursor_shape));
}
}
@@ -706,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() {