scene_tree.h 14 KB

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