node_2d.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*************************************************************************/
  2. /* node_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "message_queue.h"
  32. #include "scene/gui/control.h"
  33. #include "scene/main/viewport.h"
  34. #include "servers/visual_server.h"
  35. void Node2D::edit_set_pivot(const Point2 &p_pivot) {
  36. }
  37. Point2 Node2D::edit_get_pivot() const {
  38. return Point2();
  39. }
  40. bool Node2D::edit_has_pivot() const {
  41. return false;
  42. }
  43. Variant Node2D::edit_get_state() const {
  44. Array state;
  45. state.push_back(get_position());
  46. state.push_back(get_rotation());
  47. state.push_back(get_scale());
  48. return state;
  49. }
  50. void Node2D::edit_set_state(const Variant &p_state) {
  51. Array state = p_state;
  52. ERR_FAIL_COND(state.size() != 3);
  53. pos = state[0];
  54. angle = state[1];
  55. _scale = state[2];
  56. _update_transform();
  57. _change_notify("rotation");
  58. _change_notify("rotation_deg");
  59. _change_notify("scale");
  60. _change_notify("position");
  61. }
  62. void Node2D::edit_set_rect(const Rect2 &p_edit_rect) {
  63. Rect2 r = get_item_rect();
  64. Vector2 zero_offset;
  65. if (r.size.x != 0)
  66. zero_offset.x = -r.position.x / r.size.x;
  67. if (r.size.y != 0)
  68. zero_offset.y = -r.position.y / r.size.y;
  69. Size2 new_scale(1, 1);
  70. if (r.size.x != 0)
  71. new_scale.x = p_edit_rect.size.x / r.size.x;
  72. if (r.size.y != 0)
  73. new_scale.y = p_edit_rect.size.y / r.size.y;
  74. Point2 new_pos = p_edit_rect.position + p_edit_rect.size * zero_offset; //p_edit_rect.pos - r.pos;
  75. Transform2D postxf;
  76. postxf.set_rotation_and_scale(angle, _scale);
  77. new_pos = postxf.xform(new_pos);
  78. pos += new_pos;
  79. _scale *= new_scale;
  80. _update_transform();
  81. _change_notify("scale");
  82. _change_notify("position");
  83. }
  84. void Node2D::edit_rotate(float p_rot) {
  85. angle += p_rot;
  86. _update_transform();
  87. _change_notify("rotation");
  88. _change_notify("rotation_deg");
  89. }
  90. void Node2D::_update_xform_values() {
  91. pos = _mat.elements[2];
  92. angle = _mat.get_rotation();
  93. _scale = _mat.get_scale();
  94. _xform_dirty = false;
  95. }
  96. void Node2D::_update_transform() {
  97. Transform2D mat(angle, pos);
  98. _mat.set_rotation_and_scale(angle, _scale);
  99. _mat.elements[2] = pos;
  100. VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), _mat);
  101. if (!is_inside_tree())
  102. return;
  103. _notify_transform();
  104. }
  105. void Node2D::set_position(const Point2 &p_pos) {
  106. if (_xform_dirty)
  107. ((Node2D *)this)->_update_xform_values();
  108. pos = p_pos;
  109. _update_transform();
  110. _change_notify("position");
  111. }
  112. void Node2D::set_rotation(float p_radians) {
  113. if (_xform_dirty)
  114. ((Node2D *)this)->_update_xform_values();
  115. angle = p_radians;
  116. _update_transform();
  117. _change_notify("rotation");
  118. _change_notify("rotation_deg");
  119. }
  120. void Node2D::set_rotation_in_degrees(float p_degrees) {
  121. set_rotation(Math::deg2rad(p_degrees));
  122. }
  123. // Kept for compatibility after rename to set_rotd.
  124. // Could be removed after a couple releases.
  125. void Node2D::_set_rotd(float p_degrees) {
  126. WARN_PRINT("Deprecated method Node2D._set_rotd(): This method was renamed to set_rotd. Please adapt your code accordingly, as the old method will be obsoleted.");
  127. set_rotation_in_degrees(p_degrees);
  128. }
  129. void Node2D::set_scale(const Size2 &p_scale) {
  130. if (_xform_dirty)
  131. ((Node2D *)this)->_update_xform_values();
  132. _scale = p_scale;
  133. if (_scale.x == 0)
  134. _scale.x = CMP_EPSILON;
  135. if (_scale.y == 0)
  136. _scale.y = CMP_EPSILON;
  137. _update_transform();
  138. _change_notify("scale");
  139. }
  140. Point2 Node2D::get_position() const {
  141. if (_xform_dirty)
  142. ((Node2D *)this)->_update_xform_values();
  143. return pos;
  144. }
  145. float Node2D::get_rotation() const {
  146. if (_xform_dirty)
  147. ((Node2D *)this)->_update_xform_values();
  148. return angle;
  149. }
  150. float Node2D::get_rotation_in_degrees() const {
  151. return Math::rad2deg(get_rotation());
  152. }
  153. // Kept for compatibility after rename to get_rotd.
  154. // Could be removed after a couple releases.
  155. float Node2D::_get_rotd() const {
  156. WARN_PRINT("Deprecated method Node2D._get_rotd(): This method was renamed to get_rotd. Please adapt your code accordingly, as the old method will be obsoleted.");
  157. return get_rotation_in_degrees();
  158. }
  159. Size2 Node2D::get_scale() const {
  160. if (_xform_dirty)
  161. ((Node2D *)this)->_update_xform_values();
  162. return _scale;
  163. }
  164. void Node2D::_notification(int p_what) {
  165. switch (p_what) {
  166. }
  167. }
  168. Transform2D Node2D::get_transform() const {
  169. return _mat;
  170. }
  171. Rect2 Node2D::get_item_rect() const {
  172. if (get_script_instance()) {
  173. Variant::CallError err;
  174. Rect2 r = get_script_instance()->call("_get_item_rect", NULL, 0, err);
  175. if (err.error == Variant::CallError::CALL_OK)
  176. return r;
  177. }
  178. return Rect2(Point2(-32, -32), Size2(64, 64));
  179. }
  180. void Node2D::rotate(float p_radians) {
  181. set_rotation(get_rotation() + p_radians);
  182. }
  183. void Node2D::translate(const Vector2 &p_amount) {
  184. set_position(get_position() + p_amount);
  185. }
  186. void Node2D::global_translate(const Vector2 &p_amount) {
  187. set_global_position(get_global_position() + p_amount);
  188. }
  189. void Node2D::apply_scale(const Size2 &p_amount) {
  190. set_scale(get_scale() * p_amount);
  191. }
  192. void Node2D::move_x(float p_delta, bool p_scaled) {
  193. Transform2D t = get_transform();
  194. Vector2 m = t[0];
  195. if (!p_scaled)
  196. m.normalize();
  197. set_position(t[2] + m * p_delta);
  198. }
  199. void Node2D::move_y(float p_delta, bool p_scaled) {
  200. Transform2D t = get_transform();
  201. Vector2 m = t[1];
  202. if (!p_scaled)
  203. m.normalize();
  204. set_position(t[2] + m * p_delta);
  205. }
  206. Point2 Node2D::get_global_position() const {
  207. return get_global_transform().get_origin();
  208. }
  209. void Node2D::set_global_position(const Point2 &p_pos) {
  210. Transform2D inv;
  211. CanvasItem *pi = get_parent_item();
  212. if (pi) {
  213. inv = pi->get_global_transform().affine_inverse();
  214. set_position(inv.xform(p_pos));
  215. } else {
  216. set_position(p_pos);
  217. }
  218. }
  219. float Node2D::get_global_rotation() const {
  220. return get_global_transform().get_rotation();
  221. }
  222. void Node2D::set_global_rotation(float p_radians) {
  223. CanvasItem *pi = get_parent_item();
  224. if (pi) {
  225. const float parent_global_rot = pi->get_global_transform().get_rotation();
  226. set_rotation(p_radians - parent_global_rot);
  227. } else {
  228. set_rotation(p_radians);
  229. }
  230. }
  231. float Node2D::get_global_rotation_in_degrees() const {
  232. return Math::rad2deg(get_global_rotation());
  233. }
  234. void Node2D::set_global_rotation_in_degrees(float p_degrees) {
  235. set_global_rotation(Math::deg2rad(p_degrees));
  236. }
  237. Size2 Node2D::get_global_scale() const {
  238. return get_global_transform().get_scale();
  239. }
  240. void Node2D::set_global_scale(const Size2 &p_scale) {
  241. CanvasItem *pi = get_parent_item();
  242. if (pi) {
  243. const Size2 parent_global_scale = pi->get_global_transform().get_scale();
  244. set_scale(p_scale / parent_global_scale);
  245. } else {
  246. set_scale(p_scale);
  247. }
  248. }
  249. void Node2D::set_transform(const Transform2D &p_transform) {
  250. _mat = p_transform;
  251. _xform_dirty = true;
  252. VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), _mat);
  253. if (!is_inside_tree())
  254. return;
  255. _notify_transform();
  256. }
  257. void Node2D::set_global_transform(const Transform2D &p_transform) {
  258. CanvasItem *pi = get_parent_item();
  259. if (pi)
  260. set_transform(pi->get_global_transform().affine_inverse() * p_transform);
  261. else
  262. set_transform(p_transform);
  263. }
  264. void Node2D::set_z(int p_z) {
  265. ERR_FAIL_COND(p_z < VS::CANVAS_ITEM_Z_MIN);
  266. ERR_FAIL_COND(p_z > VS::CANVAS_ITEM_Z_MAX);
  267. z = p_z;
  268. VS::get_singleton()->canvas_item_set_z(get_canvas_item(), z);
  269. _change_notify("z");
  270. }
  271. void Node2D::set_z_as_relative(bool p_enabled) {
  272. if (z_relative == p_enabled)
  273. return;
  274. z_relative = p_enabled;
  275. VS::get_singleton()->canvas_item_set_z_as_relative_to_parent(get_canvas_item(), p_enabled);
  276. }
  277. bool Node2D::is_z_relative() const {
  278. return z_relative;
  279. }
  280. int Node2D::get_z() const {
  281. return z;
  282. }
  283. Transform2D Node2D::get_relative_transform_to_parent(const Node *p_parent) const {
  284. if (p_parent == this)
  285. return Transform2D();
  286. Node2D *parent_2d = Object::cast_to<Node2D>(get_parent());
  287. ERR_FAIL_COND_V(!parent_2d, Transform2D());
  288. if (p_parent == parent_2d)
  289. return get_transform();
  290. else
  291. return parent_2d->get_relative_transform_to_parent(p_parent) * get_transform();
  292. }
  293. void Node2D::look_at(const Vector2 &p_pos) {
  294. rotate(get_angle_to(p_pos));
  295. }
  296. float Node2D::get_angle_to(const Vector2 &p_pos) const {
  297. return (get_global_transform().affine_inverse().xform(p_pos)).angle();
  298. }
  299. Point2 Node2D::to_local(Point2 p_global) const {
  300. return get_global_transform().affine_inverse().xform(p_global);
  301. }
  302. Point2 Node2D::to_global(Point2 p_local) const {
  303. return get_global_transform().xform(p_local);
  304. }
  305. void Node2D::_bind_methods() {
  306. // TODO: Obsolete those two methods (old name) properly (GH-4397)
  307. ClassDB::bind_method(D_METHOD("_get_rotd"), &Node2D::_get_rotd);
  308. ClassDB::bind_method(D_METHOD("_set_rotd", "degrees"), &Node2D::_set_rotd);
  309. ClassDB::bind_method(D_METHOD("set_position", "position"), &Node2D::set_position);
  310. ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &Node2D::set_rotation);
  311. ClassDB::bind_method(D_METHOD("set_rotation_in_degrees", "degrees"), &Node2D::set_rotation_in_degrees);
  312. ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Node2D::set_scale);
  313. ClassDB::bind_method(D_METHOD("get_position"), &Node2D::get_position);
  314. ClassDB::bind_method(D_METHOD("get_rotation"), &Node2D::get_rotation);
  315. ClassDB::bind_method(D_METHOD("get_rotation_in_degrees"), &Node2D::get_rotation_in_degrees);
  316. ClassDB::bind_method(D_METHOD("get_scale"), &Node2D::get_scale);
  317. ClassDB::bind_method(D_METHOD("rotate", "radians"), &Node2D::rotate);
  318. ClassDB::bind_method(D_METHOD("move_local_x", "delta", "scaled"), &Node2D::move_x, DEFVAL(false));
  319. ClassDB::bind_method(D_METHOD("move_local_y", "delta", "scaled"), &Node2D::move_y, DEFVAL(false));
  320. ClassDB::bind_method(D_METHOD("translate", "offset"), &Node2D::translate);
  321. ClassDB::bind_method(D_METHOD("global_translate", "offset"), &Node2D::global_translate);
  322. ClassDB::bind_method(D_METHOD("apply_scale", "ratio"), &Node2D::apply_scale);
  323. ClassDB::bind_method(D_METHOD("set_global_position", "position"), &Node2D::set_global_position);
  324. ClassDB::bind_method(D_METHOD("get_global_position"), &Node2D::get_global_position);
  325. ClassDB::bind_method(D_METHOD("set_global_rotation", "radians"), &Node2D::set_global_rotation);
  326. ClassDB::bind_method(D_METHOD("get_global_rotation"), &Node2D::get_global_rotation);
  327. ClassDB::bind_method(D_METHOD("set_global_rotation_in_degrees", "degrees"), &Node2D::set_global_rotation_in_degrees);
  328. ClassDB::bind_method(D_METHOD("get_global_rotation_in_degrees"), &Node2D::get_global_rotation_in_degrees);
  329. ClassDB::bind_method(D_METHOD("set_global_scale", "scale"), &Node2D::set_global_scale);
  330. ClassDB::bind_method(D_METHOD("get_global_scale"), &Node2D::get_global_scale);
  331. ClassDB::bind_method(D_METHOD("set_transform", "xform"), &Node2D::set_transform);
  332. ClassDB::bind_method(D_METHOD("set_global_transform", "xform"), &Node2D::set_global_transform);
  333. ClassDB::bind_method(D_METHOD("look_at", "point"), &Node2D::look_at);
  334. ClassDB::bind_method(D_METHOD("get_angle_to", "point"), &Node2D::get_angle_to);
  335. ClassDB::bind_method(D_METHOD("to_local", "global_point"), &Node2D::to_local);
  336. ClassDB::bind_method(D_METHOD("to_global", "local_point"), &Node2D::to_global);
  337. ClassDB::bind_method(D_METHOD("set_z", "z"), &Node2D::set_z);
  338. ClassDB::bind_method(D_METHOD("get_z"), &Node2D::get_z);
  339. ClassDB::bind_method(D_METHOD("set_z_as_relative", "enable"), &Node2D::set_z_as_relative);
  340. ClassDB::bind_method(D_METHOD("is_z_relative"), &Node2D::is_z_relative);
  341. ClassDB::bind_method(D_METHOD("edit_set_pivot", "pivot"), &Node2D::edit_set_pivot);
  342. ClassDB::bind_method(D_METHOD("get_relative_transform_to_parent", "parent"), &Node2D::get_relative_transform_to_parent);
  343. ADD_GROUP("Transform", "");
  344. ADD_PROPERTYNZ(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
  345. ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_rotation", "get_rotation");
  346. ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "rotation_deg", PROPERTY_HINT_RANGE, "-1440,1440,0.1", PROPERTY_USAGE_EDITOR), "set_rotation_in_degrees", "get_rotation_in_degrees");
  347. ADD_PROPERTYNO(PropertyInfo(Variant::VECTOR2, "scale"), "set_scale", "get_scale");
  348. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform", PROPERTY_HINT_NONE, "", 0), "set_transform", "get_transform");
  349. ADD_PROPERTY(PropertyInfo(Variant::REAL, "global_position", PROPERTY_HINT_NONE, "", 0), "set_global_position", "get_global_position");
  350. ADD_PROPERTY(PropertyInfo(Variant::REAL, "global_rotation", PROPERTY_HINT_NONE, "", 0), "set_global_rotation", "get_global_rotation");
  351. ADD_PROPERTY(PropertyInfo(Variant::REAL, "global_rotation_deg", PROPERTY_HINT_NONE, "", 0), "set_global_rotation_in_degrees", "get_global_rotation_in_degrees");
  352. ADD_PROPERTY(PropertyInfo(Variant::REAL, "global_scale", PROPERTY_HINT_NONE, "", 0), "set_global_scale", "get_global_scale");
  353. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "global_transform", PROPERTY_HINT_NONE, "", 0), "set_global_transform", "get_global_transform");
  354. ADD_GROUP("Z", "");
  355. ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "z", PROPERTY_HINT_RANGE, itos(VS::CANVAS_ITEM_Z_MIN) + "," + itos(VS::CANVAS_ITEM_Z_MAX) + ",1"), "set_z", "get_z");
  356. ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "z_as_relative"), "set_z_as_relative", "is_z_relative");
  357. }
  358. Node2D::Node2D() {
  359. angle = 0;
  360. _scale = Vector2(1, 1);
  361. _xform_dirty = false;
  362. z = 0;
  363. z_relative = true;
  364. }