瀏覽代碼

Added an option to ParallaxLayer to add an extra scroll offset, helps if you want to make clouds to move by themselves and animate that, for example.

Juan Linietsky 9 年之前
父節點
當前提交
282da142ae
共有 4 個文件被更改,包括 50 次插入7 次删除
  1. 11 4
      scene/2d/parallax_background.cpp
  2. 5 2
      scene/2d/parallax_background.h
  3. 30 1
      scene/2d/parallax_layer.cpp
  4. 4 0
      scene/2d/parallax_layer.h

+ 11 - 4
scene/2d/parallax_background.cpp

@@ -104,16 +104,18 @@ void ParallaxBackground::_update_scroll() {
 	}
 	}
 	ofs = -ofs;
 	ofs = -ofs;
 
 
+	final_offset=ofs;
+
 	for(int i=0;i<get_child_count();i++) {
 	for(int i=0;i<get_child_count();i++) {
 
 
 		ParallaxLayer *l=get_child(i)->cast_to<ParallaxLayer>();
 		ParallaxLayer *l=get_child(i)->cast_to<ParallaxLayer>();
 		if (!l)
 		if (!l)
 			continue;
 			continue;
 
 
-        if (ignore_camera_zoom)
-            l->set_base_offset_and_scale(ofs, 1.0);
-        else
-            l->set_base_offset_and_scale(ofs, scale);
+		if (ignore_camera_zoom)
+			l->set_base_offset_and_scale(ofs, 1.0);
+		else
+			l->set_base_offset_and_scale(ofs, scale);
 	}
 	}
 }
 }
 
 
@@ -180,6 +182,11 @@ bool ParallaxBackground::is_ignore_camera_zoom(){
 
 
 }
 }
 
 
