path_2d.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*************************************************************************/
  2. /* path_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "path_2d.h"
  30. #include "scene/scene_string_names.h"
  31. void Path2D::_notification(int p_what) {
  32. if (p_what==NOTIFICATION_DRAW && curve.is_valid()) {
  33. //draw the curve!!
  34. if (!get_tree()->is_editor_hint() && !get_tree()->is_debugging_navigation_hint()) {
  35. return;
  36. }
  37. for(int i=0;i<curve->get_point_count();i++) {
  38. Vector2 prev_p=curve->get_point_pos(i);
  39. for(int j=1;j<=8;j++) {
  40. real_t frac = j/8.0;
  41. Vector2 p = curve->interpolate(i,frac);
  42. draw_line(prev_p,p,Color(0.5,0.6,1.0,0.7),2);
  43. prev_p=p;
  44. }
  45. }
  46. }
  47. }
  48. void Path2D::_curve_changed() {
  49. if (is_inside_tree() && get_tree()->is_editor_hint())
  50. update();
  51. }
  52. void Path2D::set_curve(const Ref<Curve2D>& p_curve) {
  53. if (curve.is_valid()) {
  54. curve->disconnect("changed",this,"_curve_changed");
  55. }
  56. curve=p_curve;
  57. if (curve.is_valid()) {
  58. curve->connect("changed",this,"_curve_changed");
  59. }
  60. _curve_changed();
  61. }
  62. Ref<Curve2D> Path2D::get_curve() const{
  63. return curve;
  64. }
  65. void Path2D::_bind_methods() {
  66. ClassDB::bind_method(_MD("set_curve","curve:Curve2D"),&Path2D::set_curve);
  67. ClassDB::bind_method(_MD("get_curve:Curve2D","curve"),&Path2D::get_curve);
  68. ClassDB::bind_method(_MD("_curve_changed"),&Path2D::_curve_changed);
  69. ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve2D"), "set_curve","get_curve");
  70. }
  71. Path2D::Path2D() {
  72. set_curve(Ref<Curve2D>( memnew( Curve2D ))); //create one by default
  73. }
  74. /////////////////////////////////////////////////////////////////////////////////
  75. void PathFollow2D::_update_transform() {
  76. if (!path)
  77. return;
  78. Ref<Curve2D> c =path->get_curve();
  79. if (!c.is_valid())
  80. return;
  81. float o = offset;
  82. if (loop)
  83. o=Math::fposmod(o,c->get_baked_length());
  84. Vector2 pos = c->interpolate_baked(o,cubic);
  85. if (rotate) {
  86. Vector2 n = (c->interpolate_baked(o+lookahead,cubic)-pos).normalized();
  87. Vector2 t = -n.tangent();
  88. pos+=n*h_offset;
  89. pos+=t*v_offset;
  90. set_rotation(t.angle());
  91. } else {
  92. pos.x+=h_offset;
  93. pos.y+=v_offset;
  94. }
  95. set_position(pos);
  96. }
  97. void PathFollow2D::_notification(int p_what) {
  98. switch(p_what) {
  99. case NOTIFICATION_ENTER_TREE: {
  100. Node *parent=get_parent();
  101. if (parent) {
  102. path=parent->cast_to<Path2D>();
  103. if (path) {
  104. _update_transform();
  105. }
  106. }
  107. } break;
  108. case NOTIFICATION_EXIT_TREE: {
  109. path=NULL;
  110. } break;
  111. }
  112. }
  113. void PathFollow2D::set_cubic_interpolation(bool p_enable) {
  114. cubic=p_enable;
  115. }
  116. bool PathFollow2D::get_cubic_interpolation() const {
  117. return cubic;
  118. }
  119. bool PathFollow2D::_set(const StringName& p_name, const Variant& p_value) {
  120. if (p_name==SceneStringNames::get_singleton()->offset) {
  121. set_offset(p_value);
  122. } else if (p_name==SceneStringNames::get_singleton()->unit_offset) {
  123. set_unit_offset(p_value);
  124. } else if (p_name==SceneStringNames::get_singleton()->rotate) {
  125. set_rotate(p_value);
  126. } else if (p_name==SceneStringNames::get_singleton()->v_offset) {
  127. set_v_offset(p_value);
  128. } else if (p_name==SceneStringNames::get_singleton()->h_offset) {
  129. set_h_offset(p_value);
  130. } else if (String(p_name)=="cubic_interp") {
  131. set_cubic_interpolation(p_value);
  132. } else if (String(p_name)=="loop") {
  133. set_loop(p_value);
  134. } else if (String(p_name)=="lookahead") {
  135. set_lookahead(p_value);
  136. } else
  137. return false;
  138. return true;
  139. }
  140. bool PathFollow2D::_get(const StringName& p_name,Variant &r_ret) const{
  141. if (p_name==SceneStringNames::get_singleton()->offset) {
  142. r_ret=get_offset();
  143. } else if (p_name==SceneStringNames::get_singleton()->unit_offset) {
  144. r_ret=get_unit_offset();
  145. } else if (p_name==SceneStringNames::get_singleton()->rotate) {
  146. r_ret=is_rotating();
  147. } else if (p_name==SceneStringNames::get_singleton()->v_offset) {
  148. r_ret=get_v_offset();
  149. } else if (p_name==SceneStringNames::get_singleton()->h_offset) {
  150. r_ret=get_h_offset();
  151. } else if (String(p_name)=="cubic_interp") {
  152. r_ret=cubic;
  153. } else if (String(p_name)=="loop") {
  154. r_ret=loop;
  155. } else if (String(p_name)=="lookahead") {
  156. r_ret=lookahead;
  157. } else
  158. return false;
  159. return true;
  160. }
  161. void PathFollow2D::_get_property_list( List<PropertyInfo> *p_list) const{
  162. float max=10000;
  163. if (path && path->get_curve().is_valid())
  164. max=path->get_curve()->get_baked_length();
  165. p_list->push_back( PropertyInfo( Variant::REAL, "offset", PROPERTY_HINT_RANGE,"0,"+rtos(max)+",0.01"));
  166. p_list->push_back( PropertyInfo( Variant::REAL, "unit_offset", PROPERTY_HINT_RANGE,"0,1,0.0001",PROPERTY_USAGE_EDITOR));
  167. p_list->push_back( PropertyInfo( Variant::REAL, "h_offset") );
  168. p_list->push_back( PropertyInfo( Variant::REAL, "v_offset") );
  169. p_list->push_back( PropertyInfo( Variant::BOOL, "rotate") );
  170. p_list->push_back( PropertyInfo( Variant::BOOL, "cubic_interp"));
  171. p_list->push_back( PropertyInfo( Variant::BOOL, "loop"));
  172. p_list->push_back( PropertyInfo( Variant::REAL, "lookahead",PROPERTY_HINT_RANGE,"0.001,1024.0,0.001"));
  173. }
  174. String PathFollow2D::get_configuration_warning() const {
  175. if (!is_visible_in_tree() || !is_inside_tree())
  176. return String();
  177. if (!get_parent() || !get_parent()->cast_to<Path2D>()) {
  178. return TTR("PathFollow2D only works when set as a child of a Path2D node.");
  179. }
  180. return String();
  181. }
  182. void PathFollow2D::_bind_methods() {
  183. ClassDB::bind_method(_MD("set_offset","offset"),&PathFollow2D::set_offset);
  184. ClassDB::bind_method(_MD("get_offset"),&PathFollow2D::get_offset);
  185. ClassDB::bind_method(_MD("set_h_offset","h_offset"),&PathFollow2D::set_h_offset);
  186. ClassDB::bind_method(_MD("get_h_offset"),&PathFollow2D::get_h_offset);
  187. ClassDB::bind_method(_MD("set_v_offset","v_offset"),&PathFollow2D::set_v_offset);
  188. ClassDB::bind_method(_MD("get_v_offset"),&PathFollow2D::get_v_offset);
  189. ClassDB::bind_method(_MD("set_unit_offset","unit_offset"),&PathFollow2D::set_unit_offset);
  190. ClassDB::bind_method(_MD("get_unit_offset"),&PathFollow2D::get_unit_offset);
  191. ClassDB::bind_method(_MD("set_rotate","enable"),&PathFollow2D::set_rotate);
  192. ClassDB::bind_method(_MD("is_rotating"),&PathFollow2D::is_rotating);
  193. ClassDB::bind_method(_MD("set_cubic_interpolation","enable"),&PathFollow2D::set_cubic_interpolation);
  194. ClassDB::bind_method(_MD("get_cubic_interpolation"),&PathFollow2D::get_cubic_interpolation);
  195. ClassDB::bind_method(_MD("set_loop","loop"),&PathFollow2D::set_loop);
  196. ClassDB::bind_method(_MD("has_loop"),&PathFollow2D::has_loop);
  197. }
  198. void PathFollow2D::set_offset(float p_offset) {
  199. offset=p_offset;
  200. if (path)
  201. _update_transform();
  202. _change_notify("offset");
  203. _change_notify("unit_offset");
  204. }
  205. void PathFollow2D::set_h_offset(float p_h_offset) {
  206. h_offset=p_h_offset;
  207. if (path)
  208. _update_transform();
  209. }
  210. float PathFollow2D::get_h_offset() const {
  211. return h_offset;
  212. }
  213. void PathFollow2D::set_v_offset(float p_v_offset) {
  214. v_offset=p_v_offset;
  215. if (path)
  216. _update_transform();
  217. }
  218. float PathFollow2D::get_v_offset() const {
  219. return v_offset;
  220. }
  221. float PathFollow2D::get_offset() const{
  222. return offset;
  223. }
  224. void PathFollow2D::set_unit_offset(float p_unit_offset) {
  225. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
  226. set_offset(p_unit_offset*path->get_curve()->get_baked_length());
  227. }
  228. float PathFollow2D::get_unit_offset() const{
  229. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
  230. return get_offset()/path->get_curve()->get_baked_length();
  231. else
  232. return 0;
  233. }
  234. void PathFollow2D::set_lookahead(float p_lookahead) {
  235. lookahead=p_lookahead;
  236. }
  237. float PathFollow2D::get_lookahead() const{
  238. return lookahead;
  239. }
  240. void PathFollow2D::set_rotate(bool p_rotate) {
  241. rotate=p_rotate;
  242. _update_transform();
  243. }
  244. bool PathFollow2D::is_rotating() const {
  245. return rotate;
  246. }
  247. void PathFollow2D::set_loop(bool p_loop) {
  248. loop=p_loop;
  249. }
  250. bool PathFollow2D::has_loop() const{
  251. return loop;
  252. }
  253. PathFollow2D::PathFollow2D() {
  254. offset=0;
  255. h_offset=0;
  256. v_offset=0;
  257. path=NULL;
  258. rotate=true;
  259. cubic=true;
  260. loop=true;
  261. lookahead=4;
  262. }