Basic type constants for visual script
This commit is contained in:
parent
65ae4976eb
commit
89702d545b
5 changed files with 200 additions and 3 deletions
|
@ -426,7 +426,7 @@ public:
|
|||
static void get_constructor_list(Variant::Type p_type, List<MethodInfo> *p_list);
|
||||
static void get_numeric_constants_for_type(Variant::Type p_type, List<StringName> *p_constants);
|
||||
static bool has_numeric_constant(Variant::Type p_type, const StringName& p_value);
|
||||
static int get_numeric_constant_value(Variant::Type p_type, const StringName& p_value);
|
||||
static int get_numeric_constant_value(Variant::Type p_type, const StringName& p_value,bool *r_valid=NULL);
|
||||
|
||||
typedef String (*ObjectDeConstruct)(const Variant& p_object,void *ud);
|
||||
typedef void (*ObjectConstruct)(const String& p_text,void *ud,Variant& r_value);
|
||||
|
|
|
@ -1324,14 +1324,22 @@ bool Variant::has_numeric_constant(Variant::Type p_type, const StringName& p_val
|
|||
return cd.value.has(p_value);
|
||||
}
|
||||
|
||||
int Variant::get_numeric_constant_value(Variant::Type p_type, const StringName& p_value) {
|
||||
int Variant::get_numeric_constant_value(Variant::Type p_type, const StringName& p_value, bool *r_valid) {
|
||||
|
||||
if (r_valid)
|
||||
*r_valid=false;
|
||||
|
||||
ERR_FAIL_INDEX_V(p_type,Variant::VARIANT_MAX,0);
|
||||
_VariantCall::ConstantData& cd = _VariantCall::constant_data[p_type];
|
||||
|
||||
|
||||
Map<StringName,int>::Element *E = cd.value.find(p_value);
|
||||
ERR_FAIL_COND_V(!E,0);
|
||||
if (!E) {
|
||||
return -1;
|
||||
}
|
||||
if (r_valid)
|
||||
*r_valid=true;
|
||||
|
||||
return E->get();
|
||||
}
|
||||
|
||||
|
|
|
@ -60,6 +60,7 @@ void register_visual_script_types() {
|
|||
ObjectTypeDB::register_type<VisualScriptGlobalConstant>();
|
||||
ObjectTypeDB::register_type<VisualScriptClassConstant>();
|
||||
ObjectTypeDB::register_type<VisualScriptMathConstant>();
|
||||
ObjectTypeDB::register_type<VisualScriptBasicTypeConstant>();
|
||||
ObjectTypeDB::register_type<VisualScriptEngineSingleton>();
|
||||
ObjectTypeDB::register_type<VisualScriptSceneNode>();
|
||||
ObjectTypeDB::register_type<VisualScriptSceneTree>();
|
||||
|
|
|
@ -1569,6 +1569,151 @@ VisualScriptClassConstant::VisualScriptClassConstant() {
|
|||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////
|
||||
////////////////BASICTYPECONSTANT///////////
|
||||
//////////////////////////////////////////
|
||||
|
||||
int VisualScriptBasicTypeConstant::get_output_sequence_port_count() const {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool VisualScriptBasicTypeConstant::has_input_sequence_port() const{
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int VisualScriptBasicTypeConstant::get_input_value_port_count() const{
|
||||
|
||||
return 0;
|
||||
}
|
||||
int VisualScriptBasicTypeConstant::get_output_value_port_count() const{
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
String VisualScriptBasicTypeConstant::get_output_sequence_port_text(int p_port) const {
|
||||
|
||||
return String();
|
||||
}
|
||||
|
||||
PropertyInfo VisualScriptBasicTypeConstant::get_input_value_port_info(int p_idx) const{
|
||||
|
||||
return PropertyInfo();
|
||||
}
|
||||
|
||||
PropertyInfo VisualScriptBasicTypeConstant::get_output_value_port_info(int p_idx) const{
|
||||
|
||||
return PropertyInfo(Variant::INT,"value");
|
||||
}
|
||||
|
||||
|
||||
String VisualScriptBasicTypeConstant::get_caption() const {
|
||||
|
||||
return "BasicConst";
|
||||
}
|
||||
|
||||
String VisualScriptBasicTypeConstant::get_text() const {
|
||||
|
||||
return Variant::get_type_name(type)+"."+String(name);
|
||||
}
|
||||
|
||||
void VisualScriptBasicTypeConstant::set_basic_type_constant(const StringName& p_which) {
|
||||
|
||||
name=p_which;
|
||||
_change_notify();
|
||||
ports_changed_notify();
|
||||
}
|
||||
|
||||
StringName VisualScriptBasicTypeConstant::get_basic_type_constant() const {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
void VisualScriptBasicTypeConstant::set_basic_type(Variant::Type p_which) {
|
||||
|
||||
type=p_which;
|
||||
_change_notify();
|
||||
ports_changed_notify();
|
||||
}
|
||||
|
||||
Variant::Type VisualScriptBasicTypeConstant::get_basic_type() const {
|
||||
return type;
|
||||
}
|
||||
|
||||
class VisualScriptNodeInstanceBasicTypeConstant : public VisualScriptNodeInstance {
|
||||
public:
|
||||
|
||||
int value;
|
||||
bool valid;
|
||||
//virtual int get_working_memory_size() const { return 0; }
|
||||
|
||||
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 (!valid) {
|
||||
r_error_str="Invalid constant name, pick a valid basic type constant.";
|
||||
r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD;
|
||||
}
|
||||
|
||||
*p_outputs[0] = value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
VisualScriptNodeInstance* VisualScriptBasicTypeConstant::instance(VisualScriptInstance* p_instance) {
|
||||
|
||||
VisualScriptNodeInstanceBasicTypeConstant * instance = memnew(VisualScriptNodeInstanceBasicTypeConstant );
|
||||
instance->value=Variant::get_numeric_constant_value(type,name,&instance->valid);
|
||||
return instance;
|
||||
}
|
||||
|
||||
void VisualScriptBasicTypeConstant::_validate_property(PropertyInfo& property) const {
|
||||
|
||||
if (property.name=="constant") {
|
||||
|
||||
List<StringName> constants;
|
||||
Variant::get_numeric_constants_for_type(type,&constants);
|
||||
|
||||
if (constants.size()==0) {
|
||||
property.usage=0;
|
||||
return;
|
||||
}
|
||||
property.hint_string="";
|
||||
for(List<StringName>::Element *E=constants.front();E;E=E->next()) {
|
||||
if (property.hint_string!=String()) {
|
||||
property.hint_string+=",";
|
||||
}
|
||||
property.hint_string+=String(E->get());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void VisualScriptBasicTypeConstant::_bind_methods() {
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("set_basic_type","name"),&VisualScriptBasicTypeConstant::set_basic_type);
|
||||
ObjectTypeDB::bind_method(_MD("get_basic_type"),&VisualScriptBasicTypeConstant::get_basic_type);
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("set_basic_type_constant","name"),&VisualScriptBasicTypeConstant::set_basic_type_constant);
|
||||
ObjectTypeDB::bind_method(_MD("get_basic_type_constant"),&VisualScriptBasicTypeConstant::get_basic_type_constant);
|
||||
|
||||
|
||||
String argt="Null";
|
||||
for(int i=1;i<Variant::VARIANT_MAX;i++) {
|
||||
argt+=","+Variant::get_type_name(Variant::Type(i));
|
||||
}
|
||||
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT,"basic_type",PROPERTY_HINT_ENUM,argt),_SCS("set_basic_type"),_SCS("get_basic_type"));
|
||||
ADD_PROPERTY(PropertyInfo(Variant::STRING,"constant",PROPERTY_HINT_ENUM,""),_SCS("set_basic_type_constant"),_SCS("get_basic_type_constant"));
|
||||
}
|
||||
|
||||
VisualScriptBasicTypeConstant::VisualScriptBasicTypeConstant() {
|
||||
|
||||
type=Variant::NIL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//////////////////////////////////////////
|
||||
|
@ -3646,6 +3791,7 @@ void register_visual_script_nodes() {
|
|||
VisualScriptLanguage::singleton->add_register_func("constants/math_constant",create_node_generic<VisualScriptMathConstant>);
|
||||
VisualScriptLanguage::singleton->add_register_func("constants/class_constant",create_node_generic<VisualScriptClassConstant>);
|
||||
VisualScriptLanguage::singleton->add_register_func("constants/global_constant",create_node_generic<VisualScriptGlobalConstant>);
|
||||
VisualScriptLanguage::singleton->add_register_func("constants/basic_type_constant",create_node_generic<VisualScriptBasicTypeConstant>);
|
||||
|
||||
VisualScriptLanguage::singleton->add_register_func("custom/custom_node",create_node_generic<VisualScriptCustomNode>);
|
||||
VisualScriptLanguage::singleton->add_register_func("custom/sub_call",create_node_generic<VisualScriptSubCall>);
|
||||
|
|
|
@ -412,6 +412,48 @@ public:
|
|||
VisualScriptClassConstant();
|
||||
};
|
||||
|
||||
class VisualScriptBasicTypeConstant : public VisualScriptNode {
|
||||
|
||||
OBJ_TYPE(VisualScriptBasicTypeConstant,VisualScriptNode)
|
||||
|
||||
Variant::Type type;
|
||||
StringName name;
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
virtual void _validate_property(PropertyInfo& property) const;
|
||||
|
||||
public:
|
||||
|
||||
virtual int get_output_sequence_port_count() const;
|
||||
virtual bool has_input_sequence_port() const;
|
||||
|
||||
|
||||
virtual String get_output_sequence_port_text(int p_port) const;
|
||||
|
||||
|
||||
virtual int get_input_value_port_count() const;
|
||||
virtual int get_output_value_port_count() const;
|
||||
|
||||
|
||||
virtual PropertyInfo get_input_value_port_info(int p_idx) const;
|
||||
virtual PropertyInfo get_output_value_port_info(int p_idx) const;
|
||||
|
||||
virtual String get_caption() const;
|
||||
virtual String get_text() const;
|
||||
virtual String get_category() const { return "constants"; }
|
||||
|
||||
void set_basic_type_constant(const StringName& p_which);
|
||||
StringName get_basic_type_constant() const;
|
||||
|
||||
void set_basic_type(Variant::Type p_which);
|
||||
Variant::Type get_basic_type() const;
|
||||
|
||||
virtual VisualScriptNodeInstance* instance(VisualScriptInstance* p_instance);
|
||||
|
||||
VisualScriptBasicTypeConstant();
|
||||
};
|
||||
|
||||
|
||||
|
||||
class VisualScriptMathConstant : public VisualScriptNode {
|
||||
|
||||
|
|
Loading…
Reference in a new issue