parallax_background.cpp 6.8 KB

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