// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <ColorControlPoint.h>
#include <DataNode.h>

// ****************************************************************************
// Method: ColorControlPoint::ColorControlPoint
//
// Purpose:
//   Init utility for the ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

void ColorControlPoint::Init()
{
    colors[0] = 0;
    colors[1] = 0;
    colors[2] = 0;
    colors[3] = 255;
    position = 0;

    ColorControlPoint::SelectAll();
}

// ****************************************************************************
// Method: ColorControlPoint::ColorControlPoint
//
// Purpose:
//   Copy utility for the ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

void ColorControlPoint::Copy(const ColorControlPoint &obj)
{

    for(int i = 0; i < 4; ++i)
        colors[i] = obj.colors[i];

    position = obj.position;

    ColorControlPoint::SelectAll();
}

// Type map format string
const char *ColorControlPoint::TypeMapFormatString = COLORCONTROLPOINT_TMFS;
const AttributeGroup::private_tmfs_t ColorControlPoint::TmfsStruct = {COLORCONTROLPOINT_TMFS};


// ****************************************************************************
// Method: ColorControlPoint::ColorControlPoint
//
// Purpose:
//   Default constructor for the ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

ColorControlPoint::ColorControlPoint() :
    AttributeSubject(ColorControlPoint::TypeMapFormatString)
{
    ColorControlPoint::Init();
}

// ****************************************************************************
// Method: ColorControlPoint::ColorControlPoint
//
// Purpose:
//   Constructor for the derived classes of ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

ColorControlPoint::ColorControlPoint(private_tmfs_t tmfs) :
    AttributeSubject(tmfs.tmfs)
{
    ColorControlPoint::Init();
}

// ****************************************************************************
// Method: ColorControlPoint::ColorControlPoint
//
// Purpose:
//   Copy constructor for the ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

ColorControlPoint::ColorControlPoint(const ColorControlPoint &obj) :
    AttributeSubject(ColorControlPoint::TypeMapFormatString)
{
    ColorControlPoint::Copy(obj);
}

// ****************************************************************************
// Method: ColorControlPoint::ColorControlPoint
//
// Purpose:
//   Copy constructor for derived classes of the ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

ColorControlPoint::ColorControlPoint(const ColorControlPoint &obj, private_tmfs_t tmfs) :
    AttributeSubject(tmfs.tmfs)
{
    ColorControlPoint::Copy(obj);
}

// ****************************************************************************
// Method: ColorControlPoint::~ColorControlPoint
//
// Purpose:
//   Destructor for the ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

ColorControlPoint::~ColorControlPoint()
{
    // nothing here
}

// ****************************************************************************
// Method: ColorControlPoint::operator =
//
// Purpose:
//   Assignment operator for the ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

ColorControlPoint&
ColorControlPoint::operator = (const ColorControlPoint &obj)
{
    if (this == &obj) return *this;

    ColorControlPoint::Copy(obj);

    return *this;
}

// ****************************************************************************
// Method: ColorControlPoint::operator ==
//
// Purpose:
//   Comparison operator == for the ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

bool
ColorControlPoint::operator == (const ColorControlPoint &obj) const
{
    // Compare the colors arrays.
    bool colors_equal = true;
    for(int i = 0; i < 4 && colors_equal; ++i)
        colors_equal = (colors[i] == obj.colors[i]);

    // Create the return value
    return (colors_equal &&
            (position == obj.position));
}

// ****************************************************************************
// Method: ColorControlPoint::operator !=
//
// Purpose:
//   Comparison operator != for the ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

bool
ColorControlPoint::operator != (const ColorControlPoint &obj) const
{
    return !(this->operator == (obj));
}

// ****************************************************************************
// Method: ColorControlPoint::TypeName
//
// Purpose:
//   Type name method for the ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

const std::string
ColorControlPoint::TypeName() const
{
    return "ColorControlPoint";
}

// ****************************************************************************
// Method: ColorControlPoint::CopyAttributes
//
// Purpose:
//   CopyAttributes method for the ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

bool
ColorControlPoint::CopyAttributes(const AttributeGroup *atts)
{
    if(TypeName() != atts->TypeName())
        return false;

    // Call assignment operator.
    const ColorControlPoint *tmp = (const ColorControlPoint *)atts;
    *this = *tmp;

    return true;
}

// ****************************************************************************
// Method: ColorControlPoint::CreateCompatible
//
// Purpose:
//   CreateCompatible method for the ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

AttributeSubject *
ColorControlPoint::CreateCompatible(const std::string &tname) const
{
    AttributeSubject *retval = 0;
    if(TypeName() == tname)
        retval = new ColorControlPoint(*this);
    // Other cases could go here too.

    return retval;
}

// ****************************************************************************
// Method: ColorControlPoint::NewInstance
//
// Purpose:
//   NewInstance method for the ColorControlPoint class.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

AttributeSubject *
ColorControlPoint::NewInstance(bool copy) const
{
    AttributeSubject *retval = 0;
    if(copy)
        retval = new ColorControlPoint(*this);
    else
        retval = new ColorControlPoint;

    return retval;
}

// ****************************************************************************
// Method: ColorControlPoint::SelectAll
//
// Purpose:
//   Selects all attributes.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

void
ColorControlPoint::SelectAll()
{
    Select(ID_colors,   (void *)colors, 4);
    Select(ID_position, (void *)&position);
}

///////////////////////////////////////////////////////////////////////////////
// Persistence methods
///////////////////////////////////////////////////////////////////////////////

// ****************************************************************************
// Method: ColorControlPoint::CreateNode
//
// Purpose:
//   This method creates a DataNode representation of the object so it can be saved to a config file.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

