00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <osgText/Text>
00026 #include <osg/LineWidth>
00027
00028 #include "../../WGraphicsEngine.h"
00029
00030 #include "WGEBorderLayout.h"
00031
00032 WGEBorderLayout::WGEBorderLayout():
00033 WGEGroupNode(),
00034 m_geode( new osg::Geode() ),
00035 m_lineGeode( new osg::Geode() ),
00036 m_screen( new osg::Projection() )
00037
00038 {
00039
00040 osg::ref_ptr< osg::MatrixTransform > matrix = new osg::MatrixTransform();
00041 setDataVariance( osg::Object::DYNAMIC );
00042 matrix->setMatrix( osg::Matrix::identity() );
00043 matrix->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
00044 matrix->addChild( m_geode );
00045 matrix->addChild( m_lineGeode );
00046 m_screen->addChild( matrix );
00047 insert( m_screen );
00048
00049 m_geode->setDataVariance( osg::Object::DYNAMIC );
00050 m_lineGeode->setDataVariance( osg::Object::DYNAMIC );
00051 m_screen->setDataVariance( osg::Object::DYNAMIC );
00052
00053 addUpdateCallback( new SafeUpdateCallback( this ) );
00054
00055
00056 getOrCreateStateSet()->setRenderBinDetails( 11, "RenderBin" );
00057 getOrCreateStateSet()->setDataVariance( osg::Object::DYNAMIC );
00058 getOrCreateStateSet()->setMode( GL_DEPTH_TEST, osg::StateAttribute::OFF );
00059 getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
00060 }
00061
00062 WGEBorderLayout::~WGEBorderLayout()
00063 {
00064
00065 }
00066
00067 void WGEBorderLayout::addLayoutable( osg::ref_ptr< WGELabel > obj )
00068 {
00069 m_geode->addDrawable( obj );
00070 }
00071
00072 void WGEBorderLayout::SafeUpdateCallback::operator()( osg::Node* node, osg::NodeVisitor* nv )
00073 {
00074 osg::ref_ptr<osg::Camera> cam = WGraphicsEngine::getGraphicsEngine()->getViewer()->getCamera();
00075
00076
00077 unsigned int vwidth = cam->getViewport()->width();
00078 unsigned int vheight = cam->getViewport()->height();
00079 m_layouter->m_screen->setMatrix( osg::Matrix::ortho2D( 0, vwidth, 0, vheight ) );
00080
00081
00082 osg::ref_ptr< osg::Vec3Array > v = new osg::Vec3Array;
00083
00084 for( unsigned int i = 0; i < m_layouter->m_geode->getNumDrawables(); ++i )
00085 {
00086
00087 osg::ref_ptr< WGELabel > t = static_cast< WGELabel* >( m_layouter->m_geode->getDrawable( i ) );
00088
00089
00090 osg::Vec4 anchor = osg::Vec4( t->getAnchor(), 1.0 );
00091 osg::Matrixd projection = cam->getProjectionMatrix();
00092 osg::Matrixd view = cam->getViewMatrix();
00093 osg::Matrixd window = cam->getViewport()->computeWindowMatrix();
00094 osg::Vec4 anchorScreen = anchor * view * projection * window;
00095
00096
00097
00098
00099
00100 osg::Vec3 leadPos;
00101
00102 if( anchorScreen.x() >= vwidth / 2 )
00103 {
00104 leadPos = osg::Vec3( vwidth - 10.0, anchorScreen.y(), 0.0 );
00105
00106 t->setPosition( osg::Vec3( vwidth - 10.0, anchorScreen.y() + 5, 0.0 ) );
00107 t->setAlignment( osgText::Text::RIGHT_BOTTOM );
00108 }
00109 else
00110 {
00111 leadPos = osg::Vec3( 10.0, anchorScreen.y(), 0.0 );
00112
00113 t->setPosition( osg::Vec3( 10.0, anchorScreen.y() + 5, 0.0 ) );
00114 t->setAlignment( osgText::Text::LEFT_BOTTOM );
00115 }
00116
00117 v->push_back( leadPos );
00118 v->push_back( osg::Vec3( anchorScreen.x(), anchorScreen.y(), anchorScreen.z() ) );
00119 }
00120
00121
00122 osg::ref_ptr< osg::Geometry > g = new osg::Geometry;
00123 g->setDataVariance( osg::Object::DYNAMIC );
00124 osg::ref_ptr< osg::DrawArrays > da = new osg::DrawArrays( osg::PrimitiveSet::LINES, 0, v->size() );
00125 g->setVertexArray( v );
00126 osg::ref_ptr< osg::Vec4Array > colors = new osg::Vec4Array;
00127 colors->push_back( osg::Vec4( 0.0f, 0.0f, 0.0f, 1.0f ) );
00128 g->setColorArray( colors );
00129 g->setColorBinding( osg::Geometry::BIND_OVERALL );
00130 g->addPrimitiveSet( da );
00131
00132 osg::LineWidth* linewidth = new osg::LineWidth();
00133 linewidth->setWidth( 2.0f );
00134 g->getOrCreateStateSet()->setAttributeAndModes( linewidth, osg::StateAttribute::ON );
00135
00136
00137 m_layouter->m_lineGeode->removeDrawables( 0, m_layouter->m_lineGeode->getNumDrawables() );
00138 m_layouter->m_lineGeode->addDrawable( g );
00139
00140 traverse( node, nv );
00141 }
00142