UIButton 的实现原理
.cpp文件:
#include "HelloWorldScene.h" #include "ui/CocosGUI.h" #include "cocostudio/CocoStudio.h" #include "an01.h" using namespace cocos2d::ui; const char* font_UIListViewTest = "fonts/Marker Felt.ttf"; USING_NS_CC; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); // position the label on the center of the screen label->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer this->addChild(label, 1); //// //Size widgetSize = _widget->getContentSize(); // Add a label in which the button events will be displayed _displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32); _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f)); _displayValueLabel->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height / 2.0f)); addChild(_displayValueLabel); // Add the alert Text* alert = Text::create("Button", "fonts/Marker Felt.ttf", 30); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height / 2.0f - alert->getContentSize().height * 1.75f)); addChild(alert); // Create the button Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); CCLOG("content size should be greater than 0: width = %f, height = %f", button->getContentSize().width, button->getContentSize().height); button->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height / 2.0f)); button->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchEvent, this)); button->setZoomScale(0.4f); button->setPressedActionEnabled(true); addChild(button); button->setOpacity(100); // Create the imageview ImageView* imageView = ImageView::create(); imageView->setPosition(Vec2(visibleSize.width / 2.0f + 50 + button->getContentSize().width / 2, visibleSize.height / 2.0f)); imageView->setTag(12); addChild(imageView); return true; } void HelloWorld::touchEvent(Ref *pSender, Widget::TouchEventType type) { switch (type) { case Widget::TouchEventType::BEGAN: _displayValueLabel->setString(StringUtils::format("Touch Down")); break; case Widget::TouchEventType::MOVED: _displayValueLabel->setString(StringUtils::format("Touch Move")); break; case Widget::TouchEventType::ENDED: { _displayValueLabel->setString(StringUtils::format("Touch Up")); ImageView* imageView = (ImageView*)getChildByTag(12); imageView->setVisible(false); imageView->loadTexture("cocosui/ccicon.png"); imageView->setOpacity(0); imageView->setVisible(true); imageView->runAction(Sequence::create(FadeIn::create(0.5), DelayTime::create(1.0), FadeOut::create(0.5), nullptr)); Button *btn = (Button*)pSender; auto sc = an01::createScene(); auto reScene = TransitionSplitCols::create(5.0f, sc); // gridNodeTarget->runAction(PageTurn3D::create(2.0f, Size(15, 10))); Director::getInstance()->pushScene(reScene); btn->loadTextureNormal("cocosui/animationbuttonnormal.png"); } break; case Widget::TouchEventType::CANCELED: _displayValueLabel->setString(StringUtils::format("Touch Cancelled")); break; default: break; } } void HelloWorld::menuCloseCallback(Ref* pSender) { Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }
.h文件:
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "cocos-ext.h" #include "ui/CocosGUI.h" using namespace cocos2d::ui; class HelloWorld : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); void touchEvent(cocos2d::Ref* sender, cocos2d::ui::Widget::TouchEventType type); // a selector callback void menuCloseCallback(cocos2d::Ref* pSender); protected: //cocos2d::ui::Text* _displayValueLabel; //cocos2d::ui::Text* _displayValueLabel; cocos2d::ui::Text* _displayValueLabel; cocos2d::Layer* _uiLayer; cocos2d::ui::Layout* _widget; CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
效果: