1 // Canvas with 2D rendering api
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "canvas_mgr.hxx"
22 #include <Main/fg_props.hxx>
25 #include <osg/Texture2D>
30 //------------------------------------------------------------------------------
31 CanvasMgr::CanvasMgr():
32 _props( fgGetNode("/canvas", true) )
37 //------------------------------------------------------------------------------
38 CanvasMgr::~CanvasMgr()
43 //------------------------------------------------------------------------------
44 void CanvasMgr::init()
46 _props->addChangeListener(this);
47 triggerChangeRecursive(_props);
50 //------------------------------------------------------------------------------
51 void CanvasMgr::reinit()
56 //------------------------------------------------------------------------------
57 void CanvasMgr::shutdown()
59 _props->removeChangeListener(this);
62 //------------------------------------------------------------------------------
63 void CanvasMgr::bind()
67 //------------------------------------------------------------------------------
68 void CanvasMgr::unbind()
72 //------------------------------------------------------------------------------
73 void CanvasMgr::update(double delta_time_sec)
75 for( size_t i = 0; i < _canvases.size(); ++i )
76 _canvases[i].update(delta_time_sec);
79 //------------------------------------------------------------------------------
80 void CanvasMgr::childAdded( SGPropertyNode * parent,
81 SGPropertyNode * child )
83 if( parent != _props )
86 if( child->getNameString() == "texture" )
89 std::cout << "CanvasMgr::childAdded: " << child->getPath() << std::endl;
92 //------------------------------------------------------------------------------
93 void CanvasMgr::childRemoved( SGPropertyNode * parent,
94 SGPropertyNode * child )
96 if( parent != _props )
99 std::cout << "CanvasMgr::childRemoved: " << child->getPath() << std::endl;
102 //------------------------------------------------------------------------------
103 void CanvasMgr::textureAdded(SGPropertyNode* node)
105 size_t index = node->getIndex();
107 if( index >= _canvases.size() )
109 if( index > _canvases.size() )
110 SG_LOG(SG_GL, SG_WARN, "Skipping unused texture slot(s)!");
111 SG_LOG(SG_GL, SG_INFO, "Add new texture[" << index << "]");
113 _canvases.resize(index + 1);
118 SG_LOG(SG_GL, SG_WARN, "texture[" << index << "] already exists!");
121 _canvases[index].reset(node);
124 //------------------------------------------------------------------------------
125 void CanvasMgr::triggerChangeRecursive(SGPropertyNode* node)
127 node->getParent()->fireChildAdded(node);
129 if( node->nChildren() == 0 && node->getType() != simgear::props::NONE )
130 return node->fireValueChanged();
132 for( int i = 0; i < node->nChildren(); ++i )
133 triggerChangeRecursive( node->getChild(i) );
136 //------------------------------------------------------------------------------
138 T CanvasMgr::getParam(const SGPropertyNode* node, const char* prop)
140 const SGPropertyNode* child = node->getChild(prop);
142 throw std::runtime_error(std::string("Missing property ") + prop);
143 return getValue<T>(child);