parallax_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**************************************************************************/
  2. /* parallax_2d.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_2d.h"
  31. #include "scene/main/viewport.h"
  32. void Parallax2D::_notification(int p_what) {
  33. switch (p_what) {
  34. case NOTIFICATION_ENTER_TREE: {
  35. group_name = "__cameras_" + itos(get_viewport_rid().get_id());
  36. add_to_group(group_name);
  37. _update_repeat();
  38. _update_scroll();
  39. } break;
  40. case NOTIFICATION_READY: {
  41. _update_process();
  42. } break;
  43. case NOTIFICATION_INTERNAL_PROCESS: {
  44. Point2 offset = scroll_offset;
  45. offset += autoscroll * get_process_delta_time();
  46. if (repeat_size.x) {
  47. offset.x = Math::fposmod(offset.x, repeat_size.x);
  48. }
  49. if (repeat_size.y) {
  50. offset.y = Math::fposmod(offset.y, repeat_size.y);
  51. }
  52. scroll_offset = offset;
  53. _update_scroll();
  54. } break;
  55. case NOTIFICATION_EXIT_TREE: {
  56. remove_from_group(group_name);
  57. } break;
  58. }
  59. }
  60. #ifdef TOOLS_ENABLED
  61. void Parallax2D::_edit_set_position(const Point2 &p_position) {
  62. // Avoids early return for grid snap compatibility
  63. scroll_offset = p_position;
  64. _update_scroll();
  65. }
  66. #endif // TOOLS_ENABLED
  67. void Parallax2D::_validate_property(PropertyInfo &p_property) const {
  68. if (p_property.name == "position") {
  69. p_property.usage = PROPERTY_USAGE_NONE;
  70. }
  71. }
  72. void Parallax2D::_camera_moved(const Transform2D &p_transform, const Point2 &p_screen_offset, const Point2 &p_adj_screen_pos) {
  73. if (!ignore_camera_scroll) {
  74. if (get_viewport() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) {
  75. Size2 vps = get_viewport_rect().size;
  76. Vector2 offset;
  77. offset.x = ((int)vps.width % 2) ? 0.0 : 0.5;
  78. offset.y = ((int)vps.height % 2) ? 0.0 : 0.5;
  79. set_screen_offset((p_adj_screen_pos + offset).floor());
  80. } else {
  81. set_screen_offset(p_adj_screen_pos);
  82. }
  83. }
  84. }
  85. void Parallax2D::_update_process() {
  86. set_process_internal(!Engine::get_singleton()->is_editor_hint() && (repeat_size.x || repeat_size.y) && (autoscroll.x || autoscroll.y));
  87. }
  88. void Parallax2D::_update_scroll() {
  89. if (!is_inside_tree()) {
  90. return;
  91. }
  92. Point2 scroll_ofs = screen_offset;
  93. if (!Engine::get_singleton()->is_editor_hint()) {
  94. Size2 vps = get_viewport_rect().size;
  95. if (limit_begin.x <= limit_end.x - vps.x) {
  96. scroll_ofs.x = CLAMP(scroll_ofs.x, limit_begin.x, limit_end.x - vps.x);
  97. }
  98. if (limit_begin.y <= limit_end.y - vps.y) {
  99. scroll_ofs.y = CLAMP(scroll_ofs.y, limit_begin.y, limit_end.y - vps.y);
  100. }
  101. }
  102. scroll_ofs *= scroll_scale;
  103. if (repeat_size.x) {
  104. real_t mod = Math::fposmod(scroll_ofs.x - scroll_offset.x, repeat_size.x * get_scale().x);
  105. scroll_ofs.x = screen_offset.x - mod;
  106. } else {
  107. scroll_ofs.x = screen_offset.x + scroll_offset.x - scroll_ofs.x;
  108. }
  109. if (repeat_size.y) {
  110. real_t mod = Math::fposmod(scroll_ofs.y - scroll_offset.y, repeat_size.y * get_scale().y);
  111. scroll_ofs.y = screen_offset.y - mod;
  112. } else {
  113. scroll_ofs.y = screen_offset.y + scroll_offset.y - scroll_ofs.y;
  114. }
  115. if (!follow_viewport) {
  116. scroll_ofs -= screen_offset;
  117. }
  118. set_position(scroll_ofs);
  119. }
  120. void Parallax2D::_update_repeat() {
  121. if (!is_inside_tree()) {
  122. return;
  123. }
  124. RenderingServer::get_singleton()->canvas_set_item_repeat(get_canvas_item(), repeat_size, repeat_times);
  125. RenderingServer::get_singleton()->canvas_item_set_interpolated(get_canvas_item(), false);
  126. }
  127. void Parallax2D::set_scroll_scale(const Size2 &p_scale) {
  128. scroll_scale = p_scale;
  129. }
  130. Size2 Parallax2D::get_scroll_scale() const {
  131. return scroll_scale;
  132. }
  133. void Parallax2D::set_repeat_size(const Size2 &p_repeat_size) {
  134. if (p_repeat_size == repeat_size) {
  135. return;
  136. }
  137. repeat_size = p_repeat_size.maxf(0);
  138. _update_process();
  139. _update_repeat();
  140. _update_scroll();
  141. }
  142. Size2 Parallax2D::get_repeat_size() const {
  143. return repeat_size;
  144. }
  145. void Parallax2D::set_repeat_times(int p_repeat_times) {
  146. if (p_repeat_times == repeat_times) {
  147. return;
  148. }
  149. repeat_times = MAX(p_repeat_times, 1);
  150. _update_repeat();
  151. }
  152. int Parallax2D::get_repeat_times() const {
  153. return repeat_times;
  154. }
  155. void Parallax2D::set_scroll_offset(const Point2 &p_offset) {
  156. if (p_offset == scroll_offset) {
  157. return;
  158. }
  159. scroll_offset = p_offset;
  160. _update_scroll();
  161. }
  162. Point2 Parallax2D::get_scroll_offset() const {
  163. return scroll_offset;
  164. }
  165. void Parallax2D::set_autoscroll(const Point2 &p_autoscroll) {
  166. if (p_autoscroll == autoscroll) {
  167. return;
  168. }
  169. autoscroll = p_autoscroll;
  170. _update_process();
  171. _update_scroll();
  172. }
  173. Point2 Parallax2D::get_autoscroll() const {
  174. return autoscroll;
  175. }
  176. void Parallax2D::set_screen_offset(const Point2 &p_offset) {
  177. if (p_offset == screen_offset) {
  178. return;
  179. }
  180. screen_offset = p_offset;
  181. _update_scroll();
  182. }
  183. Point2 Parallax2D::get_screen_offset() const {
  184. return screen_offset;
  185. }
  186. void Parallax2D::set_limit_begin(const Point2 &p_offset) {
  187. limit_begin = p_offset;
  188. }
  189. Point2 Parallax2D::get_limit_begin() const {
  190. return limit_begin;
  191. }
  192. void Parallax2D::set_limit_end(const Point2 &p_offset) {
  193. limit_end = p_offset;
  194. }
  195. Point2 Parallax2D::get_limit_end() const {
  196. return limit_end;
  197. }
  198. void Parallax2D::set_follow_viewport(bool p_follow) {
  199. follow_viewport = p_follow;
  200. }
  201. bool Parallax2D::get_follow_viewport() {
  202. return follow_viewport;
  203. }
  204. void Parallax2D::set_ignore_camera_scroll(bool p_ignore) {
  205. ignore_camera_scroll = p_ignore;
  206. }
  207. bool Parallax2D::is_ignore_camera_scroll() {
  208. return ignore_camera_scroll;
  209. }
  210. void Parallax2D::_bind_methods() {
  211. ClassDB::bind_method(D_METHOD("_camera_moved", "transform", "screen_offset", "adj_screen_offset"), &Parallax2D::_camera_moved);
  212. ClassDB::bind_method(D_METHOD("set_scroll_scale", "scale"), &Parallax2D::set_scroll_scale);
  213. ClassDB::bind_method(D_METHOD("get_scroll_scale"), &Parallax2D::get_scroll_scale);
  214. ClassDB::bind_method(D_METHOD("set_repeat_size", "repeat_size"), &Parallax2D::set_repeat_size);
  215. ClassDB::bind_method(D_METHOD("get_repeat_size"), &Parallax2D::get_repeat_size);
  216. ClassDB::bind_method(D_METHOD("set_repeat_times", "repeat_times"), &Parallax2D::set_repeat_times);
  217. ClassDB::bind_method(D_METHOD("get_repeat_times"), &Parallax2D::get_repeat_times);
  218. ClassDB::bind_method(D_METHOD("set_autoscroll", "autoscroll"), &Parallax2D::set_autoscroll);
  219. ClassDB::bind_method(D_METHOD("get_autoscroll"), &Parallax2D::get_autoscroll);
  220. ClassDB::bind_method(D_METHOD("set_scroll_offset", "offset"), &Parallax2D::set_scroll_offset);
  221. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &Parallax2D::get_scroll_offset);
  222. ClassDB::bind_method(D_METHOD("set_screen_offset", "offset"), &Parallax2D::set_screen_offset);
  223. ClassDB::bind_method(D_METHOD("get_screen_offset"), &Parallax2D::get_screen_offset);
  224. ClassDB::bind_method(D_METHOD("set_limit_begin", "offset"), &Parallax2D::set_limit_begin);
  225. ClassDB::bind_method(D_METHOD("get_limit_begin"), &Parallax2D::get_limit_begin);
  226. ClassDB::bind_method(D_METHOD("set_limit_end", "offset"), &Parallax2D::set_limit_end);
  227. ClassDB::bind_method(D_METHOD("get_limit_end"), &Parallax2D::get_limit_end);
  228. ClassDB::bind_method(D_METHOD("set_follow_viewport", "follow"), &Parallax2D::set_follow_viewport);
  229. ClassDB::bind_method(D_METHOD("get_follow_viewport"), &Parallax2D::get_follow_viewport);
  230. ClassDB::bind_method(D_METHOD("set_ignore_camera_scroll", "ignore"), &Parallax2D::set_ignore_camera_scroll);
  231. ClassDB::bind_method(D_METHOD("is_ignore_camera_scroll"), &Parallax2D::is_ignore_camera_scroll);
  232. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_scale", PROPERTY_HINT_LINK), "set_scroll_scale", "get_scroll_scale");
  233. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_scroll_offset", "get_scroll_offset");
  234. ADD_GROUP("Repeat", "");
  235. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "repeat_size"), "set_repeat_size", "get_repeat_size");
  236. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "autoscroll", PROPERTY_HINT_NONE, "suffix:px/s"), "set_autoscroll", "get_autoscroll");
  237. ADD_PROPERTY(PropertyInfo(Variant::INT, "repeat_times"), "set_repeat_times", "get_repeat_times");
  238. ADD_GROUP("Limit", "limit_");
  239. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "limit_begin", PROPERTY_HINT_NONE, "suffix:px"), "set_limit_begin", "get_limit_begin");
  240. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "limit_end", PROPERTY_HINT_NONE, "suffix:px"), "set_limit_end", "get_limit_end");
  241. ADD_GROUP("Override", "");
  242. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "follow_viewport"), "set_follow_viewport", "get_follow_viewport");
  243. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_camera_scroll"), "set_ignore_camera_scroll", "is_ignore_camera_scroll");
  244. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "screen_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_screen_offset", "get_screen_offset");
  245. }
  246. Parallax2D::Parallax2D() {
  247. // Parallax2D is always updated every frame so there is no need to interpolate.
  248. set_physics_interpolation_mode(Node::PHYSICS_INTERPOLATION_MODE_OFF);
  249. }