parallax_background.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*************************************************************************/
  2. /* parallax_background.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "parallax_background.h"
  31. #include "parallax_layer.h"
  32. void ParallaxBackground::_notification(int p_what) {
  33. switch (p_what) {
  34. case NOTIFICATION_ENTER_TREE: {
  35. group_name = "__cameras_" + itos(get_viewport().get_id());
  36. add_to_group(group_name);
  37. } break;
  38. case NOTIFICATION_EXIT_TREE: {
  39. remove_from_group(group_name);
  40. } break;
  41. }
  42. }
  43. void ParallaxBackground::_camera_moved(const Transform2D &p_transform) {
  44. set_scroll_scale(p_transform.get_scale().dot(Vector2(0.5, 0.5)));
  45. set_scroll_offset(p_transform.get_origin() / p_transform.get_scale());
  46. }
  47. void ParallaxBackground::set_scroll_scale(float p_scale) {
  48. scale = p_scale;
  49. }
  50. float ParallaxBackground::get_scroll_scale() const {
  51. return scale;
  52. }
  53. void ParallaxBackground::set_scroll_offset(const Point2 &p_ofs) {
  54. offset = p_ofs;
  55. _update_scroll();
  56. }
  57. void ParallaxBackground::_update_scroll() {
  58. if (!is_inside_tree())
  59. return;
  60. Vector2 ofs = base_offset + offset * base_scale;
  61. Size2 vps = get_viewport_size();
  62. ofs = -ofs;
  63. if (limit_begin.x < limit_end.x) {
  64. if (ofs.x < limit_begin.x)
  65. ofs.x = limit_begin.x;
  66. else if (ofs.x + vps.x > limit_end.x)
  67. ofs.x = limit_end.x - vps.x;
  68. }
  69. if (limit_begin.y < limit_end.y) {
  70. if (ofs.y < limit_begin.y)
  71. ofs.y = limit_begin.y;
  72. else if (ofs.y + vps.y > limit_end.y)
  73. ofs.y = limit_end.y - vps.y;
  74. }
  75. ofs = -ofs;
  76. final_offset = ofs;
  77. for (int i = 0; i < get_child_count(); i++) {
  78. ParallaxLayer *l = Object::cast_to<ParallaxLayer>(get_child(i));
  79. if (!l)
  80. continue;
  81. if (ignore_camera_zoom)
  82. l->set_base_offset_and_scale(ofs, 1.0);
  83. else
  84. l->set_base_offset_and_scale(ofs, scale);
  85. }
  86. }
  87. Point2 ParallaxBackground::get_scroll_offset() const {
  88. return offset;
  89. }
  90. void ParallaxBackground::set_scroll_base_offset(const Point2 &p_ofs) {
  91. base_offset = p_ofs;
  92. _update_scroll();
  93. }
  94. Point2 ParallaxBackground::get_scroll_base_offset() const {
  95. return base_offset;
  96. }
  97. void ParallaxBackground::set_scroll_base_scale(const Point2 &p_ofs) {
  98. base_scale = p_ofs;
  99. _update_scroll();
  100. }
  101. Point2 ParallaxBackground::get_scroll_base_scale() const {
  102. return base_scale;
  103. }
  104. void ParallaxBackground::set_limit_begin(const Point2 &p_ofs) {
  105. limit_begin = p_ofs;
  106. _update_scroll();
  107. }
  108. Point2 ParallaxBackground::get_limit_begin() const {
  109. return limit_begin;
  110. }
  111. void ParallaxBackground::set_limit_end(const Point2 &p_ofs) {
  112. limit_end = p_ofs;
  113. _update_scroll();
  114. }
  115. Point2 ParallaxBackground::get_limit_end() const {
  116. return limit_end;
  117. }
  118. void ParallaxBackground::set_ignore_camera_zoom(bool ignore) {
  119. ignore_camera_zoom = ignore;
  120. }
  121. bool ParallaxBackground::is_ignore_camera_zoom() {
  122. return ignore_camera_zoom;
  123. }
  124. Vector2 ParallaxBackground::get_final_offset() const {
  125. return final_offset;
  126. }
  127. void ParallaxBackground::_bind_methods() {
  128. ClassDB::bind_method(D_METHOD("_camera_moved"), &ParallaxBackground::_camera_moved);
  129. ClassDB::bind_method(D_METHOD("set_scroll_offset", "ofs"), &ParallaxBackground::set_scroll_offset);
  130. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &ParallaxBackground::get_scroll_offset);
  131. ClassDB::bind_method(D_METHOD("set_scroll_base_offset", "ofs"), &ParallaxBackground::set_scroll_base_offset);
  132. ClassDB::bind_method(D_METHOD("get_scroll_base_offset"), &ParallaxBackground::get_scroll_base_offset);
  133. ClassDB::bind_method(D_METHOD("set_scroll_base_scale", "scale"), &ParallaxBackground::set_scroll_base_scale);
  134. ClassDB::bind_method(D_METHOD("get_scroll_base_scale"), &ParallaxBackground::get_scroll_base_scale);
  135. ClassDB::bind_method(D_METHOD("set_limit_begin", "ofs"), &ParallaxBackground::set_limit_begin);
  136. ClassDB::bind_method(D_METHOD("get_limit_begin"), &ParallaxBackground::get_limit_begin);
  137. ClassDB::bind_method(D_METHOD("set_limit_end", "ofs"), &ParallaxBackground::set_limit_end);
  138. ClassDB::bind_method(D_METHOD("get_limit_end"), &ParallaxBackground::get_limit_end);
  139. ClassDB::bind_method(D_METHOD("set_ignore_camera_zoom", "ignore"), &ParallaxBackground::set_ignore_camera_zoom);
  140. ClassDB::bind_method(D_METHOD("is_ignore_camera_zoom"), &ParallaxBackground::is_ignore_camera_zoom);
  141. ADD_GROUP("Scroll", "scroll_");
  142. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset"), "set_scroll_offset", "get_scroll_offset");
  143. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_base_offset"), "set_scroll_base_offset", "get_scroll_base_offset");
  144. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_base_scale"), "set_scroll_base_scale", "get_scroll_base_scale");
  145. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_limit_begin"), "set_limit_begin", "get_limit_begin");
  146. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_limit_end"), "set_limit_end", "get_limit_end");
  147. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_ignore_camera_zoom"), "set_ignore_camera_zoom", "is_ignore_camera_zoom");
  148. }
  149. ParallaxBackground::ParallaxBackground() {
  150. base_scale = Vector2(1, 1);
  151. scale = 1.0;
  152. set_layer(-1); //behind all by default
  153. }