scene_tree.cpp 60 KB

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