node.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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/string/node_path.h"
  33. #include "core/templates/rb_map.h"
  34. #include "core/variant/typed_array.h"
  35. #include "scene/main/scene_tree.h"
  36. class Viewport;
  37. class SceneState;
  38. class Tween;
  39. class PropertyTweener;
  40. class Node : public Object {
  41. GDCLASS(Node, Object);
  42. public:
  43. enum ProcessMode {
  44. PROCESS_MODE_INHERIT, // same as parent node
  45. PROCESS_MODE_PAUSABLE, // process only if not paused
  46. PROCESS_MODE_WHEN_PAUSED, // process only if paused
  47. PROCESS_MODE_ALWAYS, // process always
  48. PROCESS_MODE_DISABLED, // never 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. enum InternalMode {
  65. INTERNAL_MODE_DISABLED,
  66. INTERNAL_MODE_FRONT,
  67. INTERNAL_MODE_BACK,
  68. };
  69. struct Comparator {
  70. bool operator()(const Node *p_a, const Node *p_b) const { return p_b->is_greater_than(p_a); }
  71. };
  72. struct ComparatorWithPriority {
  73. 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; }
  74. };
  75. static int orphan_node_count;
  76. private:
  77. struct GroupData {
  78. bool persistent = false;
  79. SceneTree::Group *group = nullptr;
  80. };
  81. struct Data {
  82. String scene_file_path;
  83. Ref<SceneState> instance_state;
  84. Ref<SceneState> inherited_state;
  85. Node *parent = nullptr;
  86. Node *owner = nullptr;
  87. Vector<Node *> children;
  88. HashMap<StringName, Node *> owned_unique_nodes;
  89. bool unique_name_in_owner = false;
  90. int internal_children_front = 0;
  91. int internal_children_back = 0;
  92. int pos = -1;
  93. int depth = -1;
  94. int blocked = 0; // Safeguard that throws an error when attempting to modify the tree in a harmful way while being traversed.
  95. StringName name;
  96. SceneTree *tree = nullptr;
  97. bool inside_tree = false;
  98. bool ready_notified = false; // This is a small hack, so if a node is added during _ready() to the tree, it correctly gets the _ready() notification.
  99. bool ready_first = true;
  100. #ifdef TOOLS_ENABLED
  101. NodePath import_path; // Path used when imported, used by scene editors to keep tracking.
  102. #endif
  103. String editor_description;
  104. Viewport *viewport = nullptr;
  105. HashMap<StringName, GroupData> grouped;
  106. List<Node *>::Element *OW = nullptr; // Owned element.
  107. List<Node *> owned;
  108. ProcessMode process_mode = PROCESS_MODE_INHERIT;
  109. Node *process_owner = nullptr;
  110. int multiplayer_authority = 1; // Server by default.
  111. Variant rpc_config;
  112. // Variables used to properly sort the node when processing, ignored otherwise.
  113. // TODO: Should move all the stuff below to bits.
  114. bool physics_process = false;
  115. bool process = false;
  116. int process_priority = 0;
  117. bool physics_process_internal = false;
  118. bool process_internal = false;
  119. bool input = false;
  120. bool shortcut_input = false;
  121. bool unhandled_input = false;
  122. bool unhandled_key_input = false;
  123. bool parent_owned = false;
  124. bool in_constructor = true;
  125. bool use_placeholder = false;
  126. bool display_folded = false;
  127. bool editable_instance = false;
  128. mutable NodePath *path_cache = nullptr;
  129. } data;
  130. Ref<MultiplayerAPI> multiplayer;
  131. void _print_tree_pretty(const String &prefix, const bool last);
  132. void _print_tree(const Node *p_node);
  133. Node *_get_child_by_name(const StringName &p_name) const;
  134. void _replace_connections_target(Node *p_new_target);
  135. void _validate_child_name(Node *p_child, bool p_force_human_readable = false);
  136. void _generate_serial_child_name(const Node *p_child, StringName &name) const;
  137. void _propagate_reverse_notification(int p_notification);
  138. void _propagate_deferred_notification(int p_notification, bool p_reverse);
  139. void _propagate_enter_tree();
  140. void _propagate_ready();
  141. void _propagate_exit_tree();
  142. void _propagate_after_exit_tree();
  143. void _print_orphan_nodes();
  144. void _propagate_process_owner(Node *p_owner, int p_pause_notification, int p_enabled_notification);
  145. void _propagate_groups_dirty();
  146. Array _get_node_and_resource(const NodePath &p_path);
  147. void _duplicate_signals(const Node *p_original, Node *p_copy) const;
  148. Node *_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap = nullptr) const;
  149. TypedArray<Node> _get_children(bool p_include_internal = true) const;
  150. TypedArray<StringName> _get_groups() const;
  151. Error _rpc_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  152. Error _rpc_id_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  153. _FORCE_INLINE_ bool _is_internal_front() const { return data.parent && data.pos < data.parent->data.internal_children_front; }
  154. _FORCE_INLINE_ bool _is_internal_back() const { return data.parent && data.pos >= data.parent->data.children.size() - data.parent->data.internal_children_back; }
  155. friend class SceneTree;
  156. void _set_tree(SceneTree *p_tree);
  157. void _propagate_pause_notification(bool p_enable);
  158. _FORCE_INLINE_ bool _can_process(bool p_paused) const;
  159. _FORCE_INLINE_ bool _is_enabled() const;
  160. void _release_unique_name_in_owner();
  161. void _acquire_unique_name_in_owner();
  162. protected:
  163. void _block() { data.blocked++; }
  164. void _unblock() { data.blocked--; }
  165. void _notification(int p_notification);
  166. virtual void add_child_notify(Node *p_child);
  167. virtual void remove_child_notify(Node *p_child);
  168. virtual void move_child_notify(Node *p_child);
  169. virtual void owner_changed_notify();
  170. void _propagate_replace_owner(Node *p_owner, Node *p_by_owner);
  171. static void _bind_methods();
  172. static String _get_name_num_separator();
  173. friend class SceneState;
  174. void _add_child_nocheck(Node *p_child, const StringName &p_name);
  175. void _set_owner_nocheck(Node *p_owner);
  176. void _set_name_nocheck(const StringName &p_name);
  177. //call from SceneTree
  178. void _call_input(const Ref<InputEvent> &p_event);
  179. void _call_shortcut_input(const Ref<InputEvent> &p_event);
  180. void _call_unhandled_input(const Ref<InputEvent> &p_event);
  181. void _call_unhandled_key_input(const Ref<InputEvent> &p_event);
  182. protected:
  183. virtual void input(const Ref<InputEvent> &p_event);
  184. virtual void shortcut_input(const Ref<InputEvent> &p_key_event);
  185. virtual void unhandled_input(const Ref<InputEvent> &p_event);
  186. virtual void unhandled_key_input(const Ref<InputEvent> &p_key_event);
  187. GDVIRTUAL1(_process, double)
  188. GDVIRTUAL1(_physics_process, double)
  189. GDVIRTUAL0(_enter_tree)
  190. GDVIRTUAL0(_exit_tree)
  191. GDVIRTUAL0(_ready)
  192. GDVIRTUAL0RC(Vector<String>, _get_configuration_warnings)
  193. GDVIRTUAL1(_input, Ref<InputEvent>)
  194. GDVIRTUAL1(_shortcut_input, Ref<InputEvent>)
  195. GDVIRTUAL1(_unhandled_input, Ref<InputEvent>)
  196. GDVIRTUAL1(_unhandled_key_input, Ref<InputEvent>)
  197. public:
  198. enum {
  199. // you can make your own, but don't use the same numbers as other notifications in other nodes
  200. NOTIFICATION_ENTER_TREE = 10,
  201. NOTIFICATION_EXIT_TREE = 11,
  202. NOTIFICATION_MOVED_IN_PARENT = 12,
  203. NOTIFICATION_READY = 13,
  204. NOTIFICATION_PAUSED = 14,
  205. NOTIFICATION_UNPAUSED = 15,
  206. NOTIFICATION_PHYSICS_PROCESS = 16,
  207. NOTIFICATION_PROCESS = 17,
  208. NOTIFICATION_PARENTED = 18,
  209. NOTIFICATION_UNPARENTED = 19,
  210. NOTIFICATION_SCENE_INSTANTIATED = 20,
  211. NOTIFICATION_DRAG_BEGIN = 21,
  212. NOTIFICATION_DRAG_END = 22,
  213. NOTIFICATION_PATH_RENAMED = 23,
  214. //NOTIFICATION_TRANSLATION_CHANGED = 24, moved below
  215. NOTIFICATION_INTERNAL_PROCESS = 25,
  216. NOTIFICATION_INTERNAL_PHYSICS_PROCESS = 26,
  217. NOTIFICATION_POST_ENTER_TREE = 27,
  218. NOTIFICATION_DISABLED = 28,
  219. NOTIFICATION_ENABLED = 29,
  220. //keep these linked to node
  221. NOTIFICATION_WM_MOUSE_ENTER = 1002,
  222. NOTIFICATION_WM_MOUSE_EXIT = 1003,
  223. NOTIFICATION_WM_WINDOW_FOCUS_IN = 1004,
  224. NOTIFICATION_WM_WINDOW_FOCUS_OUT = 1005,
  225. NOTIFICATION_WM_CLOSE_REQUEST = 1006,
  226. NOTIFICATION_WM_GO_BACK_REQUEST = 1007,
  227. NOTIFICATION_WM_SIZE_CHANGED = 1008,
  228. NOTIFICATION_WM_DPI_CHANGE = 1009,
  229. NOTIFICATION_VP_MOUSE_ENTER = 1010,
  230. NOTIFICATION_VP_MOUSE_EXIT = 1011,
  231. NOTIFICATION_OS_MEMORY_WARNING = MainLoop::NOTIFICATION_OS_MEMORY_WARNING,
  232. NOTIFICATION_TRANSLATION_CHANGED = MainLoop::NOTIFICATION_TRANSLATION_CHANGED,
  233. NOTIFICATION_WM_ABOUT = MainLoop::NOTIFICATION_WM_ABOUT,
  234. NOTIFICATION_CRASH = MainLoop::NOTIFICATION_CRASH,
  235. NOTIFICATION_OS_IME_UPDATE = MainLoop::NOTIFICATION_OS_IME_UPDATE,
  236. NOTIFICATION_APPLICATION_RESUMED = MainLoop::NOTIFICATION_APPLICATION_RESUMED,
  237. NOTIFICATION_APPLICATION_PAUSED = MainLoop::NOTIFICATION_APPLICATION_PAUSED,
  238. NOTIFICATION_APPLICATION_FOCUS_IN = MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN,
  239. NOTIFICATION_APPLICATION_FOCUS_OUT = MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT,
  240. NOTIFICATION_TEXT_SERVER_CHANGED = MainLoop::NOTIFICATION_TEXT_SERVER_CHANGED,
  241. // Editor specific node notifications
  242. NOTIFICATION_EDITOR_PRE_SAVE = 9001,
  243. NOTIFICATION_EDITOR_POST_SAVE = 9002,
  244. };
  245. /* NODE/TREE */
  246. StringName get_name() const;
  247. void set_name(const String &p_name);
  248. void add_child(Node *p_child, bool p_legible_unique_name = false, InternalMode p_internal = INTERNAL_MODE_DISABLED);
  249. void add_sibling(Node *p_sibling, bool p_legible_unique_name = false);
  250. void remove_child(Node *p_child);
  251. int get_child_count(bool p_include_internal = true) const;
  252. Node *get_child(int p_index, bool p_include_internal = true) const;
  253. bool has_node(const NodePath &p_path) const;
  254. Node *get_node(const NodePath &p_path) const;
  255. Node *get_node_or_null(const NodePath &p_path) const;
  256. Node *find_child(const String &p_pattern, bool p_recursive = true, bool p_owned = true) const;
  257. TypedArray<Node> find_children(const String &p_pattern, const String &p_type = "", bool p_recursive = true, bool p_owned = true) const;
  258. bool has_node_and_resource(const NodePath &p_path) const;
  259. Node *get_node_and_resource(const NodePath &p_path, Ref<Resource> &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property = true) const;
  260. Node *get_parent() const;
  261. Node *find_parent(const String &p_pattern) const;
  262. _FORCE_INLINE_ SceneTree *get_tree() const {
  263. ERR_FAIL_COND_V(!data.tree, nullptr);
  264. return data.tree;
  265. }
  266. _FORCE_INLINE_ bool is_inside_tree() const { return data.inside_tree; }
  267. bool is_ancestor_of(const Node *p_node) const;
  268. bool is_greater_than(const Node *p_node) const;
  269. NodePath get_path() const;
  270. NodePath get_path_to(const Node *p_node) const;
  271. Node *find_common_parent_with(const Node *p_node) const;
  272. void add_to_group(const StringName &p_identifier, bool p_persistent = false);
  273. void remove_from_group(const StringName &p_identifier);
  274. bool is_in_group(const StringName &p_identifier) const;
  275. struct GroupInfo {
  276. StringName name;
  277. bool persistent = false;
  278. };
  279. void get_groups(List<GroupInfo> *p_groups) const;
  280. int get_persistent_group_count() const;
  281. void move_child(Node *p_child, int p_pos);
  282. void _move_child(Node *p_child, int p_pos, bool p_ignore_end = false);
  283. void raise();
  284. void set_owner(Node *p_owner);
  285. Node *get_owner() const;
  286. void get_owned_by(Node *p_by, List<Node *> *p_owned);
  287. void set_unique_name_in_owner(bool p_enabled);
  288. bool is_unique_name_in_owner() const;
  289. void remove_and_skip();
  290. int get_index(bool p_include_internal = true) const;
  291. Ref<Tween> create_tween();
  292. void print_tree();
  293. void print_tree_pretty();
  294. void set_scene_file_path(const String &p_scene_file_path);
  295. String get_scene_file_path() const;
  296. void set_editor_description(const String &p_editor_description);
  297. String get_editor_description() const;
  298. void set_editable_instance(Node *p_node, bool p_editable);
  299. bool is_editable_instance(const Node *p_node) const;
  300. Node *get_deepest_editable_node(Node *p_start_node) const;
  301. #ifdef TOOLS_ENABLED
  302. void set_property_pinned(const String &p_property, bool p_pinned);
  303. bool is_property_pinned(const StringName &p_property) const;
  304. virtual StringName get_property_store_alias(const StringName &p_property) const;
  305. #endif
  306. void get_storable_properties(HashSet<StringName> &r_storable_properties) const;
  307. virtual String to_string() override;
  308. /* NOTIFICATIONS */
  309. void propagate_notification(int p_notification);
  310. void propagate_call(const StringName &p_method, const Array &p_args = Array(), const bool p_parent_first = false);
  311. /* PROCESSING */
  312. void set_physics_process(bool p_process);
  313. double get_physics_process_delta_time() const;
  314. bool is_physics_processing() const;
  315. void set_process(bool p_process);
  316. double get_process_delta_time() const;
  317. bool is_processing() const;
  318. void set_physics_process_internal(bool p_process_internal);
  319. bool is_physics_processing_internal() const;
  320. void set_process_internal(bool p_process_internal);
  321. bool is_processing_internal() const;
  322. void set_process_priority(int p_priority);
  323. int get_process_priority() const;
  324. void set_process_input(bool p_enable);
  325. bool is_processing_input() const;
  326. void set_process_shortcut_input(bool p_enable);
  327. bool is_processing_shortcut_input() const;
  328. void set_process_unhandled_input(bool p_enable);
  329. bool is_processing_unhandled_input() const;
  330. void set_process_unhandled_key_input(bool p_enable);
  331. bool is_processing_unhandled_key_input() const;
  332. Node *duplicate(int p_flags = DUPLICATE_GROUPS | DUPLICATE_SIGNALS | DUPLICATE_SCRIPTS) const;
  333. #ifdef TOOLS_ENABLED
  334. Node *duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap) const;
  335. Node *duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const;
  336. void remap_node_resources(Node *p_node, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const;
  337. void remap_nested_resources(Ref<Resource> p_resource, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const;
  338. #endif
  339. // used by editors, to save what has changed only
  340. void set_scene_instance_state(const Ref<SceneState> &p_state);
  341. Ref<SceneState> get_scene_instance_state() const;
  342. void set_scene_inherited_state(const Ref<SceneState> &p_state);
  343. Ref<SceneState> get_scene_inherited_state() const;
  344. void set_scene_instance_load_placeholder(bool p_enable);
  345. bool get_scene_instance_load_placeholder() const;
  346. template <typename... VarArgs>
  347. Vector<Variant> make_binds(VarArgs... p_args) {
  348. Vector<Variant> binds = { p_args... };
  349. return binds;
  350. }
  351. void replace_by(Node *p_node, bool p_keep_data = false);
  352. void set_process_mode(ProcessMode p_mode);
  353. ProcessMode get_process_mode() const;
  354. bool can_process() const;
  355. bool can_process_notification(int p_what) const;
  356. bool is_enabled() const;
  357. void request_ready();
  358. static void print_orphan_nodes();
  359. #ifdef TOOLS_ENABLED
  360. String validate_child_name(Node *p_child);
  361. #endif
  362. static String adjust_name_casing(const String &p_name);
  363. void queue_delete();
  364. //hacks for speed
  365. static void init_node_hrcr();
  366. void force_parent_owned() { data.parent_owned = true; } //hack to avoid duplicate nodes
  367. void set_import_path(const NodePath &p_import_path); //path used when imported, used by scene editors to keep tracking
  368. NodePath get_import_path() const;
  369. bool is_owned_by_parent() const;
  370. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  371. void clear_internal_tree_resource_paths();
  372. _FORCE_INLINE_ Viewport *get_viewport() const { return data.viewport; }
  373. virtual TypedArray<String> get_configuration_warnings() const;
  374. String get_configuration_warnings_as_string() const;
  375. void update_configuration_warnings();
  376. void set_display_folded(bool p_folded);
  377. bool is_displayed_folded() const;
  378. /* NETWORK */
  379. virtual void set_multiplayer_authority(int p_peer_id, bool p_recursive = true);
  380. int get_multiplayer_authority() const;
  381. bool is_multiplayer_authority() const;
  382. void rpc_config(const StringName &p_method, const Variant &p_config); // config a local method for RPC
  383. const Variant get_node_rpc_config() const;
  384. template <typename... VarArgs>
  385. Error rpc(const StringName &p_method, VarArgs... p_args);
  386. template <typename... VarArgs>
  387. Error rpc_id(int p_peer_id, const StringName &p_method, VarArgs... p_args);
  388. Error rpcp(int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount);
  389. Ref<MultiplayerAPI> get_multiplayer() const;
  390. Node();
  391. ~Node();
  392. };
  393. VARIANT_ENUM_CAST(Node::DuplicateFlags);
  394. typedef HashSet<Node *, Node::Comparator> NodeSet;
  395. // Template definitions must be in the header so they are always fully initialized before their usage.
  396. // See this StackOverflow question for more information: https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
  397. template <typename... VarArgs>
  398. Error Node::rpc(const StringName &p_method, VarArgs... p_args) {
  399. return rpc_id(0, p_method, p_args...);
  400. }
  401. template <typename... VarArgs>
  402. Error Node::rpc_id(int p_peer_id, const StringName &p_method, VarArgs... p_args) {
  403. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  404. const Variant *argptrs[sizeof...(p_args) + 1];
  405. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  406. argptrs[i] = &args[i];
  407. }
  408. return rpcp(p_peer_id, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  409. }
  410. #endif // NODE_H