]> git.mxchange.org Git - flightgear.git/blobdiff - src/Canvas/canvas_mgr.cxx
Canvas: generate keypress event for text input.
[flightgear.git] / src / Canvas / canvas_mgr.cxx
index 72db55fd0dea11c6f4d6fff7f3818e14a6909baa..6646b7762d42b86e39bc22fe15a5eb4010b97407 100644 (file)
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 #include "canvas_mgr.hxx"
-#include "canvas.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) )
+static sc::Placements addSceneObjectPlacement( SGPropertyNode* placement,
+                                               sc::CanvasPtr canvas )
 {
-
+  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(),
+    canvas
+  );
 }
 
 //------------------------------------------------------------------------------
-CanvasMgr::~CanvasMgr()
+CanvasMgr::CanvasMgr():
+  simgear::canvas::CanvasMgr( fgGetNode("/canvas/by-index", true) ),
+  _cb_model_reinit
+  (
+    this,
+    &CanvasMgr::handleModelReinit,
+    fgGetNode("/sim/signals/model-reinit", true)
+  )
 {
 
 }
 
-//------------------------------------------------------------------------------
+//----------------------------------------------------------------------------
 void CanvasMgr::init()
 {
-  _props->addChangeListener(this);
-  triggerChangeRecursive(_props);
+  _gui_camera = flightgear::getGUICamera(flightgear::CameraGroup::getDefault());
+  assert(_gui_camera.valid());
+
+  sc::Canvas::addPlacementFactory
+  (
+    "object",
+    boost::bind
+    (
+      &FGODGauge::set_aircraft_texture,
+      _1,
+      boost::bind(&sc::Canvas::getTexture, _2),
+      boost::bind(&sc::Canvas::getCullCallback, _2),
+      _2
+    )
+  );
+  sc::Canvas::addPlacementFactory("scenery-object", &addSceneObjectPlacement);
+
+  simgear::canvas::CanvasMgr::init();
 }
 
-//------------------------------------------------------------------------------
-void CanvasMgr::reinit()
+//----------------------------------------------------------------------------
+void CanvasMgr::shutdown()
 {
+  simgear::canvas::CanvasMgr::shutdown();
 
-}
+  sc::Canvas::removePlacementFactory("object");
+  sc::Canvas::removePlacementFactory("scenery-object");
 
-//------------------------------------------------------------------------------
-void CanvasMgr::shutdown()
-{
-  _props->removeChangeListener(this);
+  _gui_camera = 0;
 }
 
 //------------------------------------------------------------------------------
-void CanvasMgr::bind()
+unsigned int
+CanvasMgr::getCanvasTexId(const simgear::canvas::CanvasPtr& canvas) const
 {
-}
+  if( !canvas )
+    return 0;
 
-//------------------------------------------------------------------------------
-void CanvasMgr::unbind()
-{
-}
+  osg::Texture2D* tex = canvas->getTexture();
+  if( !tex )
+    return 0;
 
-//------------------------------------------------------------------------------
-void CanvasMgr::update(double delta_time_sec)
-{
- for( size_t i = 0; i < _canvases.size(); ++i )
-   if( _canvases[i] )
-     _canvases[i]->update(delta_time_sec);
-}
+//  osgViewer::Viewer::Contexts contexts;
+//  globals->get_renderer()->getViewer()->getContexts(contexts);
+//
+//  if( contexts.empty() )
+//    return 0;
 
-//------------------------------------------------------------------------------
-void CanvasMgr::childAdded( SGPropertyNode * parent,
-                            SGPropertyNode * child )
-{
-  if( parent != _props )
-    return;
+  osg::ref_ptr<osg::Camera> guiCamera;
+  if( !_gui_camera.lock(guiCamera) )
+    return 0;
 
-  if( child->getNameString() == "texture" )
-    textureAdded(child);
-}
+  osg::State* state = guiCamera->getGraphicsContext()->getState(); //contexts[0]->getState();
+  if( !state )
+    return 0;
 
-//------------------------------------------------------------------------------
-void CanvasMgr::childRemoved( SGPropertyNode * parent,
-                              SGPropertyNode * child )
-{
-  if( parent != _props )
-    return;
-
-  if( child->getNameString() == "texture" )
-  {
-    size_t index = child->getIndex();
-
-    if( index >= _canvases.size() )
-      SG_LOG(SG_GL, SG_WARN, "can't removed unknown texture[" << index << "]!");
-    else
-      // remove the canvas...
-      _canvases[index].reset();
-  }
-}
+  osg::Texture::TextureObject* tobj =
+    tex->getTextureObject( state->getContextID() );
+  if( !tobj )
+    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)!");
-
-    _canvases.resize(index + 1);
-  }
-  else
-  {
-    SG_LOG(SG_GL, SG_WARN, "texture[" << index << "] already exists!");
-  }
-
-  _canvases[index].reset( new Canvas() );
-  _canvases[index]->reset(node);
+  return tobj->_id;
 }
 
-//------------------------------------------------------------------------------
-void CanvasMgr::triggerChangeRecursive(SGPropertyNode* node)
+//----------------------------------------------------------------------------
+void CanvasMgr::handleModelReinit(SGPropertyNode*)
 {
-  node->getParent()->fireChildAdded(node);
-
-  if( node->nChildren() == 0 && node->getType() != simgear::props::NONE )
-    return node->fireValueChanged();
-
-  for( int i = 0; i < node->nChildren(); ++i )
-    triggerChangeRecursive( node->getChild(i) );
+  for(size_t i = 0; i < _elements.size(); ++i)
+    static_cast<sc::Canvas*>(_elements[i].get())->reloadPlacements("object");
 }