openscenegraph
Callback
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSG_CALLBACK
15#define OSG_CALLBACK 1
16
17#include <osg/Object>
18#include <osg/UserDataContainer>
19
20// forward declare
21namespace osgGA { class EventHandler; }
22
23namespace osg {
24
25// forward declare
26class CallbackObject;
27class NodeCallback;
28class StateAttributeCallback;
29class UniformCallback;
30class DrawableUpdateCallback;
31class DrawableEventCallback;
32class DrawableCullCallback;
33
34class OSG_EXPORT Callback : public virtual Object {
35
36 public :
37
38 Callback(){}
39
40 Callback(const Callback& cb,const CopyOp& copyop):
41 osg::Object(cb, copyop),
42 _nestedCallback(cb._nestedCallback) {}
43
44 META_Object(osg, Callback);
45
46 virtual Callback* asCallback() { return this; }
47 virtual const Callback* asCallback() const { return this; }
48
49 virtual CallbackObject* asCallbackObject() { return 0; }
50 virtual const CallbackObject* asCallbackObject() const { return 0; }
51
52 virtual NodeCallback* asNodeCallback() { return 0; }
53 virtual const NodeCallback* asNodeCallback() const { return 0; }
54
55 virtual StateAttributeCallback* asStateAttributeCallback() { return 0; }
56 virtual const StateAttributeCallback* asStateAttributeCallback() const { return 0; }
57
58 virtual UniformCallback* asUniformCallback() { return 0; }
59 virtual const UniformCallback* asUniformCallback() const { return 0; }
60
61 virtual DrawableUpdateCallback* asDrawableUpdateCallback() { return 0; }
62 virtual const DrawableUpdateCallback* asDrawableUpdateCallback() const { return 0; }
63
64 virtual DrawableEventCallback* asDrawableEventCallback() { return 0; }
65 virtual const DrawableEventCallback* asDrawableEventCallback() const { return 0; }
66
67 virtual DrawableCullCallback* asDrawableCullCallback() { return 0; }
68 virtual const DrawableCullCallback* asDrawableCullCallback() const { return 0; }
69
70 virtual osgGA::EventHandler* asEventHandler() { return 0; }
71 virtual const osgGA::EventHandler* asEventHandler() const { return 0; }
72
73 /** Invoke the callback, first parameter is the Object that the callback is attached to,
74 * the second parameter, the data, is typically the NodeVisitor that is invoking the callback.
75 * The run(..) method may be overridden by users directly, or if the user is using one of the old
76 * style callbacks such as NodeCallback or Drawable::UpdateCallback then you can just override
77 * the appropriate callback method on those callback subclasses.
78 * If you are implementing your own callback then one should call traverse() to make sure nested callbacks
79 * and visitor traversal() is completed. */
80 virtual bool run(osg::Object* object, osg::Object* data)
81 {
82 return traverse(object, data);
83 }
84
85 /** traverse the nested callbacks or call NodeVisitor::traverse() if the object is Node, and data is NodeVisitor.*/
86 bool traverse(osg::Object* object, osg::Object* data);
87
88 void setNestedCallback(osg::Callback* cb) { _nestedCallback = cb; }
89 osg::Callback* getNestedCallback() { return _nestedCallback.get(); }
90 const osg::Callback* getNestedCallback() const { return _nestedCallback.get(); }
91
92 inline void addNestedCallback(osg::Callback* nc)
93 {
94 if (nc)
95 {
96 if (_nestedCallback.valid())
97 {
98 _nestedCallback->addNestedCallback(nc);
99 }
100 else
101 {
102 _nestedCallback = nc;
103 }
104 }
105 }
106
107 inline void removeNestedCallback(osg::Callback* nc)
108 {
109 if (nc)
110 {
111 if (_nestedCallback==nc)
112 {
113 ref_ptr<osg::Callback> new_nested_callback = _nestedCallback->getNestedCallback();
114 _nestedCallback->setNestedCallback(0);
115 _nestedCallback = new_nested_callback;
116 }
117 else if (_nestedCallback.valid())
118 {
119 _nestedCallback->removeNestedCallback(nc);
120 }
121 }
122 }
123
124 protected:
125
126 virtual ~Callback() {}
127 ref_ptr<Callback> _nestedCallback;
128};
129
130typedef std::vector< osg::ref_ptr<osg::Object> > Parameters;
131
132/** Callback for attaching a script to a Node's via there UserDataContainer for the purpose of overriding class methods within scripts.*/
133class OSG_EXPORT CallbackObject : public virtual osg::Callback
134{
135public:
136 CallbackObject() {}
137 CallbackObject(const std::string& name) { setName(name); }
138 CallbackObject(const CallbackObject& co, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY):
139 osg::Object(co, copyop),
140 osg::Callback(co,copyop) {}
141
142 META_Object(osg, CallbackObject);
143
144 virtual CallbackObject* asCallbackObject() { return this; }
145 virtual const CallbackObject* asCallbackObject() const { return this; }
146
147 /** override Callback::run() entry point to adapt to CallbackObject::run(..) method.*/
148 bool run(osg::Object* object, osg::Object* data);
149
150 inline bool run(osg::Object* object) const
151 {
152 osg::Parameters inputParameters;
153 osg::Parameters outputParameters;
154 return run(object, inputParameters, outputParameters);
155 }
156
157 virtual bool run(osg::Object* object, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const;
158
159};
160
161/** Convenience function for getting the CallbackObject associated with specified name from an Object's UserDataContainer.*/
162inline CallbackObject* getCallbackObject(osg::Object* object, const std::string& name)
163{
164 osg::UserDataContainer* udc = object->getUserDataContainer();
165 if (!udc) return 0;
166
167 osg::Object* obj = udc->getUserObject(name);
168 if (!obj) return 0;
169
170 return obj->asCallbackObject();
171}
172
173
174/** Convenience function for getting the CallbackObject associated with specified name from an Object's UserDataContainer.*/
175inline const CallbackObject* getCallbackObject(const osg::Object* object, const std::string& name)
176{
177 const osg::UserDataContainer* udc = object->getUserDataContainer();
178 if (!udc) return 0;
179
180 const osg::Object* obj = udc->getUserObject(name);
181 if (!obj) return 0;
182
183 return obj->asCallbackObject();
184}
185
186/** Call run(..) on named CallbackObjects attached to specified Object. Return true if at least one CallbackObject has been successfully invoked.*/
187inline bool runNamedCallbackObjects(osg::Object* object, const std::string& name, osg::Parameters& inputParameters, osg::Parameters& outputParameters)
188{
189 bool result = false;
190 osg::UserDataContainer* udc = object->getUserDataContainer();
191 if (udc)
192 {
193 for(unsigned int i = 0; i<udc->getNumUserObjects(); ++i)
194 {
195 osg::Object* obj = udc->getUserObject(i);
196 if (obj && obj->getName()==name)
197 {
198 osg::CallbackObject* co = obj->asCallbackObject();
199 if (co) result = co->run(object, inputParameters, outputParameters) | result;
200 }
201 }
202 }
203
204 return result;
205}
206
207
208
209// forward declare
210class Node;
211class NodeVisitor;
212
213
214/** Deprecated. */
215class OSG_EXPORT NodeCallback : public virtual Callback {
216
217 public :
218
219 NodeCallback(){}
220
221 NodeCallback(const NodeCallback& nc,const CopyOp& copyop):
222 Object(nc, copyop),
223 Callback(nc, copyop) {}
224
225 META_Object(osg,NodeCallback);
226
227 virtual NodeCallback* asNodeCallback() { return this; }
228 virtual const NodeCallback* asNodeCallback() const { return this; }
229
230 /** NodeCallback overrides the Callback::run() method to adapt it the old style NodeCallback::operator()(Node* node, NodeVisitor* nv) method.*/
231 virtual bool run(osg::Object* object, osg::Object* data);
232
233 /** Callback method called by the NodeVisitor when visiting a node.*/
234 virtual void operator()(Node* node, NodeVisitor* nv);
235
236 protected:
237
238 virtual ~NodeCallback() {}
239};
240
241// forward declare
242class StateAttribute;
243
244/** Deprecated. */
245class OSG_EXPORT StateAttributeCallback : public virtual osg::Callback
246{
247 public:
248 StateAttributeCallback() {}
249
250 StateAttributeCallback(const StateAttributeCallback& org,const CopyOp& copyop) :
251 Object(org, copyop),
252 Callback(org, copyop) {}
253
254 META_Object(osg,StateAttributeCallback);
255
256 virtual StateAttributeCallback* asStateAttributeCallback() { return this; }
257 virtual const StateAttributeCallback* asStateAttributeCallback() const { return this; }
258
259 /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
260 virtual bool run(osg::Object* object, osg::Object* data);
261
262 /** do customized update code.*/
263 virtual void operator () (StateAttribute*, NodeVisitor*) {}
264};
265
266// forward declare
267class Uniform;
268
269/** Deprecated. */
270class OSG_EXPORT UniformCallback : public virtual osg::Callback
271{
272public:
273 UniformCallback() {}
274
275 UniformCallback(const UniformCallback& org, const CopyOp& copyop) :
276 Object(org, copyop),
277 Callback(org, copyop) {}
278
279 META_Object(osg, UniformCallback);
280
281 virtual UniformCallback* asUniformCallback() { return this; }
282 virtual const UniformCallback* asUniformCallback() const { return this; }
283
284 /** override Callback::run() entry point to adapt to UniformCallback::run(..) method.*/
285 virtual bool run(osg::Object* object, osg::Object* data);
286
287 /** do customized update code.*/
288 virtual void operator () (Uniform*, NodeVisitor*) {}
289};
290
291
292// forward declare
293class Drawable;
294class State;
295class RenderInfo;
296
297class OSG_EXPORT DrawableUpdateCallback : public virtual Callback
298{
299public:
300 DrawableUpdateCallback() {}
301
302 DrawableUpdateCallback(const DrawableUpdateCallback& org,const CopyOp& copyop):
303 Object(org, copyop),
304 Callback(org, copyop) {}
305
306 META_Object(osg,DrawableUpdateCallback);
307
308 virtual DrawableUpdateCallback* asDrawableUpdateCallback() { return this; }
309 virtual const DrawableUpdateCallback* asDrawableUpdateCallback() const { return this; }
310
311 /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
312 virtual bool run(osg::Object* object, osg::Object* data);
313
314 /** do customized update code.*/
315 virtual void update(osg::NodeVisitor*, osg::Drawable*) {}
316};
317
318
319class OSG_EXPORT DrawableEventCallback : public virtual Callback
320{
321public:
322 DrawableEventCallback() {}
323
324 DrawableEventCallback(const DrawableEventCallback& org, const CopyOp& copyop) :
325 Object(org, copyop),
326 Callback(org, copyop) {}
327
328 META_Object(osg,DrawableEventCallback);
329
330 virtual DrawableEventCallback* asDrawableEventCallback() { return this; }
331 virtual const DrawableEventCallback* asDrawableEventCallback() const { return this; }
332
333 /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
334 virtual bool run(osg::Object* object, osg::Object* data);
335
336 /** do customized Event code. */
337 virtual void event(osg::NodeVisitor*, osg::Drawable*) {}
338};
339
340class OSG_EXPORT DrawableCullCallback : public virtual Callback
341{
342public:
343 DrawableCullCallback() {}
344
345 DrawableCullCallback(const DrawableCullCallback& org, const CopyOp& copyop) :
346 Object(org, copyop),
347 Callback(org, copyop) {}
348
349 META_Object(osg,DrawableCullCallback);
350
351 virtual DrawableCullCallback* asDrawableCullCallback() { return this; }
352 virtual const DrawableCullCallback* asDrawableCullCallback() const { return this; }
353
354 // just use the standard run implementation to passes run onto any nested callbacks.
355 using Callback::run;
356
357 /** deprecated.*/
358 virtual bool cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) const { return false; }
359
360 /** do customized cull code, return true if drawable should be culled.*/
361 virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const;
362};
363
364
365} // namespace
366
367#endif
368