path_2d.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*************************************************************************/
  2. /* path_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "engine.h"
  32. #include "scene/scene_string_names.h"
  33. void Path2D::_notification(int p_what) {
  34. if (p_what == NOTIFICATION_DRAW && curve.is_valid()) {
  35. //draw the curve!!
  36. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_navigation_hint()) {
  37. return;
  38. }
  39. for (int i = 0; i < curve->get_point_count(); i++) {
  40. Vector2 prev_p = curve->get_point_position(i);
  41. for (int j = 1; j <= 8; j++) {
  42. real_t frac = j / 8.0;
  43. Vector2 p = curve->interpolate(i, frac);
  44. draw_line(prev_p, p, Color(0.5, 0.6, 1.0, 0.7), 2);
  45. prev_p = p;
  46. }
  47. }
  48. }
  49. }
  50. void Path2D::_curve_changed() {
  51. if (is_inside_tree() && Engine::get_singleton()->is_editor_hint())
  52. update();
  53. }
  54. void Path2D::set_curve(const Ref<Curve2D> &p_curve) {
  55. if (curve.is_valid()) {
  56. curve->disconnect("changed", this, "_curve_changed");
  57. }
  58. curve = p_curve;
  59. if (curve.is_valid()) {
  60. curve->connect("changed", this, "_curve_changed");
  61. }
  62. _curve_changed();
  63. }
  64. Ref<Curve2D> Path2D::get_curve() const {
  65. return curve;
  66. }
  67. void Path2D::_bind_methods() {
  68. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path2D::set_curve);
  69. ClassDB::bind_method(D_METHOD("get_curve"), &Path2D::get_curve);
  70. ClassDB::bind_method(D_METHOD("_curve_changed"), &Path2D::_curve_changed);
  71. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve2D"), "set_curve", "get_curve");
  72. }
  73. Path2D::Path2D() {
  74. set_curve(Ref<Curve2D>(memnew(Curve2D))); //create one by default
  75. }
  76. /////////////////////////////////////////////////////////////////////////////////
  77. void PathFollow2D::_update_transform() {
  78. if (!path)
  79. return;
  80. Ref<Curve2D> c = path->get_curve();
  81. if (!c.is_valid())
  82. return;
  83. float o = offset;
  84. if (loop)
  85. o = Math::fposmod(o, c->get_baked_length());
  86. Vector2 pos = c->interpolate_baked(o, cubic);
  87. if (rotate) {
  88. Vector2 n = (c->interpolate_baked(o + lookahead, cubic) - pos).normalized();
  89. Vector2 t = -n.tangent();
  90. pos += n * h_offset;
  91. pos += t * v_offset;
  92. set_rotation(t.angle());
  93. } else {
  94. pos.x += h_offset;
  95. pos.y += v_offset;
  96. }
  97. set_position(pos);
  98. }
  99. void PathFollow2D::_notification(int p_what) {
  100. switch (p_what) {
  101. case NOTIFICATION_ENTER_TREE: {
  102. path = Object::cast_to<Path2D>(get_parent());
  103. if (path) {
  104. _update_transform();
  105. }
  106. } break;
  107. case NOTIFICATION_EXIT_TREE: {
  108. path = NULL;
  109. } break;
  110. }
  111. }
  112. void PathFollow2D::set_cubic_interpolation(bool p_enable) {
  113. cubic = p_enable;
  114. }
  115. bool PathFollow2D::get_cubic_interpolation() const {
  116. return cubic;
  117. }
  118. bool PathFollow2D::_set(const StringName &p_name, const Variant &p_value) {
  119. if (p_name == SceneStringNames::get_singleton()->offset) {
  120. set_offset(p_value);
  121. } else if (p_name == SceneStringNames::get_singleton()->unit_offset) {
  122. set_unit_offset(p_value);
  123. } else if (p_name == SceneStringNames::get_singleton()->rotate) {
  124. set_rotate(p_value);
  125. } else if (p_name == SceneStringNames::get_singleton()->v_offset) {
  126. set_v_offset(p_value);
  127. } else if (p_name == SceneStringNames::get_singleton()->h_offset) {
  128. set_h_offset(p_value);
  129. } else if (String(p_name) == "cubic_interp") {
  130. set_cubic_interpolation(p_value);
  131. } else if (String(p_name) == "loop") {
  132. set_loop(p_value);
  133. } else if (String(p_name) == "lookahead") {
  134. set_lookahead(p_value);
  135. } else
  136. return false;
  137. return true;
  138. }
  139. bool PathFollow2D::_get(const StringName &p_name, Variant &r_ret) const {
  140. if (p_name == SceneStringNames::get_singleton()->offset) {
  141. r_ret = get_offset();
  142. } else if (p_name == SceneStringNames::get_singleton()->unit_offset) {
  143. r_ret = get_unit_offset();
  144. } else if (p_name == SceneStringNames::get_singleton()->rotate) {
  145. r_ret = is_rotating();
  146. } else if (p_name == SceneStringNames::get_singleton()->v_offset) {
  147. r_ret = get_v_offset();
  148. } else if (p_name == SceneStringNames::get_singleton()->h_offset) {
  149. r_ret = get_h_offset();
  150. } else if (String(p_name) == "cubic_interp") {
  151. r_ret = cubic;
  152. } else if (String(p_name) == "loop") {
  153. r_ret = loop;
  154. } else if (String(p_name) == "lookahead") {
  155. r_ret = lookahead;
  156. } else
  157. return false;
  158. return true;
  159. }
  160. void PathFollow2D::_get_property_list(List<PropertyInfo> *p_list) const {
  161. float max = 10000;
  162. if (path && path->get_curve().is_valid())
  163. max = path->get_curve()->get_baked_length();
  164. p_list->push_back(PropertyInfo(Variant::REAL, "offset", PROPERTY_HINT_RANGE, "0," + rtos(max) + ",0.01"));
  165. p_list->push_back(PropertyInfo(Variant::REAL, "unit_offset", PROPERTY_HINT_RANGE, "0,1,0.0001", PROPERTY_USAGE_EDITOR));
  166. p_list->push_back(PropertyInfo(Variant::REAL, "h_offset"));
  167. p_list->push_back(PropertyInfo(Variant::REAL, "v_offset"));
  168. p_list->push_back(PropertyInfo(Variant::BOOL, "rotate"));
  169. p_list->push_back(PropertyInfo(Variant::BOOL, "cubic_interp"));
  170. p_list->push_back(PropertyInfo(Variant::BOOL, "loop"));
  171. p_list->push_back(PropertyInfo(Variant::REAL, "lookahead", PROPERTY_HINT_RANGE, "0.001,1024.0,0.001"));
  172. }
  173. String PathFollow2D::get_configuration_warning() const {
  174. if (!is_visible_in_tree() || !is_inside_tree())
  175. return String();
  176. if (!Object::cast_to<Path2D>(get_parent())) {
  177. return TTR("PathFollow2D only works when set as a child of a Path2D node.");
  178. }
  179. return String();
  180. }
  181. void PathFollow2D::_bind_methods() {
  182. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &PathFollow2D::set_offset);
  183. ClassDB::bind_method(D_METHOD("get_offset"), &PathFollow2D::get_offset);
  184. ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow2D::set_h_offset);
  185. ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow2D::get_h_offset);
  186. ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow2D::set_v_offset);
  187. ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow2D::get_v_offset);
  188. ClassDB::bind_method(D_METHOD("set_unit_offset", "unit_offset"), &PathFollow2D::set_unit_offset);
  189. ClassDB::bind_method(D_METHOD("get_unit_offset"), &PathFollow2D::get_unit_offset);
  190. ClassDB::bind_method(D_METHOD("set_rotate", "enable"), &PathFollow2D::set_rotate);
  191. ClassDB::bind_method(D_METHOD("is_rotating"), &PathFollow2D::is_rotating);
  192. ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enable"), &PathFollow2D::set_cubic_interpolation);
  193. ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow2D::get_cubic_interpolation);
  194. ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow2D::set_loop);
  195. ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow2D::has_loop);
  196. }
  197. void PathFollow2D::set_offset(float p_offset) {
  198. offset = p_offset;
  199. if (path)
  200. _update_transform();
  201. _change_notify("offset");
  202. _change_notify("unit_offset");
  203. }
  204. void PathFollow2D::set_h_offset(float p_h_offset) {
  205. h_offset = p_h_offset;
  206. if (path)
  207. _update_transform();
  208. }
  209. float PathFollow2D::get_h_offset() const {
  210. return h_offset;
  211. }
  212. void PathFollow2D::set_v_offset(float p_v_offset) {
  213. v_offset = p_v_offset;
  214. if (path)
  215. _update_transform();
  216. }
  217. float PathFollow2D::get_v_offset() const {
  218. return v_offset;
  219. }
  220. float PathFollow2D::get_offset() const {
  221. return offset;
  222. }
  223. void PathFollow2D::set_unit_offset(float p_unit_offset) {
  224. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
  225. set_offset(p_unit_offset * path->get_curve()->get_baked_length());
  226. }
  227. float PathFollow2D::get_unit_offset() const {
  228. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
  229. return get_offset() / path->get_curve()->get_baked_length();
  230. else
  231. return 0;
  232. }
  233. void PathFollow2D::set_lookahead(float p_lookahead) {
  234. lookahead = p_lookahead;
  235. }
  236. float PathFollow2D::get_lookahead() const {
  237. return lookahead;
  238. }
  239. void PathFollow2D::set_rotate(bool p_rotate) {
  240. rotate = p_rotate;
  241. _update_transform();
  242. }
  243. bool PathFollow2D::is_rotating() const {
  244. return rotate;
  245. }
  246. void PathFollow2D::set_loop(bool p_loop) {
  247. loop = p_loop;
  248. }
  249. bool PathFollow2D::has_loop() const {
  250. return loop;
  251. }
  252. PathFollow2D::PathFollow2D() {
  253. offset = 0;
  254. h_offset = 0;
  255. v_offset = 0;
  256. path = NULL;
  257. rotate = true;
  258. cubic = true;
  259. loop = true;
  260. lookahead = 4;
  261. }