visual_script.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. #include "visual_script.h"
  2. #include "visual_script_nodes.h"
  3. void VisualScriptNode::_notification(int p_what) {
  4. if (p_what==NOTIFICATION_POSTINITIALIZE) {
  5. int dvc = get_input_value_port_count();
  6. for(int i=0;i<dvc;i++) {
  7. Variant::Type expected = get_input_value_port_info(i).type;
  8. Variant::CallError ce;
  9. default_input_values.push_back(Variant::construct(expected,NULL,0,ce,false));
  10. }
  11. }
  12. }
  13. void VisualScriptNode::ports_changed_notify(){
  14. default_input_values.resize( MAX(default_input_values.size(),get_input_value_port_count()) ); //let it grow as big as possible, we don't want to lose values on resize
  15. emit_signal("ports_changed");
  16. }
  17. void VisualScriptNode::set_default_input_value(int p_port,const Variant& p_value) {
  18. ERR_FAIL_INDEX(p_port,default_input_values.size());
  19. default_input_values[p_port]=p_value;
  20. }
  21. Variant VisualScriptNode::get_default_input_value(int p_port) const {
  22. ERR_FAIL_INDEX_V(p_port,default_input_values.size(),Variant());
  23. return default_input_values[p_port];
  24. }
  25. void VisualScriptNode::_set_default_input_values(Array p_values) {
  26. default_input_values=p_values;
  27. }
  28. Array VisualScriptNode::_get_default_input_values() const {
  29. //validate on save, since on load there is little info about this
  30. Array saved_values;
  31. //actually validate on save
  32. for(int i=0;i<get_input_value_port_count();i++) {
  33. Variant::Type expected = get_input_value_port_info(i).type;
  34. if (i>=default_input_values.size()) {
  35. Variant::CallError ce;
  36. saved_values.push_back(Variant::construct(expected,NULL,0,ce,false));
  37. } else {
  38. if (expected==Variant::NIL || expected==default_input_values[i].get_type()) {
  39. saved_values.push_back(default_input_values[i]);
  40. } else {
  41. //not the same, reconvert
  42. Variant::CallError ce;
  43. Variant existing = default_input_values[i];
  44. const Variant *existingp=&existing;
  45. saved_values.push_back( Variant::construct(expected,&existingp,1,ce,false) );
  46. }
  47. }
  48. }
  49. return saved_values;
  50. }
  51. void VisualScriptNode::_bind_methods() {
  52. ObjectTypeDB::bind_method(_MD("get_visual_script:VisualScript"),&VisualScriptNode::get_visual_script);
  53. ObjectTypeDB::bind_method(_MD("set_default_input_value","port_idx","value:Variant"),&VisualScriptNode::set_default_input_value);
  54. ObjectTypeDB::bind_method(_MD("get_default_input_value:Variant","port_idx"),&VisualScriptNode::get_default_input_value);
  55. ObjectTypeDB::bind_method(_MD("_set_default_input_values","values"),&VisualScriptNode::_set_default_input_values);
  56. ObjectTypeDB::bind_method(_MD("_get_default_input_values"),&VisualScriptNode::_get_default_input_values);
  57. ADD_PROPERTY(PropertyInfo(Variant::ARRAY,"_default_input_values",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_default_input_values"),_SCS("_get_default_input_values"));
  58. ADD_SIGNAL(MethodInfo("ports_changed"));
  59. }
  60. Ref<VisualScript> VisualScriptNode::get_visual_script() const {
  61. if (scripts_used.size())
  62. return Ref<VisualScript>(scripts_used.front()->get());
  63. return Ref<VisualScript>();
  64. }
  65. ////////////////
  66. /////////////////////
  67. VisualScriptNodeInstance::~VisualScriptNodeInstance() {
  68. }
  69. void VisualScript::add_function(const StringName& p_name) {
  70. ERR_FAIL_COND(!String(p_name).is_valid_identifier());
  71. ERR_FAIL_COND(functions.has(p_name));
  72. functions[p_name]=Function();
  73. }
  74. bool VisualScript::has_function(const StringName& p_name) const {
  75. return functions.has(p_name);
  76. }
  77. void VisualScript::remove_function(const StringName& p_name) {
  78. ERR_FAIL_COND(!functions.has(p_name));
  79. for (Map<int,Function::NodeData>::Element *E=functions[p_name].nodes.front();E;E=E->next()) {
  80. E->get().node->disconnect("ports_changed",this,"_node_ports_changed");
  81. E->get().node->scripts_used.erase(this);
  82. }
  83. functions.erase(p_name);
  84. }
  85. void VisualScript::rename_function(const StringName& p_name,const StringName& p_new_name) {
  86. ERR_FAIL_COND(!functions.has(p_name));
  87. if (p_new_name==p_name)
  88. return;
  89. ERR_FAIL_COND(!String(p_new_name).is_valid_identifier());
  90. ERR_FAIL_COND(functions.has(p_new_name));
  91. ERR_FAIL_COND(variables.has(p_new_name));
  92. ERR_FAIL_COND(custom_signals.has(p_new_name));
  93. functions[p_new_name]=functions[p_name];
  94. functions.erase(p_name);
  95. }
  96. void VisualScript::get_function_list(List<StringName> *r_functions) const {
  97. for (const Map<StringName,Function>::Element *E=functions.front();E;E=E->next()) {
  98. r_functions->push_back(E->key());
  99. }
  100. r_functions->sort_custom<StringName::AlphCompare>();
  101. }
  102. int VisualScript::get_function_node_id(const StringName& p_name) const {
  103. ERR_FAIL_COND_V(!functions.has(p_name),-1);
  104. return functions[p_name].function_id;
  105. }
  106. void VisualScript::_node_ports_changed(int p_id) {
  107. StringName function;
  108. for (Map<StringName,Function>::Element *E=functions.front();E;E=E->next()) {
  109. if (E->get().nodes.has(p_id)) {
  110. function=E->key();
  111. break;
  112. }
  113. }
  114. ERR_FAIL_COND(function==StringName());
  115. Function &func = functions[function];
  116. Ref<VisualScriptNode> vsn = func.nodes[p_id].node;
  117. //must revalidate all the functions
  118. {
  119. List<SequenceConnection> to_remove;
  120. for (Set<SequenceConnection>::Element *E=func.sequence_connections.front();E;E=E->next()) {
  121. if (E->get().from_node==p_id && E->get().from_output>=vsn->get_output_sequence_port_count()) {
  122. to_remove.push_back(E->get());
  123. }
  124. if (E->get().to_node==p_id && !vsn->has_input_sequence_port()) {
  125. to_remove.push_back(E->get());
  126. }
  127. }
  128. while(to_remove.size()) {
  129. func.sequence_connections.erase(to_remove.front()->get());
  130. to_remove.pop_front();
  131. }
  132. }
  133. {
  134. List<DataConnection> to_remove;
  135. for (Set<DataConnection>::Element *E=func.data_connections.front();E;E=E->next()) {
  136. if (E->get().from_node==p_id && E->get().from_port>=vsn->get_output_value_port_count()) {
  137. to_remove.push_back(E->get());
  138. }
  139. if (E->get().to_node==p_id && E->get().to_port>=vsn->get_input_value_port_count()) {
  140. to_remove.push_back(E->get());
  141. }
  142. }
  143. while(to_remove.size()) {
  144. func.data_connections.erase(to_remove.front()->get());
  145. to_remove.pop_front();
  146. }
  147. }
  148. emit_signal("node_ports_changed",function,p_id);
  149. }
  150. void VisualScript::add_node(const StringName& p_func,int p_id, const Ref<VisualScriptNode>& p_node, const Point2 &p_pos) {
  151. ERR_FAIL_COND(!functions.has(p_func));
  152. for (Map<StringName,Function>::Element *E=functions.front();E;E=E->next()) {
  153. ERR_FAIL_COND(E->get().nodes.has(p_id)); //id can exist only one in script, even for different functions
  154. }
  155. Function &func = functions[p_func];
  156. if (p_node->cast_to<VisualScriptFunction>()) {
  157. //the function indeed
  158. ERR_EXPLAIN("A function node already has been set here.");
  159. ERR_FAIL_COND(func.function_id>=0);
  160. func.function_id=p_id;
  161. }
  162. Function::NodeData nd;
  163. nd.node=p_node;
  164. nd.pos=p_pos;
  165. Ref<VisualScriptNode> vsn = p_node;
  166. vsn->connect("ports_changed",this,"_node_ports_changed",varray(p_id));
  167. vsn->scripts_used.insert(this);
  168. func.nodes[p_id]=nd;
  169. }
  170. void VisualScript::remove_node(const StringName& p_func,int p_id){
  171. ERR_FAIL_COND(!functions.has(p_func));
  172. Function &func = functions[p_func];
  173. ERR_FAIL_COND(!func.nodes.has(p_id));
  174. {
  175. List<SequenceConnection> to_remove;
  176. for (Set<SequenceConnection>::Element *E=func.sequence_connections.front();E;E=E->next()) {
  177. if (E->get().from_node==p_id || E->get().to_node==p_id) {
  178. to_remove.push_back(E->get());
  179. }
  180. }
  181. while(to_remove.size()) {
  182. func.sequence_connections.erase(to_remove.front()->get());
  183. to_remove.pop_front();
  184. }
  185. }
  186. {
  187. List<DataConnection> to_remove;
  188. for (Set<DataConnection>::Element *E=func.data_connections.front();E;E=E->next()) {
  189. if (E->get().from_node==p_id || E->get().to_node==p_id) {
  190. to_remove.push_back(E->get());
  191. }
  192. }
  193. while(to_remove.size()) {
  194. func.data_connections.erase(to_remove.front()->get());
  195. to_remove.pop_front();
  196. }
  197. }
  198. if (func.nodes[p_id].node->cast_to<VisualScriptFunction>()) {
  199. func.function_id=-1; //revert to invalid
  200. }
  201. func.nodes[p_id].node->disconnect("ports_changed",this,"_node_ports_changed");
  202. func.nodes[p_id].node->scripts_used.erase(this);
  203. func.nodes.erase(p_id);
  204. }
  205. Ref<VisualScriptNode> VisualScript::get_node(const StringName& p_func,int p_id) const{
  206. ERR_FAIL_COND_V(!functions.has(p_func),Ref<VisualScriptNode>());
  207. const Function &func = functions[p_func];
  208. ERR_FAIL_COND_V(!func.nodes.has(p_id),Ref<VisualScriptNode>());
  209. return func.nodes[p_id].node;
  210. }
  211. void VisualScript::set_node_pos(const StringName& p_func,int p_id,const Point2& p_pos) {
  212. ERR_FAIL_COND(!functions.has(p_func));
  213. Function &func = functions[p_func];
  214. ERR_FAIL_COND(!func.nodes.has(p_id));
  215. func.nodes[p_id].pos=p_pos;
  216. }
  217. Point2 VisualScript::get_node_pos(const StringName& p_func,int p_id) const{
  218. ERR_FAIL_COND_V(!functions.has(p_func),Point2());
  219. const Function &func = functions[p_func];
  220. ERR_FAIL_COND_V(!func.nodes.has(p_id),Point2());
  221. return func.nodes[p_id].pos;
  222. }
  223. void VisualScript::get_node_list(const StringName& p_func,List<int> *r_nodes) const{
  224. ERR_FAIL_COND(!functions.has(p_func));
  225. const Function &func = functions[p_func];
  226. for (const Map<int,Function::NodeData>::Element *E=func.nodes.front();E;E=E->next()) {
  227. r_nodes->push_back(E->key());
  228. }
  229. }
  230. void VisualScript::sequence_connect(const StringName& p_func,int p_from_node,int p_from_output,int p_to_node){
  231. ERR_FAIL_COND(!functions.has(p_func));
  232. Function &func = functions[p_func];
  233. SequenceConnection sc;
  234. sc.from_node=p_from_node;
  235. sc.from_output=p_from_output;
  236. sc.to_node=p_to_node;
  237. ERR_FAIL_COND(func.sequence_connections.has(sc));
  238. func.sequence_connections.insert(sc);
  239. }
  240. void VisualScript::sequence_disconnect(const StringName& p_func,int p_from_node,int p_from_output,int p_to_node){
  241. ERR_FAIL_COND(!functions.has(p_func));
  242. Function &func = functions[p_func];
  243. SequenceConnection sc;
  244. sc.from_node=p_from_node;
  245. sc.from_output=p_from_output;
  246. sc.to_node=p_to_node;
  247. ERR_FAIL_COND(!func.sequence_connections.has(sc));
  248. func.sequence_connections.erase(sc);
  249. }
  250. bool VisualScript::has_sequence_connection(const StringName& p_func,int p_from_node,int p_from_output,int p_to_node) const{
  251. ERR_FAIL_COND_V(!functions.has(p_func),false);
  252. const Function &func = functions[p_func];
  253. SequenceConnection sc;
  254. sc.from_node=p_from_node;
  255. sc.from_output=p_from_output;
  256. sc.to_node=p_to_node;
  257. return func.sequence_connections.has(sc);
  258. }
  259. void VisualScript::get_sequence_connection_list(const StringName& p_func,List<SequenceConnection> *r_connection) const {
  260. ERR_FAIL_COND(!functions.has(p_func));
  261. const Function &func = functions[p_func];
  262. for (const Set<SequenceConnection>::Element *E=func.sequence_connections.front();E;E=E->next()) {
  263. r_connection->push_back(E->get());
  264. }
  265. }
  266. void VisualScript::data_connect(const StringName& p_func,int p_from_node,int p_from_port,int p_to_node,int p_to_port) {
  267. ERR_FAIL_COND(!functions.has(p_func));
  268. Function &func = functions[p_func];
  269. DataConnection dc;
  270. dc.from_node=p_from_node;
  271. dc.from_port=p_from_port;
  272. dc.to_node=p_to_node;
  273. dc.to_port=p_to_port;
  274. ERR_FAIL_COND( func.data_connections.has(dc));
  275. func.data_connections.insert(dc);
  276. }
  277. void VisualScript::data_disconnect(const StringName& p_func,int p_from_node,int p_from_port,int p_to_node,int p_to_port) {
  278. ERR_FAIL_COND(!functions.has(p_func));
  279. Function &func = functions[p_func];
  280. DataConnection dc;
  281. dc.from_node=p_from_node;
  282. dc.from_port=p_from_port;
  283. dc.to_node=p_to_node;
  284. dc.to_port=p_to_port;
  285. ERR_FAIL_COND( !func.data_connections.has(dc));
  286. func.data_connections.erase(dc);
  287. }
  288. bool VisualScript::has_data_connection(const StringName& p_func,int p_from_node,int p_from_port,int p_to_node,int p_to_port) const {
  289. ERR_FAIL_COND_V(!functions.has(p_func),false);
  290. const Function &func = functions[p_func];
  291. DataConnection dc;
  292. dc.from_node=p_from_node;
  293. dc.from_port=p_from_port;
  294. dc.to_node=p_to_node;
  295. dc.to_port=p_to_port;
  296. return func.data_connections.has(dc);
  297. }
  298. bool VisualScript::is_input_value_port_connected(const StringName& p_func,int p_node,int p_port) const {
  299. ERR_FAIL_COND_V(!functions.has(p_func),false);
  300. const Function &func = functions[p_func];
  301. for (const Set<DataConnection>::Element *E=func.data_connections.front();E;E=E->next()) {
  302. if (E->get().to_node==p_node && E->get().to_port==p_port)
  303. return true;
  304. }
  305. return false;
  306. }
  307. void VisualScript::get_data_connection_list(const StringName& p_func,List<DataConnection> *r_connection) const {
  308. ERR_FAIL_COND(!functions.has(p_func));
  309. const Function &func = functions[p_func];
  310. for (const Set<DataConnection>::Element *E=func.data_connections.front();E;E=E->next()) {
  311. r_connection->push_back(E->get());
  312. }
  313. }
  314. void VisualScript::add_variable(const StringName& p_name,const Variant& p_default_value) {
  315. ERR_FAIL_COND(!String(p_name).is_valid_identifier());
  316. ERR_FAIL_COND(variables.has(p_name));
  317. Variable v;
  318. v.default_value=p_default_value;
  319. v.info.type=p_default_value.get_type();
  320. v.info.name=p_name;
  321. v.info.hint=PROPERTY_HINT_NONE;
  322. variables[p_name]=v;
  323. }
  324. bool VisualScript::has_variable(const StringName& p_name) const {
  325. return variables.has(p_name);
  326. }
  327. void VisualScript::remove_variable(const StringName& p_name) {
  328. ERR_FAIL_COND(!variables.has(p_name));
  329. variables.erase(p_name);
  330. }
  331. void VisualScript::set_variable_default_value(const StringName& p_name,const Variant& p_value){
  332. ERR_FAIL_COND(!variables.has(p_name));
  333. variables[p_name].default_value=p_value;
  334. }
  335. Variant VisualScript::get_variable_default_value(const StringName& p_name) const{
  336. ERR_FAIL_COND_V(!variables.has(p_name),Variant());
  337. return variables[p_name].default_value;
  338. }
  339. void VisualScript::set_variable_info(const StringName& p_name,const PropertyInfo& p_info){
  340. ERR_FAIL_COND(!variables.has(p_name));
  341. variables[p_name].info=p_info;
  342. variables[p_name].info.name=p_name;
  343. }
  344. PropertyInfo VisualScript::get_variable_info(const StringName& p_name) const{
  345. ERR_FAIL_COND_V(!variables.has(p_name),PropertyInfo());
  346. return variables[p_name].info;
  347. }
  348. void VisualScript::_set_variable_info(const StringName& p_name,const Dictionary& p_info) {
  349. PropertyInfo pinfo;
  350. if (p_info.has("type"))
  351. pinfo.type=Variant::Type(int(p_info["type"]));
  352. if (p_info.has("name"))
  353. pinfo.name=p_info["name"];
  354. if (p_info.has("hint"))
  355. pinfo.hint=PropertyHint(int(p_info["hint"]));
  356. if (p_info.has("hint_string"))
  357. pinfo.hint_string=p_info["hint_string"];
  358. if (p_info.has("usage"))
  359. pinfo.usage=p_info["usage"];
  360. set_variable_info(p_name,pinfo);
  361. }
  362. Dictionary VisualScript::_get_variable_info(const StringName& p_name) const{
  363. PropertyInfo pinfo=get_variable_info(p_name);
  364. Dictionary d;
  365. d["type"]=pinfo.type;
  366. d["name"]=pinfo.name;
  367. d["hint"]=pinfo.hint;
  368. d["hint_string"]=pinfo.hint_string;
  369. d["usage"]=pinfo.usage;
  370. return d;
  371. }
  372. void VisualScript::get_variable_list(List<StringName> *r_variables){
  373. for (Map<StringName,Variable>::Element *E=variables.front();E;E=E->next()) {
  374. r_variables->push_back(E->key());
  375. }
  376. r_variables->sort_custom<StringName::AlphCompare>();
  377. }
  378. void VisualScript::set_instance_base_type(const StringName& p_type) {
  379. base_type=p_type;
  380. }
  381. void VisualScript::rename_variable(const StringName& p_name,const StringName& p_new_name) {
  382. ERR_FAIL_COND(!variables.has(p_name));
  383. if (p_new_name==p_name)
  384. return;
  385. ERR_FAIL_COND(!String(p_new_name).is_valid_identifier());
  386. ERR_FAIL_COND(functions.has(p_new_name));
  387. ERR_FAIL_COND(variables.has(p_new_name));
  388. ERR_FAIL_COND(custom_signals.has(p_new_name));
  389. variables[p_new_name]=variables[p_name];
  390. variables.erase(p_name);
  391. }
  392. void VisualScript::add_custom_signal(const StringName& p_name) {
  393. ERR_FAIL_COND(!String(p_name).is_valid_identifier());
  394. ERR_FAIL_COND(custom_signals.has(p_name));
  395. custom_signals[p_name]=Vector<Argument>();
  396. }
  397. bool VisualScript::has_custom_signal(const StringName& p_name) const {
  398. return custom_signals.has(p_name);
  399. }
  400. void VisualScript::custom_signal_add_argument(const StringName& p_func,Variant::Type p_type,const String& p_name,int p_index) {
  401. ERR_FAIL_COND(!custom_signals.has(p_func));
  402. Argument arg;
  403. arg.type=p_type;
  404. arg.name=p_name;
  405. if (p_index<0)
  406. custom_signals[p_func].push_back(arg);
  407. else
  408. custom_signals[p_func].insert(0,arg);
  409. }
  410. void VisualScript::custom_signal_set_argument_type(const StringName& p_func,int p_argidx,Variant::Type p_type) {
  411. ERR_FAIL_COND(!custom_signals.has(p_func));
  412. ERR_FAIL_INDEX(p_argidx,custom_signals[p_func].size());
  413. custom_signals[p_func][p_argidx].type=p_type;
  414. }
  415. Variant::Type VisualScript::custom_signal_get_argument_type(const StringName& p_func,int p_argidx) const {
  416. ERR_FAIL_COND_V(!custom_signals.has(p_func),Variant::NIL);
  417. ERR_FAIL_INDEX_V(p_argidx,custom_signals[p_func].size(),Variant::NIL);
  418. return custom_signals[p_func][p_argidx].type;
  419. }
  420. void VisualScript::custom_signal_set_argument_name(const StringName& p_func,int p_argidx,const String& p_name) {
  421. ERR_FAIL_COND(!custom_signals.has(p_func));
  422. ERR_FAIL_INDEX(p_argidx,custom_signals[p_func].size());
  423. custom_signals[p_func][p_argidx].name=p_name;
  424. }
  425. String VisualScript::custom_signal_get_argument_name(const StringName& p_func,int p_argidx) const {
  426. ERR_FAIL_COND_V(!custom_signals.has(p_func),String());
  427. ERR_FAIL_INDEX_V(p_argidx,custom_signals[p_func].size(),String());
  428. return custom_signals[p_func][p_argidx].name;
  429. }
  430. void VisualScript::custom_signal_remove_argument(const StringName& p_func,int p_argidx) {
  431. ERR_FAIL_COND(!custom_signals.has(p_func));
  432. ERR_FAIL_INDEX(p_argidx,custom_signals[p_func].size());
  433. custom_signals[p_func].remove(p_argidx);
  434. }
  435. int VisualScript::custom_signal_get_argument_count(const StringName& p_func) const {
  436. ERR_FAIL_COND_V(!custom_signals.has(p_func),0);
  437. return custom_signals[p_func].size();
  438. }
  439. void VisualScript::custom_signal_swap_argument(const StringName& p_func,int p_argidx,int p_with_argidx) {
  440. ERR_FAIL_COND(!custom_signals.has(p_func));
  441. ERR_FAIL_INDEX(p_argidx,custom_signals[p_func].size());
  442. ERR_FAIL_INDEX(p_with_argidx,custom_signals[p_func].size());
  443. SWAP( custom_signals[p_func][p_argidx], custom_signals[p_func][p_with_argidx] );
  444. }
  445. void VisualScript::remove_custom_signal(const StringName& p_name) {
  446. ERR_FAIL_COND(!custom_signals.has(p_name));
  447. custom_signals.erase(p_name);
  448. }
  449. void VisualScript::rename_custom_signal(const StringName& p_name,const StringName& p_new_name) {
  450. ERR_FAIL_COND(!custom_signals.has(p_name));
  451. if (p_new_name==p_name)
  452. return;
  453. ERR_FAIL_COND(!String(p_new_name).is_valid_identifier());
  454. ERR_FAIL_COND(functions.has(p_new_name));
  455. ERR_FAIL_COND(variables.has(p_new_name));
  456. ERR_FAIL_COND(custom_signals.has(p_new_name));
  457. custom_signals[p_new_name]=custom_signals[p_name];
  458. custom_signals.erase(p_name);
  459. }
  460. void VisualScript::get_custom_signal_list(List<StringName> *r_custom_signals) const {
  461. for (const Map<StringName,Vector<Argument> >::Element *E=custom_signals.front();E;E=E->next()) {
  462. r_custom_signals->push_back(E->key());
  463. }
  464. r_custom_signals->sort_custom<StringName::AlphCompare>();
  465. }
  466. int VisualScript::get_available_id() const {
  467. int max_id=0;
  468. for (Map<StringName,Function>::Element *E=functions.front();E;E=E->next()) {
  469. if (E->get().nodes.empty())
  470. continue;
  471. int last_id = E->get().nodes.back()->key();
  472. max_id=MAX(max_id,last_id+1);
  473. }
  474. return max_id;
  475. }
  476. /////////////////////////////////
  477. bool VisualScript::can_instance() const {
  478. return ScriptServer::is_scripting_enabled();
  479. }
  480. StringName VisualScript::get_instance_base_type() const {
  481. return base_type;
  482. }
  483. ScriptInstance* VisualScript::instance_create(Object *p_this) {
  484. return NULL;
  485. }
  486. bool VisualScript::instance_has(const Object *p_this) const {
  487. return false;
  488. }
  489. bool VisualScript::has_source_code() const {
  490. return false;
  491. }
  492. String VisualScript::get_source_code() const {
  493. return String();
  494. }
  495. void VisualScript::set_source_code(const String& p_code) {
  496. }
  497. Error VisualScript::reload(bool p_keep_state) {
  498. return OK;
  499. }
  500. bool VisualScript::is_tool() const {
  501. return false;
  502. }
  503. String VisualScript::get_node_type() const {
  504. return String();
  505. }
  506. ScriptLanguage *VisualScript::get_language() const {
  507. return VisualScriptLanguage::singleton;
  508. }
  509. bool VisualScript::has_script_signal(const StringName& p_signal) const {
  510. return false;
  511. }
  512. void VisualScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  513. }
  514. bool VisualScript::get_property_default_value(const StringName& p_property,Variant& r_value) const {
  515. return false;
  516. }
  517. void VisualScript::get_method_list(List<MethodInfo> *p_list) const {
  518. for (Map<StringName,Function>::Element *E=functions.front();E;E=E->next()) {
  519. MethodInfo mi;
  520. mi.name=E->key();
  521. if (E->get().function_id>=0) {
  522. Ref<VisualScriptFunction> func=E->get().nodes[E->get().function_id].node;
  523. if (func.is_valid()) {
  524. for(int i=0;i<func->get_argument_count();i++) {
  525. PropertyInfo arg;
  526. arg.name=func->get_argument_name(i);
  527. arg.type=func->get_argument_type(i);
  528. mi.arguments.push_back(arg);
  529. }
  530. }
  531. }
  532. p_list->push_back(mi);
  533. }
  534. }
  535. void VisualScript::_set_data(const Dictionary& p_data) {
  536. Dictionary d = p_data;
  537. if (d.has("base_type"))
  538. base_type=d["base_type"];
  539. variables.clear();
  540. Array vars=d["variables"];
  541. for (int i=0;i<vars.size();i++) {
  542. Dictionary v=vars[i];
  543. add_variable(v["name"],v["default_value"]);
  544. _set_variable_info(v["name"],v);
  545. }
  546. custom_signals.clear();
  547. Array sigs=d["signals"];
  548. for (int i=0;i<sigs.size();i++) {
  549. Dictionary cs=sigs[i];
  550. add_custom_signal(cs["name"]);
  551. Array args=cs["arguments"];
  552. for(int j=0;j<args.size();j+=2) {
  553. custom_signal_add_argument(cs["name"],Variant::Type(int(args[j+1])),args[j]);
  554. }
  555. }
  556. Array funcs=d["functions"];
  557. functions.clear();
  558. for (int i=0;i<funcs.size();i++) {
  559. Dictionary func=funcs[i];
  560. StringName name=func["name"];
  561. //int id=func["function_id"];
  562. add_function(name);
  563. Array nodes = func["nodes"];
  564. for(int i=0;i<nodes.size();i+=3) {
  565. add_node(name,nodes[i],nodes[i+2],nodes[i+1]);
  566. }
  567. Array sequence_connections=func["sequence_connections"];
  568. for (int j=0;j<sequence_connections.size();j+=3) {
  569. sequence_connect(name,sequence_connections[j+0],sequence_connections[j+1],sequence_connections[j+2]);
  570. }
  571. Array data_connections=func["data_connections"];
  572. for (int j=0;j<data_connections.size();j+=4) {
  573. data_connect(name,data_connections[j+0],data_connections[j+1],data_connections[j+2],data_connections[j+3]);
  574. }
  575. }
  576. }
  577. Dictionary VisualScript::_get_data() const{
  578. Dictionary d;
  579. d["base_type"]=base_type;
  580. Array vars;
  581. for (const Map<StringName,Variable>::Element *E=variables.front();E;E=E->next()) {
  582. Dictionary var = _get_variable_info(E->key());
  583. var["name"]=E->key(); //make sure it's the right one
  584. var["default_value"]=E->get().default_value;
  585. vars.push_back(var);
  586. }
  587. d["variables"]=vars;
  588. Array sigs;
  589. for (const Map<StringName,Vector<Argument> >::Element *E=custom_signals.front();E;E=E->next()) {
  590. Dictionary cs;
  591. cs["name"]=E->key();
  592. Array args;
  593. for(int i=0;i<E->get().size();i++) {
  594. args.push_back(E->get()[i].name);
  595. args.push_back(E->get()[i].type);
  596. }
  597. cs["arguments"]=args;
  598. sigs.push_back(cs);
  599. }
  600. d["signals"]=sigs;
  601. Array funcs;
  602. for (const Map<StringName,Function>::Element *E=functions.front();E;E=E->next()) {
  603. Dictionary func;
  604. func["name"]=E->key();
  605. func["function_id"]=E->get().function_id;
  606. Array nodes;
  607. for (const Map<int,Function::NodeData>::Element *F=E->get().nodes.front();F;F=F->next()) {
  608. nodes.push_back(F->key());
  609. nodes.push_back(F->get().pos);
  610. nodes.push_back(F->get().node);
  611. }
  612. func["nodes"]=nodes;
  613. Array sequence_connections;
  614. for (const Set<SequenceConnection>::Element *F=E->get().sequence_connections.front();F;F=F->next()) {
  615. sequence_connections.push_back(F->get().from_node);
  616. sequence_connections.push_back(F->get().from_output);
  617. sequence_connections.push_back(F->get().to_node);
  618. }
  619. func["sequence_connections"]=sequence_connections;
  620. Array data_connections;
  621. for (const Set<DataConnection>::Element *F=E->get().data_connections.front();F;F=F->next()) {
  622. data_connections.push_back(F->get().from_node);
  623. data_connections.push_back(F->get().from_port);
  624. data_connections.push_back(F->get().to_node);
  625. data_connections.push_back(F->get().to_port);
  626. }
  627. func["data_connections"]=data_connections;
  628. funcs.push_back(func);
  629. }
  630. d["functions"]=funcs;
  631. return d;
  632. }
  633. void VisualScript::_bind_methods() {
  634. ObjectTypeDB::bind_method(_MD("_node_ports_changed"),&VisualScript::_node_ports_changed);
  635. ObjectTypeDB::bind_method(_MD("add_function","name"),&VisualScript::add_function);
  636. ObjectTypeDB::bind_method(_MD("has_function","name"),&VisualScript::has_function);
  637. ObjectTypeDB::bind_method(_MD("remove_function","name"),&VisualScript::remove_function);
  638. ObjectTypeDB::bind_method(_MD("rename_function","name","new_name"),&VisualScript::rename_function);
  639. ObjectTypeDB::bind_method(_MD("add_node","func","id","node","pos"),&VisualScript::add_node,DEFVAL(Point2()));
  640. ObjectTypeDB::bind_method(_MD("remove_node","func","id"),&VisualScript::remove_node);
  641. ObjectTypeDB::bind_method(_MD("get_function_node_id","name"),&VisualScript::get_function_node_id);
  642. ObjectTypeDB::bind_method(_MD("get_node","func","id"),&VisualScript::get_node);
  643. ObjectTypeDB::bind_method(_MD("set_node_pos","func","id","pos"),&VisualScript::set_node_pos);
  644. ObjectTypeDB::bind_method(_MD("get_node_pos","func","id"),&VisualScript::get_node_pos);
  645. ObjectTypeDB::bind_method(_MD("sequence_connect","func","from_node","from_output","to_node"),&VisualScript::sequence_connect);
  646. ObjectTypeDB::bind_method(_MD("sequence_disconnect","func","from_node","from_output","to_node"),&VisualScript::sequence_disconnect);
  647. ObjectTypeDB::bind_method(_MD("has_sequence_connection","func","from_node","from_output","to_node"),&VisualScript::has_sequence_connection);
  648. ObjectTypeDB::bind_method(_MD("data_connect","func","from_node","from_port","to_node","to_port"),&VisualScript::data_connect);
  649. ObjectTypeDB::bind_method(_MD("data_disconnect","func","from_node","from_port","to_node","to_port"),&VisualScript::data_disconnect);
  650. ObjectTypeDB::bind_method(_MD("has_data_connection","func","from_node","from_port","to_node","to_port"),&VisualScript::has_data_connection);
  651. ObjectTypeDB::bind_method(_MD("add_variable","name","default_value"),&VisualScript::add_variable,DEFVAL(Variant()));
  652. ObjectTypeDB::bind_method(_MD("has_variable","name"),&VisualScript::has_variable);
  653. ObjectTypeDB::bind_method(_MD("remove_variable","name"),&VisualScript::remove_variable);
  654. ObjectTypeDB::bind_method(_MD("set_variable_default_value","name","value"),&VisualScript::set_variable_default_value);
  655. ObjectTypeDB::bind_method(_MD("get_variable_default_value","name"),&VisualScript::get_variable_default_value);
  656. ObjectTypeDB::bind_method(_MD("set_variable_info","name","value"),&VisualScript::_set_variable_info);
  657. ObjectTypeDB::bind_method(_MD("get_variable_info","name"),&VisualScript::_get_variable_info);
  658. ObjectTypeDB::bind_method(_MD("rename_variable","name","new_name"),&VisualScript::rename_variable);
  659. ObjectTypeDB::bind_method(_MD("add_custom_signal","name"),&VisualScript::add_custom_signal);
  660. ObjectTypeDB::bind_method(_MD("has_custom_signal","name"),&VisualScript::has_custom_signal);
  661. ObjectTypeDB::bind_method(_MD("custom_signal_add_argument","name","type","argname","index"),&VisualScript::custom_signal_add_argument,DEFVAL(-1));
  662. ObjectTypeDB::bind_method(_MD("custom_signal_set_argument_type","name","argidx","type"),&VisualScript::custom_signal_set_argument_type);
  663. ObjectTypeDB::bind_method(_MD("custom_signal_get_argument_type","name","argidx"),&VisualScript::custom_signal_get_argument_type);
  664. ObjectTypeDB::bind_method(_MD("custom_signal_set_argument_name","name","argidx","argname"),&VisualScript::custom_signal_set_argument_name);
  665. ObjectTypeDB::bind_method(_MD("custom_signal_get_argument_name","name","argidx"),&VisualScript::custom_signal_get_argument_name);
  666. ObjectTypeDB::bind_method(_MD("custom_signal_remove_argument","argidx"),&VisualScript::custom_signal_remove_argument);
  667. ObjectTypeDB::bind_method(_MD("custom_signal_get_argument_count","name"),&VisualScript::custom_signal_get_argument_count);
  668. ObjectTypeDB::bind_method(_MD("custom_signal_swap_argument","name","argidx","withidx"),&VisualScript::custom_signal_swap_argument);
  669. ObjectTypeDB::bind_method(_MD("remove_custom_signal","name"),&VisualScript::remove_custom_signal);
  670. ObjectTypeDB::bind_method(_MD("rename_custom_signal","name","new_name"),&VisualScript::rename_custom_signal);
  671. //ObjectTypeDB::bind_method(_MD("set_variable_info","name","info"),&VScript::set_variable_info);
  672. //ObjectTypeDB::bind_method(_MD("get_variable_info","name"),&VScript::set_variable_info);
  673. ObjectTypeDB::bind_method(_MD("set_instance_base_type","type"),&VisualScript::set_instance_base_type);
  674. ObjectTypeDB::bind_method(_MD("_set_data","data"),&VisualScript::_set_data);
  675. ObjectTypeDB::bind_method(_MD("_get_data"),&VisualScript::_get_data);
  676. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY,"data",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_data"),_SCS("_get_data"));
  677. ADD_SIGNAL(MethodInfo("node_ports_changed",PropertyInfo(Variant::STRING,"function"),PropertyInfo(Variant::INT,"id")));
  678. }
  679. VisualScript::VisualScript() {
  680. base_type="Object";
  681. }
  682. VisualScript::~VisualScript() {
  683. while(!functions.empty()) {
  684. remove_function(functions.front()->key());
  685. }
  686. }
  687. ////////////////////////////////////////////
  688. String VisualScriptLanguage::get_name() const {
  689. return "VisualScript";
  690. }
  691. /* LANGUAGE FUNCTIONS */
  692. void VisualScriptLanguage::init() {
  693. }
  694. String VisualScriptLanguage::get_type() const {
  695. return "VisualScript";
  696. }
  697. String VisualScriptLanguage::get_extension() const {
  698. return "vs";
  699. }
  700. Error VisualScriptLanguage::execute_file(const String& p_path) {
  701. return OK;
  702. }
  703. void VisualScriptLanguage::finish() {
  704. }
  705. /* EDITOR FUNCTIONS */
  706. void VisualScriptLanguage::get_reserved_words(List<String> *p_words) const {
  707. }
  708. void VisualScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  709. }
  710. void VisualScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  711. }
  712. String VisualScriptLanguage::get_template(const String& p_class_name, const String& p_base_class_name) const {
  713. return String();
  714. }
  715. bool VisualScriptLanguage::validate(const String& p_script, int &r_line_error,int &r_col_error,String& r_test_error, const String& p_path,List<String> *r_functions) const {
  716. return false;
  717. }
  718. Script *VisualScriptLanguage::create_script() const {
  719. return memnew( VisualScript );
  720. }
  721. bool VisualScriptLanguage::has_named_classes() const {
  722. return false;
  723. }
  724. int VisualScriptLanguage::find_function(const String& p_function,const String& p_code) const {
  725. return -1;
  726. }
  727. String VisualScriptLanguage::make_function(const String& p_class,const String& p_name,const StringArray& p_args) const {
  728. return String();
  729. }
  730. void VisualScriptLanguage::auto_indent_code(String& p_code,int p_from_line,int p_to_line) const {
  731. }
  732. void VisualScriptLanguage::add_global_constant(const StringName& p_variable,const Variant& p_value) {
  733. }
  734. /* DEBUGGER FUNCTIONS */
  735. String VisualScriptLanguage::debug_get_error() const {
  736. return String();
  737. }
  738. int VisualScriptLanguage::debug_get_stack_level_count() const {
  739. return 0;
  740. }
  741. int VisualScriptLanguage::debug_get_stack_level_line(int p_level) const {
  742. return 0;
  743. }
  744. String VisualScriptLanguage::debug_get_stack_level_function(int p_level) const {
  745. return String();
  746. }
  747. String VisualScriptLanguage::debug_get_stack_level_source(int p_level) const {
  748. return String();
  749. }
  750. void VisualScriptLanguage::debug_get_stack_level_locals(int p_level,List<String> *p_locals, List<Variant> *p_values, int p_max_subitems,int p_max_depth) {
  751. }
  752. void VisualScriptLanguage::debug_get_stack_level_members(int p_level,List<String> *p_members, List<Variant> *p_values, int p_max_subitems,int p_max_depth) {
  753. }
  754. void VisualScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems,int p_max_depth) {
  755. }
  756. String VisualScriptLanguage::debug_parse_stack_level_expression(int p_level,const String& p_expression,int p_max_subitems,int p_max_depth) {
  757. return String();
  758. }
  759. void VisualScriptLanguage::reload_all_scripts() {
  760. }
  761. void VisualScriptLanguage::reload_tool_script(const Ref<Script>& p_script,bool p_soft_reload) {
  762. }
  763. /* LOADER FUNCTIONS */
  764. void VisualScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  765. p_extensions->push_back("vs");
  766. }
  767. void VisualScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  768. }
  769. void VisualScriptLanguage::get_public_constants(List<Pair<String,Variant> > *p_constants) const {
  770. }
  771. void VisualScriptLanguage::profiling_start() {
  772. }
  773. void VisualScriptLanguage::profiling_stop() {
  774. }
  775. int VisualScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr,int p_info_max) {
  776. return 0;
  777. }
  778. int VisualScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr,int p_info_max) {
  779. return 0;
  780. }
  781. VisualScriptLanguage* VisualScriptLanguage::singleton=NULL;
  782. void VisualScriptLanguage::add_register_func(const String& p_name,VisualScriptNodeRegisterFunc p_func) {
  783. ERR_FAIL_COND(register_funcs.has(p_name));
  784. register_funcs[p_name]=p_func;
  785. }
  786. Ref<VisualScriptNode> VisualScriptLanguage::create_node_from_name(const String& p_name) {
  787. ERR_FAIL_COND_V(!register_funcs.has(p_name),Ref<VisualScriptNode>());
  788. return register_funcs[p_name](p_name);
  789. }
  790. void VisualScriptLanguage::get_registered_node_names(List<String> *r_names) {
  791. for (Map<String,VisualScriptNodeRegisterFunc>::Element *E=register_funcs.front();E;E=E->next()) {
  792. r_names->push_back(E->key());
  793. }
  794. }
  795. VisualScriptLanguage::VisualScriptLanguage() {
  796. singleton=this;
  797. }