undoRedoMsgCmd.cpp
#include <maya/MFnPlugin.h>
#include <maya/MStatus.h>
#include <maya/MPxCommand.h>
#include <maya/MMessage.h>
#include <maya/MEventMessage.h>
#include <maya/MGlobal.h>
#include <maya/MArgList.h>
#include <maya/MCallbackIdArray.h>
#define UndoString "Undo"
#define RedoString "Redo"
static MCallbackIdArray callbackIds;
class undoRedoMsg : public MPxCommand
{
public:
virtual MStatus doIt( const MArgList& );
void runTests();
static void *creator();
};
static void undoCB( void *clientData )
{
MString info = "undoCallback : clientData = ";
if ( clientData )
info += (unsigned)(MUintPtrSz)clientData;
else
info += "NULL";
MGlobal::displayInfo( info );
}
static void redoCB( void *clientData )
{
MString info = "redoCallback : clientData ";
if ( clientData )
info += (unsigned)(MUintPtrSz) clientData;
else
info += "NULL";
MGlobal::displayInfo( info );
}
MStatus undoRedoMsg::doIt( const MArgList& args )
{
MStatus result = MS::kSuccess;
if ( args.length() > 0 )
{
MString argStr;
unsigned last = args.length();
for ( unsigned i = 0; i < last; i++ )
{
args.get( i, argStr );
if ( argStr == "add" )
{
MCallbackId undoId = MEventMessage::addEventCallback( UndoString, undoCB, NULL, &result );
if ( MS::kSuccess != result )
return MS::kFailure;
callbackIds.append( undoId );
MCallbackId redoId = MEventMessage::addEventCallback( RedoString, redoCB, NULL, &result );
if ( MS::kSuccess != result )
return MS::kFailure;
callbackIds.append( redoId );
}
else if ( argStr == "remove" )
{
if ( MS::kSuccess != MMessage::removeCallbacks( callbackIds ) )
return MS::kFailure;
}
else
{
MGlobal::displayInfo("Failure condition");
return MS::kFailure;
}
}
}
return result;
}
void* undoRedoMsg::creator()
{
return new undoRedoMsg;
}
MStatus initializePlugin(MObject obj)
{
MFnPlugin pluginFn(obj, PLUGIN_COMPANY, "6.0");
MStatus status;
status = pluginFn.registerCommand("undoRedoMsg", undoRedoMsg::creator);
if( !status)
status.perror("register Command failed");
return status;
}
MStatus uninitializePlugin ( MObject obj )
{
MFnPlugin pluginFn(obj);
MStatus status = MS::kSuccess;
MMessage::removeCallbacks( callbackIds );
status = pluginFn.deregisterCommand("undoRedoMsg");
return status;
}