node.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*************************************************************************/
  2. /* node.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. enum NameCasing {
  60. NAME_CASING_PASCAL_CASE,
  61. NAME_CASING_CAMEL_CASE,
  62. NAME_CASING_SNAKE_CASE
  63. };
  64. struct Comparator {
  65. bool operator()(const Node *p_a, const Node *p_b) const { return p_b->is_greater_than(p_a); }
  66. };
  67. struct ComparatorWithPriority {
  68. 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; }
  69. };
  70. static int orphan_node_count;
  71. private:
  72. struct GroupData {
  73. bool persistent;
  74. SceneTree::Group *group;
  75. GroupData() { persistent = false; }
  76. };
  77. struct Data {
  78. String filename;
  79. Ref<SceneState> instance_state;
  80. Ref<SceneState> inherited_state;
  81. Node *parent;
  82. Node *owner;
  83. Vector<Node *> children; // list of children
  84. int pos;
  85. int depth;
  86. int blocked; // safeguard that throws an error when attempting to modify the tree in a harmful way while being traversed.
  87. StringName name;
  88. SceneTree *tree;
  89. bool inside_tree;
  90. 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
  91. bool ready_first;
  92. #ifdef TOOLS_ENABLED
  93. NodePath import_path; //path used when imported, used by scene editors to keep tracking
  94. #endif
  95. Viewport *viewport;
  96. Map<StringName, GroupData> grouped;
  97. List<Node *>::Element *OW; // owned element
  98. List<Node *> owned;
  99. PauseMode pause_mode;
  100. Node *pause_owner;
  101. int network_master;
  102. Map<StringName, MultiplayerAPI::RPCMode> rpc_methods;
  103. Map<StringName, MultiplayerAPI::RPCMode> rpc_properties;
  104. // variables used to properly sort the node when processing, ignored otherwise
  105. //should move all the stuff below to bits
  106. bool physics_process;
  107. bool idle_process;
  108. int process_priority;
  109. bool physics_process_internal;
  110. bool idle_process_internal;
  111. bool input;
  112. bool unhandled_input;
  113. bool unhandled_key_input;
  114. bool parent_owned;
  115. bool in_constructor;
  116. bool use_placeholder;
  117. bool display_folded;
  118. bool editable_instance;
  119. mutable NodePath *path_cache;
  120. } data;
  121. Ref<MultiplayerAPI> multiplayer;
  122. void _print_tree_pretty(const String &prefix, const bool last);
  123. void _print_tree(const Node *p_node);
  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. void _generate_serial_child_name(const Node *p_child, StringName &name) const;
  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 = nullptr) 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. protected:
  150. void _block() { data.blocked++; }
  151. void _unblock() { data.blocked--; }
  152. void _notification(int p_notification);
  153. virtual void add_child_notify(Node *p_child);
  154. virtual void remove_child_notify(Node *p_child);
  155. virtual void move_child_notify(Node *p_child);
  156. void _propagate_replace_owner(Node *p_owner, Node *p_by_owner);
  157. static void _bind_methods();
  158. static String _get_name_num_separator();
  159. friend class SceneState;
  160. void _add_child_nocheck(Node *p_child, const StringName &p_name);
  161. void _set_owner_nocheck(Node *p_owner);
  162. void _set_name_nocheck(const StringName &p_name);
  163. public:
  164. enum {
  165. // you can make your own, but don't use the same numbers as other notifications in other nodes
  166. NOTIFICATION_ENTER_TREE = 10,
  167. NOTIFICATION_EXIT_TREE = 11,
  168. NOTIFICATION_MOVED_IN_PARENT = 12,
  169. NOTIFICATION_READY = 13,
  170. NOTIFICATION_PAUSED = 14,
  171. NOTIFICATION_UNPAUSED = 15,
  172. NOTIFICATION_PHYSICS_PROCESS = 16,
  173. NOTIFICATION_PROCESS = 17,
  174. NOTIFICATION_PARENTED = 18,
  175. NOTIFICATION_UNPARENTED = 19,
  176. NOTIFICATION_INSTANCED = 20,
  177. NOTIFICATION_DRAG_BEGIN = 21,
  178. NOTIFICATION_DRAG_END = 22,
  179. NOTIFICATION_PATH_CHANGED = 23,
  180. //NOTIFICATION_TRANSLATION_CHANGED = 24, moved below
  181. NOTIFICATION_INTERNAL_PROCESS = 25,
  182. NOTIFICATION_INTERNAL_PHYSICS_PROCESS = 26,
  183. NOTIFICATION_POST_ENTER_TREE = 27,
  184. //keep these linked to node
  185. NOTIFICATION_WM_MOUSE_ENTER = MainLoop::NOTIFICATION_WM_MOUSE_ENTER,
  186. NOTIFICATION_WM_MOUSE_EXIT = MainLoop::NOTIFICATION_WM_MOUSE_EXIT,
  187. NOTIFICATION_WM_FOCUS_IN = MainLoop::NOTIFICATION_WM_FOCUS_IN,
  188. NOTIFICATION_WM_FOCUS_OUT = MainLoop::NOTIFICATION_WM_FOCUS_OUT,
  189. NOTIFICATION_WM_QUIT_REQUEST = MainLoop::NOTIFICATION_WM_QUIT_REQUEST,
  190. NOTIFICATION_WM_GO_BACK_REQUEST = MainLoop::NOTIFICATION_WM_GO_BACK_REQUEST,
  191. NOTIFICATION_WM_UNFOCUS_REQUEST = MainLoop::NOTIFICATION_WM_UNFOCUS_REQUEST,
  192. NOTIFICATION_OS_MEMORY_WARNING = MainLoop::NOTIFICATION_OS_MEMORY_WARNING,
  193. NOTIFICATION_TRANSLATION_CHANGED = MainLoop::NOTIFICATION_TRANSLATION_CHANGED,
  194. NOTIFICATION_WM_ABOUT = MainLoop::NOTIFICATION_WM_ABOUT,
  195. NOTIFICATION_CRASH = MainLoop::NOTIFICATION_CRASH,
  196. NOTIFICATION_OS_IME_UPDATE = MainLoop::NOTIFICATION_OS_IME_UPDATE,
  197. NOTIFICATION_APP_RESUMED = MainLoop::NOTIFICATION_APP_RESUMED,
  198. NOTIFICATION_APP_PAUSED = MainLoop::NOTIFICATION_APP_PAUSED
  199. };
  200. /* NODE/TREE */
  201. StringName get_name() const;
  202. void set_name(const String &p_name);
  203. void add_child(Node *p_child, bool p_legible_unique_name = false);
  204. void add_child_below_node(Node *p_node, Node *p_child, bool p_legible_unique_name = false);
  205. void remove_child(Node *p_child);
  206. int get_child_count() const;
  207. Node *get_child(int p_index) const;
  208. bool has_node(const NodePath &p_path) const;
  209. Node *get_node(const NodePath &p_path) const;
  210. Node *get_node_or_null(const NodePath &p_path) const;
  211. Node *find_node(const String &p_mask, bool p_recursive = true, bool p_owned = true) const;
  212. bool has_node_and_resource(const NodePath &p_path) const;
  213. Node *get_node_and_resource(const NodePath &p_path, RES &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property = true) const;
  214. Node *get_parent() const;
  215. Node *find_parent(const String &p_mask) const;
  216. _FORCE_INLINE_ SceneTree *get_tree() const {
  217. ERR_FAIL_COND_V(!data.tree, nullptr);
  218. return data.tree;
  219. }
  220. _FORCE_INLINE_ bool is_inside_tree() const { return data.inside_tree; }
  221. bool is_a_parent_of(const Node *p_node) const;
  222. bool is_greater_than(const Node *p_node) const;
  223. NodePath get_path() const;
  224. NodePath get_path_to(const Node *p_node) const;
  225. Node *find_common_parent_with(const Node *p_node) const;
  226. void add_to_group(const StringName &p_identifier, bool p_persistent = false);
  227. void remove_from_group(const StringName &p_identifier);
  228. bool is_in_group(const StringName &p_identifier) const;
  229. struct GroupInfo {
  230. StringName name;
  231. bool persistent;
  232. };
  233. void get_groups(List<GroupInfo> *p_groups) const;
  234. int get_persistent_group_count() const;
  235. void move_child(Node *p_child, int p_pos);
  236. void raise();
  237. void set_owner(Node *p_owner);
  238. Node *get_owner() const;
  239. void get_owned_by(Node *p_by, List<Node *> *p_owned);
  240. void remove_and_skip();
  241. int get_index() const;
  242. void print_tree();
  243. void print_tree_pretty();
  244. void set_filename(const String &p_filename);
  245. String get_filename() const;
  246. void set_editor_description(const String &p_editor_description);
  247. String get_editor_description() const;
  248. void set_editable_instance(Node *p_node, bool p_editable);
  249. bool is_editable_instance(const Node *p_node) const;
  250. Node *get_deepest_editable_node(Node *start_node) const;
  251. virtual String to_string();
  252. /* NOTIFICATIONS */
  253. void propagate_notification(int p_notification);
  254. void propagate_call(const StringName &p_method, const Array &p_args = Array(), const bool p_parent_first = false);
  255. /* PROCESSING */
  256. void set_physics_process(bool p_process);
  257. float get_physics_process_delta_time() const;
  258. bool is_physics_processing() const;
  259. void set_process(bool p_idle_process);
  260. float get_process_delta_time() const;
  261. bool is_processing() const;
  262. void set_physics_process_internal(bool p_process_internal);
  263. bool is_physics_processing_internal() const;
  264. void set_process_internal(bool p_idle_process_internal);
  265. bool is_processing_internal() const;
  266. void set_process_priority(int p_priority);
  267. int get_process_priority() const;
  268. void set_process_input(bool p_enable);
  269. bool is_processing_input() const;
  270. void set_process_unhandled_input(bool p_enable);
  271. bool is_processing_unhandled_input() const;
  272. void set_process_unhandled_key_input(bool p_enable);
  273. bool is_processing_unhandled_key_input() const;
  274. int get_position_in_parent() const;
  275. Node *duplicate(int p_flags = DUPLICATE_GROUPS | DUPLICATE_SIGNALS | DUPLICATE_SCRIPTS) const;
  276. Node *duplicate_and_reown(const Map<Node *, Node *> &p_reown_map) const;
  277. #ifdef TOOLS_ENABLED
  278. Node *duplicate_from_editor(Map<const Node *, Node *> &r_duplimap) const;
  279. Node *duplicate_from_editor(Map<const Node *, Node *> &r_duplimap, const Map<RES, RES> &p_resource_remap) const;
  280. void remap_node_resources(Node *p_node, const Map<RES, RES> &p_resource_remap) const;
  281. void remap_nested_resources(RES p_resource, const Map<RES, RES> &p_resource_remap) const;
  282. #endif
  283. // used by editors, to save what has changed only
  284. void set_scene_instance_state(const Ref<SceneState> &p_state);
  285. Ref<SceneState> get_scene_instance_state() const;
  286. void set_scene_inherited_state(const Ref<SceneState> &p_state);
  287. Ref<SceneState> get_scene_inherited_state() const;
  288. void set_scene_instance_load_placeholder(bool p_enable);
  289. bool get_scene_instance_load_placeholder() const;
  290. static Vector<Variant> make_binds(VARIANT_ARG_LIST);
  291. void replace_by(Node *p_node, bool p_keep_data = false);
  292. void set_pause_mode(PauseMode p_mode);
  293. PauseMode get_pause_mode() const;
  294. bool can_process() const;
  295. bool can_process_notification(int p_what) const;
  296. void request_ready();
  297. static void print_stray_nodes();
  298. #ifdef TOOLS_ENABLED
  299. String validate_child_name(Node *p_child);
  300. #endif
  301. void queue_delete();
  302. //hacks for speed
  303. static void set_human_readable_collision_renaming(bool p_enabled);
  304. static void init_node_hrcr();
  305. void force_parent_owned() { data.parent_owned = true; } //hack to avoid duplicate nodes
  306. void set_import_path(const NodePath &p_import_path); //path used when imported, used by scene editors to keep tracking
  307. NodePath get_import_path() const;
  308. bool is_owned_by_parent() const;
  309. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  310. void clear_internal_tree_resource_paths();
  311. _FORCE_INLINE_ Viewport *get_viewport() const { return data.viewport; }
  312. virtual String get_configuration_warning() const;
  313. void update_configuration_warning();
  314. void set_display_folded(bool p_folded);
  315. bool is_displayed_folded() const;
  316. /* NETWORK */
  317. void set_network_master(int p_peer_id, bool p_recursive = true);
  318. int get_network_master() const;
  319. bool is_network_master() const;
  320. void rpc_config(const StringName &p_method, MultiplayerAPI::RPCMode p_mode); // config a local method for RPC
  321. void rset_config(const StringName &p_property, MultiplayerAPI::RPCMode p_mode); // config a local property for RPC
  322. void rpc(const StringName &p_method, VARIANT_ARG_LIST); //rpc call, honors RPCMode
  323. void rpc_unreliable(const StringName &p_method, VARIANT_ARG_LIST); //rpc call, honors RPCMode
  324. void rpc_id(int p_peer_id, const StringName &p_method, VARIANT_ARG_LIST); //rpc call, honors RPCMode
  325. void rpc_unreliable_id(int p_peer_id, const StringName &p_method, VARIANT_ARG_LIST); //rpc call, honors RPCMode
  326. void rset(const StringName &p_property, const Variant &p_value); //remote set call, honors RPCMode
  327. void rset_unreliable(const StringName &p_property, const Variant &p_value); //remote set call, honors RPCMode
  328. void rset_id(int p_peer_id, const StringName &p_property, const Variant &p_value); //remote set call, honors RPCMode
  329. void rset_unreliable_id(int p_peer_id, const StringName &p_property, const Variant &p_value); //remote set call, honors RPCMode
  330. void rpcp(int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount);
  331. void rsetp(int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value);
  332. Ref<MultiplayerAPI> get_multiplayer() const;
  333. Ref<MultiplayerAPI> get_custom_multiplayer() const;
  334. void set_custom_multiplayer(Ref<MultiplayerAPI> p_multiplayer);
  335. const Map<StringName, MultiplayerAPI::RPCMode>::Element *get_node_rpc_mode(const StringName &p_method);
  336. const Map<StringName, MultiplayerAPI::RPCMode>::Element *get_node_rset_mode(const StringName &p_property);
  337. Node();
  338. ~Node();
  339. };
  340. VARIANT_ENUM_CAST(Node::DuplicateFlags);
  341. typedef Set<Node *, Node::Comparator> NodeSet;
  342. #endif