// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "canvas_mgr.hxx"
-#include "canvas.hxx"
+#include <Canvas/FGCanvasSystemAdapter.hxx>
+#include <Cockpit/od_gauge.hxx>
#include <Main/fg_props.hxx>
+#include <Scripting/NasalModelData.hxx>
+#include <Viewer/CameraGroup.hxx>
-#include <osg/Camera>
-#include <osg/Texture2D>
+#include <simgear/canvas/Canvas.hxx>
-#include <stdexcept>
-#include <string>
+namespace sc = simgear::canvas;
//------------------------------------------------------------------------------
-CanvasMgr::CanvasMgr():
- _props( fgGetNode("/canvas", true) )
-{
-
-}
-
-//------------------------------------------------------------------------------
-CanvasMgr::~CanvasMgr()
-{
-
-}
-
-//------------------------------------------------------------------------------
-void CanvasMgr::init()
-{
- _props->addChangeListener(this);
- triggerChangeRecursive(_props);
-}
-
-//------------------------------------------------------------------------------
-void CanvasMgr::reinit()
-{
-
-}
-
-//------------------------------------------------------------------------------
-void CanvasMgr::shutdown()
-{
- _props->removeChangeListener(this);
-}
-
-//------------------------------------------------------------------------------
-void CanvasMgr::bind()
-{
-}
-
-//------------------------------------------------------------------------------
-void CanvasMgr::unbind()
-{
-}
-
-//------------------------------------------------------------------------------
-void CanvasMgr::update(double delta_time_sec)
+static sc::Placements addSceneObjectPlacement( SGPropertyNode* placement,
+ sc::CanvasPtr canvas )
{
- for( size_t i = 0; i < _canvases.size(); ++i )
- _canvases[i].update(delta_time_sec);
+ int module_id = placement->getIntValue("module-id", -1);
+ if( module_id < 0 )
+ return sc::Placements();
+
+ FGNasalModelData* model_data =
+ FGNasalModelData::getByModuleId( static_cast<unsigned int>(module_id) );
+
+ if( !model_data )
+ return sc::Placements();
+
+ if( !model_data->getNode() )
+ return sc::Placements();
+
+ return FGODGauge::set_texture
+ (
+ model_data->getNode(),
+ placement,
+ canvas->getTexture(),
+ canvas->getCullCallback()
+ );
}
//------------------------------------------------------------------------------
-void CanvasMgr::childAdded( SGPropertyNode * parent,
- SGPropertyNode * child )
+CanvasMgr::CanvasMgr():
+ simgear::canvas::CanvasMgr
+ (
+ fgGetNode("/canvas/by-index", true),
+ sc::SystemAdapterPtr( new canvas::FGCanvasSystemAdapter )
+ ),
+ _cb_model_reinit
+ (
+ this,
+ &CanvasMgr::handleModelReinit,
+ fgGetNode("/sim/signals/model-reinit", true)
+ )
{
- if( parent != _props )
- return;
-
- if( child->getNameString() == "texture" )
- textureAdded(child);
- else
- std::cout << "CanvasMgr::childAdded: " << child->getPath() << std::endl;
+ sc::Canvas::addPlacementFactory
+ (
+ "object",
+ boost::bind
+ (
+ &FGODGauge::set_aircraft_texture,
+ _1,
+ boost::bind(&sc::Canvas::getTexture, _2),
+ boost::bind(&sc::Canvas::getCullCallback, _2)
+ )
+ );
+
+ sc::Canvas::addPlacementFactory("scenery-object", &addSceneObjectPlacement);
}
//------------------------------------------------------------------------------
-void CanvasMgr::childRemoved( SGPropertyNode * parent,
- SGPropertyNode * child )
+unsigned int
+CanvasMgr::getCanvasTexId(const simgear::canvas::CanvasPtr& canvas) const
{
- if( parent != _props )
- return;
+ osg::Texture2D* tex = canvas->getTexture();
+ if( !tex )
+ return 0;
- std::cout << "CanvasMgr::childRemoved: " << child->getPath() << std::endl;
-}
+// osgViewer::Viewer::Contexts contexts;
+// globals->get_renderer()->getViewer()->getContexts(contexts);
+//
+// if( contexts.empty() )
+// return 0;
-//------------------------------------------------------------------------------
-void CanvasMgr::textureAdded(SGPropertyNode* node)
-{
- size_t index = node->getIndex();
-
- if( index >= _canvases.size() )
- {
- if( index > _canvases.size() )
- SG_LOG(SG_GL, SG_WARN, "Skipping unused texture slot(s)!");
- SG_LOG(SG_GL, SG_INFO, "Add new texture[" << index << "]");
-
- _canvases.resize(index + 1);
- _canvases[index];
- }
- else
- {
- SG_LOG(SG_GL, SG_WARN, "texture[" << index << "] already exists!");
- }
-
- _canvases[index].reset(node);
-}
+ static osg::Camera* guiCamera =
+ flightgear::getGUICamera(flightgear::CameraGroup::getDefault());
-//------------------------------------------------------------------------------
-void CanvasMgr::triggerChangeRecursive(SGPropertyNode* node)
-{
- node->getParent()->fireChildAdded(node);
+ osg::State* state = guiCamera->getGraphicsContext()->getState(); //contexts[0]->getState();
+ if( !state )
+ return 0;
- if( node->nChildren() == 0 && node->getType() != simgear::props::NONE )
- return node->fireValueChanged();
+ osg::Texture::TextureObject* tobj =
+ tex->getTextureObject( state->getContextID() );
+ if( !tobj )
+ return 0;
- for( int i = 0; i < node->nChildren(); ++i )
- triggerChangeRecursive( node->getChild(i) );
+ return tobj->_id;
}
-//------------------------------------------------------------------------------
-template<class T>
-T CanvasMgr::getParam(const SGPropertyNode* node, const char* prop)
+//----------------------------------------------------------------------------
+void CanvasMgr::handleModelReinit(SGPropertyNode*)
{
- const SGPropertyNode* child = node->getChild(prop);
- if( !child )
- throw std::runtime_error(std::string("Missing property ") + prop);
- return getValue<T>(child);
+ for(size_t i = 0; i < _elements.size(); ++i)
+ boost::static_pointer_cast<sc::Canvas>(_elements[i])
+ ->reloadPlacements("object");
}