parallax_background.cpp 7.1 KB

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