parallax_layer.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*************************************************************************/
  2. /* parallax_layer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_layer.h"
  31. #include "core/engine.h"
  32. #include "parallax_background.h"
  33. void ParallaxLayer::set_motion_scale(const Size2 &p_scale) {
  34. motion_scale = p_scale;
  35. ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
  36. if (pb && is_inside_tree()) {
  37. Vector2 ofs = pb->get_final_offset();
  38. float scale = pb->get_scroll_scale();
  39. set_base_offset_and_scale(ofs, scale, screen_offset);
  40. }
  41. }
  42. Size2 ParallaxLayer::get_motion_scale() const {
  43. return motion_scale;
  44. }
  45. void ParallaxLayer::set_motion_offset(const Size2 &p_offset) {
  46. motion_offset = p_offset;
  47. ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
  48. if (pb && is_inside_tree()) {
  49. Vector2 ofs = pb->get_final_offset();
  50. float scale = pb->get_scroll_scale();
  51. set_base_offset_and_scale(ofs, scale, screen_offset);
  52. }
  53. }
  54. Size2 ParallaxLayer::get_motion_offset() const {
  55. return motion_offset;
  56. }
  57. void ParallaxLayer::_update_mirroring() {
  58. if (!is_inside_tree()) {
  59. return;
  60. }
  61. ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
  62. if (pb) {
  63. RID c = pb->get_canvas();
  64. RID ci = get_canvas_item();
  65. Point2 mirrorScale = mirroring * get_scale();
  66. RenderingServer::get_singleton()->canvas_set_item_mirroring(c, ci, mirrorScale);
  67. }
  68. }
  69. void ParallaxLayer::set_mirroring(const Size2 &p_mirroring) {
  70. mirroring = p_mirroring;
  71. if (mirroring.x < 0) {
  72. mirroring.x = 0;
  73. }
  74. if (mirroring.y < 0) {
  75. mirroring.y = 0;
  76. }
  77. _update_mirroring();
  78. }
  79. Size2 ParallaxLayer::get_mirroring() const {
  80. return mirroring;
  81. }
  82. void ParallaxLayer::_notification(int p_what) {
  83. switch (p_what) {
  84. case NOTIFICATION_ENTER_TREE: {
  85. orig_offset = get_position();
  86. orig_scale = get_scale();
  87. _update_mirroring();
  88. } break;
  89. case NOTIFICATION_EXIT_TREE: {
  90. set_position(orig_offset);
  91. set_scale(orig_scale);
  92. } break;
  93. }
  94. }
  95. void ParallaxLayer::set_base_offset_and_scale(const Point2 &p_offset, float p_scale, const Point2 &p_screen_offset) {
  96. screen_offset = p_screen_offset;
  97. if (!is_inside_tree()) {
  98. return;
  99. }
  100. if (Engine::get_singleton()->is_editor_hint()) {
  101. return;
  102. }
  103. Point2 new_ofs = (screen_offset + (p_offset - screen_offset) * motion_scale) + motion_offset * p_scale + orig_offset * p_scale;
  104. if (mirroring.x) {
  105. double den = mirroring.x * p_scale;
  106. new_ofs.x -= den * ceil(new_ofs.x / den);
  107. }
  108. if (mirroring.y) {
  109. double den = mirroring.y * p_scale;
  110. new_ofs.y -= den * ceil(new_ofs.y / den);
  111. }
  112. set_position(new_ofs);
  113. set_scale(Vector2(1, 1) * p_scale * orig_scale);
  114. _update_mirroring();
  115. }
  116. String ParallaxLayer::get_configuration_warning() const {
  117. String warning = Node2D::get_configuration_warning();
  118. if (!Object::cast_to<ParallaxBackground>(get_parent())) {
  119. if (!warning.empty()) {
  120. warning += "\n\n";
  121. }
  122. warning += TTR("ParallaxLayer node only works when set as child of a ParallaxBackground node.");
  123. }
  124. return warning;
  125. }
  126. void ParallaxLayer::_bind_methods() {
  127. ClassDB::bind_method(D_METHOD("set_motion_scale", "scale"), &ParallaxLayer::set_motion_scale);
  128. ClassDB::bind_method(D_METHOD("get_motion_scale"), &ParallaxLayer::get_motion_scale);
  129. ClassDB::bind_method(D_METHOD("set_motion_offset", "offset"), &ParallaxLayer::set_motion_offset);
  130. ClassDB::bind_method(D_METHOD("get_motion_offset"), &ParallaxLayer::get_motion_offset);
  131. ClassDB::bind_method(D_METHOD("set_mirroring", "mirror"), &ParallaxLayer::set_mirroring);
  132. ClassDB::bind_method(D_METHOD("get_mirroring"), &ParallaxLayer::get_mirroring);
  133. ADD_GROUP("Motion", "motion_");
  134. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_scale"), "set_motion_scale", "get_motion_scale");
  135. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_offset"), "set_motion_offset", "get_motion_offset");
  136. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_mirroring"), "set_mirroring", "get_mirroring");
  137. }
  138. ParallaxLayer::ParallaxLayer() {
  139. motion_scale = Size2(1, 1);
  140. }