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