path_2d.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 segment_a = curve->get_point_position(i);
  60. for (int j = 1; j <= 8; j++) {
  61. real_t frac = j / 8.0;
  62. const Vector2 segment_b = curve->sample(i, frac);
  63. Vector2 p = Geometry2D::get_closest_point_to_segment(p_point, segment_a, segment_b);
  64. if (p.distance_to(p_point) <= p_tolerance) {
  65. return true;
  66. }
  67. segment_a = segment_b;
  68. }
  69. }
  70. return false;
  71. }
  72. #endif
  73. void Path2D::_notification(int p_what) {
  74. switch (p_what) {
  75. case NOTIFICATION_ENTER_TREE: {
  76. #ifdef DEBUG_ENABLED
  77. _debug_create();
  78. #endif
  79. } break;
  80. case NOTIFICATION_EXIT_TREE: {
  81. #ifdef DEBUG_ENABLED
  82. _debug_free();
  83. #endif
  84. } break;
  85. // Draw the curve if path debugging is enabled.
  86. case NOTIFICATION_DRAW: {
  87. #ifdef DEBUG_ENABLED
  88. _debug_update();
  89. #endif
  90. } break;
  91. }
  92. }
  93. #ifdef DEBUG_ENABLED
  94. void Path2D::_debug_create() {
  95. ERR_FAIL_NULL(RS::get_singleton());
  96. if (debug_mesh_rid.is_null()) {
  97. debug_mesh_rid = RS::get_singleton()->mesh_create();
  98. }
  99. if (debug_instance.is_null()) {
  100. debug_instance = RS::get_singleton()->instance_create();
  101. }
  102. RS::get_singleton()->instance_set_base(debug_instance, debug_mesh_rid);
  103. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(debug_instance, RS::SHADOW_CASTING_SETTING_OFF);
  104. }
  105. void Path2D::_debug_free() {
  106. ERR_FAIL_NULL(RS::get_singleton());
  107. if (debug_instance.is_valid()) {
  108. RS::get_singleton()->free(debug_instance);
  109. debug_instance = RID();
  110. }
  111. if (debug_mesh_rid.is_valid()) {
  112. RS::get_singleton()->free(debug_mesh_rid);
  113. debug_mesh_rid = RID();
  114. }
  115. }
  116. void Path2D::_debug_update() {
  117. ERR_FAIL_NULL(RS::get_singleton());
  118. RenderingServer *rs = RS::get_singleton();
  119. ERR_FAIL_NULL(SceneTree::get_singleton());
  120. ERR_FAIL_NULL(RenderingServer::get_singleton());
  121. const bool path_debug_enabled = (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_paths_hint());
  122. if (!path_debug_enabled) {
  123. _debug_free();
  124. return;
  125. }
  126. if (debug_mesh_rid.is_null() || debug_instance.is_null()) {
  127. _debug_create();
  128. }
  129. rs->mesh_clear(debug_mesh_rid);
  130. if (curve.is_null()) {
  131. return;
  132. }
  133. if (curve->get_point_count() < 2) {
  134. return;
  135. }
  136. const real_t baked_length = curve->get_baked_length();
  137. if (baked_length <= CMP_EPSILON) {
  138. return;
  139. }
  140. const Color debug_color = get_tree()->get_debug_paths_color();
  141. bool debug_paths_show_fish_bones = true;
  142. real_t sample_interval = 10.0;
  143. const int sample_count = int(baked_length / sample_interval) + 2;
  144. sample_interval = baked_length / (sample_count - 1); // Recalculate real interval length.
  145. Vector<Transform2D> samples;
  146. samples.resize(sample_count);
  147. Transform2D *samples_ptrw = samples.ptrw();
  148. for (int i = 0; i < sample_count; i++) {
  149. samples_ptrw[i] = curve->sample_baked_with_rotation(i * sample_interval, false);
  150. }
  151. const Transform2D *samples_ptr = samples.ptr();
  152. // Draw curve segments
  153. {
  154. Vector<Vector2> ribbon;
  155. ribbon.resize(sample_count);
  156. Vector2 *ribbon_ptrw = ribbon.ptrw();
  157. for (int i = 0; i < sample_count; i++) {
  158. ribbon_ptrw[i] = samples_ptr[i].get_origin();
  159. }
  160. Array ribbon_array;
  161. ribbon_array.resize(Mesh::ARRAY_MAX);
  162. ribbon_array[Mesh::ARRAY_VERTEX] = ribbon;
  163. Vector<Color> ribbon_color;
  164. ribbon_color.resize(ribbon.size());
  165. ribbon_color.fill(debug_color);
  166. ribbon_array[Mesh::ARRAY_COLOR] = ribbon_color;
  167. rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_LINE_STRIP, ribbon_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  168. }
  169. // Render path fish bones.
  170. if (debug_paths_show_fish_bones) {
  171. int fish_bones_interval = 4;
  172. const int vertex_per_bone = 4;
  173. Vector<Vector2> bones;
  174. bones.resize(sample_count * vertex_per_bone);
  175. Vector2 *bones_ptrw = bones.ptrw();
  176. for (int i = 0; i < sample_count; i += fish_bones_interval) {
  177. const Transform2D &sample_transform = samples_ptr[i];
  178. const Vector2 point = sample_transform.get_origin();
  179. const Vector2 &side = sample_transform.columns[1];
  180. const Vector2 &forward = sample_transform.columns[0];
  181. const int bone_idx = i * vertex_per_bone;
  182. bones_ptrw[bone_idx] = point;
  183. bones_ptrw[bone_idx + 1] = point + (side - forward) * 5;
  184. bones_ptrw[bone_idx + 2] = point;
  185. bones_ptrw[bone_idx + 3] = point + (-side - forward) * 5;
  186. }
  187. Array bone_array;
  188. bone_array.resize(Mesh::ARRAY_MAX);
  189. bone_array[Mesh::ARRAY_VERTEX] = bones;
  190. Vector<Color> bones_color;
  191. bones_color.resize(bones.size());
  192. bones_color.fill(debug_color);
  193. bone_array[Mesh::ARRAY_COLOR] = bones_color;
  194. rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_LINES, bone_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  195. }
  196. rs->canvas_item_clear(get_canvas_item());
  197. rs->canvas_item_add_mesh(get_canvas_item(), debug_mesh_rid, Transform2D());
  198. }
  199. #endif // DEBUG_ENABLED
  200. void Path2D::_curve_changed() {
  201. if (!is_inside_tree()) {
  202. return;
  203. }
  204. for (int i = 0; i < get_child_count(); i++) {
  205. PathFollow2D *follow = Object::cast_to<PathFollow2D>(get_child(i));
  206. if (follow) {
  207. follow->path_changed();
  208. }
  209. }
  210. if (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_paths_hint()) {
  211. queue_redraw();
  212. }
  213. }
  214. void Path2D::set_curve(const Ref<Curve2D> &p_curve) {
  215. if (curve.is_valid()) {
  216. curve->disconnect_changed(callable_mp(this, &Path2D::_curve_changed));
  217. }
  218. curve = p_curve;
  219. if (curve.is_valid()) {
  220. curve->connect_changed(callable_mp(this, &Path2D::_curve_changed));
  221. }
  222. _curve_changed();
  223. }
  224. Ref<Curve2D> Path2D::get_curve() const {
  225. return curve;
  226. }
  227. void Path2D::_bind_methods() {
  228. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path2D::set_curve);
  229. ClassDB::bind_method(D_METHOD("get_curve"), &Path2D::get_curve);
  230. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_curve", "get_curve");
  231. }
  232. /////////////////////////////////////////////////////////////////////////////////
  233. void PathFollow2D::path_changed() {
  234. if (update_timer && !update_timer->is_stopped()) {
  235. update_timer->start();
  236. } else {
  237. _update_transform();
  238. }
  239. }
  240. void PathFollow2D::_update_transform() {
  241. if (!path) {
  242. return;
  243. }
  244. Ref<Curve2D> c = path->get_curve();
  245. if (c.is_null()) {
  246. return;
  247. }
  248. real_t path_length = c->get_baked_length();
  249. if (path_length == 0) {
  250. return;
  251. }
  252. if (rotates) {
  253. Transform2D xform = c->sample_baked_with_rotation(progress, cubic);
  254. xform.translate_local(h_offset, v_offset);
  255. set_rotation(xform[0].angle());
  256. set_position(xform[2]);
  257. } else {
  258. Vector2 pos = c->sample_baked(progress, cubic);
  259. pos.x += h_offset;
  260. pos.y += v_offset;
  261. set_position(pos);
  262. }
  263. }
  264. void PathFollow2D::_notification(int p_what) {
  265. switch (p_what) {
  266. case NOTIFICATION_READY: {
  267. if (Engine::get_singleton()->is_editor_hint()) {
  268. update_timer = memnew(Timer);
  269. update_timer->set_wait_time(0.2);
  270. update_timer->set_one_shot(true);
  271. update_timer->connect("timeout", callable_mp(this, &PathFollow2D::_update_transform));
  272. add_child(update_timer, false, Node::INTERNAL_MODE_BACK);
  273. }
  274. } break;
  275. case NOTIFICATION_ENTER_TREE: {
  276. path = Object::cast_to<Path2D>(get_parent());
  277. if (path) {
  278. _update_transform();
  279. }
  280. } break;
  281. case NOTIFICATION_EXIT_TREE: {
  282. path = nullptr;
  283. } break;
  284. }
  285. }
  286. void PathFollow2D::set_cubic_interpolation_enabled(bool p_enabled) {
  287. cubic = p_enabled;
  288. }
  289. bool PathFollow2D::is_cubic_interpolation_enabled() const {
  290. return cubic;
  291. }
  292. void PathFollow2D::_validate_property(PropertyInfo &p_property) const {
  293. if (!Engine::get_singleton()->is_editor_hint()) {
  294. return;
  295. }
  296. if (p_property.name == "offset") {
  297. real_t max = 10000.0;
  298. if (path && path->get_curve().is_valid()) {
  299. max = path->get_curve()->get_baked_length();
  300. }
  301. p_property.hint_string = "0," + rtos(max) + ",0.01,or_less,or_greater";
  302. }
  303. }
  304. PackedStringArray PathFollow2D::get_configuration_warnings() const {
  305. PackedStringArray warnings = Node2D::get_configuration_warnings();
  306. if (is_visible_in_tree() && is_inside_tree()) {
  307. if (!Object::cast_to<Path2D>(get_parent())) {
  308. warnings.push_back(RTR("PathFollow2D only works when set as a child of a Path2D node."));
  309. }
  310. }
  311. return warnings;
  312. }
  313. void PathFollow2D::_bind_methods() {
  314. ClassDB::bind_method(D_METHOD("set_progress", "progress"), &PathFollow2D::set_progress);
  315. ClassDB::bind_method(D_METHOD("get_progress"), &PathFollow2D::get_progress);
  316. ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow2D::set_h_offset);
  317. ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow2D::get_h_offset);
  318. ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow2D::set_v_offset);
  319. ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow2D::get_v_offset);
  320. ClassDB::bind_method(D_METHOD("set_progress_ratio", "ratio"), &PathFollow2D::set_progress_ratio);
  321. ClassDB::bind_method(D_METHOD("get_progress_ratio"), &PathFollow2D::get_progress_ratio);
  322. ClassDB::bind_method(D_METHOD("set_rotates", "enabled"), &PathFollow2D::set_rotation_enabled);
  323. ClassDB::bind_method(D_METHOD("is_rotating"), &PathFollow2D::is_rotation_enabled);
  324. ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enabled"), &PathFollow2D::set_cubic_interpolation_enabled);
  325. ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow2D::is_cubic_interpolation_enabled);
  326. ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow2D::set_loop);
  327. ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow2D::has_loop);
  328. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress", PROPERTY_HINT_RANGE, "0,10000,0.01,or_less,or_greater,suffix:px"), "set_progress", "get_progress");
  329. 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");
  330. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset"), "set_h_offset", "get_h_offset");
  331. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset"), "set_v_offset", "get_v_offset");
  332. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotates"), "set_rotates", "is_rotating");
  333. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp"), "set_cubic_interpolation", "get_cubic_interpolation");
  334. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  335. }
  336. void PathFollow2D::set_progress(real_t p_progress) {
  337. ERR_FAIL_COND(!std::isfinite(p_progress));
  338. progress = p_progress;
  339. if (path) {
  340. if (path->get_curve().is_valid()) {
  341. real_t path_length = path->get_curve()->get_baked_length();
  342. if (loop && path_length) {
  343. progress = Math::fposmod(progress, path_length);
  344. if (!Math::is_zero_approx(p_progress) && Math::is_zero_approx(progress)) {
  345. progress = path_length;
  346. }
  347. } else {
  348. progress = CLAMP(progress, 0, path_length);
  349. }
  350. }
  351. _update_transform();
  352. }
  353. }
  354. void PathFollow2D::set_h_offset(real_t p_h_offset) {
  355. h_offset = p_h_offset;
  356. if (path) {
  357. _update_transform();
  358. }
  359. }
  360. real_t PathFollow2D::get_h_offset() const {
  361. return h_offset;
  362. }
  363. void PathFollow2D::set_v_offset(real_t p_v_offset) {
  364. v_offset = p_v_offset;
  365. if (path) {
  366. _update_transform();
  367. }
  368. }
  369. real_t PathFollow2D::get_v_offset() const {
  370. return v_offset;
  371. }
  372. real_t PathFollow2D::get_progress() const {
  373. return progress;
  374. }
  375. void PathFollow2D::set_progress_ratio(real_t p_ratio) {
  376. 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.");
  377. ERR_FAIL_COND_MSG(path->get_curve().is_null(), "Can't set progress ratio on a PathFollow2D that does not have a Curve.");
  378. ERR_FAIL_COND_MSG(!path->get_curve()->get_baked_length(), "Can't set progress ratio on a PathFollow2D that has a 0 length curve.");
  379. set_progress(p_ratio * path->get_curve()->get_baked_length());
  380. }
  381. real_t PathFollow2D::get_progress_ratio() const {
  382. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
  383. return get_progress() / path->get_curve()->get_baked_length();
  384. } else {
  385. return 0;
  386. }
  387. }
  388. void PathFollow2D::set_rotation_enabled(bool p_enabled) {
  389. rotates = p_enabled;
  390. _update_transform();
  391. }
  392. bool PathFollow2D::is_rotation_enabled() const {
  393. return rotates;
  394. }
  395. void PathFollow2D::set_loop(bool p_loop) {
  396. loop = p_loop;
  397. }
  398. bool PathFollow2D::has_loop() const {
  399. return loop;
  400. }