path_2d.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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-2016 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. ObjectTypeDB::bind_method(_MD("set_curve","curve:Curve2D"),&Path2D::set_curve);
  67. ObjectTypeDB::bind_method(_MD("get_curve:Curve2D","curve"),&Path2D::get_curve);
  68. ObjectTypeDB::bind_method(_MD("_curve_changed"),&Path2D::_curve_changed);
  69. ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve2D"), _SCS("set_curve"),_SCS("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_rot(t.angle());
  91. } else {
  92. pos.x+=h_offset;
  93. pos.y+=v_offset;
  94. }
  95. set_pos(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. void PathFollow2D::_bind_methods() {
  175. ObjectTypeDB::bind_method(_MD("set_offset","offset"),&PathFollow2D::set_offset);
  176. ObjectTypeDB::bind_method(_MD("get_offset"),&PathFollow2D::get_offset);
  177. ObjectTypeDB::bind_method(_MD("set_h_offset","h_offset"),&PathFollow2D::set_h_offset);
  178. ObjectTypeDB::bind_method(_MD("get_h_offset"),&PathFollow2D::get_h_offset);
  179. ObjectTypeDB::bind_method(_MD("set_v_offset","v_offset"),&PathFollow2D::set_v_offset);
  180. ObjectTypeDB::bind_method(_MD("get_v_offset"),&PathFollow2D::get_v_offset);
  181. ObjectTypeDB::bind_method(_MD("set_unit_offset","unit_offset"),&PathFollow2D::set_unit_offset);
  182. ObjectTypeDB::bind_method(_MD("get_unit_offset"),&PathFollow2D::get_unit_offset);
  183. ObjectTypeDB::bind_method(_MD("set_rotate","enable"),&PathFollow2D::set_rotate);
  184. ObjectTypeDB::bind_method(_MD("is_rotating"),&PathFollow2D::is_rotating);
  185. ObjectTypeDB::bind_method(_MD("set_cubic_interpolation","enable"),&PathFollow2D::set_cubic_interpolation);
  186. ObjectTypeDB::bind_method(_MD("get_cubic_interpolation"),&PathFollow2D::get_cubic_interpolation);
  187. ObjectTypeDB::bind_method(_MD("set_loop","loop"),&PathFollow2D::set_loop);
  188. ObjectTypeDB::bind_method(_MD("has_loop"),&PathFollow2D::has_loop);
  189. }
  190. void PathFollow2D::set_offset(float p_offset) {
  191. offset=p_offset;
  192. if (path)
  193. _update_transform();
  194. _change_notify("offset");
  195. _change_notify("unit_offset");
  196. }
  197. void PathFollow2D::set_h_offset(float p_h_offset) {
  198. h_offset=p_h_offset;
  199. if (path)
  200. _update_transform();
  201. }
  202. float PathFollow2D::get_h_offset() const {
  203. return h_offset;
  204. }
  205. void PathFollow2D::set_v_offset(float p_v_offset) {
  206. v_offset=p_v_offset;
  207. if (path)
  208. _update_transform();
  209. }
  210. float PathFollow2D::get_v_offset() const {
  211. return v_offset;
  212. }
  213. float PathFollow2D::get_offset() const{
  214. return offset;
  215. }
  216. void PathFollow2D::set_unit_offset(float p_unit_offset) {
  217. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
  218. set_offset(p_unit_offset*path->get_curve()->get_baked_length());
  219. }
  220. float PathFollow2D::get_unit_offset() const{
  221. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
  222. return get_offset()/path->get_curve()->get_baked_length();
  223. else
  224. return 0;
  225. }
  226. void PathFollow2D::set_lookahead(float p_lookahead) {
  227. lookahead=p_lookahead;
  228. }
  229. float PathFollow2D::get_lookahead() const{
  230. return lookahead;
  231. }
  232. void PathFollow2D::set_rotate(bool p_rotate) {
  233. rotate=p_rotate;
  234. _update_transform();
  235. }
  236. bool PathFollow2D::is_rotating() const {
  237. return rotate;
  238. }
  239. void PathFollow2D::set_loop(bool p_loop) {
  240. loop=p_loop;
  241. }
  242. bool PathFollow2D::has_loop() const{
  243. return loop;
  244. }
  245. PathFollow2D::PathFollow2D() {
  246. offset=0;
  247. h_offset=0;
  248. v_offset=0;
  249. path=NULL;
  250. rotate=true;
  251. cubic=true;
  252. loop=true;
  253. lookahead=4;
  254. }