visual_script_func_nodes.cpp 64 KB

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