Skip to content

Commit

Permalink
Fix Viewport.get_mouse_pos() for specific situations, closes #1885
Browse files Browse the repository at this point in the history
(cherry picked from commit b16f41a)
  • Loading branch information
reduz authored and akien-mga committed Sep 18, 2016
1 parent 2b43d0a commit 3ff8dea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
31 changes: 14 additions & 17 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1344,17 +1344,23 @@ Matrix32 Viewport::_get_input_pre_xform() const {
return pre_xf;
}

Vector2 Viewport::_get_window_offset() const {

if (parent_control) {
return (parent_control->get_viewport()->get_final_transform() * parent_control->get_global_transform_with_canvas()).get_origin();
}

return Vector2();
}

void Viewport::_make_input_local(InputEvent& ev) {


switch(ev.type) {

case InputEvent::MOUSE_BUTTON: {

Vector2 vp_ofs;
if (parent_control) {
vp_ofs = (parent_control->get_viewport()->get_final_transform() * parent_control->get_global_transform_with_canvas()).get_origin();
}
Vector2 vp_ofs = _get_window_offset();

Matrix32 ai = get_final_transform().affine_inverse() * _get_input_pre_xform();
Vector2 g = ai.xform(Vector2(ev.mouse_button.global_x,ev.mouse_button.global_y));
Expand All @@ -1369,10 +1375,7 @@ void Viewport::_make_input_local(InputEvent& ev) {
} break;
case InputEvent::MOUSE_MOTION: {

Vector2 vp_ofs;
if (parent_control) {
vp_ofs = (parent_control->get_viewport()->get_final_transform() * parent_control->get_global_transform_with_canvas()).get_origin();
}
Vector2 vp_ofs = _get_window_offset();

Matrix32 ai = get_final_transform().affine_inverse() * _get_input_pre_xform();
Vector2 g = ai.xform(Vector2(ev.mouse_motion.global_x,ev.mouse_motion.global_y));
Expand All @@ -1393,10 +1396,7 @@ void Viewport::_make_input_local(InputEvent& ev) {
} break;
case InputEvent::SCREEN_TOUCH: {

Vector2 vp_ofs;
if (parent_control) {
vp_ofs = (parent_control->get_viewport()->get_final_transform() * parent_control->get_global_transform_with_canvas()).get_origin();
}
Vector2 vp_ofs = _get_window_offset();

Matrix32 ai = get_final_transform().affine_inverse() * _get_input_pre_xform();
Vector2 t = ai.xform(Vector2(ev.screen_touch.x,ev.screen_touch.y)-vp_ofs);
Expand All @@ -1408,10 +1408,7 @@ void Viewport::_make_input_local(InputEvent& ev) {
} break;
case InputEvent::SCREEN_DRAG: {

Vector2 vp_ofs;
if (parent_control) {
vp_ofs = (parent_control->get_viewport()->get_final_transform() * parent_control->get_global_transform_with_canvas()).get_origin();
}
Vector2 vp_ofs = _get_window_offset();

Matrix32 ai = get_final_transform().affine_inverse() * _get_input_pre_xform();
Vector2 t = ai.xform(Vector2(ev.screen_drag.x,ev.screen_drag.y)-vp_ofs);
Expand Down Expand Up @@ -1490,7 +1487,7 @@ void Viewport::_vp_unhandled_input(const InputEvent& p_ev) {

Vector2 Viewport::get_mouse_pos() const {

return (get_final_transform().affine_inverse() * _get_input_pre_xform()).xform(Input::get_singleton()->get_mouse_pos());
return (get_final_transform().affine_inverse() * _get_input_pre_xform()).xform(Input::get_singleton()->get_mouse_pos() - _get_window_offset());
}

void Viewport::warp_mouse(const Vector2& p_pos) {
Expand Down
2 changes: 2 additions & 0 deletions scene/main/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ friend class Control;

Control *_gui_get_focus_owner();

Vector2 _get_window_offset() const;

friend class Listener;
void _listener_transform_changed_notify();
void _listener_set(Listener* p_listener);
Expand Down

0 comments on commit 3ff8dea

Please sign in to comment.