]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/canvas_mgr.cxx
Bug 1122, transponder ident.
[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
21 #include <Canvas/FGCanvasSystemAdapter.hxx>
22 #include <Cockpit/od_gauge.hxx>
23 #include <Main/fg_props.hxx>
24 #include <Scripting/NasalModelData.hxx>
25 #include <Viewer/CameraGroup.hxx>
26
27 #include <simgear/canvas/Canvas.hxx>
28
29 namespace sc = simgear::canvas;
30
31 //------------------------------------------------------------------------------
32 static sc::Placements addSceneObjectPlacement( SGPropertyNode* placement,
33                                                sc::CanvasPtr canvas )
34 {
35   int module_id = placement->getIntValue("module-id", -1);
36   if( module_id < 0 )
37     return sc::Placements();
38
39   FGNasalModelData* model_data =
40     FGNasalModelData::getByModuleId( static_cast<unsigned int>(module_id) );
41
42   if( !model_data )
43     return sc::Placements();
44
45   if( !model_data->getNode() )
46     return sc::Placements();
47
48   return FGODGauge::set_texture
49   (
50     model_data->getNode(),
51     placement,
52     canvas->getTexture(),
53     canvas->getCullCallback(),
54     canvas
55   );
56 }
57
58 //------------------------------------------------------------------------------
59 CanvasMgr::CanvasMgr():
60   simgear::canvas::CanvasMgr
61   (
62    fgGetNode("/canvas/by-index", true),
63    sc::SystemAdapterPtr( new canvas::FGCanvasSystemAdapter )
64   ),
65   _cb_model_reinit
66   (
67     this,
68     &CanvasMgr::handleModelReinit,
69     fgGetNode("/sim/signals/model-reinit", true)
70   )
71 {
72   sc::Canvas::addPlacementFactory
73   (
74     "object",
75     boost::bind
76     (
77       &FGODGauge::set_aircraft_texture,
78       _1,
79       boost::bind(&sc::Canvas::getTexture, _2),
80       boost::bind(&sc::Canvas::getCullCallback, _2),
81       _2
82     )
83   );
84
85   sc::Canvas::addPlacementFactory("scenery-object", &addSceneObjectPlacement);
86 }
87
88 //------------------------------------------------------------------------------
89 unsigned int
90 CanvasMgr::getCanvasTexId(const simgear::canvas::CanvasPtr& canvas) const
91 {
92   osg::Texture2D* tex = canvas->getTexture();
93   if( !tex )
94     return 0;
95
96 //  osgViewer::Viewer::Contexts contexts;
97 //  globals->get_renderer()->getViewer()->getContexts(contexts);
98 //
99 //  if( contexts.empty() )
100 //    return 0;
101
102   static osg::Camera* guiCamera =
103     flightgear::getGUICamera(flightgear::CameraGroup::getDefault());
104
105   osg::State* state = guiCamera->getGraphicsContext()->getState(); //contexts[0]->getState();
106   if( !state )
107     return 0;
108
109   osg::Texture::TextureObject* tobj =
110     tex->getTextureObject( state->getContextID() );
111   if( !tobj )
112     return 0;
113
114   return tobj->_id;
115 }
116
117 //----------------------------------------------------------------------------
118 void CanvasMgr::handleModelReinit(SGPropertyNode*)
119 {
120   for(size_t i = 0; i < _elements.size(); ++i)
121     boost::static_pointer_cast<sc::Canvas>(_elements[i])
122       ->reloadPlacements("object");
123 }