scene_tree.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /**************************************************************************/
  2. /* scene_tree.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. #pragma once
  31. #include "core/os/main_loop.h"
  32. #include "core/os/thread_safe.h"
  33. #include "core/templates/paged_allocator.h"
  34. #include "core/templates/self_list.h"
  35. #include "scene/main/scene_tree_fti.h"
  36. #include "scene/resources/mesh.h"
  37. #undef Window
  38. class PackedScene;
  39. class Node;
  40. #ifndef _3D_DISABLED
  41. class Node3D;
  42. #endif
  43. class Window;
  44. class Material;
  45. class Mesh;
  46. class MultiplayerAPI;
  47. class SceneDebugger;
  48. class Tween;
  49. class Viewport;
  50. class SceneTreeTimer : public RefCounted {
  51. GDCLASS(SceneTreeTimer, RefCounted);
  52. double time_left = 0.0;
  53. bool process_always = true;
  54. bool process_in_physics = false;
  55. bool ignore_time_scale = false;
  56. protected:
  57. static void _bind_methods();
  58. public:
  59. void set_time_left(double p_time);
  60. double get_time_left() const;
  61. void set_process_always(bool p_process_always);
  62. bool is_process_always();
  63. void set_process_in_physics(bool p_process_in_physics);
  64. bool is_process_in_physics();
  65. void set_ignore_time_scale(bool p_ignore);
  66. bool is_ignoring_time_scale();
  67. void release_connections();
  68. };
  69. class SceneTree : public MainLoop {
  70. _THREAD_SAFE_CLASS_
  71. GDCLASS(SceneTree, MainLoop);
  72. public:
  73. typedef void (*IdleCallback)();
  74. private:
  75. CallQueue::Allocator *process_group_call_queue_allocator = nullptr;
  76. struct ProcessGroup {
  77. CallQueue call_queue;
  78. Vector<Node *> nodes;
  79. Vector<Node *> physics_nodes;
  80. bool node_order_dirty = true;
  81. bool physics_node_order_dirty = true;
  82. bool removed = false;
  83. Node *owner = nullptr;
  84. uint64_t last_pass = 0;
  85. };
  86. struct ProcessGroupSort {
  87. _FORCE_INLINE_ bool operator()(const ProcessGroup *p_left, const ProcessGroup *p_right) const;
  88. };
  89. PagedAllocator<ProcessGroup, true> group_allocator; // Allocate groups on pages, to enhance cache usage.
  90. LocalVector<ProcessGroup *> process_groups;
  91. bool process_groups_dirty = true;
  92. LocalVector<ProcessGroup *> local_process_group_cache; // Used when processing to group what needs to
  93. uint64_t process_last_pass = 1;
  94. ProcessGroup default_process_group;
  95. bool node_threading_disabled = false;
  96. struct Group {
  97. Vector<Node *> nodes;
  98. bool changed = false;
  99. };
  100. #ifndef _3D_DISABLED
  101. struct ClientPhysicsInterpolation {
  102. SelfList<Node3D>::List _node_3d_list;
  103. void physics_process();
  104. } _client_physics_interpolation;
  105. #endif
  106. Window *root = nullptr;
  107. double physics_process_time = 0.0;
  108. double process_time = 0.0;
  109. bool accept_quit = true;
  110. bool quit_on_go_back = true;
  111. #ifdef DEBUG_ENABLED
  112. bool debug_collisions_hint = false;
  113. bool debug_paths_hint = false;
  114. bool debug_navigation_hint = false;
  115. #endif
  116. bool paused = false;
  117. bool suspended = false;
  118. HashMap<StringName, Group> group_map;
  119. bool _quit = false;
  120. // Static so we can get directly instead of via SceneTree pointer.
  121. static bool _physics_interpolation_enabled;
  122. // Note that physics interpolation is hard coded to OFF in the editor,
  123. // therefore we have a second bool to enable e.g. configuration warnings
  124. // to only take effect when the project is using physics interpolation.
  125. static bool _physics_interpolation_enabled_in_project;
  126. SceneTreeFTI scene_tree_fti;
  127. StringName tree_changed_name = "tree_changed";
  128. StringName node_added_name = "node_added";
  129. StringName node_removed_name = "node_removed";
  130. StringName node_renamed_name = "node_renamed";
  131. int64_t current_frame = 0;
  132. int nodes_in_tree_count = 0;
  133. #ifdef TOOLS_ENABLED
  134. Node *edited_scene_root = nullptr;
  135. #endif
  136. struct UGCall {
  137. StringName group;
  138. StringName call;
  139. static uint32_t hash(const UGCall &p_val) {
  140. return p_val.group.hash() ^ p_val.call.hash();
  141. }
  142. bool operator==(const UGCall &p_with) const { return group == p_with.group && call == p_with.call; }
  143. bool operator<(const UGCall &p_with) const { return group == p_with.group ? call < p_with.call : group < p_with.group; }
  144. };
  145. // Safety for when a node is deleted while a group is being called.
  146. int nodes_removed_on_group_call_lock = 0;
  147. HashSet<Node *> nodes_removed_on_group_call; // Skip erased nodes.
  148. List<ObjectID> delete_queue;
  149. uint64_t accessibility_upd_per_sec = 0;
  150. bool accessibility_force_update = true;
  151. HashSet<ObjectID> accessibility_change_queue;
  152. uint64_t accessibility_last_update = 0;
  153. HashMap<UGCall, Vector<Variant>, UGCall> unique_group_calls;
  154. bool ugc_locked = false;
  155. void _flush_ugc();
  156. _FORCE_INLINE_ void _update_group_order(Group &g);
  157. TypedArray<Node> _get_nodes_in_group(const StringName &p_group);
  158. Node *current_scene = nullptr;
  159. ObjectID prev_scene_id;
  160. ObjectID pending_new_scene_id;
  161. Color debug_collisions_color;
  162. Color debug_collision_contact_color;
  163. Color debug_paths_color;
  164. float debug_paths_width = 1.0f;
  165. Ref<ArrayMesh> debug_contact_mesh;
  166. Ref<Material> debug_paths_material;
  167. Ref<Material> collision_material;
  168. int collision_debug_contacts;
  169. void _flush_scene_change();
  170. List<Ref<SceneTreeTimer>> timers;
  171. List<Ref<Tween>> tweens;
  172. ///network///
  173. Ref<MultiplayerAPI> multiplayer;
  174. HashMap<NodePath, Ref<MultiplayerAPI>> custom_multiplayers;
  175. bool multiplayer_poll = true;
  176. static SceneTree *singleton;
  177. friend class Node;
  178. void tree_changed();
  179. void node_added(Node *p_node);
  180. void node_removed(Node *p_node);
  181. void node_renamed(Node *p_node);
  182. void process_timers(double p_delta, bool p_physics_frame);
  183. void process_tweens(double p_delta, bool p_physics_frame);
  184. Group *add_to_group(const StringName &p_group, Node *p_node);
  185. void remove_from_group(const StringName &p_group, Node *p_node);
  186. void _process_group(ProcessGroup *p_group, bool p_physics);
  187. void _process_groups_thread(uint32_t p_index, bool p_physics);
  188. void _process(bool p_physics);
  189. void _remove_process_group(Node *p_node);
  190. void _add_process_group(Node *p_node);
  191. void _remove_node_from_process_group(Node *p_node, Node *p_owner);
  192. void _add_node_to_process_group(Node *p_node, Node *p_owner);
  193. void _call_group_flags(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  194. void _call_group(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  195. void _flush_delete_queue();
  196. // Optimization.
  197. friend class CanvasItem;
  198. friend class Node3D;
  199. friend class Viewport;
  200. SelfList<Node>::List xform_change_list;
  201. #ifdef DEBUG_ENABLED // No live editor in release build.
  202. friend class LiveEditor;
  203. #endif
  204. enum {
  205. MAX_IDLE_CALLBACKS = 256
  206. };
  207. static IdleCallback idle_callbacks[MAX_IDLE_CALLBACKS];
  208. static int idle_callback_count;
  209. void _call_idle_callbacks();
  210. void _main_window_focus_in();
  211. void _main_window_close();
  212. void _main_window_go_back();
  213. enum CallInputType {
  214. CALL_INPUT_TYPE_INPUT,
  215. CALL_INPUT_TYPE_SHORTCUT_INPUT,
  216. CALL_INPUT_TYPE_UNHANDLED_INPUT,
  217. CALL_INPUT_TYPE_UNHANDLED_KEY_INPUT,
  218. };
  219. //used by viewport
  220. void _call_input_pause(const StringName &p_group, CallInputType p_call_type, const Ref<InputEvent> &p_input, Viewport *p_viewport);
  221. protected:
  222. void _notification(int p_notification);
  223. static void _bind_methods();
  224. public:
  225. enum {
  226. NOTIFICATION_TRANSFORM_CHANGED = 2000
  227. };
  228. enum GroupCallFlags {
  229. GROUP_CALL_DEFAULT = 0,
  230. GROUP_CALL_REVERSE = 1,
  231. GROUP_CALL_DEFERRED = 2,
  232. GROUP_CALL_UNIQUE = 4,
  233. };
  234. _FORCE_INLINE_ Window *get_root() const { return root; }
  235. void call_group_flagsp(uint32_t p_call_flags, const StringName &p_group, const StringName &p_function, const Variant **p_args, int p_argcount);
  236. void notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification);
  237. void set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value);
  238. // `notify_group()` is immediate by default since Godot 4.0.
  239. void notify_group(const StringName &p_group, int p_notification);
  240. // `set_group()` is immediate by default since Godot 4.0.
  241. void set_group(const StringName &p_group, const String &p_name, const Variant &p_value);
  242. template <typename... VarArgs>
  243. // `call_group()` is immediate by default since Godot 4.0.
  244. void call_group(const StringName &p_group, const StringName &p_function, VarArgs... p_args) {
  245. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  246. const Variant *argptrs[sizeof...(p_args) + 1];
  247. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  248. argptrs[i] = &args[i];
  249. }
  250. call_group_flagsp(GROUP_CALL_DEFAULT, p_group, p_function, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  251. }
  252. template <typename... VarArgs>
  253. void call_group_flags(uint32_t p_flags, const StringName &p_group, const StringName &p_function, VarArgs... p_args) {
  254. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  255. const Variant *argptrs[sizeof...(p_args) + 1];
  256. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  257. argptrs[i] = &args[i];
  258. }
  259. call_group_flagsp(p_flags, p_group, p_function, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  260. }
  261. void flush_transform_notifications();
  262. bool is_accessibility_enabled() const;
  263. bool is_accessibility_supported() const;
  264. void _accessibility_force_update();
  265. void _accessibility_notify_change(const Node *p_node, bool p_remove = false);
  266. void _flush_accessibility_changes();
  267. void _process_accessibility_changes(DisplayServer::WindowID p_window_id);
  268. virtual void initialize() override;
  269. virtual void iteration_prepare() override;
  270. virtual bool physics_process(double p_time) override;
  271. virtual void iteration_end() override;
  272. virtual bool process(double p_time) override;
  273. virtual void finalize() override;
  274. bool is_auto_accept_quit() const;
  275. void set_auto_accept_quit(bool p_enable);
  276. bool is_quit_on_go_back() const;
  277. void set_quit_on_go_back(bool p_enable);
  278. void quit(int p_exit_code = EXIT_SUCCESS);
  279. _FORCE_INLINE_ double get_physics_process_time() const { return physics_process_time; }
  280. _FORCE_INLINE_ double get_process_time() const { return process_time; }
  281. void set_pause(bool p_enabled);
  282. bool is_paused() const;
  283. void set_suspend(bool p_enabled);
  284. bool is_suspended() const;
  285. #ifdef DEBUG_ENABLED
  286. void set_debug_collisions_hint(bool p_enabled);
  287. bool is_debugging_collisions_hint() const;
  288. void set_debug_paths_hint(bool p_enabled);
  289. bool is_debugging_paths_hint() const;
  290. void set_debug_navigation_hint(bool p_enabled);
  291. bool is_debugging_navigation_hint() const;
  292. #else
  293. void set_debug_collisions_hint(bool p_enabled) {}
  294. bool is_debugging_collisions_hint() const { return false; }
  295. void set_debug_paths_hint(bool p_enabled) {}
  296. bool is_debugging_paths_hint() const { return false; }
  297. void set_debug_navigation_hint(bool p_enabled) {}
  298. bool is_debugging_navigation_hint() const { return false; }
  299. #endif
  300. void set_debug_collisions_color(const Color &p_color);
  301. Color get_debug_collisions_color() const;
  302. void set_debug_collision_contact_color(const Color &p_color);
  303. Color get_debug_collision_contact_color() const;
  304. void set_debug_paths_color(const Color &p_color);
  305. Color get_debug_paths_color() const;
  306. void set_debug_paths_width(float p_width);
  307. float get_debug_paths_width() const;
  308. Ref<Material> get_debug_paths_material();
  309. Ref<Material> get_debug_collision_material();
  310. Ref<ArrayMesh> get_debug_contact_mesh();
  311. int get_collision_debug_contact_count() { return collision_debug_contacts; }
  312. int64_t get_frame() const;
  313. int get_node_count() const;
  314. void queue_delete(Object *p_object);
  315. void get_nodes_in_group(const StringName &p_group, List<Node *> *p_list);
  316. Node *get_first_node_in_group(const StringName &p_group);
  317. bool has_group(const StringName &p_identifier) const;
  318. int get_node_count_in_group(const StringName &p_group) const;
  319. //void change_scene(const String& p_path);
  320. //Node *get_loaded_scene();
  321. void set_edited_scene_root(Node *p_node);
  322. Node *get_edited_scene_root() const;
  323. void set_current_scene(Node *p_scene);
  324. Node *get_current_scene() const;
  325. Error change_scene_to_file(const String &p_path);
  326. Error change_scene_to_packed(const Ref<PackedScene> &p_scene);
  327. Error reload_current_scene();
  328. void unload_current_scene();
  329. Ref<SceneTreeTimer> create_timer(double p_delay_sec, bool p_process_always = true, bool p_process_in_physics = false, bool p_ignore_time_scale = false);
  330. Ref<Tween> create_tween();
  331. void remove_tween(const Ref<Tween> &p_tween);
  332. TypedArray<Tween> get_processed_tweens();
  333. //used by Main::start, don't use otherwise
  334. void add_current_scene(Node *p_current);
  335. static SceneTree *get_singleton() { return singleton; }
  336. #ifdef TOOLS_ENABLED
  337. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  338. #endif
  339. //network API
  340. Ref<MultiplayerAPI> get_multiplayer(const NodePath &p_for_path = NodePath()) const;
  341. void set_multiplayer(Ref<MultiplayerAPI> p_multiplayer, const NodePath &p_root_path = NodePath());
  342. void set_multiplayer_poll_enabled(bool p_enabled);
  343. bool is_multiplayer_poll_enabled() const;
  344. static void add_idle_callback(IdleCallback p_callback);
  345. void set_disable_node_threading(bool p_disable);
  346. //default texture settings
  347. void set_physics_interpolation_enabled(bool p_enabled);
  348. bool is_physics_interpolation_enabled() const { return _physics_interpolation_enabled; }
  349. // Different name to disambiguate fast static versions from the user bound versions.
  350. static bool is_fti_enabled() { return _physics_interpolation_enabled; }
  351. static bool is_fti_enabled_in_project() { return _physics_interpolation_enabled_in_project; }
  352. #ifndef _3D_DISABLED
  353. void client_physics_interpolation_add_node_3d(SelfList<Node3D> *p_elem);
  354. void client_physics_interpolation_remove_node_3d(SelfList<Node3D> *p_elem);
  355. #endif
  356. SceneTreeFTI &get_scene_tree_fti() { return scene_tree_fti; }
  357. SceneTree();
  358. ~SceneTree();
  359. };
  360. VARIANT_ENUM_CAST(SceneTree::GroupCallFlags);