visual_script_func_nodes.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  1. #include "visual_script_func_nodes.h"
  2. #include "scene/main/scene_main_loop.h"
  3. #include "os/os.h"
  4. #include "scene/main/node.h"
  5. #include "visual_script_nodes.h"
  6. //////////////////////////////////////////
  7. ////////////////CALL//////////////////////
  8. //////////////////////////////////////////
  9. int VisualScriptFunctionCall::get_output_sequence_port_count() const {
  10. return 1;
  11. }
  12. bool VisualScriptFunctionCall::has_input_sequence_port() const{
  13. return true;
  14. }
  15. #ifdef TOOLS_ENABLED
  16. static Node* _find_script_node(Node* p_edited_scene,Node* p_current_node,const Ref<Script> &script) {
  17. if (p_edited_scene!=p_current_node && p_current_node->get_owner()!=p_edited_scene)
  18. return NULL;
  19. Ref<Script> scr = p_current_node->get_script();
  20. if (scr.is_valid() && scr==script)
  21. return p_current_node;
  22. for(int i=0;i<p_current_node->get_child_count();i++) {
  23. Node *n = _find_script_node(p_edited_scene,p_current_node->get_child(i),script);
  24. if (n)
  25. return n;
  26. }
  27. return NULL;
  28. }
  29. #endif
  30. Node *VisualScriptFunctionCall::_get_base_node() const {
  31. #ifdef TOOLS_ENABLED
  32. Ref<Script> script = get_visual_script();
  33. if (!script.is_valid())
  34. return NULL;
  35. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  36. if (!main_loop)
  37. return NULL;
  38. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  39. if (!scene_tree)
  40. return NULL;
  41. Node *edited_scene = scene_tree->get_edited_scene_root();
  42. if (!edited_scene)
  43. return NULL;
  44. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  45. if (!script_node)
  46. return NULL;
  47. if (!script_node->has_node(base_path))
  48. return NULL;
  49. Node *path_to = script_node->get_node(base_path);
  50. return path_to;
  51. #else
  52. return NULL;
  53. #endif
  54. }
  55. StringName VisualScriptFunctionCall::_get_base_type() const {
  56. if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid())
  57. return get_visual_script()->get_instance_base_type();
  58. else if (call_mode==CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  59. Node *path = _get_base_node();
  60. if (path)
  61. return path->get_type();
  62. }
  63. return base_type;
  64. }
  65. int VisualScriptFunctionCall::get_input_value_port_count() const{
  66. if (call_mode==CALL_MODE_BASIC_TYPE) {
  67. Vector<StringName> names = Variant::get_method_argument_names(basic_type,function);
  68. return names.size()+1;
  69. } else {
  70. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  71. if (!mb)
  72. return 0;
  73. return mb->get_argument_count() + (call_mode==CALL_MODE_INSTANCE?1:0) - use_default_args;
  74. }
  75. }
  76. int VisualScriptFunctionCall::get_output_value_port_count() const{
  77. if (call_mode==CALL_MODE_BASIC_TYPE) {
  78. bool returns=false;
  79. Variant::get_method_return_type(basic_type,function,&returns);
  80. return returns?1:0;
  81. } else {
  82. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  83. if (!mb)
  84. return 0;
  85. return mb->has_return() ? 1 : 0;
  86. }
  87. }
  88. String VisualScriptFunctionCall::get_output_sequence_port_text(int p_port) const {
  89. return String();
  90. }
  91. PropertyInfo VisualScriptFunctionCall::get_input_value_port_info(int p_idx) const{
  92. if (call_mode==CALL_MODE_INSTANCE || call_mode==CALL_MODE_BASIC_TYPE) {
  93. if (p_idx==0) {
  94. PropertyInfo pi;
  95. pi.type=(call_mode==CALL_MODE_INSTANCE?Variant::OBJECT:basic_type);
  96. pi.name=(call_mode==CALL_MODE_INSTANCE?String("instance"):Variant::get_type_name(basic_type).to_lower());
  97. return pi;
  98. } else {
  99. p_idx--;
  100. }
  101. }
  102. #ifdef DEBUG_METHODS_ENABLED
  103. if (call_mode==CALL_MODE_BASIC_TYPE) {
  104. Vector<StringName> names = Variant::get_method_argument_names(basic_type,function);
  105. Vector<Variant::Type> types = Variant::get_method_argument_types(basic_type,function);
  106. return PropertyInfo(types[p_idx],names[p_idx]);
  107. } else {
  108. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  109. if (!mb)
  110. return PropertyInfo();
  111. return mb->get_argument_info(p_idx);
  112. }
  113. #else
  114. return PropertyInfo();
  115. #endif
  116. }
  117. PropertyInfo VisualScriptFunctionCall::get_output_value_port_info(int p_idx) const{
  118. #ifdef DEBUG_METHODS_ENABLED
  119. if (call_mode==CALL_MODE_BASIC_TYPE) {
  120. return PropertyInfo(Variant::get_method_return_type(basic_type,function),"");
  121. } else {
  122. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  123. if (!mb)
  124. return PropertyInfo();
  125. PropertyInfo pi = mb->get_argument_info(-1);
  126. pi.name="";
  127. return pi;
  128. }
  129. #else
  130. return PropertyInfo();
  131. #endif
  132. }
  133. String VisualScriptFunctionCall::get_caption() const {
  134. static const char*cname[4]= {
  135. "CallSelf",
  136. "CallNode",
  137. "CallInstance",
  138. "CallBasic"
  139. };
  140. return cname[call_mode];
  141. }
  142. String VisualScriptFunctionCall::get_text() const {
  143. if (call_mode==CALL_MODE_SELF)
  144. return " "+String(function)+"()";
  145. else if (call_mode==CALL_MODE_BASIC_TYPE)
  146. return Variant::get_type_name(basic_type)+"."+String(function)+"()";
  147. else
  148. return " "+base_type+"."+String(function)+"()";
  149. }
  150. void VisualScriptFunctionCall::_update_defargs() {
  151. //save base type if accessible
  152. if (call_mode==CALL_MODE_NODE_PATH) {
  153. Node* node=_get_base_node();
  154. if (node) {
  155. base_type=node->get_type();
  156. }
  157. } else if (call_mode==CALL_MODE_SELF) {
  158. if (get_visual_script().is_valid()) {
  159. base_type=get_visual_script()->get_instance_base_type();
  160. }
  161. }
  162. if (call_mode==CALL_MODE_BASIC_TYPE) {
  163. use_default_args = Variant::get_method_default_arguments(basic_type,function).size();
  164. } else {
  165. if (!get_visual_script().is_valid())
  166. return; //do not change if not valid yet
  167. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  168. if (!mb)
  169. return;
  170. use_default_args=mb->get_default_argument_count();
  171. }
  172. }
  173. void VisualScriptFunctionCall::set_basic_type(Variant::Type p_type) {
  174. if (basic_type==p_type)
  175. return;
  176. basic_type=p_type;
  177. _update_defargs();
  178. _change_notify();
  179. ports_changed_notify();
  180. }
  181. Variant::Type VisualScriptFunctionCall::get_basic_type() const{
  182. return basic_type;
  183. }
  184. void VisualScriptFunctionCall::set_base_type(const StringName& p_type) {
  185. if (base_type==p_type)
  186. return;
  187. base_type=p_type;
  188. _update_defargs();
  189. _change_notify();
  190. ports_changed_notify();
  191. }
  192. StringName VisualScriptFunctionCall::get_base_type() const{
  193. return base_type;
  194. }
  195. void VisualScriptFunctionCall::set_function(const StringName& p_type){
  196. if (function==p_type)
  197. return;
  198. function=p_type;
  199. _update_defargs();
  200. _change_notify();
  201. ports_changed_notify();
  202. }
  203. StringName VisualScriptFunctionCall::get_function() const {
  204. return function;
  205. }
  206. void VisualScriptFunctionCall::set_base_path(const NodePath& p_type) {
  207. if (base_path==p_type)
  208. return;
  209. base_path=p_type;
  210. _update_defargs();
  211. _change_notify();
  212. ports_changed_notify();
  213. }
  214. NodePath VisualScriptFunctionCall::get_base_path() const {
  215. return base_path;
  216. }
  217. void VisualScriptFunctionCall::set_call_mode(CallMode p_mode) {
  218. if (call_mode==p_mode)
  219. return;
  220. call_mode=p_mode;
  221. _update_defargs();
  222. _change_notify();
  223. ports_changed_notify();
  224. }
  225. VisualScriptFunctionCall::CallMode VisualScriptFunctionCall::get_call_mode() const {
  226. return call_mode;
  227. }
  228. void VisualScriptFunctionCall::set_use_default_args(int p_amount) {
  229. if (use_default_args==p_amount)
  230. return;
  231. use_default_args=p_amount;
  232. ports_changed_notify();
  233. }
  234. int VisualScriptFunctionCall::get_use_default_args() const{
  235. return use_default_args;
  236. }
  237. void VisualScriptFunctionCall::_validate_property(PropertyInfo& property) const {
  238. if (property.name=="function/base_type") {
  239. if (call_mode!=CALL_MODE_INSTANCE) {
  240. property.usage=PROPERTY_USAGE_NOEDITOR;
  241. }
  242. }
  243. if (property.name=="function/basic_type") {
  244. if (call_mode!=CALL_MODE_BASIC_TYPE) {
  245. property.usage=0;
  246. }
  247. }
  248. if (property.name=="function/node_path") {
  249. if (call_mode!=CALL_MODE_NODE_PATH) {
  250. property.usage=0;
  251. } else {
  252. Node *bnode = _get_base_node();
  253. if (bnode) {
  254. property.hint_string=bnode->get_path(); //convert to loong string
  255. } else {
  256. }
  257. }
  258. }
  259. if (property.name=="function/function") {
  260. if (call_mode==CALL_MODE_BASIC_TYPE) {
  261. property.hint=PROPERTY_HINT_METHOD_OF_VARIANT_TYPE;
  262. property.hint_string=Variant::get_type_name(basic_type);
  263. } else if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid()) {
  264. property.hint=PROPERTY_HINT_METHOD_OF_SCRIPT;
  265. property.hint_string=itos(get_visual_script()->get_instance_ID());
  266. } else if (call_mode==CALL_MODE_INSTANCE) {
  267. property.hint=PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  268. property.hint_string=base_type;
  269. } else if (call_mode==CALL_MODE_NODE_PATH) {
  270. Node *node = _get_base_node();
  271. if (node) {
  272. property.hint=PROPERTY_HINT_METHOD_OF_INSTANCE;
  273. property.hint_string=itos(node->get_instance_ID());
  274. } else {
  275. property.hint=PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  276. property.hint_string=get_base_type();
  277. }
  278. }
  279. }
  280. if (property.name=="function/use_default_args") {
  281. property.hint=PROPERTY_HINT_RANGE;
  282. int mc=0;
  283. if (call_mode==CALL_MODE_BASIC_TYPE) {
  284. mc = Variant::get_method_default_arguments(basic_type,function).size();
  285. } else {
  286. MethodBind *mb = ObjectTypeDB::get_method(_get_base_type(),function);
  287. if (mb) {
  288. mc=mb->get_default_argument_count();
  289. }
  290. }
  291. if (mc==0) {
  292. property.usage=0; //do not show
  293. } else {
  294. property.hint_string="0,"+itos(mc)+",1";
  295. }
  296. }
  297. }
  298. void VisualScriptFunctionCall::_bind_methods() {
  299. ObjectTypeDB::bind_method(_MD("set_base_type","base_type"),&VisualScriptFunctionCall::set_base_type);
  300. ObjectTypeDB::bind_method(_MD("get_base_type"),&VisualScriptFunctionCall::get_base_type);
  301. ObjectTypeDB::bind_method(_MD("set_basic_type","basic_type"),&VisualScriptFunctionCall::set_basic_type);
  302. ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptFunctionCall::get_basic_type);
  303. ObjectTypeDB::bind_method(_MD("set_function","function"),&VisualScriptFunctionCall::set_function);
  304. ObjectTypeDB::bind_method(_MD("get_function"),&VisualScriptFunctionCall::get_function);
  305. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptFunctionCall::set_call_mode);
  306. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptFunctionCall::get_call_mode);
  307. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptFunctionCall::set_base_path);
  308. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptFunctionCall::get_base_path);
  309. ObjectTypeDB::bind_method(_MD("set_use_default_args","amount"),&VisualScriptFunctionCall::set_use_default_args);
  310. ObjectTypeDB::bind_method(_MD("get_use_default_args"),&VisualScriptFunctionCall::get_use_default_args);
  311. String bt;
  312. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  313. if (i>0)
  314. bt+=",";
  315. bt+=Variant::get_type_name(Variant::Type(i));
  316. }
  317. ADD_PROPERTY(PropertyInfo(Variant::INT,"function/call_mode",PROPERTY_HINT_ENUM,"Self,Node Path,Instance,Basic Type"),_SCS("set_call_mode"),_SCS("get_call_mode"));
  318. ADD_PROPERTY(PropertyInfo(Variant::STRING,"function/base_type",PROPERTY_HINT_TYPE_STRING,"Object"),_SCS("set_base_type"),_SCS("get_base_type"));
  319. ADD_PROPERTY(PropertyInfo(Variant::INT,"function/basic_type",PROPERTY_HINT_ENUM,bt),_SCS("set_basic_type"),_SCS("get_basic_type"));
  320. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"function/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  321. ADD_PROPERTY(PropertyInfo(Variant::STRING,"function/function"),_SCS("set_function"),_SCS("get_function"));
  322. ADD_PROPERTY(PropertyInfo(Variant::INT,"function/use_default_args"),_SCS("set_use_default_args"),_SCS("get_use_default_args"));
  323. BIND_CONSTANT( CALL_MODE_SELF );
  324. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  325. BIND_CONSTANT( CALL_MODE_INSTANCE);
  326. BIND_CONSTANT( CALL_MODE_BASIC_TYPE );
  327. }
  328. class VisualScriptNodeInstanceFunctionCall : public VisualScriptNodeInstance {
  329. public:
  330. VisualScriptFunctionCall::CallMode call_mode;
  331. NodePath node_path;
  332. int input_args;
  333. bool returns;
  334. StringName function;
  335. VisualScriptFunctionCall *node;
  336. VisualScriptInstance *instance;
  337. //virtual int get_working_memory_size() const { return 0; }
  338. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  339. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  340. 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) {
  341. switch(call_mode) {
  342. case VisualScriptFunctionCall::CALL_MODE_SELF: {
  343. Object *object=instance->get_owner_ptr();
  344. if (returns) {
  345. *p_outputs[0] = object->call(function,p_inputs,input_args,r_error);
  346. } else {
  347. object->call(function,p_inputs,input_args,r_error);
  348. }
  349. } break;
  350. case VisualScriptFunctionCall::CALL_MODE_NODE_PATH: {
  351. Node* node = instance->get_owner_ptr()->cast_to<Node>();
  352. if (!node) {
  353. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  354. r_error_str="Base object is not a Node!";
  355. return 0;
  356. }
  357. Node* another = node->get_node(node_path);
  358. if (!node) {
  359. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  360. r_error_str="Path does not lead Node!";
  361. return 0;
  362. }
  363. if (returns) {
  364. *p_outputs[0] = another->call(function,p_inputs,input_args,r_error);
  365. } else {
  366. another->call(function,p_inputs,input_args,r_error);
  367. }
  368. } break;
  369. case VisualScriptFunctionCall::CALL_MODE_INSTANCE:
  370. case VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE: {
  371. Variant v = *p_inputs[0];
  372. if (returns) {
  373. *p_outputs[0] = v.call(function,p_inputs+1,input_args,r_error);
  374. } else {
  375. v.call(function,p_inputs+1,input_args,r_error);
  376. }
  377. } break;
  378. }
  379. return 0;
  380. }
  381. };
  382. VisualScriptNodeInstance* VisualScriptFunctionCall::instance(VisualScriptInstance* p_instance) {
  383. VisualScriptNodeInstanceFunctionCall * instance = memnew(VisualScriptNodeInstanceFunctionCall );
  384. instance->node=this;
  385. instance->instance=p_instance;
  386. instance->function=function;
  387. instance->call_mode=call_mode;
  388. instance->returns=get_output_value_port_count();
  389. instance->node_path=base_path;
  390. instance->input_args = get_input_value_port_count() - ( (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE) ? 1: 0 );
  391. return instance;
  392. }
  393. VisualScriptFunctionCall::VisualScriptFunctionCall() {
  394. call_mode=CALL_MODE_SELF;
  395. basic_type=Variant::NIL;
  396. use_default_args=0;
  397. base_type="Object";
  398. }
  399. template<VisualScriptFunctionCall::CallMode cmode>
  400. static Ref<VisualScriptNode> create_function_call_node(const String& p_name) {
  401. Ref<VisualScriptFunctionCall> node;
  402. node.instance();
  403. node->set_call_mode(cmode);
  404. return node;
  405. }
  406. //////////////////////////////////////////
  407. ////////////////SET//////////////////////
  408. //////////////////////////////////////////
  409. static const char* event_type_names[InputEvent::TYPE_MAX]={
  410. "None",
  411. "Key",
  412. "MouseMotion",
  413. "MouseButton",
  414. "JoystickMotion",
  415. "JoystickButton",
  416. "ScreenTouch",
  417. "ScreenDrag",
  418. "Action"
  419. };
  420. int VisualScriptPropertySet::get_output_sequence_port_count() const {
  421. return 1;
  422. }
  423. bool VisualScriptPropertySet::has_input_sequence_port() const{
  424. return true;
  425. }
  426. Node *VisualScriptPropertySet::_get_base_node() const {
  427. #ifdef TOOLS_ENABLED
  428. Ref<Script> script = get_visual_script();
  429. if (!script.is_valid())
  430. return NULL;
  431. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  432. if (!main_loop)
  433. return NULL;
  434. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  435. if (!scene_tree)
  436. return NULL;
  437. Node *edited_scene = scene_tree->get_edited_scene_root();
  438. if (!edited_scene)
  439. return NULL;
  440. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  441. if (!script_node)
  442. return NULL;
  443. if (!script_node->has_node(base_path))
  444. return NULL;
  445. Node *path_to = script_node->get_node(base_path);
  446. return path_to;
  447. #else
  448. return NULL;
  449. #endif
  450. }
  451. StringName VisualScriptPropertySet::_get_base_type() const {
  452. if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid())
  453. return get_visual_script()->get_instance_base_type();
  454. else if (call_mode==CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  455. Node *path = _get_base_node();
  456. if (path)
  457. return path->get_type();
  458. }
  459. return base_type;
  460. }
  461. int VisualScriptPropertySet::get_input_value_port_count() const{
  462. int pc = (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE)?1:0;
  463. if (!use_builtin_value)
  464. pc++;
  465. return pc;
  466. }
  467. int VisualScriptPropertySet::get_output_value_port_count() const{
  468. return call_mode==CALL_MODE_BASIC_TYPE? 1 : 0;
  469. }
  470. String VisualScriptPropertySet::get_output_sequence_port_text(int p_port) const {
  471. return String();
  472. }
  473. PropertyInfo VisualScriptPropertySet::get_input_value_port_info(int p_idx) const{
  474. if (call_mode==CALL_MODE_INSTANCE || call_mode==CALL_MODE_BASIC_TYPE) {
  475. if (p_idx==0) {
  476. PropertyInfo pi;
  477. pi.type=(call_mode==CALL_MODE_INSTANCE?Variant::OBJECT:basic_type);
  478. pi.name=(call_mode==CALL_MODE_INSTANCE?String("instance"):Variant::get_type_name(basic_type).to_lower());
  479. return pi;
  480. } else {
  481. p_idx--;
  482. }
  483. }
  484. #ifdef DEBUG_METHODS_ENABLED
  485. //not very efficient but..
  486. List<PropertyInfo> pinfo;
  487. if (call_mode==CALL_MODE_BASIC_TYPE) {
  488. Variant v;
  489. if (basic_type==Variant::INPUT_EVENT) {
  490. InputEvent ev;
  491. ev.type=event_type;
  492. v=ev;
  493. } else {
  494. Variant::CallError ce;
  495. v = Variant::construct(basic_type,NULL,0,ce);
  496. }
  497. v.get_property_list(&pinfo);
  498. } else if (call_mode==CALL_MODE_NODE_PATH) {
  499. Node *n = _get_base_node();
  500. if (n) {
  501. n->get_property_list(&pinfo);
  502. } else {
  503. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  504. }
  505. } else {
  506. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  507. }
  508. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  509. if (E->get().name==property) {
  510. PropertyInfo info=E->get();
  511. info.name="value";
  512. return info;
  513. }
  514. }
  515. #endif
  516. return PropertyInfo(Variant::NIL,"value");
  517. }
  518. PropertyInfo VisualScriptPropertySet::get_output_value_port_info(int p_idx) const{
  519. if (call_mode==CALL_MODE_BASIC_TYPE) {
  520. return PropertyInfo(basic_type,"out");
  521. } else {
  522. return PropertyInfo();
  523. }
  524. }
  525. String VisualScriptPropertySet::get_caption() const {
  526. static const char*cname[4]= {
  527. "SelfSet",
  528. "NodeSet",
  529. "InstanceSet",
  530. "BasicSet"
  531. };
  532. return cname[call_mode];
  533. }
  534. String VisualScriptPropertySet::get_text() const {
  535. String prop;
  536. if (call_mode==CALL_MODE_BASIC_TYPE)
  537. prop=Variant::get_type_name(basic_type)+"."+property;
  538. else
  539. prop=property;
  540. if (use_builtin_value) {
  541. String bit = builtin_value.get_construct_string();
  542. if (bit.length()>40) {
  543. bit=bit.substr(0,40);
  544. bit+="...";
  545. }
  546. prop+="\n "+bit;
  547. }
  548. return prop;
  549. }
  550. void VisualScriptPropertySet::_update_base_type() {
  551. //cache it because this information may not be available on load
  552. if (call_mode==CALL_MODE_NODE_PATH) {
  553. Node* node=_get_base_node();
  554. if (node) {
  555. base_type=node->get_type();
  556. }
  557. } else if (call_mode==CALL_MODE_SELF) {
  558. if (get_visual_script().is_valid()) {
  559. base_type=get_visual_script()->get_instance_base_type();
  560. }
  561. }
  562. }
  563. void VisualScriptPropertySet::set_basic_type(Variant::Type p_type) {
  564. if (basic_type==p_type)
  565. return;
  566. basic_type=p_type;
  567. _change_notify();
  568. _update_base_type();
  569. ports_changed_notify();
  570. }
  571. Variant::Type VisualScriptPropertySet::get_basic_type() const{
  572. return basic_type;
  573. }
  574. void VisualScriptPropertySet::set_event_type(InputEvent::Type p_type) {
  575. if (event_type==p_type)
  576. return;
  577. event_type=p_type;
  578. _change_notify();
  579. _update_base_type();
  580. ports_changed_notify();
  581. }
  582. InputEvent::Type VisualScriptPropertySet::get_event_type() const{
  583. return event_type;
  584. }
  585. void VisualScriptPropertySet::set_base_type(const StringName& p_type) {
  586. if (base_type==p_type)
  587. return;
  588. base_type=p_type;
  589. _change_notify();
  590. ports_changed_notify();
  591. }
  592. StringName VisualScriptPropertySet::get_base_type() const{
  593. return base_type;
  594. }
  595. void VisualScriptPropertySet::set_property(const StringName& p_type){
  596. if (property==p_type)
  597. return;
  598. property=p_type;
  599. _change_notify();
  600. ports_changed_notify();
  601. }
  602. StringName VisualScriptPropertySet::get_property() const {
  603. return property;
  604. }
  605. void VisualScriptPropertySet::set_base_path(const NodePath& p_type) {
  606. if (base_path==p_type)
  607. return;
  608. base_path=p_type;
  609. _update_base_type();
  610. _change_notify();
  611. ports_changed_notify();
  612. }
  613. NodePath VisualScriptPropertySet::get_base_path() const {
  614. return base_path;
  615. }
  616. void VisualScriptPropertySet::set_call_mode(CallMode p_mode) {
  617. if (call_mode==p_mode)
  618. return;
  619. call_mode=p_mode;
  620. _update_base_type();
  621. _change_notify();
  622. ports_changed_notify();
  623. }
  624. VisualScriptPropertySet::CallMode VisualScriptPropertySet::get_call_mode() const {
  625. return call_mode;
  626. }
  627. void VisualScriptPropertySet::set_use_builtin_value(bool p_use) {
  628. if (use_builtin_value==p_use)
  629. return;
  630. use_builtin_value=p_use;
  631. _change_notify();
  632. ports_changed_notify();
  633. }
  634. bool VisualScriptPropertySet::is_using_builtin_value() const{
  635. return use_builtin_value;
  636. }
  637. void VisualScriptPropertySet::set_builtin_value(const Variant& p_value){
  638. if (builtin_value==p_value)
  639. return;
  640. builtin_value=p_value;
  641. }
  642. Variant VisualScriptPropertySet::get_builtin_value() const{
  643. return builtin_value;
  644. }
  645. void VisualScriptPropertySet::_validate_property(PropertyInfo& property) const {
  646. if (property.name=="property/base_type") {
  647. if (call_mode!=CALL_MODE_INSTANCE) {
  648. property.usage=PROPERTY_USAGE_NOEDITOR;
  649. }
  650. }
  651. if (property.name=="property/basic_type") {
  652. if (call_mode!=CALL_MODE_BASIC_TYPE) {
  653. property.usage=0;
  654. }
  655. }
  656. if (property.name=="property/event_type") {
  657. if (call_mode!=CALL_MODE_BASIC_TYPE || basic_type!=Variant::INPUT_EVENT) {
  658. property.usage=0;
  659. }
  660. }
  661. if (property.name=="property/node_path") {
  662. if (call_mode!=CALL_MODE_NODE_PATH) {
  663. property.usage=0;
  664. } else {
  665. Node *bnode = _get_base_node();
  666. if (bnode) {
  667. property.hint_string=bnode->get_path(); //convert to loong string
  668. } else {
  669. }
  670. }
  671. }
  672. if (property.name=="property/property") {
  673. if (call_mode==CALL_MODE_BASIC_TYPE) {
  674. property.hint=PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  675. property.hint_string=Variant::get_type_name(basic_type);
  676. } else if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid()) {
  677. property.hint=PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  678. property.hint_string=itos(get_visual_script()->get_instance_ID());
  679. } else if (call_mode==CALL_MODE_INSTANCE) {
  680. property.hint=PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  681. property.hint_string=base_type;
  682. } else if (call_mode==CALL_MODE_NODE_PATH) {
  683. Node *node = _get_base_node();
  684. if (node) {
  685. property.hint=PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  686. property.hint_string=itos(node->get_instance_ID());
  687. } else {
  688. property.hint=PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  689. property.hint_string=get_base_type();
  690. }
  691. }
  692. }
  693. if (property.name=="value/builtin") {
  694. if (!use_builtin_value) {
  695. property.usage=0;
  696. } else {
  697. List<PropertyInfo> pinfo;
  698. if (call_mode==CALL_MODE_BASIC_TYPE) {
  699. Variant::CallError ce;
  700. Variant v = Variant::construct(basic_type,NULL,0,ce);
  701. v.get_property_list(&pinfo);
  702. } else if (call_mode==CALL_MODE_NODE_PATH) {
  703. Node *n = _get_base_node();
  704. if (n) {
  705. n->get_property_list(&pinfo);
  706. } else {
  707. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  708. }
  709. } else {
  710. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  711. }
  712. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  713. if (E->get().name==this->property) {
  714. property.hint=E->get().hint;
  715. property.type=E->get().type;
  716. property.hint_string=E->get().hint_string;
  717. }
  718. }
  719. }
  720. }
  721. }
  722. void VisualScriptPropertySet::_bind_methods() {
  723. ObjectTypeDB::bind_method(_MD("set_base_type","base_type"),&VisualScriptPropertySet::set_base_type);
  724. ObjectTypeDB::bind_method(_MD("get_base_type"),&VisualScriptPropertySet::get_base_type);
  725. ObjectTypeDB::bind_method(_MD("set_basic_type","basic_type"),&VisualScriptPropertySet::set_basic_type);
  726. ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptPropertySet::get_basic_type);
  727. ObjectTypeDB::bind_method(_MD("set_event_type","event_type"),&VisualScriptPropertySet::set_event_type);
  728. ObjectTypeDB::bind_method(_MD("get_event_type"),&VisualScriptPropertySet::get_event_type);
  729. ObjectTypeDB::bind_method(_MD("set_property","property"),&VisualScriptPropertySet::set_property);
  730. ObjectTypeDB::bind_method(_MD("get_property"),&VisualScriptPropertySet::get_property);
  731. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptPropertySet::set_call_mode);
  732. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptPropertySet::get_call_mode);
  733. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptPropertySet::set_base_path);
  734. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptPropertySet::get_base_path);
  735. ObjectTypeDB::bind_method(_MD("set_builtin_value","value"),&VisualScriptPropertySet::set_builtin_value);
  736. ObjectTypeDB::bind_method(_MD("get_builtin_value"),&VisualScriptPropertySet::get_builtin_value);
  737. ObjectTypeDB::bind_method(_MD("set_use_builtin_value","enable"),&VisualScriptPropertySet::set_use_builtin_value);
  738. ObjectTypeDB::bind_method(_MD("is_using_builtin_value"),&VisualScriptPropertySet::is_using_builtin_value);
  739. String bt;
  740. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  741. if (i>0)
  742. bt+=",";
  743. bt+=Variant::get_type_name(Variant::Type(i));
  744. }
  745. String et;
  746. for(int i=0;i<InputEvent::TYPE_MAX;i++) {
  747. if (i>0)
  748. et+=",";
  749. et+=event_type_names[i];
  750. }
  751. 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"));
  752. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_type",PROPERTY_HINT_TYPE_STRING,"Object"),_SCS("set_base_type"),_SCS("get_base_type"));
  753. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/basic_type",PROPERTY_HINT_ENUM,bt),_SCS("set_basic_type"),_SCS("get_basic_type"));
  754. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/event_type",PROPERTY_HINT_ENUM,et),_SCS("set_event_type"),_SCS("get_event_type"));
  755. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"property/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  756. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/property"),_SCS("set_property"),_SCS("get_property"));
  757. ADD_PROPERTY(PropertyInfo(Variant::BOOL,"value/use_builtin"),_SCS("set_use_builtin_value"),_SCS("is_using_builtin_value"));
  758. ADD_PROPERTY(PropertyInfo(Variant::NIL,"value/builtin"),_SCS("set_builtin_value"),_SCS("get_builtin_value"));
  759. BIND_CONSTANT( CALL_MODE_SELF );
  760. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  761. BIND_CONSTANT( CALL_MODE_INSTANCE);
  762. }
  763. class VisualScriptNodeInstancePropertySet : public VisualScriptNodeInstance {
  764. public:
  765. VisualScriptPropertySet::CallMode call_mode;
  766. NodePath node_path;
  767. StringName property;
  768. bool use_builtin;
  769. Variant builtin_val;
  770. VisualScriptPropertySet *node;
  771. VisualScriptInstance *instance;
  772. //virtual int get_working_memory_size() const { return 0; }
  773. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  774. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  775. 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) {
  776. switch(call_mode) {
  777. case VisualScriptPropertySet::CALL_MODE_SELF: {
  778. Object *object=instance->get_owner_ptr();
  779. bool valid;
  780. if (use_builtin) {
  781. object->set(property,builtin_val,&valid);
  782. } else {
  783. object->set(property,*p_inputs[0],&valid);
  784. }
  785. if (!valid) {
  786. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  787. r_error_str="Invalid index property name.";
  788. }
  789. } break;
  790. case VisualScriptPropertySet::CALL_MODE_NODE_PATH: {
  791. Node* node = instance->get_owner_ptr()->cast_to<Node>();
  792. if (!node) {
  793. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  794. r_error_str="Base object is not a Node!";
  795. return 0;
  796. }
  797. Node* another = node->get_node(node_path);
  798. if (!node) {
  799. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  800. r_error_str="Path does not lead Node!";
  801. return 0;
  802. }
  803. bool valid;
  804. if (use_builtin) {
  805. another->set(property,builtin_val,&valid);
  806. } else {
  807. another->set(property,*p_inputs[0],&valid);
  808. }
  809. if (!valid) {
  810. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  811. r_error_str="Invalid index property name.";
  812. }
  813. } break;
  814. case VisualScriptPropertySet::CALL_MODE_INSTANCE:
  815. case VisualScriptPropertySet::CALL_MODE_BASIC_TYPE: {
  816. Variant v = *p_inputs[0];
  817. bool valid;
  818. if (use_builtin) {
  819. v.set(property,builtin_val,&valid);
  820. } else {
  821. v.set(property,p_inputs[1],&valid);
  822. }
  823. if (!valid) {
  824. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  825. r_error_str="Invalid index property name.";
  826. }
  827. if (call_mode==VisualScriptPropertySet::CALL_MODE_BASIC_TYPE) {
  828. *p_outputs[0]=v;
  829. }
  830. } break;
  831. }
  832. return 0;
  833. }
  834. };
  835. VisualScriptNodeInstance* VisualScriptPropertySet::instance(VisualScriptInstance* p_instance) {
  836. VisualScriptNodeInstancePropertySet * instance = memnew(VisualScriptNodeInstancePropertySet );
  837. instance->node=this;
  838. instance->instance=p_instance;
  839. instance->property=property;
  840. instance->call_mode=call_mode;
  841. instance->node_path=base_path;
  842. instance->use_builtin=use_builtin_value;
  843. instance->builtin_val=builtin_value;
  844. return instance;
  845. }
  846. VisualScriptPropertySet::VisualScriptPropertySet() {
  847. call_mode=CALL_MODE_SELF;
  848. base_type="Object";
  849. basic_type=Variant::NIL;
  850. event_type=InputEvent::NONE;
  851. }
  852. template<VisualScriptPropertySet::CallMode cmode>
  853. static Ref<VisualScriptNode> create_property_set_node(const String& p_name) {
  854. Ref<VisualScriptPropertySet> node;
  855. node.instance();
  856. node->set_call_mode(cmode);
  857. return node;
  858. }
  859. //////////////////////////////////////////
  860. ////////////////GET//////////////////////
  861. //////////////////////////////////////////
  862. int VisualScriptPropertyGet::get_output_sequence_port_count() const {
  863. return (call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?0:1;
  864. }
  865. bool VisualScriptPropertyGet::has_input_sequence_port() const{
  866. return (call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?false:true;
  867. }
  868. void VisualScriptPropertyGet::_update_base_type() {
  869. //cache it because this information may not be available on load
  870. if (call_mode==CALL_MODE_NODE_PATH) {
  871. Node* node=_get_base_node();
  872. if (node) {
  873. base_type=node->get_type();
  874. }
  875. } else if (call_mode==CALL_MODE_SELF) {
  876. if (get_visual_script().is_valid()) {
  877. base_type=get_visual_script()->get_instance_base_type();
  878. }
  879. }
  880. }
  881. Node *VisualScriptPropertyGet::_get_base_node() const {
  882. #ifdef TOOLS_ENABLED
  883. Ref<Script> script = get_visual_script();
  884. if (!script.is_valid())
  885. return NULL;
  886. MainLoop * main_loop = OS::get_singleton()->get_main_loop();
  887. if (!main_loop)
  888. return NULL;
  889. SceneTree *scene_tree = main_loop->cast_to<SceneTree>();
  890. if (!scene_tree)
  891. return NULL;
  892. Node *edited_scene = scene_tree->get_edited_scene_root();
  893. if (!edited_scene)
  894. return NULL;
  895. Node* script_node = _find_script_node(edited_scene,edited_scene,script);
  896. if (!script_node)
  897. return NULL;
  898. if (!script_node->has_node(base_path))
  899. return NULL;
  900. Node *path_to = script_node->get_node(base_path);
  901. return path_to;
  902. #else
  903. return NULL;
  904. #endif
  905. }
  906. StringName VisualScriptPropertyGet::_get_base_type() const {
  907. if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid())
  908. return get_visual_script()->get_instance_base_type();
  909. else if (call_mode==CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  910. Node *path = _get_base_node();
  911. if (path)
  912. return path->get_type();
  913. }
  914. return base_type;
  915. }
  916. int VisualScriptPropertyGet::get_input_value_port_count() const{
  917. return (call_mode==CALL_MODE_BASIC_TYPE || call_mode==CALL_MODE_INSTANCE)?1:0;
  918. }
  919. int VisualScriptPropertyGet::get_output_value_port_count() const{
  920. return 1;
  921. }
  922. String VisualScriptPropertyGet::get_output_sequence_port_text(int p_port) const {
  923. return String();
  924. }
  925. PropertyInfo VisualScriptPropertyGet::get_input_value_port_info(int p_idx) const{
  926. if (call_mode==CALL_MODE_INSTANCE || call_mode==CALL_MODE_BASIC_TYPE) {
  927. if (p_idx==0) {
  928. PropertyInfo pi;
  929. pi.type=(call_mode==CALL_MODE_INSTANCE?Variant::OBJECT:basic_type);
  930. pi.name=(call_mode==CALL_MODE_INSTANCE?String("instance"):Variant::get_type_name(basic_type).to_lower());
  931. return pi;
  932. } else {
  933. p_idx--;
  934. }
  935. }
  936. return PropertyInfo();
  937. }
  938. PropertyInfo VisualScriptPropertyGet::get_output_value_port_info(int p_idx) const{
  939. #ifdef DEBUG_METHODS_ENABLED
  940. //not very efficient but..
  941. List<PropertyInfo> pinfo;
  942. if (call_mode==CALL_MODE_BASIC_TYPE) {
  943. Variant v;
  944. if (basic_type==Variant::INPUT_EVENT) {
  945. InputEvent ev;
  946. ev.type=event_type;
  947. v=ev;
  948. } else {
  949. Variant::CallError ce;
  950. v = Variant::construct(basic_type,NULL,0,ce);
  951. }
  952. v.get_property_list(&pinfo);
  953. } else if (call_mode==CALL_MODE_NODE_PATH) {
  954. Node *n = _get_base_node();
  955. if (n) {
  956. n->get_property_list(&pinfo);
  957. } else {
  958. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  959. }
  960. } else {
  961. ObjectTypeDB::get_property_list(_get_base_type(),&pinfo);
  962. }
  963. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  964. if (E->get().name==property) {
  965. PropertyInfo info=E->get();
  966. info.name="";
  967. return info;
  968. }
  969. }
  970. #endif
  971. return PropertyInfo(Variant::NIL,"");
  972. }
  973. String VisualScriptPropertyGet::get_caption() const {
  974. static const char*cname[4]= {
  975. "SelfGet",
  976. "NodeGet",
  977. "InstanceGet",
  978. "BasicGet"
  979. };
  980. return cname[call_mode];
  981. }
  982. String VisualScriptPropertyGet::get_text() const {
  983. if (call_mode==CALL_MODE_BASIC_TYPE)
  984. return Variant::get_type_name(basic_type)+"."+property;
  985. else
  986. return property;
  987. }
  988. void VisualScriptPropertyGet::set_base_type(const StringName& p_type) {
  989. if (base_type==p_type)
  990. return;
  991. base_type=p_type;
  992. _change_notify();
  993. ports_changed_notify();
  994. }
  995. StringName VisualScriptPropertyGet::get_base_type() const{
  996. return base_type;
  997. }
  998. void VisualScriptPropertyGet::set_property(const StringName& p_type){
  999. if (property==p_type)
  1000. return;
  1001. property=p_type;
  1002. _change_notify();
  1003. ports_changed_notify();
  1004. }
  1005. StringName VisualScriptPropertyGet::get_property() const {
  1006. return property;
  1007. }
  1008. void VisualScriptPropertyGet::set_base_path(const NodePath& p_type) {
  1009. if (base_path==p_type)
  1010. return;
  1011. base_path=p_type;
  1012. _change_notify();
  1013. _update_base_type();
  1014. ports_changed_notify();
  1015. }
  1016. NodePath VisualScriptPropertyGet::get_base_path() const {
  1017. return base_path;
  1018. }
  1019. void VisualScriptPropertyGet::set_call_mode(CallMode p_mode) {
  1020. if (call_mode==p_mode)
  1021. return;
  1022. call_mode=p_mode;
  1023. _change_notify();
  1024. _update_base_type();
  1025. ports_changed_notify();
  1026. }
  1027. VisualScriptPropertyGet::CallMode VisualScriptPropertyGet::get_call_mode() const {
  1028. return call_mode;
  1029. }
  1030. void VisualScriptPropertyGet::set_basic_type(Variant::Type p_type) {
  1031. if (basic_type==p_type)
  1032. return;
  1033. basic_type=p_type;
  1034. _change_notify();
  1035. ports_changed_notify();
  1036. }
  1037. Variant::Type VisualScriptPropertyGet::get_basic_type() const{
  1038. return basic_type;
  1039. }
  1040. void VisualScriptPropertyGet::set_event_type(InputEvent::Type p_type) {
  1041. if (event_type==p_type)
  1042. return;
  1043. event_type=p_type;
  1044. _change_notify();
  1045. _update_base_type();
  1046. ports_changed_notify();
  1047. }
  1048. InputEvent::Type VisualScriptPropertyGet::get_event_type() const{
  1049. return event_type;
  1050. }
  1051. void VisualScriptPropertyGet::_validate_property(PropertyInfo& property) const {
  1052. if (property.name=="property/base_type") {
  1053. if (call_mode!=CALL_MODE_INSTANCE) {
  1054. property.usage=PROPERTY_USAGE_NOEDITOR;
  1055. }
  1056. }
  1057. if (property.name=="property/basic_type") {
  1058. if (call_mode!=CALL_MODE_BASIC_TYPE) {
  1059. property.usage=0;
  1060. }
  1061. }
  1062. if (property.name=="property/event_type") {
  1063. if (call_mode!=CALL_MODE_BASIC_TYPE || basic_type!=Variant::INPUT_EVENT) {
  1064. property.usage=0;
  1065. }
  1066. }
  1067. if (property.name=="property/node_path") {
  1068. if (call_mode!=CALL_MODE_NODE_PATH) {
  1069. property.usage=0;
  1070. } else {
  1071. Node *bnode = _get_base_node();
  1072. if (bnode) {
  1073. property.hint_string=bnode->get_path(); //convert to loong string
  1074. } else {
  1075. }
  1076. }
  1077. }
  1078. if (property.name=="property/property") {
  1079. if (call_mode==CALL_MODE_BASIC_TYPE) {
  1080. property.hint=PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  1081. property.hint_string=Variant::get_type_name(basic_type);
  1082. } else if (call_mode==CALL_MODE_SELF && get_visual_script().is_valid()) {
  1083. property.hint=PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1084. property.hint_string=itos(get_visual_script()->get_instance_ID());
  1085. } else if (call_mode==CALL_MODE_INSTANCE) {
  1086. property.hint=PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1087. property.hint_string=base_type;
  1088. } else if (call_mode==CALL_MODE_NODE_PATH) {
  1089. Node *node = _get_base_node();
  1090. if (node) {
  1091. property.hint=PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  1092. property.hint_string=itos(node->get_instance_ID());
  1093. } else {
  1094. property.hint=PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1095. property.hint_string=get_base_type();
  1096. }
  1097. }
  1098. }
  1099. }
  1100. void VisualScriptPropertyGet::_bind_methods() {
  1101. ObjectTypeDB::bind_method(_MD("set_base_type","base_type"),&VisualScriptPropertyGet::set_base_type);
  1102. ObjectTypeDB::bind_method(_MD("get_base_type"),&VisualScriptPropertyGet::get_base_type);
  1103. ObjectTypeDB::bind_method(_MD("set_basic_type","basic_type"),&VisualScriptPropertyGet::set_basic_type);
  1104. ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptPropertyGet::get_basic_type);
  1105. ObjectTypeDB::bind_method(_MD("set_event_type","event_type"),&VisualScriptPropertyGet::set_event_type);
  1106. ObjectTypeDB::bind_method(_MD("get_event_type"),&VisualScriptPropertyGet::get_event_type);
  1107. ObjectTypeDB::bind_method(_MD("set_property","property"),&VisualScriptPropertyGet::set_property);
  1108. ObjectTypeDB::bind_method(_MD("get_property"),&VisualScriptPropertyGet::get_property);
  1109. ObjectTypeDB::bind_method(_MD("set_call_mode","mode"),&VisualScriptPropertyGet::set_call_mode);
  1110. ObjectTypeDB::bind_method(_MD("get_call_mode"),&VisualScriptPropertyGet::get_call_mode);
  1111. ObjectTypeDB::bind_method(_MD("set_base_path","base_path"),&VisualScriptPropertyGet::set_base_path);
  1112. ObjectTypeDB::bind_method(_MD("get_base_path"),&VisualScriptPropertyGet::get_base_path);
  1113. String bt;
  1114. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1115. if (i>0)
  1116. bt+=",";
  1117. bt+=Variant::get_type_name(Variant::Type(i));
  1118. }
  1119. String et;
  1120. for(int i=0;i<InputEvent::TYPE_MAX;i++) {
  1121. if (i>0)
  1122. et+=",";
  1123. et+=event_type_names[i];
  1124. }
  1125. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/set_mode",PROPERTY_HINT_ENUM,"Self,Node Path,Instance"),_SCS("set_call_mode"),_SCS("get_call_mode"));
  1126. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/base_type",PROPERTY_HINT_TYPE_STRING,"Object"),_SCS("set_base_type"),_SCS("get_base_type"));
  1127. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/basic_type",PROPERTY_HINT_ENUM,bt),_SCS("set_basic_type"),_SCS("get_basic_type"));
  1128. ADD_PROPERTY(PropertyInfo(Variant::INT,"property/event_type",PROPERTY_HINT_ENUM,et),_SCS("set_event_type"),_SCS("get_event_type"));
  1129. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH,"property/node_path",PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE),_SCS("set_base_path"),_SCS("get_base_path"));
  1130. ADD_PROPERTY(PropertyInfo(Variant::STRING,"property/property"),_SCS("set_property"),_SCS("get_property"));
  1131. BIND_CONSTANT( CALL_MODE_SELF );
  1132. BIND_CONSTANT( CALL_MODE_NODE_PATH);
  1133. BIND_CONSTANT( CALL_MODE_INSTANCE);
  1134. }
  1135. class VisualScriptNodeInstancePropertyGet : public VisualScriptNodeInstance {
  1136. public:
  1137. VisualScriptPropertyGet::CallMode call_mode;
  1138. NodePath node_path;
  1139. StringName property;
  1140. VisualScriptPropertyGet *node;
  1141. VisualScriptInstance *instance;
  1142. //virtual int get_working_memory_size() const { return 0; }
  1143. virtual bool is_output_port_unsequenced(int p_idx) const { return (call_mode==VisualScriptPropertyGet::CALL_MODE_SELF || call_mode==VisualScriptPropertyGet::CALL_MODE_NODE_PATH); }
  1144. virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const {
  1145. //these two modes can be get directly, so they use unsequenced mode
  1146. switch(call_mode) {
  1147. case VisualScriptPropertyGet::CALL_MODE_SELF: {
  1148. Object *object=instance->get_owner_ptr();
  1149. bool valid;
  1150. *r_value = object->get(property,&valid);
  1151. if (!valid) {
  1152. //r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1153. r_error=RTR("Invalid index property name.");
  1154. return false;
  1155. }
  1156. } break;
  1157. case VisualScriptPropertyGet::CALL_MODE_NODE_PATH: {
  1158. Node* node = instance->get_owner_ptr()->cast_to<Node>();
  1159. if (!node) {
  1160. //r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1161. r_error=RTR("Base object is not a Node!");
  1162. return false;
  1163. }
  1164. Node* another = node->get_node(node_path);
  1165. if (!node) {
  1166. //r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1167. r_error=RTR("Path does not lead Node!");
  1168. return false;
  1169. }
  1170. bool valid;
  1171. *r_value = another->get(property,&valid);
  1172. if (!valid) {
  1173. //r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1174. r_error=vformat(RTR("Invalid index property name '%s' in node %s."),String(property),another->get_name());
  1175. return false;
  1176. }
  1177. } break;
  1178. default: {};
  1179. }
  1180. return true;
  1181. }
  1182. 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) {
  1183. bool valid;
  1184. Variant v = *p_inputs[0];
  1185. *p_outputs[0] = v.get(property,&valid);
  1186. if (!valid) {
  1187. r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1188. r_error_str=RTR("Invalid index property name.");
  1189. }
  1190. return 0;
  1191. }
  1192. };
  1193. VisualScriptNodeInstance* VisualScriptPropertyGet::instance(VisualScriptInstance* p_instance) {
  1194. VisualScriptNodeInstancePropertyGet * instance = memnew(VisualScriptNodeInstancePropertyGet );
  1195. instance->node=this;
  1196. instance->instance=p_instance;
  1197. instance->property=property;
  1198. instance->call_mode=call_mode;
  1199. instance->node_path=base_path;
  1200. return instance;
  1201. }
  1202. VisualScriptPropertyGet::VisualScriptPropertyGet() {
  1203. call_mode=CALL_MODE_SELF;
  1204. base_type="Object";
  1205. basic_type=Variant::NIL;
  1206. event_type=InputEvent::NONE;
  1207. }
  1208. template<VisualScriptPropertyGet::CallMode cmode>
  1209. static Ref<VisualScriptNode> create_property_get_node(const String& p_name) {
  1210. Ref<VisualScriptPropertyGet> node;
  1211. node.instance();
  1212. node->set_call_mode(cmode);
  1213. return node;
  1214. }
  1215. //////////////////////////////////////////
  1216. ////////////////EMIT//////////////////////
  1217. //////////////////////////////////////////
  1218. int VisualScriptEmitSignal::get_output_sequence_port_count() const {
  1219. return 1;
  1220. }
  1221. bool VisualScriptEmitSignal::has_input_sequence_port() const{
  1222. return true;
  1223. }
  1224. int VisualScriptEmitSignal::get_input_value_port_count() const{
  1225. Ref<VisualScript> vs = get_visual_script();
  1226. if (vs.is_valid()) {
  1227. if (!vs->has_custom_signal(name))
  1228. return 0;
  1229. return vs->custom_signal_get_argument_count(name);
  1230. }
  1231. return 0;
  1232. }
  1233. int VisualScriptEmitSignal::get_output_value_port_count() const{
  1234. return 0;
  1235. }
  1236. String VisualScriptEmitSignal::get_output_sequence_port_text(int p_port) const {
  1237. return String();
  1238. }
  1239. PropertyInfo VisualScriptEmitSignal::get_input_value_port_info(int p_idx) const{
  1240. Ref<VisualScript> vs = get_visual_script();
  1241. if (vs.is_valid()) {
  1242. if (!vs->has_custom_signal(name))
  1243. return PropertyInfo();
  1244. return PropertyInfo(vs->custom_signal_get_argument_type(name,p_idx),vs->custom_signal_get_argument_name(name,p_idx));
  1245. }
  1246. return PropertyInfo();
  1247. }
  1248. PropertyInfo VisualScriptEmitSignal::get_output_value_port_info(int p_idx) const{
  1249. return PropertyInfo();
  1250. }
  1251. String VisualScriptEmitSignal::get_caption() const {
  1252. return "EmitSignal";
  1253. }
  1254. String VisualScriptEmitSignal::get_text() const {
  1255. return "emit "+String(name);
  1256. }
  1257. void VisualScriptEmitSignal::set_signal(const StringName& p_type){
  1258. if (name==p_type)
  1259. return;
  1260. name=p_type;
  1261. _change_notify();
  1262. ports_changed_notify();
  1263. }
  1264. StringName VisualScriptEmitSignal::get_signal() const {
  1265. return name;
  1266. }
  1267. void VisualScriptEmitSignal::_validate_property(PropertyInfo& property) const {
  1268. if (property.name=="signal/signal") {
  1269. property.hint=PROPERTY_HINT_ENUM;
  1270. List<StringName> sigs;
  1271. Ref<VisualScript> vs = get_visual_script();
  1272. if (vs.is_valid()) {
  1273. vs->get_custom_signal_list(&sigs);
  1274. }
  1275. String ml;
  1276. for (List<StringName>::Element *E=sigs.front();E;E=E->next()) {
  1277. if (ml!=String())
  1278. ml+=",";
  1279. ml+=E->get();
  1280. }
  1281. property.hint_string=ml;
  1282. }
  1283. }
  1284. void VisualScriptEmitSignal::_bind_methods() {
  1285. ObjectTypeDB::bind_method(_MD("set_signal","name"),&VisualScriptEmitSignal::set_signal);
  1286. ObjectTypeDB::bind_method(_MD("get_signal"),&VisualScriptEmitSignal::get_signal);
  1287. ADD_PROPERTY(PropertyInfo(Variant::STRING,"signal/signal"),_SCS("set_signal"),_SCS("get_signal"));
  1288. }
  1289. class VisualScriptNodeInstanceEmitSignal : public VisualScriptNodeInstance {
  1290. public:
  1291. VisualScriptEmitSignal *node;
  1292. VisualScriptInstance *instance;
  1293. int argcount;
  1294. StringName name;
  1295. //virtual int get_working_memory_size() const { return 0; }
  1296. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1297. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1298. 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) {
  1299. Object *obj = instance->get_owner_ptr();
  1300. obj->emit_signal(name,p_inputs,argcount);
  1301. return 0;
  1302. }
  1303. };
  1304. VisualScriptNodeInstance* VisualScriptEmitSignal::instance(VisualScriptInstance* p_instance) {
  1305. VisualScriptNodeInstanceEmitSignal * instance = memnew(VisualScriptNodeInstanceEmitSignal );
  1306. instance->node=this;
  1307. instance->instance=p_instance;
  1308. instance->name=name;
  1309. instance->argcount=get_input_value_port_count();
  1310. return instance;
  1311. }
  1312. VisualScriptEmitSignal::VisualScriptEmitSignal() {
  1313. }
  1314. static Ref<VisualScriptNode> create_basic_type_call_node(const String& p_name) {
  1315. Vector<String> path = p_name.split("/");
  1316. ERR_FAIL_COND_V(path.size()<4,Ref<VisualScriptNode>());
  1317. String base_type = path[2];
  1318. String method = path[3];
  1319. Ref<VisualScriptFunctionCall> node;
  1320. node.instance();
  1321. Variant::Type type=Variant::VARIANT_MAX;
  1322. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1323. if (Variant::get_type_name(Variant::Type(i))==base_type) {
  1324. type=Variant::Type(i);
  1325. break;
  1326. }
  1327. }
  1328. ERR_FAIL_COND_V(type==Variant::VARIANT_MAX,Ref<VisualScriptNode>());
  1329. node->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  1330. node->set_basic_type(type);
  1331. node->set_function(method);
  1332. return node;
  1333. }
  1334. void register_visual_script_func_nodes() {
  1335. VisualScriptLanguage::singleton->add_register_func("functions/call",create_node_generic<VisualScriptFunctionCall>);
  1336. VisualScriptLanguage::singleton->add_register_func("functions/set",create_node_generic<VisualScriptPropertySet>);
  1337. VisualScriptLanguage::singleton->add_register_func("functions/get",create_node_generic<VisualScriptPropertyGet>);
  1338. //VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_self",create_script_call_node<VisualScriptScriptCall::CALL_MODE_SELF>);
  1339. // VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_node",create_script_call_node<VisualScriptScriptCall::CALL_MODE_NODE_PATH>);
  1340. VisualScriptLanguage::singleton->add_register_func("functions/emit_signal",create_node_generic<VisualScriptEmitSignal>);
  1341. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  1342. Variant::Type t = Variant::Type(i);
  1343. String type_name = Variant::get_type_name(t);
  1344. Variant::CallError ce;
  1345. Variant vt = Variant::construct(t,NULL,0,ce);
  1346. List<MethodInfo> ml;
  1347. vt.get_method_list(&ml);
  1348. for (List<MethodInfo>::Element *E=ml.front();E;E=E->next()) {
  1349. VisualScriptLanguage::singleton->add_register_func("functions/by_type/"+type_name+"/"+E->get().name,create_basic_type_call_node);
  1350. }
  1351. }
  1352. }