scene_main_loop.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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-2015 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. void SceneTree::tree_changed() {
  45. tree_version++;
  46. emit_signal(tree_changed_name);
  47. }
  48. void SceneTree::node_removed(Node *p_node) {
  49. if (current_scene==p_node) {
  50. current_scene=NULL;
  51. }
  52. emit_signal(node_removed_name,p_node);
  53. if (call_lock>0)
  54. call_skip.insert(p_node);
  55. }
  56. void SceneTree::add_to_group(const StringName& p_group, Node *p_node) {
  57. Map<StringName,Group>::Element *E=group_map.find(p_group);
  58. if (!E) {
  59. E=group_map.insert(p_group,Group());
  60. }
  61. if (E->get().nodes.find(p_node)!=-1) {
  62. ERR_EXPLAIN("Already in group: "+p_group);
  63. ERR_FAIL();
  64. }
  65. E->get().nodes.push_back(p_node);
  66. E->get().last_tree_version=0;
  67. }
  68. void SceneTree::remove_from_group(const StringName& p_group, Node *p_node) {
  69. Map<StringName,Group>::Element *E=group_map.find(p_group);
  70. ERR_FAIL_COND(!E);
  71. E->get().nodes.erase(p_node);
  72. if (E->get().nodes.empty())
  73. group_map.erase(E);
  74. }
  75. void SceneTree::_flush_transform_notifications() {
  76. SelfList<Node>* n = xform_change_list.first();
  77. while(n) {
  78. Node *node=n->self();
  79. SelfList<Node>* nx = n->next();
  80. xform_change_list.remove(n);
  81. n=nx;
  82. node->notification(NOTIFICATION_TRANSFORM_CHANGED);
  83. }
  84. }
  85. void SceneTree::_flush_ugc() {
  86. ugc_locked=true;
  87. while (unique_group_calls.size()) {
  88. Map<UGCall,Vector<Variant> >::Element *E=unique_group_calls.front();
  89. Variant v[VARIANT_ARG_MAX];
  90. for(int i=0;i<E->get().size();i++)
  91. v[i]=E->get()[i];
  92. call_group(GROUP_CALL_REALTIME,E->key().group,E->key().call,v[0],v[1],v[2],v[3],v[4]);
  93. unique_group_calls.erase(E);
  94. }
  95. ugc_locked=false;
  96. }
  97. void SceneTree::_update_group_order(Group& g) {
  98. if (g.last_tree_version==tree_version)
  99. return;
  100. if (g.nodes.empty())
  101. return;
  102. Node **nodes = &g.nodes[0];
  103. int node_count=g.nodes.size();
  104. SortArray<Node*,Node::Comparator> node_sort;
  105. node_sort.sort(nodes,node_count);
  106. g.last_tree_version=tree_version;
  107. }
  108. void SceneTree::call_group(uint32_t p_call_flags,const StringName& p_group,const StringName& p_function,VARIANT_ARG_DECLARE) {
  109. Map<StringName,Group>::Element *E=group_map.find(p_group);
  110. if (!E)
  111. return;
  112. Group &g=E->get();
  113. if (g.nodes.empty())
  114. return;
  115. _update_group_order(g);
  116. if (p_call_flags&GROUP_CALL_UNIQUE && !(p_call_flags&GROUP_CALL_REALTIME)) {
  117. ERR_FAIL_COND(ugc_locked);
  118. UGCall ug;
  119. ug.call=p_function;
  120. ug.group=p_group;
  121. if (unique_group_calls.has(ug))
  122. return;
  123. VARIANT_ARGPTRS;
  124. Vector<Variant> args;
  125. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  126. if (argptr[i]->get_type()==Variant::NIL)
  127. break;
  128. args.push_back(*argptr[i]);
  129. }
  130. unique_group_calls[ug]=args;
  131. return;
  132. }
  133. Vector<Node*> nodes_copy = g.nodes;
  134. Node **nodes = &nodes_copy[0];
  135. int node_count=nodes_copy.size();
  136. call_lock++;
  137. if (p_call_flags&GROUP_CALL_REVERSE) {
  138. for(int i=node_count-1;i>=0;i--) {
  139. if (call_lock && call_skip.has(nodes[i]))
  140. continue;
  141. if (p_call_flags&GROUP_CALL_REALTIME) {
  142. if (p_call_flags&GROUP_CALL_MULIILEVEL)
  143. nodes[i]->call_multilevel(p_function,VARIANT_ARG_PASS);
  144. else
  145. nodes[i]->call(p_function,VARIANT_ARG_PASS);
  146. } else
  147. MessageQueue::get_singleton()->push_call(nodes[i],p_function,VARIANT_ARG_PASS);
  148. }
  149. } else {
  150. for(int i=0;i<node_count;i++) {
  151. if (call_lock && call_skip.has(nodes[i]))
  152. continue;
  153. if (p_call_flags&GROUP_CALL_REALTIME) {
  154. if (p_call_flags&GROUP_CALL_MULIILEVEL)
  155. nodes[i]->call_multilevel(p_function,VARIANT_ARG_PASS);
  156. else
  157. nodes[i]->call(p_function,VARIANT_ARG_PASS);
  158. } else
  159. MessageQueue::get_singleton()->push_call(nodes[i],p_function,VARIANT_ARG_PASS);
  160. }
  161. }
  162. call_lock--;
  163. if (call_lock==0)
  164. call_skip.clear();
  165. }
  166. void SceneTree::notify_group(uint32_t p_call_flags,const StringName& p_group,int p_notification) {
  167. Map<StringName,Group>::Element *E=group_map.find(p_group);
  168. if (!E)
  169. return;
  170. Group &g=E->get();
  171. if (g.nodes.empty())
  172. return;
  173. _update_group_order(g);
  174. Vector<Node*> nodes_copy = g.nodes;
  175. Node **nodes = &nodes_copy[0];
  176. int node_count=nodes_copy.size();
  177. call_lock++;
  178. if (p_call_flags&GROUP_CALL_REVERSE) {
  179. for(int i=node_count-1;i>=0;i--) {
  180. if (call_lock && call_skip.has(nodes[i]))
  181. continue;
  182. if (p_call_flags&GROUP_CALL_REALTIME)
  183. nodes[i]->notification(p_notification);
  184. else
  185. MessageQueue::get_singleton()->push_notification(nodes[i],p_notification);
  186. }
  187. } else {
  188. for(int i=0;i<node_count;i++) {
  189. if (call_lock && call_skip.has(nodes[i]))
  190. continue;
  191. if (p_call_flags&GROUP_CALL_REALTIME)
  192. nodes[i]->notification(p_notification);
  193. else
  194. MessageQueue::get_singleton()->push_notification(nodes[i],p_notification);
  195. }
  196. }
  197. call_lock--;
  198. if (call_lock==0)
  199. call_skip.clear();
  200. }
  201. void SceneTree::set_group(uint32_t p_call_flags,const StringName& p_group,const String& p_name,const Variant& p_value) {
  202. Map<StringName,Group>::Element *E=group_map.find(p_group);
  203. if (!E)
  204. return;
  205. Group &g=E->get();
  206. if (g.nodes.empty())
  207. return;
  208. _update_group_order(g);
  209. Vector<Node*> nodes_copy = g.nodes;
  210. Node **nodes = &nodes_copy[0];
  211. int node_count=nodes_copy.size();
  212. call_lock++;
  213. if (p_call_flags&GROUP_CALL_REVERSE) {
  214. for(int i=node_count-1;i>=0;i--) {
  215. if (call_lock && call_skip.has(nodes[i]))
  216. continue;
  217. if (p_call_flags&GROUP_CALL_REALTIME)
  218. nodes[i]->set(p_name,p_value);
  219. else
  220. MessageQueue::get_singleton()->push_set(nodes[i],p_name,p_value);
  221. }
  222. } else {
  223. for(int i=0;i<node_count;i++) {
  224. if (call_lock && call_skip.has(nodes[i]))
  225. continue;
  226. if (p_call_flags&GROUP_CALL_REALTIME)
  227. nodes[i]->set(p_name,p_value);
  228. else
  229. MessageQueue::get_singleton()->push_set(nodes[i],p_name,p_value);
  230. }
  231. }
  232. call_lock--;
  233. if (call_lock==0)
  234. call_skip.clear();
  235. }
  236. void SceneTree::set_input_as_handled() {
  237. input_handled=true;
  238. }
  239. void SceneTree::input_text( const String& p_text ) {
  240. root_lock++;
  241. call_group(GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"input",p_text);
  242. root_lock--;
  243. }
  244. void SceneTree::input_event( const InputEvent& p_event ) {
  245. if (is_editor_hint() && (p_event.type==InputEvent::JOYSTICK_MOTION || p_event.type==InputEvent::JOYSTICK_BUTTON))
  246. return; //avoid joy input on editor
  247. root_lock++;
  248. //last_id=p_event.ID;
  249. input_handled=false;
  250. InputEvent ev = p_event;
  251. ev.ID=++last_id; //this should work better
  252. #if 0
  253. switch(ev.type) {
  254. case InputEvent::MOUSE_BUTTON: {
  255. Matrix32 ai = root->get_final_transform().affine_inverse();
  256. Vector2 g = ai.xform(Vector2(ev.mouse_button.global_x,ev.mouse_button.global_y));
  257. Vector2 l = ai.xform(Vector2(ev.mouse_button.x,ev.mouse_button.y));
  258. ev.mouse_button.x=l.x;
  259. ev.mouse_button.y=l.y;
  260. ev.mouse_button.global_x=g.x;
  261. ev.mouse_button.global_y=g.y;
  262. } break;
  263. case InputEvent::MOUSE_MOTION: {
  264. Matrix32 ai = root->get_final_transform().affine_inverse();
  265. Vector2 g = ai.xform(Vector2(ev.mouse_motion.global_x,ev.mouse_motion.global_y));
  266. Vector2 l = ai.xform(Vector2(ev.mouse_motion.x,ev.mouse_motion.y));
  267. Vector2 r = ai.xform(Vector2(ev.mouse_motion.relative_x,ev.mouse_motion.relative_y));
  268. ev.mouse_motion.x=l.x;
  269. ev.mouse_motion.y=l.y;
  270. ev.mouse_motion.global_x=g.x;
  271. ev.mouse_motion.global_y=g.y;
  272. ev.mouse_motion.relative_x=r.x;
  273. ev.mouse_motion.relative_y=r.y;
  274. } break;
  275. case InputEvent::SCREEN_TOUCH: {
  276. Matrix32 ai = root->get_final_transform().affine_inverse();
  277. Vector2 t = ai.xform(Vector2(ev.screen_touch.x,ev.screen_touch.y));
  278. ev.screen_touch.x=t.x;
  279. ev.screen_touch.y=t.y;
  280. } break;
  281. case InputEvent::SCREEN_DRAG: {
  282. Matrix32 ai = root->get_final_transform().affine_inverse();
  283. Vector2 t = ai.xform(Vector2(ev.screen_drag.x,ev.screen_drag.y));
  284. Vector2 r = ai.xform(Vector2(ev.screen_drag.relative_x,ev.screen_drag.relative_y));
  285. Vector2 s = ai.xform(Vector2(ev.screen_drag.speed_x,ev.screen_drag.speed_y));
  286. ev.screen_drag.x=t.x;
  287. ev.screen_drag.y=t.y;
  288. ev.screen_drag.relative_x=r.x;
  289. ev.screen_drag.relative_y=r.y;
  290. ev.screen_drag.speed_x=s.x;
  291. ev.screen_drag.speed_y=s.y;
  292. } break;
  293. }
  294. #endif
  295. MainLoop::input_event(ev);
  296. #if 0
  297. _call_input_pause("input","_input",ev);
  298. 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
  299. //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"input","_input",ev);
  300. /*if (ev.type==InputEvent::KEY && ev.key.pressed && !ev.key.echo && ev.key.scancode==KEY_F12) {
  301. print_line("RAM: "+itos(Memory::get_static_mem_usage()));
  302. print_line("DRAM: "+itos(Memory::get_dynamic_mem_usage()));
  303. }
  304. */
  305. //if (ev.type==InputEvent::KEY && ev.key.pressed && !ev.key.echo && ev.key.scancode==KEY_F11) {
  306. // Memory::dump_static_mem_to_file("memdump.txt");
  307. //}
  308. //transform for the rest
  309. #else
  310. call_group(GROUP_CALL_REALTIME,"_viewports","_vp_input",ev); //special one for GUI, as controls use their own process check
  311. #endif
  312. if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_remote() && ev.type==InputEvent::KEY && ev.key.pressed && !ev.key.echo && ev.key.scancode==KEY_F8) {
  313. ScriptDebugger::get_singleton()->request_quit();
  314. }
  315. _flush_ugc();
  316. root_lock--;
  317. MessageQueue::get_singleton()->flush(); //small little hack
  318. root_lock++;
  319. if (!input_handled) {
  320. #if 0
  321. _call_input_pause("unhandled_input","_unhandled_input",ev);
  322. //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_input","_unhandled_input",ev);
  323. if (!input_handled && ev.type==InputEvent::KEY) {
  324. _call_input_pause("unhandled_key_input","_unhandled_key_input",ev);
  325. //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_key_input","_unhandled_key_input",ev);
  326. }
  327. #else
  328. call_group(GROUP_CALL_REALTIME,"_viewports","_vp_unhandled_input",ev); //special one for GUI, as controls use their own process check
  329. #endif
  330. input_handled=true;
  331. _flush_ugc();
  332. root_lock--;
  333. MessageQueue::get_singleton()->flush(); //small little hack
  334. } else {
  335. input_handled=true;
  336. root_lock--;
  337. }
  338. }
  339. void SceneTree::init() {
  340. //_quit=false;
  341. accept_quit=true;
  342. initialized=true;
  343. input_handled=false;
  344. editor_hint=false;
  345. pause=false;
  346. root->_set_tree(this);
  347. MainLoop::init();
  348. }
  349. bool SceneTree::iteration(float p_time) {
  350. root_lock++;
  351. current_frame++;
  352. _flush_transform_notifications();
  353. MainLoop::iteration(p_time);
  354. fixed_process_time=p_time;
  355. emit_signal("fixed_frame");
  356. _notify_group_pause("fixed_process",Node::NOTIFICATION_FIXED_PROCESS);
  357. _flush_ugc();
  358. _flush_transform_notifications();
  359. call_group(GROUP_CALL_REALTIME,"_viewports","update_worlds");
  360. root_lock--;
  361. _flush_delete_queue();
  362. return _quit;
  363. }
  364. bool SceneTree::idle(float p_time){
  365. // print_line("ram: "+itos(OS::get_singleton()->get_static_memory_usage())+" sram: "+itos(OS::get_singleton()->get_dynamic_memory_usage()));
  366. // print_line("node count: "+itos(get_node_count()));
  367. // print_line("TEXTURE RAM: "+itos(VS::get_singleton()->get_render_info(VS::INFO_TEXTURE_MEM_USED)));
  368. root_lock++;
  369. MainLoop::idle(p_time);
  370. idle_process_time=p_time;
  371. emit_signal("idle_frame");
  372. _flush_transform_notifications();
  373. _notify_group_pause("idle_process",Node::NOTIFICATION_PROCESS);
  374. Size2 win_size=Size2( OS::get_singleton()->get_video_mode().width, OS::get_singleton()->get_video_mode().height );
  375. if(win_size!=last_screen_size) {
  376. last_screen_size=win_size;
  377. _update_root_rect();
  378. emit_signal("screen_resized");
  379. }
  380. _flush_ugc();
  381. _flush_transform_notifications(); //transforms after world update, to avoid unnecesary enter/exit notifications
  382. call_group(GROUP_CALL_REALTIME,"_viewports","update_worlds");
  383. root_lock--;
  384. _flush_delete_queue();
  385. return _quit;
  386. }
  387. void SceneTree::finish() {
  388. _flush_delete_queue();
  389. _flush_ugc();
  390. initialized=false;
  391. MainLoop::finish();
  392. if (root) {
  393. root->_set_tree(NULL);
  394. memdelete(root); //delete root
  395. }
  396. }
  397. void SceneTree::quit() {
  398. _quit=true;
  399. }
  400. void SceneTree::_notification(int p_notification) {
  401. switch (p_notification) {
  402. case NOTIFICATION_WM_QUIT_REQUEST: {
  403. get_root()->propagate_notification(p_notification);
  404. if (accept_quit) {
  405. _quit=true;
  406. break;
  407. }
  408. } break;
  409. case NOTIFICATION_OS_MEMORY_WARNING:
  410. case NOTIFICATION_WM_FOCUS_IN:
  411. case NOTIFICATION_WM_FOCUS_OUT: {
  412. get_root()->propagate_notification(p_notification);
  413. } break;
  414. case NOTIFICATION_WM_UNFOCUS_REQUEST: {
  415. notify_group(GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"input",NOTIFICATION_WM_UNFOCUS_REQUEST);
  416. } break;
  417. default:
  418. break;
  419. };
  420. };
  421. void SceneTree::set_auto_accept_quit(bool p_enable) {
  422. accept_quit=p_enable;
  423. }
  424. void SceneTree::set_editor_hint(bool p_enabled) {
  425. editor_hint=p_enabled;
  426. }
  427. bool SceneTree::is_editor_hint() const {
  428. return editor_hint;
  429. }
  430. void SceneTree::set_pause(bool p_enabled) {
  431. if (p_enabled==pause)
  432. return;
  433. pause=p_enabled;
  434. PhysicsServer::get_singleton()->set_active(!p_enabled);
  435. Physics2DServer::get_singleton()->set_active(!p_enabled);
  436. if (get_root())
  437. get_root()->propagate_notification(p_enabled ? Node::NOTIFICATION_PAUSED : Node::NOTIFICATION_UNPAUSED);
  438. }
  439. bool SceneTree::is_paused() const {
  440. return pause;
  441. }
  442. void SceneTree::_call_input_pause(const StringName& p_group,const StringName& p_method,const InputEvent& p_input) {
  443. Map<StringName,Group>::Element *E=group_map.find(p_group);
  444. if (!E)
  445. return;
  446. Group &g=E->get();
  447. if (g.nodes.empty())
  448. return;
  449. _update_group_order(g);
  450. //copy, so copy on write happens in case something is removed from process while being called
  451. //performance is not lost because only if something is added/removed the vector is copied.
  452. Vector<Node*> nodes_copy = g.nodes;
  453. int node_count=nodes_copy.size();
  454. Node **nodes = &nodes_copy[0];
  455. Variant arg=p_input;
  456. const Variant *v[1]={&arg};
  457. call_lock++;
  458. for(int i=node_count-1;i>=0;i--) {
  459. if (input_handled)
  460. break;
  461. Node *n = nodes[i];
  462. if (call_lock && call_skip.has(n))
  463. continue;
  464. if (!n->can_process())
  465. continue;
  466. Variant::CallError ce;
  467. n->call_multilevel(p_method,(const Variant**)v,1);
  468. //ERR_FAIL_COND(node_count != g.nodes.size());
  469. }
  470. call_lock--;
  471. if (call_lock==0)
  472. call_skip.clear();
  473. }
  474. void SceneTree::_notify_group_pause(const StringName& p_group,int p_notification) {
  475. Map<StringName,Group>::Element *E=group_map.find(p_group);
  476. if (!E)
  477. return;
  478. Group &g=E->get();
  479. if (g.nodes.empty())
  480. return;
  481. _update_group_order(g);
  482. //copy, so copy on write happens in case something is removed from process while being called
  483. //performance is not lost because only if something is added/removed the vector is copied.
  484. Vector<Node*> nodes_copy = g.nodes;
  485. int node_count=nodes_copy.size();
  486. Node **nodes = &nodes_copy[0];
  487. call_lock++;
  488. for(int i=0;i<node_count;i++) {
  489. Node *n = nodes[i];
  490. if (call_lock && call_skip.has(n))
  491. continue;
  492. if (!n->can_process())
  493. continue;
  494. n->notification(p_notification);
  495. //ERR_FAIL_COND(node_count != g.nodes.size());
  496. }
  497. call_lock--;
  498. if (call_lock==0)
  499. call_skip.clear();
  500. }
  501. /*
  502. void SceneMainLoop::_update_listener_2d() {
  503. if (listener_2d.is_valid()) {
  504. SpatialSound2DServer::get_singleton()->listener_set_space( listener_2d, world_2d->get_sound_space() );
  505. }
  506. }
  507. */
  508. uint32_t SceneTree::get_last_event_id() const {
  509. return last_id;
  510. }
  511. Variant SceneTree::_call_group(const Variant** p_args, int p_argcount, Variant::CallError& r_error) {
  512. r_error.error=Variant::CallError::CALL_OK;
  513. ERR_FAIL_COND_V(p_argcount<3,Variant());
  514. ERR_FAIL_COND_V(!p_args[0]->is_num(),Variant());
  515. ERR_FAIL_COND_V(p_args[1]->get_type()!=Variant::STRING,Variant());
  516. ERR_FAIL_COND_V(p_args[2]->get_type()!=Variant::STRING,Variant());
  517. int flags = *p_args[0];
  518. StringName group = *p_args[1];
  519. StringName method = *p_args[2];
  520. Variant v[VARIANT_ARG_MAX];
  521. for(int i=0;i<MIN(p_argcount-3,5);i++) {
  522. v[i]=*p_args[i+3];
  523. }
  524. call_group(flags,group,method,v[0],v[1],v[2],v[3],v[4]);
  525. return Variant();
  526. }
  527. int64_t SceneTree::get_frame() const {
  528. return current_frame;
  529. }
  530. Array SceneTree::_get_nodes_in_group(const StringName& p_group) {
  531. Array ret;
  532. Map<StringName,Group>::Element *E=group_map.find(p_group);
  533. if (!E)
  534. return ret;
  535. _update_group_order(E->get()); //update order just in case
  536. int nc = E->get().nodes.size();
  537. if (nc==0)
  538. return ret;
  539. ret.resize(nc);
  540. Node **ptr = E->get().nodes.ptr();
  541. for(int i=0;i<nc;i++) {
  542. ret[i]=ptr[i];
  543. }
  544. return ret;
  545. }
  546. void SceneTree::get_nodes_in_group(const StringName& p_group,List<Node*> *p_list) {
  547. Map<StringName,Group>::Element *E=group_map.find(p_group);
  548. if (!E)
  549. return;
  550. _update_group_order(E->get()); //update order just in case
  551. int nc = E->get().nodes.size();
  552. if (nc==0)
  553. return;
  554. Node **ptr = E->get().nodes.ptr();
  555. for(int i=0;i<nc;i++) {
  556. p_list->push_back(ptr[i]);
  557. }
  558. }
  559. static void _fill_array(Node *p_node, Array& array, int p_level) {
  560. array.push_back(p_level);
  561. array.push_back(p_node->get_name());
  562. array.push_back(p_node->get_type());
  563. for(int i=0;i<p_node->get_child_count();i++) {
  564. _fill_array(p_node->get_child(i),array,p_level+1);
  565. }
  566. }
  567. void SceneTree::_debugger_request_tree(void *self) {
  568. SceneTree *sml = (SceneTree *)self;
  569. Array arr;
  570. _fill_array(sml->root,arr,0);
  571. ScriptDebugger::get_singleton()->send_message("scene_tree",arr);
  572. }
  573. void SceneTree::_flush_delete_queue() {
  574. _THREAD_SAFE_METHOD_
  575. while( delete_queue.size() ) {
  576. Object *obj = ObjectDB::get_instance( delete_queue.front()->get() );
  577. if (obj) {
  578. memdelete( obj );
  579. }
  580. delete_queue.pop_front();
  581. }
  582. }
  583. void SceneTree::queue_delete(Object *p_object) {
  584. _THREAD_SAFE_METHOD_
  585. ERR_FAIL_NULL(p_object);
  586. p_object->_is_queued_for_deletion = true;
  587. delete_queue.push_back(p_object->get_instance_ID());
  588. }
  589. int SceneTree::get_node_count() const {
  590. return node_count;
  591. }
  592. void SceneTree::_update_root_rect() {
  593. if (stretch_mode==STRETCH_MODE_DISABLED) {
  594. root->set_rect(Rect2(Point2(),last_screen_size));
  595. return; //user will take care
  596. }
  597. //actual screen video mode
  598. Size2 video_mode = Size2(OS::get_singleton()->get_video_mode().width,OS::get_singleton()->get_video_mode().height);
  599. Size2 desired_res = stretch_min;
  600. Size2 viewport_size;
  601. Size2 screen_size;
  602. float viewport_aspect = desired_res.get_aspect();
  603. float video_mode_aspect = video_mode.get_aspect();
  604. if (stretch_aspect==STRETCH_ASPECT_IGNORE || ABS(viewport_aspect - video_mode_aspect)<CMP_EPSILON) {
  605. //same aspect or ignore aspect
  606. viewport_size=desired_res;
  607. screen_size=video_mode;
  608. } else if (viewport_aspect < video_mode_aspect) {
  609. // screen ratio is smaller vertically
  610. if (stretch_aspect==STRETCH_ASPECT_KEEP_HEIGHT) {
  611. //will stretch horizontally
  612. viewport_size.x=desired_res.y*video_mode_aspect;
  613. viewport_size.y=desired_res.y;
  614. screen_size=video_mode;
  615. } else {
  616. //will need black bars
  617. viewport_size=desired_res;
  618. screen_size.x = video_mode.y * viewport_aspect;
  619. screen_size.y=video_mode.y;
  620. }
  621. } else {
  622. //screen ratio is smaller horizontally
  623. if (stretch_aspect==STRETCH_ASPECT_KEEP_WIDTH) {
  624. //will stretch horizontally
  625. viewport_size.x=desired_res.x;
  626. viewport_size.y=desired_res.x / video_mode_aspect;
  627. screen_size=video_mode;
  628. } else {
  629. //will need black bars
  630. viewport_size=desired_res;
  631. screen_size.x=video_mode.x;
  632. screen_size.y = video_mode.x / viewport_aspect;
  633. }
  634. }
  635. screen_size = screen_size.floor();
  636. viewport_size = viewport_size.floor();
  637. Size2 margin;
  638. Size2 offset;
  639. //black bars and margin
  640. if (screen_size.x < video_mode.x) {
  641. margin.x = Math::round((video_mode.x - screen_size.x)/2.0);
  642. VisualServer::get_singleton()->black_bars_set_margins(margin.x,0,margin.x,0);
  643. offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
  644. } else if (screen_size.y < video_mode.y) {
  645. margin.y = Math::round((video_mode.y - screen_size.y)/2.0);
  646. VisualServer::get_singleton()->black_bars_set_margins(0,margin.y,0,margin.y);
  647. offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
  648. } else {
  649. VisualServer::get_singleton()->black_bars_set_margins(0,0,0,0);
  650. }
  651. // print_line("VP SIZE: "+viewport_size+" OFFSET: "+offset+" = "+(offset*2+viewport_size));
  652. // print_line("SS: "+video_mode);
  653. switch (stretch_mode) {
  654. case STRETCH_MODE_2D: {
  655. // root->set_rect(Rect2(Point2(),video_mode));
  656. root->set_as_render_target(false);
  657. root->set_rect(Rect2(margin,screen_size));
  658. root->set_size_override_stretch(true);
  659. root->set_size_override(true,viewport_size);
  660. } break;
  661. case STRETCH_MODE_VIEWPORT: {
  662. root->set_rect(Rect2(Point2(),viewport_size));
  663. root->set_size_override_stretch(false);
  664. root->set_size_override(false,Size2());
  665. root->set_as_render_target(true);
  666. root->set_render_target_update_mode(Viewport::RENDER_TARGET_UPDATE_ALWAYS);
  667. root->set_render_target_to_screen_rect(Rect2(margin,screen_size));
  668. } break;
  669. }
  670. }
  671. void SceneTree::set_screen_stretch(StretchMode p_mode,StretchAspect p_aspect,const Size2 p_minsize) {
  672. stretch_mode=p_mode;
  673. stretch_aspect=p_aspect;
  674. stretch_min=p_minsize;
  675. _update_root_rect();
  676. }
  677. #ifdef TOOLS_ENABLED
  678. void SceneTree::set_edited_scene_root(Node *p_node) {
  679. edited_scene_root=p_node;
  680. }
  681. Node *SceneTree::get_edited_scene_root() const {
  682. return edited_scene_root;
  683. }
  684. #endif
  685. void SceneTree::set_current_scene(Node* p_scene) {
  686. ERR_FAIL_COND(p_scene && p_scene->get_parent()!=root);
  687. current_scene=p_scene;
  688. }
  689. Node* SceneTree::get_current_scene() const{
  690. return current_scene;
  691. }
  692. void SceneTree::_change_scene(Node* p_to) {
  693. if (current_scene) {
  694. memdelete( current_scene );
  695. current_scene=NULL;
  696. }
  697. if (p_to) {
  698. current_scene=p_to;
  699. root->add_child(p_to);
  700. }
  701. }
  702. Error SceneTree::change_scene(const String& p_path){
  703. Ref<PackedScene> new_scene = ResourceLoader::load(p_path);
  704. if (new_scene.is_null())
  705. return ERR_CANT_OPEN;
  706. return change_scene_to(new_scene);
  707. }
  708. Error SceneTree::change_scene_to(const Ref<PackedScene>& p_scene){
  709. Node *new_scene=NULL;
  710. if (p_scene.is_valid()) {
  711. new_scene = p_scene->instance();
  712. ERR_FAIL_COND_V(!new_scene,ERR_CANT_CREATE);
  713. }
  714. call_deferred("_change_scene",new_scene);
  715. return OK;
  716. }
  717. Error SceneTree::reload_current_scene() {
  718. ERR_FAIL_COND_V(!current_scene,ERR_UNCONFIGURED);
  719. String fname = current_scene->get_filename();
  720. return change_scene(fname);
  721. }
  722. void SceneTree::add_current_scene(Node * p_current) {
  723. current_scene=p_current;
  724. root->add_child(p_current);
  725. }
  726. void SceneTree::_bind_methods() {
  727. //ObjectTypeDB::bind_method(_MD("call_group","call_flags","group","method","arg1","arg2"),&SceneMainLoop::_call_group,DEFVAL(Variant()),DEFVAL(Variant()));
  728. ObjectTypeDB::bind_method(_MD("notify_group","call_flags","group","notification"),&SceneTree::notify_group);
  729. ObjectTypeDB::bind_method(_MD("set_group","call_flags","group","property","value"),&SceneTree::set_group);
  730. ObjectTypeDB::bind_method(_MD("get_nodes_in_group"),&SceneTree::_get_nodes_in_group);
  731. ObjectTypeDB::bind_method(_MD("get_root:Viewport"),&SceneTree::get_root);
  732. ObjectTypeDB::bind_method(_MD("set_auto_accept_quit","enabled"),&SceneTree::set_auto_accept_quit);
  733. ObjectTypeDB::bind_method(_MD("set_editor_hint","enable"),&SceneTree::set_editor_hint);
  734. ObjectTypeDB::bind_method(_MD("is_editor_hint"),&SceneTree::is_editor_hint);
  735. #ifdef TOOLS_ENABLED
  736. ObjectTypeDB::bind_method(_MD("set_edited_scene_root","scene"),&SceneTree::set_edited_scene_root);
  737. ObjectTypeDB::bind_method(_MD("get_edited_scene_root"),&SceneTree::get_edited_scene_root);
  738. #endif
  739. ObjectTypeDB::bind_method(_MD("set_pause","enable"),&SceneTree::set_pause);
  740. ObjectTypeDB::bind_method(_MD("is_paused"),&SceneTree::is_paused);
  741. ObjectTypeDB::bind_method(_MD("set_input_as_handled"),&SceneTree::set_input_as_handled);
  742. ObjectTypeDB::bind_method(_MD("get_node_count"),&SceneTree::get_node_count);
  743. ObjectTypeDB::bind_method(_MD("get_frame"),&SceneTree::get_frame);
  744. ObjectTypeDB::bind_method(_MD("quit"),&SceneTree::quit);
  745. ObjectTypeDB::bind_method(_MD("set_screen_stretch","mode","aspect","minsize"),&SceneTree::set_screen_stretch);
  746. ObjectTypeDB::bind_method(_MD("queue_delete","obj"),&SceneTree::queue_delete);
  747. MethodInfo mi;
  748. mi.name="call_group";
  749. mi.arguments.push_back( PropertyInfo( Variant::INT, "flags"));
  750. mi.arguments.push_back( PropertyInfo( Variant::STRING, "group"));
  751. mi.arguments.push_back( PropertyInfo( Variant::STRING, "method"));
  752. Vector<Variant> defargs;
  753. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  754. mi.arguments.push_back( PropertyInfo( Variant::NIL, "arg"+itos(i)));
  755. defargs.push_back(Variant());
  756. }
  757. ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"call_group",&SceneTree::_call_group,mi,defargs);
  758. ObjectTypeDB::bind_method(_MD("set_current_scene","child_node:Node"),&SceneTree::set_current_scene);
  759. ObjectTypeDB::bind_method(_MD("get_current_scene:Node"),&SceneTree::get_current_scene);
  760. ObjectTypeDB::bind_method(_MD("change_scene","path"),&SceneTree::change_scene);
  761. ObjectTypeDB::bind_method(_MD("change_scene_to","packed_scene:PackedScene"),&SceneTree::change_scene_to);
  762. ObjectTypeDB::bind_method(_MD("reload_current_scene"),&SceneTree::reload_current_scene);
  763. ObjectTypeDB::bind_method(_MD("_change_scene"),&SceneTree::_change_scene);
  764. ADD_SIGNAL( MethodInfo("tree_changed") );
  765. ADD_SIGNAL( MethodInfo("node_removed",PropertyInfo( Variant::OBJECT, "node") ) );
  766. ADD_SIGNAL( MethodInfo("screen_resized") );
  767. BIND_CONSTANT( GROUP_CALL_DEFAULT );
  768. BIND_CONSTANT( GROUP_CALL_REVERSE );
  769. BIND_CONSTANT( GROUP_CALL_REALTIME );
  770. BIND_CONSTANT( GROUP_CALL_UNIQUE );
  771. BIND_CONSTANT( STRETCH_MODE_DISABLED );
  772. BIND_CONSTANT( STRETCH_MODE_2D );
  773. BIND_CONSTANT( STRETCH_MODE_VIEWPORT );
  774. BIND_CONSTANT( STRETCH_ASPECT_IGNORE );
  775. BIND_CONSTANT( STRETCH_ASPECT_KEEP );
  776. BIND_CONSTANT( STRETCH_ASPECT_KEEP_WIDTH );
  777. BIND_CONSTANT( STRETCH_ASPECT_KEEP_HEIGHT );
  778. }
  779. SceneTree::SceneTree() {
  780. _quit=false;
  781. initialized=false;
  782. tree_version=1;
  783. fixed_process_time=1;
  784. idle_process_time=1;
  785. last_id=1;
  786. root=NULL;
  787. current_frame=0;
  788. tree_changed_name="tree_changed";
  789. node_removed_name="node_removed";
  790. ugc_locked=false;
  791. call_lock=0;
  792. root_lock=0;
  793. node_count=0;
  794. //create with mainloop
  795. root = memnew( Viewport );
  796. root->set_name("root");
  797. root->set_world( Ref<World>( memnew( World )));
  798. //root->set_world_2d( Ref<World2D>( memnew( World2D )));
  799. root->set_as_audio_listener(true);
  800. root->set_as_audio_listener_2d(true);
  801. current_scene=NULL;
  802. stretch_mode=STRETCH_MODE_DISABLED;
  803. stretch_aspect=STRETCH_ASPECT_IGNORE;
  804. last_screen_size=Size2( OS::get_singleton()->get_video_mode().width, OS::get_singleton()->get_video_mode().height );
  805. root->set_rect(Rect2(Point2(),last_screen_size));
  806. if (ScriptDebugger::get_singleton()) {
  807. ScriptDebugger::get_singleton()->set_request_scene_tree_message_func(_debugger_request_tree,this);
  808. }
  809. root->set_physics_object_picking(GLOBAL_DEF("physics/enable_object_picking",true));
  810. #ifdef TOOLS_ENABLED
  811. edited_scene_root=NULL;
  812. #endif
  813. ADD_SIGNAL( MethodInfo("idle_frame"));
  814. ADD_SIGNAL( MethodInfo("fixed_frame"));
  815. }
  816. SceneTree::~SceneTree() {
  817. }