LightWrap++
AC++wrapperfortheLightWave3DSDK
node_handler.h
Go to the documentation of this file.
1 #ifndef LWPP_NODE_HANDLER_H
2 #define LWPP_NODE_HANDLER_H
3 
4 #include "lwpp/plugin_handler.h"
5 #include "lwpp/nodes.h"
6 
7 namespace lwpp
8 {
10  class NodeHandler : public InstanceHandler, public virtual ItemHandler, public virtual RenderHandler, public LWNode
11  {
12  protected:
13  NodeID Context;
14  virtual int NodeInputEvent ( NodeInputID nid, LWNodalEvent nevent, ConnectionType type)
15  {
16  UNUSED(type);
17  UNUSED(nid);
18  if (nevent == NIE_CONNECT) Update();
19  if (nevent == NIE_INPUTNODEDESTROY) Update();
20  return 1;
21  }
22 
23  static int CB_NodeInputEvent ( void *userData, NodeInputID nid, LWNodalEvent nevent, ConnectionType type)
24  {
25  NodeHandler *plugin = static_cast<NodeHandler *>(userData);
26  return plugin->NodeInputEvent ( nid, nevent, type);
27  }
28 
29  public:
34 
35  LWNodeInput *addColorInput(const std::string name = "Color")
36  {
38  }
39  LWNodeInput *addColourInput(const std::string name = "Colour")
40  {
41  return addColorInput(name);
42  }
43  LWNodeInput *addScalarInput(const std::string name = "Scalar")
44  {
46  }
47  LWNodeInput *addVectorInput(const std::string name = "Vector")
48  {
50  }
51  LWNodeInput *addIntegerInput(const std::string name = "Integer")
52  {
54  }
55  LWNodeInput *addFunctionInput(const std::string name = "Function")
56  {
58  }
59  LWNodeInput *addMaterialInput(const std::string name = "Material")
60  {
62  }
64 
69 
70  auto_NodeInput autoColorInput(const std::string name = "Color")
71  {
73  }
74  auto_NodeInput autoColourInput(const std::string name = "Colour")
75  {
76  return auto_NodeInput(addColorInput(name));
77  }
78  auto_NodeInput autoScalarInput(const std::string name = "Scalar")
79  {
81  }
82  auto_NodeInput autoVectorInput(const std::string name = "Vector")
83  {
85  }
86  auto_NodeInput autoIntegerInput(const std::string name = "Integer")
87  {
89  }
90  auto_NodeInput autoFunctionInput(const std::string name = "Function")
91  {
93  }
94  auto_NodeInput autoMaterialInput(const std::string name = "Material")
95  {
97  }
99 
100  NodeHandler(void *priv, void *context, LWError *err) : InstanceHandler(priv, context, err, LWNODE_HCLASS)
101  {
102  Context = static_cast<NodeID>(context);
104  }
105  virtual ~NodeHandler() {;}
106  virtual void Evaluate(LWNodalAccess* na, NodeOutputID outID, NodeValue value)
107  {
108  UNUSED(na);
109  UNUSED(outID);
110  UNUSED(value);
111  }
112  virtual void CustomPreview(int width, int height )
113  {
114  UNUSED(width);
115  UNUSED(height);
116  }
117  virtual unsigned int Flags()
118  {
119  return 0;
120  }
121  };
122 
123  namespace LWSDK95_1
124  {
125  // Node flags. Returned by LWNodeHandler->flags
126 #define NF_TRANSP (1<<0) // This flag should be set if a material node might have transparency
127 
128  // Node handler activation.
129  typedef struct st_LWNodeHandler {
130  LWInstanceFuncs *inst;
131  LWItemFuncs *item;
132  LWRenderFuncs *rend;
133  void (*evaluate)( LWInstance, LWNodalAccess*, NodeOutputID, NodeValue );
134  // Evaluation function receives the LWNodalAccess structure.
135  // NodeOutputID is the output belonging to this node, and which is being currently asked the value from.
136  // NodeValue is the value you need to set with the output functions when setting a value for this evaluation.
137 
138  void (*customPreview)( LWInstance, int width, int height );
139  // customPreview is called when the node has NPT_CUSTOM preview type set.
140 
141  unsigned int (*flags)( LWInstance );
142  } LWNodeHandler;
143  }
144 
146  template <class T, int minVersion, int maxVersion>
147  class NodeAdaptor : public InstanceAdaptor<T>, public ItemAdaptor<T>, public RenderAdaptor<T>
148  {
149  public:
150  NodeAdaptor(const char *name, ServerTagInfo *tags)
151  {
152  LWServer::AddPlugin(LWNODE_HCLASS, name, Activate, tags);
153  }
155  static int Activate(int version, GlobalFunc *global, LWInstance inst, void *serverData)
156  {
157  if ( version > maxVersion ) return AFUNC_BADVERSION;
158  if ( version < minVersion ) return AFUNC_BADVERSION;
159  try
160  {
161  lwpp::SetSuperGlobal(global);
162  UNUSED(serverData);
163  if (version == 1)
164  {
165  LWNodeHandler *plugin = static_cast<LWNodeHandler *>(inst);
166  InstanceAdaptor<T>::Activate(plugin->inst);
167  RenderAdaptor<T>::Activate(plugin->rend);
168  ItemAdaptor<T>::Activate(plugin->item);
169  plugin->evaluate = NodeAdaptor::evaluate;
170  plugin->customPreview = NodeAdaptor::customPreview;
171  return AFUNC_OK;
172  }
173  else if (version == 2)
174  {
175  LWSDK95_1::LWNodeHandler *plugin = static_cast<LWSDK95_1::LWNodeHandler *>(inst);
179  plugin->evaluate = NodeAdaptor::evaluate;
180  plugin->customPreview = NodeAdaptor::customPreview;
181  plugin->flags = NodeAdaptor::flags;
182  return AFUNC_OK;
183  }
184  return AFUNC_BADVERSION;
185  }
186  catch (std::exception &e)
187  {
188  lwpp::LWMessage::Error("An exception occured in NodeHandler::Activate():", e.what());
189  return AFUNC_BADAPP;
190  }
191  }
192 
193  private:
194  static void evaluate (LWInstance instance, LWNodalAccess* na, NodeOutputID outID, NodeValue value)
195  {
196  try
197  {
198  if (instance)
199  {
200  T *plugin = static_cast<T *>(instance);
201  plugin->Evaluate(na, outID, value);
202  }
203  }
204  catch (std::exception &e)
205  {
206  lwpp::LWMessage::Error("An exception occured in NodeHandler::evaluate():", e.what());
207  }
208  }
209 
210  static void customPreview (LWInstance instance, int width, int height )
211  {
212  try
213  {
214  T *plugin = static_cast<T *>(instance);
215  plugin->CustomPreview(width, height);
216  }
217  catch (std::exception &e)
218  {
219  lwpp::LWMessage::Error("An exception occured in NodeHandler::customPreview():", e.what());
220  }
221  }
222  static unsigned int flags (LWInstance instance)
223  {
224  try
225  {
226  T *plugin = static_cast<T *>(instance);
227  return plugin->Flags();
228  }
229  catch (std::exception &e)
230  {
231  lwpp::LWMessage::Error("An exception occured in NodeHandler::Flags():", e.what());
232  return 0;
233  }
234  }
235  };
236 
239  {
240  public:
241  XPanelNodeHandler(void *priv, void *context, LWError *err) : NodeHandler(priv, context, err)
242  {
243  ;
244  }
245  virtual ~XPanelNodeHandler() {;}
246  virtual int NodeInputEvent ( NodeInputID nid, LWNodalEvent nevent, ConnectionType type)
247  {
249  return NodeHandler::NodeInputEvent(nid, nevent, type);
250  }
251  virtual void ChangeNotify (LWXPanelID , unsigned int , unsigned int , int event_type)
252  {
253  if ( ( event_type == LWXPEVENT_VALUE ) || ( event_type == LWXPEVENT_HIT ) )
254  {
255  Update();
256  }
257  else if (event_type == LWXPEVENT_TRACK)
258  {
260  }
261  }
262 
263  };
264 
267 }
268 
269 #endif // LWPP_NODE_HANDLER_H