LightWrap++
AC++wrapperfortheLightWave3DSDK
customobject_handler.h
Go to the documentation of this file.
1 #ifndef CUSTOMOBJECT_HANDLER_H
2 #define CUSTOMOBJECT_HANDLER_H
3 
4 #include <lwpp/plugin_handler.h>
5 #include <lwcustobj.h>
6 
7 namespace lwpp
8 {
9 
11  {
12  protected:
13  const LWCustomObjAccess *access;
14  public:
15  CustomObjAccess(const LWCustomObjAccess *acc) : access(acc) {;}
16  int View() {return access->view;}
17  int Flags() {return access->flags;}
18  bool isSelected() {return (Flags() & LWCOFL_SELECTED) != 0;}
19  bool isPicking() {return (Flags() & LWCOFL_PICKING) != 0;}
20  void SetColor (float rgba[4]) {access->setColor(access->dispData, rgba);}
21  void SetColor (float r, float g, float b, float a)
22  {
23  float rgba[4] = {r, g, b, a};
24  SetColor(rgba);
25  }
26  void SetPattern (int lpat) {access->setPattern(access->dispData, lpat);}
27  void SetTexture (int size, unsigned char *image) {access->setTexture(access->dispData, size, image);}
28  void SetUVs (double uv0[2], double uv1[2], double uv2[2], double uv3[2]) {access->setUVs(access->dispData, uv0, uv1, uv2, uv3);}
29  void SetUVs (double uv[4][2]) {access->setUVs(access->dispData, uv[0], uv[1], uv[2], uv[3]);}
30  void DrawPoint (double pos[3], int csys) {access->point(access->dispData, pos, csys);}
31  void DrawLine (double from[3], double to[3], int csys) {access->line(access->dispData, from, to, csys);}
32  void DrawTriangle (double v1[3], double v2[3], double v3[3], int csys) {access->triangle(access->dispData, v1, v2, v3, csys);}
33  void DrawQuad (double v1[3], double v2[3], double v3[3], double v4[3], int csys) {access->quad(access->dispData, v1, v2, v3, v4, csys);}
34  void DrawQuad (double v[4][3], int csys) {access->quad(access->dispData, v[0], v[1], v[2], v[3], csys);}
35  void DrawCircle (double center[3], double radius, int csys) {access->circle(access->dispData, center, radius, csys);}
36  void DrawText (double center[3], const char *text, int just, int csys) {access->text(access->dispData, center, text, just, csys);}
37 
38 #if LW_SDK >= 93
39  Point3d GetViewPos() {return Point3d(access->viewPos);}
40  Vector3d GetViewDir() {return Vector3d(access->viewDir);}
41  void SetCoordinateSystem(LWItemID item) {access->setCSysItem(access->dispData, item);}
42 #endif
43 #if LW_SDK >= 95
44  void DrawPolygon(unsigned int numv, double v[][3], int csys)
45  {
46  access->polygon(access->dispData, numv, v, csys);
47  }
48  void DrawPolygon(unsigned int numv, unsigned int verts[], double v[][3], int csys)
49  {
50  access->polyIndexed(access->dispData, numv, verts, v, csys);
51  }
52  void SetDrawMode(unsigned int mode) {access->setDrawMode(access->dispData, mode);}
53  void DrawDisk(double p[3], double r, int csys)
54  {
55  access->disk(access->dispData, p, r, csys);
56  }
57 #endif
58 
59 #if LW_SDK >= 96
60  // Todo, add part number
61 #endif
62 
63  };
64 
66 
71  {
72  protected:
74  LWItemID Context;
75  public:
76  CustomObjHandler(void *g, void *context, LWError *err) : InstanceHandler(g, context, err, LWCUSTOMOBJ_HCLASS)
77  {
78  Context = LWItemID(context);
79  }
80  virtual ~CustomObjHandler() {;}
81  virtual void Evaluate(CustomObjAccess &coa)
82  {
83  UNUSED(coa);
84  }
85  virtual unsigned int Flags() {return 0;}
86  };
87 
89 
92  template <class T, int maxVersion, int minVersion>
93  class CustomObjAdaptor : public InstanceAdaptor <T>, public ItemAdaptor <T>, public RenderAdaptor <T>
94  {
95  public:
96  CustomObjAdaptor(const char *name, ServerTagInfo *tags)
97  {
98  LWServer::AddPlugin(LWCUSTOMOBJ_HCLASS, name, Activate, tags);
99  }
101  static int Activate (int version, GlobalFunc *global, LWInstance inst, void *serverData)
102  {
103  if ( version > maxVersion ) return AFUNC_BADVERSION;
104  if ( version < minVersion ) return AFUNC_BADVERSION;
105  try
106  {
107  UNUSED(serverData);
108  lwpp::SetSuperGlobal(global);
109 
110  LWCustomObjHandler *plugin = static_cast<LWCustomObjHandler *>(inst);
111 
112  InstanceAdaptor<T>::Activate(plugin->inst);
113  ItemAdaptor<T>::Activate(plugin->item);
114  RenderAdaptor<T>::Activate(plugin->rend);
115  plugin->evaluate = CustomObjAdaptor::Evaluate;
116  plugin->flags = CustomObjAdaptor::Flags;
117 
118  return AFUNC_OK;
119  }
120  catch (std::exception &e)
121  {
122  lwpp::LWMessage::Error("An exception occured in CustomObject::Activate():", e.what());
123  return AFUNC_BADAPP;
124  }
125  }
126  private:
127 
128  static void Evaluate (LWInstance instance, const LWCustomObjAccess *lw_coa)
129  {
130  try
131  {
132  T *plugin = (T *) instance;
133  plugin->Evaluate(CustomObjAccess(lw_coa));
134  }
135  catch (std::exception &e)
136  {
137  lwpp::LWMessage::Error("An exception occured in CustomObject::Evaluate():", e.what());
138  }
139  }
140 
141  static unsigned int Flags (LWInstance instance)
142  {
143  try
144  {
145  T *plugin = (T *) instance;
146  return plugin->Flags();
147  }
148  catch (std::exception &e)
149  {
150  lwpp::LWMessage::Error("An exception occured in CustomObject::Flags():", e.what());
151  return 0;
152  }
153  }
154  };
155 
161 
166 
167 } // end namespace
168 
169 #endif // CUSTOMOBJECT_HANDLER_H