node.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /**************************************************************************/
  2. /* node.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 SceneTreeTween;
  42. class Node : public Object {
  43. GDCLASS(Node, Object);
  44. OBJ_CATEGORY("Nodes");
  45. public:
  46. // N.B. Any enum stored as a bitfield should
  47. // be specified as UNSIGNED to work around
  48. // some compilers trying to store it as signed,
  49. // and requiring 1 more bit than necessary.
  50. enum PauseMode : unsigned int {
  51. PAUSE_MODE_INHERIT,
  52. PAUSE_MODE_STOP,
  53. PAUSE_MODE_PROCESS
  54. };
  55. enum PhysicsInterpolationMode : unsigned int {
  56. PHYSICS_INTERPOLATION_MODE_INHERIT,
  57. PHYSICS_INTERPOLATION_MODE_OFF,
  58. PHYSICS_INTERPOLATION_MODE_ON
  59. };
  60. enum DuplicateFlags {
  61. DUPLICATE_SIGNALS = 1,
  62. DUPLICATE_GROUPS = 2,
  63. DUPLICATE_SCRIPTS = 4,
  64. DUPLICATE_USE_INSTANCING = 8,
  65. #ifdef TOOLS_ENABLED
  66. DUPLICATE_FROM_EDITOR = 16,
  67. #endif
  68. };
  69. enum NameCasing {
  70. NAME_CASING_PASCAL_CASE,
  71. NAME_CASING_CAMEL_CASE,
  72. NAME_CASING_SNAKE_CASE
  73. };
  74. struct Comparator {
  75. bool operator()(const Node *p_a, const Node *p_b) const { return p_b->is_greater_than(p_a); }
  76. };
  77. struct ComparatorWithPriority {
  78. 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; }
  79. };
  80. static int orphan_node_count;
  81. private:
  82. struct GroupData {
  83. bool persistent;
  84. SceneTree::Group *group;
  85. GroupData() { persistent = false; }
  86. };
  87. struct Data {
  88. String filename;
  89. Ref<SceneState> instance_state;
  90. Ref<SceneState> inherited_state;
  91. Node *parent;
  92. Node *owner;
  93. Vector<Node *> children; // list of children
  94. HashMap<StringName, Node *> owned_unique_nodes;
  95. bool unique_name_in_owner = false;
  96. int pos;
  97. int depth;
  98. int blocked; // safeguard that throws an error when attempting to modify the tree in a harmful way while being traversed.
  99. StringName name;
  100. SceneTree *tree;
  101. #ifdef TOOLS_ENABLED
  102. NodePath import_path; //path used when imported, used by scene editors to keep tracking
  103. #endif
  104. Viewport *viewport;
  105. Map<StringName, GroupData> grouped;
  106. List<Node *>::Element *OW; // owned element
  107. List<Node *> owned;
  108. Node *pause_owner;
  109. int network_master;
  110. Map<StringName, MultiplayerAPI::RPCMode> rpc_methods;
  111. Map<StringName, MultiplayerAPI::RPCMode> rpc_properties;
  112. int process_priority;
  113. // Keep bitpacked values together to get better packing
  114. PauseMode pause_mode : 2;
  115. PhysicsInterpolationMode physics_interpolation_mode : 2;
  116. // variables used to properly sort the node when processing, ignored otherwise
  117. //should move all the stuff below to bits
  118. bool physics_process : 1;
  119. bool idle_process : 1;
  120. bool physics_process_internal : 1;
  121. bool idle_process_internal : 1;
  122. bool input : 1;
  123. bool unhandled_input : 1;
  124. bool unhandled_key_input : 1;
  125. // Physics interpolation can be turned on and off on a per node basis.
  126. // This only takes effect when the SceneTree (or project setting) physics interpolation
  127. // is switched on.
  128. bool physics_interpolated : 1;
  129. // We can auto-reset physics interpolation when e.g. adding a node for the first time
  130. bool physics_interpolation_reset_requested : 1;
  131. // Most nodes need not be interpolated in the scene tree, physics interpolation
  132. // is normally only needed in the VisualServer. However if we need to read the
  133. // interpolated transform of a node in the SceneTree, it is necessary to duplicate
  134. // the interpolation logic client side, in order to prevent stalling the VisualServer
  135. // by reading back.
  136. bool physics_interpolated_client_side : 1;
  137. // For certain nodes (e.g. CPU Particles in global mode)
  138. // It can be useful to not send the instance transform to the
  139. // VisualServer, and specify the mesh in world space.
  140. bool use_identity_transform : 1;
  141. bool parent_owned : 1;
  142. bool in_constructor : 1;
  143. bool use_placeholder : 1;
  144. bool display_folded : 1;
  145. bool editable_instance : 1;
  146. bool inside_tree : 1;
  147. bool ready_notified : 1; //this is a small hack, so if a node is added during _ready() to the tree, it correctly gets the _ready() notification
  148. bool ready_first : 1;
  149. mutable NodePath *path_cache;
  150. } data;
  151. Ref<MultiplayerAPI> multiplayer;
  152. void _print_tree_pretty(const String &prefix, const bool last);
  153. void _print_tree(const Node *p_node);
  154. Node *_get_child_by_name(const StringName &p_name) const;
  155. void _replace_connections_target(Node *p_new_target);
  156. void _validate_child_name(Node *p_child, bool p_force_human_readable = false);
  157. void _generate_serial_child_name(const Node *p_child, StringName &name) const;
  158. void _propagate_reverse_notification(int p_notification);
  159. void _propagate_deferred_notification(int p_notification, bool p_reverse);
  160. void _propagate_enter_tree();
  161. void _propagate_ready();
  162. void _propagate_exit_tree();
  163. void _propagate_after_exit_branch(bool p_exiting_tree);
  164. void _propagate_physics_interpolated(bool p_interpolated);
  165. void _propagate_physics_interpolation_reset_requested();
  166. void _print_stray_nodes();
  167. void _propagate_pause_owner(Node *p_owner);
  168. Array _get_node_and_resource(const NodePath &p_path);
  169. void _duplicate_signals(const Node *p_original, Node *p_copy) const;
  170. void _duplicate_and_reown(Node *p_new_parent, const Map<Node *, Node *> &p_reown_map) const;
  171. Node *_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap = nullptr) const;
  172. Array _get_children() const;
  173. Array _get_groups() const;
  174. Variant _rpc_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  175. Variant _rpc_unreliable_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  176. Variant _rpc_id_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  177. Variant _rpc_unreliable_id_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
  178. friend class SceneTree;
  179. void _set_tree(SceneTree *p_tree);
  180. void _release_unique_name_in_owner();
  181. void _acquire_unique_name_in_owner();
  182. protected:
  183. void _block() { data.blocked++; }
  184. void _unblock() { data.blocked--; }
  185. void _notification(int p_notification);
  186. virtual void add_child_notify(Node *p_child);
  187. virtual void remove_child_notify(Node *p_child);
  188. virtual void move_child_notify(Node *p_child);
  189. virtual void owner_changed_notify();
  190. virtual void _physics_interpolated_changed();
  191. void _propagate_replace_owner(Node *p_owner, Node *p_by_owner);
  192. static void _bind_methods();
  193. static String _get_name_num_separator();
  194. friend class SceneState;
  195. void _add_child_nocheck(Node *p_child, const StringName &p_name);
  196. void _set_owner_nocheck(Node *p_owner);
  197. void _set_name_nocheck(const StringName &p_name);
  198. void _set_physics_interpolated_client_side(bool p_enable);
  199. bool _is_physics_interpolated_client_side() const { return data.physics_interpolated_client_side; }
  200. void _set_physics_interpolation_reset_requested(bool p_enable);
  201. bool _is_physics_interpolation_reset_requested() const { return data.physics_interpolation_reset_requested; }
  202. void _set_use_identity_transform(bool p_enable);
  203. bool _is_using_identity_transform() const { return data.use_identity_transform; }
  204. public:
  205. enum {
  206. // you can make your own, but don't use the same numbers as other notifications in other nodes
  207. NOTIFICATION_ENTER_TREE = 10,
  208. NOTIFICATION_EXIT_TREE = 11,
  209. NOTIFICATION_MOVED_IN_PARENT = 12,
  210. NOTIFICATION_READY = 13,
  211. NOTIFICATION_PAUSED = 14,
  212. NOTIFICATION_UNPAUSED = 15,
  213. NOTIFICATION_PHYSICS_PROCESS = 16,
  214. NOTIFICATION_PROCESS = 17,
  215. NOTIFICATION_PARENTED = 18,
  216. NOTIFICATION_UNPARENTED = 19,
  217. NOTIFICATION_INSTANCED = 20,
  218. NOTIFICATION_DRAG_BEGIN = 21,
  219. NOTIFICATION_DRAG_END = 22,
  220. NOTIFICATION_PATH_CHANGED = 23,
  221. //NOTIFICATION_TRANSLATION_CHANGED = 24, moved below
  222. NOTIFICATION_INTERNAL_PROCESS = 25,
  223. NOTIFICATION_INTERNAL_PHYSICS_PROCESS = 26,
  224. NOTIFICATION_POST_ENTER_TREE = 27,
  225. NOTIFICATION_RESET_PHYSICS_INTERPOLATION = 28,
  226. //keep these linked to node
  227. NOTIFICATION_WM_MOUSE_ENTER = MainLoop::NOTIFICATION_WM_MOUSE_ENTER,
  228. NOTIFICATION_WM_MOUSE_EXIT = MainLoop::NOTIFICATION_WM_MOUSE_EXIT,
  229. NOTIFICATION_WM_FOCUS_IN = MainLoop::NOTIFICATION_WM_FOCUS_IN,
  230. NOTIFICATION_WM_FOCUS_OUT = MainLoop::NOTIFICATION_WM_FOCUS_OUT,
  231. NOTIFICATION_WM_QUIT_REQUEST = MainLoop::NOTIFICATION_WM_QUIT_REQUEST,
  232. NOTIFICATION_WM_GO_BACK_REQUEST = MainLoop::NOTIFICATION_WM_GO_BACK_REQUEST,
  233. NOTIFICATION_WM_UNFOCUS_REQUEST = MainLoop::NOTIFICATION_WM_UNFOCUS_REQUEST,
  234. NOTIFICATION_OS_MEMORY_WARNING = MainLoop::NOTIFICATION_OS_MEMORY_WARNING,
  235. NOTIFICATION_TRANSLATION_CHANGED = MainLoop::NOTIFICATION_TRANSLATION_CHANGED,
  236. NOTIFICATION_WM_ABOUT = MainLoop::NOTIFICATION_WM_ABOUT,
  237. NOTIFICATION_CRASH = MainLoop::NOTIFICATION_CRASH,
  238. NOTIFICATION_OS_IME_UPDATE = MainLoop::NOTIFICATION_OS_IME_UPDATE,
  239. NOTIFICATION_APP_RESUMED = MainLoop::NOTIFICATION_APP_RESUMED,
  240. NOTIFICATION_APP_PAUSED = MainLoop::NOTIFICATION_APP_PAUSED
  241. };
  242. /* NODE/TREE */
  243. StringName get_name() const;
  244. void set_name(const String &p_name);
  245. void add_child(Node *p_child, bool p_legible_unique_name = false);
  246. void add_child_below_node(Node *p_node, Node *p_child, bool p_legible_unique_name = false);
  247. void remove_child(Node *p_child);
  248. int get_child_count() const;
  249. Node *get_child(int p_index) const;
  250. bool has_node(const NodePath &p_path) const;
  251. Node *get_node(const NodePath &p_path) const;
  252. Node *get_node_or_null(const NodePath &p_path) const;
  253. Node *find_node(const String &p_mask, bool p_recursive = true, bool p_owned = true) const;
  254. bool has_node_and_resource(const NodePath &p_path) const;
  255. Node *get_node_and_resource(const NodePath &p_path, RES &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property = true) const;
  256. Node *get_parent() const;
  257. Node *find_parent(const String &p_mask) const;
  258. _FORCE_INLINE_ SceneTree *get_tree() const {
  259. ERR_FAIL_COND_V(!data.tree, nullptr);
  260. return data.tree;
  261. }
  262. _FORCE_INLINE_ bool is_inside_tree() const { return data.inside_tree; }
  263. bool is_a_parent_of(const Node *p_node) const;
  264. bool is_greater_than(const Node *p_node) const;
  265. NodePath get_path() const;
  266. NodePath get_path_to(const Node *p_node) const;
  267. Node *find_common_parent_with(const Node *p_node) const;
  268. void add_to_group(const StringName &p_identifier, bool p_persistent = false);
  269. void remove_from_group(const StringName &p_identifier);
  270. bool is_in_group(const StringName &p_identifier) const;
  271. struct GroupInfo {
  272. StringName name;
  273. bool persistent;
  274. };
  275. void get_groups(List<GroupInfo> *p_groups) const;
  276. int get_persistent_group_count() const;
  277. void move_child(Node *p_child, int p_pos);
  278. void raise();
  279. void set_owner(Node *p_owner);
  280. Node *get_owner() const;
  281. void get_owned_by(Node *p_by, List<Node *> *p_owned);
  282. void set_unique_name_in_owner(bool p_enabled);
  283. bool is_unique_name_in_owner() const;
  284. void remove_and_skip();
  285. int get_index() const;
  286. Ref<SceneTreeTween> create_tween();
  287. void print_tree();
  288. void print_tree_pretty();
  289. void set_filename(const String &p_filename);
  290. String get_filename() const;
  291. void set_editor_description(const String &p_editor_description);
  292. String get_editor_description() const;
  293. void set_editable_instance(Node *p_node, bool p_editable);
  294. bool is_editable_instance(const Node *p_node) const;
  295. Node *get_deepest_editable_node(Node *start_node) const;
  296. #ifdef TOOLS_ENABLED
  297. void set_property_pinned(const StringName &p_property, bool p_pinned);
  298. bool is_property_pinned(const StringName &p_property) const;
  299. virtual StringName get_property_store_alias(const StringName &p_property) const;
  300. #endif
  301. void get_storable_properties(Set<StringName> &r_storable_properties) const;
  302. virtual String to_string();
  303. /* NOTIFICATIONS */
  304. void propagate_notification(int p_notification);
  305. void propagate_call(const StringName &p_method, const Array &p_args = Array(), const bool p_parent_first = false);
  306. /* PROCESSING */
  307. void set_physics_process(bool p_process);
  308. float get_physics_process_delta_time() const;
  309. bool is_physics_processing() const;
  310. void set_process(bool p_idle_process);
  311. float get_process_delta_time() const;
  312. bool is_processing() const;
  313. void set_physics_process_internal(bool p_process_internal);
  314. bool is_physics_processing_internal() const;
  315. void set_process_internal(bool p_idle_process_internal);
  316. bool is_processing_internal() const;
  317. void set_process_priority(int p_priority);
  318. int get_process_priority() const;
  319. void set_process_input(bool p_enable);
  320. bool is_processing_input() const;
  321. void set_process_unhandled_input(bool p_enable);
  322. bool is_processing_unhandled_input() const;
  323. void set_process_unhandled_key_input(bool p_enable);
  324. bool is_processing_unhandled_key_input() const;
  325. int get_position_in_parent() const;
  326. Node *duplicate(int p_flags = DUPLICATE_GROUPS | DUPLICATE_SIGNALS | DUPLICATE_SCRIPTS) const;
  327. Node *duplicate_and_reown(const Map<Node *, Node *> &p_reown_map) const;
  328. #ifdef TOOLS_ENABLED
  329. Node *duplicate_from_editor(Map<const Node *, Node *> &r_duplimap) const;
  330. Node *duplicate_from_editor(Map<const Node *, Node *> &r_duplimap, const Map<RES, RES> &p_resource_remap) const;
  331. void remap_node_resources(Node *p_node, const Map<RES, RES> &p_resource_remap) const;
  332. void remap_nested_resources(RES p_resource, const Map<RES, RES> &p_resource_remap) const;
  333. #endif
  334. // used by editors, to save what has changed only
  335. void set_scene_instance_state(const Ref<SceneState> &p_state);
  336. Ref<SceneState> get_scene_instance_state() const;
  337. void set_scene_inherited_state(const Ref<SceneState> &p_state);
  338. Ref<SceneState> get_scene_inherited_state() const;
  339. void set_scene_instance_load_placeholder(bool p_enable);
  340. bool get_scene_instance_load_placeholder() const;
  341. static Vector<Variant> make_binds(VARIANT_ARG_LIST);
  342. void replace_by(Node *p_node, bool p_keep_data = false);
  343. void set_pause_mode(PauseMode p_mode);
  344. PauseMode get_pause_mode() const;
  345. bool can_process() const;
  346. bool can_process_notification(int p_what) const;
  347. void set_physics_interpolation_mode(PhysicsInterpolationMode p_mode);
  348. PhysicsInterpolationMode get_physics_interpolation_mode() const { return data.physics_interpolation_mode; }
  349. _FORCE_INLINE_ bool is_physics_interpolated() const { return data.physics_interpolated; }
  350. _FORCE_INLINE_ bool is_physics_interpolated_and_enabled() const { return is_inside_tree() && get_tree()->is_physics_interpolation_enabled() && is_physics_interpolated(); }
  351. void reset_physics_interpolation();
  352. void request_ready();
  353. static void print_stray_nodes();
  354. #ifdef TOOLS_ENABLED
  355. String validate_child_name(Node *p_child);
  356. #endif
  357. void queue_delete();
  358. //hacks for speed
  359. static void set_human_readable_collision_renaming(bool p_enabled);
  360. static void init_node_hrcr();
  361. void force_parent_owned() { data.parent_owned = true; } //hack to avoid duplicate nodes
  362. void set_import_path(const NodePath &p_import_path); //path used when imported, used by scene editors to keep tracking
  363. NodePath get_import_path() const;
  364. bool is_owned_by_parent() const;
  365. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  366. void clear_internal_tree_resource_paths();
  367. _FORCE_INLINE_ Viewport *get_viewport() const { return data.viewport; }
  368. virtual String get_configuration_warning() const;
  369. void update_configuration_warning();
  370. void set_display_folded(bool p_folded);
  371. bool is_displayed_folded() const;
  372. /* NETWORK */
  373. void set_network_master(int p_peer_id, bool p_recursive = true);
  374. int get_network_master() const;
  375. bool is_network_master() const;
  376. void rpc_config(const StringName &p_method, MultiplayerAPI::RPCMode p_mode); // config a local method for RPC
  377. void rset_config(const StringName &p_property, MultiplayerAPI::RPCMode p_mode); // config a local property for RPC
  378. void rpc(const StringName &p_method, VARIANT_ARG_LIST); //rpc call, honors RPCMode
  379. void rpc_unreliable(const StringName &p_method, VARIANT_ARG_LIST); //rpc call, honors RPCMode
  380. void rpc_id(int p_peer_id, const StringName &p_method, VARIANT_ARG_LIST); //rpc call, honors RPCMode
  381. void rpc_unreliable_id(int p_peer_id, const StringName &p_method, VARIANT_ARG_LIST); //rpc call, honors RPCMode
  382. void rset(const StringName &p_property, const Variant &p_value); //remote set call, honors RPCMode
  383. void rset_unreliable(const StringName &p_property, const Variant &p_value); //remote set call, honors RPCMode
  384. void rset_id(int p_peer_id, const StringName &p_property, const Variant &p_value); //remote set call, honors RPCMode
  385. void rset_unreliable_id(int p_peer_id, const StringName &p_property, const Variant &p_value); //remote set call, honors RPCMode
  386. void rpcp(int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount);
  387. void rsetp(int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value);
  388. Ref<MultiplayerAPI> get_multiplayer() const;
  389. Ref<MultiplayerAPI> get_custom_multiplayer() const;
  390. void set_custom_multiplayer(Ref<MultiplayerAPI> p_multiplayer);
  391. const Map<StringName, MultiplayerAPI::RPCMode>::Element *get_node_rpc_mode(const StringName &p_method);
  392. const Map<StringName, MultiplayerAPI::RPCMode>::Element *get_node_rset_mode(const StringName &p_property);
  393. Node();
  394. ~Node();
  395. };
  396. VARIANT_ENUM_CAST(Node::DuplicateFlags);
  397. typedef Set<Node *, Node::Comparator> NodeSet;
  398. #endif // NODE_H