12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853 |
- /*************************************************************************/
- /* visual_script_flow_control.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* http://www.godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
- /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "visual_script_flow_control.h"
- #include "io/resource_loader.h"
- #include "os/keyboard.h"
- #include "project_settings.h"
- //////////////////////////////////////////
- ////////////////RETURN////////////////////
- //////////////////////////////////////////
- int VisualScriptReturn::get_output_sequence_port_count() const {
- return 0;
- }
- bool VisualScriptReturn::has_input_sequence_port() const {
- return true;
- }
- int VisualScriptReturn::get_input_value_port_count() const {
- return with_value ? 1 : 0;
- }
- int VisualScriptReturn::get_output_value_port_count() const {
- return 0;
- }
- String VisualScriptReturn::get_output_sequence_port_text(int p_port) const {
- return String();
- }
- PropertyInfo VisualScriptReturn::get_input_value_port_info(int p_idx) const {
- PropertyInfo pinfo;
- pinfo.name = "result";
- pinfo.type = type;
- return pinfo;
- }
- PropertyInfo VisualScriptReturn::get_output_value_port_info(int p_idx) const {
- return PropertyInfo();
- }
- String VisualScriptReturn::get_caption() const {
- return "Return";
- }
- String VisualScriptReturn::get_text() const {
- return get_name();
- }
- void VisualScriptReturn::set_return_type(Variant::Type p_type) {
- if (type == p_type)
- return;
- type = p_type;
- ports_changed_notify();
- }
- Variant::Type VisualScriptReturn::get_return_type() const {
- return type;
- }
- void VisualScriptReturn::set_enable_return_value(bool p_enable) {
- if (with_value == p_enable)
- return;
- with_value = p_enable;
- ports_changed_notify();
- }
- bool VisualScriptReturn::is_return_value_enabled() const {
- return with_value;
- }
- void VisualScriptReturn::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_return_type", "type"), &VisualScriptReturn::set_return_type);
- ClassDB::bind_method(D_METHOD("get_return_type"), &VisualScriptReturn::get_return_type);
- ClassDB::bind_method(D_METHOD("set_enable_return_value", "enable"), &VisualScriptReturn::set_enable_return_value);
- ClassDB::bind_method(D_METHOD("is_return_value_enabled"), &VisualScriptReturn::is_return_value_enabled);
- String argt = "Any";
- for (int i = 1; i < Variant::VARIANT_MAX; i++) {
- argt += "," + Variant::get_type_name(Variant::Type(i));
- }
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "return_enabled"), "set_enable_return_value", "is_return_value_enabled");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "return_type", PROPERTY_HINT_ENUM, argt), "set_return_type", "get_return_type");
- }
- class VisualScriptNodeInstanceReturn : public VisualScriptNodeInstance {
- public:
- VisualScriptReturn *node;
- VisualScriptInstance *instance;
- bool with_value;
- virtual int get_working_memory_size() const { return 1; }
- //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
- //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
- 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) {
- if (with_value) {
- *p_working_mem = *p_inputs[0];
- return STEP_EXIT_FUNCTION_BIT;
- } else {
- *p_working_mem = Variant();
- return 0;
- }
- }
- };
- VisualScriptNodeInstance *VisualScriptReturn::instance(VisualScriptInstance *p_instance) {
- VisualScriptNodeInstanceReturn *instance = memnew(VisualScriptNodeInstanceReturn);
- instance->node = this;
- instance->instance = p_instance;
- instance->with_value = with_value;
- return instance;
- }
- VisualScriptReturn::VisualScriptReturn() {
- with_value = false;
- type = Variant::NIL;
- }
- template <bool with_value>
- static Ref<VisualScriptNode> create_return_node(const String &p_name) {
- Ref<VisualScriptReturn> node;
- node.instance();
- node->set_enable_return_value(with_value);
- return node;
- }
- //////////////////////////////////////////
- ////////////////CONDITION/////////////////
- //////////////////////////////////////////
- int VisualScriptCondition::get_output_sequence_port_count() const {
- return 3;
- }
- bool VisualScriptCondition::has_input_sequence_port() const {
- return true;
- }
- int VisualScriptCondition::get_input_value_port_count() const {
- return 1;
- }
- int VisualScriptCondition::get_output_value_port_count() const {
- return 0;
- }
- String VisualScriptCondition::get_output_sequence_port_text(int p_port) const {
- if (p_port == 0)
- return "true";
- else if (p_port == 1)
- return "false";
- else
- return "done";
- }
- PropertyInfo VisualScriptCondition::get_input_value_port_info(int p_idx) const {
- PropertyInfo pinfo;
- pinfo.name = "cond";
- pinfo.type = Variant::BOOL;
- return pinfo;
- }
- PropertyInfo VisualScriptCondition::get_output_value_port_info(int p_idx) const {
- return PropertyInfo();
- }
- String VisualScriptCondition::get_caption() const {
- return "Condition";
- }
- String VisualScriptCondition::get_text() const {
- return "if (cond) is: ";
- }
- void VisualScriptCondition::_bind_methods() {
- }
- class VisualScriptNodeInstanceCondition : public VisualScriptNodeInstance {
- public:
- VisualScriptCondition *node;
- VisualScriptInstance *instance;
- //virtual int get_working_memory_size() const { return 1; }
- //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
- //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
- 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) {
- if (p_start_mode == START_MODE_CONTINUE_SEQUENCE)
- return 2;
- else if (p_inputs[0]->operator bool())
- return 0 | STEP_FLAG_PUSH_STACK_BIT;
- else
- return 1 | STEP_FLAG_PUSH_STACK_BIT;
- }
- };
- VisualScriptNodeInstance *VisualScriptCondition::instance(VisualScriptInstance *p_instance) {
- VisualScriptNodeInstanceCondition *instance = memnew(VisualScriptNodeInstanceCondition);
- instance->node = this;
- instance->instance = p_instance;
- return instance;
- }
- VisualScriptCondition::VisualScriptCondition() {
- }
- //////////////////////////////////////////
- ////////////////WHILE/////////////////
- //////////////////////////////////////////
- int VisualScriptWhile::get_output_sequence_port_count() const {
- return 2;
- }
- bool VisualScriptWhile::has_input_sequence_port() const {
- return true;
- }
- int VisualScriptWhile::get_input_value_port_count() const {
- return 1;
- }
- int VisualScriptWhile::get_output_value_port_count() const {
- return 0;
- }
- String VisualScriptWhile::get_output_sequence_port_text(int p_port) const {
- if (p_port == 0)
- return "repeat";
- else
- return "exit";
- }
- PropertyInfo VisualScriptWhile::get_input_value_port_info(int p_idx) const {
- PropertyInfo pinfo;
- pinfo.name = "cond";
- pinfo.type = Variant::BOOL;
- return pinfo;
- }
- PropertyInfo VisualScriptWhile::get_output_value_port_info(int p_idx) const {
- return PropertyInfo();
- }
- String VisualScriptWhile::get_caption() const {
- return "While";
- }
- String VisualScriptWhile::get_text() const {
- return "while (cond): ";
- }
- void VisualScriptWhile::_bind_methods() {
- }
- class VisualScriptNodeInstanceWhile : public VisualScriptNodeInstance {
- public:
- VisualScriptWhile *node;
- VisualScriptInstance *instance;
- //virtual int get_working_memory_size() const { return 1; }
- //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
- //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
- 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) {
- bool keep_going = p_inputs[0]->operator bool();
- if (keep_going)
- return 0 | STEP_FLAG_PUSH_STACK_BIT;
- else
- return 1;
- }
- };
- VisualScriptNodeInstance *VisualScriptWhile::instance(VisualScriptInstance *p_instance) {
- VisualScriptNodeInstanceWhile *instance = memnew(VisualScriptNodeInstanceWhile);
- instance->node = this;
- instance->instance = p_instance;
- return instance;
- }
- VisualScriptWhile::VisualScriptWhile() {
- }
- //////////////////////////////////////////
- ////////////////ITERATOR/////////////////
- //////////////////////////////////////////
- int VisualScriptIterator::get_output_sequence_port_count() const {
- return 2;
- }
- bool VisualScriptIterator::has_input_sequence_port() const {
- return true;
- }
- int VisualScriptIterator::get_input_value_port_count() const {
- return 1;
- }
- int VisualScriptIterator::get_output_value_port_count() const {
- return 1;
- }
- String VisualScriptIterator::get_output_sequence_port_text(int p_port) const {
- if (p_port == 0)
- return "each";
- else
- return "exit";
- }
- PropertyInfo VisualScriptIterator::get_input_value_port_info(int p_idx) const {
- PropertyInfo pinfo;
- pinfo.name = "input";
- pinfo.type = Variant::NIL;
- return pinfo;
- }
- PropertyInfo VisualScriptIterator::get_output_value_port_info(int p_idx) const {
- PropertyInfo pinfo;
- pinfo.name = "elem";
- pinfo.type = Variant::NIL;
- return pinfo;
- }
- String VisualScriptIterator::get_caption() const {
- return "Iterator";
- }
- String VisualScriptIterator::get_text() const {
- return "for (elem) in (input): ";
- }
- void VisualScriptIterator::_bind_methods() {
- }
- class VisualScriptNodeInstanceIterator : public VisualScriptNodeInstance {
- public:
- VisualScriptIterator *node;
- VisualScriptInstance *instance;
- virtual int get_working_memory_size() const { return 2; }
- //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
- //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
- 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) {
- if (p_start_mode == START_MODE_BEGIN_SEQUENCE) {
- p_working_mem[0] = *p_inputs[0];
- bool valid;
- bool can_iter = p_inputs[0]->iter_init(p_working_mem[1], valid);
- if (!valid) {
- r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
- r_error_str = RTR("Input type not iterable: ") + Variant::get_type_name(p_inputs[0]->get_type());
- return 0;
- }
- if (!can_iter)
- return 1; //nothing to iterate
- *p_outputs[0] = p_working_mem[0].iter_get(p_working_mem[1], valid);
- if (!valid) {
- r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
- r_error_str = RTR("Iterator became invalid");
- return 0;
- }
- } else { //continue sequence
- bool valid;
- bool can_iter = p_working_mem[0].iter_next(p_working_mem[1], valid);
- if (!valid) {
- r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
- r_error_str = RTR("Iterator became invalid: ") + Variant::get_type_name(p_inputs[0]->get_type());
- return 0;
- }
- if (!can_iter)
- return 1; //nothing to iterate
- *p_outputs[0] = p_working_mem[0].iter_get(p_working_mem[1], valid);
- if (!valid) {
- r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
- r_error_str = RTR("Iterator became invalid");
- return 0;
- }
- }
- return 0 | STEP_FLAG_PUSH_STACK_BIT; //go around
- }
- };
- VisualScriptNodeInstance *VisualScriptIterator::instance(VisualScriptInstance *p_instance) {
- VisualScriptNodeInstanceIterator *instance = memnew(VisualScriptNodeInstanceIterator);
- instance->node = this;
- instance->instance = p_instance;
- return instance;
- }
- VisualScriptIterator::VisualScriptIterator() {
- }
- //////////////////////////////////////////
- ////////////////SEQUENCE/////////////////
- //////////////////////////////////////////
- int VisualScriptSequence::get_output_sequence_port_count() const {
- return steps;
- }
- bool VisualScriptSequence::has_input_sequence_port() const {
- return true;
- }
- int VisualScriptSequence::get_input_value_port_count() const {
- return 0;
- }
- int VisualScriptSequence::get_output_value_port_count() const {
- return 1;
- }
- String VisualScriptSequence::get_output_sequence_port_text(int p_port) const {
- return itos(p_port + 1);
- }
- PropertyInfo VisualScriptSequence::get_input_value_port_info(int p_idx) const {
- return PropertyInfo();
- }
- PropertyInfo VisualScriptSequence::get_output_value_port_info(int p_idx) const {
- return PropertyInfo(Variant::INT, "current");
- }
- String VisualScriptSequence::get_caption() const {
- return "Sequence";
- }
- String VisualScriptSequence::get_text() const {
- return "in order: ";
- }
- void VisualScriptSequence::set_steps(int p_steps) {
- ERR_FAIL_COND(p_steps < 1);
- if (steps == p_steps)
- return;
- steps = p_steps;
- ports_changed_notify();
- }
- int VisualScriptSequence::get_steps() const {
- return steps;
- }
- void VisualScriptSequence::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_steps", "steps"), &VisualScriptSequence::set_steps);
- ClassDB::bind_method(D_METHOD("get_steps"), &VisualScriptSequence::get_steps);
- ADD_PROPERTY(PropertyInfo(Variant::INT, "steps", PROPERTY_HINT_RANGE, "1,64,1"), "set_steps", "get_steps");
- }
- class VisualScriptNodeInstanceSequence : public VisualScriptNodeInstance {
- public:
- VisualScriptSequence *node;
- VisualScriptInstance *instance;
- int steps;
- virtual int get_working_memory_size() const { return 1; }
- //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
- //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
- 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) {
- if (p_start_mode == START_MODE_BEGIN_SEQUENCE) {
- p_working_mem[0] = 0;
- }
- int step = p_working_mem[0];
- *p_outputs[0] = step;
- if (step + 1 == steps)
- return step;
- else {
- p_working_mem[0] = step + 1;
- return step | STEP_FLAG_PUSH_STACK_BIT;
- }
- }
- };
- VisualScriptNodeInstance *VisualScriptSequence::instance(VisualScriptInstance *p_instance) {
- VisualScriptNodeInstanceSequence *instance = memnew(VisualScriptNodeInstanceSequence);
- instance->node = this;
- instance->instance = p_instance;
- instance->steps = steps;
- return instance;
- }
- VisualScriptSequence::VisualScriptSequence() {
- steps = 1;
- }
- //////////////////////////////////////////
- ////////////////EVENT TYPE FILTER///////////
- //////////////////////////////////////////
- int VisualScriptSwitch::get_output_sequence_port_count() const {
- return case_values.size() + 1;
- }
- bool VisualScriptSwitch::has_input_sequence_port() const {
- return true;
- }
- int VisualScriptSwitch::get_input_value_port_count() const {
- return case_values.size() + 1;
- }
- int VisualScriptSwitch::get_output_value_port_count() const {
- return 0;
- }
- String VisualScriptSwitch::get_output_sequence_port_text(int p_port) const {
- if (p_port == case_values.size())
- return "done";
- return String();
- }
- PropertyInfo VisualScriptSwitch::get_input_value_port_info(int p_idx) const {
- if (p_idx < case_values.size()) {
- return PropertyInfo(case_values[p_idx].type, " =");
- } else
- return PropertyInfo(Variant::NIL, "input");
- }
- PropertyInfo VisualScriptSwitch::get_output_value_port_info(int p_idx) const {
- return PropertyInfo();
- }
- String VisualScriptSwitch::get_caption() const {
- return "Switch";
- }
- String VisualScriptSwitch::get_text() const {
- return "'input' is:";
- }
- class VisualScriptNodeInstanceSwitch : public VisualScriptNodeInstance {
- public:
- VisualScriptInstance *instance;
- int case_count;
- //virtual int get_working_memory_size() const { return 0; }
- //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
- //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return false; }
- 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) {
- if (p_start_mode == START_MODE_CONTINUE_SEQUENCE) {
- return case_count; //exit
- }
- for (int i = 0; i < case_count; i++) {
- if (*p_inputs[i] == *p_inputs[case_count]) {
- return i | STEP_FLAG_PUSH_STACK_BIT;
- }
- }
- return case_count;
- }
- };
- VisualScriptNodeInstance *VisualScriptSwitch::instance(VisualScriptInstance *p_instance) {
- VisualScriptNodeInstanceSwitch *instance = memnew(VisualScriptNodeInstanceSwitch);
- instance->instance = p_instance;
- instance->case_count = case_values.size();
- return instance;
- }
- bool VisualScriptSwitch::_set(const StringName &p_name, const Variant &p_value) {
- if (String(p_name) == "case_count") {
- case_values.resize(p_value);
- _change_notify();
- ports_changed_notify();
- return true;
- }
- if (String(p_name).begins_with("case/")) {
- int idx = String(p_name).get_slice("/", 1).to_int();
- ERR_FAIL_INDEX_V(idx, case_values.size(), false);
- case_values[idx].type = Variant::Type(int(p_value));
- _change_notify();
- ports_changed_notify();
- return true;
- }
- return false;
- }
- bool VisualScriptSwitch::_get(const StringName &p_name, Variant &r_ret) const {
- if (String(p_name) == "case_count") {
- r_ret = case_values.size();
- return true;
- }
- if (String(p_name).begins_with("case/")) {
- int idx = String(p_name).get_slice("/", 1).to_int();
- ERR_FAIL_INDEX_V(idx, case_values.size(), false);
- r_ret = case_values[idx].type;
- return true;
- }
- return false;
- }
- void VisualScriptSwitch::_get_property_list(List<PropertyInfo> *p_list) const {
- p_list->push_back(PropertyInfo(Variant::INT, "case_count", PROPERTY_HINT_RANGE, "0,128"));
- String argt = "Any";
- for (int i = 1; i < Variant::VARIANT_MAX; i++) {
- argt += "," + Variant::get_type_name(Variant::Type(i));
- }
- for (int i = 0; i < case_values.size(); i++) {
- p_list->push_back(PropertyInfo(Variant::INT, "case/" + itos(i), PROPERTY_HINT_ENUM, argt));
- }
- }
- void VisualScriptSwitch::_bind_methods() {
- }
- VisualScriptSwitch::VisualScriptSwitch() {
- }
- //////////////////////////////////////////
- ////////////////EVENT ACTION FILTER///////////
- //////////////////////////////////////////
- #if 0
- int VisualScriptInputFilter::get_output_sequence_port_count() const {
- return filters.size();
- }
- bool VisualScriptInputFilter::has_input_sequence_port() const {
- return true;
- }
- int VisualScriptInputFilter::get_input_value_port_count() const {
- return 1;
- }
- int VisualScriptInputFilter::get_output_value_port_count() const {
- return 1;
- }
- String VisualScriptInputFilter::get_output_sequence_port_text(int p_port) const {
- String text;
- switch (filters[p_port].type) {
- case Ref<InputEvent>::NONE: {
- text = "None";
- } break;
- case Ref<InputEvent>::KEY: {
- InputEventKey k = filters[p_port].key;
- if (k->get_scancode() == 0 && k.unicode == 0) {
- text = "No Key";
- } else {
- if (k->get_scancode() != 0) {
- text = "KeyCode: " + keycode_get_string(k->get_scancode());
- } else if (k.unicode != 0) {
- text = "Uniode: " + String::chr(k.unicode);
- }
- if (k->is_pressed())
- text += ", Pressed";
- else
- text += ", Released";
- if (k.echo)
- text += ", Echo";
- if (k->get_alt())
- text = "Alt+" + text;
- if (k->get_shift())
- text = "Shift+" + text;
- if (k->get_control())
- text = "Ctrl+" + text;
- if (k->get_metakey())
- text = "Meta+" + text;
- }
- } break;
- case Ref<InputEvent>::MOUSE_MOTION: {
- InputEventMouseMotion mm = filters[p_port].mouse_motion;
- text = "Mouse Motion";
- String b = "Left,Right,Middle,WheelUp,WheelDown,WheelLeft,WheelRight";
- for (int i = 0; i < 7; i++) {
- if (mm->get_button_mask() & (1 << i)) {
- text = b.get_slice(",", i) + "+" + text;
- }
- }
- if (mm->get_alt())
- text = "Alt+" + text;
- if (mm->get_shift())
- text = "Shift+" + text;
- if (mm->get_control())
- text = "Ctrl+" + text;
- if (mm->get_metakey())
- text = "Meta+" + text;
- } break;
- case Ref<InputEvent>::MOUSE_BUTTON: {
- InputEventMouseButton mb = filters[p_port].mouse_button;
- String b = "Any,Left,Right,Middle,WheelUp,WheelDown,WheelLeft,WheelRight";
- text = b.get_slice(",", mb->get_button_index()) + " Mouse Button";
- if (mb->is_pressed())
- text += ", Pressed";
- else
- text += ", Released";
- if (mb.doubleclick)
- text += ", DblClick";
- if (mb->get_alt())
- text = "Alt+" + text;
- if (mb->get_shift())
- text = "Shift+" + text;
- if (mb->get_control())
- text = "Ctrl+" + text;
- if (mb->get_metakey())
- text = "Meta+" + text;
- } break;
- case Ref<InputEvent>::JOYPAD_MOTION: {
- InputEventJoypadMotion jm = filters[p_port].joy_motion;
- text = "JoyMotion Axis " + itos(jm.axis >> 1);
- if (jm.axis & 1)
- text += " > " + rtos(jm.axis_value);
- else
- text += " < " + rtos(-jm.axis_value);
- } break;
- case Ref<InputEvent>::JOYPAD_BUTTON: {
- InputEventJoypadButton jb = filters[p_port].joy_button;
- text = "JoyButton " + itos(jb->get_button_index());
- if (jb->is_pressed())
- text += ", Pressed";
- else
- text += ", Released";
- } break;
- case Ref<InputEvent>::SCREEN_TOUCH: {
- InputEventScreenTouch sd = filters[p_port].screen_touch;
- text = "Touch Finger " + itos(sd.index);
- if (sd->is_pressed())
- text += ", Pressed";
- else
- text += ", Released";
- } break;
- case Ref<InputEvent>::SCREEN_DRAG: {
- InputEventScreenDrag sd = filters[p_port].screen_drag;
- text = "Drag Finger " + itos(sd.index);
- } break;
- case Ref<InputEvent>::ACTION: {
- List<PropertyInfo> pinfo;
- ProjectSettings::get_singleton()->get_property_list(&pinfo);
- int index = 1;
- text = "No Action";
- for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
- const PropertyInfo &pi = E->get();
- if (!pi.name.begins_with("input/"))
- continue;
- if (filters[p_port].action.action == index) {
- text = "Action " + pi.name.substr(pi.name.find("/") + 1, pi.name.length());
- break;
- }
- index++;
- }
- if (filters[p_port].action->is_pressed())
- text += ", Pressed";
- else
- text += ", Released";
- } break;
- }
- return text + " - " + itos(p_port);
- }
- PropertyInfo VisualScriptInputFilter::get_input_value_port_info(int p_idx) const {
- return PropertyInfo(Variant::INPUT_EVENT, "event");
- }
- PropertyInfo VisualScriptInputFilter::get_output_value_port_info(int p_idx) const {
- return PropertyInfo(Variant::INPUT_EVENT, "");
- }
- String VisualScriptInputFilter::get_caption() const {
- return "InputFilter";
- }
- String VisualScriptInputFilter::get_text() const {
- return "";
- }
- bool VisualScriptInputFilter::_set(const StringName &p_name, const Variant &p_value) {
- if (p_name == "filter_count") {
- filters.resize(p_value);
- _change_notify();
- ports_changed_notify();
- return true;
- }
- if (String(p_name).begins_with("filter_")) {
- int idx = String(p_name).replace_first("filters_", "").get_slice("/", 0).to_int();
- ERR_FAIL_INDEX_V(idx, filters.size(), false);
- String what = String(p_name).get_slice("/", 1);
- if (what == "type") {
- filters[idx] = Ref<InputEvent>();
- filters[idx].type = Ref<InputEvent>::Type(int(p_value));
- if (filters[idx].type == Ref<InputEvent>::JOYPAD_MOTION) {
- filters[idx].joy_motion.axis_value = 0.5; //for threshold
- } else if (filters[idx].type == Ref<InputEvent>::KEY) {
- filters[idx]->is_pressed() = true; //put these as true to make it more user friendly
- } else if (filters[idx].type == Ref<InputEvent>::MOUSE_BUTTON) {
- filters[idx]->is_pressed() = true;
- } else if (filters[idx].type == Ref<InputEvent>::JOYPAD_BUTTON) {
- filters[idx].joy_button->is_pressed() = true;
- } else if (filters[idx].type == Ref<InputEvent>::SCREEN_TOUCH) {
- filters[idx].screen_touch->is_pressed() = true;
- } else if (filters[idx].type == Ref<InputEvent>::ACTION) {
- filters[idx].action->is_pressed() = true;
- }
- _change_notify();
- ports_changed_notify();
- return true;
- }
- if (what == "device") {
- filters[idx].device = p_value;
- ports_changed_notify();
- return true;
- }
- switch (filters[idx].type) {
- case Ref<InputEvent>::KEY: {
- if (what == "scancode") {
- String sc = p_value;
- if (sc == String()) {
- filters[idx]->get_scancode() = 0;
- } else {
- filters[idx]->get_scancode() = find_keycode(p_value);
- }
- } else if (what == "unicode") {
- String uc = p_value;
- if (uc == String()) {
- filters[idx].key.unicode = 0;
- } else {
- filters[idx].key.unicode = uc[0];
- }
- } else if (what == "pressed") {
- filters[idx]->is_pressed() = p_value;
- } else if (what == "echo") {
- filters[idx]->is_echo() = p_value;
- } else if (what == "mod_alt") {
- filters[idx]->get_alt() = p_value;
- } else if (what == "mod_shift") {
- filters[idx]->get_shift() = p_value;
- } else if (what == "mod_ctrl") {
- filters[idx]->get_control() = p_value;
- } else if (what == "mod_meta") {
- filters[idx]->get_metakey() = p_value;
- } else {
- return false;
- }
- ports_changed_notify();
- return true;
- } break;
- case Ref<InputEvent>::MOUSE_MOTION: {
- if (what == "button_mask") {
- filters[idx]->get_button_mask() = p_value;
- } else if (what == "mod_alt") {
- filters[idx].mouse_motion->get_alt() = p_value;
- } else if (what == "mod_shift") {
- filters[idx].mouse_motion->get_shift() = p_value;
- } else if (what == "mod_ctrl") {
- filters[idx].mouse_motion->get_control() = p_value;
- } else if (what == "mod_meta") {
- filters[idx].mouse_motion->get_metakey() = p_value;
- } else {
- return false;
- }
- ports_changed_notify();
- return true;
- } break;
- case Ref<InputEvent>::MOUSE_BUTTON: {
- if (what == "button_index") {
- filters[idx]->get_button_index() = p_value;
- } else if (what == "pressed") {
- filters[idx]->is_pressed() = p_value;
- } else if (what == "doubleclicked") {
- filters[idx].mouse_button.doubleclick = p_value;
- } else if (what == "mod_alt") {
- filters[idx].mouse_button->get_alt() = p_value;
- } else if (what == "mod_shift") {
- filters[idx].mouse_button->get_shift() = p_value;
- } else if (what == "mod_ctrl") {
- filters[idx].mouse_button->get_control() = p_value;
- } else if (what == "mod_meta") {
- filters[idx].mouse_button->get_metakey() = p_value;
- } else {
- return false;
- }
- ports_changed_notify();
- return true;
- } break;
- case Ref<InputEvent>::JOYPAD_MOTION: {
- if (what == "axis") {
- filters[idx].joy_motion.axis = int(p_value) << 1 | filters[idx].joy_motion.axis;
- } else if (what == "mode") {
- filters[idx].joy_motion.axis |= int(p_value);
- } else if (what == "threshold") {
- filters[idx].joy_motion.axis_value = p_value;
- } else {
- return false;
- }
- ports_changed_notify();
- return true;
- } break;
- case Ref<InputEvent>::JOYPAD_BUTTON: {
- if (what == "button_index") {
- filters[idx].joy_button->get_button_index() = p_value;
- } else if (what == "pressed") {
- filters[idx].joy_button->is_pressed() = p_value;
- } else {
- return false;
- }
- ports_changed_notify();
- return true;
- } break;
- case Ref<InputEvent>::SCREEN_TOUCH: {
- if (what == "finger_index") {
- filters[idx].screen_touch.index = p_value;
- } else if (what == "pressed") {
- filters[idx].screen_touch->is_pressed() = p_value;
- } else {
- return false;
- }
- ports_changed_notify();
- return true;
- } break;
- case Ref<InputEvent>::SCREEN_DRAG: {
- if (what == "finger_index") {
- filters[idx].screen_drag.index = p_value;
- } else {
- return false;
- }
- ports_changed_notify();
- return true;
- } break;
- case Ref<InputEvent>::ACTION: {
- if (what == "action_name") {
- List<PropertyInfo> pinfo;
- ProjectSettings::get_singleton()->get_property_list(&pinfo);
- int index = 1;
- for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
- const PropertyInfo &pi = E->get();
- if (!pi.name.begins_with("input/"))
- continue;
- String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
- if (name == String(p_value)) {
- filters[idx].action.action = index;
- ports_changed_notify();
- return true;
- }
- index++;
- }
- filters[idx].action.action = 0;
- ports_changed_notify();
- return false;
- } else if (what == "pressed") {
- filters[idx].action->is_pressed() = p_value;
- ports_changed_notify();
- return true;
- }
- } break;
- }
- }
- return false;
- }
- bool VisualScriptInputFilter::_get(const StringName &p_name, Variant &r_ret) const {
- if (p_name == "filter_count") {
- r_ret = filters.size();
- return true;
- }
- if (String(p_name).begins_with("filter_")) {
- int idx = String(p_name).replace_first("filters_", "").get_slice("/", 0).to_int();
- ERR_FAIL_INDEX_V(idx, filters.size(), false);
- String what = String(p_name).get_slice("/", 1);
- if (what == "type") {
- r_ret = filters[idx].type;
- return true;
- }
- if (what == "device") {
- r_ret = filters[idx].device;
- return true;
- }
- switch (filters[idx].type) {
- case Ref<InputEvent>::KEY: {
- if (what == "scancode") {
- if (filters[idx]->get_scancode() == 0)
- r_ret = String();
- else {
- r_ret = keycode_get_string(filters[idx]->get_scancode());
- }
- } else if (what == "unicode") {
- if (filters[idx].key.unicode == 0) {
- r_ret = String();
- } else {
- CharType str[2] = { (CharType)filters[idx].key.unicode, 0 };
- r_ret = String(str);
- }
- } else if (what == "pressed") {
- r_ret = filters[idx]->is_pressed();
- } else if (what == "echo") {
- r_ret = filters[idx]->is_echo();
- } else if (what == "mod_alt") {
- r_ret = filters[idx]->get_alt();
- } else if (what == "mod_shift") {
- r_ret = filters[idx]->get_shift();
- } else if (what == "mod_ctrl") {
- r_ret = filters[idx]->get_control();
- } else if (what == "mod_meta") {
- r_ret = filters[idx]->get_metakey();
- } else {
- return false;
- }
- return true;
- } break;
- case Ref<InputEvent>::MOUSE_MOTION: {
- if (what == "button_mask") {
- r_ret = filters[idx]->get_button_mask();
- } else if (what == "mod_alt") {
- r_ret = filters[idx].mouse_motion->get_alt();
- } else if (what == "mod_shift") {
- r_ret = filters[idx].mouse_motion->get_shift();
- } else if (what == "mod_ctrl") {
- r_ret = filters[idx].mouse_motion->get_control();
- } else if (what == "mod_meta") {
- r_ret = filters[idx].mouse_motion->get_metakey();
- } else {
- return false;
- }
- return true;
- } break;
- case Ref<InputEvent>::MOUSE_BUTTON: {
- if (what == "button_index") {
- r_ret = filters[idx]->get_button_index();
- } else if (what == "pressed") {
- r_ret = filters[idx]->is_pressed();
- } else if (what == "doubleclicked") {
- r_ret = filters[idx].mouse_button.doubleclick;
- } else if (what == "mod_alt") {
- r_ret = filters[idx].mouse_button->get_alt();
- } else if (what == "mod_shift") {
- r_ret = filters[idx].mouse_button->get_shift();
- } else if (what == "mod_ctrl") {
- r_ret = filters[idx].mouse_button->get_control();
- } else if (what == "mod_meta") {
- r_ret = filters[idx].mouse_button->get_metakey();
- } else {
- return false;
- }
- return true;
- } break;
- case Ref<InputEvent>::JOYPAD_MOTION: {
- if (what == "axis_index") {
- r_ret = filters[idx].joy_motion.axis >> 1;
- } else if (what == "mode") {
- r_ret = filters[idx].joy_motion.axis & 1;
- } else if (what == "threshold") {
- r_ret = filters[idx].joy_motion.axis_value;
- } else {
- return false;
- }
- return true;
- } break;
- case Ref<InputEvent>::JOYPAD_BUTTON: {
- if (what == "button_index") {
- r_ret = filters[idx].joy_button->get_button_index();
- } else if (what == "pressed") {
- r_ret = filters[idx].joy_button->is_pressed();
- } else {
- return false;
- }
- return true;
- } break;
- case Ref<InputEvent>::SCREEN_TOUCH: {
- if (what == "finger_index") {
- r_ret = filters[idx].screen_touch.index;
- } else if (what == "pressed") {
- r_ret = filters[idx].screen_touch->is_pressed();
- } else {
- return false;
- }
- return true;
- } break;
- case Ref<InputEvent>::SCREEN_DRAG: {
- if (what == "finger_index") {
- r_ret = filters[idx].screen_drag.index;
- } else {
- return false;
- }
- return true;
- } break;
- case Ref<InputEvent>::ACTION: {
- if (what == "action_name") {
- List<PropertyInfo> pinfo;
- ProjectSettings::get_singleton()->get_property_list(&pinfo);
- int index = 1;
- for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
- const PropertyInfo &pi = E->get();
- if (!pi.name.begins_with("input/"))
- continue;
- if (filters[idx].action.action == index) {
- r_ret = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
- return true;
- }
- index++;
- }
- r_ret = "None"; //no index
- return false;
- } else if (what == "pressed") {
- r_ret = filters[idx].action->is_pressed();
- return true;
- }
- } break;
- }
- }
- return false;
- }
- static const char *event_type_names[Ref<InputEvent>::TYPE_MAX] = {
- "None",
- "Key",
- "MouseMotion",
- "MouseButton",
- "JoypadMotion",
- "JoypadButton",
- "ScreenTouch",
- "ScreenDrag",
- "Action"
- };
- void VisualScriptInputFilter::_get_property_list(List<PropertyInfo> *p_list) const {
- p_list->push_back(PropertyInfo(Variant::INT, "filter_count", PROPERTY_HINT_RANGE, "0,64"));
- String et;
- for (int i = 0; i < Ref<InputEvent>::TYPE_MAX; i++) {
- if (i > 0)
- et += ",";
- et += event_type_names[i];
- }
- String kc;
- String actions;
- for (int i = 0; i < filters.size(); i++) {
- String base = "filter_" + itos(i) + "/";
- p_list->push_back(PropertyInfo(Variant::INT, base + "type", PROPERTY_HINT_ENUM, et));
- p_list->push_back(PropertyInfo(Variant::INT, base + "device"));
- switch (filters[i].type) {
- case Ref<InputEvent>::NONE: {
- } break;
- case Ref<InputEvent>::KEY: {
- if (kc == String()) {
- int kcc = keycode_get_count();
- kc = "None";
- for (int i = 0; i < kcc; i++) {
- kc += ",";
- kc += String(keycode_get_name_by_index(i));
- }
- }
- p_list->push_back(PropertyInfo(Variant::STRING, base + "scancode", PROPERTY_HINT_ENUM, kc));
- p_list->push_back(PropertyInfo(Variant::STRING, base + "unicode"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "pressed"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "echo"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_alt"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_shift"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_ctrl"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_meta"));
- } break;
- case Ref<InputEvent>::MOUSE_MOTION: {
- p_list->push_back(PropertyInfo(Variant::INT, base + "button_mask", PROPERTY_HINT_FLAGS, "Left,Right,Middle,WheelUp,WheelDown,WheelLeft,WheelRight"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_alt"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_shift"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_ctrl"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_meta"));
- } break;
- case Ref<InputEvent>::MOUSE_BUTTON: {
- p_list->push_back(PropertyInfo(Variant::INT, base + "button_index", PROPERTY_HINT_ENUM, "Any,Left,Right,Middle,WheelUp,WheelDown,WheelLeft,WheelRight"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "pressed"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "doubleclicked"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_alt"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_shift"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_ctrl"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "mod_meta"));
- } break;
- case Ref<InputEvent>::JOYPAD_MOTION: {
- p_list->push_back(PropertyInfo(Variant::INT, base + "axis_index"));
- p_list->push_back(PropertyInfo(Variant::INT, base + "mode", PROPERTY_HINT_ENUM, "Min,Max"));
- p_list->push_back(PropertyInfo(Variant::REAL, base + "threshold", PROPERTY_HINT_RANGE, "0,1,0.01"));
- } break;
- case Ref<InputEvent>::JOYPAD_BUTTON: {
- p_list->push_back(PropertyInfo(Variant::INT, base + "button_index"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "pressed"));
- } break;
- case Ref<InputEvent>::SCREEN_TOUCH: {
- p_list->push_back(PropertyInfo(Variant::INT, base + "finger_index"));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "pressed"));
- } break;
- case Ref<InputEvent>::SCREEN_DRAG: {
- p_list->push_back(PropertyInfo(Variant::INT, base + "finger_index"));
- } break;
- case Ref<InputEvent>::ACTION: {
- if (actions == String()) {
- actions = "None";
- List<PropertyInfo> pinfo;
- ProjectSettings::get_singleton()->get_property_list(&pinfo);
- Vector<String> al;
- for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
- const PropertyInfo &pi = E->get();
- if (!pi.name.begins_with("input/"))
- continue;
- String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
- al.push_back(name);
- }
- for (int i = 0; i < al.size(); i++) {
- actions += ",";
- actions += al[i];
- }
- }
- p_list->push_back(PropertyInfo(Variant::STRING, base + "action_name", PROPERTY_HINT_ENUM, actions));
- p_list->push_back(PropertyInfo(Variant::BOOL, base + "pressed"));
- } break;
- }
- }
- }
- class VisualScriptNodeInstanceInputFilter : public VisualScriptNodeInstance {
- public:
- VisualScriptInstance *instance;
- Vector<Ref<InputEvent>> filters;
- //virtual int get_working_memory_size() const { return 0; }
- //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
- //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return false; }
- 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) {
- if (p_inputs[0]->get_type() != Variant::INPUT_EVENT) {
- r_error_str = "Input value not of type event";
- r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
- return 0;
- }
- Ref<InputEvent> event = *p_inputs[0];
- for (int i = 0; i < filters.size(); i++) {
- const Ref<InputEvent> &ie = filters[i];
- if (ie.type != event.type)
- continue;
- bool match = false;
- switch (ie.type) {
- case Ref<InputEvent>::NONE: {
- match = true;
- } break;
- case Ref<InputEvent>::KEY: {
- InputEventKey k = ie.key;
- InputEventKey k2 = event.key;
- if (k->get_scancode() == 0 && k.unicode == 0 && k2->get_scancode() == 0 && k2.unicode == 0) {
- match = true;
- } else {
- if ((k->get_scancode() != 0 && k->get_scancode() == k2->get_scancode()) || (k.unicode != 0 && k.unicode == k2.unicode)) {
- //key valid
- if (
- k->is_pressed() == k2->is_pressed() &&
- k.echo == k2.echo &&
- k.mod == k2.mod) {
- match = true;
- }
- }
- }
- } break;
- case Ref<InputEvent>::MOUSE_MOTION: {
- InputEventMouseMotion mm = ie.mouse_motion;
- InputEventMouseMotion mm2 = event.mouse_motion;
- if (mm->get_button_mask() == mm2->get_button_mask() &&
- mm.mod == mm2.mod) {
- match = true;
- }
- } break;
- case Ref<InputEvent>::MOUSE_BUTTON: {
- InputEventMouseButton mb = ie.mouse_button;
- InputEventMouseButton mb2 = event.mouse_button;
- if (mb->get_button_index() == mb2->get_button_index() &&
- mb->is_pressed() == mb2->is_pressed() &&
- mb.doubleclick == mb2.doubleclick &&
- mb.mod == mb2.mod) {
- match = true;
- }
- } break;
- case Ref<InputEvent>::JOYPAD_MOTION: {
- InputEventJoypadMotion jm = ie.joy_motion;
- InputEventJoypadMotion jm2 = event.joy_motion;
- int axis = jm.axis >> 1;
- if (axis == jm2.axis) {
- if (jm.axis & 1) {
- //greater
- if (jm2.axis_value > jm.axis_value) {
- match = true;
- }
- } else {
- //less
- if (jm2.axis_value < -jm.axis_value) {
- match = true;
- }
- }
- }
- } break;
- case Ref<InputEvent>::JOYPAD_BUTTON: {
- InputEventJoypadButton jb = ie.joy_button;
- InputEventJoypadButton jb2 = event.joy_button;
- if (jb->get_button_index() == jb2->get_button_index() &&
- jb->is_pressed() == jb2->is_pressed()) {
- match = true;
- }
- } break;
- case Ref<InputEvent>::SCREEN_TOUCH: {
- InputEventScreenTouch st = ie.screen_touch;
- InputEventScreenTouch st2 = event.screen_touch;
- if (st.index == st2.index &&
- st->is_pressed() == st2->is_pressed()) {
- match = true;
- }
- } break;
- case Ref<InputEvent>::SCREEN_DRAG: {
- InputEventScreenDrag sd = ie.screen_drag;
- InputEventScreenDrag sd2 = event.screen_drag;
- if (sd.index == sd2.index) {
- match = true;
- }
- } break;
- case Ref<InputEvent>::ACTION: {
- InputEventAction ia = ie.action;
- InputEventAction ia2 = event.action;
- if (ia.action == ia2.action &&
- ia->is_pressed() == ia2->is_pressed()) {
- match = true;
- }
- } break;
- }
- *p_outputs[0] = event;
- if (match)
- return i; //go through match output
- }
- return STEP_NO_ADVANCE_BIT; //none found, don't advance
- }
- };
- VisualScriptNodeInstance *VisualScriptInputFilter::instance(VisualScriptInstance *p_instance) {
- VisualScriptNodeInstanceInputFilter *instance = memnew(VisualScriptNodeInstanceInputFilter);
- instance->instance = p_instance;
- instance->filters = filters;
- return instance;
- }
- VisualScriptInputFilter::VisualScriptInputFilter() {
- }
- #endif
- //////////////////////////////////////////
- ////////////////TYPE CAST///////////
- //////////////////////////////////////////
- int VisualScriptTypeCast::get_output_sequence_port_count() const {
- return 2;
- }
- bool VisualScriptTypeCast::has_input_sequence_port() const {
- return true;
- }
- int VisualScriptTypeCast::get_input_value_port_count() const {
- return 1;
- }
- int VisualScriptTypeCast::get_output_value_port_count() const {
- return 1;
- }
- String VisualScriptTypeCast::get_output_sequence_port_text(int p_port) const {
- return p_port == 0 ? "yes" : "no";
- }
- PropertyInfo VisualScriptTypeCast::get_input_value_port_info(int p_idx) const {
- return PropertyInfo(Variant::OBJECT, "instance");
- }
- PropertyInfo VisualScriptTypeCast::get_output_value_port_info(int p_idx) const {
- return PropertyInfo(Variant::OBJECT, "");
- }
- String VisualScriptTypeCast::get_caption() const {
- return "TypeCast";
- }
- String VisualScriptTypeCast::get_text() const {
- if (script != String())
- return "Is " + script.get_file() + "?";
- else
- return "Is " + base_type + "?";
- }
- void VisualScriptTypeCast::set_base_type(const StringName &p_type) {
- if (base_type == p_type)
- return;
- base_type = p_type;
- _change_notify();
- ports_changed_notify();
- }
- StringName VisualScriptTypeCast::get_base_type() const {
- return base_type;
- }
- void VisualScriptTypeCast::set_base_script(const String &p_path) {
- if (script == p_path)
- return;
- script = p_path;
- _change_notify();
- ports_changed_notify();
- }
- String VisualScriptTypeCast::get_base_script() const {
- return script;
- }
- VisualScriptTypeCast::TypeGuess VisualScriptTypeCast::guess_output_type(TypeGuess *p_inputs, int p_output) const {
- TypeGuess tg;
- tg.type = Variant::OBJECT;
- if (script != String()) {
- tg.script = ResourceLoader::load(script);
- }
- //if (!tg.script.is_valid()) {
- // tg.gdclass = base_type;
- //}
- return tg;
- }
- class VisualScriptNodeInstanceTypeCast : public VisualScriptNodeInstance {
- public:
- VisualScriptInstance *instance;
- StringName base_type;
- String script;
- //virtual int get_working_memory_size() const { return 0; }
- //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
- //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return false; }
- 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) {
- Object *obj = *p_inputs[0];
- *p_outputs[0] = Variant();
- if (!obj) {
- r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
- r_error_str = "Instance is null";
- return 0;
- }
- if (script != String()) {
- Ref<Script> obj_script = obj->get_script();
- if (!obj_script.is_valid()) {
- return 1; //well, definitely not the script because object we got has no script.
- }
- if (!ResourceCache::has(script)) {
- //if the script is not in use by anyone, we can safely assume whathever we got is not casting to it.
- return 1;
- }
- Ref<Script> cast_script = Ref<Resource>(ResourceCache::get(script));
- if (!cast_script.is_valid()) {
- r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
- r_error_str = "Script path is not a script: " + script;
- return 1;
- }
- while (obj_script.is_valid()) {
- if (cast_script == obj_script) {
- *p_outputs[0] = *p_inputs[0]; //copy
- return 0; // it is the script, yey
- }
- obj_script = obj_script->get_base_script();
- }
- return 1; //not found sorry
- }
- if (ClassDB::is_parent_class(obj->get_class_name(), base_type)) {
- *p_outputs[0] = *p_inputs[0]; //copy
- return 0;
- } else
- return 1;
- }
- };
- VisualScriptNodeInstance *VisualScriptTypeCast::instance(VisualScriptInstance *p_instance) {
- VisualScriptNodeInstanceTypeCast *instance = memnew(VisualScriptNodeInstanceTypeCast);
- instance->instance = p_instance;
- instance->base_type = base_type;
- instance->script = script;
- return instance;
- }
- void VisualScriptTypeCast::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_base_type", "type"), &VisualScriptTypeCast::set_base_type);
- ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptTypeCast::get_base_type);
- ClassDB::bind_method(D_METHOD("set_base_script", "path"), &VisualScriptTypeCast::set_base_script);
- ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptTypeCast::get_base_script);
- List<String> script_extensions;
- for (int i = 0; i > ScriptServer::get_language_count(); i++) {
- ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
- }
- String script_ext_hint;
- for (List<String>::Element *E = script_extensions.front(); E; E = E->next()) {
- if (script_ext_hint != String())
- script_ext_hint += ",";
- script_ext_hint += "*." + E->get();
- }
- ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
- ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
- }
- VisualScriptTypeCast::VisualScriptTypeCast() {
- base_type = "Object";
- }
- void register_visual_script_flow_control_nodes() {
- VisualScriptLanguage::singleton->add_register_func("flow_control/return", create_return_node<false>);
- VisualScriptLanguage::singleton->add_register_func("flow_control/return_with_value", create_return_node<true>);
- VisualScriptLanguage::singleton->add_register_func("flow_control/condition", create_node_generic<VisualScriptCondition>);
- VisualScriptLanguage::singleton->add_register_func("flow_control/while", create_node_generic<VisualScriptWhile>);
- VisualScriptLanguage::singleton->add_register_func("flow_control/iterator", create_node_generic<VisualScriptIterator>);
- VisualScriptLanguage::singleton->add_register_func("flow_control/sequence", create_node_generic<VisualScriptSequence>);
- VisualScriptLanguage::singleton->add_register_func("flow_control/switch", create_node_generic<VisualScriptSwitch>);
- //VisualScriptLanguage::singleton->add_register_func("flow_control/input_filter", create_node_generic<VisualScriptInputFilter>);
- VisualScriptLanguage::singleton->add_register_func("flow_control/type_cast", create_node_generic<VisualScriptTypeCast>);
- }
|