node.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*************************************************************************/
  2. /* node.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 NODE_H
  30. #define NODE_H
  31. #include "object.h"
  32. #include "path_db.h"
  33. #include "map.h"
  34. #include "object_type_db.h"
  35. #include "script_language.h"
  36. #include "scene/main/scene_main_loop.h"
  37. class Viewport;
  38. class SceneState;
  39. class Node : public Object {
  40. OBJ_TYPE( Node, Object );
  41. OBJ_CATEGORY("Nodes");
  42. public:
  43. enum PauseMode {
  44. PAUSE_MODE_INHERIT,
  45. PAUSE_MODE_STOP,
  46. PAUSE_MODE_PROCESS
  47. };
  48. struct Comparator {
  49. bool operator()(const Node* p_a, const Node* p_b) const { return p_b->is_greater_than(p_a); }
  50. };
  51. private:
  52. struct GroupData {
  53. bool persistent;
  54. GroupData() { persistent=false; }
  55. };
  56. struct Data {
  57. String filename;
  58. Ref<SceneState> instance_state;
  59. Ref<SceneState> inherited_state;
  60. HashMap<NodePath,int> editable_instances;
  61. Node *parent;
  62. Node *owner;
  63. Vector<Node*> children; // list of children
  64. int pos;
  65. int depth;
  66. int blocked; // safeguard that throws an error when attempting to modify the tree in a harmful way while being traversed.
  67. StringName name;
  68. SceneTree *tree;
  69. bool inside_tree;
  70. #ifdef TOOLS_ENABLED
  71. NodePath import_path; //path used when imported, used by scene editors to keep tracking
  72. #endif
  73. Viewport *viewport;
  74. HashMap< StringName, GroupData,StringNameHasher> grouped;
  75. List<Node*>::Element *OW; // owned element
  76. List<Node*> owned;
  77. PauseMode pause_mode;
  78. Node *pause_owner;
  79. // variables used to properly sort the node when processing, ignored otherwise
  80. //should move all the stuff below to bits
  81. bool fixed_process;
  82. bool idle_process;
  83. bool input;
  84. bool unhandled_input;
  85. bool unhandled_key_input;
  86. bool parent_owned;
  87. bool in_constructor;
  88. bool use_placeholder;
  89. } data;
  90. void _print_tree(const Node *p_node);
  91. virtual bool _use_builtin_script() const { return true; }
  92. Node *_get_node(const NodePath& p_path) const;
  93. Node *_get_child_by_name(const StringName& p_name) const;
  94. void _validate_child_name(Node *p_name, bool p_force_human_readable=false);
  95. void _propagate_reverse_notification(int p_notification);
  96. void _propagate_deferred_notification(int p_notification, bool p_reverse);
  97. void _propagate_enter_tree();
  98. void _propagate_ready();
  99. void _propagate_exit_tree();
  100. void _propagate_validate_owner();
  101. void _print_stray_nodes();
  102. void _propagate_pause_owner(Node*p_owner);
  103. Array _get_node_and_resource(const NodePath& p_path);
  104. void _duplicate_signals(const Node* p_original,Node* p_copy) const;
  105. void _duplicate_and_reown(Node* p_new_parent, const Map<Node*,Node*>& p_reown_map) const;
  106. Node *_duplicate(bool p_use_instancing) const;
  107. Array _get_children() const;
  108. Array _get_groups() const;
  109. friend class SceneTree;
  110. void _set_tree(SceneTree *p_tree);
  111. protected:
  112. void _block() { data.blocked++; }
  113. void _unblock() { data.blocked--; }
  114. void _notification(int p_notification);
  115. virtual void add_child_notify(Node *p_child);
  116. virtual void remove_child_notify(Node *p_child);
  117. virtual void move_child_notify(Node *p_child);
  118. //void remove_and_delete_child(Node *p_child);
  119. void _propagate_replace_owner(Node *p_owner,Node* p_by_owner);
  120. static void _bind_methods();
  121. friend class SceneState;
  122. void _add_child_nocheck(Node* p_child,const StringName& p_name);
  123. void _set_owner_nocheck(Node* p_owner);
  124. void _set_name_nocheck(const StringName& p_name);
  125. public:
  126. enum {
  127. // you can make your own, but don't use the same numbers as other notifications in other nodes
  128. NOTIFICATION_ENTER_TREE=10,
  129. NOTIFICATION_EXIT_TREE =11,
  130. NOTIFICATION_MOVED_IN_PARENT =12,
  131. NOTIFICATION_READY=13,
  132. //NOTIFICATION_PARENT_DECONFIGURED =15, - it's confusing, it's going away
  133. NOTIFICATION_PAUSED=14,
  134. NOTIFICATION_UNPAUSED=15,
  135. NOTIFICATION_FIXED_PROCESS = 16,
  136. NOTIFICATION_PROCESS = 17,
  137. NOTIFICATION_PARENTED=18,
  138. NOTIFICATION_UNPARENTED=19,
  139. NOTIFICATION_INSTANCED=20,
  140. };
  141. /* NODE/TREE */
  142. StringName get_name() const;
  143. void set_name(const String& p_name);
  144. void add_child(Node *p_child,bool p_legible_unique_name=false);
  145. void remove_child(Node *p_child);
  146. int get_child_count() const;
  147. Node *get_child(int p_index) const;
  148. bool has_node(const NodePath& p_path) const;
  149. Node *get_node(const NodePath& p_path) const;
  150. Node* find_node(const String& p_mask,bool p_recursive=true,bool p_owned=true) const;
  151. bool has_node_and_resource(const NodePath& p_path) const;
  152. Node *get_node_and_resource(const NodePath& p_path,RES& r_res) const;
  153. Node *get_parent() const;
  154. _FORCE_INLINE_ SceneTree *get_tree() const { ERR_FAIL_COND_V( !data.tree, NULL ); return data.tree; }
  155. _FORCE_INLINE_ bool is_inside_tree() const { return data.inside_tree; }
  156. bool is_a_parent_of(const Node *p_node) const;
  157. bool is_greater_than(const Node *p_node) const;
  158. NodePath get_path() const;
  159. NodePath get_path_to(const Node *p_node) const;
  160. void add_to_group(const StringName& p_identifier,bool p_persistent=false);
  161. void remove_from_group(const StringName& p_identifier);
  162. bool is_in_group(const StringName& p_identifier) const;
  163. struct GroupInfo {
  164. StringName name;
  165. bool persistent;
  166. };
  167. void get_groups(List<GroupInfo> *p_groups) const;
  168. void move_child(Node *p_child,int p_pos);
  169. void raise();
  170. void set_owner(Node *p_owner);
  171. Node *get_owner() const;
  172. void get_owned_by(Node *p_by,List<Node*> *p_owned);
  173. void remove_and_skip();
  174. int get_index() const;
  175. void print_tree();
  176. void set_filename(const String& p_filename);
  177. String get_filename() const;
  178. void set_editable_instance(Node* p_node,bool p_editable);
  179. bool is_editable_instance(Node* p_node) const;
  180. void set_editable_instances(const HashMap<NodePath,int>& p_editable_instances);
  181. HashMap<NodePath,int> get_editable_instances() const;
  182. /* NOTIFICATIONS */
  183. void propagate_notification(int p_notification);
  184. /* PROCESSING */
  185. void set_fixed_process(bool p_process);
  186. float get_fixed_process_delta_time() const;
  187. bool is_fixed_processing() const;
  188. void set_process(bool p_process);
  189. float get_process_delta_time() const;
  190. bool is_processing() const;
  191. void set_process_input(bool p_enable);
  192. bool is_processing_input() const;
  193. void set_process_unhandled_input(bool p_enable);
  194. bool is_processing_unhandled_input() const;
  195. void set_process_unhandled_key_input(bool p_enable);
  196. bool is_processing_unhandled_key_input() const;
  197. int get_position_in_parent() const;
  198. Node *duplicate(bool p_use_instancing=false) const;
  199. Node *duplicate_and_reown(const Map<Node*,Node*>& p_reown_map) const;
  200. //Node *clone_tree() const;
  201. // used by editors, to save what has changed only
  202. void set_scene_instance_state(const Ref<SceneState>& p_state);
  203. Ref<SceneState> get_scene_instance_state() const;
  204. void set_scene_inherited_state(const Ref<SceneState>& p_state);
  205. Ref<SceneState> get_scene_inherited_state() const;
  206. void set_scene_instance_load_placeholder(bool p_enable);
  207. bool get_scene_instance_load_placeholder() const;
  208. static Vector<Variant> make_binds(VARIANT_ARG_LIST);
  209. void replace_by(Node* p_node,bool p_keep_data=false);
  210. void set_pause_mode(PauseMode p_mode);
  211. PauseMode get_pause_mode() const;
  212. bool can_process() const;
  213. static void print_stray_nodes();
  214. String validate_child_name(const String& p_name) const;
  215. void queue_delete();
  216. //shitty hacks for speed
  217. static void set_human_readable_collision_renaming(bool p_enabled);
  218. static void init_node_hrcr();
  219. void force_parent_owned() { data.parent_owned=true; } //hack to avoid duplicate nodes
  220. #ifdef TOOLS_ENABLED
  221. void set_import_path(const NodePath& p_import_path); //path used when imported, used by scene editors to keep tracking
  222. NodePath get_import_path() const;
  223. #endif
  224. void get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const;
  225. void clear_internal_tree_resource_paths();
  226. _FORCE_INLINE_ Viewport *get_viewport() const { return data.viewport; }
  227. /* CANVAS */
  228. Node();
  229. ~Node();
  230. };
  231. typedef Set<Node*,Node::Comparator> NodeSet;
  232. #endif