path_2d.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /**************************************************************************/
  2. /* path_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 "path_2d.h"
  31. #include "core/math/geometry_2d.h"
  32. #include "scene/main/timer.h"
  33. #ifdef TOOLS_ENABLED
  34. #include "editor/themes/editor_scale.h"
  35. #endif
  36. #ifdef DEBUG_ENABLED
  37. Rect2 Path2D::_edit_get_rect() const {
  38. if (curve.is_null() || curve->get_point_count() == 0) {
  39. return Rect2(0, 0, 0, 0);
  40. }
  41. Rect2 aabb = Rect2(curve->get_point_position(0), Vector2(0, 0));
  42. for (int i = 0; i < curve->get_point_count(); i++) {
  43. for (int j = 0; j <= 8; j++) {
  44. real_t frac = j / 8.0;
  45. Vector2 p = curve->sample(i, frac);
  46. aabb.expand_to(p);
  47. }
  48. }
  49. return aabb;
  50. }
  51. bool Path2D::_edit_use_rect() const {
  52. return curve.is_valid() && curve->get_point_count() != 0;
  53. }
  54. bool Path2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  55. if (curve.is_null()) {
  56. return false;
  57. }
  58. for (int i = 0; i < curve->get_point_count(); i++) {
  59. Vector2 s[2];
  60. s[0] = curve->get_point_position(i);
  61. for (int j = 1; j <= 8; j++) {
  62. real_t frac = j / 8.0;
  63. s[1] = curve->sample(i, frac);
  64. Vector2 p = Geometry2D::get_closest_point_to_segment(p_point, s);
  65. if (p.distance_to(p_point) <= p_tolerance) {
  66. return true;
  67. }
  68. s[0] = s[1];
  69. }
  70. }
  71. return false;
  72. }
  73. #endif
  74. void Path2D::_notification(int p_what) {
  75. switch (p_what) {
  76. // Draw the curve if path debugging is enabled.
  77. case NOTIFICATION_DRAW: {
  78. if (curve.is_null()) {
  79. break;
  80. }
  81. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_paths_hint()) {
  82. return;
  83. }
  84. if (curve->get_point_count() < 2) {
  85. return;
  86. }
  87. #ifdef TOOLS_ENABLED
  88. const real_t line_width = get_tree()->get_debug_paths_width() * EDSCALE;
  89. #else
  90. const real_t line_width = get_tree()->get_debug_paths_width();
  91. #endif
  92. real_t interval = 10;
  93. const real_t length = curve->get_baked_length();
  94. if (length > CMP_EPSILON) {
  95. const int sample_count = int(length / interval) + 2;
  96. interval = length / (sample_count - 1); // Recalculate real interval length.
  97. Vector<Transform2D> frames;
  98. frames.resize(sample_count);
  99. {
  100. Transform2D *w = frames.ptrw();
  101. for (int i = 0; i < sample_count; i++) {
  102. w[i] = curve->sample_baked_with_rotation(i * interval, false);
  103. }
  104. }
  105. const Transform2D *r = frames.ptr();
  106. // Draw curve segments
  107. {
  108. PackedVector2Array v2p;
  109. v2p.resize(sample_count);
  110. Vector2 *w = v2p.ptrw();
  111. for (int i = 0; i < sample_count; i++) {
  112. w[i] = r[i].get_origin();
  113. }
  114. draw_polyline(v2p, get_tree()->get_debug_paths_color(), line_width, false);
  115. }
  116. // Draw fish bone every 4 points to reduce visual noise and performance impact
  117. // (compared to drawing it for every point).
  118. {
  119. PackedVector2Array v2p;
  120. v2p.resize(3);
  121. Vector2 *w = v2p.ptrw();
  122. for (int i = 0; i < sample_count; i += 4) {
  123. const Vector2 p = r[i].get_origin();
  124. const Vector2 side = r[i].columns[1];
  125. const Vector2 forward = r[i].columns[0];
  126. // Fish Bone.
  127. w[0] = p + (side - forward) * 5;
  128. w[1] = p;
  129. w[2] = p + (-side - forward) * 5;
  130. draw_polyline(v2p, get_tree()->get_debug_paths_color(), line_width * 0.5, false);
  131. }
  132. }
  133. }
  134. } break;
  135. }
  136. }
  137. void Path2D::_curve_changed() {
  138. if (!is_inside_tree()) {
  139. return;
  140. }
  141. for (int i = 0; i < get_child_count(); i++) {
  142. PathFollow2D *follow = Object::cast_to<PathFollow2D>(get_child(i));
  143. if (follow) {
  144. follow->path_changed();
  145. }
  146. }
  147. if (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_paths_hint()) {
  148. queue_redraw();
  149. }
  150. }
  151. void Path2D::set_curve(const Ref<Curve2D> &p_curve) {
  152. if (curve.is_valid()) {
  153. curve->disconnect_changed(callable_mp(this, &Path2D::_curve_changed));
  154. }
  155. curve = p_curve;
  156. if (curve.is_valid()) {
  157. curve->connect_changed(callable_mp(this, &Path2D::_curve_changed));
  158. }
  159. _curve_changed();
  160. }
  161. Ref<Curve2D> Path2D::get_curve() const {
  162. return curve;
  163. }
  164. void Path2D::_bind_methods() {
  165. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path2D::set_curve);
  166. ClassDB::bind_method(D_METHOD("get_curve"), &Path2D::get_curve);
  167. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_curve", "get_curve");
  168. }
  169. /////////////////////////////////////////////////////////////////////////////////
  170. void PathFollow2D::path_changed() {
  171. if (update_timer && !update_timer->is_stopped()) {
  172. update_timer->start();
  173. } else {
  174. _update_transform();
  175. }
  176. }
  177. void PathFollow2D::_update_transform() {
  178. if (!path) {
  179. return;
  180. }
  181. Ref<Curve2D> c = path->get_curve();
  182. if (c.is_null()) {
  183. return;
  184. }
  185. real_t path_length = c->get_baked_length();
  186. if (path_length == 0) {
  187. return;
  188. }
  189. if (rotates) {
  190. Transform2D xform = c->sample_baked_with_rotation(progress, cubic);
  191. xform.translate_local(h_offset, v_offset);
  192. set_rotation(xform[0].angle());
  193. set_position(xform[2]);
  194. } else {
  195. Vector2 pos = c->sample_baked(progress, cubic);
  196. pos.x += h_offset;
  197. pos.y += v_offset;
  198. set_position(pos);
  199. }
  200. }
  201. void PathFollow2D::_notification(int p_what) {
  202. switch (p_what) {
  203. case NOTIFICATION_READY: {
  204. if (Engine::get_singleton()->is_editor_hint()) {
  205. update_timer = memnew(Timer);
  206. update_timer->set_wait_time(0.2);
  207. update_timer->set_one_shot(true);
  208. update_timer->connect("timeout", callable_mp(this, &PathFollow2D::_update_transform));
  209. add_child(update_timer, false, Node::INTERNAL_MODE_BACK);
  210. }
  211. } break;
  212. case NOTIFICATION_ENTER_TREE: {
  213. path = Object::cast_to<Path2D>(get_parent());
  214. if (path) {
  215. _update_transform();
  216. }
  217. } break;
  218. case NOTIFICATION_EXIT_TREE: {
  219. path = nullptr;
  220. } break;
  221. }
  222. }
  223. void PathFollow2D::set_cubic_interpolation_enabled(bool p_enabled) {
  224. cubic = p_enabled;
  225. }
  226. bool PathFollow2D::is_cubic_interpolation_enabled() const {
  227. return cubic;
  228. }
  229. void PathFollow2D::_validate_property(PropertyInfo &p_property) const {
  230. if (p_property.name == "offset") {
  231. real_t max = 10000.0;
  232. if (path && path->get_curve().is_valid()) {
  233. max = path->get_curve()->get_baked_length();
  234. }
  235. p_property.hint_string = "0," + rtos(max) + ",0.01,or_less,or_greater";
  236. }
  237. }
  238. PackedStringArray PathFollow2D::get_configuration_warnings() const {
  239. PackedStringArray warnings = Node2D::get_configuration_warnings();
  240. if (is_visible_in_tree() && is_inside_tree()) {
  241. if (!Object::cast_to<Path2D>(get_parent())) {
  242. warnings.push_back(RTR("PathFollow2D only works when set as a child of a Path2D node."));
  243. }
  244. }
  245. return warnings;
  246. }
  247. void PathFollow2D::_bind_methods() {
  248. ClassDB::bind_method(D_METHOD("set_progress", "progress"), &PathFollow2D::set_progress);
  249. ClassDB::bind_method(D_METHOD("get_progress"), &PathFollow2D::get_progress);
  250. ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow2D::set_h_offset);
  251. ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow2D::get_h_offset);
  252. ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow2D::set_v_offset);
  253. ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow2D::get_v_offset);
  254. ClassDB::bind_method(D_METHOD("set_progress_ratio", "ratio"), &PathFollow2D::set_progress_ratio);
  255. ClassDB::bind_method(D_METHOD("get_progress_ratio"), &PathFollow2D::get_progress_ratio);
  256. ClassDB::bind_method(D_METHOD("set_rotates", "enabled"), &PathFollow2D::set_rotation_enabled);
  257. ClassDB::bind_method(D_METHOD("is_rotating"), &PathFollow2D::is_rotation_enabled);
  258. ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enabled"), &PathFollow2D::set_cubic_interpolation_enabled);
  259. ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow2D::is_cubic_interpolation_enabled);
  260. ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow2D::set_loop);
  261. ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow2D::has_loop);
  262. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress", PROPERTY_HINT_RANGE, "0,10000,0.01,or_less,or_greater,suffix:px"), "set_progress", "get_progress");
  263. 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");
  264. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset"), "set_h_offset", "get_h_offset");
  265. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset"), "set_v_offset", "get_v_offset");
  266. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotates"), "set_rotates", "is_rotating");
  267. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp"), "set_cubic_interpolation", "get_cubic_interpolation");
  268. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  269. }
  270. void PathFollow2D::set_progress(real_t p_progress) {
  271. ERR_FAIL_COND(!isfinite(p_progress));
  272. progress = p_progress;
  273. if (path) {
  274. if (path->get_curve().is_valid()) {
  275. real_t path_length = path->get_curve()->get_baked_length();
  276. if (loop && path_length) {
  277. progress = Math::fposmod(progress, path_length);
  278. if (!Math::is_zero_approx(p_progress) && Math::is_zero_approx(progress)) {
  279. progress = path_length;
  280. }
  281. } else {
  282. progress = CLAMP(progress, 0, path_length);
  283. }
  284. }
  285. _update_transform();
  286. }
  287. }
  288. void PathFollow2D::set_h_offset(real_t p_h_offset) {
  289. h_offset = p_h_offset;
  290. if (path) {
  291. _update_transform();
  292. }
  293. }
  294. real_t PathFollow2D::get_h_offset() const {
  295. return h_offset;
  296. }
  297. void PathFollow2D::set_v_offset(real_t p_v_offset) {
  298. v_offset = p_v_offset;
  299. if (path) {
  300. _update_transform();
  301. }
  302. }
  303. real_t PathFollow2D::get_v_offset() const {
  304. return v_offset;
  305. }
  306. real_t PathFollow2D::get_progress() const {
  307. return progress;
  308. }
  309. void PathFollow2D::set_progress_ratio(real_t p_ratio) {
  310. ERR_FAIL_NULL_MSG(path, "Can only set progress ratio on a PathFollow2D that is the child of a Path2D which is itself part of the scene tree.");
  311. ERR_FAIL_COND_MSG(path->get_curve().is_null(), "Can't set progress ratio on a PathFollow2D that does not have a Curve.");
  312. ERR_FAIL_COND_MSG(!path->get_curve()->get_baked_length(), "Can't set progress ratio on a PathFollow2D that has a 0 length curve.");
  313. set_progress(p_ratio * path->get_curve()->get_baked_length());
  314. }
  315. real_t PathFollow2D::get_progress_ratio() const {
  316. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
  317. return get_progress() / path->get_curve()->get_baked_length();
  318. } else {
  319. return 0;
  320. }
  321. }
  322. void PathFollow2D::set_rotation_enabled(bool p_enabled) {
  323. rotates = p_enabled;
  324. _update_transform();
  325. }
  326. bool PathFollow2D::is_rotation_enabled() const {
  327. return rotates;
  328. }
  329. void PathFollow2D::set_loop(bool p_loop) {
  330. loop = p_loop;
  331. }
  332. bool PathFollow2D::has_loop() const {
  333. return loop;
  334. }