From 6686fbc1e0458bc87909863d4a7ed018325be2d6 Mon Sep 17 00:00:00 2001 From: Timo Schwarzer Date: Wed, 29 Jun 2016 17:39:29 +0200 Subject: [PATCH 1/4] Add reset_smoothing() for immediately fixing the camera to the destination location --- scene/2d/camera_2d.cpp | 7 +++++++ scene/2d/camera_2d.h | 1 + 2 files changed, 8 insertions(+) diff --git a/scene/2d/camera_2d.cpp b/scene/2d/camera_2d.cpp index 85256be9409..71fd421b40c 100644 --- a/scene/2d/camera_2d.cpp +++ b/scene/2d/camera_2d.cpp @@ -409,6 +409,12 @@ void Camera2D::force_update_scroll() { _update_scroll(); } +void Camera2D::reset_smoothing() { + + smoothed_camera_pos = camera_pos; + _update_scroll(); +} + void Camera2D::set_follow_smoothing(float p_speed) { @@ -544,6 +550,7 @@ void Camera2D::_bind_methods() { ObjectTypeDB::bind_method(_MD("is_follow_smoothing_enabled"),&Camera2D::is_follow_smoothing_enabled); ObjectTypeDB::bind_method(_MD("force_update_scroll"),&Camera2D::force_update_scroll); + ObjectTypeDB::bind_method(_MD("reset_smoothing"),&Camera2D::reset_smoothing); ObjectTypeDB::bind_method(_MD("_set_old_smoothing","follow_smoothing"),&Camera2D::_set_old_smoothing); diff --git a/scene/2d/camera_2d.h b/scene/2d/camera_2d.h index 22e5bc382a6..8c8071e0630 100644 --- a/scene/2d/camera_2d.h +++ b/scene/2d/camera_2d.h @@ -128,6 +128,7 @@ public: Vector2 get_camera_pos() const; void force_update_scroll(); + void reset_smoothing(); Camera2D(); }; From ae055ebf82d46fd64d79c01369d60709a2d6c44e Mon Sep 17 00:00:00 2001 From: Timo Schwarzer Date: Wed, 29 Jun 2016 18:17:11 +0200 Subject: [PATCH 2/4] Add align() to realign the Camera2D to its tracked node align() will center the tracked Node if anchor mode is set to DRAG_CENTER, otherwise the camera is set to the Node's position --- scene/2d/camera_2d.cpp | 24 ++++++++++++++++++++++++ scene/2d/camera_2d.h | 1 + 2 files changed, 25 insertions(+) diff --git a/scene/2d/camera_2d.cpp b/scene/2d/camera_2d.cpp index 71fd421b40c..94b6b0f919d 100644 --- a/scene/2d/camera_2d.cpp +++ b/scene/2d/camera_2d.cpp @@ -415,6 +415,29 @@ void Camera2D::reset_smoothing() { _update_scroll(); } +void Camera2D::align() { + + Size2 screen_size = get_viewport_rect().size; + screen_size=get_viewport_rect().size; + Point2 current_camera_pos = get_global_transform().get_origin(); + if (anchor_mode==ANCHOR_MODE_DRAG_CENTER) { + if (h_ofs<0) { + camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_RIGHT] * h_ofs; + } else { + camera_pos.x = current_camera_pos.x + screen_size.x * 0.5 * drag_margin[MARGIN_LEFT] * h_ofs; + } + if (v_ofs<0) { + camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_TOP] * v_ofs; + } else { + camera_pos.y = current_camera_pos.y + screen_size.y * 0.5 * drag_margin[MARGIN_BOTTOM] * v_ofs; + } + } else if (anchor_mode==ANCHOR_MODE_FIXED_TOP_LEFT){ + + camera_pos=current_camera_pos; + } + + _update_scroll(); +} void Camera2D::set_follow_smoothing(float p_speed) { @@ -551,6 +574,7 @@ void Camera2D::_bind_methods() { ObjectTypeDB::bind_method(_MD("force_update_scroll"),&Camera2D::force_update_scroll); ObjectTypeDB::bind_method(_MD("reset_smoothing"),&Camera2D::reset_smoothing); + ObjectTypeDB::bind_method(_MD("align"),&Camera2D::align); ObjectTypeDB::bind_method(_MD("_set_old_smoothing","follow_smoothing"),&Camera2D::_set_old_smoothing); diff --git a/scene/2d/camera_2d.h b/scene/2d/camera_2d.h index 8c8071e0630..a87909b9d74 100644 --- a/scene/2d/camera_2d.h +++ b/scene/2d/camera_2d.h @@ -129,6 +129,7 @@ public: Vector2 get_camera_pos() const; void force_update_scroll(); void reset_smoothing(); + void center(); Camera2D(); }; From 681471e3f097238b5dd25496bd04e92d5fe6ccdf Mon Sep 17 00:00:00 2001 From: Timo Schwarzer Date: Wed, 29 Jun 2016 18:31:43 +0200 Subject: [PATCH 3/4] Add documentation --- doc/base/classes.xml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/doc/base/classes.xml b/doc/base/classes.xml index 4b5c424d4bf..1880992c379 100644 --- a/doc/base/classes.xml +++ b/doc/base/classes.xml @@ -6678,6 +6678,17 @@ Force the camera to update scroll immediately. + + + Set the camera's position immediately to its current smoothing destination. + This has no effect if smoothing is disabled. + + + + + Align the camera to the tracked node + + From 185ba75bfac7fc958bf334ca017579177e094149 Mon Sep 17 00:00:00 2001 From: Timo Schwarzer Date: Thu, 30 Jun 2016 16:07:48 +0200 Subject: [PATCH 4/4] Fix Camera2D header for align() --- scene/2d/camera_2d.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scene/2d/camera_2d.h b/scene/2d/camera_2d.h index a87909b9d74..b3f55d798dd 100644 --- a/scene/2d/camera_2d.h +++ b/scene/2d/camera_2d.h @@ -129,7 +129,7 @@ public: Vector2 get_camera_pos() const; void force_update_scroll(); void reset_smoothing(); - void center(); + void align(); Camera2D(); };