Debian Codegraph @

scene/main/scene_tree.h

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