CCNotificationCenter(二)---NotificationCenter Test

16389 ワード

// 

#ifndef __NOTIFICATIONCENTERTEST_H__
#define __NOTIFICATIONCENTERTEST_H__

#include "cocos2d.h"

class NotificationCenterTest : public cocos2d::CCLayer
{
public:
    NotificationCenterTest();
    void toExtensionsMainLayer(cocos2d::CCObject* sender);
    void toggleSwitch(cocos2d::CCObject *sender);
    void connectToSwitch(cocos2d::CCObject *sender);
private:
    bool m_bShowImage;
};

void runNotificationCenterTest();

#endif /* __NOTIFICATIONCENTERTEST_H__ */
// 
void runNotificationCenterTest()
{
    CCScene* pScene = CCScene::create();
    NotificationCenterTest* pLayer = new NotificationCenterTest();
    pScene->addChild(pLayer);
    CCDirector::sharedDirector()->replaceScene(pScene);
    pLayer->release();
}
//
class Light : public CCSprite
{
public:
    Light();
    ~Light();
//
    static Light* lightWithFile(const char* name);
// 
    void setIsConnectToSwitch(bool bConnectToSwitch);
// obj 
    void switchStateChanged(CCObject* obj);
// 
    void updateLightState();

private:
// 
    bool m_bConnected;
// 
    static bool s_bSwitchOn;
};
//
Light::~Light()
{
    CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, MSG_SWITCH_STATE);
}
// 
Light* Light::lightWithFile(const char* name)
{
    Light* pLight = new Light();
    pLight->initWithFile(name);
    pLight->autorelease();
    return pLight;
}
NotificationCenterTest::NotificationCenterTest()
: m_bShowImage(false)
{
    CCSize s = CCDirector::sharedDirector()->getWinSize();
// 
    CCMenuItemFont* pBackItem = CCMenuItemFont::create("Back", this,
        menu_selector(NotificationCenterTest::toExtensionsMainLayer));
    pBackItem->setPosition(ccp(VisibleRect::rightBottom().x - 50, VisibleRect::rightBottom().y + 25));
    CCMenu* pBackMenu = CCMenu::create(pBackItem, NULL);
    pBackMenu->setPosition( CCPointZero );
    addChild(pBackMenu);
// 
    CCLabelTTF *label1 = CCLabelTTF::create("switch off", "Marker Felt", 26);
    CCLabelTTF *label2 = CCLabelTTF::create("switch on", "Marker Felt", 26);
    CCMenuItemLabel *item1 = CCMenuItemLabel::create(label1);
    CCMenuItemLabel *item2 = CCMenuItemLabel::create(label2);
    CCMenuItemToggle *item = CCMenuItemToggle::createWithTarget(this, menu_selector(NotificationCenterTest::toggleSwitch), item1, item2, NULL);
    // turn on
    item->setSelectedIndex(1);// 
    CCMenu *menu = CCMenu::create(item, NULL);
    menu->setPosition(ccp(s.width/2+100, s.height/2));
    addChild(menu);

    CCMenu *menuConnect = CCMenu::create();
    menuConnect->setPosition(CCPointZero);
    addChild(menuConnect);
// 
    for (int i = 1; i <= 3; i++)
    {
// 
        Light* light = Light::lightWithFile("Images/Pea.png");
        light->setTag(kTagLight+i);
        light->setPosition(ccp(100, s.height/4*i));
        addChild(light);
// 
        CCLabelTTF *label1 = CCLabelTTF::create("not connected", "Marker Felt", 26);
        CCLabelTTF *label2 = CCLabelTTF::create("connected", "Marker Felt", 26);
        CCMenuItemLabel *item1 = CCMenuItemLabel::create(label1);
        CCMenuItemLabel *item2 = CCMenuItemLabel::create(label2);
        CCMenuItemToggle *item = CCMenuItemToggle::createWithTarget(this, menu_selector(NotificationCenterTest::connectToSwitch), item1, item2, NULL);
        item->setTag(kTagConnect+i);
        item->setPosition(ccp(light->getPosition().x, light->getPosition().y+50));
// 
        menuConnect->addChild(item, 0);
        if (i == 2)
        {
// 
            item->setSelectedIndex(1);
        }
        bool bConnected = item->getSelectedIndex() == 1 ? true : false;
// , , 
        light->setIsConnectToSwitch(bConnected);
    }
// MSG_SWITCH_STATE 
    CCNotificationCenter::sharedNotificationCenter()->postNotification(MSG_SWITCH_STATE, (CCObject*)item->getSelectedIndex());
}
void Light::setIsConnectToSwitch(bool bConnectToSwitch)
{
    m_bConnected = bConnectToSwitch;
    if (m_bConnected)
    {
// MSG_SWITCH_STATE 
        CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(Light::switchStateChanged), MSG_SWITCH_STATE, NULL);
    }
    else
    {
        CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, MSG_SWITCH_STATE);
    }
    updateLightState();
}
// 
void Light::switchStateChanged(CCObject* obj)
{
// , false, true
    s_bSwitchOn = obj == 0x00 ? false : true;
// 
    updateLightState();
}
// 
void Light::updateLightState()
{
// , , 255, 
//
    if (s_bSwitchOn && m_bConnected)
    {
//this , 
        this->setOpacity(255);
    }
    else
    {
        this->setOpacity(50);
    }
}
// 
void NotificationCenterTest::toExtensionsMainLayer(cocos2d::CCObject* sender)
{
    ExtensionsTestScene* pScene = new ExtensionsTestScene();
    pScene->runThisTest();
    pScene->release();
}
// 
void NotificationCenterTest::toggleSwitch(CCObject *sender)
{
    CCMenuItemToggle* item = (CCMenuItemToggle*)sender;
// 
    int index = item->getSelectedIndex();
// MSG_SWITCH_STATE , index 
    CCNotificationCenter::sharedNotificationCenter()->postNotification(MSG_SWITCH_STATE, (CCObject*)index);
}