-
Notifications
You must be signed in to change notification settings - Fork 60
/
NodeOptions.h
163 lines (139 loc) · 5.15 KB
/
NodeOptions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef __NodeOptions_h__
#define __NodeOptions_h__
#include <array>
#include <maya/MFnDagNode.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MPxNode.h>
template <int Size>
class NodeOptionDefinition
{
public:
template <int Index, typename Type>
class Option;
public:
void addAttributes()
{
for (auto &attribute : myAttributes)
MPxNode::addAttribute(attribute);
}
std::array<MObject, Size> myAttributes;
};
template <int Size>
template <int Index>
class NodeOptionDefinition<Size>::Option<Index, bool>
{
public:
Option(NodeOptionDefinition<Size> *base,
const char *name,
bool defaultValue)
{
MObject &attribute = base->myAttributes[Index];
MFnNumericAttribute nAttr;
attribute = nAttr.create(
name, name, MFnNumericData::kBoolean, defaultValue);
nAttr.setInternal(true);
}
};
template <typename T>
struct DataSourceTrait
{
typedef T DataSource;
};
template <>
struct DataSourceTrait<MFnDagNode>
{
typedef MFnDependencyNode DataSource;
};
template <typename Definition, typename DataSource>
class NodeOptionAccessor
{
private:
template <typename, int, typename>
class OptionImpl;
public:
template <int Index, typename Type>
class Option
{
public:
// gcc 4.8 seems to need this to compile
Option() {}
Option(NodeOptionAccessor<Definition, DataSource> *base,
const char *name,
Type defaultValue)
: myBase(base)
{
}
Type operator()() const
{
return OptionImpl<DataSource, Index, Type>()(myBase);
}
private:
NodeOptionAccessor<Definition, DataSource> *myBase;
};
public:
NodeOptionAccessor(const Definition &definition, DataSource &dataSource)
: myDefinition(definition), myDataSource(dataSource)
{
}
private:
const Definition &myDefinition;
DataSource &myDataSource;
};
template <typename Definition, typename DataSource>
template <int Index>
class NodeOptionAccessor<Definition,
DataSource>::OptionImpl<MDataBlock, Index, bool>
{
public:
bool operator()(NodeOptionAccessor<Definition, MDataBlock> *base) const
{
const MObject &attribute = base->myDefinition.myAttributes[Index];
return base->myDataSource.inputValue(attribute).asBool();
}
};
template <typename Definition, typename DataSource>
template <int Index>
class NodeOptionAccessor<Definition,
DataSource>::OptionImpl<MFnDependencyNode, Index, bool>
{
public:
bool operator()(
NodeOptionAccessor<Definition, MFnDependencyNode> *base) const
{
const MObject &attribute = base->myDefinition.myAttributes[Index];
return base->myDataSource.findPlug(attribute, true).asBool();
}
};
#define NODE_OPTIONS_BEGIN(NAME) \
namespace NAME \
{ \
const int CounterStart = __COUNTER__ + 1; \
\
template <typename Base> \
class Interface : public Base \
{ \
private: \
template <int Index, typename Type> \
using Option = typename Base::template Option<Index, Type>; \
\
public: \
using Base::Base;
#define NODE_OPTION(NAME, TYPE, DEFAULT_VALUE) \
Option<__COUNTER__ - CounterStart, TYPE> NAME = { \
this, #NAME, (DEFAULT_VALUE)};
#define NODE_OPTIONS_END() \
} \
; \
\
typedef Interface<NodeOptionDefinition<__COUNTER__ - CounterStart>> \
Definition; \
\
template <typename DataSource> \
using Accessor = Interface<NodeOptionAccessor<Definition, DataSource>>; \
typedef Interface<NodeOptionAccessor<Definition, MDataBlock>> \
AccessorDataBlock; \
typedef Interface<NodeOptionAccessor<Definition, MFnDependencyNode>> \
AccessorFn; \
}
#endif