1 /** 2 A class stored as a resource. 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.script; 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 /** 26 A class stored as a resource. 27 28 A script extends the functionality of all objects that instance it. 29 The `new` method of a script subclass creates a new instance. $(D GodotObject.setScript) extends an existing object, if that object's class matches one of the script's base classes. 30 */ 31 @GodotBaseClass struct Script 32 { 33 package(godot) enum string _GODOT_internal_name = "Script"; 34 public: 35 @nogc nothrow: 36 union { /** */ godot_object _godot_object; /** */ Resource _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("can_instance") GodotMethod!(bool) canInstance; 44 @GodotName("get_base_script") GodotMethod!(Script) getBaseScript; 45 @GodotName("get_instance_base_type") GodotMethod!(String) getInstanceBaseType; 46 @GodotName("get_property_default_value") GodotMethod!(Variant, String) getPropertyDefaultValue; 47 @GodotName("get_script_constant_map") GodotMethod!(Dictionary) getScriptConstantMap; 48 @GodotName("get_script_method_list") GodotMethod!(Array) getScriptMethodList; 49 @GodotName("get_script_property_list") GodotMethod!(Array) getScriptPropertyList; 50 @GodotName("get_script_signal_list") GodotMethod!(Array) getScriptSignalList; 51 @GodotName("get_source_code") GodotMethod!(String) getSourceCode; 52 @GodotName("has_script_signal") GodotMethod!(bool, String) hasScriptSignal; 53 @GodotName("has_source_code") GodotMethod!(bool) hasSourceCode; 54 @GodotName("instance_has") GodotMethod!(bool, GodotObject) instanceHas; 55 @GodotName("is_tool") GodotMethod!(bool) isTool; 56 @GodotName("reload") GodotMethod!(GodotError, bool) reload; 57 @GodotName("set_source_code") GodotMethod!(void, String) setSourceCode; 58 } 59 /// 60 pragma(inline, true) bool opEquals(in Script other) const 61 { return _godot_object.ptr is other._godot_object.ptr; } 62 /// 63 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 64 { _godot_object.ptr = n; return null; } 65 /// 66 pragma(inline, true) bool opEquals(typeof(null) n) const 67 { return _godot_object.ptr is n; } 68 /// 69 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 70 mixin baseCasts; 71 /// Construct a new instance of Script. 72 /// Note: use `memnew!Script` instead. 73 static Script _new() 74 { 75 static godot_class_constructor constructor; 76 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("Script"); 77 if(constructor is null) return typeof(this).init; 78 return cast(Script)(constructor()); 79 } 80 @disable new(size_t s); 81 /** 82 Returns `true` if the script can be instanced. 83 */ 84 bool canInstance() const 85 { 86 checkClassBinding!(typeof(this))(); 87 return ptrcall!(bool)(GDNativeClassBinding.canInstance, _godot_object); 88 } 89 /** 90 Returns the script directly inherited by this script. 91 */ 92 Ref!Script getBaseScript() const 93 { 94 checkClassBinding!(typeof(this))(); 95 return ptrcall!(Script)(GDNativeClassBinding.getBaseScript, _godot_object); 96 } 97 /** 98 Returns the script's base type. 99 */ 100 String getInstanceBaseType() const 101 { 102 checkClassBinding!(typeof(this))(); 103 return ptrcall!(String)(GDNativeClassBinding.getInstanceBaseType, _godot_object); 104 } 105 /** 106 Returns the default value of the specified property. 107 */ 108 Variant getPropertyDefaultValue(in String property) 109 { 110 checkClassBinding!(typeof(this))(); 111 return ptrcall!(Variant)(GDNativeClassBinding.getPropertyDefaultValue, _godot_object, property); 112 } 113 /** 114 Returns a dictionary containing constant names and their values. 115 */ 116 Dictionary getScriptConstantMap() 117 { 118 checkClassBinding!(typeof(this))(); 119 return ptrcall!(Dictionary)(GDNativeClassBinding.getScriptConstantMap, _godot_object); 120 } 121 /** 122 Returns the list of methods in this $(D Script). 123 */ 124 Array getScriptMethodList() 125 { 126 checkClassBinding!(typeof(this))(); 127 return ptrcall!(Array)(GDNativeClassBinding.getScriptMethodList, _godot_object); 128 } 129 /** 130 Returns the list of properties in this $(D Script). 131 */ 132 Array getScriptPropertyList() 133 { 134 checkClassBinding!(typeof(this))(); 135 return ptrcall!(Array)(GDNativeClassBinding.getScriptPropertyList, _godot_object); 136 } 137 /** 138 Returns the list of user signals defined in this $(D Script). 139 */ 140 Array getScriptSignalList() 141 { 142 checkClassBinding!(typeof(this))(); 143 return ptrcall!(Array)(GDNativeClassBinding.getScriptSignalList, _godot_object); 144 } 145 /** 146 147 */ 148 String getSourceCode() const 149 { 150 checkClassBinding!(typeof(this))(); 151 return ptrcall!(String)(GDNativeClassBinding.getSourceCode, _godot_object); 152 } 153 /** 154 Returns `true` if the script, or a base class, defines a signal with the given name. 155 */ 156 bool hasScriptSignal(in String signal_name) const 157 { 158 checkClassBinding!(typeof(this))(); 159 return ptrcall!(bool)(GDNativeClassBinding.hasScriptSignal, _godot_object, signal_name); 160 } 161 /** 162 Returns `true` if the script contains non-empty source code. 163 */ 164 bool hasSourceCode() const 165 { 166 checkClassBinding!(typeof(this))(); 167 return ptrcall!(bool)(GDNativeClassBinding.hasSourceCode, _godot_object); 168 } 169 /** 170 Returns `true` if `base_object` is an instance of this script. 171 */ 172 bool instanceHas(GodotObject base_object) const 173 { 174 checkClassBinding!(typeof(this))(); 175 return ptrcall!(bool)(GDNativeClassBinding.instanceHas, _godot_object, base_object); 176 } 177 /** 178 Returns `true` if the script is a tool script. A tool script can run in the editor. 179 */ 180 bool isTool() const 181 { 182 checkClassBinding!(typeof(this))(); 183 return ptrcall!(bool)(GDNativeClassBinding.isTool, _godot_object); 184 } 185 /** 186 Reloads the script's class implementation. Returns an error code. 187 */ 188 GodotError reload(in bool keep_state = false) 189 { 190 checkClassBinding!(typeof(this))(); 191 return ptrcall!(GodotError)(GDNativeClassBinding.reload, _godot_object, keep_state); 192 } 193 /** 194 195 */ 196 void setSourceCode(in String source) 197 { 198 checkClassBinding!(typeof(this))(); 199 ptrcall!(void)(GDNativeClassBinding.setSourceCode, _godot_object, source); 200 } 201 /** 202 The script source code or an empty string if source code is not available. When set, does not reload the class implementation automatically. 203 */ 204 @property String sourceCode() 205 { 206 return getSourceCode(); 207 } 208 /// ditto 209 @property void sourceCode(String v) 210 { 211 setSourceCode(v); 212 } 213 }