scene_tree.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  1. /**************************************************************************/
  2. /* scene_tree.cpp */
  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. #include "scene_tree.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/input/input.h"
  34. #include "core/io/dir_access.h"
  35. #include "core/io/image_loader.h"
  36. #include "core/io/marshalls.h"
  37. #include "core/io/resource_loader.h"
  38. #include "core/object/message_queue.h"
  39. #include "core/object/worker_thread_pool.h"
  40. #include "core/os/keyboard.h"
  41. #include "core/os/os.h"
  42. #include "core/string/print_string.h"
  43. #include "node.h"
  44. #include "scene/animation/tween.h"
  45. #include "scene/debugger/scene_debugger.h"
  46. #include "scene/gui/control.h"
  47. #include "scene/main/multiplayer_api.h"
  48. #include "scene/main/viewport.h"
  49. #include "scene/resources/environment.h"
  50. #include "scene/resources/font.h"
  51. #include "scene/resources/image_texture.h"
  52. #include "scene/resources/material.h"
  53. #include "scene/resources/mesh.h"
  54. #include "scene/resources/packed_scene.h"
  55. #include "scene/resources/world_2d.h"
  56. #include "scene/scene_string_names.h"
  57. #include "servers/display_server.h"
  58. #include "servers/navigation_server_3d.h"
  59. #include "servers/physics_server_2d.h"
  60. #ifndef _3D_DISABLED
  61. #include "scene/resources/3d/world_3d.h"
  62. #include "servers/physics_server_3d.h"
  63. #endif // _3D_DISABLED
  64. #include "window.h"
  65. #include <stdio.h>
  66. #include <stdlib.h>
  67. void SceneTreeTimer::_bind_methods() {
  68. ClassDB::bind_method(D_METHOD("set_time_left", "time"), &SceneTreeTimer::set_time_left);
  69. ClassDB::bind_method(D_METHOD("get_time_left"), &SceneTreeTimer::get_time_left);
  70. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_left", PROPERTY_HINT_NONE, "suffix:s"), "set_time_left", "get_time_left");
  71. ADD_SIGNAL(MethodInfo("timeout"));
  72. }
  73. void SceneTreeTimer::set_time_left(double p_time) {
  74. time_left = p_time;
  75. }
  76. double SceneTreeTimer::get_time_left() const {
  77. return MAX(time_left, 0.0);
  78. }
  79. void SceneTreeTimer::set_process_always(bool p_process_always) {
  80. process_always = p_process_always;
  81. }
  82. bool SceneTreeTimer::is_process_always() {
  83. return process_always;
  84. }
  85. void SceneTreeTimer::set_process_in_physics(bool p_process_in_physics) {
  86. process_in_physics = p_process_in_physics;
  87. }
  88. bool SceneTreeTimer::is_process_in_physics() {
  89. return process_in_physics;
  90. }
  91. void SceneTreeTimer::set_ignore_time_scale(bool p_ignore) {
  92. ignore_time_scale = p_ignore;
  93. }
  94. bool SceneTreeTimer::is_ignore_time_scale() {
  95. return ignore_time_scale;
  96. }
  97. void SceneTreeTimer::release_connections() {
  98. List<Connection> signal_connections;
  99. get_all_signal_connections(&signal_connections);
  100. for (const Connection &connection : signal_connections) {
  101. disconnect(connection.signal.get_name(), connection.callable);
  102. }
  103. }
  104. SceneTreeTimer::SceneTreeTimer() {}
  105. void SceneTree::tree_changed() {
  106. tree_version++;
  107. emit_signal(tree_changed_name);
  108. }
  109. void SceneTree::node_added(Node *p_node) {
  110. emit_signal(node_added_name, p_node);
  111. }
  112. void SceneTree::node_removed(Node *p_node) {
  113. // Nodes can only be removed from the main thread.
  114. if (current_scene == p_node) {
  115. current_scene = nullptr;
  116. }
  117. emit_signal(node_removed_name, p_node);
  118. if (nodes_removed_on_group_call_lock) {
  119. nodes_removed_on_group_call.insert(p_node);
  120. }
  121. }
  122. void SceneTree::node_renamed(Node *p_node) {
  123. emit_signal(node_renamed_name, p_node);
  124. }
  125. SceneTree::Group *SceneTree::add_to_group(const StringName &p_group, Node *p_node) {
  126. _THREAD_SAFE_METHOD_
  127. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  128. if (!E) {
  129. E = group_map.insert(p_group, Group());
  130. }
  131. ERR_FAIL_COND_V_MSG(E->value.nodes.has(p_node), &E->value, "Already in group: " + p_group + ".");
  132. E->value.nodes.push_back(p_node);
  133. //E->value.last_tree_version=0;
  134. E->value.changed = true;
  135. return &E->value;
  136. }
  137. void SceneTree::remove_from_group(const StringName &p_group, Node *p_node) {
  138. _THREAD_SAFE_METHOD_
  139. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  140. ERR_FAIL_COND(!E);
  141. E->value.nodes.erase(p_node);
  142. if (E->value.nodes.is_empty()) {
  143. group_map.remove(E);
  144. }
  145. }
  146. void SceneTree::make_group_changed(const StringName &p_group) {
  147. _THREAD_SAFE_METHOD_
  148. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  149. if (E) {
  150. E->value.changed = true;
  151. }
  152. }
  153. void SceneTree::flush_transform_notifications() {
  154. _THREAD_SAFE_METHOD_
  155. SelfList<Node> *n = xform_change_list.first();
  156. while (n) {
  157. Node *node = n->self();
  158. SelfList<Node> *nx = n->next();
  159. xform_change_list.remove(n);
  160. n = nx;
  161. node->notification(NOTIFICATION_TRANSFORM_CHANGED);
  162. }
  163. }
  164. void SceneTree::_flush_ugc() {
  165. ugc_locked = true;
  166. while (unique_group_calls.size()) {
  167. HashMap<UGCall, Vector<Variant>, UGCall>::Iterator E = unique_group_calls.begin();
  168. const Variant **argptrs = (const Variant **)alloca(E->value.size() * sizeof(Variant *));
  169. for (int i = 0; i < E->value.size(); i++) {
  170. argptrs[i] = &E->value[i];
  171. }
  172. call_group_flagsp(GROUP_CALL_DEFAULT, E->key.group, E->key.call, argptrs, E->value.size());
  173. unique_group_calls.remove(E);
  174. }
  175. ugc_locked = false;
  176. }
  177. void SceneTree::_update_group_order(Group &g) {
  178. if (!g.changed) {
  179. return;
  180. }
  181. if (g.nodes.is_empty()) {
  182. return;
  183. }
  184. Node **gr_nodes = g.nodes.ptrw();
  185. int gr_node_count = g.nodes.size();
  186. SortArray<Node *, Node::Comparator> node_sort;
  187. node_sort.sort(gr_nodes, gr_node_count);
  188. g.changed = false;
  189. }
  190. 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) {
  191. Vector<Node *> nodes_copy;
  192. {
  193. _THREAD_SAFE_METHOD_
  194. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  195. if (!E) {
  196. return;
  197. }
  198. Group &g = E->value;
  199. if (g.nodes.is_empty()) {
  200. return;
  201. }
  202. if (p_call_flags & GROUP_CALL_UNIQUE && p_call_flags & GROUP_CALL_DEFERRED) {
  203. ERR_FAIL_COND(ugc_locked);
  204. UGCall ug;
  205. ug.call = p_function;
  206. ug.group = p_group;
  207. if (unique_group_calls.has(ug)) {
  208. return;
  209. }
  210. Vector<Variant> args;
  211. for (int i = 0; i < p_argcount; i++) {
  212. args.push_back(*p_args[i]);
  213. }
  214. unique_group_calls[ug] = args;
  215. return;
  216. }
  217. _update_group_order(g);
  218. nodes_copy = g.nodes;
  219. }
  220. Node **gr_nodes = nodes_copy.ptrw();
  221. int gr_node_count = nodes_copy.size();
  222. {
  223. _THREAD_SAFE_METHOD_
  224. nodes_removed_on_group_call_lock++;
  225. }
  226. if (p_call_flags & GROUP_CALL_REVERSE) {
  227. for (int i = gr_node_count - 1; i >= 0; i--) {
  228. if (nodes_removed_on_group_call_lock && nodes_removed_on_group_call.has(gr_nodes[i])) {
  229. continue;
  230. }
  231. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  232. Callable::CallError ce;
  233. gr_nodes[i]->callp(p_function, p_args, p_argcount, ce);
  234. } else {
  235. MessageQueue::get_singleton()->push_callp(gr_nodes[i], p_function, p_args, p_argcount);
  236. }
  237. }
  238. } else {
  239. for (int i = 0; i < gr_node_count; i++) {
  240. if (nodes_removed_on_group_call_lock && nodes_removed_on_group_call.has(gr_nodes[i])) {
  241. continue;
  242. }
  243. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  244. Callable::CallError ce;
  245. gr_nodes[i]->callp(p_function, p_args, p_argcount, ce);
  246. } else {
  247. MessageQueue::get_singleton()->push_callp(gr_nodes[i], p_function, p_args, p_argcount);
  248. }
  249. }
  250. }
  251. {
  252. _THREAD_SAFE_METHOD_
  253. nodes_removed_on_group_call_lock--;
  254. if (nodes_removed_on_group_call_lock == 0) {
  255. nodes_removed_on_group_call.clear();
  256. }
  257. }
  258. }
  259. void SceneTree::notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification) {
  260. Vector<Node *> nodes_copy;
  261. {
  262. _THREAD_SAFE_METHOD_
  263. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  264. if (!E) {
  265. return;
  266. }
  267. Group &g = E->value;
  268. if (g.nodes.is_empty()) {
  269. return;
  270. }
  271. _update_group_order(g);
  272. nodes_copy = g.nodes;
  273. }
  274. Node **gr_nodes = nodes_copy.ptrw();
  275. int gr_node_count = nodes_copy.size();
  276. {
  277. _THREAD_SAFE_METHOD_
  278. nodes_removed_on_group_call_lock++;
  279. }
  280. if (p_call_flags & GROUP_CALL_REVERSE) {
  281. for (int i = gr_node_count - 1; i >= 0; i--) {
  282. if (nodes_removed_on_group_call.has(gr_nodes[i])) {
  283. continue;
  284. }
  285. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  286. gr_nodes[i]->notification(p_notification, true);
  287. } else {
  288. MessageQueue::get_singleton()->push_notification(gr_nodes[i], p_notification);
  289. }
  290. }
  291. } else {
  292. for (int i = 0; i < gr_node_count; i++) {
  293. if (nodes_removed_on_group_call.has(gr_nodes[i])) {
  294. continue;
  295. }
  296. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  297. gr_nodes[i]->notification(p_notification);
  298. } else {
  299. MessageQueue::get_singleton()->push_notification(gr_nodes[i], p_notification);
  300. }
  301. }
  302. }
  303. {
  304. _THREAD_SAFE_METHOD_
  305. nodes_removed_on_group_call_lock--;
  306. if (nodes_removed_on_group_call_lock == 0) {
  307. nodes_removed_on_group_call.clear();
  308. }
  309. }
  310. }
  311. void SceneTree::set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value) {
  312. Vector<Node *> nodes_copy;
  313. {
  314. _THREAD_SAFE_METHOD_
  315. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  316. if (!E) {
  317. return;
  318. }
  319. Group &g = E->value;
  320. if (g.nodes.is_empty()) {
  321. return;
  322. }
  323. _update_group_order(g);
  324. nodes_copy = g.nodes;
  325. }
  326. Node **gr_nodes = nodes_copy.ptrw();
  327. int gr_node_count = nodes_copy.size();
  328. {
  329. _THREAD_SAFE_METHOD_
  330. nodes_removed_on_group_call_lock++;
  331. }
  332. if (p_call_flags & GROUP_CALL_REVERSE) {
  333. for (int i = gr_node_count - 1; i >= 0; i--) {
  334. if (nodes_removed_on_group_call.has(gr_nodes[i])) {
  335. continue;
  336. }
  337. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  338. gr_nodes[i]->set(p_name, p_value);
  339. } else {
  340. MessageQueue::get_singleton()->push_set(gr_nodes[i], p_name, p_value);
  341. }
  342. }
  343. } else {
  344. for (int i = 0; i < gr_node_count; i++) {
  345. if (nodes_removed_on_group_call.has(gr_nodes[i])) {
  346. continue;
  347. }
  348. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  349. gr_nodes[i]->set(p_name, p_value);
  350. } else {
  351. MessageQueue::get_singleton()->push_set(gr_nodes[i], p_name, p_value);
  352. }
  353. }
  354. }
  355. {
  356. _THREAD_SAFE_METHOD_
  357. nodes_removed_on_group_call_lock--;
  358. if (nodes_removed_on_group_call_lock == 0) {
  359. nodes_removed_on_group_call.clear();
  360. }
  361. }
  362. }
  363. void SceneTree::notify_group(const StringName &p_group, int p_notification) {
  364. notify_group_flags(GROUP_CALL_DEFAULT, p_group, p_notification);
  365. }
  366. void SceneTree::set_group(const StringName &p_group, const String &p_name, const Variant &p_value) {
  367. set_group_flags(GROUP_CALL_DEFAULT, p_group, p_name, p_value);
  368. }
  369. void SceneTree::initialize() {
  370. ERR_FAIL_NULL(root);
  371. MainLoop::initialize();
  372. root->_set_tree(this);
  373. }
  374. bool SceneTree::physics_process(double p_time) {
  375. root_lock++;
  376. current_frame++;
  377. flush_transform_notifications();
  378. if (MainLoop::physics_process(p_time)) {
  379. _quit = true;
  380. }
  381. physics_process_time = p_time;
  382. emit_signal(SNAME("physics_frame"));
  383. call_group(SNAME("_picking_viewports"), SNAME("_process_picking"));
  384. _process(true);
  385. _flush_ugc();
  386. MessageQueue::get_singleton()->flush(); //small little hack
  387. process_timers(p_time, true); //go through timers
  388. process_tweens(p_time, true);
  389. flush_transform_notifications();
  390. root_lock--;
  391. _flush_delete_queue();
  392. _call_idle_callbacks();
  393. return _quit;
  394. }
  395. bool SceneTree::process(double p_time) {
  396. root_lock++;
  397. if (MainLoop::process(p_time)) {
  398. _quit = true;
  399. }
  400. process_time = p_time;
  401. if (multiplayer_poll) {
  402. multiplayer->poll();
  403. for (KeyValue<NodePath, Ref<MultiplayerAPI>> &E : custom_multiplayers) {
  404. E.value->poll();
  405. }
  406. }
  407. emit_signal(SNAME("process_frame"));
  408. MessageQueue::get_singleton()->flush(); //small little hack
  409. flush_transform_notifications();
  410. _process(false);
  411. _flush_ugc();
  412. MessageQueue::get_singleton()->flush(); //small little hack
  413. flush_transform_notifications(); //transforms after world update, to avoid unnecessary enter/exit notifications
  414. root_lock--;
  415. _flush_delete_queue();
  416. if (unlikely(pending_new_scene)) {
  417. _flush_scene_change();
  418. }
  419. process_timers(p_time, false); //go through timers
  420. process_tweens(p_time, false);
  421. flush_transform_notifications(); //additional transforms after timers update
  422. _call_idle_callbacks();
  423. #ifdef TOOLS_ENABLED
  424. #ifndef _3D_DISABLED
  425. if (Engine::get_singleton()->is_editor_hint()) {
  426. //simple hack to reload fallback environment if it changed from editor
  427. String env_path = GLOBAL_GET(SNAME("rendering/environment/defaults/default_environment"));
  428. env_path = env_path.strip_edges(); //user may have added a space or two
  429. String cpath;
  430. Ref<Environment> fallback = get_root()->get_world_3d()->get_fallback_environment();
  431. if (fallback.is_valid()) {
  432. cpath = fallback->get_path();
  433. }
  434. if (cpath != env_path) {
  435. if (!env_path.is_empty()) {
  436. fallback = ResourceLoader::load(env_path);
  437. if (fallback.is_null()) {
  438. //could not load fallback, set as empty
  439. ProjectSettings::get_singleton()->set("rendering/environment/defaults/default_environment", "");
  440. }
  441. } else {
  442. fallback.unref();
  443. }
  444. get_root()->get_world_3d()->set_fallback_environment(fallback);
  445. }
  446. }
  447. #endif // _3D_DISABLED
  448. #endif // TOOLS_ENABLED
  449. return _quit;
  450. }
  451. void SceneTree::process_timers(double p_delta, bool p_physics_frame) {
  452. _THREAD_SAFE_METHOD_
  453. List<Ref<SceneTreeTimer>>::Element *L = timers.back(); //last element
  454. for (List<Ref<SceneTreeTimer>>::Element *E = timers.front(); E;) {
  455. List<Ref<SceneTreeTimer>>::Element *N = E->next();
  456. if ((paused && !E->get()->is_process_always()) || (E->get()->is_process_in_physics() != p_physics_frame)) {
  457. if (E == L) {
  458. break; //break on last, so if new timers were added during list traversal, ignore them.
  459. }
  460. E = N;
  461. continue;
  462. }
  463. double time_left = E->get()->get_time_left();
  464. if (E->get()->is_ignore_time_scale()) {
  465. time_left -= Engine::get_singleton()->get_process_step();
  466. } else {
  467. time_left -= p_delta;
  468. }
  469. E->get()->set_time_left(time_left);
  470. if (time_left <= 0) {
  471. E->get()->emit_signal(SNAME("timeout"));
  472. timers.erase(E);
  473. }
  474. if (E == L) {
  475. break; //break on last, so if new timers were added during list traversal, ignore them.
  476. }
  477. E = N;
  478. }
  479. }
  480. void SceneTree::process_tweens(double p_delta, bool p_physics) {
  481. _THREAD_SAFE_METHOD_
  482. // This methods works similarly to how SceneTreeTimers are handled.
  483. List<Ref<Tween>>::Element *L = tweens.back();
  484. for (List<Ref<Tween>>::Element *E = tweens.front(); E;) {
  485. List<Ref<Tween>>::Element *N = E->next();
  486. // Don't process if paused or process mode doesn't match.
  487. if (!E->get()->can_process(paused) || (p_physics == (E->get()->get_process_mode() == Tween::TWEEN_PROCESS_IDLE))) {
  488. if (E == L) {
  489. break;
  490. }
  491. E = N;
  492. continue;
  493. }
  494. if (!E->get()->step(p_delta)) {
  495. E->get()->clear();
  496. tweens.erase(E);
  497. }
  498. if (E == L) {
  499. break;
  500. }
  501. E = N;
  502. }
  503. }
  504. void SceneTree::finalize() {
  505. _flush_delete_queue();
  506. _flush_ugc();
  507. if (root) {
  508. root->_set_tree(nullptr);
  509. root->_propagate_after_exit_tree();
  510. memdelete(root); //delete root
  511. root = nullptr;
  512. // In case deletion of some objects was queued when destructing the `root`.
  513. // E.g. if `queue_free()` was called for some node outside the tree when handling NOTIFICATION_PREDELETE for some node in the tree.
  514. _flush_delete_queue();
  515. }
  516. MainLoop::finalize();
  517. // Cleanup timers.
  518. for (Ref<SceneTreeTimer> &timer : timers) {
  519. timer->release_connections();
  520. }
  521. timers.clear();
  522. // Cleanup tweens.
  523. for (Ref<Tween> &tween : tweens) {
  524. tween->clear();
  525. }
  526. tweens.clear();
  527. }
  528. void SceneTree::quit(int p_exit_code) {
  529. _THREAD_SAFE_METHOD_
  530. OS::get_singleton()->set_exit_code(p_exit_code);
  531. _quit = true;
  532. }
  533. void SceneTree::_main_window_close() {
  534. if (accept_quit) {
  535. _quit = true;
  536. }
  537. }
  538. void SceneTree::_main_window_go_back() {
  539. if (quit_on_go_back) {
  540. _quit = true;
  541. }
  542. }
  543. void SceneTree::_main_window_focus_in() {
  544. Input *id = Input::get_singleton();
  545. if (id) {
  546. id->ensure_touch_mouse_raised();
  547. }
  548. }
  549. void SceneTree::_notification(int p_notification) {
  550. switch (p_notification) {
  551. case NOTIFICATION_TRANSLATION_CHANGED: {
  552. if (!Engine::get_singleton()->is_editor_hint()) {
  553. get_root()->propagate_notification(p_notification);
  554. }
  555. } break;
  556. case NOTIFICATION_OS_MEMORY_WARNING:
  557. case NOTIFICATION_OS_IME_UPDATE:
  558. case NOTIFICATION_WM_ABOUT:
  559. case NOTIFICATION_CRASH:
  560. case NOTIFICATION_APPLICATION_RESUMED:
  561. case NOTIFICATION_APPLICATION_PAUSED:
  562. case NOTIFICATION_APPLICATION_FOCUS_IN:
  563. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  564. // Pass these to nodes, since they are mirrored.
  565. get_root()->propagate_notification(p_notification);
  566. } break;
  567. }
  568. }
  569. bool SceneTree::is_auto_accept_quit() const {
  570. return accept_quit;
  571. }
  572. void SceneTree::set_auto_accept_quit(bool p_enable) {
  573. accept_quit = p_enable;
  574. }
  575. bool SceneTree::is_quit_on_go_back() const {
  576. return quit_on_go_back;
  577. }
  578. void SceneTree::set_quit_on_go_back(bool p_enable) {
  579. quit_on_go_back = p_enable;
  580. }
  581. #ifdef TOOLS_ENABLED
  582. bool SceneTree::is_node_being_edited(const Node *p_node) const {
  583. return Engine::get_singleton()->is_editor_hint() && edited_scene_root && (edited_scene_root->is_ancestor_of(p_node) || edited_scene_root == p_node);
  584. }
  585. #endif
  586. #ifdef DEBUG_ENABLED
  587. void SceneTree::set_debug_collisions_hint(bool p_enabled) {
  588. debug_collisions_hint = p_enabled;
  589. }
  590. bool SceneTree::is_debugging_collisions_hint() const {
  591. return debug_collisions_hint;
  592. }
  593. void SceneTree::set_debug_paths_hint(bool p_enabled) {
  594. debug_paths_hint = p_enabled;
  595. }
  596. bool SceneTree::is_debugging_paths_hint() const {
  597. return debug_paths_hint;
  598. }
  599. void SceneTree::set_debug_navigation_hint(bool p_enabled) {
  600. debug_navigation_hint = p_enabled;
  601. }
  602. bool SceneTree::is_debugging_navigation_hint() const {
  603. return debug_navigation_hint;
  604. }
  605. #endif
  606. void SceneTree::set_debug_collisions_color(const Color &p_color) {
  607. debug_collisions_color = p_color;
  608. }
  609. Color SceneTree::get_debug_collisions_color() const {
  610. return debug_collisions_color;
  611. }
  612. void SceneTree::set_debug_collision_contact_color(const Color &p_color) {
  613. debug_collision_contact_color = p_color;
  614. }
  615. Color SceneTree::get_debug_collision_contact_color() const {
  616. return debug_collision_contact_color;
  617. }
  618. void SceneTree::set_debug_paths_color(const Color &p_color) {
  619. debug_paths_color = p_color;
  620. }
  621. Color SceneTree::get_debug_paths_color() const {
  622. return debug_paths_color;
  623. }
  624. void SceneTree::set_debug_paths_width(float p_width) {
  625. debug_paths_width = p_width;
  626. }
  627. float SceneTree::get_debug_paths_width() const {
  628. return debug_paths_width;
  629. }
  630. Ref<Material> SceneTree::get_debug_paths_material() {
  631. _THREAD_SAFE_METHOD_
  632. if (debug_paths_material.is_valid()) {
  633. return debug_paths_material;
  634. }
  635. Ref<StandardMaterial3D> _debug_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  636. _debug_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  637. _debug_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  638. _debug_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  639. _debug_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  640. _debug_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  641. _debug_material->set_albedo(get_debug_paths_color());
  642. debug_paths_material = _debug_material;
  643. return debug_paths_material;
  644. }
  645. Ref<Material> SceneTree::get_debug_collision_material() {
  646. _THREAD_SAFE_METHOD_
  647. if (collision_material.is_valid()) {
  648. return collision_material;
  649. }
  650. Ref<StandardMaterial3D> line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  651. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  652. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  653. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  654. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  655. line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  656. line_material->set_albedo(get_debug_collisions_color());
  657. collision_material = line_material;
  658. return collision_material;
  659. }
  660. Ref<ArrayMesh> SceneTree::get_debug_contact_mesh() {
  661. _THREAD_SAFE_METHOD_
  662. if (debug_contact_mesh.is_valid()) {
  663. return debug_contact_mesh;
  664. }
  665. debug_contact_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  666. Ref<StandardMaterial3D> mat = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  667. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  668. mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  669. mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  670. mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  671. mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  672. mat->set_albedo(get_debug_collision_contact_color());
  673. Vector3 diamond[6] = {
  674. Vector3(-1, 0, 0),
  675. Vector3(1, 0, 0),
  676. Vector3(0, -1, 0),
  677. Vector3(0, 1, 0),
  678. Vector3(0, 0, -1),
  679. Vector3(0, 0, 1)
  680. };
  681. /* clang-format off */
  682. int diamond_faces[8 * 3] = {
  683. 0, 2, 4,
  684. 0, 3, 4,
  685. 1, 2, 4,
  686. 1, 3, 4,
  687. 0, 2, 5,
  688. 0, 3, 5,
  689. 1, 2, 5,
  690. 1, 3, 5,
  691. };
  692. /* clang-format on */
  693. Vector<int> indices;
  694. for (int i = 0; i < 8 * 3; i++) {
  695. indices.push_back(diamond_faces[i]);
  696. }
  697. Vector<Vector3> vertices;
  698. for (int i = 0; i < 6; i++) {
  699. vertices.push_back(diamond[i] * 0.1);
  700. }
  701. Array arr;
  702. arr.resize(Mesh::ARRAY_MAX);
  703. arr[Mesh::ARRAY_VERTEX] = vertices;
  704. arr[Mesh::ARRAY_INDEX] = indices;
  705. debug_contact_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
  706. debug_contact_mesh->surface_set_material(0, mat);
  707. return debug_contact_mesh;
  708. }
  709. void SceneTree::set_pause(bool p_enabled) {
  710. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Pause can only be set from the main thread.");
  711. if (p_enabled == paused) {
  712. return;
  713. }
  714. paused = p_enabled;
  715. #ifndef _3D_DISABLED
  716. PhysicsServer3D::get_singleton()->set_active(!p_enabled);
  717. #endif // _3D_DISABLED
  718. PhysicsServer2D::get_singleton()->set_active(!p_enabled);
  719. if (get_root()) {
  720. get_root()->_propagate_pause_notification(p_enabled);
  721. }
  722. }
  723. bool SceneTree::is_paused() const {
  724. return paused;
  725. }
  726. void SceneTree::_process_group(ProcessGroup *p_group, bool p_physics) {
  727. // When reading this function, keep in mind that this code must work in a way where
  728. // if any node is removed, this needs to continue working.
  729. p_group->call_queue.flush(); // Flush messages before processing.
  730. Vector<Node *> &nodes = p_physics ? p_group->physics_nodes : p_group->nodes;
  731. if (nodes.is_empty()) {
  732. return;
  733. }
  734. if (p_physics) {
  735. if (p_group->physics_node_order_dirty) {
  736. nodes.sort_custom<Node::ComparatorWithPhysicsPriority>();
  737. p_group->physics_node_order_dirty = false;
  738. }
  739. } else {
  740. if (p_group->node_order_dirty) {
  741. nodes.sort_custom<Node::ComparatorWithPriority>();
  742. p_group->node_order_dirty = false;
  743. }
  744. }
  745. // Make a copy, so if nodes are added/removed from process, this does not break
  746. Vector<Node *> nodes_copy = nodes;
  747. uint32_t node_count = nodes_copy.size();
  748. Node **nodes_ptr = (Node **)nodes_copy.ptr(); // Force cast, pointer will not change.
  749. for (uint32_t i = 0; i < node_count; i++) {
  750. Node *n = nodes_ptr[i];
  751. if (nodes_removed_on_group_call.has(n)) {
  752. // Node may have been removed during process, skip it.
  753. // Keep in mind removals can only happen on the main thread.
  754. continue;
  755. }
  756. if (!n->can_process() || !n->is_inside_tree()) {
  757. continue;
  758. }
  759. if (p_physics) {
  760. if (n->is_physics_processing_internal()) {
  761. n->notification(Node::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  762. }
  763. if (n->is_physics_processing()) {
  764. n->notification(Node::NOTIFICATION_PHYSICS_PROCESS);
  765. }
  766. } else {
  767. if (n->is_processing_internal()) {
  768. n->notification(Node::NOTIFICATION_INTERNAL_PROCESS);
  769. }
  770. if (n->is_processing()) {
  771. n->notification(Node::NOTIFICATION_PROCESS);
  772. }
  773. }
  774. }
  775. p_group->call_queue.flush(); // Flush messages also after processing (for potential deferred calls).
  776. }
  777. void SceneTree::_process_groups_thread(uint32_t p_index, bool p_physics) {
  778. Node::current_process_thread_group = local_process_group_cache[p_index]->owner;
  779. _process_group(local_process_group_cache[p_index], p_physics);
  780. Node::current_process_thread_group = nullptr;
  781. }
  782. void SceneTree::_process(bool p_physics) {
  783. if (process_groups_dirty) {
  784. {
  785. // First, remove dirty groups.
  786. // This needs to be done when not processing to avoid problems.
  787. ProcessGroup **pg_ptr = (ProcessGroup **)process_groups.ptr(); // discard constness.
  788. uint32_t pg_count = process_groups.size();
  789. for (uint32_t i = 0; i < pg_count; i++) {
  790. if (pg_ptr[i]->removed) {
  791. // Replace removed with last.
  792. pg_ptr[i] = pg_ptr[pg_count - 1];
  793. // Retry
  794. i--;
  795. pg_count--;
  796. }
  797. }
  798. if (pg_count != process_groups.size()) {
  799. process_groups.resize(pg_count);
  800. }
  801. }
  802. {
  803. // Then, re-sort groups.
  804. process_groups.sort_custom<ProcessGroupSort>();
  805. }
  806. process_groups_dirty = false;
  807. }
  808. // Cache the group count, because during processing new groups may be added.
  809. // They will be added at the end, hence for consistency they will be ignored by this process loop.
  810. // No group will be removed from the array during processing (this is done earlier in this function by marking the groups dirty).
  811. uint32_t group_count = process_groups.size();
  812. if (group_count == 0) {
  813. return;
  814. }
  815. process_last_pass++; // Increment pass
  816. uint32_t from = 0;
  817. uint32_t process_count = 0;
  818. nodes_removed_on_group_call_lock++;
  819. int current_order = process_groups[0]->owner ? process_groups[0]->owner->data.process_thread_group_order : 0;
  820. bool current_threaded = process_groups[0]->owner ? process_groups[0]->owner->data.process_thread_group == Node::PROCESS_THREAD_GROUP_SUB_THREAD : false;
  821. for (uint32_t i = 0; i <= group_count; i++) {
  822. int order = i < group_count && process_groups[i]->owner ? process_groups[i]->owner->data.process_thread_group_order : 0;
  823. bool threaded = i < group_count && process_groups[i]->owner ? process_groups[i]->owner->data.process_thread_group == Node::PROCESS_THREAD_GROUP_SUB_THREAD : false;
  824. if (i == group_count || current_order != order || current_threaded != threaded) {
  825. if (process_count > 0) {
  826. // Proceed to process the group.
  827. bool using_threads = process_groups[from]->owner && process_groups[from]->owner->data.process_thread_group == Node::PROCESS_THREAD_GROUP_SUB_THREAD && !node_threading_disabled;
  828. if (using_threads) {
  829. local_process_group_cache.clear();
  830. }
  831. for (uint32_t j = from; j < i; j++) {
  832. if (process_groups[j]->last_pass == process_last_pass) {
  833. if (using_threads) {
  834. local_process_group_cache.push_back(process_groups[j]);
  835. } else {
  836. _process_group(process_groups[j], p_physics);
  837. }
  838. }
  839. }
  840. if (using_threads) {
  841. WorkerThreadPool::GroupID id = WorkerThreadPool::get_singleton()->add_template_group_task(this, &SceneTree::_process_groups_thread, p_physics, local_process_group_cache.size(), -1, true);
  842. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(id);
  843. }
  844. }
  845. if (i == group_count) {
  846. // This one is invalid, no longer process
  847. break;
  848. }
  849. from = i;
  850. current_threaded = threaded;
  851. current_order = order;
  852. }
  853. if (process_groups[i]->removed) {
  854. continue;
  855. }
  856. ProcessGroup *pg = process_groups[i];
  857. // Validate group for processing
  858. bool process_valid = false;
  859. if (p_physics) {
  860. if (!pg->physics_nodes.is_empty()) {
  861. process_valid = true;
  862. } else if ((pg == &default_process_group || (pg->owner != nullptr && pg->owner->data.process_thread_messages.has_flag(Node::FLAG_PROCESS_THREAD_MESSAGES_PHYSICS))) && pg->call_queue.has_messages()) {
  863. process_valid = true;
  864. }
  865. } else {
  866. if (!pg->nodes.is_empty()) {
  867. process_valid = true;
  868. } else if ((pg == &default_process_group || (pg->owner != nullptr && pg->owner->data.process_thread_messages.has_flag(Node::FLAG_PROCESS_THREAD_MESSAGES))) && pg->call_queue.has_messages()) {
  869. process_valid = true;
  870. }
  871. }
  872. if (process_valid) {
  873. pg->last_pass = process_last_pass; // Enable for processing
  874. process_count++;
  875. }
  876. }
  877. nodes_removed_on_group_call_lock--;
  878. if (nodes_removed_on_group_call_lock == 0) {
  879. nodes_removed_on_group_call.clear();
  880. }
  881. }
  882. bool SceneTree::ProcessGroupSort::operator()(const ProcessGroup *p_left, const ProcessGroup *p_right) const {
  883. int left_order = p_left->owner ? p_left->owner->data.process_thread_group_order : 0;
  884. int right_order = p_right->owner ? p_right->owner->data.process_thread_group_order : 0;
  885. if (left_order == right_order) {
  886. int left_threaded = p_left->owner != nullptr && p_left->owner->data.process_thread_group == Node::PROCESS_THREAD_GROUP_SUB_THREAD ? 0 : 1;
  887. int right_threaded = p_right->owner != nullptr && p_right->owner->data.process_thread_group == Node::PROCESS_THREAD_GROUP_SUB_THREAD ? 0 : 1;
  888. return left_threaded < right_threaded;
  889. } else {
  890. return left_order < right_order;
  891. }
  892. }
  893. void SceneTree::_remove_process_group(Node *p_node) {
  894. _THREAD_SAFE_METHOD_
  895. ProcessGroup *pg = (ProcessGroup *)p_node->data.process_group;
  896. ERR_FAIL_NULL(pg);
  897. ERR_FAIL_COND(pg->removed);
  898. pg->removed = true;
  899. pg->owner = nullptr;
  900. p_node->data.process_group = nullptr;
  901. process_groups_dirty = true;
  902. }
  903. void SceneTree::_add_process_group(Node *p_node) {
  904. _THREAD_SAFE_METHOD_
  905. ERR_FAIL_NULL(p_node);
  906. ProcessGroup *pg = memnew(ProcessGroup);
  907. pg->owner = p_node;
  908. p_node->data.process_group = pg;
  909. process_groups.push_back(pg);
  910. process_groups_dirty = true;
  911. }
  912. void SceneTree::_remove_node_from_process_group(Node *p_node, Node *p_owner) {
  913. _THREAD_SAFE_METHOD_
  914. ProcessGroup *pg = p_owner ? (ProcessGroup *)p_owner->data.process_group : &default_process_group;
  915. if (p_node->is_processing() || p_node->is_processing_internal()) {
  916. bool found = pg->nodes.erase(p_node);
  917. ERR_FAIL_COND(!found);
  918. }
  919. if (p_node->is_physics_processing() || p_node->is_physics_processing_internal()) {
  920. bool found = pg->physics_nodes.erase(p_node);
  921. ERR_FAIL_COND(!found);
  922. }
  923. }
  924. void SceneTree::_add_node_to_process_group(Node *p_node, Node *p_owner) {
  925. _THREAD_SAFE_METHOD_
  926. ProcessGroup *pg = p_owner ? (ProcessGroup *)p_owner->data.process_group : &default_process_group;
  927. if (p_node->is_processing() || p_node->is_processing_internal()) {
  928. pg->nodes.push_back(p_node);
  929. pg->node_order_dirty = true;
  930. }
  931. if (p_node->is_physics_processing() || p_node->is_physics_processing_internal()) {
  932. pg->physics_nodes.push_back(p_node);
  933. pg->physics_node_order_dirty = true;
  934. }
  935. }
  936. void SceneTree::_call_input_pause(const StringName &p_group, CallInputType p_call_type, const Ref<InputEvent> &p_input, Viewport *p_viewport) {
  937. Vector<Node *> nodes_copy;
  938. {
  939. _THREAD_SAFE_METHOD_
  940. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  941. if (!E) {
  942. return;
  943. }
  944. Group &g = E->value;
  945. if (g.nodes.is_empty()) {
  946. return;
  947. }
  948. _update_group_order(g);
  949. //copy, so copy on write happens in case something is removed from process while being called
  950. //performance is not lost because only if something is added/removed the vector is copied.
  951. nodes_copy = g.nodes;
  952. }
  953. int gr_node_count = nodes_copy.size();
  954. Node **gr_nodes = nodes_copy.ptrw();
  955. {
  956. _THREAD_SAFE_METHOD_
  957. nodes_removed_on_group_call_lock++;
  958. }
  959. Vector<ObjectID> no_context_node_ids; // Nodes may be deleted due to this shortcut input.
  960. for (int i = gr_node_count - 1; i >= 0; i--) {
  961. if (p_viewport->is_input_handled()) {
  962. break;
  963. }
  964. Node *n = gr_nodes[i];
  965. if (nodes_removed_on_group_call.has(n)) {
  966. continue;
  967. }
  968. if (!n->can_process()) {
  969. continue;
  970. }
  971. switch (p_call_type) {
  972. case CALL_INPUT_TYPE_INPUT:
  973. n->_call_input(p_input);
  974. break;
  975. case CALL_INPUT_TYPE_SHORTCUT_INPUT: {
  976. const Control *c = Object::cast_to<Control>(n);
  977. if (c) {
  978. // If calling shortcut input on a control, ensure it respects the shortcut context.
  979. // Shortcut context (based on focus) only makes sense for controls (UI), so don't need to worry about it for nodes
  980. if (c->get_shortcut_context() == nullptr) {
  981. no_context_node_ids.append(n->get_instance_id());
  982. continue;
  983. }
  984. if (!c->is_focus_owner_in_shortcut_context()) {
  985. continue;
  986. }
  987. }
  988. n->_call_shortcut_input(p_input);
  989. break;
  990. }
  991. case CALL_INPUT_TYPE_UNHANDLED_INPUT:
  992. n->_call_unhandled_input(p_input);
  993. break;
  994. case CALL_INPUT_TYPE_UNHANDLED_KEY_INPUT:
  995. n->_call_unhandled_key_input(p_input);
  996. break;
  997. }
  998. }
  999. for (const ObjectID &id : no_context_node_ids) {
  1000. if (p_viewport->is_input_handled()) {
  1001. break;
  1002. }
  1003. Node *n = Object::cast_to<Node>(ObjectDB::get_instance(id));
  1004. if (n) {
  1005. n->_call_shortcut_input(p_input);
  1006. }
  1007. }
  1008. {
  1009. _THREAD_SAFE_METHOD_
  1010. nodes_removed_on_group_call_lock--;
  1011. if (nodes_removed_on_group_call_lock == 0) {
  1012. nodes_removed_on_group_call.clear();
  1013. }
  1014. }
  1015. }
  1016. void SceneTree::_call_group_flags(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1017. r_error.error = Callable::CallError::CALL_OK;
  1018. ERR_FAIL_COND(p_argcount < 3);
  1019. ERR_FAIL_COND(!p_args[0]->is_num());
  1020. ERR_FAIL_COND(p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING);
  1021. ERR_FAIL_COND(p_args[2]->get_type() != Variant::STRING_NAME && p_args[2]->get_type() != Variant::STRING);
  1022. int flags = *p_args[0];
  1023. StringName group = *p_args[1];
  1024. StringName method = *p_args[2];
  1025. call_group_flagsp(flags, group, method, p_args + 3, p_argcount - 3);
  1026. }
  1027. void SceneTree::_call_group(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1028. r_error.error = Callable::CallError::CALL_OK;
  1029. ERR_FAIL_COND(p_argcount < 2);
  1030. ERR_FAIL_COND(p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING);
  1031. ERR_FAIL_COND(p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING);
  1032. StringName group = *p_args[0];
  1033. StringName method = *p_args[1];
  1034. call_group_flagsp(GROUP_CALL_DEFAULT, group, method, p_args + 2, p_argcount - 2);
  1035. }
  1036. int64_t SceneTree::get_frame() const {
  1037. return current_frame;
  1038. }
  1039. TypedArray<Node> SceneTree::_get_nodes_in_group(const StringName &p_group) {
  1040. _THREAD_SAFE_METHOD_
  1041. TypedArray<Node> ret;
  1042. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  1043. if (!E) {
  1044. return ret;
  1045. }
  1046. _update_group_order(E->value); //update order just in case
  1047. int nc = E->value.nodes.size();
  1048. if (nc == 0) {
  1049. return ret;
  1050. }
  1051. ret.resize(nc);
  1052. Node **ptr = E->value.nodes.ptrw();
  1053. for (int i = 0; i < nc; i++) {
  1054. ret[i] = ptr[i];
  1055. }
  1056. return ret;
  1057. }
  1058. bool SceneTree::has_group(const StringName &p_identifier) const {
  1059. _THREAD_SAFE_METHOD_
  1060. return group_map.has(p_identifier);
  1061. }
  1062. int SceneTree::get_node_count_in_group(const StringName &p_group) const {
  1063. _THREAD_SAFE_METHOD_
  1064. HashMap<StringName, Group>::ConstIterator E = group_map.find(p_group);
  1065. if (!E) {
  1066. return 0;
  1067. }
  1068. return E->value.nodes.size();
  1069. }
  1070. Node *SceneTree::get_first_node_in_group(const StringName &p_group) {
  1071. _THREAD_SAFE_METHOD_
  1072. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  1073. if (!E) {
  1074. return nullptr; // No group.
  1075. }
  1076. _update_group_order(E->value); // Update order just in case.
  1077. if (E->value.nodes.is_empty()) {
  1078. return nullptr;
  1079. }
  1080. return E->value.nodes[0];
  1081. }
  1082. void SceneTree::get_nodes_in_group(const StringName &p_group, List<Node *> *p_list) {
  1083. _THREAD_SAFE_METHOD_
  1084. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  1085. if (!E) {
  1086. return;
  1087. }
  1088. _update_group_order(E->value); //update order just in case
  1089. int nc = E->value.nodes.size();
  1090. if (nc == 0) {
  1091. return;
  1092. }
  1093. Node **ptr = E->value.nodes.ptrw();
  1094. for (int i = 0; i < nc; i++) {
  1095. p_list->push_back(ptr[i]);
  1096. }
  1097. }
  1098. void SceneTree::_flush_delete_queue() {
  1099. _THREAD_SAFE_METHOD_
  1100. while (delete_queue.size()) {
  1101. Object *obj = ObjectDB::get_instance(delete_queue.front()->get());
  1102. if (obj) {
  1103. memdelete(obj);
  1104. }
  1105. delete_queue.pop_front();
  1106. }
  1107. }
  1108. void SceneTree::queue_delete(Object *p_object) {
  1109. _THREAD_SAFE_METHOD_
  1110. ERR_FAIL_NULL(p_object);
  1111. p_object->_is_queued_for_deletion = true;
  1112. delete_queue.push_back(p_object->get_instance_id());
  1113. }
  1114. int SceneTree::get_node_count() const {
  1115. return nodes_in_tree_count;
  1116. }
  1117. void SceneTree::set_edited_scene_root(Node *p_node) {
  1118. #ifdef TOOLS_ENABLED
  1119. edited_scene_root = p_node;
  1120. #endif
  1121. }
  1122. Node *SceneTree::get_edited_scene_root() const {
  1123. #ifdef TOOLS_ENABLED
  1124. return edited_scene_root;
  1125. #else
  1126. return nullptr;
  1127. #endif
  1128. }
  1129. void SceneTree::set_current_scene(Node *p_scene) {
  1130. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Changing scene can only be done from the main thread.");
  1131. ERR_FAIL_COND(p_scene && p_scene->get_parent() != root);
  1132. current_scene = p_scene;
  1133. }
  1134. Node *SceneTree::get_current_scene() const {
  1135. return current_scene;
  1136. }
  1137. void SceneTree::_flush_scene_change() {
  1138. if (prev_scene) {
  1139. memdelete(prev_scene);
  1140. prev_scene = nullptr;
  1141. }
  1142. current_scene = pending_new_scene;
  1143. root->add_child(pending_new_scene);
  1144. pending_new_scene = nullptr;
  1145. // Update display for cursor instantly.
  1146. root->update_mouse_cursor_state();
  1147. }
  1148. Error SceneTree::change_scene_to_file(const String &p_path) {
  1149. ERR_FAIL_COND_V_MSG(!Thread::is_main_thread(), ERR_INVALID_PARAMETER, "Changing scene can only be done from the main thread.");
  1150. Ref<PackedScene> new_scene = ResourceLoader::load(p_path);
  1151. if (new_scene.is_null()) {
  1152. return ERR_CANT_OPEN;
  1153. }
  1154. return change_scene_to_packed(new_scene);
  1155. }
  1156. Error SceneTree::change_scene_to_packed(const Ref<PackedScene> &p_scene) {
  1157. ERR_FAIL_COND_V_MSG(p_scene.is_null(), ERR_INVALID_PARAMETER, "Can't change to a null scene. Use unload_current_scene() if you wish to unload it.");
  1158. Node *new_scene = p_scene->instantiate();
  1159. ERR_FAIL_NULL_V(new_scene, ERR_CANT_CREATE);
  1160. // If called again while a change is pending.
  1161. if (pending_new_scene) {
  1162. queue_delete(pending_new_scene);
  1163. pending_new_scene = nullptr;
  1164. }
  1165. prev_scene = current_scene;
  1166. if (current_scene) {
  1167. // Let as many side effects as possible happen or be queued now,
  1168. // so they are run before the scene is actually deleted.
  1169. root->remove_child(current_scene);
  1170. }
  1171. DEV_ASSERT(!current_scene);
  1172. pending_new_scene = new_scene;
  1173. return OK;
  1174. }
  1175. Error SceneTree::reload_current_scene() {
  1176. ERR_FAIL_COND_V_MSG(!Thread::is_main_thread(), ERR_INVALID_PARAMETER, "Reloading scene can only be done from the main thread.");
  1177. ERR_FAIL_NULL_V(current_scene, ERR_UNCONFIGURED);
  1178. String fname = current_scene->get_scene_file_path();
  1179. return change_scene_to_file(fname);
  1180. }
  1181. void SceneTree::unload_current_scene() {
  1182. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Unloading the current scene can only be done from the main thread.");
  1183. if (current_scene) {
  1184. memdelete(current_scene);
  1185. current_scene = nullptr;
  1186. }
  1187. }
  1188. void SceneTree::add_current_scene(Node *p_current) {
  1189. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Adding a current scene can only be done from the main thread.");
  1190. current_scene = p_current;
  1191. root->add_child(p_current);
  1192. }
  1193. Ref<SceneTreeTimer> SceneTree::create_timer(double p_delay_sec, bool p_process_always, bool p_process_in_physics, bool p_ignore_time_scale) {
  1194. _THREAD_SAFE_METHOD_
  1195. Ref<SceneTreeTimer> stt;
  1196. stt.instantiate();
  1197. stt->set_process_always(p_process_always);
  1198. stt->set_time_left(p_delay_sec);
  1199. stt->set_process_in_physics(p_process_in_physics);
  1200. stt->set_ignore_time_scale(p_ignore_time_scale);
  1201. timers.push_back(stt);
  1202. return stt;
  1203. }
  1204. Ref<Tween> SceneTree::create_tween() {
  1205. _THREAD_SAFE_METHOD_
  1206. Ref<Tween> tween = memnew(Tween(true));
  1207. tweens.push_back(tween);
  1208. return tween;
  1209. }
  1210. TypedArray<Tween> SceneTree::get_processed_tweens() {
  1211. _THREAD_SAFE_METHOD_
  1212. TypedArray<Tween> ret;
  1213. ret.resize(tweens.size());
  1214. int i = 0;
  1215. for (const Ref<Tween> &tween : tweens) {
  1216. ret[i] = tween;
  1217. i++;
  1218. }
  1219. return ret;
  1220. }
  1221. Ref<MultiplayerAPI> SceneTree::get_multiplayer(const NodePath &p_for_path) const {
  1222. ERR_FAIL_COND_V_MSG(!Thread::is_main_thread(), Ref<MultiplayerAPI>(), "Multiplayer can only be manipulated from the main thread.");
  1223. if (p_for_path.is_empty()) {
  1224. return multiplayer;
  1225. }
  1226. const Vector<StringName> tnames = p_for_path.get_names();
  1227. const StringName *nptr = tnames.ptr();
  1228. for (const KeyValue<NodePath, Ref<MultiplayerAPI>> &E : custom_multiplayers) {
  1229. const Vector<StringName> snames = E.key.get_names();
  1230. if (tnames.size() < snames.size()) {
  1231. continue;
  1232. }
  1233. const StringName *sptr = snames.ptr();
  1234. bool valid = true;
  1235. for (int i = 0; i < snames.size(); i++) {
  1236. if (sptr[i] != nptr[i]) {
  1237. valid = false;
  1238. break;
  1239. }
  1240. }
  1241. if (valid) {
  1242. return E.value;
  1243. }
  1244. }
  1245. return multiplayer;
  1246. }
  1247. void SceneTree::set_multiplayer(Ref<MultiplayerAPI> p_multiplayer, const NodePath &p_root_path) {
  1248. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Multiplayer can only be manipulated from the main thread.");
  1249. if (p_root_path.is_empty()) {
  1250. ERR_FAIL_COND(!p_multiplayer.is_valid());
  1251. if (multiplayer.is_valid()) {
  1252. multiplayer->object_configuration_remove(nullptr, NodePath("/" + root->get_name()));
  1253. }
  1254. multiplayer = p_multiplayer;
  1255. multiplayer->object_configuration_add(nullptr, NodePath("/" + root->get_name()));
  1256. } else {
  1257. if (custom_multiplayers.has(p_root_path)) {
  1258. custom_multiplayers[p_root_path]->object_configuration_remove(nullptr, p_root_path);
  1259. } else if (p_multiplayer.is_valid()) {
  1260. const Vector<StringName> tnames = p_root_path.get_names();
  1261. const StringName *nptr = tnames.ptr();
  1262. for (const KeyValue<NodePath, Ref<MultiplayerAPI>> &E : custom_multiplayers) {
  1263. const Vector<StringName> snames = E.key.get_names();
  1264. if (tnames.size() < snames.size()) {
  1265. continue;
  1266. }
  1267. const StringName *sptr = snames.ptr();
  1268. bool valid = true;
  1269. for (int i = 0; i < snames.size(); i++) {
  1270. if (sptr[i] != nptr[i]) {
  1271. valid = false;
  1272. break;
  1273. }
  1274. }
  1275. ERR_FAIL_COND_MSG(valid, "Multiplayer is already configured for a parent of this path: '" + p_root_path + "' in '" + E.key + "'.");
  1276. }
  1277. }
  1278. if (p_multiplayer.is_valid()) {
  1279. custom_multiplayers[p_root_path] = p_multiplayer;
  1280. p_multiplayer->object_configuration_add(nullptr, p_root_path);
  1281. } else {
  1282. custom_multiplayers.erase(p_root_path);
  1283. }
  1284. }
  1285. }
  1286. void SceneTree::set_multiplayer_poll_enabled(bool p_enabled) {
  1287. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Multiplayer can only be manipulated from the main thread.");
  1288. multiplayer_poll = p_enabled;
  1289. }
  1290. bool SceneTree::is_multiplayer_poll_enabled() const {
  1291. return multiplayer_poll;
  1292. }
  1293. void SceneTree::_bind_methods() {
  1294. ClassDB::bind_method(D_METHOD("get_root"), &SceneTree::get_root);
  1295. ClassDB::bind_method(D_METHOD("has_group", "name"), &SceneTree::has_group);
  1296. ClassDB::bind_method(D_METHOD("is_auto_accept_quit"), &SceneTree::is_auto_accept_quit);
  1297. ClassDB::bind_method(D_METHOD("set_auto_accept_quit", "enabled"), &SceneTree::set_auto_accept_quit);
  1298. ClassDB::bind_method(D_METHOD("is_quit_on_go_back"), &SceneTree::is_quit_on_go_back);
  1299. ClassDB::bind_method(D_METHOD("set_quit_on_go_back", "enabled"), &SceneTree::set_quit_on_go_back);
  1300. ClassDB::bind_method(D_METHOD("set_debug_collisions_hint", "enable"), &SceneTree::set_debug_collisions_hint);
  1301. ClassDB::bind_method(D_METHOD("is_debugging_collisions_hint"), &SceneTree::is_debugging_collisions_hint);
  1302. ClassDB::bind_method(D_METHOD("set_debug_paths_hint", "enable"), &SceneTree::set_debug_paths_hint);
  1303. ClassDB::bind_method(D_METHOD("is_debugging_paths_hint"), &SceneTree::is_debugging_paths_hint);
  1304. ClassDB::bind_method(D_METHOD("set_debug_navigation_hint", "enable"), &SceneTree::set_debug_navigation_hint);
  1305. ClassDB::bind_method(D_METHOD("is_debugging_navigation_hint"), &SceneTree::is_debugging_navigation_hint);
  1306. ClassDB::bind_method(D_METHOD("set_edited_scene_root", "scene"), &SceneTree::set_edited_scene_root);
  1307. ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &SceneTree::get_edited_scene_root);
  1308. ClassDB::bind_method(D_METHOD("set_pause", "enable"), &SceneTree::set_pause);
  1309. ClassDB::bind_method(D_METHOD("is_paused"), &SceneTree::is_paused);
  1310. ClassDB::bind_method(D_METHOD("create_timer", "time_sec", "process_always", "process_in_physics", "ignore_time_scale"), &SceneTree::create_timer, DEFVAL(true), DEFVAL(false), DEFVAL(false));
  1311. ClassDB::bind_method(D_METHOD("create_tween"), &SceneTree::create_tween);
  1312. ClassDB::bind_method(D_METHOD("get_processed_tweens"), &SceneTree::get_processed_tweens);
  1313. ClassDB::bind_method(D_METHOD("get_node_count"), &SceneTree::get_node_count);
  1314. ClassDB::bind_method(D_METHOD("get_frame"), &SceneTree::get_frame);
  1315. ClassDB::bind_method(D_METHOD("quit", "exit_code"), &SceneTree::quit, DEFVAL(EXIT_SUCCESS));
  1316. ClassDB::bind_method(D_METHOD("queue_delete", "obj"), &SceneTree::queue_delete);
  1317. MethodInfo mi;
  1318. mi.name = "call_group_flags";
  1319. mi.arguments.push_back(PropertyInfo(Variant::INT, "flags"));
  1320. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "group"));
  1321. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1322. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group_flags", &SceneTree::_call_group_flags, mi);
  1323. ClassDB::bind_method(D_METHOD("notify_group_flags", "call_flags", "group", "notification"), &SceneTree::notify_group_flags);
  1324. ClassDB::bind_method(D_METHOD("set_group_flags", "call_flags", "group", "property", "value"), &SceneTree::set_group_flags);
  1325. MethodInfo mi2;
  1326. mi2.name = "call_group";
  1327. mi2.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "group"));
  1328. mi2.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1329. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group", &SceneTree::_call_group, mi2);
  1330. ClassDB::bind_method(D_METHOD("notify_group", "group", "notification"), &SceneTree::notify_group);
  1331. ClassDB::bind_method(D_METHOD("set_group", "group", "property", "value"), &SceneTree::set_group);
  1332. ClassDB::bind_method(D_METHOD("get_nodes_in_group", "group"), &SceneTree::_get_nodes_in_group);
  1333. ClassDB::bind_method(D_METHOD("get_first_node_in_group", "group"), &SceneTree::get_first_node_in_group);
  1334. ClassDB::bind_method(D_METHOD("get_node_count_in_group", "group"), &SceneTree::get_node_count_in_group);
  1335. ClassDB::bind_method(D_METHOD("set_current_scene", "child_node"), &SceneTree::set_current_scene);
  1336. ClassDB::bind_method(D_METHOD("get_current_scene"), &SceneTree::get_current_scene);
  1337. ClassDB::bind_method(D_METHOD("change_scene_to_file", "path"), &SceneTree::change_scene_to_file);
  1338. ClassDB::bind_method(D_METHOD("change_scene_to_packed", "packed_scene"), &SceneTree::change_scene_to_packed);
  1339. ClassDB::bind_method(D_METHOD("reload_current_scene"), &SceneTree::reload_current_scene);
  1340. ClassDB::bind_method(D_METHOD("unload_current_scene"), &SceneTree::unload_current_scene);
  1341. ClassDB::bind_method(D_METHOD("set_multiplayer", "multiplayer", "root_path"), &SceneTree::set_multiplayer, DEFVAL(NodePath()));
  1342. ClassDB::bind_method(D_METHOD("get_multiplayer", "for_path"), &SceneTree::get_multiplayer, DEFVAL(NodePath()));
  1343. ClassDB::bind_method(D_METHOD("set_multiplayer_poll_enabled", "enabled"), &SceneTree::set_multiplayer_poll_enabled);
  1344. ClassDB::bind_method(D_METHOD("is_multiplayer_poll_enabled"), &SceneTree::is_multiplayer_poll_enabled);
  1345. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_accept_quit"), "set_auto_accept_quit", "is_auto_accept_quit");
  1346. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "quit_on_go_back"), "set_quit_on_go_back", "is_quit_on_go_back");
  1347. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_collisions_hint"), "set_debug_collisions_hint", "is_debugging_collisions_hint");
  1348. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_paths_hint"), "set_debug_paths_hint", "is_debugging_paths_hint");
  1349. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_navigation_hint"), "set_debug_navigation_hint", "is_debugging_navigation_hint");
  1350. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_pause", "is_paused");
  1351. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edited_scene_root", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_edited_scene_root", "get_edited_scene_root");
  1352. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "current_scene", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_current_scene", "get_current_scene");
  1353. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "", "get_root");
  1354. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multiplayer_poll"), "set_multiplayer_poll_enabled", "is_multiplayer_poll_enabled");
  1355. ADD_SIGNAL(MethodInfo("tree_changed"));
  1356. ADD_SIGNAL(MethodInfo("tree_process_mode_changed")); //editor only signal, but due to API hash it can't be removed in run-time
  1357. ADD_SIGNAL(MethodInfo("node_added", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1358. ADD_SIGNAL(MethodInfo("node_removed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1359. ADD_SIGNAL(MethodInfo("node_renamed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1360. ADD_SIGNAL(MethodInfo("node_configuration_warning_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1361. ADD_SIGNAL(MethodInfo("process_frame"));
  1362. ADD_SIGNAL(MethodInfo("physics_frame"));
  1363. BIND_ENUM_CONSTANT(GROUP_CALL_DEFAULT);
  1364. BIND_ENUM_CONSTANT(GROUP_CALL_REVERSE);
  1365. BIND_ENUM_CONSTANT(GROUP_CALL_DEFERRED);
  1366. BIND_ENUM_CONSTANT(GROUP_CALL_UNIQUE);
  1367. }
  1368. SceneTree *SceneTree::singleton = nullptr;
  1369. SceneTree::IdleCallback SceneTree::idle_callbacks[SceneTree::MAX_IDLE_CALLBACKS];
  1370. int SceneTree::idle_callback_count = 0;
  1371. void SceneTree::_call_idle_callbacks() {
  1372. for (int i = 0; i < idle_callback_count; i++) {
  1373. idle_callbacks[i]();
  1374. }
  1375. }
  1376. void SceneTree::add_idle_callback(IdleCallback p_callback) {
  1377. ERR_FAIL_COND(idle_callback_count >= MAX_IDLE_CALLBACKS);
  1378. idle_callbacks[idle_callback_count++] = p_callback;
  1379. }
  1380. void SceneTree::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1381. if (p_function == "change_scene_to_file") {
  1382. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1383. List<String> directories;
  1384. directories.push_back(dir_access->get_current_dir());
  1385. while (!directories.is_empty()) {
  1386. dir_access->change_dir(directories.back()->get());
  1387. directories.pop_back();
  1388. dir_access->list_dir_begin();
  1389. String filename = dir_access->get_next();
  1390. while (!filename.is_empty()) {
  1391. if (filename == "." || filename == "..") {
  1392. filename = dir_access->get_next();
  1393. continue;
  1394. }
  1395. if (dir_access->dir_exists(filename)) {
  1396. directories.push_back(dir_access->get_current_dir().path_join(filename));
  1397. } else if (filename.ends_with(".tscn") || filename.ends_with(".scn")) {
  1398. r_options->push_back("\"" + dir_access->get_current_dir().path_join(filename) + "\"");
  1399. }
  1400. filename = dir_access->get_next();
  1401. }
  1402. }
  1403. } else {
  1404. bool add_options = false;
  1405. if (p_idx == 0) {
  1406. add_options = p_function == "get_nodes_in_group" || p_function == "has_group" || p_function == "get_first_node_in_group" || p_function == "set_group" || p_function == "notify_group" || p_function == "call_group" || p_function == "add_to_group";
  1407. } else if (p_idx == 1) {
  1408. add_options = p_function == "set_group_flags" || p_function == "call_group_flags" || p_function == "notify_group_flags";
  1409. }
  1410. if (add_options) {
  1411. HashMap<StringName, String> global_groups = ProjectSettings::get_singleton()->get_global_groups_list();
  1412. for (const KeyValue<StringName, String> &E : global_groups) {
  1413. r_options->push_back(E.key.operator String().quote());
  1414. }
  1415. }
  1416. }
  1417. MainLoop::get_argument_options(p_function, p_idx, r_options);
  1418. }
  1419. void SceneTree::set_disable_node_threading(bool p_disable) {
  1420. node_threading_disabled = p_disable;
  1421. }
  1422. SceneTree::SceneTree() {
  1423. if (singleton == nullptr) {
  1424. singleton = this;
  1425. }
  1426. debug_collisions_color = GLOBAL_DEF("debug/shapes/collision/shape_color", Color(0.0, 0.6, 0.7, 0.42));
  1427. debug_collision_contact_color = GLOBAL_DEF("debug/shapes/collision/contact_color", Color(1.0, 0.2, 0.1, 0.8));
  1428. debug_paths_color = GLOBAL_DEF("debug/shapes/paths/geometry_color", Color(0.1, 1.0, 0.7, 0.4));
  1429. debug_paths_width = GLOBAL_DEF("debug/shapes/paths/geometry_width", 2.0);
  1430. collision_debug_contacts = GLOBAL_DEF(PropertyInfo(Variant::INT, "debug/shapes/collision/max_contacts_displayed", PROPERTY_HINT_RANGE, "0,20000,1"), 10000);
  1431. GLOBAL_DEF("debug/shapes/collision/draw_2d_outlines", true);
  1432. process_group_call_queue_allocator = memnew(CallQueue::Allocator(64));
  1433. Math::randomize();
  1434. // Create with mainloop.
  1435. root = memnew(Window);
  1436. root->set_min_size(Size2i(64, 64)); // Define a very small minimum window size to prevent bugs such as GH-37242.
  1437. root->set_process_mode(Node::PROCESS_MODE_PAUSABLE);
  1438. root->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_ALWAYS);
  1439. root->set_name("root");
  1440. root->set_title(GLOBAL_GET("application/config/name"));
  1441. if (Engine::get_singleton()->is_editor_hint()) {
  1442. root->set_wrap_controls(true);
  1443. }
  1444. #ifndef _3D_DISABLED
  1445. if (!root->get_world_3d().is_valid()) {
  1446. root->set_world_3d(Ref<World3D>(memnew(World3D)));
  1447. }
  1448. root->set_as_audio_listener_3d(true);
  1449. #endif // _3D_DISABLED
  1450. // Initialize network state.
  1451. set_multiplayer(MultiplayerAPI::create_default_interface());
  1452. root->set_as_audio_listener_2d(true);
  1453. current_scene = nullptr;
  1454. const int msaa_mode_2d = GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "rendering/anti_aliasing/quality/msaa_2d", PROPERTY_HINT_ENUM, String::utf8("Disabled (Fastest),2× (Average),4× (Slow),8× (Slowest)")), 0);
  1455. root->set_msaa_2d(Viewport::MSAA(msaa_mode_2d));
  1456. const int msaa_mode_3d = GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "rendering/anti_aliasing/quality/msaa_3d", PROPERTY_HINT_ENUM, String::utf8("Disabled (Fastest),2× (Average),4× (Slow),8× (Slowest)")), 0);
  1457. root->set_msaa_3d(Viewport::MSAA(msaa_mode_3d));
  1458. const bool transparent_background = GLOBAL_DEF("rendering/viewport/transparent_background", false);
  1459. root->set_transparent_background(transparent_background);
  1460. const bool use_hdr_2d = GLOBAL_DEF_RST_BASIC("rendering/viewport/hdr_2d", false);
  1461. root->set_use_hdr_2d(use_hdr_2d);
  1462. const int ssaa_mode = GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "rendering/anti_aliasing/quality/screen_space_aa", PROPERTY_HINT_ENUM, "Disabled (Fastest),FXAA (Fast)"), 0);
  1463. root->set_screen_space_aa(Viewport::ScreenSpaceAA(ssaa_mode));
  1464. const bool use_taa = GLOBAL_DEF_BASIC("rendering/anti_aliasing/quality/use_taa", false);
  1465. root->set_use_taa(use_taa);
  1466. const bool use_debanding = GLOBAL_DEF("rendering/anti_aliasing/quality/use_debanding", false);
  1467. root->set_use_debanding(use_debanding);
  1468. const bool use_occlusion_culling = GLOBAL_DEF("rendering/occlusion_culling/use_occlusion_culling", false);
  1469. root->set_use_occlusion_culling(use_occlusion_culling);
  1470. float mesh_lod_threshold = GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "rendering/mesh_lod/lod_change/threshold_pixels", PROPERTY_HINT_RANGE, "0,1024,0.1"), 1.0);
  1471. root->set_mesh_lod_threshold(mesh_lod_threshold);
  1472. bool snap_2d_transforms = GLOBAL_DEF_BASIC("rendering/2d/snap/snap_2d_transforms_to_pixel", false);
  1473. root->set_snap_2d_transforms_to_pixel(snap_2d_transforms);
  1474. bool snap_2d_vertices = GLOBAL_DEF_BASIC("rendering/2d/snap/snap_2d_vertices_to_pixel", false);
  1475. root->set_snap_2d_vertices_to_pixel(snap_2d_vertices);
  1476. // We setup VRS for the main viewport here, in the editor this will have little effect.
  1477. const int vrs_mode = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/vrs/mode", PROPERTY_HINT_ENUM, String::utf8("Disabled,Texture,XR")), 0);
  1478. root->set_vrs_mode(Viewport::VRSMode(vrs_mode));
  1479. const String vrs_texture_path = String(GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/vrs/texture", PROPERTY_HINT_FILE, "*.bmp,*.png,*.tga,*.webp"), String())).strip_edges();
  1480. if (vrs_mode == 1 && !vrs_texture_path.is_empty()) {
  1481. Ref<Image> vrs_image;
  1482. vrs_image.instantiate();
  1483. Error load_err = ImageLoader::load_image(vrs_texture_path, vrs_image);
  1484. if (load_err) {
  1485. ERR_PRINT("Non-existing or invalid VRS texture at '" + vrs_texture_path + "'.");
  1486. } else {
  1487. Ref<ImageTexture> vrs_texture;
  1488. vrs_texture.instantiate();
  1489. vrs_texture->create_from_image(vrs_image);
  1490. root->set_vrs_texture(vrs_texture);
  1491. }
  1492. }
  1493. int shadowmap_size = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_size", PROPERTY_HINT_RANGE, "256,16384"), 4096);
  1494. GLOBAL_DEF("rendering/lights_and_shadows/positional_shadow/atlas_size.mobile", 2048);
  1495. bool shadowmap_16_bits = GLOBAL_DEF("rendering/lights_and_shadows/positional_shadow/atlas_16_bits", true);
  1496. int atlas_q0 = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_quadrant_0_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), 2);
  1497. int atlas_q1 = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_quadrant_1_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), 2);
  1498. int atlas_q2 = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_quadrant_2_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), 3);
  1499. int atlas_q3 = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_quadrant_3_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), 4);
  1500. root->set_positional_shadow_atlas_size(shadowmap_size);
  1501. root->set_positional_shadow_atlas_16_bits(shadowmap_16_bits);
  1502. root->set_positional_shadow_atlas_quadrant_subdiv(0, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q0));
  1503. root->set_positional_shadow_atlas_quadrant_subdiv(1, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q1));
  1504. root->set_positional_shadow_atlas_quadrant_subdiv(2, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q2));
  1505. root->set_positional_shadow_atlas_quadrant_subdiv(3, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q3));
  1506. Viewport::SDFOversize sdf_oversize = Viewport::SDFOversize(int(GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/2d/sdf/oversize", PROPERTY_HINT_ENUM, "100%,120%,150%,200%"), 1)));
  1507. root->set_sdf_oversize(sdf_oversize);
  1508. Viewport::SDFScale sdf_scale = Viewport::SDFScale(int(GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/2d/sdf/scale", PROPERTY_HINT_ENUM, "100%,50%,25%"), 1)));
  1509. root->set_sdf_scale(sdf_scale);
  1510. #ifndef _3D_DISABLED
  1511. { // Load default fallback environment.
  1512. // Get possible extensions.
  1513. List<String> exts;
  1514. ResourceLoader::get_recognized_extensions_for_type("Environment", &exts);
  1515. String ext_hint;
  1516. for (const String &E : exts) {
  1517. if (!ext_hint.is_empty()) {
  1518. ext_hint += ",";
  1519. }
  1520. ext_hint += "*." + E;
  1521. }
  1522. // Get path.
  1523. String env_path = GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/environment/defaults/default_environment", PROPERTY_HINT_FILE, ext_hint), "");
  1524. // Setup property.
  1525. env_path = env_path.strip_edges();
  1526. if (!env_path.is_empty()) {
  1527. Ref<Environment> env = ResourceLoader::load(env_path);
  1528. if (env.is_valid()) {
  1529. root->get_world_3d()->set_fallback_environment(env);
  1530. } else {
  1531. if (Engine::get_singleton()->is_editor_hint()) {
  1532. // File was erased, clear the field.
  1533. ProjectSettings::get_singleton()->set("rendering/environment/defaults/default_environment", "");
  1534. } else {
  1535. // File was erased, notify user.
  1536. ERR_PRINT("Default Environment as specified in the project setting \"rendering/environment/defaults/default_environment\" could not be loaded.");
  1537. }
  1538. }
  1539. }
  1540. }
  1541. #endif // _3D_DISABLED
  1542. root->set_physics_object_picking(GLOBAL_DEF("physics/common/enable_object_picking", true));
  1543. root->connect("close_requested", callable_mp(this, &SceneTree::_main_window_close));
  1544. root->connect("go_back_requested", callable_mp(this, &SceneTree::_main_window_go_back));
  1545. root->connect("focus_entered", callable_mp(this, &SceneTree::_main_window_focus_in));
  1546. #ifdef TOOLS_ENABLED
  1547. edited_scene_root = nullptr;
  1548. #endif
  1549. process_groups.push_back(&default_process_group);
  1550. }
  1551. SceneTree::~SceneTree() {
  1552. if (prev_scene) {
  1553. memdelete(prev_scene);
  1554. prev_scene = nullptr;
  1555. }
  1556. if (pending_new_scene) {
  1557. memdelete(pending_new_scene);
  1558. pending_new_scene = nullptr;
  1559. }
  1560. if (root) {
  1561. root->_set_tree(nullptr);
  1562. root->_propagate_after_exit_tree();
  1563. memdelete(root);
  1564. }
  1565. // Process groups are not deleted immediately, they may remain around. Delete them now.
  1566. for (uint32_t i = 0; i < process_groups.size(); i++) {
  1567. if (process_groups[i] != &default_process_group) {
  1568. memdelete(process_groups[i]);
  1569. }
  1570. }
  1571. memdelete(process_group_call_queue_allocator);
  1572. if (singleton == this) {
  1573. singleton = nullptr;
  1574. }
  1575. }