Added help support for display help on screen to osgProducer::Viewer.
This commit is contained in:
@@ -45,7 +45,7 @@ void ApplicationUsage::addKeyboardMouseBinding(const std::string& option,const s
|
||||
_keyboardMouse[option]=explanation;
|
||||
}
|
||||
|
||||
void ApplicationUsage::write(std::ostream& output, const ApplicationUsage::UsageMap& um,unsigned int widthOfOutput)
|
||||
void ApplicationUsage::getFormatedString(std::string& str, const UsageMap& um,unsigned int widthOfOutput)
|
||||
{
|
||||
unsigned int maxNumCharsInOptions = 0;
|
||||
ApplicationUsage::UsageMap::const_iterator citr;
|
||||
@@ -125,9 +125,8 @@ void ApplicationUsage::write(std::ostream& output, const ApplicationUsage::Usage
|
||||
}
|
||||
|
||||
line.replace(explanationPos+offset,explanationWidth, explanation, pos, width);
|
||||
if (concatinated) output << line << '-' << std::endl;
|
||||
else output << line << std::endl;
|
||||
|
||||
if (concatinated) { str += line; str += "-\n"; }
|
||||
else { str += line; str += "\n"; }
|
||||
|
||||
// move to the next line of output.
|
||||
line.assign(fullWidth,' ');
|
||||
@@ -139,6 +138,13 @@ void ApplicationUsage::write(std::ostream& output, const ApplicationUsage::Usage
|
||||
}
|
||||
}
|
||||
|
||||
void ApplicationUsage::write(std::ostream& output, const ApplicationUsage::UsageMap& um,unsigned int widthOfOutput)
|
||||
{
|
||||
std::string str;
|
||||
getFormatedString(str, um, widthOfOutput);
|
||||
output << str << std::endl;
|
||||
}
|
||||
|
||||
void ApplicationUsage::write(std::ostream& output,unsigned int widthOfOutput)
|
||||
{
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ CXXFILES =\
|
||||
ViewerEventHandler.cpp\
|
||||
Viewer.cpp\
|
||||
|
||||
LIBS += -lProducer $(GL_LIBS) -losgGA -losgUtil -losgDB -losg $(OTHER_LIBS)
|
||||
LIBS += -lProducer $(GL_LIBS) -losgText -losgGA -losgUtil -losgDB -losg $(OTHER_LIBS)
|
||||
DEF += -DOSGPRODUCER_LIBRARY
|
||||
INC += -I/usr/X11R6/include
|
||||
TARGET_BASENAME = osgProducer
|
||||
|
||||
@@ -33,13 +33,9 @@ public:
|
||||
|
||||
virtual void operator()( const Producer::RenderSurface & rs)
|
||||
{
|
||||
if (_cameraGroup)
|
||||
if (_cameraGroup->getRealizeCallback())
|
||||
{
|
||||
if (_cameraGroup->getRealizeCallback())
|
||||
{
|
||||
(*(_cameraGroup->getRealizeCallback()))(rs,_cameraGroup,_sceneHandler);
|
||||
}
|
||||
else if (_sceneHandler) _sceneHandler->init();
|
||||
(*(_cameraGroup->getRealizeCallback()))(*_cameraGroup,*_sceneHandler,rs);
|
||||
}
|
||||
else if (_sceneHandler) _sceneHandler->init();
|
||||
}
|
||||
|
||||
@@ -38,12 +38,12 @@ void OsgSceneHandler::init()
|
||||
osg::notify(osg::INFO)<<" unlocked "<<this<<" init."<<std::endl;
|
||||
}
|
||||
|
||||
void OsgSceneHandler::clear(Producer::Camera& /*camera*/)
|
||||
void OsgSceneHandler::clearImplementation(Producer::Camera& /*camera*/)
|
||||
{
|
||||
// no-op right now as scene view manages its own cleaer.
|
||||
// no-op right now as scene view manages its own clear.
|
||||
}
|
||||
|
||||
void OsgSceneHandler::cull(Producer::Camera &cam)
|
||||
void OsgSceneHandler::cullImplementation(Producer::Camera &cam)
|
||||
{
|
||||
|
||||
pm->set(cam.getProjectionMatrix());
|
||||
@@ -60,7 +60,7 @@ void OsgSceneHandler::cull(Producer::Camera &cam)
|
||||
SceneView::cull();
|
||||
}
|
||||
|
||||
void OsgSceneHandler::draw(Producer::Camera &)
|
||||
void OsgSceneHandler::drawImplementation(Producer::Camera &)
|
||||
{
|
||||
SceneView::draw();
|
||||
}
|
||||
|
||||
@@ -1,12 +1,256 @@
|
||||
#include <osgProducer/ViewerEventHandler>
|
||||
#include <osgDB/WriteFile>
|
||||
#include <osgText/Text>
|
||||
|
||||
using namespace osgProducer;
|
||||
|
||||
ViewerEventHandler::ViewerEventHandler(osgProducer::OsgCameraGroup* cg):
|
||||
_cg(cg),
|
||||
_writeNodeFileName("savedmodel.osg")
|
||||
class DrawHelpCallback : public Producer::Camera::Callback
|
||||
{
|
||||
public:
|
||||
|
||||
DrawHelpCallback(ViewerEventHandler* veh):
|
||||
_veh(veh),
|
||||
_initialized(false)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void operator()( const Producer::Camera & camera)
|
||||
{
|
||||
|
||||
if (_veh->getDisplayHelp())
|
||||
{
|
||||
if (!_initialized) createText();
|
||||
|
||||
OsgSceneHandler* osh = _veh->getOsgCameraGroup()->getSceneHandlerList()[0].get();
|
||||
|
||||
int x,y;
|
||||
unsigned int width,height;
|
||||
camera.getProjectionRect(x,y,width,height);
|
||||
_viewport->setViewport(x,y,width,height);
|
||||
|
||||
// should possibly update _viewport...
|
||||
|
||||
// Set up the Orthographic view
|
||||
glMatrixMode( GL_PROJECTION );
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
glOrtho( 0.0, 1280.0, 0.0, 1024, -1.0, 1.0 );
|
||||
|
||||
glPushAttrib( GL_ENABLE_BIT );
|
||||
glDisable( GL_LIGHTING );
|
||||
glDisable( GL_DEPTH_TEST );
|
||||
glEnable( GL_BLEND );
|
||||
|
||||
glMatrixMode( GL_MODELVIEW );
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
|
||||
osh->getState()->pushStateSet(_stateset.get());
|
||||
|
||||
for(TextList::iterator ditr=_descriptionList.begin();
|
||||
ditr!=_descriptionList.end();
|
||||
++ditr)
|
||||
{
|
||||
(*ditr)->draw(*(osh->getState()));
|
||||
}
|
||||
|
||||
for(TextList::iterator oitr=_optionList.begin();
|
||||
oitr!=_optionList.end();
|
||||
++oitr)
|
||||
{
|
||||
(*oitr)->draw(*(osh->getState()));
|
||||
}
|
||||
|
||||
for(TextList::iterator eitr=_explanationList.begin();
|
||||
eitr!=_explanationList.end();
|
||||
++eitr)
|
||||
{
|
||||
(*eitr)->draw(*(osh->getState()));
|
||||
}
|
||||
|
||||
osh->getState()->popStateSet();
|
||||
|
||||
glPopMatrix();
|
||||
glMatrixMode( GL_PROJECTION );
|
||||
glPopMatrix();
|
||||
glMatrixMode( GL_MODELVIEW );
|
||||
|
||||
glPopAttrib();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void createText()
|
||||
{
|
||||
_stateset = new osg::StateSet;
|
||||
_viewport = new osg::Viewport(0,0,1280,1024);
|
||||
_stateset->setAttribute(_viewport.get());
|
||||
|
||||
OsgCameraGroup* ocg = _veh->getOsgCameraGroup();
|
||||
if (ocg->getApplicationUsage())
|
||||
{
|
||||
|
||||
const osg::ApplicationUsage::UsageMap& um = ocg->getApplicationUsage()->getKeyboardMouseBindings();
|
||||
|
||||
float maxWidthOfDisplayRegion = 1200.0f;
|
||||
float bottomOfDescription = 1000.0f;
|
||||
osg::Vec3 posDescription(0.0f,bottomOfDescription,0.0f);
|
||||
osg::Vec4 colorDescription(1.0f,1.0f,0.0f,1.0f);
|
||||
float characterSize = 20.0f;
|
||||
|
||||
if (!(ocg->getApplicationUsage()->getDescription()).empty())
|
||||
{
|
||||
osgText::Text* text = new osgText::Text;
|
||||
text->setFont("fonts/arial.ttf");
|
||||
text->setColor(colorDescription);
|
||||
text->setCharacterSize(characterSize);
|
||||
text->setPosition(posDescription);
|
||||
text->setMaximumWidth(maxWidthOfDisplayRegion);
|
||||
text->setAlignment(osgText::Text::BASE_LINE);
|
||||
text->setText(ocg->getApplicationUsage()->getDescription());
|
||||
|
||||
bottomOfDescription = text->getBound().yMin()-characterSize*2.0f;
|
||||
|
||||
_descriptionList.push_back(text);
|
||||
|
||||
}
|
||||
|
||||
osg::Vec3 posOption(0.0f,bottomOfDescription,0.0f);
|
||||
osg::Vec4 colorOption(1.0f,1.0f,0.0f,1.0f);
|
||||
float maxX = 0.0f;
|
||||
|
||||
// create option strings.
|
||||
osg::ApplicationUsage::UsageMap::const_iterator citr;
|
||||
for(citr=um.begin();
|
||||
citr!=um.end();
|
||||
++citr)
|
||||
{
|
||||
osgText::Text* text = new osgText::Text;
|
||||
text->setFont("fonts/arial.ttf");
|
||||
text->setColor(colorOption);
|
||||
text->setCharacterSize(characterSize);
|
||||
text->setPosition(posOption);
|
||||
text->setAlignment(osgText::Text::BASE_LINE);
|
||||
text->setText(citr->first);
|
||||
|
||||
if (text->getBound().xMax()>maxX) maxX=text->getBound().xMax();
|
||||
|
||||
_optionList.push_back(text);
|
||||
|
||||
}
|
||||
|
||||
osg::Vec3 posExplanation(maxX+characterSize,bottomOfDescription,0.0f);
|
||||
osg::Vec4 colorExplanation(1.0f,1.0f,0.0f,1.0f);
|
||||
float maxWidth = maxWidthOfDisplayRegion-maxX;
|
||||
|
||||
TextList::iterator oitr;
|
||||
TextList::iterator eitr;
|
||||
TextList::iterator ditr;
|
||||
|
||||
for(citr=um.begin(), oitr=_optionList.begin();
|
||||
citr!=um.end();
|
||||
++citr,++oitr)
|
||||
{
|
||||
osgText::Text* text = new osgText::Text;
|
||||
text->setFont("fonts/arial.ttf");
|
||||
text->setColor(colorExplanation);
|
||||
text->setCharacterSize(characterSize);
|
||||
text->setPosition(posExplanation);
|
||||
text->setMaximumWidth(maxWidth);
|
||||
text->setAlignment(osgText::Text::BASE_LINE);
|
||||
text->setText(citr->second);
|
||||
|
||||
if (text->getBound().xMax()>maxX) maxX=text->getBound().xMax();
|
||||
|
||||
// fix the position of option text to be the same height as the examplanation.
|
||||
osg::Vec3 pos((*oitr)->getPosition());
|
||||
(*oitr)->setPosition(osg::Vec3(pos.x(),posExplanation.y(),pos.z()));
|
||||
|
||||
posExplanation.y() = text->getBound().yMin()-characterSize;
|
||||
|
||||
_explanationList.push_back(text);
|
||||
|
||||
}
|
||||
|
||||
// compute the boundings of the all the text.
|
||||
osg::BoundingBox bb;
|
||||
for(ditr=_descriptionList.begin();
|
||||
ditr!=_descriptionList.end();
|
||||
++ditr)
|
||||
{
|
||||
bb.expandBy((*ditr)->getBound());
|
||||
}
|
||||
|
||||
for(oitr=_optionList.begin();
|
||||
oitr!=_optionList.end();
|
||||
++oitr)
|
||||
{
|
||||
bb.expandBy((*oitr)->getBound());
|
||||
}
|
||||
|
||||
for(eitr=_explanationList.begin();
|
||||
eitr!=_explanationList.end();
|
||||
++eitr)
|
||||
{
|
||||
bb.expandBy((*eitr)->getBound());
|
||||
}
|
||||
|
||||
float totalWidth = bb.xMax()-bb.xMin();
|
||||
float totalHeight = bb.yMax()-bb.yMin();
|
||||
float widthMargin = (1280.0f-totalWidth)*0.5f;
|
||||
float heightMargin = (1024.0f-totalHeight)*0.5f;
|
||||
|
||||
osg::Vec3 delta(widthMargin-bb.xMin(),heightMargin-bb.yMin(),0.0f);
|
||||
|
||||
// shift the text to center it.
|
||||
for(ditr=_descriptionList.begin();
|
||||
ditr!=_descriptionList.end();
|
||||
++ditr)
|
||||
{
|
||||
(*ditr)->setPosition((*ditr)->getPosition()+delta);
|
||||
}
|
||||
|
||||
for(oitr=_optionList.begin();
|
||||
oitr!=_optionList.end();
|
||||
++oitr)
|
||||
{
|
||||
(*oitr)->setPosition((*oitr)->getPosition()+delta);
|
||||
}
|
||||
|
||||
for(eitr=_explanationList.begin();
|
||||
eitr!=_explanationList.end();
|
||||
++eitr)
|
||||
{
|
||||
(*eitr)->setPosition((*eitr)->getPosition()+delta);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
ViewerEventHandler* _veh;
|
||||
bool _initialized;
|
||||
|
||||
osg::ref_ptr<osg::StateSet> _stateset;
|
||||
osg::ref_ptr<osg::Viewport> _viewport;
|
||||
|
||||
typedef std::vector< osg::ref_ptr<osgText::Text> > TextList;
|
||||
TextList _descriptionList;
|
||||
TextList _optionList;
|
||||
TextList _explanationList;
|
||||
};
|
||||
|
||||
|
||||
ViewerEventHandler::ViewerEventHandler(OsgCameraGroup* cg):
|
||||
_cg(cg),
|
||||
_writeNodeFileName("savedmodel.osg"),
|
||||
_displayHelp(false)
|
||||
{
|
||||
Producer::CameraConfig* cfg = _cg->getCameraConfig();
|
||||
Producer::Camera *cam = cfg->getCamera(0);
|
||||
cam->addPostDrawCallback(new DrawHelpCallback(this));
|
||||
}
|
||||
|
||||
bool ViewerEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
|
||||
@@ -46,10 +290,7 @@ bool ViewerEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActio
|
||||
case '/' :
|
||||
case '?' :
|
||||
{
|
||||
if (_cg->getApplicationUsage())
|
||||
{
|
||||
_cg->getApplicationUsage()->write(std::cout,_cg->getApplicationUsage()->getKeyboardMouseBindings());
|
||||
}
|
||||
setDisplayHelp(!getDisplayHelp());
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -71,4 +312,6 @@ void ViewerEventHandler::accept(osgGA::GUIEventHandlerVisitor& gehv)
|
||||
void ViewerEventHandler::getUsage(osg::ApplicationUsage& usage) const
|
||||
{
|
||||
usage.addKeyboardMouseBinding("f","Toggle fullscreen");
|
||||
usage.addKeyboardMouseBinding("?","Display help");
|
||||
usage.addKeyboardMouseBinding("o","Write scene graph to file");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user