visual_script_flow_control.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*************************************************************************/
  2. /* visual_script_flow_control.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "visual_script_flow_control.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/project_settings.h"
  34. //////////////////////////////////////////
  35. ////////////////RETURN////////////////////
  36. //////////////////////////////////////////
  37. int VisualScriptReturn::get_output_sequence_port_count() const {
  38. return 0;
  39. }
  40. bool VisualScriptReturn::has_input_sequence_port() const {
  41. return true;
  42. }
  43. int VisualScriptReturn::get_input_value_port_count() const {
  44. return with_value ? 1 : 0;
  45. }
  46. int VisualScriptReturn::get_output_value_port_count() const {
  47. return 0;
  48. }
  49. String VisualScriptReturn::get_output_sequence_port_text(int p_port) const {
  50. return String();
  51. }
  52. PropertyInfo VisualScriptReturn::get_input_value_port_info(int p_idx) const {
  53. PropertyInfo pinfo;
  54. pinfo.name = "result";
  55. pinfo.type = type;
  56. return pinfo;
  57. }
  58. PropertyInfo VisualScriptReturn::get_output_value_port_info(int p_idx) const {
  59. return PropertyInfo();
  60. }
  61. String VisualScriptReturn::get_caption() const {
  62. return "Return";
  63. }
  64. String VisualScriptReturn::get_text() const {
  65. return get_name();
  66. }
  67. void VisualScriptReturn::set_return_type(Variant::Type p_type) {
  68. if (type == p_type)
  69. return;
  70. type = p_type;
  71. ports_changed_notify();
  72. }
  73. Variant::Type VisualScriptReturn::get_return_type() const {
  74. return type;
  75. }
  76. void VisualScriptReturn::set_enable_return_value(bool p_enable) {
  77. if (with_value == p_enable)
  78. return;
  79. with_value = p_enable;
  80. ports_changed_notify();
  81. }
  82. bool VisualScriptReturn::is_return_value_enabled() const {
  83. return with_value;
  84. }
  85. void VisualScriptReturn::_bind_methods() {
  86. ClassDB::bind_method(D_METHOD("set_return_type", "type"), &VisualScriptReturn::set_return_type);
  87. ClassDB::bind_method(D_METHOD("get_return_type"), &VisualScriptReturn::get_return_type);
  88. ClassDB::bind_method(D_METHOD("set_enable_return_value", "enable"), &VisualScriptReturn::set_enable_return_value);
  89. ClassDB::bind_method(D_METHOD("is_return_value_enabled"), &VisualScriptReturn::is_return_value_enabled);
  90. String argt = "Any";
  91. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  92. argt += "," + Variant::get_type_name(Variant::Type(i));
  93. }
  94. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "return_enabled"), "set_enable_return_value", "is_return_value_enabled");
  95. ADD_PROPERTY(PropertyInfo(Variant::INT, "return_type", PROPERTY_HINT_ENUM, argt), "set_return_type", "get_return_type");
  96. }
  97. class VisualScriptNodeInstanceReturn : public VisualScriptNodeInstance {
  98. public:
  99. VisualScriptReturn *node;
  100. VisualScriptInstance *instance;
  101. bool with_value;
  102. virtual int get_working_memory_size() const { return 1; }
  103. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  104. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  105. 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) {
  106. if (with_value) {
  107. *p_working_mem = *p_inputs[0];
  108. return STEP_EXIT_FUNCTION_BIT;
  109. } else {
  110. *p_working_mem = Variant();
  111. return 0;
  112. }
  113. }
  114. };
  115. VisualScriptNodeInstance *VisualScriptReturn::instance(VisualScriptInstance *p_instance) {
  116. VisualScriptNodeInstanceReturn *instance = memnew(VisualScriptNodeInstanceReturn);
  117. instance->node = this;
  118. instance->instance = p_instance;
  119. instance->with_value = with_value;
  120. return instance;
  121. }
  122. VisualScriptReturn::VisualScriptReturn() {
  123. with_value = false;
  124. type = Variant::NIL;
  125. }
  126. template <bool with_value>
  127. static Ref<VisualScriptNode> create_return_node(const String &p_name) {
  128. Ref<VisualScriptReturn> node;
  129. node.instance();
  130. node->set_enable_return_value(with_value);
  131. return node;
  132. }
  133. //////////////////////////////////////////
  134. ////////////////CONDITION/////////////////
  135. //////////////////////////////////////////
  136. int VisualScriptCondition::get_output_sequence_port_count() const {
  137. return 3;
  138. }
  139. bool VisualScriptCondition::has_input_sequence_port() const {
  140. return true;
  141. }
  142. int VisualScriptCondition::get_input_value_port_count() const {
  143. return 1;
  144. }
  145. int VisualScriptCondition::get_output_value_port_count() const {
  146. return 0;
  147. }
  148. String VisualScriptCondition::get_output_sequence_port_text(int p_port) const {
  149. if (p_port == 0)
  150. return "true";
  151. else if (p_port == 1)
  152. return "false";
  153. else
  154. return "done";
  155. }
  156. PropertyInfo VisualScriptCondition::get_input_value_port_info(int p_idx) const {
  157. PropertyInfo pinfo;
  158. pinfo.name = "cond";
  159. pinfo.type = Variant::BOOL;
  160. return pinfo;
  161. }
  162. PropertyInfo VisualScriptCondition::get_output_value_port_info(int p_idx) const {
  163. return PropertyInfo();
  164. }
  165. String VisualScriptCondition::get_caption() const {
  166. return "Condition";
  167. }
  168. String VisualScriptCondition::get_text() const {
  169. return "if (cond) is: ";
  170. }
  171. void VisualScriptCondition::_bind_methods() {
  172. }
  173. class VisualScriptNodeInstanceCondition : public VisualScriptNodeInstance {
  174. public:
  175. VisualScriptCondition *node;
  176. VisualScriptInstance *instance;
  177. //virtual int get_working_memory_size() const { return 1; }
  178. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  179. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  180. 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) {
  181. if (p_start_mode == START_MODE_CONTINUE_SEQUENCE)
  182. return 2;
  183. else if (p_inputs[0]->operator bool())
  184. return 0 | STEP_FLAG_PUSH_STACK_BIT;
  185. else
  186. return 1 | STEP_FLAG_PUSH_STACK_BIT;
  187. }
  188. };
  189. VisualScriptNodeInstance *VisualScriptCondition::instance(VisualScriptInstance *p_instance) {
  190. VisualScriptNodeInstanceCondition *instance = memnew(VisualScriptNodeInstanceCondition);
  191. instance->node = this;
  192. instance->instance = p_instance;
  193. return instance;
  194. }
  195. VisualScriptCondition::VisualScriptCondition() {
  196. }
  197. //////////////////////////////////////////
  198. ////////////////WHILE/////////////////
  199. //////////////////////////////////////////
  200. int VisualScriptWhile::get_output_sequence_port_count() const {
  201. return 2;
  202. }
  203. bool VisualScriptWhile::has_input_sequence_port() const {
  204. return true;
  205. }
  206. int VisualScriptWhile::get_input_value_port_count() const {
  207. return 1;
  208. }
  209. int VisualScriptWhile::get_output_value_port_count() const {
  210. return 0;
  211. }
  212. String VisualScriptWhile::get_output_sequence_port_text(int p_port) const {
  213. if (p_port == 0)
  214. return "repeat";
  215. else
  216. return "exit";
  217. }
  218. PropertyInfo VisualScriptWhile::get_input_value_port_info(int p_idx) const {
  219. PropertyInfo pinfo;
  220. pinfo.name = "cond";
  221. pinfo.type = Variant::BOOL;
  222. return pinfo;
  223. }
  224. PropertyInfo VisualScriptWhile::get_output_value_port_info(int p_idx) const {
  225. return PropertyInfo();
  226. }
  227. String VisualScriptWhile::get_caption() const {
  228. return "While";
  229. }
  230. String VisualScriptWhile::get_text() const {
  231. return "while (cond): ";
  232. }
  233. void VisualScriptWhile::_bind_methods() {
  234. }
  235. class VisualScriptNodeInstanceWhile : public VisualScriptNodeInstance {
  236. public:
  237. VisualScriptWhile *node;
  238. VisualScriptInstance *instance;
  239. //virtual int get_working_memory_size() const { return 1; }
  240. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  241. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  242. 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) {
  243. bool keep_going = p_inputs[0]->operator bool();
  244. if (keep_going)
  245. return 0 | STEP_FLAG_PUSH_STACK_BIT;
  246. else
  247. return 1;
  248. }
  249. };
  250. VisualScriptNodeInstance *VisualScriptWhile::instance(VisualScriptInstance *p_instance) {
  251. VisualScriptNodeInstanceWhile *instance = memnew(VisualScriptNodeInstanceWhile);
  252. instance->node = this;
  253. instance->instance = p_instance;
  254. return instance;
  255. }
  256. VisualScriptWhile::VisualScriptWhile() {
  257. }
  258. //////////////////////////////////////////
  259. ////////////////ITERATOR/////////////////
  260. //////////////////////////////////////////
  261. int VisualScriptIterator::get_output_sequence_port_count() const {
  262. return 2;
  263. }
  264. bool VisualScriptIterator::has_input_sequence_port() const {
  265. return true;
  266. }
  267. int VisualScriptIterator::get_input_value_port_count() const {
  268. return 1;
  269. }
  270. int VisualScriptIterator::get_output_value_port_count() const {
  271. return 1;
  272. }
  273. String VisualScriptIterator::get_output_sequence_port_text(int p_port) const {
  274. if (p_port == 0)
  275. return "each";
  276. else
  277. return "exit";
  278. }
  279. PropertyInfo VisualScriptIterator::get_input_value_port_info(int p_idx) const {
  280. PropertyInfo pinfo;
  281. pinfo.name = "input";
  282. pinfo.type = Variant::NIL;
  283. return pinfo;
  284. }
  285. PropertyInfo VisualScriptIterator::get_output_value_port_info(int p_idx) const {
  286. PropertyInfo pinfo;
  287. pinfo.name = "elem";
  288. pinfo.type = Variant::NIL;
  289. return pinfo;
  290. }
  291. String VisualScriptIterator::get_caption() const {
  292. return "Iterator";
  293. }
  294. String VisualScriptIterator::get_text() const {
  295. return "for (elem) in (input): ";
  296. }
  297. void VisualScriptIterator::_bind_methods() {
  298. }
  299. class VisualScriptNodeInstanceIterator : public VisualScriptNodeInstance {
  300. public:
  301. VisualScriptIterator *node;
  302. VisualScriptInstance *instance;
  303. virtual int get_working_memory_size() const { return 2; }
  304. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  305. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  306. 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) {
  307. if (p_start_mode == START_MODE_BEGIN_SEQUENCE) {
  308. p_working_mem[0] = *p_inputs[0];
  309. bool valid;
  310. bool can_iter = p_inputs[0]->iter_init(p_working_mem[1], valid);
  311. if (!valid) {
  312. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  313. r_error_str = RTR("Input type not iterable: ") + Variant::get_type_name(p_inputs[0]->get_type());
  314. return 0;
  315. }
  316. if (!can_iter)
  317. return 1; //nothing to iterate
  318. *p_outputs[0] = p_working_mem[0].iter_get(p_working_mem[1], valid);
  319. if (!valid) {
  320. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  321. r_error_str = RTR("Iterator became invalid");
  322. return 0;
  323. }
  324. } else { //continue sequence
  325. bool valid;
  326. bool can_iter = p_working_mem[0].iter_next(p_working_mem[1], valid);
  327. if (!valid) {
  328. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  329. r_error_str = RTR("Iterator became invalid: ") + Variant::get_type_name(p_inputs[0]->get_type());
  330. return 0;
  331. }
  332. if (!can_iter)
  333. return 1; //nothing to iterate
  334. *p_outputs[0] = p_working_mem[0].iter_get(p_working_mem[1], valid);
  335. if (!valid) {
  336. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  337. r_error_str = RTR("Iterator became invalid");
  338. return 0;
  339. }
  340. }
  341. return 0 | STEP_FLAG_PUSH_STACK_BIT; //go around
  342. }
  343. };
  344. VisualScriptNodeInstance *VisualScriptIterator::instance(VisualScriptInstance *p_instance) {
  345. VisualScriptNodeInstanceIterator *instance = memnew(VisualScriptNodeInstanceIterator);
  346. instance->node = this;
  347. instance->instance = p_instance;
  348. return instance;
  349. }
  350. VisualScriptIterator::VisualScriptIterator() {
  351. }
  352. //////////////////////////////////////////
  353. ////////////////SEQUENCE/////////////////
  354. //////////////////////////////////////////
  355. int VisualScriptSequence::get_output_sequence_port_count() const {
  356. return steps;
  357. }
  358. bool VisualScriptSequence::has_input_sequence_port() const {
  359. return true;
  360. }
  361. int VisualScriptSequence::get_input_value_port_count() const {
  362. return 0;
  363. }
  364. int VisualScriptSequence::get_output_value_port_count() const {
  365. return 1;
  366. }
  367. String VisualScriptSequence::get_output_sequence_port_text(int p_port) const {
  368. return itos(p_port + 1);
  369. }
  370. PropertyInfo VisualScriptSequence::get_input_value_port_info(int p_idx) const {
  371. return PropertyInfo();
  372. }
  373. PropertyInfo VisualScriptSequence::get_output_value_port_info(int p_idx) const {
  374. return PropertyInfo(Variant::INT, "current");
  375. }
  376. String VisualScriptSequence::get_caption() const {
  377. return "Sequence";
  378. }
  379. String VisualScriptSequence::get_text() const {
  380. return "in order: ";
  381. }
  382. void VisualScriptSequence::set_steps(int p_steps) {
  383. ERR_FAIL_COND(p_steps < 1);
  384. if (steps == p_steps)
  385. return;
  386. steps = p_steps;
  387. ports_changed_notify();
  388. }
  389. int VisualScriptSequence::get_steps() const {
  390. return steps;
  391. }
  392. void VisualScriptSequence::_bind_methods() {
  393. ClassDB::bind_method(D_METHOD("set_steps", "steps"), &VisualScriptSequence::set_steps);
  394. ClassDB::bind_method(D_METHOD("get_steps"), &VisualScriptSequence::get_steps);
  395. ADD_PROPERTY(PropertyInfo(Variant::INT, "steps", PROPERTY_HINT_RANGE, "1,64,1"), "set_steps", "get_steps");
  396. }
  397. class VisualScriptNodeInstanceSequence : public VisualScriptNodeInstance {
  398. public:
  399. VisualScriptSequence *node;
  400. VisualScriptInstance *instance;
  401. int steps;
  402. virtual int get_working_memory_size() const { return 1; }
  403. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  404. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  405. 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) {
  406. if (p_start_mode == START_MODE_BEGIN_SEQUENCE) {
  407. p_working_mem[0] = 0;
  408. }
  409. int step = p_working_mem[0];
  410. *p_outputs[0] = step;
  411. if (step + 1 == steps)
  412. return step;
  413. else {
  414. p_working_mem[0] = step + 1;
  415. return step | STEP_FLAG_PUSH_STACK_BIT;
  416. }
  417. }
  418. };
  419. VisualScriptNodeInstance *VisualScriptSequence::instance(VisualScriptInstance *p_instance) {
  420. VisualScriptNodeInstanceSequence *instance = memnew(VisualScriptNodeInstanceSequence);
  421. instance->node = this;
  422. instance->instance = p_instance;
  423. instance->steps = steps;
  424. return instance;
  425. }
  426. VisualScriptSequence::VisualScriptSequence() {
  427. steps = 1;
  428. }
  429. //////////////////////////////////////////
  430. ////////////////EVENT TYPE FILTER///////////
  431. //////////////////////////////////////////
  432. int VisualScriptSwitch::get_output_sequence_port_count() const {
  433. return case_values.size() + 1;
  434. }
  435. bool VisualScriptSwitch::has_input_sequence_port() const {
  436. return true;
  437. }
  438. int VisualScriptSwitch::get_input_value_port_count() const {
  439. return case_values.size() + 1;
  440. }
  441. int VisualScriptSwitch::get_output_value_port_count() const {
  442. return 0;
  443. }
  444. String VisualScriptSwitch::get_output_sequence_port_text(int p_port) const {
  445. if (p_port == case_values.size())
  446. return "done";
  447. return String();
  448. }
  449. PropertyInfo VisualScriptSwitch::get_input_value_port_info(int p_idx) const {
  450. if (p_idx < case_values.size()) {
  451. return PropertyInfo(case_values[p_idx].type, " =");
  452. } else
  453. return PropertyInfo(Variant::NIL, "input");
  454. }
  455. PropertyInfo VisualScriptSwitch::get_output_value_port_info(int p_idx) const {
  456. return PropertyInfo();
  457. }
  458. String VisualScriptSwitch::get_caption() const {
  459. return "Switch";
  460. }
  461. String VisualScriptSwitch::get_text() const {
  462. return "'input' is:";
  463. }
  464. class VisualScriptNodeInstanceSwitch : public VisualScriptNodeInstance {
  465. public:
  466. VisualScriptInstance *instance;
  467. int case_count;
  468. //virtual int get_working_memory_size() const { return 0; }
  469. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  470. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return false; }
  471. 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) {
  472. if (p_start_mode == START_MODE_CONTINUE_SEQUENCE) {
  473. return case_count; //exit
  474. }
  475. for (int i = 0; i < case_count; i++) {
  476. if (*p_inputs[i] == *p_inputs[case_count]) {
  477. return i | STEP_FLAG_PUSH_STACK_BIT;
  478. }
  479. }
  480. return case_count;
  481. }
  482. };
  483. VisualScriptNodeInstance *VisualScriptSwitch::instance(VisualScriptInstance *p_instance) {
  484. VisualScriptNodeInstanceSwitch *instance = memnew(VisualScriptNodeInstanceSwitch);
  485. instance->instance = p_instance;
  486. instance->case_count = case_values.size();
  487. return instance;
  488. }
  489. bool VisualScriptSwitch::_set(const StringName &p_name, const Variant &p_value) {
  490. if (String(p_name) == "case_count") {
  491. case_values.resize(p_value);
  492. _change_notify();
  493. ports_changed_notify();
  494. return true;
  495. }
  496. if (String(p_name).begins_with("case/")) {
  497. int idx = String(p_name).get_slice("/", 1).to_int();
  498. ERR_FAIL_INDEX_V(idx, case_values.size(), false);
  499. case_values.write[idx].type = Variant::Type(int(p_value));
  500. _change_notify();
  501. ports_changed_notify();
  502. return true;
  503. }
  504. return false;
  505. }
  506. bool VisualScriptSwitch::_get(const StringName &p_name, Variant &r_ret) const {
  507. if (String(p_name) == "case_count") {
  508. r_ret = case_values.size();
  509. return true;
  510. }
  511. if (String(p_name).begins_with("case/")) {
  512. int idx = String(p_name).get_slice("/", 1).to_int();
  513. ERR_FAIL_INDEX_V(idx, case_values.size(), false);
  514. r_ret = case_values[idx].type;
  515. return true;
  516. }
  517. return false;
  518. }
  519. void VisualScriptSwitch::_get_property_list(List<PropertyInfo> *p_list) const {
  520. p_list->push_back(PropertyInfo(Variant::INT, "case_count", PROPERTY_HINT_RANGE, "0,128"));
  521. String argt = "Any";
  522. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  523. argt += "," + Variant::get_type_name(Variant::Type(i));
  524. }
  525. for (int i = 0; i < case_values.size(); i++) {
  526. p_list->push_back(PropertyInfo(Variant::INT, "case/" + itos(i), PROPERTY_HINT_ENUM, argt));
  527. }
  528. }
  529. void VisualScriptSwitch::_bind_methods() {
  530. }
  531. VisualScriptSwitch::VisualScriptSwitch() {
  532. }
  533. //////////////////////////////////////////
  534. ////////////////TYPE CAST///////////
  535. //////////////////////////////////////////
  536. int VisualScriptTypeCast::get_output_sequence_port_count() const {
  537. return 2;
  538. }
  539. bool VisualScriptTypeCast::has_input_sequence_port() const {
  540. return true;
  541. }
  542. int VisualScriptTypeCast::get_input_value_port_count() const {
  543. return 1;
  544. }
  545. int VisualScriptTypeCast::get_output_value_port_count() const {
  546. return 1;
  547. }
  548. String VisualScriptTypeCast::get_output_sequence_port_text(int p_port) const {
  549. return p_port == 0 ? "yes" : "no";
  550. }
  551. PropertyInfo VisualScriptTypeCast::get_input_value_port_info(int p_idx) const {
  552. return PropertyInfo(Variant::OBJECT, "instance");
  553. }
  554. PropertyInfo VisualScriptTypeCast::get_output_value_port_info(int p_idx) const {
  555. return PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_TYPE_STRING, get_base_type());
  556. }
  557. String VisualScriptTypeCast::get_caption() const {
  558. return "Type Cast";
  559. }
  560. String VisualScriptTypeCast::get_text() const {
  561. if (script != String())
  562. return "Is " + script.get_file() + "?";
  563. else
  564. return "Is " + base_type + "?";
  565. }
  566. void VisualScriptTypeCast::set_base_type(const StringName &p_type) {
  567. if (base_type == p_type)
  568. return;
  569. base_type = p_type;
  570. _change_notify();
  571. ports_changed_notify();
  572. }
  573. StringName VisualScriptTypeCast::get_base_type() const {
  574. return base_type;
  575. }
  576. void VisualScriptTypeCast::set_base_script(const String &p_path) {
  577. if (script == p_path)
  578. return;
  579. script = p_path;
  580. _change_notify();
  581. ports_changed_notify();
  582. }
  583. String VisualScriptTypeCast::get_base_script() const {
  584. return script;
  585. }
  586. VisualScriptTypeCast::TypeGuess VisualScriptTypeCast::guess_output_type(TypeGuess *p_inputs, int p_output) const {
  587. TypeGuess tg;
  588. tg.type = Variant::OBJECT;
  589. if (script != String()) {
  590. tg.script = ResourceLoader::load(script);
  591. }
  592. //if (!tg.script.is_valid()) {
  593. // tg.gdclass = base_type;
  594. //}
  595. return tg;
  596. }
  597. class VisualScriptNodeInstanceTypeCast : public VisualScriptNodeInstance {
  598. public:
  599. VisualScriptInstance *instance;
  600. StringName base_type;
  601. String script;
  602. //virtual int get_working_memory_size() const { return 0; }
  603. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  604. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return false; }
  605. 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) {
  606. Object *obj = *p_inputs[0];
  607. *p_outputs[0] = Variant();
  608. if (!obj) {
  609. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  610. r_error_str = "Instance is null";
  611. return 0;
  612. }
  613. if (script != String()) {
  614. Ref<Script> obj_script = obj->get_script();
  615. if (!obj_script.is_valid()) {
  616. return 1; //well, definitely not the script because object we got has no script.
  617. }
  618. if (!ResourceCache::has(script)) {
  619. //if the script is not in use by anyone, we can safely assume whathever we got is not casting to it.
  620. return 1;
  621. }
  622. Ref<Script> cast_script = Ref<Resource>(ResourceCache::get(script));
  623. if (!cast_script.is_valid()) {
  624. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  625. r_error_str = "Script path is not a script: " + script;
  626. return 1;
  627. }
  628. while (obj_script.is_valid()) {
  629. if (cast_script == obj_script) {
  630. *p_outputs[0] = *p_inputs[0]; //copy
  631. return 0; // it is the script, yey
  632. }
  633. obj_script = obj_script->get_base_script();
  634. }
  635. return 1; //not found sorry
  636. }
  637. if (ClassDB::is_parent_class(obj->get_class_name(), base_type)) {
  638. *p_outputs[0] = *p_inputs[0]; //copy
  639. return 0;
  640. } else
  641. return 1;
  642. }
  643. };
  644. VisualScriptNodeInstance *VisualScriptTypeCast::instance(VisualScriptInstance *p_instance) {
  645. VisualScriptNodeInstanceTypeCast *instance = memnew(VisualScriptNodeInstanceTypeCast);
  646. instance->instance = p_instance;
  647. instance->base_type = base_type;
  648. instance->script = script;
  649. return instance;
  650. }
  651. void VisualScriptTypeCast::_bind_methods() {
  652. ClassDB::bind_method(D_METHOD("set_base_type", "type"), &VisualScriptTypeCast::set_base_type);
  653. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptTypeCast::get_base_type);
  654. ClassDB::bind_method(D_METHOD("set_base_script", "path"), &VisualScriptTypeCast::set_base_script);
  655. ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptTypeCast::get_base_script);
  656. List<String> script_extensions;
  657. for (int i = 0; i > ScriptServer::get_language_count(); i++) {
  658. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  659. }
  660. String script_ext_hint;
  661. for (List<String>::Element *E = script_extensions.front(); E; E = E->next()) {
  662. if (script_ext_hint != String())
  663. script_ext_hint += ",";
  664. script_ext_hint += "*." + E->get();
  665. }
  666. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  667. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
  668. }
  669. VisualScriptTypeCast::VisualScriptTypeCast() {
  670. base_type = "Object";
  671. }
  672. void register_visual_script_flow_control_nodes() {
  673. VisualScriptLanguage::singleton->add_register_func("flow_control/return", create_return_node<false>);
  674. VisualScriptLanguage::singleton->add_register_func("flow_control/return_with_value", create_return_node<true>);
  675. VisualScriptLanguage::singleton->add_register_func("flow_control/condition", create_node_generic<VisualScriptCondition>);
  676. VisualScriptLanguage::singleton->add_register_func("flow_control/while", create_node_generic<VisualScriptWhile>);
  677. VisualScriptLanguage::singleton->add_register_func("flow_control/iterator", create_node_generic<VisualScriptIterator>);
  678. VisualScriptLanguage::singleton->add_register_func("flow_control/sequence", create_node_generic<VisualScriptSequence>);
  679. VisualScriptLanguage::singleton->add_register_func("flow_control/switch", create_node_generic<VisualScriptSwitch>);
  680. //VisualScriptLanguage::singleton->add_register_func("flow_control/input_filter", create_node_generic<VisualScriptInputFilter>);
  681. VisualScriptLanguage::singleton->add_register_func("flow_control/type_cast", create_node_generic<VisualScriptTypeCast>);
  682. }