1 /** 2 A 2D game object, inherited by all 2D-related nodes. Has a position, rotation, scale, and Z index. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.node2d; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.traits; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.globalenums; 22 import godot.object; 23 import godot.classdb; 24 import godot.canvasitem; 25 import godot.node; 26 /** 27 A 2D game object, inherited by all 2D-related nodes. Has a position, rotation, scale, and Z index. 28 29 A 2D game object, with a transform (position, rotation, and scale). All 2D nodes, including physics objects and sprites, inherit from Node2D. Use Node2D as a parent node to move, scale and rotate children in a 2D project. Also gives control of the node's render order. 30 */ 31 @GodotBaseClass struct Node2D 32 { 33 package(godot) enum string _GODOT_internal_name = "Node2D"; 34 public: 35 @nogc nothrow: 36 union { /** */ godot_object _godot_object; /** */ CanvasItem _GODOT_base; } 37 alias _GODOT_base this; 38 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 39 package(godot) __gshared bool _classBindingInitialized = false; 40 package(godot) static struct GDNativeClassBinding 41 { 42 __gshared: 43 @GodotName("apply_scale") GodotMethod!(void, Vector2) applyScale; 44 @GodotName("get_angle_to") GodotMethod!(double, Vector2) getAngleTo; 45 @GodotName("get_global_position") GodotMethod!(Vector2) getGlobalPosition; 46 @GodotName("get_global_rotation") GodotMethod!(double) getGlobalRotation; 47 @GodotName("get_global_rotation_degrees") GodotMethod!(double) getGlobalRotationDegrees; 48 @GodotName("get_global_scale") GodotMethod!(Vector2) getGlobalScale; 49 @GodotName("get_position") GodotMethod!(Vector2) getPosition; 50 @GodotName("get_relative_transform_to_parent") GodotMethod!(Transform2D, Node) getRelativeTransformToParent; 51 @GodotName("get_rotation") GodotMethod!(double) getRotation; 52 @GodotName("get_rotation_degrees") GodotMethod!(double) getRotationDegrees; 53 @GodotName("get_scale") GodotMethod!(Vector2) getScale; 54 @GodotName("get_z_index") GodotMethod!(long) getZIndex; 55 @GodotName("global_translate") GodotMethod!(void, Vector2) globalTranslate; 56 @GodotName("is_z_relative") GodotMethod!(bool) isZRelative; 57 @GodotName("look_at") GodotMethod!(void, Vector2) lookAt; 58 @GodotName("move_local_x") GodotMethod!(void, double, bool) moveLocalX; 59 @GodotName("move_local_y") GodotMethod!(void, double, bool) moveLocalY; 60 @GodotName("rotate") GodotMethod!(void, double) rotate; 61 @GodotName("set_global_position") GodotMethod!(void, Vector2) setGlobalPosition; 62 @GodotName("set_global_rotation") GodotMethod!(void, double) setGlobalRotation; 63 @GodotName("set_global_rotation_degrees") GodotMethod!(void, double) setGlobalRotationDegrees; 64 @GodotName("set_global_scale") GodotMethod!(void, Vector2) setGlobalScale; 65 @GodotName("set_global_transform") GodotMethod!(void, Transform2D) setGlobalTransform; 66 @GodotName("set_position") GodotMethod!(void, Vector2) setPosition; 67 @GodotName("set_rotation") GodotMethod!(void, double) setRotation; 68 @GodotName("set_rotation_degrees") GodotMethod!(void, double) setRotationDegrees; 69 @GodotName("set_scale") GodotMethod!(void, Vector2) setScale; 70 @GodotName("set_transform") GodotMethod!(void, Transform2D) setTransform; 71 @GodotName("set_z_as_relative") GodotMethod!(void, bool) setZAsRelative; 72 @GodotName("set_z_index") GodotMethod!(void, long) setZIndex; 73 @GodotName("to_global") GodotMethod!(Vector2, Vector2) toGlobal; 74 @GodotName("to_local") GodotMethod!(Vector2, Vector2) toLocal; 75 @GodotName("translate") GodotMethod!(void, Vector2) translate; 76 } 77 /// 78 pragma(inline, true) bool opEquals(in Node2D other) const 79 { return _godot_object.ptr is other._godot_object.ptr; } 80 /// 81 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 82 { _godot_object.ptr = n; return null; } 83 /// 84 pragma(inline, true) bool opEquals(typeof(null) n) const 85 { return _godot_object.ptr is n; } 86 /// 87 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 88 mixin baseCasts; 89 /// Construct a new instance of Node2D. 90 /// Note: use `memnew!Node2D` instead. 91 static Node2D _new() 92 { 93 static godot_class_constructor constructor; 94 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("Node2D"); 95 if(constructor is null) return typeof(this).init; 96 return cast(Node2D)(constructor()); 97 } 98 @disable new(size_t s); 99 /** 100 Multiplies the current scale by the `ratio` vector. 101 */ 102 void applyScale(in Vector2 ratio) 103 { 104 checkClassBinding!(typeof(this))(); 105 ptrcall!(void)(GDNativeClassBinding.applyScale, _godot_object, ratio); 106 } 107 /** 108 Returns the angle between the node and the `point` in radians. 109 */ 110 double getAngleTo(in Vector2 point) const 111 { 112 checkClassBinding!(typeof(this))(); 113 return ptrcall!(double)(GDNativeClassBinding.getAngleTo, _godot_object, point); 114 } 115 /** 116 117 */ 118 Vector2 getGlobalPosition() const 119 { 120 checkClassBinding!(typeof(this))(); 121 return ptrcall!(Vector2)(GDNativeClassBinding.getGlobalPosition, _godot_object); 122 } 123 /** 124 125 */ 126 double getGlobalRotation() const 127 { 128 checkClassBinding!(typeof(this))(); 129 return ptrcall!(double)(GDNativeClassBinding.getGlobalRotation, _godot_object); 130 } 131 /** 132 133 */ 134 double getGlobalRotationDegrees() const 135 { 136 checkClassBinding!(typeof(this))(); 137 return ptrcall!(double)(GDNativeClassBinding.getGlobalRotationDegrees, _godot_object); 138 } 139 /** 140 141 */ 142 Vector2 getGlobalScale() const 143 { 144 checkClassBinding!(typeof(this))(); 145 return ptrcall!(Vector2)(GDNativeClassBinding.getGlobalScale, _godot_object); 146 } 147 /** 148 149 */ 150 Vector2 getPosition() const 151 { 152 checkClassBinding!(typeof(this))(); 153 return ptrcall!(Vector2)(GDNativeClassBinding.getPosition, _godot_object); 154 } 155 /** 156 Returns the $(D Transform2D) relative to this node's parent. 157 */ 158 Transform2D getRelativeTransformToParent(Node parent) const 159 { 160 checkClassBinding!(typeof(this))(); 161 return ptrcall!(Transform2D)(GDNativeClassBinding.getRelativeTransformToParent, _godot_object, parent); 162 } 163 /** 164 165 */ 166 double getRotation() const 167 { 168 checkClassBinding!(typeof(this))(); 169 return ptrcall!(double)(GDNativeClassBinding.getRotation, _godot_object); 170 } 171 /** 172 173 */ 174 double getRotationDegrees() const 175 { 176 checkClassBinding!(typeof(this))(); 177 return ptrcall!(double)(GDNativeClassBinding.getRotationDegrees, _godot_object); 178 } 179 /** 180 181 */ 182 Vector2 getScale() const 183 { 184 checkClassBinding!(typeof(this))(); 185 return ptrcall!(Vector2)(GDNativeClassBinding.getScale, _godot_object); 186 } 187 /** 188 189 */ 190 long getZIndex() const 191 { 192 checkClassBinding!(typeof(this))(); 193 return ptrcall!(long)(GDNativeClassBinding.getZIndex, _godot_object); 194 } 195 /** 196 Adds the `offset` vector to the node's global position. 197 */ 198 void globalTranslate(in Vector2 offset) 199 { 200 checkClassBinding!(typeof(this))(); 201 ptrcall!(void)(GDNativeClassBinding.globalTranslate, _godot_object, offset); 202 } 203 /** 204 205 */ 206 bool isZRelative() const 207 { 208 checkClassBinding!(typeof(this))(); 209 return ptrcall!(bool)(GDNativeClassBinding.isZRelative, _godot_object); 210 } 211 /** 212 Rotates the node so it points towards the `point`, which is expected to use global coordinates. 213 */ 214 void lookAt(in Vector2 point) 215 { 216 checkClassBinding!(typeof(this))(); 217 ptrcall!(void)(GDNativeClassBinding.lookAt, _godot_object, point); 218 } 219 /** 220 Applies a local translation on the node's X axis based on the $(D Node._process)'s `delta`. If `scaled` is `false`, normalizes the movement. 221 */ 222 void moveLocalX(in double delta, in bool scaled = false) 223 { 224 checkClassBinding!(typeof(this))(); 225 ptrcall!(void)(GDNativeClassBinding.moveLocalX, _godot_object, delta, scaled); 226 } 227 /** 228 Applies a local translation on the node's Y axis based on the $(D Node._process)'s `delta`. If `scaled` is `false`, normalizes the movement. 229 */ 230 void moveLocalY(in double delta, in bool scaled = false) 231 { 232 checkClassBinding!(typeof(this))(); 233 ptrcall!(void)(GDNativeClassBinding.moveLocalY, _godot_object, delta, scaled); 234 } 235 /** 236 Applies a rotation to the node, in radians, starting from its current rotation. 237 */ 238 void rotate(in double radians) 239 { 240 checkClassBinding!(typeof(this))(); 241 ptrcall!(void)(GDNativeClassBinding.rotate, _godot_object, radians); 242 } 243 /** 244 245 */ 246 void setGlobalPosition(in Vector2 position) 247 { 248 checkClassBinding!(typeof(this))(); 249 ptrcall!(void)(GDNativeClassBinding.setGlobalPosition, _godot_object, position); 250 } 251 /** 252 253 */ 254 void setGlobalRotation(in double radians) 255 { 256 checkClassBinding!(typeof(this))(); 257 ptrcall!(void)(GDNativeClassBinding.setGlobalRotation, _godot_object, radians); 258 } 259 /** 260 261 */ 262 void setGlobalRotationDegrees(in double degrees) 263 { 264 checkClassBinding!(typeof(this))(); 265 ptrcall!(void)(GDNativeClassBinding.setGlobalRotationDegrees, _godot_object, degrees); 266 } 267 /** 268 269 */ 270 void setGlobalScale(in Vector2 scale) 271 { 272 checkClassBinding!(typeof(this))(); 273 ptrcall!(void)(GDNativeClassBinding.setGlobalScale, _godot_object, scale); 274 } 275 /** 276 277 */ 278 void setGlobalTransform(in Transform2D xform) 279 { 280 checkClassBinding!(typeof(this))(); 281 ptrcall!(void)(GDNativeClassBinding.setGlobalTransform, _godot_object, xform); 282 } 283 /** 284 285 */ 286 void setPosition(in Vector2 position) 287 { 288 checkClassBinding!(typeof(this))(); 289 ptrcall!(void)(GDNativeClassBinding.setPosition, _godot_object, position); 290 } 291 /** 292 293 */ 294 void setRotation(in double radians) 295 { 296 checkClassBinding!(typeof(this))(); 297 ptrcall!(void)(GDNativeClassBinding.setRotation, _godot_object, radians); 298 } 299 /** 300 301 */ 302 void setRotationDegrees(in double degrees) 303 { 304 checkClassBinding!(typeof(this))(); 305 ptrcall!(void)(GDNativeClassBinding.setRotationDegrees, _godot_object, degrees); 306 } 307 /** 308 309 */ 310 void setScale(in Vector2 scale) 311 { 312 checkClassBinding!(typeof(this))(); 313 ptrcall!(void)(GDNativeClassBinding.setScale, _godot_object, scale); 314 } 315 /** 316 317 */ 318 void setTransform(in Transform2D xform) 319 { 320 checkClassBinding!(typeof(this))(); 321 ptrcall!(void)(GDNativeClassBinding.setTransform, _godot_object, xform); 322 } 323 /** 324 325 */ 326 void setZAsRelative(in bool enable) 327 { 328 checkClassBinding!(typeof(this))(); 329 ptrcall!(void)(GDNativeClassBinding.setZAsRelative, _godot_object, enable); 330 } 331 /** 332 333 */ 334 void setZIndex(in long z_index) 335 { 336 checkClassBinding!(typeof(this))(); 337 ptrcall!(void)(GDNativeClassBinding.setZIndex, _godot_object, z_index); 338 } 339 /** 340 Transforms the provided local position into a position in global coordinate space. The input is expected to be local relative to the $(D Node2D) it is called on. e.g. Applying this method to the positions of child nodes will correctly transform their positions into the global coordinate space, but applying it to a node's own position will give an incorrect result, as it will incorporate the node's own transformation into its global position. 341 */ 342 Vector2 toGlobal(in Vector2 local_point) const 343 { 344 checkClassBinding!(typeof(this))(); 345 return ptrcall!(Vector2)(GDNativeClassBinding.toGlobal, _godot_object, local_point); 346 } 347 /** 348 Transforms the provided global position into a position in local coordinate space. The output will be local relative to the $(D Node2D) it is called on. e.g. It is appropriate for determining the positions of child nodes, but it is not appropriate for determining its own position relative to its parent. 349 */ 350 Vector2 toLocal(in Vector2 global_point) const 351 { 352 checkClassBinding!(typeof(this))(); 353 return ptrcall!(Vector2)(GDNativeClassBinding.toLocal, _godot_object, global_point); 354 } 355 /** 356 Translates the node by the given `offset` in local coordinates. 357 */ 358 void translate(in Vector2 offset) 359 { 360 checkClassBinding!(typeof(this))(); 361 ptrcall!(void)(GDNativeClassBinding.translate, _godot_object, offset); 362 } 363 /** 364 Global position. 365 */ 366 @property Vector2 globalPosition() 367 { 368 return getGlobalPosition(); 369 } 370 /// ditto 371 @property void globalPosition(Vector2 v) 372 { 373 setGlobalPosition(v); 374 } 375 /** 376 Global rotation in radians. 377 */ 378 @property double globalRotation() 379 { 380 return getGlobalRotation(); 381 } 382 /// ditto 383 @property void globalRotation(double v) 384 { 385 setGlobalRotation(v); 386 } 387 /** 388 Global rotation in degrees. 389 */ 390 @property double globalRotationDegrees() 391 { 392 return getGlobalRotationDegrees(); 393 } 394 /// ditto 395 @property void globalRotationDegrees(double v) 396 { 397 setGlobalRotationDegrees(v); 398 } 399 /** 400 Global scale. 401 */ 402 @property Vector2 globalScale() 403 { 404 return getGlobalScale(); 405 } 406 /// ditto 407 @property void globalScale(Vector2 v) 408 { 409 setGlobalScale(v); 410 } 411 /** 412 Global $(D Transform2D). 413 */ 414 @property Transform2D globalTransform() 415 { 416 return getGlobalTransform(); 417 } 418 /// ditto 419 @property void globalTransform(Transform2D v) 420 { 421 setGlobalTransform(v); 422 } 423 /** 424 Position, relative to the node's parent. 425 */ 426 @property Vector2 position() 427 { 428 return getPosition(); 429 } 430 /// ditto 431 @property void position(Vector2 v) 432 { 433 setPosition(v); 434 } 435 /** 436 Rotation in radians, relative to the node's parent. 437 */ 438 @property double rotation() 439 { 440 return getRotation(); 441 } 442 /// ditto 443 @property void rotation(double v) 444 { 445 setRotation(v); 446 } 447 /** 448 Rotation in degrees, relative to the node's parent. 449 */ 450 @property double rotationDegrees() 451 { 452 return getRotationDegrees(); 453 } 454 /// ditto 455 @property void rotationDegrees(double v) 456 { 457 setRotationDegrees(v); 458 } 459 /** 460 The node's scale. Unscaled value: `(1, 1)`. 461 */ 462 @property Vector2 scale() 463 { 464 return getScale(); 465 } 466 /// ditto 467 @property void scale(Vector2 v) 468 { 469 setScale(v); 470 } 471 /** 472 Local $(D Transform2D). 473 */ 474 @property Transform2D transform() 475 { 476 return getTransform(); 477 } 478 /// ditto 479 @property void transform(Transform2D v) 480 { 481 setTransform(v); 482 } 483 /** 484 If `true`, the node's Z index is relative to its parent's Z index. If this node's Z index is 2 and its parent's effective Z index is 3, then this node's effective Z index will be 2 + 3 = 5. 485 */ 486 @property bool zAsRelative() 487 { 488 return isZRelative(); 489 } 490 /// ditto 491 @property void zAsRelative(bool v) 492 { 493 setZAsRelative(v); 494 } 495 /** 496 Z index. Controls the order in which the nodes render. A node with a higher Z index will display in front of others. Must be between $(D constant VisualServer.CANVAS_ITEM_Z_MIN) and $(D constant VisualServer.CANVAS_ITEM_Z_MAX) (inclusive). 497 */ 498 @property long zIndex() 499 { 500 return getZIndex(); 501 } 502 /// ditto 503 @property void zIndex(long v) 504 { 505 setZIndex(v); 506 } 507 }