1 /** 2 Base class for all resources. 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.resource; 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.reference; 25 import godot.node; 26 /** 27 Base class for all resources. 28 29 Resource is the base class for all Godot-specific resource types, serving primarily as data containers. Since they inherit from $(D Reference), resources are reference-counted and freed when no longer in use. They are also cached once loaded from disk, so that any further attempts to load a resource from a given path will return the same reference (all this in contrast to a $(D Node), which is not reference-counted and can be instanced from disk as many times as desired). Resources can be saved externally on disk or bundled into another object, such as a $(D Node) or another resource. 30 $(B Note:) In C#, resources will not be freed instantly after they are no longer in use. Instead, garbage collection will run periodically and will free resources that are no longer in use. This means that unused resources will linger on for a while before being removed. 31 */ 32 @GodotBaseClass struct Resource 33 { 34 package(godot) enum string _GODOT_internal_name = "Resource"; 35 public: 36 @nogc nothrow: 37 union { /** */ godot_object _godot_object; /** */ Reference _GODOT_base; } 38 alias _GODOT_base this; 39 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 40 package(godot) __gshared bool _classBindingInitialized = false; 41 package(godot) static struct GDNativeClassBinding 42 { 43 __gshared: 44 @GodotName("_setup_local_to_scene") GodotMethod!(void) _setupLocalToScene; 45 @GodotName("duplicate") GodotMethod!(Resource, bool) duplicate; 46 @GodotName("emit_changed") GodotMethod!(void) emitChanged; 47 @GodotName("get_local_scene") GodotMethod!(Node) getLocalScene; 48 @GodotName("get_name") GodotMethod!(String) getName; 49 @GodotName("get_path") GodotMethod!(String) getPath; 50 @GodotName("get_rid") GodotMethod!(RID) getRid; 51 @GodotName("is_local_to_scene") GodotMethod!(bool) isLocalToScene; 52 @GodotName("set_local_to_scene") GodotMethod!(void, bool) setLocalToScene; 53 @GodotName("set_name") GodotMethod!(void, String) setName; 54 @GodotName("set_path") GodotMethod!(void, String) setPath; 55 @GodotName("setup_local_to_scene") GodotMethod!(void) setupLocalToScene; 56 @GodotName("take_over_path") GodotMethod!(void, String) takeOverPath; 57 } 58 /// 59 pragma(inline, true) bool opEquals(in Resource other) const 60 { return _godot_object.ptr is other._godot_object.ptr; } 61 /// 62 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 63 { _godot_object.ptr = n; return null; } 64 /// 65 pragma(inline, true) bool opEquals(typeof(null) n) const 66 { return _godot_object.ptr is n; } 67 /// 68 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 69 mixin baseCasts; 70 /// Construct a new instance of Resource. 71 /// Note: use `memnew!Resource` instead. 72 static Resource _new() 73 { 74 static godot_class_constructor constructor; 75 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("Resource"); 76 if(constructor is null) return typeof(this).init; 77 return cast(Resource)(constructor()); 78 } 79 @disable new(size_t s); 80 /** 81 Virtual function which can be overridden to customize the behavior value of $(D setupLocalToScene). 82 */ 83 void _setupLocalToScene() 84 { 85 Array _GODOT_args = Array.make(); 86 String _GODOT_method_name = String("_setup_local_to_scene"); 87 this.callv(_GODOT_method_name, _GODOT_args); 88 } 89 /** 90 Duplicates the resource, returning a new resource. By default, sub-resources are shared between resource copies for efficiency. This can be changed by passing `true` to the `subresources` argument which will copy the subresources. 91 $(B Note:) If `subresources` is `true`, this method will only perform a shallow copy. Nested resources within subresources will not be duplicated and will still be shared. 92 $(B Note:) When duplicating a resource, only `export`ed properties are copied. Other properties will be set to their default value in the new resource. 93 */ 94 Ref!Resource duplicate(in bool subresources = false) const 95 { 96 checkClassBinding!(typeof(this))(); 97 return ptrcall!(Resource)(GDNativeClassBinding.duplicate, _godot_object, subresources); 98 } 99 /** 100 Emits the $(D changed) signal. 101 If external objects which depend on this resource should be updated, this method must be called manually whenever the state of this resource has changed (such as modification of properties). 102 The method is equivalent to: 103 104 105 emit_signal("changed") 106 107 108 $(B Note:) This method is called automatically for built-in resources. 109 */ 110 void emitChanged() 111 { 112 checkClassBinding!(typeof(this))(); 113 ptrcall!(void)(GDNativeClassBinding.emitChanged, _godot_object); 114 } 115 /** 116 If $(D resourceLocalToScene) is enabled and the resource was loaded from a $(D PackedScene) instantiation, returns the local scene where this resource's unique copy is in use. Otherwise, returns `null`. 117 */ 118 Node getLocalScene() const 119 { 120 checkClassBinding!(typeof(this))(); 121 return ptrcall!(Node)(GDNativeClassBinding.getLocalScene, _godot_object); 122 } 123 /** 124 125 */ 126 String getName() 127 { 128 checkClassBinding!(typeof(this))(); 129 return ptrcall!(String)(GDNativeClassBinding.getName, _godot_object); 130 } 131 /** 132 133 */ 134 String getPath() const 135 { 136 checkClassBinding!(typeof(this))(); 137 return ptrcall!(String)(GDNativeClassBinding.getPath, _godot_object); 138 } 139 /** 140 Returns the RID of the resource (or an empty RID). Many resources (such as $(D Texture), $(D Mesh), etc) are high-level abstractions of resources stored in a server, so this function will return the original RID. 141 */ 142 RID getRid() const 143 { 144 checkClassBinding!(typeof(this))(); 145 return ptrcall!(RID)(GDNativeClassBinding.getRid, _godot_object); 146 } 147 /** 148 149 */ 150 bool isLocalToScene() const 151 { 152 checkClassBinding!(typeof(this))(); 153 return ptrcall!(bool)(GDNativeClassBinding.isLocalToScene, _godot_object); 154 } 155 /** 156 157 */ 158 void setLocalToScene(in bool enable) 159 { 160 checkClassBinding!(typeof(this))(); 161 ptrcall!(void)(GDNativeClassBinding.setLocalToScene, _godot_object, enable); 162 } 163 /** 164 165 */ 166 void setName(in String name) 167 { 168 checkClassBinding!(typeof(this))(); 169 ptrcall!(void)(GDNativeClassBinding.setName, _godot_object, name); 170 } 171 /** 172 173 */ 174 void setPath(in String path) 175 { 176 checkClassBinding!(typeof(this))(); 177 ptrcall!(void)(GDNativeClassBinding.setPath, _godot_object, path); 178 } 179 /** 180 This method is called when a resource with $(D resourceLocalToScene) enabled is loaded from a $(D PackedScene) instantiation. Its behavior can be customized by overriding $(D _setupLocalToScene) from script. 181 For most resources, this method performs no base logic. $(D ViewportTexture) performs custom logic to properly set the proxy texture and flags in the local viewport. 182 */ 183 void setupLocalToScene() 184 { 185 checkClassBinding!(typeof(this))(); 186 ptrcall!(void)(GDNativeClassBinding.setupLocalToScene, _godot_object); 187 } 188 /** 189 Sets the path of the resource, potentially overriding an existing cache entry for this path. This differs from setting $(D resourcePath), as the latter would error out if another resource was already cached for the given path. 190 */ 191 void takeOverPath(in String path) 192 { 193 checkClassBinding!(typeof(this))(); 194 ptrcall!(void)(GDNativeClassBinding.takeOverPath, _godot_object, path); 195 } 196 /** 197 If `true`, the resource will be made unique in each instance of its local scene. It can thus be modified in a scene instance without impacting other instances of that same scene. 198 */ 199 @property bool resourceLocalToScene() 200 { 201 return isLocalToScene(); 202 } 203 /// ditto 204 @property void resourceLocalToScene(bool v) 205 { 206 setLocalToScene(v); 207 } 208 /** 209 The name of the resource. This is an optional identifier. If $(D resourceName) is not empty, its value will be displayed to represent the current resource in the editor inspector. For built-in scripts, the $(D resourceName) will be displayed as the tab name in the script editor. 210 */ 211 @property String resourceName() 212 { 213 return getName(); 214 } 215 /// ditto 216 @property void resourceName(String v) 217 { 218 setName(v); 219 } 220 /** 221 The path to the resource. In case it has its own file, it will return its filepath. If it's tied to the scene, it will return the scene's path, followed by the resource's index. 222 */ 223 @property String resourcePath() 224 { 225 return getPath(); 226 } 227 /// ditto 228 @property void resourcePath(String v) 229 { 230 setPath(v); 231 } 232 }