root/OpenSceneGraph/trunk/src/osgWidget/Browser.cpp @ 13041

Revision 13041, 3.6 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
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
22using namespace osgWidget;
23
24
25osg::ref_ptr<BrowserManager>& BrowserManager::instance()
26{
27    static osg::ref_ptr<BrowserManager> s_BrowserManager = new BrowserManager;
28    return s_BrowserManager;
29}
30
31BrowserManager::BrowserManager()
32{
33    OSG_INFO<<"Constructing base BrowserManager"<<std::endl;
34}
35
36BrowserManager::~BrowserManager()
37{
38    OSG_INFO<<"Destructing base BrowserManager"<<std::endl;
39}
40
41void BrowserManager::init(const std::string& application)
42{
43    _application = application;
44}
45
46BrowserImage* BrowserManager::createBrowserImage(const std::string& url, int width, int height)
47{
48    OSG_NOTICE<<"Cannot create browser"<<std::endl;
49    return 0;
50}
51
52Browser::Browser(const std::string& url, const GeometryHints& hints)
53{
54    open(url, hints);
55}
56
57bool 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            // no need to adjust aspect ratio
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
106bool 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
112void Browser::navigateTo(const std::string& url)
113{
114    if (_browserImage.valid()) _browserImage->navigateTo(url);
115}
Note: See TracBrowser for help on using the browser.