visual_script_func_nodes.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. #include "visual_script_func_nodes.h"
  2. #include "scene/main/scene_main_loop.h"
  3. #include "os/os.h"
  4. #include "scene/main/node.h"
  5. #include "visual_script_nodes.h"
  6. //////////////////////////////////////////
  7. ////////////////CALL//////////////////////
  8. //////////////////////////////////////////
  9. int VisualScriptFunctionCall::get_output_sequence_port_count() const {
  10. return 1;
  11. }
  12. bool VisualScriptFunctionCall::has_input_sequence_port() const{
  13. return true;
  14. }
  15. #ifdef TOOLS_ENABLED
  16. static Node* _find_script_node(Node* p_edited_scene,Node* p_current_node,const Ref<Script> &script) {
  17. if (p_edited_scene!=p_current_node && p_current_node->get_owner()!=p_edited_scene)
  18. return NULL;
  19. Ref<Script> scr = p_current_node->get_script();
  20. if (scr.is_valid() && scr==script)
  21. return p_current_node;
  22. for(int i=0;i<p_current_node->get_child_count();i++) {
  23. Node *n = _find_script_node(p_edited_scene,p_current_node->get_child(i),script);
  24. if (n)
  25. return n;
  26. }
  27. return NULL;
  28. }
  29. #endif
  30. Node *VisualScriptFunctionCall::_get_base_node() const {
  31. #ifdef TOOLS_ENABLED
  32. Ref<Script> script = get_visual_script();
  33. if (!script.is_valid())
  34. return NULL;
  35. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  36. if (!main_loop)
  37. return NULL;
  38. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  39. if (!scene_tree)
  40. return NULL;
  41. Node *edited_scene = scene_tree->get_edited_scene_root();
  42. if (!edited_scene)
  43. return NULL;
  44. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  45. if (!script_node)
  46. return NULL;
  47. if (!script_node->has_node(base_path))
  48. return NULL;
  49. Node *path_to = script_node->get_node(base_path);
  50. return path_to;
  51. #else
  52. return NULL;
  53. #endif
  54. }
  55. StringName VisualScriptFunctionCall::_get_base_type() const {
  56. if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid())
  57. return get_visual_script()->get_instance_base_type();
  58. else if (call_mode==CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  59. Node *path = _get_base_node();
  60. if (path)
  61. return path->get_type();
  62. }
  63. return base_type;
  64. }
  65. int VisualScriptFunctionCall::get_input_value_port_count() const{
  66. if (call_mode==CALL_MODE_BASIC_TYPE) {
  67. Vector<StringName> names = Variant::get_method_argument_names(basic_type,function);
  68. return names.size()+1;
  69. } else {
  70. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  71. if (!mb)
  72. return 0;
  73. return mb->get_argument_count() + (call_mode==CALL_MODE_INSTANCE?1:0) - use_default_args;
  74. }
  75. }
  76. int VisualScriptFunctionCall::get_output_value_port_count() const{
  77. if (call_mode==CALL_MODE_BASIC_TYPE) {
  78. bool returns=false;
  79. Variant::get_method_return_type(basic_type,function,&returns);
  80. return returns?1:0;
  81. } else {
  82. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  83. if (!mb)
  84. return 0;
  85. return mb->has_return() ? 1 : 0;
  86. }
  87. }
  88. String VisualScriptFunctionCall::get_output_sequence_port_text(int p_port) const {
  89. return String();
  90. }
  91. PropertyInfo VisualScriptFunctionCall::get_input_value_port_info(int p_idx) const{
  92. if (call_mode==CALL_MODE_INSTANCE || call_mode==CALL_MODE_BASIC_TYPE) {
  93. if (p_idx==0) {
  94. PropertyInfo pi;
  95. pi.type=(call_mode==CALL_MODE_INSTANCE?Variant::OBJECT:basic_type);
  96. pi.name=(call_mode==CALL_MODE_INSTANCE?String("instance"):Variant::get_type_name(basic_type).to_lower());
  97. return pi;
  98. } else {
  99. p_idx--;
  100. }
  101. }
  102. #ifdef DEBUG_METHODS_ENABLED
  103. if (call_mode==CALL_MODE_BASIC_TYPE) {
  104. Vector<StringName> names = Variant::get_method_argument_names(basic_type,function);
  105. Vector<Variant::Type> types = Variant::get_method_argument_types(basic_type,function);
  106. return PropertyInfo(types[p_idx],names[p_idx]);
  107. } else {
  108. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  109. if (!mb)
  110. return PropertyInfo();
  111. return mb->get_argument_info(p_idx);
  112. }
  113. #else
  114. return PropertyInfo();
  115. #endif
  116. }
  117. PropertyInfo VisualScriptFunctionCall::get_output_value_port_info(int p_idx) const{
  118. #ifdef DEBUG_METHODS_ENABLED
  119. if (call_mode==CALL_MODE_BASIC_TYPE) {
  120. return PropertyInfo(Variant::get_method_return_type(basic_type,function),"");
  121. } else {
  122. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  123. if (!mb)
  124. return PropertyInfo();
  125. PropertyInfo pi = mb->get_argument_info(-1);
  126. pi.name="";
  127. return pi;
  128. }
  129. #else
  130. return PropertyInfo();
  131. #endif
  132. }
  133. String VisualScriptFunctionCall::get_caption() const {
  134. static const char*cname[4]= {
  135. "CallSelf",
  136. "CallNode",
  137. "CallInstance",
  138. "CallBasic"
  139. };
  140. return cname[call_mode];
  141. }
  142. String VisualScriptFunctionCall::get_text() const {
  143. if (call_mode==CALL_MODE_SELF)
  144. return " "+String(function)+"()";
  145. else if (call_mode==CALL_MODE_BASIC_TYPE)
  146. return Variant::get_type_name(basic_type)+"."+String(function)+"()";
  147. else
  148. return " "+base_type+"."+String(function)+"()";
  149. }
  150. void VisualScriptFunctionCall::_update_defargs() {
  151. //save base type if accessible
  152. if (call_mode==CALL_MODE_NODE_PATH) {
  153. Node* node=_get_base_node();
  154. if (node) {
  155. base_type=node->get_type();
  156. }
  157. } else if (call_mode==CALL_MODE_SELF) {
  158. if (get_visual_script().is_valid()) {
  159. base_type=get_visual_script()->get_instance_base_type();
  160. }
  161. }
  162. if (call_mode==CALL_MODE_BASIC_TYPE) {
  163. use_default_args = Variant::get_method_default_arguments(basic_type,function).size();
  164. } else {
  165. if (!get_visual_script().is_valid())
  166. return; //do not change if not valid yet
  167. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  168. if (!mb)
  169. return;
  170. use_default_args=mb->get_default_argument_count();
  171. }
  172. }
  173. void VisualScriptFunctionCall::set_basic_type(Variant::Type p_type) {
  174. if (basic_type==p_type)
  175. return;
  176. basic_type=p_type;
  177. _update_defargs();
  178. _change_notify();
  179. ports_changed_notify();
  180. }
  181. Variant::Type VisualScriptFunctionCall::get_basic_type() const{
  182. return basic_type;
  183. }
  184. void VisualScriptFunctionCall::set_base_type(const StringName& p_type) {
  185. if (base_type==p_type)
  186. return;
  187. base_type=p_type;
  188. _update_defargs();
  189. _change_notify();
  190. ports_changed_notify();
  191. }
  192. StringName VisualScriptFunctionCall::get_base_type() const{
  193. return base_type;
  194. }
  195. void VisualScriptFunctionCall::set_function(const StringName& p_type){
  196. if (function==p_type)
  197. return;
  198. function=p_type;
  199. _update_defargs();
  200. _change_notify();
  201. ports_changed_notify();
  202. }
  203. StringName VisualScriptFunctionCall::get_function() const {
  204. return function;
  205. }
  206. void VisualScriptFunctionCall::set_base_path(const NodePath& p_type) {
  207. if (base_path==p_type)
  208. return;
  209. base_path=p_type;
  210. _update_defargs();
  211. _change_notify();
  212. ports_changed_notify();
  213. }
  214. NodePath VisualScriptFunctionCall::get_base_path() const {
  215. return base_path;
  216. }
  217. void VisualScriptFunctionCall::set_call_mode(CallMode p_mode) {
  218. if (call_mode==p_mode)
  219. return;
  220. call_mode=p_mode;
  221. _update_defargs();
  222. _change_notify();
  223. ports_changed_notify();
  224. }
  225. VisualScriptFunctionCall::CallMode VisualScriptFunctionCall::get_call_mode() const {
  226. return call_mode;
  227. }
  228. void VisualScriptFunctionCall::set_use_default_args(int p_amount) {
  229. if (use_default_args==p_amount)
  230. return;
  231. use_default_args=p_amount;
  232. ports_changed_notify();
  233. }
  234. int VisualScriptFunctionCall::get_use_default_args() const{
  235. return use_default_args;
  236. }
  237. void VisualScriptFunctionCall::_validate_property(PropertyInfo& property) const {
  238. if (property.name=="function/base_type") {
  239. if (call_mode!=CALL_MODE_INSTANCE) {
  240. property.usage=PROPERTY_USAGE_NOEDITOR;
  241. }
  242. }
  243. if (property.name=="function/basic_type") {
  244. if (call_mode!=CALL_MODE_BASIC_TYPE) {
  245. property.usage=0;
  246. }
  247. }
  248. if (property.name=="function/node_path") {
  249. if (call_mode!=CALL_MODE_NODE_PATH) {
  250. property.usage=0;
  251. } else {
  252. Node *bnode = _get_base_node();
  253. if (bnode) {
  254. property.hint_string=bnode->get_path(); //convert to loong string
  255. } else {
  256. }
  257. }
  258. }
  259. if (property.name=="function/function") {
  260. property.hint=PROPERTY_HINT_ENUM;
  261. List<MethodInfo> methods;
  262. if (call_mode==CALL_MODE_BASIC_TYPE) {
  263. if (basic_type==Variant::NIL) {
  264. property.usage=0;
  265. return; //nothing for nil
  266. }
  267. Variant::CallError ce;
  268. Variant v = Variant::construct(basic_type,NULL,0,ce);
  269. v.get_method_list(&methods);
  270. } else {
  271. StringName base = _get_base_type();
  272. ObjectTypeDB::get_method_list(base,&methods);
  273. }
  274. List<String> mstring;
  275. for (List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  276. if (E->get().name.begins_with("_"))
  277. continue;
  278. mstring.push_back(E->get().name.get_slice(":",0));
  279. }
  280. mstring.sort();
  281. String ml;
  282. for (List<String>::Element *E=mstring.front();E;E=E->next()) {
  283. if (ml!=String())
  284. ml+=",";
  285. ml+=E->get();
  286. }
  287. property.hint_string=ml;
  288. }
  289. if (property.name=="function/use_default_args") {
  290. property.hint=PROPERTY_HINT_RANGE;
  291. int mc=0;
  292. if (call_mode==CALL_MODE_BASIC_TYPE) {
  293. mc = Variant::get_method_default_arguments(basic_type,function).size();
  294. } else {
  295. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  296. if (mb) {
  297. mc=mb->get_default_argument_count();
  298. }
  299. }
  300. if (mc==0) {
  301. property.usage=0; //do not show
  302. } else {
  303. property.hint_string="0,"+itos(mc)+",1";
  304. }
  305. }
  306. }
  307. void VisualScriptFunctionCall::_bind_methods() {
  308. ObjectTypeDB::bind_method(_MD("set_base_type","base_type"),&VisualScriptFunctionCall::set_base_type);
  309. ObjectTypeDB::bind_method(_MD("get_base_type"),&VisualScriptFunctionCall::get_base_type);
  310. ObjectTypeDB::bind_method(_MD("set_basic_type","basic_type"),&VisualScriptFunctionCall::set_basic_type);
  311. ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptFunctionCall::get_basic_type);
  312. ObjectTypeDB::bind_method(_MD("set_function","function"),&VisualScriptFunctionCall::set_function);
  313. ObjectTypeDB::bind_method(_MD("get_function"),&VisualScriptFunctionCall::get_function);
  314. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptFunctionCall::set_call_mode);
  315. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptFunctionCall::get_call_mode);
  316. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptFunctionCall::set_base_path);
  317. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptFunctionCall::get_base_path);
  318. ObjectTypeDB::bind_method(_MD("set_use_default_args","amount"),&VisualScriptFunctionCall::set_use_default_args);
  319. ObjectTypeDB::bind_method(_MD("get_use_default_args"),&VisualScriptFunctionCall::get_use_default_args);
  320. String bt;
  321. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  322. if (i>0)
  323. bt+=",";
  324. bt+=Variant::get_type_name(Variant::Type(i));
  325. }
  326. ADD_PROPERTY(PropertyInfo(Variant::INT,"function/call_mode",PROPERTY_HINT_ENUM,"Self,Node Path,Instance,Basic Type",PROPERTY_USAGE_NOEDITOR),_SCS("set_call_mode"),_SCS("get_call_mode"));
  327. ADD_PROPERTY(PropertyInfo(Variant::STRING,"function/base_type",PROPERTY_HINT_TYPE_STRING,"Object"),_SCS("set_base_type"),_SCS("get_base_type"));
  328. ADD_PROPERTY(PropertyInfo(Variant::INT,"function/basic_type",PROPERTY_HINT_ENUM,bt),_SCS("set_basic_type"),_SCS("get_basic_type"));
  329. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"function/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  330. ADD_PROPERTY(PropertyInfo(Variant::STRING,"function/function"),_SCS("set_function"),_SCS("get_function"));
  331. ADD_PROPERTY(PropertyInfo(Variant::INT,"function/use_default_args"),_SCS("set_use_default_args"),_SCS("get_use_default_args"));
  332. BIND_CONSTANT( CALL_MODE_SELF );
  333. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  334. BIND_CONSTANT( CALL_MODE_INSTANCE);
  335. BIND_CONSTANT( CALL_MODE_BASIC_TYPE );
  336. }
  337. VisualScriptNodeInstance* VisualScriptFunctionCall::instance(VScriptInstance* p_instance) {
  338. return NULL;
  339. }
  340. VisualScriptFunctionCall::VisualScriptFunctionCall() {
  341. call_mode=CALL_MODE_INSTANCE;
  342. basic_type=Variant::NIL;
  343. use_default_args=0;
  344. base_type="Object";
  345. }
  346. template<VisualScriptFunctionCall::CallMode cmode>
  347. static Ref<VisualScriptNode> create_function_call_node(const String& p_name) {
  348. Ref<VisualScriptFunctionCall> node;
  349. node.instance();
  350. node->set_call_mode(cmode);
  351. return node;
  352. }
  353. //////////////////////////////////////////
  354. ////////////////SET//////////////////////
  355. //////////////////////////////////////////
  356. int VisualScriptPropertySet::get_output_sequence_port_count() const {
  357. return 1;
  358. }
  359. bool VisualScriptPropertySet::has_input_sequence_port() const{
  360. return true;
  361. }
  362. Node *VisualScriptPropertySet::_get_base_node() const {
  363. #ifdef TOOLS_ENABLED
  364. Ref<Script> script = get_visual_script();
  365. if (!script.is_valid())
  366. return NULL;
  367. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  368. if (!main_loop)
  369. return NULL;
  370. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  371. if (!scene_tree)
  372. return NULL;
  373. Node *edited_scene = scene_tree->get_edited_scene_root();
  374. if (!edited_scene)
  375. return NULL;
  376. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  377. if (!script_node)
  378. return NULL;
  379. if (!script_node->has_node(base_path))
  380. return NULL;
  381. Node *path_to = script_node->get_node(base_path);
  382. return path_to;
  383. #else
  384. return NULL;
  385. #endif
  386. }
  387. StringName VisualScriptPropertySet::_get_base_type() const {
  388. if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid())
  389. return get_visual_script()->get_instance_base_type();
  390. else if (call_mode==CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  391. Node *path = _get_base_node();
  392. if (path)
  393. return path->get_type();
  394. }
  395. return base_type;
  396. }
  397. int VisualScriptPropertySet::get_input_value_port_count() const{
  398. int pc = (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE)?1:0;
  399. if (!use_builtin_value)
  400. pc++;
  401. return pc;
  402. }
  403. int VisualScriptPropertySet::get_output_value_port_count() const{
  404. return 0;
  405. }
  406. String VisualScriptPropertySet::get_output_sequence_port_text(int p_port) const {
  407. return String();
  408. }
  409. PropertyInfo VisualScriptPropertySet::get_input_value_port_info(int p_idx) const{
  410. if (call_mode==CALL_MODE_INSTANCE || call_mode==CALL_MODE_BASIC_TYPE) {
  411. if (p_idx==0) {
  412. PropertyInfo pi;
  413. pi.type=(call_mode==CALL_MODE_INSTANCE?Variant::OBJECT:basic_type);
  414. pi.name=(call_mode==CALL_MODE_INSTANCE?String("instance"):Variant::get_type_name(basic_type).to_lower());
  415. return pi;
  416. } else {
  417. p_idx--;
  418. }
  419. }
  420. #ifdef DEBUG_METHODS_ENABLED
  421. //not very efficient but..
  422. List<PropertyInfo> pinfo;
  423. if (call_mode==CALL_MODE_BASIC_TYPE) {
  424. Variant::CallError ce;
  425. Variant v = Variant::construct(basic_type,NULL,0,ce);
  426. v.get_property_list(&pinfo);
  427. } else if (call_mode==CALL_MODE_NODE_PATH) {
  428. Node *n = _get_base_node();
  429. if (n) {
  430. n->get_property_list(&pinfo);
  431. } else {
  432. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  433. }
  434. } else {
  435. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  436. }
  437. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  438. if (E->get().name==property) {
  439. PropertyInfo info=E->get();
  440. info.name="value";
  441. return info;
  442. }
  443. }
  444. #endif
  445. return PropertyInfo(Variant::NIL,"value");
  446. }
  447. PropertyInfo VisualScriptPropertySet::get_output_value_port_info(int p_idx) const{
  448. return PropertyInfo();
  449. }
  450. String VisualScriptPropertySet::get_caption() const {
  451. static const char*cname[4]= {
  452. "SelfSet",
  453. "NodeSet",
  454. "InstanceSet",
  455. "BasicSet"
  456. };
  457. return cname[call_mode];
  458. }
  459. String VisualScriptPropertySet::get_text() const {
  460. String prop;
  461. if (call_mode==CALL_MODE_BASIC_TYPE)
  462. prop=Variant::get_type_name(basic_type)+"."+property;
  463. else
  464. prop=property;
  465. if (use_builtin_value) {
  466. String bit = builtin_value.get_construct_string();
  467. if (bit.length()>40) {
  468. bit=bit.substr(0,40);
  469. bit+="...";
  470. }
  471. prop+="\n "+bit;
  472. }
  473. return prop;
  474. }
  475. void VisualScriptPropertySet::_update_base_type() {
  476. //cache it because this information may not be available on load
  477. if (call_mode==CALL_MODE_NODE_PATH) {
  478. Node* node=_get_base_node();
  479. if (node) {
  480. base_type=node->get_type();
  481. }
  482. } else if (call_mode==CALL_MODE_SELF) {
  483. if (get_visual_script().is_valid()) {
  484. base_type=get_visual_script()->get_instance_base_type();
  485. }
  486. }
  487. }
  488. void VisualScriptPropertySet::set_basic_type(Variant::Type p_type) {
  489. if (basic_type==p_type)
  490. return;
  491. basic_type=p_type;
  492. _change_notify();
  493. _update_base_type();
  494. ports_changed_notify();
  495. }
  496. Variant::Type VisualScriptPropertySet::get_basic_type() const{
  497. return basic_type;
  498. }
  499. void VisualScriptPropertySet::set_base_type(const StringName& p_type) {
  500. if (base_type==p_type)
  501. return;
  502. base_type=p_type;
  503. _change_notify();
  504. ports_changed_notify();
  505. }
  506. StringName VisualScriptPropertySet::get_base_type() const{
  507. return base_type;
  508. }
  509. void VisualScriptPropertySet::set_property(const StringName& p_type){
  510. if (property==p_type)
  511. return;
  512. property=p_type;
  513. _change_notify();
  514. ports_changed_notify();
  515. }
  516. StringName VisualScriptPropertySet::get_property() const {
  517. return property;
  518. }
  519. void VisualScriptPropertySet::set_base_path(const NodePath& p_type) {
  520. if (base_path==p_type)
  521. return;
  522. base_path=p_type;
  523. _update_base_type();
  524. _change_notify();
  525. ports_changed_notify();
  526. }
  527. NodePath VisualScriptPropertySet::get_base_path() const {
  528. return base_path;
  529. }
  530. void VisualScriptPropertySet::set_call_mode(CallMode p_mode) {
  531. if (call_mode==p_mode)
  532. return;
  533. call_mode=p_mode;
  534. _update_base_type();
  535. _change_notify();
  536. ports_changed_notify();
  537. }
  538. VisualScriptPropertySet::CallMode VisualScriptPropertySet::get_call_mode() const {
  539. return call_mode;
  540. }
  541. void VisualScriptPropertySet::set_use_builtin_value(bool p_use) {
  542. if (use_builtin_value==p_use)
  543. return;
  544. use_builtin_value=p_use;
  545. _change_notify();
  546. ports_changed_notify();
  547. }
  548. bool VisualScriptPropertySet::is_using_builtin_value() const{
  549. return use_builtin_value;
  550. }
  551. void VisualScriptPropertySet::set_builtin_value(const Variant& p_value){
  552. if (builtin_value==p_value)
  553. return;
  554. builtin_value=p_value;
  555. }
  556. Variant VisualScriptPropertySet::get_builtin_value() const{
  557. return builtin_value;
  558. }
  559. void VisualScriptPropertySet::_validate_property(PropertyInfo& property) const {
  560. if (property.name=="property/base_type") {
  561. if (call_mode!=CALL_MODE_INSTANCE) {
  562. property.usage=PROPERTY_USAGE_NOEDITOR;
  563. }
  564. }
  565. if (property.name=="property/basic_type") {
  566. if (call_mode!=CALL_MODE_BASIC_TYPE) {
  567. property.usage=0;
  568. }
  569. }
  570. if (property.name=="property/node_path") {
  571. if (call_mode!=CALL_MODE_NODE_PATH) {
  572. property.usage=0;
  573. } else {
  574. Node *bnode = _get_base_node();
  575. if (bnode) {
  576. property.hint_string=bnode->get_path(); //convert to loong string
  577. } else {
  578. }
  579. }
  580. }
  581. if (property.name=="property/property") {
  582. property.hint=PROPERTY_HINT_ENUM;
  583. List<PropertyInfo> pinfo;
  584. if (call_mode==CALL_MODE_BASIC_TYPE) {
  585. Variant::CallError ce;
  586. Variant v = Variant::construct(basic_type,NULL,0,ce);
  587. v.get_property_list(&pinfo);
  588. } else if (call_mode==CALL_MODE_NODE_PATH) {
  589. Node *n = _get_base_node();
  590. if (n) {
  591. n->get_property_list(&pinfo);
  592. } else {
  593. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  594. }
  595. } else {
  596. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  597. }
  598. List<String> mstring;
  599. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  600. if (E->get().usage&PROPERTY_USAGE_EDITOR) {
  601. mstring.push_back(E->get().name);
  602. }
  603. }
  604. String ml;
  605. for (List<String>::Element *E=mstring.front();E;E=E->next()) {
  606. if (ml!=String())
  607. ml+=",";
  608. ml+=E->get();
  609. }
  610. if (ml==String()) {
  611. property.usage=PROPERTY_USAGE_NOEDITOR; //do not show for editing if empty
  612. } else {
  613. property.hint_string=ml;
  614. }
  615. }
  616. if (property.name=="value/builtin") {
  617. if (!use_builtin_value) {
  618. property.usage=0;
  619. } else {
  620. List<PropertyInfo> pinfo;
  621. if (call_mode==CALL_MODE_BASIC_TYPE) {
  622. Variant::CallError ce;
  623. Variant v = Variant::construct(basic_type,NULL,0,ce);
  624. v.get_property_list(&pinfo);
  625. } else if (call_mode==CALL_MODE_NODE_PATH) {
  626. Node *n = _get_base_node();
  627. if (n) {
  628. n->get_property_list(&pinfo);
  629. } else {
  630. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  631. }
  632. } else {
  633. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  634. }
  635. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  636. if (E->get().name==this->property) {
  637. property.hint=E->get().hint;
  638. property.type=E->get().type;
  639. property.hint_string=E->get().hint_string;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. void VisualScriptPropertySet::_bind_methods() {
  646. ObjectTypeDB::bind_method(_MD("set_base_type","base_type"),&VisualScriptPropertySet::set_base_type);
  647. ObjectTypeDB::bind_method(_MD("get_base_type"),&VisualScriptPropertySet::get_base_type);
  648. ObjectTypeDB::bind_method(_MD("set_basic_type","basic_type"),&VisualScriptPropertySet::set_basic_type);
  649. ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptPropertySet::get_basic_type);
  650. ObjectTypeDB::bind_method(_MD("set_property","property"),&VisualScriptPropertySet::set_property);
  651. ObjectTypeDB::bind_method(_MD("get_property"),&VisualScriptPropertySet::get_property);
  652. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptPropertySet::set_call_mode);
  653. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptPropertySet::get_call_mode);
  654. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptPropertySet::set_base_path);
  655. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptPropertySet::get_base_path);
  656. ObjectTypeDB::bind_method(_MD("set_builtin_value","value"),&VisualScriptPropertySet::set_builtin_value);
  657. ObjectTypeDB::bind_method(_MD("get_builtin_value"),&VisualScriptPropertySet::get_builtin_value);
  658. ObjectTypeDB::bind_method(_MD("set_use_builtin_value","enable"),&VisualScriptPropertySet::set_use_builtin_value);
  659. ObjectTypeDB::bind_method(_MD("is_using_builtin_value"),&VisualScriptPropertySet::is_using_builtin_value);
  660. String bt;
  661. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  662. if (i>0)
  663. bt+=",";
  664. bt+=Variant::get_type_name(Variant::Type(i));
  665. }
  666. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/set_mode",PROPERTY_HINT_ENUM,"Self,Node Path,Instance,Basic Type",PROPERTY_USAGE_NOEDITOR),_SCS("set_call_mode"),_SCS("get_call_mode"));
  667. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_type",PROPERTY_HINT_TYPE_STRING,"Object"),_SCS("set_base_type"),_SCS("get_base_type"));
  668. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/basic_type",PROPERTY_HINT_ENUM,bt),_SCS("set_basic_type"),_SCS("get_basic_type"));
  669. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"property/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  670. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/property"),_SCS("set_property"),_SCS("get_property"));
  671. ADD_PROPERTY(PropertyInfo(Variant::BOOL,"value/use_builtin"),_SCS("set_use_builtin_value"),_SCS("is_using_builtin_value"));
  672. ADD_PROPERTY(PropertyInfo(Variant::NIL,"value/builtin"),_SCS("set_builtin_value"),_SCS("get_builtin_value"));
  673. BIND_CONSTANT( CALL_MODE_SELF );
  674. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  675. BIND_CONSTANT( CALL_MODE_INSTANCE);
  676. }
  677. VisualScriptNodeInstance* VisualScriptPropertySet::instance(VScriptInstance* p_instance) {
  678. return NULL;
  679. }
  680. VisualScriptPropertySet::VisualScriptPropertySet() {
  681. call_mode=CALL_MODE_INSTANCE;
  682. base_type="Object";
  683. basic_type=Variant::NIL;
  684. }
  685. template<VisualScriptPropertySet::CallMode cmode>
  686. static Ref<VisualScriptNode> create_property_set_node(const String& p_name) {
  687. Ref<VisualScriptPropertySet> node;
  688. node.instance();
  689. node->set_call_mode(cmode);
  690. return node;
  691. }
  692. //////////////////////////////////////////
  693. ////////////////GET//////////////////////
  694. //////////////////////////////////////////
  695. int VisualScriptPropertyGet::get_output_sequence_port_count() const {
  696. return 1;
  697. }
  698. bool VisualScriptPropertyGet::has_input_sequence_port() const{
  699. return true;
  700. }
  701. void VisualScriptPropertyGet::_update_base_type() {
  702. //cache it because this information may not be available on load
  703. if (call_mode==CALL_MODE_NODE_PATH) {
  704. Node* node=_get_base_node();
  705. if (node) {
  706. base_type=node->get_type();
  707. }
  708. } else if (call_mode==CALL_MODE_SELF) {
  709. if (get_visual_script().is_valid()) {
  710. base_type=get_visual_script()->get_instance_base_type();
  711. }
  712. }
  713. }
  714. Node *VisualScriptPropertyGet::_get_base_node() const {
  715. #ifdef TOOLS_ENABLED
  716. Ref<Script> script = get_visual_script();
  717. if (!script.is_valid())
  718. return NULL;
  719. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  720. if (!main_loop)
  721. return NULL;
  722. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  723. if (!scene_tree)
  724. return NULL;
  725. Node *edited_scene = scene_tree->get_edited_scene_root();
  726. if (!edited_scene)
  727. return NULL;
  728. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  729. if (!script_node)
  730. return NULL;
  731. if (!script_node->has_node(base_path))
  732. return NULL;
  733. Node *path_to = script_node->get_node(base_path);
  734. return path_to;
  735. #else
  736. return NULL;
  737. #endif
  738. }
  739. StringName VisualScriptPropertyGet::_get_base_type() const {
  740. if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid())
  741. return get_visual_script()->get_instance_base_type();
  742. else if (call_mode==CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  743. Node *path = _get_base_node();
  744. if (path)
  745. return path->get_type();
  746. }
  747. return base_type;
  748. }
  749. int VisualScriptPropertyGet::get_input_value_port_count() const{
  750. return (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE)?1:0;
  751. }
  752. int VisualScriptPropertyGet::get_output_value_port_count() const{
  753. return 1;
  754. }
  755. String VisualScriptPropertyGet::get_output_sequence_port_text(int p_port) const {
  756. return String();
  757. }
  758. PropertyInfo VisualScriptPropertyGet::get_input_value_port_info(int p_idx) const{
  759. if (call_mode==CALL_MODE_INSTANCE || call_mode==CALL_MODE_BASIC_TYPE) {
  760. if (p_idx==0) {
  761. PropertyInfo pi;
  762. pi.type=(call_mode==CALL_MODE_INSTANCE?Variant::OBJECT:basic_type);
  763. pi.name=(call_mode==CALL_MODE_INSTANCE?String("instance"):Variant::get_type_name(basic_type).to_lower());
  764. return pi;
  765. } else {
  766. p_idx--;
  767. }
  768. }
  769. return PropertyInfo();
  770. }
  771. PropertyInfo VisualScriptPropertyGet::get_output_value_port_info(int p_idx) const{
  772. #ifdef DEBUG_METHODS_ENABLED
  773. //not very efficient but..
  774. List<PropertyInfo> pinfo;
  775. if (call_mode==CALL_MODE_BASIC_TYPE) {
  776. Variant::CallError ce;
  777. Variant v = Variant::construct(basic_type,NULL,0,ce);
  778. v.get_property_list(&pinfo);
  779. } else if (call_mode==CALL_MODE_NODE_PATH) {
  780. Node *n = _get_base_node();
  781. if (n) {
  782. n->get_property_list(&pinfo);
  783. } else {
  784. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  785. }
  786. } else {
  787. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  788. }
  789. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  790. if (E->get().name==property) {
  791. PropertyInfo info=E->get();
  792. info.name="";
  793. return info;
  794. }
  795. }
  796. #endif
  797. return PropertyInfo(Variant::NIL,"");
  798. }
  799. String VisualScriptPropertyGet::get_caption() const {
  800. static const char*cname[4]= {
  801. "SelfGet",
  802. "NodeGet",
  803. "InstanceGet",
  804. "BasicGet"
  805. };
  806. return cname[call_mode];
  807. }
  808. String VisualScriptPropertyGet::get_text() const {
  809. if (call_mode==CALL_MODE_BASIC_TYPE)
  810. return Variant::get_type_name(basic_type)+"."+property;
  811. else
  812. return property;
  813. }
  814. void VisualScriptPropertyGet::set_base_type(const StringName& p_type) {
  815. if (base_type==p_type)
  816. return;
  817. base_type=p_type;
  818. _change_notify();
  819. ports_changed_notify();
  820. }
  821. StringName VisualScriptPropertyGet::get_base_type() const{
  822. return base_type;
  823. }
  824. void VisualScriptPropertyGet::set_property(const StringName& p_type){
  825. if (property==p_type)
  826. return;
  827. property=p_type;
  828. _change_notify();
  829. ports_changed_notify();
  830. }
  831. StringName VisualScriptPropertyGet::get_property() const {
  832. return property;
  833. }
  834. void VisualScriptPropertyGet::set_base_path(const NodePath& p_type) {
  835. if (base_path==p_type)
  836. return;
  837. base_path=p_type;
  838. _change_notify();
  839. _update_base_type();
  840. ports_changed_notify();
  841. }
  842. NodePath VisualScriptPropertyGet::get_base_path() const {
  843. return base_path;
  844. }
  845. void VisualScriptPropertyGet::set_call_mode(CallMode p_mode) {
  846. if (call_mode==p_mode)
  847. return;
  848. call_mode=p_mode;
  849. _change_notify();
  850. _update_base_type();
  851. ports_changed_notify();
  852. }
  853. VisualScriptPropertyGet::CallMode VisualScriptPropertyGet::get_call_mode() const {
  854. return call_mode;
  855. }
  856. void VisualScriptPropertyGet::set_basic_type(Variant::Type p_type) {
  857. if (basic_type==p_type)
  858. return;
  859. basic_type=p_type;
  860. _change_notify();
  861. ports_changed_notify();
  862. }
  863. Variant::Type VisualScriptPropertyGet::get_basic_type() const{
  864. return basic_type;
  865. }
  866. void VisualScriptPropertyGet::_validate_property(PropertyInfo& property) const {
  867. if (property.name=="property/base_type") {
  868. if (call_mode!=CALL_MODE_INSTANCE) {
  869. property.usage=PROPERTY_USAGE_NOEDITOR;
  870. }
  871. }
  872. if (property.name=="property/basic_type") {
  873. if (call_mode!=CALL_MODE_BASIC_TYPE) {
  874. property.usage=0;
  875. }
  876. }
  877. if (property.name=="property/node_path") {
  878. if (call_mode!=CALL_MODE_NODE_PATH) {
  879. property.usage=0;
  880. } else {
  881. Node *bnode = _get_base_node();
  882. if (bnode) {
  883. property.hint_string=bnode->get_path(); //convert to loong string
  884. } else {
  885. }
  886. }
  887. }
  888. if (property.name=="property/property") {
  889. property.hint=PROPERTY_HINT_ENUM;
  890. List<PropertyInfo> pinfo;
  891. if (call_mode==CALL_MODE_BASIC_TYPE) {
  892. Variant::CallError ce;
  893. Variant v = Variant::construct(basic_type,NULL,0,ce);
  894. v.get_property_list(&pinfo);
  895. } else if (call_mode==CALL_MODE_NODE_PATH) {
  896. Node *n = _get_base_node();
  897. if (n) {
  898. n->get_property_list(&pinfo);
  899. } else {
  900. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  901. }
  902. } else {
  903. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  904. }
  905. List<String> mstring;
  906. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  907. if (E->get().usage&PROPERTY_USAGE_EDITOR)
  908. mstring.push_back(E->get().name);
  909. }
  910. String ml;
  911. for (List<String>::Element *E=mstring.front();E;E=E->next()) {
  912. if (ml!=String())
  913. ml+=",";
  914. ml+=E->get();
  915. }
  916. if (ml==String()) {
  917. property.usage=PROPERTY_USAGE_NOEDITOR; //do not show for editing if empty
  918. } else {
  919. property.hint_string=ml;
  920. }
  921. }
  922. }
  923. void VisualScriptPropertyGet::_bind_methods() {
  924. ObjectTypeDB::bind_method(_MD("set_base_type","base_type"),&VisualScriptPropertyGet::set_base_type);
  925. ObjectTypeDB::bind_method(_MD("get_base_type"),&VisualScriptPropertyGet::get_base_type);
  926. ObjectTypeDB::bind_method(_MD("set_basic_type","basic_type"),&VisualScriptPropertyGet::set_basic_type);
  927. ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptPropertyGet::get_basic_type);
  928. ObjectTypeDB::bind_method(_MD("set_property","property"),&VisualScriptPropertyGet::set_property);
  929. ObjectTypeDB::bind_method(_MD("get_property"),&VisualScriptPropertyGet::get_property);
  930. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptPropertyGet::set_call_mode);
  931. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptPropertyGet::get_call_mode);
  932. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptPropertyGet::set_base_path);
  933. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptPropertyGet::get_base_path);
  934. String bt;
  935. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  936. if (i>0)
  937. bt+=",";
  938. bt+=Variant::get_type_name(Variant::Type(i));
  939. }
  940. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/set_mode",PROPERTY_HINT_ENUM,"Self,Node Path,Instance",PROPERTY_USAGE_NOEDITOR),_SCS("set_call_mode"),_SCS("get_call_mode"));
  941. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_type",PROPERTY_HINT_TYPE_STRING,"Object"),_SCS("set_base_type"),_SCS("get_base_type"));
  942. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/basic_type",PROPERTY_HINT_ENUM,bt),_SCS("set_basic_type"),_SCS("get_basic_type"));
  943. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"property/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  944. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/property"),_SCS("set_property"),_SCS("get_property"));
  945. BIND_CONSTANT( CALL_MODE_SELF );
  946. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  947. BIND_CONSTANT( CALL_MODE_INSTANCE);
  948. }
  949. VisualScriptNodeInstance* VisualScriptPropertyGet::instance(VScriptInstance* p_instance) {
  950. return NULL;
  951. }
  952. VisualScriptPropertyGet::VisualScriptPropertyGet() {
  953. call_mode=CALL_MODE_INSTANCE;
  954. base_type="Object";
  955. basic_type=Variant::NIL;
  956. }
  957. template<VisualScriptPropertyGet::CallMode cmode>
  958. static Ref<VisualScriptNode> create_property_get_node(const String& p_name) {
  959. Ref<VisualScriptPropertyGet> node;
  960. node.instance();
  961. node->set_call_mode(cmode);
  962. return node;
  963. }
  964. //////////////////////////////////////////
  965. ////////////////SCRIPT CALL//////////////////////
  966. //////////////////////////////////////////
  967. int VisualScriptScriptCall::get_output_sequence_port_count() const {
  968. return 1;
  969. }
  970. bool VisualScriptScriptCall::has_input_sequence_port() const{
  971. return true;
  972. }
  973. Node *VisualScriptScriptCall::_get_base_node() const {
  974. #ifdef TOOLS_ENABLED
  975. Ref<Script> script = get_visual_script();
  976. if (!script.is_valid())
  977. return NULL;
  978. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  979. if (!main_loop)
  980. return NULL;
  981. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  982. if (!scene_tree)
  983. return NULL;
  984. Node *edited_scene = scene_tree->get_edited_scene_root();
  985. if (!edited_scene)
  986. return NULL;
  987. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  988. if (!script_node)
  989. return NULL;
  990. if (!script_node->has_node(base_path))
  991. return NULL;
  992. Node *path_to = script_node->get_node(base_path);
  993. return path_to;
  994. #else
  995. return NULL;
  996. #endif
  997. }
  998. int VisualScriptScriptCall::get_input_value_port_count() const{
  999. if (call_mode==CALL_MODE_SELF) {
  1000. Ref<VisualScript> vs = get_visual_script();
  1001. if (vs.is_valid()) {
  1002. if (!vs->has_function(function))
  1003. return 0;
  1004. int id = vs->get_function_node_id(function);
  1005. if (id<0)
  1006. return 0;
  1007. Ref<VisualScriptFunction> func = vs->get_node(function,id);
  1008. return func->get_argument_count();
  1009. }
  1010. } else {
  1011. Node*base = _get_base_node();
  1012. if (!base)
  1013. return 0;
  1014. Ref<Script> script = base->get_script();
  1015. if (!script.is_valid())
  1016. return 0;
  1017. List<MethodInfo> functions;
  1018. script->get_method_list(&functions);
  1019. for (List<MethodInfo>::Element *E=functions.front();E;E=E->next()) {
  1020. if (E->get().name==function) {
  1021. return E->get().arguments.size();
  1022. }
  1023. }
  1024. }
  1025. return 0;
  1026. }
  1027. int VisualScriptScriptCall::get_output_value_port_count() const{
  1028. return 1;
  1029. }
  1030. String VisualScriptScriptCall::get_output_sequence_port_text(int p_port) const {
  1031. return String();
  1032. }
  1033. PropertyInfo VisualScriptScriptCall::get_input_value_port_info(int p_idx) const{
  1034. if (call_mode==CALL_MODE_SELF) {
  1035. Ref<VisualScript> vs = get_visual_script();
  1036. if (vs.is_valid()) {
  1037. if (!vs->has_function(function))
  1038. return PropertyInfo();
  1039. int id = vs->get_function_node_id(function);
  1040. if (id<0)
  1041. return PropertyInfo();
  1042. Ref<VisualScriptFunction> func = vs->get_node(function,id);
  1043. if (p_idx>=func->get_argument_count())
  1044. return PropertyInfo();
  1045. return PropertyInfo(func->get_argument_type(p_idx),func->get_argument_name(p_idx));
  1046. }
  1047. } else {
  1048. Node*base = _get_base_node();
  1049. if (!base)
  1050. return PropertyInfo();
  1051. Ref<Script> script = base->get_script();
  1052. if (!script.is_valid())
  1053. return PropertyInfo();
  1054. List<MethodInfo> functions;
  1055. script->get_method_list(&functions);
  1056. for (List<MethodInfo>::Element *E=functions.front();E;E=E->next()) {
  1057. if (E->get().name==function) {
  1058. if (p_idx<0 || p_idx>=E->get().arguments.size())
  1059. return PropertyInfo();
  1060. return E->get().arguments[p_idx];
  1061. }
  1062. }
  1063. }
  1064. return PropertyInfo();
  1065. }
  1066. PropertyInfo VisualScriptScriptCall::get_output_value_port_info(int p_idx) const{
  1067. return PropertyInfo();
  1068. }
  1069. String VisualScriptScriptCall::get_caption() const {
  1070. return "ScriptCall";
  1071. }
  1072. String VisualScriptScriptCall::get_text() const {
  1073. return " "+String(function)+"()";
  1074. }
  1075. void VisualScriptScriptCall::set_function(const StringName& p_type){
  1076. if (function==p_type)
  1077. return;
  1078. function=p_type;
  1079. _change_notify();
  1080. ports_changed_notify();
  1081. }
  1082. StringName VisualScriptScriptCall::get_function() const {
  1083. return function;
  1084. }
  1085. void VisualScriptScriptCall::set_base_path(const NodePath& p_type) {
  1086. if (base_path==p_type)
  1087. return;
  1088. base_path=p_type;
  1089. _change_notify();
  1090. ports_changed_notify();
  1091. }
  1092. NodePath VisualScriptScriptCall::get_base_path() const {
  1093. return base_path;
  1094. }
  1095. void VisualScriptScriptCall::set_call_mode(CallMode p_mode) {
  1096. if (call_mode==p_mode)
  1097. return;
  1098. call_mode=p_mode;
  1099. _change_notify();
  1100. ports_changed_notify();
  1101. }
  1102. VisualScriptScriptCall::CallMode VisualScriptScriptCall::get_call_mode() const {
  1103. return call_mode;
  1104. }
  1105. void VisualScriptScriptCall::_validate_property(PropertyInfo& property) const {
  1106. if (property.name=="function/node_path") {
  1107. if (call_mode!=CALL_MODE_NODE_PATH) {
  1108. property.usage=0;
  1109. } else {
  1110. Node *bnode = _get_base_node();
  1111. if (bnode) {
  1112. property.hint_string=bnode->get_path(); //convert to loong string
  1113. } else {
  1114. }
  1115. }
  1116. }
  1117. if (property.name=="function/function") {
  1118. property.hint=PROPERTY_HINT_ENUM;
  1119. List<MethodInfo> methods;
  1120. if (call_mode==CALL_MODE_SELF) {
  1121. Ref<VisualScript> vs = get_visual_script();
  1122. if (vs.is_valid()) {
  1123. vs->get_method_list(&methods);
  1124. }
  1125. } else {
  1126. Node*base = _get_base_node();
  1127. if (!base)
  1128. return;
  1129. Ref<Script> script = base->get_script();
  1130. if (!script.is_valid())
  1131. return;
  1132. script->get_method_list(&methods);
  1133. }
  1134. List<String> mstring;
  1135. for (List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1136. if (E->get().name.begins_with("_"))
  1137. continue;
  1138. mstring.push_back(E->get().name.get_slice(":",0));
  1139. }
  1140. mstring.sort();
  1141. String ml;
  1142. for (List<String>::Element *E=mstring.front();E;E=E->next()) {
  1143. if (ml!=String())
  1144. ml+=",";
  1145. ml+=E->get();
  1146. }
  1147. property.hint_string=ml;
  1148. }
  1149. }
  1150. void VisualScriptScriptCall::_bind_methods() {
  1151. ObjectTypeDB::bind_method(_MD("set_function","function"),&VisualScriptScriptCall::set_function);
  1152. ObjectTypeDB::bind_method(_MD("get_function"),&VisualScriptScriptCall::get_function);
  1153. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptScriptCall::set_call_mode);
  1154. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptScriptCall::get_call_mode);
  1155. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptScriptCall::set_base_path);
  1156. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptScriptCall::get_base_path);
  1157. ADD_PROPERTY(PropertyInfo(Variant::INT,"function/call_mode",PROPERTY_HINT_ENUM,"Self,Node Path"),_SCS("set_call_mode"),_SCS("get_call_mode"));
  1158. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"function/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  1159. ADD_PROPERTY(PropertyInfo(Variant::STRING,"function/function"),_SCS("set_function"),_SCS("get_function"));
  1160. BIND_CONSTANT( CALL_MODE_SELF );
  1161. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  1162. }
  1163. VisualScriptNodeInstance* VisualScriptScriptCall::instance(VScriptInstance* p_instance) {
  1164. return NULL;
  1165. }
  1166. VisualScriptScriptCall::VisualScriptScriptCall() {
  1167. call_mode=CALL_MODE_SELF;
  1168. }
  1169. template<VisualScriptScriptCall::CallMode cmode>
  1170. static Ref<VisualScriptNode> create_script_call_node(const String& p_name) {
  1171. Ref<VisualScriptScriptCall> node;
  1172. node.instance();
  1173. node->set_call_mode(cmode);
  1174. return node;
  1175. }
  1176. //////////////////////////////////////////
  1177. ////////////////SCRIPT CALL//////////////////////
  1178. //////////////////////////////////////////
  1179. int VisualScriptEmitSignal::get_output_sequence_port_count() const {
  1180. return 1;
  1181. }
  1182. bool VisualScriptEmitSignal::has_input_sequence_port() const{
  1183. return true;
  1184. }
  1185. int VisualScriptEmitSignal::get_input_value_port_count() const{
  1186. Ref<VisualScript> vs = get_visual_script();
  1187. if (vs.is_valid()) {
  1188. if (!vs->has_custom_signal(name))
  1189. return 0;
  1190. return vs->custom_signal_get_argument_count(name);
  1191. }
  1192. return 0;
  1193. }
  1194. int VisualScriptEmitSignal::get_output_value_port_count() const{
  1195. return 0;
  1196. }
  1197. String VisualScriptEmitSignal::get_output_sequence_port_text(int p_port) const {
  1198. return String();
  1199. }
  1200. PropertyInfo VisualScriptEmitSignal::get_input_value_port_info(int p_idx) const{
  1201. Ref<VisualScript> vs = get_visual_script();
  1202. if (vs.is_valid()) {
  1203. if (!vs->has_custom_signal(name))
  1204. return PropertyInfo();
  1205. return PropertyInfo(vs->custom_signal_get_argument_type(name,p_idx),vs->custom_signal_get_argument_name(name,p_idx));
  1206. }
  1207. return PropertyInfo();
  1208. }
  1209. PropertyInfo VisualScriptEmitSignal::get_output_value_port_info(int p_idx) const{
  1210. return PropertyInfo();
  1211. }
  1212. String VisualScriptEmitSignal::get_caption() const {
  1213. return "EmitSignal";
  1214. }
  1215. String VisualScriptEmitSignal::get_text() const {
  1216. return "emit "+String(name);
  1217. }
  1218. void VisualScriptEmitSignal::set_signal(const StringName& p_type){
  1219. if (name==p_type)
  1220. return;
  1221. name=p_type;
  1222. _change_notify();
  1223. ports_changed_notify();
  1224. }
  1225. StringName VisualScriptEmitSignal::get_signal() const {
  1226. return name;
  1227. }
  1228. void VisualScriptEmitSignal::_validate_property(PropertyInfo& property) const {
  1229. if (property.name=="signal/signal") {
  1230. property.hint=PROPERTY_HINT_ENUM;
  1231. List<StringName> sigs;
  1232. Ref<VisualScript> vs = get_visual_script();
  1233. if (vs.is_valid()) {
  1234. vs->get_custom_signal_list(&sigs);
  1235. }
  1236. String ml;
  1237. for (List<StringName>::Element *E=sigs.front();E;E=E->next()) {
  1238. if (ml!=String())
  1239. ml+=",";
  1240. ml+=E->get();
  1241. }
  1242. property.hint_string=ml;
  1243. }
  1244. }
  1245. void VisualScriptEmitSignal::_bind_methods() {
  1246. ObjectTypeDB::bind_method(_MD("set_signal","name"),&VisualScriptEmitSignal::set_signal);
  1247. ObjectTypeDB::bind_method(_MD("get_signal"),&VisualScriptEmitSignal::get_signal);
  1248. ADD_PROPERTY(PropertyInfo(Variant::STRING,"signal/signal"),_SCS("set_signal"),_SCS("get_signal"));
  1249. }
  1250. VisualScriptNodeInstance* VisualScriptEmitSignal::instance(VScriptInstance* p_instance) {
  1251. return NULL;
  1252. }
  1253. VisualScriptEmitSignal::VisualScriptEmitSignal() {
  1254. }
  1255. void register_visual_script_func_nodes() {
  1256. VisualScriptLanguage::singleton->add_register_func("functions/call_method/instance_call",create_function_call_node<VisualScriptFunctionCall::CALL_MODE_INSTANCE>);
  1257. VisualScriptLanguage::singleton->add_register_func("functions/call_method/basic_type_call",create_function_call_node<VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE>);
  1258. VisualScriptLanguage::singleton->add_register_func("functions/call_method/self_call",create_function_call_node<VisualScriptFunctionCall::CALL_MODE_SELF>);
  1259. VisualScriptLanguage::singleton->add_register_func("functions/call_method/node_call",create_function_call_node<VisualScriptFunctionCall::CALL_MODE_NODE_PATH>);
  1260. VisualScriptLanguage::singleton->add_register_func("functions/set_property/instace_set",create_property_set_node<VisualScriptPropertySet::CALL_MODE_INSTANCE>);
  1261. VisualScriptLanguage::singleton->add_register_func("functions/set_property/basic_type_set",create_property_set_node<VisualScriptPropertySet::CALL_MODE_BASIC_TYPE>);
  1262. VisualScriptLanguage::singleton->add_register_func("functions/set_property/self_set",create_property_set_node<VisualScriptPropertySet::CALL_MODE_SELF>);
  1263. VisualScriptLanguage::singleton->add_register_func("functions/set_property/node_set",create_property_set_node<VisualScriptPropertySet::CALL_MODE_NODE_PATH>);
  1264. VisualScriptLanguage::singleton->add_register_func("functions/get_property/instance_get",create_property_get_node<VisualScriptPropertyGet::CALL_MODE_INSTANCE>);
  1265. VisualScriptLanguage::singleton->add_register_func("functions/get_property/basic_type_get",create_property_get_node<VisualScriptPropertyGet::CALL_MODE_BASIC_TYPE>);
  1266. VisualScriptLanguage::singleton->add_register_func("functions/get_property/self_get",create_property_get_node<VisualScriptPropertyGet::CALL_MODE_SELF>);
  1267. VisualScriptLanguage::singleton->add_register_func("functions/get_property/node_get",create_property_get_node<VisualScriptPropertyGet::CALL_MODE_NODE_PATH>);
  1268. VisualScriptLanguage::singleton->add_register_func("functions/script/script_call",create_script_call_node<VisualScriptScriptCall::CALL_MODE_SELF>);
  1269. VisualScriptLanguage::singleton->add_register_func("functions/script/script_call_in_node",create_script_call_node<VisualScriptScriptCall::CALL_MODE_NODE_PATH>);
  1270. VisualScriptLanguage::singleton->add_register_func("functions/script/emit_signal",create_node_generic<VisualScriptEmitSignal>);
  1271. }