Skip to content

Commit

Permalink
Added Action class NodeVisitor that supports osgPresentation nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertosfield committed Sep 4, 2013
1 parent 7bc7115 commit 28155c6
Show file tree
Hide file tree
Showing 19 changed files with 355 additions and 80 deletions.
44 changes: 25 additions & 19 deletions examples/osgpresentation/osgpresentation.cpp
Expand Up @@ -29,32 +29,28 @@

#include <osg/NodeVisitor>

class PrintSupportedProperties : public osg::NodeVisitor
class PrintSupportedProperties : public osgPresentation::Action
{
public:
PrintSupportedProperties() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
PrintSupportedProperties() :
osgPresentation::Action(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}

void apply(osg::Node& node)
void apply(osgPresentation::Group& group)
{
osgPresentation::Group* pres_group = dynamic_cast<osgPresentation::Group*>(&node);
if (pres_group)
OSG_NOTICE<<"osgPresentation object : "<<group.className()<<std::endl;
osgPresentation::PropertyList properties;
if (group.getSupportedProperties(properties))
{
OSG_NOTICE<<"osgPresentation object : "<<pres_group->className()<<std::endl;
osgPresentation::PropertyList properties;
if (pres_group->getSupportedProperties(properties))
for(osgPresentation::PropertyList::iterator itr = properties.begin();
itr != properties.end();
++itr)
{
for(osgPresentation::PropertyList::iterator itr = properties.begin();
itr != properties.end();
++itr)
{
osgPresentation::ObjectDescription& od = *itr;
OSG_NOTICE<<" "<<od.first->className()<<" : "<<od.first->getName()<<", description = "<<od.second<<std::endl;

}
}
osgPresentation::ObjectDescription& od = *itr;
OSG_NOTICE<<" "<<od.first->className()<<" : "<<od.first->getName()<<", description = "<<od.second<<std::endl;

}
}
traverse(node);
traverse(group);
}
};

Expand Down Expand Up @@ -141,22 +137,32 @@ int main(int argc, char** argv)
osg::ref_ptr<osgPresentation::Layer> layer = new osgPresentation::Layer;
osg::ref_ptr<osgPresentation::Group> group = new osgPresentation::Group;
osg::ref_ptr<osgPresentation::Element> element = new osgPresentation::Element;
osg::ref_ptr<osgPresentation::Element> text = new osgPresentation::Text;
presentation->addChild(slide.get());
slide->addChild(layer.get());
//layer->addChild(element.get());
layer->addChild(group.get());
group->addChild(element.get());
element->addChild(model.get());
group->addChild(new osgPresentation::Model);
group->addChild(new osgPresentation::Text);
group->addChild(text.get());
group->addChild(new osgPresentation::Audio);
group->addChild(new osgPresentation::Movie);
group->addChild(new osgPresentation::Volume);


text->setProperty("string",std::string("This is a first test"));
text->setProperty("font",std::string("times.ttf"));
text->setProperty("character_size",2.2);
text->setProperty("width",std::string("103.2"));


PrintSupportedProperties psp;
presentation->accept(psp);

osgPresentation::LoadAction load;
presentation->accept( load );

viewer.setSceneData( presentation.get() );


Expand Down
88 changes: 88 additions & 0 deletions include/osgPresentation/Action
@@ -0,0 +1,88 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/

#ifndef OSGPRESENTATION_ACTION
#define OSGPRESENTATION_ACTION 1

#include <osgPresentation/Export>
#include <osg/NodeVisitor>

namespace osgPresentation
{

// forward declare osgPresention nodes
class Group;

class Element;
class Text;
class Volume;
class Model;
class Image;
class Movie;

class Section;
class Layer;
class Slide;
class Presentation;

/** Action base class that is a NodeVistor that addes osgPresentation node support.*/
class OSGPRESENTATION_EXPORT Action : public osg::NodeVisitor
{
public:
Action(osg::NodeVisitor::TraversalMode traversalMode=osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN):
osg::NodeVisitor(traversalMode) {}

virtual void apply(osgPresentation::Group& group);

virtual void apply(osgPresentation::Element& element);
virtual void apply(osgPresentation::Text& text);
virtual void apply(osgPresentation::Volume& volume);
virtual void apply(osgPresentation::Model& model);
virtual void apply(osgPresentation::Image& image);
virtual void apply(osgPresentation::Movie& movie);

virtual void apply(osgPresentation::Section& section);
virtual void apply(osgPresentation::Layer& layer);
virtual void apply(osgPresentation::Slide& slide);
virtual void apply(osgPresentation::Presentation& presentation);
};


struct OSGPRESENTATION_EXPORT LoadAction : public Action
{
void apply(osgPresentation::Element& element);
};

struct OSGPRESENTATION_EXPORT UnloadAction : public Action
{
void apply(osgPresentation::Element& element);
};

struct OSGPRESENTATION_EXPORT ResetAction : public Action
{
void apply(osgPresentation::Element& element);
};

struct OSGPRESENTATION_EXPORT PauseAction : public Action
{
void apply(osgPresentation::Element& element);
};

struct OSGPRESENTATION_EXPORT PlayAction : public Action
{
void apply(osgPresentation::Element& element);
};

}

