parallax_layer.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**************************************************************************/
  2. /* parallax_layer.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_layer.h"
  31. #include "parallax_background.h"
  32. void ParallaxLayer::set_motion_scale(const Size2 &p_scale) {
  33. motion_scale = p_scale;
  34. ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
  35. if (pb && is_inside_tree()) {
  36. Vector2 final_ofs = pb->get_final_offset();
  37. real_t scroll_scale = pb->get_scroll_scale();
  38. set_base_offset_and_scale(final_ofs, scroll_scale);
  39. }
  40. }
  41. Size2 ParallaxLayer::get_motion_scale() const {
  42. return motion_scale;
  43. }
  44. void ParallaxLayer::set_motion_offset(const Size2 &p_offset) {
  45. motion_offset = p_offset;
  46. ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
  47. if (pb && is_inside_tree()) {
  48. Vector2 final_ofs = pb->get_final_offset();
  49. real_t scroll_scale = pb->get_scroll_scale();
  50. set_base_offset_and_scale(final_ofs, scroll_scale);
  51. }
  52. }
  53. Size2 ParallaxLayer::get_motion_offset() const {
  54. return motion_offset;
  55. }
  56. void ParallaxLayer::_update_mirroring() {
  57. if (!is_inside_tree()) {
  58. return;
  59. }
  60. ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
  61. if (pb) {
  62. RID c = pb->get_canvas();
  63. RID ci = get_canvas_item();
  64. Point2 mirror_scale = mirroring * orig_scale;
  65. RenderingServer::get_singleton()->canvas_set_item_mirroring(c, ci, mirror_scale);
  66. RenderingServer::get_singleton()->canvas_item_set_interpolated(ci, false);
  67. }
  68. }
  69. void ParallaxLayer::set_mirroring(const Size2 &p_mirroring) {
  70. mirroring = p_mirroring.maxf(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_position();
  80. orig_scale = get_scale();
  81. _update_mirroring();
  82. } break;
  83. case NOTIFICATION_EXIT_TREE: {
  84. if (Engine::get_singleton()->is_editor_hint()) {
  85. break;
  86. }
  87. set_position(orig_offset);
  88. set_scale(orig_scale);
  89. } break;
  90. }
  91. }
  92. void ParallaxLayer::set_base_offset_and_scale(const Point2 &p_offset, real_t p_scale) {
  93. if (!is_inside_tree()) {
  94. return;
  95. }
  96. if (Engine::get_singleton()->is_editor_hint()) {
  97. return;
  98. }
  99. Point2 new_ofs = p_offset * motion_scale + motion_offset * p_scale + orig_offset * p_scale;
  100. if (mirroring.x) {
  101. real_t den = mirroring.x * p_scale;
  102. new_ofs.x -= den * ceil(new_ofs.x / den);
  103. }
  104. if (mirroring.y) {
  105. real_t den = mirroring.y * p_scale;
  106. new_ofs.y -= den * ceil(new_ofs.y / den);
  107. }
  108. set_position(new_ofs);
  109. set_scale(Vector2(1, 1) * p_scale * orig_scale);
  110. _update_mirroring();
  111. }
  112. PackedStringArray ParallaxLayer::get_configuration_warnings() const {
  113. PackedStringArray warnings = Node2D::get_configuration_warnings();
  114. if (!Object::cast_to<ParallaxBackground>(get_parent())) {
  115. warnings.push_back(RTR("ParallaxLayer node only works when set as child of a ParallaxBackground node."));
  116. }
  117. return warnings;
  118. }
  119. void ParallaxLayer::_bind_methods() {
  120. ClassDB::bind_method(D_METHOD("set_motion_scale", "scale"), &ParallaxLayer::set_motion_scale);
  121. ClassDB::bind_method(D_METHOD("get_motion_scale"), &ParallaxLayer::get_motion_scale);
  122. ClassDB::bind_method(D_METHOD("set_motion_offset", "offset"), &ParallaxLayer::set_motion_offset);
  123. ClassDB::bind_method(D_METHOD("get_motion_offset"), &ParallaxLayer::get_motion_offset);
  124. ClassDB::bind_method(D_METHOD("set_mirroring", "mirror"), &ParallaxLayer::set_mirroring);
  125. ClassDB::bind_method(D_METHOD("get_mirroring"), &ParallaxLayer::get_mirroring);
  126. ADD_GROUP("Motion", "motion_");
  127. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_scale", PROPERTY_HINT_LINK), "set_motion_scale", "get_motion_scale");
  128. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_motion_offset", "get_motion_offset");
  129. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_mirroring"), "set_mirroring", "get_mirroring");
  130. }
  131. ParallaxLayer::ParallaxLayer() {
  132. // ParallaxLayer is always updated every frame so there is no need to interpolate.
  133. set_physics_interpolation_mode(Node::PHYSICS_INTERPOLATION_MODE_OFF);
  134. }