parallax_2d.cpp 11 KB

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