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();
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCMenuItemFont* pBackItem = CCMenuItemFont::create("Back", this,
|
2012-04-19 14:35:52 +08:00
|
|
|
menu_selector(NotificationCenterTest::toExtensionsMainLayer));
|
|
|
|
pBackItem->setPosition(ccp(s.width - 50, 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);
|
2012-07-23 22:49:11 +08:00
|
|
|
CCMenuItemToggle *item = CCMenuItemToggle::createWithTarget(this, menu_selector(NotificationCenterTest::toggleSwitch), 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);
|
2012-07-23 22:49:11 +08:00
|
|
|
CCMenuItemToggle *item = CCMenuItemToggle::createWithTarget(this, menu_selector(NotificationCenterTest::connectToSwitch), 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);
|
|
|
|
}
|
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
CCNotificationCenter::sharedNotificationCenter()->postNotification(MSG_SWITCH_STATE, (CCObject*)item->getSelectedIndex());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2012-04-24 15:02:18 +08:00
|
|
|
CCNotificationCenter::sharedNotificationCenter()->postNotification(MSG_SWITCH_STATE, (CCObject*)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);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|