node_2d.cpp 14 KB

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