1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2  *
3  * This application is open source and may be redistributed and/or modified
4  * freely and without restriction, both in commercial and non commercial
5  * applications, as long as this copyright notice is maintained.
6  *
7  * This application is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 #include <osg/StateAttribute>
13 #include <osg/StateSet>
14 #include <osg/State>
15 #include <osg/NodeVisitor>
16 #include <osg/Notify>
17 
18 #include <algorithm>
19 
20 using namespace osg;
21 
StateAttribute()22 StateAttribute::StateAttribute()
23     :Object(true)
24 {
25 }
26 
27 
addParent(osg::StateSet * object)28 void StateAttribute::addParent(osg::StateSet* object)
29 {
30     OSG_DEBUG_FP<<"Adding parent"<<getRefMutex()<<std::endl;
31     OpenThreads::ScopedPointerLock<OpenThreads::Mutex> lock(getRefMutex());
32 
33     _parents.push_back(object);
34 }
35 
removeParent(osg::StateSet * object)36 void StateAttribute::removeParent(osg::StateSet* object)
37 {
38     OpenThreads::ScopedPointerLock<OpenThreads::Mutex> lock(getRefMutex());
39 
40     ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),object);
41     if (pitr!=_parents.end()) _parents.erase(pitr);
42 }
43 
44 
setUpdateCallback(StateAttributeCallback * uc)45 void StateAttribute::setUpdateCallback(StateAttributeCallback* uc)
46 {
47     OSG_DEBUG<<"StateAttribute::Setting Update callbacks"<<std::endl;
48 
49     if (_updateCallback==uc) return;
50 
51     int delta = 0;
52     if (_updateCallback.valid()) --delta;
53     if (uc) ++delta;
54 
55     _updateCallback = uc;
56 
57     if (delta!=0)
58     {
59         for(ParentList::iterator itr=_parents.begin();
60             itr!=_parents.end();
61             ++itr)
62         {
63             (*itr)->setNumChildrenRequiringUpdateTraversal((*itr)->getNumChildrenRequiringUpdateTraversal()+delta);
64         }
65     }
66 }
67 
setEventCallback(StateAttributeCallback * ec)68 void StateAttribute::setEventCallback(StateAttributeCallback* ec)
69 {
70     OSG_DEBUG<<"StateAttribute::Setting Event callbacks"<<std::endl;
71 
72     if (_eventCallback==ec) return;
73 
74     int delta = 0;
75     if (_eventCallback.valid()) --delta;
76     if (ec) ++delta;
77 
78     _eventCallback = ec;
79 
80     if (delta!=0)
81     {
82         for(ParentList::iterator itr=_parents.begin();
83             itr!=_parents.end();
84             ++itr)
85         {
86             (*itr)->setNumChildrenRequiringEventTraversal((*itr)->getNumChildrenRequiringEventTraversal()+delta);
87         }
88     }
89 }
90