Merge pull request #12421 from TheSofox/parallax-overhaul

Overhauled the ParallaxBackground system
This commit is contained in:
Rémi Verschelde 2017-11-20 15:36:03 +01:00 committed by GitHub
commit 00be297b84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 16 deletions

View File

@ -52,7 +52,11 @@ void Camera2D::_update_scroll() {
if (viewport) { if (viewport) {
viewport->set_canvas_transform(xform); viewport->set_canvas_transform(xform);
} }
get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, group_name, "_camera_moved", xform);
Size2 screen_size = viewport->get_visible_rect().size;
Point2 screen_offset = (anchor_mode == ANCHOR_MODE_DRAG_CENTER ? (screen_size * 0.5) : Point2());
get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, group_name, "_camera_moved", xform, screen_offset);
}; };
} }

View File

@ -47,10 +47,12 @@ void ParallaxBackground::_notification(int p_what) {
} }
} }
void ParallaxBackground::_camera_moved(const Transform2D &p_transform) { void ParallaxBackground::_camera_moved(const Transform2D &p_transform, const Point2 &p_screen_offset) {
screen_offset = p_screen_offset;
set_scroll_scale(p_transform.get_scale().dot(Vector2(0.5, 0.5))); set_scroll_scale(p_transform.get_scale().dot(Vector2(0.5, 0.5)));
set_scroll_offset(p_transform.get_origin() / p_transform.get_scale()); set_scroll_offset(p_transform.get_origin());
} }
void ParallaxBackground::set_scroll_scale(float p_scale) { void ParallaxBackground::set_scroll_scale(float p_scale) {
@ -106,9 +108,9 @@ void ParallaxBackground::_update_scroll() {
continue; continue;
if (ignore_camera_zoom) if (ignore_camera_zoom)
l->set_base_offset_and_scale(ofs, 1.0); l->set_base_offset_and_scale(ofs, 1.0, screen_offset);
else else
l->set_base_offset_and_scale(ofs, scale); l->set_base_offset_and_scale(ofs, scale, screen_offset);
} }
} }

View File

@ -42,6 +42,7 @@ class ParallaxBackground : public CanvasLayer {
float scale; float scale;
Point2 base_offset; Point2 base_offset;
Point2 base_scale; Point2 base_scale;
Point2 screen_offset;
String group_name; String group_name;
Point2 limit_begin; Point2 limit_begin;
Point2 limit_end; Point2 limit_end;
@ -51,7 +52,7 @@ class ParallaxBackground : public CanvasLayer {
void _update_scroll(); void _update_scroll();
protected: protected:
void _camera_moved(const Transform2D &p_transform); void _camera_moved(const Transform2D &p_transform, const Point2 &p_screen_offset);
void _notification(int p_what); void _notification(int p_what);
static void _bind_methods(); static void _bind_methods();

View File

@ -40,7 +40,7 @@ void ParallaxLayer::set_motion_scale(const Size2 &p_scale) {
if (pb && is_inside_tree()) { if (pb && is_inside_tree()) {
Vector2 ofs = pb->get_final_offset(); Vector2 ofs = pb->get_final_offset();
float scale = pb->get_scroll_scale(); float scale = pb->get_scroll_scale();
set_base_offset_and_scale(ofs, scale); set_base_offset_and_scale(ofs, scale, screen_offset);
} }
} }
@ -57,7 +57,7 @@ void ParallaxLayer::set_motion_offset(const Size2 &p_offset) {
if (pb && is_inside_tree()) { if (pb && is_inside_tree()) {
Vector2 ofs = pb->get_final_offset(); Vector2 ofs = pb->get_final_offset();
float scale = pb->get_scroll_scale(); float scale = pb->get_scroll_scale();
set_base_offset_and_scale(ofs, scale); set_base_offset_and_scale(ofs, scale, screen_offset);
} }
} }
@ -106,26 +106,28 @@ void ParallaxLayer::_notification(int p_what) {
} }
} }
void ParallaxLayer::set_base_offset_and_scale(const Point2 &p_offset, float p_scale) { void ParallaxLayer::set_base_offset_and_scale(const Point2 &p_offset, float p_scale, const Point2 &p_screen_offset) {
screen_offset = p_screen_offset;
if (!is_inside_tree()) if (!is_inside_tree())
return; return;
if (Engine::get_singleton()->is_editor_hint()) if (Engine::get_singleton()->is_editor_hint())
return; return;
Point2 new_ofs = ((orig_offset + p_offset) * motion_scale) * p_scale + motion_offset;
Point2 new_ofs = (screen_offset + (p_offset - screen_offset) * motion_scale) + motion_offset * p_scale + orig_offset * p_scale;
Vector2 mirror = Vector2(1, 1);
if (mirroring.x) { if (mirroring.x) {
double den = mirroring.x * p_scale; mirror.x = -1;
new_ofs.x -= den * ceil(new_ofs.x / den);
} }
if (mirroring.y) { if (mirroring.y) {
double den = mirroring.y * p_scale; mirror.y = -1;
new_ofs.y -= den * ceil(new_ofs.y / den);
} }
set_position(new_ofs); set_position(new_ofs);
set_scale(Vector2(1, 1) * p_scale); set_scale(mirror * p_scale * orig_scale);
} }
String ParallaxLayer::get_configuration_warning() const { String ParallaxLayer::get_configuration_warning() const {

View File

@ -43,6 +43,8 @@ class ParallaxLayer : public Node2D {
Vector2 mirroring; Vector2 mirroring;
void _update_mirroring(); void _update_mirroring();
Point2 screen_offset;
protected: protected:
void _notification(int p_what); void _notification(int p_what);
static void _bind_methods(); static void _bind_methods();
@ -57,7 +59,7 @@ public:
void set_mirroring(const Size2 &p_mirroring); void set_mirroring(const Size2 &p_mirroring);
Size2 get_mirroring() const; Size2 get_mirroring() const;
void set_base_offset_and_scale(const Point2 &p_offset, float p_scale); void set_base_offset_and_scale(const Point2 &p_offset, float p_scale, const Point2 &p_screen_offset);
virtual String get_configuration_warning() const; virtual String get_configuration_warning() const;
ParallaxLayer(); ParallaxLayer();