#endif
2 changes: 1 addition & 1 deletion include/osgPresentation/Audio
Expand Up @@ -29,7 +29,7 @@ class OSGPRESENTATION_EXPORT Audio : public osgPresentation::Element
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Audio(const Audio& audio,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osgPresentation::Element(audio,copyop) {}

META_Node(osgPresentation, Audio);
META_Presentation(Audio);

protected :

Expand Down
21 changes: 11 additions & 10 deletions include/osgPresentation/Element
@@ -1,4 +1,4 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
Expand Down Expand Up @@ -31,9 +31,7 @@ class OSGPRESENTATION_EXPORT Element : public osgPresentation::Group
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Element(const Element& element,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osgPresentation::Group(element,copyop) {}

META_Node(osgPresentation, Element);

virtual void traverse(osg::NodeVisitor& nv);
META_Presentation(Element);

/** Load the subgraph implementation of the element.*/
virtual bool load() { return false; }
Expand All @@ -45,19 +43,22 @@ class OSGPRESENTATION_EXPORT Element : public osgPresentation::Group
virtual bool loaded() const { return getNumChildren()!=0; }


/** Do updates as part of the update traversal.*/
virtual void updateTraversal(osgUtil::UpdateVisitor& uv);

/** Do updates as part of the event traversal.*/
virtual void eventTraversal(osgGA::EventVisitor& ev);

/** Enter the element for the first time, starting any animations, movies, audio etc..*/
virtual void enter() {}

/** Leave the element, stopping any animations, movies, audio etc..*/
virtual void leave() {}


/** Pause any animatios, videos, audio etc.*/
virtual void pause() {}

/** Play any animatios, videos, audio etc.*/
virtual void play() {}

/** Reset any animations, vidoes, audio etc. back to the begininng.*/
virtual void reset() {}

protected :

virtual ~Element() {}
Expand Down
59 changes: 55 additions & 4 deletions include/osgPresentation/Group
@@ -1,4 +1,4 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
Expand All @@ -15,14 +15,37 @@
#define OSGPRESENTATION_GROUP 1

#include <osg/MatrixTransform>
#include <osgPresentation/Export>
#include <osg/ValueObject>
#include <osgPresentation/Action>

namespace osgPresentation {
#include <sstream>

namespace osgPresentation {

typedef std::pair< osg::ref_ptr<osg::Object>, std::string> ObjectDescription;
typedef std::list< ObjectDescription > PropertyList;

/** META_Presentation macro define the standard clone, isSameKindAs, className
* and accept methods. Use when subclassing from Node to make it
* more convenient to define the required pure virtual methods.*/
#define META_Presentation(name) \
virtual osg::Object* cloneType() const { return new name (); } \
virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new name (*this,copyop); } \
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
virtual const char* className() const { return #name; } \
virtual const char* libraryName() const { return "osgPresentation"; } \
virtual void accept(osg::NodeVisitor& nv) \
{ \
if (nv.validNodeMask(*this)) \
{ \
nv.pushOntoNodePath(this); \
osgPresentation::Action* action = dynamic_cast<osgPresentation::Action*>(&nv); \
if (action) action->apply(*this); \
else nv.apply(*this); \
nv.popFromNodePath(); \
} \
}

/** osgPresentation::Group
*/
class OSGPRESENTATION_EXPORT Group : public osg::MatrixTransform
Expand All @@ -34,7 +57,7 @@ class OSGPRESENTATION_EXPORT Group : public osg::MatrixTransform
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Group(const Group& group,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osg::MatrixTransform(group,copyop) {}

META_Node(osgPresentation, Group);
META_Presentation(Group);

/** Convinience method that casts the named UserObject to osg::TemplateValueObject<T> and gets the value.
* To use this template method you need to include the osg/ValueObject header.*/
Expand All @@ -53,6 +76,34 @@ class OSGPRESENTATION_EXPORT Group : public osg::MatrixTransform
return setUserValue(name, value);
}

/** Check for named Property, if it doesn't exist on this object check parents recursively for instances.*/
osg::Object* getPropertyObject(const std::string& name, bool checkParents = true);

/** Check for name Property, and convert to desired template value where possible. */
template<typename T>
bool getPropertyValue(const std::string& name, T& value, bool checkParents = true)
{
osg::Object* object = getPropertyObject(name, checkParents);
if (!object) return false;

typedef osg::TemplateValueObject<T> UserValueObject;
const UserValueObject* uvo = dynamic_cast<const UserValueObject*>(object);
if (uvo)
{
value = uvo->getValue();
return true;
}

osg::StringValueObject* svo = dynamic_cast<osg::StringValueObject*>(object);
if (svo)
{
std::istringstream str(svo->getValue());
str >> value;
return !str.fail();
}
return false;
}

/** Get all types of Properties supported by Presentation Object type, return true if the Properties are supported, false otherwise.*/
virtual bool getSupportedProperties(PropertyList&) { return false; }

Expand Down
2 changes: 1 addition & 1 deletion include/osgPresentation/Image
Expand Up @@ -29,7 +29,7 @@ class OSGPRESENTATION_EXPORT Image : public osgPresentation::Element
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Image(const Image& image,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osgPresentation::Element(image,copyop) {}

META_Node(osgPresentation, Image);
META_Presentation(Image);

protected :

Expand Down
2 changes: 1 addition & 1 deletion include/osgPresentation/Layer
Expand Up @@ -29,7 +29,7 @@ class OSGPRESENTATION_EXPORT Layer : public osgPresentation::Group
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Layer(const Layer& layer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osgPresentation::Group(layer,copyop) {}

META_Node(osgPresentation, Layer);
META_Presentation(Layer);

protected :

Expand Down
2 changes: 1 addition & 1 deletion include/osgPresentation/Model
Expand Up @@ -29,7 +29,7 @@ class OSGPRESENTATION_EXPORT Model : public osgPresentation::Element
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Model(const Model& model,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osgPresentation::Element(model,copyop) {}

META_Node(osgPresentation, Model);
META_Presentation(Model);

protected :

Expand Down
2 changes: 1 addition & 1 deletion include/osgPresentation/Movie
Expand Up @@ -29,7 +29,7 @@ class OSGPRESENTATION_EXPORT Movie : public osgPresentation::Element
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Movie(const Movie& movie,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osgPresentation::Element(movie,copyop) {}

META_Node(osgPresentation, Movie);
META_Presentation(Movie);

protected :

Expand Down
2 changes: 1 addition & 1 deletion include/osgPresentation/Presentation
Expand Up @@ -29,7 +29,7 @@ class OSGPRESENTATION_EXPORT Presentation : public osgPresentation::Group
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Presentation(const Presentation& presentation,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osgPresentation::Group(presentation,copyop) {}

META_Node(osgPresentation, Presentation);
META_Presentation(Presentation);

protected :

Expand Down
2 changes: 1 addition & 1 deletion include/osgPresentation/Section
Expand Up @@ -29,7 +29,7 @@ class OSGPRESENTATION_EXPORT Section : public osgPresentation::Group
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Section(const Section& section,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osgPresentation::Group(section,copyop) {}

META_Node(osgPresentation, Section);
META_Presentation(Section);

protected :

Expand Down
2 changes: 1 addition & 1 deletion include/osgPresentation/Slide
Expand Up @@ -29,7 +29,7 @@ class OSGPRESENTATION_EXPORT Slide : public osgPresentation::Group
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Slide(const Slide& slide,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osgPresentation::Group(slide,copyop) {}

META_Node(osgPresentation, Slide);
META_Presentation(Slide);

protected :

Expand Down
4 changes: 2 additions & 2 deletions include/osgPresentation/Text
@@ -1,4 +1,4 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
Expand Down Expand Up @@ -29,7 +29,7 @@ class OSGPRESENTATION_EXPORT Text : public osgPresentation::Element
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Text(const Text& text,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osgPresentation::Element(text,copyop) {}

META_Node(osgPresentation, Text);
META_Presentation(Text);

/** load the text subgraph.*/
virtual bool load();
Expand Down

0 comments on commit 28155c6

Please sign in to comment.