node.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  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, bool p_force_human_readable) {
  429. /* Make sure the name is unique */
  430. if (node_hrcr || p_force_human_readable) {
  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. String name = "@"+String(p_child->get_name())+"@"+itos(node_hrcr_count.get());
  479. p_child->data.name=name;
  480. }
  481. }
  482. }
  483. void Node::_add_child_nocheck(Node* p_child,const StringName& p_name) {
  484. //add a child node quickly, without name validation
  485. p_child->data.name=p_name;
  486. p_child->data.pos=data.children.size();
  487. data.children.push_back( p_child );
  488. p_child->data.parent=this;
  489. p_child->notification(NOTIFICATION_PARENTED);
  490. if (data.tree) {
  491. p_child->_set_tree(data.tree);
  492. }
  493. /* Notify */
  494. //recognize childs created in this node constructor
  495. p_child->data.parent_owned=data.in_constructor;
  496. add_child_notify(p_child);
  497. }
  498. void Node::add_child(Node *p_child, bool p_legible_unique_name) {
  499. ERR_FAIL_NULL(p_child);
  500. /* Fail if node has a parent */
  501. if (p_child==this) {
  502. ERR_EXPLAIN("Can't add child "+p_child->get_name()+" to itself.")
  503. ERR_FAIL_COND( p_child==this ); // adding to itself!
  504. }
  505. ERR_EXPLAIN("Can't add child, already has a parent");
  506. ERR_FAIL_COND( p_child->data.parent );
  507. ERR_EXPLAIN("Can't add child while a notification is happening");
  508. ERR_FAIL_COND( data.blocked > 0 );
  509. /* Validate name */
  510. _validate_child_name(p_child,p_legible_unique_name);
  511. _add_child_nocheck(p_child,p_child->data.name);
  512. }
  513. void Node::_propagate_validate_owner() {
  514. if (data.owner) {
  515. bool found=false;
  516. Node *parent = data.parent;
  517. while(parent) {
  518. if (parent==data.owner) {
  519. found=true;
  520. break;
  521. }
  522. parent=parent->data.parent;
  523. }
  524. if (!found) {
  525. data.owner->data.owned.erase(data.OW);
  526. data.owner=NULL;
  527. }
  528. }
  529. for(int i=0;i<data.children.size();i++) {
  530. data.children[i]->_propagate_validate_owner();
  531. }
  532. }
  533. void Node::remove_child(Node *p_child) {
  534. ERR_FAIL_NULL(p_child);
  535. ERR_FAIL_COND( data.blocked > 0 );
  536. int idx=-1;
  537. for (int i=0;i<data.children.size();i++) {
  538. if (data.children[i]==p_child) {
  539. idx=i;
  540. break;
  541. }
  542. }
  543. ERR_FAIL_COND( idx==-1 );
  544. //ERR_FAIL_COND( p_child->data.blocked > 0 );
  545. //if (data.scene) { does not matter
  546. p_child->_set_tree(NULL);
  547. //}
  548. remove_child_notify(p_child);
  549. p_child->notification(NOTIFICATION_UNPARENTED);
  550. data.children.remove(idx);
  551. for (int i=idx;i<data.children.size();i++) {
  552. data.children[i]->data.pos=i;
  553. }
  554. p_child->data.parent=NULL;
  555. p_child->data.pos=-1;
  556. // validate owner
  557. p_child->_propagate_validate_owner();
  558. }
  559. int Node::get_child_count() const {
  560. return data.children.size();
  561. }
  562. Node *Node::get_child(int p_index) const {
  563. ERR_FAIL_INDEX_V( p_index, data.children.size(), NULL );
  564. return data.children[p_index];
  565. }
  566. Node *Node::_get_child_by_name(const StringName& p_name) const {
  567. int cc=data.children.size();
  568. Node* const* cd=data.children.ptr();
  569. for(int i=0;i<cc;i++){
  570. if (cd[i]->data.name==p_name)
  571. return cd[i];
  572. }
  573. return NULL;
  574. }
  575. Node *Node::_get_node(const NodePath& p_path) const {
  576. if (!data.inside_tree && p_path.is_absolute()) {
  577. ERR_EXPLAIN("Can't use get_node() with absolute paths from outside the active scene tree.");
  578. ERR_FAIL_V(NULL);
  579. }
  580. Node *current=NULL;
  581. Node *root=NULL;
  582. if (!p_path.is_absolute()) {
  583. current=const_cast<Node*>(this); //start from this
  584. } else {
  585. root=const_cast<Node*>(this);;
  586. while (root->data.parent)
  587. root=root->data.parent; //start from root
  588. }
  589. for(int i=0;i<p_path.get_name_count();i++) {
  590. StringName name = p_path.get_name(i);
  591. Node *next = NULL;
  592. if (name==SceneStringNames::get_singleton()->dot) { // .
  593. next=current;
  594. } else if (name==SceneStringNames::get_singleton()->doubledot) { // ..
  595. if (current==NULL || !current->data.parent)
  596. return NULL;
  597. next=current->data.parent;
  598. } else if (current==NULL) {
  599. if (name==root->get_name())
  600. next=root;
  601. } else {
  602. next=NULL;
  603. for(int j=0;j<current->data.children.size();j++) {
  604. Node *child = current->data.children[j];
  605. if ( child->data.name == name ) {
  606. next = child;
  607. break;
  608. }
  609. }
  610. if (next == NULL) {
  611. return NULL;
  612. };
  613. }
  614. current=next;
  615. }
  616. return current;
  617. }
  618. Node *Node::get_node(const NodePath& p_path) const {
  619. Node *node = _get_node(p_path);
  620. if (!node) {
  621. ERR_EXPLAIN("Node not found: "+p_path);
  622. ERR_FAIL_COND_V(!node,NULL);
  623. }
  624. return node;
  625. }
  626. bool Node::has_node(const NodePath& p_path) const {
  627. return _get_node(p_path)!=NULL;
  628. }
  629. Node* Node::find_node(const String& p_mask,bool p_recursive,bool p_owned) const {
  630. Node * const*cptr = data.children.ptr();
  631. int ccount = data.children.size();
  632. for(int i=0;i<ccount;i++) {
  633. if (p_owned && !cptr[i]->data.owner)
  634. continue;
  635. if (cptr[i]->data.name.operator String().match(p_mask))
  636. return cptr[i];
  637. if (!p_recursive)
  638. continue;
  639. Node* ret = cptr[i]->find_node(p_mask,true,p_owned);
  640. if (ret)
  641. return ret;
  642. }
  643. return NULL;
  644. }
  645. Node *Node::get_parent() const {
  646. return data.parent;
  647. }
  648. bool Node::is_a_parent_of(const Node *p_node) const {
  649. ERR_FAIL_NULL_V(p_node,false);
  650. Node *p=p_node->data.parent;
  651. while(p) {
  652. if (p==this)
  653. return true;
  654. p=p->data.parent;
  655. }
  656. return false;
  657. }
  658. bool Node::is_greater_than(const Node *p_node) const {
  659. ERR_FAIL_NULL_V(p_node,false);
  660. ERR_FAIL_COND_V( !data.inside_tree, false );
  661. ERR_FAIL_COND_V( !p_node->data.inside_tree, false );
  662. ERR_FAIL_COND_V( data.depth<0, false);
  663. ERR_FAIL_COND_V( p_node->data.depth<0, false);
  664. #ifdef NO_ALLOCA
  665. Vector<int> this_stack;
  666. Vector<int> that_stack;
  667. this_stack.resize(data.depth);
  668. that_stack.resize(p_node->data.depth);
  669. #else
  670. int *this_stack=(int*)alloca(sizeof(int)*data.depth);
  671. int *that_stack=(int*)alloca(sizeof(int)*p_node->data.depth);
  672. #endif
  673. const Node *n = this;
  674. int idx=data.depth-1;
  675. while(n) {
  676. ERR_FAIL_INDEX_V(idx, data.depth,false);
  677. this_stack[idx--]=n->data.pos;
  678. n=n->data.parent;
  679. }
  680. ERR_FAIL_COND_V(idx!=-1,false);
  681. n = p_node;
  682. idx=p_node->data.depth-1;
  683. while(n) {
  684. ERR_FAIL_INDEX_V(idx, p_node->data.depth,false);
  685. that_stack[idx--]=n->data.pos;
  686. n=n->data.parent;
  687. }
  688. ERR_FAIL_COND_V(idx!=-1,false);
  689. idx=0;
  690. bool res;
  691. while(true) {
  692. // using -2 since out-of-tree or nonroot nodes have -1
  693. int this_idx = (idx >= data.depth)? -2 : this_stack[idx];
  694. int that_idx = (idx >= p_node->data.depth)? -2 : that_stack[idx];
  695. if (this_idx > that_idx) {
  696. res=true;
  697. break;
  698. } else if (this_idx < that_idx) {
  699. res=false;
  700. break;
  701. } else if (this_idx == -2 ) {
  702. res=false; // equal
  703. break;
  704. }
  705. idx++;
  706. }
  707. return res;
  708. }
  709. void Node::get_owned_by(Node *p_by,List<Node*> *p_owned) {
  710. if (data.owner==p_by)
  711. p_owned->push_back(this);
  712. for (int i=0;i<get_child_count();i++)
  713. get_child(i)->get_owned_by(p_by,p_owned);
  714. }
  715. void Node::_set_owner_nocheck(Node* p_owner) {
  716. ERR_FAIL_COND(data.owner);
  717. data.owner=p_owner;
  718. data.owner->data.owned.push_back( this );
  719. data.OW = data.owner->data.owned.back();
  720. }
  721. void Node::set_owner(Node *p_owner) {
  722. if (data.owner) {
  723. data.owner->data.owned.erase( data.OW );
  724. data.OW=NULL;
  725. data.owner=NULL;
  726. }
  727. ERR_FAIL_COND(p_owner==this);
  728. if (!p_owner)
  729. return;
  730. Node *check=this->get_parent();
  731. bool owner_valid=false;
  732. while(check) {
  733. if (check==p_owner) {
  734. owner_valid=true;
  735. break;
  736. }
  737. check=check->data.parent;
  738. }
  739. ERR_FAIL_COND(!owner_valid);
  740. _set_owner_nocheck(p_owner);
  741. }
  742. Node *Node::get_owner() const {
  743. return data.owner;
  744. }
  745. NodePath Node::get_path_to(const Node *p_node) const {
  746. ERR_FAIL_NULL_V(p_node,NodePath());
  747. if (this==p_node)
  748. return NodePath(".");
  749. Set<const Node*> visited;
  750. const Node *n=this;
  751. while(n) {
  752. visited.insert(n);
  753. n=n->data.parent;
  754. }
  755. const Node *common_parent=p_node;
  756. while(common_parent) {
  757. if (visited.has(common_parent))
  758. break;
  759. common_parent=common_parent->data.parent;
  760. }
  761. ERR_FAIL_COND_V(!common_parent,NodePath()); //nodes not in the same tree
  762. visited.clear();
  763. Vector<StringName> path;
  764. n=p_node;
  765. while(n!=common_parent) {
  766. path.push_back( n->get_name() );
  767. n=n->data.parent;
  768. }
  769. n=this;
  770. StringName up=String("..");
  771. while(n!=common_parent) {
  772. path.push_back( up );
  773. n=n->data.parent;
  774. }
  775. path.invert();
  776. return NodePath(path,false);
  777. }
  778. NodePath Node::get_path() const {
  779. ERR_FAIL_COND_V(!is_inside_tree(),NodePath());
  780. const Node *n = this;
  781. Vector<StringName> path;
  782. while(n) {
  783. path.push_back(n->get_name());
  784. n=n->data.parent;
  785. }
  786. path.invert();
  787. return NodePath( path, true );
  788. }
  789. bool Node::is_in_group(const StringName& p_identifier) const {
  790. return data.grouped.has(p_identifier);
  791. }
  792. void Node::add_to_group(const StringName& p_identifier,bool p_persistent) {
  793. ERR_FAIL_COND(!p_identifier.operator String().length());
  794. if (data.grouped.has(p_identifier))
  795. return;
  796. GroupData gd;
  797. if (data.tree)
  798. data.tree->add_to_group(p_identifier,this);
  799. gd.persistent=p_persistent;
  800. data.grouped[p_identifier]=gd;
  801. }
  802. void Node::remove_from_group(const StringName& p_identifier) {
  803. ERR_FAIL_COND(!data.grouped.has(p_identifier) );
  804. GroupData *g=data.grouped.getptr(p_identifier);
  805. ERR_FAIL_COND(!g);
  806. if (data.tree)
  807. data.tree->remove_from_group(p_identifier,this);
  808. data.grouped.erase(p_identifier);
  809. }
  810. Array Node::_get_groups() const {
  811. Array groups;
  812. List<GroupInfo> gi;
  813. get_groups(&gi);
  814. for (List<GroupInfo>::Element *E=gi.front();E;E=E->next()) {
  815. groups.push_back(E->get().name);
  816. }
  817. return groups;
  818. }
  819. void Node::get_groups(List<GroupInfo> *p_groups) const {
  820. const StringName *K=NULL;
  821. while ((K=data.grouped.next(K))) {
  822. GroupInfo gi;
  823. gi.name=*K;
  824. gi.persistent=data.grouped[*K].persistent;
  825. p_groups->push_back(gi);
  826. }
  827. }
  828. void Node::_print_tree(const Node *p_node) {
  829. print_line(String(p_node->get_path_to(this)));
  830. for (int i=0;i<data.children.size();i++)
  831. data.children[i]->_print_tree(p_node);
  832. }
  833. void Node::print_tree() {
  834. _print_tree(this);
  835. }
  836. void Node::_propagate_reverse_notification(int p_notification) {
  837. data.blocked++;
  838. for (int i=data.children.size()-1;i>=0;i--) {
  839. data.children[i]->_propagate_reverse_notification(p_notification);
  840. }
  841. notification(p_notification,true);
  842. data.blocked--;
  843. }
  844. void Node::_propagate_deferred_notification(int p_notification, bool p_reverse) {
  845. ERR_FAIL_COND(!is_inside_tree());
  846. data.blocked++;
  847. if (!p_reverse)
  848. MessageQueue::get_singleton()->push_notification(this,p_notification);
  849. for (int i=0;i<data.children.size();i++) {
  850. data.children[i]->_propagate_deferred_notification(p_notification,p_reverse);
  851. }
  852. if (p_reverse)
  853. MessageQueue::get_singleton()->push_notification(this,p_notification);
  854. data.blocked--;
  855. }
  856. void Node::propagate_notification(int p_notification) {
  857. data.blocked++;
  858. notification(p_notification);
  859. for (int i=0;i<data.children.size();i++) {
  860. data.children[i]->propagate_notification(p_notification);
  861. }
  862. data.blocked--;
  863. }
  864. void Node::_propagate_replace_owner(Node *p_owner,Node* p_by_owner) {
  865. if (get_owner()==p_owner)
  866. set_owner(p_by_owner);
  867. data.blocked++;
  868. for (int i=0;i<data.children.size();i++)
  869. data.children[i]->_propagate_replace_owner(p_owner,p_by_owner);
  870. data.blocked--;
  871. }
  872. int Node::get_index() const {
  873. return data.pos;
  874. }
  875. void Node::remove_and_skip() {
  876. ERR_FAIL_COND(!data.parent);
  877. Node *new_owner=get_owner();
  878. List<Node*> children;
  879. while(true) {
  880. bool clear=true;
  881. for (int i=0;i<data.children.size();i++) {
  882. if (!data.children[i]->get_owner())
  883. continue;
  884. remove_child(data.children[i]);
  885. data.children[i]->_propagate_replace_owner(this,NULL);
  886. children.push_back(data.children[i]);
  887. clear=false;
  888. break;
  889. }
  890. if (clear)
  891. break;
  892. }
  893. while(!children.empty()) {
  894. Node *c=children.front()->get();
  895. data.parent->add_child(c);
  896. c->_propagate_replace_owner(NULL,new_owner);
  897. children.pop_front();
  898. }
  899. data.parent->remove_child(this);
  900. }
  901. void Node::set_filename(const String& p_filename) {
  902. data.filename=p_filename;
  903. }
  904. String Node::get_filename() const {
  905. return data.filename;
  906. }
  907. void Node::set_editable_instance(Node* p_node,bool p_editable) {
  908. ERR_FAIL_NULL(p_node);
  909. ERR_FAIL_COND(!is_a_parent_of(p_node));
  910. NodePath p = get_path_to(p_node);
  911. if (!p_editable)
  912. data.editable_instances.erase(p);
  913. else
  914. data.editable_instances[p]=true;
  915. }
  916. bool Node::is_editable_instance(Node *p_node) const {
  917. if (!p_node)
  918. return false; //easier, null is never editable :)
  919. ERR_FAIL_COND_V(!is_a_parent_of(p_node),false);
  920. NodePath p = get_path_to(p_node);
  921. return data.editable_instances.has(p);
  922. }
  923. #if 0
  924. void Node::generate_instance_state() {
  925. List<PropertyInfo> properties;
  926. get_property_list(&properties);
  927. data.instance_state.clear();
  928. for( List<PropertyInfo>::Element *E=properties.front();E;E=E->next() ) {
  929. PropertyInfo &pi=E->get();
  930. if ((pi.usage&PROPERTY_USAGE_NO_INSTANCE_STATE) || !(pi.usage&PROPERTY_USAGE_EDITOR) || !(pi.usage&PROPERTY_USAGE_STORAGE))
  931. continue;
  932. data.instance_state[pi.name]=get(pi.name);
  933. }
  934. List<GroupInfo> groups;
  935. get_groups(&groups);
  936. for(List<GroupInfo>::Element *E=groups.front();E;E=E->next()) {
  937. if (!E->get().persistent)
  938. continue;
  939. data.instance_groups.push_back(E->get().name);
  940. }
  941. List<MethodInfo> signal_list;
  942. get_signal_list(&signal_list);
  943. for(List<MethodInfo>::Element *E=signal_list.front();E;E=E->next()) {
  944. StringName name = E->get().name;
  945. List<Connection> connections;
  946. get_signal_connection_list(name,&connections);
  947. for(List<Connection>::Element *F=connections.front();F;F=F->next()) {
  948. if (F->get().flags&CONNECT_PERSIST)
  949. data.instance_connections.push_back(F->get());
  950. }
  951. }
  952. }
  953. Dictionary Node::get_instance_state() const {
  954. return data.instance_state;
  955. }
  956. #endif
  957. void Node::set_scene_instance_state(const Ref<SceneState>& p_state) {
  958. data.instance_state=p_state;
  959. }
  960. Ref<SceneState> Node::get_scene_instance_state() const{
  961. return data.instance_state;
  962. }
  963. void Node::set_scene_inherited_state(const Ref<SceneState>& p_state) {
  964. data.inherited_state=p_state;
  965. }
  966. Ref<SceneState> Node::get_scene_inherited_state() const{
  967. return data.inherited_state;
  968. }
  969. void Node::set_scene_instance_load_placeholder(bool p_enable) {
  970. data.use_placeholder=p_enable;
  971. }
  972. bool Node::get_scene_instance_load_placeholder() const{
  973. return data.use_placeholder;
  974. }
  975. int Node::get_position_in_parent() const {
  976. return data.pos;
  977. }
  978. Node *Node::duplicate(bool p_use_instancing) const {
  979. Node *node=NULL;
  980. bool instanced=false;
  981. if (p_use_instancing && get_filename()!=String()) {
  982. Ref<PackedScene> res = ResourceLoader::load(get_filename());
  983. ERR_FAIL_COND_V(res.is_null(),NULL);
  984. node=res->instance();
  985. ERR_FAIL_COND_V(!node,NULL);
  986. instanced=true;
  987. } else {
  988. Object *obj = ObjectTypeDB::instance(get_type());
  989. ERR_FAIL_COND_V(!obj,NULL);
  990. node = obj->cast_to<Node>();
  991. if (!node)
  992. memdelete(obj);
  993. ERR_FAIL_COND_V(!node,NULL);
  994. }
  995. if (get_filename()!="") { //an instance
  996. node->set_filename(get_filename());
  997. }
  998. List<PropertyInfo> plist;
  999. get_property_list(&plist);
  1000. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  1001. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  1002. continue;
  1003. String name = E->get().name;
  1004. node->set( name, get(name) );
  1005. }
  1006. node->set_name(get_name());
  1007. for(int i=0;i<get_child_count();i++) {
  1008. if (get_child(i)->data.parent_owned)
  1009. continue;
  1010. if (instanced && get_child(i)->data.owner==this)
  1011. continue; //part of instance
  1012. Node *dup = get_child(i)->duplicate(p_use_instancing);
  1013. if (!dup) {
  1014. memdelete(node);
  1015. return NULL;
  1016. }
  1017. node->add_child(dup);
  1018. }
  1019. return node;
  1020. }
  1021. void Node::_duplicate_and_reown(Node* p_new_parent, const Map<Node*,Node*>& p_reown_map) const {
  1022. if (get_owner()!=get_parent()->get_owner())
  1023. return;
  1024. Node *node=NULL;
  1025. if (get_filename()!="") {
  1026. Ref<PackedScene> res = ResourceLoader::load(get_filename());
  1027. ERR_FAIL_COND(res.is_null());
  1028. node=res->instance();
  1029. ERR_FAIL_COND(!node);
  1030. } else {
  1031. Object *obj = ObjectTypeDB::instance(get_type());
  1032. if (!obj) {
  1033. print_line("could not duplicate: "+String(get_type()));
  1034. }
  1035. ERR_FAIL_COND(!obj);
  1036. node = obj->cast_to<Node>();
  1037. if (!node)
  1038. memdelete(obj);
  1039. }
  1040. List<PropertyInfo> plist;
  1041. get_property_list(&plist);
  1042. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  1043. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  1044. continue;
  1045. String name = E->get().name;
  1046. node->set( name, get(name) );
  1047. }
  1048. node->set_name(get_name());
  1049. p_new_parent->add_child(node);
  1050. Node *owner=get_owner();
  1051. if (p_reown_map.has(owner))
  1052. owner=p_reown_map[owner];
  1053. if (owner) {
  1054. NodePath p = get_path_to(owner);
  1055. if (owner!=this) {
  1056. Node *new_owner = node->get_node(p);
  1057. if (new_owner) {
  1058. node->set_owner(new_owner);
  1059. }
  1060. }
  1061. }
  1062. for(int i=0;i<get_child_count();i++) {
  1063. get_child(i)->_duplicate_and_reown(node,p_reown_map);
  1064. }
  1065. }
  1066. void Node::_duplicate_signals(const Node* p_original,Node* p_copy) const {
  1067. if (this!=p_original && get_owner()!=p_original)
  1068. return;
  1069. List<Connection> conns;
  1070. get_all_signal_connections(&conns);
  1071. for (List<Connection>::Element *E=conns.front();E;E=E->next()) {
  1072. if (E->get().flags&CONNECT_PERSIST) {
  1073. //user connected
  1074. NodePath p = p_original->get_path_to(this);
  1075. Node *copy = p_copy->get_node(p);
  1076. Node *target = E->get().target->cast_to<Node>();
  1077. if (!target)
  1078. continue;
  1079. NodePath ptarget = p_original->get_path_to(target);
  1080. Node *copytarget = p_copy->get_node(ptarget);
  1081. if (copy && copytarget) {
  1082. copy->connect(E->get().signal,copytarget,E->get().method,E->get().binds,CONNECT_PERSIST);
  1083. }
  1084. }
  1085. }
  1086. for(int i=0;i<get_child_count();i++) {
  1087. get_child(i)->_duplicate_signals(p_original,p_copy);
  1088. }
  1089. }
  1090. Node *Node::duplicate_and_reown(const Map<Node*,Node*>& p_reown_map) const {
  1091. ERR_FAIL_COND_V(get_filename()!="",NULL);
  1092. Node *node=NULL;
  1093. Object *obj = ObjectTypeDB::instance(get_type());
  1094. if (!obj) {
  1095. print_line("could not duplicate: "+String(get_type()));
  1096. }
  1097. ERR_FAIL_COND_V(!obj,NULL);
  1098. node = obj->cast_to<Node>();
  1099. if (!node)
  1100. memdelete(obj);
  1101. ERR_FAIL_COND_V(!node,NULL);
  1102. node->set_name(get_name());
  1103. List<PropertyInfo> plist;
  1104. get_property_list(&plist);
  1105. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  1106. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  1107. continue;
  1108. String name = E->get().name;
  1109. node->set( name, get(name) );
  1110. }
  1111. for(int i=0;i<get_child_count();i++) {
  1112. get_child(i)->_duplicate_and_reown(node,p_reown_map);
  1113. }
  1114. _duplicate_signals(this,node);
  1115. return node;
  1116. }
  1117. static void find_owned_by(Node* p_by, Node* p_node, List<Node*> *p_owned) {
  1118. if (p_node->get_owner()==p_by)
  1119. p_owned->push_back(p_node);
  1120. for(int i=0;i<p_node->get_child_count();i++) {
  1121. find_owned_by(p_by,p_node->get_child(i),p_owned);
  1122. }
  1123. }
  1124. struct _NodeReplaceByPair {
  1125. String name;
  1126. Variant value;
  1127. };
  1128. void Node::replace_by(Node* p_node,bool p_keep_data) {
  1129. ERR_FAIL_NULL(p_node);
  1130. ERR_FAIL_COND(p_node->data.parent);
  1131. List<Node*> owned = data.owned;
  1132. List<Node*> owned_by_owner;
  1133. Node *owner = (data.owner==this)?p_node:data.owner;
  1134. List<_NodeReplaceByPair> replace_data;
  1135. if (p_keep_data) {
  1136. List<PropertyInfo> plist;
  1137. get_property_list(&plist);
  1138. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next() ) {
  1139. _NodeReplaceByPair rd;
  1140. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  1141. continue;
  1142. rd.name=E->get().name;
  1143. rd.value=get(rd.name);
  1144. }
  1145. }
  1146. if (data.owner) {
  1147. for(int i=0;i<get_child_count();i++)
  1148. find_owned_by(data.owner,get_child(i),&owned_by_owner);
  1149. }
  1150. Node *parent = data.parent;
  1151. int pos_in_parent = data.pos;
  1152. if (data.parent) {
  1153. parent->remove_child(this);
  1154. parent->add_child(p_node);
  1155. parent->move_child(p_node,pos_in_parent);
  1156. }
  1157. while(get_child_count()) {
  1158. Node * child = get_child(0);
  1159. remove_child(child);
  1160. p_node->add_child(child);
  1161. }
  1162. p_node->set_owner(owner);
  1163. for(int i=0;i<owned.size();i++)
  1164. owned[i]->set_owner(p_node);
  1165. for(int i=0;i<owned_by_owner.size();i++)
  1166. owned_by_owner[i]->set_owner(owner);
  1167. p_node->set_filename(get_filename());
  1168. for (List<_NodeReplaceByPair>::Element *E=replace_data.front();E;E=E->next()) {
  1169. p_node->set(E->get().name,E->get().value);
  1170. }
  1171. }
  1172. Vector<Variant> Node::make_binds(VARIANT_ARG_DECLARE) {
  1173. Vector<Variant> ret;
  1174. if (p_arg1.get_type()==Variant::NIL)
  1175. return ret;
  1176. else
  1177. ret.push_back(p_arg1);
  1178. if (p_arg2.get_type()==Variant::NIL)
  1179. return ret;
  1180. else
  1181. ret.push_back(p_arg2);
  1182. if (p_arg3.get_type()==Variant::NIL)
  1183. return ret;
  1184. else
  1185. ret.push_back(p_arg3);
  1186. if (p_arg4.get_type()==Variant::NIL)
  1187. return ret;
  1188. else
  1189. ret.push_back(p_arg4);
  1190. if (p_arg5.get_type()==Variant::NIL)
  1191. return ret;
  1192. else
  1193. ret.push_back(p_arg5);
  1194. return ret;
  1195. }
  1196. bool Node::has_node_and_resource(const NodePath& p_path) const {
  1197. if (!has_node(p_path))
  1198. return false;
  1199. Node *node = get_node(p_path);
  1200. if (p_path.get_subname_count()) {
  1201. RES r;
  1202. for(int j=0;j<p_path.get_subname_count();j++) {
  1203. r = j==0 ? node->get(p_path.get_subname(j)) : r->get(p_path.get_subname(j));
  1204. if (r.is_null())
  1205. return false;
  1206. }
  1207. }
  1208. return true;
  1209. }
  1210. Array Node::_get_node_and_resource(const NodePath& p_path) {
  1211. Node *node;
  1212. RES res;
  1213. node = get_node_and_resource(p_path,res);
  1214. Array result;
  1215. if (node)
  1216. result.push_back(node);
  1217. else
  1218. result.push_back(Variant());
  1219. if (res.is_valid())
  1220. result.push_back(res);
  1221. else
  1222. result.push_back(Variant());
  1223. return result;
  1224. }
  1225. Node *Node::get_node_and_resource(const NodePath& p_path,RES& r_res) const {
  1226. Node *node = get_node(p_path);
  1227. r_res = RES();
  1228. if (!node)
  1229. return NULL;
  1230. if (p_path.get_subname_count()) {
  1231. for(int j=0;j<p_path.get_subname_count();j++) {
  1232. r_res = j==0 ? node->get(p_path.get_subname(j)) : r_res->get(p_path.get_subname(j));
  1233. ERR_FAIL_COND_V( r_res.is_null(), node );
  1234. }
  1235. }
  1236. return node;
  1237. }
  1238. void Node::_set_tree(SceneTree *p_tree) {
  1239. SceneTree *tree_changed_a=NULL;
  1240. SceneTree *tree_changed_b=NULL;
  1241. // ERR_FAIL_COND(p_scene && data.parent && !data.parent->data.scene); //nobug if both are null
  1242. if (data.tree) {
  1243. _propagate_exit_tree();
  1244. tree_changed_a=data.tree;
  1245. }
  1246. data.tree=p_tree;
  1247. if (data.tree) {
  1248. _propagate_enter_tree();
  1249. _propagate_ready(); //reverse_notification(NOTIFICATION_READY);
  1250. tree_changed_b=data.tree;
  1251. }
  1252. if (tree_changed_a)
  1253. tree_changed_a->tree_changed();
  1254. if (tree_changed_b)
  1255. tree_changed_b->tree_changed();
  1256. }
  1257. static void _Node_debug_sn(Object *p_obj) {
  1258. Node *n = p_obj->cast_to<Node>();
  1259. if (!n)
  1260. return;
  1261. if (n->is_inside_tree())
  1262. return;
  1263. Node *p=n;
  1264. while(p->get_parent()) {
  1265. p=p->get_parent();
  1266. }
  1267. String path;
  1268. if (p==n)
  1269. path=n->get_name();
  1270. else
  1271. path=String(p->get_name())+"/"+p->get_path_to(n);
  1272. print_line(itos(p_obj->get_instance_ID())+"- Stray Node: "+path+" (Type: "+n->get_type()+")");
  1273. }
  1274. void Node::_print_stray_nodes() {
  1275. print_stray_nodes();
  1276. }
  1277. void Node::print_stray_nodes() {
  1278. #ifdef DEBUG_ENABLED
  1279. ObjectDB::debug_objects(_Node_debug_sn);
  1280. #endif
  1281. }
  1282. void Node::queue_delete() {
  1283. ERR_FAIL_COND( !is_inside_tree() );
  1284. get_tree()->queue_delete(this);
  1285. }
  1286. Array Node::_get_children() const {
  1287. Array arr;
  1288. int cc = get_child_count();
  1289. arr.resize(cc);
  1290. for(int i=0;i<cc;i++)
  1291. arr[i]=get_child(i);
  1292. return arr;
  1293. }
  1294. #ifdef TOOLS_ENABLED
  1295. void Node::set_import_path(const NodePath& p_import_path) {
  1296. data.import_path=p_import_path;
  1297. }
  1298. NodePath Node::get_import_path() const {
  1299. return data.import_path;
  1300. }
  1301. #endif
  1302. static void _add_nodes_to_options(const Node *p_base,const Node *p_node,List<String>*r_options) {
  1303. if (p_node!=p_base && !p_node->get_owner())
  1304. return;
  1305. String n = p_base->get_path_to(p_node);
  1306. r_options->push_back("\""+n+"\"");
  1307. for(int i=0;i<p_node->get_child_count();i++) {
  1308. _add_nodes_to_options(p_base,p_node->get_child(i),r_options);
  1309. }
  1310. }
  1311. void Node::get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const {
  1312. String pf=p_function;
  1313. if ((pf=="has_node" || pf=="get_node") && p_idx==0) {
  1314. _add_nodes_to_options(this,this,r_options);
  1315. }
  1316. Object::get_argument_options(p_function,p_idx,r_options);
  1317. }
  1318. void Node::clear_internal_tree_resource_paths() {
  1319. clear_internal_resource_paths();
  1320. for(int i=0;i<data.children.size();i++) {
  1321. data.children[i]->clear_internal_tree_resource_paths();
  1322. }
  1323. }
  1324. void Node::_bind_methods() {
  1325. ObjectTypeDB::bind_method(_MD("set_name","name"),&Node::set_name);
  1326. ObjectTypeDB::bind_method(_MD("get_name"),&Node::get_name);
  1327. ObjectTypeDB::bind_method(_MD("add_child","node:Node","legible_unique_name"),&Node::add_child,DEFVAL(false));
  1328. ObjectTypeDB::bind_method(_MD("remove_child","node:Node"),&Node::remove_child);
  1329. //ObjectTypeDB::bind_method(_MD("remove_and_delete_child","node:Node"),&Node::remove_and_delete_child);
  1330. ObjectTypeDB::bind_method(_MD("get_child_count"),&Node::get_child_count);
  1331. ObjectTypeDB::bind_method(_MD("get_children"),&Node::_get_children);
  1332. ObjectTypeDB::bind_method(_MD("get_child:Node","idx"),&Node::get_child);
  1333. ObjectTypeDB::bind_method(_MD("has_node","path"),&Node::has_node);
  1334. ObjectTypeDB::bind_method(_MD("get_node:Node","path"),&Node::get_node);
  1335. ObjectTypeDB::bind_method(_MD("get_parent:Parent"),&Node::get_parent);
  1336. ObjectTypeDB::bind_method(_MD("find_node:Node","mask","recursive","owned"),&Node::find_node,DEFVAL(true),DEFVAL(true));
  1337. ObjectTypeDB::bind_method(_MD("has_node_and_resource","path"),&Node::has_node_and_resource);
  1338. ObjectTypeDB::bind_method(_MD("get_node_and_resource","path"),&Node::_get_node_and_resource);
  1339. ObjectTypeDB::bind_method(_MD("is_inside_tree"),&Node::is_inside_tree);
  1340. ObjectTypeDB::bind_method(_MD("is_a_parent_of","node:Node"),&Node::is_a_parent_of);
  1341. ObjectTypeDB::bind_method(_MD("is_greater_than","node:Node"),&Node::is_greater_than);
  1342. ObjectTypeDB::bind_method(_MD("get_path"),&Node::get_path);
  1343. ObjectTypeDB::bind_method(_MD("get_path_to","node:Node"),&Node::get_path_to);
  1344. ObjectTypeDB::bind_method(_MD("add_to_group","group","persistent"),&Node::add_to_group,DEFVAL(false));
  1345. ObjectTypeDB::bind_method(_MD("remove_from_group","group"),&Node::remove_from_group);
  1346. ObjectTypeDB::bind_method(_MD("is_in_group","group"),&Node::is_in_group);
  1347. ObjectTypeDB::bind_method(_MD("move_child","child_node:Node","to_pos"),&Node::move_child);
  1348. ObjectTypeDB::bind_method(_MD("get_groups"),&Node::_get_groups);
  1349. ObjectTypeDB::bind_method(_MD("raise"),&Node::raise);
  1350. ObjectTypeDB::bind_method(_MD("set_owner","owner:Node"),&Node::set_owner);
  1351. ObjectTypeDB::bind_method(_MD("get_owner:Node"),&Node::get_owner);
  1352. ObjectTypeDB::bind_method(_MD("remove_and_skip"),&Node::remove_and_skip);
  1353. ObjectTypeDB::bind_method(_MD("get_index"),&Node::get_index);
  1354. ObjectTypeDB::bind_method(_MD("print_tree"),&Node::print_tree);
  1355. ObjectTypeDB::bind_method(_MD("set_filename","filename"),&Node::set_filename);
  1356. ObjectTypeDB::bind_method(_MD("get_filename"),&Node::get_filename);
  1357. ObjectTypeDB::bind_method(_MD("propagate_notification","what"),&Node::propagate_notification);
  1358. ObjectTypeDB::bind_method(_MD("set_fixed_process","enable"),&Node::set_fixed_process);
  1359. ObjectTypeDB::bind_method(_MD("get_fixed_process_delta_time"),&Node::get_fixed_process_delta_time);
  1360. ObjectTypeDB::bind_method(_MD("is_fixed_processing"),&Node::is_fixed_processing);
  1361. ObjectTypeDB::bind_method(_MD("set_process","enable"),&Node::set_process);
  1362. ObjectTypeDB::bind_method(_MD("get_process_delta_time"),&Node::get_process_delta_time);
  1363. ObjectTypeDB::bind_method(_MD("is_processing"),&Node::is_processing);
  1364. ObjectTypeDB::bind_method(_MD("set_process_input","enable"),&Node::set_process_input);
  1365. ObjectTypeDB::bind_method(_MD("is_processing_input"),&Node::is_processing_input);
  1366. ObjectTypeDB::bind_method(_MD("set_process_unhandled_input","enable"),&Node::set_process_unhandled_input);
  1367. ObjectTypeDB::bind_method(_MD("is_processing_unhandled_input"),&Node::is_processing_unhandled_input);
  1368. ObjectTypeDB::bind_method(_MD("set_process_unhandled_key_input","enable"),&Node::set_process_unhandled_key_input);
  1369. ObjectTypeDB::bind_method(_MD("is_processing_unhandled_key_input"),&Node::is_processing_unhandled_key_input);
  1370. ObjectTypeDB::bind_method(_MD("set_pause_mode","mode"),&Node::set_pause_mode);
  1371. ObjectTypeDB::bind_method(_MD("get_pause_mode"),&Node::get_pause_mode);
  1372. ObjectTypeDB::bind_method(_MD("can_process"),&Node::can_process);
  1373. ObjectTypeDB::bind_method(_MD("print_stray_nodes"),&Node::_print_stray_nodes);
  1374. ObjectTypeDB::bind_method(_MD("get_position_in_parent"),&Node::get_position_in_parent);
  1375. ObjectTypeDB::bind_method(_MD("get_tree:SceneTree"),&Node::get_tree);
  1376. ObjectTypeDB::bind_method(_MD("duplicate:Node","use_instancing"),&Node::duplicate,DEFVAL(false));
  1377. ObjectTypeDB::bind_method(_MD("replace_by","node:Node","keep_data"),&Node::replace_by,DEFVAL(false));
  1378. ObjectTypeDB::bind_method(_MD("set_scene_instance_load_placeholder","load_placeholder"),&Node::set_scene_instance_load_placeholder);
  1379. ObjectTypeDB::bind_method(_MD("get_scene_instance_load_placeholder"),&Node::get_scene_instance_load_placeholder);
  1380. ObjectTypeDB::bind_method(_MD("get_viewport"),&Node::get_viewport);
  1381. ObjectTypeDB::bind_method(_MD("queue_free"),&Node::queue_delete);
  1382. #ifdef TOOLS_ENABLED
  1383. ObjectTypeDB::bind_method(_MD("_set_import_path","import_path"),&Node::set_import_path);
  1384. ObjectTypeDB::bind_method(_MD("_get_import_path"),&Node::get_import_path);
  1385. ADD_PROPERTYNZ( PropertyInfo(Variant::NODE_PATH,"_import_path",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_import_path"),_SCS("_get_import_path"));
  1386. #endif
  1387. BIND_CONSTANT( NOTIFICATION_ENTER_TREE );
  1388. BIND_CONSTANT( NOTIFICATION_EXIT_TREE );
  1389. BIND_CONSTANT( NOTIFICATION_MOVED_IN_PARENT );
  1390. //BIND_CONSTANT( NOTIFICATION_PARENT_DECONFIGURED );
  1391. BIND_CONSTANT( NOTIFICATION_READY );
  1392. BIND_CONSTANT( NOTIFICATION_FIXED_PROCESS );
  1393. BIND_CONSTANT( NOTIFICATION_PROCESS );
  1394. BIND_CONSTANT( NOTIFICATION_PARENTED );
  1395. BIND_CONSTANT( NOTIFICATION_UNPARENTED );
  1396. BIND_CONSTANT( NOTIFICATION_PAUSED );
  1397. BIND_CONSTANT( NOTIFICATION_UNPAUSED );
  1398. BIND_CONSTANT( PAUSE_MODE_INHERIT );
  1399. BIND_CONSTANT( PAUSE_MODE_STOP );
  1400. BIND_CONSTANT( PAUSE_MODE_PROCESS );
  1401. ADD_SIGNAL( MethodInfo("renamed") );
  1402. ADD_SIGNAL( MethodInfo("enter_tree") );
  1403. ADD_SIGNAL( MethodInfo("exit_tree") );
  1404. // ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "process/process" ),_SCS("set_process"),_SCS("is_processing") );
  1405. // ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "process/fixed_process" ), _SCS("set_fixed_process"),_SCS("is_fixed_processing") );
  1406. //ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "process/input" ), _SCS("set_process_input"),_SCS("is_processing_input" ) );
  1407. //ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "process/unhandled_input" ), _SCS("set_process_unhandled_input"),_SCS("is_processing_unhandled_input" ) );
  1408. ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "process/pause_mode",PROPERTY_HINT_ENUM,"Inherit,Stop,Process" ), _SCS("set_pause_mode"),_SCS("get_pause_mode" ) );
  1409. BIND_VMETHOD( MethodInfo("_process",PropertyInfo(Variant::REAL,"delta")) );
  1410. BIND_VMETHOD( MethodInfo("_fixed_process",PropertyInfo(Variant::REAL,"delta")) );
  1411. BIND_VMETHOD( MethodInfo("_enter_tree") );
  1412. BIND_VMETHOD( MethodInfo("_exit_tree") );
  1413. BIND_VMETHOD( MethodInfo("_ready") );
  1414. BIND_VMETHOD( MethodInfo("_input",PropertyInfo(Variant::INPUT_EVENT,"event")) );
  1415. BIND_VMETHOD( MethodInfo("_unhandled_input",PropertyInfo(Variant::INPUT_EVENT,"event")) );
  1416. BIND_VMETHOD( MethodInfo("_unhandled_key_input",PropertyInfo(Variant::INPUT_EVENT,"key_event")) );
  1417. //ObjectTypeDB::bind_method(_MD("get_child",&Node::get_child,PH("index")));
  1418. //ObjectTypeDB::bind_method(_MD("get_node",&Node::get_node,PH("path")));
  1419. }
  1420. Node::Node() {
  1421. data.pos=-1;
  1422. data.depth=-1;
  1423. data.blocked=0;
  1424. data.parent=NULL;
  1425. data.tree=NULL;
  1426. data.fixed_process=false;
  1427. data.idle_process=false;
  1428. data.inside_tree=false;
  1429. data.owner=NULL;
  1430. data.OW=NULL;
  1431. data.input=false;
  1432. data.unhandled_input=false;
  1433. data.unhandled_key_input=false;
  1434. data.pause_mode=PAUSE_MODE_INHERIT;
  1435. data.pause_owner=NULL;
  1436. data.parent_owned=false;
  1437. data.in_constructor=true;
  1438. data.viewport=NULL;
  1439. data.use_placeholder=false;
  1440. }
  1441. Node::~Node() {
  1442. data.grouped.clear();
  1443. data.owned.clear();
  1444. data.children.clear();
  1445. ERR_FAIL_COND(data.parent);
  1446. ERR_FAIL_COND(data.children.size());
  1447. }
  1448. ////////////////////////////////