ActionTimeline* ActionTimelineCache::createActionWithFlatBuffersFile(const std::string &fileName)
{
    ActionTimeline* action = _animationActions.at(fileName);
    if (action == NULL)
    {
        action = loadAnimationActionWithFlatBuffersFile(fileName);
    }
    return action->clone();
}
ActionTimeline* ActionTimelineCache::createActionFromJson(const std::string& fileName)
{
    ActionTimeline* action = _animationActions.at(fileName);
    if (action == nullptr)
    {
        action = loadAnimationActionWithFile(fileName);
    }
    return action->clone();
}
ActionTimeline* ActionTimelineCache::createActionWithDataBuffer(Data data, const std::string &fileName)
{
    ActionTimeline* action = _animationActions.at(fileName);
    if (action == NULL)
    {
        action = loadAnimationWithDataBuffer(data, fileName);
    }
    return action->clone();
}
ActionTimeline* ActionTimelineCache::createAction(const std::string& fileName)
{
    ActionTimeline* action = static_cast<ActionTimeline*>(_timelineActions->objectForKey(fileName));
    if (action == NULL)
    {
        action = loadAnimationActionWithFile(fileName);
    }
    return action->clone();
}
예제 #5
0
//TestActionTimelineSkeleton
void TestActionTimelineSkeleton::onEnter()
{
    ActionTimelineBaseTest::onEnter();

    _changedDisplays = _changedDisplay = false;
    Node* node = CSLoader::createNode("ActionTimeline/DemoPlayer_skeleton.csb");
    ActionTimeline* action = CSLoader::createTimeline("ActionTimeline/DemoPlayer_skeleton.csb");
    node->runAction(action);
    node->setScale(0.2f);
    node->setPosition(150, 150);
    action->gotoFrameAndPlay(0);
    addChild(node);

    auto skeletonNode = static_cast<SkeletonNode*>(node);
    const std::string weapBoneName = "Layer20";
    auto weaponHandeBone = skeletonNode->getBoneNode(weapBoneName);

    /***********   debug draw bones  *************/
    auto boneDrawsBtn = cocos2d::ui::Button::create();
    addChild(boneDrawsBtn);
    boneDrawsBtn->setPosition(Vec2(VisibleRect::right().x - 30, VisibleRect::top().y - 30));
    boneDrawsBtn->setTitleText("Draw bone");

    skeletonNode->setDebugDrawEnabled(true);
    boneDrawsBtn->addClickEventListener([skeletonNode, this](Ref* sender)
    {
        skeletonNode->setDebugDrawEnabled(!skeletonNode->isDebugDrawEnabled());
    });


    /***************** change bone display **************************/

    // add display
    auto weapSkinToAdd = Sprite::create("ActionTimeline/testAnimationResource/girl_arms.png");
    weapSkinToAdd->setName("Knife");
    weapSkinToAdd->setPosition(Vec2(135, 23));
    weapSkinToAdd->setScale(3.0f);
    weapSkinToAdd->setRotation(86);
    weaponHandeBone->addSkin(weapSkinToAdd, false);

    // change display
    auto changeBoneDispBtn = cocos2d::ui::Button::create();
    addChild(changeBoneDispBtn);
    changeBoneDispBtn->setPosition(Vec2(VisibleRect::right().x - 60, VisibleRect::top().y - 60));
    changeBoneDispBtn->setTitleText("change bone display");
    changeBoneDispBtn->addClickEventListener([weapSkinToAdd, weaponHandeBone](Ref* sender)
    {
        // or use skeletonNode->display(bone name, skin name, hide)
        if (weapSkinToAdd->isVisible())
            weaponHandeBone->displaySkin("3", true);
        else
        {
            weaponHandeBone->displaySkin(weapSkinToAdd, true);
        }
    });


    /*************** debug draw boundingbox and transforms ***************/
    auto debugDrawNode = DrawNode::create();
    addChild(debugDrawNode);

    auto drawBoxBtn = cocos2d::ui::Button::create();
    addChild(drawBoxBtn);
    drawBoxBtn->setPosition(Vec2(VisibleRect::right().x - 30, VisibleRect::top().y - 45));
    drawBoxBtn->setTitleText("Draw Box");


    drawBoxBtn->addClickEventListener([debugDrawNode](Ref* sender)
    {
        debugDrawNode->setVisible(!debugDrawNode->isVisible());
    });
    skeletonNode->schedule([skeletonNode, weaponHandeBone, debugDrawNode](float interval)
    {
        if (debugDrawNode->isVisible())
        {
            debugDrawNode->clear();
            // skeleton boundingbox
            auto rect = skeletonNode->getBoundingBox();
            cocos2d::Vec2 leftbottom(rect.getMinX(), rect.getMinY());
            cocos2d::Vec2 righttop(rect.getMaxX(), rect.getMaxY());
            debugDrawNode->drawRect(leftbottom, righttop, cocos2d::Color4F::YELLOW);

            // bone boundingbox
            rect = weaponHandeBone->getBoundingBox();
            leftbottom.x = rect.getMinX();
            leftbottom.y = rect.getMinY();
            righttop.x = rect.getMaxX();
            righttop.y = rect.getMaxY();
            cocos2d::Vec2 lefttop(rect.getMinX(), rect.getMaxY());
            cocos2d::Vec2 rightbottom(rect.getMaxX(), rect.getMinY());
            auto skeletonToP = skeletonNode->getNodeToParentAffineTransform();
            auto bonePtoSkeletonPTrans = AffineTransformConcat(
                                             static_cast<BoneNode*>((weaponHandeBone->getParent())
                                                                   )->getNodeToParentAffineTransform(skeletonNode),
                                             skeletonToP);
            leftbottom = PointApplyAffineTransform(leftbottom, bonePtoSkeletonPTrans);
            righttop = PointApplyAffineTransform(righttop, bonePtoSkeletonPTrans);
            lefttop = PointApplyAffineTransform(lefttop, bonePtoSkeletonPTrans);
            rightbottom = PointApplyAffineTransform(rightbottom, bonePtoSkeletonPTrans);
            debugDrawNode->drawLine(leftbottom, rightbottom, Color4F::BLUE);
            debugDrawNode->drawLine(rightbottom, righttop, Color4F::BLUE);
            debugDrawNode->drawLine(righttop, lefttop, Color4F::BLUE);
            debugDrawNode->drawLine(lefttop, leftbottom, Color4F::BLUE);

            // skin boundingbox

            // get displaying nodes
            auto currentskin = weaponHandeBone->getVisibleSkins().front();
            rect = currentskin->getBoundingBox();
            leftbottom.x = rect.getMinX();
            leftbottom.y = rect.getMinY();
            righttop.x = rect.getMaxX();
            righttop.y = rect.getMaxY();
            lefttop.x = rect.getMinX();
            lefttop.y =  rect.getMaxY();
            rightbottom.x = rect.getMaxX();
            rightbottom.y = rect.getMinY();
            auto boneToSkeletonParentTrans = AffineTransformConcat(
                                                 weaponHandeBone->getNodeToParentAffineTransform(skeletonNode), skeletonToP);
            leftbottom = PointApplyAffineTransform(leftbottom, boneToSkeletonParentTrans);
            righttop = PointApplyAffineTransform(righttop, boneToSkeletonParentTrans);
            lefttop = PointApplyAffineTransform(lefttop, boneToSkeletonParentTrans);
            rightbottom = PointApplyAffineTransform(rightbottom, boneToSkeletonParentTrans);

            debugDrawNode->drawLine(leftbottom, rightbottom, Color4F::GREEN);
            debugDrawNode->drawLine(rightbottom, righttop, Color4F::GREEN);
            debugDrawNode->drawLine(righttop, lefttop, Color4F::GREEN);
            debugDrawNode->drawLine(lefttop, leftbottom, Color4F::GREEN);
        }
    }, 0, "update debug draw");


    // change displays , can be use for dress up a skeleton
    auto changeBoneDispsBtn = cocos2d::ui::Button::create();
    addChild(changeBoneDispsBtn);
    changeBoneDispsBtn->setPosition(Vec2(VisibleRect::right().x - 60, VisibleRect::top().y - 75));
    changeBoneDispsBtn->setTitleText("change bone displays");

    std::map < std::string, std::string> boneSkinNames;
    boneSkinNames.insert(std::make_pair("Layer20", "fire"));
    boneSkinNames.insert(std::make_pair("Layer14", "fruit"));
    skeletonNode->addSkinGroup("fruitKnife", boneSkinNames);

    std::map < std::string, std::string> boneSkinNames2;
    boneSkinNames2.insert(std::make_pair("Layer20", "3"));
    boneSkinNames2.insert(std::make_pair("Layer14", "hat"));
    skeletonNode->addSkinGroup("cowboy", boneSkinNames2);

    changeBoneDispsBtn->addClickEventListener([skeletonNode, this](Ref* sender)
    {
        if (!_changedDisplays)
        {
            skeletonNode->changeSkins("fruitKnife");
            _changedDisplays = true;
        }
        else
        {
            skeletonNode->changeSkins("cowboy");
            _changedDisplays = false;
        }
    });


    /*********** test cases for bugs        **********/
    // bug: #13060 https://github.com/cocos2d/cocos2d-x/issues/13060
    // bug: bone draw at the other edge when move to outside right edge.
    BoneNode* bugtestBoneNode = BoneNode::create(500);
    bugtestBoneNode->setRotation(-10);
    bugtestBoneNode->retain();
    bugtestBoneNode->setDebugDrawEnabled(true);
    bugtestBoneNode->setPosition(Vec2(1500, VisibleRect::top().y - 90));
    auto bug13060Btn = cocos2d::ui::Button::create();
    bug13060Btn->setPosition(Vec2(VisibleRect::right().x - 30, VisibleRect::top().y - 90));
    bug13060Btn->setTitleText("bug #13060");
    addChild(bug13060Btn);
    bug13060Btn->addClickEventListener([bugtestBoneNode, skeletonNode](Ref* sender)
    {
        if (bugtestBoneNode->getParent() == nullptr)
            skeletonNode->addChild(bugtestBoneNode);
        else
            bugtestBoneNode->removeFromParent();
        // bug fixed while bugtestBoneNode not be drawn at the bottom edge
    });

    // bug: #13005 https://github.com/cocos2d/cocos2d-x/issues/#13005
    // bug: BoneNode 's debugdraw can not be controlled by ancestor's visible
    auto leftleg = skeletonNode->getBoneNode("Layer26");
    auto bug13005Btn = cocos2d::ui::Button::create();
    addChild(bug13005Btn);
    bug13005Btn->setPosition(Vec2(VisibleRect::right().x - 30, VisibleRect::top().y - 105));
    bug13005Btn->setTitleText("bug #13005");
    bug13005Btn->addClickEventListener([leftleg](Ref* sender)
    {
        leftleg->setVisible(!leftleg->isVisible());
        // bug fixed while leftleg's child hide with leftleg's visible
    });


    /*************    Skeleton nest Skeleton test       *************/
    auto nestSkeletonBtn = cocos2d::ui::Button::create();
    nestSkeletonBtn->setTitleText("Skeleton Nest");
    nestSkeletonBtn->setPosition(Vec2(VisibleRect::right().x - 40, VisibleRect::top().y - 120));
    addChild(nestSkeletonBtn);
    auto nestSkeleton = static_cast<SkeletonNode*>(CSLoader::createNode("ActionTimeline/DemoPlayer_skeleton.csb"));
    nestSkeleton->retain();
    ActionTimeline* nestSkeletonAction = action->clone();
    nestSkeletonAction->retain();
    nestSkeleton->runAction(nestSkeletonAction);
    nestSkeleton->setScale(0.2f);
    nestSkeleton->setPosition(150, 300);
    nestSkeletonAction->gotoFrameAndPlay(0);
    // show debug draws, or comment this for hide bones draws
    for (auto& nestbonechild : nestSkeleton->getAllSubBonesMap())
    {
        nestbonechild.second->setDebugDrawEnabled(true);
    }

    nestSkeletonBtn->addClickEventListener([leftleg, nestSkeleton, nestSkeletonAction](Ref* sender)
    {
        if (nestSkeleton->getParent() == nullptr)
        {
            leftleg->addChild(nestSkeleton);
        }
        else
        {
            nestSkeleton->removeFromParentAndCleanup(false);
        }
    });
}