path_2d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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_valid() || 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_valid()) {
  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 bones
  117. {
  118. PackedVector2Array v2p;
  119. v2p.resize(3);
  120. Vector2 *w = v2p.ptrw();
  121. for (int i = 0; i < sample_count; i++) {
  122. const Vector2 p = r[i].get_origin();
  123. const Vector2 side = r[i].columns[1];
  124. const Vector2 forward = r[i].columns[0];
  125. // Fish Bone.
  126. w[0] = p + (side - forward) * 5;
  127. w[1] = p;
  128. w[2] = p + (-side - forward) * 5;
  129. draw_polyline(v2p, get_tree()->get_debug_paths_color(), line_width * 0.5, false);
  130. }
  131. }
  132. }
  133. } break;
  134. }
  135. }
  136. void Path2D::_curve_changed() {
  137. if (!is_inside_tree()) {
  138. return;
  139. }
  140. for (int i = 0; i < get_child_count(); i++) {
  141. PathFollow2D *follow = Object::cast_to<PathFollow2D>(get_child(i));
  142. if (follow) {
  143. follow->path_changed();
  144. }
  145. }
  146. if (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_paths_hint()) {
  147. queue_redraw();
  148. }
  149. }
  150. void Path2D::set_curve(const Ref<Curve2D> &p_curve) {
  151. if (curve.is_valid()) {
  152. curve->disconnect_changed(callable_mp(this, &Path2D::_curve_changed));
  153. }
  154. curve = p_curve;
  155. if (curve.is_valid()) {
  156. curve->connect_changed(callable_mp(this, &Path2D::_curve_changed));
  157. }
  158. _curve_changed();
  159. }
  160. Ref<Curve2D> Path2D::get_curve() const {
  161. return curve;
  162. }
  163. void Path2D::_bind_methods() {
  164. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path2D::set_curve);
  165. ClassDB::bind_method(D_METHOD("get_curve"), &Path2D::get_curve);
  166. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_curve", "get_curve");
  167. }
  168. /////////////////////////////////////////////////////////////////////////////////
  169. void PathFollow2D::path_changed() {
  170. if (update_timer && !update_timer->is_stopped()) {
  171. update_timer->start();
  172. } else {
  173. _update_transform();
  174. }
  175. }
  176. void PathFollow2D::_update_transform() {
  177. if (!path) {
  178. return;
  179. }
  180. Ref<Curve2D> c = path->get_curve();
  181. if (!c.is_valid()) {
  182. return;
  183. }
  184. real_t path_length = c->get_baked_length();
  185. if (path_length == 0) {
  186. return;
  187. }
  188. if (rotates) {
  189. Transform2D xform = c->sample_baked_with_rotation(progress, cubic);
  190. xform.translate_local(h_offset, v_offset);
  191. set_rotation(xform[0].angle());
  192. set_position(xform[2]);
  193. } else {
  194. Vector2 pos = c->sample_baked(progress, cubic);
  195. pos.x += h_offset;
  196. pos.y += v_offset;
  197. set_position(pos);
  198. }
  199. }
  200. void PathFollow2D::_notification(int p_what) {
  201. switch (p_what) {
  202. case NOTIFICATION_READY: {
  203. if (Engine::get_singleton()->is_editor_hint()) {
  204. update_timer = memnew(Timer);
  205. update_timer->set_wait_time(0.2);
  206. update_timer->set_one_shot(true);
  207. update_timer->connect("timeout", callable_mp(this, &PathFollow2D::_update_transform));
  208. add_child(update_timer, false, Node::INTERNAL_MODE_BACK);
  209. }
  210. } break;
  211. case NOTIFICATION_ENTER_TREE: {
  212. path = Object::cast_to<Path2D>(get_parent());
  213. if (path) {
  214. _update_transform();
  215. }
  216. } break;
  217. case NOTIFICATION_EXIT_TREE: {
  218. path = nullptr;
  219. } break;
  220. }
  221. }
  222. void PathFollow2D::set_cubic_interpolation_enabled(bool p_enabled) {
  223. cubic = p_enabled;
  224. }
  225. bool PathFollow2D::is_cubic_interpolation_enabled() const {
  226. return cubic;
  227. }
  228. void PathFollow2D::_validate_property(PropertyInfo &p_property) const {
  229. if (p_property.name == "offset") {
  230. real_t max = 10000.0;
  231. if (path && path->get_curve().is_valid()) {
  232. max = path->get_curve()->get_baked_length();
  233. }
  234. p_property.hint_string = "0," + rtos(max) + ",0.01,or_less,or_greater";
  235. }
  236. }
  237. PackedStringArray PathFollow2D::get_configuration_warnings() const {
  238. PackedStringArray warnings = Node2D::get_configuration_warnings();
  239. if (is_visible_in_tree() && is_inside_tree()) {
  240. if (!Object::cast_to<Path2D>(get_parent())) {
  241. warnings.push_back(RTR("PathFollow2D only works when set as a child of a Path2D node."));
  242. }
  243. }
  244. return warnings;
  245. }
  246. void PathFollow2D::_bind_methods() {
  247. ClassDB::bind_method(D_METHOD("set_progress", "progress"), &PathFollow2D::set_progress);
  248. ClassDB::bind_method(D_METHOD("get_progress"), &PathFollow2D::get_progress);
  249. ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow2D::set_h_offset);
  250. ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow2D::get_h_offset);
  251. ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow2D::set_v_offset);
  252. ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow2D::get_v_offset);
  253. ClassDB::bind_method(D_METHOD("set_progress_ratio", "ratio"), &PathFollow2D::set_progress_ratio);
  254. ClassDB::bind_method(D_METHOD("get_progress_ratio"), &PathFollow2D::get_progress_ratio);
  255. ClassDB::bind_method(D_METHOD("set_rotates", "enabled"), &PathFollow2D::set_rotation_enabled);
  256. ClassDB::bind_method(D_METHOD("is_rotating"), &PathFollow2D::is_rotation_enabled);
  257. ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enabled"), &PathFollow2D::set_cubic_interpolation_enabled);
  258. ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow2D::is_cubic_interpolation_enabled);
  259. ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow2D::set_loop);
  260. ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow2D::has_loop);
  261. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress", PROPERTY_HINT_RANGE, "0,10000,0.01,or_less,or_greater,suffix:px"), "set_progress", "get_progress");
  262. 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");
  263. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset"), "set_h_offset", "get_h_offset");
  264. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset"), "set_v_offset", "get_v_offset");
  265. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotates"), "set_rotates", "is_rotating");
  266. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp"), "set_cubic_interpolation", "get_cubic_interpolation");
  267. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  268. }
  269. void PathFollow2D::set_progress(real_t p_progress) {
  270. ERR_FAIL_COND(!isfinite(p_progress));
  271. progress = p_progress;
  272. if (path) {
  273. if (path->get_curve().is_valid()) {
  274. real_t path_length = path->get_curve()->get_baked_length();
  275. if (loop && path_length) {
  276. progress = Math::fposmod(progress, path_length);
  277. if (!Math::is_zero_approx(p_progress) && Math::is_zero_approx(progress)) {
  278. progress = path_length;
  279. }
  280. } else {
  281. progress = CLAMP(progress, 0, path_length);
  282. }
  283. }
  284. _update_transform();
  285. }
  286. }
  287. void PathFollow2D::set_h_offset(real_t p_h_offset) {
  288. h_offset = p_h_offset;
  289. if (path) {
  290. _update_transform();
  291. }
  292. }
  293. real_t PathFollow2D::get_h_offset() const {
  294. return h_offset;
  295. }
  296. void PathFollow2D::set_v_offset(real_t p_v_offset) {
  297. v_offset = p_v_offset;
  298. if (path) {
  299. _update_transform();
  300. }
  301. }
  302. real_t PathFollow2D::get_v_offset() const {
  303. return v_offset;
  304. }
  305. real_t PathFollow2D::get_progress() const {
  306. return progress;
  307. }
  308. void PathFollow2D::set_progress_ratio(real_t p_ratio) {
  309. 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.");
  310. ERR_FAIL_COND_MSG(path->get_curve().is_null(), "Can't set progress ratio on a PathFollow2D that does not have a Curve.");
  311. ERR_FAIL_COND_MSG(!path->get_curve()->get_baked_length(), "Can't set progress ratio on a PathFollow2D that has a 0 length curve.");
  312. set_progress(p_ratio * path->get_curve()->get_baked_length());
  313. }
  314. real_t PathFollow2D::get_progress_ratio() const {
  315. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
  316. return get_progress() / path->get_curve()->get_baked_length();
  317. } else {
  318. return 0;
  319. }
  320. }
  321. void PathFollow2D::set_rotation_enabled(bool p_enabled) {
  322. rotates = p_enabled;
  323. _update_transform();
  324. }
  325. bool PathFollow2D::is_rotation_enabled() const {
  326. return rotates;
  327. }
  328. void PathFollow2D::set_loop(bool p_loop) {
  329. loop = p_loop;
  330. }
  331. bool PathFollow2D::has_loop() const {
  332. return loop;
  333. }