2
0

visual_script_func_nodes.cpp 60 KB

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