1 /** 2 A $(D Texture) based on an $(D Image). 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.imagetexture; 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.texture; 25 import godot.image; 26 /** 27 A $(D Texture) based on an $(D Image). 28 29 For an image to be displayed, an $(D ImageTexture) has to be created from it using the $(D createFromImage) method: 30 31 32 var texture = ImageTexture.new() 33 var image = Image.new() 34 image.load("res://icon.png") 35 texture.create_from_image(image) 36 $Sprite.texture = texture 37 38 39 This way, textures can be created at run-time by loading images both from within the editor and externally. 40 $(B Warning:) Prefer to load imported textures with $(D @GDScript.load) over loading them from within the filesystem dynamically with $(D Image.load), as it may not work in exported projects: 41 42 43 var texture = load("res://icon.png") 44 $Sprite.texture = texture 45 46 47 This is because images have to be imported as $(D StreamTexture) first to be loaded with $(D @GDScript.load). If you'd still like to load an image file just like any other $(D Resource), import it as an $(D Image) resource instead, and then load it normally using the $(D @GDScript.load) method. 48 But do note that the image data can still be retrieved from an imported texture as well using the $(D Texture.getData) method, which returns a copy of the data: 49 50 51 var texture = load("res://icon.png") 52 var image : Image = texture.get_data() 53 54 55 An $(D ImageTexture) is not meant to be operated from within the editor interface directly, and is mostly useful for rendering images on screen dynamically via code. If you need to generate images procedurally from within the editor, consider saving and importing images as custom texture resources implementing a new $(D EditorImportPlugin). 56 $(B Note:) The maximum texture size is 16384×16384 pixels due to graphics hardware limitations. 57 */ 58 @GodotBaseClass struct ImageTexture 59 { 60 package(godot) enum string _GODOT_internal_name = "ImageTexture"; 61 public: 62 @nogc nothrow: 63 union { /** */ godot_object _godot_object; /** */ Texture _GODOT_base; } 64 alias _GODOT_base this; 65 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 66 package(godot) __gshared bool _classBindingInitialized = false; 67 package(godot) static struct GDNativeClassBinding 68 { 69 __gshared: 70 @GodotName("_reload_hook") GodotMethod!(void, RID) _reloadHook; 71 @GodotName("create") GodotMethod!(void, long, long, long, long) create; 72 @GodotName("create_from_image") GodotMethod!(void, Image, long) createFromImage; 73 @GodotName("get_format") GodotMethod!(Image.Format) getFormat; 74 @GodotName("get_lossy_storage_quality") GodotMethod!(double) getLossyStorageQuality; 75 @GodotName("get_storage") GodotMethod!(ImageTexture.Storage) getStorage; 76 @GodotName("load") GodotMethod!(GodotError, String) load; 77 @GodotName("set_data") GodotMethod!(void, Image) setData; 78 @GodotName("set_lossy_storage_quality") GodotMethod!(void, double) setLossyStorageQuality; 79 @GodotName("set_size_override") GodotMethod!(void, Vector2) setSizeOverride; 80 @GodotName("set_storage") GodotMethod!(void, long) setStorage; 81 } 82 /// 83 pragma(inline, true) bool opEquals(in ImageTexture other) const 84 { return _godot_object.ptr is other._godot_object.ptr; } 85 /// 86 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 87 { _godot_object.ptr = n; return null; } 88 /// 89 pragma(inline, true) bool opEquals(typeof(null) n) const 90 { return _godot_object.ptr is n; } 91 /// 92 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 93 mixin baseCasts; 94 /// Construct a new instance of ImageTexture. 95 /// Note: use `memnew!ImageTexture` instead. 96 static ImageTexture _new() 97 { 98 static godot_class_constructor constructor; 99 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("ImageTexture"); 100 if(constructor is null) return typeof(this).init; 101 return cast(ImageTexture)(constructor()); 102 } 103 @disable new(size_t s); 104 /// 105 enum Storage : int 106 { 107 /** 108 $(D Image) data is stored raw and unaltered. 109 */ 110 storageRaw = 0, 111 /** 112 $(D Image) data is compressed with a lossy algorithm. You can set the storage quality with $(D lossyQuality). 113 */ 114 storageCompressLossy = 1, 115 /** 116 $(D Image) data is compressed with a lossless algorithm. 117 */ 118 storageCompressLossless = 2, 119 } 120 /// 121 enum Constants : int 122 { 123 storageRaw = 0, 124 storageCompressLossy = 1, 125 storageCompressLossless = 2, 126 } 127 /** 128 129 */ 130 void _reloadHook(in RID rid) 131 { 132 Array _GODOT_args = Array.make(); 133 _GODOT_args.append(rid); 134 String _GODOT_method_name = String("_reload_hook"); 135 this.callv(_GODOT_method_name, _GODOT_args); 136 } 137 /** 138 Create a new $(D ImageTexture) with `width` and `height`. 139 `format` is a value from $(D Image.format), `flags` is any combination of $(D Texture.flags). 140 */ 141 void create(in long width, in long height, in long format, in long flags = 7) 142 { 143 checkClassBinding!(typeof(this))(); 144 ptrcall!(void)(GDNativeClassBinding.create, _godot_object, width, height, format, flags); 145 } 146 /** 147 Initializes the texture by allocating and setting the data from an $(D Image) with `flags` from $(D Texture.flags). An sRGB to linear color space conversion can take place, according to $(D Image.format). 148 */ 149 void createFromImage(Image image, in long flags = 7) 150 { 151 checkClassBinding!(typeof(this))(); 152 ptrcall!(void)(GDNativeClassBinding.createFromImage, _godot_object, image, flags); 153 } 154 /** 155 Returns the format of the texture, one of $(D Image.format). 156 */ 157 Image.Format getFormat() const 158 { 159 checkClassBinding!(typeof(this))(); 160 return ptrcall!(Image.Format)(GDNativeClassBinding.getFormat, _godot_object); 161 } 162 /** 163 164 */ 165 double getLossyStorageQuality() const 166 { 167 checkClassBinding!(typeof(this))(); 168 return ptrcall!(double)(GDNativeClassBinding.getLossyStorageQuality, _godot_object); 169 } 170 /** 171 172 */ 173 ImageTexture.Storage getStorage() const 174 { 175 checkClassBinding!(typeof(this))(); 176 return ptrcall!(ImageTexture.Storage)(GDNativeClassBinding.getStorage, _godot_object); 177 } 178 /** 179 Loads an image from a file path and creates a texture from it. 180 $(B Note:) the method is deprecated and will be removed in Godot 4.0, use $(D Image.load) and $(D createFromImage) instead. 181 */ 182 GodotError load(in String path) 183 { 184 checkClassBinding!(typeof(this))(); 185 return ptrcall!(GodotError)(GDNativeClassBinding.load, _godot_object, path); 186 } 187 /** 188 Replaces the texture's data with a new $(D Image). 189 $(B Note:) The texture has to be initialized first with the $(D createFromImage) method before it can be updated. The new image dimensions, format, and mipmaps configuration should match the existing texture's image configuration, otherwise it has to be re-created with the $(D createFromImage) method. 190 Use this method over $(D createFromImage) if you need to update the texture frequently, which is faster than allocating additional memory for a new texture each time. 191 */ 192 void setData(Image image) 193 { 194 checkClassBinding!(typeof(this))(); 195 ptrcall!(void)(GDNativeClassBinding.setData, _godot_object, image); 196 } 197 /** 198 199 */ 200 void setLossyStorageQuality(in double quality) 201 { 202 checkClassBinding!(typeof(this))(); 203 ptrcall!(void)(GDNativeClassBinding.setLossyStorageQuality, _godot_object, quality); 204 } 205 /** 206 Resizes the texture to the specified dimensions. 207 */ 208 void setSizeOverride(in Vector2 size) 209 { 210 checkClassBinding!(typeof(this))(); 211 ptrcall!(void)(GDNativeClassBinding.setSizeOverride, _godot_object, size); 212 } 213 /** 214 215 */ 216 void setStorage(in long mode) 217 { 218 checkClassBinding!(typeof(this))(); 219 ptrcall!(void)(GDNativeClassBinding.setStorage, _godot_object, mode); 220 } 221 /** 222 The storage quality for $(D constant STORAGE_COMPRESS_LOSSY). 223 */ 224 @property double lossyQuality() 225 { 226 return getLossyStorageQuality(); 227 } 228 /// ditto 229 @property void lossyQuality(double v) 230 { 231 setLossyStorageQuality(v); 232 } 233 /** 234 The storage type (raw, lossy, or compressed). 235 */ 236 @property ImageTexture.Storage storage() 237 { 238 return getStorage(); 239 } 240 /// ditto 241 @property void storage(long v) 242 { 243 setStorage(v); 244 } 245 }