visual_script_func_nodes.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520
  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. #include "io/resource_loader.h"
  7. #include "globals.h"
  8. //////////////////////////////////////////
  9. ////////////////CALL//////////////////////
  10. //////////////////////////////////////////
  11. int VisualScriptFunctionCall::get_output_sequence_port_count() const {
  12. if (method_cache.flags&METHOD_FLAG_CONST || call_mode==CALL_MODE_BASIC_TYPE)
  13. return 0;
  14. else
  15. return 1;
  16. }
  17. bool VisualScriptFunctionCall::has_input_sequence_port() const{
  18. if (method_cache.flags&METHOD_FLAG_CONST || call_mode==CALL_MODE_BASIC_TYPE)
  19. return false;
  20. else
  21. return true;
  22. }
  23. #ifdef TOOLS_ENABLED
  24. static Node* _find_script_node(Node* p_edited_scene,Node* p_current_node,const Ref<Script> &script) {
  25. if (p_edited_scene!=p_current_node && p_current_node->get_owner()!=p_edited_scene)
  26. return NULL;
  27. Ref<Script> scr = p_current_node->get_script();
  28. if (scr.is_valid() && scr==script)
  29. return p_current_node;
  30. for(int i=0;i<p_current_node->get_child_count();i++) {
  31. Node *n = _find_script_node(p_edited_scene,p_current_node->get_child(i),script);
  32. if (n)
  33. return n;
  34. }
  35. return NULL;
  36. }
  37. #endif
  38. Node *VisualScriptFunctionCall::_get_base_node() const {
  39. #ifdef TOOLS_ENABLED
  40. Ref<Script> script = get_visual_script();
  41. if (!script.is_valid())
  42. return NULL;
  43. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  44. if (!main_loop)
  45. return NULL;
  46. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  47. if (!scene_tree)
  48. return NULL;
  49. Node *edited_scene = scene_tree->get_edited_scene_root();
  50. if (!edited_scene)
  51. return NULL;
  52. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  53. if (!script_node)
  54. return NULL;
  55. if (!script_node->has_node(base_path))
  56. return NULL;
  57. Node *path_to = script_node->get_node(base_path);
  58. return path_to;
  59. #else
  60. return NULL;
  61. #endif
  62. }
  63. StringName VisualScriptFunctionCall::_get_base_type() const {
  64. if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid())
  65. return get_visual_script()->get_instance_base_type();
  66. else if (call_mode==CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  67. Node *path = _get_base_node();
  68. if (path)
  69. return path->get_type();
  70. }
  71. return base_type;
  72. }
  73. int VisualScriptFunctionCall::get_input_value_port_count() const{
  74. if (call_mode==CALL_MODE_BASIC_TYPE) {
  75. Vector<StringName> names = Variant::get_method_argument_names(basic_type,function);
  76. return names.size() + (rpc_call_mode>=RPC_RELIABLE_TO_ID?1:0) + 1;
  77. } else {
  78. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  79. if (mb) {
  80. return mb->get_argument_count() + (call_mode==CALL_MODE_INSTANCE?1:0) + (rpc_call_mode>=RPC_RELIABLE_TO_ID?1:0) - use_default_args;
  81. }
  82. return method_cache.arguments.size() + (call_mode==CALL_MODE_INSTANCE?1:0) + (rpc_call_mode>=RPC_RELIABLE_TO_ID?1:0) - use_default_args;
  83. }
  84. }
  85. int VisualScriptFunctionCall::get_output_value_port_count() const{
  86. if (call_mode==CALL_MODE_BASIC_TYPE) {
  87. bool returns=false;
  88. Variant::get_method_return_type(basic_type,function,&returns);
  89. return returns?1:0;
  90. } else {
  91. int ret;
  92. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  93. if (mb) {
  94. ret = mb->has_return() ? 1 : 0;
  95. } else
  96. ret = 1; //it is assumed that script always returns something
  97. if (call_mode==CALL_MODE_INSTANCE) {
  98. ret++;
  99. }
  100. return ret;
  101. }
  102. }
  103. String VisualScriptFunctionCall::get_output_sequence_port_text(int p_port) const {
  104. return String();
  105. }
  106. PropertyInfo VisualScriptFunctionCall::get_input_value_port_info(int p_idx) const{
  107. if (call_mode==CALL_MODE_INSTANCE || call_mode==CALL_MODE_BASIC_TYPE) {
  108. if (p_idx==0) {
  109. PropertyInfo pi;
  110. pi.type=(call_mode==CALL_MODE_INSTANCE?Variant::OBJECT:basic_type);
  111. pi.name=(call_mode==CALL_MODE_INSTANCE?String("instance"):Variant::get_type_name(basic_type).to_lower());
  112. return pi;
  113. } else {
  114. p_idx--;
  115. }
  116. }
  117. if (rpc_call_mode>=RPC_RELIABLE_TO_ID) {
  118. if (p_idx==0) {
  119. return PropertyInfo(Variant::INT,"peer_id");
  120. } else {
  121. p_idx--;
  122. }
  123. }
  124. #ifdef DEBUG_METHODS_ENABLED
  125. if (call_mode==CALL_MODE_BASIC_TYPE) {
  126. Vector<StringName> names = Variant::get_method_argument_names(basic_type,function);
  127. Vector<Variant::Type> types = Variant::get_method_argument_types(basic_type,function);
  128. return PropertyInfo(types[p_idx],names[p_idx]);
  129. } else {
  130. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  131. if (mb) {
  132. return mb->get_argument_info(p_idx);
  133. }
  134. if (p_idx>=0 && p_idx < method_cache.arguments.size()) {
  135. return method_cache.arguments[p_idx];
  136. }
  137. return PropertyInfo();
  138. }
  139. #else
  140. return PropertyInfo();
  141. #endif
  142. }
  143. PropertyInfo VisualScriptFunctionCall::get_output_value_port_info(int p_idx) const{
  144. #ifdef DEBUG_METHODS_ENABLED
  145. if (call_mode==CALL_MODE_BASIC_TYPE) {
  146. return PropertyInfo(Variant::get_method_return_type(basic_type,function),"");
  147. } else {
  148. if (call_mode==CALL_MODE_INSTANCE) {
  149. if (p_idx==0) {
  150. return PropertyInfo(Variant::OBJECT,"pass");
  151. } else {
  152. p_idx--;
  153. }
  154. }
  155. PropertyInfo ret;
  156. /*MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  157. if (mb) {
  158. ret = mb->get_argument_info(-1);
  159. } else {*/
  160. ret = method_cache.return_val;
  161. //}
  162. if (call_mode==CALL_MODE_INSTANCE) {
  163. ret.name="return";
  164. } else {
  165. ret.name="";
  166. }
  167. return ret;
  168. }
  169. #else
  170. return PropertyInfo();
  171. #endif
  172. }
  173. String VisualScriptFunctionCall::get_caption() const {
  174. static const char*cname[5]= {
  175. "CallSelf",
  176. "CallNode",
  177. "CallInstance",
  178. "CallBasic",
  179. "CallSingleton"
  180. };
  181. String caption = cname[call_mode];
  182. if (rpc_call_mode) {
  183. caption+=" (RPC)";
  184. }
  185. return caption;
  186. }
  187. String VisualScriptFunctionCall::get_text() const {
  188. if (call_mode==CALL_MODE_SELF)
  189. return " "+String(function)+"()";
  190. if (call_mode==CALL_MODE_SINGLETON)
  191. return String(singleton)+":"+String(function)+"()";
  192. else if (call_mode==CALL_MODE_BASIC_TYPE)
  193. return Variant::get_type_name(basic_type)+"."+String(function)+"()";
  194. else if (call_mode==CALL_MODE_NODE_PATH)
  195. return " ["+String(base_path.simplified())+"]."+String(function)+"()";
  196. else
  197. return " "+base_type+"."+String(function)+"()";
  198. }
  199. void VisualScriptFunctionCall::set_basic_type(Variant::Type p_type) {
  200. if (basic_type==p_type)
  201. return;
  202. basic_type=p_type;
  203. _change_notify();
  204. ports_changed_notify();
  205. }
  206. Variant::Type VisualScriptFunctionCall::get_basic_type() const{
  207. return basic_type;
  208. }
  209. void VisualScriptFunctionCall::set_base_type(const StringName& p_type) {
  210. if (base_type==p_type)
  211. return;
  212. base_type=p_type;
  213. _change_notify();
  214. ports_changed_notify();
  215. }
  216. StringName VisualScriptFunctionCall::get_base_type() const{
  217. return base_type;
  218. }
  219. void VisualScriptFunctionCall::set_base_script(const String& p_path) {
  220. if (base_script==p_path)
  221. return;
  222. base_script=p_path;
  223. _change_notify();
  224. ports_changed_notify();
  225. }
  226. String VisualScriptFunctionCall::get_base_script() const {
  227. return base_script;
  228. }
  229. void VisualScriptFunctionCall::set_singleton(const StringName& p_path) {
  230. if (singleton==p_path)
  231. return;
  232. singleton=p_path;
  233. Object *obj = Globals::get_singleton()->get_singleton_object(singleton);
  234. if (obj) {
  235. base_type=obj->get_type();
  236. }
  237. _change_notify();
  238. ports_changed_notify();
  239. }
  240. StringName VisualScriptFunctionCall::get_singleton() const {
  241. return singleton;
  242. }
  243. void VisualScriptFunctionCall::_update_method_cache() {
  244. StringName type;
  245. Ref<Script> script;
  246. if (call_mode==CALL_MODE_NODE_PATH) {
  247. Node* node=_get_base_node();
  248. if (node) {
  249. type=node->get_type();
  250. base_type=type; //cache, too
  251. script = node->get_script();
  252. }
  253. } else if (call_mode==CALL_MODE_SELF) {
  254. if (get_visual_script().is_valid()) {
  255. type=get_visual_script()->get_instance_base_type();
  256. base_type=type; //cache, too
  257. script=get_visual_script();
  258. }
  259. } else if (call_mode==CALL_MODE_SINGLETON) {
  260. Object *obj = Globals::get_singleton()->get_singleton_object(singleton);
  261. if (obj) {
  262. type=obj->get_type();
  263. script=obj->get_script();
  264. }
  265. } else if (call_mode==CALL_MODE_INSTANCE) {
  266. type=base_type;
  267. if (base_script!=String()) {
  268. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  269. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  270. }
  271. if (ResourceCache::has(base_script)) {
  272. script = Ref<Resource>( ResourceCache::get(base_script) );
  273. } else {
  274. return;
  275. }
  276. }
  277. }
  278. // print_line("BASE: "+String(type)+" FUNC: "+String(function));
  279. MethodBind *mb = ObjectTypeDB::get_method(type,function);
  280. if (mb) {
  281. use_default_args=mb->get_default_argument_count();
  282. method_cache = MethodInfo();
  283. for(int i=0;i<mb->get_argument_count();i++) {
  284. #ifdef DEBUG_METHODS_ENABLED
  285. method_cache.arguments.push_back(mb->get_argument_info(i));
  286. #else
  287. method_cache.arguments.push_back(PropertyInfo());
  288. #endif
  289. }
  290. if (mb->is_const()) {
  291. method_cache.flags|=METHOD_FLAG_CONST;
  292. }
  293. #ifdef DEBUG_METHODS_ENABLED
  294. method_cache.return_val = mb->get_argument_info(-1);
  295. #endif
  296. } else if (script.is_valid() && script->has_method(function)) {
  297. method_cache = script->get_method_info(function);
  298. use_default_args=method_cache.default_arguments.size();
  299. }
  300. }
  301. void VisualScriptFunctionCall::set_function(const StringName& p_type){
  302. if (function==p_type)
  303. return;
  304. function=p_type;
  305. if (call_mode==CALL_MODE_BASIC_TYPE) {
  306. use_default_args = Variant::get_method_default_arguments(basic_type,function).size();
  307. } else {
  308. //update all caches
  309. _update_method_cache();
  310. }
  311. _change_notify();
  312. ports_changed_notify();
  313. }
  314. StringName VisualScriptFunctionCall::get_function() const {
  315. return function;
  316. }
  317. void VisualScriptFunctionCall::set_base_path(const NodePath& p_type) {
  318. if (base_path==p_type)
  319. return;
  320. base_path=p_type;
  321. _change_notify();
  322. ports_changed_notify();
  323. }
  324. NodePath VisualScriptFunctionCall::get_base_path() const {
  325. return base_path;
  326. }
  327. void VisualScriptFunctionCall::set_call_mode(CallMode p_mode) {
  328. if (call_mode==p_mode)
  329. return;
  330. call_mode=p_mode;
  331. _change_notify();
  332. ports_changed_notify();
  333. }
  334. VisualScriptFunctionCall::CallMode VisualScriptFunctionCall::get_call_mode() const {
  335. return call_mode;
  336. }
  337. void VisualScriptFunctionCall::set_use_default_args(int p_amount) {
  338. if (use_default_args==p_amount)
  339. return;
  340. use_default_args=p_amount;
  341. ports_changed_notify();
  342. }
  343. void VisualScriptFunctionCall::set_rpc_call_mode(VisualScriptFunctionCall::RPCCallMode p_mode) {
  344. if (rpc_call_mode==p_mode)
  345. return;
  346. rpc_call_mode=p_mode;
  347. ports_changed_notify();
  348. _change_notify();
  349. }
  350. VisualScriptFunctionCall::RPCCallMode VisualScriptFunctionCall::get_rpc_call_mode() const{
  351. return rpc_call_mode;
  352. }
  353. int VisualScriptFunctionCall::get_use_default_args() const{
  354. return use_default_args;
  355. }
  356. void VisualScriptFunctionCall::set_validate(bool p_amount) {
  357. validate=p_amount;
  358. }
  359. bool VisualScriptFunctionCall::get_validate() const {
  360. return validate;
  361. }
  362. void VisualScriptFunctionCall::_set_argument_cache(const Dictionary& p_cache) {
  363. //so everything works in case all else fails
  364. method_cache=MethodInfo::from_dict(p_cache);
  365. }
  366. Dictionary VisualScriptFunctionCall::_get_argument_cache() const {
  367. return method_cache;
  368. }
  369. void VisualScriptFunctionCall::_validate_property(PropertyInfo& property) const {
  370. if (property.name=="function/base_type") {
  371. if (call_mode!=CALL_MODE_INSTANCE) {
  372. property.usage=PROPERTY_USAGE_NOEDITOR;
  373. }
  374. }
  375. if (property.name=="function/base_script") {
  376. if (call_mode!=CALL_MODE_INSTANCE) {
  377. property.usage=0;
  378. }
  379. }
  380. if (property.name=="function/basic_type") {
  381. if (call_mode!=CALL_MODE_BASIC_TYPE) {
  382. property.usage=0;
  383. }
  384. }
  385. if (property.name=="function/singleton") {
  386. if (call_mode!=CALL_MODE_SINGLETON) {
  387. property.usage=0;
  388. } else {
  389. List<Globals::Singleton> names;
  390. Globals::get_singleton()->get_singletons(&names);
  391. property.hint=PROPERTY_HINT_ENUM;
  392. String sl;
  393. for (List<Globals::Singleton>::Element *E=names.front();E;E=E->next()) {
  394. if (sl!=String())
  395. sl+=",";
  396. sl+=E->get().name;
  397. }
  398. property.hint_string=sl;
  399. }
  400. }
  401. if (property.name=="function/node_path") {
  402. if (call_mode!=CALL_MODE_NODE_PATH) {
  403. property.usage=0;
  404. } else {
  405. Node *bnode = _get_base_node();
  406. if (bnode) {
  407. property.hint_string=bnode->get_path(); //convert to loong string
  408. } else {
  409. }
  410. }
  411. }
  412. if (property.name=="function/function") {
  413. if (call_mode==CALL_MODE_BASIC_TYPE) {
  414. property.hint=PROPERTY_HINT_METHOD_OF_VARIANT_TYPE;
  415. property.hint_string=Variant::get_type_name(basic_type);
  416. } else if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid()) {
  417. property.hint=PROPERTY_HINT_METHOD_OF_SCRIPT;
  418. property.hint_string=itos(get_visual_script()->get_instance_ID());
  419. } else if (call_mode==CALL_MODE_SINGLETON) {
  420. Object *obj = Globals::get_singleton()->get_singleton_object(singleton);
  421. if (obj) {
  422. property.hint=PROPERTY_HINT_METHOD_OF_INSTANCE;
  423. property.hint_string=itos(obj->get_instance_ID());
  424. } else {
  425. property.hint=PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  426. property.hint_string=base_type;//should be cached
  427. }
  428. } else if (call_mode==CALL_MODE_INSTANCE) {
  429. property.hint=PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  430. property.hint_string=base_type;
  431. if (base_script!=String()) {
  432. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  433. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  434. }
  435. if (ResourceCache::has(base_script)) {
  436. Ref<Script> script = Ref<Resource>( ResourceCache::get(base_script) );
  437. if (script.is_valid()) {
  438. property.hint=PROPERTY_HINT_METHOD_OF_SCRIPT;
  439. property.hint_string=itos(script->get_instance_ID());
  440. }
  441. }
  442. }
  443. } else if (call_mode==CALL_MODE_NODE_PATH) {
  444. Node *node = _get_base_node();
  445. if (node) {
  446. property.hint=PROPERTY_HINT_METHOD_OF_INSTANCE;
  447. property.hint_string=itos(node->get_instance_ID());
  448. } else {
  449. property.hint=PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  450. property.hint_string=get_base_type();
  451. }
  452. }
  453. }
  454. if (property.name=="function/use_default_args") {
  455. property.hint=PROPERTY_HINT_RANGE;
  456. int mc=0;
  457. if (call_mode==CALL_MODE_BASIC_TYPE) {
  458. mc = Variant::get_method_default_arguments(basic_type,function).size();
  459. } else {
  460. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  461. if (mb) {
  462. mc=mb->get_default_argument_count();
  463. }
  464. }
  465. if (mc==0) {
  466. property.usage=0; //do not show
  467. } else {
  468. property.hint_string="0,"+itos(mc)+",1";
  469. }
  470. }
  471. if (property.name=="rpc/call_mode") {
  472. if (call_mode==CALL_MODE_BASIC_TYPE) {
  473. property.usage=0;
  474. }
  475. }
  476. }
  477. void VisualScriptFunctionCall::_bind_methods() {
  478. ObjectTypeDB::bind_method(_MD("set_base_type","base_type"),&VisualScriptFunctionCall::set_base_type);
  479. ObjectTypeDB::bind_method(_MD("get_base_type"),&VisualScriptFunctionCall::get_base_type);
  480. ObjectTypeDB::bind_method(_MD("set_base_script","base_script"),&VisualScriptFunctionCall::set_base_script);
  481. ObjectTypeDB::bind_method(_MD("get_base_script"),&VisualScriptFunctionCall::get_base_script);
  482. ObjectTypeDB::bind_method(_MD("set_basic_type","basic_type"),&VisualScriptFunctionCall::set_basic_type);
  483. ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptFunctionCall::get_basic_type);
  484. ObjectTypeDB::bind_method(_MD("set_singleton","singleton"),&VisualScriptFunctionCall::set_singleton);
  485. ObjectTypeDB::bind_method(_MD("get_singleton"),&VisualScriptFunctionCall::get_singleton);
  486. ObjectTypeDB::bind_method(_MD("set_function","function"),&VisualScriptFunctionCall::set_function);
  487. ObjectTypeDB::bind_method(_MD("get_function"),&VisualScriptFunctionCall::get_function);
  488. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptFunctionCall::set_call_mode);
  489. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptFunctionCall::get_call_mode);
  490. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptFunctionCall::set_base_path);
  491. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptFunctionCall::get_base_path);
  492. ObjectTypeDB::bind_method(_MD("set_use_default_args","amount"),&VisualScriptFunctionCall::set_use_default_args);
  493. ObjectTypeDB::bind_method(_MD("get_use_default_args"),&VisualScriptFunctionCall::get_use_default_args);
  494. ObjectTypeDB::bind_method(_MD("_set_argument_cache","argument_cache"),&VisualScriptFunctionCall::_set_argument_cache);
  495. ObjectTypeDB::bind_method(_MD("_get_argument_cache"),&VisualScriptFunctionCall::_get_argument_cache);
  496. ObjectTypeDB::bind_method(_MD("set_rpc_call_mode","mode"),&VisualScriptFunctionCall::set_rpc_call_mode);
  497. ObjectTypeDB::bind_method(_MD("get_rpc_call_mode"),&VisualScriptFunctionCall::get_rpc_call_mode);
  498. ObjectTypeDB::bind_method(_MD("set_validate","enable"),&VisualScriptFunctionCall::set_validate);
  499. ObjectTypeDB::bind_method(_MD("get_validate"),&VisualScriptFunctionCall::get_validate);
  500. String bt;
  501. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  502. if (i>0)
  503. bt+=",";
  504. bt+=Variant::get_type_name(Variant::Type(i));
  505. }
  506. List<String> script_extensions;
  507. for(int i=0;i<ScriptServer::get_language_count();i++) {
  508. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  509. }
  510. String script_ext_hint;
  511. for (List<String>::Element *E=script_extensions.front();E;E=E->next()) {
  512. if (script_ext_hint!=String())
  513. script_ext_hint+=",";
  514. script_ext_hint+="*."+E->get();
  515. }
  516. ADD_PROPERTY(PropertyInfo(Variant::INT,"function/call_mode",PROPERTY_HINT_ENUM,"Self,Node Path,Instance,Basic Type,Singleton"),_SCS("set_call_mode"),_SCS("get_call_mode"));
  517. ADD_PROPERTY(PropertyInfo(Variant::STRING,"function/base_type",PROPERTY_HINT_TYPE_STRING,"Object"),_SCS("set_base_type"),_SCS("get_base_type"));
  518. ADD_PROPERTY(PropertyInfo(Variant::STRING,"function/base_script",PROPERTY_HINT_FILE,script_ext_hint),_SCS("set_base_script"),_SCS("get_base_script"));
  519. ADD_PROPERTY(PropertyInfo(Variant::STRING,"function/singleton"),_SCS("set_singleton"),_SCS("get_singleton"));
  520. ADD_PROPERTY(PropertyInfo(Variant::INT,"function/basic_type",PROPERTY_HINT_ENUM,bt),_SCS("set_basic_type"),_SCS("get_basic_type"));
  521. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"function/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  522. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY,"function/argument_cache",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_argument_cache"),_SCS("_get_argument_cache"));
  523. ADD_PROPERTY(PropertyInfo(Variant::STRING,"function/function"),_SCS("set_function"),_SCS("get_function")); //when set, if loaded properly, will override argument count.
  524. ADD_PROPERTY(PropertyInfo(Variant::INT,"function/use_default_args"),_SCS("set_use_default_args"),_SCS("get_use_default_args"));
  525. ADD_PROPERTY(PropertyInfo(Variant::BOOL,"function/validate"),_SCS("set_validate"),_SCS("get_validate"));
  526. ADD_PROPERTY(PropertyInfo(Variant::INT,"rpc/call_mode",PROPERTY_HINT_ENUM,"Disabled,Reliable,Unreliable,ReliableToID,UnreliableToID"),_SCS("set_rpc_call_mode"),_SCS("get_rpc_call_mode")); //when set, if loaded properly, will override argument count.
  527. BIND_CONSTANT( CALL_MODE_SELF );
  528. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  529. BIND_CONSTANT( CALL_MODE_INSTANCE);
  530. BIND_CONSTANT( CALL_MODE_BASIC_TYPE );
  531. }
  532. class VisualScriptNodeInstanceFunctionCall : public VisualScriptNodeInstance {
  533. public:
  534. VisualScriptFunctionCall::CallMode call_mode;
  535. NodePath node_path;
  536. int input_args;
  537. bool validate;
  538. bool returns;
  539. VisualScriptFunctionCall::RPCCallMode rpc_mode;
  540. StringName function;
  541. StringName singleton;
  542. VisualScriptFunctionCall *node;
  543. VisualScriptInstance *instance;
  544. //virtual int get_working_memory_size() const { return 0; }
  545. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  546. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  547. _FORCE_INLINE_ bool call_rpc(Object* p_base,const Variant** p_args,int p_argcount) {
  548. if (!p_base)
  549. return false;
  550. Node * node = p_base->cast_to<Node>();
  551. if (!node)
  552. return false;
  553. int to_id=0;
  554. bool reliable=true;
  555. if (rpc_mode>=VisualScriptFunctionCall::RPC_RELIABLE_TO_ID) {
  556. to_id = *p_args[0];
  557. p_args+=1;
  558. p_argcount-=1;
  559. if (rpc_mode==VisualScriptFunctionCall::RPC_UNRELIABLE_TO_ID) {
  560. reliable=false;
  561. }
  562. } else if (rpc_mode==VisualScriptFunctionCall::RPC_UNRELIABLE) {
  563. reliable=false;
  564. }
  565. node->rpcp(to_id,!reliable,function,p_args,p_argcount);
  566. return true;
  567. }
  568. 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) {
  569. switch(call_mode) {
  570. case VisualScriptFunctionCall::CALL_MODE_SELF: {
  571. Object *object=instance->get_owner_ptr();
  572. if (rpc_mode) {
  573. call_rpc(object,p_inputs,input_args);
  574. } else if (returns) {
  575. *p_outputs[0] = object->call(function,p_inputs,input_args,r_error);
  576. } else {
  577. object->call(function,p_inputs,input_args,r_error);
  578. }
  579. } break;
  580. case VisualScriptFunctionCall::CALL_MODE_NODE_PATH: {
  581. Node* node = instance->get_owner_ptr()->cast_to<Node>();
  582. if (!node) {
  583. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  584. r_error_str="Base object is not a Node!";
  585. return 0;
  586. }
  587. Node* another = node->get_node(node_path);
  588. if (!node) {
  589. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  590. r_error_str="Path does not lead Node!";
  591. return 0;
  592. }
  593. if (rpc_mode) {
  594. call_rpc(node,p_inputs,input_args);
  595. } else if (returns) {
  596. *p_outputs[0] = another->call(function,p_inputs,input_args,r_error);
  597. } else {
  598. another->call(function,p_inputs,input_args,r_error);
  599. }
  600. } break;
  601. case VisualScriptFunctionCall::CALL_MODE_INSTANCE:
  602. case VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE: {
  603. Variant v = *p_inputs[0];
  604. if (rpc_mode) {
  605. Object *obj = v;
  606. if (obj) {
  607. call_rpc(obj,p_inputs+1,input_args-1);
  608. }
  609. } else if (returns) {
  610. if (call_mode==VisualScriptFunctionCall::CALL_MODE_INSTANCE) {
  611. *p_outputs[1] = v.call(function,p_inputs+1,input_args,r_error);
  612. } else {
  613. *p_outputs[0] = v.call(function,p_inputs+1,input_args,r_error);
  614. }
  615. } else {
  616. v.call(function,p_inputs+1,input_args,r_error);
  617. }
  618. if (call_mode==VisualScriptFunctionCall::CALL_MODE_INSTANCE) {
  619. *p_outputs[0]=*p_inputs[0];
  620. }
  621. } break;
  622. case VisualScriptFunctionCall::CALL_MODE_SINGLETON: {
  623. Object *object=Globals::get_singleton()->get_singleton_object(singleton);
  624. if (!object) {
  625. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  626. r_error_str="Invalid singleton name: '"+String(singleton)+"'";
  627. return 0;
  628. }
  629. if (rpc_mode) {
  630. call_rpc(object,p_inputs,input_args);
  631. } else if (returns) {
  632. *p_outputs[0] = object->call(function,p_inputs,input_args,r_error);
  633. } else {
  634. object->call(function,p_inputs,input_args,r_error);
  635. }
  636. } break;
  637. }
  638. if (!validate) {
  639. //ignore call errors if validation is disabled
  640. r_error.error=Variant::CallError::CALL_OK;
  641. r_error_str=String();
  642. }
  643. return 0;
  644. }
  645. };
  646. VisualScriptNodeInstance* VisualScriptFunctionCall::instance(VisualScriptInstance* p_instance) {
  647. VisualScriptNodeInstanceFunctionCall * instance = memnew(VisualScriptNodeInstanceFunctionCall );
  648. instance->node=this;
  649. instance->instance=p_instance;
  650. instance->singleton=singleton;
  651. instance->function=function;
  652. instance->call_mode=call_mode;
  653. instance->returns=get_output_value_port_count();
  654. instance->node_path=base_path;
  655. instance->input_args = get_input_value_port_count() - ( (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE) ? 1: 0 );
  656. instance->rpc_mode=rpc_call_mode;
  657. instance->validate=validate;
  658. return instance;
  659. }
  660. VisualScriptFunctionCall::TypeGuess VisualScriptFunctionCall::guess_output_type(TypeGuess* p_inputs, int p_output) const {
  661. if (p_output==0 && call_mode==CALL_MODE_INSTANCE) {
  662. return p_inputs[0];
  663. }
  664. return VisualScriptNode::guess_output_type(p_inputs,p_output);
  665. }
  666. VisualScriptFunctionCall::VisualScriptFunctionCall() {
  667. validate=true;
  668. call_mode=CALL_MODE_SELF;
  669. basic_type=Variant::NIL;
  670. use_default_args=0;
  671. base_type="Object";
  672. rpc_call_mode=RPC_DISABLED;
  673. }
  674. template<VisualScriptFunctionCall::CallMode cmode>
  675. static Ref<VisualScriptNode> create_function_call_node(const String& p_name) {
  676. Ref<VisualScriptFunctionCall> node;
  677. node.instance();
  678. node->set_call_mode(cmode);
  679. return node;
  680. }
  681. //////////////////////////////////////////
  682. ////////////////SET//////////////////////
  683. //////////////////////////////////////////
  684. static const char* event_type_names[InputEvent::TYPE_MAX]={
  685. "None",
  686. "Key",
  687. "MouseMotion",
  688. "MouseButton",
  689. "JoystickMotion",
  690. "JoystickButton",
  691. "ScreenTouch",
  692. "ScreenDrag",
  693. "Action"
  694. };
  695. int VisualScriptPropertySet::get_output_sequence_port_count() const {
  696. return call_mode!=CALL_MODE_BASIC_TYPE ? 1 : 0;
  697. }
  698. bool VisualScriptPropertySet::has_input_sequence_port() const{
  699. return call_mode!=CALL_MODE_BASIC_TYPE ? true : false;
  700. }
  701. Node *VisualScriptPropertySet::_get_base_node() const {
  702. #ifdef TOOLS_ENABLED
  703. Ref<Script> script = get_visual_script();
  704. if (!script.is_valid())
  705. return NULL;
  706. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  707. if (!main_loop)
  708. return NULL;
  709. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  710. if (!scene_tree)
  711. return NULL;
  712. Node *edited_scene = scene_tree->get_edited_scene_root();
  713. if (!edited_scene)
  714. return NULL;
  715. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  716. if (!script_node)
  717. return NULL;
  718. if (!script_node->has_node(base_path))
  719. return NULL;
  720. Node *path_to = script_node->get_node(base_path);
  721. return path_to;
  722. #else
  723. return NULL;
  724. #endif
  725. }
  726. StringName VisualScriptPropertySet::_get_base_type() const {
  727. if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid())
  728. return get_visual_script()->get_instance_base_type();
  729. else if (call_mode==CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  730. Node *path = _get_base_node();
  731. if (path)
  732. return path->get_type();
  733. }
  734. return base_type;
  735. }
  736. int VisualScriptPropertySet::get_input_value_port_count() const{
  737. int pc = (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE)?2:1;
  738. return pc;
  739. }
  740. int VisualScriptPropertySet::get_output_value_port_count() const{
  741. return (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE) ? 1 : 0;
  742. }
  743. String VisualScriptPropertySet::get_output_sequence_port_text(int p_port) const {
  744. return String();
  745. }
  746. PropertyInfo VisualScriptPropertySet::get_input_value_port_info(int p_idx) const{
  747. if (call_mode==CALL_MODE_INSTANCE || call_mode==CALL_MODE_BASIC_TYPE) {
  748. if (p_idx==0) {
  749. PropertyInfo pi;
  750. pi.type=(call_mode==CALL_MODE_INSTANCE?Variant::OBJECT:basic_type);
  751. pi.name=(call_mode==CALL_MODE_INSTANCE?String("instance"):Variant::get_type_name(basic_type).to_lower());
  752. return pi;
  753. } else {
  754. p_idx--;
  755. }
  756. }
  757. PropertyInfo pinfo=type_cache;
  758. pinfo.name="value";
  759. return pinfo;
  760. }
  761. PropertyInfo VisualScriptPropertySet::get_output_value_port_info(int p_idx) const{
  762. if (call_mode==CALL_MODE_BASIC_TYPE) {
  763. return PropertyInfo(basic_type,"out");
  764. } else if (call_mode==CALL_MODE_INSTANCE) {
  765. return PropertyInfo(Variant::OBJECT,"pass");
  766. } else {
  767. return PropertyInfo();
  768. }
  769. }
  770. String VisualScriptPropertySet::get_caption() const {
  771. static const char*cname[4]= {
  772. "SelfSet",
  773. "NodeSet",
  774. "InstanceSet",
  775. "BasicSet"
  776. };
  777. return cname[call_mode];
  778. }
  779. String VisualScriptPropertySet::get_text() const {
  780. String prop;
  781. if (call_mode==CALL_MODE_BASIC_TYPE)
  782. prop=Variant::get_type_name(basic_type)+"."+property;
  783. else if (call_mode==CALL_MODE_NODE_PATH)
  784. prop=String(base_path)+":"+property;
  785. else if (call_mode==CALL_MODE_SELF)
  786. prop=property;
  787. else if (call_mode==CALL_MODE_INSTANCE)
  788. prop=String(base_type)+":"+property;
  789. return prop;
  790. }
  791. void VisualScriptPropertySet::_update_base_type() {
  792. //cache it because this information may not be available on load
  793. if (call_mode==CALL_MODE_NODE_PATH) {
  794. Node* node=_get_base_node();
  795. if (node) {
  796. base_type=node->get_type();
  797. }
  798. } else if (call_mode==CALL_MODE_SELF) {
  799. if (get_visual_script().is_valid()) {
  800. base_type=get_visual_script()->get_instance_base_type();
  801. }
  802. }
  803. }
  804. void VisualScriptPropertySet::set_basic_type(Variant::Type p_type) {
  805. if (basic_type==p_type)
  806. return;
  807. basic_type=p_type;
  808. _change_notify();
  809. _update_base_type();
  810. ports_changed_notify();
  811. }
  812. Variant::Type VisualScriptPropertySet::get_basic_type() const{
  813. return basic_type;
  814. }
  815. void VisualScriptPropertySet::set_event_type(InputEvent::Type p_type) {
  816. if (event_type==p_type)
  817. return;
  818. event_type=p_type;
  819. if (call_mode==CALL_MODE_BASIC_TYPE) {
  820. _update_cache();
  821. }
  822. _change_notify();
  823. _update_base_type();
  824. ports_changed_notify();
  825. }
  826. InputEvent::Type VisualScriptPropertySet::get_event_type() const{
  827. return event_type;
  828. }
  829. void VisualScriptPropertySet::set_base_type(const StringName& p_type) {
  830. if (base_type==p_type)
  831. return;
  832. base_type=p_type;
  833. _change_notify();
  834. ports_changed_notify();
  835. }
  836. StringName VisualScriptPropertySet::get_base_type() const{
  837. return base_type;
  838. }
  839. void VisualScriptPropertySet::set_base_script(const String& p_path) {
  840. if (base_script==p_path)
  841. return;
  842. base_script=p_path;
  843. _change_notify();
  844. ports_changed_notify();
  845. }
  846. String VisualScriptPropertySet::get_base_script() const {
  847. return base_script;
  848. }
  849. void VisualScriptPropertySet::_update_cache() {
  850. if (!OS::get_singleton()->get_main_loop())
  851. return;
  852. if (!OS::get_singleton()->get_main_loop()->cast_to<SceneTree>())
  853. return;
  854. if (!OS::get_singleton()->get_main_loop()->cast_to<SceneTree>()->is_editor_hint()) //only update cache if editor exists, it's pointless otherwise
  855. return;
  856. if (call_mode==CALL_MODE_BASIC_TYPE) {
  857. //not super efficient..
  858. Variant v;
  859. if (basic_type==Variant::INPUT_EVENT) {
  860. InputEvent ev;
  861. ev.type=event_type;
  862. v=ev;
  863. } else {
  864. Variant::CallError ce;
  865. v = Variant::construct(basic_type,NULL,0,ce);
  866. }
  867. List<PropertyInfo> pinfo;
  868. v.get_property_list(&pinfo);
  869. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  870. if (E->get().name==property) {
  871. type_cache=E->get();
  872. }
  873. }
  874. } else {
  875. StringName type;
  876. Ref<Script> script;
  877. Node *node=NULL;
  878. if (call_mode==CALL_MODE_NODE_PATH) {
  879. node=_get_base_node();
  880. if (node) {
  881. type=node->get_type();
  882. base_type=type; //cache, too
  883. script = node->get_script();
  884. }
  885. } else if (call_mode==CALL_MODE_SELF) {
  886. if (get_visual_script().is_valid()) {
  887. type=get_visual_script()->get_instance_base_type();
  888. base_type=type; //cache, too
  889. script=get_visual_script();
  890. }
  891. } else if (call_mode==CALL_MODE_INSTANCE) {
  892. type=base_type;
  893. if (base_script!=String()) {
  894. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  895. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  896. }
  897. if (ResourceCache::has(base_script)) {
  898. script = Ref<Resource>( ResourceCache::get(base_script) );
  899. } else {
  900. return;
  901. }
  902. }
  903. }
  904. List<PropertyInfo> pinfo;
  905. if (node) {
  906. node->get_property_list(&pinfo);
  907. } else {
  908. ObjectTypeDB::get_property_list(type,&pinfo);
  909. }
  910. if (script.is_valid()) {
  911. script->get_script_property_list(&pinfo);
  912. }
  913. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  914. if (E->get().name==property) {
  915. type_cache=E->get();
  916. return;
  917. }
  918. }
  919. }
  920. }
  921. void VisualScriptPropertySet::set_property(const StringName& p_type){
  922. if (property==p_type)
  923. return;
  924. property=p_type;
  925. _update_cache();
  926. _change_notify();
  927. ports_changed_notify();
  928. }
  929. StringName VisualScriptPropertySet::get_property() const {
  930. return property;
  931. }
  932. void VisualScriptPropertySet::set_base_path(const NodePath& p_type) {
  933. if (base_path==p_type)
  934. return;
  935. base_path=p_type;
  936. _update_base_type();
  937. _change_notify();
  938. ports_changed_notify();
  939. }
  940. NodePath VisualScriptPropertySet::get_base_path() const {
  941. return base_path;
  942. }
  943. void VisualScriptPropertySet::set_call_mode(CallMode p_mode) {
  944. if (call_mode==p_mode)
  945. return;
  946. call_mode=p_mode;
  947. _update_base_type();
  948. _change_notify();
  949. ports_changed_notify();
  950. }
  951. VisualScriptPropertySet::CallMode VisualScriptPropertySet::get_call_mode() const {
  952. return call_mode;
  953. }
  954. void VisualScriptPropertySet::_set_type_cache(const Dictionary &p_type) {
  955. type_cache=PropertyInfo::from_dict(p_type);
  956. }
  957. Dictionary VisualScriptPropertySet::_get_type_cache() const {
  958. return type_cache;
  959. }
  960. void VisualScriptPropertySet::_validate_property(PropertyInfo& property) const {
  961. if (property.name=="property/base_type") {
  962. if (call_mode!=CALL_MODE_INSTANCE) {
  963. property.usage=PROPERTY_USAGE_NOEDITOR;
  964. }
  965. }
  966. if (property.name=="property/base_script") {
  967. if (call_mode!=CALL_MODE_INSTANCE) {
  968. property.usage=0;
  969. }
  970. }
  971. if (property.name=="property/basic_type") {
  972. if (call_mode!=CALL_MODE_BASIC_TYPE) {
  973. property.usage=0;
  974. }
  975. }
  976. if (property.name=="property/event_type") {
  977. if (call_mode!=CALL_MODE_BASIC_TYPE || basic_type!=Variant::INPUT_EVENT) {
  978. property.usage=0;
  979. }
  980. }
  981. if (property.name=="property/node_path") {
  982. if (call_mode!=CALL_MODE_NODE_PATH) {
  983. property.usage=0;
  984. } else {
  985. Node *bnode = _get_base_node();
  986. if (bnode) {
  987. property.hint_string=bnode->get_path(); //convert to loong string
  988. } else {
  989. }
  990. }
  991. }
  992. if (property.name=="property/property") {
  993. if (call_mode==CALL_MODE_BASIC_TYPE) {
  994. property.hint=PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  995. property.hint_string=Variant::get_type_name(basic_type);
  996. } else if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid()) {
  997. property.hint=PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  998. property.hint_string=itos(get_visual_script()->get_instance_ID());
  999. } else if (call_mode==CALL_MODE_INSTANCE) {
  1000. property.hint=PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1001. property.hint_string=base_type;
  1002. if (base_script!=String()) {
  1003. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1004. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1005. }
  1006. if (ResourceCache::has(base_script)) {
  1007. Ref<Script> script = Ref<Resource>( ResourceCache::get(base_script) );
  1008. if (script.is_valid()) {
  1009. property.hint=PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1010. property.hint_string=itos(script->get_instance_ID());
  1011. }
  1012. }
  1013. }
  1014. } else if (call_mode==CALL_MODE_NODE_PATH) {
  1015. Node *node = _get_base_node();
  1016. if (node) {
  1017. property.hint=PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  1018. property.hint_string=itos(node->get_instance_ID());
  1019. } else {
  1020. property.hint=PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1021. property.hint_string=get_base_type();
  1022. }
  1023. }
  1024. }
  1025. }
  1026. void VisualScriptPropertySet::_bind_methods() {
  1027. ObjectTypeDB::bind_method(_MD("set_base_type","base_type"),&VisualScriptPropertySet::set_base_type);
  1028. ObjectTypeDB::bind_method(_MD("get_base_type"),&VisualScriptPropertySet::get_base_type);
  1029. ObjectTypeDB::bind_method(_MD("set_base_script","base_script"),&VisualScriptPropertySet::set_base_script);
  1030. ObjectTypeDB::bind_method(_MD("get_base_script"),&VisualScriptPropertySet::get_base_script);
  1031. ObjectTypeDB::bind_method(_MD("set_basic_type","basic_type"),&VisualScriptPropertySet::set_basic_type);
  1032. ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptPropertySet::get_basic_type);
  1033. ObjectTypeDB::bind_method(_MD("_set_type_cache","type_cache"),&VisualScriptPropertySet::_set_type_cache);
  1034. ObjectTypeDB::bind_method(_MD("_get_type_cache"),&VisualScriptPropertySet::_get_type_cache);
  1035. ObjectTypeDB::bind_method(_MD("set_event_type","event_type"),&VisualScriptPropertySet::set_event_type);
  1036. ObjectTypeDB::bind_method(_MD("get_event_type"),&VisualScriptPropertySet::get_event_type);
  1037. ObjectTypeDB::bind_method(_MD("set_property","property"),&VisualScriptPropertySet::set_property);
  1038. ObjectTypeDB::bind_method(_MD("get_property"),&VisualScriptPropertySet::get_property);
  1039. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptPropertySet::set_call_mode);
  1040. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptPropertySet::get_call_mode);
  1041. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptPropertySet::set_base_path);
  1042. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptPropertySet::get_base_path);
  1043. String bt;
  1044. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1045. if (i>0)
  1046. bt+=",";
  1047. bt+=Variant::get_type_name(Variant::Type(i));
  1048. }
  1049. String et;
  1050. for(int i=0;i<InputEvent::TYPE_MAX;i++) {
  1051. if (i>0)
  1052. et+=",";
  1053. et+=event_type_names[i];
  1054. }
  1055. List<String> script_extensions;
  1056. for(int i=0;i<ScriptServer::get_language_count();i++) {
  1057. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  1058. }
  1059. String script_ext_hint;
  1060. for (List<String>::Element *E=script_extensions.front();E;E=E->next()) {
  1061. if (script_ext_hint!=String())
  1062. script_ext_hint+=",";
  1063. script_ext_hint+="*."+E->get();
  1064. }
  1065. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/set_mode",PROPERTY_HINT_ENUM,"Self,Node Path,Instance,Basic Type"),_SCS("set_call_mode"),_SCS("get_call_mode"));
  1066. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_type",PROPERTY_HINT_TYPE_STRING,"Object"),_SCS("set_base_type"),_SCS("get_base_type"));
  1067. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_script",PROPERTY_HINT_FILE,script_ext_hint),_SCS("set_base_script"),_SCS("get_base_script"));
  1068. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/type_cache",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_type_cache"),_SCS("_get_type_cache"));
  1069. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/basic_type",PROPERTY_HINT_ENUM,bt),_SCS("set_basic_type"),_SCS("get_basic_type"));
  1070. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/event_type",PROPERTY_HINT_ENUM,et),_SCS("set_event_type"),_SCS("get_event_type"));
  1071. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"property/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  1072. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/property"),_SCS("set_property"),_SCS("get_property"));
  1073. BIND_CONSTANT( CALL_MODE_SELF );
  1074. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  1075. BIND_CONSTANT( CALL_MODE_INSTANCE);
  1076. }
  1077. class VisualScriptNodeInstancePropertySet : public VisualScriptNodeInstance {
  1078. public:
  1079. VisualScriptPropertySet::CallMode call_mode;
  1080. NodePath node_path;
  1081. StringName property;
  1082. VisualScriptPropertySet *node;
  1083. VisualScriptInstance *instance;
  1084. //virtual int get_working_memory_size() const { return 0; }
  1085. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1086. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1087. 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) {
  1088. switch(call_mode) {
  1089. case VisualScriptPropertySet::CALL_MODE_SELF: {
  1090. Object *object=instance->get_owner_ptr();
  1091. bool valid;
  1092. object->set(property,*p_inputs[0],&valid);
  1093. if (!valid) {
  1094. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1095. r_error_str="Invalid set value '"+String(*p_inputs[0])+"' on property '"+String(property)+"' of type "+object->get_type();
  1096. }
  1097. } break;
  1098. case VisualScriptPropertySet::CALL_MODE_NODE_PATH: {
  1099. Node* node = instance->get_owner_ptr()->cast_to<Node>();
  1100. if (!node) {
  1101. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1102. r_error_str="Base object is not a Node!";
  1103. return 0;
  1104. }
  1105. Node* another = node->get_node(node_path);
  1106. if (!node) {
  1107. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1108. r_error_str="Path does not lead Node!";
  1109. return 0;
  1110. }
  1111. bool valid;
  1112. another->set(property,*p_inputs[0],&valid);
  1113. if (!valid) {
  1114. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1115. r_error_str="Invalid set value '"+String(*p_inputs[0])+"' on property '"+String(property)+"' of type "+another->get_type();
  1116. }
  1117. } break;
  1118. case VisualScriptPropertySet::CALL_MODE_INSTANCE:
  1119. case VisualScriptPropertySet::CALL_MODE_BASIC_TYPE: {
  1120. Variant v = *p_inputs[0];
  1121. bool valid;
  1122. v.set(property,*p_inputs[1],&valid);
  1123. if (!valid) {
  1124. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1125. r_error_str="Invalid set value '"+String(*p_inputs[1])+"' ("+Variant::get_type_name(p_inputs[1]->get_type())+") on property '"+String(property)+"' of type "+Variant::get_type_name(v.get_type());
  1126. }
  1127. *p_outputs[0]=v;
  1128. } break;
  1129. }
  1130. return 0;
  1131. }
  1132. };
  1133. VisualScriptNodeInstance* VisualScriptPropertySet::instance(VisualScriptInstance* p_instance) {
  1134. VisualScriptNodeInstancePropertySet * instance = memnew(VisualScriptNodeInstancePropertySet );
  1135. instance->node=this;
  1136. instance->instance=p_instance;
  1137. instance->property=property;
  1138. instance->call_mode=call_mode;
  1139. instance->node_path=base_path;
  1140. return instance;
  1141. }
  1142. VisualScriptPropertySet::TypeGuess VisualScriptPropertySet::guess_output_type(TypeGuess* p_inputs, int p_output) const {
  1143. if (p_output==0 && call_mode==CALL_MODE_INSTANCE) {
  1144. return p_inputs[0];
  1145. }
  1146. return VisualScriptNode::guess_output_type(p_inputs,p_output);
  1147. }
  1148. VisualScriptPropertySet::VisualScriptPropertySet() {
  1149. call_mode=CALL_MODE_SELF;
  1150. base_type="Object";
  1151. basic_type=Variant::NIL;
  1152. event_type=InputEvent::NONE;
  1153. }
  1154. template<VisualScriptPropertySet::CallMode cmode>
  1155. static Ref<VisualScriptNode> create_property_set_node(const String& p_name) {
  1156. Ref<VisualScriptPropertySet> node;
  1157. node.instance();
  1158. node->set_call_mode(cmode);
  1159. return node;
  1160. }
  1161. //////////////////////////////////////////
  1162. ////////////////GET//////////////////////
  1163. //////////////////////////////////////////
  1164. int VisualScriptPropertyGet::get_output_sequence_port_count() const {
  1165. return 0;// (call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?0:1;
  1166. }
  1167. bool VisualScriptPropertyGet::has_input_sequence_port() const{
  1168. return false;//(call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?false:true;
  1169. }
  1170. void VisualScriptPropertyGet::_update_base_type() {
  1171. //cache it because this information may not be available on load
  1172. if (call_mode==CALL_MODE_NODE_PATH) {
  1173. Node* node=_get_base_node();
  1174. if (node) {
  1175. base_type=node->get_type();
  1176. }
  1177. } else if (call_mode==CALL_MODE_SELF) {
  1178. if (get_visual_script().is_valid()) {
  1179. base_type=get_visual_script()->get_instance_base_type();
  1180. }
  1181. }
  1182. }
  1183. Node *VisualScriptPropertyGet::_get_base_node() const {
  1184. #ifdef TOOLS_ENABLED
  1185. Ref<Script> script = get_visual_script();
  1186. if (!script.is_valid())
  1187. return NULL;
  1188. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  1189. if (!main_loop)
  1190. return NULL;
  1191. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  1192. if (!scene_tree)
  1193. return NULL;
  1194. Node *edited_scene = scene_tree->get_edited_scene_root();
  1195. if (!edited_scene)
  1196. return NULL;
  1197. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  1198. if (!script_node)
  1199. return NULL;
  1200. if (!script_node->has_node(base_path))
  1201. return NULL;
  1202. Node *path_to = script_node->get_node(base_path);
  1203. return path_to;
  1204. #else
  1205. return NULL;
  1206. #endif
  1207. }
  1208. StringName VisualScriptPropertyGet::_get_base_type() const {
  1209. if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid())
  1210. return get_visual_script()->get_instance_base_type();
  1211. else if (call_mode==CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  1212. Node *path = _get_base_node();
  1213. if (path)
  1214. return path->get_type();
  1215. }
  1216. return base_type;
  1217. }
  1218. int VisualScriptPropertyGet::get_input_value_port_count() const{
  1219. return (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE)?1:0;
  1220. }
  1221. int VisualScriptPropertyGet::get_output_value_port_count() const{
  1222. return 1;
  1223. }
  1224. String VisualScriptPropertyGet::get_output_sequence_port_text(int p_port) const {
  1225. return String();
  1226. }
  1227. PropertyInfo VisualScriptPropertyGet::get_input_value_port_info(int p_idx) const{
  1228. if (call_mode==CALL_MODE_INSTANCE || call_mode==CALL_MODE_BASIC_TYPE) {
  1229. if (p_idx==0) {
  1230. PropertyInfo pi;
  1231. pi.type=(call_mode==CALL_MODE_INSTANCE?Variant::OBJECT:basic_type);
  1232. pi.name=(call_mode==CALL_MODE_INSTANCE?String("instance"):Variant::get_type_name(basic_type).to_lower());
  1233. return pi;
  1234. } else {
  1235. p_idx--;
  1236. }
  1237. }
  1238. return PropertyInfo();
  1239. }
  1240. PropertyInfo VisualScriptPropertyGet::get_output_value_port_info(int p_idx) const{
  1241. return PropertyInfo(type_cache,"value");
  1242. }
  1243. String VisualScriptPropertyGet::get_caption() const {
  1244. static const char*cname[4]= {
  1245. "SelfGet",
  1246. "NodeGet",
  1247. "InstanceGet",
  1248. "BasicGet"
  1249. };
  1250. return cname[call_mode];
  1251. }
  1252. String VisualScriptPropertyGet::get_text() const {
  1253. String prop;
  1254. if (call_mode==CALL_MODE_BASIC_TYPE)
  1255. prop=Variant::get_type_name(basic_type)+"."+property;
  1256. else if (call_mode==CALL_MODE_NODE_PATH)
  1257. prop=String(base_path)+":"+property;
  1258. else if (call_mode==CALL_MODE_SELF)
  1259. prop=property;
  1260. else if (call_mode==CALL_MODE_INSTANCE)
  1261. prop=String(base_type)+":"+property;
  1262. return prop;
  1263. }
  1264. void VisualScriptPropertyGet::set_base_type(const StringName& p_type) {
  1265. if (base_type==p_type)
  1266. return;
  1267. base_type=p_type;
  1268. _change_notify();
  1269. ports_changed_notify();
  1270. }
  1271. StringName VisualScriptPropertyGet::get_base_type() const{
  1272. return base_type;
  1273. }
  1274. void VisualScriptPropertyGet::set_base_script(const String& p_path) {
  1275. if (base_script==p_path)
  1276. return;
  1277. base_script=p_path;
  1278. _change_notify();
  1279. ports_changed_notify();
  1280. }
  1281. String VisualScriptPropertyGet::get_base_script() const {
  1282. return base_script;
  1283. }
  1284. void VisualScriptPropertyGet::_update_cache() {
  1285. if (call_mode==CALL_MODE_BASIC_TYPE) {
  1286. //not super efficient..
  1287. Variant v;
  1288. if (basic_type==Variant::INPUT_EVENT) {
  1289. InputEvent ev;
  1290. ev.type=event_type;
  1291. v=ev;
  1292. } else {
  1293. Variant::CallError ce;
  1294. v = Variant::construct(basic_type,NULL,0,ce);
  1295. }
  1296. List<PropertyInfo> pinfo;
  1297. v.get_property_list(&pinfo);
  1298. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  1299. if (E->get().name==property) {
  1300. type_cache=E->get().type;
  1301. return;
  1302. }
  1303. }
  1304. } else {
  1305. StringName type;
  1306. Ref<Script> script;
  1307. Node *node=NULL;
  1308. if (call_mode==CALL_MODE_NODE_PATH) {
  1309. node=_get_base_node();
  1310. if (node) {
  1311. type=node->get_type();
  1312. base_type=type; //cache, too
  1313. script = node->get_script();
  1314. }
  1315. } else if (call_mode==CALL_MODE_SELF) {
  1316. if (get_visual_script().is_valid()) {
  1317. type=get_visual_script()->get_instance_base_type();
  1318. base_type=type; //cache, too
  1319. script=get_visual_script();
  1320. }
  1321. } else if (call_mode==CALL_MODE_INSTANCE) {
  1322. type=base_type;
  1323. if (base_script!=String()) {
  1324. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1325. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1326. }
  1327. if (ResourceCache::has(base_script)) {
  1328. script = Ref<Resource>( ResourceCache::get(base_script) );
  1329. } else {
  1330. return;
  1331. }
  1332. }
  1333. }
  1334. bool valid=false;
  1335. Variant::Type type_ret;
  1336. type_ret=ObjectTypeDB::get_property_type(base_type,property,&valid);
  1337. if (valid) {
  1338. type_cache=type_ret;
  1339. return; //all dandy
  1340. }
  1341. if (node) {
  1342. Variant prop = node->get(property,&valid);
  1343. if (valid) {
  1344. type_cache=prop.get_type();
  1345. return; //all dandy again
  1346. }
  1347. }
  1348. if (script.is_valid()) {
  1349. type_ret=script->get_static_property_type(property,&valid);
  1350. if (valid) {
  1351. type_cache=type_ret;
  1352. return; //all dandy
  1353. }
  1354. }
  1355. }
  1356. }
  1357. void VisualScriptPropertyGet::set_property(const StringName& p_type){
  1358. if (property==p_type)
  1359. return;
  1360. property=p_type;
  1361. _update_cache();
  1362. _change_notify();
  1363. ports_changed_notify();
  1364. }
  1365. StringName VisualScriptPropertyGet::get_property() const {
  1366. return property;
  1367. }
  1368. void VisualScriptPropertyGet::set_base_path(const NodePath& p_type) {
  1369. if (base_path==p_type)
  1370. return;
  1371. base_path=p_type;
  1372. _change_notify();
  1373. _update_base_type();
  1374. ports_changed_notify();
  1375. }
  1376. NodePath VisualScriptPropertyGet::get_base_path() const {
  1377. return base_path;
  1378. }
  1379. void VisualScriptPropertyGet::set_call_mode(CallMode p_mode) {
  1380. if (call_mode==p_mode)
  1381. return;
  1382. call_mode=p_mode;
  1383. _change_notify();
  1384. _update_base_type();
  1385. ports_changed_notify();
  1386. }
  1387. VisualScriptPropertyGet::CallMode VisualScriptPropertyGet::get_call_mode() const {
  1388. return call_mode;
  1389. }
  1390. void VisualScriptPropertyGet::set_basic_type(Variant::Type p_type) {
  1391. if (basic_type==p_type)
  1392. return;
  1393. basic_type=p_type;
  1394. _change_notify();
  1395. ports_changed_notify();
  1396. }
  1397. Variant::Type VisualScriptPropertyGet::get_basic_type() const{
  1398. return basic_type;
  1399. }
  1400. void VisualScriptPropertyGet::set_event_type(InputEvent::Type p_type) {
  1401. if (event_type==p_type)
  1402. return;
  1403. event_type=p_type;
  1404. if(call_mode==CALL_MODE_BASIC_TYPE) {
  1405. _update_cache();
  1406. }
  1407. _change_notify();
  1408. _update_base_type();
  1409. ports_changed_notify();
  1410. }
  1411. InputEvent::Type VisualScriptPropertyGet::get_event_type() const{
  1412. return event_type;
  1413. }
  1414. void VisualScriptPropertyGet::_set_type_cache(Variant::Type p_type) {
  1415. type_cache=p_type;
  1416. }
  1417. Variant::Type VisualScriptPropertyGet::_get_type_cache() const {
  1418. return type_cache;
  1419. }
  1420. void VisualScriptPropertyGet::_validate_property(PropertyInfo& property) const {
  1421. if (property.name=="property/base_type") {
  1422. if (call_mode!=CALL_MODE_INSTANCE) {
  1423. property.usage=PROPERTY_USAGE_NOEDITOR;
  1424. }
  1425. }
  1426. if (property.name=="property/base_script") {
  1427. if (call_mode!=CALL_MODE_INSTANCE) {
  1428. property.usage=0;
  1429. }
  1430. }
  1431. if (property.name=="property/basic_type") {
  1432. if (call_mode!=CALL_MODE_BASIC_TYPE) {
  1433. property.usage=0;
  1434. }
  1435. }
  1436. if (property.name=="property/event_type") {
  1437. if (call_mode!=CALL_MODE_BASIC_TYPE || basic_type!=Variant::INPUT_EVENT) {
  1438. property.usage=0;
  1439. }
  1440. }
  1441. if (property.name=="property/node_path") {
  1442. if (call_mode!=CALL_MODE_NODE_PATH) {
  1443. property.usage=0;
  1444. } else {
  1445. Node *bnode = _get_base_node();
  1446. if (bnode) {
  1447. property.hint_string=bnode->get_path(); //convert to loong string
  1448. } else {
  1449. }
  1450. }
  1451. }
  1452. if (property.name=="property/property") {
  1453. if (call_mode==CALL_MODE_BASIC_TYPE) {
  1454. property.hint=PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  1455. property.hint_string=Variant::get_type_name(basic_type);
  1456. } else if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid()) {
  1457. property.hint=PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1458. property.hint_string=itos(get_visual_script()->get_instance_ID());
  1459. } else if (call_mode==CALL_MODE_INSTANCE) {
  1460. property.hint=PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1461. property.hint_string=base_type;
  1462. if (base_script!=String()) {
  1463. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1464. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1465. }
  1466. if (ResourceCache::has(base_script)) {
  1467. Ref<Script> script = Ref<Resource>( ResourceCache::get(base_script) );
  1468. if (script.is_valid()) {
  1469. property.hint=PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1470. property.hint_string=itos(script->get_instance_ID());
  1471. }
  1472. }
  1473. }
  1474. } else if (call_mode==CALL_MODE_NODE_PATH) {
  1475. Node *node = _get_base_node();
  1476. if (node) {
  1477. property.hint=PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  1478. property.hint_string=itos(node->get_instance_ID());
  1479. } else {
  1480. property.hint=PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1481. property.hint_string=get_base_type();
  1482. }
  1483. }
  1484. }
  1485. }
  1486. void VisualScriptPropertyGet::_bind_methods() {
  1487. ObjectTypeDB::bind_method(_MD("set_base_type","base_type"),&VisualScriptPropertyGet::set_base_type);
  1488. ObjectTypeDB::bind_method(_MD("get_base_type"),&VisualScriptPropertyGet::get_base_type);
  1489. ObjectTypeDB::bind_method(_MD("set_base_script","base_script"),&VisualScriptPropertyGet::set_base_script);
  1490. ObjectTypeDB::bind_method(_MD("get_base_script"),&VisualScriptPropertyGet::get_base_script);
  1491. ObjectTypeDB::bind_method(_MD("set_basic_type","basic_type"),&VisualScriptPropertyGet::set_basic_type);
  1492. ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptPropertyGet::get_basic_type);
  1493. ObjectTypeDB::bind_method(_MD("_set_type_cache","type_cache"),&VisualScriptPropertyGet::_set_type_cache);
  1494. ObjectTypeDB::bind_method(_MD("_get_type_cache"),&VisualScriptPropertyGet::_get_type_cache);
  1495. ObjectTypeDB::bind_method(_MD("set_event_type","event_type"),&VisualScriptPropertyGet::set_event_type);
  1496. ObjectTypeDB::bind_method(_MD("get_event_type"),&VisualScriptPropertyGet::get_event_type);
  1497. ObjectTypeDB::bind_method(_MD("set_property","property"),&VisualScriptPropertyGet::set_property);
  1498. ObjectTypeDB::bind_method(_MD("get_property"),&VisualScriptPropertyGet::get_property);
  1499. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptPropertyGet::set_call_mode);
  1500. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptPropertyGet::get_call_mode);
  1501. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptPropertyGet::set_base_path);
  1502. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptPropertyGet::get_base_path);
  1503. String bt;
  1504. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1505. if (i>0)
  1506. bt+=",";
  1507. bt+=Variant::get_type_name(Variant::Type(i));
  1508. }
  1509. String et;
  1510. for(int i=0;i<InputEvent::TYPE_MAX;i++) {
  1511. if (i>0)
  1512. et+=",";
  1513. et+=event_type_names[i];
  1514. }
  1515. List<String> script_extensions;
  1516. for(int i=0;i<ScriptServer::get_language_count();i++) {
  1517. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  1518. }
  1519. String script_ext_hint;
  1520. for (List<String>::Element *E=script_extensions.front();E;E=E->next()) {
  1521. if (script_ext_hint!=String())
  1522. script_ext_hint+=",";
  1523. script_ext_hint+="."+E->get();
  1524. }
  1525. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/set_mode",PROPERTY_HINT_ENUM,"Self,Node Path,Instance,Basic Type"),_SCS("set_call_mode"),_SCS("get_call_mode"));
  1526. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_type",PROPERTY_HINT_TYPE_STRING,"Object"),_SCS("set_base_type"),_SCS("get_base_type"));
  1527. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_script",PROPERTY_HINT_FILE,script_ext_hint),_SCS("set_base_script"),_SCS("get_base_script"));
  1528. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/type_cache",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_type_cache"),_SCS("_get_type_cache"));
  1529. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/basic_type",PROPERTY_HINT_ENUM,bt),_SCS("set_basic_type"),_SCS("get_basic_type"));
  1530. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/event_type",PROPERTY_HINT_ENUM,et),_SCS("set_event_type"),_SCS("get_event_type"));
  1531. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"property/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  1532. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/property"),_SCS("set_property"),_SCS("get_property"));
  1533. BIND_CONSTANT( CALL_MODE_SELF );
  1534. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  1535. BIND_CONSTANT( CALL_MODE_INSTANCE);
  1536. }
  1537. class VisualScriptNodeInstancePropertyGet : public VisualScriptNodeInstance {
  1538. public:
  1539. VisualScriptPropertyGet::CallMode call_mode;
  1540. NodePath node_path;
  1541. StringName property;
  1542. VisualScriptPropertyGet *node;
  1543. VisualScriptInstance *instance;
  1544. 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) {
  1545. switch(call_mode) {
  1546. case VisualScriptPropertyGet::CALL_MODE_SELF: {
  1547. Object *object=instance->get_owner_ptr();
  1548. bool valid;
  1549. *p_outputs[0] = object->get(property,&valid);
  1550. if (!valid) {
  1551. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1552. r_error_str=RTR("Invalid index property name.");
  1553. return 0;
  1554. }
  1555. } break;
  1556. case VisualScriptPropertyGet::CALL_MODE_NODE_PATH: {
  1557. Node* node = instance->get_owner_ptr()->cast_to<Node>();
  1558. if (!node) {
  1559. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1560. r_error_str=RTR("Base object is not a Node!");
  1561. return 0;
  1562. }
  1563. Node* another = node->get_node(node_path);
  1564. if (!node) {
  1565. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1566. r_error_str=RTR("Path does not lead Node!");
  1567. return 0;
  1568. }
  1569. bool valid;
  1570. *p_outputs[0] = another->get(property,&valid);
  1571. if (!valid) {
  1572. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1573. r_error_str=vformat(RTR("Invalid index property name '%s' in node %s."),String(property),another->get_name());
  1574. return 0;
  1575. }
  1576. } break;
  1577. default: {
  1578. bool valid;
  1579. Variant v = *p_inputs[0];
  1580. *p_outputs[0] = v.get(property,&valid);
  1581. if (!valid) {
  1582. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1583. r_error_str=RTR("Invalid index property name.");
  1584. }
  1585. };
  1586. }
  1587. return 0;
  1588. }
  1589. };
  1590. VisualScriptNodeInstance* VisualScriptPropertyGet::instance(VisualScriptInstance* p_instance) {
  1591. VisualScriptNodeInstancePropertyGet * instance = memnew(VisualScriptNodeInstancePropertyGet );
  1592. instance->node=this;
  1593. instance->instance=p_instance;
  1594. instance->property=property;
  1595. instance->call_mode=call_mode;
  1596. instance->node_path=base_path;
  1597. return instance;
  1598. }
  1599. VisualScriptPropertyGet::VisualScriptPropertyGet() {
  1600. call_mode=CALL_MODE_SELF;
  1601. base_type="Object";
  1602. basic_type=Variant::NIL;
  1603. event_type=InputEvent::NONE;
  1604. type_cache=Variant::NIL;
  1605. }
  1606. template<VisualScriptPropertyGet::CallMode cmode>
  1607. static Ref<VisualScriptNode> create_property_get_node(const String& p_name) {
  1608. Ref<VisualScriptPropertyGet> node;
  1609. node.instance();
  1610. node->set_call_mode(cmode);
  1611. return node;
  1612. }
  1613. //////////////////////////////////////////
  1614. ////////////////EMIT//////////////////////
  1615. //////////////////////////////////////////
  1616. int VisualScriptEmitSignal::get_output_sequence_port_count() const {
  1617. return 1;
  1618. }
  1619. bool VisualScriptEmitSignal::has_input_sequence_port() const{
  1620. return true;
  1621. }
  1622. int VisualScriptEmitSignal::get_input_value_port_count() const{
  1623. Ref<VisualScript> vs = get_visual_script();
  1624. if (vs.is_valid()) {
  1625. if (!vs->has_custom_signal(name))
  1626. return 0;
  1627. return vs->custom_signal_get_argument_count(name);
  1628. }
  1629. return 0;
  1630. }
  1631. int VisualScriptEmitSignal::get_output_value_port_count() const{
  1632. return 0;
  1633. }
  1634. String VisualScriptEmitSignal::get_output_sequence_port_text(int p_port) const {
  1635. return String();
  1636. }
  1637. PropertyInfo VisualScriptEmitSignal::get_input_value_port_info(int p_idx) const{
  1638. Ref<VisualScript> vs = get_visual_script();
  1639. if (vs.is_valid()) {
  1640. if (!vs->has_custom_signal(name))
  1641. return PropertyInfo();
  1642. return PropertyInfo(vs->custom_signal_get_argument_type(name,p_idx),vs->custom_signal_get_argument_name(name,p_idx));
  1643. }
  1644. return PropertyInfo();
  1645. }
  1646. PropertyInfo VisualScriptEmitSignal::get_output_value_port_info(int p_idx) const{
  1647. return PropertyInfo();
  1648. }
  1649. String VisualScriptEmitSignal::get_caption() const {
  1650. return "EmitSignal";
  1651. }
  1652. String VisualScriptEmitSignal::get_text() const {
  1653. return "emit "+String(name);
  1654. }
  1655. void VisualScriptEmitSignal::set_signal(const StringName& p_type){
  1656. if (name==p_type)
  1657. return;
  1658. name=p_type;
  1659. _change_notify();
  1660. ports_changed_notify();
  1661. }
  1662. StringName VisualScriptEmitSignal::get_signal() const {
  1663. return name;
  1664. }
  1665. void VisualScriptEmitSignal::_validate_property(PropertyInfo& property) const {
  1666. if (property.name=="signal/signal") {
  1667. property.hint=PROPERTY_HINT_ENUM;
  1668. List<StringName> sigs;
  1669. Ref<VisualScript> vs = get_visual_script();
  1670. if (vs.is_valid()) {
  1671. vs->get_custom_signal_list(&sigs);
  1672. }
  1673. String ml;
  1674. for (List<StringName>::Element *E=sigs.front();E;E=E->next()) {
  1675. if (ml!=String())
  1676. ml+=",";
  1677. ml+=E->get();
  1678. }
  1679. property.hint_string=ml;
  1680. }
  1681. }
  1682. void VisualScriptEmitSignal::_bind_methods() {
  1683. ObjectTypeDB::bind_method(_MD("set_signal","name"),&VisualScriptEmitSignal::set_signal);
  1684. ObjectTypeDB::bind_method(_MD("get_signal"),&VisualScriptEmitSignal::get_signal);
  1685. ADD_PROPERTY(PropertyInfo(Variant::STRING,"signal/signal"),_SCS("set_signal"),_SCS("get_signal"));
  1686. }
  1687. class VisualScriptNodeInstanceEmitSignal : public VisualScriptNodeInstance {
  1688. public:
  1689. VisualScriptEmitSignal *node;
  1690. VisualScriptInstance *instance;
  1691. int argcount;
  1692. StringName name;
  1693. //virtual int get_working_memory_size() const { return 0; }
  1694. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1695. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1696. 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) {
  1697. Object *obj = instance->get_owner_ptr();
  1698. obj->emit_signal(name,p_inputs,argcount);
  1699. return 0;
  1700. }
  1701. };
  1702. VisualScriptNodeInstance* VisualScriptEmitSignal::instance(VisualScriptInstance* p_instance) {
  1703. VisualScriptNodeInstanceEmitSignal * instance = memnew(VisualScriptNodeInstanceEmitSignal );
  1704. instance->node=this;
  1705. instance->instance=p_instance;
  1706. instance->name=name;
  1707. instance->argcount=get_input_value_port_count();
  1708. return instance;
  1709. }
  1710. VisualScriptEmitSignal::VisualScriptEmitSignal() {
  1711. }
  1712. static Ref<VisualScriptNode> create_basic_type_call_node(const String& p_name) {
  1713. Vector<String> path = p_name.split("/");
  1714. ERR_FAIL_COND_V(path.size()<4,Ref<VisualScriptNode>());
  1715. String base_type = path[2];
  1716. String method = path[3];
  1717. Ref<VisualScriptFunctionCall> node;
  1718. node.instance();
  1719. Variant::Type type=Variant::VARIANT_MAX;
  1720. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1721. if (Variant::get_type_name(Variant::Type(i))==base_type) {
  1722. type=Variant::Type(i);
  1723. break;
  1724. }
  1725. }
  1726. ERR_FAIL_COND_V(type==Variant::VARIANT_MAX,Ref<VisualScriptNode>());
  1727. node->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  1728. node->set_basic_type(type);
  1729. node->set_function(method);
  1730. return node;
  1731. }
  1732. void register_visual_script_func_nodes() {
  1733. VisualScriptLanguage::singleton->add_register_func("functions/call",create_node_generic<VisualScriptFunctionCall>);
  1734. VisualScriptLanguage::singleton->add_register_func("functions/set",create_node_generic<VisualScriptPropertySet>);
  1735. VisualScriptLanguage::singleton->add_register_func("functions/get",create_node_generic<VisualScriptPropertyGet>);
  1736. //VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_self",create_script_call_node<VisualScriptScriptCall::CALL_MODE_SELF>);
  1737. // VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_node",create_script_call_node<VisualScriptScriptCall::CALL_MODE_NODE_PATH>);
  1738. VisualScriptLanguage::singleton->add_register_func("functions/emit_signal",create_node_generic<VisualScriptEmitSignal>);
  1739. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1740. Variant::Type t = Variant::Type(i);
  1741. String type_name = Variant::get_type_name(t);
  1742. Variant::CallError ce;
  1743. Variant vt = Variant::construct(t,NULL,0,ce);
  1744. List<MethodInfo> ml;
  1745. vt.get_method_list(&ml);
  1746. for (List<MethodInfo>::Element *E=ml.front();E;E=E->next()) {
  1747. VisualScriptLanguage::singleton->add_register_func("functions/by_type/"+type_name+"/"+E->get().name,create_basic_type_call_node);
  1748. }
  1749. }
  1750. }