1 /** 2 General-purpose sprite node. 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.sprite; 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.node2d; 25 import godot.canvasitem; 26 import godot.node; 27 import godot.texture; 28 /** 29 General-purpose sprite node. 30 31 A node that displays a 2D texture. The texture displayed can be a region from a larger atlas texture, or a frame from a sprite sheet animation. 32 */ 33 @GodotBaseClass struct Sprite 34 { 35 package(godot) enum string _GODOT_internal_name = "Sprite"; 36 public: 37 @nogc nothrow: 38 union { /** */ godot_object _godot_object; /** */ Node2D _GODOT_base; } 39 alias _GODOT_base this; 40 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 41 package(godot) __gshared bool _classBindingInitialized = false; 42 package(godot) static struct GDNativeClassBinding 43 { 44 __gshared: 45 @GodotName("_texture_changed") GodotMethod!(void) _textureChanged; 46 @GodotName("get_frame") GodotMethod!(long) getFrame; 47 @GodotName("get_frame_coords") GodotMethod!(Vector2) getFrameCoords; 48 @GodotName("get_hframes") GodotMethod!(long) getHframes; 49 @GodotName("get_normal_map") GodotMethod!(Texture) getNormalMap; 50 @GodotName("get_offset") GodotMethod!(Vector2) getOffset; 51 @GodotName("get_rect") GodotMethod!(Rect2) getRect; 52 @GodotName("get_region_rect") GodotMethod!(Rect2) getRegionRect; 53 @GodotName("get_texture") GodotMethod!(Texture) getTexture; 54 @GodotName("get_vframes") GodotMethod!(long) getVframes; 55 @GodotName("is_centered") GodotMethod!(bool) isCentered; 56 @GodotName("is_flipped_h") GodotMethod!(bool) isFlippedH; 57 @GodotName("is_flipped_v") GodotMethod!(bool) isFlippedV; 58 @GodotName("is_pixel_opaque") GodotMethod!(bool, Vector2) isPixelOpaque; 59 @GodotName("is_region") GodotMethod!(bool) isRegion; 60 @GodotName("is_region_filter_clip_enabled") GodotMethod!(bool) isRegionFilterClipEnabled; 61 @GodotName("set_centered") GodotMethod!(void, bool) setCentered; 62 @GodotName("set_flip_h") GodotMethod!(void, bool) setFlipH; 63 @GodotName("set_flip_v") GodotMethod!(void, bool) setFlipV; 64 @GodotName("set_frame") GodotMethod!(void, long) setFrame; 65 @GodotName("set_frame_coords") GodotMethod!(void, Vector2) setFrameCoords; 66 @GodotName("set_hframes") GodotMethod!(void, long) setHframes; 67 @GodotName("set_normal_map") GodotMethod!(void, Texture) setNormalMap; 68 @GodotName("set_offset") GodotMethod!(void, Vector2) setOffset; 69 @GodotName("set_region") GodotMethod!(void, bool) setRegion; 70 @GodotName("set_region_filter_clip") GodotMethod!(void, bool) setRegionFilterClip; 71 @GodotName("set_region_rect") GodotMethod!(void, Rect2) setRegionRect; 72 @GodotName("set_texture") GodotMethod!(void, Texture) setTexture; 73 @GodotName("set_vframes") GodotMethod!(void, long) setVframes; 74 } 75 /// 76 pragma(inline, true) bool opEquals(in Sprite other) const 77 { return _godot_object.ptr is other._godot_object.ptr; } 78 /// 79 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 80 { _godot_object.ptr = n; return null; } 81 /// 82 pragma(inline, true) bool opEquals(typeof(null) n) const 83 { return _godot_object.ptr is n; } 84 /// 85 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 86 mixin baseCasts; 87 /// Construct a new instance of Sprite. 88 /// Note: use `memnew!Sprite` instead. 89 static Sprite _new() 90 { 91 static godot_class_constructor constructor; 92 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("Sprite"); 93 if(constructor is null) return typeof(this).init; 94 return cast(Sprite)(constructor()); 95 } 96 @disable new(size_t s); 97 /** 98 99 */ 100 void _textureChanged() 101 { 102 Array _GODOT_args = Array.make(); 103 String _GODOT_method_name = String("_texture_changed"); 104 this.callv(_GODOT_method_name, _GODOT_args); 105 } 106 /** 107 108 */ 109 long getFrame() const 110 { 111 checkClassBinding!(typeof(this))(); 112 return ptrcall!(long)(GDNativeClassBinding.getFrame, _godot_object); 113 } 114 /** 115 116 */ 117 Vector2 getFrameCoords() const 118 { 119 checkClassBinding!(typeof(this))(); 120 return ptrcall!(Vector2)(GDNativeClassBinding.getFrameCoords, _godot_object); 121 } 122 /** 123 124 */ 125 long getHframes() const 126 { 127 checkClassBinding!(typeof(this))(); 128 return ptrcall!(long)(GDNativeClassBinding.getHframes, _godot_object); 129 } 130 /** 131 132 */ 133 Ref!Texture getNormalMap() const 134 { 135 checkClassBinding!(typeof(this))(); 136 return ptrcall!(Texture)(GDNativeClassBinding.getNormalMap, _godot_object); 137 } 138 /** 139 140 */ 141 Vector2 getOffset() const 142 { 143 checkClassBinding!(typeof(this))(); 144 return ptrcall!(Vector2)(GDNativeClassBinding.getOffset, _godot_object); 145 } 146 /** 147 Returns a $(D Rect2) representing the Sprite's boundary in local coordinates. Can be used to detect if the Sprite was clicked. Example: 148 149 150 func _input(event): 151 if event is InputEventMouseButton and event.pressed and event.button_index == BUTTON_LEFT: 152 if get_rect().has_point(to_local(event.position)): 153 print("A click!") 154 155 156 */ 157 Rect2 getRect() const 158 { 159 checkClassBinding!(typeof(this))(); 160 return ptrcall!(Rect2)(GDNativeClassBinding.getRect, _godot_object); 161 } 162 /** 163 164 */ 165 Rect2 getRegionRect() const 166 { 167 checkClassBinding!(typeof(this))(); 168 return ptrcall!(Rect2)(GDNativeClassBinding.getRegionRect, _godot_object); 169 } 170 /** 171 172 */ 173 Ref!Texture getTexture() const 174 { 175 checkClassBinding!(typeof(this))(); 176 return ptrcall!(Texture)(GDNativeClassBinding.getTexture, _godot_object); 177 } 178 /** 179 180 */ 181 long getVframes() const 182 { 183 checkClassBinding!(typeof(this))(); 184 return ptrcall!(long)(GDNativeClassBinding.getVframes, _godot_object); 185 } 186 /** 187 188 */ 189 bool isCentered() const 190 { 191 checkClassBinding!(typeof(this))(); 192 return ptrcall!(bool)(GDNativeClassBinding.isCentered, _godot_object); 193 } 194 /** 195 196 */ 197 bool isFlippedH() const 198 { 199 checkClassBinding!(typeof(this))(); 200 return ptrcall!(bool)(GDNativeClassBinding.isFlippedH, _godot_object); 201 } 202 /** 203 204 */ 205 bool isFlippedV() const 206 { 207 checkClassBinding!(typeof(this))(); 208 return ptrcall!(bool)(GDNativeClassBinding.isFlippedV, _godot_object); 209 } 210 /** 211 Returns `true`, if the pixel at the given position is opaque and `false` in other case. 212 $(B Note:) It also returns `false`, if the sprite's texture is `null` or if the given position is invalid. 213 */ 214 bool isPixelOpaque(in Vector2 pos) const 215 { 216 checkClassBinding!(typeof(this))(); 217 return ptrcall!(bool)(GDNativeClassBinding.isPixelOpaque, _godot_object, pos); 218 } 219 /** 220 221 */ 222 bool isRegion() const 223 { 224 checkClassBinding!(typeof(this))(); 225 return ptrcall!(bool)(GDNativeClassBinding.isRegion, _godot_object); 226 } 227 /** 228 229 */ 230 bool isRegionFilterClipEnabled() const 231 { 232 checkClassBinding!(typeof(this))(); 233 return ptrcall!(bool)(GDNativeClassBinding.isRegionFilterClipEnabled, _godot_object); 234 } 235 /** 236 237 */ 238 void setCentered(in bool centered) 239 { 240 checkClassBinding!(typeof(this))(); 241 ptrcall!(void)(GDNativeClassBinding.setCentered, _godot_object, centered); 242 } 243 /** 244 245 */ 246 void setFlipH(in bool flip_h) 247 { 248 checkClassBinding!(typeof(this))(); 249 ptrcall!(void)(GDNativeClassBinding.setFlipH, _godot_object, flip_h); 250 } 251 /** 252 253 */ 254 void setFlipV(in bool flip_v) 255 { 256 checkClassBinding!(typeof(this))(); 257 ptrcall!(void)(GDNativeClassBinding.setFlipV, _godot_object, flip_v); 258 } 259 /** 260 261 */ 262 void setFrame(in long frame) 263 { 264 checkClassBinding!(typeof(this))(); 265 ptrcall!(void)(GDNativeClassBinding.setFrame, _godot_object, frame); 266 } 267 /** 268 269 */ 270 void setFrameCoords(in Vector2 coords) 271 { 272 checkClassBinding!(typeof(this))(); 273 ptrcall!(void)(GDNativeClassBinding.setFrameCoords, _godot_object, coords); 274 } 275 /** 276 277 */ 278 void setHframes(in long hframes) 279 { 280 checkClassBinding!(typeof(this))(); 281 ptrcall!(void)(GDNativeClassBinding.setHframes, _godot_object, hframes); 282 } 283 /** 284 285 */ 286 void setNormalMap(Texture normal_map) 287 { 288 checkClassBinding!(typeof(this))(); 289 ptrcall!(void)(GDNativeClassBinding.setNormalMap, _godot_object, normal_map); 290 } 291 /** 292 293 */ 294 void setOffset(in Vector2 offset) 295 { 296 checkClassBinding!(typeof(this))(); 297 ptrcall!(void)(GDNativeClassBinding.setOffset, _godot_object, offset); 298 } 299 /** 300 301 */ 302 void setRegion(in bool enabled) 303 { 304 checkClassBinding!(typeof(this))(); 305 ptrcall!(void)(GDNativeClassBinding.setRegion, _godot_object, enabled); 306 } 307 /** 308 309 */ 310 void setRegionFilterClip(in bool enabled) 311 { 312 checkClassBinding!(typeof(this))(); 313 ptrcall!(void)(GDNativeClassBinding.setRegionFilterClip, _godot_object, enabled); 314 } 315 /** 316 317 */ 318 void setRegionRect(in Rect2 rect) 319 { 320 checkClassBinding!(typeof(this))(); 321 ptrcall!(void)(GDNativeClassBinding.setRegionRect, _godot_object, rect); 322 } 323 /** 324 325 */ 326 void setTexture(Texture texture) 327 { 328 checkClassBinding!(typeof(this))(); 329 ptrcall!(void)(GDNativeClassBinding.setTexture, _godot_object, texture); 330 } 331 /** 332 333 */ 334 void setVframes(in long vframes) 335 { 336 checkClassBinding!(typeof(this))(); 337 ptrcall!(void)(GDNativeClassBinding.setVframes, _godot_object, vframes); 338 } 339 /** 340 If `true`, texture is centered. 341 */ 342 @property bool centered() 343 { 344 return isCentered(); 345 } 346 /// ditto 347 @property void centered(bool v) 348 { 349 setCentered(v); 350 } 351 /** 352 If `true`, texture is flipped horizontally. 353 */ 354 @property bool flipH() 355 { 356 return isFlippedH(); 357 } 358 /// ditto 359 @property void flipH(bool v) 360 { 361 setFlipH(v); 362 } 363 /** 364 If `true`, texture is flipped vertically. 365 */ 366 @property bool flipV() 367 { 368 return isFlippedV(); 369 } 370 /// ditto 371 @property void flipV(bool v) 372 { 373 setFlipV(v); 374 } 375 /** 376 Current frame to display from sprite sheet. $(D hframes) or $(D vframes) must be greater than 1. 377 */ 378 @property long frame() 379 { 380 return getFrame(); 381 } 382 /// ditto 383 @property void frame(long v) 384 { 385 setFrame(v); 386 } 387 /** 388 Coordinates of the frame to display from sprite sheet. This is as an alias for the $(D frame) property. $(D hframes) or $(D vframes) must be greater than 1. 389 */ 390 @property Vector2 frameCoords() 391 { 392 return getFrameCoords(); 393 } 394 /// ditto 395 @property void frameCoords(Vector2 v) 396 { 397 setFrameCoords(v); 398 } 399 /** 400 The number of columns in the sprite sheet. 401 */ 402 @property long hframes() 403 { 404 return getHframes(); 405 } 406 /// ditto 407 @property void hframes(long v) 408 { 409 setHframes(v); 410 } 411 /** 412 The normal map gives depth to the Sprite. 413 $(B Note:) Godot expects the normal map to use X+, Y-, and Z+ coordinates. See $(D url=http://wiki.polycount.com/wiki/Normal_Map_Technical_Details#Common_Swizzle_Coordinates)this page$(D /url) for a comparison of normal map coordinates expected by popular engines. 414 */ 415 @property Texture normalMap() 416 { 417 return getNormalMap(); 418 } 419 /// ditto 420 @property void normalMap(Texture v) 421 { 422 setNormalMap(v); 423 } 424 /** 425 The texture's drawing offset. 426 */ 427 @property Vector2 offset() 428 { 429 return getOffset(); 430 } 431 /// ditto 432 @property void offset(Vector2 v) 433 { 434 setOffset(v); 435 } 436 /** 437 If `true`, texture is cut from a larger atlas texture. See $(D regionRect). 438 */ 439 @property bool regionEnabled() 440 { 441 return isRegion(); 442 } 443 /// ditto 444 @property void regionEnabled(bool v) 445 { 446 setRegion(v); 447 } 448 /** 449 If `true`, the outermost pixels get blurred out. 450 */ 451 @property bool regionFilterClip() 452 { 453 return isRegionFilterClipEnabled(); 454 } 455 /// ditto 456 @property void regionFilterClip(bool v) 457 { 458 setRegionFilterClip(v); 459 } 460 /** 461 The region of the atlas texture to display. $(D regionEnabled) must be `true`. 462 */ 463 @property Rect2 regionRect() 464 { 465 return getRegionRect(); 466 } 467 /// ditto 468 @property void regionRect(Rect2 v) 469 { 470 setRegionRect(v); 471 } 472 /** 473 $(D Texture) object to draw. 474 */ 475 @property Texture texture() 476 { 477 return getTexture(); 478 } 479 /// ditto 480 @property void texture(Texture v) 481 { 482 setTexture(v); 483 } 484 /** 485 The number of rows in the sprite sheet. 486 */ 487 @property long vframes() 488 { 489 return getVframes(); 490 } 491 /// ditto 492 @property void vframes(long v) 493 { 494 setVframes(v); 495 } 496 }