File: Callback

package info (click to toggle)
openscenegraph 3.6.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 40,100 kB
  • sloc: cpp: 392,065; ansic: 21,495; java: 1,020; yacc: 548; makefile: 430; objc: 406; xml: 155; lex: 151; javascript: 34
file content (368 lines) | stat: -rw-r--r-- 12,702 bytes parent folder | download | duplicates (4)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
 *
 * This library is open source and may be redistributed and/or modified under
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSG_CALLBACK
#define OSG_CALLBACK 1

#include <osg/Object>
#include <osg/UserDataContainer>

// forward declare
namespace osgGA { class EventHandler; }

namespace osg {

// forward declare
class CallbackObject;
class NodeCallback;
class StateAttributeCallback;
class UniformCallback;
class DrawableUpdateCallback;
class DrawableEventCallback;
class DrawableCullCallback;

class OSG_EXPORT Callback : public virtual Object {

    public :

        Callback(){}

        Callback(const Callback& cb,const CopyOp& copyop):
            osg::Object(cb, copyop),
            _nestedCallback(cb._nestedCallback) {}

        META_Object(osg, Callback);

        virtual Callback* asCallback() { return this; }
        virtual const Callback* asCallback() const { return this; }

        virtual CallbackObject* asCallbackObject() { return 0; }
        virtual const CallbackObject* asCallbackObject() const { return 0; }

        virtual NodeCallback* asNodeCallback() { return 0; }
        virtual const NodeCallback* asNodeCallback() const { return 0; }

        virtual StateAttributeCallback* asStateAttributeCallback() { return 0; }
        virtual const StateAttributeCallback* asStateAttributeCallback() const { return 0; }

        virtual UniformCallback* asUniformCallback() { return 0; }
        virtual const UniformCallback* asUniformCallback() const { return 0; }

        virtual DrawableUpdateCallback* asDrawableUpdateCallback() { return 0; }
        virtual const DrawableUpdateCallback* asDrawableUpdateCallback() const { return 0; }

        virtual DrawableEventCallback* asDrawableEventCallback() { return 0; }
        virtual const DrawableEventCallback* asDrawableEventCallback() const { return 0; }

        virtual DrawableCullCallback* asDrawableCullCallback() { return 0; }
        virtual const DrawableCullCallback* asDrawableCullCallback() const { return 0; }

        virtual osgGA::EventHandler* asEventHandler() { return 0; }
        virtual const osgGA::EventHandler* asEventHandler() const { return 0; }

        /** Invoke the callback, first parameter is the Object that the callback is attached to,
         *  the second parameter, the data, is typically the NodeVisitor that is invoking the callback.
         *  The run(..) method may be overridden by users directly, or if the user is using one of the old
         *  style callbacks such as NodeCallback or Drawable::UpdateCallback then you can just override
         *  the appropriate callback method on those callback subclasses.
         *  If you are implementing your own callback then one should call traverse() to make sure nested callbacks
         *  and visitor traversal() is completed. */
        virtual bool run(osg::Object* object, osg::Object* data)
        {
            return traverse(object, data);
        }

        /** traverse the nested callbacks or call NodeVisitor::traverse() if the object is Node, and data is NodeVisitor.*/
        bool traverse(osg::Object* object, osg::Object* data);

        void setNestedCallback(osg::Callback* cb) { _nestedCallback = cb; }
        osg::Callback* getNestedCallback() { return _nestedCallback.get(); }
        const osg::Callback* getNestedCallback() const { return _nestedCallback.get(); }

        inline void addNestedCallback(osg::Callback* nc)
        {
            if (nc)
            {
                if (_nestedCallback.valid())
                {
                    _nestedCallback->addNestedCallback(nc);
                }
                else
                {
                    _nestedCallback = nc;
                }
            }
        }

        inline void removeNestedCallback(osg::Callback* nc)
        {
            if (nc)
            {
                if (_nestedCallback==nc)
                {
                    ref_ptr<osg::Callback> new_nested_callback = _nestedCallback->getNestedCallback();
                    _nestedCallback->setNestedCallback(0);
                    _nestedCallback = new_nested_callback;
                }
                else if (_nestedCallback.valid())
                {
                    _nestedCallback->removeNestedCallback(nc);
                }
            }
        }

    protected:

        virtual ~Callback() {}
        ref_ptr<Callback> _nestedCallback;
};

typedef std::vector< osg::ref_ptr<osg::Object> > Parameters;

/** Callback for attaching a script to a Node's via there UserDataContainer for the purpose of overriding class methods within scripts.*/
class OSG_EXPORT CallbackObject : public virtual osg::Callback
{
public:
    CallbackObject() {}
    CallbackObject(const std::string& name) { setName(name); }
    CallbackObject(const CallbackObject& co, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY):
        osg::Object(co, copyop),
        osg::Callback(co,copyop) {}

    META_Object(osg, CallbackObject);

    virtual CallbackObject* asCallbackObject() { return this; }
    virtual const CallbackObject* asCallbackObject() const { return this; }

    /** override Callback::run() entry point to adapt to CallbackObject::run(..) method.*/
    bool run(osg::Object* object, osg::Object* data);

    inline bool run(osg::Object* object) const
    {
        osg::Parameters inputParameters;
        osg::Parameters outputParameters;
        return run(object, inputParameters, outputParameters);
    }

