]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/canvas_mgr.cxx
b8300654a669ee18275ca552a931b988b7a573bf
[flightgear.git] / src / Canvas / canvas_mgr.cxx
1 // Canvas with 2D rendering api
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
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.
9 //
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.
14 //
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.
18
19 #include "canvas_mgr.hxx"
20 #include "canvas.hxx"
21
22 #include <Main/fg_props.hxx>
23
24 #include <osg/Camera>
25 #include <osg/Texture2D>
26
27 #include <stdexcept>
28 #include <string>
29
30 //------------------------------------------------------------------------------
31 CanvasMgr::CanvasMgr():
32   _props( fgGetNode("/canvas", true) )
33 {
34
35 }
36
37 //------------------------------------------------------------------------------
38 CanvasMgr::~CanvasMgr()
39 {
40
41 }
42
43 //------------------------------------------------------------------------------
44 void CanvasMgr::init()
45 {
46   _props->addChangeListener(this);
47   triggerChangeRecursive(_props);
48 }
49
50 //------------------------------------------------------------------------------
51 void CanvasMgr::reinit()
52 {
53
54 }
55
56 //------------------------------------------------------------------------------
57 void CanvasMgr::shutdown()
58 {
59   _props->removeChangeListener(this);
60 }
61
62 //------------------------------------------------------------------------------
63 void CanvasMgr::bind()
64 {
65 }
66
67 //------------------------------------------------------------------------------
68 void CanvasMgr::unbind()
69 {
70 }
71
72 //------------------------------------------------------------------------------
73 void CanvasMgr::update(double delta_time_sec)
74 {
75  for( size_t i = 0; i < _canvases.size(); ++i )
76    if( _canvases[i] )
77      _canvases[i]->update(delta_time_sec);
78 }
79
80 //------------------------------------------------------------------------------
81 void CanvasMgr::childAdded( SGPropertyNode * parent,
82                             SGPropertyNode * child )
83 {
84   if( parent != _props )
85     return;
86
87   if( child->getNameString() == "texture" )
88     textureAdded(child);
89 }
90
91 //------------------------------------------------------------------------------
92 void CanvasMgr::childRemoved( SGPropertyNode * parent,
93                               SGPropertyNode * child )
94 {
95   if( parent != _props )
96     return;
97
98   if( child->getNameString() == "texture" )
99   {
100     size_t index = child->getIndex();
101
102     if( index >= _canvases.size() )
103       SG_LOG(SG_GL, SG_WARN, "can't removed unknown texture[" << index << "]!");
104     else
105       // remove the canvas...
106       _canvases[index].reset();
107   }
108 }
109
110 //------------------------------------------------------------------------------
111 unsigned int CanvasMgr::getCanvasTexId(size_t index) const
112 {
113   if(    index >= _canvases.size()
114       || !_canvases[index] )
115     return 0;
116
117   return _canvases[index]->getTexId();
118 }
119
120 //------------------------------------------------------------------------------
121 void CanvasMgr::textureAdded(SGPropertyNode* node)
122 {
123   size_t index = node->getIndex();
124
125   if( index >= _canvases.size() )
126   {
127     if( index > _canvases.size() )
128       SG_LOG(SG_GL, SG_WARN, "Skipping unused texture slot(s)!");
129
130     _canvases.resize(index + 1);
131   }
132   else if( _canvases[index] )
133     SG_LOG(SG_GL, SG_WARN, "texture[" << index << "] already exists!");
134
135   _canvases[index].reset( new Canvas() );
136   _canvases[index]->reset(node);
137 }
138
139 //------------------------------------------------------------------------------
140 void CanvasMgr::triggerChangeRecursive(SGPropertyNode* node)
141 {
142   node->getParent()->fireChildAdded(node);
143
144   if( node->nChildren() == 0 && node->getType() != simgear::props::NONE )
145     return node->fireValueChanged();
146
147   for( int i = 0; i < node->nChildren(); ++i )
148     triggerChangeRecursive( node->getChild(i) );
149 }