]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/canvas_mgr.cxx
Allow placing canvas on scenery objects.
[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   );
55 }
56
57 //------------------------------------------------------------------------------
58 CanvasMgr::CanvasMgr():
59   simgear::canvas::CanvasMgr
60   (
61    fgGetNode("/canvas/by-index", true),
62    sc::SystemAdapterPtr( new canvas::FGCanvasSystemAdapter )
63   ),
64   _cb_model_reinit
65   (
66     this,
67     &CanvasMgr::handleModelReinit,
68     fgGetNode("/sim/signals/model-reinit", true)
69   )
70 {
71   sc::Canvas::addPlacementFactory
72   (
73     "object",
74     boost::bind
75     (
76       &FGODGauge::set_aircraft_texture,
77       _1,
78       boost::bind(&sc::Canvas::getTexture, _2),
79       boost::bind(&sc::Canvas::getCullCallback, _2)
80     )
81   );
82
83   sc::Canvas::addPlacementFactory("scenery-object", &addSceneObjectPlacement);
84 }
85
86 //------------------------------------------------------------------------------
87 unsigned int
88 CanvasMgr::getCanvasTexId(const simgear::canvas::CanvasPtr& canvas) const
89 {
90   osg::Texture2D* tex = canvas->getTexture();
91   if( !tex )
92     return 0;
93
94 //  osgViewer::Viewer::Contexts contexts;
95 //  globals->get_renderer()->getViewer()->getContexts(contexts);
96 //
97 //  if( contexts.empty() )
98 //    return 0;
99
100   static osg::Camera* guiCamera =
101     flightgear::getGUICamera(flightgear::CameraGroup::getDefault());
102
103   osg::State* state = guiCamera->getGraphicsContext()->getState(); //contexts[0]->getState();
104   if( !state )
105     return 0;
106
107   osg::Texture::TextureObject* tobj =
108     tex->getTextureObject( state->getContextID() );
109   if( !tobj )
110     return 0;
111
112   return tobj->_id;
113 }
114
115 //----------------------------------------------------------------------------
116 void CanvasMgr::handleModelReinit(SGPropertyNode*)
117 {
118   for(size_t i = 0; i < _elements.size(); ++i)
119     boost::static_pointer_cast<sc::Canvas>(_elements[i])
120       ->reloadPlacements("object");
121 }