path_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. if (rotates) {
  145. Transform2D xform = c->sample_baked_with_rotation(progress, cubic);
  146. xform.translate_local(v_offset, h_offset);
  147. set_rotation(xform[1].angle());
  148. set_position(xform[2]);
  149. } else {
  150. Vector2 pos = c->sample_baked(progress, cubic);
  151. pos.x += h_offset;
  152. pos.y += v_offset;
  153. set_position(pos);
  154. }
  155. }
  156. void PathFollow2D::_notification(int p_what) {
  157. switch (p_what) {
  158. case NOTIFICATION_ENTER_TREE: {
  159. path = Object::cast_to<Path2D>(get_parent());
  160. if (path) {
  161. _update_transform();
  162. }
  163. } break;
  164. case NOTIFICATION_EXIT_TREE: {
  165. path = nullptr;
  166. } break;
  167. }
  168. }
  169. void PathFollow2D::set_cubic_interpolation(bool p_enable) {
  170. cubic = p_enable;
  171. }
  172. bool PathFollow2D::get_cubic_interpolation() const {
  173. return cubic;
  174. }
  175. void PathFollow2D::_validate_property(PropertyInfo &p_property) const {
  176. if (p_property.name == "offset") {
  177. real_t max = 10000.0;
  178. if (path && path->get_curve().is_valid()) {
  179. max = path->get_curve()->get_baked_length();
  180. }
  181. p_property.hint_string = "0," + rtos(max) + ",0.01,or_less,or_greater";
  182. }
  183. }
  184. PackedStringArray PathFollow2D::get_configuration_warnings() const {
  185. PackedStringArray warnings = Node::get_configuration_warnings();
  186. if (is_visible_in_tree() && is_inside_tree()) {
  187. if (!Object::cast_to<Path2D>(get_parent())) {
  188. warnings.push_back(RTR("PathFollow2D only works when set as a child of a Path2D node."));
  189. }
  190. }
  191. return warnings;
  192. }
  193. void PathFollow2D::_bind_methods() {
  194. ClassDB::bind_method(D_METHOD("set_progress", "progress"), &PathFollow2D::set_progress);
  195. ClassDB::bind_method(D_METHOD("get_progress"), &PathFollow2D::get_progress);
  196. ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow2D::set_h_offset);
  197. ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow2D::get_h_offset);
  198. ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow2D::set_v_offset);
  199. ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow2D::get_v_offset);
  200. ClassDB::bind_method(D_METHOD("set_progress_ratio", "ratio"), &PathFollow2D::set_progress_ratio);
  201. ClassDB::bind_method(D_METHOD("get_progress_ratio"), &PathFollow2D::get_progress_ratio);
  202. ClassDB::bind_method(D_METHOD("set_rotates", "enable"), &PathFollow2D::set_rotates);
  203. ClassDB::bind_method(D_METHOD("is_rotating"), &PathFollow2D::is_rotating);
  204. ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enable"), &PathFollow2D::set_cubic_interpolation);
  205. ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow2D::get_cubic_interpolation);
  206. ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow2D::set_loop);
  207. ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow2D::has_loop);
  208. ClassDB::bind_method(D_METHOD("set_lookahead", "lookahead"), &PathFollow2D::set_lookahead);
  209. ClassDB::bind_method(D_METHOD("get_lookahead"), &PathFollow2D::get_lookahead);
  210. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress", PROPERTY_HINT_RANGE, "0,10000,0.01,or_less,or_greater,suffix:px"), "set_progress", "get_progress");
  211. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress_ratio", PROPERTY_HINT_RANGE, "0,1,0.0001,or_less,or_greater", PROPERTY_USAGE_EDITOR), "set_progress_ratio", "get_progress_ratio");
  212. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset"), "set_h_offset", "get_h_offset");
  213. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset"), "set_v_offset", "get_v_offset");
  214. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotates"), "set_rotates", "is_rotating");
  215. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp"), "set_cubic_interpolation", "get_cubic_interpolation");
  216. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  217. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lookahead", PROPERTY_HINT_RANGE, "0.001,1024.0,0.001"), "set_lookahead", "get_lookahead");
  218. }
  219. void PathFollow2D::set_progress(real_t p_progress) {
  220. ERR_FAIL_COND(!isfinite(p_progress));
  221. progress = p_progress;
  222. if (path) {
  223. if (path->get_curve().is_valid()) {
  224. real_t path_length = path->get_curve()->get_baked_length();
  225. if (loop && path_length) {
  226. progress = Math::fposmod(progress, path_length);
  227. if (!Math::is_zero_approx(p_progress) && Math::is_zero_approx(progress)) {
  228. progress = path_length;
  229. }
  230. } else {
  231. progress = CLAMP(progress, 0, path_length);
  232. }
  233. }
  234. _update_transform();
  235. }
  236. }
  237. void PathFollow2D::set_h_offset(real_t p_h_offset) {
  238. h_offset = p_h_offset;
  239. if (path) {
  240. _update_transform();
  241. }
  242. }
  243. real_t PathFollow2D::get_h_offset() const {
  244. return h_offset;
  245. }
  246. void PathFollow2D::set_v_offset(real_t p_v_offset) {
  247. v_offset = p_v_offset;
  248. if (path) {
  249. _update_transform();
  250. }
  251. }
  252. real_t PathFollow2D::get_v_offset() const {
  253. return v_offset;
  254. }
  255. real_t PathFollow2D::get_progress() const {
  256. return progress;
  257. }
  258. void PathFollow2D::set_progress_ratio(real_t p_ratio) {
  259. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
  260. set_progress(p_ratio * path->get_curve()->get_baked_length());
  261. }
  262. }
  263. real_t PathFollow2D::get_progress_ratio() const {
  264. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
  265. return get_progress() / path->get_curve()->get_baked_length();
  266. } else {
  267. return 0;
  268. }
  269. }
  270. void PathFollow2D::set_lookahead(real_t p_lookahead) {
  271. lookahead = p_lookahead;
  272. }
  273. real_t PathFollow2D::get_lookahead() const {
  274. return lookahead;
  275. }
  276. void PathFollow2D::set_rotates(bool p_rotates) {
  277. rotates = p_rotates;
  278. _update_transform();
  279. }
  280. bool PathFollow2D::is_rotating() const {
  281. return rotates;
  282. }
  283. void PathFollow2D::set_loop(bool p_loop) {
  284. loop = p_loop;
  285. }
  286. bool PathFollow2D::has_loop() const {
  287. return loop;
  288. }