객체 텍스쳐 복사

2808 단어

-- 
function g_CutPicture(pContainer)
    local winSize = cc.Director:getInstance():getWinSize();
    local aniSize = pContainer:getContentSize();
    --aniSize = cc.size(120,200);
    local aniAnchorPoint = cc.p( pContainer:getAnchorPoint()  );
    --print(aniAnchorPoint.x,aniAnchorPoint.y);
    aniAnchorPoint.x = 0.5;
    local worldPos = pContainer:getParent():convertToWorldSpace( cc.p( pContainer:getPosition() ) );

    local cutSizeWidth   = worldPos.x + aniSize.width*(1 - aniAnchorPoint.x);
    local cutSizeHeight  = worldPos.y + aniSize.height*(1 - aniAnchorPoint.y);

    -- cutSizeWidth = cutSizeWidth > 0 and cutSizeWidth or -1;
    -- cutSizeHeight = cutSizeHeight > 0 and cutSizeHeight or -1;
    -- print(cutSizeWidth,cutSizeHeight);

    local rt = nil; 
    if cutSizeWidth > 0 and cutSizeHeight > 0 then
        rt = cc.RenderTexture:create(  cutSizeWidth  ,  cutSizeHeight );
    else
        print("-----------------------------------------");
        print(pContainer:getPositionX(),pContainer:getPositionY());
        print( "worldPos.x = "..worldPos.x.."; worldPos.y = "..worldPos.y );
        print(cutSizeWidth,cutSizeHeight);
    end

    if rt  then
        
        rt:retain();
        rt:beginWithClear(255,255,255,0); 
        --pContainer:visit();
        -- 
        function drawAllNode( node )
            if node:getChildrenCount() > 0 and node:isVisible() == true then
                for k,v in pairs( node:getChildren() ) do
                     drawAllNode(v);
                    -- v:visit();
                end
            end
            if node:isVisible() == true then
                node:visit();
            end
        end
        drawAllNode(pContainer);
        rt:endToLua();
       
        
        local texture = rt:getSprite():getTexture(); 

        local cutX  = worldPos.x - aniSize.width*aniAnchorPoint.x;
        local maxX  = aniSize.width ;
        -- if cutX < 0 then
        --  --maxX = maxX + cutX;
        --  --cutX = 0;
        -- end

        local cutY  = worldPos.y - aniSize.height*aniAnchorPoint.y;
        local maxY  = aniSize.height ;
        -- if maxY < 0 then
        --  --maxY = maxY + cutY;
        --  --cutY = 0;
        -- end

        local texRect = cc.rect( cutX,
                        cutY,
                        maxX,
                        maxY );
        -- for k,v in pairs(texRect) do
        --  print(k,v)
        -- end
        if texture then
            local pSprite = cc.Sprite:createWithTexture(texture,texRect  );
            pSprite:setFlippedY(true);
            pSprite:setAnchorPoint( aniAnchorPoint );
            return pSprite; 
        end
        rt:release();
        rt = nil
    end

    return nil;
    
end

좋은 웹페이지 즐겨찾기