packed_scene.h
1 /*************************************************************************/
2 /* packed_scene.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef PACKED_SCENE_H
30 #define PACKED_SCENE_H
31 
32 #include "resource.h"
33 #include "scene/main/node.h"
34 
35 
36 class SceneState : public Reference {
37 
38  OBJ_TYPE( SceneState, Reference );
39 
40 
41  Vector<StringName> names;
42  Vector<Variant> variants;
43  Vector<NodePath> node_paths;
44  Vector<NodePath> editable_instances;
45  mutable HashMap<NodePath,int> node_path_cache;
46  mutable Map<int,int> base_scene_node_remap;
47 
48  int base_scene_idx;
49 
50  enum {
51  NO_PARENT_SAVED=0x7FFFFFFF,
52  };
53 
54  struct NodeData {
55 
56  int parent;
57  int owner;
58  int type;
59  int name;
60  int instance;
61 
62  struct Property {
63 
64  int name;
65  int value;
66  };
67 
68  Vector<Property> properties;
69  Vector<int> groups;
70 
71  };
72 
73  struct PackState {
74  Ref<SceneState> state;
75  int node;
76  PackState() { node=-1; }
77  };
78 
79  Vector<NodeData> nodes;
80 
81  struct ConnectionData {
82 
83  int from;
84  int to;
85  int signal;
86  int method;
87  int flags;
88  Vector<int> binds;
89  };
90 
91  Vector<ConnectionData> connections;
92 
93 
94  Error _parse_node(Node *p_owner,Node *p_node,int p_parent_idx, Map<StringName,int> &name_map,HashMap<Variant,int,VariantHasher> &variant_map,Map<Node*,int> &node_map,Map<Node*,int> &nodepath_map);
95  Error _parse_connections(Node *p_owner,Node *p_node, Map<StringName,int> &name_map,HashMap<Variant,int,VariantHasher> &variant_map,Map<Node*,int> &node_map,Map<Node*,int> &nodepath_map);
96 
97  String path;
98 
99  uint64_t last_modified_time;
100 
101  _FORCE_INLINE_ Ref<SceneState> _get_base_scene_state() const;
102 
103  static bool disable_placeholders;
104 
105  DVector<String> _get_node_groups(int p_idx) const;
106 
107 protected:
108 
109  static void _bind_methods();
110 
111 public:
112 
113  enum {
114  FLAG_ID_IS_PATH=(1<<30),
115  TYPE_INSTANCED=0x7FFFFFFF,
116  FLAG_INSTANCE_IS_PLACEHOLDER=(1<<30),
117  FLAG_MASK=(1<<24)-1,
118  };
119 
120  static void set_disable_placeholders(bool p_disable);
121 
122  int find_node_by_path(const NodePath& p_node) const;
123  Variant get_property_value(int p_node,const StringName& p_property,bool &found) const;
124  bool is_node_in_group(int p_node,const StringName& p_group) const;
125  bool is_connection(int p_node,const StringName& p_signal,int p_to_node,const StringName& p_to_method) const;
126 
127 
128  void set_bundled_scene(const Dictionary& p_dictionary);
129  Dictionary get_bundled_scene() const;
130 
131  Error pack(Node *p_scene);
132 
133  void set_path(const String &p_path);
134  String get_path() const;
135 
136  void clear();
137 
138  bool can_instance() const;
139  Node *instance(bool p_gen_edit_state=false) const;
140 
141 
142  //unbuild API
143 
144  int get_node_count() const;
145  StringName get_node_type(int p_idx) const;
146  StringName get_node_name(int p_idx) const;
147  NodePath get_node_path(int p_idx,bool p_for_parent=false) const;
148  NodePath get_node_owner_path(int p_idx) const;
149  Ref<PackedScene> get_node_instance(int p_idx) const;
150  String get_node_instance_placeholder(int p_idx) const;
151  bool is_node_instance_placeholder(int p_idx) const;
152  Vector<StringName> get_node_groups(int p_idx) const;
153 
154  int get_node_property_count(int p_idx) const;
155  StringName get_node_property_name(int p_idx,int p_prop) const;
156  Variant get_node_property_value(int p_idx,int p_prop) const;
157 
158  int get_connection_count() const;
159  NodePath get_connection_source(int p_idx) const;
160  StringName get_connection_signal(int p_idx) const;
161  NodePath get_connection_target(int p_idx) const;
162  StringName get_connection_method(int p_idx) const;
163  int get_connection_flags(int p_idx) const;
164  Array get_connection_binds(int p_idx) const;
165 
166  Vector<NodePath> get_editable_instances() const;
167 
168  //build API
169 
170  int add_name(const StringName& p_name);
171  int add_value(const Variant& p_value);
172  int add_node_path(const NodePath& p_path);
173  int add_node(int p_parent,int p_owner,int p_type,int p_name, int p_instance);
174  void add_node_property(int p_node,int p_name,int p_value);
175  void add_node_group(int p_node,int p_group);
176  void set_base_scene(int p_idx);
177  void add_connection(int p_from,int p_to, int p_signal, int p_method, int p_flags,const Vector<int>& p_binds);
178  void add_editable_instance(const NodePath& p_path);
179 
180  virtual void set_last_modified_time(uint64_t p_time) { last_modified_time=p_time; }
181  uint64_t get_last_modified_time() const { return last_modified_time; }
182 
183 
184  SceneState();
185 };
186 
187 class PackedScene : public Resource {
188 
189  OBJ_TYPE(PackedScene, Resource );
190  RES_BASE_EXTENSION("scn");
191 
192  Ref<SceneState> state;
193 
194  void _set_bundled_scene(const Dictionary& p_scene);
195  Dictionary _get_bundled_scene() const;
196 
197 protected:
198 
199  static void _bind_methods();
200 public:
201 
202 
203  Error pack(Node *p_scene);
204 
205  void clear();
206 
207  bool can_instance() const;
208  Node *instance(bool p_gen_edit_state=false) const;
209 
210  void recreate_state();
211  void replace_state(Ref<SceneState> p_by);
212 
213  virtual void set_path(const String& p_path,bool p_take_over=false);
214 #ifdef TOOLS_ENABLED
215  virtual void set_last_modified_time(uint64_t p_time) { state->set_last_modified_time(p_time); }
216 
217 #endif
218  Ref<SceneState> get_state();
219 
220  PackedScene();
221 
222 };
223 
224 #endif // SCENE_PRELOADER_H
Definition: array.h:38
Definition: variant.h:74
Definition: path_db.h:41
Definition: node.h:42
Definition: packed_scene.h:62
Definition: string_db.h:48
Definition: reference.h:40
Definition: packed_scene.h:36
Definition: resource.h:89
Definition: dictionary.h:42
Definition: packed_scene.h:187
Definition: dvector.h:43
Definition: ustring.h:64