node.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*************************************************************************/
  2. /* node.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 NODE_H
  31. #define NODE_H
  32. #include "class_db.h"
  33. #include "map.h"
  34. #include "node_path.h"
  35. #include "object.h"
  36. #include "project_settings.h"
  37. #include "scene/main/scene_tree.h"
  38. #include "script_language.h"
  39. class Viewport;
  40. class SceneState;
  41. class Node : public Object {
  42. GDCLASS(Node, Object);
  43. OBJ_CATEGORY("Nodes");
  44. public:
  45. enum PauseMode {
  46. PAUSE_MODE_INHERIT,
  47. PAUSE_MODE_STOP,
  48. PAUSE_MODE_PROCESS
  49. };
  50. enum DuplicateFlags {
  51. DUPLICATE_SIGNALS = 1,
  52. DUPLICATE_GROUPS = 2,
  53. DUPLICATE_SCRIPTS = 4,
  54. DUPLICATE_USE_INSTANCING = 8
  55. };
  56. enum RPCMode {
  57. RPC_MODE_DISABLED, //no rpc for this method, calls to this will be blocked (default)
  58. RPC_MODE_REMOTE, // using rpc() on it will call method / set property in all other peers
  59. RPC_MODE_SYNC, // using rpc() on it will call method / set property in all other peers and locally
  60. RPC_MODE_MASTER, // usinc rpc() on it will call method on wherever the master is, be it local or remote
  61. RPC_MODE_SLAVE, // usinc rpc() on it will call method for all slaves, be it local or remote
  62. };
  63. struct Comparator {
  64. bool operator()(const Node *p_a, const Node *p_b) const { return p_b->is_greater_than(p_a); }
  65. };
  66. private:
  67. struct GroupData {
  68. bool persistent;
  69. SceneTree::Group *group;
  70. GroupData() { persistent = false; }
  71. };
  72. struct Data {
  73. String filename;
  74. Ref<SceneState> instance_state;
  75. Ref<SceneState> inherited_state;
  76. HashMap<NodePath, int> editable_instances;
  77. Node *parent;
  78. Node *owner;
  79. Vector<Node *> children; // list of children
  80. int pos;
  81. int depth;
  82. int blocked; // safeguard that throws an error when attempting to modify the tree in a harmful way while being traversed.
  83. StringName name;
  84. SceneTree *tree;
  85. bool inside_tree;
  86. bool ready_notified; //this is a small hack, so if a node is added during _ready() to the tree, it corretly gets the _ready() notification
  87. bool ready_first;
  88. #ifdef TOOLS_ENABLED
  89. NodePath import_path; //path used when imported, used by scene editors to keep tracking
  90. #endif
  91. Viewport *viewport;
  92. Map<StringName, GroupData> grouped;
  93. List<Node *>::Element *OW; // owned element
  94. List<Node *> owned;
  95. PauseMode pause_mode;
  96. Node *pause_owner;
  97. int network_master;
  98. Map<StringName, RPCMode> rpc_methods;
  99. Map<StringName, RPCMode> rpc_properties;
  100. // variables used to properly sort the node when processing, ignored otherwise
  101. //should move all the stuff below to bits
  102. bool physics_process;
  103. bool idle_process;
  104. bool physics_process_internal;
  105. bool idle_process_internal;
  106. bool input;
  107. bool unhandled_input;
  108. bool unhandled_key_input;
  109. bool parent_owned;
  110. bool in_constructor;
  111. bool use_placeholder;
  112. bool display_folded;
  113. mutable NodePath *path_cache;
  114. } data;
  115. enum NameCasing {
  116. NAME_CASING_PASCAL_CASE,
  117. NAME_CASING_CAMEL_CASE,
  118. NAME_CASING_SNAKE_CASE
  119. };
  120. void _print_tree(const Node *p_node);
  121. Node *_get_node(const NodePath &p_path) const;
  122. Node *_get_child_by_name(const StringName &p_name) const;
  123. void _replace_connections_target(Node *p_new_target);
  124. void _validate_child_name(Node *p_child, bool p_force_human_readable = false);
  125. String _generate_serial_child_name(Node *p_child);
  126. void _propagate_reverse_notification(int p_notification);
  127. void _propagate_deferred_notification(int p_notification, bool p_reverse);
  128. void _propagate_enter_tree();
  129. void _propagate_ready();
  130. void _propagate_exit_tree();
  131. void _propagate_validate_owner();
  132. void _print_stray_nodes();
  133. void _propagate_pause_owner(Node *p_owner);
  134. Array _get_node_and_resource(const NodePath &p_path);
  135. void _duplicate_signals(const Node *p_original, Node *p_copy) const;
  136. void _duplicate_and_reown(Node *p_new_parent, const Map<Node *, Node *> &p_reown_map) const;
  137. Node *_duplicate(int p_flags) const;
  138. Array _get_children() const;
  139. Array _get_groups() const;
  140. Variant _rpc_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  141. Variant _rpc_unreliable_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  142. Variant _rpc_id_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  143. Variant _rpc_unreliable_id_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  144. friend class SceneTree;
  145. void _set_tree(SceneTree *p_tree);
  146. protected:
  147. void _block() { data.blocked++; }
  148. void _unblock() { data.blocked--; }
  149. void _notification(int p_notification);
  150. virtual void add_child_notify(Node *p_child);
  151. virtual void remove_child_notify(Node *p_child);
  152. virtual void move_child_notify(Node *p_child);
  153. void _propagate_replace_owner(Node *p_owner, Node *p_by_owner);
  154. static void _bind_methods();
  155. static String _get_name_num_separator();
  156. friend class SceneState;
  157. void _add_child_nocheck(Node *p_child, const StringName &p_name);
  158. void _set_owner_nocheck(Node *p_owner);
  159. void _set_name_nocheck(const StringName &p_name);
  160. public:
  161. enum {
  162. // you can make your own, but don't use the same numbers as other notifications in other nodes
  163. NOTIFICATION_ENTER_TREE = 10,
  164. NOTIFICATION_EXIT_TREE = 11,
  165. NOTIFICATION_MOVED_IN_PARENT = 12,
  166. NOTIFICATION_READY = 13,
  167. NOTIFICATION_PAUSED = 14,
  168. NOTIFICATION_UNPAUSED = 15,
  169. NOTIFICATION_PHYSICS_PROCESS = 16,
  170. NOTIFICATION_PROCESS = 17,
  171. NOTIFICATION_PARENTED = 18,
  172. NOTIFICATION_UNPARENTED = 19,
  173. NOTIFICATION_INSTANCED = 20,
  174. NOTIFICATION_DRAG_BEGIN = 21,
  175. NOTIFICATION_DRAG_END = 22,
  176. NOTIFICATION_PATH_CHANGED = 23,
  177. NOTIFICATION_TRANSLATION_CHANGED = 24,
  178. NOTIFICATION_INTERNAL_PROCESS = 25,
  179. NOTIFICATION_INTERNAL_PHYSICS_PROCESS = 26,
  180. };
  181. /* NODE/TREE */
  182. StringName get_name() const;
  183. void set_name(const String &p_name);
  184. void add_child(Node *p_child, bool p_legible_unique_name = false);
  185. void add_child_below_node(Node *p_node, Node *p_child, bool p_legible_unique_name = false);
  186. void remove_child(Node *p_child);
  187. int get_child_count() const;
  188. Node *get_child(int p_index) const;
  189. bool has_node(const NodePath &p_path) const;
  190. Node *get_node(const NodePath &p_path) const;
  191. Node *find_node(const String &p_mask, bool p_recursive = true, bool p_owned = true) const;
  192. bool has_node_and_resource(const NodePath &p_path) const;
  193. Node *get_node_and_resource(const NodePath &p_path, RES &r_res) const;
  194. Node *get_parent() const;
  195. _FORCE_INLINE_ SceneTree *get_tree() const {
  196. ERR_FAIL_COND_V(!data.tree, NULL);
  197. return data.tree;
  198. }
  199. _FORCE_INLINE_ bool is_inside_tree() const { return data.inside_tree; }
  200. bool is_a_parent_of(const Node *p_node) const;
  201. bool is_greater_than(const Node *p_node) const;
  202. NodePath get_path() const;
  203. NodePath get_path_to(const Node *p_node) const;
  204. Node *find_common_parent_with(const Node *p_node) const;
  205. void add_to_group(const StringName &p_identifier, bool p_persistent = false);
  206. void remove_from_group(const StringName &p_identifier);
  207. bool is_in_group(const StringName &p_identifier) const;
  208. struct GroupInfo {
  209. StringName name;
  210. bool persistent;
  211. };
  212. void get_groups(List<GroupInfo> *p_groups) const;
  213. bool has_persistent_groups() const;
  214. void move_child(Node *p_child, int p_pos);
  215. void raise();
  216. void set_owner(Node *p_owner);
  217. Node *get_owner() const;
  218. void get_owned_by(Node *p_by, List<Node *> *p_owned);
  219. void remove_and_skip();
  220. int get_index() const;
  221. void print_tree();
  222. void set_filename(const String &p_filename);
  223. String get_filename() const;
  224. void set_editable_instance(Node *p_node, bool p_editable);
  225. bool is_editable_instance(Node *p_node) const;
  226. void set_editable_instances(const HashMap<NodePath, int> &p_editable_instances);
  227. HashMap<NodePath, int> get_editable_instances() const;
  228. /* NOTIFICATIONS */
  229. void propagate_notification(int p_notification);
  230. void propagate_call(const StringName &p_method, const Array &p_args = Array(), const bool p_parent_first = false);
  231. /* PROCESSING */
  232. void set_physics_process(bool p_process);
  233. float get_physics_process_delta_time() const;
  234. bool is_physics_processing() const;
  235. void set_process(bool p_idle_process);
  236. float get_process_delta_time() const;
  237. bool is_processing() const;
  238. void set_physics_process_internal(bool p_process_internal);
  239. bool is_physics_processing_internal() const;
  240. void set_process_internal(bool p_idle_process_internal);
  241. bool is_processing_internal() const;
  242. void set_process_input(bool p_enable);
  243. bool is_processing_input() const;
  244. void set_process_unhandled_input(bool p_enable);
  245. bool is_processing_unhandled_input() const;
  246. void set_process_unhandled_key_input(bool p_enable);
  247. bool is_processing_unhandled_key_input() const;
  248. int get_position_in_parent() const;
  249. Node *duplicate(int p_flags = DUPLICATE_GROUPS | DUPLICATE_SIGNALS | DUPLICATE_SCRIPTS) const;
  250. Node *duplicate_and_reown(const Map<Node *, Node *> &p_reown_map) const;
  251. //Node *clone_tree() const;
  252. // used by editors, to save what has changed only
  253. void set_scene_instance_state(const Ref<SceneState> &p_state);
  254. Ref<SceneState> get_scene_instance_state() const;
  255. void set_scene_inherited_state(const Ref<SceneState> &p_state);
  256. Ref<SceneState> get_scene_inherited_state() const;
  257. void set_scene_instance_load_placeholder(bool p_enable);
  258. bool get_scene_instance_load_placeholder() const;
  259. static Vector<Variant> make_binds(VARIANT_ARG_LIST);
  260. void replace_by(Node *p_node, bool p_keep_data = false);
  261. void set_pause_mode(PauseMode p_mode);
  262. PauseMode get_pause_mode() const;
  263. bool can_process() const;
  264. void request_ready();
  265. static void print_stray_nodes();
  266. #ifdef TOOLS_ENABLED
  267. String validate_child_name(Node *p_child);
  268. #endif
  269. void queue_delete();
  270. //shitty hacks for speed
  271. static void set_human_readable_collision_renaming(bool p_enabled);
  272. static void init_node_hrcr();
  273. void force_parent_owned() { data.parent_owned = true; } //hack to avoid duplicate nodes
  274. #ifdef TOOLS_ENABLED
  275. void set_import_path(const NodePath &p_import_path); //path used when imported, used by scene editors to keep tracking
  276. NodePath get_import_path() const;
  277. #endif
  278. bool is_owned_by_parent() const;
  279. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  280. void clear_internal_tree_resource_paths();
  281. _FORCE_INLINE_ Viewport *get_viewport() const { return data.viewport; }
  282. virtual String get_configuration_warning() const;
  283. void update_configuration_warning();
  284. void set_display_folded(bool p_folded);
  285. bool is_displayed_folded() const;
  286. /* NETWORK */
  287. void set_network_master(int p_peer_id, bool p_recursive = true);
  288. int get_network_master() const;
  289. bool is_network_master() const;
  290. void rpc_config(const StringName &p_method, RPCMode p_mode); // config a local method for RPC
  291. void rset_config(const StringName &p_property, RPCMode p_mode); // config a local property for RPC
  292. void rpc(const StringName &p_method, VARIANT_ARG_LIST); //rpc call, honors RPCMode
  293. void rpc_unreliable(const StringName &p_method, VARIANT_ARG_LIST); //rpc call, honors RPCMode
  294. void rpc_id(int p_peer_id, const StringName &p_method, VARIANT_ARG_LIST); //rpc call, honors RPCMode
  295. void rpc_unreliable_id(int p_peer_id, const StringName &p_method, VARIANT_ARG_LIST); //rpc call, honors RPCMode
  296. void rpcp(int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount);
  297. void rset(const StringName &p_property, const Variant &p_value); //remote set call, honors RPCMode
  298. void rset_unreliable(const StringName &p_property, const Variant &p_value); //remote set call, honors RPCMode
  299. void rset_id(int p_peer_id, const StringName &p_property, const Variant &p_value); //remote set call, honors RPCMode
  300. void rset_unreliable_id(int p_peer_id, const StringName &p_property, const Variant &p_value); //remote set call, honors RPCMode
  301. void rsetp(int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value);
  302. bool can_call_rpc(const StringName &p_method, int p_from) const;
  303. bool can_call_rset(const StringName &p_property, int p_from) const;
  304. Node();
  305. ~Node();
  306. };
  307. VARIANT_ENUM_CAST(Node::DuplicateFlags);
  308. typedef Set<Node *, Node::Comparator> NodeSet;
  309. #endif