visual_script_func_nodes.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511
  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. class VisualScriptNodeInstanceFunctionCall : public VisualScriptNodeInstance {
  338. public:
  339. VisualScriptFunctionCall::CallMode call_mode;
  340. NodePath node_path;
  341. int input_args;
  342. bool returns;
  343. StringName function;
  344. VisualScriptFunctionCall *node;
  345. VisualScriptInstance *instance;
  346. //virtual int get_working_memory_size() const { return 0; }
  347. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  348. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  349. virtual int step(const Variant** p_inputs,Variant** p_outputs,StartMode p_start_mode,Variant* p_working_mem,Variant::CallError& r_error,String& r_error_str) {
  350. switch(call_mode) {
  351. case VisualScriptFunctionCall::CALL_MODE_SELF: {
  352. Object *object=instance->get_owner_ptr();
  353. if (returns) {
  354. *p_outputs[0] = object->call(function,p_inputs,input_args,r_error);
  355. } else {
  356. object->call(function,p_inputs,input_args,r_error);
  357. }
  358. } break;
  359. case VisualScriptFunctionCall::CALL_MODE_NODE_PATH: {
  360. Node* node = instance->get_owner_ptr()->cast_to<Node>();
  361. if (!node) {
  362. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  363. r_error_str="Base object is not a Node!";
  364. return 0;
  365. }
  366. Node* another = node->get_node(node_path);
  367. if (!node) {
  368. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  369. r_error_str="Path does not lead Node!";
  370. return 0;
  371. }
  372. if (returns) {
  373. *p_outputs[0] = another->call(function,p_inputs,input_args,r_error);
  374. } else {
  375. another->call(function,p_inputs,input_args,r_error);
  376. }
  377. } break;
  378. case VisualScriptFunctionCall::CALL_MODE_INSTANCE:
  379. case VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE: {
  380. Variant v = *p_inputs[0];
  381. if (returns) {
  382. *p_outputs[0] = v.call(function,p_inputs+1,input_args,r_error);
  383. } else {
  384. v.call(function,p_inputs+1,input_args,r_error);
  385. }
  386. } break;
  387. }
  388. return 0;
  389. }
  390. };
  391. VisualScriptNodeInstance* VisualScriptFunctionCall::instance(VisualScriptInstance* p_instance) {
  392. VisualScriptNodeInstanceFunctionCall * instance = memnew(VisualScriptNodeInstanceFunctionCall );
  393. instance->node=this;
  394. instance->instance=p_instance;
  395. instance->function=function;
  396. instance->call_mode=call_mode;
  397. instance->returns=get_output_value_port_count();
  398. instance->node_path=base_path;
  399. instance->input_args = get_input_value_port_count() - ( (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE) ? 1: 0 );
  400. return instance;
  401. }
  402. VisualScriptFunctionCall::VisualScriptFunctionCall() {
  403. call_mode=CALL_MODE_INSTANCE;
  404. basic_type=Variant::NIL;
  405. use_default_args=0;
  406. base_type="Object";
  407. }
  408. template<VisualScriptFunctionCall::CallMode cmode>
  409. static Ref<VisualScriptNode> create_function_call_node(const String& p_name) {
  410. Ref<VisualScriptFunctionCall> node;
  411. node.instance();
  412. node->set_call_mode(cmode);
  413. return node;
  414. }
  415. //////////////////////////////////////////
  416. ////////////////SET//////////////////////
  417. //////////////////////////////////////////
  418. static const char* event_type_names[InputEvent::TYPE_MAX]={
  419. "None",
  420. "Key",
  421. "MouseMotion",
  422. "MouseButton",
  423. "JoystickMotion",
  424. "JoystickButton",
  425. "ScreenTouch",
  426. "ScreenDrag",
  427. "Action"
  428. };
  429. int VisualScriptPropertySet::get_output_sequence_port_count() const {
  430. return 1;
  431. }
  432. bool VisualScriptPropertySet::has_input_sequence_port() const{
  433. return true;
  434. }
  435. Node *VisualScriptPropertySet::_get_base_node() const {
  436. #ifdef TOOLS_ENABLED
  437. Ref<Script> script = get_visual_script();
  438. if (!script.is_valid())
  439. return NULL;
  440. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  441. if (!main_loop)
  442. return NULL;
  443. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  444. if (!scene_tree)
  445. return NULL;
  446. Node *edited_scene = scene_tree->get_edited_scene_root();
  447. if (!edited_scene)
  448. return NULL;
  449. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  450. if (!script_node)
  451. return NULL;
  452. if (!script_node->has_node(base_path))
  453. return NULL;
  454. Node *path_to = script_node->get_node(base_path);
  455. return path_to;
  456. #else
  457. return NULL;
  458. #endif
  459. }
  460. StringName VisualScriptPropertySet::_get_base_type() const {
  461. if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid())
  462. return get_visual_script()->get_instance_base_type();
  463. else if (call_mode==CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  464. Node *path = _get_base_node();
  465. if (path)
  466. return path->get_type();
  467. }
  468. return base_type;
  469. }
  470. int VisualScriptPropertySet::get_input_value_port_count() const{
  471. int pc = (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE)?1:0;
  472. if (!use_builtin_value)
  473. pc++;
  474. return pc;
  475. }
  476. int VisualScriptPropertySet::get_output_value_port_count() const{
  477. return call_mode==CALL_MODE_BASIC_TYPE? 1 : 0;
  478. }
  479. String VisualScriptPropertySet::get_output_sequence_port_text(int p_port) const {
  480. return String();
  481. }
  482. PropertyInfo VisualScriptPropertySet::get_input_value_port_info(int p_idx) const{
  483. if (call_mode==CALL_MODE_INSTANCE || call_mode==CALL_MODE_BASIC_TYPE) {
  484. if (p_idx==0) {
  485. PropertyInfo pi;
  486. pi.type=(call_mode==CALL_MODE_INSTANCE?Variant::OBJECT:basic_type);
  487. pi.name=(call_mode==CALL_MODE_INSTANCE?String("instance"):Variant::get_type_name(basic_type).to_lower());
  488. return pi;
  489. } else {
  490. p_idx--;
  491. }
  492. }
  493. #ifdef DEBUG_METHODS_ENABLED
  494. //not very efficient but..
  495. List<PropertyInfo> pinfo;
  496. if (call_mode==CALL_MODE_BASIC_TYPE) {
  497. Variant v;
  498. if (basic_type==Variant::INPUT_EVENT) {
  499. InputEvent ev;
  500. ev.type=event_type;
  501. v=ev;
  502. } else {
  503. Variant::CallError ce;
  504. v = Variant::construct(basic_type,NULL,0,ce);
  505. }
  506. v.get_property_list(&pinfo);
  507. } else if (call_mode==CALL_MODE_NODE_PATH) {
  508. Node *n = _get_base_node();
  509. if (n) {
  510. n->get_property_list(&pinfo);
  511. } else {
  512. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  513. }
  514. } else {
  515. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  516. }
  517. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  518. if (E->get().name==property) {
  519. PropertyInfo info=E->get();
  520. info.name="value";
  521. return info;
  522. }
  523. }
  524. #endif
  525. return PropertyInfo(Variant::NIL,"value");
  526. }
  527. PropertyInfo VisualScriptPropertySet::get_output_value_port_info(int p_idx) const{
  528. if (call_mode==CALL_MODE_BASIC_TYPE) {
  529. return PropertyInfo(basic_type,"out");
  530. } else {
  531. return PropertyInfo();
  532. }
  533. }
  534. String VisualScriptPropertySet::get_caption() const {
  535. static const char*cname[4]= {
  536. "SelfSet",
  537. "NodeSet",
  538. "InstanceSet",
  539. "BasicSet"
  540. };
  541. return cname[call_mode];
  542. }
  543. String VisualScriptPropertySet::get_text() const {
  544. String prop;
  545. if (call_mode==CALL_MODE_BASIC_TYPE)
  546. prop=Variant::get_type_name(basic_type)+"."+property;
  547. else
  548. prop=property;
  549. if (use_builtin_value) {
  550. String bit = builtin_value.get_construct_string();
  551. if (bit.length()>40) {
  552. bit=bit.substr(0,40);
  553. bit+="...";
  554. }
  555. prop+="\n "+bit;
  556. }
  557. return prop;
  558. }
  559. void VisualScriptPropertySet::_update_base_type() {
  560. //cache it because this information may not be available on load
  561. if (call_mode==CALL_MODE_NODE_PATH) {
  562. Node* node=_get_base_node();
  563. if (node) {
  564. base_type=node->get_type();
  565. }
  566. } else if (call_mode==CALL_MODE_SELF) {
  567. if (get_visual_script().is_valid()) {
  568. base_type=get_visual_script()->get_instance_base_type();
  569. }
  570. }
  571. }
  572. void VisualScriptPropertySet::set_basic_type(Variant::Type p_type) {
  573. if (basic_type==p_type)
  574. return;
  575. basic_type=p_type;
  576. _change_notify();
  577. _update_base_type();
  578. ports_changed_notify();
  579. }
  580. Variant::Type VisualScriptPropertySet::get_basic_type() const{
  581. return basic_type;
  582. }
  583. void VisualScriptPropertySet::set_event_type(InputEvent::Type p_type) {
  584. if (event_type==p_type)
  585. return;
  586. event_type=p_type;
  587. _change_notify();
  588. _update_base_type();
  589. ports_changed_notify();
  590. }
  591. InputEvent::Type VisualScriptPropertySet::get_event_type() const{
  592. return event_type;
  593. }
  594. void VisualScriptPropertySet::set_base_type(const StringName& p_type) {
  595. if (base_type==p_type)
  596. return;
  597. base_type=p_type;
  598. _change_notify();
  599. ports_changed_notify();
  600. }
  601. StringName VisualScriptPropertySet::get_base_type() const{
  602. return base_type;
  603. }
  604. void VisualScriptPropertySet::set_property(const StringName& p_type){
  605. if (property==p_type)
  606. return;
  607. property=p_type;
  608. _change_notify();
  609. ports_changed_notify();
  610. }
  611. StringName VisualScriptPropertySet::get_property() const {
  612. return property;
  613. }
  614. void VisualScriptPropertySet::set_base_path(const NodePath& p_type) {
  615. if (base_path==p_type)
  616. return;
  617. base_path=p_type;
  618. _update_base_type();
  619. _change_notify();
  620. ports_changed_notify();
  621. }
  622. NodePath VisualScriptPropertySet::get_base_path() const {
  623. return base_path;
  624. }
  625. void VisualScriptPropertySet::set_call_mode(CallMode p_mode) {
  626. if (call_mode==p_mode)
  627. return;
  628. call_mode=p_mode;
  629. _update_base_type();
  630. _change_notify();
  631. ports_changed_notify();
  632. }
  633. VisualScriptPropertySet::CallMode VisualScriptPropertySet::get_call_mode() const {
  634. return call_mode;
  635. }
  636. void VisualScriptPropertySet::set_use_builtin_value(bool p_use) {
  637. if (use_builtin_value==p_use)
  638. return;
  639. use_builtin_value=p_use;
  640. _change_notify();
  641. ports_changed_notify();
  642. }
  643. bool VisualScriptPropertySet::is_using_builtin_value() const{
  644. return use_builtin_value;
  645. }
  646. void VisualScriptPropertySet::set_builtin_value(const Variant& p_value){
  647. if (builtin_value==p_value)
  648. return;
  649. builtin_value=p_value;
  650. }
  651. Variant VisualScriptPropertySet::get_builtin_value() const{
  652. return builtin_value;
  653. }
  654. void VisualScriptPropertySet::_validate_property(PropertyInfo& property) const {
  655. if (property.name=="property/base_type") {
  656. if (call_mode!=CALL_MODE_INSTANCE) {
  657. property.usage=PROPERTY_USAGE_NOEDITOR;
  658. }
  659. }
  660. if (property.name=="property/basic_type") {
  661. if (call_mode!=CALL_MODE_BASIC_TYPE) {
  662. property.usage=0;
  663. }
  664. }
  665. if (property.name=="property/event_type") {
  666. if (call_mode!=CALL_MODE_BASIC_TYPE || basic_type!=Variant::INPUT_EVENT) {
  667. property.usage=0;
  668. }
  669. }
  670. if (property.name=="property/node_path") {
  671. if (call_mode!=CALL_MODE_NODE_PATH) {
  672. property.usage=0;
  673. } else {
  674. Node *bnode = _get_base_node();
  675. if (bnode) {
  676. property.hint_string=bnode->get_path(); //convert to loong string
  677. } else {
  678. }
  679. }
  680. }
  681. if (property.name=="property/property") {
  682. property.hint=PROPERTY_HINT_ENUM;
  683. List<PropertyInfo> pinfo;
  684. if (call_mode==CALL_MODE_BASIC_TYPE) {
  685. Variant::CallError ce;
  686. Variant v;
  687. if (basic_type==Variant::INPUT_EVENT) {
  688. InputEvent ev;
  689. ev.type=event_type;
  690. v=ev;
  691. } else {
  692. v = Variant::construct(basic_type,NULL,0,ce);
  693. }
  694. v.get_property_list(&pinfo);
  695. } else if (call_mode==CALL_MODE_NODE_PATH) {
  696. Node *n = _get_base_node();
  697. if (n) {
  698. n->get_property_list(&pinfo);
  699. } else {
  700. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  701. }
  702. } else {
  703. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  704. }
  705. List<String> mstring;
  706. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  707. if (E->get().usage&PROPERTY_USAGE_EDITOR) {
  708. mstring.push_back(E->get().name);
  709. }
  710. }
  711. String ml;
  712. for (List<String>::Element *E=mstring.front();E;E=E->next()) {
  713. if (ml!=String())
  714. ml+=",";
  715. ml+=E->get();
  716. }
  717. if (ml==String()) {
  718. property.usage=PROPERTY_USAGE_NOEDITOR; //do not show for editing if empty
  719. } else {
  720. property.hint_string=ml;
  721. }
  722. }
  723. if (property.name=="value/builtin") {
  724. if (!use_builtin_value) {
  725. property.usage=0;
  726. } else {
  727. List<PropertyInfo> pinfo;
  728. if (call_mode==CALL_MODE_BASIC_TYPE) {
  729. Variant::CallError ce;
  730. Variant v = Variant::construct(basic_type,NULL,0,ce);
  731. v.get_property_list(&pinfo);
  732. } else if (call_mode==CALL_MODE_NODE_PATH) {
  733. Node *n = _get_base_node();
  734. if (n) {
  735. n->get_property_list(&pinfo);
  736. } else {
  737. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  738. }
  739. } else {
  740. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  741. }
  742. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  743. if (E->get().name==this->property) {
  744. property.hint=E->get().hint;
  745. property.type=E->get().type;
  746. property.hint_string=E->get().hint_string;
  747. }
  748. }
  749. }
  750. }
  751. }
  752. void VisualScriptPropertySet::_bind_methods() {
  753. ObjectTypeDB::bind_method(_MD("set_base_type","base_type"),&VisualScriptPropertySet::set_base_type);
  754. ObjectTypeDB::bind_method(_MD("get_base_type"),&VisualScriptPropertySet::get_base_type);
  755. ObjectTypeDB::bind_method(_MD("set_basic_type","basic_type"),&VisualScriptPropertySet::set_basic_type);
  756. ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptPropertySet::get_basic_type);
  757. ObjectTypeDB::bind_method(_MD("set_event_type","event_type"),&VisualScriptPropertySet::set_event_type);
  758. ObjectTypeDB::bind_method(_MD("get_event_type"),&VisualScriptPropertySet::get_event_type);
  759. ObjectTypeDB::bind_method(_MD("set_property","property"),&VisualScriptPropertySet::set_property);
  760. ObjectTypeDB::bind_method(_MD("get_property"),&VisualScriptPropertySet::get_property);
  761. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptPropertySet::set_call_mode);
  762. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptPropertySet::get_call_mode);
  763. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptPropertySet::set_base_path);
  764. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptPropertySet::get_base_path);
  765. ObjectTypeDB::bind_method(_MD("set_builtin_value","value"),&VisualScriptPropertySet::set_builtin_value);
  766. ObjectTypeDB::bind_method(_MD("get_builtin_value"),&VisualScriptPropertySet::get_builtin_value);
  767. ObjectTypeDB::bind_method(_MD("set_use_builtin_value","enable"),&VisualScriptPropertySet::set_use_builtin_value);
  768. ObjectTypeDB::bind_method(_MD("is_using_builtin_value"),&VisualScriptPropertySet::is_using_builtin_value);
  769. String bt;
  770. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  771. if (i>0)
  772. bt+=",";
  773. bt+=Variant::get_type_name(Variant::Type(i));
  774. }
  775. String et;
  776. for(int i=0;i<InputEvent::TYPE_MAX;i++) {
  777. if (i>0)
  778. et+=",";
  779. et+=event_type_names[i];
  780. }
  781. 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"));
  782. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_type",PROPERTY_HINT_TYPE_STRING,"Object"),_SCS("set_base_type"),_SCS("get_base_type"));
  783. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/basic_type",PROPERTY_HINT_ENUM,bt),_SCS("set_basic_type"),_SCS("get_basic_type"));
  784. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/event_type",PROPERTY_HINT_ENUM,et),_SCS("set_event_type"),_SCS("get_event_type"));
  785. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"property/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  786. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/property"),_SCS("set_property"),_SCS("get_property"));
  787. ADD_PROPERTY(PropertyInfo(Variant::BOOL,"value/use_builtin"),_SCS("set_use_builtin_value"),_SCS("is_using_builtin_value"));
  788. ADD_PROPERTY(PropertyInfo(Variant::NIL,"value/builtin"),_SCS("set_builtin_value"),_SCS("get_builtin_value"));
  789. BIND_CONSTANT( CALL_MODE_SELF );
  790. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  791. BIND_CONSTANT( CALL_MODE_INSTANCE);
  792. }
  793. class VisualScriptNodeInstancePropertySet : public VisualScriptNodeInstance {
  794. public:
  795. VisualScriptPropertySet::CallMode call_mode;
  796. NodePath node_path;
  797. StringName property;
  798. bool use_builtin;
  799. Variant builtin_val;
  800. VisualScriptPropertySet *node;
  801. VisualScriptInstance *instance;
  802. //virtual int get_working_memory_size() const { return 0; }
  803. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  804. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  805. virtual int step(const Variant** p_inputs,Variant** p_outputs,StartMode p_start_mode,Variant* p_working_mem,Variant::CallError& r_error,String& r_error_str) {
  806. switch(call_mode) {
  807. case VisualScriptPropertySet::CALL_MODE_SELF: {
  808. Object *object=instance->get_owner_ptr();
  809. bool valid;
  810. if (use_builtin) {
  811. object->set(property,builtin_val,&valid);
  812. } else {
  813. object->set(property,*p_inputs[0],&valid);
  814. }
  815. if (!valid) {
  816. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  817. r_error_str="Invalid index property name.";
  818. }
  819. } break;
  820. case VisualScriptPropertySet::CALL_MODE_NODE_PATH: {
  821. Node* node = instance->get_owner_ptr()->cast_to<Node>();
  822. if (!node) {
  823. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  824. r_error_str="Base object is not a Node!";
  825. return 0;
  826. }
  827. Node* another = node->get_node(node_path);
  828. if (!node) {
  829. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  830. r_error_str="Path does not lead Node!";
  831. return 0;
  832. }
  833. bool valid;
  834. if (use_builtin) {
  835. another->set(property,builtin_val,&valid);
  836. } else {
  837. another->set(property,*p_inputs[0],&valid);
  838. }
  839. if (!valid) {
  840. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  841. r_error_str="Invalid index property name.";
  842. }
  843. } break;
  844. case VisualScriptPropertySet::CALL_MODE_INSTANCE:
  845. case VisualScriptPropertySet::CALL_MODE_BASIC_TYPE: {
  846. Variant v = *p_inputs[0];
  847. bool valid;
  848. if (use_builtin) {
  849. v.set(property,builtin_val,&valid);
  850. } else {
  851. v.set(property,p_inputs[1],&valid);
  852. }
  853. if (!valid) {
  854. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  855. r_error_str="Invalid index property name.";
  856. }
  857. if (call_mode==VisualScriptPropertySet::CALL_MODE_BASIC_TYPE) {
  858. *p_outputs[0]=v;
  859. }
  860. } break;
  861. }
  862. return 0;
  863. }
  864. };
  865. VisualScriptNodeInstance* VisualScriptPropertySet::instance(VisualScriptInstance* p_instance) {
  866. VisualScriptNodeInstancePropertySet * instance = memnew(VisualScriptNodeInstancePropertySet );
  867. instance->node=this;
  868. instance->instance=p_instance;
  869. instance->property=property;
  870. instance->call_mode=call_mode;
  871. instance->node_path=base_path;
  872. instance->use_builtin=use_builtin_value;
  873. instance->builtin_val=builtin_value;
  874. return instance;
  875. }
  876. VisualScriptPropertySet::VisualScriptPropertySet() {
  877. call_mode=CALL_MODE_INSTANCE;
  878. base_type="Object";
  879. basic_type=Variant::NIL;
  880. event_type=InputEvent::NONE;
  881. }
  882. template<VisualScriptPropertySet::CallMode cmode>
  883. static Ref<VisualScriptNode> create_property_set_node(const String& p_name) {
  884. Ref<VisualScriptPropertySet> node;
  885. node.instance();
  886. node->set_call_mode(cmode);
  887. return node;
  888. }
  889. //////////////////////////////////////////
  890. ////////////////GET//////////////////////
  891. //////////////////////////////////////////
  892. int VisualScriptPropertyGet::get_output_sequence_port_count() const {
  893. return (call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?0:1;
  894. }
  895. bool VisualScriptPropertyGet::has_input_sequence_port() const{
  896. return (call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?false:true;
  897. }
  898. void VisualScriptPropertyGet::_update_base_type() {
  899. //cache it because this information may not be available on load
  900. if (call_mode==CALL_MODE_NODE_PATH) {
  901. Node* node=_get_base_node();
  902. if (node) {
  903. base_type=node->get_type();
  904. }
  905. } else if (call_mode==CALL_MODE_SELF) {
  906. if (get_visual_script().is_valid()) {
  907. base_type=get_visual_script()->get_instance_base_type();
  908. }
  909. }
  910. }
  911. Node *VisualScriptPropertyGet::_get_base_node() const {
  912. #ifdef TOOLS_ENABLED
  913. Ref<Script> script = get_visual_script();
  914. if (!script.is_valid())
  915. return NULL;
  916. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  917. if (!main_loop)
  918. return NULL;
  919. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  920. if (!scene_tree)
  921. return NULL;
  922. Node *edited_scene = scene_tree->get_edited_scene_root();
  923. if (!edited_scene)
  924. return NULL;
  925. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  926. if (!script_node)
  927. return NULL;
  928. if (!script_node->has_node(base_path))
  929. return NULL;
  930. Node *path_to = script_node->get_node(base_path);
  931. return path_to;
  932. #else
  933. return NULL;
  934. #endif
  935. }
  936. StringName VisualScriptPropertyGet::_get_base_type() const {
  937. if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid())
  938. return get_visual_script()->get_instance_base_type();
  939. else if (call_mode==CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  940. Node *path = _get_base_node();
  941. if (path)
  942. return path->get_type();
  943. }
  944. return base_type;
  945. }
  946. int VisualScriptPropertyGet::get_input_value_port_count() const{
  947. return (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE)?1:0;
  948. }
  949. int VisualScriptPropertyGet::get_output_value_port_count() const{
  950. return 1;
  951. }
  952. String VisualScriptPropertyGet::get_output_sequence_port_text(int p_port) const {
  953. return String();
  954. }
  955. PropertyInfo VisualScriptPropertyGet::get_input_value_port_info(int p_idx) const{
  956. if (call_mode==CALL_MODE_INSTANCE || call_mode==CALL_MODE_BASIC_TYPE) {
  957. if (p_idx==0) {
  958. PropertyInfo pi;
  959. pi.type=(call_mode==CALL_MODE_INSTANCE?Variant::OBJECT:basic_type);
  960. pi.name=(call_mode==CALL_MODE_INSTANCE?String("instance"):Variant::get_type_name(basic_type).to_lower());
  961. return pi;
  962. } else {
  963. p_idx--;
  964. }
  965. }
  966. return PropertyInfo();
  967. }
  968. PropertyInfo VisualScriptPropertyGet::get_output_value_port_info(int p_idx) const{
  969. #ifdef DEBUG_METHODS_ENABLED
  970. //not very efficient but..
  971. List<PropertyInfo> pinfo;
  972. if (call_mode==CALL_MODE_BASIC_TYPE) {
  973. Variant v;
  974. if (basic_type==Variant::INPUT_EVENT) {
  975. InputEvent ev;
  976. ev.type=event_type;
  977. v=ev;
  978. } else {
  979. Variant::CallError ce;
  980. v = Variant::construct(basic_type,NULL,0,ce);
  981. }
  982. v.get_property_list(&pinfo);
  983. } else if (call_mode==CALL_MODE_NODE_PATH) {
  984. Node *n = _get_base_node();
  985. if (n) {
  986. n->get_property_list(&pinfo);
  987. } else {
  988. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  989. }
  990. } else {
  991. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  992. }
  993. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  994. if (E->get().name==property) {
  995. PropertyInfo info=E->get();
  996. info.name="";
  997. return info;
  998. }
  999. }
  1000. #endif
  1001. return PropertyInfo(Variant::NIL,"");
  1002. }
  1003. String VisualScriptPropertyGet::get_caption() const {
  1004. static const char*cname[4]= {
  1005. "SelfGet",
  1006. "NodeGet",
  1007. "InstanceGet",
  1008. "BasicGet"
  1009. };
  1010. return cname[call_mode];
  1011. }
  1012. String VisualScriptPropertyGet::get_text() const {
  1013. if (call_mode==CALL_MODE_BASIC_TYPE)
  1014. return Variant::get_type_name(basic_type)+"."+property;
  1015. else
  1016. return property;
  1017. }
  1018. void VisualScriptPropertyGet::set_base_type(const StringName& p_type) {
  1019. if (base_type==p_type)
  1020. return;
  1021. base_type=p_type;
  1022. _change_notify();
  1023. ports_changed_notify();
  1024. }
  1025. StringName VisualScriptPropertyGet::get_base_type() const{
  1026. return base_type;
  1027. }
  1028. void VisualScriptPropertyGet::set_property(const StringName& p_type){
  1029. if (property==p_type)
  1030. return;
  1031. property=p_type;
  1032. _change_notify();
  1033. ports_changed_notify();
  1034. }
  1035. StringName VisualScriptPropertyGet::get_property() const {
  1036. return property;
  1037. }
  1038. void VisualScriptPropertyGet::set_base_path(const NodePath& p_type) {
  1039. if (base_path==p_type)
  1040. return;
  1041. base_path=p_type;
  1042. _change_notify();
  1043. _update_base_type();
  1044. ports_changed_notify();
  1045. }
  1046. NodePath VisualScriptPropertyGet::get_base_path() const {
  1047. return base_path;
  1048. }
  1049. void VisualScriptPropertyGet::set_call_mode(CallMode p_mode) {
  1050. if (call_mode==p_mode)
  1051. return;
  1052. call_mode=p_mode;
  1053. _change_notify();
  1054. _update_base_type();
  1055. ports_changed_notify();
  1056. }
  1057. VisualScriptPropertyGet::CallMode VisualScriptPropertyGet::get_call_mode() const {
  1058. return call_mode;
  1059. }
  1060. void VisualScriptPropertyGet::set_basic_type(Variant::Type p_type) {
  1061. if (basic_type==p_type)
  1062. return;
  1063. basic_type=p_type;
  1064. _change_notify();
  1065. ports_changed_notify();
  1066. }
  1067. Variant::Type VisualScriptPropertyGet::get_basic_type() const{
  1068. return basic_type;
  1069. }
  1070. void VisualScriptPropertyGet::set_event_type(InputEvent::Type p_type) {
  1071. if (event_type==p_type)
  1072. return;
  1073. event_type=p_type;
  1074. _change_notify();
  1075. _update_base_type();
  1076. ports_changed_notify();
  1077. }
  1078. InputEvent::Type VisualScriptPropertyGet::get_event_type() const{
  1079. return event_type;
  1080. }
  1081. void VisualScriptPropertyGet::_validate_property(PropertyInfo& property) const {
  1082. if (property.name=="property/base_type") {
  1083. if (call_mode!=CALL_MODE_INSTANCE) {
  1084. property.usage=PROPERTY_USAGE_NOEDITOR;
  1085. }
  1086. }
  1087. if (property.name=="property/basic_type") {
  1088. if (call_mode!=CALL_MODE_BASIC_TYPE) {
  1089. property.usage=0;
  1090. }
  1091. }
  1092. if (property.name=="property/event_type") {
  1093. if (call_mode!=CALL_MODE_BASIC_TYPE || basic_type!=Variant::INPUT_EVENT) {
  1094. property.usage=0;
  1095. }
  1096. }
  1097. if (property.name=="property/node_path") {
  1098. if (call_mode!=CALL_MODE_NODE_PATH) {
  1099. property.usage=0;
  1100. } else {
  1101. Node *bnode = _get_base_node();
  1102. if (bnode) {
  1103. property.hint_string=bnode->get_path(); //convert to loong string
  1104. } else {
  1105. }
  1106. }
  1107. }
  1108. if (property.name=="property/property") {
  1109. property.hint=PROPERTY_HINT_ENUM;
  1110. List<PropertyInfo> pinfo;
  1111. if (call_mode==CALL_MODE_BASIC_TYPE) {
  1112. Variant::CallError ce;
  1113. Variant v;
  1114. if (basic_type==Variant::INPUT_EVENT) {
  1115. InputEvent ev;
  1116. ev.type=event_type;
  1117. v=ev;
  1118. } else {
  1119. v = Variant::construct(basic_type,NULL,0,ce);
  1120. }
  1121. v.get_property_list(&pinfo);
  1122. } else if (call_mode==CALL_MODE_NODE_PATH) {
  1123. Node *n = _get_base_node();
  1124. if (n) {
  1125. n->get_property_list(&pinfo);
  1126. } else {
  1127. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  1128. }
  1129. } else {
  1130. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  1131. }
  1132. List<String> mstring;
  1133. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  1134. if (E->get().usage&PROPERTY_USAGE_EDITOR)
  1135. mstring.push_back(E->get().name);
  1136. }
  1137. String ml;
  1138. for (List<String>::Element *E=mstring.front();E;E=E->next()) {
  1139. if (ml!=String())
  1140. ml+=",";
  1141. ml+=E->get();
  1142. }
  1143. if (ml==String()) {
  1144. property.usage=PROPERTY_USAGE_NOEDITOR; //do not show for editing if empty
  1145. } else {
  1146. property.hint_string=ml;
  1147. }
  1148. }
  1149. }
  1150. void VisualScriptPropertyGet::_bind_methods() {
  1151. ObjectTypeDB::bind_method(_MD("set_base_type","base_type"),&VisualScriptPropertyGet::set_base_type);
  1152. ObjectTypeDB::bind_method(_MD("get_base_type"),&VisualScriptPropertyGet::get_base_type);
  1153. ObjectTypeDB::bind_method(_MD("set_basic_type","basic_type"),&VisualScriptPropertyGet::set_basic_type);
  1154. ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptPropertyGet::get_basic_type);
  1155. ObjectTypeDB::bind_method(_MD("set_event_type","event_type"),&VisualScriptPropertyGet::set_event_type);
  1156. ObjectTypeDB::bind_method(_MD("get_event_type"),&VisualScriptPropertyGet::get_event_type);
  1157. ObjectTypeDB::bind_method(_MD("set_property","property"),&VisualScriptPropertyGet::set_property);
  1158. ObjectTypeDB::bind_method(_MD("get_property"),&VisualScriptPropertyGet::get_property);
  1159. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptPropertyGet::set_call_mode);
  1160. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptPropertyGet::get_call_mode);
  1161. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptPropertyGet::set_base_path);
  1162. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptPropertyGet::get_base_path);
  1163. String bt;
  1164. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1165. if (i>0)
  1166. bt+=",";
  1167. bt+=Variant::get_type_name(Variant::Type(i));
  1168. }
  1169. String et;
  1170. for(int i=0;i<InputEvent::TYPE_MAX;i++) {
  1171. if (i>0)
  1172. et+=",";
  1173. et+=event_type_names[i];
  1174. }
  1175. 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"));
  1176. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_type",PROPERTY_HINT_TYPE_STRING,"Object"),_SCS("set_base_type"),_SCS("get_base_type"));
  1177. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/basic_type",PROPERTY_HINT_ENUM,bt),_SCS("set_basic_type"),_SCS("get_basic_type"));
  1178. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/event_type",PROPERTY_HINT_ENUM,et),_SCS("set_event_type"),_SCS("get_event_type"));
  1179. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"property/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  1180. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/property"),_SCS("set_property"),_SCS("get_property"));
  1181. BIND_CONSTANT( CALL_MODE_SELF );
  1182. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  1183. BIND_CONSTANT( CALL_MODE_INSTANCE);
  1184. }
  1185. class VisualScriptNodeInstancePropertyGet : public VisualScriptNodeInstance {
  1186. public:
  1187. VisualScriptPropertyGet::CallMode call_mode;
  1188. NodePath node_path;
  1189. StringName property;
  1190. VisualScriptPropertyGet *node;
  1191. VisualScriptInstance *instance;
  1192. //virtual int get_working_memory_size() const { return 0; }
  1193. virtual bool is_output_port_unsequenced(int p_idx) const { return (call_mode==VisualScriptPropertyGet::CALL_MODE_SELF || call_mode==VisualScriptPropertyGet::CALL_MODE_NODE_PATH); }
  1194. virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const {
  1195. //these two modes can be get directly, so they use unsequenced mode
  1196. switch(call_mode) {
  1197. case VisualScriptPropertyGet::CALL_MODE_SELF: {
  1198. Object *object=instance->get_owner_ptr();
  1199. bool valid;
  1200. *r_value = object->get(property,&valid);
  1201. if (!valid) {
  1202. //r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1203. r_error=RTR("Invalid index property name.");
  1204. return false;
  1205. }
  1206. } break;
  1207. case VisualScriptPropertyGet::CALL_MODE_NODE_PATH: {
  1208. Node* node = instance->get_owner_ptr()->cast_to<Node>();
  1209. if (!node) {
  1210. //r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1211. r_error=RTR("Base object is not a Node!");
  1212. return false;
  1213. }
  1214. Node* another = node->get_node(node_path);
  1215. if (!node) {
  1216. //r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1217. r_error=RTR("Path does not lead Node!");
  1218. return false;
  1219. }
  1220. bool valid;
  1221. *r_value = another->get(property,&valid);
  1222. if (!valid) {
  1223. //r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1224. r_error=vformat(RTR("Invalid index property name '%s' in node %s."),String(property),another->get_name());
  1225. return false;
  1226. }
  1227. } break;
  1228. default: {};
  1229. }
  1230. return true;
  1231. }
  1232. virtual int step(const Variant** p_inputs,Variant** p_outputs,StartMode p_start_mode,Variant* p_working_mem,Variant::CallError& r_error,String& r_error_str) {
  1233. bool valid;
  1234. Variant v = *p_inputs[0];
  1235. *p_outputs[0] = v.get(property,&valid);
  1236. if (!valid) {
  1237. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1238. r_error_str=RTR("Invalid index property name.");
  1239. }
  1240. return 0;
  1241. }
  1242. };
  1243. VisualScriptNodeInstance* VisualScriptPropertyGet::instance(VisualScriptInstance* p_instance) {
  1244. VisualScriptNodeInstancePropertyGet * instance = memnew(VisualScriptNodeInstancePropertyGet );
  1245. instance->node=this;
  1246. instance->instance=p_instance;
  1247. instance->property=property;
  1248. instance->call_mode=call_mode;
  1249. instance->node_path=base_path;
  1250. return instance;
  1251. }
  1252. VisualScriptPropertyGet::VisualScriptPropertyGet() {
  1253. call_mode=CALL_MODE_INSTANCE;
  1254. base_type="Object";
  1255. basic_type=Variant::NIL;
  1256. event_type=InputEvent::NONE;
  1257. }
  1258. template<VisualScriptPropertyGet::CallMode cmode>
  1259. static Ref<VisualScriptNode> create_property_get_node(const String& p_name) {
  1260. Ref<VisualScriptPropertyGet> node;
  1261. node.instance();
  1262. node->set_call_mode(cmode);
  1263. return node;
  1264. }
  1265. //////////////////////////////////////////
  1266. ////////////////SCRIPT CALL//////////////////////
  1267. //////////////////////////////////////////
  1268. int VisualScriptScriptCall::get_output_sequence_port_count() const {
  1269. return 1;
  1270. }
  1271. bool VisualScriptScriptCall::has_input_sequence_port() const{
  1272. return true;
  1273. }
  1274. Node *VisualScriptScriptCall::_get_base_node() const {
  1275. #ifdef TOOLS_ENABLED
  1276. Ref<Script> script = get_visual_script();
  1277. if (!script.is_valid())
  1278. return NULL;
  1279. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  1280. if (!main_loop)
  1281. return NULL;
  1282. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  1283. if (!scene_tree)
  1284. return NULL;
  1285. Node *edited_scene = scene_tree->get_edited_scene_root();
  1286. if (!edited_scene)
  1287. return NULL;
  1288. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  1289. if (!script_node)
  1290. return NULL;
  1291. if (!script_node->has_node(base_path))
  1292. return NULL;
  1293. Node *path_to = script_node->get_node(base_path);
  1294. return path_to;
  1295. #else
  1296. return NULL;
  1297. #endif
  1298. }
  1299. int VisualScriptScriptCall::get_input_value_port_count() const{
  1300. #if 1
  1301. return argument_count;
  1302. #else
  1303. if (call_mode==CALL_MODE_SELF) {
  1304. Ref<VisualScript> vs = get_visual_script();
  1305. if (vs.is_valid()) {
  1306. if (!vs->has_function(function))
  1307. return 0;
  1308. int id = vs->get_function_node_id(function);
  1309. if (id<0)
  1310. return 0;
  1311. Ref<VisualScriptFunction> func = vs->get_node(function,id);
  1312. return func->get_argument_count();
  1313. }
  1314. } else {
  1315. Node*base = _get_base_node();
  1316. if (!base)
  1317. return 0;
  1318. Ref<Script> script = base->get_script();
  1319. if (!script.is_valid())
  1320. return 0;
  1321. List<MethodInfo> functions;
  1322. script->get_method_list(&functions);
  1323. for (List<MethodInfo>::Element *E=functions.front();E;E=E->next()) {
  1324. if (E->get().name==function) {
  1325. return E->get().arguments.size();
  1326. }
  1327. }
  1328. }
  1329. return 0;
  1330. #endif
  1331. }
  1332. int VisualScriptScriptCall::get_output_value_port_count() const{
  1333. return 1;
  1334. }
  1335. String VisualScriptScriptCall::get_output_sequence_port_text(int p_port) const {
  1336. return String();
  1337. }
  1338. PropertyInfo VisualScriptScriptCall::get_input_value_port_info(int p_idx) const{
  1339. if (call_mode==CALL_MODE_SELF) {
  1340. Ref<VisualScript> vs = get_visual_script();
  1341. if (vs.is_valid()) {
  1342. if (!vs->has_function(function))
  1343. return PropertyInfo();
  1344. int id = vs->get_function_node_id(function);
  1345. if (id<0)
  1346. return PropertyInfo();
  1347. Ref<VisualScriptFunction> func = vs->get_node(function,id);
  1348. if (p_idx>=func->get_argument_count())
  1349. return PropertyInfo();
  1350. return PropertyInfo(func->get_argument_type(p_idx),func->get_argument_name(p_idx));
  1351. }
  1352. } else {
  1353. Node*base = _get_base_node();
  1354. if (!base)
  1355. return PropertyInfo();
  1356. Ref<Script> script = base->get_script();
  1357. if (!script.is_valid())
  1358. return PropertyInfo();
  1359. List<MethodInfo> functions;
  1360. script->get_method_list(&functions);
  1361. for (List<MethodInfo>::Element *E=functions.front();E;E=E->next()) {
  1362. if (E->get().name==function) {
  1363. if (p_idx<0 || p_idx>=E->get().arguments.size())
  1364. return PropertyInfo();
  1365. return E->get().arguments[p_idx];
  1366. }
  1367. }
  1368. }
  1369. return PropertyInfo();
  1370. }
  1371. PropertyInfo VisualScriptScriptCall::get_output_value_port_info(int p_idx) const{
  1372. return PropertyInfo();
  1373. }
  1374. String VisualScriptScriptCall::get_caption() const {
  1375. return "ScriptCall";
  1376. }
  1377. String VisualScriptScriptCall::get_text() const {
  1378. return " "+String(function)+"()";
  1379. }
  1380. void VisualScriptScriptCall::_update_argument_count() {
  1381. //try to remember the amount of arguments in the function, because if loaded from scratch
  1382. //this information will not be available
  1383. if (call_mode==CALL_MODE_SELF) {
  1384. Ref<VisualScript> vs = get_visual_script();
  1385. if (vs.is_valid()) {
  1386. if (!vs->has_function(function))
  1387. return ;
  1388. int id = vs->get_function_node_id(function);
  1389. if (id<0)
  1390. return;
  1391. Ref<VisualScriptFunction> func = vs->get_node(function,id);
  1392. argument_count=func->get_argument_count();
  1393. }
  1394. } else {
  1395. Node*base = _get_base_node();
  1396. if (!base)
  1397. return;
  1398. Ref<Script> script = base->get_script();
  1399. if (!script.is_valid())
  1400. return ;
  1401. List<MethodInfo> functions;
  1402. script->get_method_list(&functions);
  1403. for (List<MethodInfo>::Element *E=functions.front();E;E=E->next()) {
  1404. if (E->get().name==function) {
  1405. argument_count=E->get().arguments.size();
  1406. return;
  1407. }
  1408. }
  1409. }
  1410. }
  1411. void VisualScriptScriptCall::set_function(const StringName& p_type){
  1412. if (function==p_type)
  1413. return;
  1414. function=p_type;
  1415. _update_argument_count();
  1416. _change_notify();
  1417. ports_changed_notify();
  1418. }
  1419. StringName VisualScriptScriptCall::get_function() const {
  1420. return function;
  1421. }
  1422. void VisualScriptScriptCall::set_base_path(const NodePath& p_type) {
  1423. if (base_path==p_type)
  1424. return;
  1425. base_path=p_type;
  1426. _update_argument_count();
  1427. _change_notify();
  1428. ports_changed_notify();
  1429. }
  1430. NodePath VisualScriptScriptCall::get_base_path() const {
  1431. return base_path;
  1432. }
  1433. void VisualScriptScriptCall::set_call_mode(CallMode p_mode) {
  1434. if (call_mode==p_mode)
  1435. return;
  1436. call_mode=p_mode;
  1437. _update_argument_count();
  1438. _change_notify();
  1439. ports_changed_notify();
  1440. }
  1441. void VisualScriptScriptCall::set_argument_count(int p_count) {
  1442. argument_count=p_count;
  1443. _change_notify();
  1444. ports_changed_notify();
  1445. }
  1446. int VisualScriptScriptCall::get_argument_count() const {
  1447. return argument_count;
  1448. }
  1449. VisualScriptScriptCall::CallMode VisualScriptScriptCall::get_call_mode() const {
  1450. return call_mode;
  1451. }
  1452. void VisualScriptScriptCall::_validate_property(PropertyInfo& property) const {
  1453. if (property.name=="function/node_path") {
  1454. if (call_mode!=CALL_MODE_NODE_PATH) {
  1455. property.usage=0;
  1456. } else {
  1457. Node *bnode = _get_base_node();
  1458. if (bnode) {
  1459. property.hint_string=bnode->get_path(); //convert to loong string
  1460. } else {
  1461. }
  1462. }
  1463. }
  1464. if (property.name=="function/function") {
  1465. property.hint=PROPERTY_HINT_ENUM;
  1466. List<MethodInfo> methods;
  1467. if (call_mode==CALL_MODE_SELF) {
  1468. Ref<VisualScript> vs = get_visual_script();
  1469. if (vs.is_valid()) {
  1470. vs->get_method_list(&methods);
  1471. }
  1472. } else {
  1473. Node*base = _get_base_node();
  1474. if (!base)
  1475. return;
  1476. Ref<Script> script = base->get_script();
  1477. if (!script.is_valid())
  1478. return;
  1479. script->get_method_list(&methods);
  1480. }
  1481. List<String> mstring;
  1482. for (List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1483. if (E->get().name.begins_with("_"))
  1484. continue;
  1485. mstring.push_back(E->get().name.get_slice(":",0));
  1486. }
  1487. mstring.sort();
  1488. String ml;
  1489. for (List<String>::Element *E=mstring.front();E;E=E->next()) {
  1490. if (ml!=String())
  1491. ml+=",";
  1492. ml+=E->get();
  1493. }
  1494. property.hint_string=ml;
  1495. }
  1496. }
  1497. void VisualScriptScriptCall::_bind_methods() {
  1498. ObjectTypeDB::bind_method(_MD("set_function","function"),&VisualScriptScriptCall::set_function);
  1499. ObjectTypeDB::bind_method(_MD("get_function"),&VisualScriptScriptCall::get_function);
  1500. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptScriptCall::set_call_mode);
  1501. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptScriptCall::get_call_mode);
  1502. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptScriptCall::set_base_path);
  1503. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptScriptCall::get_base_path);
  1504. ObjectTypeDB::bind_method(_MD("set_argument_count","argument_count"),&VisualScriptScriptCall::set_argument_count);
  1505. ObjectTypeDB::bind_method(_MD("get_argument_count"),&VisualScriptScriptCall::get_argument_count);
  1506. ADD_PROPERTY(PropertyInfo(Variant::INT,"function/call_mode",PROPERTY_HINT_ENUM,"Self,Node Path"),_SCS("set_call_mode"),_SCS("get_call_mode"));
  1507. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"function/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  1508. ADD_PROPERTY(PropertyInfo(Variant::STRING,"function/function"),_SCS("set_function"),_SCS("get_function"));
  1509. ADD_PROPERTY(PropertyInfo(Variant::STRING,"function/argument_count"),_SCS("set_argument_count"),_SCS("get_argument_count"));
  1510. BIND_CONSTANT( CALL_MODE_SELF );
  1511. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  1512. }
  1513. class VisualScriptNodeInstanceScriptCall : public VisualScriptNodeInstance {
  1514. public:
  1515. VisualScriptScriptCall::CallMode call_mode;
  1516. NodePath node_path;
  1517. int input_args;
  1518. bool returns;
  1519. StringName function;
  1520. VisualScriptScriptCall *node;
  1521. VisualScriptInstance *instance;
  1522. //virtual int get_working_memory_size() const { return 0; }
  1523. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1524. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1525. virtual int step(const Variant** p_inputs,Variant** p_outputs,StartMode p_start_mode,Variant* p_working_mem,Variant::CallError& r_error,String& r_error_str) {
  1526. switch(call_mode) {
  1527. case VisualScriptScriptCall::CALL_MODE_SELF: {
  1528. Object *object=instance->get_owner_ptr();
  1529. *p_outputs[0] = object->call(function,p_inputs,input_args,r_error);
  1530. } break;
  1531. case VisualScriptScriptCall::CALL_MODE_NODE_PATH: {
  1532. Node* node = instance->get_owner_ptr()->cast_to<Node>();
  1533. if (!node) {
  1534. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1535. r_error_str="Base object is not a Node!";
  1536. return 0;
  1537. }
  1538. Node* another = node->get_node(node_path);
  1539. if (!node) {
  1540. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1541. r_error_str="Path does not lead Node!";
  1542. return 0;
  1543. }
  1544. *p_outputs[0] = another->call(function,p_inputs,input_args,r_error);
  1545. } break;
  1546. }
  1547. return 0;
  1548. }
  1549. };
  1550. VisualScriptNodeInstance* VisualScriptScriptCall::instance(VisualScriptInstance* p_instance) {
  1551. VisualScriptNodeInstanceScriptCall * instance = memnew(VisualScriptNodeInstanceScriptCall );
  1552. instance->node=this;
  1553. instance->instance=p_instance;
  1554. instance->function=function;
  1555. instance->call_mode=call_mode;
  1556. instance->node_path=base_path;
  1557. instance->input_args = argument_count;
  1558. return instance;
  1559. }
  1560. VisualScriptScriptCall::VisualScriptScriptCall() {
  1561. call_mode=CALL_MODE_SELF;
  1562. argument_count=0;
  1563. }
  1564. template<VisualScriptScriptCall::CallMode cmode>
  1565. static Ref<VisualScriptNode> create_script_call_node(const String& p_name) {
  1566. Ref<VisualScriptScriptCall> node;
  1567. node.instance();
  1568. node->set_call_mode(cmode);
  1569. return node;
  1570. }
  1571. //////////////////////////////////////////
  1572. ////////////////EMIT//////////////////////
  1573. //////////////////////////////////////////
  1574. int VisualScriptEmitSignal::get_output_sequence_port_count() const {
  1575. return 1;
  1576. }
  1577. bool VisualScriptEmitSignal::has_input_sequence_port() const{
  1578. return true;
  1579. }
  1580. int VisualScriptEmitSignal::get_input_value_port_count() const{
  1581. Ref<VisualScript> vs = get_visual_script();
  1582. if (vs.is_valid()) {
  1583. if (!vs->has_custom_signal(name))
  1584. return 0;
  1585. return vs->custom_signal_get_argument_count(name);
  1586. }
  1587. return 0;
  1588. }
  1589. int VisualScriptEmitSignal::get_output_value_port_count() const{
  1590. return 0;
  1591. }
  1592. String VisualScriptEmitSignal::get_output_sequence_port_text(int p_port) const {
  1593. return String();
  1594. }
  1595. PropertyInfo VisualScriptEmitSignal::get_input_value_port_info(int p_idx) const{
  1596. Ref<VisualScript> vs = get_visual_script();
  1597. if (vs.is_valid()) {
  1598. if (!vs->has_custom_signal(name))
  1599. return PropertyInfo();
  1600. return PropertyInfo(vs->custom_signal_get_argument_type(name,p_idx),vs->custom_signal_get_argument_name(name,p_idx));
  1601. }
  1602. return PropertyInfo();
  1603. }
  1604. PropertyInfo VisualScriptEmitSignal::get_output_value_port_info(int p_idx) const{
  1605. return PropertyInfo();
  1606. }
  1607. String VisualScriptEmitSignal::get_caption() const {
  1608. return "EmitSignal";
  1609. }
  1610. String VisualScriptEmitSignal::get_text() const {
  1611. return "emit "+String(name);
  1612. }
  1613. void VisualScriptEmitSignal::set_signal(const StringName& p_type){
  1614. if (name==p_type)
  1615. return;
  1616. name=p_type;
  1617. _change_notify();
  1618. ports_changed_notify();
  1619. }
  1620. StringName VisualScriptEmitSignal::get_signal() const {
  1621. return name;
  1622. }
  1623. void VisualScriptEmitSignal::_validate_property(PropertyInfo& property) const {
  1624. if (property.name=="signal/signal") {
  1625. property.hint=PROPERTY_HINT_ENUM;
  1626. List<StringName> sigs;
  1627. Ref<VisualScript> vs = get_visual_script();
  1628. if (vs.is_valid()) {
  1629. vs->get_custom_signal_list(&sigs);
  1630. }
  1631. String ml;
  1632. for (List<StringName>::Element *E=sigs.front();E;E=E->next()) {
  1633. if (ml!=String())
  1634. ml+=",";
  1635. ml+=E->get();
  1636. }
  1637. property.hint_string=ml;
  1638. }
  1639. }
  1640. void VisualScriptEmitSignal::_bind_methods() {
  1641. ObjectTypeDB::bind_method(_MD("set_signal","name"),&VisualScriptEmitSignal::set_signal);
  1642. ObjectTypeDB::bind_method(_MD("get_signal"),&VisualScriptEmitSignal::get_signal);
  1643. ADD_PROPERTY(PropertyInfo(Variant::STRING,"signal/signal"),_SCS("set_signal"),_SCS("get_signal"));
  1644. }
  1645. class VisualScriptNodeInstanceEmitSignal : public VisualScriptNodeInstance {
  1646. public:
  1647. VisualScriptEmitSignal *node;
  1648. VisualScriptInstance *instance;
  1649. int argcount;
  1650. StringName name;
  1651. //virtual int get_working_memory_size() const { return 0; }
  1652. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1653. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1654. virtual int step(const Variant** p_inputs,Variant** p_outputs,StartMode p_start_mode,Variant* p_working_mem,Variant::CallError& r_error,String& r_error_str) {
  1655. Object *obj = instance->get_owner_ptr();
  1656. obj->emit_signal(name,p_inputs,argcount);
  1657. return 0;
  1658. }
  1659. };
  1660. VisualScriptNodeInstance* VisualScriptEmitSignal::instance(VisualScriptInstance* p_instance) {
  1661. VisualScriptNodeInstanceEmitSignal * instance = memnew(VisualScriptNodeInstanceEmitSignal );
  1662. instance->node=this;
  1663. instance->instance=p_instance;
  1664. instance->name=name;
  1665. instance->argcount=get_input_value_port_count();
  1666. return instance;
  1667. }
  1668. VisualScriptEmitSignal::VisualScriptEmitSignal() {
  1669. }
  1670. static Ref<VisualScriptNode> create_basic_type_call_node(const String& p_name) {
  1671. Vector<String> path = p_name.split("/");
  1672. ERR_FAIL_COND_V(path.size()<4,Ref<VisualScriptNode>());
  1673. String base_type = path[2];
  1674. String method = path[3];
  1675. Ref<VisualScriptFunctionCall> node;
  1676. node.instance();
  1677. Variant::Type type=Variant::VARIANT_MAX;
  1678. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1679. if (Variant::get_type_name(Variant::Type(i))==base_type) {
  1680. type=Variant::Type(i);
  1681. break;
  1682. }
  1683. }
  1684. ERR_FAIL_COND_V(type==Variant::VARIANT_MAX,Ref<VisualScriptNode>());
  1685. node->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  1686. node->set_basic_type(type);
  1687. node->set_function(method);
  1688. return node;
  1689. }
  1690. void register_visual_script_func_nodes() {
  1691. VisualScriptLanguage::singleton->add_register_func("functions/call_method/call_instance",create_function_call_node<VisualScriptFunctionCall::CALL_MODE_INSTANCE>);
  1692. VisualScriptLanguage::singleton->add_register_func("functions/call_method/call_basic_type",create_function_call_node<VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE>);
  1693. VisualScriptLanguage::singleton->add_register_func("functions/call_method/call_self",create_function_call_node<VisualScriptFunctionCall::CALL_MODE_SELF>);
  1694. VisualScriptLanguage::singleton->add_register_func("functions/call_method/call_node",create_function_call_node<VisualScriptFunctionCall::CALL_MODE_NODE_PATH>);
  1695. VisualScriptLanguage::singleton->add_register_func("functions/property_set/instace_set",create_property_set_node<VisualScriptPropertySet::CALL_MODE_INSTANCE>);
  1696. VisualScriptLanguage::singleton->add_register_func("functions/property_set/basic_type_set",create_property_set_node<VisualScriptPropertySet::CALL_MODE_BASIC_TYPE>);
  1697. VisualScriptLanguage::singleton->add_register_func("functions/property_set/self_set",create_property_set_node<VisualScriptPropertySet::CALL_MODE_SELF>);
  1698. VisualScriptLanguage::singleton->add_register_func("functions/property_set/node_set",create_property_set_node<VisualScriptPropertySet::CALL_MODE_NODE_PATH>);
  1699. VisualScriptLanguage::singleton->add_register_func("functions/property_get/instance_get",create_property_get_node<VisualScriptPropertyGet::CALL_MODE_INSTANCE>);
  1700. VisualScriptLanguage::singleton->add_register_func("functions/property_get/basic_type_get",create_property_get_node<VisualScriptPropertyGet::CALL_MODE_BASIC_TYPE>);
  1701. VisualScriptLanguage::singleton->add_register_func("functions/property_get/self_get",create_property_get_node<VisualScriptPropertyGet::CALL_MODE_SELF>);
  1702. VisualScriptLanguage::singleton->add_register_func("functions/property_get/node_get",create_property_get_node<VisualScriptPropertyGet::CALL_MODE_NODE_PATH>);
  1703. VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_self",create_script_call_node<VisualScriptScriptCall::CALL_MODE_SELF>);
  1704. VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_node",create_script_call_node<VisualScriptScriptCall::CALL_MODE_NODE_PATH>);
  1705. VisualScriptLanguage::singleton->add_register_func("functions/call_script/emit_signal",create_node_generic<VisualScriptEmitSignal>);
  1706. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1707. Variant::Type t = Variant::Type(i);
  1708. String type_name = Variant::get_type_name(t);
  1709. Variant::CallError ce;
  1710. Variant vt = Variant::construct(t,NULL,0,ce);
  1711. List<MethodInfo> ml;
  1712. vt.get_method_list(&ml);
  1713. for (List<MethodInfo>::Element *E=ml.front();E;E=E->next()) {
  1714. VisualScriptLanguage::singleton->add_register_func("functions/by_type/"+type_name+"/"+E->get().name,create_basic_type_call_node);
  1715. }
  1716. }
  1717. }