root/OpenSceneGraph/trunk/src/osg/CopyOp.cpp @ 10671

Revision 10671, 3.1 kB (checked in by robert, 4 years ago)

From Cedric Pinson, "Here a patch to be able to clone stateattribute, in order to do that i
moved the StateAttribute::Callback structure to a file
StateAttributeCallback? with the same behavior as NodeCallback?.
"

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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#include <osg/CopyOp>
14#include <osg/Node>
15#include <osg/StateSet>
16#include <osg/Texture>
17#include <osg/Drawable>
18#include <osg/Array>
19#include <osg/PrimitiveSet>
20#include <osg/Shape>
21#include <osg/StateAttribute>
22
23using namespace osg;
24
25#define COPY_OP( TYPE, FLAG ) \
26TYPE* CopyOp::operator() (const TYPE* obj) const \
27{ \
28    if (obj && _flags&FLAG) \
29        return dynamic_cast<TYPE*>( obj->clone(*this) ); \
30    else \
31        return const_cast<TYPE*>(obj); \
32}
33
34COPY_OP( Object,         DEEP_COPY_OBJECTS )
35COPY_OP( Node,           DEEP_COPY_NODES )
36COPY_OP( Drawable,       DEEP_COPY_DRAWABLES )
37COPY_OP( StateSet,       DEEP_COPY_STATESETS )
38COPY_OP( Texture,        DEEP_COPY_TEXTURES )
39COPY_OP( Image,          DEEP_COPY_IMAGES )
40COPY_OP( Array,          DEEP_COPY_ARRAYS )
41COPY_OP( PrimitiveSet,   DEEP_COPY_PRIMITIVES )
42COPY_OP( Shape,          DEEP_COPY_SHAPES )
43COPY_OP( Uniform,        DEEP_COPY_UNIFORMS )
44
45Referenced* CopyOp::operator() (const Referenced* ref) const
46{
47    return const_cast<Referenced*>(ref);
48}
49
50StateAttribute* CopyOp::operator() (const StateAttribute* attr) const
51{
52    if (attr && _flags&DEEP_COPY_STATEATTRIBUTES)
53    {
54        const Texture* textbase = dynamic_cast<const Texture*>(attr);
55        if (textbase)
56        {
57            return operator()(textbase);
58        }
59        else 
60        {
61            return dynamic_cast<StateAttribute*>(attr->clone(*this));
62        }
63    }
64    else
65        return const_cast<StateAttribute*>(attr);
66}
67
68
69NodeCallback* CopyOp::operator() (const NodeCallback* nc) const
70{
71    if (nc && _flags&DEEP_COPY_CALLBACKS)
72    {
73        // deep copy the full chain of callback
74        osg::NodeCallback* first = dynamic_cast<osg::NodeCallback*>(nc->clone(*this));
75        first->setNestedCallback(0);
76        nc = nc->getNestedCallback();
77        while (nc)
78        {
79            osg::NodeCallback* ucb = dynamic_cast<osg::NodeCallback*>(nc->clone(*this));
80            ucb->setNestedCallback(0);
81            first->addNestedCallback(ucb);
82            nc = nc->getNestedCallback();
83        }
84        return first;
85    }
86    else
87        return const_cast<NodeCallback*>(nc);
88}
89
90
91StateAttributeCallback* CopyOp::operator() (const StateAttributeCallback* sc) const
92{
93    if (sc && _flags&DEEP_COPY_CALLBACKS)
94    {
95        // deep copy the full chain of callback
96        StateAttributeCallback* cb = dynamic_cast<StateAttributeCallback*>(sc->clone(*this));
97        return cb;
98    }
99    else
100        return const_cast<StateAttributeCallback*>(sc);
101}
Note: See TracBrowser for help on using the browser.