// LODP.C: LOD nodes (private source).
// Automatically generated with nodegen 0.5a - do not edit.

#include "LOD.H"
#include "xrmlP.H"

namespace xrml {
int LOD::major_version(void) { return 0; }
int LOD::minor_version(void) { return 5; }
bool LOD::is_leaf_class(void) { return true; }

bool IsLOD(SFNode* node)
{
  return (dynamic_cast<LOD*>(node) != 0);
}

void LOD::synchronize_level(void)
{
  if (_level.size > level.size) level.grow(_level.size - level.size);
  for (int i=level.size=0; i<_level.size; i++)
    level.append(dynamic_cast<Children*>(_level[i]->firstnode()));
}

bool LOD::assign_level(const MFNode& value)
{
  MFNode oldval = _level;
  _level = value;
  if (checkValue(_level, IsChildren, "LOD.level", "Children")) {
    synchronize_level();
    status |= level_updated;
    timestamp = interface[0]->timestamp = world ? world->time : 0;
    return true;
  } else {
    _level = oldval;
    return false;
  }
}

static bool assign_LOD_level(SFNode* node, const fieldValue& value)
{
  LOD* LOD_node = dynamic_cast<LOD*>(node);
  if (!LOD_node)
    Fatal(-1, "assign_LOD_level", "called for a non-LOD node");
  return LOD_node->assign_level((const MFNode&)value);
}

void LOD::set_level(const MFNode& val)
{
  MFNode tmpval(val);
  fieldValue v(&tmpval);
  interface[0]->assign(this, v);
}

bool LOD::assign_center(const SFVec3f& value)
{
  // TODO: value range check!
  center = value;
  status |= center_updated;
  timestamp = interface[1]->timestamp = world ? world->time : 0;
  return true;
}

static bool assign_LOD_center(SFNode* node, const fieldValue& value)
{
  LOD* LOD_node = dynamic_cast<LOD*>(node);
  if (!LOD_node)
    Fatal(-1, "assign_LOD_center", "called for a non-LOD node");
  return LOD_node->assign_center((const SFVec3f&)value);
}

bool LOD::assign_range(const MFFloat& value)
{
  // TODO: value range check!
  range = value;
  status |= range_updated;
  timestamp = interface[2]->timestamp = world ? world->time : 0;
  return true;
}

static bool assign_LOD_range(SFNode* node, const fieldValue& value)
{
  LOD* LOD_node = dynamic_cast<LOD*>(node);
  if (!LOD_node)
    Fatal(-1, "assign_LOD_range", "called for a non-LOD node");
  return LOD_node->assign_range((const MFFloat&)value);
}

LOD::LOD()
{
  interfaceMember* the_interface[3] = {
    new exposedField("level", fieldValue(&_level), assign_LOD_level),
    new field("center", fieldValue(&center), assign_LOD_center),
    new field("range", fieldValue(&range), assign_LOD_range),
  };
  SFNode::construct(LODTypeId, 3, the_interface);

  level = array<Children*>(0); 	_level = MFNode(0);
  center = SFVec3f(0,0,0);
  range = MFFloat(0);
  
  Grouping::util_init();
  Children::util_init();
  LOD::util_init();
}

void LOD::destruct(void)
{
  LOD::util_destruct();
  Grouping::util_destruct();
  Children::util_destruct();
  SFNode::destruct();
}

LOD::~LOD()
{
  destruct();
}

void LOD::clone_here(const LOD& src)
{
  SFNode::clone(src);
  
  interface[0]->value = fieldValue(&_level);
  interface[1]->value = fieldValue(&center);
  interface[2]->value = fieldValue(&range);
  
  level = src.level;	  _level = src._level;
  center = src.center;
  range = src.range;
  
  Grouping::util_clone(src);
  Children::util_clone(src);
  LOD::util_clone(src);
}

LOD::LOD(const LOD& src)
{
  clone_here(src);
}

LOD& LOD::operator=(const LOD& src)
{
  if (this != &src) {
    destruct();
    clone_here(src);
  }
  return *this;
}

SFNode* LOD::instantiate(Proto *enclosing_proto)
{
  if (nameIndex>=0)
    // named node: return pointer to pre-made instance
    return enclosing_proto ? enclosing_proto->namedNodes[nameIndex] : this;
  
  LOD* instance = new LOD(*this);
  instance->instantiate_interface(enclosing_proto);
  instance->synchronize_level();
  return instance;
}

}  // namespace xrml
