scene_tree.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  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, 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. bool SceneTree::idle(float p_time) {
  360. //print_line("ram: "+itos(OS::get_singleton()->get_static_memory_usage())+" sram: "+itos(OS::get_singleton()->get_dynamic_memory_usage()));
  361. //print_line("node count: "+itos(get_node_count()));
  362. //print_line("TEXTURE RAM: "+itos(VS::get_singleton()->get_render_info(VS::INFO_TEXTURE_MEM_USED)));
  363. root_lock++;
  364. MainLoop::idle(p_time);
  365. idle_process_time = p_time;
  366. if (multiplayer_poll) {
  367. multiplayer->poll();
  368. }
  369. emit_signal("idle_frame");
  370. MessageQueue::get_singleton()->flush(); //small little hack
  371. flush_transform_notifications();
  372. _notify_group_pause("idle_process_internal", Node::NOTIFICATION_INTERNAL_PROCESS);
  373. _notify_group_pause("idle_process", Node::NOTIFICATION_PROCESS);
  374. Size2 win_size = Size2(OS::get_singleton()->get_window_size().width, OS::get_singleton()->get_window_size().height);
  375. if (win_size != last_screen_size) {
  376. last_screen_size = win_size;
  377. _update_root_rect();
  378. if (use_font_oversampling) {
  379. DynamicFontAtSize::font_oversampling = OS::get_singleton()->get_window_size().width / root->get_visible_rect().size.width;
  380. DynamicFont::update_oversampling();
  381. }
  382. emit_signal("screen_resized");
  383. }
  384. _flush_ugc();
  385. MessageQueue::get_singleton()->flush(); //small little hack
  386. flush_transform_notifications(); //transforms after world update, to avoid unnecessary enter/exit notifications
  387. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "update_worlds");
  388. root_lock--;
  389. _flush_delete_queue();
  390. //go through timers
  391. List<Ref<SceneTreeTimer> >::Element *L = timers.back(); //last element
  392. for (List<Ref<SceneTreeTimer> >::Element *E = timers.front(); E;) {
  393. List<Ref<SceneTreeTimer> >::Element *N = E->next();
  394. if (pause && !E->get()->is_pause_mode_process()) {
  395. if (E == L) {
  396. break; //break on last, so if new timers were added during list traversal, ignore them.
  397. }
  398. E = N;
  399. continue;
  400. }
  401. float time_left = E->get()->get_time_left();
  402. time_left -= p_time;
  403. E->get()->set_time_left(time_left);
  404. if (time_left < 0) {
  405. E->get()->emit_signal("timeout");
  406. timers.erase(E);
  407. }
  408. if (E == L) {
  409. break; //break on last, so if new timers were added during list traversal, ignore them.
  410. }
  411. E = N;
  412. }
  413. _call_idle_callbacks();
  414. #ifdef TOOLS_ENABLED
  415. if (Engine::get_singleton()->is_editor_hint()) {
  416. //simple hack to reload fallback environment if it changed from editor
  417. String env_path = ProjectSettings::get_singleton()->get("rendering/environment/default_environment");
  418. env_path = env_path.strip_edges(); //user may have added a space or two
  419. String cpath;
  420. Ref<Environment> fallback = get_root()->get_world()->get_fallback_environment();
  421. if (fallback.is_valid()) {
  422. cpath = fallback->get_path();
  423. }
  424. if (cpath != env_path) {
  425. if (env_path != String()) {
  426. fallback = ResourceLoader::load(env_path);
  427. if (fallback.is_null()) {
  428. //could not load fallback, set as empty
  429. ProjectSettings::get_singleton()->set("rendering/environment/default_environment", "");
  430. }
  431. } else {
  432. fallback.unref();
  433. }
  434. get_root()->get_world()->set_fallback_environment(fallback);
  435. }
  436. }
  437. #endif
  438. return _quit;
  439. }
  440. void SceneTree::finish() {
  441. _flush_delete_queue();
  442. _flush_ugc();
  443. initialized = false;
  444. MainLoop::finish();
  445. if (root) {
  446. root->_set_tree(NULL);
  447. root->_propagate_after_exit_tree();
  448. memdelete(root); //delete root
  449. }
  450. }
  451. void SceneTree::quit() {
  452. _quit = true;
  453. }
  454. void SceneTree::_notification(int p_notification) {
  455. switch (p_notification) {
  456. case NOTIFICATION_WM_QUIT_REQUEST: {
  457. get_root()->propagate_notification(p_notification);
  458. if (accept_quit) {
  459. _quit = true;
  460. break;
  461. }
  462. } break;
  463. case NOTIFICATION_WM_GO_BACK_REQUEST: {
  464. get_root()->propagate_notification(p_notification);
  465. if (quit_on_go_back) {
  466. _quit = true;
  467. break;
  468. }
  469. } break;
  470. case NOTIFICATION_OS_MEMORY_WARNING:
  471. case NOTIFICATION_OS_IME_UPDATE:
  472. case NOTIFICATION_WM_MOUSE_ENTER:
  473. case NOTIFICATION_WM_MOUSE_EXIT:
  474. case NOTIFICATION_WM_FOCUS_IN:
  475. case NOTIFICATION_WM_FOCUS_OUT: {
  476. if (p_notification == NOTIFICATION_WM_FOCUS_IN) {
  477. InputDefault *id = Object::cast_to<InputDefault>(Input::get_singleton());
  478. if (id) {
  479. id->ensure_touch_mouse_raised();
  480. }
  481. }
  482. get_root()->propagate_notification(p_notification);
  483. } break;
  484. case NOTIFICATION_TRANSLATION_CHANGED: {
  485. if (!Engine::get_singleton()->is_editor_hint()) {
  486. get_root()->propagate_notification(Node::NOTIFICATION_TRANSLATION_CHANGED);
  487. }
  488. } break;
  489. case NOTIFICATION_WM_UNFOCUS_REQUEST: {
  490. notify_group_flags(GROUP_CALL_REALTIME | GROUP_CALL_MULTILEVEL, "input", NOTIFICATION_WM_UNFOCUS_REQUEST);
  491. } break;
  492. case NOTIFICATION_WM_ABOUT: {
  493. #ifdef TOOLS_ENABLED
  494. if (EditorNode::get_singleton()) {
  495. EditorNode::get_singleton()->show_about();
  496. } else {
  497. #endif
  498. get_root()->propagate_notification(p_notification);
  499. #ifdef TOOLS_ENABLED
  500. }
  501. #endif
  502. } break;
  503. case NOTIFICATION_CRASH: {
  504. get_root()->propagate_notification(p_notification);
  505. } break;
  506. default:
  507. break;
  508. };
  509. };
  510. void SceneTree::set_auto_accept_quit(bool p_enable) {
  511. accept_quit = p_enable;
  512. }
  513. void SceneTree::set_quit_on_go_back(bool p_enable) {
  514. quit_on_go_back = p_enable;
  515. }
  516. #ifdef TOOLS_ENABLED
  517. bool SceneTree::is_node_being_edited(const Node *p_node) const {
  518. return Engine::get_singleton()->is_editor_hint() && edited_scene_root && (edited_scene_root->is_a_parent_of(p_node) || edited_scene_root == p_node);
  519. }
  520. #endif
  521. #ifdef DEBUG_ENABLED
  522. void SceneTree::set_debug_collisions_hint(bool p_enabled) {
  523. debug_collisions_hint = p_enabled;
  524. }
  525. bool SceneTree::is_debugging_collisions_hint() const {
  526. return debug_collisions_hint;
  527. }
  528. void SceneTree::set_debug_navigation_hint(bool p_enabled) {
  529. debug_navigation_hint = p_enabled;
  530. }
  531. bool SceneTree::is_debugging_navigation_hint() const {
  532. return debug_navigation_hint;
  533. }
  534. #endif
  535. void SceneTree::set_debug_collisions_color(const Color &p_color) {
  536. debug_collisions_color = p_color;
  537. }
  538. Color SceneTree::get_debug_collisions_color() const {
  539. return debug_collisions_color;
  540. }
  541. void SceneTree::set_debug_collision_contact_color(const Color &p_color) {
  542. debug_collision_contact_color = p_color;
  543. }
  544. Color SceneTree::get_debug_collision_contact_color() const {
  545. return debug_collision_contact_color;
  546. }
  547. void SceneTree::set_debug_navigation_color(const Color &p_color) {
  548. debug_navigation_color = p_color;
  549. }
  550. Color SceneTree::get_debug_navigation_color() const {
  551. return debug_navigation_color;
  552. }
  553. void SceneTree::set_debug_navigation_disabled_color(const Color &p_color) {
  554. debug_navigation_disabled_color = p_color;
  555. }
  556. Color SceneTree::get_debug_navigation_disabled_color() const {
  557. return debug_navigation_disabled_color;
  558. }
  559. Ref<Material> SceneTree::get_debug_navigation_material() {
  560. if (navigation_material.is_valid())
  561. return navigation_material;
  562. Ref<SpatialMaterial> line_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  563. line_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  564. line_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  565. line_material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  566. line_material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  567. line_material->set_albedo(get_debug_navigation_color());
  568. navigation_material = line_material;
  569. return navigation_material;
  570. }
  571. Ref<Material> SceneTree::get_debug_navigation_disabled_material() {
  572. if (navigation_disabled_material.is_valid())
  573. return navigation_disabled_material;
  574. Ref<SpatialMaterial> line_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  575. line_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  576. line_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  577. line_material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  578. line_material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  579. line_material->set_albedo(get_debug_navigation_disabled_color());
  580. navigation_disabled_material = line_material;
  581. return navigation_disabled_material;
  582. }
  583. Ref<Material> SceneTree::get_debug_collision_material() {
  584. if (collision_material.is_valid())
  585. return collision_material;
  586. Ref<SpatialMaterial> line_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  587. line_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  588. line_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  589. line_material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  590. line_material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  591. line_material->set_albedo(get_debug_collisions_color());
  592. collision_material = line_material;
  593. return collision_material;
  594. }
  595. Ref<ArrayMesh> SceneTree::get_debug_contact_mesh() {
  596. if (debug_contact_mesh.is_valid())
  597. return debug_contact_mesh;
  598. debug_contact_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  599. Ref<SpatialMaterial> mat = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  600. mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  601. mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  602. mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  603. mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  604. mat->set_albedo(get_debug_collision_contact_color());
  605. Vector3 diamond[6] = {
  606. Vector3(-1, 0, 0),
  607. Vector3(1, 0, 0),
  608. Vector3(0, -1, 0),
  609. Vector3(0, 1, 0),
  610. Vector3(0, 0, -1),
  611. Vector3(0, 0, 1)
  612. };
  613. /* clang-format off */
  614. int diamond_faces[8 * 3] = {
  615. 0, 2, 4,
  616. 0, 3, 4,
  617. 1, 2, 4,
  618. 1, 3, 4,
  619. 0, 2, 5,
  620. 0, 3, 5,
  621. 1, 2, 5,
  622. 1, 3, 5,
  623. };
  624. /* clang-format on */
  625. PoolVector<int> indices;
  626. for (int i = 0; i < 8 * 3; i++)
  627. indices.push_back(diamond_faces[i]);
  628. PoolVector<Vector3> vertices;
  629. for (int i = 0; i < 6; i++)
  630. vertices.push_back(diamond[i] * 0.1);
  631. Array arr;
  632. arr.resize(Mesh::ARRAY_MAX);
  633. arr[Mesh::ARRAY_VERTEX] = vertices;
  634. arr[Mesh::ARRAY_INDEX] = indices;
  635. debug_contact_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
  636. debug_contact_mesh->surface_set_material(0, mat);
  637. return debug_contact_mesh;
  638. }
  639. void SceneTree::set_pause(bool p_enabled) {
  640. if (p_enabled == pause)
  641. return;
  642. pause = p_enabled;
  643. PhysicsServer::get_singleton()->set_active(!p_enabled);
  644. Physics2DServer::get_singleton()->set_active(!p_enabled);
  645. if (get_root())
  646. get_root()->propagate_notification(p_enabled ? Node::NOTIFICATION_PAUSED : Node::NOTIFICATION_UNPAUSED);
  647. }
  648. bool SceneTree::is_paused() const {
  649. return pause;
  650. }
  651. void SceneTree::_call_input_pause(const StringName &p_group, const StringName &p_method, const Ref<InputEvent> &p_input) {
  652. Map<StringName, Group>::Element *E = group_map.find(p_group);
  653. if (!E)
  654. return;
  655. Group &g = E->get();
  656. if (g.nodes.empty())
  657. return;
  658. _update_group_order(g);
  659. //copy, so copy on write happens in case something is removed from process while being called
  660. //performance is not lost because only if something is added/removed the vector is copied.
  661. Vector<Node *> nodes_copy = g.nodes;
  662. int node_count = nodes_copy.size();
  663. Node **nodes = nodes_copy.ptrw();
  664. Variant arg = p_input;
  665. const Variant *v[1] = { &arg };
  666. call_lock++;
  667. for (int i = node_count - 1; i >= 0; i--) {
  668. if (input_handled)
  669. break;
  670. Node *n = nodes[i];
  671. if (call_lock && call_skip.has(n))
  672. continue;
  673. if (!n->can_process())
  674. continue;
  675. n->call_multilevel(p_method, (const Variant **)v, 1);
  676. //ERR_FAIL_COND(node_count != g.nodes.size());
  677. }
  678. call_lock--;
  679. if (call_lock == 0)
  680. call_skip.clear();
  681. }
  682. void SceneTree::_notify_group_pause(const StringName &p_group, int p_notification) {
  683. Map<StringName, Group>::Element *E = group_map.find(p_group);
  684. if (!E)
  685. return;
  686. Group &g = E->get();
  687. if (g.nodes.empty())
  688. return;
  689. _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);
  690. //copy, so copy on write happens in case something is removed from process while being called
  691. //performance is not lost because only if something is added/removed the vector is copied.
  692. Vector<Node *> nodes_copy = g.nodes;
  693. int node_count = nodes_copy.size();
  694. Node **nodes = nodes_copy.ptrw();
  695. call_lock++;
  696. for (int i = 0; i < node_count; i++) {
  697. Node *n = nodes[i];
  698. if (call_lock && call_skip.has(n))
  699. continue;
  700. if (!n->can_process())
  701. continue;
  702. if (!n->can_process_notification(p_notification))
  703. continue;
  704. n->notification(p_notification);
  705. //ERR_FAIL_COND(node_count != g.nodes.size());
  706. }
  707. call_lock--;
  708. if (call_lock == 0)
  709. call_skip.clear();
  710. }
  711. /*
  712. void SceneMainLoop::_update_listener_2d() {
  713. if (listener_2d.is_valid()) {
  714. SpatialSound2DServer::get_singleton()->listener_set_space( listener_2d, world_2d->get_sound_space() );
  715. }
  716. }
  717. */
  718. Variant SceneTree::_call_group_flags(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  719. r_error.error = Variant::CallError::CALL_OK;
  720. ERR_FAIL_COND_V(p_argcount < 3, Variant());
  721. ERR_FAIL_COND_V(!p_args[0]->is_num(), Variant());
  722. ERR_FAIL_COND_V(p_args[1]->get_type() != Variant::STRING, Variant());
  723. ERR_FAIL_COND_V(p_args[2]->get_type() != Variant::STRING, Variant());
  724. int flags = *p_args[0];
  725. StringName group = *p_args[1];
  726. StringName method = *p_args[2];
  727. Variant v[VARIANT_ARG_MAX];
  728. for (int i = 0; i < MIN(p_argcount - 3, 5); i++) {
  729. v[i] = *p_args[i + 3];
  730. }
  731. call_group_flags(flags, group, method, v[0], v[1], v[2], v[3], v[4]);
  732. return Variant();
  733. }
  734. Variant SceneTree::_call_group(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  735. r_error.error = Variant::CallError::CALL_OK;
  736. ERR_FAIL_COND_V(p_argcount < 2, Variant());
  737. ERR_FAIL_COND_V(p_args[0]->get_type() != Variant::STRING, Variant());
  738. ERR_FAIL_COND_V(p_args[1]->get_type() != Variant::STRING, Variant());
  739. StringName group = *p_args[0];
  740. StringName method = *p_args[1];
  741. Variant v[VARIANT_ARG_MAX];
  742. for (int i = 0; i < MIN(p_argcount - 2, 5); i++) {
  743. v[i] = *p_args[i + 2];
  744. }
  745. call_group_flags(0, group, method, v[0], v[1], v[2], v[3], v[4]);
  746. return Variant();
  747. }
  748. int64_t SceneTree::get_frame() const {
  749. return current_frame;
  750. }
  751. int64_t SceneTree::get_event_count() const {
  752. return current_event;
  753. }
  754. Array SceneTree::_get_nodes_in_group(const StringName &p_group) {
  755. Array ret;
  756. Map<StringName, Group>::Element *E = group_map.find(p_group);
  757. if (!E)
  758. return ret;
  759. _update_group_order(E->get()); //update order just in case
  760. int nc = E->get().nodes.size();
  761. if (nc == 0)
  762. return ret;
  763. ret.resize(nc);
  764. Node **ptr = E->get().nodes.ptrw();
  765. for (int i = 0; i < nc; i++) {
  766. ret[i] = ptr[i];
  767. }
  768. return ret;
  769. }
  770. bool SceneTree::has_group(const StringName &p_identifier) const {
  771. return group_map.has(p_identifier);
  772. }
  773. void SceneTree::get_nodes_in_group(const StringName &p_group, List<Node *> *p_list) {
  774. Map<StringName, Group>::Element *E = group_map.find(p_group);
  775. if (!E)
  776. return;
  777. _update_group_order(E->get()); //update order just in case
  778. int nc = E->get().nodes.size();
  779. if (nc == 0)
  780. return;
  781. Node **ptr = E->get().nodes.ptrw();
  782. for (int i = 0; i < nc; i++) {
  783. p_list->push_back(ptr[i]);
  784. }
  785. }
  786. static void _fill_array(Node *p_node, Array &array, int p_level) {
  787. array.push_back(p_level);
  788. array.push_back(p_node->get_name());
  789. array.push_back(p_node->get_class());
  790. array.push_back(p_node->get_instance_id());
  791. for (int i = 0; i < p_node->get_child_count(); i++) {
  792. _fill_array(p_node->get_child(i), array, p_level + 1);
  793. }
  794. }
  795. void SceneTree::_debugger_request_tree(void *self) {
  796. SceneTree *sml = (SceneTree *)self;
  797. Array arr;
  798. _fill_array(sml->root, arr, 0);
  799. ScriptDebugger::get_singleton()->send_message("scene_tree", arr);
  800. }
  801. void SceneTree::_flush_delete_queue() {
  802. _THREAD_SAFE_METHOD_
  803. while (delete_queue.size()) {
  804. Object *obj = ObjectDB::get_instance(delete_queue.front()->get());
  805. if (obj) {
  806. memdelete(obj);
  807. }
  808. delete_queue.pop_front();
  809. }
  810. }
  811. void SceneTree::queue_delete(Object *p_object) {
  812. _THREAD_SAFE_METHOD_
  813. ERR_FAIL_NULL(p_object);
  814. p_object->_is_queued_for_deletion = true;
  815. delete_queue.push_back(p_object->get_instance_id());
  816. }
  817. int SceneTree::get_node_count() const {
  818. return node_count;
  819. }
  820. void SceneTree::_update_root_rect() {
  821. if (stretch_mode == STRETCH_MODE_DISABLED) {
  822. root->set_size((last_screen_size / stretch_shrink).floor());
  823. root->set_attach_to_screen_rect(Rect2(Point2(), last_screen_size));
  824. root->set_size_override_stretch(false);
  825. root->set_size_override(false, Size2());
  826. return; //user will take care
  827. }
  828. //actual screen video mode
  829. Size2 video_mode = Size2(OS::get_singleton()->get_window_size().width, OS::get_singleton()->get_window_size().height);
  830. Size2 desired_res = stretch_min;
  831. Size2 viewport_size;
  832. Size2 screen_size;
  833. float viewport_aspect = desired_res.aspect();
  834. float video_mode_aspect = video_mode.aspect();
  835. if (stretch_aspect == STRETCH_ASPECT_IGNORE || ABS(viewport_aspect - video_mode_aspect) < CMP_EPSILON) {
  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. } break;
  886. case STRETCH_MODE_2D: {
  887. root->set_size((screen_size / stretch_shrink).floor());
  888. root->set_attach_to_screen_rect(Rect2(margin, screen_size));
  889. root->set_size_override_stretch(true);
  890. root->set_size_override(true, (viewport_size / stretch_shrink).floor());
  891. } break;
  892. case STRETCH_MODE_VIEWPORT: {
  893. root->set_size((viewport_size / stretch_shrink).floor());
  894. root->set_attach_to_screen_rect(Rect2(margin, screen_size));
  895. root->set_size_override_stretch(false);
  896. root->set_size_override(false, Size2());
  897. } break;
  898. }
  899. }
  900. void SceneTree::set_screen_stretch(StretchMode p_mode, StretchAspect p_aspect, const Size2 p_minsize, real_t p_shrink) {
  901. stretch_mode = p_mode;
  902. stretch_aspect = p_aspect;
  903. stretch_min = p_minsize;
  904. stretch_shrink = p_shrink;
  905. _update_root_rect();
  906. }
  907. void SceneTree::set_edited_scene_root(Node *p_node) {
  908. #ifdef TOOLS_ENABLED
  909. edited_scene_root = p_node;
  910. #endif
  911. }
  912. Node *SceneTree::get_edited_scene_root() const {
  913. #ifdef TOOLS_ENABLED
  914. return edited_scene_root;
  915. #else
  916. return NULL;
  917. #endif
  918. }
  919. void SceneTree::set_current_scene(Node *p_scene) {
  920. ERR_FAIL_COND(p_scene && p_scene->get_parent() != root);
  921. current_scene = p_scene;
  922. }
  923. Node *SceneTree::get_current_scene() const {
  924. return current_scene;
  925. }
  926. void SceneTree::_change_scene(Node *p_to) {
  927. if (current_scene) {
  928. memdelete(current_scene);
  929. current_scene = NULL;
  930. }
  931. if (p_to) {
  932. current_scene = p_to;
  933. root->add_child(p_to);
  934. }
  935. }
  936. Error SceneTree::change_scene(const String &p_path) {
  937. Ref<PackedScene> new_scene = ResourceLoader::load(p_path);
  938. if (new_scene.is_null())
  939. return ERR_CANT_OPEN;
  940. return change_scene_to(new_scene);
  941. }
  942. Error SceneTree::change_scene_to(const Ref<PackedScene> &p_scene) {
  943. Node *new_scene = NULL;
  944. if (p_scene.is_valid()) {
  945. new_scene = p_scene->instance();
  946. ERR_FAIL_COND_V(!new_scene, ERR_CANT_CREATE);
  947. }
  948. call_deferred("_change_scene", new_scene);
  949. return OK;
  950. }
  951. Error SceneTree::reload_current_scene() {
  952. ERR_FAIL_COND_V(!current_scene, ERR_UNCONFIGURED);
  953. String fname = current_scene->get_filename();
  954. return change_scene(fname);
  955. }
  956. void SceneTree::add_current_scene(Node *p_current) {
  957. current_scene = p_current;
  958. root->add_child(p_current);
  959. }
  960. #ifdef DEBUG_ENABLED
  961. void SceneTree::_live_edit_node_path_func(const NodePath &p_path, int p_id) {
  962. live_edit_node_path_cache[p_id] = p_path;
  963. }
  964. void SceneTree::_live_edit_res_path_func(const String &p_path, int p_id) {
  965. live_edit_resource_cache[p_id] = p_path;
  966. }
  967. void SceneTree::_live_edit_node_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  968. if (!live_edit_node_path_cache.has(p_id))
  969. return;
  970. NodePath np = live_edit_node_path_cache[p_id];
  971. Node *base = NULL;
  972. if (root->has_node(live_edit_root))
  973. base = root->get_node(live_edit_root);
  974. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  975. if (!E)
  976. return; //scene not editable
  977. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  978. Node *n = F->get();
  979. if (base && !base->is_a_parent_of(n))
  980. continue;
  981. if (!n->has_node(np))
  982. continue;
  983. Node *n2 = n->get_node(np);
  984. n2->set(p_prop, p_value);
  985. }
  986. }
  987. void SceneTree::_live_edit_node_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  988. RES r = ResourceLoader::load(p_value);
  989. if (!r.is_valid())
  990. return;
  991. _live_edit_node_set_func(p_id, p_prop, r);
  992. }
  993. void SceneTree::_live_edit_node_call_func(int p_id, const StringName &p_method, VARIANT_ARG_DECLARE) {
  994. if (!live_edit_node_path_cache.has(p_id))
  995. return;
  996. NodePath np = live_edit_node_path_cache[p_id];
  997. Node *base = NULL;
  998. if (root->has_node(live_edit_root))
  999. base = root->get_node(live_edit_root);
  1000. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1001. if (!E)
  1002. return; //scene not editable
  1003. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1004. Node *n = F->get();
  1005. if (base && !base->is_a_parent_of(n))
  1006. continue;
  1007. if (!n->has_node(np))
  1008. continue;
  1009. Node *n2 = n->get_node(np);
  1010. n2->call(p_method, VARIANT_ARG_PASS);
  1011. }
  1012. }
  1013. void SceneTree::_live_edit_res_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  1014. if (!live_edit_resource_cache.has(p_id))
  1015. return;
  1016. String resp = live_edit_resource_cache[p_id];
  1017. if (!ResourceCache::has(resp))
  1018. return;
  1019. RES r = ResourceCache::get(resp);
  1020. if (!r.is_valid())
  1021. return;
  1022. r->set(p_prop, p_value);
  1023. }
  1024. void SceneTree::_live_edit_res_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  1025. RES r = ResourceLoader::load(p_value);
  1026. if (!r.is_valid())
  1027. return;
  1028. _live_edit_res_set_func(p_id, p_prop, r);
  1029. }
  1030. void SceneTree::_live_edit_res_call_func(int p_id, const StringName &p_method, VARIANT_ARG_DECLARE) {
  1031. if (!live_edit_resource_cache.has(p_id))
  1032. return;
  1033. String resp = live_edit_resource_cache[p_id];
  1034. if (!ResourceCache::has(resp))
  1035. return;
  1036. RES r = ResourceCache::get(resp);
  1037. if (!r.is_valid())
  1038. return;
  1039. r->call(p_method, VARIANT_ARG_PASS);
  1040. }
  1041. void SceneTree::_live_edit_root_func(const NodePath &p_scene_path, const String &p_scene_from) {
  1042. live_edit_root = p_scene_path;
  1043. live_edit_scene = p_scene_from;
  1044. }
  1045. void SceneTree::_live_edit_create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name) {
  1046. Node *base = NULL;
  1047. if (root->has_node(live_edit_root))
  1048. base = root->get_node(live_edit_root);
  1049. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1050. if (!E)
  1051. return; //scene not editable
  1052. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1053. Node *n = F->get();
  1054. if (base && !base->is_a_parent_of(n))
  1055. continue;
  1056. if (!n->has_node(p_parent))
  1057. continue;
  1058. Node *n2 = n->get_node(p_parent);
  1059. Node *no = Object::cast_to<Node>(ClassDB::instance(p_type));
  1060. if (!no) {
  1061. continue;
  1062. }
  1063. no->set_name(p_name);
  1064. n2->add_child(no);
  1065. }
  1066. }
  1067. void SceneTree::_live_edit_instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name) {
  1068. Ref<PackedScene> ps = ResourceLoader::load(p_path);
  1069. if (!ps.is_valid())
  1070. return;
  1071. Node *base = NULL;
  1072. if (root->has_node(live_edit_root))
  1073. base = root->get_node(live_edit_root);
  1074. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1075. if (!E)
  1076. return; //scene not editable
  1077. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1078. Node *n = F->get();
  1079. if (base && !base->is_a_parent_of(n))
  1080. continue;
  1081. if (!n->has_node(p_parent))
  1082. continue;
  1083. Node *n2 = n->get_node(p_parent);
  1084. Node *no = ps->instance();
  1085. no->set_name(p_name);
  1086. n2->add_child(no);
  1087. }
  1088. }
  1089. void SceneTree::_live_edit_remove_node_func(const NodePath &p_at) {
  1090. Node *base = NULL;
  1091. if (root->has_node(live_edit_root))
  1092. base = root->get_node(live_edit_root);
  1093. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1094. if (!E)
  1095. return; //scene not editable
  1096. for (Set<Node *>::Element *F = E->get().front(); F;) {
  1097. Set<Node *>::Element *N = F->next();
  1098. Node *n = F->get();
  1099. if (base && !base->is_a_parent_of(n))
  1100. continue;
  1101. if (!n->has_node(p_at))
  1102. continue;
  1103. Node *n2 = n->get_node(p_at);
  1104. memdelete(n2);
  1105. F = N;
  1106. }
  1107. }
  1108. void SceneTree::_live_edit_remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id) {
  1109. Node *base = NULL;
  1110. if (root->has_node(live_edit_root))
  1111. base = root->get_node(live_edit_root);
  1112. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1113. if (!E)
  1114. return; //scene not editable
  1115. for (Set<Node *>::Element *F = E->get().front(); F;) {
  1116. Set<Node *>::Element *N = F->next();
  1117. Node *n = F->get();
  1118. if (base && !base->is_a_parent_of(n))
  1119. continue;
  1120. if (!n->has_node(p_at))
  1121. continue;
  1122. Node *n2 = n->get_node(p_at);
  1123. n2->get_parent()->remove_child(n2);
  1124. live_edit_remove_list[n][p_keep_id] = n2;
  1125. F = N;
  1126. }
  1127. }
  1128. void SceneTree::_live_edit_restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  1129. Node *base = NULL;
  1130. if (root->has_node(live_edit_root))
  1131. base = root->get_node(live_edit_root);
  1132. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1133. if (!E)
  1134. return; //scene not editable
  1135. for (Set<Node *>::Element *F = E->get().front(); F;) {
  1136. Set<Node *>::Element *N = F->next();
  1137. Node *n = F->get();
  1138. if (base && !base->is_a_parent_of(n))
  1139. continue;
  1140. if (!n->has_node(p_at))
  1141. continue;
  1142. Node *n2 = n->get_node(p_at);
  1143. Map<Node *, Map<ObjectID, Node *> >::Element *EN = live_edit_remove_list.find(n);
  1144. if (!EN)
  1145. continue;
  1146. Map<ObjectID, Node *>::Element *FN = EN->get().find(p_id);
  1147. if (!FN)
  1148. continue;
  1149. n2->add_child(FN->get());
  1150. EN->get().erase(FN);
  1151. if (EN->get().size() == 0) {
  1152. live_edit_remove_list.erase(EN);
  1153. }
  1154. F = N;
  1155. }
  1156. }
  1157. void SceneTree::_live_edit_duplicate_node_func(const NodePath &p_at, const String &p_new_name) {
  1158. Node *base = NULL;
  1159. if (root->has_node(live_edit_root))
  1160. base = root->get_node(live_edit_root);
  1161. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1162. if (!E)
  1163. return; //scene not editable
  1164. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1165. Node *n = F->get();
  1166. if (base && !base->is_a_parent_of(n))
  1167. continue;
  1168. if (!n->has_node(p_at))
  1169. continue;
  1170. Node *n2 = n->get_node(p_at);
  1171. Node *dup = n2->duplicate(Node::DUPLICATE_SIGNALS | Node::DUPLICATE_GROUPS | Node::DUPLICATE_SCRIPTS);
  1172. if (!dup)
  1173. continue;
  1174. dup->set_name(p_new_name);
  1175. n2->get_parent()->add_child(dup);
  1176. }
  1177. }
  1178. 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) {
  1179. Node *base = NULL;
  1180. if (root->has_node(live_edit_root))
  1181. base = root->get_node(live_edit_root);
  1182. Map<String, Set<Node *> >::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1183. if (!E)
  1184. return; //scene not editable
  1185. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1186. Node *n = F->get();
  1187. if (base && !base->is_a_parent_of(n))
  1188. continue;
  1189. if (!n->has_node(p_at))
  1190. continue;
  1191. Node *nfrom = n->get_node(p_at);
  1192. if (!n->has_node(p_new_place))
  1193. continue;
  1194. Node *nto = n->get_node(p_new_place);
  1195. nfrom->get_parent()->remove_child(nfrom);
  1196. nfrom->set_name(p_new_name);
  1197. nto->add_child(nfrom);
  1198. if (p_at_pos >= 0)
  1199. nto->move_child(nfrom, p_at_pos);
  1200. }
  1201. }
  1202. #endif
  1203. void SceneTree::drop_files(const Vector<String> &p_files, int p_from_screen) {
  1204. emit_signal("files_dropped", p_files, p_from_screen);
  1205. MainLoop::drop_files(p_files, p_from_screen);
  1206. }
  1207. Ref<SceneTreeTimer> SceneTree::create_timer(float p_delay_sec, bool p_process_pause) {
  1208. Ref<SceneTreeTimer> stt;
  1209. stt.instance();
  1210. stt->set_pause_mode_process(p_process_pause);
  1211. stt->set_time_left(p_delay_sec);
  1212. timers.push_back(stt);
  1213. return stt;
  1214. }
  1215. void SceneTree::_network_peer_connected(int p_id) {
  1216. emit_signal("network_peer_connected", p_id);
  1217. }
  1218. void SceneTree::_network_peer_disconnected(int p_id) {
  1219. emit_signal("network_peer_disconnected", p_id);
  1220. }
  1221. void SceneTree::_connected_to_server() {
  1222. emit_signal("connected_to_server");
  1223. }
  1224. void SceneTree::_connection_failed() {
  1225. emit_signal("connection_failed");
  1226. }
  1227. void SceneTree::_server_disconnected() {
  1228. emit_signal("server_disconnected");
  1229. }
  1230. Ref<MultiplayerAPI> SceneTree::get_multiplayer() const {
  1231. return multiplayer;
  1232. }
  1233. void SceneTree::set_multiplayer_poll_enabled(bool p_enabled) {
  1234. multiplayer_poll = p_enabled;
  1235. }
  1236. bool SceneTree::is_multiplayer_poll_enabled() const {
  1237. return multiplayer_poll;
  1238. }
  1239. void SceneTree::set_multiplayer(Ref<MultiplayerAPI> p_multiplayer) {
  1240. ERR_FAIL_COND(!p_multiplayer.is_valid());
  1241. if (multiplayer.is_valid()) {
  1242. multiplayer->disconnect("network_peer_connected", this, "_network_peer_connected");
  1243. multiplayer->disconnect("network_peer_disconnected", this, "_network_peer_disconnected");
  1244. multiplayer->disconnect("connected_to_server", this, "_connected_to_server");
  1245. multiplayer->disconnect("connection_failed", this, "_connection_failed");
  1246. multiplayer->disconnect("server_disconnected", this, "_server_disconnected");
  1247. }
  1248. multiplayer = p_multiplayer;
  1249. multiplayer->set_root_node(root);
  1250. multiplayer->connect("network_peer_connected", this, "_network_peer_connected");
  1251. multiplayer->connect("network_peer_disconnected", this, "_network_peer_disconnected");
  1252. multiplayer->connect("connected_to_server", this, "_connected_to_server");
  1253. multiplayer->connect("connection_failed", this, "_connection_failed");
  1254. multiplayer->connect("server_disconnected", this, "_server_disconnected");
  1255. }
  1256. void SceneTree::set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_network_peer) {
  1257. multiplayer->set_network_peer(p_network_peer);
  1258. }
  1259. Ref<NetworkedMultiplayerPeer> SceneTree::get_network_peer() const {
  1260. return multiplayer->get_network_peer();
  1261. }
  1262. bool SceneTree::is_network_server() const {
  1263. return multiplayer->is_network_server();
  1264. }
  1265. bool SceneTree::has_network_peer() const {
  1266. return multiplayer->has_network_peer();
  1267. }
  1268. int SceneTree::get_network_unique_id() const {
  1269. return multiplayer->get_network_unique_id();
  1270. }
  1271. Vector<int> SceneTree::get_network_connected_peers() const {
  1272. return multiplayer->get_network_connected_peers();
  1273. }
  1274. int SceneTree::get_rpc_sender_id() const {
  1275. return multiplayer->get_rpc_sender_id();
  1276. }
  1277. void SceneTree::set_refuse_new_network_connections(bool p_refuse) {
  1278. multiplayer->set_refuse_new_network_connections(p_refuse);
  1279. }
  1280. bool SceneTree::is_refusing_new_network_connections() const {
  1281. return multiplayer->is_refusing_new_network_connections();
  1282. }
  1283. void SceneTree::_bind_methods() {
  1284. //ClassDB::bind_method(D_METHOD("call_group","call_flags","group","method","arg1","arg2"),&SceneMainLoop::_call_group,DEFVAL(Variant()),DEFVAL(Variant()));
  1285. ClassDB::bind_method(D_METHOD("get_root"), &SceneTree::get_root);
  1286. ClassDB::bind_method(D_METHOD("has_group", "name"), &SceneTree::has_group);
  1287. ClassDB::bind_method(D_METHOD("set_auto_accept_quit", "enabled"), &SceneTree::set_auto_accept_quit);
  1288. ClassDB::bind_method(D_METHOD("set_quit_on_go_back", "enabled"), &SceneTree::set_quit_on_go_back);
  1289. ClassDB::bind_method(D_METHOD("set_debug_collisions_hint", "enable"), &SceneTree::set_debug_collisions_hint);
  1290. ClassDB::bind_method(D_METHOD("is_debugging_collisions_hint"), &SceneTree::is_debugging_collisions_hint);
  1291. ClassDB::bind_method(D_METHOD("set_debug_navigation_hint", "enable"), &SceneTree::set_debug_navigation_hint);
  1292. ClassDB::bind_method(D_METHOD("is_debugging_navigation_hint"), &SceneTree::is_debugging_navigation_hint);
  1293. ClassDB::bind_method(D_METHOD("set_edited_scene_root", "scene"), &SceneTree::set_edited_scene_root);
  1294. ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &SceneTree::get_edited_scene_root);
  1295. ClassDB::bind_method(D_METHOD("set_pause", "enable"), &SceneTree::set_pause);
  1296. ClassDB::bind_method(D_METHOD("is_paused"), &SceneTree::is_paused);
  1297. ClassDB::bind_method(D_METHOD("set_input_as_handled"), &SceneTree::set_input_as_handled);
  1298. ClassDB::bind_method(D_METHOD("is_input_handled"), &SceneTree::is_input_handled);
  1299. ClassDB::bind_method(D_METHOD("create_timer", "time_sec", "pause_mode_process"), &SceneTree::create_timer, DEFVAL(true));
  1300. ClassDB::bind_method(D_METHOD("get_node_count"), &SceneTree::get_node_count);
  1301. ClassDB::bind_method(D_METHOD("get_frame"), &SceneTree::get_frame);
  1302. ClassDB::bind_method(D_METHOD("quit"), &SceneTree::quit);
  1303. ClassDB::bind_method(D_METHOD("set_screen_stretch", "mode", "aspect", "minsize", "shrink"), &SceneTree::set_screen_stretch, DEFVAL(1));
  1304. ClassDB::bind_method(D_METHOD("queue_delete", "obj"), &SceneTree::queue_delete);
  1305. MethodInfo mi;
  1306. mi.name = "call_group_flags";
  1307. mi.arguments.push_back(PropertyInfo(Variant::INT, "flags"));
  1308. mi.arguments.push_back(PropertyInfo(Variant::STRING, "group"));
  1309. mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  1310. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group_flags", &SceneTree::_call_group_flags, mi);
  1311. ClassDB::bind_method(D_METHOD("notify_group_flags", "call_flags", "group", "notification"), &SceneTree::notify_group_flags);
  1312. ClassDB::bind_method(D_METHOD("set_group_flags", "call_flags", "group", "property", "value"), &SceneTree::set_group_flags);
  1313. MethodInfo mi2;
  1314. mi2.name = "call_group";
  1315. mi2.arguments.push_back(PropertyInfo(Variant::STRING, "group"));
  1316. mi2.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  1317. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group", &SceneTree::_call_group, mi2);
  1318. ClassDB::bind_method(D_METHOD("notify_group", "group", "notification"), &SceneTree::notify_group);
  1319. ClassDB::bind_method(D_METHOD("set_group", "group", "property", "value"), &SceneTree::set_group);
  1320. ClassDB::bind_method(D_METHOD("get_nodes_in_group", "group"), &SceneTree::_get_nodes_in_group);
  1321. ClassDB::bind_method(D_METHOD("set_current_scene", "child_node"), &SceneTree::set_current_scene);
  1322. ClassDB::bind_method(D_METHOD("get_current_scene"), &SceneTree::get_current_scene);
  1323. ClassDB::bind_method(D_METHOD("change_scene", "path"), &SceneTree::change_scene);
  1324. ClassDB::bind_method(D_METHOD("change_scene_to", "packed_scene"), &SceneTree::change_scene_to);
  1325. ClassDB::bind_method(D_METHOD("reload_current_scene"), &SceneTree::reload_current_scene);
  1326. ClassDB::bind_method(D_METHOD("_change_scene"), &SceneTree::_change_scene);
  1327. ClassDB::bind_method(D_METHOD("set_multiplayer", "multiplayer"), &SceneTree::set_multiplayer);
  1328. ClassDB::bind_method(D_METHOD("get_multiplayer"), &SceneTree::get_multiplayer);
  1329. ClassDB::bind_method(D_METHOD("set_multiplayer_poll_enabled", "enabled"), &SceneTree::set_multiplayer_poll_enabled);
  1330. ClassDB::bind_method(D_METHOD("is_multiplayer_poll_enabled"), &SceneTree::is_multiplayer_poll_enabled);
  1331. ClassDB::bind_method(D_METHOD("set_network_peer", "peer"), &SceneTree::set_network_peer);
  1332. ClassDB::bind_method(D_METHOD("get_network_peer"), &SceneTree::get_network_peer);
  1333. ClassDB::bind_method(D_METHOD("is_network_server"), &SceneTree::is_network_server);
  1334. ClassDB::bind_method(D_METHOD("has_network_peer"), &SceneTree::has_network_peer);
  1335. ClassDB::bind_method(D_METHOD("get_network_connected_peers"), &SceneTree::get_network_connected_peers);
  1336. ClassDB::bind_method(D_METHOD("get_network_unique_id"), &SceneTree::get_network_unique_id);
  1337. ClassDB::bind_method(D_METHOD("get_rpc_sender_id"), &SceneTree::get_rpc_sender_id);
  1338. ClassDB::bind_method(D_METHOD("set_refuse_new_network_connections", "refuse"), &SceneTree::set_refuse_new_network_connections);
  1339. ClassDB::bind_method(D_METHOD("is_refusing_new_network_connections"), &SceneTree::is_refusing_new_network_connections);
  1340. ClassDB::bind_method(D_METHOD("_network_peer_connected"), &SceneTree::_network_peer_connected);
  1341. ClassDB::bind_method(D_METHOD("_network_peer_disconnected"), &SceneTree::_network_peer_disconnected);
  1342. ClassDB::bind_method(D_METHOD("_connected_to_server"), &SceneTree::_connected_to_server);
  1343. ClassDB::bind_method(D_METHOD("_connection_failed"), &SceneTree::_connection_failed);
  1344. ClassDB::bind_method(D_METHOD("_server_disconnected"), &SceneTree::_server_disconnected);
  1345. ClassDB::bind_method(D_METHOD("set_use_font_oversampling", "enable"), &SceneTree::set_use_font_oversampling);
  1346. ClassDB::bind_method(D_METHOD("is_using_font_oversampling"), &SceneTree::is_using_font_oversampling);
  1347. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_collisions_hint"), "set_debug_collisions_hint", "is_debugging_collisions_hint");
  1348. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_navigation_hint"), "set_debug_navigation_hint", "is_debugging_navigation_hint");
  1349. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_pause", "is_paused");
  1350. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_network_connections"), "set_refuse_new_network_connections", "is_refusing_new_network_connections");
  1351. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_font_oversampling"), "set_use_font_oversampling", "is_using_font_oversampling");
  1352. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edited_scene_root", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_edited_scene_root", "get_edited_scene_root");
  1353. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "current_scene", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_current_scene", "get_current_scene");
  1354. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "network_peer", PROPERTY_HINT_RESOURCE_TYPE, "NetworkedMultiplayerPeer", 0), "set_network_peer", "get_network_peer");
  1355. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "", "get_root");
  1356. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "set_multiplayer", "get_multiplayer");
  1357. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multiplayer_poll"), "set_multiplayer_poll_enabled", "is_multiplayer_poll_enabled");
  1358. ADD_SIGNAL(MethodInfo("tree_changed"));
  1359. ADD_SIGNAL(MethodInfo("node_added", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1360. ADD_SIGNAL(MethodInfo("node_removed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1361. ADD_SIGNAL(MethodInfo("screen_resized"));
  1362. ADD_SIGNAL(MethodInfo("node_configuration_warning_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1363. ADD_SIGNAL(MethodInfo("idle_frame"));
  1364. ADD_SIGNAL(MethodInfo("physics_frame"));
  1365. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::POOL_STRING_ARRAY, "files"), PropertyInfo(Variant::INT, "screen")));
  1366. ADD_SIGNAL(MethodInfo("network_peer_connected", PropertyInfo(Variant::INT, "id")));
  1367. ADD_SIGNAL(MethodInfo("network_peer_disconnected", PropertyInfo(Variant::INT, "id")));
  1368. ADD_SIGNAL(MethodInfo("connected_to_server"));
  1369. ADD_SIGNAL(MethodInfo("connection_failed"));
  1370. ADD_SIGNAL(MethodInfo("server_disconnected"));
  1371. BIND_ENUM_CONSTANT(GROUP_CALL_DEFAULT);
  1372. BIND_ENUM_CONSTANT(GROUP_CALL_REVERSE);
  1373. BIND_ENUM_CONSTANT(GROUP_CALL_REALTIME);
  1374. BIND_ENUM_CONSTANT(GROUP_CALL_UNIQUE);
  1375. BIND_ENUM_CONSTANT(STRETCH_MODE_DISABLED);
  1376. BIND_ENUM_CONSTANT(STRETCH_MODE_2D);
  1377. BIND_ENUM_CONSTANT(STRETCH_MODE_VIEWPORT);
  1378. BIND_ENUM_CONSTANT(STRETCH_ASPECT_IGNORE);
  1379. BIND_ENUM_CONSTANT(STRETCH_ASPECT_KEEP);
  1380. BIND_ENUM_CONSTANT(STRETCH_ASPECT_KEEP_WIDTH);
  1381. BIND_ENUM_CONSTANT(STRETCH_ASPECT_KEEP_HEIGHT);
  1382. BIND_ENUM_CONSTANT(STRETCH_ASPECT_EXPAND);
  1383. }
  1384. SceneTree *SceneTree::singleton = NULL;
  1385. SceneTree::IdleCallback SceneTree::idle_callbacks[SceneTree::MAX_IDLE_CALLBACKS];
  1386. int SceneTree::idle_callback_count = 0;
  1387. void SceneTree::_call_idle_callbacks() {
  1388. for (int i = 0; i < idle_callback_count; i++) {
  1389. idle_callbacks[i]();
  1390. }
  1391. }
  1392. void SceneTree::add_idle_callback(IdleCallback p_callback) {
  1393. ERR_FAIL_COND(idle_callback_count >= MAX_IDLE_CALLBACKS);
  1394. idle_callbacks[idle_callback_count++] = p_callback;
  1395. }
  1396. void SceneTree::set_use_font_oversampling(bool p_oversampling) {
  1397. use_font_oversampling = p_oversampling;
  1398. if (use_font_oversampling) {
  1399. DynamicFontAtSize::font_oversampling = OS::get_singleton()->get_window_size().width / root->get_visible_rect().size.width;
  1400. } else {
  1401. DynamicFontAtSize::font_oversampling = 1.0;
  1402. }
  1403. }
  1404. bool SceneTree::is_using_font_oversampling() const {
  1405. return use_font_oversampling;
  1406. }
  1407. SceneTree::SceneTree() {
  1408. singleton = this;
  1409. _quit = false;
  1410. accept_quit = true;
  1411. quit_on_go_back = true;
  1412. initialized = false;
  1413. #ifdef DEBUG_ENABLED
  1414. debug_collisions_hint = false;
  1415. debug_navigation_hint = false;
  1416. #endif
  1417. debug_collisions_color = GLOBAL_DEF("debug/shapes/collision/shape_color", Color(0.0, 0.6, 0.7, 0.5));
  1418. debug_collision_contact_color = GLOBAL_DEF("debug/shapes/collision/contact_color", Color(1.0, 0.2, 0.1, 0.8));
  1419. debug_navigation_color = GLOBAL_DEF("debug/shapes/navigation/geometry_color", Color(0.1, 1.0, 0.7, 0.4));
  1420. debug_navigation_disabled_color = GLOBAL_DEF("debug/shapes/navigation/disabled_geometry_color", Color(1.0, 0.7, 0.1, 0.4));
  1421. collision_debug_contacts = GLOBAL_DEF("debug/shapes/collision/max_contacts_displayed", 10000);
  1422. 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
  1423. tree_version = 1;
  1424. physics_process_time = 1;
  1425. idle_process_time = 1;
  1426. root = NULL;
  1427. current_frame = 0;
  1428. current_event = 0;
  1429. tree_changed_name = "tree_changed";
  1430. node_added_name = "node_added";
  1431. node_removed_name = "node_removed";
  1432. ugc_locked = false;
  1433. call_lock = 0;
  1434. root_lock = 0;
  1435. node_count = 0;
  1436. //create with mainloop
  1437. root = memnew(Viewport);
  1438. root->set_name("root");
  1439. root->set_handle_input_locally(false);
  1440. if (!root->get_world().is_valid())
  1441. root->set_world(Ref<World>(memnew(World)));
  1442. // Initialize network state
  1443. multiplayer_poll = true;
  1444. set_multiplayer(Ref<MultiplayerAPI>(memnew(MultiplayerAPI)));
  1445. //root->set_world_2d( Ref<World2D>( memnew( World2D )));
  1446. root->set_as_audio_listener(true);
  1447. root->set_as_audio_listener_2d(true);
  1448. current_scene = NULL;
  1449. int ref_atlas_size = GLOBAL_DEF("rendering/quality/reflections/atlas_size", 2048);
  1450. 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
  1451. int ref_atlas_subdiv = GLOBAL_DEF("rendering/quality/reflections/atlas_subdiv", 8);
  1452. 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
  1453. int msaa_mode = GLOBAL_DEF("rendering/quality/filters/msaa", 0);
  1454. 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"));
  1455. root->set_msaa(Viewport::MSAA(msaa_mode));
  1456. GLOBAL_DEF("rendering/quality/depth/hdr", true);
  1457. GLOBAL_DEF("rendering/quality/depth/hdr.mobile", false);
  1458. bool hdr = GLOBAL_GET("rendering/quality/depth/hdr");
  1459. root->set_hdr(hdr);
  1460. VS::get_singleton()->scenario_set_reflection_atlas_size(root->get_world()->get_scenario(), ref_atlas_size, ref_atlas_subdiv);
  1461. { //load default fallback environment
  1462. //get possible extensions
  1463. List<String> exts;
  1464. ResourceLoader::get_recognized_extensions_for_type("Environment", &exts);
  1465. String ext_hint;
  1466. for (List<String>::Element *E = exts.front(); E; E = E->next()) {
  1467. if (ext_hint != String())
  1468. ext_hint += ",";
  1469. ext_hint += "*." + E->get();
  1470. }
  1471. //get path
  1472. String env_path = GLOBAL_DEF("rendering/environment/default_environment", "");
  1473. //setup property
  1474. ProjectSettings::get_singleton()->set_custom_property_info("rendering/environment/default_environment", PropertyInfo(Variant::STRING, "rendering/viewport/default_environment", PROPERTY_HINT_FILE, ext_hint));
  1475. env_path = env_path.strip_edges();
  1476. if (env_path != String()) {
  1477. Ref<Environment> env = ResourceLoader::load(env_path);
  1478. if (env.is_valid()) {
  1479. root->get_world()->set_fallback_environment(env);
  1480. } else {
  1481. if (Engine::get_singleton()->is_editor_hint()) {
  1482. //file was erased, clear the field.
  1483. ProjectSettings::get_singleton()->set("rendering/environment/default_environment", "");
  1484. } else {
  1485. //file was erased, notify user.
  1486. ERR_PRINTS(RTR("Default Environment as specified in Project Settings (Rendering -> Environment -> Default Environment) could not be loaded."));
  1487. }
  1488. }
  1489. }
  1490. }
  1491. stretch_mode = STRETCH_MODE_DISABLED;
  1492. stretch_aspect = STRETCH_ASPECT_IGNORE;
  1493. stretch_shrink = 1;
  1494. last_screen_size = Size2(OS::get_singleton()->get_window_size().width, OS::get_singleton()->get_window_size().height);
  1495. _update_root_rect();
  1496. if (ScriptDebugger::get_singleton()) {
  1497. ScriptDebugger::get_singleton()->set_request_scene_tree_message_func(_debugger_request_tree, this);
  1498. }
  1499. root->set_physics_object_picking(GLOBAL_DEF("physics/common/enable_object_picking", true));
  1500. #ifdef TOOLS_ENABLED
  1501. edited_scene_root = NULL;
  1502. #endif
  1503. #ifdef DEBUG_ENABLED
  1504. live_edit_funcs.udata = this;
  1505. live_edit_funcs.node_path_func = _live_edit_node_path_funcs;
  1506. live_edit_funcs.res_path_func = _live_edit_res_path_funcs;
  1507. live_edit_funcs.node_set_func = _live_edit_node_set_funcs;
  1508. live_edit_funcs.node_set_res_func = _live_edit_node_set_res_funcs;
  1509. live_edit_funcs.node_call_func = _live_edit_node_call_funcs;
  1510. live_edit_funcs.res_set_func = _live_edit_res_set_funcs;
  1511. live_edit_funcs.res_set_res_func = _live_edit_res_set_res_funcs;
  1512. live_edit_funcs.res_call_func = _live_edit_res_call_funcs;
  1513. live_edit_funcs.root_func = _live_edit_root_funcs;
  1514. live_edit_funcs.tree_create_node_func = _live_edit_create_node_funcs;
  1515. live_edit_funcs.tree_instance_node_func = _live_edit_instance_node_funcs;
  1516. live_edit_funcs.tree_remove_node_func = _live_edit_remove_node_funcs;
  1517. live_edit_funcs.tree_remove_and_keep_node_func = _live_edit_remove_and_keep_node_funcs;
  1518. live_edit_funcs.tree_restore_node_func = _live_edit_restore_node_funcs;
  1519. live_edit_funcs.tree_duplicate_node_func = _live_edit_duplicate_node_funcs;
  1520. live_edit_funcs.tree_reparent_node_func = _live_edit_reparent_node_funcs;
  1521. if (ScriptDebugger::get_singleton()) {
  1522. ScriptDebugger::get_singleton()->set_live_edit_funcs(&live_edit_funcs);
  1523. }
  1524. live_edit_root = NodePath("/root");
  1525. #endif
  1526. use_font_oversampling = false;
  1527. }
  1528. SceneTree::~SceneTree() {
  1529. }