Blender V5.0
node_fn_float_to_int.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <cmath>
6
7#include "BLI_string_utf8.h"
8
9#include "RNA_enum_types.hh"
10
12#include "UI_resources.hh"
13
14#include "node_function_util.hh"
15
17
19{
20 b.is_function_node();
21 b.add_input<decl::Float>("Float");
22 b.add_output<decl::Int>("Integer");
23}
24
25static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
26{
27 layout->prop(ptr, "rounding_mode", UI_ITEM_NONE, "", ICON_NONE);
28}
29
30static void node_label(const bNodeTree * /*tree*/,
31 const bNode *node,
32 char *label,
33 int label_maxncpy)
34{
35 const char *name;
37 if (!enum_label) {
39 }
41}
42
43static const mf::MultiFunction *get_multi_function(const bNode &bnode)
44{
45 static auto exec_preset = mf::build::exec_presets::AllSpanOrSingle();
46 static auto round_fn = mf::build::SI1_SO<float, int>(
47 "Round", [](float a) { return int(round(a)); }, exec_preset);
48 static auto floor_fn = mf::build::SI1_SO<float, int>(
49 "Floor", [](float a) { return int(floor(a)); }, exec_preset);
50 static auto ceil_fn = mf::build::SI1_SO<float, int>(
51 "Ceiling", [](float a) { return int(ceil(a)); }, exec_preset);
52 static auto trunc_fn = mf::build::SI1_SO<float, int>(
53 "Truncate", [](float a) { return int(trunc(a)); }, exec_preset);
54
55 switch (static_cast<FloatToIntRoundingMode>(bnode.custom1)) {
57 return &round_fn;
59 return &floor_fn;
61 return &ceil_fn;
63 return &trunc_fn;
64 }
65
67 return nullptr;
68}
69
71{
72 const mf::MultiFunction *fn = get_multi_function(builder.node());
73 builder.set_matching_fn(fn);
74}
75
76static void node_register()
77{
78 static blender::bke::bNodeType ntype;
79
80 fn_node_type_base(&ntype, "FunctionNodeFloatToInt", FN_NODE_FLOAT_TO_INT);
81 ntype.ui_name = "Float to Integer";
82 ntype.ui_description =
83 "Convert the given floating-point number to an integer, with a choice of methods";
84 ntype.enum_name_legacy = "FLOAT_TO_INT";
86 ntype.declare = node_declare;
87 ntype.labelfunc = node_label;
91}
93
94} // namespace blender::nodes::node_fn_float_to_int_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define FN_NODE_FLOAT_TO_INT
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
#define CTX_N_(context, msgid)
#define BLT_I18NCONTEXT_ID_NODETREE
#define CTX_IFACE_(context, msgid)
FloatToIntRoundingMode
@ FN_NODE_FLOAT_TO_INT_TRUNCATE
@ FN_NODE_FLOAT_TO_INT_CEIL
@ FN_NODE_FLOAT_TO_INT_ROUND
@ FN_NODE_FLOAT_TO_INT_FLOOR
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define UI_ITEM_NONE
void set_matching_fn(const mf::MultiFunction *fn)
#define round
#define trunc
#define floor
#define ceil
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
static void node_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static const mf::MultiFunction * get_multi_function(const bNode &bnode)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
void fn_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
const char * name
bool RNA_enum_name(const EnumPropertyItem *item, const int value, const char **r_name)
const EnumPropertyItem rna_enum_node_float_to_int_items[]
int16_t custom1
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* labelfunc)(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:270
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4238