/// This file is part of the X3DToolKit library
/// Copyright (C) 2002-2004  Yannick Le Goc (legoc@imag.fr)

/// http://artis.imag.fr/Members/Yannick.Legoc/X3D/

/// This library is free software; you can redistribute it and/or
/// modify it under the terms of the GNU Lesser General Public
/// License as published by the Free Software Foundation; either
/// version 2.1 of the License, or (at your option) any later version.

/// 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 GNU
/// Lesser General Public License for more details.

/// You should have received a copy of the GNU Lesser General Public
/// License along with this library; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#ifndef X3DTK_X3D_MESHBUILDERSTATEVARIABLES_H
#define X3DTK_X3D_MESHBUILDERSTATEVARIABLES_H

#include "StateVariables.h"
#include "Cloner.h"
#include "X3D_X3DNode.h"
#include "MESH_X3DNode.h"
#include "X3D_X3DAppearanceNode.h"
#include "X3D_Shape.h"

#include <map>

namespace X3DTK {
namespace X3D {

class X3DAppearanceNode;
class Shape;

/*! \brief Class declared in <b><X3DTK/X3D/meshbuilder.h></b> providing StateVariables
 *  for the X3D::TemplateMeshBuilder processor.
 *
 *  \ingroup X3D
 */

template<class MData, class VData, class EData, class FData, bool RW> 
class TemplateMeshBuilderStateVariables : public StateVariables
{
public:
  /// Constructor.
  TemplateMeshBuilderStateVariables();
  /// Destructor.
  virtual ~TemplateMeshBuilderStateVariables();
  /// Initializes the traversal.
  void init();
  /// Finishes the traversal.
  virtual void finish();
  
  /// Enables the visit of children.
  void enableChildrenVisit();
  /// Disables the visit of children.
  void disableChildrenVisit();
  
  /// Returns true if childrenVisit is enabled.
  inline bool getChildrenVisit() const {return _childrenVisit;};
  /// Clones the node.
  X3DNode *clone(X3DNode *N);
  
  /// Adds the couple of nodes.
  void addCoupleNode(SFNode N, MESH::X3DNode *MN);
  /// Pushes the node.
  void pushNode(MESH::X3DNode *N);
  /// Pops the node.
  void popNode();
    
  /// Gets the GLScene.
  inline MESH::X3DNode *getRoot() const {return _root;};
  /// Gets the top of the stack.
  inline MESH::X3DNode *getTop() const {return _nodeStack.front();};
  /// Gets the MESH::SFNode related with N. Returns 0 if N not found.
  MESH::X3DNode *getNode(SFNode N) const;
  
private:
  MESH::X3DNode *_root;
  std::list<MESH::X3DNode *> _nodeStack;
  std::map<SFNode, MESH::X3DNode *> _nodeCoupleMap; 
  
  bool _childrenVisit;
  Cloner *_cloner;
};

}
}

#include "X3D_MeshBuilderStateVariables.inl"

#endif
