parallax_layer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*************************************************************************/
  2. /* parallax_layer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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_layer.h"
  30. #include "parallax_background.h"
  31. void ParallaxLayer::set_motion_scale(const Size2& p_scale) {
  32. motion_scale=p_scale;
  33. ParallaxBackground *pb = get_parent()->cast_to<ParallaxBackground>();
  34. if (is_inside_tree() && pb) {
  35. Vector2 ofs = pb->get_final_offset();
  36. float scale = pb->get_scroll_scale();
  37. set_base_offset_and_scale(ofs,scale);
  38. }
  39. }
  40. Size2 ParallaxLayer::get_motion_scale() const {
  41. return motion_scale;
  42. }
  43. void ParallaxLayer::set_motion_offset(const Size2& p_offset) {
  44. motion_offset=p_offset;
  45. ParallaxBackground *pb = get_parent()->cast_to<ParallaxBackground>();
  46. if (is_inside_tree() && pb) {
  47. Vector2 ofs = pb->get_final_offset();
  48. float scale = pb->get_scroll_scale();
  49. set_base_offset_and_scale(ofs,scale);
  50. }
  51. }
  52. Size2 ParallaxLayer::get_motion_offset() const {
  53. return motion_offset;
  54. }
  55. void ParallaxLayer::_update_mirroring() {
  56. if (!get_parent())
  57. return;
  58. ParallaxBackground *pb = get_parent()->cast_to<ParallaxBackground>();
  59. if (pb) {
  60. RID c = pb->get_world_2d()->get_canvas();
  61. RID ci = get_canvas_item();
  62. VisualServer::get_singleton()->canvas_set_item_mirroring(c,ci,mirroring);
  63. }
  64. }
  65. void ParallaxLayer::set_mirroring(const Size2& p_mirroring) {
  66. mirroring=p_mirroring;
  67. if (mirroring.x<0)
  68. mirroring.x=0;
  69. if (mirroring.y<0)
  70. mirroring.y=0;
  71. _update_mirroring();
  72. }
  73. Size2 ParallaxLayer::get_mirroring() const{
  74. return mirroring;
  75. }
  76. void ParallaxLayer::_notification(int p_what) {
  77. switch(p_what) {
  78. case NOTIFICATION_ENTER_TREE: {
  79. orig_offset=get_pos();
  80. orig_scale=get_scale();
  81. _update_mirroring();
  82. } break;
  83. }
  84. }
  85. void ParallaxLayer::set_base_offset_and_scale(const Point2& p_offset,float p_scale) {
  86. if (!is_inside_tree())
  87. return;
  88. if (get_tree()->is_editor_hint())
  89. return;
  90. Point2 new_ofs = ((orig_offset+p_offset)*motion_scale)*p_scale+motion_offset;
  91. if (mirroring.x) {
  92. while( new_ofs.x>=0) {
  93. new_ofs.x -= mirroring.x*p_scale;
  94. }
  95. while(new_ofs.x < -mirroring.x*p_scale) {
  96. new_ofs.x += mirroring.x*p_scale;
  97. }
  98. }
  99. if (mirroring.y) {
  100. while( new_ofs.y>=0) {
  101. new_ofs.y -= mirroring.y*p_scale;
  102. }
  103. while(new_ofs.y < -mirroring.y*p_scale) {
  104. new_ofs.y += mirroring.y*p_scale;
  105. }
  106. }
  107. set_pos(new_ofs);
  108. set_scale(Vector2(1,1)*p_scale);
  109. }
  110. String ParallaxLayer::get_configuration_warning() const {
  111. if (!get_parent() || !get_parent()->cast_to<ParallaxBackground>()) {
  112. return TTR("ParallaxLayer node only works when set as child of a ParallaxBackground node.");
  113. }
  114. return String();
  115. }
  116. void ParallaxLayer::_bind_methods() {
  117. ObjectTypeDB::bind_method(_MD("set_motion_scale","scale"),&ParallaxLayer::set_motion_scale);
  118. ObjectTypeDB::bind_method(_MD("get_motion_scale"),&ParallaxLayer::get_motion_scale);
  119. ObjectTypeDB::bind_method(_MD("set_motion_offset","offset"),&ParallaxLayer::set_motion_offset);
  120. ObjectTypeDB::bind_method(_MD("get_motion_offset"),&ParallaxLayer::get_motion_offset);
  121. ObjectTypeDB::bind_method(_MD("set_mirroring","mirror"),&ParallaxLayer::set_mirroring);
  122. ObjectTypeDB::bind_method(_MD("get_mirroring"),&ParallaxLayer::get_mirroring);
  123. ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"motion/scale"),_SCS("set_motion_scale"),_SCS("get_motion_scale"));
  124. ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"motion/offset"),_SCS("set_motion_offset"),_SCS("get_motion_offset"));
  125. ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"motion/mirroring"),_SCS("set_mirroring"),_SCS("get_mirroring"));
  126. }
  127. ParallaxLayer::ParallaxLayer()
  128. {
  129. motion_scale=Size2(1,1);
  130. }