visual_script_flow_control.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  1. /*************************************************************************/
  2. /* visual_script_flow_control.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "io/resource_loader.h"
  32. #include "os/keyboard.h"
  33. #include "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[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. ////////////////EVENT ACTION FILTER///////////
  535. //////////////////////////////////////////
  536. #if 0
  537. int VisualScriptInputFilter::get_output_sequence_port_count() const {
  538. return filters.size();
  539. }
  540. bool VisualScriptInputFilter::has_input_sequence_port() const {
  541. return true;
  542. }
  543. int VisualScriptInputFilter::get_input_value_port_count() const {
  544. return 1;
  545. }
  546. int VisualScriptInputFilter::get_output_value_port_count() const {
  547. return 1;
  548. }
  549. String VisualScriptInputFilter::get_output_sequence_port_text(int p_port) const {
  550. String text;
  551. switch (filters[p_port].type) {
  552. case Ref<InputEvent>::NONE: {
  553. text = "None";
  554. } break;
  555. case Ref<InputEvent>::KEY: {
  556. InputEventKey k = filters[p_port].key;
  557. if (k->get_scancode() == 0 && k.unicode == 0) {
  558. text = "No Key";
  559. } else {
  560. if (k->get_scancode() != 0) {
  561. text = "KeyCode: " + keycode_get_string(k->get_scancode());
  562. } else if (k.unicode != 0) {
  563. text = "Uniode: " + String::chr(k.unicode);
  564. }
  565. if (k->is_pressed())
  566. text += ", Pressed";
  567. else
  568. text += ", Released";
  569. if (k.echo)
  570. text += ", Echo";
  571. if (k->get_alt())
  572. text = "Alt+" + text;
  573. if (k->get_shift())
  574. text = "Shift+" + text;
  575. if (k->get_control())
  576. text = "Ctrl+" + text;
  577. if (k->get_metakey())
  578. text = "Meta+" + text;
  579. }
  580. } break;
  581. case Ref<InputEvent>::MOUSE_MOTION: {
  582. InputEventMouseMotion mm = filters[p_port].mouse_motion;
  583. text = "Mouse Motion";
  584. String b = "Left,Right,Middle,WheelUp,WheelDown,WheelLeft,WheelRight";
  585. for (int i = 0; i < 7; i++) {
  586. if (mm->get_button_mask() & (1 << i)) {
  587. text = b.get_slice(",", i) + "+" + text;
  588. }
  589. }
  590. if (mm->get_alt())
  591. text = "Alt+" + text;
  592. if (mm->get_shift())
  593. text = "Shift+" + text;
  594. if (mm->get_control())
  595. text = "Ctrl+" + text;
  596. if (mm->get_metakey())
  597. text = "Meta+" + text;
  598. } break;
  599. case Ref<InputEvent>::MOUSE_BUTTON: {
  600. InputEventMouseButton mb = filters[p_port].mouse_button;
  601. String b = "Any,Left,Right,Middle,WheelUp,WheelDown,WheelLeft,WheelRight";
  602. text = b.get_slice(",", mb->get_button_index()) + " Mouse Button";
  603. if (mb->is_pressed())
  604. text += ", Pressed";
  605. else
  606. text += ", Released";
  607. if (mb.doubleclick)
  608. text += ", DblClick";
  609. if (mb->get_alt())
  610. text = "Alt+" + text;
  611. if (mb->get_shift())
  612. text = "Shift+" + text;
  613. if (mb->get_control())
  614. text = "Ctrl+" + text;
  615. if (mb->get_metakey())
  616. text = "Meta+" + text;
  617. } break;
  618. case Ref<InputEvent>::JOYPAD_MOTION: {
  619. InputEventJoypadMotion jm = filters[p_port].joy_motion;
  620. text = "JoyMotion Axis " + itos(jm.axis >> 1);
  621. if (jm.axis & 1)
  622. text += " > " + rtos(jm.axis_value);
  623. else
  624. text += " < " + rtos(-jm.axis_value);
  625. } break;
  626. case Ref<InputEvent>::JOYPAD_BUTTON: {
  627. InputEventJoypadButton jb = filters[p_port].joy_button;
  628. text = "JoyButton " + itos(jb->get_button_index());
  629. if (jb->is_pressed())
  630. text += ", Pressed";
  631. else
  632. text += ", Released";
  633. } break;
  634. case Ref<InputEvent>::SCREEN_TOUCH: {
  635. InputEventScreenTouch sd = filters[p_port].screen_touch;
  636. text = "Touch Finger " + itos(sd.index);
  637. if (sd->is_pressed())
  638. text += ", Pressed";
  639. else
  640. text += ", Released";
  641. } break;
  642. case Ref<InputEvent>::SCREEN_DRAG: {
  643. InputEventScreenDrag sd = filters[p_port].screen_drag;
  644. text = "Drag Finger " + itos(sd.index);
  645. } break;
  646. case Ref<InputEvent>::ACTION: {
  647. List<PropertyInfo> pinfo;
  648. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  649. int index = 1;
  650. text = "No Action";
  651. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  652. const PropertyInfo &pi = E->get();
  653. if (!pi.name.begins_with("input/"))
  654. continue;
  655. if (filters[p_port].action.action == index) {
  656. text = "Action " + pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  657. break;
  658. }
  659. index++;
  660. }
  661. if (filters[p_port].action->is_pressed())
  662. text += ", Pressed";
  663. else
  664. text += ", Released";
  665. } break;
  666. }
  667. return text + " - " + itos(p_port);
  668. }
  669. PropertyInfo VisualScriptInputFilter::get_input_value_port_info(int p_idx) const {
  670. return PropertyInfo(Variant::INPUT_EVENT, "event");
  671. }
  672. PropertyInfo VisualScriptInputFilter::get_output_value_port_info(int p_idx) const {
  673. return PropertyInfo(Variant::INPUT_EVENT, "");
  674. }
  675. String VisualScriptInputFilter::get_caption() const {
  676. return "InputFilter";
  677. }
  678. String VisualScriptInputFilter::get_text() const {
  679. return "";
  680. }
  681. bool VisualScriptInputFilter::_set(const StringName &p_name, const Variant &p_value) {
  682. if (p_name == "filter_count") {
  683. filters.resize(p_value);
  684. _change_notify();
  685. ports_changed_notify();
  686. return true;
  687. }
  688. if (String(p_name).begins_with("filter_")) {
  689. int idx = String(p_name).replace_first("filters_", "").get_slice("/", 0).to_int();
  690. ERR_FAIL_INDEX_V(idx, filters.size(), false);
  691. String what = String(p_name).get_slice("/", 1);
  692. if (what == "type") {
  693. filters[idx] = Ref<InputEvent>();
  694. filters[idx].type = Ref<InputEvent>::Type(int(p_value));
  695. if (filters[idx].type == Ref<InputEvent>::JOYPAD_MOTION) {
  696. filters[idx].joy_motion.axis_value = 0.5; //for threshold
  697. } else if (filters[idx].type == Ref<InputEvent>::KEY) {
  698. filters[idx]->is_pressed() = true; //put these as true to make it more user friendly
  699. } else if (filters[idx].type == Ref<InputEvent>::MOUSE_BUTTON) {
  700. filters[idx]->is_pressed() = true;
  701. } else if (filters[idx].type == Ref<InputEvent>::JOYPAD_BUTTON) {
  702. filters[idx].joy_button->is_pressed() = true;
  703. } else if (filters[idx].type == Ref<InputEvent>::SCREEN_TOUCH) {
  704. filters[idx].screen_touch->is_pressed() = true;
  705. } else if (filters[idx].type == Ref<InputEvent>::ACTION) {
  706. filters[idx].action->is_pressed() = true;
  707. }
  708. _change_notify();
  709. ports_changed_notify();
  710. return true;
  711. }
  712. if (what == "device") {
  713. filters[idx].device = p_value;
  714. ports_changed_notify();
  715. return true;
  716. }
  717. switch (filters[idx].type) {
  718. case Ref<InputEvent>::KEY: {
  719. if (what == "scancode") {
  720. String sc = p_value;
  721. if (sc == String()) {
  722. filters[idx]->get_scancode() = 0;
  723. } else {
  724. filters[idx]->get_scancode() = find_keycode(p_value);
  725. }
  726. } else if (what == "unicode") {
  727. String uc = p_value;
  728. if (uc == String()) {
  729. filters[idx].key.unicode = 0;
  730. } else {
  731. filters[idx].key.unicode = uc[0];
  732. }
  733. } else if (what == "pressed") {
  734. filters[idx]->is_pressed() = p_value;
  735. } else if (what == "echo") {
  736. filters[idx]->is_echo() = p_value;
  737. } else if (what == "mod_alt") {
  738. filters[idx]->get_alt() = p_value;
  739. } else if (what == "mod_shift") {
  740. filters[idx]->get_shift() = p_value;
  741. } else if (what == "mod_ctrl") {
  742. filters[idx]->get_control() = p_value;
  743. } else if (what == "mod_meta") {
  744. filters[idx]->get_metakey() = p_value;
  745. } else {
  746. return false;
  747. }
  748. ports_changed_notify();
  749. return true;
  750. } break;
  751. case Ref<InputEvent>::MOUSE_MOTION: {
  752. if (what == "button_mask") {
  753. filters[idx]->get_button_mask() = p_value;
  754. } else if (what == "mod_alt") {
  755. filters[idx].mouse_motion->get_alt() = p_value;
  756. } else if (what == "mod_shift") {
  757. filters[idx].mouse_motion->get_shift() = p_value;
  758. } else if (what == "mod_ctrl") {
  759. filters[idx].mouse_motion->get_control() = p_value;
  760. } else if (what == "mod_meta") {
  761. filters[idx].mouse_motion->get_metakey() = p_value;
  762. } else {
  763. return false;
  764. }
  765. ports_changed_notify();
  766. return true;
  767. } break;
  768. case Ref<InputEvent>::MOUSE_BUTTON: {
  769. if (what == "button_index") {
  770. filters[idx]->get_button_index() = p_value;
  771. } else if (what == "pressed") {
  772. filters[idx]->is_pressed() = p_value;
  773. } else if (what == "doubleclicked") {
  774. filters[idx].mouse_button.doubleclick = p_value;
  775. } else if (what == "mod_alt") {
  776. filters[idx].mouse_button->get_alt() = p_value;
  777. } else if (what == "mod_shift") {
  778. filters[idx].mouse_button->get_shift() = p_value;
  779. } else if (what == "mod_ctrl") {
  780. filters[idx].mouse_button->get_control() = p_value;
  781. } else if (what == "mod_meta") {
  782. filters[idx].mouse_button->get_metakey() = p_value;
  783. } else {
  784. return false;
  785. }
  786. ports_changed_notify();
  787. return true;
  788. } break;
  789. case Ref<InputEvent>::JOYPAD_MOTION: {
  790. if (what == "axis") {
  791. filters[idx].joy_motion.axis = int(p_value) << 1 | filters[idx].joy_motion.axis;
  792. } else if (what == "mode") {
  793. filters[idx].joy_motion.axis |= int(p_value);
  794. } else if (what == "threshold") {
  795. filters[idx].joy_motion.axis_value = p_value;
  796. } else {
  797. return false;
  798. }
  799. ports_changed_notify();
  800. return true;
  801. } break;
  802. case Ref<InputEvent>::JOYPAD_BUTTON: {
  803. if (what == "button_index") {
  804. filters[idx].joy_button->get_button_index() = p_value;
  805. } else if (what == "pressed") {
  806. filters[idx].joy_button->is_pressed() = p_value;
  807. } else {
  808. return false;
  809. }
  810. ports_changed_notify();
  811. return true;
  812. } break;
  813. case Ref<InputEvent>::SCREEN_TOUCH: {
  814. if (what == "finger_index") {
  815. filters[idx].screen_touch.index = p_value;
  816. } else if (what == "pressed") {
  817. filters[idx].screen_touch->is_pressed() = p_value;
  818. } else {
  819. return false;
  820. }
  821. ports_changed_notify();
  822. return true;
  823. } break;
  824. case Ref<InputEvent>::SCREEN_DRAG: {
  825. if (what == "finger_index") {
  826. filters[idx].screen_drag.index = p_value;
  827. } else {
  828. return false;
  829. }
  830. ports_changed_notify();
  831. return true;
  832. } break;
  833. case Ref<InputEvent>::ACTION: {
  834. if (what == "action_name") {
  835. List<PropertyInfo> pinfo;
  836. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  837. int index = 1;
  838. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  839. const PropertyInfo &pi = E->get();
  840. if (!pi.name.begins_with("input/"))
  841. continue;
  842. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  843. if (name == String(p_value)) {
  844. filters[idx].action.action = index;
  845. ports_changed_notify();
  846. return true;
  847. }
  848. index++;
  849. }
  850. filters[idx].action.action = 0;
  851. ports_changed_notify();
  852. return false;
  853. } else if (what == "pressed") {
  854. filters[idx].action->is_pressed() = p_value;
  855. ports_changed_notify();
  856. return true;
  857. }
  858. } break;
  859. }
  860. }
  861. return false;
  862. }
  863. bool VisualScriptInputFilter::_get(const StringName &p_name, Variant &r_ret) const {
  864. if (p_name == "filter_count") {
  865. r_ret = filters.size();
  866. return true;
  867. }
  868. if (String(p_name).begins_with("filter_")) {
  869. int idx = String(p_name).replace_first("filters_", "").get_slice("/", 0).to_int();
  870. ERR_FAIL_INDEX_V(idx, filters.size(), false);
  871. String what = String(p_name).get_slice("/", 1);
  872. if (what == "type") {
  873. r_ret = filters[idx].type;
  874. return true;
  875. }
  876. if (what == "device") {
  877. r_ret = filters[idx].device;
  878. return true;
  879. }
  880. switch (filters[idx].type) {
  881. case Ref<InputEvent>::KEY: {
  882. if (what == "scancode") {
  883. if (filters[idx]->get_scancode() == 0)
  884. r_ret = String();
  885. else {
  886. r_ret = keycode_get_string(filters[idx]->get_scancode());
  887. }
  888. } else if (what == "unicode") {
  889. if (filters[idx].key.unicode == 0) {
  890. r_ret = String();
  891. } else {
  892. CharType str[2] = { (CharType)filters[idx].key.unicode, 0 };
  893. r_ret = String(str);
  894. }
  895. } else if (what == "pressed") {
  896. r_ret = filters[idx]->is_pressed();
  897. } else if (what == "echo") {
  898. r_ret = filters[idx]->is_echo();
  899. } else if (what == "mod_alt") {
  900. r_ret = filters[idx]->get_alt();
  901. } else if (what == "mod_shift") {
  902. r_ret = filters[idx]->get_shift();
  903. } else if (what == "mod_ctrl") {
  904. r_ret = filters[idx]->get_control();
  905. } else if (what == "mod_meta") {
  906. r_ret = filters[idx]->get_metakey();
  907. } else {
  908. return false;
  909. }
  910. return true;
  911. } break;
  912. case Ref<InputEvent>::MOUSE_MOTION: {
  913. if (what == "button_mask") {
  914. r_ret = filters[idx]->get_button_mask();
  915. } else if (what == "mod_alt") {
  916. r_ret = filters[idx].mouse_motion->get_alt();
  917. } else if (what == "mod_shift") {
  918. r_ret = filters[idx].mouse_motion->get_shift();
  919. } else if (what == "mod_ctrl") {
  920. r_ret = filters[idx].mouse_motion->get_control();
  921. } else if (what == "mod_meta") {
  922. r_ret = filters[idx].mouse_motion->get_metakey();
  923. } else {
  924. return false;
  925. }
  926. return true;
  927. } break;
  928. case Ref<InputEvent>::MOUSE_BUTTON: {
  929. if (what == "button_index") {
  930. r_ret = filters[idx]->get_button_index();
  931. } else if (what == "pressed") {
  932. r_ret = filters[idx]->is_pressed();
  933. } else if (what == "doubleclicked") {
  934. r_ret = filters[idx].mouse_button.doubleclick;
  935. } else if (what == "mod_alt") {
  936. r_ret = filters[idx].mouse_button->get_alt();
  937. } else if (what == "mod_shift") {
  938. r_ret = filters[idx].mouse_button->get_shift();
  939. } else if (what == "mod_ctrl") {
  940. r_ret = filters[idx].mouse_button->get_control();
  941. } else if (what == "mod_meta") {
  942. r_ret = filters[idx].mouse_button->get_metakey();
  943. } else {
  944. return false;
  945. }
  946. return true;
  947. } break;
  948. case Ref<InputEvent>::JOYPAD_MOTION: {
  949. if (what == "axis_index") {
  950. r_ret = filters[idx].joy_motion.axis >> 1;
  951. } else if (what == "mode") {
  952. r_ret = filters[idx].joy_motion.axis & 1;
  953. } else if (what == "threshold") {
  954. r_ret = filters[idx].joy_motion.axis_value;
  955. } else {
  956. return false;
  957. }
  958. return true;
  959. } break;
  960. case Ref<InputEvent>::JOYPAD_BUTTON: {
  961. if (what == "button_index") {
  962. r_ret = filters[idx].joy_button->get_button_index();
  963. } else if (what == "pressed") {
  964. r_ret = filters[idx].joy_button->is_pressed();
  965. } else {
  966. return false;
  967. }
  968. return true;
  969. } break;
  970. case Ref<InputEvent>::SCREEN_TOUCH: {
  971. if (what == "finger_index") {
  972. r_ret = filters[idx].screen_touch.index;
  973. } else if (what == "pressed") {
  974. r_ret = filters[idx].screen_touch->is_pressed();
  975. } else {
  976. return false;
  977. }
  978. return true;
  979. } break;
  980. case Ref<InputEvent>::SCREEN_DRAG: {
  981. if (what == "finger_index") {
  982. r_ret = filters[idx].screen_drag.index;
  983. } else {
  984. return false;
  985. }
  986. return true;
  987. } break;
  988. case Ref<InputEvent>::ACTION: {
  989. if (what == "action_name") {
  990. List<PropertyInfo> pinfo;
  991. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  992. int index = 1;
  993. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  994. const PropertyInfo &pi = E->get();
  995. if (!pi.name.begins_with("input/"))
  996. continue;
  997. if (filters[idx].action.action == index) {
  998. r_ret = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  999. return true;
  1000. }
  1001. index++;
  1002. }
  1003. r_ret = "None"; //no index
  1004. return false;
  1005. } else if (what == "pressed") {
  1006. r_ret = filters[idx].action->is_pressed();
  1007. return true;
  1008. }
  1009. } break;
  1010. }
  1011. }
  1012. return false;
  1013. }
  1014. static const char *event_type_names[Ref<InputEvent>::TYPE_MAX] = {
  1015. "None",
  1016. "Key",
  1017. "MouseMotion",
  1018. "MouseButton",
  1019. "JoypadMotion",
  1020. "JoypadButton",
  1021. "ScreenTouch",
  1022. "ScreenDrag",
  1023. "Action"
  1024. };
  1025. void VisualScriptInputFilter::_get_property_list(List<PropertyInfo> *p_list) const {
  1026. p_list->push_back(PropertyInfo(Variant::INT, "filter_count", PROPERTY_HINT_RANGE, "0,64"));
  1027. String et;
  1028. for (int i = 0; i < Ref<InputEvent>::TYPE_MAX; i++) {
  1029. if (i > 0)
  1030. et += ",";
  1031. et += event_type_names[i];
  1032. }
  1033. String kc;
  1034. String actions;
  1035. for (int i = 0; i < filters.size(); i++) {
  1036. String base = "filter_" + itos(i) + "/";
  1037. p_list->push_back(PropertyInfo(Variant::INT, base + "type", PROPERTY_HINT_ENUM, et));
  1038. p_list->push_back(PropertyInfo(Variant::INT, base + "device"));
  1039. switch (filters[i].type) {
  1040. case Ref<InputEvent>::NONE: {
  1041. } break;
  1042. case Ref<InputEvent>::KEY: {
  1043. if (kc == String()) {
  1044. int kcc = keycode_get_count();
  1045. kc = "None";
  1046. for (int i = 0; i < kcc; i++) {
  1047. kc += ",";
  1048. kc += String(keycode_get_name_by_index(i));
  1049. }
  1050. }
  1051. p_list->push_back(PropertyInfo(Variant::STRING, base + "scancode", PROPERTY_HINT_ENUM, kc));
  1052. p_list->push_back(PropertyInfo(Variant::STRING, base + "unicode"));
  1053. p_list->push_back(PropertyInfo(Variant::BOOL, base + "pressed"));
  1054. p_list->push_back(PropertyInfo(Variant::BOOL, base + "echo"));
  1055. p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_alt"));
  1056. p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_shift"));
  1057. p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_ctrl"));
  1058. p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_meta"));
  1059. } break;
  1060. case Ref<InputEvent>::MOUSE_MOTION: {
  1061. p_list->push_back(PropertyInfo(Variant::INT, base + "button_mask", PROPERTY_HINT_FLAGS, "Left,Right,Middle,WheelUp,WheelDown,WheelLeft,WheelRight"));
  1062. p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_alt"));
  1063. p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_shift"));
  1064. p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_ctrl"));
  1065. p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_meta"));
  1066. } break;
  1067. case Ref<InputEvent>::MOUSE_BUTTON: {
  1068. p_list->push_back(PropertyInfo(Variant::INT, base + "button_index", PROPERTY_HINT_ENUM, "Any,Left,Right,Middle,WheelUp,WheelDown,WheelLeft,WheelRight"));
  1069. p_list->push_back(PropertyInfo(Variant::BOOL, base + "pressed"));
  1070. p_list->push_back(PropertyInfo(Variant::BOOL, base + "doubleclicked"));
  1071. p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_alt"));
  1072. p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_shift"));
  1073. p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_ctrl"));
  1074. p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_meta"));
  1075. } break;
  1076. case Ref<InputEvent>::JOYPAD_MOTION: {
  1077. p_list->push_back(PropertyInfo(Variant::INT, base + "axis_index"));
  1078. p_list->push_back(PropertyInfo(Variant::INT, base + "mode", PROPERTY_HINT_ENUM, "Min,Max"));
  1079. p_list->push_back(PropertyInfo(Variant::REAL, base + "threshold", PROPERTY_HINT_RANGE, "0,1,0.01"));
  1080. } break;
  1081. case Ref<InputEvent>::JOYPAD_BUTTON: {
  1082. p_list->push_back(PropertyInfo(Variant::INT, base + "button_index"));
  1083. p_list->push_back(PropertyInfo(Variant::BOOL, base + "pressed"));
  1084. } break;
  1085. case Ref<InputEvent>::SCREEN_TOUCH: {
  1086. p_list->push_back(PropertyInfo(Variant::INT, base + "finger_index"));
  1087. p_list->push_back(PropertyInfo(Variant::BOOL, base + "pressed"));
  1088. } break;
  1089. case Ref<InputEvent>::SCREEN_DRAG: {
  1090. p_list->push_back(PropertyInfo(Variant::INT, base + "finger_index"));
  1091. } break;
  1092. case Ref<InputEvent>::ACTION: {
  1093. if (actions == String()) {
  1094. actions = "None";
  1095. List<PropertyInfo> pinfo;
  1096. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  1097. Vector<String> al;
  1098. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1099. const PropertyInfo &pi = E->get();
  1100. if (!pi.name.begins_with("input/"))
  1101. continue;
  1102. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  1103. al.push_back(name);
  1104. }
  1105. for (int i = 0; i < al.size(); i++) {
  1106. actions += ",";
  1107. actions += al[i];
  1108. }
  1109. }
  1110. p_list->push_back(PropertyInfo(Variant::STRING, base + "action_name", PROPERTY_HINT_ENUM, actions));
  1111. p_list->push_back(PropertyInfo(Variant::BOOL, base + "pressed"));
  1112. } break;
  1113. }
  1114. }
  1115. }
  1116. class VisualScriptNodeInstanceInputFilter : public VisualScriptNodeInstance {
  1117. public:
  1118. VisualScriptInstance *instance;
  1119. Vector<Ref<InputEvent>> filters;
  1120. //virtual int get_working_memory_size() const { return 0; }
  1121. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1122. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return false; }
  1123. 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) {
  1124. if (p_inputs[0]->get_type() != Variant::INPUT_EVENT) {
  1125. r_error_str = "Input value not of type event";
  1126. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1127. return 0;
  1128. }
  1129. Ref<InputEvent> event = *p_inputs[0];
  1130. for (int i = 0; i < filters.size(); i++) {
  1131. const Ref<InputEvent> &ie = filters[i];
  1132. if (ie.type != event.type)
  1133. continue;
  1134. bool match = false;
  1135. switch (ie.type) {
  1136. case Ref<InputEvent>::NONE: {
  1137. match = true;
  1138. } break;
  1139. case Ref<InputEvent>::KEY: {
  1140. InputEventKey k = ie.key;
  1141. InputEventKey k2 = event.key;
  1142. if (k->get_scancode() == 0 && k.unicode == 0 && k2->get_scancode() == 0 && k2.unicode == 0) {
  1143. match = true;
  1144. } else {
  1145. if ((k->get_scancode() != 0 && k->get_scancode() == k2->get_scancode()) || (k.unicode != 0 && k.unicode == k2.unicode)) {
  1146. //key valid
  1147. if (
  1148. k->is_pressed() == k2->is_pressed() &&
  1149. k.echo == k2.echo &&
  1150. k.mod == k2.mod) {
  1151. match = true;
  1152. }
  1153. }
  1154. }
  1155. } break;
  1156. case Ref<InputEvent>::MOUSE_MOTION: {
  1157. InputEventMouseMotion mm = ie.mouse_motion;
  1158. InputEventMouseMotion mm2 = event.mouse_motion;
  1159. if (mm->get_button_mask() == mm2->get_button_mask() &&
  1160. mm.mod == mm2.mod) {
  1161. match = true;
  1162. }
  1163. } break;
  1164. case Ref<InputEvent>::MOUSE_BUTTON: {
  1165. InputEventMouseButton mb = ie.mouse_button;
  1166. InputEventMouseButton mb2 = event.mouse_button;
  1167. if (mb->get_button_index() == mb2->get_button_index() &&
  1168. mb->is_pressed() == mb2->is_pressed() &&
  1169. mb.doubleclick == mb2.doubleclick &&
  1170. mb.mod == mb2.mod) {
  1171. match = true;
  1172. }
  1173. } break;
  1174. case Ref<InputEvent>::JOYPAD_MOTION: {
  1175. InputEventJoypadMotion jm = ie.joy_motion;
  1176. InputEventJoypadMotion jm2 = event.joy_motion;
  1177. int axis = jm.axis >> 1;
  1178. if (axis == jm2.axis) {
  1179. if (jm.axis & 1) {
  1180. //greater
  1181. if (jm2.axis_value > jm.axis_value) {
  1182. match = true;
  1183. }
  1184. } else {
  1185. //less
  1186. if (jm2.axis_value < -jm.axis_value) {
  1187. match = true;
  1188. }
  1189. }
  1190. }
  1191. } break;
  1192. case Ref<InputEvent>::JOYPAD_BUTTON: {
  1193. InputEventJoypadButton jb = ie.joy_button;
  1194. InputEventJoypadButton jb2 = event.joy_button;
  1195. if (jb->get_button_index() == jb2->get_button_index() &&
  1196. jb->is_pressed() == jb2->is_pressed()) {
  1197. match = true;
  1198. }
  1199. } break;
  1200. case Ref<InputEvent>::SCREEN_TOUCH: {
  1201. InputEventScreenTouch st = ie.screen_touch;
  1202. InputEventScreenTouch st2 = event.screen_touch;
  1203. if (st.index == st2.index &&
  1204. st->is_pressed() == st2->is_pressed()) {
  1205. match = true;
  1206. }
  1207. } break;
  1208. case Ref<InputEvent>::SCREEN_DRAG: {
  1209. InputEventScreenDrag sd = ie.screen_drag;
  1210. InputEventScreenDrag sd2 = event.screen_drag;
  1211. if (sd.index == sd2.index) {
  1212. match = true;
  1213. }
  1214. } break;
  1215. case Ref<InputEvent>::ACTION: {
  1216. InputEventAction ia = ie.action;
  1217. InputEventAction ia2 = event.action;
  1218. if (ia.action == ia2.action &&
  1219. ia->is_pressed() == ia2->is_pressed()) {
  1220. match = true;
  1221. }
  1222. } break;
  1223. }
  1224. *p_outputs[0] = event;
  1225. if (match)
  1226. return i; //go through match output
  1227. }
  1228. return STEP_NO_ADVANCE_BIT; //none found, don't advance
  1229. }
  1230. };
  1231. VisualScriptNodeInstance *VisualScriptInputFilter::instance(VisualScriptInstance *p_instance) {
  1232. VisualScriptNodeInstanceInputFilter *instance = memnew(VisualScriptNodeInstanceInputFilter);
  1233. instance->instance = p_instance;
  1234. instance->filters = filters;
  1235. return instance;
  1236. }
  1237. VisualScriptInputFilter::VisualScriptInputFilter() {
  1238. }
  1239. #endif
  1240. //////////////////////////////////////////
  1241. ////////////////TYPE CAST///////////
  1242. //////////////////////////////////////////
  1243. int VisualScriptTypeCast::get_output_sequence_port_count() const {
  1244. return 2;
  1245. }
  1246. bool VisualScriptTypeCast::has_input_sequence_port() const {
  1247. return true;
  1248. }
  1249. int VisualScriptTypeCast::get_input_value_port_count() const {
  1250. return 1;
  1251. }
  1252. int VisualScriptTypeCast::get_output_value_port_count() const {
  1253. return 1;
  1254. }
  1255. String VisualScriptTypeCast::get_output_sequence_port_text(int p_port) const {
  1256. return p_port == 0 ? "yes" : "no";
  1257. }
  1258. PropertyInfo VisualScriptTypeCast::get_input_value_port_info(int p_idx) const {
  1259. return PropertyInfo(Variant::OBJECT, "instance");
  1260. }
  1261. PropertyInfo VisualScriptTypeCast::get_output_value_port_info(int p_idx) const {
  1262. return PropertyInfo(Variant::OBJECT, "");
  1263. }
  1264. String VisualScriptTypeCast::get_caption() const {
  1265. return "TypeCast";
  1266. }
  1267. String VisualScriptTypeCast::get_text() const {
  1268. if (script != String())
  1269. return "Is " + script.get_file() + "?";
  1270. else
  1271. return "Is " + base_type + "?";
  1272. }
  1273. void VisualScriptTypeCast::set_base_type(const StringName &p_type) {
  1274. if (base_type == p_type)
  1275. return;
  1276. base_type = p_type;
  1277. _change_notify();
  1278. ports_changed_notify();
  1279. }
  1280. StringName VisualScriptTypeCast::get_base_type() const {
  1281. return base_type;
  1282. }
  1283. void VisualScriptTypeCast::set_base_script(const String &p_path) {
  1284. if (script == p_path)
  1285. return;
  1286. script = p_path;
  1287. _change_notify();
  1288. ports_changed_notify();
  1289. }
  1290. String VisualScriptTypeCast::get_base_script() const {
  1291. return script;
  1292. }
  1293. VisualScriptTypeCast::TypeGuess VisualScriptTypeCast::guess_output_type(TypeGuess *p_inputs, int p_output) const {
  1294. TypeGuess tg;
  1295. tg.type = Variant::OBJECT;
  1296. if (script != String()) {
  1297. tg.script = ResourceLoader::load(script);
  1298. }
  1299. //if (!tg.script.is_valid()) {
  1300. // tg.gdclass = base_type;
  1301. //}
  1302. return tg;
  1303. }
  1304. class VisualScriptNodeInstanceTypeCast : public VisualScriptNodeInstance {
  1305. public:
  1306. VisualScriptInstance *instance;
  1307. StringName base_type;
  1308. String script;
  1309. //virtual int get_working_memory_size() const { return 0; }
  1310. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1311. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return false; }
  1312. 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) {
  1313. Object *obj = *p_inputs[0];
  1314. *p_outputs[0] = Variant();
  1315. if (!obj) {
  1316. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1317. r_error_str = "Instance is null";
  1318. return 0;
  1319. }
  1320. if (script != String()) {
  1321. Ref<Script> obj_script = obj->get_script();
  1322. if (!obj_script.is_valid()) {
  1323. return 1; //well, definitely not the script because object we got has no script.
  1324. }
  1325. if (!ResourceCache::has(script)) {
  1326. //if the script is not in use by anyone, we can safely assume whathever we got is not casting to it.
  1327. return 1;
  1328. }
  1329. Ref<Script> cast_script = Ref<Resource>(ResourceCache::get(script));
  1330. if (!cast_script.is_valid()) {
  1331. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1332. r_error_str = "Script path is not a script: " + script;
  1333. return 1;
  1334. }
  1335. while (obj_script.is_valid()) {
  1336. if (cast_script == obj_script) {
  1337. *p_outputs[0] = *p_inputs[0]; //copy
  1338. return 0; // it is the script, yey
  1339. }
  1340. obj_script = obj_script->get_base_script();
  1341. }
  1342. return 1; //not found sorry
  1343. }
  1344. if (ClassDB::is_parent_class(obj->get_class_name(), base_type)) {
  1345. *p_outputs[0] = *p_inputs[0]; //copy
  1346. return 0;
  1347. } else
  1348. return 1;
  1349. }
  1350. };
  1351. VisualScriptNodeInstance *VisualScriptTypeCast::instance(VisualScriptInstance *p_instance) {
  1352. VisualScriptNodeInstanceTypeCast *instance = memnew(VisualScriptNodeInstanceTypeCast);
  1353. instance->instance = p_instance;
  1354. instance->base_type = base_type;
  1355. instance->script = script;
  1356. return instance;
  1357. }
  1358. void VisualScriptTypeCast::_bind_methods() {
  1359. ClassDB::bind_method(D_METHOD("set_base_type", "type"), &VisualScriptTypeCast::set_base_type);
  1360. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptTypeCast::get_base_type);
  1361. ClassDB::bind_method(D_METHOD("set_base_script", "path"), &VisualScriptTypeCast::set_base_script);
  1362. ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptTypeCast::get_base_script);
  1363. List<String> script_extensions;
  1364. for (int i = 0; i > ScriptServer::get_language_count(); i++) {
  1365. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  1366. }
  1367. String script_ext_hint;
  1368. for (List<String>::Element *E = script_extensions.front(); E; E = E->next()) {
  1369. if (script_ext_hint != String())
  1370. script_ext_hint += ",";
  1371. script_ext_hint += "*." + E->get();
  1372. }
  1373. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  1374. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
  1375. }
  1376. VisualScriptTypeCast::VisualScriptTypeCast() {
  1377. base_type = "Object";
  1378. }
  1379. void register_visual_script_flow_control_nodes() {
  1380. VisualScriptLanguage::singleton->add_register_func("flow_control/return", create_return_node<false>);
  1381. VisualScriptLanguage::singleton->add_register_func("flow_control/return_with_value", create_return_node<true>);
  1382. VisualScriptLanguage::singleton->add_register_func("flow_control/condition", create_node_generic<VisualScriptCondition>);
  1383. VisualScriptLanguage::singleton->add_register_func("flow_control/while", create_node_generic<VisualScriptWhile>);
  1384. VisualScriptLanguage::singleton->add_register_func("flow_control/iterator", create_node_generic<VisualScriptIterator>);
  1385. VisualScriptLanguage::singleton->add_register_func("flow_control/sequence", create_node_generic<VisualScriptSequence>);
  1386. VisualScriptLanguage::singleton->add_register_func("flow_control/switch", create_node_generic<VisualScriptSwitch>);
  1387. //VisualScriptLanguage::singleton->add_register_func("flow_control/input_filter", create_node_generic<VisualScriptInputFilter>);
  1388. VisualScriptLanguage::singleton->add_register_func("flow_control/type_cast", create_node_generic<VisualScriptTypeCast>);
  1389. }