node.h 15 KB

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