scene_tree.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. /*************************************************************************/
  2. /* scene_tree.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/os/keyboard.h"
  40. #include "core/os/os.h"
  41. #include "core/string/print_string.h"
  42. #include "node.h"
  43. #include "scene/animation/tween.h"
  44. #include "scene/debugger/scene_debugger.h"
  45. #include "scene/main/multiplayer_api.h"
  46. #include "scene/main/viewport.h"
  47. #include "scene/resources/environment.h"
  48. #include "scene/resources/font.h"
  49. #include "scene/resources/material.h"
  50. #include "scene/resources/mesh.h"
  51. #include "scene/resources/packed_scene.h"
  52. #include "scene/resources/world_2d.h"
  53. #include "scene/resources/world_3d.h"
  54. #include "scene/scene_string_names.h"
  55. #include "servers/display_server.h"
  56. #include "servers/navigation_server_3d.h"
  57. #include "servers/physics_server_2d.h"
  58. #include "servers/physics_server_3d.h"
  59. #include "window.h"
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. void SceneTreeTimer::_bind_methods() {
  63. ClassDB::bind_method(D_METHOD("set_time_left", "time"), &SceneTreeTimer::set_time_left);
  64. ClassDB::bind_method(D_METHOD("get_time_left"), &SceneTreeTimer::get_time_left);
  65. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_left", PROPERTY_HINT_NONE, "suffix:s"), "set_time_left", "get_time_left");
  66. ADD_SIGNAL(MethodInfo("timeout"));
  67. }
  68. void SceneTreeTimer::set_time_left(double p_time) {
  69. time_left = p_time;
  70. }
  71. double SceneTreeTimer::get_time_left() const {
  72. return time_left;
  73. }
  74. void SceneTreeTimer::set_process_always(bool p_process_always) {
  75. process_always = p_process_always;
  76. }
  77. bool SceneTreeTimer::is_process_always() {
  78. return process_always;
  79. }
  80. void SceneTreeTimer::set_ignore_time_scale(bool p_ignore) {
  81. ignore_time_scale = p_ignore;
  82. }
  83. bool SceneTreeTimer::is_ignore_time_scale() {
  84. return ignore_time_scale;
  85. }
  86. void SceneTreeTimer::release_connections() {
  87. List<Connection> connections;
  88. get_all_signal_connections(&connections);
  89. for (const Connection &connection : connections) {
  90. disconnect(connection.signal.get_name(), connection.callable);
  91. }
  92. }
  93. SceneTreeTimer::SceneTreeTimer() {}
  94. void SceneTree::tree_changed() {
  95. tree_version++;
  96. emit_signal(tree_changed_name);
  97. }
  98. void SceneTree::node_added(Node *p_node) {
  99. emit_signal(node_added_name, p_node);
  100. }
  101. void SceneTree::node_removed(Node *p_node) {
  102. if (current_scene == p_node) {
  103. current_scene = nullptr;
  104. }
  105. emit_signal(node_removed_name, p_node);
  106. if (call_lock > 0) {
  107. call_skip.insert(p_node);
  108. }
  109. }
  110. void SceneTree::node_renamed(Node *p_node) {
  111. emit_signal(node_renamed_name, p_node);
  112. }
  113. SceneTree::Group *SceneTree::add_to_group(const StringName &p_group, Node *p_node) {
  114. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  115. if (!E) {
  116. E = group_map.insert(p_group, Group());
  117. }
  118. ERR_FAIL_COND_V_MSG(E->value.nodes.has(p_node), &E->value, "Already in group: " + p_group + ".");
  119. E->value.nodes.push_back(p_node);
  120. //E->value.last_tree_version=0;
  121. E->value.changed = true;
  122. return &E->value;
  123. }
  124. void SceneTree::remove_from_group(const StringName &p_group, Node *p_node) {
  125. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  126. ERR_FAIL_COND(!E);
  127. E->value.nodes.erase(p_node);
  128. if (E->value.nodes.is_empty()) {
  129. group_map.remove(E);
  130. }
  131. }
  132. void SceneTree::make_group_changed(const StringName &p_group) {
  133. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  134. if (E) {
  135. E->value.changed = true;
  136. }
  137. }
  138. void SceneTree::flush_transform_notifications() {
  139. SelfList<Node> *n = xform_change_list.first();
  140. while (n) {
  141. Node *node = n->self();
  142. SelfList<Node> *nx = n->next();
  143. xform_change_list.remove(n);
  144. n = nx;
  145. node->notification(NOTIFICATION_TRANSFORM_CHANGED);
  146. }
  147. }
  148. void SceneTree::_flush_ugc() {
  149. ugc_locked = true;
  150. while (unique_group_calls.size()) {
  151. HashMap<UGCall, Vector<Variant>, UGCall>::Iterator E = unique_group_calls.begin();
  152. const Variant **argptrs = (const Variant **)alloca(E->value.size() * sizeof(Variant *));
  153. for (int i = 0; i < E->value.size(); i++) {
  154. argptrs[i] = &E->value[i];
  155. }
  156. call_group_flagsp(GROUP_CALL_DEFAULT, E->key.group, E->key.call, argptrs, E->value.size());
  157. unique_group_calls.remove(E);
  158. }
  159. ugc_locked = false;
  160. }
  161. void SceneTree::_update_group_order(Group &g, bool p_use_priority) {
  162. if (!g.changed) {
  163. return;
  164. }
  165. if (g.nodes.is_empty()) {
  166. return;
  167. }
  168. Node **nodes = g.nodes.ptrw();
  169. int node_count = g.nodes.size();
  170. if (p_use_priority) {
  171. SortArray<Node *, Node::ComparatorWithPriority> node_sort;
  172. node_sort.sort(nodes, node_count);
  173. } else {
  174. SortArray<Node *, Node::Comparator> node_sort;
  175. node_sort.sort(nodes, node_count);
  176. }
  177. g.changed = false;
  178. }
  179. 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) {
  180. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  181. if (!E) {
  182. return;
  183. }
  184. Group &g = E->value;
  185. if (g.nodes.is_empty()) {
  186. return;
  187. }
  188. if (p_call_flags & GROUP_CALL_UNIQUE && p_call_flags & GROUP_CALL_DEFERRED) {
  189. ERR_FAIL_COND(ugc_locked);
  190. UGCall ug;
  191. ug.call = p_function;
  192. ug.group = p_group;
  193. if (unique_group_calls.has(ug)) {
  194. return;
  195. }
  196. Vector<Variant> args;
  197. for (int i = 0; i < p_argcount; i++) {
  198. args.push_back(*p_args[i]);
  199. }
  200. unique_group_calls[ug] = args;
  201. return;
  202. }
  203. _update_group_order(g);
  204. Vector<Node *> nodes_copy = g.nodes;
  205. Node **nodes = nodes_copy.ptrw();
  206. int node_count = nodes_copy.size();
  207. call_lock++;
  208. if (p_call_flags & GROUP_CALL_REVERSE) {
  209. for (int i = node_count - 1; i >= 0; i--) {
  210. if (call_lock && call_skip.has(nodes[i])) {
  211. continue;
  212. }
  213. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  214. Callable::CallError ce;
  215. nodes[i]->callp(p_function, p_args, p_argcount, ce);
  216. } else {
  217. MessageQueue::get_singleton()->push_callp(nodes[i], p_function, p_args, p_argcount);
  218. }
  219. }
  220. } else {
  221. for (int i = 0; i < node_count; i++) {
  222. if (call_lock && call_skip.has(nodes[i])) {
  223. continue;
  224. }
  225. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  226. Callable::CallError ce;
  227. nodes[i]->callp(p_function, p_args, p_argcount, ce);
  228. } else {
  229. MessageQueue::get_singleton()->push_callp(nodes[i], p_function, p_args, p_argcount);
  230. }
  231. }
  232. }
  233. call_lock--;
  234. if (call_lock == 0) {
  235. call_skip.clear();
  236. }
  237. }
  238. void SceneTree::notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification) {
  239. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  240. if (!E) {
  241. return;
  242. }
  243. Group &g = E->value;
  244. if (g.nodes.is_empty()) {
  245. return;
  246. }
  247. _update_group_order(g);
  248. Vector<Node *> nodes_copy = g.nodes;
  249. Node **nodes = nodes_copy.ptrw();
  250. int node_count = nodes_copy.size();
  251. call_lock++;
  252. if (p_call_flags & GROUP_CALL_REVERSE) {
  253. for (int i = node_count - 1; i >= 0; i--) {
  254. if (call_lock && call_skip.has(nodes[i])) {
  255. continue;
  256. }
  257. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  258. nodes[i]->notification(p_notification);
  259. } else {
  260. MessageQueue::get_singleton()->push_notification(nodes[i], p_notification);
  261. }
  262. }
  263. } else {
  264. for (int i = 0; i < node_count; i++) {
  265. if (call_lock && call_skip.has(nodes[i])) {
  266. continue;
  267. }
  268. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  269. nodes[i]->notification(p_notification);
  270. } else {
  271. MessageQueue::get_singleton()->push_notification(nodes[i], p_notification);
  272. }
  273. }
  274. }
  275. call_lock--;
  276. if (call_lock == 0) {
  277. call_skip.clear();
  278. }
  279. }
  280. void SceneTree::set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value) {
  281. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  282. if (!E) {
  283. return;
  284. }
  285. Group &g = E->value;
  286. if (g.nodes.is_empty()) {
  287. return;
  288. }
  289. _update_group_order(g);
  290. Vector<Node *> nodes_copy = g.nodes;
  291. Node **nodes = nodes_copy.ptrw();
  292. int node_count = nodes_copy.size();
  293. call_lock++;
  294. if (p_call_flags & GROUP_CALL_REVERSE) {
  295. for (int i = node_count - 1; i >= 0; i--) {
  296. if (call_lock && call_skip.has(nodes[i])) {
  297. continue;
  298. }
  299. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  300. nodes[i]->set(p_name, p_value);
  301. } else {
  302. MessageQueue::get_singleton()->push_set(nodes[i], p_name, p_value);
  303. }
  304. }
  305. } else {
  306. for (int i = 0; i < node_count; i++) {
  307. if (call_lock && call_skip.has(nodes[i])) {
  308. continue;
  309. }
  310. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  311. nodes[i]->set(p_name, p_value);
  312. } else {
  313. MessageQueue::get_singleton()->push_set(nodes[i], p_name, p_value);
  314. }
  315. }
  316. }
  317. call_lock--;
  318. if (call_lock == 0) {
  319. call_skip.clear();
  320. }
  321. }
  322. void SceneTree::notify_group(const StringName &p_group, int p_notification) {
  323. notify_group_flags(GROUP_CALL_DEFAULT, p_group, p_notification);
  324. }
  325. void SceneTree::set_group(const StringName &p_group, const String &p_name, const Variant &p_value) {
  326. set_group_flags(GROUP_CALL_DEFAULT, p_group, p_name, p_value);
  327. }
  328. void SceneTree::initialize() {
  329. ERR_FAIL_COND(!root);
  330. initialized = true;
  331. root->_set_tree(this);
  332. MainLoop::initialize();
  333. }
  334. bool SceneTree::physics_process(double p_time) {
  335. root_lock++;
  336. current_frame++;
  337. flush_transform_notifications();
  338. MainLoop::physics_process(p_time);
  339. physics_process_time = p_time;
  340. emit_signal(SNAME("physics_frame"));
  341. _notify_group_pause(SNAME("_physics_process_internal"), Node::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  342. call_group(SNAME("_picking_viewports"), SNAME("_process_picking"));
  343. _notify_group_pause(SNAME("_physics_process"), Node::NOTIFICATION_PHYSICS_PROCESS);
  344. _flush_ugc();
  345. MessageQueue::get_singleton()->flush(); //small little hack
  346. process_tweens(p_time, true);
  347. flush_transform_notifications();
  348. root_lock--;
  349. _flush_delete_queue();
  350. _call_idle_callbacks();
  351. return _quit;
  352. }
  353. bool SceneTree::process(double p_time) {
  354. root_lock++;
  355. MainLoop::process(p_time);
  356. process_time = p_time;
  357. if (multiplayer_poll) {
  358. multiplayer->poll();
  359. for (KeyValue<NodePath, Ref<MultiplayerAPI>> &E : custom_multiplayers) {
  360. E.value->poll();
  361. }
  362. }
  363. emit_signal(SNAME("process_frame"));
  364. MessageQueue::get_singleton()->flush(); //small little hack
  365. flush_transform_notifications();
  366. _notify_group_pause(SNAME("_process_internal"), Node::NOTIFICATION_INTERNAL_PROCESS);
  367. _notify_group_pause(SNAME("_process"), Node::NOTIFICATION_PROCESS);
  368. _flush_ugc();
  369. MessageQueue::get_singleton()->flush(); //small little hack
  370. flush_transform_notifications(); //transforms after world update, to avoid unnecessary enter/exit notifications
  371. root_lock--;
  372. _flush_delete_queue();
  373. //go through timers
  374. List<Ref<SceneTreeTimer>>::Element *L = timers.back(); //last element
  375. for (List<Ref<SceneTreeTimer>>::Element *E = timers.front(); E;) {
  376. List<Ref<SceneTreeTimer>>::Element *N = E->next();
  377. if (paused && !E->get()->is_process_always()) {
  378. if (E == L) {
  379. break; //break on last, so if new timers were added during list traversal, ignore them.
  380. }
  381. E = N;
  382. continue;
  383. }
  384. double time_left = E->get()->get_time_left();
  385. if (E->get()->is_ignore_time_scale()) {
  386. time_left -= Engine::get_singleton()->get_process_step();
  387. } else {
  388. time_left -= p_time;
  389. }
  390. E->get()->set_time_left(time_left);
  391. if (time_left <= 0) {
  392. E->get()->emit_signal(SNAME("timeout"));
  393. timers.erase(E);
  394. }
  395. if (E == L) {
  396. break; //break on last, so if new timers were added during list traversal, ignore them.
  397. }
  398. E = N;
  399. }
  400. process_tweens(p_time, false);
  401. flush_transform_notifications(); //additional transforms after timers update
  402. _call_idle_callbacks();
  403. #ifdef TOOLS_ENABLED
  404. #ifndef _3D_DISABLED
  405. if (Engine::get_singleton()->is_editor_hint()) {
  406. //simple hack to reload fallback environment if it changed from editor
  407. String env_path = ProjectSettings::get_singleton()->get(SNAME("rendering/environment/defaults/default_environment"));
  408. env_path = env_path.strip_edges(); //user may have added a space or two
  409. String cpath;
  410. Ref<Environment> fallback = get_root()->get_world_3d()->get_fallback_environment();
  411. if (fallback.is_valid()) {
  412. cpath = fallback->get_path();
  413. }
  414. if (cpath != env_path) {
  415. if (!env_path.is_empty()) {
  416. fallback = ResourceLoader::load(env_path);
  417. if (fallback.is_null()) {
  418. //could not load fallback, set as empty
  419. ProjectSettings::get_singleton()->set("rendering/environment/defaults/default_environment", "");
  420. }
  421. } else {
  422. fallback.unref();
  423. }
  424. get_root()->get_world_3d()->set_fallback_environment(fallback);
  425. }
  426. }
  427. #endif // _3D_DISABLED
  428. #endif // TOOLS_ENABLED
  429. return _quit;
  430. }
  431. void SceneTree::process_tweens(float p_delta, bool p_physics) {
  432. // This methods works similarly to how SceneTreeTimers are handled.
  433. List<Ref<Tween>>::Element *L = tweens.back();
  434. for (List<Ref<Tween>>::Element *E = tweens.front(); E;) {
  435. List<Ref<Tween>>::Element *N = E->next();
  436. // Don't process if paused or process mode doesn't match.
  437. if (!E->get()->can_process(paused) || (p_physics == (E->get()->get_process_mode() == Tween::TWEEN_PROCESS_IDLE))) {
  438. if (E == L) {
  439. break;
  440. }
  441. E = N;
  442. continue;
  443. }
  444. if (!E->get()->step(p_delta)) {
  445. E->get()->clear();
  446. tweens.erase(E);
  447. }
  448. if (E == L) {
  449. break;
  450. }
  451. E = N;
  452. }
  453. }
  454. void SceneTree::finalize() {
  455. _flush_delete_queue();
  456. _flush_ugc();
  457. initialized = false;
  458. MainLoop::finalize();
  459. if (root) {
  460. root->_set_tree(nullptr);
  461. root->_propagate_after_exit_tree();
  462. memdelete(root); //delete root
  463. root = nullptr;
  464. }
  465. // In case deletion of some objects was queued when destructing the `root`.
  466. // E.g. if `queue_free()` was called for some node outside the tree when handling NOTIFICATION_PREDELETE for some node in the tree.
  467. _flush_delete_queue();
  468. // Cleanup timers.
  469. for (Ref<SceneTreeTimer> &timer : timers) {
  470. timer->release_connections();
  471. }
  472. timers.clear();
  473. }
  474. void SceneTree::quit(int p_exit_code) {
  475. OS::get_singleton()->set_exit_code(p_exit_code);
  476. _quit = true;
  477. }
  478. void SceneTree::_main_window_close() {
  479. if (accept_quit) {
  480. _quit = true;
  481. }
  482. }
  483. void SceneTree::_main_window_go_back() {
  484. if (quit_on_go_back) {
  485. _quit = true;
  486. }
  487. }
  488. void SceneTree::_main_window_focus_in() {
  489. Input *id = Input::get_singleton();
  490. if (id) {
  491. id->ensure_touch_mouse_raised();
  492. }
  493. }
  494. void SceneTree::_notification(int p_notification) {
  495. switch (p_notification) {
  496. case NOTIFICATION_TRANSLATION_CHANGED: {
  497. if (!Engine::get_singleton()->is_editor_hint()) {
  498. get_root()->propagate_notification(p_notification);
  499. }
  500. } break;
  501. case NOTIFICATION_OS_MEMORY_WARNING:
  502. case NOTIFICATION_OS_IME_UPDATE:
  503. case NOTIFICATION_WM_ABOUT:
  504. case NOTIFICATION_CRASH:
  505. case NOTIFICATION_APPLICATION_RESUMED:
  506. case NOTIFICATION_APPLICATION_PAUSED:
  507. case NOTIFICATION_APPLICATION_FOCUS_IN:
  508. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  509. // Pass these to nodes, since they are mirrored.
  510. get_root()->propagate_notification(p_notification);
  511. } break;
  512. }
  513. }
  514. bool SceneTree::is_auto_accept_quit() const {
  515. return accept_quit;
  516. }
  517. void SceneTree::set_auto_accept_quit(bool p_enable) {
  518. accept_quit = p_enable;
  519. }
  520. bool SceneTree::is_quit_on_go_back() const {
  521. return quit_on_go_back;
  522. }
  523. void SceneTree::set_quit_on_go_back(bool p_enable) {
  524. quit_on_go_back = p_enable;
  525. }
  526. #ifdef TOOLS_ENABLED
  527. bool SceneTree::is_node_being_edited(const Node *p_node) const {
  528. return Engine::get_singleton()->is_editor_hint() && edited_scene_root && (edited_scene_root->is_ancestor_of(p_node) || edited_scene_root == p_node);
  529. }
  530. #endif
  531. #ifdef DEBUG_ENABLED
  532. void SceneTree::set_debug_collisions_hint(bool p_enabled) {
  533. debug_collisions_hint = p_enabled;
  534. }
  535. bool SceneTree::is_debugging_collisions_hint() const {
  536. return debug_collisions_hint;
  537. }
  538. void SceneTree::set_debug_paths_hint(bool p_enabled) {
  539. debug_paths_hint = p_enabled;
  540. }
  541. bool SceneTree::is_debugging_paths_hint() const {
  542. return debug_paths_hint;
  543. }
  544. void SceneTree::set_debug_navigation_hint(bool p_enabled) {
  545. debug_navigation_hint = p_enabled;
  546. }
  547. bool SceneTree::is_debugging_navigation_hint() const {
  548. return debug_navigation_hint;
  549. }
  550. #endif
  551. void SceneTree::set_debug_collisions_color(const Color &p_color) {
  552. debug_collisions_color = p_color;
  553. }
  554. Color SceneTree::get_debug_collisions_color() const {
  555. return debug_collisions_color;
  556. }
  557. void SceneTree::set_debug_collision_contact_color(const Color &p_color) {
  558. debug_collision_contact_color = p_color;
  559. }
  560. Color SceneTree::get_debug_collision_contact_color() const {
  561. return debug_collision_contact_color;
  562. }
  563. void SceneTree::set_debug_paths_color(const Color &p_color) {
  564. debug_paths_color = p_color;
  565. }
  566. Color SceneTree::get_debug_paths_color() const {
  567. return debug_paths_color;
  568. }
  569. void SceneTree::set_debug_paths_width(float p_width) {
  570. debug_paths_width = p_width;
  571. }
  572. float SceneTree::get_debug_paths_width() const {
  573. return debug_paths_width;
  574. }
  575. void SceneTree::set_debug_navigation_color(const Color &p_color) {
  576. debug_navigation_color = p_color;
  577. }
  578. Color SceneTree::get_debug_navigation_color() const {
  579. return debug_navigation_color;
  580. }
  581. void SceneTree::set_debug_navigation_disabled_color(const Color &p_color) {
  582. debug_navigation_disabled_color = p_color;
  583. }
  584. Color SceneTree::get_debug_navigation_disabled_color() const {
  585. return debug_navigation_disabled_color;
  586. }
  587. Ref<Material> SceneTree::get_debug_paths_material() {
  588. if (debug_paths_material.is_valid()) {
  589. return debug_paths_material;
  590. }
  591. Ref<StandardMaterial3D> _debug_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  592. _debug_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  593. _debug_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  594. _debug_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  595. _debug_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  596. _debug_material->set_albedo(get_debug_paths_color());
  597. debug_paths_material = _debug_material;
  598. return debug_paths_material;
  599. }
  600. Ref<Material> SceneTree::get_debug_navigation_material() {
  601. if (navigation_material.is_valid()) {
  602. return navigation_material;
  603. }
  604. Ref<StandardMaterial3D> line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  605. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  606. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  607. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  608. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  609. line_material->set_albedo(get_debug_navigation_color());
  610. navigation_material = line_material;
  611. return navigation_material;
  612. }
  613. Ref<Material> SceneTree::get_debug_navigation_disabled_material() {
  614. if (navigation_disabled_material.is_valid()) {
  615. return navigation_disabled_material;
  616. }
  617. Ref<StandardMaterial3D> line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  618. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  619. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  620. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  621. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  622. line_material->set_albedo(get_debug_navigation_disabled_color());
  623. navigation_disabled_material = line_material;
  624. return navigation_disabled_material;
  625. }
  626. Ref<Material> SceneTree::get_debug_collision_material() {
  627. if (collision_material.is_valid()) {
  628. return collision_material;
  629. }
  630. Ref<StandardMaterial3D> line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  631. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  632. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  633. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  634. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  635. line_material->set_albedo(get_debug_collisions_color());
  636. collision_material = line_material;
  637. return collision_material;
  638. }
  639. Ref<ArrayMesh> SceneTree::get_debug_contact_mesh() {
  640. if (debug_contact_mesh.is_valid()) {
  641. return debug_contact_mesh;
  642. }
  643. debug_contact_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  644. Ref<StandardMaterial3D> mat = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  645. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  646. mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  647. mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  648. mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  649. mat->set_albedo(get_debug_collision_contact_color());
  650. Vector3 diamond[6] = {
  651. Vector3(-1, 0, 0),
  652. Vector3(1, 0, 0),
  653. Vector3(0, -1, 0),
  654. Vector3(0, 1, 0),
  655. Vector3(0, 0, -1),
  656. Vector3(0, 0, 1)
  657. };
  658. /* clang-format off */
  659. int diamond_faces[8 * 3] = {
  660. 0, 2, 4,
  661. 0, 3, 4,
  662. 1, 2, 4,
  663. 1, 3, 4,
  664. 0, 2, 5,
  665. 0, 3, 5,
  666. 1, 2, 5,
  667. 1, 3, 5,
  668. };
  669. /* clang-format on */
  670. Vector<int> indices;
  671. for (int i = 0; i < 8 * 3; i++) {
  672. indices.push_back(diamond_faces[i]);
  673. }
  674. Vector<Vector3> vertices;
  675. for (int i = 0; i < 6; i++) {
  676. vertices.push_back(diamond[i] * 0.1);
  677. }
  678. Array arr;
  679. arr.resize(Mesh::ARRAY_MAX);
  680. arr[Mesh::ARRAY_VERTEX] = vertices;
  681. arr[Mesh::ARRAY_INDEX] = indices;
  682. debug_contact_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
  683. debug_contact_mesh->surface_set_material(0, mat);
  684. return debug_contact_mesh;
  685. }
  686. void SceneTree::set_pause(bool p_enabled) {
  687. if (p_enabled == paused) {
  688. return;
  689. }
  690. paused = p_enabled;
  691. NavigationServer3D::get_singleton()->set_active(!p_enabled);
  692. PhysicsServer3D::get_singleton()->set_active(!p_enabled);
  693. PhysicsServer2D::get_singleton()->set_active(!p_enabled);
  694. if (get_root()) {
  695. get_root()->_propagate_pause_notification(p_enabled);
  696. }
  697. }
  698. bool SceneTree::is_paused() const {
  699. return paused;
  700. }
  701. void SceneTree::_notify_group_pause(const StringName &p_group, int p_notification) {
  702. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  703. if (!E) {
  704. return;
  705. }
  706. Group &g = E->value;
  707. if (g.nodes.is_empty()) {
  708. return;
  709. }
  710. _update_group_order(g, p_notification == Node::NOTIFICATION_PROCESS || p_notification == Node::NOTIFICATION_INTERNAL_PROCESS || p_notification == Node::NOTIFICATION_PHYSICS_PROCESS || p_notification == Node::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  711. //copy, so copy on write happens in case something is removed from process while being called
  712. //performance is not lost because only if something is added/removed the vector is copied.
  713. Vector<Node *> nodes_copy = g.nodes;
  714. int node_count = nodes_copy.size();
  715. Node **nodes = nodes_copy.ptrw();
  716. call_lock++;
  717. for (int i = 0; i < node_count; i++) {
  718. Node *n = nodes[i];
  719. if (call_lock && call_skip.has(n)) {
  720. continue;
  721. }
  722. if (!n->can_process()) {
  723. continue;
  724. }
  725. if (!n->can_process_notification(p_notification)) {
  726. continue;
  727. }
  728. n->notification(p_notification);
  729. //ERR_FAIL_COND(node_count != g.nodes.size());
  730. }
  731. call_lock--;
  732. if (call_lock == 0) {
  733. call_skip.clear();
  734. }
  735. }
  736. void SceneTree::_call_input_pause(const StringName &p_group, CallInputType p_call_type, const Ref<InputEvent> &p_input, Viewport *p_viewport) {
  737. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  738. if (!E) {
  739. return;
  740. }
  741. Group &g = E->value;
  742. if (g.nodes.is_empty()) {
  743. return;
  744. }
  745. _update_group_order(g);
  746. //copy, so copy on write happens in case something is removed from process while being called
  747. //performance is not lost because only if something is added/removed the vector is copied.
  748. Vector<Node *> nodes_copy = g.nodes;
  749. int node_count = nodes_copy.size();
  750. Node **nodes = nodes_copy.ptrw();
  751. call_lock++;
  752. for (int i = node_count - 1; i >= 0; i--) {
  753. if (p_viewport->is_input_handled()) {
  754. break;
  755. }
  756. Node *n = nodes[i];
  757. if (call_lock && call_skip.has(n)) {
  758. continue;
  759. }
  760. if (!n->can_process()) {
  761. continue;
  762. }
  763. switch (p_call_type) {
  764. case CALL_INPUT_TYPE_INPUT:
  765. n->_call_input(p_input);
  766. break;
  767. case CALL_INPUT_TYPE_SHORTCUT_INPUT:
  768. n->_call_shortcut_input(p_input);
  769. break;
  770. case CALL_INPUT_TYPE_UNHANDLED_INPUT:
  771. n->_call_unhandled_input(p_input);
  772. break;
  773. case CALL_INPUT_TYPE_UNHANDLED_KEY_INPUT:
  774. n->_call_unhandled_key_input(p_input);
  775. break;
  776. }
  777. }
  778. call_lock--;
  779. if (call_lock == 0) {
  780. call_skip.clear();
  781. }
  782. }
  783. void SceneTree::_call_group_flags(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  784. r_error.error = Callable::CallError::CALL_OK;
  785. ERR_FAIL_COND(p_argcount < 3);
  786. ERR_FAIL_COND(!p_args[0]->is_num());
  787. ERR_FAIL_COND(p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING);
  788. ERR_FAIL_COND(p_args[2]->get_type() != Variant::STRING_NAME && p_args[2]->get_type() != Variant::STRING);
  789. int flags = *p_args[0];
  790. StringName group = *p_args[1];
  791. StringName method = *p_args[2];
  792. call_group_flagsp(flags, group, method, p_args + 3, p_argcount - 3);
  793. }
  794. void SceneTree::_call_group(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  795. r_error.error = Callable::CallError::CALL_OK;
  796. ERR_FAIL_COND(p_argcount < 2);
  797. ERR_FAIL_COND(p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING);
  798. ERR_FAIL_COND(p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING);
  799. StringName group = *p_args[0];
  800. StringName method = *p_args[1];
  801. call_group_flagsp(GROUP_CALL_DEFAULT, group, method, p_args + 2, p_argcount - 2);
  802. }
  803. int64_t SceneTree::get_frame() const {
  804. return current_frame;
  805. }
  806. TypedArray<Node> SceneTree::_get_nodes_in_group(const StringName &p_group) {
  807. TypedArray<Node> ret;
  808. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  809. if (!E) {
  810. return ret;
  811. }
  812. _update_group_order(E->value); //update order just in case
  813. int nc = E->value.nodes.size();
  814. if (nc == 0) {
  815. return ret;
  816. }
  817. ret.resize(nc);
  818. Node **ptr = E->value.nodes.ptrw();
  819. for (int i = 0; i < nc; i++) {
  820. ret[i] = ptr[i];
  821. }
  822. return ret;
  823. }
  824. bool SceneTree::has_group(const StringName &p_identifier) const {
  825. return group_map.has(p_identifier);
  826. }
  827. Node *SceneTree::get_first_node_in_group(const StringName &p_group) {
  828. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  829. if (!E) {
  830. return nullptr; // No group.
  831. }
  832. _update_group_order(E->value); // Update order just in case.
  833. if (E->value.nodes.is_empty()) {
  834. return nullptr;
  835. }
  836. return E->value.nodes[0];
  837. }
  838. void SceneTree::get_nodes_in_group(const StringName &p_group, List<Node *> *p_list) {
  839. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  840. if (!E) {
  841. return;
  842. }
  843. _update_group_order(E->value); //update order just in case
  844. int nc = E->value.nodes.size();
  845. if (nc == 0) {
  846. return;
  847. }
  848. Node **ptr = E->value.nodes.ptrw();
  849. for (int i = 0; i < nc; i++) {
  850. p_list->push_back(ptr[i]);
  851. }
  852. }
  853. void SceneTree::_flush_delete_queue() {
  854. _THREAD_SAFE_METHOD_
  855. while (delete_queue.size()) {
  856. Object *obj = ObjectDB::get_instance(delete_queue.front()->get());
  857. if (obj) {
  858. memdelete(obj);
  859. }
  860. delete_queue.pop_front();
  861. }
  862. }
  863. void SceneTree::queue_delete(Object *p_object) {
  864. _THREAD_SAFE_METHOD_
  865. ERR_FAIL_NULL(p_object);
  866. p_object->_is_queued_for_deletion = true;
  867. delete_queue.push_back(p_object->get_instance_id());
  868. }
  869. int SceneTree::get_node_count() const {
  870. return node_count;
  871. }
  872. void SceneTree::set_edited_scene_root(Node *p_node) {
  873. #ifdef TOOLS_ENABLED
  874. edited_scene_root = p_node;
  875. #endif
  876. }
  877. Node *SceneTree::get_edited_scene_root() const {
  878. #ifdef TOOLS_ENABLED
  879. return edited_scene_root;
  880. #else
  881. return nullptr;
  882. #endif
  883. }
  884. void SceneTree::set_current_scene(Node *p_scene) {
  885. ERR_FAIL_COND(p_scene && p_scene->get_parent() != root);
  886. current_scene = p_scene;
  887. }
  888. Node *SceneTree::get_current_scene() const {
  889. return current_scene;
  890. }
  891. void SceneTree::_change_scene(Node *p_to) {
  892. if (current_scene) {
  893. memdelete(current_scene);
  894. current_scene = nullptr;
  895. }
  896. // If we're quitting, abort.
  897. if (unlikely(_quit)) {
  898. if (p_to) { // Prevent memory leak.
  899. memdelete(p_to);
  900. }
  901. return;
  902. }
  903. if (p_to) {
  904. current_scene = p_to;
  905. root->add_child(p_to);
  906. }
  907. }
  908. Error SceneTree::change_scene(const String &p_path) {
  909. Ref<PackedScene> new_scene = ResourceLoader::load(p_path);
  910. if (new_scene.is_null()) {
  911. return ERR_CANT_OPEN;
  912. }
  913. return change_scene_to(new_scene);
  914. }
  915. Error SceneTree::change_scene_to(const Ref<PackedScene> &p_scene) {
  916. Node *new_scene = nullptr;
  917. if (p_scene.is_valid()) {
  918. new_scene = p_scene->instantiate();
  919. ERR_FAIL_COND_V(!new_scene, ERR_CANT_CREATE);
  920. }
  921. call_deferred(SNAME("_change_scene"), new_scene);
  922. return OK;
  923. }
  924. Error SceneTree::reload_current_scene() {
  925. ERR_FAIL_COND_V(!current_scene, ERR_UNCONFIGURED);
  926. String fname = current_scene->get_scene_file_path();
  927. return change_scene(fname);
  928. }
  929. void SceneTree::add_current_scene(Node *p_current) {
  930. current_scene = p_current;
  931. root->add_child(p_current);
  932. }
  933. Ref<SceneTreeTimer> SceneTree::create_timer(double p_delay_sec, bool p_process_always) {
  934. Ref<SceneTreeTimer> stt;
  935. stt.instantiate();
  936. stt->set_process_always(p_process_always);
  937. stt->set_time_left(p_delay_sec);
  938. timers.push_back(stt);
  939. return stt;
  940. }
  941. Ref<Tween> SceneTree::create_tween() {
  942. Ref<Tween> tween = memnew(Tween(true));
  943. tweens.push_back(tween);
  944. return tween;
  945. }
  946. TypedArray<Tween> SceneTree::get_processed_tweens() {
  947. TypedArray<Tween> ret;
  948. ret.resize(tweens.size());
  949. int i = 0;
  950. for (const Ref<Tween> &tween : tweens) {
  951. ret[i] = tween;
  952. i++;
  953. }
  954. return ret;
  955. }
  956. Ref<MultiplayerAPI> SceneTree::get_multiplayer(const NodePath &p_for_path) const {
  957. Ref<MultiplayerAPI> out = multiplayer;
  958. for (const KeyValue<NodePath, Ref<MultiplayerAPI>> &E : custom_multiplayers) {
  959. const Vector<StringName> snames = E.key.get_names();
  960. const Vector<StringName> tnames = p_for_path.get_names();
  961. if (tnames.size() < snames.size()) {
  962. continue;
  963. }
  964. const StringName *sptr = snames.ptr();
  965. const StringName *nptr = tnames.ptr();
  966. bool valid = true;
  967. for (int i = 0; i < snames.size(); i++) {
  968. if (sptr[i] != nptr[i]) {
  969. valid = false;
  970. break;
  971. }
  972. }
  973. if (valid) {
  974. out = E.value;
  975. break;
  976. }
  977. }
  978. return out;
  979. }
  980. void SceneTree::set_multiplayer(Ref<MultiplayerAPI> p_multiplayer, const NodePath &p_root_path) {
  981. if (p_root_path.is_empty()) {
  982. ERR_FAIL_COND(!p_multiplayer.is_valid());
  983. if (multiplayer.is_valid()) {
  984. multiplayer->object_configuration_remove(nullptr, NodePath("/" + root->get_name()));
  985. }
  986. multiplayer = p_multiplayer;
  987. multiplayer->object_configuration_add(nullptr, NodePath("/" + root->get_name()));
  988. } else {
  989. if (custom_multiplayers.has(p_root_path)) {
  990. custom_multiplayers[p_root_path]->object_configuration_remove(nullptr, p_root_path);
  991. }
  992. if (p_multiplayer.is_valid()) {
  993. custom_multiplayers[p_root_path] = p_multiplayer;
  994. p_multiplayer->object_configuration_add(nullptr, p_root_path);
  995. }
  996. }
  997. }
  998. void SceneTree::set_multiplayer_poll_enabled(bool p_enabled) {
  999. multiplayer_poll = p_enabled;
  1000. }
  1001. bool SceneTree::is_multiplayer_poll_enabled() const {
  1002. return multiplayer_poll;
  1003. }
  1004. void SceneTree::_bind_methods() {
  1005. ClassDB::bind_method(D_METHOD("get_root"), &SceneTree::get_root);
  1006. ClassDB::bind_method(D_METHOD("has_group", "name"), &SceneTree::has_group);
  1007. ClassDB::bind_method(D_METHOD("is_auto_accept_quit"), &SceneTree::is_auto_accept_quit);
  1008. ClassDB::bind_method(D_METHOD("set_auto_accept_quit", "enabled"), &SceneTree::set_auto_accept_quit);
  1009. ClassDB::bind_method(D_METHOD("is_quit_on_go_back"), &SceneTree::is_quit_on_go_back);
  1010. ClassDB::bind_method(D_METHOD("set_quit_on_go_back", "enabled"), &SceneTree::set_quit_on_go_back);
  1011. ClassDB::bind_method(D_METHOD("set_debug_collisions_hint", "enable"), &SceneTree::set_debug_collisions_hint);
  1012. ClassDB::bind_method(D_METHOD("is_debugging_collisions_hint"), &SceneTree::is_debugging_collisions_hint);
  1013. ClassDB::bind_method(D_METHOD("set_debug_paths_hint", "enable"), &SceneTree::set_debug_paths_hint);
  1014. ClassDB::bind_method(D_METHOD("is_debugging_paths_hint"), &SceneTree::is_debugging_paths_hint);
  1015. ClassDB::bind_method(D_METHOD("set_debug_navigation_hint", "enable"), &SceneTree::set_debug_navigation_hint);
  1016. ClassDB::bind_method(D_METHOD("is_debugging_navigation_hint"), &SceneTree::is_debugging_navigation_hint);
  1017. ClassDB::bind_method(D_METHOD("set_edited_scene_root", "scene"), &SceneTree::set_edited_scene_root);
  1018. ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &SceneTree::get_edited_scene_root);
  1019. ClassDB::bind_method(D_METHOD("set_pause", "enable"), &SceneTree::set_pause);
  1020. ClassDB::bind_method(D_METHOD("is_paused"), &SceneTree::is_paused);
  1021. ClassDB::bind_method(D_METHOD("create_timer", "time_sec", "process_always"), &SceneTree::create_timer, DEFVAL(true));
  1022. ClassDB::bind_method(D_METHOD("create_tween"), &SceneTree::create_tween);
  1023. ClassDB::bind_method(D_METHOD("get_processed_tweens"), &SceneTree::get_processed_tweens);
  1024. ClassDB::bind_method(D_METHOD("get_node_count"), &SceneTree::get_node_count);
  1025. ClassDB::bind_method(D_METHOD("get_frame"), &SceneTree::get_frame);
  1026. ClassDB::bind_method(D_METHOD("quit", "exit_code"), &SceneTree::quit, DEFVAL(EXIT_SUCCESS));
  1027. ClassDB::bind_method(D_METHOD("queue_delete", "obj"), &SceneTree::queue_delete);
  1028. MethodInfo mi;
  1029. mi.name = "call_group_flags";
  1030. mi.arguments.push_back(PropertyInfo(Variant::INT, "flags"));
  1031. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "group"));
  1032. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1033. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group_flags", &SceneTree::_call_group_flags, mi);
  1034. ClassDB::bind_method(D_METHOD("notify_group_flags", "call_flags", "group", "notification"), &SceneTree::notify_group_flags);
  1035. ClassDB::bind_method(D_METHOD("set_group_flags", "call_flags", "group", "property", "value"), &SceneTree::set_group_flags);
  1036. MethodInfo mi2;
  1037. mi2.name = "call_group";
  1038. mi2.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "group"));
  1039. mi2.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1040. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group", &SceneTree::_call_group, mi2);
  1041. ClassDB::bind_method(D_METHOD("notify_group", "group", "notification"), &SceneTree::notify_group);
  1042. ClassDB::bind_method(D_METHOD("set_group", "group", "property", "value"), &SceneTree::set_group);
  1043. ClassDB::bind_method(D_METHOD("get_nodes_in_group", "group"), &SceneTree::_get_nodes_in_group);
  1044. ClassDB::bind_method(D_METHOD("get_first_node_in_group", "group"), &SceneTree::get_first_node_in_group);
  1045. ClassDB::bind_method(D_METHOD("set_current_scene", "child_node"), &SceneTree::set_current_scene);
  1046. ClassDB::bind_method(D_METHOD("get_current_scene"), &SceneTree::get_current_scene);
  1047. ClassDB::bind_method(D_METHOD("change_scene", "path"), &SceneTree::change_scene);
  1048. ClassDB::bind_method(D_METHOD("change_scene_to", "packed_scene"), &SceneTree::change_scene_to);
  1049. ClassDB::bind_method(D_METHOD("reload_current_scene"), &SceneTree::reload_current_scene);
  1050. ClassDB::bind_method(D_METHOD("_change_scene"), &SceneTree::_change_scene);
  1051. ClassDB::bind_method(D_METHOD("set_multiplayer", "multiplayer", "root_path"), &SceneTree::set_multiplayer, DEFVAL(NodePath()));
  1052. ClassDB::bind_method(D_METHOD("get_multiplayer", "for_path"), &SceneTree::get_multiplayer, DEFVAL(NodePath()));
  1053. ClassDB::bind_method(D_METHOD("set_multiplayer_poll_enabled", "enabled"), &SceneTree::set_multiplayer_poll_enabled);
  1054. ClassDB::bind_method(D_METHOD("is_multiplayer_poll_enabled"), &SceneTree::is_multiplayer_poll_enabled);
  1055. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_accept_quit"), "set_auto_accept_quit", "is_auto_accept_quit");
  1056. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "quit_on_go_back"), "set_quit_on_go_back", "is_quit_on_go_back");
  1057. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_collisions_hint"), "set_debug_collisions_hint", "is_debugging_collisions_hint");
  1058. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_paths_hint"), "set_debug_paths_hint", "is_debugging_paths_hint");
  1059. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_navigation_hint"), "set_debug_navigation_hint", "is_debugging_navigation_hint");
  1060. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_pause", "is_paused");
  1061. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edited_scene_root", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_edited_scene_root", "get_edited_scene_root");
  1062. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "current_scene", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_current_scene", "get_current_scene");
  1063. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "", "get_root");
  1064. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multiplayer_poll"), "set_multiplayer_poll_enabled", "is_multiplayer_poll_enabled");
  1065. ADD_SIGNAL(MethodInfo("tree_changed"));
  1066. ADD_SIGNAL(MethodInfo("tree_process_mode_changed")); //editor only signal, but due to API hash it can't be removed in run-time
  1067. ADD_SIGNAL(MethodInfo("node_added", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1068. ADD_SIGNAL(MethodInfo("node_removed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1069. ADD_SIGNAL(MethodInfo("node_renamed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1070. ADD_SIGNAL(MethodInfo("node_configuration_warning_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1071. ADD_SIGNAL(MethodInfo("process_frame"));
  1072. ADD_SIGNAL(MethodInfo("physics_frame"));
  1073. BIND_ENUM_CONSTANT(GROUP_CALL_DEFAULT);
  1074. BIND_ENUM_CONSTANT(GROUP_CALL_REVERSE);
  1075. BIND_ENUM_CONSTANT(GROUP_CALL_DEFERRED);
  1076. BIND_ENUM_CONSTANT(GROUP_CALL_UNIQUE);
  1077. }
  1078. SceneTree *SceneTree::singleton = nullptr;
  1079. SceneTree::IdleCallback SceneTree::idle_callbacks[SceneTree::MAX_IDLE_CALLBACKS];
  1080. int SceneTree::idle_callback_count = 0;
  1081. void SceneTree::_call_idle_callbacks() {
  1082. for (int i = 0; i < idle_callback_count; i++) {
  1083. idle_callbacks[i]();
  1084. }
  1085. }
  1086. void SceneTree::add_idle_callback(IdleCallback p_callback) {
  1087. ERR_FAIL_COND(idle_callback_count >= MAX_IDLE_CALLBACKS);
  1088. idle_callbacks[idle_callback_count++] = p_callback;
  1089. }
  1090. void SceneTree::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1091. if (p_function == "change_scene") {
  1092. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1093. List<String> directories;
  1094. directories.push_back(dir_access->get_current_dir());
  1095. while (!directories.is_empty()) {
  1096. dir_access->change_dir(directories.back()->get());
  1097. directories.pop_back();
  1098. dir_access->list_dir_begin();
  1099. String filename = dir_access->get_next();
  1100. while (!filename.is_empty()) {
  1101. if (filename == "." || filename == "..") {
  1102. filename = dir_access->get_next();
  1103. continue;
  1104. }
  1105. if (dir_access->dir_exists(filename)) {
  1106. directories.push_back(dir_access->get_current_dir().path_join(filename));
  1107. } else if (filename.ends_with(".tscn") || filename.ends_with(".scn")) {
  1108. r_options->push_back("\"" + dir_access->get_current_dir().path_join(filename) + "\"");
  1109. }
  1110. filename = dir_access->get_next();
  1111. }
  1112. }
  1113. }
  1114. }
  1115. SceneTree::SceneTree() {
  1116. if (singleton == nullptr) {
  1117. singleton = this;
  1118. }
  1119. debug_collisions_color = GLOBAL_DEF("debug/shapes/collision/shape_color", Color(0.0, 0.6, 0.7, 0.42));
  1120. debug_collision_contact_color = GLOBAL_DEF("debug/shapes/collision/contact_color", Color(1.0, 0.2, 0.1, 0.8));
  1121. debug_paths_color = GLOBAL_DEF("debug/shapes/paths/geometry_color", Color(0.1, 1.0, 0.7, 0.4));
  1122. debug_paths_width = GLOBAL_DEF("debug/shapes/paths/geometry_width", 2.0);
  1123. debug_navigation_color = GLOBAL_DEF("debug/shapes/navigation/geometry_color", Color(0.1, 1.0, 0.7, 0.4));
  1124. debug_navigation_disabled_color = GLOBAL_DEF("debug/shapes/navigation/disabled_geometry_color", Color(1.0, 0.7, 0.1, 0.4));
  1125. collision_debug_contacts = GLOBAL_DEF("debug/shapes/collision/max_contacts_displayed", 10000);
  1126. ProjectSettings::get_singleton()->set_custom_property_info("debug/shapes/collision/max_contacts_displayed", PropertyInfo(Variant::INT, "debug/shapes/collision/max_contacts_displayed", PROPERTY_HINT_RANGE, "0,20000,1")); // No negative
  1127. GLOBAL_DEF("debug/shapes/collision/draw_2d_outlines", true);
  1128. Math::randomize();
  1129. // Create with mainloop.
  1130. root = memnew(Window);
  1131. root->set_process_mode(Node::PROCESS_MODE_PAUSABLE);
  1132. root->set_name("root");
  1133. root->set_title(ProjectSettings::get_singleton()->get("application/config/name"));
  1134. #ifndef _3D_DISABLED
  1135. if (!root->get_world_3d().is_valid()) {
  1136. root->set_world_3d(Ref<World3D>(memnew(World3D)));
  1137. }
  1138. root->set_as_audio_listener_3d(true);
  1139. #endif // _3D_DISABLED
  1140. // Initialize network state.
  1141. set_multiplayer(MultiplayerAPI::create_default_interface());
  1142. root->set_as_audio_listener_2d(true);
  1143. current_scene = nullptr;
  1144. const int msaa_mode_2d = GLOBAL_DEF_BASIC("rendering/anti_aliasing/quality/msaa_2d", 0);
  1145. ProjectSettings::get_singleton()->set_custom_property_info("rendering/anti_aliasing/quality/msaa_2d", PropertyInfo(Variant::INT, "rendering/anti_aliasing/quality/msaa_2d", PROPERTY_HINT_ENUM, String::utf8("Disabled (Fastest),2× (Average),4× (Slow),8× (Slowest)")));
  1146. root->set_msaa_2d(Viewport::MSAA(msaa_mode_2d));
  1147. const int msaa_mode_3d = GLOBAL_DEF_BASIC("rendering/anti_aliasing/quality/msaa_3d", 0);
  1148. ProjectSettings::get_singleton()->set_custom_property_info("rendering/anti_aliasing/quality/msaa_3d", PropertyInfo(Variant::INT, "rendering/anti_aliasing/quality/msaa_3d", PROPERTY_HINT_ENUM, String::utf8("Disabled (Fastest),2× (Average),4× (Slow),8× (Slowest)")));
  1149. root->set_msaa_3d(Viewport::MSAA(msaa_mode_3d));
  1150. const int ssaa_mode = GLOBAL_DEF_BASIC("rendering/anti_aliasing/quality/screen_space_aa", 0);
  1151. ProjectSettings::get_singleton()->set_custom_property_info("rendering/anti_aliasing/quality/screen_space_aa", PropertyInfo(Variant::INT, "rendering/anti_aliasing/quality/screen_space_aa", PROPERTY_HINT_ENUM, "Disabled (Fastest),FXAA (Fast)"));
  1152. root->set_screen_space_aa(Viewport::ScreenSpaceAA(ssaa_mode));
  1153. const bool use_taa = GLOBAL_DEF_BASIC("rendering/anti_aliasing/quality/use_taa", false);
  1154. root->set_use_taa(use_taa);
  1155. const bool use_debanding = GLOBAL_DEF("rendering/anti_aliasing/quality/use_debanding", false);
  1156. root->set_use_debanding(use_debanding);
  1157. const bool use_occlusion_culling = GLOBAL_DEF("rendering/occlusion_culling/use_occlusion_culling", false);
  1158. root->set_use_occlusion_culling(use_occlusion_culling);
  1159. float mesh_lod_threshold = GLOBAL_DEF("rendering/mesh_lod/lod_change/threshold_pixels", 1.0);
  1160. ProjectSettings::get_singleton()->set_custom_property_info("rendering/mesh_lod/lod_change/threshold_pixels", PropertyInfo(Variant::FLOAT, "rendering/mesh_lod/lod_change/threshold_pixels", PROPERTY_HINT_RANGE, "0,1024,0.1"));
  1161. root->set_mesh_lod_threshold(mesh_lod_threshold);
  1162. bool snap_2d_transforms = GLOBAL_DEF("rendering/2d/snap/snap_2d_transforms_to_pixel", false);
  1163. root->set_snap_2d_transforms_to_pixel(snap_2d_transforms);
  1164. bool snap_2d_vertices = GLOBAL_DEF("rendering/2d/snap/snap_2d_vertices_to_pixel", false);
  1165. root->set_snap_2d_vertices_to_pixel(snap_2d_vertices);
  1166. // We setup VRS for the main viewport here, in the editor this will have little effect.
  1167. const int vrs_mode = GLOBAL_DEF("rendering/vrs/mode", 0);
  1168. ProjectSettings::get_singleton()->set_custom_property_info("rendering/vrs/mode", PropertyInfo(Variant::INT, "rendering/vrs/mode", PROPERTY_HINT_ENUM, String::utf8("Disabled,Texture,XR")));
  1169. root->set_vrs_mode(Viewport::VRSMode(vrs_mode));
  1170. const String vrs_texture_path = String(GLOBAL_DEF("rendering/vrs/texture", String())).strip_edges();
  1171. ProjectSettings::get_singleton()->set_custom_property_info("rendering/vrs/texture",
  1172. PropertyInfo(Variant::STRING,
  1173. "rendering/vrs/texture",
  1174. PROPERTY_HINT_FILE, "*.png"));
  1175. if (vrs_mode == 1 && !vrs_texture_path.is_empty()) {
  1176. Ref<Image> vrs_image;
  1177. vrs_image.instantiate();
  1178. Error load_err = ImageLoader::load_image(vrs_texture_path, vrs_image);
  1179. if (load_err) {
  1180. ERR_PRINT("Non-existing or invalid VRS texture at '" + vrs_texture_path + "'.");
  1181. } else {
  1182. Ref<ImageTexture> vrs_texture;
  1183. vrs_texture.instantiate();
  1184. vrs_texture->create_from_image(vrs_image);
  1185. root->set_vrs_texture(vrs_texture);
  1186. }
  1187. }
  1188. int shadowmap_size = GLOBAL_DEF("rendering/lights_and_shadows/positional_shadow/atlas_size", 4096);
  1189. ProjectSettings::get_singleton()->set_custom_property_info("rendering/lights_and_shadows/positional_shadow/atlas_size", PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_size", PROPERTY_HINT_RANGE, "256,16384"));
  1190. GLOBAL_DEF("rendering/lights_and_shadows/positional_shadow/atlas_size.mobile", 2048);
  1191. bool shadowmap_16_bits = GLOBAL_DEF("rendering/lights_and_shadows/positional_shadow/atlas_16_bits", true);
  1192. int atlas_q0 = GLOBAL_DEF("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_0_subdiv", 2);
  1193. int atlas_q1 = GLOBAL_DEF("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_1_subdiv", 2);
  1194. int atlas_q2 = GLOBAL_DEF("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_2_subdiv", 3);
  1195. int atlas_q3 = GLOBAL_DEF("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_3_subdiv", 4);
  1196. ProjectSettings::get_singleton()->set_custom_property_info("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_0_subdiv", 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"));
  1197. ProjectSettings::get_singleton()->set_custom_property_info("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_1_subdiv", 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"));
  1198. ProjectSettings::get_singleton()->set_custom_property_info("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_2_subdiv", 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"));
  1199. ProjectSettings::get_singleton()->set_custom_property_info("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_3_subdiv", 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"));
  1200. root->set_positional_shadow_atlas_size(shadowmap_size);
  1201. root->set_positional_shadow_atlas_16_bits(shadowmap_16_bits);
  1202. root->set_positional_shadow_atlas_quadrant_subdiv(0, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q0));
  1203. root->set_positional_shadow_atlas_quadrant_subdiv(1, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q1));
  1204. root->set_positional_shadow_atlas_quadrant_subdiv(2, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q2));
  1205. root->set_positional_shadow_atlas_quadrant_subdiv(3, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q3));
  1206. Viewport::SDFOversize sdf_oversize = Viewport::SDFOversize(int(GLOBAL_DEF("rendering/2d/sdf/oversize", 1)));
  1207. root->set_sdf_oversize(sdf_oversize);
  1208. Viewport::SDFScale sdf_scale = Viewport::SDFScale(int(GLOBAL_DEF("rendering/2d/sdf/scale", 1)));
  1209. root->set_sdf_scale(sdf_scale);
  1210. ProjectSettings::get_singleton()->set_custom_property_info("rendering/2d/sdf/oversize", PropertyInfo(Variant::INT, "rendering/2d/sdf/oversize", PROPERTY_HINT_ENUM, "100%,120%,150%,200%"));
  1211. ProjectSettings::get_singleton()->set_custom_property_info("rendering/2d/sdf/scale", PropertyInfo(Variant::INT, "rendering/2d/sdf/scale", PROPERTY_HINT_ENUM, "100%,50%,25%"));
  1212. #ifndef _3D_DISABLED
  1213. { // Load default fallback environment.
  1214. // Get possible extensions.
  1215. List<String> exts;
  1216. ResourceLoader::get_recognized_extensions_for_type("Environment", &exts);
  1217. String ext_hint;
  1218. for (const String &E : exts) {
  1219. if (!ext_hint.is_empty()) {
  1220. ext_hint += ",";
  1221. }
  1222. ext_hint += "*." + E;
  1223. }
  1224. // Get path.
  1225. String env_path = GLOBAL_DEF("rendering/environment/defaults/default_environment", "");
  1226. // Setup property.
  1227. ProjectSettings::get_singleton()->set_custom_property_info("rendering/environment/defaults/default_environment", PropertyInfo(Variant::STRING, "rendering/viewport/default_environment", PROPERTY_HINT_FILE, ext_hint));
  1228. env_path = env_path.strip_edges();
  1229. if (!env_path.is_empty()) {
  1230. Ref<Environment> env = ResourceLoader::load(env_path);
  1231. if (env.is_valid()) {
  1232. root->get_world_3d()->set_fallback_environment(env);
  1233. } else {
  1234. if (Engine::get_singleton()->is_editor_hint()) {
  1235. // File was erased, clear the field.
  1236. ProjectSettings::get_singleton()->set("rendering/environment/defaults/default_environment", "");
  1237. } else {
  1238. // File was erased, notify user.
  1239. ERR_PRINT(RTR("Default Environment as specified in Project Settings (Rendering -> Environment -> Default Environment) could not be loaded."));
  1240. }
  1241. }
  1242. }
  1243. }
  1244. #endif // _3D_DISABLED
  1245. root->set_physics_object_picking(GLOBAL_DEF("physics/common/enable_object_picking", true));
  1246. root->connect("close_requested", callable_mp(this, &SceneTree::_main_window_close));
  1247. root->connect("go_back_requested", callable_mp(this, &SceneTree::_main_window_go_back));
  1248. root->connect("focus_entered", callable_mp(this, &SceneTree::_main_window_focus_in));
  1249. #ifdef TOOLS_ENABLED
  1250. edited_scene_root = nullptr;
  1251. #endif
  1252. }
  1253. SceneTree::~SceneTree() {
  1254. if (root) {
  1255. root->_set_tree(nullptr);
  1256. root->_propagate_after_exit_tree();
  1257. memdelete(root);
  1258. }
  1259. if (singleton == this) {
  1260. singleton = nullptr;
  1261. }
  1262. }