| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 15 | #include <osgViewer/Renderer> |
|---|
| 16 | |
|---|
| 17 | #include <osg/PolygonMode> |
|---|
| 18 | |
|---|
| 19 | #include <osgText/Text> |
|---|
| 20 | |
|---|
| 21 | using namespace osgViewer; |
|---|
| 22 | |
|---|
| 23 | HelpHandler::HelpHandler(osg::ApplicationUsage* au): |
|---|
| 24 | _applicationUsage(au), |
|---|
| 25 | _keyEventTogglesOnScreenHelp('h'), |
|---|
| 26 | _helpEnabled(false), |
|---|
| 27 | _initialized(false) |
|---|
| 28 | { |
|---|
| 29 | _camera = new osg::Camera; |
|---|
| 30 | _camera->setRenderer(new Renderer(_camera.get())); |
|---|
| 31 | _camera->setRenderOrder(osg::Camera::POST_RENDER, 11); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | void HelpHandler::reset() |
|---|
| 36 | { |
|---|
| 37 | _initialized = false; |
|---|
| 38 | _camera->setGraphicsContext(0); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | bool HelpHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 42 | { |
|---|
| 43 | osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa); |
|---|
| 44 | if (!view) return false; |
|---|
| 45 | |
|---|
| 46 | osgViewer::ViewerBase* viewer = view->getViewerBase(); |
|---|
| 47 | if (!viewer) return false; |
|---|
| 48 | |
|---|
| 49 | if (ea.getHandled()) return false; |
|---|
| 50 | |
|---|
| 51 | switch(ea.getEventType()) |
|---|
| 52 | { |
|---|
| 53 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 54 | { |
|---|
| 55 | if (ea.getKey()==_keyEventTogglesOnScreenHelp) |
|---|
| 56 | { |
|---|
| 57 | if (!_initialized) |
|---|
| 58 | { |
|---|
| 59 | setUpHUDCamera(viewer); |
|---|
| 60 | setUpScene(viewer); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | _helpEnabled = !_helpEnabled; |
|---|
| 64 | |
|---|
| 65 | if (_helpEnabled) |
|---|
| 66 | { |
|---|
| 67 | _camera->setNodeMask(0xffffffff); |
|---|
| 68 | } |
|---|
| 69 | else |
|---|
| 70 | { |
|---|
| 71 | _camera->setNodeMask(0); |
|---|
| 72 | } |
|---|
| 73 | return true; |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | default: break; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | return false; |
|---|
| 80 | |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void HelpHandler::setUpHUDCamera(osgViewer::ViewerBase* viewer) |
|---|
| 84 | { |
|---|
| 85 | osgViewer::GraphicsWindow* window = dynamic_cast<osgViewer::GraphicsWindow*>(_camera->getGraphicsContext()); |
|---|
| 86 | |
|---|
| 87 | if (!window) |
|---|
| 88 | { |
|---|
| 89 | osgViewer::Viewer::Windows windows; |
|---|
| 90 | viewer->getWindows(windows); |
|---|
| 91 | |
|---|
| 92 | if (windows.empty()) return; |
|---|
| 93 | |
|---|
| 94 | window = windows.front(); |
|---|
| 95 | |
|---|
| 96 | _camera->setGraphicsContext(window); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | _camera->setGraphicsContext(window); |
|---|
| 100 | _camera->setViewport(0, 0, window->getTraits()->width, window->getTraits()->height); |
|---|
| 101 | |
|---|
| 102 | _camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); |
|---|
| 103 | _camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 104 | _camera->setViewMatrix(osg::Matrix::identity()); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | _camera->setClearMask(0); |
|---|
| 108 | |
|---|
| 109 | _initialized = true; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void HelpHandler::setUpScene(osgViewer::ViewerBase* viewer) |
|---|
| 113 | { |
|---|
| 114 | _switch = new osg::Switch; |
|---|
| 115 | |
|---|
| 116 | _camera->addChild(_switch.get()); |
|---|
| 117 | |
|---|
| 118 | osg::StateSet* stateset = _switch->getOrCreateStateSet(); |
|---|
| 119 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 120 | stateset->setMode(GL_BLEND,osg::StateAttribute::ON); |
|---|
| 121 | stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
|---|
| 122 | stateset->setAttribute(new osg::PolygonMode(), osg::StateAttribute::PROTECTED); |
|---|
| 123 | |
|---|
| 124 | std::string font("fonts/arial.ttf"); |
|---|
| 125 | |
|---|
| 126 | if (!_applicationUsage) setApplicationUsage(new osg::ApplicationUsage()); |
|---|
| 127 | |
|---|
| 128 | viewer->getUsage(*_applicationUsage); |
|---|
| 129 | |
|---|
| 130 | float leftPos = 10.0f; |
|---|
| 131 | float startDescription = 200.0f; |
|---|
| 132 | float characterSize = 20.0f; |
|---|
| 133 | |
|---|
| 134 | osg::Vec3 pos(leftPos,1000.0f,0.0f); |
|---|
| 135 | osg::Vec4 color(1.0f,1.0f,1.0f,1.0f); |
|---|
| 136 | |
|---|
| 137 | osg::Geode* geode = new osg::Geode(); |
|---|
| 138 | _switch->addChild(geode, true); |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | if (!_applicationUsage->getDescription().empty()) |
|---|
| 142 | { |
|---|
| 143 | |
|---|
| 144 | osg::ref_ptr<osgText::Text> label = new osgText::Text; |
|---|
| 145 | geode->addDrawable( label.get() ); |
|---|
| 146 | |
|---|
| 147 | label->setColor(color); |
|---|
| 148 | label->setBackdropType(osgText::Text::OUTLINE); |
|---|
| 149 | label->setFont(font); |
|---|
| 150 | label->setCharacterSize(characterSize); |
|---|
| 151 | label->setPosition(pos); |
|---|
| 152 | label->setText(_applicationUsage->getDescription()); |
|---|
| 153 | |
|---|
| 154 | pos.x() = label->getBound().xMax(); |
|---|
| 155 | pos.y() -= characterSize*2.5f; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | const osg::ApplicationUsage::UsageMap& keyboardBinding = _applicationUsage->getKeyboardMouseBindings(); |
|---|
| 159 | |
|---|
| 160 | for(osg::ApplicationUsage::UsageMap::const_iterator itr = keyboardBinding.begin(); |
|---|
| 161 | itr != keyboardBinding.end(); |
|---|
| 162 | ++itr) |
|---|
| 163 | { |
|---|
| 164 | pos.x() = leftPos; |
|---|
| 165 | |
|---|
| 166 | osg::ref_ptr<osgText::Text> key = new osgText::Text; |
|---|
| 167 | geode->addDrawable( key.get() ); |
|---|
| 168 | key->setColor(color); |
|---|
| 169 | key->setBackdropType(osgText::Text::OUTLINE); |
|---|
| 170 | key->setFont(font); |
|---|
| 171 | key->setCharacterSize(characterSize); |
|---|
| 172 | key->setPosition(pos); |
|---|
| 173 | key->setText(itr->first); |
|---|
| 174 | |
|---|
| 175 | pos.x() = startDescription; |
|---|
| 176 | |
|---|
| 177 | osg::ref_ptr<osgText::Text> description = new osgText::Text; |
|---|
| 178 | geode->addDrawable( description.get() ); |
|---|
| 179 | description->setColor(color); |
|---|
| 180 | description->setBackdropType(osgText::Text::OUTLINE); |
|---|
| 181 | description->setFont(font); |
|---|
| 182 | description->setCharacterSize(characterSize); |
|---|
| 183 | description->setPosition(pos); |
|---|
| 184 | |
|---|
| 185 | description->setText(itr->second); |
|---|
| 186 | |
|---|
| 187 | pos.y() -= characterSize*1.5f; |
|---|
| 188 | |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | osg::BoundingBox bb = geode->getBoundingBox(); |
|---|
| 192 | if (bb.valid()) |
|---|
| 193 | { |
|---|
| 194 | float width = bb.xMax() - bb.xMin(); |
|---|
| 195 | float height = bb.yMax() - bb.yMin(); |
|---|
| 196 | float ratio = 1.0; |
|---|
| 197 | if (width > 1024.0f) ratio = 1024.0f/width; |
|---|
| 198 | if (height*ratio > 800.0f) ratio = 800.0f/height; |
|---|
| 199 | |
|---|
| 200 | _camera->setViewMatrix(osg::Matrix::translate(-bb.center()) * |
|---|
| 201 | osg::Matrix::scale(ratio,ratio,ratio) * |
|---|
| 202 | osg::Matrix::translate(osg::Vec3(640.0f, 520.0f, 0.0f))); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | void HelpHandler::getUsage(osg::ApplicationUsage& usage) const |
|---|
| 208 | { |
|---|
| 209 | usage.addKeyboardMouseBinding("h","Onscreen help."); |
|---|
| 210 | } |
|---|