node_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*************************************************************************/
  2. /* node_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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 "node_2d.h"
  30. #include "servers/visual_server.h"
  31. #include "scene/gui/control.h"
  32. #include "scene/main/viewport.h"
  33. #include "message_queue.h"
  34. void Node2D::edit_set_pivot(const Point2& p_pivot) {
  35. }
  36. Point2 Node2D::edit_get_pivot() const {
  37. return Point2();
  38. }
  39. bool Node2D::edit_has_pivot() const {
  40. return false;
  41. }
  42. Variant Node2D::edit_get_state() const {
  43. Array state;
  44. state.push_back(get_pos());
  45. state.push_back(get_rot());
  46. state.push_back(get_scale());
  47. return state;
  48. }
  49. void Node2D::edit_set_state(const Variant& p_state) {
  50. Array state = p_state;
  51. ERR_FAIL_COND( state.size() != 3);
  52. pos = state[0];
  53. angle = state[1];
  54. _scale = state[2];
  55. _update_transform();
  56. _change_notify("transform/rot");
  57. _change_notify("transform/scale");
  58. _change_notify("transform/pos");
  59. }
  60. void Node2D::edit_set_rect(const Rect2& p_edit_rect) {
  61. Rect2 r = get_item_rect();
  62. Vector2 zero_offset;
  63. if (r.size.x!=0)
  64. zero_offset.x = -r.pos.x / r.size.x;
  65. if (r.size.y!=0)
  66. zero_offset.y = -r.pos.y / r.size.y;
  67. Size2 new_scale(1,1);
  68. if (r.size.x!=0)
  69. new_scale.x = p_edit_rect.size.x / r.size.x;
  70. if (r.size.y!=0)
  71. new_scale.y = p_edit_rect.size.y / r.size.y;
  72. Point2 new_pos = p_edit_rect.pos + p_edit_rect.size*zero_offset;//p_edit_rect.pos - r.pos;
  73. Matrix32 postxf;
  74. postxf.set_rotation_and_scale(angle,_scale);
  75. new_pos = postxf.xform(new_pos);
  76. pos+=new_pos;
  77. _scale*=new_scale;
  78. _update_transform();
  79. _change_notify("transform/scale");
  80. _change_notify("transform/pos");
  81. }
  82. void Node2D::edit_rotate(float p_rot) {
  83. angle+=p_rot;
  84. _update_transform();
  85. _change_notify("transform/rot");
  86. }
  87. void Node2D::_update_xform_values() {
  88. pos=_mat.elements[2];
  89. angle=_mat.get_rotation();
  90. _scale=_mat.get_scale();
  91. _xform_dirty=false;
  92. }
  93. void Node2D::_update_transform() {
  94. Matrix32 mat(angle,pos);
  95. _mat.set_rotation_and_scale(angle,_scale);
  96. _mat.elements[2]=pos;
  97. VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(),_mat);
  98. if (!is_inside_tree())
  99. return;
  100. _notify_transform();
  101. }
  102. void Node2D::set_pos(const Point2& p_pos) {
  103. if (_xform_dirty)
  104. ((Node2D*)this)->_update_xform_values();
  105. pos=p_pos;
  106. _update_transform();
  107. _change_notify("transform/pos");
  108. }
  109. void Node2D::set_rot(float p_angle) {
  110. if (_xform_dirty)
  111. ((Node2D*)this)->_update_xform_values();
  112. angle=p_angle;
  113. _update_transform();
  114. _change_notify("transform/rot");
  115. }
  116. void Node2D::set_scale(const Size2& p_scale) {
  117. if (_xform_dirty)
  118. ((Node2D*)this)->_update_xform_values();
  119. _scale=p_scale;
  120. if (_scale.x==0)
  121. _scale.x=CMP_EPSILON;
  122. if (_scale.y==0)
  123. _scale.y=CMP_EPSILON;
  124. _update_transform();
  125. _change_notify("transform/scale");
  126. }
  127. Point2 Node2D::get_pos() const {
  128. if (_xform_dirty)
  129. ((Node2D*)this)->_update_xform_values();
  130. return pos;
  131. }
  132. float Node2D::get_rot() const {
  133. if (_xform_dirty)
  134. ((Node2D*)this)->_update_xform_values();
  135. return angle;
  136. }
  137. Size2 Node2D::get_scale() const {
  138. if (_xform_dirty)
  139. ((Node2D*)this)->_update_xform_values();
  140. return _scale;
  141. }
  142. void Node2D::_set_rotd(float p_angle) {
  143. set_rot(Math::deg2rad(p_angle));
  144. }
  145. float Node2D::_get_rotd() const {
  146. return Math::rad2deg(get_rot());
  147. }
  148. void Node2D::_notification(int p_what) {
  149. switch(p_what) {
  150. }
  151. }
  152. Matrix32 Node2D::get_transform() const {
  153. return _mat;
  154. }
  155. Rect2 Node2D::get_item_rect() const {
  156. if (get_script_instance()) {
  157. Variant::CallError err;
  158. Rect2 r = get_script_instance()->call("_get_item_rect",NULL,0,err);
  159. if (err.error==Variant::CallError::CALL_OK)
  160. return r;
  161. }
  162. return Rect2(Point2(-32,-32),Size2(64,64));
  163. }
  164. void Node2D::rotate(float p_radians) {
  165. set_rot( get_rot() + p_radians);
  166. }
  167. void Node2D::translate(const Vector2& p_amount) {
  168. set_pos( get_pos() + p_amount );
  169. }
  170. void Node2D::global_translate(const Vector2& p_amount) {
  171. set_global_pos( get_global_pos() + p_amount );
  172. }
  173. void Node2D::scale(const Vector2& p_amount) {
  174. set_scale( get_scale() * p_amount );
  175. }
  176. void Node2D::move_x(float p_delta,bool p_scaled){
  177. Matrix32 t = get_transform();
  178. Vector2 m = t[0];
  179. if (!p_scaled)
  180. m.normalize();
  181. set_pos(t[2]+m*p_delta);
  182. }
  183. void Node2D::move_y(float p_delta,bool p_scaled){
  184. Matrix32 t = get_transform();
  185. Vector2 m = t[1];
  186. if (!p_scaled)
  187. m.normalize();
  188. set_pos(t[2]+m*p_delta);
  189. }
  190. Point2 Node2D::get_global_pos() const {
  191. return get_global_transform().get_origin();
  192. }
  193. void Node2D::set_global_pos(const Point2& p_pos) {
  194. Matrix32 inv;
  195. CanvasItem *pi = get_parent_item();
  196. if (pi) {
  197. inv = pi->get_global_transform().affine_inverse();
  198. set_pos(inv.xform(p_pos));
  199. } else {
  200. set_pos(p_pos);
  201. }
  202. }
  203. void Node2D::set_transform(const Matrix32& p_transform) {
  204. _mat=p_transform;
  205. _xform_dirty=true;
  206. VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(),_mat);
  207. if (!is_inside_tree())
  208. return;
  209. _notify_transform();
  210. }
  211. void Node2D::set_global_transform(const Matrix32& p_transform) {
  212. CanvasItem *pi = get_parent_item();
  213. if (pi)
  214. set_transform( pi->get_global_transform().affine_inverse() * p_transform);
  215. else
  216. set_transform(p_transform);
  217. }
  218. void Node2D::set_z(int p_z) {
  219. ERR_FAIL_COND(p_z<VS::CANVAS_ITEM_Z_MIN);
  220. ERR_FAIL_COND(p_z>VS::CANVAS_ITEM_Z_MAX);
  221. z=p_z;
  222. VS::get_singleton()->canvas_item_set_z(get_canvas_item(),z);
  223. }
  224. void Node2D::set_z_as_relative(bool p_enabled) {
  225. if (z_relative==p_enabled)
  226. return;
  227. z_relative=p_enabled;
  228. VS::get_singleton()->canvas_item_set_z_as_relative_to_parent(get_canvas_item(),p_enabled);
  229. }
  230. bool Node2D::is_z_relative() const {
  231. return z_relative;
  232. }
  233. int Node2D::get_z() const{
  234. return z;
  235. }
  236. Matrix32 Node2D::get_relative_transform(const Node *p_parent) const {
  237. if (p_parent==this)
  238. return Matrix32();
  239. Node2D *parent_2d = get_parent()->cast_to<Node2D>();
  240. ERR_FAIL_COND_V(!parent_2d,Matrix32());
  241. if (p_parent==parent_2d)
  242. return get_transform();
  243. else
  244. return parent_2d->get_relative_transform(p_parent) * get_transform();
  245. }
  246. void Node2D::look_at(const Vector2& p_pos) {
  247. rotate(get_angle_to(p_pos));
  248. }
  249. float Node2D::get_angle_to(const Vector2& p_pos) const {
  250. return (get_global_transform().affine_inverse().xform(p_pos)).atan2();
  251. }
  252. void Node2D::_bind_methods() {
  253. ObjectTypeDB::bind_method(_MD("_get_rotd"),&Node2D::_get_rotd);
  254. ObjectTypeDB::bind_method(_MD("_set_rotd"),&Node2D::_set_rotd);
  255. ObjectTypeDB::bind_method(_MD("set_pos","pos"),&Node2D::set_pos);
  256. ObjectTypeDB::bind_method(_MD("set_rot","rot"),&Node2D::set_rot);
  257. ObjectTypeDB::bind_method(_MD("set_scale","scale"),&Node2D::set_scale);
  258. ObjectTypeDB::bind_method(_MD("get_pos"),&Node2D::get_pos);
  259. ObjectTypeDB::bind_method(_MD("get_rot"),&Node2D::get_rot);
  260. ObjectTypeDB::bind_method(_MD("get_scale"),&Node2D::get_scale);
  261. ObjectTypeDB::bind_method(_MD("rotate","radians"),&Node2D::rotate);
  262. ObjectTypeDB::bind_method(_MD("move_local_x","delta","scaled"),&Node2D::move_x,DEFVAL(false));
  263. ObjectTypeDB::bind_method(_MD("move_local_y","delta","scaled"),&Node2D::move_y,DEFVAL(false));
  264. ObjectTypeDB::bind_method(_MD("translate","offset"),&Node2D::translate);
  265. ObjectTypeDB::bind_method(_MD("global_translate","offset"),&Node2D::global_translate);
  266. ObjectTypeDB::bind_method(_MD("scale","ratio"),&Node2D::scale);
  267. ObjectTypeDB::bind_method(_MD("set_global_pos","pos"),&Node2D::set_global_pos);
  268. ObjectTypeDB::bind_method(_MD("get_global_pos"),&Node2D::get_global_pos);
  269. ObjectTypeDB::bind_method(_MD("set_transform","xform"),&Node2D::set_transform);
  270. ObjectTypeDB::bind_method(_MD("set_global_transform","xform"),&Node2D::set_global_transform);
  271. ObjectTypeDB::bind_method(_MD("look_at","point"),&Node2D::look_at);
  272. ObjectTypeDB::bind_method(_MD("get_angle_to","point"),&Node2D::get_angle_to);
  273. ObjectTypeDB::bind_method(_MD("set_z","z"),&Node2D::set_z);
  274. ObjectTypeDB::bind_method(_MD("get_z"),&Node2D::get_z);
  275. ObjectTypeDB::bind_method(_MD("set_z_as_relative","enable"),&Node2D::set_z_as_relative);
  276. ObjectTypeDB::bind_method(_MD("is_z_relative"),&Node2D::is_z_relative);
  277. ObjectTypeDB::bind_method(_MD("edit_set_pivot"),&Node2D::edit_set_pivot);
  278. ObjectTypeDB::bind_method(_MD("get_relative_transform"),&Node2D::get_relative_transform);
  279. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2,"transform/pos"),_SCS("set_pos"),_SCS("get_pos"));
  280. ADD_PROPERTY(PropertyInfo(Variant::REAL,"transform/rot",PROPERTY_HINT_RANGE,"-1440,1440,0.1"),_SCS("_set_rotd"),_SCS("_get_rotd"));
  281. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2,"transform/scale"),_SCS("set_scale"),_SCS("get_scale"));
  282. ADD_PROPERTY(PropertyInfo(Variant::INT,"z/z",PROPERTY_HINT_RANGE,itos(VS::CANVAS_ITEM_Z_MIN)+","+itos(VS::CANVAS_ITEM_Z_MAX)+",1"),_SCS("set_z"),_SCS("get_z"));
  283. ADD_PROPERTY(PropertyInfo(Variant::BOOL,"z/relative"),_SCS("set_z_as_relative"),_SCS("is_z_relative"));
  284. }
  285. Node2D::Node2D() {
  286. angle=0;
  287. _scale=Vector2(1,1);
  288. _xform_dirty=false;
  289. z=0;
  290. z_relative=true;
  291. }