2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* visual_script_flow_control.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
2022-01-13 08:45:09 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-03-05 14:47:28 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2016-08-02 22:11:05 +00:00
|
|
|
#include "visual_script_flow_control.h"
|
2017-03-05 14:47:28 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/io/resource_loader.h"
|
|
|
|
#include "core/os/keyboard.h"
|
|
|
|
#include "core/project_settings.h"
|
2016-08-25 20:45:20 +00:00
|
|
|
|
2016-08-02 22:11:05 +00:00
|
|
|
//////////////////////////////////////////
|
|
|
|
////////////////RETURN////////////////////
|
|
|
|
//////////////////////////////////////////
|
|
|
|
|
|
|
|
int VisualScriptReturn::get_output_sequence_port_count() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool VisualScriptReturn::has_input_sequence_port() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptReturn::get_input_value_port_count() const {
|
|
|
|
return with_value ? 1 : 0;
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptReturn::get_output_value_port_count() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptReturn::get_output_sequence_port_text(int p_port) const {
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptReturn::get_input_value_port_info(int p_idx) const {
|
2016-08-02 22:11:05 +00:00
|
|
|
PropertyInfo pinfo;
|
2017-03-05 15:44:50 +00:00
|
|
|
pinfo.name = "result";
|
|
|
|
pinfo.type = type;
|
2016-08-02 22:11:05 +00:00
|
|
|
return pinfo;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptReturn::get_output_value_port_info(int p_idx) const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return PropertyInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptReturn::get_caption() const {
|
2022-03-11 15:36:16 +00:00
|
|
|
return RTR("Return");
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptReturn::get_text() const {
|
|
|
|
return get_name();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptReturn::set_return_type(Variant::Type p_type) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (type == p_type) {
|
2016-08-02 22:11:05 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
type = p_type;
|
2016-08-04 01:06:39 +00:00
|
|
|
ports_changed_notify();
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Variant::Type VisualScriptReturn::get_return_type() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptReturn::set_enable_return_value(bool p_enable) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (with_value == p_enable) {
|
2016-08-02 22:11:05 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-02 22:11:05 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
with_value = p_enable;
|
2016-08-04 01:06:39 +00:00
|
|
|
ports_changed_notify();
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VisualScriptReturn::is_return_value_enabled() const {
|
|
|
|
return with_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptReturn::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
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);
|
2016-08-02 22:11:05 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String argt = "Any";
|
|
|
|
for (int i = 1; i < Variant::VARIANT_MAX; i++) {
|
|
|
|
argt += "," + Variant::get_type_name(Variant::Type(i));
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2017-07-01 00:30:17 +00:00
|
|
|
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");
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 01:46:45 +00:00
|
|
|
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; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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) {
|
2016-08-06 01:46:45 +00:00
|
|
|
if (with_value) {
|
|
|
|
*p_working_mem = *p_inputs[0];
|
2017-07-22 04:15:55 +00:00
|
|
|
return STEP_EXIT_FUNCTION_BIT;
|
2016-08-06 01:46:45 +00:00
|
|
|
} else {
|
|
|
|
*p_working_mem = Variant();
|
2017-07-22 04:15:55 +00:00
|
|
|
return 0;
|
2016-08-06 01:46:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualScriptNodeInstance *VisualScriptReturn::instance(VisualScriptInstance *p_instance) {
|
|
|
|
VisualScriptNodeInstanceReturn *instance = memnew(VisualScriptNodeInstanceReturn);
|
|
|
|
instance->node = this;
|
|
|
|
instance->instance = p_instance;
|
|
|
|
instance->with_value = with_value;
|
2016-08-06 01:46:45 +00:00
|
|
|
return instance;
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VisualScriptReturn::VisualScriptReturn() {
|
2017-03-05 15:44:50 +00:00
|
|
|
with_value = false;
|
|
|
|
type = Variant::NIL;
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <bool with_value>
|
|
|
|
static Ref<VisualScriptNode> create_return_node(const String &p_name) {
|
2016-08-02 22:11:05 +00:00
|
|
|
Ref<VisualScriptReturn> node;
|
|
|
|
node.instance();
|
|
|
|
node->set_enable_return_value(with_value);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
////////////////CONDITION/////////////////
|
|
|
|
//////////////////////////////////////////
|
|
|
|
|
|
|
|
int VisualScriptCondition::get_output_sequence_port_count() const {
|
2016-08-28 23:57:27 +00:00
|
|
|
return 3;
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool VisualScriptCondition::has_input_sequence_port() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptCondition::get_input_value_port_count() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptCondition::get_output_value_port_count() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptCondition::get_output_sequence_port_text(int p_port) const {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_port == 0) {
|
2016-08-02 22:11:05 +00:00
|
|
|
return "true";
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (p_port == 1) {
|
2016-08-02 22:11:05 +00:00
|
|
|
return "false";
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2016-08-28 23:57:27 +00:00
|
|
|
return "done";
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptCondition::get_input_value_port_info(int p_idx) const {
|
2016-08-02 22:11:05 +00:00
|
|
|
PropertyInfo pinfo;
|
2017-03-05 15:44:50 +00:00
|
|
|
pinfo.name = "cond";
|
|
|
|
pinfo.type = Variant::BOOL;
|
2016-08-02 22:11:05 +00:00
|
|
|
return pinfo;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptCondition::get_output_value_port_info(int p_idx) const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return PropertyInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptCondition::get_caption() const {
|
2022-03-11 15:36:16 +00:00
|
|
|
return RTR("Condition");
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptCondition::get_text() const {
|
2022-03-11 15:36:16 +00:00
|
|
|
return RTR("if (cond) is:");
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptCondition::_bind_methods() {
|
|
|
|
}
|
|
|
|
|
2016-08-06 01:46:45 +00:00
|
|
|
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; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_start_mode == START_MODE_CONTINUE_SEQUENCE) {
|
2016-08-28 23:57:27 +00:00
|
|
|
return 2;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (p_inputs[0]->operator bool()) {
|
2016-08-28 23:57:27 +00:00
|
|
|
return 0 | STEP_FLAG_PUSH_STACK_BIT;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2016-08-28 23:57:27 +00:00
|
|
|
return 1 | STEP_FLAG_PUSH_STACK_BIT;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-06 01:46:45 +00:00
|
|
|
}
|
|
|
|
};
|
2016-08-02 22:11:05 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualScriptNodeInstance *VisualScriptCondition::instance(VisualScriptInstance *p_instance) {
|
|
|
|
VisualScriptNodeInstanceCondition *instance = memnew(VisualScriptNodeInstanceCondition);
|
|
|
|
instance->node = this;
|
|
|
|
instance->instance = p_instance;
|
2016-08-06 01:46:45 +00:00
|
|
|
return instance;
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VisualScriptCondition::VisualScriptCondition() {
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
////////////////WHILE/////////////////
|
|
|
|
//////////////////////////////////////////
|
|
|
|
|
|
|
|
int VisualScriptWhile::get_output_sequence_port_count() const {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool VisualScriptWhile::has_input_sequence_port() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptWhile::get_input_value_port_count() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptWhile::get_output_value_port_count() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptWhile::get_output_sequence_port_text(int p_port) const {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_port == 0) {
|
2016-08-02 22:11:05 +00:00
|
|
|
return "repeat";
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2016-08-02 22:11:05 +00:00
|
|
|
return "exit";
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptWhile::get_input_value_port_info(int p_idx) const {
|
2016-08-02 22:11:05 +00:00
|
|
|
PropertyInfo pinfo;
|
2017-03-05 15:44:50 +00:00
|
|
|
pinfo.name = "cond";
|
|
|
|
pinfo.type = Variant::BOOL;
|
2016-08-02 22:11:05 +00:00
|
|
|
return pinfo;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptWhile::get_output_value_port_info(int p_idx) const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return PropertyInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptWhile::get_caption() const {
|
2022-03-11 15:36:16 +00:00
|
|
|
return RTR("While");
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptWhile::get_text() const {
|
2022-03-11 15:36:16 +00:00
|
|
|
return RTR("while (cond):");
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptWhile::_bind_methods() {
|
|
|
|
}
|
|
|
|
|
2016-08-06 01:46:45 +00:00
|
|
|
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; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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) {
|
2016-08-06 01:46:45 +00:00
|
|
|
bool keep_going = p_inputs[0]->operator bool();
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (keep_going) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return 0 | STEP_FLAG_PUSH_STACK_BIT;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2016-08-06 01:46:45 +00:00
|
|
|
return 1;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-06 01:46:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualScriptNodeInstance *VisualScriptWhile::instance(VisualScriptInstance *p_instance) {
|
|
|
|
VisualScriptNodeInstanceWhile *instance = memnew(VisualScriptNodeInstanceWhile);
|
|
|
|
instance->node = this;
|
|
|
|
instance->instance = p_instance;
|
2016-08-06 01:46:45 +00:00
|
|
|
return instance;
|
|
|
|
}
|
2016-08-02 22:11:05 +00:00
|
|
|
VisualScriptWhile::VisualScriptWhile() {
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
////////////////ITERATOR/////////////////
|
|
|
|
//////////////////////////////////////////
|
|
|
|
|
|
|
|
int VisualScriptIterator::get_output_sequence_port_count() const {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool VisualScriptIterator::has_input_sequence_port() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptIterator::get_input_value_port_count() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptIterator::get_output_value_port_count() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptIterator::get_output_sequence_port_text(int p_port) const {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_port == 0) {
|
2016-08-02 22:11:05 +00:00
|
|
|
return "each";
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2016-08-02 22:11:05 +00:00
|
|
|
return "exit";
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptIterator::get_input_value_port_info(int p_idx) const {
|
2016-08-02 22:11:05 +00:00
|
|
|
PropertyInfo pinfo;
|
2017-03-05 15:44:50 +00:00
|
|
|
pinfo.name = "input";
|
|
|
|
pinfo.type = Variant::NIL;
|
2016-08-02 22:11:05 +00:00
|
|
|
return pinfo;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptIterator::get_output_value_port_info(int p_idx) const {
|
2016-08-02 22:11:05 +00:00
|
|
|
PropertyInfo pinfo;
|
2017-03-05 15:44:50 +00:00
|
|
|
pinfo.name = "elem";
|
|
|
|
pinfo.type = Variant::NIL;
|
2016-08-02 22:11:05 +00:00
|
|
|
return pinfo;
|
|
|
|
}
|
|
|
|
String VisualScriptIterator::get_caption() const {
|
2022-03-11 15:36:16 +00:00
|
|
|
return RTR("Iterator");
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptIterator::get_text() const {
|
2022-03-11 15:36:16 +00:00
|
|
|
return RTR("for (elem) in (input):");
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptIterator::_bind_methods() {
|
|
|
|
}
|
|
|
|
|
2016-08-06 01:46:45 +00:00
|
|
|
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; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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];
|
2016-08-06 01:46:45 +00:00
|
|
|
bool valid;
|
2017-03-05 15:44:50 +00:00
|
|
|
bool can_iter = p_inputs[0]->iter_init(p_working_mem[1], valid);
|
2016-08-06 01:46:45 +00:00
|
|
|
|
|
|
|
if (!valid) {
|
2017-03-05 15:44:50 +00:00
|
|
|
r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
|
2022-06-08 09:42:51 +00:00
|
|
|
r_error_str = RTR("Input type not iterable:") + " " + Variant::get_type_name(p_inputs[0]->get_type());
|
2016-08-06 01:46:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-08-02 22:11:05 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!can_iter) {
|
2016-08-06 01:46:45 +00:00
|
|
|
return 1; //nothing to iterate
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-06 01:46:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
*p_outputs[0] = p_working_mem[0].iter_get(p_working_mem[1], valid);
|
2016-08-06 01:46:45 +00:00
|
|
|
|
|
|
|
if (!valid) {
|
2017-03-05 15:44:50 +00:00
|
|
|
r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
r_error_str = RTR("Iterator became invalid");
|
2016-08-06 01:46:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-07 22:22:33 +00:00
|
|
|
} else { //continue sequence
|
2016-08-06 01:46:45 +00:00
|
|
|
|
|
|
|
bool valid;
|
2017-03-05 15:44:50 +00:00
|
|
|
bool can_iter = p_working_mem[0].iter_next(p_working_mem[1], valid);
|
2016-08-06 01:46:45 +00:00
|
|
|
|
|
|
|
if (!valid) {
|
2017-03-05 15:44:50 +00:00
|
|
|
r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
|
2022-06-08 09:42:51 +00:00
|
|
|
r_error_str = RTR("Iterator became invalid:") + " " + Variant::get_type_name(p_inputs[0]->get_type());
|
2016-08-06 01:46:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!can_iter) {
|
2016-08-06 01:46:45 +00:00
|
|
|
return 1; //nothing to iterate
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-06 01:46:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
*p_outputs[0] = p_working_mem[0].iter_get(p_working_mem[1], valid);
|
2016-08-06 01:46:45 +00:00
|
|
|
|
|
|
|
if (!valid) {
|
2017-03-05 15:44:50 +00:00
|
|
|
r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
r_error_str = RTR("Iterator became invalid");
|
2016-08-06 01:46:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return 0 | STEP_FLAG_PUSH_STACK_BIT; //go around
|
2016-08-06 01:46:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualScriptNodeInstance *VisualScriptIterator::instance(VisualScriptInstance *p_instance) {
|
|
|
|
VisualScriptNodeInstanceIterator *instance = memnew(VisualScriptNodeInstanceIterator);
|
|
|
|
instance->node = this;
|
|
|
|
instance->instance = p_instance;
|
2016-08-06 01:46:45 +00:00
|
|
|
return instance;
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VisualScriptIterator::VisualScriptIterator() {
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////
|
|
|
|
////////////////SEQUENCE/////////////////
|
|
|
|
//////////////////////////////////////////
|
|
|
|
|
|
|
|
int VisualScriptSequence::get_output_sequence_port_count() const {
|
|
|
|
return steps;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool VisualScriptSequence::has_input_sequence_port() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptSequence::get_input_value_port_count() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptSequence::get_output_value_port_count() const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptSequence::get_output_sequence_port_text(int p_port) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
return itos(p_port + 1);
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptSequence::get_input_value_port_info(int p_idx) const {
|
2016-08-02 22:11:05 +00:00
|
|
|
return PropertyInfo();
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptSequence::get_output_value_port_info(int p_idx) const {
|
|
|
|
return PropertyInfo(Variant::INT, "current");
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
String VisualScriptSequence::get_caption() const {
|
2022-03-11 15:36:16 +00:00
|
|
|
return RTR("Sequence");
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptSequence::get_text() const {
|
2022-03-11 15:36:16 +00:00
|
|
|
return RTR("in order:");
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptSequence::set_steps(int p_steps) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(p_steps < 1);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (steps == p_steps) {
|
2016-08-02 22:11:05 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-02 22:11:05 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
steps = p_steps;
|
2016-08-04 01:06:39 +00:00
|
|
|
ports_changed_notify();
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int VisualScriptSequence::get_steps() const {
|
|
|
|
return steps;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptSequence::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_steps", "steps"), &VisualScriptSequence::set_steps);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_steps"), &VisualScriptSequence::get_steps);
|
2016-08-02 22:11:05 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "steps", PROPERTY_HINT_RANGE, "1,64,1"), "set_steps", "get_steps");
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 01:46:45 +00:00
|
|
|
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; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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;
|
2016-08-06 01:46:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int step = p_working_mem[0];
|
2016-08-02 22:11:05 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
*p_outputs[0] = step;
|
2016-08-06 01:46:45 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (step + 1 == steps) {
|
2016-08-06 01:46:45 +00:00
|
|
|
return step;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
p_working_mem[0] = step + 1;
|
|
|
|
return step | STEP_FLAG_PUSH_STACK_BIT;
|
2016-08-06 01:46:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualScriptNodeInstance *VisualScriptSequence::instance(VisualScriptInstance *p_instance) {
|
|
|
|
VisualScriptNodeInstanceSequence *instance = memnew(VisualScriptNodeInstanceSequence);
|
|
|
|
instance->node = this;
|
|
|
|
instance->instance = p_instance;
|
|
|
|
instance->steps = steps;
|
2016-08-06 01:46:45 +00:00
|
|
|
return instance;
|
|
|
|
}
|
2016-08-02 22:11:05 +00:00
|
|
|
VisualScriptSequence::VisualScriptSequence() {
|
2017-03-05 15:44:50 +00:00
|
|
|
steps = 1;
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 22:22:33 +00:00
|
|
|
//////////////////////////////////////////
|
|
|
|
////////////////EVENT TYPE FILTER///////////
|
|
|
|
//////////////////////////////////////////
|
|
|
|
|
2016-08-31 15:49:45 +00:00
|
|
|
int VisualScriptSwitch::get_output_sequence_port_count() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
return case_values.size() + 1;
|
2016-08-07 22:22:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool VisualScriptSwitch::has_input_sequence_port() const {
|
2016-08-07 22:22:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptSwitch::get_input_value_port_count() const {
|
|
|
|
return case_values.size() + 1;
|
2016-08-07 22:22:33 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptSwitch::get_output_value_port_count() const {
|
2016-08-31 15:49:45 +00:00
|
|
|
return 0;
|
2016-08-07 22:22:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 15:49:45 +00:00
|
|
|
String VisualScriptSwitch::get_output_sequence_port_text(int p_port) const {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_port == case_values.size()) {
|
2016-08-31 20:58:51 +00:00
|
|
|
return "done";
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-31 20:58:51 +00:00
|
|
|
|
2016-09-01 23:04:17 +00:00
|
|
|
return String();
|
2016-08-07 22:22:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptSwitch::get_input_value_port_info(int p_idx) const {
|
|
|
|
if (p_idx < case_values.size()) {
|
|
|
|
return PropertyInfo(case_values[p_idx].type, " =");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
return PropertyInfo(Variant::NIL, "input");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-07 22:22:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptSwitch::get_output_value_port_info(int p_idx) const {
|
2016-08-31 15:49:45 +00:00
|
|
|
return PropertyInfo();
|
2016-08-07 22:22:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 15:49:45 +00:00
|
|
|
String VisualScriptSwitch::get_caption() const {
|
2022-03-11 15:36:16 +00:00
|
|
|
return RTR("Switch");
|
2016-08-07 22:22:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 15:49:45 +00:00
|
|
|
String VisualScriptSwitch::get_text() const {
|
2022-03-11 15:36:16 +00:00
|
|
|
return RTR("'input' is:");
|
2016-08-07 22:22:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 15:49:45 +00:00
|
|
|
class VisualScriptNodeInstanceSwitch : public VisualScriptNodeInstance {
|
2016-08-07 22:22:33 +00:00
|
|
|
public:
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualScriptInstance *instance;
|
2016-09-01 23:04:17 +00:00
|
|
|
int case_count;
|
2016-08-07 22:22:33 +00:00
|
|
|
|
|
|
|
//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; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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) {
|
2016-09-01 23:04:17 +00:00
|
|
|
return case_count; //exit
|
2016-08-07 22:22:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < case_count; i++) {
|
|
|
|
if (*p_inputs[i] == *p_inputs[case_count]) {
|
|
|
|
return i | STEP_FLAG_PUSH_STACK_BIT;
|
2016-08-31 15:49:45 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-07 22:22:33 +00:00
|
|
|
|
2016-09-01 23:04:17 +00:00
|
|
|
return case_count;
|
2016-08-07 22:22:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualScriptNodeInstance *VisualScriptSwitch::instance(VisualScriptInstance *p_instance) {
|
|
|
|
VisualScriptNodeInstanceSwitch *instance = memnew(VisualScriptNodeInstanceSwitch);
|
|
|
|
instance->instance = p_instance;
|
|
|
|
instance->case_count = case_values.size();
|
2016-08-07 22:22:33 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool VisualScriptSwitch::_set(const StringName &p_name, const Variant &p_value) {
|
|
|
|
if (String(p_name) == "case_count") {
|
2016-08-31 15:49:45 +00:00
|
|
|
case_values.resize(p_value);
|
|
|
|
_change_notify();
|
|
|
|
ports_changed_notify();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (String(p_name).begins_with("case/")) {
|
2017-03-05 15:44:50 +00:00
|
|
|
int idx = String(p_name).get_slice("/", 1).to_int();
|
|
|
|
ERR_FAIL_INDEX_V(idx, case_values.size(), false);
|
2016-08-31 15:49:45 +00:00
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
case_values.write[idx].type = Variant::Type(int(p_value));
|
2016-09-01 23:04:17 +00:00
|
|
|
_change_notify();
|
|
|
|
ports_changed_notify();
|
2016-08-31 15:49:45 +00:00
|
|
|
|
2016-09-01 23:04:17 +00:00
|
|
|
return true;
|
2016-08-31 15:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool VisualScriptSwitch::_get(const StringName &p_name, Variant &r_ret) const {
|
|
|
|
if (String(p_name) == "case_count") {
|
|
|
|
r_ret = case_values.size();
|
2016-08-31 15:49:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (String(p_name).begins_with("case/")) {
|
2017-03-05 15:44:50 +00:00
|
|
|
int idx = String(p_name).get_slice("/", 1).to_int();
|
|
|
|
ERR_FAIL_INDEX_V(idx, case_values.size(), false);
|
2016-08-31 15:49:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
r_ret = case_values[idx].type;
|
2016-09-01 23:04:17 +00:00
|
|
|
return true;
|
2016-08-31 15:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
void VisualScriptSwitch::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "case_count", PROPERTY_HINT_RANGE, "0,128"));
|
2016-08-07 22:22:33 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String argt = "Any";
|
|
|
|
for (int i = 1; i < Variant::VARIANT_MAX; i++) {
|
|
|
|
argt += "," + Variant::get_type_name(Variant::Type(i));
|
2016-08-31 15:49:45 +00:00
|
|
|
}
|
2016-08-07 22:22:33 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < case_values.size(); i++) {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "case/" + itos(i), PROPERTY_HINT_ENUM, argt));
|
2016-08-31 15:49:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptSwitch::_bind_methods() {
|
2016-08-07 22:22:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 15:49:45 +00:00
|
|
|
VisualScriptSwitch::VisualScriptSwitch() {
|
2016-08-07 22:22:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 20:45:20 +00:00
|
|
|
//////////////////////////////////////////
|
2016-08-28 23:57:27 +00:00
|
|
|
////////////////TYPE CAST///////////
|
2016-08-25 20:45:20 +00:00
|
|
|
//////////////////////////////////////////
|
|
|
|
|
|
|
|
int VisualScriptTypeCast::get_output_sequence_port_count() const {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool VisualScriptTypeCast::has_input_sequence_port() const {
|
2016-08-25 20:45:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptTypeCast::get_input_value_port_count() const {
|
2016-08-25 20:45:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
int VisualScriptTypeCast::get_output_value_port_count() const {
|
2016-08-25 20:45:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptTypeCast::get_output_sequence_port_text(int p_port) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
return p_port == 0 ? "yes" : "no";
|
2016-08-25 20:45:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptTypeCast::get_input_value_port_info(int p_idx) const {
|
|
|
|
return PropertyInfo(Variant::OBJECT, "instance");
|
2016-08-25 20:45:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
PropertyInfo VisualScriptTypeCast::get_output_value_port_info(int p_idx) const {
|
2018-05-13 03:34:35 +00:00
|
|
|
return PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_TYPE_STRING, get_base_type());
|
2016-08-25 20:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptTypeCast::get_caption() const {
|
2022-03-11 15:36:16 +00:00
|
|
|
return RTR("Type Cast");
|
2016-08-25 20:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String VisualScriptTypeCast::get_text() const {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (script != String()) {
|
2022-03-11 15:36:16 +00:00
|
|
|
return vformat(RTR("Is %s?"), script.get_file());
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2022-03-11 15:36:16 +00:00
|
|
|
return vformat(RTR("Is %s?"), base_type);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-25 20:45:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void VisualScriptTypeCast::set_base_type(const StringName &p_type) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (base_type == p_type) {
|
2016-08-25 20:45:20 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-25 20:45:20 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
base_type = p_type;
|
2016-08-25 20:45:20 +00:00
|
|
|
_change_notify();
|
|
|
|
ports_changed_notify();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
StringName VisualScriptTypeCast::get_base_type() const {
|
2016-08-25 20:45:20 +00:00
|
|
|
return base_type;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void VisualScriptTypeCast::set_base_script(const String &p_path) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (script == p_path) {
|
2016-08-25 20:45:20 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-25 20:45:20 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
script = p_path;
|
2016-08-25 20:45:20 +00:00
|
|
|
_change_notify();
|
|
|
|
ports_changed_notify();
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
String VisualScriptTypeCast::get_base_script() const {
|
2016-08-25 20:45:20 +00:00
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2017-07-01 00:30:17 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:45:20 +00:00
|
|
|
class VisualScriptNodeInstanceTypeCast : public VisualScriptNodeInstance {
|
|
|
|
public:
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualScriptInstance *instance;
|
2016-08-25 20:45:20 +00:00
|
|
|
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; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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) {
|
2016-08-25 20:45:20 +00:00
|
|
|
Object *obj = *p_inputs[0];
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
*p_outputs[0] = Variant();
|
2016-08-25 20:45:20 +00:00
|
|
|
|
|
|
|
if (!obj) {
|
2017-03-05 15:44:50 +00:00
|
|
|
r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
r_error_str = "Instance is null";
|
2016-08-25 20:45:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (script != String()) {
|
2016-08-25 20:45:20 +00:00
|
|
|
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)) {
|
2022-02-10 11:00:11 +00:00
|
|
|
//if the script is not in use by anyone, we can safely assume whatever we got is not casting to it.
|
2016-08-25 20:45:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Ref<Script> cast_script = Ref<Resource>(ResourceCache::get(script));
|
|
|
|
if (!cast_script.is_valid()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
r_error_str = "Script path is not a script: " + script;
|
2016-08-25 20:45:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (obj_script.is_valid()) {
|
|
|
|
if (cast_script == obj_script) {
|
|
|
|
*p_outputs[0] = *p_inputs[0]; //copy
|
2016-08-25 20:45:20 +00:00
|
|
|
return 0; // it is the script, yey
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
obj_script = obj_script->get_base_script();
|
2016-08-25 20:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1; //not found sorry
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (ClassDB::is_parent_class(obj->get_class_name(), base_type)) {
|
|
|
|
*p_outputs[0] = *p_inputs[0]; //copy
|
2016-08-25 20:45:20 +00:00
|
|
|
return 0;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2016-08-25 20:45:20 +00:00
|
|
|
return 1;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-08-25 20:45:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualScriptNodeInstance *VisualScriptTypeCast::instance(VisualScriptInstance *p_instance) {
|
|
|
|
VisualScriptNodeInstanceTypeCast *instance = memnew(VisualScriptNodeInstanceTypeCast);
|
|
|
|
instance->instance = p_instance;
|
|
|
|
instance->base_type = base_type;
|
|
|
|
instance->script = script;
|
2016-08-25 20:45:20 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualScriptTypeCast::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
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);
|
2016-08-25 20:45:20 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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);
|
2016-08-25 20:45:20 +00:00
|
|
|
|
|
|
|
List<String> script_extensions;
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i > ScriptServer::get_language_count(); i++) {
|
2016-08-25 20:45:20 +00:00
|
|
|
ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
|
|
|
|
}
|
|
|
|
|
|
|
|
String script_ext_hint;
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<String>::Element *E = script_extensions.front(); E; E = E->next()) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (script_ext_hint != String()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
script_ext_hint += ",";
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
script_ext_hint += "*." + E->get();
|
2016-08-25 20:45:20 +00:00
|
|
|
}
|
|
|
|
|
2017-07-01 00:30:17 +00:00
|
|
|
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");
|
2016-08-25 20:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VisualScriptTypeCast::VisualScriptTypeCast() {
|
2017-03-05 15:44:50 +00:00
|
|
|
base_type = "Object";
|
2016-08-25 20:45:20 +00:00
|
|
|
}
|
2016-08-07 22:22:33 +00:00
|
|
|
|
2016-08-02 22:11:05 +00:00
|
|
|
void register_visual_script_flow_control_nodes() {
|
2017-03-05 15:44:50 +00:00
|
|
|
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>);
|
2017-05-20 15:38:03 +00:00
|
|
|
//VisualScriptLanguage::singleton->add_register_func("flow_control/input_filter", create_node_generic<VisualScriptInputFilter>);
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualScriptLanguage::singleton->add_register_func("flow_control/type_cast", create_node_generic<VisualScriptTypeCast>);
|
2016-08-02 22:11:05 +00:00
|
|
|
}
|