]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/canvas_mgr.cxx
Basic 2D canvas implementation.
[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    _canvases[i].update(delta_time_sec);
77 }
78
79 //------------------------------------------------------------------------------
80 void CanvasMgr::childAdded( SGPropertyNode * parent,
81                             SGPropertyNode * child )
82 {
83   if( parent != _props )
84     return;
85
86   if( child->getNameString() == "texture" )
87     textureAdded(child);
88   else
89     std::cout << "CanvasMgr::childAdded: " << child->getPath() << std::endl;
90 }
91
92 //------------------------------------------------------------------------------
93 void CanvasMgr::childRemoved( SGPropertyNode * parent,
94                               SGPropertyNode * child )
95 {
96   if( parent != _props )
97     return;
98
99   std::cout << "CanvasMgr::childRemoved: " << child->getPath() << std::endl;
100 }
101
102 //------------------------------------------------------------------------------
103 void CanvasMgr::textureAdded(SGPropertyNode* node)
104 {
105   size_t index = node->getIndex();
106
107   if( index >= _canvases.size() )
108   {
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 << "]");
112
113     _canvases.resize(index + 1);
114     _canvases[index];
115   }
116   else
117   {
118     SG_LOG(SG_GL, SG_WARN, "texture[" << index << "] already exists!");
119   }
120
121   _canvases[index].reset(node);
122 }
123
124 //------------------------------------------------------------------------------
125 void CanvasMgr::triggerChangeRecursive(SGPropertyNode* node)
126 {
127   node->getParent()->fireChildAdded(node);
128
129   if( node->nChildren() == 0 && node->getType() != simgear::props::NONE )
130     return node->fireValueChanged();
131
132   for( int i = 0; i < node->nChildren(); ++i )
133     triggerChangeRecursive( node->getChild(i) );
134 }
135
136 //------------------------------------------------------------------------------
137 template<class T>
138 T CanvasMgr::getParam(const SGPropertyNode* node, const char* prop)
139 {
140   const SGPropertyNode* child = node->getChild(prop);
141   if( !child )
142     throw std::runtime_error(std::string("Missing property ") + prop);
143   return getValue<T>(child);
144 }