path_2d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*************************************************************************/
  2. /* path_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "path_2d.h"
  31. #include "core/math/geometry_2d.h"
  32. #ifdef TOOLS_ENABLED
  33. #include "editor/editor_scale.h"
  34. #endif
  35. #ifdef TOOLS_ENABLED
  36. Rect2 Path2D::_edit_get_rect() const {
  37. if (!curve.is_valid() || curve->get_point_count() == 0) {
  38. return Rect2(0, 0, 0, 0);
  39. }
  40. Rect2 aabb = Rect2(curve->get_point_position(0), Vector2(0, 0));
  41. for (int i = 0; i < curve->get_point_count(); i++) {
  42. for (int j = 0; j <= 8; j++) {
  43. real_t frac = j / 8.0;
  44. Vector2 p = curve->sample(i, frac);
  45. aabb.expand_to(p);
  46. }
  47. }
  48. return aabb;
  49. }
  50. bool Path2D::_edit_use_rect() const {
  51. return curve.is_valid() && curve->get_point_count() != 0;
  52. }
  53. bool Path2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  54. if (curve.is_null()) {
  55. return false;
  56. }
  57. for (int i = 0; i < curve->get_point_count(); i++) {
  58. Vector2 s[2];
  59. s[0] = curve->get_point_position(i);
  60. for (int j = 1; j <= 8; j++) {
  61. real_t frac = j / 8.0;
  62. s[1] = curve->sample(i, frac);
  63. Vector2 p = Geometry2D::get_closest_point_to_segment(p_point, s);
  64. if (p.distance_to(p_point) <= p_tolerance) {
  65. return true;
  66. }
  67. s[0] = s[1];
  68. }
  69. }
  70. return false;
  71. }
  72. #endif
  73. void Path2D::_notification(int p_what) {
  74. switch (p_what) {
  75. // Draw the curve if path debugging is enabled.
  76. case NOTIFICATION_DRAW: {
  77. if (!curve.is_valid()) {
  78. break;
  79. }
  80. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_paths_hint()) {
  81. return;
  82. }
  83. if (curve->get_point_count() < 2) {
  84. return;
  85. }
  86. #ifdef TOOLS_ENABLED
  87. const real_t line_width = get_tree()->get_debug_paths_width() * EDSCALE;
  88. #else
  89. const real_t line_width = get_tree()->get_debug_paths_width();
  90. #endif
  91. _cached_draw_pts.resize(curve->get_point_count() * 8);
  92. int count = 0;
  93. for (int i = 0; i < curve->get_point_count(); i++) {
  94. for (int j = 0; j < 8; j++) {
  95. real_t frac = j * (1.0 / 8.0);
  96. Vector2 p = curve->sample(i, frac);
  97. _cached_draw_pts.set(count++, p);
  98. }
  99. }
  100. draw_polyline(_cached_draw_pts, get_tree()->get_debug_paths_color(), line_width, true);
  101. } break;
  102. }
  103. }
  104. void Path2D::_curve_changed() {
  105. if (!is_inside_tree()) {
  106. return;
  107. }
  108. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_paths_hint()) {
  109. return;
  110. }
  111. queue_redraw();
  112. }
  113. void Path2D::set_curve(const Ref<Curve2D> &p_curve) {
  114. if (curve.is_valid()) {
  115. curve->disconnect("changed", callable_mp(this, &Path2D::_curve_changed));
  116. }
  117. curve = p_curve;
  118. if (curve.is_valid()) {
  119. curve->connect("changed", callable_mp(this, &Path2D::_curve_changed));
  120. }
  121. _curve_changed();
  122. }
  123. Ref<Curve2D> Path2D::get_curve() const {
  124. return curve;
  125. }
  126. void Path2D::_bind_methods() {
  127. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path2D::set_curve);
  128. ClassDB::bind_method(D_METHOD("get_curve"), &Path2D::get_curve);
  129. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_curve", "get_curve");
  130. }
  131. /////////////////////////////////////////////////////////////////////////////////
  132. void PathFollow2D::_update_transform() {
  133. if (!path) {
  134. return;
  135. }
  136. Ref<Curve2D> c = path->get_curve();
  137. if (!c.is_valid()) {
  138. return;
  139. }
  140. real_t path_length = c->get_baked_length();
  141. if (path_length == 0) {
  142. return;
  143. }
  144. Vector2 pos = c->sample_baked(progress, cubic);
  145. if (rotates) {
  146. real_t ahead = progress + lookahead;
  147. if (loop && ahead >= path_length) {
  148. // If our lookahead will loop, we need to check if the path is closed.
  149. int point_count = c->get_point_count();
  150. if (point_count > 0) {
  151. Vector2 start_point = c->get_point_position(0);
  152. Vector2 end_point = c->get_point_position(point_count - 1);
  153. if (start_point == end_point) {
  154. // Since the path is closed we want to 'smooth off'
  155. // the corner at the start/end.
  156. // So we wrap the lookahead back round.
  157. ahead = Math::fmod(ahead, path_length);
  158. }
  159. }
  160. }
  161. Vector2 ahead_pos = c->sample_baked(ahead, cubic);
  162. Vector2 tangent_to_curve;
  163. if (ahead_pos == pos) {
  164. // This will happen at the end of non-looping or non-closed paths.
  165. // We'll try a look behind instead, in order to get a meaningful angle.
  166. tangent_to_curve =
  167. (pos - c->sample_baked(progress - lookahead, cubic)).normalized();
  168. } else {
  169. tangent_to_curve = (ahead_pos - pos).normalized();
  170. }
  171. Vector2 normal_of_curve = -tangent_to_curve.orthogonal();
  172. pos += tangent_to_curve * h_offset;
  173. pos += normal_of_curve * v_offset;
  174. set_rotation(tangent_to_curve.angle());
  175. } else {
  176. pos.x += h_offset;
  177. pos.y += v_offset;
  178. }
  179. set_position(pos);
  180. }
  181. void PathFollow2D::_notification(int p_what) {
  182. switch (p_what) {
  183. case NOTIFICATION_ENTER_TREE: {
  184. path = Object::cast_to<Path2D>(get_parent());
  185. if (path) {
  186. _update_transform();
  187. }
  188. } break;
  189. case NOTIFICATION_EXIT_TREE: {
  190. path = nullptr;
  191. } break;
  192. }
  193. }
  194. void PathFollow2D::set_cubic_interpolation(bool p_enable) {
  195. cubic = p_enable;
  196. }
  197. bool PathFollow2D::get_cubic_interpolation() const {
  198. return cubic;
  199. }
  200. void PathFollow2D::_validate_property(PropertyInfo &p_property) const {
  201. if (p_property.name == "offset") {
  202. real_t max = 10000.0;
  203. if (path && path->get_curve().is_valid()) {
  204. max = path->get_curve()->get_baked_length();
  205. }
  206. p_property.hint_string = "0," + rtos(max) + ",0.01,or_lesser,or_greater";
  207. }
  208. }
  209. TypedArray<String> PathFollow2D::get_configuration_warnings() const {
  210. TypedArray<String> warnings = Node::get_configuration_warnings();
  211. if (is_visible_in_tree() && is_inside_tree()) {
  212. if (!Object::cast_to<Path2D>(get_parent())) {
  213. warnings.push_back(RTR("PathFollow2D only works when set as a child of a Path2D node."));
  214. }
  215. }
  216. return warnings;
  217. }
  218. void PathFollow2D::_bind_methods() {
  219. ClassDB::bind_method(D_METHOD("set_progress", "progress"), &PathFollow2D::set_progress);
  220. ClassDB::bind_method(D_METHOD("get_progress"), &PathFollow2D::get_progress);
  221. ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow2D::set_h_offset);
  222. ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow2D::get_h_offset);
  223. ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow2D::set_v_offset);
  224. ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow2D::get_v_offset);
  225. ClassDB::bind_method(D_METHOD("set_progress_ratio", "ratio"), &PathFollow2D::set_progress_ratio);
  226. ClassDB::bind_method(D_METHOD("get_progress_ratio"), &PathFollow2D::get_progress_ratio);
  227. ClassDB::bind_method(D_METHOD("set_rotates", "enable"), &PathFollow2D::set_rotates);
  228. ClassDB::bind_method(D_METHOD("is_rotating"), &PathFollow2D::is_rotating);
  229. ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enable"), &PathFollow2D::set_cubic_interpolation);
  230. ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow2D::get_cubic_interpolation);
  231. ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow2D::set_loop);
  232. ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow2D::has_loop);
  233. ClassDB::bind_method(D_METHOD("set_lookahead", "lookahead"), &PathFollow2D::set_lookahead);
  234. ClassDB::bind_method(D_METHOD("get_lookahead"), &PathFollow2D::get_lookahead);
  235. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress", PROPERTY_HINT_RANGE, "0,10000,0.01,or_lesser,or_greater,suffix:px"), "set_progress", "get_progress");
  236. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress_ratio", PROPERTY_HINT_RANGE, "0,1,0.0001,or_lesser,or_greater", PROPERTY_USAGE_EDITOR), "set_progress_ratio", "get_progress_ratio");
  237. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset"), "set_h_offset", "get_h_offset");
  238. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset"), "set_v_offset", "get_v_offset");
  239. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotates"), "set_rotates", "is_rotating");
  240. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp"), "set_cubic_interpolation", "get_cubic_interpolation");
  241. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  242. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lookahead", PROPERTY_HINT_RANGE, "0.001,1024.0,0.001"), "set_lookahead", "get_lookahead");
  243. }
  244. void PathFollow2D::set_progress(real_t p_progress) {
  245. ERR_FAIL_COND(!isfinite(p_progress));
  246. progress = p_progress;
  247. if (path) {
  248. if (path->get_curve().is_valid()) {
  249. real_t path_length = path->get_curve()->get_baked_length();
  250. if (loop && path_length) {
  251. progress = Math::fposmod(progress, path_length);
  252. if (!Math::is_zero_approx(p_progress) && Math::is_zero_approx(progress)) {
  253. progress = path_length;
  254. }
  255. } else {
  256. progress = CLAMP(progress, 0, path_length);
  257. }
  258. }
  259. _update_transform();
  260. }
  261. }
  262. void PathFollow2D::set_h_offset(real_t p_h_offset) {
  263. h_offset = p_h_offset;
  264. if (path) {
  265. _update_transform();
  266. }
  267. }
  268. real_t PathFollow2D::get_h_offset() const {
  269. return h_offset;
  270. }
  271. void PathFollow2D::set_v_offset(real_t p_v_offset) {
  272. v_offset = p_v_offset;
  273. if (path) {
  274. _update_transform();
  275. }
  276. }
  277. real_t PathFollow2D::get_v_offset() const {
  278. return v_offset;
  279. }
  280. real_t PathFollow2D::get_progress() const {
  281. return progress;
  282. }
  283. void PathFollow2D::set_progress_ratio(real_t p_ratio) {
  284. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
  285. set_progress(p_ratio * path->get_curve()->get_baked_length());
  286. }
  287. }
  288. real_t PathFollow2D::get_progress_ratio() const {
  289. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
  290. return get_progress() / path->get_curve()->get_baked_length();
  291. } else {
  292. return 0;
  293. }
  294. }
  295. void PathFollow2D::set_lookahead(real_t p_lookahead) {
  296. lookahead = p_lookahead;
  297. }
  298. real_t PathFollow2D::get_lookahead() const {
  299. return lookahead;
  300. }
  301. void PathFollow2D::set_rotates(bool p_rotates) {
  302. rotates = p_rotates;
  303. _update_transform();
  304. }
  305. bool PathFollow2D::is_rotating() const {
  306. return rotates;
  307. }
  308. void PathFollow2D::set_loop(bool p_loop) {
  309. loop = p_loop;
  310. }
  311. bool PathFollow2D::has_loop() const {
  312. return loop;
  313. }