#include <maya/MIOStream.h>
#include <maya/MPxNode.h>
#include <maya/MPxManipulatorNode.h>
#include <maya/MPxSelectionContext.h>
#include <maya/MPxContextCommand.h>
#include <maya/MModelMessage.h>
#include <maya/MFnPlugin.h>
#include <maya/MGlobal.h>
#include <maya/MItSelectionList.h>
#include <maya/MPoint.h>
#include <maya/MVector.h>
#include <maya/MDagPath.h>
#include <maya/MManipData.h>
#include <maya/MHardwareRenderer.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnTransform.h>
#include <maya/MMatrix.h>
#include <maya/MFnCamera.h>
#include <maya/MTemplateCommand.h>
#include <maya/MTemplateManipulator.h>
#include "squareScaleManipContext.h"
MTypeId squareScaleManipulator::id( 0x81048 );
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::postConstructor()
{
MGLuint glPickableItem;
glFirstHandle(glPickableItem);
fTopName = glPickableItem++;
fBottomName = glPickableItem++;
fLeftName = glPickableItem++;
fRightName = glPickableItem++;
}
void squareScaleManipulator::preDrawUI(
const M3dView &view)
{
fDrawManip = shouldDraw(dpath);
}
{
if (!fDrawManip)
return;
const short defaultCol = mainColor();
setHandleColor(drawManager, fTopName, defaultCol);
drawManager.
text(center,
MString(
"The bottom line is not selectable"));
setHandleColor(drawManager, fBottomName, defaultCol);
setHandleColor(drawManager, fLeftName, defaultCol);
setHandleColor(drawManager, fRightName, defaultCol);
}
{
updateDragInformation();
return MS::kSuccess;
}
{
updateDragInformation();
return MS::kSuccess;
}
{
{
iter.getDependNode(node);
if (MS::kSuccess == status)
{
double newScale[3];
newScale[0] = fMousePoint.x + 1;
newScale[1] = fMousePoint.y + 1;
newScale[2] = fMousePoint.z + 1;
xform.setScale(newScale);
}
}
return MS::kSuccess;
}
MStatus squareScaleManipulator::updateDragInformation()
{
if (MS::kFailure == mouseRay(localMousePoint, localMouseDirection))
return MS::kFailure;
MPoint mouseIntersectionWithManipPlane;
if (!fPlane.intersect(localMousePoint, localMouseDirection, mouseIntersectionWithManipPlane))
return MS::kFailure;
fMousePoint = 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(fMousePoint, closestPoint))
{
fMousePoint.
x -= closestPoint.
x;
fMousePoint.y -= closestPoint.
y;
fMousePoint.z -= closestPoint.
z;
}
start[0] += (float)fMousePoint.x;
start[1] += (float)fMousePoint.y;
start[2] += (float)fMousePoint.z;
end[0] += (float)fMousePoint.x;
end[1] += (float)fMousePoint.y;
end[2] += (float)fMousePoint.z;
}
}
return MS::kSuccess;
}
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;
}
MStatus squareScaleManipulator::connectToDependNode(
const MObject& node)
{
return MS::kSuccess;
}
class squareManipCmd;
char cmdName[] = "squareManipCmd";
char nodeName[] = "squareScaleManipulator";
{
public:
squareManipCmd()
{}
};
static squareManipCmd _squareManipCmd;
void* squareScaleManipulator::creator()
{
return new squareScaleManipulator();
}
MStatus squareScaleManipulator::initialize()
{
return MS::kSuccess;
}
class squareScaleManipContext;
char contextName[] = "squareScaleManipContext";
char manipulatorNodeName[] = "squareScaleContextManipulator";
class squareScaleManipContext :
MFn::kTransform, squareScaleManipulator, manipulatorNodeName >
{
public:
squareScaleManipContext() {}
~squareScaleManipContext() override {}
{
namesOfAttributes.
append(
"scaleX");
}
};
class squareScaleManipContextCommand;
char contextCommandName[] = "squareScaleManipContext";
class squareScaleManipContextCommand :
{
public:
squareScaleManipContextCommand() {}
~squareScaleManipContextCommand() override {}
};
static squareScaleManipContextCommand _squareScaleManipContextCommand;
{
MFnPlugin plugin(obj, PLUGIN_COMPANY,
"2009",
"Any");
status = _squareScaleManipContextCommand.registerContextCommand( obj );
if (!status)
{
MString errorInfo(
"Error: registering context command : ");
errorInfo += contextCommandName;
return status;
}
status = plugin.registerNode(manipulatorNodeName, squareScaleManipulator::id,
&squareScaleManipulator::creator, &squareScaleManipulator::initialize,
if (!status)
{
MString str(
"Error registering node: ");
str+= manipulatorNodeName;
return status;
}
return status;
}
{
status = _squareScaleManipContextCommand.deregisterContextCommand( obj );
if (!status)
{
MString errorInfo(
"Error: deregistering context command : ");
errorInfo += contextCommandName;
return status;
}
status = plugin.deregisterNode(squareScaleManipulator::id);
if (!status)
{
MString str(
"Error deregistering node: ");
str+= manipulatorNodeName;
return status;
}
return status;
}