node.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  1. /*************************************************************************/
  2. /* node.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 "node.h"
  30. #include "print_string.h"
  31. #include "message_queue.h"
  32. #include "scene/scene_string_names.h"
  33. #include "scene/resources/packed_scene.h"
  34. #include "io/resource_loader.h"
  35. #include "viewport.h"
  36. VARIANT_ENUM_CAST(Node::PauseMode);
  37. void Node::_notification(int p_notification) {
  38. switch(p_notification) {
  39. case NOTIFICATION_PROCESS: {
  40. if (get_script_instance()) {
  41. Variant time=get_process_delta_time();
  42. const Variant*ptr[1]={&time};
  43. Variant::CallError err;
  44. get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_process,ptr,1);
  45. }
  46. } break;
  47. case NOTIFICATION_FIXED_PROCESS: {
  48. if (get_script_instance()) {
  49. Variant time=get_fixed_process_delta_time();
  50. const Variant*ptr[1]={&time};
  51. Variant::CallError err;
  52. get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_fixed_process,ptr,1);
  53. }
  54. } break;
  55. case NOTIFICATION_ENTER_TREE: {
  56. if (data.pause_mode==PAUSE_MODE_INHERIT) {
  57. if (data.parent)
  58. data.pause_owner=data.parent->data.pause_owner;
  59. else
  60. data.pause_owner=NULL;
  61. } else {
  62. data.pause_owner=this;
  63. }
  64. if (data.input)
  65. add_to_group("_vp_input"+itos(get_viewport()->get_instance_ID()));
  66. if (data.unhandled_input)
  67. add_to_group("_vp_unhandled_input"+itos(get_viewport()->get_instance_ID()));
  68. if (data.unhandled_key_input)
  69. add_to_group("_vp_unhandled_key_input"+itos(get_viewport()->get_instance_ID()));
  70. get_tree()->node_count++;
  71. } break;
  72. case NOTIFICATION_EXIT_TREE: {
  73. get_tree()->node_count--;
  74. if (data.input)
  75. remove_from_group("_vp_input"+itos(get_viewport()->get_instance_ID()));
  76. if (data.unhandled_input)
  77. remove_from_group("_vp_unhandled_input"+itos(get_viewport()->get_instance_ID()));
  78. if (data.unhandled_key_input)
  79. remove_from_group("_vp_unhandled_key_input"+itos(get_viewport()->get_instance_ID()));
  80. } break;
  81. case NOTIFICATION_READY: {
  82. if (get_script_instance()) {
  83. Variant::CallError err;
  84. get_script_instance()->call_multilevel_reversed(SceneStringNames::get_singleton()->_ready,NULL,0);
  85. }
  86. //emit_signal(SceneStringNames::get_singleton()->enter_tree);
  87. } break;
  88. case NOTIFICATION_POSTINITIALIZE: {
  89. data.in_constructor=false;
  90. } break;
  91. case NOTIFICATION_PREDELETE: {
  92. set_owner(NULL);
  93. while ( data.owned.size() ) {
  94. data.owned.front()->get()->set_owner(NULL);
  95. }
  96. if (data.parent) {
  97. data.parent->remove_child(this);
  98. }
  99. // kill children as cleanly as possible
  100. while( data.children.size() ) {
  101. Node *child = data.children[0];
  102. remove_child(child);
  103. memdelete( child );
  104. }
  105. } break;
  106. }
  107. }
  108. void Node::_propagate_ready() {
  109. data.blocked++;
  110. for (int i=0;i<data.children.size();i++) {
  111. data.children[i]->_propagate_ready();
  112. }
  113. data.blocked--;
  114. notification(NOTIFICATION_READY);
  115. }
  116. void Node::_propagate_enter_tree() {
  117. // this needs to happen to all childs before any enter_tree
  118. if (data.parent) {
  119. data.tree=data.parent->data.tree;
  120. data.depth=data.parent->data.depth+1;
  121. } else {
  122. data.depth=1;
  123. }
  124. data.viewport = cast_to<Viewport>();
  125. if (!data.viewport)
  126. data.viewport = data.parent->data.viewport;
  127. data.inside_tree=true;
  128. const StringName *K=NULL;
  129. while ((K=data.grouped.next(K))) {
  130. data.tree->add_to_group(*K,this);
  131. }
  132. notification(NOTIFICATION_ENTER_TREE);
  133. if (get_script_instance()) {
  134. Variant::CallError err;
  135. get_script_instance()->call_multilevel_reversed(SceneStringNames::get_singleton()->_enter_tree,NULL,0);
  136. }
  137. emit_signal(SceneStringNames::get_singleton()->enter_tree);
  138. data.blocked++;
  139. //block while adding children
  140. for (int i=0;i<data.children.size();i++) {
  141. if (!data.children[i]->is_inside_tree()) // could have been added in enter_tree
  142. data.children[i]->_propagate_enter_tree();
  143. }
  144. data.blocked--;
  145. #ifdef DEBUG_ENABLED
  146. if (ScriptDebugger::get_singleton() && data.filename!=String()) {
  147. //used for live edit
  148. data.tree->live_scene_edit_cache[data.filename].insert(this);
  149. }
  150. #endif
  151. // enter groups
  152. }
  153. void Node::_propagate_exit_tree() {
  154. //block while removing children
  155. #ifdef DEBUG_ENABLED
  156. if (ScriptDebugger::get_singleton() && data.filename!=String()) {
  157. //used for live edit
  158. Map<String,Set<Node*> >::Element *E=data.tree->live_scene_edit_cache.find(data.filename);
  159. if (E) {
  160. E->get().erase(this);
  161. if (E->get().size()==0) {
  162. data.tree->live_scene_edit_cache.erase(E);
  163. }
  164. }
  165. Map<Node*,Map<ObjectID,Node*> >::Element *F=data.tree->live_edit_remove_list.find(this);
  166. if (F) {
  167. for (Map<ObjectID,Node*>::Element*G=F->get().front();G;G=G->next()) {
  168. memdelete(G->get());
  169. }
  170. data.tree->live_edit_remove_list.erase(F);
  171. }
  172. }
  173. #endif
  174. data.blocked++;
  175. for (int i=data.children.size()-1;i>=0;i--) {
  176. data.children[i]->_propagate_exit_tree();
  177. }
  178. data.blocked--;
  179. if (get_script_instance()) {
  180. Variant::CallError err;
  181. get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_exit_tree,NULL,0);
  182. }
  183. emit_signal(SceneStringNames::get_singleton()->exit_tree);
  184. notification(NOTIFICATION_EXIT_TREE,true);
  185. if (data.tree)
  186. data.tree->node_removed(this);
  187. // exit groups
  188. const StringName *K=NULL;
  189. while ((K=data.grouped.next(K))) {
  190. data.tree->remove_from_group(*K,this);
  191. }
  192. data.viewport = NULL;
  193. if (data.tree)
  194. data.tree->tree_changed();
  195. data.inside_tree=false;
  196. data.tree=NULL;
  197. data.depth=-1;
  198. }
  199. void Node::move_child(Node *p_child,int p_pos) {
  200. ERR_FAIL_NULL(p_child);
  201. ERR_EXPLAIN("Invalid new child position: "+itos(p_pos));
  202. ERR_FAIL_INDEX( p_pos, data.children.size()+1 );
  203. ERR_EXPLAIN("child is not a child of this node.");
  204. ERR_FAIL_COND(p_child->data.parent!=this);
  205. ERR_FAIL_COND(data.blocked>0);
  206. data.children.remove( p_child->data.pos );
  207. data.children.insert( p_pos, p_child );
  208. if (data.tree) {
  209. data.tree->tree_changed();
  210. }
  211. data.blocked++;
  212. //new pos first
  213. for (int i=0;i<data.children.size();i++) {
  214. data.children[i]->data.pos=i;
  215. }
  216. // notification second
  217. move_child_notify(p_child);
  218. for (int i=0;i<data.children.size();i++) {
  219. data.children[i]->notification( NOTIFICATION_MOVED_IN_PARENT );
  220. }
  221. data.blocked--;
  222. }
  223. void Node::raise() {
  224. if (!data.parent)
  225. return;
  226. data.parent->move_child(this,data.parent->data.children.size()-1);
  227. }
  228. void Node::add_child_notify(Node *p_child) {
  229. // to be used when not wanted
  230. }
  231. /*
  232. void Node::remove_and_delete_child(Node *p_child) {
  233. ERR_FAIL_NULL( p_child );
  234. ERR_FAIL_COND( p_child->get_parent()!=this );
  235. remove_child(p_child);
  236. memdelete(p_child);
  237. }
  238. */
  239. void Node::remove_child_notify(Node *p_child) {
  240. // to be used when not wanted
  241. }
  242. void Node::move_child_notify(Node *p_child) {
  243. // to be used when not wanted
  244. }
  245. void Node::set_fixed_process(bool p_process) {
  246. if (data.fixed_process==p_process)
  247. return;
  248. data.fixed_process=p_process;
  249. if (data.fixed_process)
  250. add_to_group("fixed_process",false);
  251. else
  252. remove_from_group("fixed_process");
  253. data.fixed_process=p_process;
  254. _change_notify("fixed_process");
  255. }
  256. void Node::set_pause_mode(PauseMode p_mode) {
  257. if (data.pause_mode==p_mode)
  258. return;
  259. bool prev_inherits=data.pause_mode==PAUSE_MODE_INHERIT;
  260. data.pause_mode=p_mode;
  261. if (!is_inside_tree())
  262. return; //pointless
  263. if ((data.pause_mode==PAUSE_MODE_INHERIT) == prev_inherits)
  264. return; ///nothing changed
  265. Node *owner=NULL;
  266. if (data.pause_mode==PAUSE_MODE_INHERIT) {
  267. if (data.parent)
  268. owner=data.parent->data.pause_owner;
  269. } else {
  270. owner=this;
  271. }
  272. _propagate_pause_owner(owner);
  273. }
  274. Node::PauseMode Node::get_pause_mode() const {
  275. return data.pause_mode;
  276. }
  277. void Node::_propagate_pause_owner(Node*p_owner) {
  278. if (data.pause_mode!=PAUSE_MODE_INHERIT)
  279. return;
  280. data.pause_owner=p_owner;
  281. for(int i=0;i<data.children.size();i++) {
  282. data.children[i]->_propagate_pause_owner(p_owner);
  283. }
  284. }
  285. bool Node::can_process() const {
  286. ERR_FAIL_COND_V( !is_inside_tree(), false );
  287. if (get_tree()->is_paused()) {
  288. if (data.pause_mode==PAUSE_MODE_STOP)
  289. return false;
  290. if (data.pause_mode==PAUSE_MODE_PROCESS)
  291. return true;
  292. if (data.pause_mode==PAUSE_MODE_INHERIT) {
  293. if (!data.pause_owner)
  294. return false; //clearly no pause owner by default
  295. if (data.pause_owner->data.pause_mode==PAUSE_MODE_PROCESS)
  296. return true;
  297. if (data.pause_owner->data.pause_mode==PAUSE_MODE_STOP)
  298. return false;
  299. }
  300. }
  301. return true;
  302. }
  303. float Node::get_fixed_process_delta_time() const {
  304. if (data.tree)
  305. return data.tree->get_fixed_process_time();
  306. else
  307. return 0;
  308. }
  309. void Node::set_process(bool p_idle_process) {
  310. if (data.idle_process==p_idle_process)
  311. return;
  312. data.idle_process=p_idle_process;
  313. if (data.idle_process)
  314. add_to_group("idle_process",false);
  315. else
  316. remove_from_group("idle_process");
  317. data.idle_process=p_idle_process;
  318. _change_notify("idle_process");
  319. }
  320. float Node::get_process_delta_time() const {
  321. if (data.tree)
  322. return data.tree->get_idle_process_time();
  323. else
  324. return 0;
  325. }
  326. bool Node::is_fixed_processing() const {
  327. return data.fixed_process;
  328. }
  329. bool Node::is_processing() const {
  330. return data.idle_process;
  331. }
  332. void Node::set_process_input(bool p_enable) {
  333. if (p_enable==data.input)
  334. return;
  335. data.input=p_enable;
  336. if (!is_inside_tree())
  337. return;
  338. if (p_enable)
  339. add_to_group("_vp_input"+itos(get_viewport()->get_instance_ID()));
  340. else
  341. remove_from_group("_vp_input"+itos(get_viewport()->get_instance_ID()));
  342. }
  343. bool Node::is_processing_input() const {
  344. return data.input;
  345. }
  346. void Node::set_process_unhandled_input(bool p_enable) {
  347. if (p_enable==data.unhandled_input)
  348. return;
  349. data.unhandled_input=p_enable;
  350. if (!is_inside_tree())
  351. return;
  352. if (p_enable)
  353. add_to_group("_vp_unhandled_input"+itos(get_viewport()->get_instance_ID()));
  354. else
  355. remove_from_group("_vp_unhandled_input"+itos(get_viewport()->get_instance_ID()));
  356. }
  357. bool Node::is_processing_unhandled_input() const {
  358. return data.unhandled_input;
  359. }
  360. void Node::set_process_unhandled_key_input(bool p_enable) {
  361. if (p_enable==data.unhandled_key_input)
  362. return;
  363. data.unhandled_key_input=p_enable;
  364. if (!is_inside_tree())
  365. return;
  366. if (p_enable)
  367. add_to_group("_vp_unhandled_key_input"+itos(get_viewport()->get_instance_ID()));
  368. else
  369. remove_from_group("_vp_unhandled_key_input"+itos(get_viewport()->get_instance_ID()));
  370. }
  371. bool Node::is_processing_unhandled_key_input() const {
  372. return data.unhandled_key_input;
  373. }
  374. StringName Node::get_name() const {
  375. return data.name;
  376. }
  377. void Node::_set_name_nocheck(const StringName& p_name) {
  378. data.name=p_name;
  379. }
  380. void Node::set_name(const String& p_name) {
  381. String name=p_name.replace(":","").replace("/","").replace("@","");
  382. ERR_FAIL_COND(name=="");
  383. data.name=name;
  384. if (data.parent) {
  385. data.parent->_validate_child_name(this);
  386. }
  387. if (is_inside_tree()) {
  388. emit_signal("renamed");
  389. get_tree()->tree_changed();
  390. }
  391. }
  392. static bool node_hrcr=false;
  393. static SafeRefCount node_hrcr_count;
  394. void Node::init_node_hrcr() {
  395. node_hrcr_count.init(1);
  396. }
  397. void Node::set_human_readable_collision_renaming(bool p_enabled) {
  398. node_hrcr=p_enabled;
  399. }
  400. String Node::validate_child_name(const String& p_name) const {
  401. //this approach to autoset node names is human readable but very slow
  402. //it's turned on while running in the editor
  403. String basename = p_name;
  404. if (basename==String()) {
  405. return String();
  406. }
  407. int val=1;
  408. for(;;) {
  409. String attempted = val > 1 ? (basename + " " +itos(val) ) : basename;
  410. bool found=false;
  411. for (int i=0;i<data.children.size();i++) {
  412. //if (data.children[i]==p_child)
  413. // continue;
  414. if (data.children[i]->get_name() == attempted) {
  415. found=true;
  416. break;
  417. }
  418. }
  419. if (found) {
  420. val++;
  421. continue;
  422. }
  423. return attempted;
  424. break;
  425. }
  426. return basename;
  427. }
  428. void Node::_validate_child_name(Node *p_child) {
  429. /* Make sure the name is unique */
  430. if (node_hrcr) {
  431. //this approach to autoset node names is human readable but very slow
  432. //it's turned on while running in the editor
  433. String basename = p_child->data.name;
  434. if (basename=="") {
  435. basename = p_child->get_type();
  436. }
  437. int val=1;
  438. for(;;) {
  439. String attempted = val > 1 ? (basename + " " +itos(val) ) : basename;
  440. bool found=false;
  441. for (int i=0;i<data.children.size();i++) {
  442. if (data.children[i]==p_child)
  443. continue;
  444. if (data.children[i]->get_name() == attempted) {
  445. found=true;
  446. break;
  447. }
  448. }
  449. if (found) {
  450. val++;
  451. continue;
  452. }
  453. p_child->data.name=attempted;
  454. break;
  455. }
  456. } else {
  457. //this approach to autoset node names is fast but not as readable
  458. //it's the default and reserves the '@' character for unique names.
  459. bool unique=true;
  460. if (p_child->data.name==StringName() || p_child->data.name.operator String()[0]=='@') {
  461. //new unique name must be assigned
  462. unique=false;
  463. } else {
  464. //check if exists
  465. Node **childs=data.children.ptr();
  466. int cc = data.children.size();
  467. for(int i=0;i<cc;i++) {
  468. if (childs[i]==p_child)
  469. continue;
  470. if (childs[i]->data.name==p_child->data.name) {
  471. unique=false;
  472. break;
  473. }
  474. }
  475. }
  476. if (!unique) {
  477. node_hrcr_count.ref();
  478. #ifdef DEBUG_ENABLED
  479. String name = "@"+String(p_child->get_type_name())+itos(node_hrcr_count.get());
  480. #else
  481. String name = "@"+itos(node_hrcr_count.get());
  482. #endif
  483. p_child->data.name=name;
  484. }
  485. }
  486. }
  487. void Node::_add_child_nocheck(Node* p_child,const StringName& p_name) {
  488. //add a child node quickly, without name validation
  489. p_child->data.name=p_name;
  490. p_child->data.pos=data.children.size();
  491. data.children.push_back( p_child );
  492. p_child->data.parent=this;
  493. p_child->notification(NOTIFICATION_PARENTED);
  494. if (data.tree) {
  495. p_child->_set_tree(data.tree);
  496. }
  497. /* Notify */
  498. //recognize childs created in this node constructor
  499. p_child->data.parent_owned=data.in_constructor;
  500. add_child_notify(p_child);
  501. }
  502. void Node::add_child(Node *p_child) {
  503. ERR_FAIL_NULL(p_child);
  504. /* Fail if node has a parent */
  505. ERR_EXPLAIN("Can't add child "+p_child->get_name()+" to itself.")
  506. ERR_FAIL_COND( p_child==this ); // adding to itself!
  507. ERR_EXPLAIN("Can't add child, already has a parent");
  508. ERR_FAIL_COND( p_child->data.parent );
  509. ERR_EXPLAIN("Can't add child while a notification is happening");
  510. ERR_FAIL_COND( data.blocked > 0 );
  511. /* Validate name */
  512. _validate_child_name(p_child);
  513. _add_child_nocheck(p_child,p_child->data.name);
  514. }
  515. void Node::_propagate_validate_owner() {
  516. if (data.owner) {
  517. bool found=false;
  518. Node *parent = data.parent;
  519. while(parent) {
  520. if (parent==data.owner) {
  521. found=true;
  522. break;
  523. }
  524. parent=parent->data.parent;
  525. }
  526. if (!found) {
  527. data.owner->data.owned.erase(data.OW);
  528. data.owner=NULL;
  529. }
  530. }
  531. for(int i=0;i<data.children.size();i++) {
  532. data.children[i]->_propagate_validate_owner();
  533. }
  534. }
  535. void Node::remove_child(Node *p_child) {
  536. ERR_FAIL_NULL(p_child);
  537. ERR_FAIL_COND( data.blocked > 0 );
  538. int idx=-1;
  539. for (int i=0;i<data.children.size();i++) {
  540. if (data.children[i]==p_child) {
  541. idx=i;
  542. break;
  543. }
  544. }
  545. ERR_FAIL_COND( idx==-1 );
  546. //if (data.scene) { does not matter
  547. p_child->_set_tree(NULL);
  548. //}
  549. remove_child_notify(p_child);
  550. p_child->notification(NOTIFICATION_UNPARENTED);
  551. data.children.remove(idx);
  552. for (int i=idx;i<data.children.size();i++) {
  553. data.children[i]->data.pos=i;
  554. }
  555. p_child->data.parent=NULL;
  556. p_child->data.pos=-1;
  557. // validate owner
  558. p_child->_propagate_validate_owner();
  559. }
  560. int Node::get_child_count() const {
  561. return data.children.size();
  562. }
  563. Node *Node::get_child(int p_index) const {
  564. ERR_FAIL_INDEX_V( p_index, data.children.size(), NULL );
  565. return data.children[p_index];
  566. }
  567. Node *Node::_get_child_by_name(const StringName& p_name) const {
  568. int cc=data.children.size();
  569. Node* const* cd=data.children.ptr();
  570. for(int i=0;i<cc;i++){
  571. if (cd[i]->data.name==p_name)
  572. return cd[i];
  573. }
  574. return NULL;
  575. }
  576. Node *Node::_get_node(const NodePath& p_path) const {
  577. ERR_FAIL_COND_V( !data.inside_tree && p_path.is_absolute(), NULL );
  578. Node *current=NULL;
  579. Node *root=NULL;
  580. if (!p_path.is_absolute()) {
  581. current=const_cast<Node*>(this); //start from this
  582. } else {
  583. root=const_cast<Node*>(this);;
  584. while (root->data.parent)
  585. root=root->data.parent; //start from root
  586. }
  587. for(int i=0;i<p_path.get_name_count();i++) {
  588. StringName name = p_path.get_name(i);
  589. Node *next = NULL;
  590. if (name==SceneStringNames::get_singleton()->dot) { // .
  591. next=current;
  592. } else if (name==SceneStringNames::get_singleton()->doubledot) { // ..
  593. if (current==NULL || !current->data.parent)
  594. return NULL;
  595. next=current->data.parent;
  596. } else if (current==NULL) {
  597. if (name==root->get_name())
  598. next=root;
  599. } else {
  600. next=NULL;
  601. for(int j=0;j<current->data.children.size();j++) {
  602. Node *child = current->data.children[j];
  603. if ( child->data.name == name ) {
  604. next = child;
  605. break;
  606. }
  607. }
  608. if (next == NULL) {
  609. return NULL;
  610. };
  611. }
  612. current=next;
  613. }
  614. return current;
  615. }
  616. Node *Node::get_node(const NodePath& p_path) const {
  617. Node *node = _get_node(p_path);
  618. if (!node) {
  619. ERR_EXPLAIN("Node not found: "+p_path);
  620. ERR_FAIL_COND_V(!node,NULL);
  621. }
  622. return node;
  623. }
  624. bool Node::has_node(const NodePath& p_path) const {
  625. return _get_node(p_path)!=NULL;
  626. }
  627. Node* Node::find_node(const String& p_mask,bool p_recursive,bool p_owned) const {
  628. Node * const*cptr = data.children.ptr();
  629. int ccount = data.children.size();
  630. for(int i=0;i<ccount;i++) {
  631. if (p_owned && !cptr[i]->data.owner)
  632. continue;
  633. if (cptr[i]->data.name.operator String().match(p_mask))
  634. return cptr[i];
  635. if (!p_recursive)
  636. continue;
  637. Node* ret = cptr[i]->find_node(p_mask,true,p_owned);
  638. if (ret)
  639. return ret;
  640. }
  641. return NULL;
  642. }
  643. Node *Node::get_parent() const {
  644. return data.parent;
  645. }
  646. bool Node::is_a_parent_of(const Node *p_node) const {
  647. ERR_FAIL_NULL_V(p_node,false);
  648. Node *p=p_node->data.parent;
  649. while(p) {
  650. if (p==this)
  651. return true;
  652. p=p->data.parent;
  653. }
  654. return false;
  655. }
  656. bool Node::is_greater_than(const Node *p_node) const {
  657. ERR_FAIL_NULL_V(p_node,false);
  658. ERR_FAIL_COND_V( !data.inside_tree, false );
  659. ERR_FAIL_COND_V( !p_node->data.inside_tree, false );
  660. ERR_FAIL_COND_V( data.depth<0, false);
  661. ERR_FAIL_COND_V( p_node->data.depth<0, false);
  662. #ifdef NO_ALLOCA
  663. Vector<int> this_stack;
  664. Vector<int> that_stack;
  665. this_stack.resize(data.depth);
  666. that_stack.resize(p_node->data.depth);
  667. #else
  668. int *this_stack=(int*)alloca(sizeof(int)*data.depth);
  669. int *that_stack=(int*)alloca(sizeof(int)*p_node->data.depth);
  670. #endif
  671. const Node *n = this;
  672. int idx=data.depth-1;
  673. while(n) {
  674. ERR_FAIL_INDEX_V(idx, data.depth,false);
  675. this_stack[idx--]=n->data.pos;
  676. n=n->data.parent;
  677. }
  678. ERR_FAIL_COND_V(idx!=-1,false);
  679. n = p_node;
  680. idx=p_node->data.depth-1;
  681. while(n) {
  682. ERR_FAIL_INDEX_V(idx, p_node->data.depth,false);
  683. that_stack[idx--]=n->data.pos;
  684. n=n->data.parent;
  685. }
  686. ERR_FAIL_COND_V(idx!=-1,false);
  687. idx=0;
  688. bool res;
  689. while(true) {
  690. // using -2 since out-of-tree or nonroot nodes have -1
  691. int this_idx = (idx >= data.depth)? -2 : this_stack[idx];
  692. int that_idx = (idx >= p_node->data.depth)? -2 : that_stack[idx];
  693. if (this_idx > that_idx) {
  694. res=true;
  695. break;
  696. } else if (this_idx < that_idx) {
  697. res=false;
  698. break;
  699. } else if (this_idx == -2 ) {
  700. res=false; // equal
  701. break;
  702. }
  703. idx++;
  704. }
  705. return res;
  706. }
  707. void Node::get_owned_by(Node *p_by,List<Node*> *p_owned) {
  708. if (data.owner==p_by)
  709. p_owned->push_back(this);
  710. for (int i=0;i<get_child_count();i++)
  711. get_child(i)->get_owned_by(p_by,p_owned);
  712. }
  713. void Node::_set_owner_nocheck(Node* p_owner) {
  714. ERR_FAIL_COND(data.owner);
  715. data.owner=p_owner;
  716. data.owner->data.owned.push_back( this );
  717. data.OW = data.owner->data.owned.back();
  718. }
  719. void Node::set_owner(Node *p_owner) {
  720. if (data.owner) {
  721. data.owner->data.owned.erase( data.OW );
  722. data.OW=NULL;
  723. data.owner=NULL;
  724. }
  725. ERR_FAIL_COND(p_owner==this);
  726. if (!p_owner)
  727. return;
  728. Node *check=this->get_parent();
  729. bool owner_valid=false;
  730. while(check) {
  731. if (check==p_owner) {
  732. owner_valid=true;
  733. break;
  734. }
  735. check=check->data.parent;
  736. }
  737. ERR_FAIL_COND(!owner_valid);
  738. _set_owner_nocheck(p_owner);
  739. }
  740. Node *Node::get_owner() const {
  741. return data.owner;
  742. }
  743. NodePath Node::get_path_to(const Node *p_node) const {
  744. ERR_FAIL_NULL_V(p_node,NodePath());
  745. if (this==p_node)
  746. return NodePath(".");
  747. Set<const Node*> visited;
  748. const Node *n=this;
  749. while(n) {
  750. visited.insert(n);
  751. n=n->data.parent;
  752. }
  753. const Node *common_parent=p_node;
  754. while(common_parent) {
  755. if (visited.has(common_parent))
  756. break;
  757. common_parent=common_parent->data.parent;
  758. }
  759. ERR_FAIL_COND_V(!common_parent,NodePath()); //nodes not in the same tree
  760. visited.clear();
  761. Vector<StringName> path;
  762. n=p_node;
  763. while(n!=common_parent) {
  764. path.push_back( n->get_name() );
  765. n=n->data.parent;
  766. }
  767. n=this;
  768. StringName up=String("..");
  769. while(n!=common_parent) {
  770. path.push_back( up );
  771. n=n->data.parent;
  772. }
  773. path.invert();
  774. return NodePath(path,false);
  775. }
  776. NodePath Node::get_path() const {
  777. ERR_FAIL_COND_V(!is_inside_tree(),NodePath());
  778. const Node *n = this;
  779. Vector<StringName> path;
  780. while(n) {
  781. path.push_back(n->get_name());
  782. n=n->data.parent;
  783. }
  784. path.invert();
  785. return NodePath( path, true );
  786. }
  787. bool Node::is_in_group(const StringName& p_identifier) const {
  788. return data.grouped.has(p_identifier);
  789. }
  790. void Node::add_to_group(const StringName& p_identifier,bool p_persistent) {
  791. ERR_FAIL_COND(!p_identifier.operator String().length());
  792. if (data.grouped.has(p_identifier))
  793. return;
  794. GroupData gd;
  795. if (data.tree)
  796. data.tree->add_to_group(p_identifier,this);
  797. gd.persistent=p_persistent;
  798. data.grouped[p_identifier]=gd;
  799. }
  800. void Node::remove_from_group(const StringName& p_identifier) {
  801. ERR_FAIL_COND(!data.grouped.has(p_identifier) );
  802. GroupData *g=data.grouped.getptr(p_identifier);
  803. ERR_FAIL_COND(!g);
  804. if (data.tree)
  805. data.tree->remove_from_group(p_identifier,this);
  806. data.grouped.erase(p_identifier);
  807. }
  808. Array Node::_get_groups() const {
  809. Array groups;
  810. List<GroupInfo> gi;
  811. get_groups(&gi);
  812. for (List<GroupInfo>::Element *E=gi.front();E;E=E->next()) {
  813. groups.push_back(E->get().name);
  814. }
  815. return groups;
  816. }
  817. void Node::get_groups(List<GroupInfo> *p_groups) const {
  818. const StringName *K=NULL;
  819. while ((K=data.grouped.next(K))) {
  820. GroupInfo gi;
  821. gi.name=*K;
  822. gi.persistent=data.grouped[*K].persistent;
  823. p_groups->push_back(gi);
  824. }
  825. }
  826. void Node::_print_tree(const Node *p_node) {
  827. print_line(String(p_node->get_path_to(this)));
  828. for (int i=0;i<data.children.size();i++)
  829. data.children[i]->_print_tree(p_node);
  830. }
  831. void Node::print_tree() {
  832. _print_tree(this);
  833. }
  834. void Node::_propagate_reverse_notification(int p_notification) {
  835. data.blocked++;
  836. for (int i=data.children.size()-1;i>=0;i--) {
  837. data.children[i]->_propagate_reverse_notification(p_notification);
  838. }
  839. notification(p_notification,true);
  840. data.blocked--;
  841. }
  842. void Node::_propagate_deferred_notification(int p_notification, bool p_reverse) {
  843. ERR_FAIL_COND(!is_inside_tree());
  844. data.blocked++;
  845. if (!p_reverse)
  846. MessageQueue::get_singleton()->push_notification(this,p_notification);
  847. for (int i=0;i<data.children.size();i++) {
  848. data.children[i]->_propagate_deferred_notification(p_notification,p_reverse);
  849. }
  850. if (p_reverse)
  851. MessageQueue::get_singleton()->push_notification(this,p_notification);
  852. data.blocked--;
  853. }
  854. void Node::propagate_notification(int p_notification) {
  855. data.blocked++;
  856. notification(p_notification);
  857. for (int i=0;i<data.children.size();i++) {
  858. data.children[i]->propagate_notification(p_notification);
  859. }
  860. data.blocked--;
  861. }
  862. void Node::_propagate_replace_owner(Node *p_owner,Node* p_by_owner) {
  863. if (get_owner()==p_owner)
  864. set_owner(p_by_owner);
  865. data.blocked++;
  866. for (int i=0;i<data.children.size();i++)
  867. data.children[i]->_propagate_replace_owner(p_owner,p_by_owner);
  868. data.blocked--;
  869. }
  870. int Node::get_index() const {
  871. return data.pos;
  872. }
  873. void Node::remove_and_skip() {
  874. ERR_FAIL_COND(!data.parent);
  875. Node *new_owner=get_owner();
  876. List<Node*> children;
  877. while(true) {
  878. bool clear=true;
  879. for (int i=0;i<data.children.size();i++) {
  880. if (!data.children[i]->get_owner())
  881. continue;
  882. remove_child(data.children[i]);
  883. data.children[i]->_propagate_replace_owner(this,NULL);
  884. children.push_back(data.children[i]);
  885. clear=false;
  886. break;
  887. }
  888. if (clear)
  889. break;
  890. }
  891. while(!children.empty()) {
  892. Node *c=children.front()->get();
  893. data.parent->add_child(c);
  894. c->_propagate_replace_owner(NULL,new_owner);
  895. children.pop_front();
  896. }
  897. data.parent->remove_child(this);
  898. }
  899. void Node::set_filename(const String& p_filename) {
  900. data.filename=p_filename;
  901. }
  902. String Node::get_filename() const {
  903. return data.filename;
  904. }
  905. void Node::set_editable_instance(Node* p_node,bool p_editable) {
  906. ERR_FAIL_NULL(p_node);
  907. ERR_FAIL_COND(!is_a_parent_of(p_node));
  908. NodePath p = get_path_to(p_node);
  909. if (!p_editable)
  910. data.editable_instances.erase(p);
  911. else
  912. data.editable_instances[p]=true;
  913. }
  914. bool Node::is_editable_instance(Node *p_node) const {
  915. if (!p_node)
  916. return false; //easier, null is never editable :)
  917. ERR_FAIL_COND_V(!is_a_parent_of(p_node),false);
  918. NodePath p = get_path_to(p_node);
  919. return data.editable_instances.has(p);
  920. }
  921. #if 0
  922. void Node::generate_instance_state() {
  923. List<PropertyInfo> properties;
  924. get_property_list(&properties);
  925. data.instance_state.clear();
  926. for( List<PropertyInfo>::Element *E=properties.front();E;E=E->next() ) {
  927. PropertyInfo &pi=E->get();
  928. if ((pi.usage&PROPERTY_USAGE_NO_INSTANCE_STATE) || !(pi.usage&PROPERTY_USAGE_EDITOR) || !(pi.usage&PROPERTY_USAGE_STORAGE))
  929. continue;
  930. data.instance_state[pi.name]=get(pi.name);
  931. }
  932. List<GroupInfo> groups;
  933. get_groups(&groups);
  934. for(List<GroupInfo>::Element *E=groups.front();E;E=E->next()) {
  935. if (!E->get().persistent)
  936. continue;
  937. data.instance_groups.push_back(E->get().name);
  938. }
  939. List<MethodInfo> signal_list;
  940. get_signal_list(&signal_list);
  941. for(List<MethodInfo>::Element *E=signal_list.front();E;E=E->next()) {
  942. StringName name = E->get().name;
  943. List<Connection> connections;
  944. get_signal_connection_list(name,&connections);
  945. for(List<Connection>::Element *F=connections.front();F;F=F->next()) {
  946. if (F->get().flags&CONNECT_PERSIST)
  947. data.instance_connections.push_back(F->get());
  948. }
  949. }
  950. }
  951. Dictionary Node::get_instance_state() const {
  952. return data.instance_state;
  953. }
  954. #endif
  955. void Node::set_scene_instance_state(const Ref<SceneState>& p_state) {
  956. data.instance_state=p_state;
  957. }
  958. Ref<SceneState> Node::get_scene_instance_state() const{
  959. return data.instance_state;
  960. }
  961. void Node::set_scene_inherited_state(const Ref<SceneState>& p_state) {
  962. data.inherited_state=p_state;
  963. }
  964. Ref<SceneState> Node::get_scene_inherited_state() const{
  965. return data.inherited_state;
  966. }
  967. void Node::set_scene_instance_load_placeholder(bool p_enable) {
  968. data.use_placeholder=p_enable;
  969. }
  970. bool Node::get_scene_instance_load_placeholder() const{
  971. return data.use_placeholder;
  972. }
  973. int Node::get_position_in_parent() const {
  974. return data.pos;
  975. }
  976. Node *Node::duplicate(bool p_use_instancing) const {
  977. Node *node=NULL;
  978. bool instanced=false;
  979. if (p_use_instancing && get_filename()!=String()) {
  980. Ref<PackedScene> res = ResourceLoader::load(get_filename());
  981. ERR_FAIL_COND_V(res.is_null(),NULL);
  982. node=res->instance();
  983. ERR_FAIL_COND_V(!node,NULL);
  984. instanced=true;
  985. } else {
  986. Object *obj = ObjectTypeDB::instance(get_type());
  987. ERR_FAIL_COND_V(!obj,NULL);
  988. node = obj->cast_to<Node>();
  989. if (!node)
  990. memdelete(obj);
  991. ERR_FAIL_COND_V(!node,NULL);
  992. }
  993. if (get_filename()!="") { //an instance
  994. node->set_filename(get_filename());
  995. }
  996. List<PropertyInfo> plist;
  997. get_property_list(&plist);
  998. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  999. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  1000. continue;
  1001. String name = E->get().name;
  1002. node->set( name, get(name) );
  1003. }
  1004. node->set_name(get_name());
  1005. for(int i=0;i<get_child_count();i++) {
  1006. if (get_child(i)->data.parent_owned)
  1007. continue;
  1008. if (instanced && get_child(i)->data.owner==this)
  1009. continue; //part of instance
  1010. Node *dup = get_child(i)->duplicate(p_use_instancing);
  1011. if (!dup) {
  1012. memdelete(node);
  1013. return NULL;
  1014. }
  1015. node->add_child(dup);
  1016. }
  1017. return node;
  1018. }
  1019. void Node::_duplicate_and_reown(Node* p_new_parent, const Map<Node*,Node*>& p_reown_map) const {
  1020. if (get_owner()!=get_parent()->get_owner())
  1021. return;
  1022. Node *node=NULL;
  1023. if (get_filename()!="") {
  1024. Ref<PackedScene> res = ResourceLoader::load(get_filename());
  1025. ERR_FAIL_COND(res.is_null());
  1026. node=res->instance();
  1027. ERR_FAIL_COND(!node);
  1028. } else {
  1029. Object *obj = ObjectTypeDB::instance(get_type());
  1030. if (!obj) {
  1031. print_line("could not duplicate: "+String(get_type()));
  1032. }
  1033. ERR_FAIL_COND(!obj);
  1034. node = obj->cast_to<Node>();
  1035. if (!node)
  1036. memdelete(obj);
  1037. }
  1038. List<PropertyInfo> plist;
  1039. get_property_list(&plist);
  1040. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  1041. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  1042. continue;
  1043. String name = E->get().name;
  1044. node->set( name, get(name) );
  1045. }
  1046. node->set_name(get_name());
  1047. p_new_parent->add_child(node);
  1048. Node *owner=get_owner();
  1049. if (p_reown_map.has(owner))
  1050. owner=p_reown_map[owner];
  1051. if (owner) {
  1052. NodePath p = get_path_to(owner);
  1053. if (owner!=this) {
  1054. Node *new_owner = node->get_node(p);
  1055. if (new_owner) {
  1056. node->set_owner(new_owner);
  1057. }
  1058. }
  1059. }
  1060. for(int i=0;i<get_child_count();i++) {
  1061. get_child(i)->_duplicate_and_reown(node,p_reown_map);
  1062. }
  1063. }
  1064. void Node::_duplicate_signals(const Node* p_original,Node* p_copy) const {
  1065. if (this!=p_original && get_owner()!=p_original)
  1066. return;
  1067. List<Connection> conns;
  1068. get_all_signal_connections(&conns);
  1069. for (List<Connection>::Element *E=conns.front();E;E=E->next()) {
  1070. if (E->get().flags&CONNECT_PERSIST) {
  1071. //user connected
  1072. NodePath p = p_original->get_path_to(this);
  1073. Node *copy = p_copy->get_node(p);
  1074. Node *target = E->get().target->cast_to<Node>();
  1075. if (!target)
  1076. continue;
  1077. NodePath ptarget = p_original->get_path_to(target);
  1078. Node *copytarget = p_copy->get_node(ptarget);
  1079. if (copy && copytarget) {
  1080. copy->connect(E->get().signal,copytarget,E->get().method,E->get().binds,CONNECT_PERSIST);
  1081. }
  1082. }
  1083. }
  1084. for(int i=0;i<get_child_count();i++) {
  1085. get_child(i)->_duplicate_signals(p_original,p_copy);
  1086. }
  1087. }
  1088. Node *Node::duplicate_and_reown(const Map<Node*,Node*>& p_reown_map) const {
  1089. ERR_FAIL_COND_V(get_filename()!="",NULL);
  1090. Node *node=NULL;
  1091. Object *obj = ObjectTypeDB::instance(get_type());
  1092. if (!obj) {
  1093. print_line("could not duplicate: "+String(get_type()));
  1094. }
  1095. ERR_FAIL_COND_V(!obj,NULL);
  1096. node = obj->cast_to<Node>();
  1097. if (!node)
  1098. memdelete(obj);
  1099. ERR_FAIL_COND_V(!node,NULL);
  1100. node->set_name(get_name());
  1101. List<PropertyInfo> plist;
  1102. get_property_list(&plist);
  1103. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  1104. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  1105. continue;
  1106. String name = E->get().name;
  1107. node->set( name, get(name) );
  1108. }
  1109. for(int i=0;i<get_child_count();i++) {
  1110. get_child(i)->_duplicate_and_reown(node,p_reown_map);
  1111. }
  1112. _duplicate_signals(this,node);
  1113. return node;
  1114. }
  1115. static void find_owned_by(Node* p_by, Node* p_node, List<Node*> *p_owned) {
  1116. if (p_node->get_owner()==p_by)
  1117. p_owned->push_back(p_node);
  1118. for(int i=0;i<p_node->get_child_count();i++) {
  1119. find_owned_by(p_by,p_node->get_child(i),p_owned);
  1120. }
  1121. }
  1122. struct _NodeReplaceByPair {
  1123. String name;
  1124. Variant value;
  1125. };
  1126. void Node::replace_by(Node* p_node,bool p_keep_data) {
  1127. ERR_FAIL_NULL(p_node);
  1128. ERR_FAIL_COND(p_node->data.parent);
  1129. List<Node*> owned = data.owned;
  1130. List<Node*> owned_by_owner;
  1131. Node *owner = (data.owner==this)?p_node:data.owner;
  1132. List<_NodeReplaceByPair> replace_data;
  1133. if (p_keep_data) {
  1134. List<PropertyInfo> plist;
  1135. get_property_list(&plist);
  1136. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next() ) {
  1137. _NodeReplaceByPair rd;
  1138. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  1139. continue;
  1140. rd.name=E->get().name;
  1141. rd.value=get(rd.name);
  1142. }
  1143. }
  1144. if (data.owner) {
  1145. for(int i=0;i<get_child_count();i++)
  1146. find_owned_by(data.owner,get_child(i),&owned_by_owner);
  1147. }
  1148. Node *parent = data.parent;
  1149. int pos_in_parent = data.pos;
  1150. if (data.parent) {
  1151. parent->remove_child(this);
  1152. parent->add_child(p_node);
  1153. parent->move_child(p_node,pos_in_parent);
  1154. }
  1155. while(get_child_count()) {
  1156. Node * child = get_child(0);
  1157. remove_child(child);
  1158. p_node->add_child(child);
  1159. }
  1160. p_node->set_owner(owner);
  1161. for(int i=0;i<owned.size();i++)
  1162. owned[i]->set_owner(p_node);
  1163. for(int i=0;i<owned_by_owner.size();i++)
  1164. owned_by_owner[i]->set_owner(owner);
  1165. p_node->set_filename(get_filename());
  1166. for (List<_NodeReplaceByPair>::Element *E=replace_data.front();E;E=E->next()) {
  1167. p_node->set(E->get().name,E->get().value);
  1168. }
  1169. }
  1170. Vector<Variant> Node::make_binds(VARIANT_ARG_DECLARE) {
  1171. Vector<Variant> ret;
  1172. if (p_arg1.get_type()==Variant::NIL)
  1173. return ret;
  1174. else
  1175. ret.push_back(p_arg1);
  1176. if (p_arg2.get_type()==Variant::NIL)
  1177. return ret;
  1178. else
  1179. ret.push_back(p_arg2);
  1180. if (p_arg3.get_type()==Variant::NIL)
  1181. return ret;
  1182. else
  1183. ret.push_back(p_arg3);
  1184. if (p_arg4.get_type()==Variant::NIL)
  1185. return ret;
  1186. else
  1187. ret.push_back(p_arg4);
  1188. if (p_arg5.get_type()==Variant::NIL)
  1189. return ret;
  1190. else
  1191. ret.push_back(p_arg5);
  1192. return ret;
  1193. }
  1194. bool Node::has_node_and_resource(const NodePath& p_path) const {
  1195. if (!has_node(p_path))
  1196. return false;
  1197. Node *node = get_node(p_path);
  1198. if (p_path.get_subname_count()) {
  1199. RES r;
  1200. for(int j=0;j<p_path.get_subname_count();j++) {
  1201. r = j==0 ? node->get(p_path.get_subname(j)) : r->get(p_path.get_subname(j));
  1202. if (r.is_null())
  1203. return false;
  1204. }
  1205. }
  1206. return true;
  1207. }
  1208. Array Node::_get_node_and_resource(const NodePath& p_path) {
  1209. Node *node;
  1210. RES res;
  1211. node = get_node_and_resource(p_path,res);
  1212. Array result;
  1213. if (node)
  1214. result.push_back(node);
  1215. else
  1216. result.push_back(Variant());
  1217. if (res.is_valid())
  1218. result.push_back(res);
  1219. else
  1220. result.push_back(Variant());
  1221. return result;
  1222. }
  1223. Node *Node::get_node_and_resource(const NodePath& p_path,RES& r_res) const {
  1224. Node *node = get_node(p_path);
  1225. r_res = RES();
  1226. if (!node)
  1227. return NULL;
  1228. if (p_path.get_subname_count()) {
  1229. for(int j=0;j<p_path.get_subname_count();j++) {
  1230. r_res = j==0 ? node->get(p_path.get_subname(j)) : r_res->get(p_path.get_subname(j));
  1231. ERR_FAIL_COND_V( r_res.is_null(), node );
  1232. }
  1233. }
  1234. return node;
  1235. }
  1236. void Node::_set_tree(SceneTree *p_tree) {
  1237. SceneTree *tree_changed_a=NULL;
  1238. SceneTree *tree_changed_b=NULL;
  1239. // ERR_FAIL_COND(p_scene && data.parent && !data.parent->data.scene); //nobug if both are null
  1240. if (data.tree) {
  1241. _propagate_exit_tree();
  1242. tree_changed_a=data.tree;
  1243. }
  1244. data.tree=p_tree;
  1245. if (data.tree) {
  1246. _propagate_enter_tree();
  1247. _propagate_ready(); //reverse_notification(NOTIFICATION_READY);
  1248. tree_changed_b=data.tree;
  1249. }
  1250. if (tree_changed_a)
  1251. tree_changed_a->tree_changed();
  1252. if (tree_changed_b)
  1253. tree_changed_b->tree_changed();
  1254. }
  1255. static void _Node_debug_sn(Object *p_obj) {
  1256. Node *n = p_obj->cast_to<Node>();
  1257. if (!n)
  1258. return;
  1259. if (n->is_inside_tree())
  1260. return;
  1261. Node *p=n;
  1262. while(p->get_parent()) {
  1263. p=p->get_parent();
  1264. }
  1265. String path;
  1266. if (p==n)
  1267. path=n->get_name();
  1268. else
  1269. path=String(p->get_name())+"/"+p->get_path_to(n);
  1270. print_line(itos(p_obj->get_instance_ID())+"- Stray Node: "+path+" (Type: "+n->get_type()+")");
  1271. }
  1272. void Node::_print_stray_nodes() {
  1273. print_stray_nodes();
  1274. }
  1275. void Node::print_stray_nodes() {
  1276. #ifdef DEBUG_ENABLED
  1277. ObjectDB::debug_objects(_Node_debug_sn);
  1278. #endif
  1279. }
  1280. void Node::queue_delete() {
  1281. ERR_FAIL_COND( !is_inside_tree() );
  1282. get_tree()->queue_delete(this);
  1283. }
  1284. Array Node::_get_children() const {
  1285. Array arr;
  1286. int cc = get_child_count();
  1287. arr.resize(cc);
  1288. for(int i=0;i<cc;i++)
  1289. arr[i]=get_child(i);
  1290. return arr;
  1291. }
  1292. #ifdef TOOLS_ENABLED
  1293. void Node::set_import_path(const NodePath& p_import_path) {
  1294. data.import_path=p_import_path;
  1295. }
  1296. NodePath Node::get_import_path() const {
  1297. return data.import_path;
  1298. }
  1299. #endif
  1300. static void _add_nodes_to_options(const Node *p_base,const Node *p_node,List<String>*r_options) {
  1301. if (p_node!=p_base && !p_node->get_owner())
  1302. return;
  1303. String n = p_base->get_path_to(p_node);
  1304. r_options->push_back("\""+n+"\"");
  1305. for(int i=0;i<p_node->get_child_count();i++) {
  1306. _add_nodes_to_options(p_base,p_node->get_child(i),r_options);
  1307. }
  1308. }
  1309. void Node::get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const {
  1310. String pf=p_function;
  1311. if ((pf=="has_node" || pf=="get_node") && p_idx==0) {
  1312. _add_nodes_to_options(this,this,r_options);
  1313. }
  1314. Object::get_argument_options(p_function,p_idx,r_options);
  1315. }
  1316. void Node::clear_internal_tree_resource_paths() {
  1317. clear_internal_resource_paths();
  1318. for(int i=0;i<data.children.size();i++) {
  1319. data.children[i]->clear_internal_tree_resource_paths();
  1320. }
  1321. }
  1322. void Node::_bind_methods() {
  1323. ObjectTypeDB::bind_method(_MD("set_name","name"),&Node::set_name);
  1324. ObjectTypeDB::bind_method(_MD("get_name"),&Node::get_name);
  1325. ObjectTypeDB::bind_method(_MD("add_child","node:Node"),&Node::add_child);
  1326. ObjectTypeDB::bind_method(_MD("remove_child","node:Node"),&Node::remove_child);
  1327. //ObjectTypeDB::bind_method(_MD("remove_and_delete_child","node:Node"),&Node::remove_and_delete_child);
  1328. ObjectTypeDB::bind_method(_MD("get_child_count"),&Node::get_child_count);
  1329. ObjectTypeDB::bind_method(_MD("get_children"),&Node::_get_children);
  1330. ObjectTypeDB::bind_method(_MD("get_child:Node","idx"),&Node::get_child);
  1331. ObjectTypeDB::bind_method(_MD("has_node","path"),&Node::has_node);
  1332. ObjectTypeDB::bind_method(_MD("get_node:Node","path"),&Node::get_node);
  1333. ObjectTypeDB::bind_method(_MD("get_parent:Parent"),&Node::get_parent);
  1334. ObjectTypeDB::bind_method(_MD("find_node:Node","mask","recursive","owned"),&Node::find_node,DEFVAL(true),DEFVAL(true));
  1335. ObjectTypeDB::bind_method(_MD("has_node_and_resource","path"),&Node::has_node_and_resource);
  1336. ObjectTypeDB::bind_method(_MD("get_node_and_resource","path"),&Node::_get_node_and_resource);
  1337. ObjectTypeDB::bind_method(_MD("is_inside_tree"),&Node::is_inside_tree);
  1338. ObjectTypeDB::bind_method(_MD("is_a_parent_of","node:Node"),&Node::is_a_parent_of);
  1339. ObjectTypeDB::bind_method(_MD("is_greater_than","node:Node"),&Node::is_greater_than);
  1340. ObjectTypeDB::bind_method(_MD("get_path"),&Node::get_path);
  1341. ObjectTypeDB::bind_method(_MD("get_path_to","node:Node"),&Node::get_path_to);
  1342. ObjectTypeDB::bind_method(_MD("add_to_group","group"),&Node::add_to_group,DEFVAL(false));
  1343. ObjectTypeDB::bind_method(_MD("remove_from_group","group"),&Node::remove_from_group);
  1344. ObjectTypeDB::bind_method(_MD("is_in_group","group"),&Node::is_in_group);
  1345. ObjectTypeDB::bind_method(_MD("move_child","child_node:Node","to_pos"),&Node::move_child);
  1346. ObjectTypeDB::bind_method(_MD("get_groups"),&Node::_get_groups);
  1347. ObjectTypeDB::bind_method(_MD("raise"),&Node::raise);
  1348. ObjectTypeDB::bind_method(_MD("set_owner","owner:Node"),&Node::set_owner);
  1349. ObjectTypeDB::bind_method(_MD("get_owner:Node"),&Node::get_owner);
  1350. ObjectTypeDB::bind_method(_MD("remove_and_skip"),&Node::remove_and_skip);
  1351. ObjectTypeDB::bind_method(_MD("get_index"),&Node::get_index);
  1352. ObjectTypeDB::bind_method(_MD("print_tree"),&Node::print_tree);
  1353. ObjectTypeDB::bind_method(_MD("set_filename","filename"),&Node::set_filename);
  1354. ObjectTypeDB::bind_method(_MD("get_filename"),&Node::get_filename);
  1355. ObjectTypeDB::bind_method(_MD("propagate_notification","what"),&Node::propagate_notification);
  1356. ObjectTypeDB::bind_method(_MD("set_fixed_process","enable"),&Node::set_fixed_process);
  1357. ObjectTypeDB::bind_method(_MD("get_fixed_process_delta_time"),&Node::get_fixed_process_delta_time);
  1358. ObjectTypeDB::bind_method(_MD("is_fixed_processing"),&Node::is_fixed_processing);
  1359. ObjectTypeDB::bind_method(_MD("set_process","enable"),&Node::set_process);
  1360. ObjectTypeDB::bind_method(_MD("get_process_delta_time"),&Node::get_process_delta_time);
  1361. ObjectTypeDB::bind_method(_MD("is_processing"),&Node::is_processing);
  1362. ObjectTypeDB::bind_method(_MD("set_process_input","enable"),&Node::set_process_input);
  1363. ObjectTypeDB::bind_method(_MD("is_processing_input"),&Node::is_processing_input);
  1364. ObjectTypeDB::bind_method(_MD("set_process_unhandled_input","enable"),&Node::set_process_unhandled_input);
  1365. ObjectTypeDB::bind_method(_MD("is_processing_unhandled_input"),&Node::is_processing_unhandled_input);
  1366. ObjectTypeDB::bind_method(_MD("set_process_unhandled_key_input","enable"),&Node::set_process_unhandled_key_input);
  1367. ObjectTypeDB::bind_method(_MD("is_processing_unhandled_key_input"),&Node::is_processing_unhandled_key_input);
  1368. ObjectTypeDB::bind_method(_MD("set_pause_mode","mode"),&Node::set_pause_mode);
  1369. ObjectTypeDB::bind_method(_MD("get_pause_mode"),&Node::get_pause_mode);
  1370. ObjectTypeDB::bind_method(_MD("can_process"),&Node::can_process);
  1371. ObjectTypeDB::bind_method(_MD("print_stray_nodes"),&Node::_print_stray_nodes);
  1372. ObjectTypeDB::bind_method(_MD("get_position_in_parent"),&Node::get_position_in_parent);
  1373. ObjectTypeDB::bind_method(_MD("get_tree:SceneTree"),&Node::get_tree);
  1374. ObjectTypeDB::bind_method(_MD("duplicate:Node","use_instancing"),&Node::duplicate,DEFVAL(false));
  1375. ObjectTypeDB::bind_method(_MD("replace_by","node:Node","keep_data"),&Node::replace_by,DEFVAL(false));
  1376. ObjectTypeDB::bind_method(_MD("get_viewport"),&Node::get_viewport);
  1377. ObjectTypeDB::bind_method(_MD("queue_free"),&Node::queue_delete);
  1378. #ifdef TOOLS_ENABLED
  1379. ObjectTypeDB::bind_method(_MD("_set_import_path","import_path"),&Node::set_import_path);
  1380. ObjectTypeDB::bind_method(_MD("_get_import_path"),&Node::get_import_path);
  1381. ADD_PROPERTYNZ( PropertyInfo(Variant::NODE_PATH,"_import_path",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_import_path"),_SCS("_get_import_path"));
  1382. #endif
  1383. BIND_CONSTANT( NOTIFICATION_ENTER_TREE );
  1384. BIND_CONSTANT( NOTIFICATION_EXIT_TREE );
  1385. BIND_CONSTANT( NOTIFICATION_MOVED_IN_PARENT );
  1386. //BIND_CONSTANT( NOTIFICATION_PARENT_DECONFIGURED );
  1387. BIND_CONSTANT( NOTIFICATION_READY );
  1388. BIND_CONSTANT( NOTIFICATION_FIXED_PROCESS );
  1389. BIND_CONSTANT( NOTIFICATION_PROCESS );
  1390. BIND_CONSTANT( NOTIFICATION_PARENTED );
  1391. BIND_CONSTANT( NOTIFICATION_UNPARENTED );
  1392. BIND_CONSTANT( NOTIFICATION_PAUSED );
  1393. BIND_CONSTANT( NOTIFICATION_UNPAUSED );
  1394. BIND_CONSTANT( PAUSE_MODE_INHERIT );
  1395. BIND_CONSTANT( PAUSE_MODE_STOP );
  1396. BIND_CONSTANT( PAUSE_MODE_PROCESS );
  1397. ADD_SIGNAL( MethodInfo("renamed") );
  1398. ADD_SIGNAL( MethodInfo("enter_tree") );
  1399. ADD_SIGNAL( MethodInfo("exit_tree") );
  1400. // ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "process/process" ),_SCS("set_process"),_SCS("is_processing") );
  1401. // ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "process/fixed_process" ), _SCS("set_fixed_process"),_SCS("is_fixed_processing") );
  1402. //ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "process/input" ), _SCS("set_process_input"),_SCS("is_processing_input" ) );
  1403. //ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "process/unhandled_input" ), _SCS("set_process_unhandled_input"),_SCS("is_processing_unhandled_input" ) );
  1404. ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "process/pause_mode",PROPERTY_HINT_ENUM,"Inherit,Stop,Process" ), _SCS("set_pause_mode"),_SCS("get_pause_mode" ) );
  1405. BIND_VMETHOD( MethodInfo("_process",PropertyInfo(Variant::REAL,"delta")) );
  1406. BIND_VMETHOD( MethodInfo("_fixed_process",PropertyInfo(Variant::REAL,"delta")) );
  1407. BIND_VMETHOD( MethodInfo("_enter_tree") );
  1408. BIND_VMETHOD( MethodInfo("_exit_tree") );
  1409. BIND_VMETHOD( MethodInfo("_ready") );
  1410. BIND_VMETHOD( MethodInfo("_input",PropertyInfo(Variant::INPUT_EVENT,"event")) );
  1411. BIND_VMETHOD( MethodInfo("_unhandled_input",PropertyInfo(Variant::INPUT_EVENT,"event")) );
  1412. BIND_VMETHOD( MethodInfo("_unhandled_key_input",PropertyInfo(Variant::INPUT_EVENT,"key_event")) );
  1413. //ObjectTypeDB::bind_method(_MD("get_child",&Node::get_child,PH("index")));
  1414. //ObjectTypeDB::bind_method(_MD("get_node",&Node::get_node,PH("path")));
  1415. }
  1416. Node::Node() {
  1417. data.pos=-1;
  1418. data.depth=-1;
  1419. data.blocked=0;
  1420. data.parent=NULL;
  1421. data.tree=NULL;
  1422. data.fixed_process=false;
  1423. data.idle_process=false;
  1424. data.inside_tree=false;
  1425. data.owner=NULL;
  1426. data.OW=NULL;
  1427. data.input=false;
  1428. data.unhandled_input=false;
  1429. data.unhandled_key_input=false;
  1430. data.pause_mode=PAUSE_MODE_INHERIT;
  1431. data.pause_owner=NULL;
  1432. data.parent_owned=false;
  1433. data.in_constructor=true;
  1434. data.viewport=NULL;
  1435. data.use_placeholder=false;
  1436. }
  1437. Node::~Node() {
  1438. data.grouped.clear();
  1439. data.owned.clear();
  1440. data.children.clear();
  1441. ERR_FAIL_COND(data.parent);
  1442. ERR_FAIL_COND(data.children.size());
  1443. }
  1444. ////////////////////////////////