In icosahedronViewer, we defined a new simple X3DTK::X3D::X3DNode
that had no child. This is an example on how to create a parent node, meaning a node that can have children. Here we define an X3DTK::X3D::Info
node that can contain two nodes, X3DTK::X3D::Price
and X3DTK::X3D::Modeler
.
Like a node that can only be a child, the three special methods load
, write
and clone
still have to be redefined. Nevertheless others methods must be redefined. For the X3DTK::X3D::Price
child, the methods setPrice
and getPrice
are defined. Note in setPrice
the calls to removeLink
and addLink
. Four other necessary methods are redefined: setChild
, addChild
and removeChild
, and getChildList
. This method returns a list of child nodes. The order of which they appeared is important because, it will be the traversal order.
These nodes are simple child nodes and are defined like the nodes of the precedent examples.
To load the new nodes, the creator for the new Grouping component is defined.
To test our new scene graph, we define a new processor called X3DTK::X3D::InfoReader
that extracts and displays the content of the nodes X3DTK::X3D::Info
, X3DTK::X3D::Price
and X3DTK::X3D::Modeler
. You can refer to glNormalViewer for an example page of processor creation.
#ifndef INFO_H
#define INFO_H
#include <X3DTK/X3D/scenegraph.h>
namespace X3DTK {
namespace X3D {
class Price;
class Modeler;
class Info : public X3DChildNode
{
public:
Info();
Info(const SFString &date,
SFNode price,
SFNode modeler);
virtual ~Info();
virtual SFAbstractNode clone() const;
void setDate(const SFString &date);
void setPrice(Price *price);
void setModeler(Modeler *modeler);
inline const SFString &getDate() const {return _date;};
inline SFNode getPrice() const {return _price;};
inline SFNode getModeler() const {return _modeler;};
virtual MFAbstractNode getChildList() const;
virtual bool addChild(const SFAbstractNode &N);
virtual bool setChild(const SFAbstractNode &N);
virtual bool removeChild(const SFAbstractNode &N);
virtual void load(const X3DFileElement *element);
virtual SFString write() const;
protected:
Info(const Info &I);
private:
SFString _date;
SFNode _price;
SFNode _modeler;
};
}
}
#endif
#include "X3D_Info.h"
#include "X3D_Price.h"
#include "X3D_Modeler.h"
namespace X3DTK {
namespace X3D {
Info::Info()
: X3DChildNode(), _date("0/0/0"), _price(0), _modeler(0)
{
defineTypeName("Info");
}
Info::Info(const SFString &date, SFNode price, SFNode modeler)
: X3DChildNode(), _date(date), _price(price), _modeler(modeler)
{
defineTypeName("Info");
}
Info::Info(const Info &I)
: X3DChildNode(I), _date(I._date), _price(I._price), _modeler(I._modeler)
{
}
Info::~Info()
{
removeParentFromChild(this, _price);
removeParentFromChild(this, _modeler);
}
SFAbstractNode Info::clone() const
{
return new Info(*this);
}
void Info::setDate(const SFString &date)
{
_date = date;
}
void Info::setPrice(Price *price)
{
removeParentFromChild(this, _price);
_price = price;
addParentToChild(this, _price);
}
void Info::setModeler(Modeler *modeler)
{
removeParentFromChild(this, _modeler);
_modeler = modeler;
addParentToChild(this, _modeler);
}
MFAbstractNode Info::getChildList() const
{
MFAbstractNode childrenList;
if (_price != 0)
childrenList.push_back(_price);
if (_modeler != 0)
childrenList.push_back(_modeler);
return childrenList;
}
bool Info::addChild(const SFAbstractNode &N)
{
if (dynamic_cast<Price *>(N) != 0)
{
if (_price != 0)
return false;
setPrice(static_cast<Price *>(N));
return true;
}
if (dynamic_cast<Modeler *>(N) != 0)
{
if (_modeler != 0)
return false;
setModeler(static_cast<Modeler *>(N));
return true;
}
return false;
}
bool Info::setChild(const SFAbstractNode &N)
{
if (dynamic_cast<Price *>(N) != 0)
{
setPrice(static_cast<Price *>(N));
return true;
}
if (dynamic_cast<Modeler *>(N) != 0)
{
setModeler(static_cast<Modeler *>(N));
return true;
}
return false;
}
bool Info::removeChild(const SFAbstractNode &N)
{
if (_price == N)
{
setPrice(0);
return true;
}
if (_modeler == N)
{
setModeler(0);
return true;
}
return false;
}
void Info::load(const X3DFileElement *element)
{
int index;
index = element->getIndexAttribute("date");
if (index != -1)
_date = element->getAttribute(index);
}
SFString Info::write() const
{
SFString attr;
if (_date != "0/0/0")
attr += " date=\"" + toSFString(_date) + "\"";
return attr;
}
}
}
#ifndef PRICE_H
#define PRICE_H
#include <X3DTK/X3D/scenegraph.h>
namespace X3DTK {
namespace X3D {
class Price : public X3DChildNode
{
public:
Price();
Price(const SFFloat &price,
const SFString &money);
virtual SFAbstractNode clone() const;
void setPrice(const SFFloat &price);
void setMoney(const SFString &money);
inline const SFFloat &getPrice() const {return _price;};
inline const SFString &getMoney() const {return _money;};
virtual void load(const X3DFileElement *element);
virtual SFString write() const;
protected:
Price(const Price &I);
private:
SFFloat _price;
SFString _money;
};
}
}
#endif
#include "X3D_Price.h"
namespace X3DTK {
namespace X3D {
Price::Price()
: X3DChildNode(), _price(0.0f), _money("euro")
{
defineTypeName("Price");
}
Price::Price(const SFFloat &price, const SFString &money)
: X3DChildNode(), _price(price), _money(money)
{
defineTypeName("Price");
}
Price::Price(const Price &P)
: X3DChildNode(P), _price(P._price), _money(P._money)
{
}
SFAbstractNode Price::clone() const
{
return new Price(*this);
}
void Price::setPrice(const SFFloat &price)
{
_price = price;
}
void Price::setMoney(const SFString &money)
{
_money = money;
}
void Price::load(const X3DFileElement *element)
{
int index;
index = element->getIndexAttribute("price");
if (index != -1)
_price = element->getAttribute(index).toFloat();
index = element->getIndexAttribute("money");
if (index != -1)
_money = element->getAttribute(index);
}
SFString Price::write() const
{
SFString attr;
if (_price != 0.0f)
attr += " price=\"" + toSFString(_price) + "\"";
if (_money != "euro")
attr += " money=\"" + _money + "\"";
return attr;
}
}
}
#ifndef MODELER_H
#define MODELER_H
#include <X3DTK/X3D/scenegraph.h>
namespace X3DTK {
namespace X3D {
class Modeler : public X3DChildNode
{
public:
Modeler();
Modeler(const SFString &name);
virtual SFAbstractNode clone() const;
void setMName(const SFString &name);
inline const SFString &getMName() const {return _name;};
virtual void load(const X3DFileElement *element);
virtual SFString write() const;
protected:
Modeler(const Modeler &I);
private:
SFString _name;
};
}
}
#endif
#include "X3D_Modeler.h"
namespace X3DTK {
namespace X3D {
Modeler::Modeler()
: X3DChildNode(), _name("")
{
defineTypeName("Modeler", "MyComponent");
}
Modeler::Modeler(const SFString &name)
: X3DChildNode(), _name(name)
{
defineTypeName("Modeler", "MyComponent");
}
Modeler::Modeler(const Modeler &P)
: X3DChildNode(P), _name(P._name)
{
}
SFAbstractNode Modeler::clone() const
{
return new Modeler(*this);
}
void Modeler::setMName(const SFString &name)
{
_name = name;
}
void Modeler::load(const X3DFileElement *element)
{
int index;
index = element->getIndexAttribute("name");
if (index != -1)
_name = element->getAttribute(index);
}
SFString Modeler::write() const
{
SFString attr;
if (_name != "")
attr += " name=\"" + _name + "\"";
return attr;
}
}
}
#ifndef MYGROUPINGCREATOR_H
#define MYGROUPINGCREATOR_H
#include <X3DTK/X3D/scenegraph.h>
namespace X3DTK {
namespace X3D {
class MyGroupingCreator : public GroupingCreator
{
public:
MyGroupingCreator();
};
}
}
#endif
#include "X3D_MyGroupingCreator.h"
#include "X3D_Price.h"
#include "X3D_Modeler.h"
namespace X3DTK {
namespace X3D {
MyGroupingCreator::MyGroupingCreator()
: GroupingCreator()
{
define(Recorder<Price>::getCreationFunction());
define(Recorder<Modeler>::getCreationFunction());
}
}
}
#ifndef INFOREADERGROUPINGVISITOR_H
#define INFOREADERGROUPINGVISITOR_H
#include <X3DTK/X3D/scenegraph.h>
namespace X3DTK {
namespace X3D {
class Info;
class Price;
class Modeler;
class InfoReaderGroupingVisitor : public GroupingVisitor
{
public:
InfoReaderGroupingVisitor();
static void enterInfo(Info *I);
static void enterPrice(Price *P);
static void enterModeler(Modeler *M);
};
}
}
#endif
#include "X3D_InfoReaderGroupingVisitor.h"
#include "X3D_Info.h"
#include "X3D_Price.h"
#include "X3D_Modeler.h"
#include <iostream>
using namespace std;
namespace X3DTK {
namespace X3D {
InfoReaderGroupingVisitor::InfoReaderGroupingVisitor()
: GroupingVisitor()
{
define(Recorder<Info>::getEnterFunction(&InfoReaderGroupingVisitor::enterInfo));
define(Recorder<Price>::getEnterFunction(&InfoReaderGroupingVisitor::enterPrice));
define(Recorder<Modeler>::getEnterFunction(&InfoReaderGroupingVisitor::enterModeler));
}
void InfoReaderGroupingVisitor::enterInfo(Info *)
{
cout << "Informations relative to the scene:"<< endl;
}
void InfoReaderGroupingVisitor::enterPrice(Price *P)
{
cout << " price = " << P->getPrice() << " " << P->getMoney() << endl;
}
void InfoReaderGroupingVisitor::enterModeler(Modeler *M)
{
cout << " realized on the " << M->getMName() << " modeler" << endl;
}
}
}
#ifndef INFOREADER_H
#define INFOREADER_H
#include <X3DTK/kernel.h>
#include <X3DTK/X3D/scenegraph.h>
namespace X3DTK {
namespace X3D {
class InfoReader : public X3DOnePassProcessor
{
public:
InfoReader();
virtual ~InfoReader();
virtual void read(SFNode N);
};
}
}
#endif
#include "X3D_InfoReader.h"
#include "X3D_InfoReaderGroupingVisitor.h"
namespace X3DTK {
namespace X3D {
InfoReader::InfoReader()
{
graphTraversal = new DFSGraphTraversal();
graphTraversal->setComponentVisitor(new InfoReaderGroupingVisitor());
}
InfoReader::~InfoReader()
{
delete graphTraversal;
}
void InfoReader::read(SFNode N)
{
graphTraversal->traverse(N);
}
}
}
#include "X3D_InfoReader.h"
#include "X3D_MyGroupingCreator.h"
#include <X3DTK/X3D/scenegraph.h>
#include <iostream>
using namespace X3DTK;
using namespace std;
int main(int argc, char *argv[])
{
if (argc <= 1)
{
cerr << "usage: infoReader input" << endl;
exit(0);
}
X3D::Loader *loader = Singleton<X3D::Loader>::getInstance();
loader->setComponentCreator(new X3D::MyGroupingCreator());
X3D::InfoReader *infoReader = Singleton<X3D::InfoReader>::getInstance();
X3D::Scene *s = loader->load(argv[1], false);
infoReader->read(s);
Singleton<X3D::InfoReader>::removeInstance();
Singleton<X3D::InfoReader>::removeInstance();
return 1;
}
Generated on Thu Jun 3 10:12:13 2004 for X3DToolKit by
1.3.6