scene_main_loop.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  1. /*************************************************************************/
  2. /* scene_main_loop.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "scene_main_loop.h"
  30. #include "print_string.h"
  31. #include "os/os.h"
  32. #include "message_queue.h"
  33. #include "node.h"
  34. #include "globals.h"
  35. #include <stdio.h>
  36. #include "os/keyboard.h"
  37. #include "servers/spatial_sound_2d_server.h"
  38. #include "servers/physics_2d_server.h"
  39. #include "servers/physics_server.h"
  40. #include "scene/scene_string_names.h"
  41. #include "io/resource_loader.h"
  42. #include "viewport.h"
  43. #include "scene/resources/packed_scene.h"
  44. #include "scene/resources/material.h"
  45. #include "scene/resources/mesh.h"
  46. void SceneTree::tree_changed() {
  47. tree_version++;
  48. emit_signal(tree_changed_name);
  49. }
  50. void SceneTree::node_removed(Node *p_node) {
  51. if (current_scene==p_node) {
  52. current_scene=NULL;
  53. }
  54. emit_signal(node_removed_name,p_node);
  55. if (call_lock>0)
  56. call_skip.insert(p_node);
  57. }
  58. void SceneTree::add_to_group(const StringName& p_group, Node *p_node) {
  59. Map<StringName,Group>::Element *E=group_map.find(p_group);
  60. if (!E) {
  61. E=group_map.insert(p_group,Group());
  62. }
  63. if (E->get().nodes.find(p_node)!=-1) {
  64. ERR_EXPLAIN("Already in group: "+p_group);
  65. ERR_FAIL();
  66. }
  67. E->get().nodes.push_back(p_node);
  68. E->get().last_tree_version=0;
  69. }
  70. void SceneTree::remove_from_group(const StringName& p_group, Node *p_node) {
  71. Map<StringName,Group>::Element *E=group_map.find(p_group);
  72. ERR_FAIL_COND(!E);
  73. E->get().nodes.erase(p_node);
  74. if (E->get().nodes.empty())
  75. group_map.erase(E);
  76. }
  77. void SceneTree::_flush_transform_notifications() {
  78. SelfList<Node>* n = xform_change_list.first();
  79. while(n) {
  80. Node *node=n->self();
  81. SelfList<Node>* nx = n->next();
  82. xform_change_list.remove(n);
  83. n=nx;
  84. node->notification(NOTIFICATION_TRANSFORM_CHANGED);
  85. }
  86. }
  87. void SceneTree::_flush_ugc() {
  88. ugc_locked=true;
  89. while (unique_group_calls.size()) {
  90. Map<UGCall,Vector<Variant> >::Element *E=unique_group_calls.front();
  91. Variant v[VARIANT_ARG_MAX];
  92. for(int i=0;i<E->get().size();i++)
  93. v[i]=E->get()[i];
  94. call_group(GROUP_CALL_REALTIME,E->key().group,E->key().call,v[0],v[1],v[2],v[3],v[4]);
  95. unique_group_calls.erase(E);
  96. }
  97. ugc_locked=false;
  98. }
  99. void SceneTree::_update_group_order(Group& g) {
  100. if (g.last_tree_version==tree_version)
  101. return;
  102. if (g.nodes.empty())
  103. return;
  104. Node **nodes = &g.nodes[0];
  105. int node_count=g.nodes.size();
  106. SortArray<Node*,Node::Comparator> node_sort;
  107. node_sort.sort(nodes,node_count);
  108. g.last_tree_version=tree_version;
  109. }
  110. void SceneTree::call_group(uint32_t p_call_flags,const StringName& p_group,const StringName& p_function,VARIANT_ARG_DECLARE) {
  111. Map<StringName,Group>::Element *E=group_map.find(p_group);
  112. if (!E)
  113. return;
  114. Group &g=E->get();
  115. if (g.nodes.empty())
  116. return;
  117. _update_group_order(g);
  118. if (p_call_flags&GROUP_CALL_UNIQUE && !(p_call_flags&GROUP_CALL_REALTIME)) {
  119. ERR_FAIL_COND(ugc_locked);
  120. UGCall ug;
  121. ug.call=p_function;
  122. ug.group=p_group;
  123. if (unique_group_calls.has(ug))
  124. return;
  125. VARIANT_ARGPTRS;
  126. Vector<Variant> args;
  127. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  128. if (argptr[i]->get_type()==Variant::NIL)
  129. break;
  130. args.push_back(*argptr[i]);
  131. }
  132. unique_group_calls[ug]=args;
  133. return;
  134. }
  135. Vector<Node*> nodes_copy = g.nodes;
  136. Node **nodes = &nodes_copy[0];
  137. int node_count=nodes_copy.size();
  138. call_lock++;
  139. if (p_call_flags&GROUP_CALL_REVERSE) {
  140. for(int i=node_count-1;i>=0;i--) {
  141. if (call_lock && call_skip.has(nodes[i]))
  142. continue;
  143. if (p_call_flags&GROUP_CALL_REALTIME) {
  144. if (p_call_flags&GROUP_CALL_MULIILEVEL)
  145. nodes[i]->call_multilevel(p_function,VARIANT_ARG_PASS);
  146. else
  147. nodes[i]->call(p_function,VARIANT_ARG_PASS);
  148. } else
  149. MessageQueue::get_singleton()->push_call(nodes[i],p_function,VARIANT_ARG_PASS);
  150. }
  151. } else {
  152. for(int i=0;i<node_count;i++) {
  153. if (call_lock && call_skip.has(nodes[i]))
  154. continue;
  155. if (p_call_flags&GROUP_CALL_REALTIME) {
  156. if (p_call_flags&GROUP_CALL_MULIILEVEL)
  157. nodes[i]->call_multilevel(p_function,VARIANT_ARG_PASS);
  158. else
  159. nodes[i]->call(p_function,VARIANT_ARG_PASS);
  160. } else
  161. MessageQueue::get_singleton()->push_call(nodes[i],p_function,VARIANT_ARG_PASS);
  162. }
  163. }
  164. call_lock--;
  165. if (call_lock==0)
  166. call_skip.clear();
  167. }
  168. void SceneTree::notify_group(uint32_t p_call_flags,const StringName& p_group,int p_notification) {
  169. Map<StringName,Group>::Element *E=group_map.find(p_group);
  170. if (!E)
  171. return;
  172. Group &g=E->get();
  173. if (g.nodes.empty())
  174. return;
  175. _update_group_order(g);
  176. Vector<Node*> nodes_copy = g.nodes;
  177. Node **nodes = &nodes_copy[0];
  178. int node_count=nodes_copy.size();
  179. call_lock++;
  180. if (p_call_flags&GROUP_CALL_REVERSE) {
  181. for(int i=node_count-1;i>=0;i--) {
  182. if (call_lock && call_skip.has(nodes[i]))
  183. continue;
  184. if (p_call_flags&GROUP_CALL_REALTIME)
  185. nodes[i]->notification(p_notification);
  186. else
  187. MessageQueue::get_singleton()->push_notification(nodes[i],p_notification);
  188. }
  189. } else {
  190. for(int i=0;i<node_count;i++) {
  191. if (call_lock && call_skip.has(nodes[i]))
  192. continue;
  193. if (p_call_flags&GROUP_CALL_REALTIME)
  194. nodes[i]->notification(p_notification);
  195. else
  196. MessageQueue::get_singleton()->push_notification(nodes[i],p_notification);
  197. }
  198. }
  199. call_lock--;
  200. if (call_lock==0)
  201. call_skip.clear();
  202. }
  203. void SceneTree::set_group(uint32_t p_call_flags,const StringName& p_group,const String& p_name,const Variant& p_value) {
  204. Map<StringName,Group>::Element *E=group_map.find(p_group);
  205. if (!E)
  206. return;
  207. Group &g=E->get();
  208. if (g.nodes.empty())
  209. return;
  210. _update_group_order(g);
  211. Vector<Node*> nodes_copy = g.nodes;
  212. Node **nodes = &nodes_copy[0];
  213. int node_count=nodes_copy.size();
  214. call_lock++;
  215. if (p_call_flags&GROUP_CALL_REVERSE) {
  216. for(int i=node_count-1;i>=0;i--) {
  217. if (call_lock && call_skip.has(nodes[i]))
  218. continue;
  219. if (p_call_flags&GROUP_CALL_REALTIME)
  220. nodes[i]->set(p_name,p_value);
  221. else
  222. MessageQueue::get_singleton()->push_set(nodes[i],p_name,p_value);
  223. }
  224. } else {
  225. for(int i=0;i<node_count;i++) {
  226. if (call_lock && call_skip.has(nodes[i]))
  227. continue;
  228. if (p_call_flags&GROUP_CALL_REALTIME)
  229. nodes[i]->set(p_name,p_value);
  230. else
  231. MessageQueue::get_singleton()->push_set(nodes[i],p_name,p_value);
  232. }
  233. }
  234. call_lock--;
  235. if (call_lock==0)
  236. call_skip.clear();
  237. }
  238. void SceneTree::set_input_as_handled() {
  239. input_handled=true;
  240. }
  241. void SceneTree::input_text( const String& p_text ) {
  242. root_lock++;
  243. call_group(GROUP_CALL_REALTIME,"_viewports","_vp_input_text",p_text); //special one for GUI, as controls use their own process check
  244. root_lock--;
  245. }
  246. void SceneTree::input_event( const InputEvent& p_event ) {
  247. if (is_editor_hint() && (p_event.type==InputEvent::JOYSTICK_MOTION || p_event.type==InputEvent::JOYSTICK_BUTTON))
  248. return; //avoid joy input on editor
  249. root_lock++;
  250. //last_id=p_event.ID;
  251. input_handled=false;
  252. InputEvent ev = p_event;
  253. ev.ID=++last_id; //this should work better
  254. #if 0
  255. switch(ev.type) {
  256. case InputEvent::MOUSE_BUTTON: {
  257. Matrix32 ai = root->get_final_transform().affine_inverse();
  258. Vector2 g = ai.xform(Vector2(ev.mouse_button.global_x,ev.mouse_button.global_y));
  259. Vector2 l = ai.xform(Vector2(ev.mouse_button.x,ev.mouse_button.y));
  260. ev.mouse_button.x=l.x;
  261. ev.mouse_button.y=l.y;
  262. ev.mouse_button.global_x=g.x;
  263. ev.mouse_button.global_y=g.y;
  264. } break;
  265. case InputEvent::MOUSE_MOTION: {
  266. Matrix32 ai = root->get_final_transform().affine_inverse();
  267. Vector2 g = ai.xform(Vector2(ev.mouse_motion.global_x,ev.mouse_motion.global_y));
  268. Vector2 l = ai.xform(Vector2(ev.mouse_motion.x,ev.mouse_motion.y));
  269. Vector2 r = ai.xform(Vector2(ev.mouse_motion.relative_x,ev.mouse_motion.relative_y));
  270. ev.mouse_motion.x=l.x;
  271. ev.mouse_motion.y=l.y;
  272. ev.mouse_motion.global_x=g.x;
  273. ev.mouse_motion.global_y=g.y;
  274. ev.mouse_motion.relative_x=r.x;
  275. ev.mouse_motion.relative_y=r.y;
  276. } break;
  277. case InputEvent::SCREEN_TOUCH: {
  278. Matrix32 ai = root->get_final_transform().affine_inverse();
  279. Vector2 t = ai.xform(Vector2(ev.screen_touch.x,ev.screen_touch.y));
  280. ev.screen_touch.x=t.x;
  281. ev.screen_touch.y=t.y;
  282. } break;
  283. case InputEvent::SCREEN_DRAG: {
  284. Matrix32 ai = root->get_final_transform().affine_inverse();
  285. Vector2 t = ai.xform(Vector2(ev.screen_drag.x,ev.screen_drag.y));
  286. Vector2 r = ai.xform(Vector2(ev.screen_drag.relative_x,ev.screen_drag.relative_y));
  287. Vector2 s = ai.xform(Vector2(ev.screen_drag.speed_x,ev.screen_drag.speed_y));
  288. ev.screen_drag.x=t.x;
  289. ev.screen_drag.y=t.y;
  290. ev.screen_drag.relative_x=r.x;
  291. ev.screen_drag.relative_y=r.y;
  292. ev.screen_drag.speed_x=s.x;
  293. ev.screen_drag.speed_y=s.y;
  294. } break;
  295. }
  296. #endif
  297. MainLoop::input_event(ev);
  298. #if 0
  299. _call_input_pause("input","_input",ev);
  300. call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"_gui_input","_gui_input",p_event); //special one for GUI, as controls use their own process check
  301. //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"input","_input",ev);
  302. /*if (ev.type==InputEvent::KEY && ev.key.pressed && !ev.key.echo && ev.key.scancode==KEY_F12) {
  303. print_line("RAM: "+itos(Memory::get_static_mem_usage()));
  304. print_line("DRAM: "+itos(Memory::get_dynamic_mem_usage()));
  305. }
  306. */
  307. //if (ev.type==InputEvent::KEY && ev.key.pressed && !ev.key.echo && ev.key.scancode==KEY_F11) {
  308. // Memory::dump_static_mem_to_file("memdump.txt");
  309. //}
  310. //transform for the rest
  311. #else
  312. call_group(GROUP_CALL_REALTIME,"_viewports","_vp_input",ev); //special one for GUI, as controls use their own process check
  313. #endif
  314. if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_remote() && ev.type==InputEvent::KEY && ev.key.pressed && !ev.key.echo && ev.key.scancode==KEY_F8) {
  315. ScriptDebugger::get_singleton()->request_quit();
  316. }
  317. _flush_ugc();
  318. root_lock--;
  319. MessageQueue::get_singleton()->flush(); //small little hack
  320. root_lock++;
  321. if (!input_handled) {
  322. #if 0
  323. _call_input_pause("unhandled_input","_unhandled_input",ev);
  324. //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_input","_unhandled_input",ev);
  325. if (!input_handled && ev.type==InputEvent::KEY) {
  326. _call_input_pause("unhandled_key_input","_unhandled_key_input",ev);
  327. //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_key_input","_unhandled_key_input",ev);
  328. }
  329. #else
  330. call_group(GROUP_CALL_REALTIME,"_viewports","_vp_unhandled_input",ev); //special one for GUI, as controls use their own process check
  331. #endif
  332. input_handled=true;
  333. _flush_ugc();
  334. root_lock--;
  335. MessageQueue::get_singleton()->flush(); //small little hack
  336. } else {
  337. input_handled=true;
  338. root_lock--;
  339. }
  340. }
  341. void SceneTree::init() {
  342. //_quit=false;
  343. accept_quit=true;
  344. initialized=true;
  345. input_handled=false;
  346. pause=false;
  347. root->_set_tree(this);
  348. MainLoop::init();
  349. }
  350. bool SceneTree::iteration(float p_time) {
  351. root_lock++;
  352. current_frame++;
  353. _flush_transform_notifications();
  354. MainLoop::iteration(p_time);
  355. fixed_process_time=p_time;
  356. emit_signal("fixed_frame");
  357. _notify_group_pause("fixed_process",Node::NOTIFICATION_FIXED_PROCESS);
  358. _flush_ugc();
  359. _flush_transform_notifications();
  360. call_group(GROUP_CALL_REALTIME,"_viewports","update_worlds");
  361. root_lock--;
  362. _flush_delete_queue();
  363. return _quit;
  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. emit_signal("idle_frame");
  373. _flush_transform_notifications();
  374. _notify_group_pause("idle_process",Node::NOTIFICATION_PROCESS);
  375. Size2 win_size=Size2( OS::get_singleton()->get_video_mode().width, OS::get_singleton()->get_video_mode().height );
  376. if(win_size!=last_screen_size) {
  377. last_screen_size=win_size;
  378. _update_root_rect();
  379. emit_signal("screen_resized");
  380. }
  381. _flush_ugc();
  382. _flush_transform_notifications(); //transforms after world update, to avoid unnecesary enter/exit notifications
  383. call_group(GROUP_CALL_REALTIME,"_viewports","update_worlds");
  384. root_lock--;
  385. _flush_delete_queue();
  386. return _quit;
  387. }
  388. void SceneTree::finish() {
  389. _flush_delete_queue();
  390. _flush_ugc();
  391. initialized=false;
  392. MainLoop::finish();
  393. if (root) {
  394. root->_set_tree(NULL);
  395. memdelete(root); //delete root
  396. }
  397. }
  398. void SceneTree::quit() {
  399. _quit=true;
  400. }
  401. void SceneTree::_notification(int p_notification) {
  402. switch (p_notification) {
  403. case NOTIFICATION_WM_QUIT_REQUEST: {
  404. get_root()->propagate_notification(p_notification);
  405. if (accept_quit) {
  406. _quit=true;
  407. break;
  408. }
  409. } break;
  410. case NOTIFICATION_OS_MEMORY_WARNING:
  411. case NOTIFICATION_WM_FOCUS_IN:
  412. case NOTIFICATION_WM_FOCUS_OUT: {
  413. get_root()->propagate_notification(p_notification);
  414. } break;
  415. case NOTIFICATION_WM_UNFOCUS_REQUEST: {
  416. notify_group(GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"input",NOTIFICATION_WM_UNFOCUS_REQUEST);
  417. } break;
  418. default:
  419. break;
  420. };
  421. };
  422. void SceneTree::set_auto_accept_quit(bool p_enable) {
  423. accept_quit=p_enable;
  424. }
  425. void SceneTree::set_editor_hint(bool p_enabled) {
  426. editor_hint=p_enabled;
  427. }
  428. bool SceneTree::is_node_being_edited(const Node* p_node) const {
  429. #ifdef TOOLS_ENABLED
  430. return editor_hint && edited_scene_root && edited_scene_root->is_a_parent_of(p_node);
  431. #else
  432. return false;
  433. #endif
  434. }
  435. bool SceneTree::is_editor_hint() const {
  436. return editor_hint;
  437. }
  438. void SceneTree::set_debug_collisions_hint(bool p_enabled) {
  439. debug_collisions_hint=p_enabled;
  440. }
  441. bool SceneTree::is_debugging_collisions_hint() const {
  442. return debug_collisions_hint;
  443. }
  444. void SceneTree::set_debug_navigation_hint(bool p_enabled) {
  445. debug_navigation_hint=p_enabled;
  446. }
  447. bool SceneTree::is_debugging_navigation_hint() const {
  448. return debug_navigation_hint;
  449. }
  450. void SceneTree::set_debug_collisions_color(const Color& p_color) {
  451. debug_collisions_color=p_color;
  452. }
  453. Color SceneTree::get_debug_collisions_color() const {
  454. return debug_collisions_color;
  455. }
  456. void SceneTree::set_debug_collision_contact_color(const Color& p_color) {
  457. debug_collision_contact_color=p_color;
  458. }
  459. Color SceneTree::get_debug_collision_contact_color() const {
  460. return debug_collision_contact_color;
  461. }
  462. void SceneTree::set_debug_navigation_color(const Color& p_color) {
  463. debug_navigation_color=p_color;
  464. }
  465. Color SceneTree::get_debug_navigation_color() const {
  466. return debug_navigation_color;
  467. }
  468. void SceneTree::set_debug_navigation_disabled_color(const Color& p_color) {
  469. debug_navigation_disabled_color=p_color;
  470. }
  471. Color SceneTree::get_debug_navigation_disabled_color() const {
  472. return debug_navigation_disabled_color;
  473. }
  474. Ref<Material> SceneTree::get_debug_navigation_material() {
  475. if (navigation_material.is_valid())
  476. return navigation_material;
  477. Ref<FixedMaterial> line_material = Ref<FixedMaterial>( memnew( FixedMaterial ));
  478. line_material->set_flag(Material::FLAG_UNSHADED, true);
  479. line_material->set_line_width(3.0);
  480. line_material->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  481. line_material->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, true);
  482. line_material->set_parameter(FixedMaterial::PARAM_DIFFUSE,get_debug_navigation_color());
  483. navigation_material=line_material;
  484. return navigation_material;
  485. }
  486. Ref<Material> SceneTree::get_debug_navigation_disabled_material(){
  487. if (navigation_disabled_material.is_valid())
  488. return navigation_disabled_material;
  489. Ref<FixedMaterial> line_material = Ref<FixedMaterial>( memnew( FixedMaterial ));
  490. line_material->set_flag(Material::FLAG_UNSHADED, true);
  491. line_material->set_line_width(3.0);
  492. line_material->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  493. line_material->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, true);
  494. line_material->set_parameter(FixedMaterial::PARAM_DIFFUSE,get_debug_navigation_disabled_color());
  495. navigation_disabled_material=line_material;
  496. return navigation_disabled_material;
  497. }
  498. Ref<Material> SceneTree::get_debug_collision_material() {
  499. if (collision_material.is_valid())
  500. return collision_material;
  501. Ref<FixedMaterial> line_material = Ref<FixedMaterial>( memnew( FixedMaterial ));
  502. line_material->set_flag(Material::FLAG_UNSHADED, true);
  503. line_material->set_line_width(3.0);
  504. line_material->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  505. line_material->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, true);
  506. line_material->set_parameter(FixedMaterial::PARAM_DIFFUSE,get_debug_collisions_color());
  507. collision_material=line_material;
  508. return collision_material;
  509. }
  510. Ref<Mesh> SceneTree::get_debug_contact_mesh() {
  511. if (debug_contact_mesh.is_valid())
  512. return debug_contact_mesh;
  513. debug_contact_mesh = Ref<Mesh>( memnew( Mesh ) );
  514. Ref<FixedMaterial> mat = memnew( FixedMaterial );
  515. mat->set_flag(Material::FLAG_UNSHADED,true);
  516. mat->set_flag(Material::FLAG_DOUBLE_SIDED,true);
  517. mat->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA,true);
  518. mat->set_parameter(FixedMaterial::PARAM_DIFFUSE,get_debug_collision_contact_color());
  519. Vector3 diamond[6]={
  520. Vector3(-1, 0, 0),
  521. Vector3( 1, 0, 0),
  522. Vector3( 0, -1, 0),
  523. Vector3( 0, 1, 0),
  524. Vector3( 0, 0, -1),
  525. Vector3( 0, 0, 1)
  526. };
  527. int diamond_faces[8*3]={
  528. 0,2,4,
  529. 0,3,4,
  530. 1,2,4,
  531. 1,3,4,
  532. 0,2,5,
  533. 0,3,5,
  534. 1,2,5,
  535. 1,3,5,
  536. };
  537. DVector<int> indices;
  538. for(int i=0;i<8*3;i++)
  539. indices.push_back(diamond_faces[i]);
  540. DVector<Vector3> vertices;
  541. for(int i=0;i<6;i++)
  542. vertices.push_back(diamond[i]*0.1);
  543. Array arr;
  544. arr.resize(Mesh::ARRAY_MAX);
  545. arr[Mesh::ARRAY_VERTEX]=vertices;
  546. arr[Mesh::ARRAY_INDEX]=indices;
  547. debug_contact_mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES,arr);
  548. debug_contact_mesh->surface_set_material(0,mat);
  549. return debug_contact_mesh;
  550. }
  551. void SceneTree::set_pause(bool p_enabled) {
  552. if (p_enabled==pause)
  553. return;
  554. pause=p_enabled;
  555. PhysicsServer::get_singleton()->set_active(!p_enabled);
  556. Physics2DServer::get_singleton()->set_active(!p_enabled);
  557. if (get_root())
  558. get_root()->propagate_notification(p_enabled ? Node::NOTIFICATION_PAUSED : Node::NOTIFICATION_UNPAUSED);
  559. }
  560. bool SceneTree::is_paused() const {
  561. return pause;
  562. }
  563. void SceneTree::_call_input_pause(const StringName& p_group,const StringName& p_method,const InputEvent& p_input) {
  564. Map<StringName,Group>::Element *E=group_map.find(p_group);
  565. if (!E)
  566. return;
  567. Group &g=E->get();
  568. if (g.nodes.empty())
  569. return;
  570. _update_group_order(g);
  571. //copy, so copy on write happens in case something is removed from process while being called
  572. //performance is not lost because only if something is added/removed the vector is copied.
  573. Vector<Node*> nodes_copy = g.nodes;
  574. int node_count=nodes_copy.size();
  575. Node **nodes = &nodes_copy[0];
  576. Variant arg=p_input;
  577. const Variant *v[1]={&arg};
  578. call_lock++;
  579. for(int i=node_count-1;i>=0;i--) {
  580. if (input_handled)
  581. break;
  582. Node *n = nodes[i];
  583. if (call_lock && call_skip.has(n))
  584. continue;
  585. if (!n->can_process())
  586. continue;
  587. Variant::CallError ce;
  588. n->call_multilevel(p_method,(const Variant**)v,1);
  589. //ERR_FAIL_COND(node_count != g.nodes.size());
  590. }
  591. call_lock--;
  592. if (call_lock==0)
  593. call_skip.clear();
  594. }
  595. void SceneTree::_notify_group_pause(const StringName& p_group,int p_notification) {
  596. Map<StringName,Group>::Element *E=group_map.find(p_group);
  597. if (!E)
  598. return;
  599. Group &g=E->get();
  600. if (g.nodes.empty())
  601. return;
  602. _update_group_order(g);
  603. //copy, so copy on write happens in case something is removed from process while being called
  604. //performance is not lost because only if something is added/removed the vector is copied.
  605. Vector<Node*> nodes_copy = g.nodes;
  606. int node_count=nodes_copy.size();
  607. Node **nodes = &nodes_copy[0];
  608. call_lock++;
  609. for(int i=0;i<node_count;i++) {
  610. Node *n = nodes[i];
  611. if (call_lock && call_skip.has(n))
  612. continue;
  613. if (!n->can_process())
  614. continue;
  615. n->notification(p_notification);
  616. //ERR_FAIL_COND(node_count != g.nodes.size());
  617. }
  618. call_lock--;
  619. if (call_lock==0)
  620. call_skip.clear();
  621. }
  622. /*
  623. void SceneMainLoop::_update_listener_2d() {
  624. if (listener_2d.is_valid()) {
  625. SpatialSound2DServer::get_singleton()->listener_set_space( listener_2d, world_2d->get_sound_space() );
  626. }
  627. }
  628. */
  629. uint32_t SceneTree::get_last_event_id() const {
  630. return last_id;
  631. }
  632. Variant SceneTree::_call_group(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
  633. r_error.error=Variant::CallError::CALL_OK;
  634. ERR_FAIL_COND_V(p_argcount<3,Variant());
  635. ERR_FAIL_COND_V(!p_args[0]->is_num(),Variant());
  636. ERR_FAIL_COND_V(p_args[1]->get_type()!=Variant::STRING,Variant());
  637. ERR_FAIL_COND_V(p_args[2]->get_type()!=Variant::STRING,Variant());
  638. int flags = *p_args[0];
  639. StringName group = *p_args[1];
  640. StringName method = *p_args[2];
  641. Variant v[VARIANT_ARG_MAX];
  642. for(int i=0;i<MIN(p_argcount-3,5);i++) {
  643. v[i]=*p_args[i+3];
  644. }
  645. call_group(flags,group,method,v[0],v[1],v[2],v[3],v[4]);
  646. return Variant();
  647. }
  648. int64_t SceneTree::get_frame() const {
  649. return current_frame;
  650. }
  651. Array SceneTree::_get_nodes_in_group(const StringName& p_group) {
  652. Array ret;
  653. Map<StringName,Group>::Element *E=group_map.find(p_group);
  654. if (!E)
  655. return ret;
  656. _update_group_order(E->get()); //update order just in case
  657. int nc = E->get().nodes.size();
  658. if (nc==0)
  659. return ret;
  660. ret.resize(nc);
  661. Node **ptr = E->get().nodes.ptr();
  662. for(int i=0;i<nc;i++) {
  663. ret[i]=ptr[i];
  664. }
  665. return ret;
  666. }
  667. bool SceneTree::has_group(const StringName& p_identifier) const {
  668. return group_map.has(p_identifier);
  669. }
  670. void SceneTree::get_nodes_in_group(const StringName& p_group,List<Node*> *p_list) {
  671. Map<StringName,Group>::Element *E=group_map.find(p_group);
  672. if (!E)
  673. return;
  674. _update_group_order(E->get()); //update order just in case
  675. int nc = E->get().nodes.size();
  676. if (nc==0)
  677. return;
  678. Node **ptr = E->get().nodes.ptr();
  679. for(int i=0;i<nc;i++) {
  680. p_list->push_back(ptr[i]);
  681. }
  682. }
  683. static void _fill_array(Node *p_node, Array& array, int p_level) {
  684. array.push_back(p_level);
  685. array.push_back(p_node->get_name());
  686. array.push_back(p_node->get_type());
  687. for(int i=0;i<p_node->get_child_count();i++) {
  688. _fill_array(p_node->get_child(i),array,p_level+1);
  689. }
  690. }
  691. void SceneTree::_debugger_request_tree(void *self) {
  692. SceneTree *sml = (SceneTree *)self;
  693. Array arr;
  694. _fill_array(sml->root,arr,0);
  695. ScriptDebugger::get_singleton()->send_message("scene_tree",arr);
  696. }
  697. void SceneTree::_flush_delete_queue() {
  698. _THREAD_SAFE_METHOD_
  699. while( delete_queue.size() ) {
  700. Object *obj = ObjectDB::get_instance( delete_queue.front()->get() );
  701. if (obj) {
  702. memdelete( obj );
  703. }
  704. delete_queue.pop_front();
  705. }
  706. }
  707. void SceneTree::queue_delete(Object *p_object) {
  708. _THREAD_SAFE_METHOD_
  709. ERR_FAIL_NULL(p_object);
  710. p_object->_is_queued_for_deletion = true;
  711. delete_queue.push_back(p_object->get_instance_ID());
  712. }
  713. int SceneTree::get_node_count() const {
  714. return node_count;
  715. }
  716. void SceneTree::_update_root_rect() {
  717. if (stretch_mode==STRETCH_MODE_DISABLED) {
  718. root->set_rect(Rect2(Point2(),last_screen_size));
  719. return; //user will take care
  720. }
  721. //actual screen video mode
  722. Size2 video_mode = Size2(OS::get_singleton()->get_video_mode().width,OS::get_singleton()->get_video_mode().height);
  723. Size2 desired_res = stretch_min;
  724. Size2 viewport_size;
  725. Size2 screen_size;
  726. float viewport_aspect = desired_res.get_aspect();
  727. float video_mode_aspect = video_mode.get_aspect();
  728. if (stretch_aspect==STRETCH_ASPECT_IGNORE || ABS(viewport_aspect - video_mode_aspect)<CMP_EPSILON) {
  729. //same aspect or ignore aspect
  730. viewport_size=desired_res;
  731. screen_size=video_mode;
  732. } else if (viewport_aspect < video_mode_aspect) {
  733. // screen ratio is smaller vertically
  734. if (stretch_aspect==STRETCH_ASPECT_KEEP_HEIGHT) {
  735. //will stretch horizontally
  736. viewport_size.x=desired_res.y*video_mode_aspect;
  737. viewport_size.y=desired_res.y;
  738. screen_size=video_mode;
  739. } else {
  740. //will need black bars
  741. viewport_size=desired_res;
  742. screen_size.x = video_mode.y * viewport_aspect;
  743. screen_size.y=video_mode.y;
  744. }
  745. } else {
  746. //screen ratio is smaller horizontally
  747. if (stretch_aspect==STRETCH_ASPECT_KEEP_WIDTH) {
  748. //will stretch horizontally
  749. viewport_size.x=desired_res.x;
  750. viewport_size.y=desired_res.x / video_mode_aspect;
  751. screen_size=video_mode;
  752. } else {
  753. //will need black bars
  754. viewport_size=desired_res;
  755. screen_size.x=video_mode.x;
  756. screen_size.y = video_mode.x / viewport_aspect;
  757. }
  758. }
  759. screen_size = screen_size.floor();
  760. viewport_size = viewport_size.floor();
  761. Size2 margin;
  762. Size2 offset;
  763. //black bars and margin
  764. if (screen_size.x < video_mode.x) {
  765. margin.x = Math::round((video_mode.x - screen_size.x)/2.0);
  766. VisualServer::get_singleton()->black_bars_set_margins(margin.x,0,margin.x,0);
  767. offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
  768. } else if (screen_size.y < video_mode.y) {
  769. margin.y = Math::round((video_mode.y - screen_size.y)/2.0);
  770. VisualServer::get_singleton()->black_bars_set_margins(0,margin.y,0,margin.y);
  771. offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
  772. } else {
  773. VisualServer::get_singleton()->black_bars_set_margins(0,0,0,0);
  774. }
  775. // print_line("VP SIZE: "+viewport_size+" OFFSET: "+offset+" = "+(offset*2+viewport_size));
  776. // print_line("SS: "+video_mode);
  777. switch (stretch_mode) {
  778. case STRETCH_MODE_2D: {
  779. // root->set_rect(Rect2(Point2(),video_mode));
  780. root->set_as_render_target(false);
  781. root->set_rect(Rect2(margin,screen_size));
  782. root->set_size_override_stretch(true);
  783. root->set_size_override(true,viewport_size);
  784. } break;
  785. case STRETCH_MODE_VIEWPORT: {
  786. root->set_rect(Rect2(Point2(),viewport_size));
  787. root->set_size_override_stretch(false);
  788. root->set_size_override(false,Size2());
  789. root->set_as_render_target(true);
  790. root->set_render_target_update_mode(Viewport::RENDER_TARGET_UPDATE_ALWAYS);
  791. root->set_render_target_to_screen_rect(Rect2(margin,screen_size));
  792. } break;
  793. }
  794. }
  795. void SceneTree::set_screen_stretch(StretchMode p_mode,StretchAspect p_aspect,const Size2 p_minsize) {
  796. stretch_mode=p_mode;
  797. stretch_aspect=p_aspect;
  798. stretch_min=p_minsize;
  799. _update_root_rect();
  800. }
  801. #ifdef TOOLS_ENABLED
  802. void SceneTree::set_edited_scene_root(Node *p_node) {
  803. edited_scene_root=p_node;
  804. }
  805. Node *SceneTree::get_edited_scene_root() const {
  806. return edited_scene_root;
  807. }
  808. #endif
  809. void SceneTree::set_current_scene(Node* p_scene) {
  810. ERR_FAIL_COND(p_scene && p_scene->get_parent()!=root);
  811. current_scene=p_scene;
  812. }
  813. Node* SceneTree::get_current_scene() const{
  814. return current_scene;
  815. }
  816. void SceneTree::_change_scene(Node* p_to) {
  817. if (current_scene) {
  818. memdelete( current_scene );
  819. current_scene=NULL;
  820. }
  821. if (p_to) {
  822. current_scene=p_to;
  823. root->add_child(p_to);
  824. }
  825. }
  826. Error SceneTree::change_scene(const String& p_path){
  827. Ref<PackedScene> new_scene = ResourceLoader::load(p_path);
  828. if (new_scene.is_null())
  829. return ERR_CANT_OPEN;
  830. return change_scene_to(new_scene);
  831. }
  832. Error SceneTree::change_scene_to(const Ref<PackedScene>& p_scene){
  833. Node *new_scene=NULL;
  834. if (p_scene.is_valid()) {
  835. new_scene = p_scene->instance();
  836. ERR_FAIL_COND_V(!new_scene,ERR_CANT_CREATE);
  837. }
  838. call_deferred("_change_scene",new_scene);
  839. return OK;
  840. }
  841. Error SceneTree::reload_current_scene() {
  842. ERR_FAIL_COND_V(!current_scene,ERR_UNCONFIGURED);
  843. String fname = current_scene->get_filename();
  844. return change_scene(fname);
  845. }
  846. void SceneTree::add_current_scene(Node * p_current) {
  847. current_scene=p_current;
  848. root->add_child(p_current);
  849. }
  850. #ifdef DEBUG_ENABLED
  851. void SceneTree::_live_edit_node_path_func(const NodePath &p_path,int p_id) {
  852. live_edit_node_path_cache[p_id]=p_path;
  853. }
  854. void SceneTree::_live_edit_res_path_func(const String &p_path,int p_id) {
  855. live_edit_resource_cache[p_id]=p_path;
  856. }
  857. void SceneTree::_live_edit_node_set_func(int p_id,const StringName& p_prop,const Variant& p_value) {
  858. if (!live_edit_node_path_cache.has(p_id))
  859. return;
  860. NodePath np = live_edit_node_path_cache[p_id];
  861. Node *base = NULL;
  862. if (root->has_node(live_edit_root))
  863. base = root->get_node(live_edit_root);
  864. Map<String,Set<Node*> >::Element *E=live_scene_edit_cache.find(live_edit_scene);
  865. if (!E)
  866. return; //scene not editable
  867. for(Set<Node*>::Element *F=E->get().front();F;F=F->next()) {
  868. Node *n=F->get();
  869. if (base && !base->is_a_parent_of(n))
  870. continue;
  871. if (!n->has_node(np))
  872. continue;
  873. Node *n2 = n->get_node(np);
  874. n2->set(p_prop,p_value);
  875. }
  876. }
  877. void SceneTree::_live_edit_node_set_res_func(int p_id,const StringName& p_prop,const String& p_value) {
  878. RES r = ResourceLoader::load(p_value);
  879. if (!r.is_valid())
  880. return;
  881. _live_edit_node_set_func(p_id,p_prop,r);
  882. }
  883. void SceneTree::_live_edit_node_call_func(int p_id,const StringName& p_method,VARIANT_ARG_DECLARE) {
  884. if (!live_edit_node_path_cache.has(p_id))
  885. return;
  886. NodePath np = live_edit_node_path_cache[p_id];
  887. Node *base = NULL;
  888. if (root->has_node(live_edit_root))
  889. base = root->get_node(live_edit_root);
  890. Map<String,Set<Node*> >::Element *E=live_scene_edit_cache.find(live_edit_scene);
  891. if (!E)
  892. return; //scene not editable
  893. for(Set<Node*>::Element *F=E->get().front();F;F=F->next()) {
  894. Node *n=F->get();
  895. if (base && !base->is_a_parent_of(n))
  896. continue;
  897. if (!n->has_node(np))
  898. continue;
  899. Node *n2 = n->get_node(np);
  900. n2->call(p_method,VARIANT_ARG_PASS);
  901. }
  902. }
  903. void SceneTree::_live_edit_res_set_func(int p_id,const StringName& p_prop,const Variant& p_value) {
  904. if (!live_edit_resource_cache.has(p_id))
  905. return;
  906. String resp = live_edit_resource_cache[p_id];
  907. if (!ResourceCache::has(resp))
  908. return;
  909. RES r = ResourceCache::get(resp);
  910. if (!r.is_valid())
  911. return;
  912. r->set(p_prop,p_value);
  913. }
  914. void SceneTree::_live_edit_res_set_res_func(int p_id,const StringName& p_prop,const String& p_value) {
  915. RES r = ResourceLoader::load(p_value);
  916. if (!r.is_valid())
  917. return;
  918. _live_edit_res_set_func(p_id,p_prop,r);
  919. }
  920. void SceneTree::_live_edit_res_call_func(int p_id,const StringName& p_method,VARIANT_ARG_DECLARE) {
  921. if (!live_edit_resource_cache.has(p_id))
  922. return;
  923. String resp = live_edit_resource_cache[p_id];
  924. if (!ResourceCache::has(resp))
  925. return;
  926. RES r = ResourceCache::get(resp);
  927. if (!r.is_valid())
  928. return;
  929. r->call(p_method,VARIANT_ARG_PASS);
  930. }
  931. void SceneTree::_live_edit_root_func(const NodePath& p_scene_path,const String& p_scene_from) {
  932. live_edit_root=p_scene_path;
  933. live_edit_scene=p_scene_from;
  934. }
  935. void SceneTree::_live_edit_create_node_func(const NodePath& p_parent,const String& p_type,const String& p_name) {
  936. Node *base = NULL;
  937. if (root->has_node(live_edit_root))
  938. base = root->get_node(live_edit_root);
  939. Map<String,Set<Node*> >::Element *E=live_scene_edit_cache.find(live_edit_scene);
  940. if (!E)
  941. return; //scene not editable
  942. for(Set<Node*>::Element *F=E->get().front();F;F=F->next()) {
  943. Node *n=F->get();
  944. if (base && !base->is_a_parent_of(n))
  945. continue;
  946. if (!n->has_node(p_parent))
  947. continue;
  948. Node *n2 = n->get_node(p_parent);
  949. Object *o = ObjectTypeDB::instance(p_type);
  950. if (!o)
  951. continue;
  952. Node *no=o->cast_to<Node>();
  953. no->set_name(p_name);
  954. n2->add_child(no);
  955. }
  956. }
  957. void SceneTree::_live_edit_instance_node_func(const NodePath& p_parent,const String& p_path,const String& p_name){
  958. Ref<PackedScene> ps = ResourceLoader::load(p_path);
  959. if (!ps.is_valid())
  960. return;
  961. Node *base = NULL;
  962. if (root->has_node(live_edit_root))
  963. base = root->get_node(live_edit_root);
  964. Map<String,Set<Node*> >::Element *E=live_scene_edit_cache.find(live_edit_scene);
  965. if (!E)
  966. return; //scene not editable
  967. for(Set<Node*>::Element *F=E->get().front();F;F=F->next()) {
  968. Node *n=F->get();
  969. if (base && !base->is_a_parent_of(n))
  970. continue;
  971. if (!n->has_node(p_parent))
  972. continue;
  973. Node *n2 = n->get_node(p_parent);
  974. Node *no=ps->instance();
  975. no->set_name(p_name);
  976. n2->add_child(no);
  977. }
  978. }
  979. void SceneTree::_live_edit_remove_node_func(const NodePath& p_at){
  980. Node *base = NULL;
  981. if (root->has_node(live_edit_root))
  982. base = root->get_node(live_edit_root);
  983. Map<String,Set<Node*> >::Element *E=live_scene_edit_cache.find(live_edit_scene);
  984. if (!E)
  985. return; //scene not editable
  986. for(Set<Node*>::Element *F=E->get().front();F;) {
  987. Set<Node*>::Element *N=F->next();
  988. Node *n=F->get();
  989. if (base && !base->is_a_parent_of(n))
  990. continue;
  991. if (!n->has_node(p_at))
  992. continue;
  993. Node *n2 = n->get_node(p_at);
  994. memdelete(n2);
  995. F=N;
  996. }
  997. }
  998. void SceneTree::_live_edit_remove_and_keep_node_func(const NodePath& p_at,ObjectID p_keep_id){
  999. Node *base = NULL;
  1000. if (root->has_node(live_edit_root))
  1001. base = root->get_node(live_edit_root);
  1002. Map<String,Set<Node*> >::Element *E=live_scene_edit_cache.find(live_edit_scene);
  1003. if (!E)
  1004. return; //scene not editable
  1005. for(Set<Node*>::Element *F=E->get().front();F;) {
  1006. Set<Node*>::Element *N=F->next();
  1007. Node *n=F->get();
  1008. if (base && !base->is_a_parent_of(n))
  1009. continue;
  1010. if (!n->has_node(p_at))
  1011. continue;
  1012. Node *n2 = n->get_node(p_at);
  1013. n2->get_parent()->remove_child(n2);
  1014. live_edit_remove_list[n][p_keep_id]=n2;
  1015. F=N;
  1016. }
  1017. }
  1018. void SceneTree::_live_edit_restore_node_func(ObjectID p_id,const NodePath& p_at,int p_at_pos){
  1019. Node *base = NULL;
  1020. if (root->has_node(live_edit_root))
  1021. base = root->get_node(live_edit_root);
  1022. Map<String,Set<Node*> >::Element *E=live_scene_edit_cache.find(live_edit_scene);
  1023. if (!E)
  1024. return; //scene not editable
  1025. for(Set<Node*>::Element *F=E->get().front();F;) {
  1026. Set<Node*>::Element *N=F->next();
  1027. Node *n=F->get();
  1028. if (base && !base->is_a_parent_of(n))
  1029. continue;
  1030. if (!n->has_node(p_at))
  1031. continue;
  1032. Node *n2 = n->get_node(p_at);
  1033. Map<Node*,Map<ObjectID,Node*> >::Element *EN=live_edit_remove_list.find(n);
  1034. if (!EN)
  1035. continue;
  1036. Map<ObjectID,Node*>::Element *FN=EN->get().find(p_id);
  1037. if (!FN)
  1038. continue;
  1039. n2->add_child(FN->get());
  1040. EN->get().erase(FN);
  1041. if (EN->get().size()==0) {
  1042. live_edit_remove_list.erase(EN);
  1043. }
  1044. F=N;
  1045. }
  1046. }
  1047. void SceneTree::_live_edit_duplicate_node_func(const NodePath& p_at,const String& p_new_name){
  1048. Node *base = NULL;
  1049. if (root->has_node(live_edit_root))
  1050. base = root->get_node(live_edit_root);
  1051. Map<String,Set<Node*> >::Element *E=live_scene_edit_cache.find(live_edit_scene);
  1052. if (!E)
  1053. return; //scene not editable
  1054. for(Set<Node*>::Element *F=E->get().front();F;F=F->next()) {
  1055. Node *n=F->get();
  1056. if (base && !base->is_a_parent_of(n))
  1057. continue;
  1058. if (!n->has_node(p_at))
  1059. continue;
  1060. Node *n2 = n->get_node(p_at);
  1061. Node *dup = n2->duplicate(true);
  1062. if (!dup)
  1063. continue;
  1064. dup->set_name(p_new_name);
  1065. n2->get_parent()->add_child(dup);
  1066. }
  1067. }
  1068. 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){
  1069. Node *base = NULL;
  1070. if (root->has_node(live_edit_root))
  1071. base = root->get_node(live_edit_root);
  1072. Map<String,Set<Node*> >::Element *E=live_scene_edit_cache.find(live_edit_scene);
  1073. if (!E)
  1074. return; //scene not editable
  1075. for(Set<Node*>::Element *F=E->get().front();F;F=F->next()) {
  1076. Node *n=F->get();
  1077. if (base && !base->is_a_parent_of(n))
  1078. continue;
  1079. if (!n->has_node(p_at))
  1080. continue;
  1081. Node *nfrom = n->get_node(p_at);
  1082. if (!n->has_node(p_new_place))
  1083. continue;
  1084. Node *nto = n->get_node(p_new_place);
  1085. nfrom->get_parent()->remove_child(nfrom);
  1086. nfrom->set_name(p_new_name);
  1087. nto->add_child(nfrom);
  1088. if (p_at_pos>=0)
  1089. nto->move_child(nfrom,p_at_pos);
  1090. }
  1091. }
  1092. #endif
  1093. void SceneTree::_bind_methods() {
  1094. //ObjectTypeDB::bind_method(_MD("call_group","call_flags","group","method","arg1","arg2"),&SceneMainLoop::_call_group,DEFVAL(Variant()),DEFVAL(Variant()));
  1095. ObjectTypeDB::bind_method(_MD("notify_group","call_flags","group","notification"),&SceneTree::notify_group);
  1096. ObjectTypeDB::bind_method(_MD("set_group","call_flags","group","property","value"),&SceneTree::set_group);
  1097. ObjectTypeDB::bind_method(_MD("get_nodes_in_group","group"),&SceneTree::_get_nodes_in_group);
  1098. ObjectTypeDB::bind_method(_MD("get_root:Viewport"),&SceneTree::get_root);
  1099. ObjectTypeDB::bind_method(_MD("has_group","name"),&SceneTree::has_group);
  1100. ObjectTypeDB::bind_method(_MD("set_auto_accept_quit","enabled"),&SceneTree::set_auto_accept_quit);
  1101. ObjectTypeDB::bind_method(_MD("set_editor_hint","enable"),&SceneTree::set_editor_hint);
  1102. ObjectTypeDB::bind_method(_MD("is_editor_hint"),&SceneTree::is_editor_hint);
  1103. ObjectTypeDB::bind_method(_MD("set_debug_collisions_hint","enable"),&SceneTree::set_debug_collisions_hint);
  1104. ObjectTypeDB::bind_method(_MD("is_debugging_collisions_hint"),&SceneTree::is_debugging_collisions_hint);
  1105. ObjectTypeDB::bind_method(_MD("set_debug_navigation_hint","enable"),&SceneTree::set_debug_navigation_hint);
  1106. ObjectTypeDB::bind_method(_MD("is_debugging_navigation_hint"),&SceneTree::is_debugging_navigation_hint);
  1107. #ifdef TOOLS_ENABLED
  1108. ObjectTypeDB::bind_method(_MD("set_edited_scene_root","scene"),&SceneTree::set_edited_scene_root);
  1109. ObjectTypeDB::bind_method(_MD("get_edited_scene_root"),&SceneTree::get_edited_scene_root);
  1110. #endif
  1111. ObjectTypeDB::bind_method(_MD("set_pause","enable"),&SceneTree::set_pause);
  1112. ObjectTypeDB::bind_method(_MD("is_paused"),&SceneTree::is_paused);
  1113. ObjectTypeDB::bind_method(_MD("set_input_as_handled"),&SceneTree::set_input_as_handled);
  1114. ObjectTypeDB::bind_method(_MD("get_node_count"),&SceneTree::get_node_count);
  1115. ObjectTypeDB::bind_method(_MD("get_frame"),&SceneTree::get_frame);
  1116. ObjectTypeDB::bind_method(_MD("quit"),&SceneTree::quit);
  1117. ObjectTypeDB::bind_method(_MD("set_screen_stretch","mode","aspect","minsize"),&SceneTree::set_screen_stretch);
  1118. ObjectTypeDB::bind_method(_MD("queue_delete","obj"),&SceneTree::queue_delete);
  1119. MethodInfo mi;
  1120. mi.name="call_group";
  1121. mi.arguments.push_back( PropertyInfo( Variant::INT, "flags"));
  1122. mi.arguments.push_back( PropertyInfo( Variant::STRING, "group"));
  1123. mi.arguments.push_back( PropertyInfo( Variant::STRING, "method"));
  1124. Vector<Variant> defargs;
  1125. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  1126. mi.arguments.push_back( PropertyInfo( Variant::NIL, "arg"+itos(i)));
  1127. defargs.push_back(Variant());
  1128. }
  1129. ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"call_group",&SceneTree::_call_group,mi,defargs);
  1130. ObjectTypeDB::bind_method(_MD("set_current_scene","child_node:Node"),&SceneTree::set_current_scene);
  1131. ObjectTypeDB::bind_method(_MD("get_current_scene:Node"),&SceneTree::get_current_scene);
  1132. ObjectTypeDB::bind_method(_MD("change_scene","path"),&SceneTree::change_scene);
  1133. ObjectTypeDB::bind_method(_MD("change_scene_to","packed_scene:PackedScene"),&SceneTree::change_scene_to);
  1134. ObjectTypeDB::bind_method(_MD("reload_current_scene"),&SceneTree::reload_current_scene);
  1135. ObjectTypeDB::bind_method(_MD("_change_scene"),&SceneTree::_change_scene);
  1136. ADD_SIGNAL( MethodInfo("tree_changed") );
  1137. ADD_SIGNAL( MethodInfo("node_removed",PropertyInfo( Variant::OBJECT, "node") ) );
  1138. ADD_SIGNAL( MethodInfo("screen_resized") );
  1139. ADD_SIGNAL( MethodInfo("idle_frame"));
  1140. ADD_SIGNAL( MethodInfo("fixed_frame"));
  1141. BIND_CONSTANT( GROUP_CALL_DEFAULT );
  1142. BIND_CONSTANT( GROUP_CALL_REVERSE );
  1143. BIND_CONSTANT( GROUP_CALL_REALTIME );
  1144. BIND_CONSTANT( GROUP_CALL_UNIQUE );
  1145. BIND_CONSTANT( STRETCH_MODE_DISABLED );
  1146. BIND_CONSTANT( STRETCH_MODE_2D );
  1147. BIND_CONSTANT( STRETCH_MODE_VIEWPORT );
  1148. BIND_CONSTANT( STRETCH_ASPECT_IGNORE );
  1149. BIND_CONSTANT( STRETCH_ASPECT_KEEP );
  1150. BIND_CONSTANT( STRETCH_ASPECT_KEEP_WIDTH );
  1151. BIND_CONSTANT( STRETCH_ASPECT_KEEP_HEIGHT );
  1152. }
  1153. SceneTree *SceneTree::singleton=NULL;
  1154. SceneTree::SceneTree() {
  1155. singleton=this;
  1156. _quit=false;
  1157. initialized=false;
  1158. editor_hint=false;
  1159. debug_collisions_hint=false;
  1160. debug_navigation_hint=false;
  1161. debug_collisions_color=GLOBAL_DEF("debug/collision_shape_color",Color(0.0,0.6,0.7,0.5));
  1162. debug_collision_contact_color=GLOBAL_DEF("debug/collision_contact_color",Color(1.0,0.2,0.1,0.8));
  1163. debug_navigation_color=GLOBAL_DEF("debug/navigation_geometry_color",Color(0.1,1.0,0.7,0.4));
  1164. debug_navigation_disabled_color=GLOBAL_DEF("debug/navigation_disabled_geometry_color",Color(1.0,0.7,0.1,0.4));
  1165. collision_debug_contacts=GLOBAL_DEF("debug/collision_max_contacts_displayed",10000);
  1166. tree_version=1;
  1167. fixed_process_time=1;
  1168. idle_process_time=1;
  1169. last_id=1;
  1170. root=NULL;
  1171. current_frame=0;
  1172. tree_changed_name="tree_changed";
  1173. node_removed_name="node_removed";
  1174. ugc_locked=false;
  1175. call_lock=0;
  1176. root_lock=0;
  1177. node_count=0;
  1178. //create with mainloop
  1179. root = memnew( Viewport );
  1180. root->set_name("root");
  1181. root->set_world( Ref<World>( memnew( World )));
  1182. //root->set_world_2d( Ref<World2D>( memnew( World2D )));
  1183. root->set_as_audio_listener(true);
  1184. root->set_as_audio_listener_2d(true);
  1185. current_scene=NULL;
  1186. stretch_mode=STRETCH_MODE_DISABLED;
  1187. stretch_aspect=STRETCH_ASPECT_IGNORE;
  1188. last_screen_size=Size2( OS::get_singleton()->get_video_mode().width, OS::get_singleton()->get_video_mode().height );
  1189. root->set_rect(Rect2(Point2(),last_screen_size));
  1190. if (ScriptDebugger::get_singleton()) {
  1191. ScriptDebugger::get_singleton()->set_request_scene_tree_message_func(_debugger_request_tree,this);
  1192. }
  1193. root->set_physics_object_picking(GLOBAL_DEF("physics/enable_object_picking",true));
  1194. #ifdef TOOLS_ENABLED
  1195. edited_scene_root=NULL;
  1196. #endif
  1197. #ifdef DEBUG_ENABLED
  1198. live_edit_funcs.udata=this;
  1199. live_edit_funcs.node_path_func=_live_edit_node_path_funcs;
  1200. live_edit_funcs.res_path_func=_live_edit_res_path_funcs;
  1201. live_edit_funcs.node_set_func=_live_edit_node_set_funcs;
  1202. live_edit_funcs.node_set_res_func=_live_edit_node_set_res_funcs;
  1203. live_edit_funcs.node_call_func=_live_edit_node_call_funcs;
  1204. live_edit_funcs.res_set_func=_live_edit_res_set_funcs;
  1205. live_edit_funcs.res_set_res_func=_live_edit_res_set_res_funcs;
  1206. live_edit_funcs.res_call_func=_live_edit_res_call_funcs;
  1207. live_edit_funcs.root_func=_live_edit_root_funcs;
  1208. live_edit_funcs.tree_create_node_func=_live_edit_create_node_funcs;
  1209. live_edit_funcs.tree_instance_node_func=_live_edit_instance_node_funcs;
  1210. live_edit_funcs.tree_remove_node_func=_live_edit_remove_node_funcs;
  1211. live_edit_funcs.tree_remove_and_keep_node_func=_live_edit_remove_and_keep_node_funcs;
  1212. live_edit_funcs.tree_restore_node_func=_live_edit_restore_node_funcs;
  1213. live_edit_funcs.tree_duplicate_node_func=_live_edit_duplicate_node_funcs;
  1214. live_edit_funcs.tree_reparent_node_func=_live_edit_reparent_node_funcs;
  1215. if (ScriptDebugger::get_singleton()) {
  1216. ScriptDebugger::get_singleton()->set_live_edit_funcs(&live_edit_funcs);
  1217. }
  1218. live_edit_root=NodePath("/root");
  1219. #endif
  1220. }
  1221. SceneTree::~SceneTree() {
  1222. }