Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

browser.h

00001 //
00002 // OpenVRML
00003 //
00004 // Copyright (C) 1998  Chris Morley
00005 // 
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License, or (at your option) any later version.
00010 // 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 // 
00020 
00021 # ifndef OPENVRML_BROWSER_H
00022 #   define OPENVRML_BROWSER_H
00023 
00024 #   include <stddef.h>
00025 #   include <list>
00026 #   include <map>
00027 #   include "common.h"
00028 #   include "nodeclassptr.h"
00029 #   include "script.h"
00030 
00031 namespace OpenVRML {
00032 
00033     class OPENVRML_SCOPE InvalidVrml : public std::runtime_error {
00034     public:
00035         InvalidVrml();
00036         virtual ~InvalidVrml() throw ();
00037     };
00038     
00039     class Doc2;
00040     class Viewer;
00041     class ProtoNode;
00042     class Scene;
00043     class Vrml97RootScope;
00044 
00045     class OPENVRML_SCOPE Browser {
00046         friend class Vrml97Parser;
00047         friend class ProtoNodeClass;
00048         friend class Vrml97RootScope;
00049         
00050     public:
00051         enum CBReason {
00052             DESTROY_WORLD,
00053             REPLACE_WORLD
00054         };
00055 
00056         typedef void (*SceneCB)(CBReason reason);
00057 
00058     private:
00059         typedef std::map<std::string, NodeClassPtr> NodeClassMap;
00060         NodeClassMap nodeClassMap;
00061         ScriptNodeClass scriptNodeClass;
00062         Scene * scene;
00063         typedef std::list<NodePtr> BindStack;
00064         BindStack d_navigationInfoStack;
00065         BindStack d_viewpointStack;
00066         std::list<Node *> d_navigationInfos;
00067         std::list<Node *> d_viewpoints;
00068         std::list<Node *> d_scopedLights;
00069         std::list<ScriptNode *> d_scripts;
00070         std::list<Node *> d_timers;
00071         std::list<Node *> d_audioClips;
00072         std::list<Node *> d_movies;
00073         std::list<ProtoNode *> protoNodeList;
00074         bool d_modified;
00075         bool d_newView;
00076         double d_deltaTime;
00077 
00078     protected:
00079         typedef std::list < SceneCB > SceneCBList;
00080     
00081         struct Event {
00082             double timeStamp;
00083             FieldValue * value;
00084             NodePtr toNode;
00085             std::string toEventIn;
00086         };
00087 
00088         SceneCBList d_sceneCallbacks;
00089 
00090         double d_frameRate;
00091 
00092         enum { MAXEVENTS = 400 };
00093         Event d_eventMem[MAXEVENTS];
00094         size_t d_firstEvent;
00095         size_t d_lastEvent;
00096         
00097     public:
00098         std::ostream & out;
00099         std::ostream & err;
00100         bool d_flags_need_updating;
00101 
00102         Browser(std::ostream & out, std::ostream & err) throw (std::bad_alloc);
00103         virtual ~Browser() throw ();
00104 
00105         const MFNode & getRootNodes() const throw ();
00106 
00107         virtual const char * getName() const throw ();
00108         virtual const char * getVersion() const throw ();
00109         const std::string getWorldURI() const throw (std::bad_alloc);
00110         void replaceWorld(const MFNode & nodes);
00111         virtual void loadURI(const MFString & uri, const MFString & parameter)
00112             throw (std::bad_alloc);
00113         virtual void setDescription(const std::string & description);
00114         const MFNode createVrmlFromStream(std::istream & in);
00115         void createVrmlFromURI(const MFString & uri,
00116                                const NodePtr & node,
00117                                const std::string & event);
00118 
00119         void addWorldChangedCallback(SceneCB);
00120         
00121         void sensitiveEvent(Node * object, double timeStamp,
00122                             bool isOver, bool isActive, double *point );
00123 
00124         void queueEvent(double timeStamp, FieldValue * value,
00125                         const NodePtr & toNode, const std::string & toEventIn);
00126 
00127         bool eventsPending();
00128 
00129         void flushEvents();
00130 
00131         double getFrameRate() const;
00132 
00133         bool update(double currentTime = -1.0);
00134 
00135         void render(Viewer & viewer);
00136 
00137         void setModified();
00138         void clearModified();
00139         bool isModified() const;
00140 
00141         void setDelta(double d);
00142         double getDelta() const;
00143 
00144         void addNavigationInfo(Vrml97Node::NavigationInfo &);
00145         void removeNavigationInfo(Vrml97Node::NavigationInfo &);
00146         Vrml97Node::NavigationInfo *bindableNavigationInfoTop();
00147         void bindablePush(Vrml97Node::NavigationInfo *);
00148         void bindableRemove(Vrml97Node::NavigationInfo *);
00149 
00150         void addViewpoint(Vrml97Node::Viewpoint &);
00151         void removeViewpoint(Vrml97Node::Viewpoint &);
00152         Vrml97Node::Viewpoint *bindableViewpointTop();
00153         void bindablePush(Vrml97Node::Viewpoint *);
00154         void bindableRemove(Vrml97Node::Viewpoint *);
00155 
00156         void nextViewpoint();
00157         void prevViewpoint();  
00158         size_t nViewpoints();
00159         void getViewpoint(size_t index,
00160                           std::string & name, std::string & description);
00161         void setViewpoint(const std::string & name);
00162         void setViewpoint(size_t index);
00163 
00164         void addScopedLight(Vrml97Node::AbstractLight &);
00165         void removeScopedLight(Vrml97Node::AbstractLight &);
00166 
00167         void addTimeSensor(Vrml97Node::TimeSensor &);
00168         void removeTimeSensor(Vrml97Node::TimeSensor &);
00169 
00170         void addAudioClip(Vrml97Node::AudioClip &);
00171         void removeAudioClip(Vrml97Node::AudioClip &);
00172 
00173         void addMovie(Vrml97Node::MovieTexture &);
00174         void removeMovie(Vrml97Node::MovieTexture &);
00175         
00176         void addProto(ProtoNode & node);
00177         void removeProto(ProtoNode & node);
00178         void addScript(ScriptNode &);
00179         void removeScript(ScriptNode &);
00180 
00181         void updateFlags();
00182 
00183     protected:
00184         bool headlightOn();
00185         void doCallbacks(CBReason reason);
00186 
00187         const NodePtr bindableTop(const BindStack & stack);
00188         void bindablePush(BindStack & stack, const NodePtr & node);
00189         void bindableRemove(BindStack & stack, const NodePtr & node);
00190 
00191     private:
00192         // Not copyable.
00193         Browser(const Browser &);
00194         Browser & operator=(const Browser &);
00195         
00196         void initNodeClassMap();
00197     };
00198 
00199 
00200     class OPENVRML_SCOPE BadURI : public std::runtime_error {
00201     public:
00202         BadURI(const std::string & message);
00203         virtual ~BadURI() throw ();
00204     };
00205     
00206     
00207     class OPENVRML_SCOPE InvalidURI : public BadURI {
00208     public:
00209         InvalidURI();
00210         virtual ~InvalidURI() throw ();
00211     };
00212     
00213     
00214     class OPENVRML_SCOPE UnreachableURI : public BadURI {
00215     public:
00216         UnreachableURI();
00217         virtual ~UnreachableURI() throw ();
00218     };
00219     
00220     
00221     class OPENVRML_SCOPE Scene {
00222         MFNode nodes;
00223         std::string uri;
00224         
00225     public:
00226         Browser & browser;
00227         Scene * const parent;
00228     
00229         Scene(Browser & browser, const MFString & uri, Scene * parent = 0)
00230             throw (InvalidVrml, std::bad_alloc);
00231 
00232         const MFNode & getNodes() const throw ();
00233         const std::string getURI() const throw (std::bad_alloc);
00234         
00235         void initialize(double timestamp);
00236         void render(Viewer & viewer, VrmlRenderContext context);
00237         
00238         void loadURI(const MFString & uri, const MFString & parameter)
00239                 throw (std::bad_alloc);
00240     
00241     private:
00242         // Noncopyable.
00243         Scene(const Scene &);
00244         Scene & operator=(const Scene &);
00245     };
00246     
00247     inline const MFNode & Scene::getNodes() const throw() {
00248         return this->nodes;
00249     }
00250 }
00251 
00252 # endif