visual_script_flow_control.cpp 26 KB

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