#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::classification(
"drawdb/geometry/manip/squareScaleManipulator");
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()
, activeName(0)
, topName(0)
, rightName(0)
, bottomName(0)
, leftName(0)
{
topLeft.get(tl);
topRight.get(tr);
bottomLeft.get(bl);
bottomRight.get(br);
plane.setPlane(pointOnPlane, normalToPlane);
}
squareScaleManipulator::~squareScaleManipulator()
{
}
void squareScaleManipulator::draw(
{
if (!shouldDraw(dpath))
{
return;
}
MGLuint glPickableItem;
glFirstHandle(glPickableItem);
topName = glPickableItem;
colorAndName(view, glPickableItem, true, mainColor());
glBegin(GL_LINES);
glVertex3fv(tl);
glVertex3fv(tr);
glEnd();
glPickableItem++;
rightName = glPickableItem;
colorAndName(view, glPickableItem, true, mainColor());
glBegin(GL_LINES);
glVertex3fv(tr);
glVertex3fv(br);
glEnd();
glPickableItem++;
bottomName = glPickableItem;
colorAndName(view, glPickableItem, true, mainColor());
glBegin(GL_LINES);
glVertex3fv(br);
glVertex3fv(bl);
glEnd();
glPickableItem++;
leftName = glPickableItem;
colorAndName(view, glPickableItem, true, mainColor());
glBegin(GL_LINES);
glVertex3fv(bl);
glVertex3fv(tl);
glEnd();
}
{
updateDragInformation();
}
{
updateDragInformation();
}
{
{
iter.getDependNode(node);
{
double newScale[3];
newScale[0] = mousePointGlName.x + 1;
newScale[1] = mousePointGlName.y + 1;
newScale[2] = mousePointGlName.z + 1;
xform.setScale(newScale);
}
}
}
MStatus squareScaleManipulator::updateDragInformation()
{
if (
MS::kFailure == mouseRay(localMousePoint, localMouseDirection))
MPoint mouseIntersectionWithManipPlane;
if (!plane.intersect(localMousePoint, localMouseDirection, mouseIntersectionWithManipPlane))
mousePointGlName = mouseIntersectionWithManipPlane;
if (glActiveName(activeName))
{
topRight.get(tr);
bottomLeft.get(bl);
bottomRight.get(br);
float* start = 0;
float* end = 0;
if (activeName == topName)
{
start = tl;
end = tr;
}
if (activeName == bottomName)
{
start = bl;
end = br;
}
if (activeName == rightName)
{
start = tr;
end = br;
}
if (activeName == leftName)
{
start = tl;
end = bl;
}
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(mousePointGlName, cpt))
{
mousePointGlName.
x -= cpt.
x;
mousePointGlName.y -= cpt.
y;
mousePointGlName.z -= cpt.
z;
}
start[0] += (float)mousePointGlName.x;
start[1] += (float)mousePointGlName.y;
start[2] +=(float) mousePointGlName.z;
end[0] += (float)mousePointGlName.x;
end[1] += (float)mousePointGlName.y;
end[2] +=(float) mousePointGlName.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 squareScaleManipulatorData :
public MUserData
{
public:
enum ManipEdge
{
kTop,
kRight,
kBottom,
kLeft
};
squareScaleManipulatorData()
, tl(0), tr(0), br(0), bl(0), activeEdge(
kNone) {}
virtual ~squareScaleManipulatorData() {}
float* tl; float* tr; float* br; float* bl;
ManipEdge activeEdge;
};
squareScaleManipulatorOverride::squareScaleManipulatorOverride(
const MObject& obj)
: MHWRender::MPxDrawOverride(obj, squareScaleManipulatorOverride::draw)
{
}
squareScaleManipulatorOverride::~squareScaleManipulatorOverride()
{
}
{
}
bool squareScaleManipulatorOverride::isBounded(
{
return true;
}
{
squareScaleManipulator::topLeft,
squareScaleManipulator::bottomRight);
}
bool squareScaleManipulatorOverride::disableInternalBoundingBoxDraw() const
{
return true;
}
MUserData* squareScaleManipulatorOverride::prepareForDraw(
{
if (!status) return NULL;
squareScaleManipulator* manipNode =
dynamic_cast<squareScaleManipulator*>(node.userNode());
if (!manipNode) return NULL;
squareScaleManipulatorData* data =
dynamic_cast<squareScaleManipulatorData*>(oldData);
if (!data)
{
data = new squareScaleManipulatorData();
}
data->tl = data->tr = data->br = data->bl = 0;
data->activeEdge = squareScaleManipulatorData::kNone;
if (manipNode->shouldDraw(cameraPath))
{
data->tl = manipNode->tl;
data->tr = manipNode->tr;
data->br = manipNode->br;
data->bl = manipNode->bl;
if (manipNode->activeName == manipNode->topName)
{
data->activeEdge = squareScaleManipulatorData::kTop;
}
else if (manipNode->activeName == manipNode->rightName)
{
data->activeEdge = squareScaleManipulatorData::kRight;
}
else if (manipNode->activeName == manipNode->bottomName)
{
data->activeEdge = squareScaleManipulatorData::kBottom;
}
else if (manipNode->activeName == manipNode->leftName)
{
data->activeEdge = squareScaleManipulatorData::kLeft;
}
}
return data;
}
void squareScaleManipulatorOverride::draw(
{
const squareScaleManipulatorData* manipData =
dynamic_cast<const squareScaleManipulatorData*>(data);
if (stateMgr && manipData &&
manipData->tl && manipData->tr && manipData->br && manipData->bl)
{
if (oldDepthState)
{
if (!depthState)
{
}
if (depthState)
{
}
}
static const float dormantColor[4] = { 0.39f, 0.94f, 1.0f, 1.0f };
static const float activeColor[4] = { 1.0f, 1.0f, 0.0f, 1.0f };
glColor4fv(manipData->activeEdge == squareScaleManipulatorData::kTop ? activeColor : dormantColor);
glBegin(GL_LINES);
glVertex3fv(manipData->tl);
glVertex3fv(manipData->tr);
glEnd();
glColor4fv(manipData->activeEdge == squareScaleManipulatorData::kRight ? activeColor : dormantColor);
glBegin(GL_LINES);
glVertex3fv(manipData->tr);
glVertex3fv(manipData->br);
glEnd();
glColor4fv(manipData->activeEdge == squareScaleManipulatorData::kBottom ? activeColor : dormantColor);
glBegin(GL_LINES);
glVertex3fv(manipData->br);
glVertex3fv(manipData->bl);
glEnd();
glColor4fv(manipData->activeEdge == squareScaleManipulatorData::kLeft ? activeColor : dormantColor);
glBegin(GL_LINES);
glVertex3fv(manipData->bl);
glVertex3fv(manipData->tl);
glEnd();
if (oldDepthState)
{
}
}
}
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,
&squareScaleManipulator::classification);
if (!status) {
status.
perror(
"registerNode");
return status;
}
status = _squareManipCmd.registerCommand( obj );
if (!status) {
status.
perror(
"registerCommand");
return status;
}
squareScaleManipulator::classification,
squareScaleManipulator::registrantId,
squareScaleManipulatorOverride::Creator);
if (!status) {
status.
perror(
"registerDrawOverrideCreator");
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;
}
squareScaleManipulator::classification,
squareScaleManipulator::registrantId);
if (!status) {
status.
perror(
"deregisterDrawOverrideCreator");
return status;
}
return status;
}