LightWrap++
AC++wrapperfortheLightWave3DSDK
plugin_handler.h
Go to the documentation of this file.
1 #ifndef LWPP_HANDLER_H
2 #define LWPP_HANDLER_H
3 
4 #include <lwfilter.h>
5 #include "lwpp/message.h"
6 #include "lwpp/lwpanel_handler.h"
7 #include "lwpp/item.h"
8 
9 #pragma warning( push )
10 #pragma warning( disable : 4100 )
11 
12 namespace lwpp
13 {
14  class InstanceUpdate : public GlobalBase<LWInstUpdate>
15  {
16  public:
17  static void Update (const char *className, LWInstance inst)
18  {
19  if ((globPtr) && (className)) globPtr(className, inst);
20  }
21  };
22 
24  class InstanceHandler : public virtual Storeable
25  {
26  protected:
27  std::string m_description;
28  const char *m_pluginName;
29  const char *m_className;
31  public:
32  InstanceHandler(void *priv, void *context, LWError *err, const char *_className) : m_pluginName(0), m_className(_className)
33  {
34  UNUSED(priv);
35  UNUSED(context);
36  UNUSED(err);
37  return;
38  }
39  virtual ~InstanceHandler() {;}
40  virtual const char *DescLn() {return m_description.c_str();}
41  void SetName(const char *n) {m_pluginName = n;}
42  void SetClassName(const char *n) {m_className = n;}
43  void SetDescription(const char *n) {m_description = n;}
44  void SetDescription(const std::string& s) {m_description = s;}
45  void Update () {m_update.Update(m_className, this);}
46  void Update (const char *cName) {m_update.Update(cName, this);}
47  };
48 
50 
53  template <class T>
55  {
56  public:
58  static void Activate(LWInstanceFuncs *inst)
59  {
60  if (inst)
61  {
62  inst->create = InstanceAdaptor::Create;
63  inst->destroy = InstanceAdaptor::Destroy;
64  inst->copy = InstanceAdaptor::Copy;
65  inst->load = InstanceAdaptor::Load;
66  inst->save = InstanceAdaptor::Save;
67  inst->descln = InstanceAdaptor::DescLn;
68  inst->priv = 0; // pass on nothing
69  }
70  }
71  private:
72  static LWInstance Create (void *priv, void *context, LWError *err)
73  {
74  try
75  {
76  LWError localErr = "\0";
77  T *plugin = new T(priv, context, &localErr);
78  if (*localErr)
79  {
80  delete plugin;
81  plugin = 0;
82  *err = localErr;
83  }
84  return static_cast<LWInstance> (plugin);
85  }
86  catch (std::exception &e)
87  {
88  static LWError error = e.what();
89  err = &error;
90  return 0;
91  }
92  }
93 
94  static void Destroy (LWInstance instance)
95  {
96  try
97  {
98  T *plugin = static_cast<T *>(instance);
99  delete plugin;
100  }
101  catch (std::exception &e)
102  {
103  lwpp::LWMessage::Error("An exception occured in Instance::Destroy():", e.what());
104  return;
105  }
106  }
107 
108  static LWError Copy (LWInstance _to, LWInstance _from)
109  {
110  try
111  {
112  T *to = (T *) _to;
113  T *from = (T *) _from;
114  *to = *from;
115  return NULL;
116  }
117  catch (std::exception &e)
118  {
119  return e.what();
120  }
121  }
122 
123  static LWError Load (LWInstance instance, const LWLoadState *ls)
124  {
125  try
126  {
127  T *plugin = static_cast<T *>(instance);
128  return plugin->Load(lwpp::LoadState(ls));
129  }
130  catch (std::exception &e)
131  {
132  return e.what();
133  }
134  }
135 
136  static LWError Save (LWInstance instance, const LWSaveState *ss)
137  {
138  try
139  {
140  T *plugin = static_cast<T *>(instance);
141  return plugin->Save(lwpp::SaveState(ss));
142  }
143  catch (std::exception &e)
144  {
145 
146  return e.what();
147  }
148  }
149 
150  static const char *DescLn (LWInstance instance)
151  {
152  try
153  {
154  T *plugin = static_cast<T *>(instance);
155  return plugin->DescLn();
156  }
157  catch (std::exception &e)
158  {
159  return e.what();
160  }
161  }
162  };
163 
166  {
167  protected:
169  public:
170  virtual const LWItemID *UseItems()
171  {
172  return ItemTracker.useItems();
173  }
174  virtual void ChangeID(const LWItemID *id_list)
175  {
176  ItemTracker.changeID(id_list);
177  }
178  };
179 
181 
184  template <class T>
186  {
187  public:
189  static void Activate(LWItemFuncs *inst)
190  {
191  if ((inst) && (!lwpp::LightWave::isModeler()))
192  {
193  inst->useItems = ItemAdaptor::UseItems;
194  inst->changeID = ItemAdaptor::ChangeID;
195  }
196  }
197  private:
198  static const LWItemID *UseItems (LWInstance instance)
199  {
200  try
201  {
202  T *plugin = static_cast<T *>(instance);
203  return plugin->UseItems();
204  }
205  catch (std::exception &e)
206  {
207  lwpp::LWMessage::Error("An exception occured in Item::UseItems():", e.what());
208  return 0;
209  }
210  }
211 
212  static void ChangeID (LWInstance instance, const LWItemID *ids)
213  {
214  try
215  {
216  T *plugin = static_cast<T *>(instance);
217  if (plugin)
218  {
219  plugin->ChangeID(ids);
220  }
221  }
222  catch (std::exception &e)
223  {
224  lwpp::LWMessage::Error("An exception occured in Item::ChangeID():", e.what());
225  return;
226  }
227  }
228  };
229 
231 
232 #define LWINIT_NONE -1
233 
235  {
236  protected:
237  LWFrame currentFrame;
238  LWTime currentTime;
240  public:
242 
243  virtual LWError Init(int mode)
244  {
245  renderMode = mode;
246  return 0;
247  }
248  virtual void Cleanup()
249  {
251  }
252 
253  virtual LWError NewTime(LWFrame frame, LWTime time)
254  {
255  currentFrame = frame;
256  currentTime = time;
257  return 0;
258  }
259  };
260 
262 
265  template <class T>
267  {
268  public:
270  static void Activate(LWRenderFuncs *inst)
271  {
272  if (inst)
273  {
274  inst->init = RenderAdaptor::Init;
275  inst->cleanup = RenderAdaptor::Cleanup;
276  inst->newTime = RenderAdaptor::NewTime;
277  }
278  }
279  private:
280  static LWError Init (LWInstance instance, int mode)
281  {
282  try
283  {
284  T *plugin = static_cast<T *>(instance);
285  return plugin->Init(mode);
286  }
287  catch (std::exception &e)
288  {
289  return e.what();
290  }
291  }
292  static void Cleanup (LWInstance instance)
293  {
294  try
295  {
296  T *plugin = static_cast<T *>(instance);
297  plugin->Cleanup();
298  }
299  catch (std::exception &e)
300  {
301  lwpp::LWMessage::Error("An exception occured in Render::Cleanup():", e.what());
302  return;
303  }
304  }
305 
306  static LWError NewTime (LWInstance instance, LWFrame frame, LWTime time)
307  {
308  try
309  {
310  T *plugin = static_cast<T *>(instance);
311  return plugin->NewTime(frame, time);
312  }
313  catch (std::exception &e)
314  {
315  return e.what();
316  }
317  }
318  };
319 
322  {
323  public:
324  virtual LWError Command(const char *command) {UNUSED(command);return NULL;}
325  };
326 
328 
331  template <class T>
333  {
334  protected:
336  static int ActivateUI(LWInterface *ui)
337  {
338  ui->command = InterfaceAdaptor::Command;
339  return AFUNC_OK;
340  }
341  private:
342  static LWError Command (LWInstance instance, const char* command)
343  {
344  T *plugin = (T *) instance;
345  return plugin->Command(command);
346  }
347  };
348 
351  {
352  public:
354 
356  virtual void *DataGet(unsigned int )
357  {
358  return 0;
359  }
361  virtual LWXPRefreshCode DataSet(unsigned int , void *)
362  {
363  return LWXPRC_NONE;
364  }
365 
366  virtual void ButtonClick(unsigned int)
367  {
368  ;
369  }
370 
371  virtual void ChangeNotify (LWXPanelID , unsigned int , unsigned int , int)
372  {
373  ;
374  }
375 
376  virtual void PanelDestroyNotify(void)
377  {
378  LW_XPanel.setID(0); // just set the ID to 0 and don't actually close the XPanel
379  LW_XPanel.DestroyOnExit(false); // let LW handle this now
380  }
381 
382  virtual void ControlDraw ( unsigned int , XPDrawArea &)
383  {
384  ;
385  }
386  virtual void ControlZoom ( unsigned int , int , int , int *, int )
387  {
388  ;
389  }
390 
391  void PopCommand (int, int)
392  {
393  ;
394  }
395  // protected:
396  // Callbacks
397  static void LWXPanelPopCmdFunc (LWXPanelID panel, int cid, int cmd);
398 
399  static void LWXPanelControlDrawFunc (LWXPanelID panel, unsigned int cid, LWXPDrAreaID reg, int w, int h );
400 
401  static void LWXPanelControlZoomFunc (LWXPanelID panel, unsigned int cid, int x, int y, int *region, int clickcount );
402 
404  static void *CB_DataGet(LWInstance inst, unsigned int vid );
405 
407  static LWXPRefreshCode CB_DataSet(LWInstance inst,unsigned int vid, void *value);
408 
410  static void LWXPanelBtnClickFunc (LWXPanelID panel, int cid);
411 
412  static void LWXPanelChangeNotifyFunc (LWXPanelID panel, unsigned int cid, unsigned int vid, int event_type);
413 
414  static void LWXPanelDestroyNotifyFunc(void *data);
415 
416  virtual void CreateViewXPanel(LWXPanelControl *controls, LWXPanelDataDesc *desc, LWXPanelHint *hints = 0, bool do_destroy = false);
417 
419 
420  virtual ~XPanelInterface(){;}
421 
422  virtual int Interface (int version, LWInterface *local, void *serverdata)
423  {
424  UNUSED(version);
425  UNUSED(local);
426  UNUSED(serverdata);
427  return AFUNC_OK;
428  }
429  };
430 
431  template <class T>
433  {
434  public:
435  XPanelAdaptor(const char *name, const char *className, ServerTagInfo tags[])
436  {
437  LWServer::AddPlugin(className, name, Interface, tags);
438  }
439 
440  static int Interface (int version, GlobalFunc *global, void *loc, void *serverdata)
441  {
442  if ( version < LWINTERFACE_VERSION ) return AFUNC_BADVERSION;
443  try
444  {
445  if (loc)
446  {
447  lwpp::SetSuperGlobal(global);
448  LWInterface *local = static_cast<LWInterface *>(loc);
449  T *plugin = static_cast<T *>(local->inst);
450  return plugin->Interface(version, local, serverdata);
451  }
452  return AFUNC_OK;
453  }
454  catch (std::exception &e)
455  {
456  lwpp::LWMessage::Error("An exception occured in Xpanel::Interface():", e.what());
457  return AFUNC_BADAPP;
458  }
459  }
460  };
461 
462  // Macros
463 
464 #define IMPLEMENT_XPANELHANDLER(pluginClass) \
465  class XPanel##pluginClass##Handler : public pluginClass##Handler, public XPanelInterface \
466  { \
467  public: \
468  XPanel##pluginClass##Handler(void *g, void *context, LWError *err) \
469  : pluginClass##Handler(g, context, err) \
470  { ; } \
471  }
472 
473 #define IMPLEMENT_XPANELADAPTOR(pluginClass, min, max) \
474  template <class T, int minV = min, int maxV = max> class XPanel##pluginClass##Adaptor : public pluginClass##Adaptor<T, minV, maxV>, public XPanelAdaptor<T> \
475  { \
476  public: \
477  XPanel##pluginClass##Adaptor(const char *name, ServerTagInfo tags[] = 0) \
478  : XPanelAdaptor<T>(name, #pluginClass "Interface", tags), \
479  pluginClass##Adaptor<T, minV, maxV>(name, tags) \
480  { ; } \
481  }
482 
483 #define IMPLEMENT_LWPANELHANDLER(pluginClass) \
484  class LWPanel##pluginClass##Handler : public pluginClass##Handler, public LWPanelInterface \
485  { \
486  public: \
487  LWPanel##pluginClass##Handler(void *g, void *context, LWError *err) \
488  : pluginClass##Handler(g, context, err) \
489  { ; } \
490  }
491 
492 #define IMPLEMENT_LWPANELADAPTOR(pluginClass, min, max) \
493  template <class T, int minV = min, int maxV = max> class LWPanel##pluginClass##Adaptor : public pluginClass##Adaptor<T, minV, maxV>, public LWPanelAdaptor<T> \
494  { \
495  public: \
496  LWPanel##pluginClass##Adaptor(const char *name, ServerTagInfo tags[] = 0) \
497  : LWPanelAdaptor<T>(name, #pluginClass "Interface", tags), \
498  pluginClass##Adaptor<T, minV, maxV>(name, tags) \
499  { ; } \
500  }
501 
502 } // end of namespace lwpp
503 
504 #pragma warning( pop )
505 #endif // LWPP_HANDLER_H