Cleanup: improve consistency between function node implementations

This commit is contained in:
Jacques Lucke 2021-10-22 15:34:53 +02:00
parent 0c16ac9ddf
commit 39f88480bb
11 changed files with 88 additions and 97 deletions

View File

@ -34,8 +34,6 @@ static void fn_node_boolean_math_declare(NodeDeclarationBuilder &b)
b.add_output<decl::Bool>("Boolean");
};
} // namespace blender::nodes
static void fn_node_boolean_math_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
@ -59,13 +57,13 @@ static void node_boolean_math_label(bNodeTree *UNUSED(ntree), bNode *node, char
BLI_strncpy(label, IFACE_(name), maxlen);
}
static const blender::fn::MultiFunction *get_multi_function(bNode &bnode)
static const fn::MultiFunction *get_multi_function(bNode &bnode)
{
static blender::fn::CustomMF_SI_SI_SO<bool, bool, bool> and_fn{
"And", [](bool a, bool b) { return a && b; }};
static blender::fn::CustomMF_SI_SI_SO<bool, bool, bool> or_fn{
"Or", [](bool a, bool b) { return a || b; }};
static blender::fn::CustomMF_SI_SO<bool, bool> not_fn{"Not", [](bool a) { return !a; }};
static fn::CustomMF_SI_SI_SO<bool, bool, bool> and_fn{"And",
[](bool a, bool b) { return a && b; }};
static fn::CustomMF_SI_SI_SO<bool, bool, bool> or_fn{"Or",
[](bool a, bool b) { return a || b; }};
static fn::CustomMF_SI_SO<bool, bool> not_fn{"Not", [](bool a) { return !a; }};
switch (bnode.custom1) {
case NODE_BOOLEAN_MATH_AND:
@ -80,22 +78,23 @@ static const blender::fn::MultiFunction *get_multi_function(bNode &bnode)
return nullptr;
}
static void fn_node_boolean_math_build_multi_function(
blender::nodes::NodeMultiFunctionBuilder &builder)
static void fn_node_boolean_math_build_multi_function(NodeMultiFunctionBuilder &builder)
{
const blender::fn::MultiFunction *fn = get_multi_function(builder.node());
const fn::MultiFunction *fn = get_multi_function(builder.node());
builder.set_matching_fn(fn);
}
} // namespace blender::nodes
void register_node_type_fn_boolean_math()
{
static bNodeType ntype;
fn_node_type_base(&ntype, FN_NODE_BOOLEAN_MATH, "Boolean Math", NODE_CLASS_CONVERTER, 0);
ntype.declare = blender::nodes::fn_node_boolean_math_declare;
node_type_label(&ntype, node_boolean_math_label);
node_type_update(&ntype, node_boolean_math_update);
ntype.build_multi_function = fn_node_boolean_math_build_multi_function;
ntype.draw_buttons = fn_node_boolean_math_layout;
node_type_label(&ntype, blender::nodes::node_boolean_math_label);
node_type_update(&ntype, blender::nodes::node_boolean_math_update);
ntype.build_multi_function = blender::nodes::fn_node_boolean_math_build_multi_function;
ntype.draw_buttons = blender::nodes::fn_node_boolean_math_layout;
nodeRegisterType(&ntype);
}

View File

