Esempio n. 1
0
	static LWFTextTTF *create(bool useTTF, const char *string,
		const char *fontName, float fontSize,
		const cocos2d::Size& dimensions, cocos2d::TextHAlignment hAlignment, 
		cocos2d::TextVAlignment vAlignment, float red, float green, float blue)
	{
		LWFTextTTF *ret = new LWFTextTTF(nullptr, hAlignment, vAlignment);
		if (!ret)
			return nullptr;

		if (useTTF) {
			cocos2d::TTFConfig ttfConfig(
				fontName, fontSize, cocos2d::GlyphCollection::DYNAMIC);
			if (!ret->setTTFConfig(ttfConfig)) {
				delete ret;
				return nullptr;
			}
		} else {
			ret->setSystemFontName(fontName);
			ret->setSystemFontSize(fontSize);
			ret->setBlendFunc(cocos2d::BlendFunc::ALPHA_NON_PREMULTIPLIED);
		}

		ret->setDimensions(dimensions.width, dimensions.height);
		ret->setParameter(useTTF, fontSize, red, green, blue);
		ret->setString(string);
		ret->autorelease();

		return ret;
	}
bool TextFieldTTF::initWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize)
{
    _placeHolder = placeholder;

    do
    {
        // If fontName is ttf file and it corrected, use TTFConfig
        if (FileUtils::getInstance()->isFileExist(fontName))
        {
            TTFConfig ttfConfig(fontName, fontSize, GlyphCollection::DYNAMIC);
            if (setTTFConfig(ttfConfig))
            {
                break;
            }
        }

        setSystemFontName(fontName);
        setSystemFontSize(fontSize);

    } while (false);


    Label::setTextColor(_colorSpaceHolder);
    Label::setString(_placeHolder);

#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
    // On desktop default enable cursor
    if (_currentLabelType == LabelType::TTF)
    {
        setCursorEnabled(true);
    }
#endif

    return true;
}
Esempio n. 3
0
LabelTTFColor::LabelTTFColor()
{
    auto size = Director::getInstance()->getWinSize();

    TTFConfig ttfConfig("fonts/arial.ttf", 35);
    // Green
    auto label1 = Label::createWithTTF(ttfConfig,"Green", TextHAlignment::CENTER, size.width);
    label1->setPosition( Point(size.width/2, size.height/5 * 1.5) );
    label1->setColor( Color3B::GREEN );
    label1->setAnchorPoint(Point::ANCHOR_MIDDLE);
    addChild(label1);

    // Red
    auto label2 = Label::createWithTTF(ttfConfig,"Red", TextHAlignment::CENTER, size.width);
    label2->setPosition( Point(size.width/2, size.height/5 * 2.0) );
    label2->setColor( Color3B::RED );
    label2->setAnchorPoint(Point::ANCHOR_MIDDLE);
    addChild(label2);

    // Blue
    auto label3 = Label::createWithTTF(ttfConfig,"Blue", TextHAlignment::CENTER, size.width);
    label3->setPosition( Point(size.width/2, size.height/5 * 2.5) );
    label3->setColor( Color3B::BLUE );
    label3->setAnchorPoint(Point::ANCHOR_MIDDLE);
    addChild(label3);
}
Esempio n. 4
0
LabelTTFFontsTestNew::LabelTTFFontsTestNew()
{
    const char *ttfpaths[] = {
        "fonts/A Damn Mess.ttf",
        "fonts/Abberancy.ttf",
        "fonts/Abduction.ttf",
        "fonts/American Typewriter.ttf",
        "fonts/Paint Boy.ttf",
        "fonts/Schwarzwald Regular.ttf",
        "fonts/Scissor Cuts.ttf",
    };
#define arraysize(ar)  (sizeof(ar) / sizeof(ar[0]))

    auto size = Director::getInstance()->getWinSize();
    TTFConfig ttfConfig(ttfpaths[0],40, GlyphCollection::NEHE);
    for(size_t i=0;i < arraysize(ttfpaths); ++i) {
        ttfConfig.fontFilePath = ttfpaths[i];
        auto label = Label::createWithTTF(ttfConfig, ttfpaths[i], TextHAlignment::CENTER,0);
        if( label ) {
            
            label->setPosition( Point(size.width/2, ((size.height * 0.6)/arraysize(ttfpaths) * i) + (size.height/5)));
            addChild(label);
            
            label->setAnchorPoint(Point::ANCHOR_MIDDLE);
        } else {
            log("ERROR: Cannot load: %s", ttfpaths[i]);
        }
    }
}
Esempio n. 5
0
Sprite3DReskinTest::Sprite3DReskinTest()
: _sprite(nullptr)
{
    auto s = Director::getInstance()->getWinSize();
    addNewSpriteWithCoords( Vec2(s.width/2, s.height/2) );
    
    auto listener = EventListenerTouchAllAtOnce::create();
    listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DReskinTest::onTouchesEnded, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
    TTFConfig ttfConfig("fonts/arial.ttf", 20);
    auto label1 = Label::createWithTTF(ttfConfig,"Hair");
    auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchHair,this) );
    auto label2 = Label::createWithTTF(ttfConfig,"Glasses");
    auto item2 = MenuItemLabel::create(label2,	CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchGlasses,this) );
    auto label3 = Label::createWithTTF(ttfConfig,"Coat");
    auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchCoat,this) );
    auto label4 = Label::createWithTTF(ttfConfig,"Pants");
    auto item4 = MenuItemLabel::create(label4,	CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchPants,this) );
    auto label5 = Label::createWithTTF(ttfConfig,"Shoes");
    auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchShoes,this) );
    item1->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*4 ) );
    item2->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *5 ) );
    item3->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*6 ) );
    item4->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *7 ) );
    item5->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *8 ) );
    auto pMenu1 = CCMenu::create(item1, item2, item3, item4, item5, nullptr);
    pMenu1->setPosition(Vec2(0,0));
    this->addChild(pMenu1, 10);
    
}
Esempio n. 6
0
LabelTTFDistanceField::LabelTTFDistanceField()
{
    auto size = Director::getInstance()->getWinSize();
    TTFConfig ttfConfig("fonts/arial.ttf", 80, GlyphCollection::DYNAMIC,nullptr,true);

    auto label1 = Label::createWithTTF(ttfConfig,"Distance Field",TextHAlignment::CENTER,size.width);
    label1->setPosition( Point(size.width/2, size.height * 0.6f) );
    label1->setColor( Color3B::GREEN );
    label1->setAnchorPoint(Point::ANCHOR_MIDDLE);
    addChild(label1);

    auto action = Sequence::create(
        DelayTime::create(1.0f),
        ScaleTo::create(6.0f,5.0f,5.0f),
        ScaleTo::create(6.0f,1.0f,1.0f),
        nullptr);
    label1->runAction(RepeatForever::create(action));

    auto label2 = Label::createWithTTF(ttfConfig,"Distance Field",TextHAlignment::CENTER,size.width);
    label2->setPosition( Point(size.width/2, size.height * 0.3f) );
    label2->setColor( Color3B::RED );
    label2->setAnchorPoint(Point::ANCHOR_MIDDLE);
    addChild(label2);

}
Esempio n. 7
0
LabelFNTandTTFEmpty::LabelFNTandTTFEmpty()
{
    auto s = Director::getInstance()->getWinSize();

    // LabelBMFont
    auto label1 = Label::createWithBMFont("fonts/bitmapFontTest3.fnt", "", TextHAlignment::CENTER, s.width);
    addChild(label1, 0, kTagBitmapAtlas1);
    label1->setAnchorPoint(Point::ANCHOR_MIDDLE);
    label1->setPosition(Point(s.width/2, s.height - 100));

    // LabelTTF
    TTFConfig ttfConfig("fonts/arial.ttf",48);
    auto label2 = Label::createWithTTF(ttfConfig,"", TextHAlignment::CENTER,s.width);
    addChild(label2, 0, kTagBitmapAtlas2);
    label2->setAnchorPoint(Point::ANCHOR_MIDDLE);
    label2->setPosition(Point(s.width/2, s.height / 2));

    auto label3 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.png", 48, 64, ' ');
    label3->setAnchorPoint(Point::ANCHOR_MIDDLE);
    addChild(label3, 0, kTagBitmapAtlas3);
    label3->setPosition(Point(s.width/2, 100));

    schedule(schedule_selector(LabelFNTandTTFEmpty::updateStrings), 1.0f);

    setEmpty = false;
}
Esempio n. 8
0
Label* Label::create(const std::string& text, const std::string& fontName, float fontSize, const Size& dimensions /* = Size::ZERO */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */, TextVAlignment vAlignment /* = TextVAlignment::TOP */)
{
    auto ret = new Label(nullptr,hAlignment,vAlignment);

    if (ret)
    {
        do 
        {
            if (FileUtils::getInstance()->isFileExist(fontName))
            {
                TTFConfig ttfConfig(fontName.c_str(),fontSize,GlyphCollection::DYNAMIC);
                if (ret->setTTFConfig(ttfConfig))
                {
                    break;
                }
            }

            FontDefinition fontDef;
            fontDef._fontName = fontName;
            fontDef._fontSize = fontSize;
            fontDef._dimensions = dimensions;
            fontDef._alignment = hAlignment;
            fontDef._vertAlignment = vAlignment;
            ret->setFontDefinition(fontDef);
        } while (0);

        ret->setDimensions(dimensions.width,dimensions.height);
        ret->setString(text);
        ret->autorelease();
    }   

    return ret;
}
Esempio n. 9
0
LabelTTFDistanceFieldEffect::LabelTTFDistanceFieldEffect()
{
    auto size = Director::getInstance()->getWinSize();

    auto bg = LayerColor::create(Color4B(200,191,231,255));
    this->addChild(bg);

    TTFConfig ttfConfig("fonts/arial.ttf", 80, GlyphCollection::DYNAMIC,nullptr,true);

    auto label1 = Label::createWithTTF(ttfConfig,"Glow", TextHAlignment::CENTER, size.width);
    label1->setPosition( Point(size.width/2, size.height*0.65) );
    label1->setColor( Color3B::GREEN );
    label1->setAnchorPoint(Point::ANCHOR_MIDDLE);
    label1->setLabelEffect(LabelEffect::GLOW,Color3B::YELLOW);
    addChild(label1);

    auto label2 = Label::createWithTTF(ttfConfig,"Outline", TextHAlignment::CENTER, size.width);
    label2->setPosition( Point(size.width/2, size.height*0.5) );
    label2->setColor( Color3B::RED );
    label2->setAnchorPoint(Point::ANCHOR_MIDDLE);
    label2->setLabelEffect(LabelEffect::OUTLINE,Color3B::BLUE);
    addChild(label2);

    auto label3 = Label::createWithTTF(ttfConfig,"Shadow", TextHAlignment::CENTER, size.width);
    label3->setPosition( Point(size.width/2, size.height*0.35f) );
    label3->setColor( Color3B::RED );
    label3->setAnchorPoint(Point::ANCHOR_MIDDLE);
    label3->setLabelEffect(LabelEffect::SHADOW,Color3B::BLACK);
    addChild(label3);

}
Esempio n. 10
0
void CProgressBar::initText( const std::string& text, const std::string& fontFile, float fontSize, const Size& dimensions,const Color3B &color )
{
	TTFConfig ttfConfig(fontFile.c_str(),fontSize,GlyphCollection::DYNAMIC);
	getLabel()->setTTFConfig(ttfConfig);
	getLabel()->setString(text);
	getLabel()->setColor(color);
	getLabel()->setDimensions(dimensions.width,dimensions.height);
}
cocos2d::Label* SceneBase::addLabel(const std::string text, const float posx, const float posy, const int size,  SceneBase* pScence,
    const cocos2d::Vec2 point)
{
    cocos2d::TTFConfig ttfConfig("fonts/arial.ttf", size);
    auto playLabel = cocos2d::Label::createWithTTF(ttfConfig, text);
    playLabel->setPosition(cocos2d::Point(posx, posy));
    playLabel->setAnchorPoint(point/*Vec2::ANCHOR_BOTTOM_LEFT*/);
    pScence->addChild(playLabel, 1);
    return playLabel;
};
Esempio n. 12
0
// on "init" you need to initialize your instance
bool MainMenuScene::init()
{
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();


    TTFConfig ttfConfig("fonts/PacFont.ttf", 72);
    auto headerLabel1 = Label::createWithTTF(ttfConfig, "pa", TextHAlignment::CENTER,100);
    auto headerLabel2 = Label::createWithTTF(ttfConfig, "c", TextHAlignment::CENTER,100);
    auto headerLabel3 = Label::createWithTTF(ttfConfig, "man", TextHAlignment::CENTER,100);

    headerLabel2->setColor(Color3B(248, 248, 50));

    headerLabel1->setPosition(visibleSize.width / 2 + origin.x - 137,
                             3*visibleSize.height / 4 + origin.y);
    headerLabel2->setPosition(visibleSize.width / 2 + origin.x - 51,
                             3*visibleSize.height / 4 + origin.y);
    headerLabel3->setPosition(visibleSize.width / 2 + origin.x + 90,
                             3*visibleSize.height / 4 + origin.y);

    this->addChild(headerLabel1);
    this->addChild(headerLabel2);
    this->addChild(headerLabel3);

    TTFConfig ttfConfig1("fonts/emulogic.ttf", 28);
    auto label1 = Label::createWithTTF(ttfConfig1, "PLAY", TextHAlignment::CENTER,100);
    auto label2 = Label::createWithTTF(ttfConfig1, "ABOUT", TextHAlignment::CENTER,100);
    auto label3 = Label::createWithTTF(ttfConfig1, "EXIT", TextHAlignment::CENTER,100);
    auto *menuItem1 = MenuItemLabel::create( label1,
                                           CC_CALLBACK_1( MainMenuScene::GoToGameScene, this ));
    auto *menuItem2 = MenuItemLabel::create( label2,
                                           CC_CALLBACK_1( MainMenuScene::GoToAboutScene, this ));
    auto *menuItem3 = MenuItemLabel::create( label3,
                                           CC_CALLBACK_1( MainMenuScene::menuCloseCallback, this ));

    menuItem3->setPosition(Point(visibleSize.width / 2 + origin.x,
                                 3*visibleSize.height / 8 + origin.y));
    menuItem2->setPosition(Point(visibleSize.width/ 2 + origin.x,
                                 3*visibleSize.height / 8 + origin.y + 60));
    menuItem1->setPosition(Point(visibleSize.width/ 2 + origin.x,
                                 3*visibleSize.height / 8 + origin.y + 120));

    auto menu = Menu::create(menuItem1, menuItem2, menuItem3, NULL);
    menu->setPosition(Point::ZERO);

    this->addChild(menu);

    return true;
}
Esempio n. 13
0
LabelTTFLongLineWrapping::LabelTTFLongLineWrapping()
{
    auto size = Director::getInstance()->getWinSize();

    // Long sentence
    TTFConfig ttfConfig("fonts/arial.ttf", 28);
    auto label1 = Label::createWithTTF(ttfConfig, LongSentencesExample, TextHAlignment::CENTER,size.width);
    label1->setPosition( Point(size.width/2, size.height/2) );
    label1->setAnchorPoint(Point(0.5, 1.0));
    addChild(label1);
}
Esempio n. 14
0
LabelCrashTest::LabelCrashTest()
{
    auto size = Director::getInstance()->getWinSize();

    TTFConfig ttfConfig("fonts/arial.ttf", 80, GlyphCollection::DYNAMIC,nullptr,true);

    auto label1 = Label::createWithTTF(ttfConfig,"Test崩溃123", TextHAlignment::CENTER, size.width);
    label1->setPosition( Point(size.width/2, size.height/2) );
    label1->setAnchorPoint(Point::ANCHOR_MIDDLE);
    addChild(label1);
}
Esempio n. 15
0
LabelTTFUnicodeChinese::LabelTTFUnicodeChinese()
{
    auto size = Director::getInstance()->getWinSize();
    // Adding "啊" letter at the end of string to make VS2012 happy, otherwise VS will generate errors  
    // like "Error 3 error C2146: syntax error : missing ')' before identifier 'label'"; 
    TTFConfig ttfConfig("fonts/wt021.ttf",55,GlyphCollection::CUSTOM, "美好的一天啊");
    auto label = Label::createWithTTF(ttfConfig,"美好的一天啊", TextHAlignment::CENTER, size.width);
    label->setAnchorPoint(Point::ANCHOR_MIDDLE);
    label->setPosition(Point(size.width / 2, size.height /2));
    this->addChild(label);
}
Esempio n. 16
0
void Physics3DDemoDisabled::onEnter()
{
	TTFConfig ttfConfig("fonts/arial.ttf", 16);
	auto label = Label::createWithTTF(ttfConfig, "Should define CC_USE_3D_PHYSICS\n to run this test case");
	
	auto size = Director::getInstance()->getWinSize();
	label->setPosition(Vec2(size.width / 2, size.height / 2));

	addChild(label);

	TestCase::onEnter();
}
cocos2d::MenuItemLabel* SceneBase::addLabelEx(const std::string text, const float posx, const float posy, const int size,  SceneBase* pScence, const cocos2d::ccMenuCallback& callback,
    const cocos2d::TextHAlignment hAlignment /* = TextHAlignment::CENTER */, const cocos2d::Vec2 point/* = cocos2d::Vec2::ANCHOR_BOTTOM_LEFT*/)
{
    cocos2d::TTFConfig ttfConfig("fonts/arial.ttf", size);
    auto playLabel = cocos2d::Label::createWithTTF(ttfConfig, text, hAlignment);
    auto playItem  = cocos2d::MenuItemLabel::create(playLabel, callback);
    playItem->setPosition(cocos2d::Point(posx, posy));
    auto pmenu = cocos2d::Menu::create(playItem, NULL);
    pmenu->setPosition(cocos2d::Point::ZERO);
    pScence->addChild(pmenu, 1);
    playItem->setAnchorPoint(point);
    pScence->m_menu.push_back(playItem);
    return playItem;
}
Esempio n. 18
0
void TestScene::onEnter()
{
    Scene::onEnter();

    //add the menu item for back to main menu
    TTFConfig ttfConfig("fonts/arial.ttf", 20);
    auto label = Label::createWithTTF(ttfConfig,"MainMenu");

    auto menuItem = MenuItemLabel::create(label, testScene_callback );
    auto menu = Menu::create(menuItem, NULL);

    menu->setPosition( Vec2::ZERO );
    menuItem->setPosition( Vec2( VisibleRect::right().x - 50, VisibleRect::bottom().y + 25) );

    addChild(menu, 1);
}
Esempio n. 19
0
LabelTTFOldNew::LabelTTFOldNew()
{
    auto s = Director::getInstance()->getWinSize();
    float delta = s.height/4;

    auto label1 = LabelTTF::create("Cocos2d-x Label Test", "arial", 24);
    addChild(label1, 0, kTagBitmapAtlas1);
    label1->setAnchorPoint(Point::ANCHOR_MIDDLE);
    label1->setPosition(Point(s.width/2, delta * 2));
    label1->setColor(Color3B::RED);

    TTFConfig ttfConfig("fonts/arial.ttf", 48);
    auto label2 = Label::createWithTTF(ttfConfig, "Cocos2d-x Label Test");
    addChild(label2, 0, kTagBitmapAtlas2);
    label2->setAnchorPoint(Point::ANCHOR_MIDDLE);
    label2->setPosition(Point(s.width/2, delta * 2));
}
Esempio n. 20
0
bool Physics3DTestDemo::init()
{
    if (!TestCase::init()) return false;
    
    if (initWithPhysics())
    {
        getPhysics3DWorld()->setDebugDrawEnable(false);
        
        physicsScene = this;
        Size size = Director::getInstance()->getWinSize();
        _camera = Camera::createPerspective(30.0f, size.width / size.height, 1.0f, 1000.0f);
        _camera->setPosition3D(Vec3(0.0f, 50.0f, 100.0f));
        _camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
        _camera->setCameraFlag(CameraFlag::USER1);
        this->addChild(_camera);
        
        auto listener = EventListenerTouchAllAtOnce::create();
        listener->onTouchesBegan = CC_CALLBACK_2(Physics3DTestDemo::onTouchesBegan, this);
        listener->onTouchesMoved = CC_CALLBACK_2(Physics3DTestDemo::onTouchesMoved, this);
        listener->onTouchesEnded = CC_CALLBACK_2(Physics3DTestDemo::onTouchesEnded, this);
        _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
        
        TTFConfig ttfConfig("fonts/arial.ttf", 10);
        auto label = Label::createWithTTF(ttfConfig,"DebugDraw OFF");
        auto menuItem = MenuItemLabel::create(label, [=](Ref *ref){
            if (getPhysics3DWorld()->isDebugDrawEnabled()){
                getPhysics3DWorld()->setDebugDrawEnable(false);
                label->setString("DebugDraw OFF");
            }else{
                getPhysics3DWorld()->setDebugDrawEnable(true);
                label->setString("DebugDraw ON");
            }
        });

        auto menu = Menu::create(menuItem, nullptr);
        menu->setPosition(Vec2::ZERO);
        menuItem->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
        menuItem->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y-50) );
        this->addChild(menu);

        _angle = 0.0f;
        return true;
    }
    physicsScene = nullptr;
    return false;
}
Esempio n. 21
0
bool LabelTTF::initWithString(const std::string& string, const std::string& fontFile, float fontSize,
                                const cocos2d::Size &dimensions, TextHAlignment hAlignment,
                                TextVAlignment vAlignment)
{
	if ( FileUtils::getInstance()->isFileExist(fontFile) )
	{
		TTFConfig ttfConfig(fontFile.c_str(),fontSize,GlyphCollection::DYNAMIC);
		if (setTTFConfig(ttfConfig))
		{
			setDimensions(dimensions.width,dimensions.height);
			setString(string);
			setAlignment(hAlignment,vAlignment);
			enableOutline(Color4B::BLACK,2);
			return true;
		}
	}

	return false;
}
Esempio n. 22
0
TestController::TestController()
: _beginPos(Vec2::ZERO)
,_exitThread(false)
{
    // add close menu
    auto closeItem = MenuItemImage::create(s_pathClose, s_pathClose, CC_CALLBACK_1(TestController::closeCallback, this) );
    auto menu =Menu::create(closeItem, nullptr);

    menu->setPosition( Vec2::ZERO );
    closeItem->setPosition(Vec2( VisibleRect::right().x - 30, VisibleRect::top().y - 30));

    // add menu items for tests
    TTFConfig ttfConfig("fonts/arial.ttf", 24);
    _itemMenu = Menu::create();
    for (int i = 0; i < g_testCount; ++i)
    {
        auto label = Label::createWithTTF(ttfConfig, g_aTestNames[i].test_name);       
        auto menuItem = MenuItemLabel::create(label, CC_CALLBACK_1(TestController::menuCallback, this));

        _itemMenu->addChild(menuItem, i + 10000);
        menuItem->setPosition( Vec2( VisibleRect::center().x, (VisibleRect::top().y - (i + 1) * LINE_SPACE) ));
    }

    _itemMenu->setContentSize(Size(VisibleRect::getVisibleRect().size.width, (g_testCount + 1) * (LINE_SPACE)));
    _itemMenu->setPosition(s_tCurPos);
    addChild(_itemMenu);

    addChild(menu, 1);

    // Register Touch Event
    auto listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
    
    listener->onTouchBegan = CC_CALLBACK_2(TestController::onTouchBegan, this);
    listener->onTouchMoved = CC_CALLBACK_2(TestController::onTouchMoved, this);
    
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

    auto mouseListener = EventListenerMouse::create();
    mouseListener->onMouseScroll = CC_CALLBACK_1(TestController::onMouseScroll, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this);
}
Esempio n. 23
0
LabelTTFDynamicAlignment::LabelTTFDynamicAlignment()
{
    auto size = Director::getInstance()->getWinSize();
    TTFConfig ttfConfig("fonts/arial.ttf", 45);
    _label = Label::createWithTTF(ttfConfig,LongSentencesExample, TextHAlignment::CENTER, size.width);
    _label->setPosition( Point(size.width/2, size.height/2) );
    _label->setAnchorPoint(Point::ANCHOR_MIDDLE);  
    
    auto menu = Menu::create(
                              MenuItemFont::create("Left", CC_CALLBACK_1(LabelTTFDynamicAlignment::setAlignmentLeft, this)),
                              MenuItemFont::create("Center", CC_CALLBACK_1(LabelTTFDynamicAlignment::setAlignmentCenter, this)),
                              MenuItemFont::create("Right", CC_CALLBACK_1(LabelTTFDynamicAlignment::setAlignmentRight, this)),
                              NULL);
    
    menu->alignItemsVerticallyWithPadding(4);
    menu->setPosition(Point(50, size.height / 4 ));
    
    addChild(_label);
    this->addChild(menu);
}
Esempio n. 24
0
LightTest::LightTest()
    : _directionalLight(nullptr)
    , _pointLight(nullptr)
    , _spotLight(nullptr)
{
    addSprite();
    addLights();
    scheduleUpdate();

    auto s = Director::getInstance()->getWinSize();
    auto camera = Camera::createPerspective(60, (GLfloat)s.width/s.height, 1.0f, 1000.0f);
    camera->setCameraFlag(CameraFlag::USER1);
    camera->setPosition3D(Vec3(0.0, 100, 100));
    camera->lookAt(Vec3(0.0, 0.0, 0.0), Vec3(0.0, 1.0, 0.0));
    addChild(camera);

    TTFConfig ttfConfig("fonts/arial.ttf", 15);
    _ambientLightLabel = Label::createWithTTF(ttfConfig,"Ambient Light ON");
    _ambientLightLabel->retain();
    auto menuItem0 = MenuItemLabel::create(_ambientLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::AMBIENT));
    _directionalLightLabel = Label::createWithTTF(ttfConfig,"Directional Light OFF");
    _directionalLightLabel->retain();
    auto menuItem1 = MenuItemLabel::create(_directionalLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::DIRECTIONAL));
    _pointLightLabel = Label::createWithTTF(ttfConfig,"Point Light OFF");
    _pointLightLabel->retain();
    auto menuItem2 = MenuItemLabel::create(_pointLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::POINT));
    _spotLightLabel = Label::createWithTTF(ttfConfig,"Spot Light OFF");
    _spotLightLabel->retain();
    auto menuItem3 = MenuItemLabel::create(_spotLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::SPOT));
    auto menu = Menu::create(menuItem0, menuItem1, menuItem2, menuItem3, nullptr);
    menu->setPosition(Vec2::ZERO);
    menuItem0->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
    menuItem0->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y-50) );
    menuItem1->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
    menuItem1->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y-100) );
    menuItem2->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
    menuItem2->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y -150));
    menuItem3->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
    menuItem3->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y -200));
    addChild(menu);
}
Esempio n. 25
0
Label* Label::createWithTTF(const std::string& text, const std::string& fontFile, float fontSize, const Size& dimensions /* = Size::ZERO */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */, TextVAlignment vAlignment /* = TextVAlignment::TOP */)
{
    auto ret = new (std::nothrow) Label(nullptr,hAlignment,vAlignment);

    if (ret && FileUtils::getInstance()->isFileExist(fontFile))
    {
        TTFConfig ttfConfig(fontFile.c_str(),fontSize,GlyphCollection::DYNAMIC);
        if (ret->setTTFConfig(ttfConfig))
        {
            ret->setDimensions(dimensions.width,dimensions.height);
            ret->setString(text);

            ret->autorelease();

            return ret;
        }
    }

    delete ret;
    return nullptr;
}
Esempio n. 26
0
SpritePolygonPerformance::SpritePolygonPerformance()
{
    SpritePolygonCache::getInstance()->removeAllSpritePolygonCache();
    Director::getInstance()->setClearColor(Color4F(102.f/255, 184.f/255, 204.f/255, 255.f));
    TTFConfig ttfConfig("fonts/arial.ttf", 10);
    perfLabel = Label::createWithTTF(ttfConfig, "performance test");
    addChild(perfLabel);
    perfLabel->setPosition(Director::getInstance()->getVisibleSize().width/2, 80);
    
    spriteCount = vertCount = triCount = pixelCount = continuousLowDt =0;
    auto size = Director::getInstance()->getVisibleSize();
    elapsedTime = 0;
    _posX = _leftX = size.width*0.15;
    _rightX = size.width*0.85;
    _posY = size.height/2;
    prevDt = 0.016f;
    goRight = true;
    ended = false;
    scheduleUpdate();
    continuousHighDtTime = 0.0;
    waitingTime = 0.0;
}
Esempio n. 27
0
bool TestCase::init()
{
    if (Scene::init())
    {
        // add title and subtitle
        TTFConfig ttfConfig("fonts/arial.ttf", 26);
        _titleLabel = Label::createWithTTF(ttfConfig, title());
        addChild(_titleLabel, 9999);
        _titleLabel->setPosition(VisibleRect::center().x, VisibleRect::top().y - 30);
        
        ttfConfig.fontSize = 16;
        _subtitleLabel = Label::createWithTTF(ttfConfig, subtitle());
        _subtitleLabel->setMaxLineWidth(VisibleRect::getVisibleRect().size.width);
        addChild(_subtitleLabel, 9999);
        _subtitleLabel->setPosition(VisibleRect::center().x, VisibleRect::top().y - 60);
        
        _priorTestItem = MenuItemImage::create(s_pathB1, s_pathB2, CC_CALLBACK_1(TestCase::priorTestCallback, this));
        _restartTestItem = MenuItemImage::create(s_pathR1, s_pathR2, CC_CALLBACK_1(TestCase::restartTestCallback, this));
        _nextTestItem = MenuItemImage::create(s_pathF1, s_pathF2, CC_CALLBACK_1(TestCase::nextTestCallback, this));
        
        ttfConfig.fontSize = 20;
        auto backLabel = Label::createWithTTF(ttfConfig, "Back");
        auto backItem = MenuItemLabel::create(backLabel, CC_CALLBACK_1(TestCase::onBackCallback, this));

        auto menu = Menu::create(_priorTestItem, _restartTestItem, _nextTestItem, backItem, nullptr);

        menu->setPosition(Vec2::ZERO);
        _priorTestItem->setPosition(VisibleRect::center().x - _restartTestItem->getContentSize().width * 2, VisibleRect::bottom().y + _restartTestItem->getContentSize().height / 2);
        _restartTestItem->setPosition(VisibleRect::center().x, VisibleRect::bottom().y + _restartTestItem->getContentSize().height / 2);
        _nextTestItem->setPosition(VisibleRect::center().x + _restartTestItem->getContentSize().width * 2, VisibleRect::bottom().y + _restartTestItem->getContentSize().height / 2);
        backItem->setPosition(Vec2(VisibleRect::right().x - 50, VisibleRect::bottom().y + 25));

        addChild(menu, 9999);

        return true;
    }

    return false;
}
Esempio n. 28
0
//传入一个CCrenderTexture 
//相当于一个正在运行的游戏的截图作为这个暂停对话框的背景 
//这样就看起来像是对话框在游戏界面之上,一般游戏当中都是这样子写的。
Scene* SceneOver::createScene(RenderTexture* sqr, int killNum)
{

    Scene *scene = Scene::create();
	SceneOver *layer = SceneOver::create();
		scene->addChild(layer,1);//把游戏层放上面,我们还要在这上面放按钮


	//增加部分:使用Game界面中截图的sqr纹理图片创建Sprite
	//并将Sprite添加到GamePause场景层中
	//得到窗口的大小
	
	Sprite *back_spr = Sprite::createWithTexture(sqr->getSprite()->getTexture());  
	back_spr->setPosition(Vec2(winSize.width / 2, winSize.height / 2)); //放置位置,这个相对于中心位置。
	back_spr->setFlippedY(true);            //翻转,因为UI坐标和OpenGL坐标不同
	back_spr->setColor(Color3B::GRAY); //图片颜色变灰色
	scene->addChild(back_spr);


	//添加游戏暂停背景小图,用来放按钮
	Sprite *back_small_spr = Sprite::create("back_over.png");
	back_small_spr->setPosition(Vec2(winSize.width / 2, winSize.height / 2)); //放置位置,这个相对于中心位置。
	scene->addChild(back_small_spr);

	char strBuffer[20] = { 0 };
	sprintf(strBuffer, "%d", killNum);
	TTFConfig ttfConfig("font/HKYuanMini.ttf", 35, GlyphCollection::DYNAMIC);
	auto labelKillNum = Label::createWithTTF(ttfConfig,
		strBuffer, TextHAlignment::RIGHT, winSize.width * 0.75);
	if (labelKillNum) {
		labelKillNum->setTextColor(Color4B(255, 128, 255, 255));
		labelKillNum->setPosition(Vec2(winSize.width / 2 + 15, winSize.height / 2 + 25));
		labelKillNum->setAnchorPoint(Vec2(0, 0.5));
		scene->addChild(labelKillNum);
	}


    return scene;
}
Esempio n. 29
0
void BaseTest::onEnter()
{
	Layer::onEnter();
    AppDelegate* app = (AppDelegate *)Application::getInstance();
    app->setCurrentTest(this);
	// add title and subtitle
    std::string str = title();
    const char * pTitle = str.c_str();
    TTFConfig ttfConfig("fonts/arial.ttf", 32);
    auto label = Label::createWithTTF(ttfConfig,pTitle);
    addChild(label, 9999);
    label->setPosition(VisibleRect::center().x, VisibleRect::top().y - 30);

    std::string strSubtitle = subtitle();
    if( ! strSubtitle.empty() )
    {
        ttfConfig.fontFilePath = "fonts/Thonburi.ttf";
        ttfConfig.fontSize = 16;
        auto l = Label::createWithTTF(ttfConfig,strSubtitle.c_str());
        addChild(l, 9999);
        l->setPosition(VisibleRect::center().x, VisibleRect::top().y - 60);
    }

    // add menu
	// CC_CALLBACK_1 == std::bind( function_ptr, instance, std::placeholders::_1, ...)
    auto item1 = MenuItemImage::create(s_pathB1, s_pathB2, CC_CALLBACK_1(BaseTest::backCallback, this) );
    auto item2 = MenuItemImage::create(s_pathR1, s_pathR2, CC_CALLBACK_1(BaseTest::restartCallback, this) );
    auto item3 = MenuItemImage::create(s_pathF1, s_pathF2, CC_CALLBACK_1(BaseTest::nextCallback, this) );

    auto menu = Menu::create(item1, item2, item3, nullptr);

    menu->setPosition(Vec2::ZERO);
    item1->setPosition(VisibleRect::center().x - item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2);
    item2->setPosition(VisibleRect::center().x, VisibleRect::bottom().y+item2->getContentSize().height/2);
    item3->setPosition(VisibleRect::center().x + item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2);

    addChild(menu, 9999);
}
Esempio n. 30
0
void EScene::onEnter(){
    Layer::onEnter();
    auto design_size = Size(1920,1080);
    setScale(0.9);
    setContentSize(design_size);
    //外框
    {
        auto draw = DrawNode::create();
        draw->drawRect(Vec2(0,0), Vec2(design_size.width,design_size.height), Color4F(1,0,0,1));
        addChild(draw,100);
    }
    //分辨率文字
    {
        
        TTFConfig ttfConfig("arial.ttf", 50, GlyphCollection::DYNAMIC,nullptr,true);
        // Green
        char str[32] = "";
        sprintf(str, "%d x %d",(int)design_size.width,(int)design_size.height);
        auto label = Label::createWithTTF(ttfConfig,str, TextHAlignment::CENTER, design_size.width);
        label->setAnchorPoint(Vec2(0.5,0));
        //        label->setVerticalAlignment(TextVAlignment::TOP);
        label->setPosition(design_size.width/2,design_size.height);
        label->setTextColor( Color4B::WHITE );
        //        label->setScale(sprite_frame_width/label->getContentSize().width);
        addChild(label);
        //        FileUtils::getInstance()->
    }
//    {
//        auto spr = Sprite::create("/Users/liao/test.png");
//        addChild(spr);
//    }
    
//    WebSocketDelegate* wd = new WebSocketDelegate();
//    auto socket = new network::WebSocket();
//    socket->init(*wd, "ws://127.0.0.1:9000");
}