bool
ColorControlPoint::CreateNode(DataNode *parentNode, bool completeSave, bool forceAdd)
{
    if(parentNode == 0)
        return false;

    ColorControlPoint defaultObject;
    bool addToParent = false;
    // Create a node for ColorControlPoint.
    DataNode *node = new DataNode("ColorControlPoint");

    if(completeSave || !FieldsEqual(ID_colors, &defaultObject))
    {
        addToParent = true;
        node->AddNode(new DataNode("colors", colors, 4));
    }

    if(completeSave || !FieldsEqual(ID_position, &defaultObject))
    {
        addToParent = true;
        node->AddNode(new DataNode("position", position));
    }


    // Add the node to the parent node.
    if(addToParent || forceAdd)
        parentNode->AddNode(node);
    else
        delete node;

    return (addToParent || forceAdd);
}

// ****************************************************************************
// Method: ColorControlPoint::SetFromNode
//
// Purpose:
//   This method sets attributes in this object from values in a DataNode representation of the object.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

void
ColorControlPoint::SetFromNode(DataNode *parentNode)
{
    if(parentNode == 0)
        return;

    DataNode *searchNode = parentNode->GetNode("ColorControlPoint");
    if(searchNode == 0)
        return;

    DataNode *node;
    if((node = searchNode->GetNode("colors")) != 0)
        SetColors(node->AsUnsignedCharArray());
    if((node = searchNode->GetNode("position")) != 0)
        SetPosition(node->AsFloat());
}

///////////////////////////////////////////////////////////////////////////////
// Set property methods
///////////////////////////////////////////////////////////////////////////////

void
ColorControlPoint::SetColors(const unsigned char *colors_)
{
    colors[0] = colors_[0];
    colors[1] = colors_[1];
    colors[2] = colors_[2];
    colors[3] = colors_[3];
    Select(ID_colors, (void *)colors, 4);
}

void
ColorControlPoint::SetPosition(float position_)
{
    position = position_;
    Select(ID_position, (void *)&position);
}

///////////////////////////////////////////////////////////////////////////////
// Get property methods
///////////////////////////////////////////////////////////////////////////////

const unsigned char *
ColorControlPoint::GetColors() const
{
    return colors;
}

unsigned char *
ColorControlPoint::GetColors()
{
    return colors;
}

float
ColorControlPoint::GetPosition() const
{
    return position;
}

///////////////////////////////////////////////////////////////////////////////
// Select property methods
///////////////////////////////////////////////////////////////////////////////

void
ColorControlPoint::SelectColors()
{
    Select(ID_colors, (void *)colors, 4);
}

///////////////////////////////////////////////////////////////////////////////
// Keyframing methods
///////////////////////////////////////////////////////////////////////////////

// ****************************************************************************
// Method: ColorControlPoint::GetFieldName
//
// Purpose:
//   This method returns the name of a field given its index.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

std::string
ColorControlPoint::GetFieldName(int index) const
{
    switch (index)
    {
    case ID_colors:   return "colors";
    case ID_position: return "position";
    default:  return "invalid index";
    }
}

// ****************************************************************************
// Method: ColorControlPoint::GetFieldType
//
// Purpose:
//   This method returns the type of a field given its index.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

AttributeGroup::FieldType
ColorControlPoint::GetFieldType(int index) const
{
    switch (index)
    {
    case ID_colors:   return FieldType_ucharArray;
    case ID_position: return FieldType_float;
    default:  return FieldType_unknown;
    }
}

// ****************************************************************************
// Method: ColorControlPoint::GetFieldTypeName
//
// Purpose:
//   This method returns the name of a field type given its index.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

std::string
ColorControlPoint::GetFieldTypeName(int index) const
{
    switch (index)
    {
    case ID_colors:   return "ucharArray";
    case ID_position: return "float";
    default:  return "invalid index";
    }
}

// ****************************************************************************
// Method: ColorControlPoint::FieldsEqual
//
// Purpose:
//   This method compares two fields and return true if they are equal.
//
// Note:       Autogenerated by xml2atts.
//
// Programmer: xml2atts
// Creation:   omitted
//
// Modifications:
//
// ****************************************************************************

bool
ColorControlPoint::FieldsEqual(int index_, const AttributeGroup *rhs) const
{
    const ColorControlPoint &obj = *((const ColorControlPoint*)rhs);
    bool retval = false;
    switch (index_)
    {
    case ID_colors:
        {  // new scope
        // Compare the colors arrays.
        bool colors_equal = true;
        for(int i = 0; i < 4 && colors_equal; ++i)
            colors_equal = (colors[i] == obj.colors[i]);

        retval = colors_equal;
        }
        break;
    case ID_position:
        {  // new scope
        retval = (position == obj.position);
        }
        break;
    default: retval = false;
    }

    return retval;
}

///////////////////////////////////////////////////////////////////////////////
// User-defined methods.
///////////////////////////////////////////////////////////////////////////////

// ****************************************************************************
// Method: ColorControlPoint::ColorControlPoint
//
// Purpose:
//   Constructor for the ColorControlPoint class.
//
// Programmer: Brad Whitlock
// Creation:   Tue Jun 12 16:51:24 PST 2001
//
// Modifications:
//
// ****************************************************************************

ColorControlPoint::ColorControlPoint(float pos, unsigned char r,
    unsigned char g, unsigned char b, unsigned char a) : AttributeSubject("Uf")
{
    position = pos;
    colors[0] = r;
    colors[1] = g;
    colors[2] = b;
    colors[3] = a;
}

