1 /** 2 Texture for 2D and 3D. 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.texture; 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.resource; 24 import godot.reference; 25 import godot.image; 26 /** 27 Texture for 2D and 3D. 28 29 A texture works by registering an image in the video hardware, which then can be used in 3D models or 2D $(D Sprite) or GUI $(D Control). 30 Textures are often created by loading them from a file. See $(D @GDScript.load). 31 $(D Texture) is a base for other resources. It cannot be used directly. 32 $(B Note:) The maximum texture size is 16384×16384 pixels due to graphics hardware limitations. Larger textures may fail to import. 33 */ 34 @GodotBaseClass struct Texture 35 { 36 package(godot) enum string _GODOT_internal_name = "Texture"; 37 public: 38 @nogc nothrow: 39 union { /** */ godot_object _godot_object; /** */ Resource _GODOT_base; } 40 alias _GODOT_base this; 41 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 42 package(godot) __gshared bool _classBindingInitialized = false; 43 package(godot) static struct GDNativeClassBinding 44 { 45 __gshared: 46 @GodotName("draw") GodotMethod!(void, RID, Vector2, Color, bool, Texture) draw; 47 @GodotName("draw_rect") GodotMethod!(void, RID, Rect2, bool, Color, bool, Texture) drawRect; 48 @GodotName("draw_rect_region") GodotMethod!(void, RID, Rect2, Rect2, Color, bool, Texture, bool) drawRectRegion; 49 @GodotName("get_data") GodotMethod!(Image) getData; 50 @GodotName("get_flags") GodotMethod!(long) getFlags; 51 @GodotName("get_height") GodotMethod!(long) getHeight; 52 @GodotName("get_size") GodotMethod!(Vector2) getSize; 53 @GodotName("get_width") GodotMethod!(long) getWidth; 54 @GodotName("has_alpha") GodotMethod!(bool) hasAlpha; 55 @GodotName("set_flags") GodotMethod!(void, long) setFlags; 56 } 57 /// 58 pragma(inline, true) bool opEquals(in Texture other) const 59 { return _godot_object.ptr is other._godot_object.ptr; } 60 /// 61 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 62 { _godot_object.ptr = n; return null; } 63 /// 64 pragma(inline, true) bool opEquals(typeof(null) n) const 65 { return _godot_object.ptr is n; } 66 /// 67 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 68 mixin baseCasts; 69 /// Construct a new instance of Texture. 70 /// Note: use `memnew!Texture` instead. 71 static Texture _new() 72 { 73 static godot_class_constructor constructor; 74 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("Texture"); 75 if(constructor is null) return typeof(this).init; 76 return cast(Texture)(constructor()); 77 } 78 @disable new(size_t s); 79 /// 80 enum Flags : int 81 { 82 /** 83 Generates mipmaps, which are smaller versions of the same texture to use when zoomed out, keeping the aspect ratio. 84 */ 85 flagMipmaps = 1, 86 /** 87 Repeats the texture (instead of clamp to edge). 88 $(B Note:) Ignored when using an $(D AtlasTexture) as these don't support repetition. 89 */ 90 flagRepeat = 2, 91 /** 92 Uses a magnifying filter, to enable smooth zooming in of the texture. 93 */ 94 flagFilter = 4, 95 /** 96 Default flags. $(D constant FLAG_MIPMAPS), $(D constant FLAG_REPEAT) and $(D constant FLAG_FILTER) are enabled. 97 */ 98 flagsDefault = 7, 99 /** 100 Uses anisotropic mipmap filtering. Generates smaller versions of the same texture with different aspect ratios. 101 This results in better-looking textures when viewed from oblique angles. 102 */ 103 flagAnisotropicFilter = 8, 104 /** 105 Converts the texture to the sRGB color space. 106 */ 107 flagConvertToLinear = 16, 108 /** 109 Repeats the texture with alternate sections mirrored. 110 $(B Note:) Ignored when using an $(D AtlasTexture) as these don't support repetition. 111 */ 112 flagMirroredRepeat = 32, 113 /** 114 Texture is a video surface. 115 */ 116 flagVideoSurface = 2048, 117 } 118 /// 119 enum Constants : int 120 { 121 flagMipmaps = 1, 122 flagRepeat = 2, 123 flagFilter = 4, 124 flagsDefault = 7, 125 flagAnisotropicFilter = 8, 126 flagConvertToLinear = 16, 127 flagMirroredRepeat = 32, 128 flagVideoSurface = 2048, 129 } 130 /** 131 Draws the texture using a $(D CanvasItem) with the $(D VisualServer) API at the specified `position`. Equivalent to $(D VisualServer.canvasItemAddTextureRect) with a rect at `position` and the size of this $(D Texture). 132 */ 133 void draw(in RID canvas_item, in Vector2 position, in Color modulate = Color(1,1,1,1), in bool transpose = false, Texture normal_map = Texture.init) const 134 { 135 checkClassBinding!(typeof(this))(); 136 ptrcall!(void)(GDNativeClassBinding.draw, _godot_object, canvas_item, position, modulate, transpose, normal_map); 137 } 138 /** 139 Draws the texture using a $(D CanvasItem) with the $(D VisualServer) API. Equivalent to $(D VisualServer.canvasItemAddTextureRect). 140 */ 141 void drawRect(in RID canvas_item, in Rect2 rect, in bool tile, in Color modulate = Color(1,1,1,1), in bool transpose = false, Texture normal_map = Texture.init) const 142 { 143 checkClassBinding!(typeof(this))(); 144 ptrcall!(void)(GDNativeClassBinding.drawRect, _godot_object, canvas_item, rect, tile, modulate, transpose, normal_map); 145 } 146 /** 147 Draws a part of the texture using a $(D CanvasItem) with the $(D VisualServer) API. Equivalent to $(D VisualServer.canvasItemAddTextureRectRegion). 148 */ 149 void drawRectRegion(in RID canvas_item, in Rect2 rect, in Rect2 src_rect, in Color modulate = Color(1,1,1,1), in bool transpose = false, Texture normal_map = Texture.init, in bool clip_uv = true) const 150 { 151 checkClassBinding!(typeof(this))(); 152 ptrcall!(void)(GDNativeClassBinding.drawRectRegion, _godot_object, canvas_item, rect, src_rect, modulate, transpose, normal_map, clip_uv); 153 } 154 /** 155 Returns an $(D Image) that is a copy of data from this $(D Texture). $(D Image)s can be accessed and manipulated directly. 156 */ 157 Ref!Image getData() const 158 { 159 checkClassBinding!(typeof(this))(); 160 return ptrcall!(Image)(GDNativeClassBinding.getData, _godot_object); 161 } 162 /** 163 164 */ 165 long getFlags() const 166 { 167 checkClassBinding!(typeof(this))(); 168 return ptrcall!(long)(GDNativeClassBinding.getFlags, _godot_object); 169 } 170 /** 171 Returns the texture height. 172 */ 173 long getHeight() const 174 { 175 checkClassBinding!(typeof(this))(); 176 return ptrcall!(long)(GDNativeClassBinding.getHeight, _godot_object); 177 } 178 /** 179 Returns the texture size. 180 */ 181 Vector2 getSize() const 182 { 183 checkClassBinding!(typeof(this))(); 184 return ptrcall!(Vector2)(GDNativeClassBinding.getSize, _godot_object); 185 } 186 /** 187 Returns the texture width. 188 */ 189 long getWidth() const 190 { 191 checkClassBinding!(typeof(this))(); 192 return ptrcall!(long)(GDNativeClassBinding.getWidth, _godot_object); 193 } 194 /** 195 Returns `true` if this $(D Texture) has an alpha channel. 196 */ 197 bool hasAlpha() const 198 { 199 checkClassBinding!(typeof(this))(); 200 return ptrcall!(bool)(GDNativeClassBinding.hasAlpha, _godot_object); 201 } 202 /** 203 204 */ 205 void setFlags(in long flags) 206 { 207 checkClassBinding!(typeof(this))(); 208 ptrcall!(void)(GDNativeClassBinding.setFlags, _godot_object, flags); 209 } 210 /** 211 The texture's $(D flags). $(D flags) are used to set various properties of the $(D Texture). 212 */ 213 @property long flags() 214 { 215 return getFlags(); 216 } 217 /// ditto 218 @property void flags(long v) 219 { 220 setFlags(v); 221 } 222 }