2 * Copyright 2011-2016 Blender Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "graph/node_type.h"
18 #include "util/util_foreach.h"
19 #include "util/util_transform.h"
23 /* Node Socket Type */
25 size_t SocketType::size() const
30 bool SocketType::is_array() const
32 return (type >= BOOLEAN_ARRAY);
35 size_t SocketType::size(Type type)
39 case UNDEFINED: return 0;
41 case BOOLEAN: return sizeof(bool);
42 case FLOAT: return sizeof(float);
43 case INT: return sizeof(int);
44 case UINT: return sizeof(uint);
45 case COLOR: return sizeof(float3);
46 case VECTOR: return sizeof(float3);
47 case POINT: return sizeof(float3);
48 case NORMAL: return sizeof(float3);
49 case POINT2: return sizeof(float2);
50 case CLOSURE: return 0;
51 case STRING: return sizeof(ustring);
52 case ENUM: return sizeof(int);
53 case TRANSFORM: return sizeof(Transform);
54 case NODE: return sizeof(void*);
56 case BOOLEAN_ARRAY: return sizeof(array<bool>);
57 case FLOAT_ARRAY: return sizeof(array<float>);
58 case INT_ARRAY: return sizeof(array<int>);
59 case COLOR_ARRAY: return sizeof(array<float3>);
60 case VECTOR_ARRAY: return sizeof(array<float3>);
61 case POINT_ARRAY: return sizeof(array<float3>);
62 case NORMAL_ARRAY: return sizeof(array<float3>);
63 case POINT2_ARRAY: return sizeof(array<float2>);
64 case STRING_ARRAY: return sizeof(array<ustring>);
65 case TRANSFORM_ARRAY: return sizeof(array<Transform>);
66 case NODE_ARRAY: return sizeof(array<void*>);
73 size_t SocketType::max_size()
75 return sizeof(Transform);
78 void *SocketType::zero_default_value()
80 static Transform zero_transform = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
81 return &zero_transform;
84 ustring SocketType::type_name(Type type)
86 static ustring names[] = {
101 ustring("transform"),
104 ustring("array_boolean"),
105 ustring("array_float"),
106 ustring("array_int"),
107 ustring("array_color"),
108 ustring("array_vector"),
109 ustring("array_point"),
110 ustring("array_normal"),
111 ustring("array_point2"),
112 ustring("array_string"),
113 ustring("array_transform"),
114 ustring("array_node")};
116 return names[(int)type];
119 bool SocketType::is_float3(Type type)
121 return (type == COLOR || type == VECTOR || type == POINT || type == NORMAL);
126 NodeType::NodeType(Type type_)
131 NodeType::~NodeType()
135 void NodeType::register_input(ustring name, ustring ui_name, SocketType::Type type, int struct_offset,
136 const void *default_value, const NodeEnum *enum_values,
137 const NodeType **node_type, int flags, int extra_flags)
141 socket.ui_name = ui_name;
143 socket.struct_offset = struct_offset;
144 socket.default_value = default_value;
145 socket.enum_values = enum_values;
146 socket.node_type = node_type;
147 socket.flags = flags | extra_flags;
148 inputs.push_back(socket);
151 void NodeType::register_output(ustring name, ustring ui_name, SocketType::Type type)
155 socket.ui_name = ui_name;
157 socket.struct_offset = 0;
158 socket.default_value = NULL;
159 socket.enum_values = NULL;
160 socket.node_type = NULL;
161 socket.flags = SocketType::LINKABLE;
162 outputs.push_back(socket);
165 const SocketType *NodeType::find_input(ustring name) const
167 foreach(const SocketType& socket, inputs) {
168 if(socket.name == name) {
176 const SocketType *NodeType::find_output(ustring name) const
178 foreach(const SocketType& socket, outputs) {
179 if(socket.name == name) {
187 /* Node Type Registry */
189 unordered_map<ustring, NodeType, ustringHash>& NodeType::types()
191 static unordered_map<ustring, NodeType, ustringHash> _types;
195 NodeType *NodeType::add(const char *name_, CreateFunc create_, Type type_)
199 if(types().find(name) != types().end()) {
200 fprintf(stderr, "Node type %s registered twice!\n", name_);
205 types()[name] = NodeType(type_);
207 NodeType *type = &types()[name];
209 type->create = create_;
213 const NodeType *NodeType::find(ustring name)
215 unordered_map<ustring, NodeType, ustringHash>::iterator it = types().find(name);
216 return (it == types().end()) ? NULL : &it->second;