diff options
author | Juan Linietsky <reduzio@gmail.com> | 2017-05-20 12:38:03 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2017-05-20 17:05:38 -0300 |
commit | 5b3709d3096df737b8bb2344446be818b0389bfe (patch) | |
tree | 649a0989b1494f3c4687d59e503310f4e6bbeb40 /main | |
parent | 93f9a83062dbe74474a4a7928758c5cf5588238e (diff) |
Removal of InputEvent as built-in Variant type..
this might cause bugs I haven't found yet..
Diffstat (limited to 'main')
-rw-r--r-- | main/input_default.cpp | 178 | ||||
-rw-r--r-- | main/input_default.h | 4 | ||||
-rw-r--r-- | main/tests/test_image.cpp | 2 | ||||
-rw-r--r-- | main/tests/test_physics.cpp | 28 | ||||
-rw-r--r-- | main/tests/test_physics_2d.cpp | 25 | ||||
-rw-r--r-- | main/tests/test_render.cpp | 4 |
6 files changed, 111 insertions, 130 deletions
diff --git a/main/input_default.cpp b/main/input_default.cpp index 8f0b8464eb..2cce62ab51 100644 --- a/main/input_default.cpp +++ b/main/input_default.cpp @@ -115,19 +115,19 @@ bool InputDefault::is_action_pressed(const StringName &p_action) const { case InputEvent::KEY: { const InputEventKey &iek=E->get().key; - if ((keys_pressed.has(iek.scancode))) + if ((keys_pressed.has(iek->get_scancode()))) return true; } break; case InputEvent::MOUSE_BUTTON: { const InputEventMouseButton &iemb=E->get().mouse_button; - if(mouse_button_mask&(1<<iemb.button_index)) + if(mouse_button_mask&(1<<iemb->get_button_index())) return true; } break; case InputEvent::JOYPAD_BUTTON: { const InputEventJoypadButton &iejb=E->get().joy_button; - int c = _combine_device(iejb.button_index,device); + int c = _combine_device(iejb->get_button_index(),device); if (joy_buttons_pressed.has(c)) return true; } break; @@ -297,94 +297,86 @@ Vector3 InputDefault::get_gyroscope() const { return gyroscope; } -void InputDefault::parse_input_event(const InputEvent &p_event) { +void InputDefault::parse_input_event(const Ref<InputEvent> &p_event) { _THREAD_SAFE_METHOD_ - switch (p_event.type) { - - case InputEvent::KEY: { - - if (p_event.key.echo) - break; - if (p_event.key.scancode == 0) - break; - - //print_line(p_event); - - if (p_event.key.pressed) - keys_pressed.insert(p_event.key.scancode); - else - keys_pressed.erase(p_event.key.scancode); - } break; - case InputEvent::MOUSE_BUTTON: { - - if (p_event.mouse_button.doubleclick) - break; - - if (p_event.mouse_button.pressed) - mouse_button_mask |= (1 << p_event.mouse_button.button_index); - else - mouse_button_mask &= ~(1 << p_event.mouse_button.button_index); - - if (main_loop && emulate_touch && p_event.mouse_button.button_index == 1) { - InputEventScreenTouch touch_event; - touch_event.index = 0; - touch_event.pressed = p_event.mouse_button.pressed; - touch_event.x = p_event.mouse_button.x; - touch_event.y = p_event.mouse_button.y; - InputEvent ev; - ev.type = InputEvent::SCREEN_TOUCH; - ev.screen_touch = touch_event; - main_loop->input_event(ev); - } - Point2 pos = Point2(p_event.mouse_button.global_x, p_event.mouse_button.global_y); - if (mouse_pos != pos) { - set_mouse_position(pos); - } - } break; - case InputEvent::MOUSE_MOTION: { - - if (main_loop && emulate_touch && p_event.mouse_motion.button_mask & 1) { - InputEventScreenDrag drag_event; - drag_event.index = 0; - drag_event.x = p_event.mouse_motion.x; - drag_event.y = p_event.mouse_motion.y; - drag_event.relative_x = p_event.mouse_motion.relative_x; - drag_event.relative_y = p_event.mouse_motion.relative_y; - drag_event.speed_x = p_event.mouse_motion.speed_x; - drag_event.speed_y = p_event.mouse_motion.speed_y; - - InputEvent ev; - ev.type = InputEvent::SCREEN_DRAG; - ev.screen_drag = drag_event; - - main_loop->input_event(ev); - } + Ref<InputEventKey> k = p_event; + if (k.is_valid() && k->is_echo() && k->get_scancode() != 0) { + + //print_line(p_event); + + if (k->is_pressed()) + keys_pressed.insert(k->get_scancode()); + else + keys_pressed.erase(k->get_scancode()); + } + + Ref<InputEventMouseButton> mb = p_event; + + if (mb.is_valid() && !mb->is_doubleclick()) { + + if (mb->is_pressed()) + mouse_button_mask |= (1 << mb->get_button_index()); + else + mouse_button_mask &= ~(1 << mb->get_button_index()); + + if (main_loop && emulate_touch && mb->get_button_index() == 1) { + Ref<InputEventScreenTouch> touch_event; + touch_event.instance(); + touch_event->set_pressed(mb->is_pressed()); + touch_event->set_pos(mb->get_pos()); + main_loop->input_event(touch_event); + } + + Point2 pos = mb->get_global_pos(); + if (mouse_pos != pos) { + set_mouse_position(pos); + } + } + + Ref<InputEventMouseMotion> mm = p_event; + + if (mm.is_valid()) { + + if (main_loop && emulate_touch && mm->get_button_mask() & 1) { + Ref<InputEventScreenDrag> drag_event; + drag_event.instance(); - } break; - case InputEvent::JOYPAD_BUTTON: { + drag_event->set_pos(mm->get_pos()); + drag_event->set_relative(mm->get_relative()); + drag_event->set_speed(mm->get_speed()); + + main_loop->input_event(drag_event); + } + } + + Ref<InputEventJoypadButton> jb = p_event; + + if (jb.is_valid()) { + + int c = _combine_device(jb->get_button_index(), jb->get_device()); + + if (jb->is_pressed()) + joy_buttons_pressed.insert(c); + else + joy_buttons_pressed.erase(c); + } - int c = _combine_device(p_event.joy_button.button_index, p_event.device); + Ref<InputEventJoypadMotion> jm = p_event; - if (p_event.joy_button.pressed) - joy_buttons_pressed.insert(c); - else - joy_buttons_pressed.erase(c); - } break; - case InputEvent::JOYPAD_MOTION: { - set_joy_axis(p_event.device, p_event.joy_motion.axis, p_event.joy_motion.axis_value); - } break; + if (jm.is_valid()) { + set_joy_axis(jm->get_device(), jm->get_axis(), jm->get_axis_value()); } - if (!p_event.is_echo()) { + if (p_event->is_echo()) { for (const Map<StringName, InputMap::Action>::Element *E = InputMap::get_singleton()->get_action_map().front(); E; E = E->next()) { - if (InputMap::get_singleton()->event_is_action(p_event, E->key()) && is_action_pressed(E->key()) != p_event.is_pressed()) { + if (InputMap::get_singleton()->event_is_action(p_event, E->key()) && is_action_pressed(E->key()) != p_event->is_pressed()) { Action action; action.fixed_frame = Engine::get_singleton()->get_fixed_frames(); action.idle_frame = Engine::get_singleton()->get_idle_frames(); - action.pressed = p_event.is_pressed(); + action.pressed = p_event->is_pressed(); action_state[E->key()] = action; } } @@ -484,7 +476,7 @@ void InputDefault::warp_mouse_pos(const Vector2 &p_to) { OS::get_singleton()->warp_mouse_pos(p_to); } -Point2i InputDefault::warp_mouse_motion(const InputEventMouseMotion &p_motion, const Rect2 &p_rect) { +Point2i InputDefault::warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect) { // The relative distance reported for the next event after a warp is in the boundaries of the // size of the rect on that axis, but it may be greater, in which case there's not problem as fmod() @@ -495,13 +487,13 @@ Point2i InputDefault::warp_mouse_motion(const InputEventMouseMotion &p_motion, c // detect the warp: if the relative distance is greater than the half of the size of the relevant rect // (checked per each axis), it will be considered as the consequence of a former pointer warp. - const Point2i rel_sgn(p_motion.relative_x >= 0.0f ? 1 : -1, p_motion.relative_y >= 0.0 ? 1 : -1); + const Point2i rel_sgn(p_motion->get_relative().x >= 0.0f ? 1 : -1, p_motion->get_relative().y >= 0.0 ? 1 : -1); const Size2i warp_margin = p_rect.size * 0.5f; const Point2i rel_warped( - Math::fmod(p_motion.relative_x + rel_sgn.x * warp_margin.x, p_rect.size.x) - rel_sgn.x * warp_margin.x, - Math::fmod(p_motion.relative_y + rel_sgn.y * warp_margin.y, p_rect.size.y) - rel_sgn.y * warp_margin.y); + Math::fmod(p_motion->get_relative().x + rel_sgn.x * warp_margin.x, p_rect.size.x) - rel_sgn.x * warp_margin.x, + Math::fmod(p_motion->get_relative().y + rel_sgn.y * warp_margin.y, p_rect.size.y) - rel_sgn.y * warp_margin.y); - const Point2i pos_local = Point2i(p_motion.global_x, p_motion.global_y) - p_rect.pos; + const Point2i pos_local = p_motion->get_global_pos() - p_rect.pos; const Point2i pos_warped(Math::fposmod(pos_local.x, p_rect.size.x), Math::fposmod(pos_local.y, p_rect.size.y)); if (pos_warped != pos_local) { OS::get_singleton()->warp_mouse_pos(pos_warped + p_rect.pos); @@ -1020,22 +1012,22 @@ void InputDefault::joy_hat(int p_device, int p_val) { void InputDefault::_button_event(int p_device, int p_index, bool p_pressed) { - InputEvent ievent; - ievent.type = InputEvent::JOYPAD_BUTTON; - ievent.device = p_device; - ievent.joy_button.button_index = p_index; - ievent.joy_button.pressed = p_pressed; + Ref<InputEventJoypadButton> ievent; + ievent.instance(); + ievent->set_device(p_device); + ievent->set_button_index(p_index); + ievent->set_pressed(p_pressed); parse_input_event(ievent); }; void InputDefault::_axis_event(int p_device, int p_axis, float p_value) { - InputEvent ievent; - ievent.type = InputEvent::JOYPAD_MOTION; - ievent.device = p_device; - ievent.joy_motion.axis = p_axis; - ievent.joy_motion.axis_value = p_value; + Ref<InputEventJoypadMotion> ievent; + ievent.instance(); + ievent->set_device(p_device); + ievent->set_axis(p_axis); + ievent->set_axis_value(p_value); parse_input_event(ievent); }; diff --git a/main/input_default.h b/main/input_default.h index 55b7555ef9..35e841a488 100644 --- a/main/input_default.h +++ b/main/input_default.h @@ -201,9 +201,9 @@ public: virtual int get_mouse_button_mask() const; virtual void warp_mouse_pos(const Vector2 &p_to); - virtual Point2i warp_mouse_motion(const InputEventMouseMotion &p_motion, const Rect2 &p_rect); + virtual Point2i warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect); - virtual void parse_input_event(const InputEvent &p_event); + virtual void parse_input_event(const Ref<InputEvent> &p_event); void set_gravity(const Vector3 &p_gravity); void set_accelerometer(const Vector3 &p_accel); diff --git a/main/tests/test_image.cpp b/main/tests/test_image.cpp index c2c742e75d..0b120f87d5 100644 --- a/main/tests/test_image.cpp +++ b/main/tests/test_image.cpp @@ -41,7 +41,7 @@ class TestMainLoop : public MainLoop { bool quit; public: - virtual void input_event(const InputEvent &p_event) { + virtual void input_event(const Ref<InputEvent> &p_event) { } virtual void init() { diff --git a/main/tests/test_physics.cpp b/main/tests/test_physics.cpp index d32756937a..5a233e818e 100644 --- a/main/tests/test_physics.cpp +++ b/main/tests/test_physics.cpp @@ -263,18 +263,19 @@ protected: } public: - virtual void input_event(const InputEvent &p_event) { + virtual void input_event(const Ref<InputEvent> &p_event) { - if (p_event.type == InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask & 4) { + Ref<InputEventMouseMotion> mm = p_event; + if (mm.is_valid() && mm->get_button_mask() & 4) { - ofs_y -= p_event.mouse_motion.relative_y / 200.0; - ofs_x += p_event.mouse_motion.relative_x / 200.0; + ofs_y -= mm->get_relative().y / 200.0; + ofs_x += mm->get_relative().x / 200.0; } - if (p_event.type == InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask & 1) { + if (mm.is_valid() && mm->get_button_mask() & 1) { - float y = -p_event.mouse_motion.relative_y / 20.0; - float x = p_event.mouse_motion.relative_x / 20.0; + float y = -mm->get_relative().y / 20.0; + float x = mm->get_relative().x / 20.0; if (mover.is_valid()) { @@ -285,19 +286,6 @@ public: ps->body_set_state(mover, PhysicsServer::BODY_STATE_TRANSFORM, t); } } - - if (p_event.type == InputEvent::JOYPAD_MOTION) { - - if (p_event.joy_motion.axis == 0) { - - joy_direction.x = p_event.joy_motion.axis_value; - }; - - if (p_event.joy_motion.axis == 1) { - - joy_direction.y = p_event.joy_motion.axis_value; - }; - }; } virtual void request_quit() { diff --git a/main/tests/test_physics_2d.cpp b/main/tests/test_physics_2d.cpp index d8a00a589a..5f57275503 100644 --- a/main/tests/test_physics_2d.cpp +++ b/main/tests/test_physics_2d.cpp @@ -208,35 +208,36 @@ class TestPhysics2DMainLoop : public MainLoop { } protected: - void input_event(const InputEvent &p_event) { + void input_event(const Ref<InputEvent> &p_event) { - if (p_event.type == InputEvent::MOUSE_BUTTON) { + Ref<InputEventMouseButton> mb = p_event; - const InputEventMouseButton &mb = p_event.mouse_button; + if (mb.is_valid()) { - if (mb.pressed) { + if (mb->is_pressed()) { - Point2 p(mb.x, mb.y); + Point2 p(mb->get_pos().x, mb->get_pos().y); - if (mb.button_index == 1) { + if (mb->get_button_index() == 1) { ray_to = p; _do_ray_query(); - } else if (mb.button_index == 2) { + } else if (mb->get_button_index() == 2) { ray_from = p; _do_ray_query(); } } } - if (p_event.type == InputEvent::MOUSE_MOTION) { - const InputEventMouseMotion &mm = p_event.mouse_motion; + Ref<InputEventMouseMotion> mm = p_event; - Point2 p(mm.x, mm.y); + if (mm.is_valid()) { - if (mm.button_mask & BUTTON_MASK_LEFT) { + Point2 p = mm->get_pos(); + + if (mm->get_button_mask() & BUTTON_MASK_LEFT) { ray_to = p; _do_ray_query(); - } else if (mm.button_mask & BUTTON_MASK_RIGHT) { + } else if (mm->get_button_mask() & BUTTON_MASK_RIGHT) { ray_from = p; _do_ray_query(); } diff --git a/main/tests/test_render.cpp b/main/tests/test_render.cpp index 89bd5db60f..74f7453d1b 100644 --- a/main/tests/test_render.cpp +++ b/main/tests/test_render.cpp @@ -64,9 +64,9 @@ class TestMainLoop : public MainLoop { protected: public: - virtual void input_event(const InputEvent &p_event) { + virtual void input_event(const Ref<InputEvent> &p_event) { - if (p_event.type == InputEvent::KEY && p_event.key.pressed) + if (p_event->is_pressed()) quit = true; } |