]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/canvas_mgr.cxx
Canvas/GUI: add/remove placement factories on init/shutdown.
[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
73 }
74
75 //----------------------------------------------------------------------------
76 void CanvasMgr::init()
77 {
78   sc::Canvas::addPlacementFactory
79   (
80     "object",
81     boost::bind
82     (
83       &FGODGauge::set_aircraft_texture,
84       _1,
85       boost::bind(&sc::Canvas::getTexture, _2),
86       boost::bind(&sc::Canvas::getCullCallback, _2),
87       _2
88     )
89   );
90   sc::Canvas::addPlacementFactory("scenery-object", &addSceneObjectPlacement);
91
92   simgear::canvas::CanvasMgr::init();
93 }
94
95 //----------------------------------------------------------------------------
96 void CanvasMgr::shutdown()
97 {
98   simgear::canvas::CanvasMgr::shutdown();
99
100   sc::Canvas::removePlacementFactory("object");
101   sc::Canvas::removePlacementFactory("scenery-object");
102 }
103
104 //------------------------------------------------------------------------------
105 unsigned int
106 CanvasMgr::getCanvasTexId(const simgear::canvas::CanvasPtr& canvas) const
107 {
108   osg::Texture2D* tex = canvas->getTexture();
109   if( !tex )
110     return 0;
111
112 //  osgViewer::Viewer::Contexts contexts;
113 //  globals->get_renderer()->getViewer()->getContexts(contexts);
114 //
115 //  if( contexts.empty() )
116 //    return 0;
117
118   static osg::Camera* guiCamera =
119     flightgear::getGUICamera(flightgear::CameraGroup::getDefault());
120
121   osg::State* state = guiCamera->getGraphicsContext()->getState(); //contexts[0]->getState();
122   if( !state )
123     return 0;
124
125   osg::Texture::TextureObject* tobj =
126     tex->getTextureObject( state->getContextID() );
127   if( !tobj )
128     return 0;
129
130   return tobj->_id;
131 }
132
133 //----------------------------------------------------------------------------
134 void CanvasMgr::handleModelReinit(SGPropertyNode*)
135 {
136   for(size_t i = 0; i < _elements.size(); ++i)
137     boost::static_pointer_cast<sc::Canvas>(_elements[i])
138       ->reloadPlacements("object");
139 }