#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>
MTypeId lineManip::id( 0x81047 );
class lineGeometry
{
public:
static MPoint topPoint() {
return MPoint( 1.0f, 1.0f, 0.0f );
}
static MPoint bottomPoint() {
return MPoint( 1.0f, -1.0f, 0.0f );
}
static MPoint otherPoint() {
return MPoint( 2.0f, -1.0f, 0.0f );
}
};
lineManip::lineManip()
{
MPoint pointOnPlane(lineGeometry::topPoint());
MVector normalToPlane = (MVector(lineGeometry::topPoint()) - MVector(lineGeometry::otherPoint())) ^
(MVector(lineGeometry::otherPoint()) - MVector(lineGeometry::bottomPoint()));
normalToPlane.normalize();
plane.setPlane( pointOnPlane, normalToPlane );
}
lineManip::~lineManip()
{
}
void lineManip::draw(M3dView &view, const MDagPath &path,
M3dView::DisplayStyle style, M3dView::DisplayStatus status)
{
static MGLFunctionTable *gGLFT = 0;
if ( 0 == gGLFT )
gGLFT = MHardwareRenderer::theRenderer()->glFunctionTable();
MDagPath dpath;
view.getCamera(dpath);
MFnCamera viewCamera(dpath);
const char *nameBuffer = viewCamera.name().asChar();
if ( 0 == nameBuffer )
return;
if ( ( 0 == strstr(nameBuffer,"persp") ) && ( 0 == strstr(nameBuffer,"front") ) )
return;
float top[4],bottom[4];
lineGeometry::topPoint().get(top);
lineGeometry::bottomPoint().get(bottom);
MGLuint active = 0;
if ( glActiveName( active ) )
{
float *a = 0,*b = 0;
if ( active == lineName )
{
a = &top[0]; b = &bottom[0];
}
if ( active != 0 )
{
a[0] += (float) mousePointGlName.x; a[1] += (float) mousePointGlName.y; a[2] += (float) mousePointGlName.z;
b[0] += (float) mousePointGlName.x; b[1] += (float) mousePointGlName.y; b[2] += (float) mousePointGlName.z;
}
}
view.beginGL();
MGLuint glPickableItem;
glFirstHandle( glPickableItem );
lineName = glPickableItem;
colorAndName( view, glPickableItem, true, mainColor() );
gGLFT->glBegin( MGL_LINES );
gGLFT->glVertex3fv( top );
gGLFT->glVertex3fv( bottom );
gGLFT->glEnd();
view.endGL();
}
MStatus lineManip::doPress( M3dView& view )
{
mousePointGlName = MPoint::origin;
updateDragInformation();
return MS::kSuccess;
}
MStatus lineManip::doDrag( M3dView& view )
{
updateDragInformation();
return MS::kSuccess;
}
MStatus lineManip::doRelease( M3dView& view )
{
MSelectionList list;
MGlobal::getActiveSelectionList( list );
for ( MItSelectionList iter( list ); !iter.isDone(); iter.next() )
{
MObject node;
MStatus status;
iter.getDependNode( node );
MFnTransform xform( node, &status );
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()
{
MPoint localMousePoint;
MVector localMouseDirection;
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] );
MPoint vab = a - b;
line.setLine( start, vab );
MPoint cpt;
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";
class lineManipCmd : public MTemplateCreateNodeCommand<lineManipCmd,cmdName,nodeName>
{
public:
lineManipCmd()
{}
};
static lineManipCmd _lineManipCmd;
void* lineManip::creator()
{
return new lineManip();
}
MStatus lineManip::initialize()
{
return MS::kSuccess;
}
MStatus initializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj, PLUGIN_COMPANY, "2009", "Any");
status = plugin.registerNode( nodeName, lineManip::id, lineManip::creator,
lineManip::initialize, MPxNode::kManipulatorNode );
if (!status)
{
status.perror("registerNode");
return status;
}
status = _lineManipCmd.registerCommand( obj );
if (!status)
{
status.perror("registerCommand");
return status;
}
return status;
}
MStatus uninitializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj );
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;
}