zoukankan      html  css  js  c++  java
  • 七、context command

    context command是用来新建自己的工具,可以调用OPENGL,获取鼠标操作函数,在view窗口画自己想画的东西。(我是这麽理解的,可以以后再确定一下)

    下面是一个context command的例子,通过例子来说明context command的代码怎么写,又怎么样来activate。

    包含的头文件:

    #include <maya/MIOStream.h>
    #include <math.h>
    #include <stdlib.h>
    
    #include <maya/MFnPlugin.h>
    #include <maya/MString.h>
    #include <maya/MGlobal.h>
    #include <maya/M3dView.h>
    #include <maya/MDagPath.h>
     
    #include <maya/MItSelectionList.h>
    #include <maya/MSelectionList.h>
    
    #include <maya/MPxContextCommand.h>
    #include <maya/MPxContext.h>
    #include <maya/MEvent.h>
    
    #include <maya/MUIDrawManager.h>
    #include <maya/MFrameContext.h>
    #include <maya/MPoint.h>
    #include <maya/MColor.h>
    
    #include <GL/gl.h>
    #include <GL/glu.h>

    1. 定义继承于MPxContext的类:

    const char helpString[] =
                "Click with left button or drag with middle button to select";
    
    class marqueeContext : public MPxContext
    {
    public:
        marqueeContext();
        virtual void    toolOnSetup( MEvent & event );
    
        // Default viewport or hardware viewport methods override, will not be triggered in viewport 2.0.
        virtual MStatus    doPress( MEvent & event );
        virtual MStatus    doDrag( MEvent & event );
        virtual MStatus    doRelease( MEvent & event );
        virtual MStatus    doEnterRegion( MEvent & event );
    
        // Viewport 2.0 methods, will only be triggered in viewport 2.0.
        virtual MStatus        doPress ( MEvent & event,     MHWRender::MUIDrawManager& drawMgr, const MHWRender::MFrameContext& context);
        virtual MStatus        doRelease( MEvent & event, MHWRender::MUIDrawManager& drawMgr, const MHWRender::MFrameContext& context);
        virtual MStatus        doDrag ( MEvent & event, MHWRender::MUIDrawManager& drawMgr, const MHWRender::MFrameContext& context);
    
    private:
        // Marquee draw method in default viewport or hardware viewport with immediate OpenGL call
        void                    drawMarqueeGL();
        // Common operation to handle when pressed
        void                    doPressCommon( MEvent & event );
        // Common operation to handle when released
        void                    doReleaseCommon( MEvent & event );
    
        short                    start_x, start_y;
        short                    last_x, last_y;
    
        bool                    fsDrawn;
    
        MGlobal::ListAdjustment    listAdjustment;
        M3dView                    view;
    };

    其中doPress(), doRelease(), doDrag()函数,为函数重载,以方便不同版本的view进行绘制。

    doPressCommon(), doReleaseCommon(), drawMarqueeGL()函数为上面三个函数不同版本之间的共同部分。以doDrag()为例子,具体参看document。

    MStatus marqueeContext::doDrag( MEvent & event )
    //
    // Drag out the marquee (using OpenGL)
    //
    {
        view.beginXorDrawing();
    
    
        if (fsDrawn) {
            // Redraw the marquee at its old position to erase it.
            drawMarqueeGL();
        }
    
        fsDrawn = true;
    
        //    Get the marquee's new end position.
        event.getPosition( last_x, last_y );
    
        // Draw the marquee at its new position.
        drawMarqueeGL();
    
        view.endXorDrawing();
    
        return MS::kSuccess;        
    }

    void marqueeContext::drawMarqueeGL()
    {
      glBegin( GL_LINE_LOOP );
        glVertex2i( start_x, start_y );
        glVertex2i( last_x, start_y );
        glVertex2i( last_x, last_y );
        glVertex2i( start_x, last_y );
      glEnd();
    }

    2. 编好自己的MPxContext之后,创建MPxContextCommand的子类来创建context 命令,该命令用来创建context工具。

    class marqueeContextCmd : public MPxContextCommand
    {
    public:    
                            marqueeContextCmd();
        virtual MPxContext*    makeObj();
        static    void*        creator();
    };
    
    marqueeContextCmd::marqueeContextCmd() {}
    
    MPxContext* marqueeContextCmd::makeObj()
    {
        return new marqueeContext();
    }
    
    void* marqueeContextCmd::creator()
    {
        return new marqueeContextCmd;
    }

    3. 最后通过MFnPlugin函数注册context command:

    MStatus initializePlugin( MObject obj )
    {
        MStatus        status;
        MFnPlugin    plugin( obj, PLUGIN_COMPANY, "12.0", "Any");
    
        status = plugin.registerContextCommand( "marqueeToolContext",
                                                marqueeContextCmd::creator );
        return status;
    }
    
    MStatus uninitializePlugin( MObject obj )
    {
        MStatus        status;
        MFnPlugin    plugin( obj );
    
        status = plugin.deregisterContextCommand( "marqueeToolContext" );
    
        return status;
    }

    4. Adding a context command to the Maya shelf

    There are two ways to "activate" or make your context the current context in Maya. The first is through the use of the setToolTo command. This command takes the name of a context (tool) and makes it the current context.

    A second method is by making an icon to represent your context and putting it in the Maya tool shelf. The Maya tool shelf can store two kinds of buttons, command buttons and tool buttons. When the tool is activated, its icon is displayed next to the standard Maya tools in the toolbar.

    The following is a set of MEL commands you can use to create a context and tool button for the context.

    marqueeToolContext marqueeToolContext1;
    setParent Shelf1;    //注意这的shelf1应该用shelf子目录的名字代替,例如Custom
    toolButton -cl toolCluster
     -t marqueeToolContext1
     -i1 "marqueeTool.xpm" marqueeTool1;

    This MEL code instantiates an instance of the marqueeToolContext and adds it to the "Common" tools shelf.

    marqueeTool.xpm, the icon for the tool, must be in the XBMLANGPATH to be found and added to the UI. If it is not found, a blank spot will appear on the shelf, but the tool will still be usable.

    This code could either be sourced by hand from the MEL command window, or it could be invoked withMGlobal::sourceFile() in the initializePlugin() method of the plug-in.

     

  • 相关阅读:
    scanf和cin的返回值
    C++ STL中Map的按Key排序跟按Value排序
    name lookup of 'res' changed for new ISO 'res' scoping
    string中c_str()、data()、copy(p,n)函数的用法
    C中malloc的使用(转)
    codeforces 653B B. Bear and Compressing(dfs)
    codeforces 653A A. Bear and Three Balls(水题)
    hdu-5646 DZY Loves Partition(贪心)
    hdu-5645 DZY Loves Balls(水题)
    codeforces 655D D. Robot Rapping Results Report(拓扑排序+拓扑序记录)
  • 原文地址:https://www.cnblogs.com/bubbler/p/5156788.html
Copyright © 2011-2022 走看看