visual_script_func_nodes.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497
  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_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::VisualScriptFunctionCall() {
  661. validate=true;
  662. call_mode=CALL_MODE_SELF;
  663. basic_type=Variant::NIL;
  664. use_default_args=0;
  665. base_type="Object";
  666. rpc_call_mode=RPC_DISABLED;
  667. }
  668. template<VisualScriptFunctionCall::CallMode cmode>
  669. static Ref<VisualScriptNode> create_function_call_node(const String& p_name) {
  670. Ref<VisualScriptFunctionCall> node;
  671. node.instance();
  672. node->set_call_mode(cmode);
  673. return node;
  674. }
  675. //////////////////////////////////////////
  676. ////////////////SET//////////////////////
  677. //////////////////////////////////////////
  678. static const char* event_type_names[InputEvent::TYPE_MAX]={
  679. "None",
  680. "Key",
  681. "MouseMotion",
  682. "MouseButton",
  683. "JoystickMotion",
  684. "JoystickButton",
  685. "ScreenTouch",
  686. "ScreenDrag",
  687. "Action"
  688. };
  689. int VisualScriptPropertySet::get_output_sequence_port_count() const {
  690. return call_mode!=CALL_MODE_BASIC_TYPE ? 1 : 0;
  691. }
  692. bool VisualScriptPropertySet::has_input_sequence_port() const{
  693. return call_mode!=CALL_MODE_BASIC_TYPE ? true : false;
  694. }
  695. Node *VisualScriptPropertySet::_get_base_node() const {
  696. #ifdef TOOLS_ENABLED
  697. Ref<Script> script = get_visual_script();
  698. if (!script.is_valid())
  699. return NULL;
  700. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  701. if (!main_loop)
  702. return NULL;
  703. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  704. if (!scene_tree)
  705. return NULL;
  706. Node *edited_scene = scene_tree->get_edited_scene_root();
  707. if (!edited_scene)
  708. return NULL;
  709. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  710. if (!script_node)
  711. return NULL;
  712. if (!script_node->has_node(base_path))
  713. return NULL;
  714. Node *path_to = script_node->get_node(base_path);
  715. return path_to;
  716. #else
  717. return NULL;
  718. #endif
  719. }
  720. StringName VisualScriptPropertySet::_get_base_type() const {
  721. if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid())
  722. return get_visual_script()->get_instance_base_type();
  723. else if (call_mode==CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  724. Node *path = _get_base_node();
  725. if (path)
  726. return path->get_type();
  727. }
  728. return base_type;
  729. }
  730. int VisualScriptPropertySet::get_input_value_port_count() const{
  731. int pc = (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE)?2:1;
  732. return pc;
  733. }
  734. int VisualScriptPropertySet::get_output_value_port_count() const{
  735. return (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE) ? 1 : 0;
  736. }
  737. String VisualScriptPropertySet::get_output_sequence_port_text(int p_port) const {
  738. return String();
  739. }
  740. PropertyInfo VisualScriptPropertySet::get_input_value_port_info(int p_idx) const{
  741. if (call_mode==CALL_MODE_INSTANCE || call_mode==CALL_MODE_BASIC_TYPE) {
  742. if (p_idx==0) {
  743. PropertyInfo pi;
  744. pi.type=(call_mode==CALL_MODE_INSTANCE?Variant::OBJECT:basic_type);
  745. pi.name=(call_mode==CALL_MODE_INSTANCE?String("instance"):Variant::get_type_name(basic_type).to_lower());
  746. return pi;
  747. } else {
  748. p_idx--;
  749. }
  750. }
  751. PropertyInfo pinfo=type_cache;
  752. pinfo.name="value";
  753. return pinfo;
  754. }
  755. PropertyInfo VisualScriptPropertySet::get_output_value_port_info(int p_idx) const{
  756. if (call_mode==CALL_MODE_BASIC_TYPE) {
  757. return PropertyInfo(basic_type,"out");
  758. } else if (call_mode==CALL_MODE_INSTANCE) {
  759. return PropertyInfo(Variant::OBJECT,"pass");
  760. } else {
  761. return PropertyInfo();
  762. }
  763. }
  764. String VisualScriptPropertySet::get_caption() const {
  765. static const char*cname[4]= {
  766. "SelfSet",
  767. "NodeSet",
  768. "InstanceSet",
  769. "BasicSet"
  770. };
  771. return cname[call_mode];
  772. }
  773. String VisualScriptPropertySet::get_text() const {
  774. String prop;
  775. if (call_mode==CALL_MODE_BASIC_TYPE)
  776. prop=Variant::get_type_name(basic_type)+"."+property;
  777. else if (call_mode==CALL_MODE_NODE_PATH)
  778. prop=String(base_path)+":"+property;
  779. else if (call_mode==CALL_MODE_SELF)
  780. prop=property;
  781. else if (call_mode==CALL_MODE_INSTANCE)
  782. prop=String(base_type)+":"+property;
  783. return prop;
  784. }
  785. void VisualScriptPropertySet::_update_base_type() {
  786. //cache it because this information may not be available on load
  787. if (call_mode==CALL_MODE_NODE_PATH) {
  788. Node* node=_get_base_node();
  789. if (node) {
  790. base_type=node->get_type();
  791. }
  792. } else if (call_mode==CALL_MODE_SELF) {
  793. if (get_visual_script().is_valid()) {
  794. base_type=get_visual_script()->get_instance_base_type();
  795. }
  796. }
  797. }
  798. void VisualScriptPropertySet::set_basic_type(Variant::Type p_type) {
  799. if (basic_type==p_type)
  800. return;
  801. basic_type=p_type;
  802. _change_notify();
  803. _update_base_type();
  804. ports_changed_notify();
  805. }
  806. Variant::Type VisualScriptPropertySet::get_basic_type() const{
  807. return basic_type;
  808. }
  809. void VisualScriptPropertySet::set_event_type(InputEvent::Type p_type) {
  810. if (event_type==p_type)
  811. return;
  812. event_type=p_type;
  813. if (call_mode==CALL_MODE_BASIC_TYPE) {
  814. _update_cache();
  815. }
  816. _change_notify();
  817. _update_base_type();
  818. ports_changed_notify();
  819. }
  820. InputEvent::Type VisualScriptPropertySet::get_event_type() const{
  821. return event_type;
  822. }
  823. void VisualScriptPropertySet::set_base_type(const StringName& p_type) {
  824. if (base_type==p_type)
  825. return;
  826. base_type=p_type;
  827. _change_notify();
  828. ports_changed_notify();
  829. }
  830. StringName VisualScriptPropertySet::get_base_type() const{
  831. return base_type;
  832. }
  833. void VisualScriptPropertySet::set_base_script(const String& p_path) {
  834. if (base_script==p_path)
  835. return;
  836. base_script=p_path;
  837. _change_notify();
  838. ports_changed_notify();
  839. }
  840. String VisualScriptPropertySet::get_base_script() const {
  841. return base_script;
  842. }
  843. void VisualScriptPropertySet::_update_cache() {
  844. if (!OS::get_singleton()->get_main_loop())
  845. return;
  846. if (!OS::get_singleton()->get_main_loop()->cast_to<SceneTree>())
  847. return;
  848. if (!OS::get_singleton()->get_main_loop()->cast_to<SceneTree>()->is_editor_hint()) //only update cache if editor exists, it's pointless otherwise
  849. return;
  850. if (call_mode==CALL_MODE_BASIC_TYPE) {
  851. //not super efficient..
  852. Variant v;
  853. if (basic_type==Variant::INPUT_EVENT) {
  854. InputEvent ev;
  855. ev.type=event_type;
  856. v=ev;
  857. } else {
  858. Variant::CallError ce;
  859. v = Variant::construct(basic_type,NULL,0,ce);
  860. }
  861. List<PropertyInfo> pinfo;
  862. v.get_property_list(&pinfo);
  863. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  864. if (E->get().name==property) {
  865. type_cache=E->get();
  866. }
  867. }
  868. } else {
  869. StringName type;
  870. Ref<Script> script;
  871. Node *node=NULL;
  872. if (call_mode==CALL_MODE_NODE_PATH) {
  873. node=_get_base_node();
  874. if (node) {
  875. type=node->get_type();
  876. base_type=type; //cache, too
  877. script = node->get_script();
  878. }
  879. } else if (call_mode==CALL_MODE_SELF) {
  880. if (get_visual_script().is_valid()) {
  881. type=get_visual_script()->get_instance_base_type();
  882. base_type=type; //cache, too
  883. script=get_visual_script();
  884. }
  885. } else if (call_mode==CALL_MODE_INSTANCE) {
  886. type=base_type;
  887. if (base_script!=String()) {
  888. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  889. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  890. }
  891. if (ResourceCache::has(base_script)) {
  892. script = Ref<Resource>( ResourceCache::get(base_script) );
  893. } else {
  894. return;
  895. }
  896. }
  897. }
  898. List<PropertyInfo> pinfo;
  899. if (node) {
  900. node->get_property_list(&pinfo);
  901. } else {
  902. ObjectTypeDB::get_property_list(type,&pinfo);
  903. }
  904. if (script.is_valid()) {
  905. script->get_script_property_list(&pinfo);
  906. }
  907. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  908. if (E->get().name==property) {
  909. type_cache=E->get();
  910. return;
  911. }
  912. }
  913. }
  914. }
  915. void VisualScriptPropertySet::set_property(const StringName& p_type){
  916. if (property==p_type)
  917. return;
  918. property=p_type;
  919. _update_cache();
  920. _change_notify();
  921. ports_changed_notify();
  922. }
  923. StringName VisualScriptPropertySet::get_property() const {
  924. return property;
  925. }
  926. void VisualScriptPropertySet::set_base_path(const NodePath& p_type) {
  927. if (base_path==p_type)
  928. return;
  929. base_path=p_type;
  930. _update_base_type();
  931. _change_notify();
  932. ports_changed_notify();
  933. }
  934. NodePath VisualScriptPropertySet::get_base_path() const {
  935. return base_path;
  936. }
  937. void VisualScriptPropertySet::set_call_mode(CallMode p_mode) {
  938. if (call_mode==p_mode)
  939. return;
  940. call_mode=p_mode;
  941. _update_base_type();
  942. _change_notify();
  943. ports_changed_notify();
  944. }
  945. VisualScriptPropertySet::CallMode VisualScriptPropertySet::get_call_mode() const {
  946. return call_mode;
  947. }
  948. void VisualScriptPropertySet::_set_type_cache(const Dictionary &p_type) {
  949. type_cache=PropertyInfo::from_dict(p_type);
  950. }
  951. Dictionary VisualScriptPropertySet::_get_type_cache() const {
  952. return type_cache;
  953. }
  954. void VisualScriptPropertySet::_validate_property(PropertyInfo& property) const {
  955. if (property.name=="property/base_type") {
  956. if (call_mode!=CALL_MODE_INSTANCE) {
  957. property.usage=PROPERTY_USAGE_NOEDITOR;
  958. }
  959. }
  960. if (property.name=="property/base_script") {
  961. if (call_mode!=CALL_MODE_INSTANCE) {
  962. property.usage=0;
  963. }
  964. }
  965. if (property.name=="property/basic_type") {
  966. if (call_mode!=CALL_MODE_BASIC_TYPE) {
  967. property.usage=0;
  968. }
  969. }
  970. if (property.name=="property/event_type") {
  971. if (call_mode!=CALL_MODE_BASIC_TYPE || basic_type!=Variant::INPUT_EVENT) {
  972. property.usage=0;
  973. }
  974. }
  975. if (property.name=="property/node_path") {
  976. if (call_mode!=CALL_MODE_NODE_PATH) {
  977. property.usage=0;
  978. } else {
  979. Node *bnode = _get_base_node();
  980. if (bnode) {
  981. property.hint_string=bnode->get_path(); //convert to loong string
  982. } else {
  983. }
  984. }
  985. }
  986. if (property.name=="property/property") {
  987. if (call_mode==CALL_MODE_BASIC_TYPE) {
  988. property.hint=PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  989. property.hint_string=Variant::get_type_name(basic_type);
  990. } else if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid()) {
  991. property.hint=PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  992. property.hint_string=itos(get_visual_script()->get_instance_ID());
  993. } else if (call_mode==CALL_MODE_INSTANCE) {
  994. property.hint=PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  995. property.hint_string=base_type;
  996. if (base_script!=String()) {
  997. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  998. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  999. }
  1000. if (ResourceCache::has(base_script)) {
  1001. Ref<Script> script = Ref<Resource>( ResourceCache::get(base_script) );
  1002. if (script.is_valid()) {
  1003. property.hint=PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1004. property.hint_string=itos(script->get_instance_ID());
  1005. }
  1006. }
  1007. }
  1008. } else if (call_mode==CALL_MODE_NODE_PATH) {
  1009. Node *node = _get_base_node();
  1010. if (node) {
  1011. property.hint=PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  1012. property.hint_string=itos(node->get_instance_ID());
  1013. } else {
  1014. property.hint=PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1015. property.hint_string=get_base_type();
  1016. }
  1017. }
  1018. }
  1019. }
  1020. void VisualScriptPropertySet::_bind_methods() {
  1021. ObjectTypeDB::bind_method(_MD("set_base_type","base_type"),&VisualScriptPropertySet::set_base_type);
  1022. ObjectTypeDB::bind_method(_MD("get_base_type"),&VisualScriptPropertySet::get_base_type);
  1023. ObjectTypeDB::bind_method(_MD("set_base_script","base_script"),&VisualScriptPropertySet::set_base_script);
  1024. ObjectTypeDB::bind_method(_MD("get_base_script"),&VisualScriptPropertySet::get_base_script);
  1025. ObjectTypeDB::bind_method(_MD("set_basic_type","basic_type"),&VisualScriptPropertySet::set_basic_type);
  1026. ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptPropertySet::get_basic_type);
  1027. ObjectTypeDB::bind_method(_MD("_set_type_cache","type_cache"),&VisualScriptPropertySet::_set_type_cache);
  1028. ObjectTypeDB::bind_method(_MD("_get_type_cache"),&VisualScriptPropertySet::_get_type_cache);
  1029. ObjectTypeDB::bind_method(_MD("set_event_type","event_type"),&VisualScriptPropertySet::set_event_type);
  1030. ObjectTypeDB::bind_method(_MD("get_event_type"),&VisualScriptPropertySet::get_event_type);
  1031. ObjectTypeDB::bind_method(_MD("set_property","property"),&VisualScriptPropertySet::set_property);
  1032. ObjectTypeDB::bind_method(_MD("get_property"),&VisualScriptPropertySet::get_property);
  1033. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptPropertySet::set_call_mode);
  1034. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptPropertySet::get_call_mode);
  1035. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptPropertySet::set_base_path);
  1036. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptPropertySet::get_base_path);
  1037. String bt;
  1038. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1039. if (i>0)
  1040. bt+=",";
  1041. bt+=Variant::get_type_name(Variant::Type(i));
  1042. }
  1043. String et;
  1044. for(int i=0;i<InputEvent::TYPE_MAX;i++) {
  1045. if (i>0)
  1046. et+=",";
  1047. et+=event_type_names[i];
  1048. }
  1049. List<String> script_extensions;
  1050. for(int i=0;i<ScriptServer::get_language_count();i++) {
  1051. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  1052. }
  1053. String script_ext_hint;
  1054. for (List<String>::Element *E=script_extensions.front();E;E=E->next()) {
  1055. if (script_ext_hint!=String())
  1056. script_ext_hint+=",";
  1057. script_ext_hint+="*."+E->get();
  1058. }
  1059. 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"));
  1060. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_type",PROPERTY_HINT_TYPE_STRING,"Object"),_SCS("set_base_type"),_SCS("get_base_type"));
  1061. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_script",PROPERTY_HINT_FILE,script_ext_hint),_SCS("set_base_script"),_SCS("get_base_script"));
  1062. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/type_cache",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_type_cache"),_SCS("_get_type_cache"));
  1063. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/basic_type",PROPERTY_HINT_ENUM,bt),_SCS("set_basic_type"),_SCS("get_basic_type"));
  1064. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/event_type",PROPERTY_HINT_ENUM,et),_SCS("set_event_type"),_SCS("get_event_type"));
  1065. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"property/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  1066. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/property"),_SCS("set_property"),_SCS("get_property"));
  1067. BIND_CONSTANT( CALL_MODE_SELF );
  1068. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  1069. BIND_CONSTANT( CALL_MODE_INSTANCE);
  1070. }
  1071. class VisualScriptNodeInstancePropertySet : public VisualScriptNodeInstance {
  1072. public:
  1073. VisualScriptPropertySet::CallMode call_mode;
  1074. NodePath node_path;
  1075. StringName property;
  1076. VisualScriptPropertySet *node;
  1077. VisualScriptInstance *instance;
  1078. //virtual int get_working_memory_size() const { return 0; }
  1079. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1080. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1081. 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) {
  1082. switch(call_mode) {
  1083. case VisualScriptPropertySet::CALL_MODE_SELF: {
  1084. Object *object=instance->get_owner_ptr();
  1085. bool valid;
  1086. object->set(property,*p_inputs[0],&valid);
  1087. if (!valid) {
  1088. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1089. r_error_str="Invalid set value '"+String(*p_inputs[0])+"' on property '"+String(property)+"' of type "+object->get_type();
  1090. }
  1091. } break;
  1092. case VisualScriptPropertySet::CALL_MODE_NODE_PATH: {
  1093. Node* node = instance->get_owner_ptr()->cast_to<Node>();
  1094. if (!node) {
  1095. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1096. r_error_str="Base object is not a Node!";
  1097. return 0;
  1098. }
  1099. Node* another = node->get_node(node_path);
  1100. if (!node) {
  1101. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1102. r_error_str="Path does not lead Node!";
  1103. return 0;
  1104. }
  1105. bool valid;
  1106. another->set(property,*p_inputs[0],&valid);
  1107. if (!valid) {
  1108. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1109. r_error_str="Invalid set value '"+String(*p_inputs[0])+"' on property '"+String(property)+"' of type "+another->get_type();
  1110. }
  1111. } break;
  1112. case VisualScriptPropertySet::CALL_MODE_INSTANCE:
  1113. case VisualScriptPropertySet::CALL_MODE_BASIC_TYPE: {
  1114. Variant v = *p_inputs[0];
  1115. bool valid;
  1116. v.set(property,*p_inputs[1],&valid);
  1117. if (!valid) {
  1118. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1119. 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());
  1120. }
  1121. *p_outputs[0]=v;
  1122. } break;
  1123. }
  1124. return 0;
  1125. }
  1126. };
  1127. VisualScriptNodeInstance* VisualScriptPropertySet::instance(VisualScriptInstance* p_instance) {
  1128. VisualScriptNodeInstancePropertySet * instance = memnew(VisualScriptNodeInstancePropertySet );
  1129. instance->node=this;
  1130. instance->instance=p_instance;
  1131. instance->property=property;
  1132. instance->call_mode=call_mode;
  1133. instance->node_path=base_path;
  1134. return instance;
  1135. }
  1136. VisualScriptPropertySet::VisualScriptPropertySet() {
  1137. call_mode=CALL_MODE_SELF;
  1138. base_type="Object";
  1139. basic_type=Variant::NIL;
  1140. event_type=InputEvent::NONE;
  1141. }
  1142. template<VisualScriptPropertySet::CallMode cmode>
  1143. static Ref<VisualScriptNode> create_property_set_node(const String& p_name) {
  1144. Ref<VisualScriptPropertySet> node;
  1145. node.instance();
  1146. node->set_call_mode(cmode);
  1147. return node;
  1148. }
  1149. //////////////////////////////////////////
  1150. ////////////////GET//////////////////////
  1151. //////////////////////////////////////////
  1152. int VisualScriptPropertyGet::get_output_sequence_port_count() const {
  1153. return 0;// (call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?0:1;
  1154. }
  1155. bool VisualScriptPropertyGet::has_input_sequence_port() const{
  1156. return false;//(call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?false:true;
  1157. }
  1158. void VisualScriptPropertyGet::_update_base_type() {
  1159. //cache it because this information may not be available on load
  1160. if (call_mode==CALL_MODE_NODE_PATH) {
  1161. Node* node=_get_base_node();
  1162. if (node) {
  1163. base_type=node->get_type();
  1164. }
  1165. } else if (call_mode==CALL_MODE_SELF) {
  1166. if (get_visual_script().is_valid()) {
  1167. base_type=get_visual_script()->get_instance_base_type();
  1168. }
  1169. }
  1170. }
  1171. Node *VisualScriptPropertyGet::_get_base_node() const {
  1172. #ifdef TOOLS_ENABLED
  1173. Ref<Script> script = get_visual_script();
  1174. if (!script.is_valid())
  1175. return NULL;
  1176. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  1177. if (!main_loop)
  1178. return NULL;
  1179. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  1180. if (!scene_tree)
  1181. return NULL;
  1182. Node *edited_scene = scene_tree->get_edited_scene_root();
  1183. if (!edited_scene)
  1184. return NULL;
  1185. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  1186. if (!script_node)
  1187. return NULL;
  1188. if (!script_node->has_node(base_path))
  1189. return NULL;
  1190. Node *path_to = script_node->get_node(base_path);
  1191. return path_to;
  1192. #else
  1193. return NULL;
  1194. #endif
  1195. }
  1196. StringName VisualScriptPropertyGet::_get_base_type() const {
  1197. if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid())
  1198. return get_visual_script()->get_instance_base_type();
  1199. else if (call_mode==CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  1200. Node *path = _get_base_node();
  1201. if (path)
  1202. return path->get_type();
  1203. }
  1204. return base_type;
  1205. }
  1206. int VisualScriptPropertyGet::get_input_value_port_count() const{
  1207. return (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE)?1:0;
  1208. }
  1209. int VisualScriptPropertyGet::get_output_value_port_count() const{
  1210. return 1;
  1211. }
  1212. String VisualScriptPropertyGet::get_output_sequence_port_text(int p_port) const {
  1213. return String();
  1214. }
  1215. PropertyInfo VisualScriptPropertyGet::get_input_value_port_info(int p_idx) const{
  1216. if (call_mode==CALL_MODE_INSTANCE || call_mode==CALL_MODE_BASIC_TYPE) {
  1217. if (p_idx==0) {
  1218. PropertyInfo pi;
  1219. pi.type=(call_mode==CALL_MODE_INSTANCE?Variant::OBJECT:basic_type);
  1220. pi.name=(call_mode==CALL_MODE_INSTANCE?String("instance"):Variant::get_type_name(basic_type).to_lower());
  1221. return pi;
  1222. } else {
  1223. p_idx--;
  1224. }
  1225. }
  1226. return PropertyInfo();
  1227. }
  1228. PropertyInfo VisualScriptPropertyGet::get_output_value_port_info(int p_idx) const{
  1229. return PropertyInfo(type_cache,"value");
  1230. }
  1231. String VisualScriptPropertyGet::get_caption() const {
  1232. static const char*cname[4]= {
  1233. "SelfGet",
  1234. "NodeGet",
  1235. "InstanceGet",
  1236. "BasicGet"
  1237. };
  1238. return cname[call_mode];
  1239. }
  1240. String VisualScriptPropertyGet::get_text() const {
  1241. String prop;
  1242. if (call_mode==CALL_MODE_BASIC_TYPE)
  1243. prop=Variant::get_type_name(basic_type)+"."+property;
  1244. else if (call_mode==CALL_MODE_NODE_PATH)
  1245. prop=String(base_path)+":"+property;
  1246. else if (call_mode==CALL_MODE_SELF)
  1247. prop=property;
  1248. else if (call_mode==CALL_MODE_INSTANCE)
  1249. prop=String(base_type)+":"+property;
  1250. return prop;
  1251. }
  1252. void VisualScriptPropertyGet::set_base_type(const StringName& p_type) {
  1253. if (base_type==p_type)
  1254. return;
  1255. base_type=p_type;
  1256. _change_notify();
  1257. ports_changed_notify();
  1258. }
  1259. StringName VisualScriptPropertyGet::get_base_type() const{
  1260. return base_type;
  1261. }
  1262. void VisualScriptPropertyGet::set_base_script(const String& p_path) {
  1263. if (base_script==p_path)
  1264. return;
  1265. base_script=p_path;
  1266. _change_notify();
  1267. ports_changed_notify();
  1268. }
  1269. String VisualScriptPropertyGet::get_base_script() const {
  1270. return base_script;
  1271. }
  1272. void VisualScriptPropertyGet::_update_cache() {
  1273. if (call_mode==CALL_MODE_BASIC_TYPE) {
  1274. //not super efficient..
  1275. Variant v;
  1276. if (basic_type==Variant::INPUT_EVENT) {
  1277. InputEvent ev;
  1278. ev.type=event_type;
  1279. v=ev;
  1280. } else {
  1281. Variant::CallError ce;
  1282. v = Variant::construct(basic_type,NULL,0,ce);
  1283. }
  1284. List<PropertyInfo> pinfo;
  1285. v.get_property_list(&pinfo);
  1286. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  1287. if (E->get().name==property) {
  1288. type_cache=E->get().type;
  1289. return;
  1290. }
  1291. }
  1292. } else {
  1293. StringName type;
  1294. Ref<Script> script;
  1295. Node *node=NULL;
  1296. if (call_mode==CALL_MODE_NODE_PATH) {
  1297. node=_get_base_node();
  1298. if (node) {
  1299. type=node->get_type();
  1300. base_type=type; //cache, too
  1301. script = node->get_script();
  1302. }
  1303. } else if (call_mode==CALL_MODE_SELF) {
  1304. if (get_visual_script().is_valid()) {
  1305. type=get_visual_script()->get_instance_base_type();
  1306. base_type=type; //cache, too
  1307. script=get_visual_script();
  1308. }
  1309. } else if (call_mode==CALL_MODE_INSTANCE) {
  1310. type=base_type;
  1311. if (base_script!=String()) {
  1312. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1313. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1314. }
  1315. if (ResourceCache::has(base_script)) {
  1316. script = Ref<Resource>( ResourceCache::get(base_script) );
  1317. } else {
  1318. return;
  1319. }
  1320. }
  1321. }
  1322. bool valid=false;
  1323. Variant::Type type_ret;
  1324. type_ret=ObjectTypeDB::get_property_type(base_type,property,&valid);
  1325. if (valid) {
  1326. type_cache=type_ret;
  1327. return; //all dandy
  1328. }
  1329. if (node) {
  1330. Variant prop = node->get(property,&valid);
  1331. if (valid) {
  1332. type_cache=prop.get_type();
  1333. return; //all dandy again
  1334. }
  1335. }
  1336. if (script.is_valid()) {
  1337. type_ret=script->get_static_property_type(property,&valid);
  1338. if (valid) {
  1339. type_cache=type_ret;
  1340. return; //all dandy
  1341. }
  1342. }
  1343. }
  1344. }
  1345. void VisualScriptPropertyGet::set_property(const StringName& p_type){
  1346. if (property==p_type)
  1347. return;
  1348. property=p_type;
  1349. _update_cache();
  1350. _change_notify();
  1351. ports_changed_notify();
  1352. }
  1353. StringName VisualScriptPropertyGet::get_property() const {
  1354. return property;
  1355. }
  1356. void VisualScriptPropertyGet::set_base_path(const NodePath& p_type) {
  1357. if (base_path==p_type)
  1358. return;
  1359. base_path=p_type;
  1360. _change_notify();
  1361. _update_base_type();
  1362. ports_changed_notify();
  1363. }
  1364. NodePath VisualScriptPropertyGet::get_base_path() const {
  1365. return base_path;
  1366. }
  1367. void VisualScriptPropertyGet::set_call_mode(CallMode p_mode) {
  1368. if (call_mode==p_mode)
  1369. return;
  1370. call_mode=p_mode;
  1371. _change_notify();
  1372. _update_base_type();
  1373. ports_changed_notify();
  1374. }
  1375. VisualScriptPropertyGet::CallMode VisualScriptPropertyGet::get_call_mode() const {
  1376. return call_mode;
  1377. }
  1378. void VisualScriptPropertyGet::set_basic_type(Variant::Type p_type) {
  1379. if (basic_type==p_type)
  1380. return;
  1381. basic_type=p_type;
  1382. _change_notify();
  1383. ports_changed_notify();
  1384. }
  1385. Variant::Type VisualScriptPropertyGet::get_basic_type() const{
  1386. return basic_type;
  1387. }
  1388. void VisualScriptPropertyGet::set_event_type(InputEvent::Type p_type) {
  1389. if (event_type==p_type)
  1390. return;
  1391. event_type=p_type;
  1392. if(call_mode==CALL_MODE_BASIC_TYPE) {
  1393. _update_cache();
  1394. }
  1395. _change_notify();
  1396. _update_base_type();
  1397. ports_changed_notify();
  1398. }
  1399. InputEvent::Type VisualScriptPropertyGet::get_event_type() const{
  1400. return event_type;
  1401. }
  1402. void VisualScriptPropertyGet::_set_type_cache(Variant::Type p_type) {
  1403. type_cache=p_type;
  1404. }
  1405. Variant::Type VisualScriptPropertyGet::_get_type_cache() const {
  1406. return type_cache;
  1407. }
  1408. void VisualScriptPropertyGet::_validate_property(PropertyInfo& property) const {
  1409. if (property.name=="property/base_type") {
  1410. if (call_mode!=CALL_MODE_INSTANCE) {
  1411. property.usage=PROPERTY_USAGE_NOEDITOR;
  1412. }
  1413. }
  1414. if (property.name=="property/base_script") {
  1415. if (call_mode!=CALL_MODE_INSTANCE) {
  1416. property.usage=0;
  1417. }
  1418. }
  1419. if (property.name=="property/basic_type") {
  1420. if (call_mode!=CALL_MODE_BASIC_TYPE) {
  1421. property.usage=0;
  1422. }
  1423. }
  1424. if (property.name=="property/event_type") {
  1425. if (call_mode!=CALL_MODE_BASIC_TYPE || basic_type!=Variant::INPUT_EVENT) {
  1426. property.usage=0;
  1427. }
  1428. }
  1429. if (property.name=="property/node_path") {
  1430. if (call_mode!=CALL_MODE_NODE_PATH) {
  1431. property.usage=0;
  1432. } else {
  1433. Node *bnode = _get_base_node();
  1434. if (bnode) {
  1435. property.hint_string=bnode->get_path(); //convert to loong string
  1436. } else {
  1437. }
  1438. }
  1439. }
  1440. if (property.name=="property/property") {
  1441. if (call_mode==CALL_MODE_BASIC_TYPE) {
  1442. property.hint=PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  1443. property.hint_string=Variant::get_type_name(basic_type);
  1444. } else if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid()) {
  1445. property.hint=PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1446. property.hint_string=itos(get_visual_script()->get_instance_ID());
  1447. } else if (call_mode==CALL_MODE_INSTANCE) {
  1448. property.hint=PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1449. property.hint_string=base_type;
  1450. if (base_script!=String()) {
  1451. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1452. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1453. }
  1454. if (ResourceCache::has(base_script)) {
  1455. Ref<Script> script = Ref<Resource>( ResourceCache::get(base_script) );
  1456. if (script.is_valid()) {
  1457. property.hint=PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1458. property.hint_string=itos(script->get_instance_ID());
  1459. }
  1460. }
  1461. }
  1462. } else if (call_mode==CALL_MODE_NODE_PATH) {
  1463. Node *node = _get_base_node();
  1464. if (node) {
  1465. property.hint=PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  1466. property.hint_string=itos(node->get_instance_ID());
  1467. } else {
  1468. property.hint=PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1469. property.hint_string=get_base_type();
  1470. }
  1471. }
  1472. }
  1473. }
  1474. void VisualScriptPropertyGet::_bind_methods() {
  1475. ObjectTypeDB::bind_method(_MD("set_base_type","base_type"),&VisualScriptPropertyGet::set_base_type);
  1476. ObjectTypeDB::bind_method(_MD("get_base_type"),&VisualScriptPropertyGet::get_base_type);
  1477. ObjectTypeDB::bind_method(_MD("set_base_script","base_script"),&VisualScriptPropertyGet::set_base_script);
  1478. ObjectTypeDB::bind_method(_MD("get_base_script"),&VisualScriptPropertyGet::get_base_script);
  1479. ObjectTypeDB::bind_method(_MD("set_basic_type","basic_type"),&VisualScriptPropertyGet::set_basic_type);
  1480. ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptPropertyGet::get_basic_type);
  1481. ObjectTypeDB::bind_method(_MD("_set_type_cache","type_cache"),&VisualScriptPropertyGet::_set_type_cache);
  1482. ObjectTypeDB::bind_method(_MD("_get_type_cache"),&VisualScriptPropertyGet::_get_type_cache);
  1483. ObjectTypeDB::bind_method(_MD("set_event_type","event_type"),&VisualScriptPropertyGet::set_event_type);
  1484. ObjectTypeDB::bind_method(_MD("get_event_type"),&VisualScriptPropertyGet::get_event_type);
  1485. ObjectTypeDB::bind_method(_MD("set_property","property"),&VisualScriptPropertyGet::set_property);
  1486. ObjectTypeDB::bind_method(_MD("get_property"),&VisualScriptPropertyGet::get_property);
  1487. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptPropertyGet::set_call_mode);
  1488. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptPropertyGet::get_call_mode);
  1489. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptPropertyGet::set_base_path);
  1490. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptPropertyGet::get_base_path);
  1491. String bt;
  1492. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1493. if (i>0)
  1494. bt+=",";
  1495. bt+=Variant::get_type_name(Variant::Type(i));
  1496. }
  1497. String et;
  1498. for(int i=0;i<InputEvent::TYPE_MAX;i++) {
  1499. if (i>0)
  1500. et+=",";
  1501. et+=event_type_names[i];
  1502. }
  1503. List<String> script_extensions;
  1504. for(int i=0;i<ScriptServer::get_language_count();i++) {
  1505. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  1506. }
  1507. String script_ext_hint;
  1508. for (List<String>::Element *E=script_extensions.front();E;E=E->next()) {
  1509. if (script_ext_hint!=String())
  1510. script_ext_hint+=",";
  1511. script_ext_hint+="."+E->get();
  1512. }
  1513. 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"));
  1514. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_type",PROPERTY_HINT_TYPE_STRING,"Object"),_SCS("set_base_type"),_SCS("get_base_type"));
  1515. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_script",PROPERTY_HINT_FILE,script_ext_hint),_SCS("set_base_script"),_SCS("get_base_script"));
  1516. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/type_cache",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_type_cache"),_SCS("_get_type_cache"));
  1517. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/basic_type",PROPERTY_HINT_ENUM,bt),_SCS("set_basic_type"),_SCS("get_basic_type"));
  1518. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/event_type",PROPERTY_HINT_ENUM,et),_SCS("set_event_type"),_SCS("get_event_type"));
  1519. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"property/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  1520. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/property"),_SCS("set_property"),_SCS("get_property"));
  1521. BIND_CONSTANT( CALL_MODE_SELF );
  1522. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  1523. BIND_CONSTANT( CALL_MODE_INSTANCE);
  1524. }
  1525. class VisualScriptNodeInstancePropertyGet : public VisualScriptNodeInstance {
  1526. public:
  1527. VisualScriptPropertyGet::CallMode call_mode;
  1528. NodePath node_path;
  1529. StringName property;
  1530. VisualScriptPropertyGet *node;
  1531. VisualScriptInstance *instance;
  1532. 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) {
  1533. switch(call_mode) {
  1534. case VisualScriptPropertyGet::CALL_MODE_SELF: {
  1535. Object *object=instance->get_owner_ptr();
  1536. bool valid;
  1537. *p_outputs[0] = object->get(property,&valid);
  1538. if (!valid) {
  1539. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1540. r_error_str=RTR("Invalid index property name.");
  1541. return 0;
  1542. }
  1543. } break;
  1544. case VisualScriptPropertyGet::CALL_MODE_NODE_PATH: {
  1545. Node* node = instance->get_owner_ptr()->cast_to<Node>();
  1546. if (!node) {
  1547. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1548. r_error_str=RTR("Base object is not a Node!");
  1549. return 0;
  1550. }
  1551. Node* another = node->get_node(node_path);
  1552. if (!node) {
  1553. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1554. r_error_str=RTR("Path does not lead Node!");
  1555. return 0;
  1556. }
  1557. bool valid;
  1558. *p_outputs[0] = another->get(property,&valid);
  1559. if (!valid) {
  1560. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1561. r_error_str=vformat(RTR("Invalid index property name '%s' in node %s."),String(property),another->get_name());
  1562. return 0;
  1563. }
  1564. } break;
  1565. default: {
  1566. bool valid;
  1567. Variant v = *p_inputs[0];
  1568. *p_outputs[0] = v.get(property,&valid);
  1569. if (!valid) {
  1570. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1571. r_error_str=RTR("Invalid index property name.");
  1572. }
  1573. };
  1574. }
  1575. return 0;
  1576. }
  1577. };
  1578. VisualScriptNodeInstance* VisualScriptPropertyGet::instance(VisualScriptInstance* p_instance) {
  1579. VisualScriptNodeInstancePropertyGet * instance = memnew(VisualScriptNodeInstancePropertyGet );
  1580. instance->node=this;
  1581. instance->instance=p_instance;
  1582. instance->property=property;
  1583. instance->call_mode=call_mode;
  1584. instance->node_path=base_path;
  1585. return instance;
  1586. }
  1587. VisualScriptPropertyGet::VisualScriptPropertyGet() {
  1588. call_mode=CALL_MODE_SELF;
  1589. base_type="Object";
  1590. basic_type=Variant::NIL;
  1591. event_type=InputEvent::NONE;
  1592. type_cache=Variant::NIL;
  1593. }
  1594. template<VisualScriptPropertyGet::CallMode cmode>
  1595. static Ref<VisualScriptNode> create_property_get_node(const String& p_name) {
  1596. Ref<VisualScriptPropertyGet> node;
  1597. node.instance();
  1598. node->set_call_mode(cmode);
  1599. return node;
  1600. }
  1601. //////////////////////////////////////////
  1602. ////////////////EMIT//////////////////////
  1603. //////////////////////////////////////////
  1604. int VisualScriptEmitSignal::get_output_sequence_port_count() const {
  1605. return 1;
  1606. }
  1607. bool VisualScriptEmitSignal::has_input_sequence_port() const{
  1608. return true;
  1609. }
  1610. int VisualScriptEmitSignal::get_input_value_port_count() const{
  1611. Ref<VisualScript> vs = get_visual_script();
  1612. if (vs.is_valid()) {
  1613. if (!vs->has_custom_signal(name))
  1614. return 0;
  1615. return vs->custom_signal_get_argument_count(name);
  1616. }
  1617. return 0;
  1618. }
  1619. int VisualScriptEmitSignal::get_output_value_port_count() const{
  1620. return 0;
  1621. }
  1622. String VisualScriptEmitSignal::get_output_sequence_port_text(int p_port) const {
  1623. return String();
  1624. }
  1625. PropertyInfo VisualScriptEmitSignal::get_input_value_port_info(int p_idx) const{
  1626. Ref<VisualScript> vs = get_visual_script();
  1627. if (vs.is_valid()) {
  1628. if (!vs->has_custom_signal(name))
  1629. return PropertyInfo();
  1630. return PropertyInfo(vs->custom_signal_get_argument_type(name,p_idx),vs->custom_signal_get_argument_name(name,p_idx));
  1631. }
  1632. return PropertyInfo();
  1633. }
  1634. PropertyInfo VisualScriptEmitSignal::get_output_value_port_info(int p_idx) const{
  1635. return PropertyInfo();
  1636. }
  1637. String VisualScriptEmitSignal::get_caption() const {
  1638. return "EmitSignal";
  1639. }
  1640. String VisualScriptEmitSignal::get_text() const {
  1641. return "emit "+String(name);
  1642. }
  1643. void VisualScriptEmitSignal::set_signal(const StringName& p_type){
  1644. if (name==p_type)
  1645. return;
  1646. name=p_type;
  1647. _change_notify();
  1648. ports_changed_notify();
  1649. }
  1650. StringName VisualScriptEmitSignal::get_signal() const {
  1651. return name;
  1652. }
  1653. void VisualScriptEmitSignal::_validate_property(PropertyInfo& property) const {
  1654. if (property.name=="signal/signal") {
  1655. property.hint=PROPERTY_HINT_ENUM;
  1656. List<StringName> sigs;
  1657. Ref<VisualScript> vs = get_visual_script();
  1658. if (vs.is_valid()) {
  1659. vs->get_custom_signal_list(&sigs);
  1660. }
  1661. String ml;
  1662. for (List<StringName>::Element *E=sigs.front();E;E=E->next()) {
  1663. if (ml!=String())
  1664. ml+=",";
  1665. ml+=E->get();
  1666. }
  1667. property.hint_string=ml;
  1668. }
  1669. }
  1670. void VisualScriptEmitSignal::_bind_methods() {
  1671. ObjectTypeDB::bind_method(_MD("set_signal","name"),&VisualScriptEmitSignal::set_signal);
  1672. ObjectTypeDB::bind_method(_MD("get_signal"),&VisualScriptEmitSignal::get_signal);
  1673. ADD_PROPERTY(PropertyInfo(Variant::STRING,"signal/signal"),_SCS("set_signal"),_SCS("get_signal"));
  1674. }
  1675. class VisualScriptNodeInstanceEmitSignal : public VisualScriptNodeInstance {
  1676. public:
  1677. VisualScriptEmitSignal *node;
  1678. VisualScriptInstance *instance;
  1679. int argcount;
  1680. StringName name;
  1681. //virtual int get_working_memory_size() const { return 0; }
  1682. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1683. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1684. 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) {
  1685. Object *obj = instance->get_owner_ptr();
  1686. obj->emit_signal(name,p_inputs,argcount);
  1687. return 0;
  1688. }
  1689. };
  1690. VisualScriptNodeInstance* VisualScriptEmitSignal::instance(VisualScriptInstance* p_instance) {
  1691. VisualScriptNodeInstanceEmitSignal * instance = memnew(VisualScriptNodeInstanceEmitSignal );
  1692. instance->node=this;
  1693. instance->instance=p_instance;
  1694. instance->name=name;
  1695. instance->argcount=get_input_value_port_count();
  1696. return instance;
  1697. }
  1698. VisualScriptEmitSignal::VisualScriptEmitSignal() {
  1699. }
  1700. static Ref<VisualScriptNode> create_basic_type_call_node(const String& p_name) {
  1701. Vector<String> path = p_name.split("/");
  1702. ERR_FAIL_COND_V(path.size()<4,Ref<VisualScriptNode>());
  1703. String base_type = path[2];
  1704. String method = path[3];
  1705. Ref<VisualScriptFunctionCall> node;
  1706. node.instance();
  1707. Variant::Type type=Variant::VARIANT_MAX;
  1708. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1709. if (Variant::get_type_name(Variant::Type(i))==base_type) {
  1710. type=Variant::Type(i);
  1711. break;
  1712. }
  1713. }
  1714. ERR_FAIL_COND_V(type==Variant::VARIANT_MAX,Ref<VisualScriptNode>());
  1715. node->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  1716. node->set_basic_type(type);
  1717. node->set_function(method);
  1718. return node;
  1719. }
  1720. void register_visual_script_func_nodes() {
  1721. VisualScriptLanguage::singleton->add_register_func("functions/call",create_node_generic<VisualScriptFunctionCall>);
  1722. VisualScriptLanguage::singleton->add_register_func("functions/set",create_node_generic<VisualScriptPropertySet>);
  1723. VisualScriptLanguage::singleton->add_register_func("functions/get",create_node_generic<VisualScriptPropertyGet>);
  1724. //VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_self",create_script_call_node<VisualScriptScriptCall::CALL_MODE_SELF>);
  1725. // VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_node",create_script_call_node<VisualScriptScriptCall::CALL_MODE_NODE_PATH>);
  1726. VisualScriptLanguage::singleton->add_register_func("functions/emit_signal",create_node_generic<VisualScriptEmitSignal>);
  1727. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1728. Variant::Type t = Variant::Type(i);
  1729. String type_name = Variant::get_type_name(t);
  1730. Variant::CallError ce;
  1731. Variant vt = Variant::construct(t,NULL,0,ce);
  1732. List<MethodInfo> ml;
  1733. vt.get_method_list(&ml);
  1734. for (List<MethodInfo>::Element *E=ml.front();E;E=E->next()) {
  1735. VisualScriptLanguage::singleton->add_register_func("functions/by_type/"+type_name+"/"+E->get().name,create_basic_type_call_node);
  1736. }
  1737. }
  1738. }