| 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_NODECALLBACK |
|---|
| 15 | #define OSG_NODECALLBACK 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Object> |
|---|
| 18 | #include <osg/ref_ptr> |
|---|
| 19 | |
|---|
| 20 | namespace osg { |
|---|
| 21 | |
|---|
| 22 | class Node; |
|---|
| 23 | class NodeVisitor; |
|---|
| 24 | |
|---|
| 25 | class OSG_EXPORT NodeCallback : public virtual Object { |
|---|
| 26 | |
|---|
| 27 | public : |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | NodeCallback(){} |
|---|
| 31 | |
|---|
| 32 | NodeCallback(const NodeCallback& nc,const CopyOp&): |
|---|
| 33 | _nestedCallback(nc._nestedCallback) {} |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | META_Object(osg,NodeCallback); |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | /** Callback method called by the NodeVisitor when visiting a node.*/ |
|---|
| 40 | virtual void operator()(Node* node, NodeVisitor* nv) |
|---|
| 41 | { |
|---|
| 42 | // note, callback is responsible for scenegraph traversal so |
|---|
| 43 | // they must call traverse(node,nv) to ensure that the |
|---|
| 44 | // scene graph subtree (and associated callbacks) are traversed. |
|---|
| 45 | traverse(node,nv); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /** Call any nested callbacks and then traverse the scene graph. */ |
|---|
| 49 | void traverse(Node* node,NodeVisitor* nv); |
|---|
| 50 | |
|---|
| 51 | void setNestedCallback(NodeCallback* nc) { _nestedCallback = nc; } |
|---|
| 52 | NodeCallback* getNestedCallback() { return _nestedCallback.get(); } |
|---|
| 53 | const NodeCallback* getNestedCallback() const { return _nestedCallback.get(); } |
|---|
| 54 | |
|---|
| 55 | inline void addNestedCallback(NodeCallback* nc) |
|---|
| 56 | { |
|---|
| 57 | if (nc) |
|---|
| 58 | { |
|---|
| 59 | if (_nestedCallback.valid()) |
|---|
| 60 | { |
|---|
| 61 | nc->addNestedCallback(_nestedCallback.get()); |
|---|
| 62 | _nestedCallback = nc; |
|---|
| 63 | } |
|---|
| 64 | else |
|---|
| 65 | { |
|---|
| 66 | _nestedCallback = nc; |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | inline void removeNestedCallback(NodeCallback* nc) |
|---|
| 72 | { |
|---|
| 73 | if (nc) |
|---|
| 74 | { |
|---|
| 75 | if (_nestedCallback==nc) |
|---|
| 76 | { |
|---|
| 77 | _nestedCallback = _nestedCallback->getNestedCallback(); |
|---|
| 78 | } |
|---|
| 79 | else if (_nestedCallback.valid()) |
|---|
| 80 | { |
|---|
| 81 | _nestedCallback->removeNestedCallback(nc); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | public: |
|---|
| 87 | |
|---|
| 88 | ref_ptr<NodeCallback> _nestedCallback; |
|---|
| 89 | |
|---|
| 90 | protected: |
|---|
| 91 | |
|---|
| 92 | virtual ~NodeCallback() {} |
|---|
| 93 | }; |
|---|
| 94 | |
|---|
| 95 | } // namespace |
|---|
| 96 | |
|---|
| 97 | #endif |
|---|
| 98 | |
|---|