1 /** 2 An abstraction of a serialized scene. 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.packedscene; 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.resource; 25 import godot.scenestate; 26 import godot.node; 27 /** 28 An abstraction of a serialized scene. 29 30 A simplified interface to a scene file. Provides access to operations and checks that can be performed on the scene resource itself. 31 Can be used to save a node to a file. When saving, the node as well as all the nodes it owns get saved (see `owner` property on $(D Node)). 32 $(B Note:) The node doesn't need to own itself. 33 $(B Example of loading a saved scene:) 34 35 36 # Use `load()` instead of `preload()` if the path isn't known at compile-time. 37 var scene = preload("res://scene.tscn").instance() 38 # Add the node as a child of the node the script is attached to. 39 add_child(scene) 40 41 42 $(B Example of saving a node with different owners:) The following example creates 3 objects: `Node2D` (`node`), `RigidBody2D` (`rigid`) and `CollisionObject2D` (`collision`). `collision` is a child of `rigid` which is a child of `node`. Only `rigid` is owned by `node` and `pack` will therefore only save those two nodes, but not `collision`. 43 44 45 # Create the objects. 46 var node = Node2D.new() 47 var rigid = RigidBody2D.new() 48 var collision = CollisionShape2D.new() 49 50 # Create the object hierarchy. 51 rigid.add_child(collision) 52 node.add_child(rigid) 53 54 # Change owner of `rigid`, but not of `collision`. 55 rigid.owner = node 56 57 var scene = PackedScene.new() 58 # Only `node` and `rigid` are now packed. 59 var result = scene.pack(node) 60 if result == OK: 61 var error = ResourceSaver.save("res://path/name.scn", scene) # Or "user://..." 62 if error != OK: 63 push_error("An error occurred while saving the scene to disk.") 64 65 66 */ 67 @GodotBaseClass struct PackedScene 68 { 69 package(godot) enum string _GODOT_internal_name = "PackedScene"; 70 public: 71 @nogc nothrow: 72 union { /** */ godot_object _godot_object; /** */ Resource _GODOT_base; } 73 alias _GODOT_base this; 74 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 75 package(godot) __gshared bool _classBindingInitialized = false; 76 package(godot) static struct GDNativeClassBinding 77 { 78 __gshared: 79 @GodotName("_get_bundled_scene") GodotMethod!(Dictionary) _getBundledScene; 80 @GodotName("_set_bundled_scene") GodotMethod!(void, Dictionary) _setBundledScene; 81 @GodotName("can_instance") GodotMethod!(bool) canInstance; 82 @GodotName("get_state") GodotMethod!(SceneState) getState; 83 @GodotName("instance") GodotMethod!(Node, long) instance; 84 @GodotName("pack") GodotMethod!(GodotError, Node) pack; 85 } 86 /// 87 pragma(inline, true) bool opEquals(in PackedScene other) const 88 { return _godot_object.ptr is other._godot_object.ptr; } 89 /// 90 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 91 { _godot_object.ptr = n; return null; } 92 /// 93 pragma(inline, true) bool opEquals(typeof(null) n) const 94 { return _godot_object.ptr is n; } 95 /// 96 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 97 mixin baseCasts; 98 /// Construct a new instance of PackedScene. 99 /// Note: use `memnew!PackedScene` instead. 100 static PackedScene _new() 101 { 102 static godot_class_constructor constructor; 103 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("PackedScene"); 104 if(constructor is null) return typeof(this).init; 105 return cast(PackedScene)(constructor()); 106 } 107 @disable new(size_t s); 108 /// 109 enum GenEditState : int 110 { 111 /** 112 If passed to $(D instance), blocks edits to the scene state. 113 */ 114 genEditStateDisabled = 0, 115 /** 116 If passed to $(D instance), provides local scene resources to the local scene. 117 $(B Note:) Only available in editor builds. 118 */ 119 genEditStateInstance = 1, 120 /** 121 If passed to $(D instance), provides local scene resources to the local scene. Only the main scene should receive the main edit state. 122 $(B Note:) Only available in editor builds. 123 */ 124 genEditStateMain = 2, 125 } 126 /// 127 enum Constants : int 128 { 129 genEditStateDisabled = 0, 130 genEditStateInstance = 1, 131 genEditStateMain = 2, 132 } 133 /** 134 135 */ 136 Dictionary _getBundledScene() const 137 { 138 Array _GODOT_args = Array.make(); 139 String _GODOT_method_name = String("_get_bundled_scene"); 140 return this.callv(_GODOT_method_name, _GODOT_args).as!(RefOrT!Dictionary); 141 } 142 /** 143 144 */ 145 void _setBundledScene(in Dictionary arg0) 146 { 147 Array _GODOT_args = Array.make(); 148 _GODOT_args.append(arg0); 149 String _GODOT_method_name = String("_set_bundled_scene"); 150 this.callv(_GODOT_method_name, _GODOT_args); 151 } 152 /** 153 Returns `true` if the scene file has nodes. 154 */ 155 bool canInstance() const 156 { 157 checkClassBinding!(typeof(this))(); 158 return ptrcall!(bool)(GDNativeClassBinding.canInstance, _godot_object); 159 } 160 /** 161 Returns the `SceneState` representing the scene file contents. 162 */ 163 Ref!SceneState getState() 164 { 165 checkClassBinding!(typeof(this))(); 166 return ptrcall!(SceneState)(GDNativeClassBinding.getState, _godot_object); 167 } 168 /** 169 Instantiates the scene's node hierarchy. Triggers child scene instantiation(s). Triggers a $(D constant Node.NOTIFICATION_INSTANCED) notification on the root node. 170 */ 171 Node instance(in long edit_state = 0) const 172 { 173 checkClassBinding!(typeof(this))(); 174 return ptrcall!(Node)(GDNativeClassBinding.instance, _godot_object, edit_state); 175 } 176 /** 177 Pack will ignore any sub-nodes not owned by given node. See $(D Node.owner). 178 */ 179 GodotError pack(Node path) 180 { 181 checkClassBinding!(typeof(this))(); 182 return ptrcall!(GodotError)(GDNativeClassBinding.pack, _godot_object, path); 183 } 184 /** 185 A dictionary representation of the scene contents. 186 Available keys include "rnames" and "variants" for resources, "node_count", "nodes", "node_paths" for nodes, "editable_instances" for base scene children overrides, "conn_count" and "conns" for signal connections, and "version" for the format style of the PackedScene. 187 */ 188 @property Dictionary _bundled() 189 { 190 return _getBundledScene(); 191 } 192 /// ditto 193 @property void _bundled(Dictionary v) 194 { 195 _setBundledScene(v); 196 } 197 }