scene_tree.h 14 KB

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