#include "lineManip.h"
#include <maya/MHardwareRenderer.h>
#include <maya/MIOStream.h>
#include <maya/MMatrix.h>
#include <maya/MPoint.h>
#include <maya/MVector.h>
#include <maya/MSelectionList.h>
#include <maya/MItSelectionList.h>
#include <maya/MFnTransform.h>
#include <maya/MGlobal.h>
#include <maya/MFnCamera.h>
#include <maya/MTemplateCommand.h>
class lineGeometry
{
public:
        return MPoint( 1.0f, 1.0f, 0.0f );
 
    }
        return MPoint( 1.0f, -1.0f, 0.0f );
 
    }
        return MPoint( 2.0f, -1.0f, 0.0f );
 
    }
};
lineManip::lineManip()
{
    
    
    MPoint pointOnPlane(lineGeometry::topPoint());
 
    
                    (
MVector(lineGeometry::otherPoint()) - 
MVector(lineGeometry::bottomPoint()));
 
    
    
    plane.setPlane( pointOnPlane, normalToPlane );
}
lineManip::~lineManip()
{
    
}
void lineManip::postConstructor()
{
    
    glFirstHandle( lineName );
}
void    lineManip::preDrawUI( 
const M3dView &view )
 
{
    
    fDrawManip = true;
    
    const char *nameBuffer = viewCamera.name().asChar();
    if ( 0 == nameBuffer )
        fDrawManip = false;
    if ( ( 0 == strstr(nameBuffer,"persp") ) && ( 0 == strstr(nameBuffer,"front") ) )
        fDrawManip = false;
    if ( !fDrawManip )
        return;
    fLineColorIndex = mainColor();
    fSelectedLineColorIndex = selectedColor();
    fLineStart = lineGeometry::topPoint() + 
MVector( mousePointGlName );
 
    fLineEnd   = lineGeometry::bottomPoint()   + 
MVector( mousePointGlName );
 
}
void    lineManip::drawUI(
{
    if ( !fDrawManip )
        return;
    setHandleColor(drawManager, lineName, fLineColorIndex);
    drawManager.
line( fLineStart, fLineEnd );
 
}
{
    
    
    updateDragInformation();
    return MS::kSuccess;
}
{
    updateDragInformation();
    return MS::kSuccess;
}
{
    
    
    
    {
            iter.getDependNode( node );
        if ( MS::kSuccess == status )
        {
            double newScale[3];
            newScale[0] = mousePointGlName.x + 1;
            newScale[1] = mousePointGlName.y + 1;
            newScale[2] = mousePointGlName.z + 1;
            xform.setScale( newScale );
        }
    }
    return MS::kSuccess;
}
MStatus lineManip::updateDragInformation()
 
{
    
    if ( MS::kFailure == mouseRay( localMousePoint, localMouseDirection) )
        return MS::kFailure;
    
    
    MPoint mouseIntersectionWithManipPlane;
 
    if ( ! plane.intersect( localMousePoint, localMouseDirection,   mouseIntersectionWithManipPlane ) )
        return MS::kFailure;
    mousePointGlName = mouseIntersectionWithManipPlane;
    MGLuint active = 0;
    if ( glActiveName( active ) )
    {
        float start[4],end[4];
        if ( active == lineName )
        {
            lineGeometry::topPoint().
get(start);
 
            lineGeometry::bottomPoint().get(end);
        }
        if ( active != 0 )
        {
            lineMath line;
            
            MPoint a( start[0], start[1], start[2] );
 
            MPoint b( end[0], end[1], end[2] );
 
            
            line.setLine( start, vab );
            
            
            if ( line.closestPoint( mousePointGlName, cpt ) )
            {
                mousePointGlName.
x -= cpt.
x;
 
                mousePointGlName.y -= cpt.
y;
 
                mousePointGlName.z -= cpt.
z;
 
            }
        }
    }
    return MS::kFailure;
}
class lineManipCmd;
char cmdName[] = "lineManipCmd";
char nodeName[] = "simpleLineManip"; 
{
public:
    
    lineManipCmd()
    {}
};
static lineManipCmd _lineManipCmd;
void* lineManip::creator()
{
    return new lineManip();
}
{
    return MS::kSuccess;
}
{ 
    MFnPlugin plugin( obj, PLUGIN_COMPANY, 
"2009", 
"Any");
 
    status = plugin.registerNode( nodeName, lineManip::id, lineManip::creator,
    if (!status) 
    {
        status.
perror(
"registerNode");
 
        return status;
    }
    status = _lineManipCmd.registerCommand( obj );
    if (!status) 
    {
        status.
perror(
"registerCommand");
 
        return status;
    }
    return status;
}
{
    status = plugin.deregisterNode( lineManip::id );
    if (!status) 
    {
        status.
perror(
"deregisterNode");
 
        return status;
    }
    status = _lineManipCmd.deregisterCommand( obj );
    if (!status) 
    {
        status.
perror(
"deregisterCommand");
 
        return status;
    }
    return status;
}