| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osg/Notify> |
|---|
| 15 | #include <osgDB/ReadFile> |
|---|
| 16 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 17 | |
|---|
| 18 | #include <osgWidget/Browser> |
|---|
| 19 | |
|---|
| 20 | #include <osg/io_utils> |
|---|
| 21 | |
|---|
| 22 | using namespace osgWidget; |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | osg::ref_ptr<BrowserManager>& BrowserManager::instance() |
|---|
| 26 | { |
|---|
| 27 | static osg::ref_ptr<BrowserManager> s_BrowserManager = new BrowserManager; |
|---|
| 28 | return s_BrowserManager; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | BrowserManager::BrowserManager() |
|---|
| 32 | { |
|---|
| 33 | OSG_INFO<<"Constructing base BrowserManager"<<std::endl; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | BrowserManager::~BrowserManager() |
|---|
| 37 | { |
|---|
| 38 | OSG_INFO<<"Destructing base BrowserManager"<<std::endl; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | void BrowserManager::init(const std::string& application) |
|---|
| 42 | { |
|---|
| 43 | _application = application; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | BrowserImage* BrowserManager::createBrowserImage(const std::string& url, int width, int height) |
|---|
| 47 | { |
|---|
| 48 | OSG_NOTICE<<"Cannot create browser"<<std::endl; |
|---|
| 49 | return 0; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | Browser::Browser(const std::string& url, const GeometryHints& hints) |
|---|
| 53 | { |
|---|
| 54 | open(url, hints); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | bool Browser::assign(BrowserImage* browserImage, const GeometryHints& hints) |
|---|
| 58 | { |
|---|
| 59 | if (!browserImage) return false; |
|---|
| 60 | |
|---|
| 61 | _browserImage = browserImage; |
|---|
| 62 | |
|---|
| 63 | bool flip = _browserImage->getOrigin()==osg::Image::TOP_LEFT; |
|---|
| 64 | |
|---|
| 65 | float aspectRatio = (_browserImage->t()>0 && _browserImage->s()>0) ? float(_browserImage->t()) / float(_browserImage->s()) : 1.0; |
|---|
| 66 | |
|---|
| 67 | osg::Vec3 widthVec(hints.widthVec); |
|---|
| 68 | osg::Vec3 heightVec(hints.heightVec); |
|---|
| 69 | |
|---|
| 70 | switch(hints.aspectRatioPolicy) |
|---|
| 71 | { |
|---|
| 72 | case(GeometryHints::RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO): |
|---|
| 73 | heightVec *= aspectRatio; |
|---|
| 74 | break; |
|---|
| 75 | case(GeometryHints::RESIZE_WIDTH_TO_MAINTAINCE_ASPECT_RATIO): |
|---|
| 76 | widthVec /= aspectRatio; |
|---|
| 77 | break; |
|---|
| 78 | default: |
|---|
| 79 | |
|---|
| 80 | break; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(hints.position, widthVec, heightVec, |
|---|
| 84 | 0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f); |
|---|
| 85 | |
|---|
| 86 | osg::Texture2D* texture = new osg::Texture2D(_browserImage.get()); |
|---|
| 87 | texture->setResizeNonPowerOfTwoHint(false); |
|---|
| 88 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
|---|
| 89 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 90 | texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 91 | |
|---|
| 92 | pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, |
|---|
| 93 | texture, |
|---|
| 94 | osg::StateAttribute::ON); |
|---|
| 95 | |
|---|
| 96 | osg::ref_ptr<osgViewer::InteractiveImageHandler> handler = new osgViewer::InteractiveImageHandler(_browserImage.get()); |
|---|
| 97 | |
|---|
| 98 | pictureQuad->setEventCallback(handler.get()); |
|---|
| 99 | pictureQuad->setCullCallback(handler.get()); |
|---|
| 100 | |
|---|
| 101 | addDrawable(pictureQuad); |
|---|
| 102 | |
|---|
| 103 | return true; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | bool Browser::open(const std::string& hostname, const GeometryHints& hints) |
|---|
| 107 | { |
|---|
| 108 | osg::ref_ptr<osg::Image> image = osgDB::readImageFile(hostname+".gecko"); |
|---|
| 109 | return assign(dynamic_cast<BrowserImage*>(image.get()), hints); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void Browser::navigateTo(const std::string& url) |
|---|
| 113 | { |
|---|
| 114 | if (_browserImage.valid()) _browserImage->navigateTo(url); |
|---|
| 115 | } |
|---|