Merge pull request #12854 from yangws/issue_11310

fixed #11310 : multiple camera case utils::captureScreen bug.
This commit is contained in:
pandamicro 2015-07-15 15:00:29 +08:00
commit 99e504e668
2 changed files with 68 additions and 7 deletions

View File

@ -29,6 +29,7 @@ THE SOFTWARE.
#include "base/CCDirector.h" #include "base/CCDirector.h"
#include "base/CCAsyncTaskPool.h" #include "base/CCAsyncTaskPool.h"
#include "base/CCEventDispatcher.h"
#include "renderer/CCCustomCommand.h" #include "renderer/CCCustomCommand.h"
#include "renderer/CCRenderer.h" #include "renderer/CCRenderer.h"
#include "platform/CCImage.h" #include "platform/CCImage.h"
@ -151,12 +152,24 @@ void onCaptureScreen(const std::function<void(bool, const std::string&)>& afterC
/* /*
* Capture screen interface * Capture screen interface
*/ */
static EventListenerCustom* s_captureScreenListener;
static CustomCommand s_captureScreenCommand;
void captureScreen(const std::function<void(bool, const std::string&)>& afterCaptured, const std::string& filename) void captureScreen(const std::function<void(bool, const std::string&)>& afterCaptured, const std::string& filename)
{ {
static CustomCommand captureScreenCommand; if (s_captureScreenListener)
captureScreenCommand.init(std::numeric_limits<float>::max()); {
captureScreenCommand.func = std::bind(onCaptureScreen, afterCaptured, filename); CCLOG("Warning: CaptureScreen has been caled yet, don't call more than once in one frame.");
Director::getInstance()->getRenderer()->addCommand(&captureScreenCommand); return;
}
s_captureScreenCommand.init(std::numeric_limits<float>::max());
s_captureScreenCommand.func = std::bind(onCaptureScreen, afterCaptured, filename);
s_captureScreenListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_AFTER_DRAW, [](EventCustom *event) {
auto director = Director::getInstance();
director->getEventDispatcher()->removeEventListener((EventListener*)(s_captureScreenListener));
s_captureScreenListener = nullptr;
director->getRenderer()->addCommand(&s_captureScreenCommand);
director->getRenderer()->render();
});
} }
std::vector<Node*> findChildren(const Node &node, const std::string &name) std::vector<Node*> findChildren(const Node &node, const std::string &name)

View File

@ -105,6 +105,10 @@ private:
std::vector<std::string> _skins[(int)SkinType::MAX_TYPE]; //all skins std::vector<std::string> _skins[(int)SkinType::MAX_TYPE]; //all skins
int _curSkin[(int)SkinType::MAX_TYPE]; //current skin index int _curSkin[(int)SkinType::MAX_TYPE]; //current skin index
cocos2d::Sprite3D* _reskinGirl; cocos2d::Sprite3D* _reskinGirl;
// for capture screen
static const int SNAPSHOT_TAG = 119;
std::string _snapshotFile;
}; };
/** Define the sub scenes in test. */ /** Define the sub scenes in test. */
@ -652,6 +656,50 @@ void Scene3DTestScene::createDetailDlg()
title->setPosition(dlgSize.width / 2, dlgSize.height - margin * 2); title->setPosition(dlgSize.width / 2, dlgSize.height - margin * 2);
_detailDlg->addChild(title); _detailDlg->addChild(title);
// add capture screen buttons
ui::Button* capture = ui::Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
capture->setScale(0.5);
capture->setAnchorPoint(Vec2(0.5, 0));
capture->setPosition(Vec2(dlgSize.width / 3, margin));
capture->addClickEventListener([this](Ref* sender)
{
Director::getInstance()->getTextureCache()->removeTextureForKey(_snapshotFile);
_osdScene->removeChildByTag(SNAPSHOT_TAG);
_snapshotFile = "CaptureScreenTest.png";
utils::captureScreen([this](bool succeed, const std::string& outputFile)
{
if (!succeed)
{
log("Capture screen failed.");
return;
}
auto sp = Sprite::create(outputFile);
_osdScene->addChild(sp, 0, SNAPSHOT_TAG);
Size s = Director::getInstance()->getWinSize();
sp->setPosition(s.width / 2, s.height / 2);
sp->setScale(0.25);
_snapshotFile = outputFile;
}, _snapshotFile);
});
capture->setTitleText("Take Snapshot");
capture->setName("Take Snapshot");
_detailDlg->addChild(capture);
ui::Button* remove = ui::Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
remove->setScale(0.5);
remove->setAnchorPoint(Vec2(0.5, 0));
remove->setPosition(Vec2(dlgSize.width * 2 / 3, margin));
remove->addClickEventListener([this](Ref* sender)
{
_osdScene->removeChildByTag(SNAPSHOT_TAG);
});
remove->setTitleText("Del Snapshot");
remove->setName("Del Snapshot");
_detailDlg->addChild(remove);
// add a spine ffd animation on it // add a spine ffd animation on it
auto skeletonNode = auto skeletonNode =
SkeletonAnimationCullingFix::createWithFile("spine/goblins-ffd.json", SkeletonAnimationCullingFix::createWithFile("spine/goblins-ffd.json",
@ -662,7 +710,7 @@ void Scene3DTestScene::createDetailDlg()
skeletonNode->setScale(0.25); skeletonNode->setScale(0.25);
Size windowSize = Director::getInstance()->getWinSize(); Size windowSize = Director::getInstance()->getWinSize();
skeletonNode->setPosition(Vec2(dlgSize.width / 2, 20)); skeletonNode->setPosition(Vec2(dlgSize.width / 2, remove->getContentSize().height / 2 + 2 * margin));
_detailDlg->addChild(skeletonNode); _detailDlg->addChild(skeletonNode);
} }
@ -706,7 +754,7 @@ void Scene3DTestScene::createDescDlg()
"- OSD scene contains description dialog.\n" "- OSD scene contains description dialog.\n"
"\n" "\n"
"Click \"Description\" button to hide this dialog.\n"); "Click \"Description\" button to hide this dialog.\n");
auto text = Label::createWithSystemFont(desc, "Helvetica", 9, textSize); auto text = Label::createWithSystemFont(desc, "", 9, textSize);
text->setAnchorPoint(Vec2(0, 1)); text->setAnchorPoint(Vec2(0, 1));
text->setPosition(textPos); text->setPosition(textPos);
_descDlg->addChild(text); _descDlg->addChild(text);