#include "squareScaleManip.h"
#include <maya/MIOStream.h>
#include <maya/MMatrix.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>
#include <maya/MDrawRegistry.h>
#include <maya/MDrawContext.h>
#include <maya/MUserData.h>
#include <maya/MStateManager.h>
MTypeId squareScaleManipulator::id( 0x81046 );
 
MString squareScaleManipulator::registrantId(
"SquareScaleManipPlugin");
 
const MPoint squareScaleManipulator::topLeft(-0.5f, 0.5f, 0.0f);
 
const MPoint squareScaleManipulator::topRight(0.5f, 0.5f, 0.0f);
 
const MPoint squareScaleManipulator::bottomLeft(-0.5f, -0.5f, 0.0f);
 
const MPoint squareScaleManipulator::bottomRight(0.5f, -0.5f, 0.0f);
 
squareScaleManipulator::squareScaleManipulator()
, fActiveName(0)
, fTopName(0)
, fRightName(0)
, fBottomName(0)
, fLeftName(0)
{
    
    topLeft.get(fTopLeft);
    topRight.get(fTopRight);
    bottomLeft.get(fBottomLeft);
    bottomRight.get(fBottomRight);
    
    
    
    
    
    fPlane.setPlane(pointOnPlane, normalToPlane);
}
squareScaleManipulator::~squareScaleManipulator()
{
}
void squareScaleManipulator::preDrawUI( 
const M3dView &view )
 
{
    fDrawManip = shouldDraw(dpath);
}
{
    if(!fDrawManip)
        return;
    const short defaultCol = mainColor();
    setHandleColor(drawManager, fTopName, defaultCol);
    setHandleColor(drawManager, fBottomName, defaultCol);
    setHandleColor(drawManager, fLeftName, defaultCol);
    setHandleColor(drawManager, fRightName, defaultCol);
}
void squareScaleManipulator::draw(
{
    
    if (!shouldDraw(dpath))
    {
        return;
    }
    
    
    MGLuint glPickableItem;
    glFirstHandle(glPickableItem);
    
    fTopName = glPickableItem;
    
    
    colorAndName(view, glPickableItem, true, mainColor());
    glBegin(GL_LINES);
        glVertex3fv(fTopLeft);
        glVertex3fv(fTopRight);
    glEnd();
    
    glPickableItem++;
    fRightName = glPickableItem;
    colorAndName(view, glPickableItem, true, mainColor());
    glBegin(GL_LINES);
        glVertex3fv(fTopRight);
        glVertex3fv(fBottomRight);
    glEnd();
    
    glPickableItem++;
    fBottomName = glPickableItem;
    colorAndName(view, glPickableItem, true, mainColor());
    glBegin(GL_LINES);
        glVertex3fv(fBottomRight);
        glVertex3fv(fBottomLeft);
    glEnd();
    
    glPickableItem++;
    fLeftName = glPickableItem;
    colorAndName(view, glPickableItem, true, mainColor());
    glBegin(GL_LINES);
        glVertex3fv(fBottomLeft);
        glVertex3fv(fTopLeft);
    glEnd();
}
{
    
    updateDragInformation();
}
{
    updateDragInformation();
}
{
    
    
    {
        iter.getDependNode(node);
        {
            double newScale[3];
            newScale[0] = fMousePointGlName.x + 1;
            newScale[1] = fMousePointGlName.y + 1;
            newScale[2] = fMousePointGlName.z + 1;
            xform.setScale(newScale);
        }
    }
}
MStatus squareScaleManipulator::updateDragInformation()
 
{
    
    if (
MS::kFailure == mouseRay(localMousePoint, localMouseDirection))
 
    
    
    MPoint mouseIntersectionWithManipPlane;
 
    if (!fPlane.intersect(localMousePoint, localMouseDirection, mouseIntersectionWithManipPlane))
    fMousePointGlName = mouseIntersectionWithManipPlane;
    if (glActiveName(fActiveName))
    {
        
        topRight.get(fTopRight);
        bottomLeft.get(fBottomLeft);
        bottomRight.get(fBottomRight);
        float* start = 0;
        float* end = 0;
        if (fActiveName == fTopName)
        {
            start = fTopLeft;
            end = fTopRight;
        }
        if (fActiveName == fBottomName)
        {
            start = fBottomLeft;
            end = fBottomRight;
        }
        if (fActiveName == fRightName)
        {
            start = fTopRight;
            end = fBottomRight;
        }
        if (fActiveName == fLeftName)
        {
            start = fTopLeft;
            end = fBottomLeft;
        }
        if (start && end)
        {
            
            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(fMousePointGlName, cpt))
            {
                fMousePointGlName.
x -= cpt.
x;
                fMousePointGlName.y -= cpt.
y;
                fMousePointGlName.z -= cpt.
z;
            }
            
            start[0] += (float)fMousePointGlName.x;
            start[1] += (float)fMousePointGlName.y;
            start[2] +=(float) fMousePointGlName.z;
            end[0] += (float)fMousePointGlName.x;
            end[1] += (float)fMousePointGlName.y;
            end[2] +=(float) fMousePointGlName.z;
        }
    }
}
bool squareScaleManipulator::shouldDraw(
const MDagPath& cameraPath)
 const 
{
    if (!status)
    {
        return false;
    }
    const char* nameBuffer = camera.name().asChar();
    if (0 == nameBuffer)
    {
        return false;
    }
    if ((0 == strstr(nameBuffer,"persp")) &&
        (0 == strstr(nameBuffer,"front")))
    {
        return false;
    }
    return true;
}
class squareManipCmd;
char cmdName[] = "squareManipCmd";
char nodeName[] = "squareScaleManipulator";
{
public:
    
    squareManipCmd()
    {}
};
static squareManipCmd _squareManipCmd;
void* squareScaleManipulator::creator()
{
    return new squareScaleManipulator();
}
MStatus squareScaleManipulator::initialize()
 
{
    
}
{
    MFnPlugin plugin( obj, PLUGIN_COMPANY, 
"2009", 
"Any");
 
    status = plugin.registerNode(
        nodeName,
        squareScaleManipulator::id,
        squareScaleManipulator::creator,
        squareScaleManipulator::initialize,
    if (!status) {
        status.
perror(
"registerNode");
        return status;
    }
    status = _squareManipCmd.registerCommand( obj );
    if (!status) {
        status.
perror(
"registerCommand");
        return status;
    }
    return status;
}
{
    status = plugin.deregisterNode( squareScaleManipulator::id );
    if (!status) {
        status.
perror(
"deregisterNode");
        return status;
    }
    status = _squareManipCmd.deregisterCommand( obj );
    if (!status) {
        status.
perror(
"deregisterCommand");
        return status;
    }
    return status;
}