    virtual bool run(osg::Object* object, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const;

};

/** Convenience function for getting the CallbackObject associated with specified name from an Object's UserDataContainer.*/
inline CallbackObject* getCallbackObject(osg::Object* object, const std::string& name)
{
    osg::UserDataContainer* udc = object->getUserDataContainer();
    if (!udc) return 0;

    osg::Object* obj = udc->getUserObject(name);
    if (!obj) return 0;

    return obj->asCallbackObject();
}


/** Convenience function for getting the CallbackObject associated with specified name from an Object's UserDataContainer.*/
inline const CallbackObject* getCallbackObject(const osg::Object* object, const std::string& name)
{
    const osg::UserDataContainer* udc = object->getUserDataContainer();
    if (!udc) return 0;

    const osg::Object* obj = udc->getUserObject(name);
    if (!obj) return 0;

    return obj->asCallbackObject();
}

/** Call run(..) on named CallbackObjects attached to specified Object. Return true if at least one CallbackObject has been successfully invoked.*/
inline bool runNamedCallbackObjects(osg::Object* object, const std::string& name, osg::Parameters& inputParameters, osg::Parameters& outputParameters)
{
    bool result = false;
    osg::UserDataContainer* udc = object->getUserDataContainer();
    if (udc)
    {
        for(unsigned int i = 0; i<udc->getNumUserObjects(); ++i)
        {
            osg::Object* obj = udc->getUserObject(i);
            if (obj && obj->getName()==name)
            {
                osg::CallbackObject* co = obj->asCallbackObject();
                if (co) result = co->run(object, inputParameters, outputParameters) | result;
            }
        }
    }

    return result;
}



// forward declare
class Node;
class NodeVisitor;


/** Deprecated. */
class OSG_EXPORT NodeCallback : public virtual Callback {

    public :

        NodeCallback(){}

        NodeCallback(const NodeCallback& nc,const CopyOp& copyop):
            Object(nc, copyop),
            Callback(nc, copyop) {}

        META_Object(osg,NodeCallback);

        virtual NodeCallback* asNodeCallback() { return this; }
        virtual const NodeCallback* asNodeCallback() const { return this; }

        /** NodeCallback overrides the Callback::run() method to adapt it the old style NodeCallback::operator()(Node* node, NodeVisitor* nv) method.*/
        virtual bool run(osg::Object* object, osg::Object* data);

        /** Callback method called by the NodeVisitor when visiting a node.*/
        virtual void operator()(Node* node, NodeVisitor* nv);

    protected:

        virtual ~NodeCallback() {}
};

// forward declare
class StateAttribute;

/** Deprecated. */
class OSG_EXPORT StateAttributeCallback : public virtual osg::Callback
{
    public:
        StateAttributeCallback() {}

        StateAttributeCallback(const StateAttributeCallback& org,const CopyOp& copyop) :
            Object(org, copyop),
            Callback(org, copyop) {}

        META_Object(osg,StateAttributeCallback);

        virtual StateAttributeCallback* asStateAttributeCallback() { return this; }
        virtual const StateAttributeCallback* asStateAttributeCallback() const { return this; }

        /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
        virtual bool run(osg::Object* object, osg::Object* data);

        /** do customized update code.*/
        virtual void operator () (StateAttribute*, NodeVisitor*) {}
};

// forward declare
class Uniform;

/** Deprecated. */
class OSG_EXPORT UniformCallback : public virtual osg::Callback
{
public:
    UniformCallback() {}

    UniformCallback(const UniformCallback& org, const CopyOp& copyop) :
        Object(org, copyop),
        Callback(org, copyop) {}

    META_Object(osg, UniformCallback);

    virtual UniformCallback* asUniformCallback() { return this; }
    virtual const UniformCallback* asUniformCallback() const { return this; }

    /** override Callback::run() entry point to adapt to UniformCallback::run(..) method.*/
    virtual bool run(osg::Object* object, osg::Object* data);

    /** do customized update code.*/
    virtual void operator () (Uniform*, NodeVisitor*) {}
};


// forward declare
class Drawable;
class State;
class RenderInfo;

class OSG_EXPORT DrawableUpdateCallback : public virtual Callback
{
public:
    DrawableUpdateCallback() {}

    DrawableUpdateCallback(const DrawableUpdateCallback& org,const CopyOp& copyop):
    Object(org, copyop),
    Callback(org, copyop) {}

    META_Object(osg,DrawableUpdateCallback);

    virtual DrawableUpdateCallback* asDrawableUpdateCallback() { return this; }
    virtual const DrawableUpdateCallback* asDrawableUpdateCallback() const { return this; }

    /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
    virtual bool run(osg::Object* object, osg::Object* data);

    /** do customized update code.*/
    virtual void update(osg::NodeVisitor*, osg::Drawable*) {}
};


class OSG_EXPORT DrawableEventCallback : public virtual Callback
{
public:
    DrawableEventCallback() {}

    DrawableEventCallback(const DrawableEventCallback& org, const CopyOp& copyop) :
        Object(org, copyop),
        Callback(org, copyop) {}

    META_Object(osg,DrawableEventCallback);

    virtual DrawableEventCallback* asDrawableEventCallback() { return this; }
    virtual const DrawableEventCallback* asDrawableEventCallback() const { return this; }

    /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
    virtual bool run(osg::Object* object, osg::Object* data);

    /** do customized Event code. */
    virtual void event(osg::NodeVisitor*, osg::Drawable*) {}
};

class OSG_EXPORT DrawableCullCallback : public virtual Callback
{
public:
    DrawableCullCallback() {}

    DrawableCullCallback(const DrawableCullCallback& org, const CopyOp& copyop) :
        Object(org, copyop),
        Callback(org, copyop) {}

    META_Object(osg,DrawableCullCallback);

    virtual DrawableCullCallback* asDrawableCullCallback() { return this; }
    virtual const DrawableCullCallback* asDrawableCullCallback() const { return this; }

    // just use the standard run implementation to passes run onto any nested callbacks.
    using Callback::run;

    /** deprecated.*/
    virtual bool cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) const { return false; }

    /** do customized cull code, return true if drawable should be culled.*/
    virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const;
};


} // namespace

#endif