parallax_2d.cpp 11 KB

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