2012-04-19 14:35:52 +08:00
|
|
|
#include "NotificationCenterTest.h"
|
2012-04-16 23:16:03 +08:00
|
|
|
#include "../ExtensionsTest.h"
|
2012-07-19 17:22:36 +08:00
|
|
|
#include "support/CCNotificationCenter.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#define kTagLight 100
|
|
|
|
#define kTagConnect 200
|
|
|
|
|
|
|
|
#define MSG_SWITCH_STATE "SwitchState"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
|
|
|
|
class Light : public CCSprite
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Light();
|
|
|
|
~Light();
|
|
|
|
|
|
|
|
static Light* lightWithFile(const char* name);
|
|
|
|
|
|
|
|
void setIsConnectToSwitch(bool bConnectToSwitch);
|
|
|
|
void switchStateChanged(CCObject* obj);
|
|
|
|
void updateLightState();
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_bConnected;
|
|
|
|
static bool s_bSwitchOn;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool Light::s_bSwitchOn = false;
|
|
|
|
|
|
|
|
Light::Light()
|
|
|
|
: m_bConnected(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Light::~Light()
|
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, MSG_SWITCH_STATE);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Light* Light::lightWithFile(const char* name)
|
|
|
|
{
|
|
|
|
Light* pLight = new Light();
|
|
|
|
pLight->initWithFile(name);
|
|
|
|
pLight->autorelease();
|
|
|
|
return pLight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Light::setIsConnectToSwitch(bool bConnectToSwitch)
|
|
|
|
{
|
|
|
|
m_bConnected = bConnectToSwitch;
|
|
|
|
if (m_bConnected)
|
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(Light::switchStateChanged), MSG_SWITCH_STATE, NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, MSG_SWITCH_STATE);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
updateLightState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Light::switchStateChanged(CCObject* obj)
|
|
|
|
{
|
2012-08-02 13:02:59 +08:00
|
|
|
s_bSwitchOn = obj == 0x00 ? false : true;
|
2012-04-19 14:35:52 +08:00
|
|
|
updateLightState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Light::updateLightState()
|
|
|
|
{
|
|
|
|
if (s_bSwitchOn && m_bConnected)
|
|
|
|
{
|
|
|
|
this->setOpacity(255);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->setOpacity(50);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationCenterTest::NotificationCenterTest()
|
|
|
|
: m_bShowImage(false)
|
|
|
|
{
|
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
|
2013-06-12 07:30:05 +08:00
|
|
|
CCMenuItemFont* pBackItem = CCMenuItemFont::create("Back", CALLBACK_1(NotificationCenterTest::toExtensionsMainLayer, this));
|
2012-10-23 17:48:50 +08:00
|
|
|
pBackItem->setPosition(ccp(VisibleRect::rightBottom().x - 50, VisibleRect::rightBottom().y + 25));
|
2012-06-14 15:13:16 +08:00
|
|
|
CCMenu* pBackMenu = CCMenu::create(pBackItem, NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
pBackMenu->setPosition( CCPointZero );
|
|
|
|
addChild(pBackMenu);
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
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);
|
2013-06-12 07:30:05 +08:00
|
|
|
CCMenuItemToggle *item = CCMenuItemToggle::createWithCallback( CALLBACK_1(NotificationCenterTest::toggleSwitch, this), item1, item2, NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
// turn on
|
|
|
|
item->setSelectedIndex(1);
|
2012-06-14 15:13:16 +08:00
|
|
|
CCMenu *menu = CCMenu::create(item, NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
menu->setPosition(ccp(s.width/2+100, s.height/2));
|
|
|
|
addChild(menu);
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCMenu *menuConnect = CCMenu::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
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);
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
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);
|
2013-06-12 07:30:05 +08:00
|
|
|
CCMenuItemToggle *item = CCMenuItemToggle::createWithCallback( CALLBACK_1(NotificationCenterTest::connectToSwitch, this), item1, item2, NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-03-03 10:32:09 +08:00
|
|
|
CCNotificationCenter::sharedNotificationCenter()->postNotification(MSG_SWITCH_STATE, (CCObject*)(intptr_t)item->getSelectedIndex());
|
2013-03-10 22:34:01 +08:00
|
|
|
|
|
|
|
/* for testing removeAllObservers */
|
|
|
|
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(NotificationCenterTest::doNothing), "random-observer1", NULL);
|
|
|
|
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(NotificationCenterTest::doNothing), "random-observer2", NULL);
|
|
|
|
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(NotificationCenterTest::doNothing), "random-observer3", NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationCenterTest::toExtensionsMainLayer(cocos2d::CCObject* sender)
|
|
|
|
{
|
2013-03-10 22:34:01 +08:00
|
|
|
/* for testing removeAllObservers */
|
2013-03-13 07:52:10 +08:00
|
|
|
int CC_UNUSED numObserversRemoved = CCNotificationCenter::sharedNotificationCenter()->removeAllObservers(this);
|
2013-03-10 22:34:01 +08:00
|
|
|
CCAssert(numObserversRemoved >= 3, "All observers were not removed!");
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ExtensionsTestScene* pScene = new ExtensionsTestScene();
|
|
|
|
pScene->runThisTest();
|
|
|
|
pScene->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationCenterTest::toggleSwitch(CCObject *sender)
|
|
|
|
{
|
|
|
|
CCMenuItemToggle* item = (CCMenuItemToggle*)sender;
|
|
|
|
int index = item->getSelectedIndex();
|
2013-03-03 10:32:09 +08:00
|
|
|
CCNotificationCenter::sharedNotificationCenter()->postNotification(MSG_SWITCH_STATE, (CCObject*)(intptr_t)index);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationCenterTest::connectToSwitch(CCObject *sender)
|
|
|
|
{
|
|
|
|
CCMenuItemToggle* item = (CCMenuItemToggle*)sender;
|
|
|
|
bool bConnected = item->getSelectedIndex() == 0 ? false : true;
|
|
|
|
Light* pLight = (Light*)this->getChildByTag(item->getTag()-kTagConnect+kTagLight);
|
|
|
|
pLight->setIsConnectToSwitch(bConnected);
|
|
|
|
}
|
|
|
|
|
2013-03-10 22:34:01 +08:00
|
|
|
void NotificationCenterTest::doNothing(cocos2d::CCObject *sender)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-14 18:04:56 +08:00
|
|
|
void runNotificationCenterTest()
|
2012-02-10 11:46:18 +08:00
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
CCScene* pScene = CCScene::create();
|
2012-02-14 18:04:56 +08:00
|
|
|
NotificationCenterTest* pLayer = new NotificationCenterTest();
|
|
|
|
pScene->addChild(pLayer);
|
|
|
|
CCDirector::sharedDirector()->replaceScene(pScene);
|
2012-02-10 11:46:18 +08:00
|
|
|
pLayer->release();
|
|
|
|
}
|