packed_scene.h 7.6 KB

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