parallax_background.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**************************************************************************/
  2. /* parallax_background.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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, const Point2 &p_screen_offset) {
  44. screen_offset = p_screen_offset;
  45. set_scroll_scale(p_transform.get_scale().dot(Vector2(0.5, 0.5)));
  46. set_scroll_offset(p_transform.get_origin());
  47. }
  48. void ParallaxBackground::set_scroll_scale(float p_scale) {
  49. scale = p_scale;
  50. }
  51. float ParallaxBackground::get_scroll_scale() const {
  52. return scale;
  53. }
  54. void ParallaxBackground::set_scroll_offset(const Point2 &p_ofs) {
  55. offset = p_ofs;
  56. _update_scroll();
  57. }
  58. void ParallaxBackground::_update_scroll() {
  59. if (!is_inside_tree()) {
  60. return;
  61. }
  62. Vector2 ofs = base_offset + offset * base_scale;
  63. Size2 vps = get_viewport_size();
  64. ofs = -ofs;
  65. if (limit_begin.x < limit_end.x) {
  66. if (ofs.x < limit_begin.x) {
  67. ofs.x = limit_begin.x;
  68. } else if (ofs.x + vps.x > limit_end.x) {
  69. ofs.x = limit_end.x - vps.x;
  70. }
  71. }
  72. if (limit_begin.y < limit_end.y) {
  73. if (ofs.y < limit_begin.y) {
  74. ofs.y = limit_begin.y;
  75. } else if (ofs.y + vps.y > limit_end.y) {
  76. ofs.y = limit_end.y - vps.y;
  77. }
  78. }
  79. ofs = -ofs;
  80. final_offset = ofs;
  81. for (int i = 0; i < get_child_count(); i++) {
  82. ParallaxLayer *l = Object::cast_to<ParallaxLayer>(get_child(i));
  83. if (!l) {
  84. continue;
  85. }
  86. if (ignore_camera_zoom) {
  87. l->set_base_offset_and_scale((ofs + screen_offset * (scale - 1)) / scale, 1.0, screen_offset);
  88. } else {
  89. l->set_base_offset_and_scale(ofs, scale, screen_offset);
  90. }
  91. }
  92. }
  93. Point2 ParallaxBackground::get_scroll_offset() const {
  94. return offset;
  95. }
  96. void ParallaxBackground::set_scroll_base_offset(const Point2 &p_ofs) {
  97. base_offset = p_ofs;
  98. _update_scroll();
  99. }
  100. Point2 ParallaxBackground::get_scroll_base_offset() const {
  101. return base_offset;
  102. }
  103. void ParallaxBackground::set_scroll_base_scale(const Point2 &p_ofs) {
  104. base_scale = p_ofs;
  105. _update_scroll();
  106. }
  107. Point2 ParallaxBackground::get_scroll_base_scale() const {
  108. return base_scale;
  109. }
  110. void ParallaxBackground::set_limit_begin(const Point2 &p_ofs) {
  111. limit_begin = p_ofs;
  112. _update_scroll();
  113. }
  114. Point2 ParallaxBackground::get_limit_begin() const {
  115. return limit_begin;
  116. }
  117. void ParallaxBackground::set_limit_end(const Point2 &p_ofs) {
  118. limit_end = p_ofs;
  119. _update_scroll();
  120. }
  121. Point2 ParallaxBackground::get_limit_end() const {
  122. return limit_end;
  123. }
  124. void ParallaxBackground::set_ignore_camera_zoom(bool ignore) {
  125. ignore_camera_zoom = ignore;
  126. }
  127. bool ParallaxBackground::is_ignore_camera_zoom() {
  128. return ignore_camera_zoom;
  129. }
  130. Vector2 ParallaxBackground::get_final_offset() const {
  131. return final_offset;
  132. }
  133. void ParallaxBackground::_bind_methods() {
  134. ClassDB::bind_method(D_METHOD("_camera_moved"), &ParallaxBackground::_camera_moved);
  135. ClassDB::bind_method(D_METHOD("set_scroll_offset", "ofs"), &ParallaxBackground::set_scroll_offset);
  136. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &ParallaxBackground::get_scroll_offset);
  137. ClassDB::bind_method(D_METHOD("set_scroll_base_offset", "ofs"), &ParallaxBackground::set_scroll_base_offset);
  138. ClassDB::bind_method(D_METHOD("get_scroll_base_offset"), &ParallaxBackground::get_scroll_base_offset);
  139. ClassDB::bind_method(D_METHOD("set_scroll_base_scale", "scale"), &ParallaxBackground::set_scroll_base_scale);
  140. ClassDB::bind_method(D_METHOD("get_scroll_base_scale"), &ParallaxBackground::get_scroll_base_scale);
  141. ClassDB::bind_method(D_METHOD("set_limit_begin", "ofs"), &ParallaxBackground::set_limit_begin);
  142. ClassDB::bind_method(D_METHOD("get_limit_begin"), &ParallaxBackground::get_limit_begin);
  143. ClassDB::bind_method(D_METHOD("set_limit_end", "ofs"), &ParallaxBackground::set_limit_end);
  144. ClassDB::bind_method(D_METHOD("get_limit_end"), &ParallaxBackground::get_limit_end);
  145. ClassDB::bind_method(D_METHOD("set_ignore_camera_zoom", "ignore"), &ParallaxBackground::set_ignore_camera_zoom);
  146. ClassDB::bind_method(D_METHOD("is_ignore_camera_zoom"), &ParallaxBackground::is_ignore_camera_zoom);
  147. ADD_GROUP("Scroll", "scroll_");
  148. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset"), "set_scroll_offset", "get_scroll_offset");
  149. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_base_offset"), "set_scroll_base_offset", "get_scroll_base_offset");
  150. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_base_scale"), "set_scroll_base_scale", "get_scroll_base_scale");
  151. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_limit_begin"), "set_limit_begin", "get_limit_begin");
  152. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_limit_end"), "set_limit_end", "get_limit_end");
  153. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_ignore_camera_zoom"), "set_ignore_camera_zoom", "is_ignore_camera_zoom");
  154. }
  155. ParallaxBackground::ParallaxBackground() {
  156. scale = 1.0;
  157. set_layer(-100); //behind all by default
  158. base_scale = Vector2(1, 1);
  159. ignore_camera_zoom = false;
  160. }