]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/canvas_mgr.cxx
72db55fd0dea11c6f4d6fff7f3818e14a6909baa
[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 void CanvasMgr::textureAdded(SGPropertyNode* node)
112 {
113   size_t index = node->getIndex();
114
115   if( index >= _canvases.size() )
116   {
117     if( index > _canvases.size() )
118       SG_LOG(SG_GL, SG_WARN, "Skipping unused texture slot(s)!");
119
120     _canvases.resize(index + 1);
121   }
122   else
123   {
124     SG_LOG(SG_GL, SG_WARN, "texture[" << index << "] already exists!");
125   }
126
127   _canvases[index].reset( new Canvas() );
128   _canvases[index]->reset(node);
129 }
130
131 //------------------------------------------------------------------------------
132 void CanvasMgr::triggerChangeRecursive(SGPropertyNode* node)
133 {
134   node->getParent()->fireChildAdded(node);
135
136   if( node->nChildren() == 0 && node->getType() != simgear::props::NONE )
137     return node->fireValueChanged();
138
139   for( int i = 0; i < node->nChildren(); ++i )
140     triggerChangeRecursive( node->getChild(i) );
141 }