scene_tree.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158
  1. /*************************************************************************/
  2. /* scene_tree.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/io/marshalls.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/message_queue.h"
  34. #include "core/os/dir_access.h"
  35. #include "core/os/keyboard.h"
  36. #include "core/os/os.h"
  37. #include "core/print_string.h"
  38. #include "core/project_settings.h"
  39. #include "main/input_default.h"
  40. #include "node.h"
  41. #include "scene/debugger/script_debugger_remote.h"
  42. #include "scene/resources/dynamic_font.h"
  43. #include "scene/resources/material.h"
  44. #include "scene/resources/mesh.h"
  45. #include "scene/resources/packed_scene.h"
  46. #include "scene/scene_string_names.h"
  47. #include "servers/physics_2d_server.h"
  48. #include "servers/physics_server.h"
  49. #include "viewport.h"
  50. #include <stdio.h>
  51. void SceneTreeTimer::_bind_methods() {
  52. ClassDB::bind_method(D_METHOD("set_time_left", "time"), &SceneTreeTimer::set_time_left);
  53. ClassDB::bind_method(D_METHOD("get_time_left"), &SceneTreeTimer::get_time_left);
  54. ADD_PROPERTY(PropertyInfo(Variant::REAL, "time_left"), "set_time_left", "get_time_left");
  55. ADD_SIGNAL(MethodInfo("timeout"));
  56. }
  57. void SceneTreeTimer::set_time_left(float p_time) {
  58. time_left = p_time;
  59. }
  60. float SceneTreeTimer::get_time_left() const {
  61. return time_left;
  62. }
  63. void SceneTreeTimer::set_pause_mode_process(bool p_pause_mode_process) {
  64. process_pause = p_pause_mode_process;
  65. }
  66. bool SceneTreeTimer::is_pause_mode_process() {
  67. return process_pause;
  68. }
  69. void SceneTreeTimer::release_connections() {
  70. List<Connection> connections;
  71. get_all_signal_connections(&connections);
  72. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  73. Connection const &connection = E->get();
  74. disconnect(connection.signal, connection.target, connection.method);
  75. }
  76. }
  77. SceneTreeTimer::SceneTreeTimer() {
  78. time_left = 0;
  79. process_pause = true;
  80. }
  81. void SceneTree::tree_changed() {
  82. tree_version++;
  83. emit_signal(tree_changed_name);
  84. }
  85. void SceneTree::node_added(Node *p_node) {
  86. emit_signal(node_added_name, p_node);
  87. }
  88. void SceneTree::node_removed(Node *p_node) {
  89. if (current_scene == p_node) {
  90. current_scene = NULL;
  91. }
  92. emit_signal(node_removed_name, p_node);
  93. if (call_lock > 0)
  94. call_skip.insert(p_node);
  95. }
  96. void SceneTree::node_renamed(Node *p_node) {
  97. emit_signal(node_renamed_name, p_node);
  98. }
  99. SceneTree::Group *SceneTree::add_to_group(const StringName &p_group, Node *p_node) {
  100. Map<StringName, Group>::Element *E = group_map.find(p_group);
  101. if (!E) {
  102. E = group_map.insert(p_group, Group());
  103. }
  104. ERR_FAIL_COND_V_MSG(E->get().nodes.find(p_node) != -1, &E->get(), "Already in group: " + p_group + ".");
  105. E->get().nodes.push_back(p_node);
  106. //E->get().last_tree_version=0;
  107. E->get().changed = true;
  108. return &E->get();
  109. }
  110. void SceneTree::remove_from_group(const StringName &p_group, Node *p_node) {
  111. Map<StringName, Group>::Element *E = group_map.find(p_group);
  112. ERR_FAIL_COND(!E);
  113. E->get().nodes.erase(p_node);
  114. if (E->get().nodes.empty())
  115. group_map.erase(E);
  116. }
  117. void SceneTree::make_group_changed(const StringName &p_group) {
  118. Map<StringName, Group>::Element *E = group_map.find(p_group);
  119. if (E)
  120. E->get().changed = true;
  121. }
  122. void SceneTree::flush_transform_notifications() {
  123. SelfList<Node> *n = xform_change_list.first();
  124. while (n) {
  125. Node *node = n->self();
  126. SelfList<Node> *nx = n->next();
  127. xform_change_list.remove(n);
  128. n = nx;
  129. node->notification(NOTIFICATION_TRANSFORM_CHANGED);
  130. }
  131. }
  132. void SceneTree::_flush_ugc() {
  133. ugc_locked = true;
  134. while (unique_group_calls.size()) {
  135. Map<UGCall, Vector<Variant> >::Element *E = unique_group_calls.front();
  136. Variant v[VARIANT_ARG_MAX];
  137. for (int i = 0; i < E->get().size(); i++)
  138. v[i] = E->get()[i];
  139. call_group_flags(GROUP_CALL_REALTIME, E->key().group, E->key().call, v[0], v[1], v[2], v[3], v[4]);
  140. unique_group_calls.erase(E);
  141. }
  142. ugc_locked = false;
  143. }
  144. void SceneTree::_update_group_order(Group &g, bool p_use_priority) {
  145. if (!g.changed)
  146. return;
  147. if (g.nodes.empty())
  148. return;
  149. Node **nodes = g.nodes.ptrw();
  150. int node_count = g.nodes.size();
  151. if (p_use_priority) {
  152. SortArray<Node *, Node::ComparatorWithPriority> node_sort;
  153. node_sort.sort(nodes, node_count);
  154. } else {
  155. SortArray<Node *, Node::Comparator> node_sort;
  156. node_sort.sort(nodes, node_count);
  157. }
  158. g.changed = false;
  159. }
  160. void SceneTree::call_group_flags(uint32_t p_call_flags, const StringName &p_group, const StringName &p_function, VARIANT_ARG_DECLARE) {
  161. Map<StringName, Group>::Element *E = group_map.find(p_group);
  162. if (!E)
  163. return;
  164. Group &g = E->get();
  165. if (g.nodes.empty())
  166. return;
  167. if (p_call_flags & GROUP_CALL_UNIQUE && !(p_call_flags & GROUP_CALL_REALTIME)) {
  168. ERR_FAIL_COND(ugc_locked);
  169. UGCall ug;
  170. ug.call = p_function;
  171. ug.group = p_group;
  172. if (unique_group_calls.has(ug))
  173. return;
  174. VARIANT_ARGPTRS;
  175. Vector<Variant> args;
  176. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  177. if (argptr[i]->get_type() == Variant::NIL)
  178. break;
  179. args.push_back(*argptr[i]);
  180. }
  181. unique_group_calls[ug] = args;
  182. return;
  183. }
  184. _update_group_order(g);
  185. Vector<Node *> nodes_copy = g.nodes;
  186. Node **nodes = nodes_copy.ptrw();
  187. int node_count = nodes_copy.size();
  188. call_lock++;
  189. if (p_call_flags & GROUP_CALL_REVERSE) {
  190. for (int i = node_count - 1; i >= 0; i--) {
  191. if (call_lock && call_skip.has(nodes[i]))
  192. continue;
  193. if (p_call_flags & GROUP_CALL_REALTIME) {
  194. if (p_call_flags & GROUP_CALL_MULTILEVEL)
  195. nodes[i]->call_multilevel(p_function, VARIANT_ARG_PASS);
  196. else
  197. nodes[i]->call(p_function, VARIANT_ARG_PASS);
  198. } else
  199. MessageQueue::get_singleton()->push_call(nodes[i], p_function, VARIANT_ARG_PASS);
  200. }
  201. } else {
  202. for (int i = 0; i < node_count; i++) {
  203. if (call_lock && call_skip.has(nodes[i]))
  204. continue;
  205. if (p_call_flags & GROUP_CALL_REALTIME) {
  206. if (p_call_flags & GROUP_CALL_MULTILEVEL)
  207. nodes[i]->call_multilevel(p_function, VARIANT_ARG_PASS);
  208. else
  209. nodes[i]->call(p_function, VARIANT_ARG_PASS);
  210. } else
  211. MessageQueue::get_singleton()->push_call(nodes[i], p_function, VARIANT_ARG_PASS);
  212. }
  213. }
  214. call_lock--;
  215. if (call_lock == 0)
  216. call_skip.clear();
  217. }
  218. void SceneTree::notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification) {
  219. Map<StringName, Group>::Element *E = group_map.find(p_group);
  220. if (!E)
  221. return;
  222. Group &g = E->get();
  223. if (g.nodes.empty())
  224. return;
  225. _update_group_order(g);
  226. Vector<Node *> nodes_copy = g.nodes;
  227. Node **nodes = nodes_copy.ptrw();
  228. int node_count = nodes_copy.size();
  229. call_lock++;
  230. if (p_call_flags & GROUP_CALL_REVERSE) {
  231. for (int i = node_count - 1; i >= 0; i--) {
  232. if (call_lock && call_skip.has(nodes[i]))
  233. continue;
  234. if (p_call_flags & GROUP_CALL_REALTIME)
  235. nodes[i]->notification(p_notification);
  236. else
  237. MessageQueue::get_singleton()->push_notification(nodes[i], p_notification);
  238. }
  239. } else {
  240. for (int i = 0; i < node_count; i++) {
  241. if (call_lock && call_skip.has(nodes[i]))
  242. continue;
  243. if (p_call_flags & GROUP_CALL_REALTIME)
  244. nodes[i]->notification(p_notification);
  245. else
  246. MessageQueue::get_singleton()->push_notification(nodes[i], p_notification);
  247. }
  248. }
  249. call_lock--;
  250. if (call_lock == 0)
  251. call_skip.clear();
  252. }
  253. void SceneTree::set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value) {
  254. Map<StringName, Group>::Element *E = group_map.find(p_group);
  255. if (!E)
  256. return;
  257. Group &g = E->get();
  258. if (g.nodes.empty())
  259. return;
  260. _update_group_order(g);
  261. Vector<Node *> nodes_copy = g.nodes;
  262. Node **nodes = nodes_copy.ptrw();
  263. int node_count = nodes_copy.size();
  264. call_lock++;
  265. if (p_call_flags & GROUP_CALL_REVERSE) {
  266. for (int i = node_count - 1; i >= 0; i--) {
  267. if (call_lock && call_skip.has(nodes[i]))
  268. continue;
  269. if (p_call_flags & GROUP_CALL_REALTIME)
  270. nodes[i]->set(p_name, p_value);
  271. else
  272. MessageQueue::get_singleton()->push_set(nodes[i], p_name, p_value);
  273. }
  274. } else {
  275. for (int i = 0; i < node_count; i++) {
  276. if (call_lock && call_skip.has(nodes[i]))
  277. continue;
  278. if (p_call_flags & GROUP_CALL_REALTIME)
  279. nodes[i]->set(p_name, p_value);
  280. else
  281. MessageQueue::get_singleton()->push_set(nodes[i], p_name, p_value);
  282. }
  283. }
  284. call_lock--;
  285. if (call_lock == 0)
  286. call_skip.clear();
  287. }
  288. void SceneTree::call_group(const StringName &p_group, const StringName &p_function, VARIANT_ARG_DECLARE) {
  289. call_group_flags(0, p_group, p_function, VARIANT_ARG_PASS);
  290. }
  291. void SceneTree::notify_group(const StringName &p_group, int p_notification) {
  292. notify_group_flags(0, p_group, p_notification);
  293. }
  294. void SceneTree::set_group(const StringName &p_group, const String &p_name, const Variant &p_value) {
  295. set_group_flags(0, p_group, p_name, p_value);
  296. }
  297. void SceneTree::set_input_as_handled() {
  298. input_handled = true;
  299. }
  300. void SceneTree::input_text(const String &p_text) {
  301. root_lock++;
  302. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "_vp_input_text", p_text); //special one for GUI, as controls use their own process check
  303. root_lock--;
  304. }
  305. bool SceneTree::is_input_handled() {
  306. return input_handled;
  307. }
  308. void SceneTree::input_event(const Ref<InputEvent> &p_event) {
  309. if (Engine::get_singleton()->is_editor_hint() && (Object::cast_to<InputEventJoypadButton>(p_event.ptr()) || Object::cast_to<InputEventJoypadMotion>(*p_event)))
  310. return; //avoid joy input on editor
  311. current_event++;
  312. root_lock++;
  313. input_handled = false;
  314. // Don't make const ref unless you can find and fix what caused GH-34691.
  315. Ref<InputEvent> ev = p_event;
  316. MainLoop::input_event(ev);
  317. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "_vp_input", ev); //special one for GUI, as controls use their own process check
  318. if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_remote()) {
  319. //quit from game window using F8
  320. Ref<InputEventKey> k = ev;
  321. if (k.is_valid() && k->is_pressed() && !k->is_echo() && k->get_scancode() == KEY_F8) {
  322. ScriptDebugger::get_singleton()->request_quit();
  323. }
  324. }
  325. _flush_ugc();
  326. root_lock--;
  327. //MessageQueue::get_singleton()->flush(); //flushing here causes UI and other places slowness
  328. root_lock++;
  329. if (!input_handled) {
  330. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "_vp_unhandled_input", ev); //special one for GUI, as controls use their own process check
  331. _flush_ugc();
  332. // input_handled = true; - no reason to set this as handled
  333. root_lock--;
  334. //MessageQueue::get_singleton()->flush(); //flushing here causes UI and other places slowness
  335. } else {
  336. // input_handled = true; - no reason to set this as handled
  337. root_lock--;
  338. }
  339. _call_idle_callbacks();
  340. }
  341. void SceneTree::init() {
  342. initialized = true;
  343. root->_set_tree(this);
  344. MainLoop::init();
  345. }
  346. bool SceneTree::iteration(float p_time) {
  347. root_lock++;
  348. current_frame++;
  349. flush_transform_notifications();
  350. MainLoop::iteration(p_time);
  351. physics_process_time = p_time;
  352. emit_signal("physics_frame");
  353. _notify_group_pause("physics_process_internal", Node::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  354. _notify_group_pause("physics_process", Node::NOTIFICATION_PHYSICS_PROCESS);
  355. _flush_ugc();
  356. MessageQueue::get_singleton()->flush(); //small little hack
  357. flush_transform_notifications();
  358. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "update_worlds");
  359. root_lock--;
  360. _flush_delete_queue();
  361. _call_idle_callbacks();
  362. return _quit;
  363. }
  364. void SceneTree::_update_font_oversampling(float p_ratio) {
  365. if (use_font_oversampling) {
  366. DynamicFontAtSize::font_oversampling = p_ratio;
  367. DynamicFont::update_oversampling();
  368. }
  369. }
  370. bool SceneTree::idle(float p_time) {
  371. //print_line("ram: "+itos(OS::get_singleton()->get_static_memory_usage())+" sram: "+itos(OS::get_singleton()->get_dynamic_memory_usage()));
  372. //print_line("node count: "+itos(get_node_count()));
  373. //print_line("TEXTURE RAM: "+itos(VS::get_singleton()->get_render_info(VS::INFO_TEXTURE_MEM_USED)));
  374. root_lock++;
  375. MainLoop::idle(p_time);
  376. idle_process_time = p_time;
  377. if (multiplayer_poll) {
  378. multiplayer->poll();
  379. }
  380. emit_signal("idle_frame");
  381. MessageQueue::get_singleton()->flush(); //small little hack
  382. flush_transform_notifications();
  383. _notify_group_pause("idle_process_internal", Node::NOTIFICATION_INTERNAL_PROCESS);
  384. _notify_group_pause("idle_process", Node::NOTIFICATION_PROCESS);
  385. Size2 win_size = Size2(OS::get_singleton()->get_window_size().width, OS::get_singleton()->get_window_size().height);
  386. if (win_size != last_screen_size) {
  387. last_screen_size = win_size;
  388. _update_root_rect();
  389. emit_signal("screen_resized");
  390. }
  391. _flush_ugc();
  392. MessageQueue::get_singleton()->flush(); //small little hack
  393. flush_transform_notifications(); //transforms after world update, to avoid unnecessary enter/exit notifications
  394. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "update_worlds");
  395. root_lock--;
  396. _flush_delete_queue();
  397. //go through timers
  398. List<Ref<SceneTreeTimer> >::Element *L = timers.back(); //last element
  399. for (List<Ref<SceneTreeTimer> >::Element *E = timers.front(); E;) {
  400. List<Ref<SceneTreeTimer> >::Element *N = E->next();
  401. if (pause && !E->get()->is_pause_mode_process()) {
  402. if (E == L) {
  403. break; //break on last, so if new timers were added during list traversal, ignore them.
  404. }
  405. E = N;
  406. continue;
  407. }
  408. float time_left = E->get()->get_time_left();
  409. time_left -= p_time;
  410. E->get()->set_time_left(time_left);
  411. if (time_left < 0) {
  412. E->get()->emit_signal("timeout");
  413. timers.erase(E);
  414. }
  415. if (E == L) {
  416. break; //break on last, so if new timers were added during list traversal, ignore them.
  417. }
  418. E = N;
  419. }
  420. flush_transform_notifications(); //additional transforms after timers update
  421. _call_idle_callbacks();
  422. #ifdef TOOLS_ENABLED
  423. if (Engine::get_singleton()->is_editor_hint()) {
  424. //simple hack to reload fallback environment if it changed from editor
  425. String env_path = ProjectSettings::get_singleton()->get("rendering/environment/default_environment");
  426. env_path = env_path.strip_edges(); //user may have added a space or two
  427. String cpath;
  428. Ref<Environment> fallback = get_root()->get_world()->get_fallback_environment();
  429. if (fallback.is_valid()) {
  430. cpath = fallback->get_path();
  431. }
  432. if (cpath != env_path) {
  433. if (env_path != String()) {
  434. fallback = ResourceLoader::load(env_path);
  435. if (fallback.is_null()) {
  436. //could not load fallback, set as empty
  437. ProjectSettings::get_singleton()->set("rendering/environment/default_environment", "");
  438. }
  439. } else {
  440. fallback.unref();
  441. }
  442. get_root()->get_world()->set_fallback_environment(fallback);
  443. }
  444. }
  445. #endif
  446. return _quit;
  447. }
  448. void SceneTree::finish() {
  449. _flush_delete_queue();
  450. _flush_ugc();
  451. initialized = false;
  452. MainLoop::finish();
  453. if (root) {
  454. root->_set_tree(NULL);
  455. root->_propagate_after_exit_tree();
  456. memdelete(root); //delete root
  457. root = NULL;
  458. }
  459. // cleanup timers
  460. for (List<Ref<SceneTreeTimer> >::Element *E = timers.front(); E; E = E->next()) {
  461. E->get()->release_connections();
  462. }
  463. timers.clear();
  464. }
  465. void SceneTree::quit(int p_exit_code) {
  466. if (p_exit_code >= 0) {
  467. // Override the exit code if a positive argument is given (the default is `-1`).
  468. // This is a shorthand for calling `set_exit_code()` on the OS singleton then quitting.
  469. OS::get_singleton()->set_exit_code(p_exit_code);
  470. }
  471. _quit = true;
  472. }
  473. void SceneTree::_notification(int p_notification) {
  474. switch (p_notification) {
  475. case NOTIFICATION_WM_QUIT_REQUEST: {
  476. get_root()->propagate_notification(p_notification);
  477. if (accept_quit) {
  478. _quit = true;
  479. break;
  480. }
  481. } break;
  482. case NOTIFICATION_WM_GO_BACK_REQUEST: {
  483. get_root()->propagate_notification(p_notification);
  484. if (quit_on_go_back) {
  485. _quit = true;
  486. break;
  487. }
  488. } break;
  489. case NOTIFICATION_WM_FOCUS_IN: {
  490. InputDefault *id = Object::cast_to<InputDefault>(Input::get_singleton());
  491. if (id) {
  492. id->ensure_touch_mouse_raised();
  493. }
  494. get_root()->propagate_notification(p_notification);
  495. } break;
  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_WM_UNFOCUS_REQUEST: {
  502. notify_group_flags(GROUP_CALL_REALTIME | GROUP_CALL_MULTILEVEL, "input", NOTIFICATION_WM_UNFOCUS_REQUEST);
  503. get_root()->propagate_notification(p_notification);
  504. } break;
  505. case NOTIFICATION_OS_MEMORY_WARNING:
  506. case NOTIFICATION_OS_IME_UPDATE:
  507. case NOTIFICATION_WM_MOUSE_ENTER:
  508. case NOTIFICATION_WM_MOUSE_EXIT:
  509. case NOTIFICATION_WM_FOCUS_OUT:
  510. case NOTIFICATION_WM_ABOUT:
  511. case NOTIFICATION_CRASH:
  512. case NOTIFICATION_APP_RESUMED:
  513. case NOTIFICATION_APP_PAUSED: {
  514. get_root()->propagate_notification(p_notification);
  515. } break;
  516. default:
  517. break;
  518. };
  519. };
  520. void SceneTree::set_auto_accept_quit(bool p_enable) {
  521. accept_quit = p_enable;
  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_a_parent_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_navigation_hint(bool p_enabled) {
  539. debug_navigation_hint = p_enabled;
  540. }
  541. bool SceneTree::is_debugging_navigation_hint() const {
  542. return debug_navigation_hint;
  543. }
  544. #endif
  545. void SceneTree::set_debug_collisions_color(const Color &p_color) {
  546. debug_collisions_color = p_color;
  547. }
  548. Color SceneTree::get_debug_collisions_color() const {
  549. return debug_collisions_color;
  550. }
  551. void SceneTree::set_debug_collision_contact_color(const Color &p_color) {
  552. debug_collision_contact_color = p_color;
  553. }
  554. Color SceneTree::get_debug_collision_contact_color() const {
  555. return debug_collision_contact_color;
  556. }
  557. void SceneTree::set_debug_navigation_color(const Color &p_color) {
  558. debug_navigation_color = p_color;
  559. }
  560. Color SceneTree::get_debug_navigation_color() const {
  561. return debug_navigation_color;
  562. }
  563. void SceneTree::set_debug_navigation_disabled_color(const Color &p_color) {
  564. debug_navigation_disabled_color = p_color;
  565. }
  566. Color SceneTree::get_debug_navigation_disabled_color() const {
  567. return debug_navigation_disabled_color;
  568. }
  569. Ref<Material> SceneTree::get_debug_navigation_material() {
  570. if (navigation_material.is_valid())
  571. return navigation_material;
  572. Ref<SpatialMaterial> line_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  573. line_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  574. line_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  575. line_material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  576. line_material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  577. line_material->set_albedo(get_debug_navigation_color());
  578. navigation_material = line_material;
  579. return navigation_material;
  580. }
  581. Ref<Material> SceneTree::get_debug_navigation_disabled_material() {
  582. if (navigation_disabled_material.is_valid())
  583. return navigation_disabled_material;
  584. Ref<SpatialMaterial> line_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  585. line_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  586. line_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  587. line_material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  588. line_material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  589. line_material->set_albedo(get_debug_navigation_disabled_color());
  590. navigation_disabled_material = line_material;
  591. return navigation_disabled_material;
  592. }
  593. Ref<Material> SceneTree::get_debug_collision_material() {
  594. if (collision_material.is_valid())
  595. return collision_material;
  596. Ref<SpatialMaterial> line_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  597. line_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  598. line_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  599. line_material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  600. line_material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  601. line_material->set_albedo(get_debug_collisions_color());
  602. collision_material = line_material;
  603. return collision_material;
  604. }
  605. Ref<ArrayMesh> SceneTree::get_debug_contact_mesh() {
  606. if (debug_contact_mesh.is_valid())
  607. return debug_contact_mesh;
  608. debug_contact_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  609. Ref<SpatialMaterial> mat = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  610. mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  611. mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  612. mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  613. mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  614. mat->set_albedo(get_debug_collision_contact_color());
  615. Vector3 diamond[6] = {
  616. Vector3(-1, 0, 0),
  617. Vector3(1, 0, 0),
  618. Vector3(0, -1, 0),
  619. Vector3(0, 1, 0),
  620. Vector3(0, 0, -1),
  621. Vector3(0, 0, 1)
  622. };
  623. /* clang-format off */
  624. int diamond_faces[8 * 3] = {
  625. 0, 2, 4,
  626. 0, 3, 4,
  627. 1, 2, 4,
  628. 1, 3, 4,
  629. 0, 2, 5,
  630. 0, 3, 5,
  631. 1, 2, 5,
  632. 1, 3, 5,
  633. };
  634. /* clang-format on */
  635. PoolVector<int> indices;
  636. for (int i = 0; i < 8 * 3; i++)
  637. indices.push_back(diamond_faces[i]);
  638. PoolVector<Vector3> vertices;
  639. for (int i = 0; i < 6; i++)
  640. vertices.push_back(diamond[i] * 0.1);
  641. Array arr;
  642. arr.resize(Mesh::ARRAY_MAX);
  643. arr[Mesh::ARRAY_VERTEX] = vertices;
  644. arr[Mesh::ARRAY_INDEX] = indices;
  645. debug_contact_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
  646. debug_contact_mesh->surface_set_material(0, mat);
  647. return debug_contact_mesh;
  648. }
  649. void SceneTree::set_pause(bool p_enabled) {
  650. if (p_enabled == pause)
  651. return;
  652. pause = p_enabled;
  653. PhysicsServer::get_singleton()->set_active(!p_enabled);
  654. Physics2DServer::get_singleton()->set_active(!p_enabled);
  655. if (get_root())
  656. get_root()->propagate_notification(p_enabled ? Node::NOTIFICATION_PAUSED : Node::NOTIFICATION_UNPAUSED);
  657. }
  658. bool SceneTree::is_paused() const {
  659. return pause;
  660. }
  661. void SceneTree::_call_input_pause(const StringName &p_group, const StringName &p_method, const Ref<InputEvent> &p_input) {
  662. Map<StringName, Group>::Element *E = group_map.find(p_group);
  663. if (!E)
  664. return;
  665. Group &g = E->get();
  666. if (g.nodes.empty())
  667. return;
  668. _update_group_order(g);
  669. //copy, so copy on write happens in case something is removed from process while being called
  670. //performance is not lost because only if something is added/removed the vector is copied.
  671. Vector<Node *> nodes_copy = g.nodes;
  672. int node_count = nodes_copy.size();
  673. Node **nodes = nodes_copy.ptrw();
  674. Variant arg = p_input;
  675. const Variant *v[1] = { &arg };
  676. call_lock++;
  677. for (int i = node_count - 1; i >= 0; i--) {
  678. if (input_handled)
  679. break;
  680. Node *n = nodes[i];
  681. if (call_lock && call_skip.has(n))
  682. continue;
  683. if (!n->can_process())
  684. continue;
  685. n->call_multilevel(p_method, (const Variant **)v, 1);
  686. //ERR_FAIL_COND(node_count != g.nodes.size());
  687. }
  688. call_lock--;
  689. if (call_lock == 0)
  690. call_skip.clear();
  691. }
  692. void SceneTree::_notify_group_pause(const StringName &p_group, int p_notification) {
  693. Map<StringName, Group>::Element *E = group_map.find(p_group);
  694. if (!E)
  695. return;
  696. Group &g = E->get();
  697. if (g.nodes.empty())
  698. return;
  699. _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);
  700. //copy, so copy on write happens in case something is removed from process while being called
  701. //performance is not lost because only if something is added/removed the vector is copied.
  702. Vector<Node *> nodes_copy = g.nodes;
  703. int node_count = nodes_copy.size();
  704. Node **nodes = nodes_copy.ptrw();
  705. call_lock++;
  706. for (int i = 0; i < node_count; i++) {
  707. Node *n = nodes[i];
  708. if (call_lock && call_skip.has(n))
  709. continue;
  710. if (!n->can_process())
  711. continue;
  712. if (!n->can_process_notification(p_notification))
  713. continue;
  714. n->notification(p_notification);
  715. //ERR_FAIL_COND(node_count != g.nodes.size());
  716. }
  717. call_lock--;
  718. if (call_lock == 0)
  719. call_skip.clear();
  720. }
  721. /*
  722. void SceneMainLoop::_update_listener_2d() {
  723. if (listener_2d.is_valid()) {
  724. SpatialSound2DServer::get_singleton()->listener_set_space( listener_2d, world_2d->get_sound_space() );
  725. }
  726. }
  727. */
  728. Variant SceneTree::_call_group_flags(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  729. r_error.error = Variant::CallError::CALL_OK;
  730. ERR_FAIL_COND_V(p_argcount < 3, Variant());
  731. ERR_FAIL_COND_V(!p_args[0]->is_num(), Variant());
  732. ERR_FAIL_COND_V(p_args[1]->get_type() != Variant::STRING, Variant());
  733. ERR_FAIL_COND_V(p_args[2]->get_type() != Variant::STRING, Variant());
  734. int flags = *p_args[0];
  735. StringName group = *p_args[1];
  736. StringName method = *p_args[2];
  737. Variant v[VARIANT_ARG_MAX];
  738. for (int i = 0; i < MIN(p_argcount - 3, 5); i++) {
  739. v[i] = *p_args[i + 3];
  740. }
  741. call_group_flags(flags, group, method, v[0], v[1], v[2], v[3], v[4]);
  742. return Variant();
  743. }
  744. Variant SceneTree::_call_group(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  745. r_error.error = Variant::CallError::CALL_OK;
  746. ERR_FAIL_COND_V(p_argcount < 2, Variant());
  747. ERR_FAIL_COND_V(p_args[0]->get_type() != Variant::STRING, Variant());
  748. ERR_FAIL_COND_V(p_args[1]->get_type() != Variant::STRING, Variant());
  749. StringName group = *p_args[0];
  750. StringName method = *p_args[1];
  751. Variant v[VARIANT_ARG_MAX];
  752. for (int i = 0; i < MIN(p_argcount - 2, 5); i++) {
  753. v[i] = *p_args[i + 2];
  754. }
  755. call_group_flags(0, group, method, v[0], v[1], v[2], v[3], v[4]);
  756. return Variant();
  757. }
  758. int64_t SceneTree::get_frame() const {
  759. return current_frame;
  760. }
  761. int64_t SceneTree::get_event_count() const {
  762. return current_event;
  763. }
  764. Array SceneTree::_get_nodes_in_group(const StringName &p_group) {
  765. Array ret;
  766. Map<StringName, Group>::Element *E = group_map.find(p_group);
  767. if (!E)
  768. return ret;
  769. _update_group_order(E->get()); //update order just in case
  770. int nc = E->get().nodes.size();
  771. if (nc == 0)
  772. return ret;
  773. ret.resize(nc);
  774. Node **ptr = E->get().nodes.ptrw();
  775. for (int i = 0; i < nc; i++) {
  776. ret[i] = ptr[i];
  777. }
  778. return ret;
  779. }
  780. bool SceneTree::has_group(const StringName &p_identifier) const {
  781. return group_map.has(p_identifier);
  782. }
  783. void SceneTree::get_nodes_in_group(const StringName &p_group, List<Node *> *p_list) {
  784. Map<StringName, Group>::Element *E = group_map.find(p_group);
  785. if (!E)
  786. return;
  787. _update_group_order(E->get()); //update order just in case
  788. int nc = E->get().nodes.size();
  789. if (nc == 0)
  790. return;
  791. Node **ptr = E->get().nodes.ptrw();
  792. for (int i = 0; i < nc; i++) {
  793. p_list->push_back(ptr[i]);
  794. }
  795. }
  796. void SceneTree::_flush_delete_queue() {
  797. _THREAD_SAFE_METHOD_
  798. while (delete_queue.size()) {
  799. Object *obj = ObjectDB::get_instance(delete_queue.front()->get());
  800. if (obj) {
  801. memdelete(obj);
  802. }
  803. delete_queue.pop_front();
  804. }
  805. }
  806. void SceneTree::queue_delete(Object *p_object) {
  807. _THREAD_SAFE_METHOD_
  808. ERR_FAIL_NULL(p_object);
  809. p_object->_is_queued_for_deletion = true;
  810. delete_queue.push_back(p_object->get_instance_id());
  811. }
  812. int SceneTree::get_node_count() const {
  813. return node_count;
  814. }
  815. void SceneTree::_update_root_rect() {
  816. if (stretch_mode == STRETCH_MODE_DISABLED) {
  817. _update_font_oversampling(1.0);
  818. root->set_size((last_screen_size / stretch_shrink).floor());
  819. root->set_attach_to_screen_rect(Rect2(Point2(), last_screen_size));
  820. root->set_size_override_stretch(false);
  821. root->set_size_override(false, Size2());
  822. root->update_canvas_items();
  823. return; //user will take care
  824. }
  825. //actual screen video mode
  826. Size2 video_mode = Size2(OS::get_singleton()->get_window_size().width, OS::get_singleton()->get_window_size().height);
  827. Size2 desired_res = stretch_min;
  828. Size2 viewport_size;
  829. Size2 screen_size;
  830. float viewport_aspect = desired_res.aspect();
  831. float video_mode_aspect = video_mode.aspect();
  832. if (use_font_oversampling && stretch_aspect == STRETCH_ASPECT_IGNORE) {
  833. WARN_PRINT("Font oversampling only works with the resize modes 'Keep Width', 'Keep Height', and 'Expand'.");
  834. }
  835. if (stretch_aspect == STRETCH_ASPECT_IGNORE || Math::is_equal_approx(viewport_aspect, video_mode_aspect)) {
  836. //same aspect or ignore aspect
  837. viewport_size = desired_res;
  838. screen_size = video_mode;
  839. } else if (viewport_aspect < video_mode_aspect) {
  840. // screen ratio is smaller vertically
  841. if (stretch_aspect == STRETCH_ASPECT_KEEP_HEIGHT || stretch_aspect == STRETCH_ASPECT_EXPAND) {
  842. //will stretch horizontally
  843. viewport_size.x = desired_res.y * video_mode_aspect;
  844. viewport_size.y = desired_res.y;
  845. screen_size = video_mode;
  846. } else {
  847. //will need black bars
  848. viewport_size = desired_res;
  849. screen_size.x = video_mode.y * viewport_aspect;
  850. screen_size.y = video_mode.y;
  851. }
  852. } else {
  853. //screen ratio is smaller horizontally
  854. if (stretch_aspect == STRETCH_ASPECT_KEEP_WIDTH || stretch_aspect == STRETCH_ASPECT_EXPAND) {
  855. //will stretch horizontally
  856. viewport_size.x = desired_res.x;
  857. viewport_size.y = desired_res.x / video_mode_aspect;
  858. screen_size = video_mode;
  859. } else {
  860. //will need black bars
  861. viewport_size = desired_res;
  862. screen_size.x = video_mode.x;
  863. screen_size.y = video_mode.x / viewport_aspect;
  864. }
  865. }
  866. screen_size = screen_size.floor();
  867. viewport_size = viewport_size.floor();
  868. Size2 margin;
  869. Size2 offset;
  870. //black bars and margin
  871. if (stretch_aspect != STRETCH_ASPECT_EXPAND && screen_size.x < video_mode.x) {
  872. margin.x = Math::round((video_mode.x - screen_size.x) / 2.0);
  873. VisualServer::get_singleton()->black_bars_set_margins(margin.x, 0, margin.x, 0);
  874. offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
  875. } else if (stretch_aspect != STRETCH_ASPECT_EXPAND && screen_size.y < video_mode.y) {
  876. margin.y = Math::round((video_mode.y - screen_size.y) / 2.0);
  877. VisualServer::get_singleton()->black_bars_set_margins(0, margin.y, 0, margin.y);
  878. offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
  879. } else {
  880. VisualServer::get_singleton()->black_bars_set_margins(0, 0, 0, 0);
  881. }
  882. switch (stretch_mode) {
  883. case STRETCH_MODE_DISABLED: {
  884. // Already handled above
  885. _update_font_oversampling(1.0);
  886. } break;
  887. case STRETCH_MODE_2D: {
  888. _update_font_oversampling(screen_size.x / viewport_size.x); //screen / viewport radio drives oversampling
  889. root->set_size((screen_size / stretch_shrink).floor());
  890. root->set_attach_to_screen_rect(Rect2(margin, screen_size));
  891. root->set_size_override_stretch(true);
  892. root->set_size_override(true, (viewport_size / stretch_shrink).floor());
  893. root->update_canvas_items(); //force them to update just in case
  894. } break;
  895. case STRETCH_MODE_VIEWPORT: {
  896. _update_font_oversampling(1.0);
  897. root->set_size((viewport_size / stretch_shrink).floor());
  898. root->set_attach_to_screen_rect(Rect2(margin, screen_size));
  899. root->set_size_override_stretch(false);
  900. root->set_size_override(false, Size2());
  901. root->update_canvas_items(); //force them to update just in case
  902. if (use_font_oversampling) {
  903. WARN_PRINT("Font oversampling does not work in 'Viewport' stretch mode, only '2D'.");
  904. }
  905. } break;
  906. }
  907. }
  908. void SceneTree::set_screen_stretch(StretchMode p_mode, StretchAspect p_aspect, const Size2 &p_minsize, real_t p_shrink) {
  909. stretch_mode = p_mode;
  910. stretch_aspect = p_aspect;
  911. stretch_min = p_minsize;
  912. stretch_shrink = p_shrink;
  913. _update_root_rect();
  914. }
  915. void SceneTree::set_edited_scene_root(Node *p_node) {
  916. #ifdef TOOLS_ENABLED
  917. edited_scene_root = p_node;
  918. #endif
  919. }
  920. Node *SceneTree::get_edited_scene_root() const {
  921. #ifdef TOOLS_ENABLED
  922. return edited_scene_root;
  923. #else
  924. return NULL;
  925. #endif
  926. }
  927. void SceneTree::set_current_scene(Node *p_scene) {
  928. ERR_FAIL_COND(p_scene && p_scene->get_parent() != root);
  929. current_scene = p_scene;
  930. }
  931. Node *SceneTree::get_current_scene() const {
  932. return current_scene;
  933. }
  934. void SceneTree::_change_scene(Node *p_to) {
  935. if (current_scene) {
  936. memdelete(current_scene);
  937. current_scene = NULL;
  938. }
  939. // If we're quitting, abort.
  940. if (unlikely(_quit)) {
  941. if (p_to) { // Prevent memory leak.
  942. memdelete(p_to);
  943. }
  944. return;
  945. }
  946. if (p_to) {
  947. current_scene = p_to;
  948. root->add_child(p_to);
  949. }
  950. }
  951. Error SceneTree::change_scene(const String &p_path) {
  952. Ref<PackedScene> new_scene = ResourceLoader::load(p_path);
  953. if (new_scene.is_null())
  954. return ERR_CANT_OPEN;
  955. return change_scene_to(new_scene);
  956. }
  957. Error SceneTree::change_scene_to(const Ref<PackedScene> &p_scene) {
  958. Node *new_scene = NULL;
  959. if (p_scene.is_valid()) {
  960. new_scene = p_scene->instance();
  961. ERR_FAIL_COND_V(!new_scene, ERR_CANT_CREATE);
  962. }
  963. call_deferred("_change_scene", new_scene);
  964. return OK;
  965. }
  966. Error SceneTree::reload_current_scene() {
  967. ERR_FAIL_COND_V(!current_scene, ERR_UNCONFIGURED);
  968. String fname = current_scene->get_filename();
  969. return change_scene(fname);
  970. }
  971. void SceneTree::add_current_scene(Node *p_current) {
  972. current_scene = p_current;
  973. root->add_child(p_current);
  974. }
  975. #ifdef DEBUG_ENABLED
  976. static void _fill_array(Node *p_node, Array &array, int p_level) {
  977. array.push_back(p_node->get_child_count());
  978. array.push_back(p_node->get_name());
  979. array.push_back(p_node->get_class());
  980. array.push_back(p_node->get_instance_id());
  981. for (int i = 0; i < p_node->get_child_count(); i++) {
  982. _fill_array(p_node->get_child(i), array, p_level + 1);
  983. }
  984. }
  985. void SceneTree::_debugger_request_tree() {
  986. Array arr;
  987. _fill_array(root, arr, 0);
  988. ScriptDebugger::get_singleton()->send_message("scene_tree", arr);
  989. }
  990. void SceneTree::_live_edit_node_path_func(const NodePath &p_path, int p_id) {
  991. live_edit_node_path_cache[p_id] = p_path;
  992. }
  993. void SceneTree::_live_edit_res_path_func(const String &p_path, int p_id) {
  994. live_edit_resource_cache[p_id] = p_path;
  995. }
  996. void SceneTree::_live_edit_node_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  997. if (!live_edit_node_path_cache.has(p_id))
  998. return;
  999. NodePath np = live_edit_node_path_cache[p_id];
  1000. Node *base = NULL;
  1001. if (root->has_node(live_edit_root))
  1002. base = root->get_node(live_edit_root);
  1003. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1004. if (!E)
  1005. return; //scene not editable
  1006. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1007. Node *n = F->get();
  1008. if (base && !base->is_a_parent_of(n))
  1009. continue;
  1010. if (!n->has_node(np))
  1011. continue;
  1012. Node *n2 = n->get_node(np);
  1013. n2->set(p_prop, p_value);
  1014. }
  1015. }
  1016. void SceneTree::_live_edit_node_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  1017. RES r = ResourceLoader::load(p_value);
  1018. if (!r.is_valid())
  1019. return;
  1020. _live_edit_node_set_func(p_id, p_prop, r);
  1021. }
  1022. void SceneTree::_live_edit_node_call_func(int p_id, const StringName &p_method, VARIANT_ARG_DECLARE) {
  1023. if (!live_edit_node_path_cache.has(p_id))
  1024. return;
  1025. NodePath np = live_edit_node_path_cache[p_id];
  1026. Node *base = NULL;
  1027. if (root->has_node(live_edit_root))
  1028. base = root->get_node(live_edit_root);
  1029. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1030. if (!E)
  1031. return; //scene not editable
  1032. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1033. Node *n = F->get();
  1034. if (base && !base->is_a_parent_of(n))
  1035. continue;
  1036. if (!n->has_node(np))
  1037. continue;
  1038. Node *n2 = n->get_node(np);
  1039. n2->call(p_method, VARIANT_ARG_PASS);
  1040. }
  1041. }
  1042. void SceneTree::_live_edit_res_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  1043. if (!live_edit_resource_cache.has(p_id))
  1044. return;
  1045. String resp = live_edit_resource_cache[p_id];
  1046. if (!ResourceCache::has(resp))
  1047. return;
  1048. RES r = ResourceCache::get(resp);
  1049. if (!r.is_valid())
  1050. return;
  1051. r->set(p_prop, p_value);
  1052. }
  1053. void SceneTree::_live_edit_res_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  1054. RES r = ResourceLoader::load(p_value);
  1055. if (!r.is_valid())
  1056. return;
  1057. _live_edit_res_set_func(p_id, p_prop, r);
  1058. }
  1059. void SceneTree::_live_edit_res_call_func(int p_id, const StringName &p_method, VARIANT_ARG_DECLARE) {
  1060. if (!live_edit_resource_cache.has(p_id))
  1061. return;
  1062. String resp = live_edit_resource_cache[p_id];
  1063. if (!ResourceCache::has(resp))
  1064. return;
  1065. RES r = ResourceCache::get(resp);
  1066. if (!r.is_valid())
  1067. return;
  1068. r->call(p_method, VARIANT_ARG_PASS);
  1069. }
  1070. void SceneTree::_live_edit_root_func(const NodePath &p_scene_path, const String &p_scene_from) {
  1071. live_edit_root = p_scene_path;
  1072. live_edit_scene = p_scene_from;
  1073. }
  1074. void SceneTree::_live_edit_create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name) {
  1075. Node *base = NULL;
  1076. if (root->has_node(live_edit_root))
  1077. base = root->get_node(live_edit_root);
  1078. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1079. if (!E)
  1080. return; //scene not editable
  1081. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1082. Node *n = F->get();
  1083. if (base && !base->is_a_parent_of(n))
  1084. continue;
  1085. if (!n->has_node(p_parent))
  1086. continue;
  1087. Node *n2 = n->get_node(p_parent);
  1088. Node *no = Object::cast_to<Node>(ClassDB::instance(p_type));
  1089. if (!no) {
  1090. continue;
  1091. }
  1092. no->set_name(p_name);
  1093. n2->add_child(no);
  1094. }
  1095. }
  1096. void SceneTree::_live_edit_instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name) {
  1097. Ref<PackedScene> ps = ResourceLoader::load(p_path);
  1098. if (!ps.is_valid())
  1099. return;
  1100. Node *base = NULL;
  1101. if (root->has_node(live_edit_root))
  1102. base = root->get_node(live_edit_root);
  1103. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1104. if (!E)
  1105. return; //scene not editable
  1106. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1107. Node *n = F->get();
  1108. if (base && !base->is_a_parent_of(n))
  1109. continue;
  1110. if (!n->has_node(p_parent))
  1111. continue;
  1112. Node *n2 = n->get_node(p_parent);
  1113. Node *no = ps->instance();
  1114. if (!no) {
  1115. continue;
  1116. }
  1117. no->set_name(p_name);
  1118. n2->add_child(no);
  1119. }
  1120. }
  1121. void SceneTree::_live_edit_remove_node_func(const NodePath &p_at) {
  1122. Node *base = NULL;
  1123. if (root->has_node(live_edit_root))
  1124. base = root->get_node(live_edit_root);
  1125. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1126. if (!E)
  1127. return; //scene not editable
  1128. for (Set<Node *>::Element *F = E->get().front(); F;) {
  1129. Set<Node *>::Element *N = F->next();
  1130. Node *n = F->get();
  1131. if (base && !base->is_a_parent_of(n))
  1132. continue;
  1133. if (!n->has_node(p_at))
  1134. continue;
  1135. Node *n2 = n->get_node(p_at);
  1136. memdelete(n2);
  1137. F = N;
  1138. }
  1139. }
  1140. void SceneTree::_live_edit_remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id) {
  1141. Node *base = NULL;
  1142. if (root->has_node(live_edit_root))
  1143. base = root->get_node(live_edit_root);
  1144. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1145. if (!E)
  1146. return; //scene not editable
  1147. for (Set<Node *>::Element *F = E->get().front(); F;) {
  1148. Set<Node *>::Element *N = F->next();
  1149. Node *n = F->get();
  1150. if (base && !base->is_a_parent_of(n))
  1151. continue;
  1152. if (!n->has_node(p_at))
  1153. continue;
  1154. Node *n2 = n->get_node(p_at);
  1155. n2->get_parent()->remove_child(n2);
  1156. live_edit_remove_list[n][p_keep_id] = n2;
  1157. F = N;
  1158. }
  1159. }
  1160. void SceneTree::_live_edit_restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  1161. Node *base = NULL;
  1162. if (root->has_node(live_edit_root))
  1163. base = root->get_node(live_edit_root);
  1164. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1165. if (!E)
  1166. return; //scene not editable
  1167. for (Set<Node *>::Element *F = E->get().front(); F;) {
  1168. Set<Node *>::Element *N = F->next();
  1169. Node *n = F->get();
  1170. if (base && !base->is_a_parent_of(n))
  1171. continue;
  1172. if (!n->has_node(p_at))
  1173. continue;
  1174. Node *n2 = n->get_node(p_at);
  1175. Map<Node *, Map<ObjectID, Node *> >::Element *EN = live_edit_remove_list.find(n);
  1176. if (!EN)
  1177. continue;
  1178. Map<ObjectID, Node *>::Element *FN = EN->get().find(p_id);
  1179. if (!FN)
  1180. continue;
  1181. n2->add_child(FN->get());
  1182. EN->get().erase(FN);
  1183. if (EN->get().size() == 0) {
  1184. live_edit_remove_list.erase(EN);
  1185. }
  1186. F = N;
  1187. }
  1188. }
  1189. void SceneTree::_live_edit_duplicate_node_func(const NodePath &p_at, const String &p_new_name) {
  1190. Node *base = NULL;
  1191. if (root->has_node(live_edit_root))
  1192. base = root->get_node(live_edit_root);
  1193. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1194. if (!E)
  1195. return; //scene not editable
  1196. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1197. Node *n = F->get();
  1198. if (base && !base->is_a_parent_of(n))
  1199. continue;
  1200. if (!n->has_node(p_at))
  1201. continue;
  1202. Node *n2 = n->get_node(p_at);
  1203. Node *dup = n2->duplicate(Node::DUPLICATE_SIGNALS | Node::DUPLICATE_GROUPS | Node::DUPLICATE_SCRIPTS);
  1204. if (!dup)
  1205. continue;
  1206. dup->set_name(p_new_name);
  1207. n2->get_parent()->add_child(dup);
  1208. }
  1209. }
  1210. void SceneTree::_live_edit_reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  1211. Node *base = NULL;
  1212. if (root->has_node(live_edit_root))
  1213. base = root->get_node(live_edit_root);
  1214. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1215. if (!E)
  1216. return; //scene not editable
  1217. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1218. Node *n = F->get();
  1219. if (base && !base->is_a_parent_of(n))
  1220. continue;
  1221. if (!n->has_node(p_at))
  1222. continue;
  1223. Node *nfrom = n->get_node(p_at);
  1224. if (!n->has_node(p_new_place))
  1225. continue;
  1226. Node *nto = n->get_node(p_new_place);
  1227. nfrom->get_parent()->remove_child(nfrom);
  1228. nfrom->set_name(p_new_name);
  1229. nto->add_child(nfrom);
  1230. if (p_at_pos >= 0)
  1231. nto->move_child(nfrom, p_at_pos);
  1232. }
  1233. }
  1234. #endif
  1235. void SceneTree::drop_files(const Vector<String> &p_files, int p_from_screen) {
  1236. emit_signal("files_dropped", p_files, p_from_screen);
  1237. MainLoop::drop_files(p_files, p_from_screen);
  1238. }
  1239. void SceneTree::global_menu_action(const Variant &p_id, const Variant &p_meta) {
  1240. emit_signal("global_menu_action", p_id, p_meta);
  1241. MainLoop::global_menu_action(p_id, p_meta);
  1242. }
  1243. Ref<SceneTreeTimer> SceneTree::create_timer(float p_delay_sec, bool p_process_pause) {
  1244. Ref<SceneTreeTimer> stt;
  1245. stt.instance();
  1246. stt->set_pause_mode_process(p_process_pause);
  1247. stt->set_time_left(p_delay_sec);
  1248. timers.push_back(stt);
  1249. return stt;
  1250. }
  1251. void SceneTree::_network_peer_connected(int p_id) {
  1252. emit_signal("network_peer_connected", p_id);
  1253. }
  1254. void SceneTree::_network_peer_disconnected(int p_id) {
  1255. emit_signal("network_peer_disconnected", p_id);
  1256. }
  1257. void SceneTree::_connected_to_server() {
  1258. emit_signal("connected_to_server");
  1259. }
  1260. void SceneTree::_connection_failed() {
  1261. emit_signal("connection_failed");
  1262. }
  1263. void SceneTree::_server_disconnected() {
  1264. emit_signal("server_disconnected");
  1265. }
  1266. Ref<MultiplayerAPI> SceneTree::get_multiplayer() const {
  1267. return multiplayer;
  1268. }
  1269. void SceneTree::set_multiplayer_poll_enabled(bool p_enabled) {
  1270. multiplayer_poll = p_enabled;
  1271. }
  1272. bool SceneTree::is_multiplayer_poll_enabled() const {
  1273. return multiplayer_poll;
  1274. }
  1275. void SceneTree::set_multiplayer(Ref<MultiplayerAPI> p_multiplayer) {
  1276. ERR_FAIL_COND(!p_multiplayer.is_valid());
  1277. if (multiplayer.is_valid()) {
  1278. multiplayer->disconnect("network_peer_connected", this, "_network_peer_connected");
  1279. multiplayer->disconnect("network_peer_disconnected", this, "_network_peer_disconnected");
  1280. multiplayer->disconnect("connected_to_server", this, "_connected_to_server");
  1281. multiplayer->disconnect("connection_failed", this, "_connection_failed");
  1282. multiplayer->disconnect("server_disconnected", this, "_server_disconnected");
  1283. }
  1284. multiplayer = p_multiplayer;
  1285. multiplayer->set_root_node(root);
  1286. multiplayer->connect("network_peer_connected", this, "_network_peer_connected");
  1287. multiplayer->connect("network_peer_disconnected", this, "_network_peer_disconnected");
  1288. multiplayer->connect("connected_to_server", this, "_connected_to_server");
  1289. multiplayer->connect("connection_failed", this, "_connection_failed");
  1290. multiplayer->connect("server_disconnected", this, "_server_disconnected");
  1291. }
  1292. void SceneTree::set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_network_peer) {
  1293. multiplayer->set_network_peer(p_network_peer);
  1294. }
  1295. Ref<NetworkedMultiplayerPeer> SceneTree::get_network_peer() const {
  1296. return multiplayer->get_network_peer();
  1297. }
  1298. bool SceneTree::is_network_server() const {
  1299. return multiplayer->is_network_server();
  1300. }
  1301. bool SceneTree::has_network_peer() const {
  1302. return multiplayer->has_network_peer();
  1303. }
  1304. int SceneTree::get_network_unique_id() const {
  1305. return multiplayer->get_network_unique_id();
  1306. }
  1307. Vector<int> SceneTree::get_network_connected_peers() const {
  1308. return multiplayer->get_network_connected_peers();
  1309. }
  1310. int SceneTree::get_rpc_sender_id() const {
  1311. return multiplayer->get_rpc_sender_id();
  1312. }
  1313. void SceneTree::set_refuse_new_network_connections(bool p_refuse) {
  1314. multiplayer->set_refuse_new_network_connections(p_refuse);
  1315. }
  1316. bool SceneTree::is_refusing_new_network_connections() const {
  1317. return multiplayer->is_refusing_new_network_connections();
  1318. }
  1319. void SceneTree::_bind_methods() {
  1320. ClassDB::bind_method(D_METHOD("get_root"), &SceneTree::get_root);
  1321. ClassDB::bind_method(D_METHOD("has_group", "name"), &SceneTree::has_group);
  1322. ClassDB::bind_method(D_METHOD("set_auto_accept_quit", "enabled"), &SceneTree::set_auto_accept_quit);
  1323. ClassDB::bind_method(D_METHOD("set_quit_on_go_back", "enabled"), &SceneTree::set_quit_on_go_back);
  1324. ClassDB::bind_method(D_METHOD("set_debug_collisions_hint", "enable"), &SceneTree::set_debug_collisions_hint);
  1325. ClassDB::bind_method(D_METHOD("is_debugging_collisions_hint"), &SceneTree::is_debugging_collisions_hint);
  1326. ClassDB::bind_method(D_METHOD("set_debug_navigation_hint", "enable"), &SceneTree::set_debug_navigation_hint);
  1327. ClassDB::bind_method(D_METHOD("is_debugging_navigation_hint"), &SceneTree::is_debugging_navigation_hint);
  1328. ClassDB::bind_method(D_METHOD("set_edited_scene_root", "scene"), &SceneTree::set_edited_scene_root);
  1329. ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &SceneTree::get_edited_scene_root);
  1330. ClassDB::bind_method(D_METHOD("set_pause", "enable"), &SceneTree::set_pause);
  1331. ClassDB::bind_method(D_METHOD("is_paused"), &SceneTree::is_paused);
  1332. ClassDB::bind_method(D_METHOD("set_input_as_handled"), &SceneTree::set_input_as_handled);
  1333. ClassDB::bind_method(D_METHOD("is_input_handled"), &SceneTree::is_input_handled);
  1334. ClassDB::bind_method(D_METHOD("create_timer", "time_sec", "pause_mode_process"), &SceneTree::create_timer, DEFVAL(true));
  1335. ClassDB::bind_method(D_METHOD("get_node_count"), &SceneTree::get_node_count);
  1336. ClassDB::bind_method(D_METHOD("get_frame"), &SceneTree::get_frame);
  1337. ClassDB::bind_method(D_METHOD("quit", "exit_code"), &SceneTree::quit, DEFVAL(-1));
  1338. ClassDB::bind_method(D_METHOD("set_screen_stretch", "mode", "aspect", "minsize", "shrink"), &SceneTree::set_screen_stretch, DEFVAL(1));
  1339. ClassDB::bind_method(D_METHOD("queue_delete", "obj"), &SceneTree::queue_delete);
  1340. MethodInfo mi;
  1341. mi.name = "call_group_flags";
  1342. mi.arguments.push_back(PropertyInfo(Variant::INT, "flags"));
  1343. mi.arguments.push_back(PropertyInfo(Variant::STRING, "group"));
  1344. mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  1345. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group_flags", &SceneTree::_call_group_flags, mi);
  1346. ClassDB::bind_method(D_METHOD("notify_group_flags", "call_flags", "group", "notification"), &SceneTree::notify_group_flags);
  1347. ClassDB::bind_method(D_METHOD("set_group_flags", "call_flags", "group", "property", "value"), &SceneTree::set_group_flags);
  1348. MethodInfo mi2;
  1349. mi2.name = "call_group";
  1350. mi2.arguments.push_back(PropertyInfo(Variant::STRING, "group"));
  1351. mi2.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  1352. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group", &SceneTree::_call_group, mi2);
  1353. ClassDB::bind_method(D_METHOD("notify_group", "group", "notification"), &SceneTree::notify_group);
  1354. ClassDB::bind_method(D_METHOD("set_group", "group", "property", "value"), &SceneTree::set_group);
  1355. ClassDB::bind_method(D_METHOD("get_nodes_in_group", "group"), &SceneTree::_get_nodes_in_group);
  1356. ClassDB::bind_method(D_METHOD("set_current_scene", "child_node"), &SceneTree::set_current_scene);
  1357. ClassDB::bind_method(D_METHOD("get_current_scene"), &SceneTree::get_current_scene);
  1358. ClassDB::bind_method(D_METHOD("change_scene", "path"), &SceneTree::change_scene);
  1359. ClassDB::bind_method(D_METHOD("change_scene_to", "packed_scene"), &SceneTree::change_scene_to);
  1360. ClassDB::bind_method(D_METHOD("reload_current_scene"), &SceneTree::reload_current_scene);
  1361. ClassDB::bind_method(D_METHOD("_change_scene"), &SceneTree::_change_scene);
  1362. ClassDB::bind_method(D_METHOD("set_multiplayer", "multiplayer"), &SceneTree::set_multiplayer);
  1363. ClassDB::bind_method(D_METHOD("get_multiplayer"), &SceneTree::get_multiplayer);
  1364. ClassDB::bind_method(D_METHOD("set_multiplayer_poll_enabled", "enabled"), &SceneTree::set_multiplayer_poll_enabled);
  1365. ClassDB::bind_method(D_METHOD("is_multiplayer_poll_enabled"), &SceneTree::is_multiplayer_poll_enabled);
  1366. ClassDB::bind_method(D_METHOD("set_network_peer", "peer"), &SceneTree::set_network_peer);
  1367. ClassDB::bind_method(D_METHOD("get_network_peer"), &SceneTree::get_network_peer);
  1368. ClassDB::bind_method(D_METHOD("is_network_server"), &SceneTree::is_network_server);
  1369. ClassDB::bind_method(D_METHOD("has_network_peer"), &SceneTree::has_network_peer);
  1370. ClassDB::bind_method(D_METHOD("get_network_connected_peers"), &SceneTree::get_network_connected_peers);
  1371. ClassDB::bind_method(D_METHOD("get_network_unique_id"), &SceneTree::get_network_unique_id);
  1372. ClassDB::bind_method(D_METHOD("get_rpc_sender_id"), &SceneTree::get_rpc_sender_id);
  1373. ClassDB::bind_method(D_METHOD("set_refuse_new_network_connections", "refuse"), &SceneTree::set_refuse_new_network_connections);
  1374. ClassDB::bind_method(D_METHOD("is_refusing_new_network_connections"), &SceneTree::is_refusing_new_network_connections);
  1375. ClassDB::bind_method(D_METHOD("_network_peer_connected"), &SceneTree::_network_peer_connected);
  1376. ClassDB::bind_method(D_METHOD("_network_peer_disconnected"), &SceneTree::_network_peer_disconnected);
  1377. ClassDB::bind_method(D_METHOD("_connected_to_server"), &SceneTree::_connected_to_server);
  1378. ClassDB::bind_method(D_METHOD("_connection_failed"), &SceneTree::_connection_failed);
  1379. ClassDB::bind_method(D_METHOD("_server_disconnected"), &SceneTree::_server_disconnected);
  1380. ClassDB::bind_method(D_METHOD("set_use_font_oversampling", "enable"), &SceneTree::set_use_font_oversampling);
  1381. ClassDB::bind_method(D_METHOD("is_using_font_oversampling"), &SceneTree::is_using_font_oversampling);
  1382. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_collisions_hint"), "set_debug_collisions_hint", "is_debugging_collisions_hint");
  1383. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_navigation_hint"), "set_debug_navigation_hint", "is_debugging_navigation_hint");
  1384. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_pause", "is_paused");
  1385. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_network_connections"), "set_refuse_new_network_connections", "is_refusing_new_network_connections");
  1386. ADD_PROPERTY_DEFAULT("refuse_new_network_connections", false);
  1387. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_font_oversampling"), "set_use_font_oversampling", "is_using_font_oversampling");
  1388. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edited_scene_root", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_edited_scene_root", "get_edited_scene_root");
  1389. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "current_scene", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_current_scene", "get_current_scene");
  1390. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "network_peer", PROPERTY_HINT_RESOURCE_TYPE, "NetworkedMultiplayerPeer", 0), "set_network_peer", "get_network_peer");
  1391. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "", "get_root");
  1392. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "set_multiplayer", "get_multiplayer");
  1393. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multiplayer_poll"), "set_multiplayer_poll_enabled", "is_multiplayer_poll_enabled");
  1394. ADD_SIGNAL(MethodInfo("tree_changed"));
  1395. ADD_SIGNAL(MethodInfo("node_added", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1396. ADD_SIGNAL(MethodInfo("node_removed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1397. ADD_SIGNAL(MethodInfo("node_renamed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1398. ADD_SIGNAL(MethodInfo("screen_resized"));
  1399. ADD_SIGNAL(MethodInfo("node_configuration_warning_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1400. ADD_SIGNAL(MethodInfo("idle_frame"));
  1401. ADD_SIGNAL(MethodInfo("physics_frame"));
  1402. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::POOL_STRING_ARRAY, "files"), PropertyInfo(Variant::INT, "screen")));
  1403. ADD_SIGNAL(MethodInfo("global_menu_action", PropertyInfo(Variant::NIL, "id"), PropertyInfo(Variant::NIL, "meta")));
  1404. ADD_SIGNAL(MethodInfo("network_peer_connected", PropertyInfo(Variant::INT, "id")));
  1405. ADD_SIGNAL(MethodInfo("network_peer_disconnected", PropertyInfo(Variant::INT, "id")));
  1406. ADD_SIGNAL(MethodInfo("connected_to_server"));
  1407. ADD_SIGNAL(MethodInfo("connection_failed"));
  1408. ADD_SIGNAL(MethodInfo("server_disconnected"));
  1409. BIND_ENUM_CONSTANT(GROUP_CALL_DEFAULT);
  1410. BIND_ENUM_CONSTANT(GROUP_CALL_REVERSE);
  1411. BIND_ENUM_CONSTANT(GROUP_CALL_REALTIME);
  1412. BIND_ENUM_CONSTANT(GROUP_CALL_UNIQUE);
  1413. BIND_ENUM_CONSTANT(STRETCH_MODE_DISABLED);
  1414. BIND_ENUM_CONSTANT(STRETCH_MODE_2D);
  1415. BIND_ENUM_CONSTANT(STRETCH_MODE_VIEWPORT);
  1416. BIND_ENUM_CONSTANT(STRETCH_ASPECT_IGNORE);
  1417. BIND_ENUM_CONSTANT(STRETCH_ASPECT_KEEP);
  1418. BIND_ENUM_CONSTANT(STRETCH_ASPECT_KEEP_WIDTH);
  1419. BIND_ENUM_CONSTANT(STRETCH_ASPECT_KEEP_HEIGHT);
  1420. BIND_ENUM_CONSTANT(STRETCH_ASPECT_EXPAND);
  1421. }
  1422. SceneTree *SceneTree::singleton = NULL;
  1423. SceneTree::IdleCallback SceneTree::idle_callbacks[SceneTree::MAX_IDLE_CALLBACKS];
  1424. int SceneTree::idle_callback_count = 0;
  1425. void SceneTree::_call_idle_callbacks() {
  1426. for (int i = 0; i < idle_callback_count; i++) {
  1427. idle_callbacks[i]();
  1428. }
  1429. }
  1430. void SceneTree::add_idle_callback(IdleCallback p_callback) {
  1431. ERR_FAIL_COND(idle_callback_count >= MAX_IDLE_CALLBACKS);
  1432. idle_callbacks[idle_callback_count++] = p_callback;
  1433. }
  1434. void SceneTree::set_use_font_oversampling(bool p_oversampling) {
  1435. if (use_font_oversampling == p_oversampling)
  1436. return;
  1437. use_font_oversampling = p_oversampling;
  1438. _update_root_rect();
  1439. }
  1440. bool SceneTree::is_using_font_oversampling() const {
  1441. return use_font_oversampling;
  1442. }
  1443. void SceneTree::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1444. if (p_function == "change_scene") {
  1445. DirAccessRef dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1446. List<String> directories;
  1447. directories.push_back(dir_access->get_current_dir());
  1448. while (!directories.empty()) {
  1449. dir_access->change_dir(directories.back()->get());
  1450. directories.pop_back();
  1451. dir_access->list_dir_begin();
  1452. String filename = dir_access->get_next();
  1453. while (filename != "") {
  1454. if (filename == "." || filename == "..") {
  1455. filename = dir_access->get_next();
  1456. continue;
  1457. }
  1458. if (dir_access->dir_exists(filename)) {
  1459. directories.push_back(dir_access->get_current_dir().plus_file(filename));
  1460. } else if (filename.ends_with(".tscn") || filename.ends_with(".scn")) {
  1461. r_options->push_back("\"" + dir_access->get_current_dir().plus_file(filename) + "\"");
  1462. }
  1463. filename = dir_access->get_next();
  1464. }
  1465. }
  1466. }
  1467. }
  1468. SceneTree::SceneTree() {
  1469. if (singleton == NULL) singleton = this;
  1470. _quit = false;
  1471. accept_quit = true;
  1472. quit_on_go_back = true;
  1473. initialized = false;
  1474. use_font_oversampling = false;
  1475. #ifdef DEBUG_ENABLED
  1476. debug_collisions_hint = false;
  1477. debug_navigation_hint = false;
  1478. #endif
  1479. debug_collisions_color = GLOBAL_DEF("debug/shapes/collision/shape_color", Color(0.0, 0.6, 0.7, 0.5));
  1480. debug_collision_contact_color = GLOBAL_DEF("debug/shapes/collision/contact_color", Color(1.0, 0.2, 0.1, 0.8));
  1481. debug_navigation_color = GLOBAL_DEF("debug/shapes/navigation/geometry_color", Color(0.1, 1.0, 0.7, 0.4));
  1482. debug_navigation_disabled_color = GLOBAL_DEF("debug/shapes/navigation/disabled_geometry_color", Color(1.0, 0.7, 0.1, 0.4));
  1483. collision_debug_contacts = GLOBAL_DEF("debug/shapes/collision/max_contacts_displayed", 10000);
  1484. 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
  1485. tree_version = 1;
  1486. physics_process_time = 1;
  1487. idle_process_time = 1;
  1488. root = NULL;
  1489. input_handled = false;
  1490. pause = false;
  1491. current_frame = 0;
  1492. current_event = 0;
  1493. tree_changed_name = "tree_changed";
  1494. node_added_name = "node_added";
  1495. node_removed_name = "node_removed";
  1496. node_renamed_name = "node_renamed";
  1497. ugc_locked = false;
  1498. call_lock = 0;
  1499. root_lock = 0;
  1500. node_count = 0;
  1501. //create with mainloop
  1502. root = memnew(Viewport);
  1503. root->set_name("root");
  1504. root->set_handle_input_locally(false);
  1505. if (!root->get_world().is_valid())
  1506. root->set_world(Ref<World>(memnew(World)));
  1507. // Initialize network state
  1508. multiplayer_poll = true;
  1509. set_multiplayer(Ref<MultiplayerAPI>(memnew(MultiplayerAPI)));
  1510. //root->set_world_2d( Ref<World2D>( memnew( World2D )));
  1511. root->set_as_audio_listener(true);
  1512. root->set_as_audio_listener_2d(true);
  1513. current_scene = NULL;
  1514. int ref_atlas_size = GLOBAL_DEF_RST("rendering/quality/reflections/atlas_size", 2048);
  1515. ProjectSettings::get_singleton()->set_custom_property_info("rendering/quality/reflections/atlas_size", PropertyInfo(Variant::INT, "rendering/quality/reflections/atlas_size", PROPERTY_HINT_RANGE, "0,8192,or_greater")); //next_power_of_2 will return a 0 as min value
  1516. int ref_atlas_subdiv = GLOBAL_DEF_RST("rendering/quality/reflections/atlas_subdiv", 8);
  1517. ProjectSettings::get_singleton()->set_custom_property_info("rendering/quality/reflections/atlas_subdiv", PropertyInfo(Variant::INT, "rendering/quality/reflections/atlas_subdiv", PROPERTY_HINT_RANGE, "0,32,or_greater")); //next_power_of_2 will return a 0 as min value
  1518. int msaa_mode = GLOBAL_DEF("rendering/quality/filters/msaa", 0);
  1519. ProjectSettings::get_singleton()->set_custom_property_info("rendering/quality/filters/msaa", PropertyInfo(Variant::INT, "rendering/quality/filters/msaa", PROPERTY_HINT_ENUM, "Disabled,2x,4x,8x,16x,AndroidVR 2x,AndroidVR 4x"));
  1520. root->set_msaa(Viewport::MSAA(msaa_mode));
  1521. GLOBAL_DEF_RST("rendering/quality/depth/hdr", true);
  1522. GLOBAL_DEF("rendering/quality/depth/hdr.mobile", false);
  1523. bool hdr = GLOBAL_GET("rendering/quality/depth/hdr");
  1524. root->set_hdr(hdr);
  1525. VS::get_singleton()->scenario_set_reflection_atlas_size(root->get_world()->get_scenario(), ref_atlas_size, ref_atlas_subdiv);
  1526. { //load default fallback environment
  1527. //get possible extensions
  1528. List<String> exts;
  1529. ResourceLoader::get_recognized_extensions_for_type("Environment", &exts);
  1530. String ext_hint;
  1531. for (List<String>::Element *E = exts.front(); E; E = E->next()) {
  1532. if (ext_hint != String())
  1533. ext_hint += ",";
  1534. ext_hint += "*." + E->get();
  1535. }
  1536. //get path
  1537. String env_path = GLOBAL_DEF("rendering/environment/default_environment", "");
  1538. //setup property
  1539. ProjectSettings::get_singleton()->set_custom_property_info("rendering/environment/default_environment", PropertyInfo(Variant::STRING, "rendering/viewport/default_environment", PROPERTY_HINT_FILE, ext_hint));
  1540. env_path = env_path.strip_edges();
  1541. if (env_path != String()) {
  1542. Ref<Environment> env = ResourceLoader::load(env_path);
  1543. if (env.is_valid()) {
  1544. root->get_world()->set_fallback_environment(env);
  1545. } else {
  1546. if (Engine::get_singleton()->is_editor_hint()) {
  1547. //file was erased, clear the field.
  1548. ProjectSettings::get_singleton()->set("rendering/environment/default_environment", "");
  1549. } else {
  1550. //file was erased, notify user.
  1551. ERR_PRINTS(RTR("Default Environment as specified in Project Settings (Rendering -> Environment -> Default Environment) could not be loaded."));
  1552. }
  1553. }
  1554. }
  1555. }
  1556. stretch_mode = STRETCH_MODE_DISABLED;
  1557. stretch_aspect = STRETCH_ASPECT_IGNORE;
  1558. stretch_shrink = 1;
  1559. last_screen_size = Size2(OS::get_singleton()->get_window_size().width, OS::get_singleton()->get_window_size().height);
  1560. _update_root_rect();
  1561. if (ScriptDebugger::get_singleton()) {
  1562. if (ScriptDebugger::get_singleton()->is_remote()) {
  1563. ScriptDebuggerRemote *remote_debugger = static_cast<ScriptDebuggerRemote *>(ScriptDebugger::get_singleton());
  1564. remote_debugger->set_scene_tree(this);
  1565. }
  1566. ScriptDebugger::get_singleton()->set_multiplayer(multiplayer);
  1567. }
  1568. root->set_physics_object_picking(GLOBAL_DEF("physics/common/enable_object_picking", true));
  1569. #ifdef TOOLS_ENABLED
  1570. edited_scene_root = NULL;
  1571. #endif
  1572. #ifdef DEBUG_ENABLED
  1573. live_edit_root = NodePath("/root");
  1574. #endif
  1575. }
  1576. SceneTree::~SceneTree() {
  1577. if (root) {
  1578. root->_set_tree(NULL);
  1579. root->_propagate_after_exit_tree();
  1580. memdelete(root);
  1581. }
  1582. if (singleton == this) singleton = NULL;
  1583. }