scene_tree.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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/resources/mesh.h"
  36. #undef Window
  37. class PackedScene;
  38. class Node;
  39. #ifndef _3D_DISABLED
  40. class Node3D;
  41. #endif
  42. class Window;
  43. class Material;
  44. class Mesh;
  45. class MultiplayerAPI;
  46. class SceneDebugger;
  47. class Tween;
  48. class Viewport;
  49. class SceneTreeTimer : public RefCounted {
  50. GDCLASS(SceneTreeTimer, RefCounted);
  51. double time_left = 0.0;
  52. bool process_always = true;
  53. bool process_in_physics = false;
  54. bool ignore_time_scale = false;
  55. protected:
  56. static void _bind_methods();
  57. public:
  58. void set_time_left(double p_time);
  59. double get_time_left() const;
  60. void set_process_always(bool p_process_always);
  61. bool is_process_always();
  62. void set_process_in_physics(bool p_process_in_physics);
  63. bool is_process_in_physics();
  64. void set_ignore_time_scale(bool p_ignore);
  65. bool is_ignoring_time_scale();
  66. void release_connections();
  67. SceneTreeTimer();
  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. bool _physics_interpolation_enabled = false;
  121. StringName tree_changed_name = "tree_changed";
  122. StringName node_added_name = "node_added";
  123. StringName node_removed_name = "node_removed";
  124. StringName node_renamed_name = "node_renamed";
  125. int64_t current_frame = 0;
  126. int nodes_in_tree_count = 0;
  127. #ifdef TOOLS_ENABLED
  128. Node *edited_scene_root = nullptr;
  129. #endif
  130. struct UGCall {
  131. StringName group;
  132. StringName call;
  133. static uint32_t hash(const UGCall &p_val) {
  134. return p_val.group.hash() ^ p_val.call.hash();
  135. }
  136. bool operator==(const UGCall &p_with) const { return group == p_with.group && call == p_with.call; }
  137. bool operator<(const UGCall &p_with) const { return group == p_with.group ? call < p_with.call : group < p_with.group; }
  138. };
  139. // Safety for when a node is deleted while a group is being called.
  140. int nodes_removed_on_group_call_lock = 0;
  141. HashSet<Node *> nodes_removed_on_group_call; // Skip erased nodes.
  142. List<ObjectID> delete_queue;
  143. HashMap<UGCall, Vector<Variant>, UGCall> unique_group_calls;
  144. bool ugc_locked = false;
  145. void _flush_ugc();
  146. _FORCE_INLINE_ void _update_group_order(Group &g);
  147. TypedArray<Node> _get_nodes_in_group(const StringName &p_group);
  148. Node *current_scene = nullptr;
  149. ObjectID prev_scene_id;
  150. ObjectID pending_new_scene_id;
  151. Color debug_collisions_color;
  152. Color debug_collision_contact_color;
  153. Color debug_paths_color;
  154. float debug_paths_width = 1.0f;
  155. Ref<ArrayMesh> debug_contact_mesh;
  156. Ref<Material> debug_paths_material;
  157. Ref<Material> collision_material;
  158. int collision_debug_contacts;
  159. void _flush_scene_change();
  160. List<Ref<SceneTreeTimer>> timers;
  161. List<Ref<Tween>> tweens;
  162. ///network///
  163. Ref<MultiplayerAPI> multiplayer;
  164. HashMap<NodePath, Ref<MultiplayerAPI>> custom_multiplayers;
  165. bool multiplayer_poll = true;
  166. static SceneTree *singleton;
  167. friend class Node;
  168. void tree_changed();
  169. void node_added(Node *p_node);
  170. void node_removed(Node *p_node);
  171. void node_renamed(Node *p_node);
  172. void process_timers(double p_delta, bool p_physics_frame);
  173. void process_tweens(double p_delta, bool p_physics_frame);
  174. Group *add_to_group(const StringName &p_group, Node *p_node);
  175. void remove_from_group(const StringName &p_group, Node *p_node);
  176. void make_group_changed(const StringName &p_group);
  177. void _process_group(ProcessGroup *p_group, bool p_physics);
  178. void _process_groups_thread(uint32_t p_index, bool p_physics);
  179. void _process(bool p_physics);
  180. void _remove_process_group(Node *p_node);
  181. void _add_process_group(Node *p_node);
  182. void _remove_node_from_process_group(Node *p_node, Node *p_owner);
  183. void _add_node_to_process_group(Node *p_node, Node *p_owner);
  184. void _call_group_flags(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  185. void _call_group(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  186. void _flush_delete_queue();
  187. // Optimization.
  188. friend class CanvasItem;
  189. friend class Node3D;
  190. friend class Viewport;
  191. SelfList<Node>::List xform_change_list;
  192. #ifdef DEBUG_ENABLED // No live editor in release build.
  193. friend class LiveEditor;
  194. #endif
  195. enum {
  196. MAX_IDLE_CALLBACKS = 256
  197. };
  198. static IdleCallback idle_callbacks[MAX_IDLE_CALLBACKS];
  199. static int idle_callback_count;
  200. void _call_idle_callbacks();
  201. void _main_window_focus_in();
  202. void _main_window_close();
  203. void _main_window_go_back();
  204. enum CallInputType {
  205. CALL_INPUT_TYPE_INPUT,
  206. CALL_INPUT_TYPE_SHORTCUT_INPUT,
  207. CALL_INPUT_TYPE_UNHANDLED_INPUT,
  208. CALL_INPUT_TYPE_UNHANDLED_KEY_INPUT,
  209. };
  210. //used by viewport
  211. void _call_input_pause(const StringName &p_group, CallInputType p_call_type, const Ref<InputEvent> &p_input, Viewport *p_viewport);
  212. protected:
  213. void _notification(int p_notification);
  214. static void _bind_methods();
  215. public:
  216. enum {
  217. NOTIFICATION_TRANSFORM_CHANGED = 2000
  218. };
  219. enum GroupCallFlags {
  220. GROUP_CALL_DEFAULT = 0,
  221. GROUP_CALL_REVERSE = 1,
  222. GROUP_CALL_DEFERRED = 2,
  223. GROUP_CALL_UNIQUE = 4,
  224. };
  225. _FORCE_INLINE_ Window *get_root() const { return root; }
  226. void call_group_flagsp(uint32_t p_call_flags, const StringName &p_group, const StringName &p_function, const Variant **p_args, int p_argcount);
  227. void notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification);
  228. void set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value);
  229. // `notify_group()` is immediate by default since Godot 4.0.
  230. void notify_group(const StringName &p_group, int p_notification);
  231. // `set_group()` is immediate by default since Godot 4.0.
  232. void set_group(const StringName &p_group, const String &p_name, const Variant &p_value);
  233. template <typename... VarArgs>
  234. // `call_group()` is immediate by default since Godot 4.0.
  235. void call_group(const StringName &p_group, const StringName &p_function, VarArgs... p_args) {
  236. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  237. const Variant *argptrs[sizeof...(p_args) + 1];
  238. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  239. argptrs[i] = &args[i];
  240. }
  241. call_group_flagsp(GROUP_CALL_DEFAULT, p_group, p_function, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  242. }
  243. template <typename... VarArgs>
  244. void call_group_flags(uint32_t p_flags, 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(p_flags, p_group, p_function, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  251. }
  252. void flush_transform_notifications();
  253. virtual void initialize() override;
  254. virtual void iteration_prepare() override;
  255. virtual bool physics_process(double p_time) override;
  256. virtual void iteration_end() override;
  257. virtual bool process(double p_time) override;
  258. virtual void finalize() override;
  259. bool is_auto_accept_quit() const;
  260. void set_auto_accept_quit(bool p_enable);
  261. bool is_quit_on_go_back() const;
  262. void set_quit_on_go_back(bool p_enable);
  263. void quit(int p_exit_code = EXIT_SUCCESS);
  264. _FORCE_INLINE_ double get_physics_process_time() const { return physics_process_time; }
  265. _FORCE_INLINE_ double get_process_time() const { return process_time; }
  266. void set_pause(bool p_enabled);
  267. bool is_paused() const;
  268. void set_suspend(bool p_enabled);
  269. bool is_suspended() const;
  270. #ifdef DEBUG_ENABLED
  271. void set_debug_collisions_hint(bool p_enabled);
  272. bool is_debugging_collisions_hint() const;
  273. void set_debug_paths_hint(bool p_enabled);
  274. bool is_debugging_paths_hint() const;
  275. void set_debug_navigation_hint(bool p_enabled);
  276. bool is_debugging_navigation_hint() const;
  277. #else
  278. void set_debug_collisions_hint(bool p_enabled) {}
  279. bool is_debugging_collisions_hint() const { return false; }
  280. void set_debug_paths_hint(bool p_enabled) {}
  281. bool is_debugging_paths_hint() const { return false; }
  282. void set_debug_navigation_hint(bool p_enabled) {}
  283. bool is_debugging_navigation_hint() const { return false; }
  284. #endif
  285. void set_debug_collisions_color(const Color &p_color);
  286. Color get_debug_collisions_color() const;
  287. void set_debug_collision_contact_color(const Color &p_color);
  288. Color get_debug_collision_contact_color() const;
  289. void set_debug_paths_color(const Color &p_color);
  290. Color get_debug_paths_color() const;
  291. void set_debug_paths_width(float p_width);
  292. float get_debug_paths_width() const;
  293. Ref<Material> get_debug_paths_material();
  294. Ref<Material> get_debug_collision_material();
  295. Ref<ArrayMesh> get_debug_contact_mesh();
  296. int get_collision_debug_contact_count() { return collision_debug_contacts; }
  297. int64_t get_frame() const;
  298. int get_node_count() const;
  299. void queue_delete(Object *p_object);
  300. void get_nodes_in_group(const StringName &p_group, List<Node *> *p_list);
  301. Node *get_first_node_in_group(const StringName &p_group);
  302. bool has_group(const StringName &p_identifier) const;
  303. int get_node_count_in_group(const StringName &p_group) const;
  304. //void change_scene(const String& p_path);
  305. //Node *get_loaded_scene();
  306. void set_edited_scene_root(Node *p_node);
  307. Node *get_edited_scene_root() const;
  308. void set_current_scene(Node *p_scene);
  309. Node *get_current_scene() const;
  310. Error change_scene_to_file(const String &p_path);
  311. Error change_scene_to_packed(const Ref<PackedScene> &p_scene);
  312. Error reload_current_scene();
  313. void unload_current_scene();
  314. 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);
  315. Ref<Tween> create_tween();
  316. void remove_tween(const Ref<Tween> &p_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);