Merge branch 'v3_addClearforCamera' of github.com:dabingnn/cocos2d-x into v3_addClearforCamera

This commit is contained in:
Huabing.Xu 2015-05-12 10:08:12 +08:00
commit 35940ba8ef
1 changed files with 354 additions and 156 deletions

View File

@ -1,23 +1,27 @@
#include "Scene3DTest.h" #include "Scene3DTest.h"
#include "ui/CocosGUI.h"
#include <spine/spine-cocos2dx.h>
#include "../testResource.h" #include "../testResource.h"
#include "TerrainTest.h" #include "TerrainTest.h"
USING_NS_CC; USING_NS_CC;
using namespace spine;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Implements Scene3DTestScene // Implements Scene3DTestScene
static Vec3 camera_offset(0, 45, 60);
class Scene3DTestScene : public TestCase class Scene3DTestScene : public TerrainWalkThru
{ {
public: public:
CREATE_FUNC(Scene3DTestScene); CREATE_FUNC(Scene3DTestScene);
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event);
void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
void onTouchesEnd(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event); cocos2d::EventListenerCustom* _backToForegroundListener;
#endif
private: private:
Scene3DTestScene(); Scene3DTestScene();
virtual ~Scene3DTestScene(); virtual ~Scene3DTestScene();
@ -25,52 +29,58 @@ private:
void create3DWorld(); void create3DWorld();
void createUI(); void createUI();
Node* createDialog(); void createPlayerDlg();
void createDetailDlg();
void createDescDlg();
std::vector<Camera *> _gameCameras; std::vector<Camera *> _gameCameras;
Terrain *_terrain; Node* _playerDlg;
Player *_player; Node* _detailDlg;
Node* _descDlg;
static const int DLG_COUNT = 3; TextureCube* _textureCube;
std::vector<Node*> _dlgs; Skybox* _skyBox;
}; };
enum LAYER_DEPTH { enum GAME_LAYER {
LAYER_DEPTH_GAME = 0, LAYER_SKYBOX = 0,
LAYER_DEPTH_UI, LAYER_GAME,
LAYER_DEPTH_ACTOR, LAYER_UI,
LAYER_DEPTH_ZOOM, LAYER_ACTOR,
LAYER_DEPTH_COUNT, LAYER_ZOOM,
LAYER_OSD,
LAYER_COUNT,
}; };
static CameraFlag s_CF[LAYER_DEPTH_COUNT] = { static CameraFlag s_CF[LAYER_COUNT] = {
CameraFlag::DEFAULT, CameraFlag::DEFAULT,
CameraFlag::USER1, CameraFlag::USER1,
CameraFlag::USER2, CameraFlag::USER2,
CameraFlag::USER3, CameraFlag::USER3,
CameraFlag::USER4,
CameraFlag::USER5,
}; };
static unsigned short s_CM[LAYER_DEPTH_COUNT] = { static unsigned short s_CM[LAYER_COUNT] = {
(unsigned short)s_CF[0], (unsigned short)s_CF[0],
(unsigned short)s_CF[1], (unsigned short)s_CF[1],
(unsigned short)s_CF[2], (unsigned short)s_CF[2],
(unsigned short)s_CF[3], (unsigned short)s_CF[3],
(unsigned short)s_CF[4],
(unsigned short)s_CF[5],
}; };
Scene3DTestScene::Scene3DTestScene() Scene3DTestScene::Scene3DTestScene()
: _terrain(nullptr) : _playerDlg(nullptr)
, _player(nullptr) , _detailDlg(nullptr)
, _descDlg(nullptr)
{ {
} }
Scene3DTestScene::~Scene3DTestScene() Scene3DTestScene::~Scene3DTestScene()
{ {
for (auto dlg : _dlgs) {
CC_SAFE_RELEASE(dlg);
}
} }
bool Scene3DTestScene::init() bool Scene3DTestScene::init()
@ -78,66 +88,67 @@ bool Scene3DTestScene::init()
bool ret = false; bool ret = false;
do do
{ {
CC_BREAK_IF(false == TestCase::init()); CC_BREAK_IF(false == TerrainWalkThru::init());
Director::getInstance()->setDisplayStats(false); Director::getInstance()->setDisplayStats(false);
// prepare for camera creation, we need create three custom cameras // prepare for camera creation, we need create three custom cameras
Size visibleSize = Director::getInstance()->getVisibleSize(); _gameCameras.resize(LAYER_COUNT);
_gameCameras.resize(LAYER_DEPTH_COUNT);
// first, create a camera to look the 3d game scene Camera *ca = nullptr;
Camera *ca = Camera::createPerspective(60,visibleSize.width/visibleSize.height,0.1,200); auto visibleSize = Director::getInstance()->getVisibleSize();
ca->setCameraFlag(s_CF[LAYER_DEPTH_GAME]); // first, create a camera to look the skybox
ca->setDepth(LAYER_DEPTH_GAME); ca = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 10, 1000);
_gameCameras[LAYER_DEPTH_GAME] = ca; ca->setPosition3D(Vec3(0.f, 0.f, 50.f));
ca->setCameraFlag(s_CF[LAYER_SKYBOX]);
ca->setDepth(LAYER_SKYBOX);
_gameCameras[LAYER_SKYBOX] = ca;
this->addChild(ca); this->addChild(ca);
// second, use the default camera to look 2d base ui layer // second, create a camera to look the 3D game scene
// it has been create in TerrainWalkThru::init
ca = this->_camera;
ca->setCameraFlag(s_CF[LAYER_GAME]);
ca->setDepth(LAYER_GAME);
_gameCameras[LAYER_GAME] = ca;
// third, use the default camera to look 2D base UI layer
ca =this->getDefaultCamera(); ca =this->getDefaultCamera();
ca->setCameraFlag(s_CF[LAYER_DEPTH_UI]); ca->setCameraFlag(s_CF[LAYER_UI]);
ca->setDepth(LAYER_DEPTH_UI); ca->setDepth(LAYER_UI);
_gameCameras[LAYER_DEPTH_UI] = ca; _gameCameras[LAYER_UI] = ca;
// third, create a camera to look the 3d model in dialogs // forth, create a camera to look the 3D model in dialogs
ca = Camera::create(); ca = Camera::create();
ca->setCameraFlag(s_CF[LAYER_DEPTH_ACTOR]); ca->setCameraFlag(s_CF[LAYER_ACTOR]);
ca->setDepth(LAYER_DEPTH_ACTOR); ca->setDepth(LAYER_ACTOR);
_gameCameras[LAYER_DEPTH_ACTOR] = ca; _gameCameras[LAYER_ACTOR] = ca;
this->addChild(ca); this->addChild(ca);
// forth, create a camera to kook the ui element over on the 3d models // fifth, create a camera to look the UI element over on the 3D models
ca = Camera::create(); ca = Camera::create();
ca->setCameraFlag(s_CF[LAYER_DEPTH_ZOOM]); ca->setCameraFlag(s_CF[LAYER_ZOOM]);
ca->setDepth(LAYER_DEPTH_ZOOM); ca->setDepth(LAYER_ZOOM);
_gameCameras[LAYER_DEPTH_ZOOM] = ca; _gameCameras[LAYER_ZOOM] = ca;
this->addChild(ca);
// sixth, create a camera to look the OSD UI
ca = Camera::create();
ca->setCameraFlag(s_CF[LAYER_OSD]);
ca->setDepth(LAYER_OSD);
_gameCameras[LAYER_OSD] = ca;
this->addChild(ca); this->addChild(ca);
// create all object in game // create all object in game
create3DWorld(); create3DWorld();
createUI(); createUI();
Size step = VisibleRect::getVisibleRect().size; createPlayerDlg();
step.width /= (DLG_COUNT + 1); createDetailDlg();
step.height /= (DLG_COUNT + 1); createDescDlg();
Vec2 pos = VisibleRect::leftBottom();
Node * dlg;
for (int i = 0; i < DLG_COUNT; ++i) {
dlg = createDialog();
dlg->setPosition(pos);
dlg->retain();
_dlgs.push_back(dlg);
pos.x += step.width;
pos.y += step.height;
}
// add touch callback
// auto listener = EventListenerTouchAllAtOnce::create();
// listener->onTouchesBegan = CC_CALLBACK_2(Scene3DTestScene::onTouchesBegan, this);
// listener->onTouchesMoved = CC_CALLBACK_2(Scene3DTestScene::onTouchesMoved, this);
// listener->onTouchesEnded = CC_CALLBACK_2(Scene3DTestScene::onTouchesEnd, this);
// _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
_playerDlg->setVisible(false);
_detailDlg->setVisible(false);
//_terrain->setVisible(false);
ret = true; ret = true;
} while (0); } while (0);
@ -146,137 +157,324 @@ bool Scene3DTestScene::init()
void Scene3DTestScene::create3DWorld() void Scene3DTestScene::create3DWorld()
{ {
// Size visibleSize = Director::getInstance()->getVisibleSize(); // terrain and player has been create in TerrainWalkThru::init
auto camera = _gameCameras[LAYER_DEPTH_GAME]; // we only need override there camera mask
_terrain->setCameraMask(s_CM[LAYER_GAME]);
_player->setCameraMask(s_CM[LAYER_GAME]);
_player->setScale(_player->getScale() * 1.5);
// first, create terrain // add two Sprite3D monster, one is transparent
Terrain::DetailMap r("TerrainTest/dirt.jpg"); auto monster = Sprite3D::create("Sprite3DTest/orc.c3b");
Terrain::DetailMap g("TerrainTest/Grass2.jpg",10); monster->setRotation3D(Vec3(0,180,0));
Terrain::DetailMap b("TerrainTest/road.jpg"); monster->setPosition3D(_player->getPosition3D() + Vec3(50, -10, 0));
Terrain::DetailMap a("TerrainTest/GreenSkin.jpg",20); monster->setCameraMask(s_CM[LAYER_GAME]);
Terrain::TerrainData data("TerrainTest/heightmap16.jpg", monster->setOpacity(128);
"TerrainTest/alphamap.png", this->addChild(monster);
r,g,b,a,Size(32,32),40.0f,2); monster = Sprite3D::create("Sprite3DTest/orc.c3b");
_terrain = Terrain::create(data,Terrain::CrackFixedType::SKIRT); monster->setRotation3D(Vec3(0,180,0));
_terrain->setMaxDetailMapAmount(4); monster->setPosition3D(_player->getPosition3D() + Vec3(-50, -5, 0));
_terrain->setCameraMask(s_CM[LAYER_DEPTH_GAME]); monster->setCameraMask(s_CM[LAYER_GAME]);
_terrain->setDrawWire(false); this->addChild(monster);
_terrain->setSkirtHeightRatio(3);
_terrain->setLODDistance(64,128,192);
this->addChild(_terrain);
// second, create actor // add a particle 3d above player
_player = Player::create("Sprite3DTest/girl.c3b",camera,_terrain); auto rootps = PUParticleSystem3D::create("Particle3D/scripts/blackHole.pu", "Particle3D/materials/pu_mediapack_01.material");
_player->setCameraMask(s_CM[LAYER_DEPTH_GAME]); rootps->setScale(2);
_player->setScale(0.08); rootps->setPosition3D(Vec3(0, 150, 0));
_player->setPositionY(_terrain->getHeight(_player->getPositionX(),_player->getPositionZ())); rootps->setCameraMask(s_CM[LAYER_GAME]);
auto moveby = MoveBy::create(2.0f, Vec2(50.0f, 0.0f));
auto moveby1 = MoveBy::create(2.0f, Vec2(-50.0f, 0.0f));
rootps->runAction(RepeatForever::create(Sequence::create(moveby, moveby1, nullptr)));
rootps->startParticleSystem();
auto animation = Animation3D::create("Sprite3DTest/girl.c3b","Take 001"); _player->addChild(rootps, 0);
if (animation)
// then, create skybox
//create and set our custom shader
auto shader = GLProgram::createWithFilenames("Sprite3DTest/cube_map.vert", "Sprite3DTest/cube_map.frag");
auto state = GLProgramState::create(shader);
// create the second texture for cylinder
_textureCube = TextureCube::create("Sprite3DTest/skybox/left.jpg", "Sprite3DTest/skybox/right.jpg",
"Sprite3DTest/skybox/top.jpg", "Sprite3DTest/skybox/bottom.jpg",
"Sprite3DTest/skybox/front.jpg", "Sprite3DTest/skybox/back.jpg");
_textureCube->retain();
//set texture parameters
Texture2D::TexParams tRepeatParams;
tRepeatParams.magFilter = GL_LINEAR;
tRepeatParams.minFilter = GL_LINEAR;
tRepeatParams.wrapS = GL_MIRRORED_REPEAT;
tRepeatParams.wrapT = GL_MIRRORED_REPEAT;
_textureCube->setTexParameters(tRepeatParams);
// pass the texture sampler to our custom shader
state->setUniformTexture("u_cubeTex", _textureCube);
// add skybox
_skyBox = Skybox::create();
_skyBox->retain();
_skyBox->setCameraMask(s_CM[LAYER_SKYBOX]);
_skyBox->setTexture(_textureCube);
_skyBox->setScale(700.f);
this->addChild(_skyBox);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
[this](EventCustom*)
{ {
auto animate = Animate3D::create(animation); auto state = _skyBox->getGLProgramState();
_player->runAction(RepeatForever::create(animate)); auto glProgram = state->getGLProgram();
} glProgram->reset();
glProgram->initWithFilenames("Sprite3DTest/cube_map.vert", "Sprite3DTest/cube_map.frag");
glProgram->link();
glProgram->updateUniforms();
addChild(_player); _textureCube->reloadTexture();
// third, set camera position Texture2D::TexParams tRepeatParams;
camera->setPosition3D(_player->getPosition3D()+camera_offset); tRepeatParams.magFilter = GL_NEAREST;
camera->setRotation3D(Vec3(-45,0,0)); tRepeatParams.minFilter = GL_NEAREST;
tRepeatParams.wrapS = GL_MIRRORED_REPEAT;
tRepeatParams.wrapT = GL_MIRRORED_REPEAT;
_textureCube->setTexParameters(tRepeatParams);
state->setUniformTexture("u_cubeTex", _textureCube);
_skyBox->reload();
_skyBox->setTexture(_textureCube);
});
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
#endif
} }
void Scene3DTestScene::createUI() void Scene3DTestScene::createUI()
{ {
auto showLeftDlgItem = MenuItemImage::create(s_pathClose, s_pathClose, [this](Ref* sender){ // add player button
this->_dlgs[0]->setVisible(!this->_dlgs[0]->isVisible()); auto showLeftDlgItem = MenuItemImage::create("Images/Pea.png", "Images/Pea.png", [this](Ref* sender){
this->_playerDlg->setVisible(!this->_playerDlg->isVisible());
}); });
showLeftDlgItem->setPosition(VisibleRect::left().x + 30, VisibleRect::top().y - 30); showLeftDlgItem->setPosition(VisibleRect::left().x + 30, VisibleRect::top().y - 30);
auto showMidDlgItem = MenuItemImage::create(s_pathClose, s_pathClose, [this](Ref* sender){ // add discription button
this->_dlgs[1]->setVisible(!this->_dlgs[1]->isVisible()); TTFConfig ttfConfig("fonts/arial.ttf", 20);
auto descItem = MenuItemLabel::create(Label::createWithTTF(ttfConfig, "Description"),
[this](Ref* sender)
{
this->_descDlg->setVisible(!this->_descDlg->isVisible());
}); });
showMidDlgItem->setPosition(VisibleRect::top().x, VisibleRect::top().y - 30); descItem->setPosition(Vec2(VisibleRect::right().x - 50, VisibleRect::top().y - 25));
auto showRightDlgItem = MenuItemImage::create(s_pathClose, s_pathClose, [this](Ref* sender){ // add back button
this->_dlgs[2]->setVisible(!this->_dlgs[2]->isVisible()); auto backItem = MenuItemLabel::create(Label::createWithTTF(ttfConfig, "Back"),
[this](Ref* sender){
this->getTestSuite()->backsUpOneLevel();
}); });
backItem->setPosition(Vec2(VisibleRect::right().x - 50, VisibleRect::bottom().y + 25));
showRightDlgItem->setPosition(VisibleRect::right().x - 30, VisibleRect::top().y - 30); MenuItemImage *moveCameraLeft = nullptr, *moveCameraRight = nullptr;
// auto uiCamera = _gameCameras[LAYER_UI];
auto uiCamera = _gameCameras[LAYER_DEPTH_UI]; // moveCameraLeft = MenuItemImage::create(s_pathClose, s_pathClose, [uiCamera](Ref* sender){
auto moveCameraLeft = MenuItemImage::create(s_pathClose, s_pathClose, [uiCamera](Ref* sender){ // uiCamera->setPositionX(uiCamera->getPositionX() - 10);
uiCamera->setPositionX(uiCamera->getPositionX() - 10); // });
}); // moveCameraLeft->setPosition(VisibleRect::left().x + 30, VisibleRect::left().y);
moveCameraLeft->setPosition(VisibleRect::left().x + 30, VisibleRect::left().y); //
// moveCameraRight = MenuItemImage::create(s_pathClose, s_pathClose, [uiCamera](Ref* sender){
auto moveCameraRight = MenuItemImage::create(s_pathClose, s_pathClose, [uiCamera](Ref* sender){ // uiCamera->setPositionX(uiCamera->getPositionX() + 10);
uiCamera->setPositionX(uiCamera->getPositionX() + 10); // });
}); // moveCameraRight->setPosition(VisibleRect::right().x - 30, VisibleRect::right().y);
moveCameraRight->setPosition(VisibleRect::right().x - 30, VisibleRect::right().y);
auto menu = Menu::create(showLeftDlgItem, auto menu = Menu::create(showLeftDlgItem,
showMidDlgItem, descItem,
showRightDlgItem, backItem,
moveCameraLeft, moveCameraLeft,
moveCameraRight, moveCameraRight,
nullptr); nullptr);
menu->setPosition(Vec2::ZERO); menu->setPosition(Vec2::ZERO);
menu->setCameraMask(s_CM[LAYER_DEPTH_UI], true); menu->setCameraMask(s_CM[LAYER_UI], true);
this->addChild(menu); this->addChild(menu);
} }
Node* Scene3DTestScene::createDialog() void Scene3DTestScene::createPlayerDlg()
{ {
// create dialog layer SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_ui_plist);
int layerW = 240;
int layerH = 160;
int margin = 10;
static char clr = 0x3f;
auto layer = LayerColor::create(Color4B(clr, clr, clr, 255), layerW, layerH);
clr = clr << 1;
layer->setVisible(false);
// add 2d ui element on dialog: Size dlgSize(190, 240);
// actor background Vec2 pos = VisibleRect::center();
auto actorBg = LayerColor::create(Color4B(0, 0, 128, 255), layerW / 2 - 2 * margin, layerH - 2 * margin - 16); float margin = 10;
actorBg->setPosition(margin, margin);
layer->addChild(actorBg); // first, create dialog ui part, include background, title and buttons
_playerDlg = ui::Scale9Sprite::createWithSpriteFrameName("button_actived.png");
_playerDlg->setContentSize(dlgSize);
_playerDlg->setAnchorPoint(Vec2(1, 0.5));
pos.y -= margin;
pos.x -= margin;
_playerDlg->setPosition(pos);
// title // title
auto title = Label::createWithTTF("Actor Dialog","fonts/arial.ttf",16); auto title = Label::createWithTTF("Player Dialog","fonts/arial.ttf",16);
title->setPosition(layerW / 2, layerH - margin); title->setPosition(dlgSize.width / 2, dlgSize.height - margin * 2);
layer->addChild(title); _playerDlg->addChild(title);
// add layer to scene and set camera mask // player background
layer->setCameraMask(s_CM[LAYER_DEPTH_UI]); Size bgSize(110, 180);
this->addChild(layer); Vec2 bgPos(margin, dlgSize.height / 2 - margin);
auto playerBg = ui::Scale9Sprite::createWithSpriteFrameName("item_bg.png");
playerBg->setContentSize(bgSize);
playerBg->setAnchorPoint(Vec2(0, 0.5));
playerBg->setPosition(bgPos);
_playerDlg->addChild(playerBg);
// add actor, which on dialog layer // item background and item
Size itemSize(48, 48);
Vec2 itemAnchor(0, 1);
Vec2 itemPos(bgPos.x + bgSize.width + margin, bgPos.y + bgSize.height / 2);
auto itemBg = ui::Scale9Sprite::createWithSpriteFrameName("item_bg.png");
itemBg->setContentSize(itemSize);
itemBg->setAnchorPoint(itemAnchor);
itemBg->setPosition(itemPos);
_playerDlg->addChild(itemBg);
auto item = ui::Button::create("crystal.png", "", "", ui::TextureResType::PLIST);
item->setScale(1.5);
item->setAnchorPoint(itemAnchor);
item->setPosition(itemPos);
item->addClickEventListener([this](Ref* sender){
this->_detailDlg->setVisible(!this->_detailDlg->isVisible());
});
_playerDlg->addChild(item);
// after add ui element, add player dialog to scene
_playerDlg->setCameraMask(s_CM[LAYER_UI]);
this->addChild(_playerDlg);
// second, add 3d actor, which on dialog layer
std::string filename = "Sprite3DTest/girl.c3b"; std::string filename = "Sprite3DTest/girl.c3b";
auto girl = Sprite3D::create(filename); auto girl = Sprite3D::create(filename);
girl->setScale(0.5); girl->setScale(0.5);
girl->setPosition(layerW / 4, margin * 3); girl->setPosition(bgSize.width / 2, margin * 2);
girl->setCameraMask(s_CM[LAYER_DEPTH_ACTOR]); girl->setCameraMask(s_CM[LAYER_ACTOR]);
layer->addChild(girl); playerBg->addChild(girl);
// add zoom in/out button, which is 2d ui element and over 3d actor // third, add zoom in/out button, which is 2d ui element and over 3d actor
auto zoomIn = MenuItemImage::create(s_pathClose, s_pathClose, [girl](Ref* sender){ ui::Button* zoomIn = ui::Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
zoomIn->setScale(0.5);
zoomIn->setAnchorPoint(Vec2(1, 1));
zoomIn->setPosition(Vec2(bgSize.width / 2 - margin / 2, bgSize.height - margin));
zoomIn->addClickEventListener([girl](Ref* sender){
girl->setScale(girl->getScale() * 2); girl->setScale(girl->getScale() * 2);
}); });
zoomIn->setPosition(margin, layerH - margin); zoomIn->setTitleText("Zoom In");
zoomIn->setCameraMask(s_CM[LAYER_ZOOM]);
playerBg->addChild(zoomIn);
auto zoomOut = MenuItemImage::create(s_pathClose, s_pathClose, [girl](Ref* sender){ ui::Button* zoomOut = ui::Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
zoomOut->setScale(0.5);
zoomOut->setAnchorPoint(Vec2(0, 1));
zoomOut->setPosition(Vec2(bgSize.width / 2 + margin / 2, bgSize.height - margin));
zoomOut->addClickEventListener([girl](Ref* sender){
girl->setScale(girl->getScale() / 2); girl->setScale(girl->getScale() / 2);
}); });
zoomOut->setPosition(layerW / 2 - margin, layerH - margin); zoomOut->setTitleText("Zoom Out");
zoomOut->setCameraMask(s_CM[LAYER_ZOOM]);
playerBg->addChild(zoomOut);
}
auto menu = Menu::create(zoomIn, zoomOut, nullptr); void Scene3DTestScene::createDetailDlg()
menu->setPosition(Vec2::ZERO); {
menu->setCameraMask(s_CM[LAYER_DEPTH_ZOOM]); SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_ui_plist);
layer->addChild(menu);
return layer; Size dlgSize(190, 240);
Vec2 pos = VisibleRect::center();
float margin = 10;
// create dialog
_detailDlg = ui::Scale9Sprite::createWithSpriteFrameName("button_actived.png");
_detailDlg->setContentSize(dlgSize);
_detailDlg->setAnchorPoint(Vec2(0, 0.5));
_detailDlg->setOpacity(224);
pos.y -= margin;
pos.x += margin;
_detailDlg->setPosition(pos);
// title
auto title = Label::createWithTTF("Detail Dialog","fonts/arial.ttf",16);
title->setPosition(dlgSize.width / 2, dlgSize.height - margin * 2);
_detailDlg->addChild(title);
// add a spine ffd animation on it
auto skeletonNode = SkeletonAnimation::createWithFile("spine/goblins-ffd.json", "spine/goblins-ffd.atlas", 1.5f);
skeletonNode->setAnimation(0, "walk", true);
skeletonNode->setSkin("goblin");
skeletonNode->setScale(0.25);
Size windowSize = Director::getInstance()->getWinSize();
skeletonNode->setPosition(Vec2(dlgSize.width / 2, 20));
_detailDlg->addChild(skeletonNode);
scheduleUpdate();
EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [skeletonNode] (Touch* touch, Event* event) -> bool {
if (!skeletonNode->getDebugBonesEnabled())
skeletonNode->setDebugBonesEnabled(true);
else if (skeletonNode->getTimeScale() == 1)
skeletonNode->setTimeScale(0.3f);
else
{
skeletonNode->setTimeScale(1);
skeletonNode->setDebugBonesEnabled(false);
}
return true;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
// after add ui element, add dialog to scene
_detailDlg->setCameraMask(s_CM[LAYER_UI]);
this->addChild(_detailDlg);
}
void Scene3DTestScene::createDescDlg()
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_ui_plist);
Size dlgSize(440, 240);
Vec2 pos = VisibleRect::center();
float margin = 10;
// create dialog
_descDlg = ui::Scale9Sprite::createWithSpriteFrameName("button_actived.png");
_descDlg->setContentSize(dlgSize);
_descDlg->setOpacity(224);
_descDlg->setPosition(pos);
// title
auto title = Label::createWithTTF("Description Dialog","fonts/arial.ttf",16);
title->setPosition(dlgSize.width / 2, dlgSize.height - margin * 2);
_descDlg->addChild(title);
// add a label to retain description text
Size textSize(400, 220);
auto desc =
"Scene 3D test for 2D and 3D mix rendering.\n"
"\n"
"- Game world composite with terrain, skybox and 3D objects.\n"
"- UI composite with 2D nodes.\n"
"- Click the icon at the topleft conner, will show a player dialog which "
"there is a 3D sprite on it.\n"
"- There are two button to zoom the player model, which should keep above "
"on 3D model.\n"
" - This description dialog should above all other elements.\n"
"\n"
"Click \"Description\" button to hide this dialog.\n";
auto text = Label::createWithSystemFont(desc, "Helvetica", 12, textSize);
text->setAnchorPoint(Vec2(0, 1));
text->setPosition(margin, dlgSize.height - (20 + margin));
_descDlg->addChild(text);
// after add ui element, add dialog to scene
_descDlg->setCameraMask(s_CM[LAYER_OSD]);
this->addChild(_descDlg);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////