Window_Weapon::Window_Weapon() :_coin_listener(nullptr),_money_listener(nullptr) { auto dispatcher = Director::getInstance()->getEventDispatcher(); _coin_listener = EventListenerCustom::create(EVENT_COIN_UPDATE, CC_CALLBACK_1(Window_Weapon::updateCoin, this)); _money_listener = EventListenerCustom::create(EVENT_MONEY_UPDATE, CC_CALLBACK_1(Window_Weapon::updateMoney, this)); dispatcher->addEventListenerWithFixedPriority(_coin_listener, 1); dispatcher->addEventListenerWithFixedPriority(_money_listener, 1); }
FontAtlas::FontAtlas(Font &theFont) : _font(&theFont) , _currentPageData(nullptr) , _fontAscender(0) , _toForegroundListener(nullptr) , _toBackgroundListener(nullptr) , _antialiasEnabled(true) { _font->retain(); FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(_font); if (fontTTf) { _commonLineHeight = _font->getFontMaxHeight(); _fontAscender = fontTTf->getFontAscender(); auto texture = new Texture2D; _currentPage = 0; _currentPageOrigX = 0; _currentPageOrigY = 0; _letterPadding = 0; if(fontTTf->isDistanceFieldEnabled()) { _letterPadding += 2 * FontFreeType::DistanceMapSpread; } _currentPageDataSize = CacheTextureWidth * CacheTextureHeight; if(fontTTf->getOutlineSize() > 0) { _currentPageDataSize *= 2; } _currentPageData = new unsigned char[_currentPageDataSize]; memset(_currentPageData, 0, _currentPageDataSize); auto pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8; texture->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) ); addTexture(texture,0); texture->release(); #if CC_ENABLE_CACHE_TEXTURE_DATA auto eventDispatcher = Director::getInstance()->getEventDispatcher(); _toBackgroundListener = EventListenerCustom::create(EVENT_COME_TO_BACKGROUND, CC_CALLBACK_1(FontAtlas::listenToBackground, this)); eventDispatcher->addEventListenerWithFixedPriority(_toBackgroundListener, 1); _toForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, CC_CALLBACK_1(FontAtlas::listenToForeground, this)); eventDispatcher->addEventListenerWithFixedPriority(_toForegroundListener, 1); #endif } }
void GetUnitScene::regEventTapAfterAnimation() { auto listenner =EventListenerCustom::create(GatchaResultLayer::EventNameTapAfterAnimation, CC_CALLBACK_1(GetUnitScene::onEventTapAfterAnimation, this)); auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->addEventListenerWithFixedPriority(listenner, 100); }
void CustomEventDispatchingPerfTest::generateTestFunctions() { TestFunction testFunctions[] = { { "custom-scenegraph", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listener = EventListenerCustom::create("custom_event_test_scenegraph", [](EventCustom* event){}); // Create new nodes listen to custom event for (int i = 0; i < this->_quantityOfNodes; ++i) { auto node = Node::create(); node->setTag(1000 + i); this->addChild(node); this->_nodes.push_back(node); dispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), node); } _lastRenderedCount = _quantityOfNodes; } EventCustom event("custom_event_test_scenegraph"); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&event); CC_PROFILER_STOP(this->profilerName()); } } , { "custom-fixed", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listener = EventListenerCustom::create("custom_event_test_fixed", [](EventCustom* event){}); for (int i = 0; i < this->_quantityOfNodes; ++i) { auto l = listener->clone(); this->_fixedPriorityListeners.push_back(l); dispatcher->addEventListenerWithFixedPriority(l, i+1); } _lastRenderedCount = _quantityOfNodes; } EventCustom event("custom_event_test_fixed"); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&event); CC_PROFILER_STOP(this->profilerName()); } } , }; for (const auto& func : testFunctions) { _testFunctions.push_back(func); } }
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } cocos2d::Size visibleSize = Director::getInstance()->getVisibleSize(); cocos2d::Point origin = Director::getInstance()->getVisibleOrigin(); _start = false; // プレイヤー表示 _player = PlayerView::create(); _player->setPosition(visibleSize.width/2, visibleSize.height/2); //_player->getPlayerModel()->setPosition(_player->getPosition()); this->addChild(_player, 0); _prev_pos = _player->getPosition(); // 操作系設定 auto dispatcher = Director::getInstance()->getEventDispatcher(); auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [=](Touch* touch, Event* event) { _player->getPlayerModel()->setIsShooting(true); _prev_pos = touch->getLocation(); return true; }; listener->onTouchMoved = [=](Touch* touch, Event* event) { auto diff = touch->getLocation() - _prev_pos; //auto pos = _player->getPlayerModel()->getPosition() + diff; //_player->getPlayerModel()->moveTo(pos); auto pos = _player->getPosition() + diff; _player->setPosition(pos); _prev_pos = touch->getLocation(); }; listener->onTouchEnded = [=](Touch* touch, Event* event) { _player->getPlayerModel()->setIsShooting(false); _prev_pos = touch->getLocation(); }; dispatcher->addEventListenerWithFixedPriority(listener, 128); // ロジックの登録 this->schedule(schedule_selector(HelloWorld::playerLogic)); // エネミーの表示 _enemy = PlayerView::create(); _enemy->setPosition(visibleSize.width * 3.0f / 4.0f + origin.x, visibleSize.height/2 + origin.y); //_enemy->getPlayerModel()->setPosition(_enemy->getPosition()); this->addChild(_enemy, 0); _enemy->setFlippedX(true); ConnectManager::getInstance()->connectAction(this); return true; }
FontAtlas::FontAtlas(Font &theFont) : _font(&theFont) , _fontFreeType(nullptr) , _iconv(nullptr) , _currentPageData(nullptr) , _fontAscender(0) //, _rendererRecreatedListener(nullptr) , _antialiasEnabled(true) , _currLineHeight(0) { _font->retain(); _fontFreeType = dynamic_cast<FontFreeType*>(_font); if (_fontFreeType) { _lineHeight = _font->getFontMaxHeight(); _fontAscender = _fontFreeType->getFontAscender(); auto texture = new (std::nothrow) Texture2D; _currentPage = 0; _currentPageOrigX = 0; _currentPageOrigY = 0; _letterEdgeExtend = 2; _letterPadding = 0; if (_fontFreeType->isDistanceFieldEnabled()) { _letterPadding += 2 * FontFreeType::DistanceMapSpread; } _currentPageDataSize = CacheTextureWidth * CacheTextureHeight; auto outlineSize = _fontFreeType->getOutlineSize(); if(outlineSize > 0) { _lineHeight += 2 * outlineSize; _currentPageDataSize *= 2; } _currentPageData = new unsigned char[_currentPageDataSize]; memset(_currentPageData, 0, _currentPageDataSize); auto pixelFormat = outlineSize > 0 ? CCTexture2DPixelFormat::kCCTexture2DPixelFormat_AI88 : CCTexture2DPixelFormat::kCCTexture2DPixelFormat_A8; texture->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) ); addTexture(texture,0); texture->release(); #if CC_ENABLE_CACHE_TEXTURE_DATA auto eventDispatcher = Director::getInstance()->getEventDispatcher(); _rendererRecreatedListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(FontAtlas::listenRendererRecreated, this)); eventDispatcher->addEventListenerWithFixedPriority(_rendererRecreatedListener, 1); #endif } }
void PopupLayer::initTouch() { auto dispatcher = Director::getInstance()->getEventDispatcher(); mTouchListener = EventListenerTouchOneByOne::create(); mTouchListener->setSwallowTouches(true); mTouchListener->onTouchBegan = CC_CALLBACK_2(PopupLayer::onTouchBegan, this); mTouchListener->onTouchMoved = CC_CALLBACK_2(PopupLayer::onTouchMoved, this); mTouchListener->onTouchEnded = CC_CALLBACK_2(PopupLayer::onTouchEnded, this); mTouchListener->onTouchCancelled = CC_CALLBACK_2(PopupLayer::onTouchCancelled, this); // dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); dispatcher->addEventListenerWithFixedPriority(mTouchListener, INT_MIN + 101); }
void InputDelegate::setTouchEnabled(bool enabled) { if (_touchEnabled != enabled) { auto dispatcher = Director::getInstance()->getEventDispatcher(); _touchEnabled = enabled; if (enabled) { if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE ) { // Register Touch Event auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(InputDelegate::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(InputDelegate::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(InputDelegate::onTouchesEnded, this); listener->onTouchesCancelled = CC_CALLBACK_2(InputDelegate::onTouchesCancelled, this); dispatcher->addEventListenerWithFixedPriority(listener, _touchPriority); _touchListener = listener; } else { // Register Touch Event auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(InputDelegate::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(InputDelegate::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(InputDelegate::onTouchEnded, this); listener->onTouchCancelled = CC_CALLBACK_2(InputDelegate::onTouchCancelled, this); dispatcher->addEventListenerWithFixedPriority(listener, _touchPriority); _touchListener = listener; } } else { dispatcher->removeEventListener(_touchListener); } } }
void PlayerWin::registerKeyboardEvent() { auto eventDispatcher = cocos2d::Director::getInstance()->getEventDispatcher(); auto keyEvent = cocos2d::EventListenerKeyboard::create(); keyEvent->onKeyReleased = [](EventKeyboard::KeyCode key, Event*) { auto event = EventCustom("APP.EVENT"); stringstream data; data << "{\"name\":\"keyReleased\",\"data\":" << (int)key << "}"; event.setDataString(data.str()); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); }; eventDispatcher->addEventListenerWithFixedPriority(keyEvent, 1); }
void InputDelegate::setAccelerometerEnabled(bool enabled) { if (enabled != _accelerometerEnabled) { _accelerometerEnabled = enabled; auto dispatcher = EventDispatcher::getInstance(); dispatcher->removeEventListener(_accelerometerListener); _accelerometerListener = nullptr; if (enabled) { auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(InputDelegate::onAcceleration, this)); dispatcher->addEventListenerWithFixedPriority(listener, -1); _accelerometerListener = listener; } } }
void InputDelegate::setKeypadEnabled(bool enabled) { if (enabled != _keypadEnabled) { _keypadEnabled = enabled; auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->removeEventListener(_keyboardListener); if (enabled) { auto listener = EventListenerKeyboard::create(); listener->onKeyPressed = CC_CALLBACK_2(InputDelegate::onKeyPressed, this); listener->onKeyReleased = CC_CALLBACK_2(InputDelegate::onKeyReleased, this); dispatcher->addEventListenerWithFixedPriority(listener, -1); _keyboardListener = listener; } } }
FontAtlas::FontAtlas(Font &theFont) : _font(&theFont) , _fontFreeType(nullptr) , _iconv(nullptr) , _currentPageData(nullptr) , _fontAscender(0) , _rendererRecreatedListener(nullptr) , _antialiasEnabled(true) , _currLineHeight(0) { _font->retain(); _fontFreeType = dynamic_cast<FontFreeType*>(_font); if (_fontFreeType) { _lineHeight = _font->getFontMaxHeight(); _fontAscender = _fontFreeType->getFontAscender(); _currentPage = 0; _currentPageOrigX = 0; _currentPageOrigY = 0; _letterEdgeExtend = 2; _letterPadding = 0; if (_fontFreeType->isDistanceFieldEnabled()) { _letterPadding += 2 * FontFreeType::DistanceMapSpread; } reinit(); #if CC_ENABLE_CACHE_TEXTURE_DATA auto eventDispatcher = Director::getInstance()->getEventDispatcher(); _rendererRecreatedListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(FontAtlas::listenRendererRecreated, this)); eventDispatcher->addEventListenerWithFixedPriority(_rendererRecreatedListener, 1); #endif } }
void KeyboardEventDispatchingPerfTest::generateTestFunctions() { TestFunction testFunctions[] = { { "keyboard-scenegraph", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listener = EventListenerKeyboard::create(); listener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event){}; listener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event){}; // Create new nodes listen to keyboard event for (int i = 0; i < this->_quantityOfNodes; ++i) { auto node = Node::create(); node->setTag(1000 + i); this->addChild(node); this->_nodes.push_back(node); dispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), node); } _lastRenderedCount = _quantityOfNodes; } EventKeyboard event(EventKeyboard::KeyCode::KEY_RETURN, true); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&event); CC_PROFILER_STOP(this->profilerName()); } } , { "keyboard-fixed", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listener = EventListenerKeyboard::create(); listener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event){}; listener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event){}; for (int i = 0; i < this->_quantityOfNodes; ++i) { auto l = listener->clone(); this->_fixedPriorityListeners.push_back(l); dispatcher->addEventListenerWithFixedPriority(l, i+1); } _lastRenderedCount = _quantityOfNodes; } EventKeyboard event(EventKeyboard::KeyCode::KEY_RETURN, true); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&event); CC_PROFILER_STOP(this->profilerName()); } } , }; for (const auto& func : testFunctions) { _testFunctions.push_back(func); } }
void TouchEventDispatchingPerfTest::generateTestFunctions() { TestFunction testFunctions[] = { { "OneByOne-scenegraph", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [](Touch* touch, Event* event){ return false; }; listener->onTouchMoved = [](Touch* touch, Event* event){}; listener->onTouchEnded = [](Touch* touch, Event* event){}; // Create new touchable nodes for (int i = 0; i < this->_quantityOfNodes; ++i) { auto node = Node::create(); node->setTag(1000 + i); this->addChild(node); this->_nodes.push_back(node); dispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), node); } _lastRenderedCount = _quantityOfNodes; } Size size = Director::getInstance()->getWinSize(); EventTouch touchEvent; touchEvent.setEventCode(EventTouch::EventCode::BEGAN); std::vector<Touch*> touches; for (int i = 0; i < 4; ++i) { Touch* touch = new (std::nothrow) Touch(); touch->autorelease(); touch->setTouchInfo(i, rand() % 200, rand() % 200); touches.push_back(touch); } touchEvent.setTouches(touches); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&touchEvent); CC_PROFILER_STOP(this->profilerName()); } } , { "OneByOne-fixed", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [](Touch* touch, Event* event){ return false; }; listener->onTouchMoved = [](Touch* touch, Event* event){}; listener->onTouchEnded = [](Touch* touch, Event* event){}; for (int i = 0; i < this->_quantityOfNodes; ++i) { auto l = listener->clone(); this->_fixedPriorityListeners.push_back(l); dispatcher->addEventListenerWithFixedPriority(l, i+1); } _lastRenderedCount = _quantityOfNodes; } Size size = Director::getInstance()->getWinSize(); EventTouch touchEvent; touchEvent.setEventCode(EventTouch::EventCode::BEGAN); std::vector<Touch*> touches; for (int i = 0; i < 4; ++i) { Touch* touch = new (std::nothrow) Touch(); touch->autorelease(); touch->setTouchInfo(i, rand() % 200, rand() % 200); touches.push_back(touch); } touchEvent.setTouches(touches); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&touchEvent); CC_PROFILER_STOP(this->profilerName()); } } , { "AllAtOnce-scenegraph", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){}; listener->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){}; listener->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){}; // Create new touchable nodes for (int i = 0; i < this->_quantityOfNodes; ++i) { auto node = Node::create(); node->setTag(1000 + i); this->addChild(node); this->_nodes.push_back(node); dispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), node); } _lastRenderedCount = _quantityOfNodes; } Size size = Director::getInstance()->getWinSize(); EventTouch touchEvent; touchEvent.setEventCode(EventTouch::EventCode::BEGAN); std::vector<Touch*> touches; for (int i = 0; i < 4; ++i) { Touch* touch = new (std::nothrow) Touch(); touch->autorelease(); touch->setTouchInfo(i, rand() % 200, rand() % 200); touches.push_back(touch); } touchEvent.setTouches(touches); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&touchEvent); CC_PROFILER_STOP(this->profilerName()); } } , { "AllAtOnce-fixed", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){}; listener->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){}; listener->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){}; for (int i = 0; i < this->_quantityOfNodes; ++i) { auto l = listener->clone(); this->_fixedPriorityListeners.push_back(l); dispatcher->addEventListenerWithFixedPriority(l, i+1); } _lastRenderedCount = _quantityOfNodes; } Size size = Director::getInstance()->getWinSize(); EventTouch touchEvent; touchEvent.setEventCode(EventTouch::EventCode::BEGAN); std::vector<Touch*> touches; for (int i = 0; i < 4; ++i) { Touch* touch = new (std::nothrow) Touch(); touch->autorelease(); touch->setTouchInfo(i, rand() % 200, rand() % 200); touches.push_back(touch); } touchEvent.setTouches(touches); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&touchEvent); CC_PROFILER_STOP(this->profilerName()); } } , { "TouchModeMix-scenegraph", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listenerOneByOne = EventListenerTouchOneByOne::create(); listenerOneByOne->onTouchBegan = [](Touch* touch, Event* event){ return false; }; listenerOneByOne->onTouchMoved = [](Touch* touch, Event* event){}; listenerOneByOne->onTouchEnded = [](Touch* touch, Event* event){}; auto listenerAllAtOnce = EventListenerTouchAllAtOnce::create(); listenerAllAtOnce->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){}; listenerAllAtOnce->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){}; listenerAllAtOnce->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){}; int i = 0; // Create new touchable nodes for (; i < this->_quantityOfNodes/2; ++i) { auto node = Node::create(); node->setTag(1000 + i); this->addChild(node); this->_nodes.push_back(node); dispatcher->addEventListenerWithSceneGraphPriority(listenerOneByOne->clone(), node); } for (; i < this->_quantityOfNodes; ++i) { auto node = Node::create(); node->setTag(1000 + i); this->addChild(node); this->_nodes.push_back(node); dispatcher->addEventListenerWithSceneGraphPriority(listenerAllAtOnce->clone(), node); } _lastRenderedCount = _quantityOfNodes; } Size size = Director::getInstance()->getWinSize(); EventTouch touchEvent; touchEvent.setEventCode(EventTouch::EventCode::BEGAN); std::vector<Touch*> touches; for (int i = 0; i < 4; ++i) { Touch* touch = new (std::nothrow) Touch(); touch->autorelease(); touch->setTouchInfo(i, rand() % 200, rand() % 200); touches.push_back(touch); } touchEvent.setTouches(touches); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&touchEvent); CC_PROFILER_STOP(this->profilerName()); } } , { "TouchModeMix-fixed", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listenerOneByOne = EventListenerTouchOneByOne::create(); listenerOneByOne->onTouchBegan = [](Touch* touch, Event* event){ return false; }; listenerOneByOne->onTouchMoved = [](Touch* touch, Event* event){}; listenerOneByOne->onTouchEnded = [](Touch* touch, Event* event){}; auto listenerAllAtOnce = EventListenerTouchAllAtOnce::create(); listenerAllAtOnce->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){}; listenerAllAtOnce->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){}; listenerAllAtOnce->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){}; int i = 0; for (; i < this->_quantityOfNodes/2; ++i) { auto l = listenerOneByOne->clone(); this->_fixedPriorityListeners.push_back(l); dispatcher->addEventListenerWithFixedPriority(l, i+1); } for (; i < this->_quantityOfNodes; ++i) { auto l = listenerAllAtOnce->clone(); this->_fixedPriorityListeners.push_back(l); dispatcher->addEventListenerWithFixedPriority(l, i+1); } _lastRenderedCount = _quantityOfNodes; } Size size = Director::getInstance()->getWinSize(); EventTouch touchEvent; touchEvent.setEventCode(EventTouch::EventCode::BEGAN); std::vector<Touch*> touches; for (int i = 0; i < 4; ++i) { Touch* touch = new (std::nothrow) Touch(); touch->autorelease(); touch->setTouchInfo(i, rand() % 200, rand() % 200); touches.push_back(touch); } touchEvent.setTouches(touches); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&touchEvent); CC_PROFILER_STOP(this->profilerName()); } } , }; for (const auto& func : testFunctions) { _testFunctions.push_back(func); } }
void SimulatorWin::setupUI() { auto menuBar = player::PlayerProtocol::getInstance()->getMenuService(); // FILE menuBar->addItem("FILE_MENU", tr("File")); menuBar->addItem("EXIT_MENU", tr("Exit"), "FILE_MENU"); // VIEW menuBar->addItem("VIEW_MENU", tr("View")); SimulatorConfig *config = SimulatorConfig::getInstance(); int current = config->checkScreenSize(_project.getFrameSize()); for (int i = 0; i < config->getScreenSizeCount(); i++) { SimulatorScreenSize size = config->getScreenSize(i); std::stringstream menuId; menuId << "VIEWSIZE_ITEM_MENU_" << i; auto menuItem = menuBar->addItem(menuId.str(), size.title.c_str(), "VIEW_MENU"); if (i == current) { menuItem->setChecked(true); } } menuBar->addItem("DIRECTION_MENU_SEP", "-", "VIEW_MENU"); menuBar->addItem("DIRECTION_PORTRAIT_MENU", tr("Portrait"), "VIEW_MENU") ->setChecked(_project.isPortraitFrame()); menuBar->addItem("DIRECTION_LANDSCAPE_MENU", tr("Landscape"), "VIEW_MENU") ->setChecked(_project.isLandscapeFrame()); menuBar->addItem("VIEW_SCALE_MENU_SEP", "-", "VIEW_MENU"); std::vector<player::PlayerMenuItem*> scaleMenuVector; auto scale100Menu = menuBar->addItem("VIEW_SCALE_MENU_100", tr("Zoom Out").append(" (100%)"), "VIEW_MENU"); auto scale75Menu = menuBar->addItem("VIEW_SCALE_MENU_75", tr("Zoom Out").append(" (75%)"), "VIEW_MENU"); auto scale50Menu = menuBar->addItem("VIEW_SCALE_MENU_50", tr("Zoom Out").append(" (50%)"), "VIEW_MENU"); auto scale25Menu = menuBar->addItem("VIEW_SCALE_MENU_25", tr("Zoom Out").append(" (25%)"), "VIEW_MENU"); int frameScale = int(_project.getFrameScale() * 100); if (frameScale == 100) { scale100Menu->setChecked(true); } else if (frameScale == 75) { scale75Menu->setChecked(true); } else if (frameScale == 50) { scale50Menu->setChecked(true); } else if (frameScale == 25) { scale25Menu->setChecked(true); } else { scale100Menu->setChecked(true); } scaleMenuVector.push_back(scale100Menu); scaleMenuVector.push_back(scale75Menu); scaleMenuVector.push_back(scale50Menu); scaleMenuVector.push_back(scale25Menu); menuBar->addItem("REFRESH_MENU_SEP", "-", "VIEW_MENU"); menuBar->addItem("REFRESH_MENU", tr("Refresh"), "VIEW_MENU"); HWND &hwnd = _hwnd; ProjectConfig &project = _project; auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->addEventListenerWithFixedPriority(EventListenerCustom::create("APP.EVENT", [&project, &hwnd, scaleMenuVector](EventCustom* event){ auto menuEvent = dynamic_cast<AppEvent*>(event); if (menuEvent) { rapidjson::Document dArgParse; dArgParse.Parse<0>(menuEvent->getDataString().c_str()); if (dArgParse.HasMember("name")) { string strcmd = dArgParse["name"].GetString(); if (strcmd == "menuClicked") { player::PlayerMenuItem *menuItem = static_cast<player::PlayerMenuItem*>(menuEvent->getUserData()); if (menuItem) { if (menuItem->isChecked()) { return; } string data = dArgParse["data"].GetString(); auto player = player::PlayerProtocol::getInstance(); if ((data == "CLOSE_MENU") || (data == "EXIT_MENU")) { player->quit(); } else if (data == "REFRESH_MENU") { player->relaunch(); } else if (data.find("VIEW_SCALE_MENU_") == 0) // begin with VIEW_SCALE_MENU_ { string tmp = data.erase(0, strlen("VIEW_SCALE_MENU_")); float scale = atof(tmp.c_str()) / 100.0f; project.setFrameScale(scale); auto glview = static_cast<GLViewImpl*>(Director::getInstance()->getOpenGLView()); glview->setFrameZoomFactor(scale); // update scale menu state for (auto &it : scaleMenuVector) { it->setChecked(false); } menuItem->setChecked(true); // update window size RECT rect; GetWindowRect(hwnd, &rect); MoveWindow(hwnd, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top + GetSystemMetrics(SM_CYMENU), FALSE); // fix: can not update window on some windows system ::SendMessage(hwnd, WM_MOVE, NULL, NULL); } else if (data.find("VIEWSIZE_ITEM_MENU_") == 0) // begin with VIEWSIZE_ITEM_MENU_ { string tmp = data.erase(0, strlen("VIEWSIZE_ITEM_MENU_")); int index = atoi(tmp.c_str()); SimulatorScreenSize size = SimulatorConfig::getInstance()->getScreenSize(index); if (project.isLandscapeFrame()) { std::swap(size.width, size.height); } project.setFrameSize(cocos2d::Size(size.width, size.height)); project.setWindowOffset(cocos2d::Vec2(player->getPositionX(), player->getPositionY())); player->openProjectWithProjectConfig(project); } else if (data == "DIRECTION_PORTRAIT_MENU") { project.changeFrameOrientationToPortait(); player->openProjectWithProjectConfig(project); } else if (data == "DIRECTION_LANDSCAPE_MENU") { project.changeFrameOrientationToLandscape(); player->openProjectWithProjectConfig(project); } } } } } }), 1); AppDelegate *app = _app; auto listener = EventListenerCustom::create(kAppEventDropName, [&project, app](EventCustom* event) { AppEvent *dropEvent = dynamic_cast<AppEvent*>(event); if (dropEvent) { string dirPath = dropEvent->getDataString() + "/"; string configFilePath = dirPath + CONFIG_FILE; if (FileUtils::getInstance()->isDirectoryExist(dirPath) && FileUtils::getInstance()->isFileExist(configFilePath)) { // parse config.json ConfigParser::getInstance()->readConfig(configFilePath); project.setProjectDir(dirPath); project.setScriptFile(ConfigParser::getInstance()->getEntryFile()); project.setWritablePath(dirPath); app->setProjectConfig(project); app->reopenProject(); } } }); dispatcher->addEventListenerWithFixedPriority(listener, 1); }
EventListenerCustom* EventDispatcher::addCustomEventListener(const std::string &eventName, const std::function<void(EventCustom*)>& callback) { EventListenerCustom *listener = EventListenerCustom::create(eventName, callback); addEventListenerWithFixedPriority(listener, 1); return listener; }