Update HTML5 platform for new InputEvents

This commit is contained in:
L. Krause 2017-06-19 15:43:46 +02:00
parent 33bf180067
commit 6f17d41bcf

View file

@ -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);
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;
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->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;
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->get_button_mask() = _input->get_mouse_button_mask();
int mask = _input->get_mouse_button_mask();
if (ev->is_pressed())
ev->get_button_mask() |= 1 << ev->get_button_index();
mask |= 1 << ev->get_button_index();
else
ev->get_button_mask() &= ~(1 << ev->get_button_index());
ev->get_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);
Ref<InputEvent> ev;
ev.type = Ref<InputEvent>::MOUSE_MOTION;
ev.mouse_motion.mod = dom2godot_mod(mouse_event);
ev->get_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->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;
_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);
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);
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->get_button_index() = BUTTON_WHEEL_UP;
ev->set_button_index(BUTTON_WHEEL_UP);
else if (wheel_event->deltaY > 0)
ev->get_button_index() = BUTTON_WHEEL_DOWN;
ev->set_button_index(BUTTON_WHEEL_DOWN);
else if (wheel_event->deltaX > 0)
ev->get_button_index() = BUTTON_WHEEL_LEFT;
ev->set_button_index(BUTTON_WHEEL_LEFT);
else if (wheel_event->deltaX < 0)
ev->get_button_index() = BUTTON_WHEEL_RIGHT;
ev->set_button_index(BUTTON_WHEEL_RIGHT);
else
return false;
ev->is_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->is_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);
Ref<InputEvent> ev;
ev.type = Ref<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->is_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 = Ref<InputEvent>::MOUSE_BUTTON;
ev.mouse_button.mod = dom2godot_mod(touch_event);
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;
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);
Ref<InputEvent> ev;
ev.type = Ref<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 = Ref<InputEvent>::MOUSE_MOTION;
ev.mouse_motion.mod = dom2godot_mod(touch_event);
ev->get_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->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;
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 Ref<InputEvent> _setup_key_event(const EmscriptenKeyboardEvent *emscripten_event) {
static Ref<InputEventKey> _setup_key_event(const EmscriptenKeyboardEvent *emscripten_event) {
Ref<InputEvent> ev;
ev.type = Ref<InputEvent>::KEY;
ev->is_echo() = emscripten_event->repeat;
ev.key.mod = dom2godot_mod(emscripten_event);
ev->get_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 Ref<InputEvent> _setup_key_event(const EmscriptenKeyboardEvent *emscripte
unicode = String::utf8(emscripten_event->charValue);
}
if (unicode.length() == 1) {
ev.key.unicode = unicode[0];
ev->set_unicode(unicode[0]);
}
return ev;
}
static Ref<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);
Ref<InputEvent> ev = _setup_key_event(key_event);
ev->is_pressed() = true;
if (ev.key.unicode == 0 && keycode_has_unicode(ev->get_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,8 +373,8 @@ static EM_BOOL _keyup_callback(int event_type, const EmscriptenKeyboardEvent *ke
ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYUP, false);
Ref<InputEvent> ev = _setup_key_event(key_event);
ev->is_pressed() = false;
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;
}