@ -37,8 +37,6 @@ static void fn_node_float_compare_declare(NodeDeclarationBuilder &b)
b.add_output<decl::Bool>("Result");
};
} // namespace blender::nodes
static void geo_node_float_compare_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
@ -65,19 +63,19 @@ static void node_float_compare_label(bNodeTree *UNUSED(ntree),
BLI_strncpy(label, IFACE_(name), maxlen);
}
static const blender::fn::MultiFunction *get_multi_function(bNode &node)
static const fn::MultiFunction *get_multi_function(bNode &node)
{
static blender::fn::CustomMF_SI_SI_SO<float, float, bool> less_than_fn{
static fn::CustomMF_SI_SI_SO<float, float, bool> less_than_fn{
"Less Than", [](float a, float b) { return a < b; }};
static blender::fn::CustomMF_SI_SI_SO<float, float, bool> less_equal_fn{
static fn::CustomMF_SI_SI_SO<float, float, bool> less_equal_fn{
"Less Equal", [](float a, float b) { return a <= b; }};
static blender::fn::CustomMF_SI_SI_SO<float, float, bool> greater_than_fn{
static fn::CustomMF_SI_SI_SO<float, float, bool> greater_than_fn{
"Greater Than", [](float a, float b) { return a > b; }};
static blender::fn::CustomMF_SI_SI_SO<float, float, bool> greater_equal_fn{
static fn::CustomMF_SI_SI_SO<float, float, bool> greater_equal_fn{
"Greater Equal", [](float a, float b) { return a >= b; }};
static blender::fn::CustomMF_SI_SI_SI_SO<float, float, float, bool> equal_fn{
static fn::CustomMF_SI_SI_SI_SO<float, float, float, bool> equal_fn{
"Equal", [](float a, float b, float epsilon) { return std::abs(a - b) <= epsilon; }};
static blender::fn::CustomMF_SI_SI_SI_SO<float, float, float, bool> not_equal_fn{
static fn::CustomMF_SI_SI_SI_SO<float, float, float, bool> not_equal_fn{
"Not Equal", [](float a, float b, float epsilon) { return std::abs(a - b) > epsilon; }};
switch (node.custom1) {
@ -99,22 +97,23 @@ static const blender::fn::MultiFunction *get_multi_function(bNode &node)
return nullptr;
}
static void fn_node_float_compare_build_multi_function(
blender::nodes::NodeMultiFunctionBuilder &builder)
static void fn_node_float_compare_build_multi_function(NodeMultiFunctionBuilder &builder)
{
const blender::fn::MultiFunction *fn = get_multi_function(builder.node());
const fn::MultiFunction *fn = get_multi_function(builder.node());
builder.set_matching_fn(fn);
}
} // namespace blender::nodes
void register_node_type_fn_float_compare()
{
static bNodeType ntype;
fn_node_type_base(&ntype, FN_NODE_COMPARE_FLOATS, "Compare Floats", NODE_CLASS_CONVERTER, 0);
ntype.declare = blender::nodes::fn_node_float_compare_declare;
node_type_label(&ntype, node_float_compare_label);
node_type_update(&ntype, node_float_compare_update);
ntype.build_multi_function = fn_node_float_compare_build_multi_function;
ntype.draw_buttons = geo_node_float_compare_layout;
node_type_label(&ntype, blender::nodes::node_float_compare_label);
node_type_update(&ntype, blender::nodes::node_float_compare_update);
ntype.build_multi_function = blender::nodes::fn_node_float_compare_build_multi_function;
ntype.draw_buttons = blender::nodes::geo_node_float_compare_layout;
nodeRegisterType(&ntype);
}

View File

@ -34,8 +34,6 @@ static void fn_node_float_to_int_declare(NodeDeclarationBuilder &b)
b.add_output<decl::Int>("Integer");
};
} // namespace blender::nodes
static void fn_node_float_to_int_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
uiItemR(layout, ptr, "rounding_mode", 0, "", ICON_NONE);
@ -51,16 +49,13 @@ static void node_float_to_int_label(bNodeTree *UNUSED(ntree), bNode *node, char
BLI_strncpy(label, IFACE_(name), maxlen);
}
static const blender::fn::MultiFunction *get_multi_function(bNode &bnode)
static const fn::MultiFunction *get_multi_function(bNode &bnode)
{
static blender::fn::CustomMF_SI_SO<float, int> round_fn{"Round",
[](float a) { return (int)round(a); }};
static blender::fn::CustomMF_SI_SO<float, int> floor_fn{"Floor",
[](float a) { return (int)floor(a); }};
static blender::fn::CustomMF_SI_SO<float, int> ceil_fn{"Ceiling",
[](float a) { return (int)ceil(a); }};
static blender::fn::CustomMF_SI_SO<float, int> trunc_fn{"Truncate",
[](float a) { return (int)trunc(a); }};
static fn::CustomMF_SI_SO<float, int> round_fn{"Round", [](float a) { return (int)round(a); }};
static fn::CustomMF_SI_SO<float, int> floor_fn{"Floor", [](float a) { return (int)floor(a); }};
static fn::CustomMF_SI_SO<float, int> ceil_fn{"Ceiling", [](float a) { return (int)ceil(a); }};
static fn::CustomMF_SI_SO<float, int> trunc_fn{"Truncate",
[](float a) { return (int)trunc(a); }};
switch (static_cast<FloatToIntRoundingMode>(bnode.custom1)) {
case FN_NODE_FLOAT_TO_INT_ROUND:
@ -77,21 +72,22 @@ static const blender::fn::MultiFunction *get_multi_function(bNode &bnode)
return nullptr;
}
static void fn_node_float_to_int_build_multi_function(
blender::nodes::NodeMultiFunctionBuilder &builder)
static void fn_node_float_to_int_build_multi_function(NodeMultiFunctionBuilder &builder)
{
const blender::fn::MultiFunction *fn = get_multi_function(builder.node());
const fn::MultiFunction *fn = get_multi_function(builder.node());
builder.set_matching_fn(fn);
}
} // namespace blender::nodes
void register_node_type_fn_float_to_int()
{
static bNodeType ntype;
fn_node_type_base(&ntype, FN_NODE_FLOAT_TO_INT, "Float to Integer", NODE_CLASS_CONVERTER, 0);
ntype.declare = blender::nodes::fn_node_float_to_int_declare;
node_type_label(&ntype, node_float_to_int_label);
ntype.build_multi_function = fn_node_float_to_int_build_multi_function;
ntype.draw_buttons = fn_node_float_to_int_layout;
node_type_label(&ntype, blender::nodes::node_float_to_int_label);
ntype.build_multi_function = blender::nodes::fn_node_float_to_int_build_multi_function;
ntype.draw_buttons = blender::nodes::fn_node_float_to_int_layout;
nodeRegisterType(&ntype);
}

View File

@ -32,7 +32,7 @@ static void fn_node_input_color_layout(uiLayout *layout, bContext *UNUSED(C), Po
uiItemR(layout, ptr, "color", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
}
static void fn_node_color_input_build_multi_function(
static void fn_node_input_color_build_multi_function(
blender::nodes::NodeMultiFunctionBuilder &builder)
{
bNode &bnode = builder.node();
@ -59,7 +59,7 @@ void register_node_type_fn_input_color()
node_type_init(&ntype, blender::nodes::fn_node_input_color_init);
node_type_storage(
&ntype, "NodeInputColor", node_free_standard_storage, node_copy_standard_storage);
ntype.build_multi_function = blender::nodes::fn_node_color_input_build_multi_function;
ntype.build_multi_function = blender::nodes::fn_node_input_color_build_multi_function;
ntype.draw_buttons = blender::nodes::fn_node_input_color_layout;
nodeRegisterType(&ntype);
}

View File

@ -27,21 +27,17 @@ static void fn_node_input_string_declare(NodeDeclarationBuilder &b)
b.add_output<decl::String>("String");
};
} // namespace blender::nodes
static void fn_node_input_string_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
uiItemR(layout, ptr, "string", 0, "", ICON_NONE);
}
static void fn_node_input_string_build_multi_function(
blender::nodes::NodeMultiFunctionBuilder &builder)
static void fn_node_input_string_build_multi_function(NodeMultiFunctionBuilder &builder)
{
bNode &bnode = builder.node();
NodeInputString *node_storage = static_cast<NodeInputString *>(bnode.storage);
std::string string = std::string((node_storage->string) ? node_storage->string : "");
builder.construct_and_set_matching_fn<blender::fn::CustomMF_Constant<std::string>>(
std::move(string));
builder.construct_and_set_matching_fn<fn::CustomMF_Constant<std::string>>(std::move(string));
}
static void fn_node_input_string_init(bNodeTree *UNUSED(ntree), bNode *node)
@ -75,15 +71,20 @@ static void fn_node_string_copy(bNodeTree *UNUSED(dest_ntree),
dest_node->storage = destination_storage;
}
} // namespace blender::nodes
void register_node_type_fn_input_string()
{
static bNodeType ntype;
fn_node_type_base(&ntype, FN_NODE_INPUT_STRING, "String", NODE_CLASS_INPUT, 0);
ntype.declare = blender::nodes::fn_node_input_string_declare;
node_type_init(&ntype, fn_node_input_string_init);
node_type_storage(&ntype, "NodeInputString", fn_node_input_string_free, fn_node_string_copy);
ntype.build_multi_function = fn_node_input_string_build_multi_function;
ntype.draw_buttons = fn_node_input_string_layout;
node_type_init(&ntype, blender::nodes::fn_node_input_string_init);
node_type_storage(&ntype,
"NodeInputString",
blender::nodes::fn_node_input_string_free,
blender::nodes::fn_node_string_copy);
ntype.build_multi_function = blender::nodes::fn_node_input_string_build_multi_function;
ntype.draw_buttons = blender::nodes::fn_node_input_string_layout;
nodeRegisterType(&ntype);
}

View File

@ -28,22 +28,20 @@ static void fn_node_input_vector_declare(NodeDeclarationBuilder &b)
b.add_output<decl::Vector>("Vector");
};
} // namespace blender::nodes
static void fn_node_input_vector_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
uiLayout *col = uiLayoutColumn(layout, true);
uiItemR(col, ptr, "vector", UI_ITEM_R_EXPAND, "", ICON_NONE);
}
static void fn_node_vector_input_build_multi_function(
blender::nodes::NodeMultiFunctionBuilder &builder)
static void fn_node_input_vector_build_multi_function(NodeMultiFunctionBuilder &builder)
{
bNode &bnode = builder.node();
NodeInputVector *node_storage = static_cast<NodeInputVector *>(bnode.storage);
blender::float3 vector(node_storage->vector);
builder.construct_and_set_matching_fn<blender::fn::CustomMF_Constant<blender::float3>>(vector);
float3 vector(node_storage->vector);
builder.construct_and_set_matching_fn<fn::CustomMF_Constant<float3>>(vector);
}
static void fn_node_input_vector_init(bNodeTree *UNUSED(ntree), bNode *node)
{
NodeInputVector *data = (NodeInputVector *)MEM_callocN(sizeof(NodeInputVector),
@ -51,16 +49,18 @@ static void fn_node_input_vector_init(bNodeTree *UNUSED(ntree), bNode *node)
node->storage = data;
}
} // namespace blender::nodes
void register_node_type_fn_input_vector()
{
static bNodeType ntype;
fn_node_type_base(&ntype, FN_NODE_INPUT_VECTOR, "Vector", 0, 0);
ntype.declare = blender::nodes::fn_node_input_vector_declare;
node_type_init(&ntype, fn_node_input_vector_init);
node_type_init(&ntype, blender::nodes::fn_node_input_vector_init);
node_type_storage(
&ntype, "NodeInputVector", node_free_standard_storage, node_copy_standard_storage);
ntype.build_multi_function = fn_node_vector_input_build_multi_function;
ntype.draw_buttons = fn_node_input_vector_layout;
ntype.build_multi_function = blender::nodes::fn_node_input_vector_build_multi_function;
ntype.draw_buttons = blender::nodes::fn_node_input_vector_layout;
nodeRegisterType(&ntype);
}