+Vector2 ParallaxBackground::get_final_offset() const {
+
+	return final_offset;
+}
+
 void ParallaxBackground::_bind_methods() {
 void ParallaxBackground::_bind_methods() {
 
 
 	ObjectTypeDB::bind_method(_MD("_camera_moved"),&ParallaxBackground::_camera_moved);
 	ObjectTypeDB::bind_method(_MD("_camera_moved"),&ParallaxBackground::_camera_moved);

+ 5 - 2
scene/2d/parallax_background.h

@@ -44,6 +44,7 @@ class ParallaxBackground : public CanvasLayer {
 	String group_name;
 	String group_name;
 	Point2 limit_begin;
 	Point2 limit_begin;
 	Point2 limit_end;
 	Point2 limit_end;
+	Point2 final_offset;
 	bool ignore_camera_zoom;
 	bool ignore_camera_zoom;
 
 
 	void _update_scroll();
 	void _update_scroll();
@@ -73,8 +74,10 @@ public:
 	void set_limit_end(const Point2& p_ofs);
 	void set_limit_end(const Point2& p_ofs);
 	Point2 get_limit_end() const;
 	Point2 get_limit_end() const;
 
 
-    void set_ignore_camera_zoom(bool ignore);
-    bool is_ignore_camera_zoom();
+	void set_ignore_camera_zoom(bool ignore);
+	bool is_ignore_camera_zoom();
+
+	Vector2 get_final_offset() const;
 
 
 	ParallaxBackground();
 	ParallaxBackground();
 };
 };

+ 30 - 1
scene/2d/parallax_layer.cpp

@@ -32,6 +32,15 @@
 void ParallaxLayer::set_motion_scale(const Size2& p_scale) {
 void ParallaxLayer::set_motion_scale(const Size2& p_scale) {
 
 
 	motion_scale=p_scale;
 	motion_scale=p_scale;
+
+
+	ParallaxBackground *pb = get_parent()->cast_to<ParallaxBackground>();
+	if (is_inside_tree() && pb) {
+		Vector2 ofs = pb->get_final_offset();
+		float scale = pb->get_scroll_scale();
+		set_base_offset_and_scale(ofs,scale);
+	}
+
 }
 }
 
 
 Size2 ParallaxLayer::get_motion_scale() const {
 Size2 ParallaxLayer::get_motion_scale() const {
@@ -40,6 +49,23 @@ Size2 ParallaxLayer::get_motion_scale() const {
 
 
 }
 }
 
 
+void ParallaxLayer::set_motion_offset(const Size2& p_offset) {
+
+	motion_offset=p_offset;
+
+	ParallaxBackground *pb = get_parent()->cast_to<ParallaxBackground>();
+	if (is_inside_tree() && pb) {
+		Vector2 ofs = pb->get_final_offset();
+		float scale = pb->get_scroll_scale();
+		set_base_offset_and_scale(ofs,scale);
+	}
+}
+
+Size2 ParallaxLayer::get_motion_offset() const {
+
+	return motion_offset;
+
+}
 
 
 void ParallaxLayer::_update_mirroring() {
 void ParallaxLayer::_update_mirroring() {
 
 
@@ -89,7 +115,7 @@ void ParallaxLayer::set_base_offset_and_scale(const Point2& p_offset,float p_sca
 		return;
 		return;
 	if (get_tree()->is_editor_hint())
 	if (get_tree()->is_editor_hint())
 		return;
 		return;
-	Point2 new_ofs = ((orig_offset+p_offset)*motion_scale)*p_scale;
+	Point2 new_ofs = ((orig_offset+p_offset)*motion_scale)*p_scale+motion_offset;
 
 
 	if (mirroring.x) {
 	if (mirroring.x) {
 
 
@@ -132,10 +158,13 @@ void ParallaxLayer::_bind_methods() {
 
 
 	ObjectTypeDB::bind_method(_MD("set_motion_scale","scale"),&ParallaxLayer::set_motion_scale);
 	ObjectTypeDB::bind_method(_MD("set_motion_scale","scale"),&ParallaxLayer::set_motion_scale);
 	ObjectTypeDB::bind_method(_MD("get_motion_scale"),&ParallaxLayer::get_motion_scale);
 	ObjectTypeDB::bind_method(_MD("get_motion_scale"),&ParallaxLayer::get_motion_scale);
+	ObjectTypeDB::bind_method(_MD("set_motion_offset","offset"),&ParallaxLayer::set_motion_offset);
+	ObjectTypeDB::bind_method(_MD("get_motion_offset"),&ParallaxLayer::get_motion_offset);
 	ObjectTypeDB::bind_method(_MD("set_mirroring","mirror"),&ParallaxLayer::set_mirroring);
 	ObjectTypeDB::bind_method(_MD("set_mirroring","mirror"),&ParallaxLayer::set_mirroring);
 	ObjectTypeDB::bind_method(_MD("get_mirroring"),&ParallaxLayer::get_mirroring);
 	ObjectTypeDB::bind_method(_MD("get_mirroring"),&ParallaxLayer::get_mirroring);
 
 
 	ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"motion/scale"),_SCS("set_motion_scale"),_SCS("get_motion_scale"));
 	ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"motion/scale"),_SCS("set_motion_scale"),_SCS("get_motion_scale"));
+	ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"motion/offset"),_SCS("set_motion_offset"),_SCS("get_motion_offset"));
 	ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"motion/mirroring"),_SCS("set_mirroring"),_SCS("get_mirroring"));
 	ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"motion/mirroring"),_SCS("set_mirroring"),_SCS("get_mirroring"));
 
 
 }
 }

+ 4 - 0
scene/2d/parallax_layer.h

@@ -38,6 +38,7 @@ class ParallaxLayer : public Node2D {
 	Point2 orig_offset;
 	Point2 orig_offset;
 	Point2 orig_scale;
 	Point2 orig_scale;
 	Size2 motion_scale;
 	Size2 motion_scale;
+	Vector2 motion_offset;
 	Vector2 mirroring;
 	Vector2 mirroring;
 	void _update_mirroring();
 	void _update_mirroring();
 
 
@@ -48,6 +49,9 @@ protected:
 
 
 public:
 public:
 
 
+	void set_motion_offset(const Size2& p_scale);
+	Size2 get_motion_offset() const;
+
 	void set_motion_scale(const Size2& p_scale);
 	void set_motion_scale(const Size2& p_scale);
 	Size2 get_motion_scale() const;
 	Size2 get_motion_scale() const;