-
-
Notifications
You must be signed in to change notification settings - Fork 21.2k
/
scene_tree.cpp
2014 lines (1647 loc) · 64.2 KB
/
scene_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* scene_tree.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "scene_tree.h"
#include "core/config/project_settings.h"
#include "core/debugger/engine_debugger.h"
#include "core/input/input.h"
#include "core/io/dir_access.h"
#include "core/io/image_loader.h"
#include "core/io/marshalls.h"
#include "core/io/resource_loader.h"
#include "core/object/message_queue.h"
#include "core/object/worker_thread_pool.h"
#include "core/os/keyboard.h"
#include "core/os/os.h"
#include "core/string/print_string.h"
#include "node.h"
#include "scene/animation/tween.h"
#include "scene/debugger/scene_debugger.h"
#include "scene/gui/control.h"
#include "scene/main/multiplayer_api.h"
#include "scene/main/viewport.h"
#include "scene/resources/environment.h"
#include "scene/resources/font.h"
#include "scene/resources/image_texture.h"
#include "scene/resources/material.h"
#include "scene/resources/mesh.h"
#include "scene/resources/packed_scene.h"
#include "scene/resources/world_2d.h"
#include "servers/display_server.h"
#include "servers/navigation_server_3d.h"
#include "servers/physics_server_2d.h"
#ifndef _3D_DISABLED
#include "scene/3d/node_3d.h"
#include "scene/resources/3d/world_3d.h"
#include "servers/physics_server_3d.h"
#endif // _3D_DISABLED
#include "window.h"
#include <stdio.h>
#include <stdlib.h>
void SceneTreeTimer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_time_left", "time"), &SceneTreeTimer::set_time_left);
ClassDB::bind_method(D_METHOD("get_time_left"), &SceneTreeTimer::get_time_left);
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_left", PROPERTY_HINT_NONE, "suffix:s"), "set_time_left", "get_time_left");
ADD_SIGNAL(MethodInfo("timeout"));
}
void SceneTreeTimer::set_time_left(double p_time) {
time_left = p_time;
}
double SceneTreeTimer::get_time_left() const {
return MAX(time_left, 0.0);
}
void SceneTreeTimer::set_process_always(bool p_process_always) {
process_always = p_process_always;
}
bool SceneTreeTimer::is_process_always() {
return process_always;
}
void SceneTreeTimer::set_process_in_physics(bool p_process_in_physics) {
process_in_physics = p_process_in_physics;
}
bool SceneTreeTimer::is_process_in_physics() {
return process_in_physics;
}
void SceneTreeTimer::set_ignore_time_scale(bool p_ignore) {
ignore_time_scale = p_ignore;
}
bool SceneTreeTimer::is_ignore_time_scale() {
return ignore_time_scale;
}
void SceneTreeTimer::release_connections() {
List<Connection> signal_connections;
get_all_signal_connections(&signal_connections);
for (const Connection &connection : signal_connections) {
disconnect(connection.signal.get_name(), connection.callable);
}
}
SceneTreeTimer::SceneTreeTimer() {}
#ifndef _3D_DISABLED
// This should be called once per physics tick, to make sure the transform previous and current
// is kept up to date on the few Node3Ds that are using client side physics interpolation.
void SceneTree::ClientPhysicsInterpolation::physics_process() {
for (SelfList<Node3D> *E = _node_3d_list.first(); E;) {
Node3D *node_3d = E->self();
SelfList<Node3D> *current = E;
// Get the next element here BEFORE we potentially delete one.
E = E->next();
// This will return false if the Node3D has timed out ..
// i.e. if get_global_transform_interpolated() has not been called
// for a few seconds, we can delete from the list to keep processing
// to a minimum.
if (!node_3d->update_client_physics_interpolation_data()) {
_node_3d_list.remove(current);
}
}
}
#endif
void SceneTree::tree_changed() {
emit_signal(tree_changed_name);
}
void SceneTree::node_added(Node *p_node) {
emit_signal(node_added_name, p_node);
}
void SceneTree::node_removed(Node *p_node) {
// Nodes can only be removed from the main thread.
if (current_scene == p_node) {
current_scene = nullptr;
}
emit_signal(node_removed_name, p_node);
if (nodes_removed_on_group_call_lock) {
nodes_removed_on_group_call.insert(p_node);
}
}
void SceneTree::node_renamed(Node *p_node) {
emit_signal(node_renamed_name, p_node);
}
SceneTree::Group *SceneTree::add_to_group(const StringName &p_group, Node *p_node) {
_THREAD_SAFE_METHOD_
HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
if (!E) {
E = group_map.insert(p_group, Group());
}
ERR_FAIL_COND_V_MSG(E->value.nodes.has(p_node), &E->value, "Already in group: " + p_group + ".");
E->value.nodes.push_back(p_node);
E->value.changed = true;
return &E->value;
}
void SceneTree::remove_from_group(const StringName &p_group, Node *p_node) {
_THREAD_SAFE_METHOD_
HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
ERR_FAIL_COND(!E);
E->value.nodes.erase(p_node);
if (E->value.nodes.is_empty()) {
group_map.remove(E);
}
}
void SceneTree::make_group_changed(const StringName &p_group) {
_THREAD_SAFE_METHOD_
HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
if (E) {
E->value.changed = true;
}
}
void SceneTree::flush_transform_notifications() {
_THREAD_SAFE_METHOD_
SelfList<Node> *n = xform_change_list.first();
while (n) {
Node *node = n->self();
SelfList<Node> *nx = n->next();
xform_change_list.remove(n);
n = nx;
node->notification(NOTIFICATION_TRANSFORM_CHANGED);
}
}
void SceneTree::_flush_ugc() {
ugc_locked = true;
while (unique_group_calls.size()) {
HashMap<UGCall, Vector<Variant>, UGCall>::Iterator E = unique_group_calls.begin();
const Variant **argptrs = (const Variant **)alloca(E->value.size() * sizeof(Variant *));
for (int i = 0; i < E->value.size(); i++) {
argptrs[i] = &E->value[i];
}
call_group_flagsp(GROUP_CALL_DEFAULT, E->key.group, E->key.call, argptrs, E->value.size());
unique_group_calls.remove(E);
}
ugc_locked = false;
}
void SceneTree::_update_group_order(Group &g) {
if (!g.changed) {
return;
}
if (g.nodes.is_empty()) {
return;
}
Node **gr_nodes = g.nodes.ptrw();
int gr_node_count = g.nodes.size();
SortArray<Node *, Node::Comparator> node_sort;
node_sort.sort(gr_nodes, gr_node_count);
g.changed = false;
}
void SceneTree::call_group_flagsp(uint32_t p_call_flags, const StringName &p_group, const StringName &p_function, const Variant **p_args, int p_argcount) {
Vector<Node *> nodes_copy;
{
_THREAD_SAFE_METHOD_
HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
if (!E) {
return;
}
Group &g = E->value;
if (g.nodes.is_empty()) {
return;
}
if (p_call_flags & GROUP_CALL_UNIQUE && p_call_flags & GROUP_CALL_DEFERRED) {
ERR_FAIL_COND(ugc_locked);
UGCall ug;
ug.call = p_function;
ug.group = p_group;
if (unique_group_calls.has(ug)) {
return;
}
Vector<Variant> args;
for (int i = 0; i < p_argcount; i++) {
args.push_back(*p_args[i]);
}
unique_group_calls[ug] = args;
return;
}
_update_group_order(g);
nodes_copy = g.nodes;
}
Node **gr_nodes = nodes_copy.ptrw();
int gr_node_count = nodes_copy.size();
{
_THREAD_SAFE_METHOD_
nodes_removed_on_group_call_lock++;
}
if (p_call_flags & GROUP_CALL_REVERSE) {
for (int i = gr_node_count - 1; i >= 0; i--) {
if (nodes_removed_on_group_call_lock && nodes_removed_on_group_call.has(gr_nodes[i])) {
continue;
}
Node *node = gr_nodes[i];
if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
Callable::CallError ce;
node->callp(p_function, p_args, p_argcount, ce);
if (unlikely(ce.error != Callable::CallError::CALL_OK && ce.error != Callable::CallError::CALL_ERROR_INVALID_METHOD)) {
ERR_PRINT(vformat("Error calling group method on node \"%s\": %s.", node->get_name(), Variant::get_callable_error_text(Callable(node, p_function), p_args, p_argcount, ce)));
}
} else {
MessageQueue::get_singleton()->push_callp(node, p_function, p_args, p_argcount);
}
}
} else {
for (int i = 0; i < gr_node_count; i++) {
if (nodes_removed_on_group_call_lock && nodes_removed_on_group_call.has(gr_nodes[i])) {
continue;
}
Node *node = gr_nodes[i];
if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
Callable::CallError ce;
node->callp(p_function, p_args, p_argcount, ce);
if (unlikely(ce.error != Callable::CallError::CALL_OK && ce.error != Callable::CallError::CALL_ERROR_INVALID_METHOD)) {
ERR_PRINT(vformat("Error calling group method on node \"%s\": %s.", node->get_name(), Variant::get_callable_error_text(Callable(node, p_function), p_args, p_argcount, ce)));
}
} else {
MessageQueue::get_singleton()->push_callp(node, p_function, p_args, p_argcount);
}
}
}
{
_THREAD_SAFE_METHOD_
nodes_removed_on_group_call_lock--;
if (nodes_removed_on_group_call_lock == 0) {
nodes_removed_on_group_call.clear();
}
}
}
void SceneTree::notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification) {
Vector<Node *> nodes_copy;
{
_THREAD_SAFE_METHOD_
HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
if (!E) {
return;
}
Group &g = E->value;
if (g.nodes.is_empty()) {
return;
}
_update_group_order(g);
nodes_copy = g.nodes;
}
Node **gr_nodes = nodes_copy.ptrw();
int gr_node_count = nodes_copy.size();
{
_THREAD_SAFE_METHOD_
nodes_removed_on_group_call_lock++;
}
if (p_call_flags & GROUP_CALL_REVERSE) {
for (int i = gr_node_count - 1; i >= 0; i--) {
if (nodes_removed_on_group_call.has(gr_nodes[i])) {
continue;
}
if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
gr_nodes[i]->notification(p_notification, true);
} else {
MessageQueue::get_singleton()->push_notification(gr_nodes[i], p_notification);
}
}
} else {
for (int i = 0; i < gr_node_count; i++) {
if (nodes_removed_on_group_call.has(gr_nodes[i])) {
continue;
}
if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
gr_nodes[i]->notification(p_notification);
} else {
MessageQueue::get_singleton()->push_notification(gr_nodes[i], p_notification);
}
}
}
{
_THREAD_SAFE_METHOD_
nodes_removed_on_group_call_lock--;
if (nodes_removed_on_group_call_lock == 0) {
nodes_removed_on_group_call.clear();
}
}
}
void SceneTree::set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value) {
Vector<Node *> nodes_copy;
{
_THREAD_SAFE_METHOD_
HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
if (!E) {
return;
}
Group &g = E->value;
if (g.nodes.is_empty()) {
return;
}
_update_group_order(g);
nodes_copy = g.nodes;
}
Node **gr_nodes = nodes_copy.ptrw();
int gr_node_count = nodes_copy.size();
{
_THREAD_SAFE_METHOD_
nodes_removed_on_group_call_lock++;
}
if (p_call_flags & GROUP_CALL_REVERSE) {
for (int i = gr_node_count - 1; i >= 0; i--) {
if (nodes_removed_on_group_call.has(gr_nodes[i])) {
continue;
}
if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
gr_nodes[i]->set(p_name, p_value);
} else {
MessageQueue::get_singleton()->push_set(gr_nodes[i], p_name, p_value);
}
}
} else {
for (int i = 0; i < gr_node_count; i++) {
if (nodes_removed_on_group_call.has(gr_nodes[i])) {
continue;
}
if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
gr_nodes[i]->set(p_name, p_value);
} else {
MessageQueue::get_singleton()->push_set(gr_nodes[i], p_name, p_value);
}
}
}
{
_THREAD_SAFE_METHOD_
nodes_removed_on_group_call_lock--;
if (nodes_removed_on_group_call_lock == 0) {
nodes_removed_on_group_call.clear();
}
}
}
void SceneTree::notify_group(const StringName &p_group, int p_notification) {
notify_group_flags(GROUP_CALL_DEFAULT, p_group, p_notification);
}
void SceneTree::set_group(const StringName &p_group, const String &p_name, const Variant &p_value) {
set_group_flags(GROUP_CALL_DEFAULT, p_group, p_name, p_value);
}
void SceneTree::initialize() {
ERR_FAIL_NULL(root);
MainLoop::initialize();
root->_set_tree(this);
}
void SceneTree::set_physics_interpolation_enabled(bool p_enabled) {
// We never want interpolation in the editor.
if (Engine::get_singleton()->is_editor_hint()) {
p_enabled = false;
}
if (p_enabled == _physics_interpolation_enabled) {
return;
}
_physics_interpolation_enabled = p_enabled;
RenderingServer::get_singleton()->set_physics_interpolation_enabled(p_enabled);
}
bool SceneTree::is_physics_interpolation_enabled() const {
return _physics_interpolation_enabled;
}
#ifndef _3D_DISABLED
void SceneTree::client_physics_interpolation_add_node_3d(SelfList<Node3D> *p_elem) {
// This ensures that _update_physics_interpolation_data() will be called at least once every
// physics tick, to ensure the previous and current transforms are kept up to date.
_client_physics_interpolation._node_3d_list.add(p_elem);
}
void SceneTree::client_physics_interpolation_remove_node_3d(SelfList<Node3D> *p_elem) {
_client_physics_interpolation._node_3d_list.remove(p_elem);
}
#endif
void SceneTree::iteration_prepare() {
if (_physics_interpolation_enabled) {
// Make sure any pending transforms from the last tick / frame
// are flushed before pumping the interpolation prev and currents.
flush_transform_notifications();
RenderingServer::get_singleton()->tick();
#ifndef _3D_DISABLED
// Any objects performing client physics interpolation
// should be given an opportunity to keep their previous transforms
// up to date before each new physics tick.
_client_physics_interpolation.physics_process();
#endif
}
}
bool SceneTree::physics_process(double p_time) {
current_frame++;
flush_transform_notifications();
if (MainLoop::physics_process(p_time)) {
_quit = true;
}
physics_process_time = p_time;
emit_signal(SNAME("physics_frame"));
call_group(SNAME("_picking_viewports"), SNAME("_process_picking"));
_process(true);
_flush_ugc();
MessageQueue::get_singleton()->flush(); //small little hack
process_timers(p_time, true); //go through timers
process_tweens(p_time, true);
flush_transform_notifications();
_flush_delete_queue();
_call_idle_callbacks();
return _quit;
}
void SceneTree::iteration_end() {
// When physics interpolation is active, we want all pending transforms
// to be flushed to the RenderingServer before finishing a physics tick.
if (_physics_interpolation_enabled) {
flush_transform_notifications();
}
}
bool SceneTree::process(double p_time) {
if (MainLoop::process(p_time)) {
_quit = true;
}
process_time = p_time;
if (multiplayer_poll) {
multiplayer->poll();
for (KeyValue<NodePath, Ref<MultiplayerAPI>> &E : custom_multiplayers) {
E.value->poll();
}
}
emit_signal(SNAME("process_frame"));
MessageQueue::get_singleton()->flush(); //small little hack
flush_transform_notifications();
_process(false);
_flush_ugc();
MessageQueue::get_singleton()->flush(); //small little hack
flush_transform_notifications(); //transforms after world update, to avoid unnecessary enter/exit notifications
_flush_delete_queue();
if (unlikely(pending_new_scene)) {
_flush_scene_change();
}
process_timers(p_time, false); //go through timers
process_tweens(p_time, false);
flush_transform_notifications(); //additional transforms after timers update
_call_idle_callbacks();
#ifdef TOOLS_ENABLED
#ifndef _3D_DISABLED
if (Engine::get_singleton()->is_editor_hint()) {
//simple hack to reload fallback environment if it changed from editor
String env_path = GLOBAL_GET(SNAME("rendering/environment/defaults/default_environment"));
env_path = env_path.strip_edges(); //user may have added a space or two
String cpath;
Ref<Environment> fallback = get_root()->get_world_3d()->get_fallback_environment();
if (fallback.is_valid()) {
cpath = fallback->get_path();
}
if (cpath != env_path) {
if (!env_path.is_empty()) {
fallback = ResourceLoader::load(env_path);
if (fallback.is_null()) {
//could not load fallback, set as empty
ProjectSettings::get_singleton()->set("rendering/environment/defaults/default_environment", "");
}
} else {
fallback.unref();
}
get_root()->get_world_3d()->set_fallback_environment(fallback);
}
}
#endif // _3D_DISABLED
#endif // TOOLS_ENABLED
if (_physics_interpolation_enabled) {
RenderingServer::get_singleton()->pre_draw(true);
}
return _quit;
}
void SceneTree::process_timers(double p_delta, bool p_physics_frame) {
_THREAD_SAFE_METHOD_
List<Ref<SceneTreeTimer>>::Element *L = timers.back(); //last element
for (List<Ref<SceneTreeTimer>>::Element *E = timers.front(); E;) {
List<Ref<SceneTreeTimer>>::Element *N = E->next();
if ((paused && !E->get()->is_process_always()) || (E->get()->is_process_in_physics() != p_physics_frame)) {
if (E == L) {
break; //break on last, so if new timers were added during list traversal, ignore them.
}
E = N;
continue;
}
double time_left = E->get()->get_time_left();
if (E->get()->is_ignore_time_scale()) {
time_left -= Engine::get_singleton()->get_process_step();
} else {
time_left -= p_delta;
}
E->get()->set_time_left(time_left);
if (time_left <= 0) {
E->get()->emit_signal(SNAME("timeout"));
timers.erase(E);
}
if (E == L) {
break; //break on last, so if new timers were added during list traversal, ignore them.
}
E = N;
}
}
void SceneTree::process_tweens(double p_delta, bool p_physics) {
_THREAD_SAFE_METHOD_
// This methods works similarly to how SceneTreeTimers are handled.
List<Ref<Tween>>::Element *L = tweens.back();
for (List<Ref<Tween>>::Element *E = tweens.front(); E;) {
List<Ref<Tween>>::Element *N = E->next();
// Don't process if paused or process mode doesn't match.
if (!E->get()->can_process(paused) || (p_physics == (E->get()->get_process_mode() == Tween::TWEEN_PROCESS_IDLE))) {
if (E == L) {
break;
}
E = N;
continue;
}
if (!E->get()->step(p_delta)) {
E->get()->clear();
tweens.erase(E);
}
if (E == L) {
break;
}
E = N;
}
}
void SceneTree::finalize() {
_flush_delete_queue();
_flush_ugc();
if (root) {
root->_set_tree(nullptr);
root->_propagate_after_exit_tree();
memdelete(root); //delete root
root = nullptr;
// In case deletion of some objects was queued when destructing the `root`.
// E.g. if `queue_free()` was called for some node outside the tree when handling NOTIFICATION_PREDELETE for some node in the tree.
_flush_delete_queue();
}
MainLoop::finalize();
// Cleanup timers.
for (Ref<SceneTreeTimer> &timer : timers) {
timer->release_connections();
}
timers.clear();
// Cleanup tweens.
for (Ref<Tween> &tween : tweens) {
tween->clear();
}
tweens.clear();
}
void SceneTree::quit(int p_exit_code) {
_THREAD_SAFE_METHOD_
OS::get_singleton()->set_exit_code(p_exit_code);
_quit = true;
}
void SceneTree::_main_window_close() {
if (accept_quit) {
_quit = true;
}
}
void SceneTree::_main_window_go_back() {
if (quit_on_go_back) {
_quit = true;
}
}
void SceneTree::_main_window_focus_in() {
Input *id = Input::get_singleton();
if (id) {
id->ensure_touch_mouse_raised();
}
}
void SceneTree::_notification(int p_notification) {
switch (p_notification) {
case NOTIFICATION_TRANSLATION_CHANGED: {
if (!Engine::get_singleton()->is_editor_hint()) {
get_root()->propagate_notification(p_notification);
}
} break;
case NOTIFICATION_OS_MEMORY_WARNING:
case NOTIFICATION_OS_IME_UPDATE:
case NOTIFICATION_WM_ABOUT:
case NOTIFICATION_CRASH:
case NOTIFICATION_APPLICATION_RESUMED:
case NOTIFICATION_APPLICATION_PAUSED:
case NOTIFICATION_APPLICATION_FOCUS_IN:
case NOTIFICATION_APPLICATION_FOCUS_OUT: {
// Pass these to nodes, since they are mirrored.
get_root()->propagate_notification(p_notification);
} break;
}
}
bool SceneTree::is_auto_accept_quit() const {
return accept_quit;
}
void SceneTree::set_auto_accept_quit(bool p_enable) {
accept_quit = p_enable;
}
bool SceneTree::is_quit_on_go_back() const {
return quit_on_go_back;
}
void SceneTree::set_quit_on_go_back(bool p_enable) {
quit_on_go_back = p_enable;
}
#ifdef DEBUG_ENABLED
void SceneTree::set_debug_collisions_hint(bool p_enabled) {
debug_collisions_hint = p_enabled;
}
bool SceneTree::is_debugging_collisions_hint() const {
return debug_collisions_hint;
}
void SceneTree::set_debug_paths_hint(bool p_enabled) {
debug_paths_hint = p_enabled;
}
bool SceneTree::is_debugging_paths_hint() const {
return debug_paths_hint;
}
void SceneTree::set_debug_navigation_hint(bool p_enabled) {
debug_navigation_hint = p_enabled;
}
bool SceneTree::is_debugging_navigation_hint() const {
return debug_navigation_hint;
}
#endif
void SceneTree::set_debug_collisions_color(const Color &p_color) {
debug_collisions_color = p_color;
}
Color SceneTree::get_debug_collisions_color() const {
return debug_collisions_color;
}
void SceneTree::set_debug_collision_contact_color(const Color &p_color) {
debug_collision_contact_color = p_color;
}
Color SceneTree::get_debug_collision_contact_color() const {
return debug_collision_contact_color;
}
void SceneTree::set_debug_paths_color(const Color &p_color) {
debug_paths_color = p_color;
}
Color SceneTree::get_debug_paths_color() const {
return debug_paths_color;
}
void SceneTree::set_debug_paths_width(float p_width) {
debug_paths_width = p_width;
}
float SceneTree::get_debug_paths_width() const {
return debug_paths_width;
}
Ref<Material> SceneTree::get_debug_paths_material() {
_THREAD_SAFE_METHOD_
if (debug_paths_material.is_valid()) {
return debug_paths_material;
}
Ref<StandardMaterial3D> _debug_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
_debug_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
_debug_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
_debug_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
_debug_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
_debug_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
_debug_material->set_albedo(get_debug_paths_color());
debug_paths_material = _debug_material;
return debug_paths_material;
}
Ref<Material> SceneTree::get_debug_collision_material() {
_THREAD_SAFE_METHOD_
if (collision_material.is_valid()) {
return collision_material;
}
Ref<StandardMaterial3D> line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
line_material->set_albedo(get_debug_collisions_color());
collision_material = line_material;
return collision_material;
}
Ref<ArrayMesh> SceneTree::get_debug_contact_mesh() {
_THREAD_SAFE_METHOD_
if (debug_contact_mesh.is_valid()) {
return debug_contact_mesh;
}
debug_contact_mesh.instantiate();
Ref<StandardMaterial3D> mat = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
mat->set_albedo(get_debug_collision_contact_color());
Vector3 diamond[6] = {
Vector3(-1, 0, 0),
Vector3(1, 0, 0),
Vector3(0, -1, 0),
Vector3(0, 1, 0),
Vector3(0, 0, -1),
Vector3(0, 0, 1)
};
/* clang-format off */
int diamond_faces[8 * 3] = {
0, 2, 4,
0, 3, 4,
1, 2, 4,
1, 3, 4,
0, 2, 5,
0, 3, 5,
1, 2, 5,
1, 3, 5,
};
/* clang-format on */
Vector<int> indices;
for (int i = 0; i < 8 * 3; i++) {
indices.push_back(diamond_faces[i]);
}
Vector<Vector3> vertices;
for (int i = 0; i < 6; i++) {
vertices.push_back(diamond[i] * 0.1);
}
Array arr;
arr.resize(Mesh::ARRAY_MAX);
arr[Mesh::ARRAY_VERTEX] = vertices;
arr[Mesh::ARRAY_INDEX] = indices;
debug_contact_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
debug_contact_mesh->surface_set_material(0, mat);
return debug_contact_mesh;
}
void SceneTree::set_pause(bool p_enabled) {
ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Pause can only be set from the main thread.");
ERR_FAIL_COND_MSG(suspended, "Pause state cannot be modified while suspended.");
if (p_enabled == paused) {
return;
}
paused = p_enabled;
#ifndef _3D_DISABLED
PhysicsServer3D::get_singleton()->set_active(!p_enabled);
#endif // _3D_DISABLED
PhysicsServer2D::get_singleton()->set_active(!p_enabled);
if (get_root()) {
get_root()->_propagate_pause_notification(p_enabled);
}
}
bool SceneTree::is_paused() const {
return paused;
}
void SceneTree::set_suspend(bool p_enabled) {
ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Suspend can only be set from the main thread.");
if (p_enabled == suspended) {
return;
}
suspended = p_enabled;
Engine::get_singleton()->set_freeze_time_scale(p_enabled);
#ifndef _3D_DISABLED
PhysicsServer3D::get_singleton()->set_active(!p_enabled && !paused);
#endif // _3D_DISABLED
PhysicsServer2D::get_singleton()->set_active(!p_enabled && !paused);
if (get_root()) {
get_root()->_propagate_suspend_notification(p_enabled);
}
}
bool SceneTree::is_suspended() const {
return suspended;
}