View File

@ -28,8 +28,6 @@ static void fn_node_replace_string_declare(NodeDeclarationBuilder &b)
b.add_output<decl::String>("String");
};
} // namespace blender::nodes
static std::string replace_all(std::string str, const std::string &from, const std::string &to)
{
if (from.length() <= 0) {
@ -45,23 +43,23 @@ static std::string replace_all(std::string str, const std::string &from, const s
return str;
}
static void fn_node_replace_string_build_multi_function(
blender::nodes::NodeMultiFunctionBuilder &builder)
static void fn_node_replace_string_build_multi_function(NodeMultiFunctionBuilder &builder)
{
static blender::fn::CustomMF_SI_SI_SI_SO<std::string, std::string, std::string, std::string>
substring_fn{"Replace",
[](const std::string &str,
const std::string &find,
const std::string &replace) { return replace_all(str, find, replace); }};
static fn::CustomMF_SI_SI_SI_SO<std::string, std::string, std::string, std::string> substring_fn{
"Replace", [](const std::string &str, const std::string &find, const std::string &replace) {
return replace_all(str, find, replace);
}};
builder.set_matching_fn(&substring_fn);
}
} // namespace blender::nodes
void register_node_type_fn_replace_string()
{
static bNodeType ntype;
fn_node_type_base(&ntype, FN_NODE_REPLACE_STRING, "Replace String", NODE_CLASS_CONVERTER, 0);
ntype.declare = blender::nodes::fn_node_replace_string_declare;
ntype.build_multi_function = fn_node_replace_string_build_multi_function;
ntype.build_multi_function = blender::nodes::fn_node_replace_string_build_multi_function;
nodeRegisterType(&ntype);
}

View File

@ -25,6 +25,7 @@
#include "node_function_util.hh"
namespace blender::nodes {
static void fn_node_rotate_euler_declare(NodeDeclarationBuilder &b)
{
b.is_function_node();

View File

@ -28,22 +28,21 @@ static void fn_node_string_length_declare(NodeDeclarationBuilder &b)
b.add_output<decl::Int>("Length");
};
} // namespace blender::nodes
static void fn_node_string_length_build_multi_function(
blender::nodes::NodeMultiFunctionBuilder &builder)
static void fn_node_string_length_build_multi_function(NodeMultiFunctionBuilder &builder)
{
static blender::fn::CustomMF_SI_SO<std::string, int> str_len_fn{
static fn::CustomMF_SI_SO<std::string, int> str_len_fn{
"String Length", [](const std::string &a) { return BLI_strlen_utf8(a.c_str()); }};
builder.set_matching_fn(&str_len_fn);
}
} // namespace blender::nodes
void register_node_type_fn_string_length()
{
static bNodeType ntype;
fn_node_type_base(&ntype, FN_NODE_STRING_LENGTH, "String Length", NODE_CLASS_CONVERTER, 0);
ntype.declare = blender::nodes::fn_node_string_length_declare;
ntype.build_multi_function = fn_node_string_length_build_multi_function;
ntype.build_multi_function = blender::nodes::fn_node_string_length_build_multi_function;
nodeRegisterType(&ntype);
}

View File

@ -28,12 +28,9 @@ static void fn_node_string_substring_declare(NodeDeclarationBuilder &b)
b.add_output<decl::String>("String");
};
} // namespace blender::nodes
static void fn_node_string_substring_build_multi_function(
blender::nodes::NodeMultiFunctionBuilder &builder)
static void fn_node_string_substring_build_multi_function(NodeMultiFunctionBuilder &builder)
{
static blender::fn::CustomMF_SI_SI_SI_SO<std::string, int, int, std::string> substring_fn{
static fn::CustomMF_SI_SI_SI_SO<std::string, int, int, std::string> substring_fn{
"Substring", [](const std::string &str, int a, int b) {
const int len = BLI_strlen_utf8(str.c_str());
const int start = BLI_str_utf8_offset_from_index(str.c_str(), std::clamp(a, 0, len));
@ -43,12 +40,14 @@ static void fn_node_string_substring_build_multi_function(
builder.set_matching_fn(&substring_fn);
}
} // namespace blender::nodes
void register_node_type_fn_string_substring()
{
static bNodeType ntype;
fn_node_type_base(&ntype, FN_NODE_STRING_SUBSTRING, "String Substring", NODE_CLASS_CONVERTER, 0);
ntype.declare = blender::nodes::fn_node_string_substring_declare;
ntype.build_multi_function = fn_node_string_substring_build_multi_function;
ntype.build_multi_function = blender::nodes::fn_node_string_substring_build_multi_function;
nodeRegisterType(&ntype);
}

View File

@ -26,12 +26,9 @@ static void fn_node_value_to_string_declare(NodeDeclarationBuilder &b)
b.add_output<decl::String>("String");
};
} // namespace blender::nodes
static void fn_node_value_to_string_build_multi_function(
blender::nodes::NodeMultiFunctionBuilder &builder)
static void fn_node_value_to_string_build_multi_function(NodeMultiFunctionBuilder &builder)
{
static blender::fn::CustomMF_SI_SI_SO<float, int, std::string> to_str_fn{
static fn::CustomMF_SI_SI_SO<float, int, std::string> to_str_fn{
"Value To String", [](float a, int b) {
std::stringstream stream;
stream << std::fixed << std::setprecision(std::max(0, b)) << a;
@ -40,12 +37,14 @@ static void fn_node_value_to_string_build_multi_function(
builder.set_matching_fn(&to_str_fn);
}
} // namespace blender::nodes
void register_node_type_fn_value_to_string()
{
static bNodeType ntype;
fn_node_type_base(&ntype, FN_NODE_VALUE_TO_STRING, "Value to String", NODE_CLASS_CONVERTER, 0);
ntype.declare = blender::nodes::fn_node_value_to_string_declare;
ntype.build_multi_function = fn_node_value_to_string_build_multi_function;
ntype.build_multi_function = blender::nodes::fn_node_value_to_string_build_multi_function;
nodeRegisterType(&ntype);
}