diff options
author | Poommetee Ketson <poommetee@protonmail.com> | 2017-10-02 23:42:43 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-02 23:42:43 +0700 |
commit | 980e78749b738ee224468730593fef401d415740 (patch) | |
tree | eb7d1f66bf427c26c7b12d4013da0ffe937458b6 | |
parent | e8f0817eac919469ef1a97a077b0d6a9cab51bf4 (diff) | |
parent | a0fb178b58d24d886bfa417a9fe5f298ce5e3e58 (diff) |
Merge pull request #11743 from groud/zoom_button_fix
Fix zoom button not zooming on the screen center
-rw-r--r-- | editor/plugins/canvas_item_editor_plugin.cpp | 65 | ||||
-rw-r--r-- | editor/plugins/canvas_item_editor_plugin.h | 1 |
2 files changed, 22 insertions, 44 deletions
diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index 0de36df94a..3310f1f496 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -1219,50 +1219,26 @@ void CanvasItemEditor::_viewport_base_gui_input(const Ref<InputEvent> &p_event) if (b->get_button_index() == BUTTON_WHEEL_DOWN) { // Scroll or pan down if (bool(EditorSettings::get_singleton()->get("editors/2d/scroll_to_pan"))) { - v_scroll->set_value(v_scroll->get_value() + int(EditorSettings::get_singleton()->get("editors/2d/pan_speed")) / zoom * b->get_factor()); - + _update_scroll(0); + viewport->update(); } else { - - if (zoom < MIN_ZOOM) - return; - - float prev_zoom = zoom; - zoom = zoom * (1 - (0.05 * b->get_factor())); - { - Point2 ofs = viewport_scrollable->get_transform().affine_inverse().xform(b->get_position()); - ofs = ofs / prev_zoom - ofs / zoom; - h_scroll->set_value(h_scroll->get_value() + ofs.x); - v_scroll->set_value(v_scroll->get_value() + ofs.y); - } + _zoom_on_position(zoom * (1 - (0.05 * b->get_factor())), viewport_scrollable->get_transform().affine_inverse().xform(b->get_position())); } - _update_scroll(0); - viewport->update(); return; } if (b->get_button_index() == BUTTON_WHEEL_UP) { // Scroll or pan up if (bool(EditorSettings::get_singleton()->get("editors/2d/scroll_to_pan"))) { - v_scroll->set_value(v_scroll->get_value() - int(EditorSettings::get_singleton()->get("editors/2d/pan_speed")) / zoom * b->get_factor()); - + _update_scroll(0); + viewport->update(); } else { - if (zoom > MAX_ZOOM) return; - - float prev_zoom = zoom; - zoom = zoom * ((0.95 + (0.05 * b->get_factor())) / 0.95); - { - Point2 ofs = viewport_scrollable->get_transform().affine_inverse().xform(b->get_position()); - ofs = ofs / prev_zoom - ofs / zoom; - h_scroll->set_value(h_scroll->get_value() + ofs.x); - v_scroll->set_value(v_scroll->get_value() + ofs.y); - } + _zoom_on_position(zoom * ((0.95 + (0.05 * b->get_factor())) / 0.95), viewport_scrollable->get_transform().affine_inverse().xform(b->get_position())); } - _update_scroll(0); - viewport->update(); return; } @@ -3069,31 +3045,32 @@ void CanvasItemEditor::_set_full_rect() { undo_redo->commit_action(); } -void CanvasItemEditor::_zoom_minus() { - if (zoom < MIN_ZOOM) +void CanvasItemEditor::_zoom_on_position(float p_zoom, Point2 p_position) { + if (p_zoom < MIN_ZOOM || p_zoom > MAX_ZOOM) return; - zoom /= 2.0; + + float prev_zoom = zoom; + zoom = p_zoom; + Point2 ofs = p_position; + ofs = ofs / prev_zoom - ofs / zoom; + h_scroll->set_value(h_scroll->get_value() + ofs.x); + v_scroll->set_value(v_scroll->get_value() + ofs.y); _update_scroll(0); viewport->update(); viewport_base->update(); } +void CanvasItemEditor::_zoom_minus() { + _zoom_on_position(zoom / 2.0, viewport_scrollable->get_size() / 2.0); +} + void CanvasItemEditor::_zoom_reset() { - zoom = 1; - _update_scroll(0); - viewport->update(); - viewport_base->update(); + _zoom_on_position(1.0, viewport_scrollable->get_size() / 2.0); } void CanvasItemEditor::_zoom_plus() { - if (zoom > MAX_ZOOM) - return; - - zoom *= 2.0; - _update_scroll(0); - viewport->update(); - viewport_base->update(); + _zoom_on_position(zoom * 2.0, viewport_scrollable->get_size() / 2.0); } void CanvasItemEditor::_toggle_snap(bool p_status) { diff --git a/editor/plugins/canvas_item_editor_plugin.h b/editor/plugins/canvas_item_editor_plugin.h index af834507e0..bb4e584f2a 100644 --- a/editor/plugins/canvas_item_editor_plugin.h +++ b/editor/plugins/canvas_item_editor_plugin.h @@ -387,6 +387,7 @@ class CanvasItemEditor : public VBoxContainer { void _set_anchors_preset(Control::LayoutPreset p_preset); void _set_full_rect(); + void _zoom_on_position(float p_zoom, Point2 p_position = Point2()); void _zoom_minus(); void _zoom_reset(); void _zoom_plus(); |