node_2d.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /**************************************************************************/
  2. /* node_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 "node_2d.h"
  31. #include "scene/main/viewport.h"
  32. #ifdef TOOLS_ENABLED
  33. Dictionary Node2D::_edit_get_state() const {
  34. Dictionary state;
  35. state["position"] = get_position();
  36. state["rotation"] = get_rotation();
  37. state["scale"] = get_scale();
  38. state["skew"] = get_skew();
  39. return state;
  40. }
  41. void Node2D::_edit_set_state(const Dictionary &p_state) {
  42. position = p_state["position"];
  43. rotation = p_state["rotation"];
  44. scale = p_state["scale"];
  45. skew = p_state["skew"];
  46. _update_transform();
  47. }
  48. void Node2D::_edit_set_position(const Point2 &p_position) {
  49. set_position(p_position);
  50. }
  51. Point2 Node2D::_edit_get_position() const {
  52. return position;
  53. }
  54. void Node2D::_edit_set_scale(const Size2 &p_scale) {
  55. set_scale(p_scale);
  56. }
  57. Size2 Node2D::_edit_get_scale() const {
  58. return scale;
  59. }
  60. void Node2D::_edit_set_rotation(real_t p_rotation) {
  61. rotation = p_rotation;
  62. _update_transform();
  63. }
  64. real_t Node2D::_edit_get_rotation() const {
  65. return rotation;
  66. }
  67. bool Node2D::_edit_use_rotation() const {
  68. return true;
  69. }
  70. void Node2D::_edit_set_rect(const Rect2 &p_edit_rect) {
  71. ERR_FAIL_COND(!_edit_use_rect());
  72. Rect2 r = _edit_get_rect();
  73. Vector2 zero_offset;
  74. Size2 new_scale(1, 1);
  75. if (r.size.x != 0) {
  76. zero_offset.x = -r.position.x / r.size.x;
  77. new_scale.x = p_edit_rect.size.x / r.size.x;
  78. }
  79. if (r.size.y != 0) {
  80. zero_offset.y = -r.position.y / r.size.y;
  81. new_scale.y = p_edit_rect.size.y / r.size.y;
  82. }
  83. Point2 new_pos = p_edit_rect.position + p_edit_rect.size * zero_offset;
  84. Transform2D postxf;
  85. postxf.set_rotation_scale_and_skew(rotation, scale, skew);
  86. new_pos = postxf.xform(new_pos);
  87. position += new_pos;
  88. scale *= new_scale;
  89. _update_transform();
  90. }
  91. #endif
  92. void Node2D::_set_xform_dirty(bool p_dirty) const {
  93. if (is_group_processing()) {
  94. if (p_dirty) {
  95. xform_dirty.mt.set();
  96. } else {
  97. xform_dirty.mt.clear();
  98. }
  99. } else {
  100. xform_dirty.st = p_dirty;
  101. }
  102. }
  103. void Node2D::_update_xform_values() const {
  104. rotation = transform.get_rotation();
  105. skew = transform.get_skew();
  106. position = transform.columns[2];
  107. scale = transform.get_scale();
  108. _set_xform_dirty(false);
  109. }
  110. void Node2D::_update_transform() {
  111. transform.set_rotation_scale_and_skew(rotation, scale, skew);
  112. transform.columns[2] = position;
  113. RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), transform);
  114. _notify_transform();
  115. }
  116. void Node2D::reparent(Node *p_parent, bool p_keep_global_transform) {
  117. ERR_THREAD_GUARD;
  118. if (p_keep_global_transform) {
  119. Transform2D temp = get_global_transform();
  120. Node::reparent(p_parent);
  121. set_global_transform(temp);
  122. } else {
  123. Node::reparent(p_parent);
  124. }
  125. }
  126. void Node2D::set_position(const Point2 &p_pos) {
  127. ERR_THREAD_GUARD;
  128. if (_is_xform_dirty()) {
  129. _update_xform_values();
  130. }
  131. position = p_pos;
  132. _update_transform();
  133. }
  134. void Node2D::set_rotation(real_t p_radians) {
  135. ERR_THREAD_GUARD;
  136. if (_is_xform_dirty()) {
  137. _update_xform_values();
  138. }
  139. rotation = p_radians;
  140. _update_transform();
  141. }
  142. void Node2D::set_rotation_degrees(real_t p_degrees) {
  143. ERR_THREAD_GUARD;
  144. set_rotation(Math::deg_to_rad(p_degrees));
  145. }
  146. void Node2D::set_skew(real_t p_radians) {
  147. ERR_THREAD_GUARD;
  148. if (_is_xform_dirty()) {
  149. _update_xform_values();
  150. }
  151. skew = p_radians;
  152. _update_transform();
  153. }
  154. void Node2D::set_scale(const Size2 &p_scale) {
  155. ERR_THREAD_GUARD;
  156. if (_is_xform_dirty()) {
  157. _update_xform_values();
  158. }
  159. scale = p_scale;
  160. // Avoid having 0 scale values, can lead to errors in physics and rendering.
  161. if (Math::is_zero_approx(scale.x)) {
  162. scale.x = CMP_EPSILON;
  163. }
  164. if (Math::is_zero_approx(scale.y)) {
  165. scale.y = CMP_EPSILON;
  166. }
  167. _update_transform();
  168. }
  169. Point2 Node2D::get_position() const {
  170. ERR_READ_THREAD_GUARD_V(Point2());
  171. if (_is_xform_dirty()) {
  172. _update_xform_values();
  173. }
  174. return position;
  175. }
  176. real_t Node2D::get_rotation() const {
  177. ERR_READ_THREAD_GUARD_V(0);
  178. if (_is_xform_dirty()) {
  179. _update_xform_values();
  180. }
  181. return rotation;
  182. }
  183. real_t Node2D::get_rotation_degrees() const {
  184. ERR_READ_THREAD_GUARD_V(0);
  185. return Math::rad_to_deg(get_rotation());
  186. }
  187. real_t Node2D::get_skew() const {
  188. ERR_READ_THREAD_GUARD_V(0);
  189. if (_is_xform_dirty()) {
  190. _update_xform_values();
  191. }
  192. return skew;
  193. }
  194. Size2 Node2D::get_scale() const {
  195. ERR_READ_THREAD_GUARD_V(Size2());
  196. if (_is_xform_dirty()) {
  197. _update_xform_values();
  198. }
  199. return scale;
  200. }
  201. Transform2D Node2D::get_transform() const {
  202. ERR_READ_THREAD_GUARD_V(Transform2D());
  203. return transform;
  204. }
  205. void Node2D::rotate(real_t p_radians) {
  206. ERR_THREAD_GUARD;
  207. set_rotation(get_rotation() + p_radians);
  208. }
  209. void Node2D::translate(const Vector2 &p_amount) {
  210. ERR_THREAD_GUARD;
  211. set_position(get_position() + p_amount);
  212. }
  213. void Node2D::global_translate(const Vector2 &p_amount) {
  214. ERR_THREAD_GUARD;
  215. set_global_position(get_global_position() + p_amount);
  216. }
  217. void Node2D::apply_scale(const Size2 &p_amount) {
  218. ERR_THREAD_GUARD;
  219. set_scale(get_scale() * p_amount);
  220. }
  221. void Node2D::move_x(real_t p_delta, bool p_scaled) {
  222. ERR_THREAD_GUARD;
  223. Transform2D t = get_transform();
  224. Vector2 m = t[0];
  225. if (!p_scaled) {
  226. m.normalize();
  227. }
  228. set_position(t[2] + m * p_delta);
  229. }
  230. void Node2D::move_y(real_t p_delta, bool p_scaled) {
  231. ERR_THREAD_GUARD;
  232. Transform2D t = get_transform();
  233. Vector2 m = t[1];
  234. if (!p_scaled) {
  235. m.normalize();
  236. }
  237. set_position(t[2] + m * p_delta);
  238. }
  239. Point2 Node2D::get_global_position() const {
  240. ERR_READ_THREAD_GUARD_V(Point2());
  241. return get_global_transform().get_origin();
  242. }
  243. void Node2D::set_global_position(const Point2 &p_pos) {
  244. ERR_THREAD_GUARD;
  245. CanvasItem *parent = get_parent_item();
  246. if (parent) {
  247. Transform2D inv = parent->get_global_transform().affine_inverse();
  248. set_position(inv.xform(p_pos));
  249. } else {
  250. set_position(p_pos);
  251. }
  252. }
  253. real_t Node2D::get_global_rotation() const {
  254. ERR_READ_THREAD_GUARD_V(0);
  255. return get_global_transform().get_rotation();
  256. }
  257. real_t Node2D::get_global_rotation_degrees() const {
  258. ERR_READ_THREAD_GUARD_V(0);
  259. return Math::rad_to_deg(get_global_rotation());
  260. }
  261. real_t Node2D::get_global_skew() const {
  262. ERR_READ_THREAD_GUARD_V(0);
  263. return get_global_transform().get_skew();
  264. }
  265. void Node2D::set_global_rotation(const real_t p_radians) {
  266. ERR_THREAD_GUARD;
  267. CanvasItem *parent = get_parent_item();
  268. if (parent) {
  269. Transform2D parent_global_transform = parent->get_global_transform();
  270. Transform2D new_transform = parent_global_transform * get_transform();
  271. new_transform.set_rotation(p_radians);
  272. new_transform = parent_global_transform.affine_inverse() * new_transform;
  273. set_rotation(new_transform.get_rotation());
  274. } else {
  275. set_rotation(p_radians);
  276. }
  277. }
  278. void Node2D::set_global_rotation_degrees(const real_t p_degrees) {
  279. ERR_THREAD_GUARD;
  280. set_global_rotation(Math::deg_to_rad(p_degrees));
  281. }
  282. void Node2D::set_global_skew(const real_t p_radians) {
  283. ERR_THREAD_GUARD;
  284. CanvasItem *parent = get_parent_item();
  285. if (parent) {
  286. Transform2D parent_global_transform = parent->get_global_transform();
  287. Transform2D new_transform = parent_global_transform * get_transform();
  288. new_transform.set_skew(p_radians);
  289. new_transform = parent_global_transform.affine_inverse() * new_transform;
  290. set_skew(new_transform.get_skew());
  291. } else {
  292. set_skew(p_radians);
  293. }
  294. }
  295. Size2 Node2D::get_global_scale() const {
  296. ERR_READ_THREAD_GUARD_V(Size2());
  297. return get_global_transform().get_scale();
  298. }
  299. void Node2D::set_global_scale(const Size2 &p_scale) {
  300. ERR_THREAD_GUARD;
  301. CanvasItem *parent = get_parent_item();
  302. if (parent) {
  303. Transform2D parent_global_transform = parent->get_global_transform();
  304. Transform2D new_transform = parent_global_transform * get_transform();
  305. new_transform.set_scale(p_scale);
  306. new_transform = parent_global_transform.affine_inverse() * new_transform;
  307. set_scale(new_transform.get_scale());
  308. } else {
  309. set_scale(p_scale);
  310. }
  311. }
  312. void Node2D::set_transform(const Transform2D &p_transform) {
  313. ERR_THREAD_GUARD;
  314. transform = p_transform;
  315. _set_xform_dirty(true);
  316. RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), transform);
  317. _notify_transform();
  318. }
  319. void Node2D::set_global_transform(const Transform2D &p_transform) {
  320. ERR_THREAD_GUARD;
  321. CanvasItem *parent = get_parent_item();
  322. if (parent) {
  323. set_transform(parent->get_global_transform().affine_inverse() * p_transform);
  324. } else {
  325. set_transform(p_transform);
  326. }
  327. }
  328. Transform2D Node2D::get_relative_transform_to_parent(const Node *p_parent) const {
  329. ERR_READ_THREAD_GUARD_V(Transform2D());
  330. if (p_parent == this) {
  331. return Transform2D();
  332. }
  333. Node2D *parent_2d = Object::cast_to<Node2D>(get_parent());
  334. ERR_FAIL_NULL_V(parent_2d, Transform2D());
  335. if (p_parent == parent_2d) {
  336. return get_transform();
  337. } else {
  338. return parent_2d->get_relative_transform_to_parent(p_parent) * get_transform();
  339. }
  340. }
  341. void Node2D::look_at(const Vector2 &p_pos) {
  342. ERR_THREAD_GUARD;
  343. rotate(get_angle_to(p_pos));
  344. }
  345. real_t Node2D::get_angle_to(const Vector2 &p_pos) const {
  346. ERR_READ_THREAD_GUARD_V(0);
  347. return (to_local(p_pos) * get_scale()).angle();
  348. }
  349. Point2 Node2D::to_local(Point2 p_global) const {
  350. ERR_READ_THREAD_GUARD_V(Point2());
  351. return get_global_transform().affine_inverse().xform(p_global);
  352. }
  353. Point2 Node2D::to_global(Point2 p_local) const {
  354. ERR_READ_THREAD_GUARD_V(Point2());
  355. return get_global_transform().xform(p_local);
  356. }
  357. void Node2D::_notification(int p_notification) {
  358. switch (p_notification) {
  359. case NOTIFICATION_ENTER_TREE: {
  360. ERR_MAIN_THREAD_GUARD;
  361. if (get_viewport()) {
  362. get_parent()->connect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::gui_set_root_order_dirty), CONNECT_REFERENCE_COUNTED);
  363. }
  364. } break;
  365. case NOTIFICATION_EXIT_TREE: {
  366. ERR_MAIN_THREAD_GUARD;
  367. if (get_viewport()) {
  368. get_parent()->disconnect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::gui_set_root_order_dirty));
  369. }
  370. } break;
  371. }
  372. }
  373. void Node2D::_bind_methods() {
  374. ClassDB::bind_method(D_METHOD("set_position", "position"), &Node2D::set_position);
  375. ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &Node2D::set_rotation);
  376. ClassDB::bind_method(D_METHOD("set_rotation_degrees", "degrees"), &Node2D::set_rotation_degrees);
  377. ClassDB::bind_method(D_METHOD("set_skew", "radians"), &Node2D::set_skew);
  378. ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Node2D::set_scale);
  379. ClassDB::bind_method(D_METHOD("get_position"), &Node2D::get_position);
  380. ClassDB::bind_method(D_METHOD("get_rotation"), &Node2D::get_rotation);
  381. ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Node2D::get_rotation_degrees);
  382. ClassDB::bind_method(D_METHOD("get_skew"), &Node2D::get_skew);
  383. ClassDB::bind_method(D_METHOD("get_scale"), &Node2D::get_scale);
  384. ClassDB::bind_method(D_METHOD("rotate", "radians"), &Node2D::rotate);
  385. ClassDB::bind_method(D_METHOD("move_local_x", "delta", "scaled"), &Node2D::move_x, DEFVAL(false));
  386. ClassDB::bind_method(D_METHOD("move_local_y", "delta", "scaled"), &Node2D::move_y, DEFVAL(false));
  387. ClassDB::bind_method(D_METHOD("translate", "offset"), &Node2D::translate);
  388. ClassDB::bind_method(D_METHOD("global_translate", "offset"), &Node2D::global_translate);
  389. ClassDB::bind_method(D_METHOD("apply_scale", "ratio"), &Node2D::apply_scale);
  390. ClassDB::bind_method(D_METHOD("set_global_position", "position"), &Node2D::set_global_position);
  391. ClassDB::bind_method(D_METHOD("get_global_position"), &Node2D::get_global_position);
  392. ClassDB::bind_method(D_METHOD("set_global_rotation", "radians"), &Node2D::set_global_rotation);
  393. ClassDB::bind_method(D_METHOD("set_global_rotation_degrees", "degrees"), &Node2D::set_global_rotation_degrees);
  394. ClassDB::bind_method(D_METHOD("get_global_rotation"), &Node2D::get_global_rotation);
  395. ClassDB::bind_method(D_METHOD("get_global_rotation_degrees"), &Node2D::get_global_rotation_degrees);
  396. ClassDB::bind_method(D_METHOD("set_global_skew", "radians"), &Node2D::set_global_skew);
  397. ClassDB::bind_method(D_METHOD("get_global_skew"), &Node2D::get_global_skew);
  398. ClassDB::bind_method(D_METHOD("set_global_scale", "scale"), &Node2D::set_global_scale);
  399. ClassDB::bind_method(D_METHOD("get_global_scale"), &Node2D::get_global_scale);
  400. ClassDB::bind_method(D_METHOD("set_transform", "xform"), &Node2D::set_transform);
  401. ClassDB::bind_method(D_METHOD("set_global_transform", "xform"), &Node2D::set_global_transform);
  402. ClassDB::bind_method(D_METHOD("look_at", "point"), &Node2D::look_at);
  403. ClassDB::bind_method(D_METHOD("get_angle_to", "point"), &Node2D::get_angle_to);
  404. ClassDB::bind_method(D_METHOD("to_local", "global_point"), &Node2D::to_local);
  405. ClassDB::bind_method(D_METHOD("to_global", "local_point"), &Node2D::to_global);
  406. ClassDB::bind_method(D_METHOD("get_relative_transform_to_parent", "parent"), &Node2D::get_relative_transform_to_parent);
  407. ADD_GROUP("Transform", "");
  408. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_RANGE, "-99999,99999,0.001,or_less,or_greater,hide_slider,suffix:px"), "set_position", "get_position");
  409. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_less,or_greater,radians_as_degrees"), "set_rotation", "get_rotation");
  410. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_rotation_degrees", "get_rotation_degrees");
  411. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale", PROPERTY_HINT_LINK), "set_scale", "get_scale");
  412. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "skew", PROPERTY_HINT_RANGE, "-89.9,89.9,0.1,radians_as_degrees"), "set_skew", "get_skew");
  413. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NONE), "set_transform", "get_transform");
  414. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NONE), "set_global_position", "get_global_position");
  415. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "global_rotation", PROPERTY_HINT_NONE, "radians_as_degrees", PROPERTY_USAGE_NONE), "set_global_rotation", "get_global_rotation");
  416. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "global_rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_rotation_degrees", "get_global_rotation_degrees");
  417. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_scale", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_scale", "get_global_scale");
  418. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "global_skew", PROPERTY_HINT_NONE, "radians_as_degrees", PROPERTY_USAGE_NONE), "set_global_skew", "get_global_skew");
  419. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "global_transform", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NONE), "set_global_transform", "get_global_transform");
  420. }