node_2d.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*************************************************************************/
  2. /* node_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "node_2d.h"
  31. #include "core/message_queue.h"
  32. #include "scene/gui/control.h"
  33. #include "scene/main/viewport.h"
  34. #include "servers/visual_server.h"
  35. #ifdef TOOLS_ENABLED
  36. Dictionary Node2D::_edit_get_state() const {
  37. Dictionary state;
  38. state["position"] = get_position();
  39. state["rotation"] = get_rotation();
  40. state["scale"] = get_scale();
  41. return state;
  42. }
  43. void Node2D::_edit_set_state(const Dictionary &p_state) {
  44. pos = p_state["position"];
  45. angle = p_state["rotation"];
  46. _scale = p_state["scale"];
  47. _update_transform();
  48. _change_notify("rotation");
  49. _change_notify("rotation_degrees");
  50. _change_notify("scale");
  51. _change_notify("position");
  52. }
  53. void Node2D::_edit_set_position(const Point2 &p_position) {
  54. set_position(p_position);
  55. }
  56. Point2 Node2D::_edit_get_position() const {
  57. return pos;
  58. }
  59. void Node2D::_edit_set_scale(const Size2 &p_scale) {
  60. set_scale(p_scale);
  61. }
  62. Size2 Node2D::_edit_get_scale() const {
  63. return _scale;
  64. }
  65. void Node2D::_edit_set_rotation(float p_rotation) {
  66. angle = p_rotation;
  67. _update_transform();
  68. _change_notify("rotation");
  69. _change_notify("rotation_degrees");
  70. }
  71. float Node2D::_edit_get_rotation() const {
  72. return angle;
  73. }
  74. bool Node2D::_edit_use_rotation() const {
  75. return true;
  76. }
  77. void Node2D::_edit_set_rect(const Rect2 &p_edit_rect) {
  78. ERR_FAIL_COND(!_edit_use_rect());
  79. Rect2 r = _edit_get_rect();
  80. Vector2 zero_offset;
  81. if (r.size.x != 0)
  82. zero_offset.x = -r.position.x / r.size.x;
  83. if (r.size.y != 0)
  84. zero_offset.y = -r.position.y / r.size.y;
  85. Size2 new_scale(1, 1);
  86. if (r.size.x != 0)
  87. new_scale.x = p_edit_rect.size.x / r.size.x;
  88. if (r.size.y != 0)
  89. new_scale.y = p_edit_rect.size.y / r.size.y;
  90. Point2 new_pos = p_edit_rect.position + p_edit_rect.size * zero_offset;
  91. Transform2D postxf;
  92. postxf.set_rotation_and_scale(angle, _scale);
  93. new_pos = postxf.xform(new_pos);
  94. pos += new_pos;
  95. _scale *= new_scale;
  96. _update_transform();
  97. _change_notify("scale");
  98. _change_notify("position");
  99. }
  100. #endif
  101. void Node2D::_update_xform_values() {
  102. pos = _mat.elements[2];
  103. angle = _mat.get_rotation();
  104. _scale = _mat.get_scale();
  105. _xform_dirty = false;
  106. }
  107. void Node2D::_update_transform() {
  108. _mat.set_rotation_and_scale(angle, _scale);
  109. _mat.elements[2] = pos;
  110. VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), _mat);
  111. if (!is_inside_tree())
  112. return;
  113. _notify_transform();
  114. }
  115. void Node2D::set_position(const Point2 &p_pos) {
  116. if (_xform_dirty)
  117. ((Node2D *)this)->_update_xform_values();
  118. pos = p_pos;
  119. _update_transform();
  120. _change_notify("position");
  121. }
  122. void Node2D::set_rotation(float p_radians) {
  123. if (_xform_dirty)
  124. ((Node2D *)this)->_update_xform_values();
  125. angle = p_radians;
  126. _update_transform();
  127. _change_notify("rotation");
  128. _change_notify("rotation_degrees");
  129. }
  130. void Node2D::set_rotation_degrees(float p_degrees) {
  131. set_rotation(Math::deg2rad(p_degrees));
  132. }
  133. void Node2D::set_scale(const Size2 &p_scale) {
  134. if (_xform_dirty)
  135. ((Node2D *)this)->_update_xform_values();
  136. _scale = p_scale;
  137. // Avoid having 0 scale values, can lead to errors in physics and rendering.
  138. if (Math::is_zero_approx(_scale.x)) {
  139. _scale.x = CMP_EPSILON;
  140. }
  141. if (Math::is_zero_approx(_scale.y)) {
  142. _scale.y = CMP_EPSILON;
  143. }
  144. _update_transform();
  145. _change_notify("scale");
  146. }
  147. Point2 Node2D::get_position() const {
  148. if (_xform_dirty)
  149. ((Node2D *)this)->_update_xform_values();
  150. return pos;
  151. }
  152. float Node2D::get_rotation() const {
  153. if (_xform_dirty)
  154. ((Node2D *)this)->_update_xform_values();
  155. return angle;
  156. }
  157. float Node2D::get_rotation_degrees() const {
  158. return Math::rad2deg(get_rotation());
  159. }
  160. Size2 Node2D::get_scale() const {
  161. if (_xform_dirty)
  162. ((Node2D *)this)->_update_xform_values();
  163. return _scale;
  164. }
  165. Transform2D Node2D::get_transform() const {
  166. return _mat;
  167. }
  168. void Node2D::rotate(float p_radians) {
  169. set_rotation(get_rotation() + p_radians);
  170. }
  171. void Node2D::translate(const Vector2 &p_amount) {
  172. set_position(get_position() + p_amount);
  173. }
  174. void Node2D::global_translate(const Vector2 &p_amount) {
  175. set_global_position(get_global_position() + p_amount);
  176. }
  177. void Node2D::apply_scale(const Size2 &p_amount) {
  178. set_scale(get_scale() * p_amount);
  179. }
  180. void Node2D::move_x(float p_delta, bool p_scaled) {
  181. Transform2D t = get_transform();
  182. Vector2 m = t[0];
  183. if (!p_scaled)
  184. m.normalize();
  185. set_position(t[2] + m * p_delta);
  186. }
  187. void Node2D::move_y(float p_delta, bool p_scaled) {
  188. Transform2D t = get_transform();
  189. Vector2 m = t[1];
  190. if (!p_scaled)
  191. m.normalize();
  192. set_position(t[2] + m * p_delta);
  193. }
  194. Point2 Node2D::get_global_position() const {
  195. return get_global_transform().get_origin();
  196. }
  197. void Node2D::set_global_position(const Point2 &p_pos) {
  198. Transform2D inv;
  199. CanvasItem *pi = get_parent_item();
  200. if (pi) {
  201. inv = pi->get_global_transform().affine_inverse();
  202. set_position(inv.xform(p_pos));
  203. } else {
  204. set_position(p_pos);
  205. }
  206. }
  207. float Node2D::get_global_rotation() const {
  208. return get_global_transform().get_rotation();
  209. }
  210. void Node2D::set_global_rotation(float p_radians) {
  211. CanvasItem *pi = get_parent_item();
  212. if (pi) {
  213. const float parent_global_rot = pi->get_global_transform().get_rotation();
  214. set_rotation(p_radians - parent_global_rot);
  215. } else {
  216. set_rotation(p_radians);
  217. }
  218. }
  219. float Node2D::get_global_rotation_degrees() const {
  220. return Math::rad2deg(get_global_rotation());
  221. }
  222. void Node2D::set_global_rotation_degrees(float p_degrees) {
  223. set_global_rotation(Math::deg2rad(p_degrees));
  224. }
  225. Size2 Node2D::get_global_scale() const {
  226. return get_global_transform().get_scale();
  227. }
  228. void Node2D::set_global_scale(const Size2 &p_scale) {
  229. CanvasItem *pi = get_parent_item();
  230. if (pi) {
  231. const Size2 parent_global_scale = pi->get_global_transform().get_scale();
  232. set_scale(p_scale / parent_global_scale);
  233. } else {
  234. set_scale(p_scale);
  235. }
  236. }
  237. void Node2D::set_transform(const Transform2D &p_transform) {
  238. _mat = p_transform;
  239. _xform_dirty = true;
  240. VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), _mat);
  241. if (!is_inside_tree())
  242. return;
  243. _notify_transform();
  244. }
  245. void Node2D::set_global_transform(const Transform2D &p_transform) {
  246. CanvasItem *pi = get_parent_item();
  247. if (pi)
  248. set_transform(pi->get_global_transform().affine_inverse() * p_transform);
  249. else
  250. set_transform(p_transform);
  251. }
  252. void Node2D::set_z_index(int p_z) {
  253. ERR_FAIL_COND(p_z < VS::CANVAS_ITEM_Z_MIN);
  254. ERR_FAIL_COND(p_z > VS::CANVAS_ITEM_Z_MAX);
  255. z_index = p_z;
  256. VS::get_singleton()->canvas_item_set_z_index(get_canvas_item(), z_index);
  257. _change_notify("z_index");
  258. }
  259. void Node2D::set_z_as_relative(bool p_enabled) {
  260. if (z_relative == p_enabled)
  261. return;
  262. z_relative = p_enabled;
  263. VS::get_singleton()->canvas_item_set_z_as_relative_to_parent(get_canvas_item(), p_enabled);
  264. }
  265. bool Node2D::is_z_relative() const {
  266. return z_relative;
  267. }
  268. int Node2D::get_z_index() const {
  269. return z_index;
  270. }
  271. Transform2D Node2D::get_relative_transform_to_parent(const Node *p_parent) const {
  272. if (p_parent == this)
  273. return Transform2D();
  274. Node2D *parent_2d = Object::cast_to<Node2D>(get_parent());
  275. ERR_FAIL_COND_V(!parent_2d, Transform2D());
  276. if (p_parent == parent_2d)
  277. return get_transform();
  278. else
  279. return parent_2d->get_relative_transform_to_parent(p_parent) * get_transform();
  280. }
  281. void Node2D::look_at(const Vector2 &p_pos) {
  282. rotate(get_angle_to(p_pos));
  283. }
  284. float Node2D::get_angle_to(const Vector2 &p_pos) const {
  285. return (to_local(p_pos) * get_scale()).angle();
  286. }
  287. Point2 Node2D::to_local(Point2 p_global) const {
  288. return get_global_transform().affine_inverse().xform(p_global);
  289. }
  290. Point2 Node2D::to_global(Point2 p_local) const {
  291. return get_global_transform().xform(p_local);
  292. }
  293. void Node2D::_bind_methods() {
  294. ClassDB::bind_method(D_METHOD("set_position", "position"), &Node2D::set_position);
  295. ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &Node2D::set_rotation);
  296. ClassDB::bind_method(D_METHOD("set_rotation_degrees", "degrees"), &Node2D::set_rotation_degrees);
  297. ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Node2D::set_scale);
  298. ClassDB::bind_method(D_METHOD("get_position"), &Node2D::get_position);
  299. ClassDB::bind_method(D_METHOD("get_rotation"), &Node2D::get_rotation);
  300. ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Node2D::get_rotation_degrees);
  301. ClassDB::bind_method(D_METHOD("get_scale"), &Node2D::get_scale);
  302. ClassDB::bind_method(D_METHOD("rotate", "radians"), &Node2D::rotate);
  303. ClassDB::bind_method(D_METHOD("move_local_x", "delta", "scaled"), &Node2D::move_x, DEFVAL(false));
  304. ClassDB::bind_method(D_METHOD("move_local_y", "delta", "scaled"), &Node2D::move_y, DEFVAL(false));
  305. ClassDB::bind_method(D_METHOD("translate", "offset"), &Node2D::translate);
  306. ClassDB::bind_method(D_METHOD("global_translate", "offset"), &Node2D::global_translate);
  307. ClassDB::bind_method(D_METHOD("apply_scale", "ratio"), &Node2D::apply_scale);
  308. ClassDB::bind_method(D_METHOD("set_global_position", "position"), &Node2D::set_global_position);
  309. ClassDB::bind_method(D_METHOD("get_global_position"), &Node2D::get_global_position);
  310. ClassDB::bind_method(D_METHOD("set_global_rotation", "radians"), &Node2D::set_global_rotation);
  311. ClassDB::bind_method(D_METHOD("get_global_rotation"), &Node2D::get_global_rotation);
  312. ClassDB::bind_method(D_METHOD("set_global_rotation_degrees", "degrees"), &Node2D::set_global_rotation_degrees);
  313. ClassDB::bind_method(D_METHOD("get_global_rotation_degrees"), &Node2D::get_global_rotation_degrees);
  314. ClassDB::bind_method(D_METHOD("set_global_scale", "scale"), &Node2D::set_global_scale);
  315. ClassDB::bind_method(D_METHOD("get_global_scale"), &Node2D::get_global_scale);
  316. ClassDB::bind_method(D_METHOD("set_transform", "xform"), &Node2D::set_transform);
  317. ClassDB::bind_method(D_METHOD("set_global_transform", "xform"), &Node2D::set_global_transform);
  318. ClassDB::bind_method(D_METHOD("look_at", "point"), &Node2D::look_at);
  319. ClassDB::bind_method(D_METHOD("get_angle_to", "point"), &Node2D::get_angle_to);
  320. ClassDB::bind_method(D_METHOD("to_local", "global_point"), &Node2D::to_local);
  321. ClassDB::bind_method(D_METHOD("to_global", "local_point"), &Node2D::to_global);
  322. ClassDB::bind_method(D_METHOD("set_z_index", "z_index"), &Node2D::set_z_index);
  323. ClassDB::bind_method(D_METHOD("get_z_index"), &Node2D::get_z_index);
  324. ClassDB::bind_method(D_METHOD("set_z_as_relative", "enable"), &Node2D::set_z_as_relative);
  325. ClassDB::bind_method(D_METHOD("is_z_relative"), &Node2D::is_z_relative);
  326. ClassDB::bind_method(D_METHOD("get_relative_transform_to_parent", "parent"), &Node2D::get_relative_transform_to_parent);
  327. ADD_GROUP("Transform", "");
  328. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  329. ADD_PROPERTY(PropertyInfo(Variant::REAL, "rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_rotation", "get_rotation");
  330. ADD_PROPERTY(PropertyInfo(Variant::REAL, "rotation_degrees", PROPERTY_HINT_RANGE, "-360,360,0.1,or_lesser,or_greater", PROPERTY_USAGE_EDITOR), "set_rotation_degrees", "get_rotation_degrees");
  331. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale"), "set_scale", "get_scale");
  332. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform", PROPERTY_HINT_NONE, "", 0), "set_transform", "get_transform");
  333. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position", PROPERTY_HINT_NONE, "", 0), "set_global_position", "get_global_position");
  334. ADD_PROPERTY(PropertyInfo(Variant::REAL, "global_rotation", PROPERTY_HINT_NONE, "", 0), "set_global_rotation", "get_global_rotation");
  335. ADD_PROPERTY(PropertyInfo(Variant::REAL, "global_rotation_degrees", PROPERTY_HINT_NONE, "", 0), "set_global_rotation_degrees", "get_global_rotation_degrees");
  336. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_scale", PROPERTY_HINT_NONE, "", 0), "set_global_scale", "get_global_scale");
  337. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "global_transform", PROPERTY_HINT_NONE, "", 0), "set_global_transform", "get_global_transform");
  338. ADD_GROUP("Z Index", "");
  339. ADD_PROPERTY(PropertyInfo(Variant::INT, "z_index", PROPERTY_HINT_RANGE, itos(VS::CANVAS_ITEM_Z_MIN) + "," + itos(VS::CANVAS_ITEM_Z_MAX) + ",1"), "set_z_index", "get_z_index");
  340. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "z_as_relative"), "set_z_as_relative", "is_z_relative");
  341. }
  342. Node2D::Node2D() {
  343. angle = 0;
  344. _scale = Vector2(1, 1);
  345. _xform_dirty = false;
  346. z_index = 0;
  347. z_relative = true;
  348. }