void DBCCArmatureNode::registerMovementEventHandler(cocos2d::LUA_FUNCTION func) { unregisterMovementEventHandler(); _movementEventHandler = func; auto dispatcher = getCCEventDispatcher(); auto f = [this](cocos2d::EventCustom *event) { auto eventData = (dragonBones::EventData*)(event->getUserData()); auto type = (int) eventData->getType(); auto movementId = eventData->animationState->name; auto lastState = eventData->armature->getAnimation()->getLastAnimationState(); auto stack = cocos2d::LuaEngine::getInstance()->getLuaStack(); stack->pushObject(this, "db.DBCCArmatureNode"); stack->pushInt(type); stack->pushString(movementId.c_str(), movementId.size()); stack->pushBoolean(lastState == eventData->animationState); stack->executeFunctionByHandler(_movementEventHandler, 4); }; dispatcher->addCustomEventListener(dragonBones::EventData::COMPLETE, f); dispatcher->addCustomEventListener(dragonBones::EventData::LOOP_COMPLETE, f); }
PlayNoteRenderer::PlayNoteRenderer(const PlayChannelPlayer* player, Sprite* baseSprite): mChannelPlayer(player), mBaseSprite(baseSprite) { scheduleUpdate(); autorelease(); auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->addCustomEventListener(Event::BpmEvent::CHANGED, std::bind(&PlayNoteRenderer::onBpmChanged, this, std::placeholders::_1)); dispatcher->addCustomEventListener(Event::JudgeEvent::JUDGE, std::bind(&PlayNoteRenderer::onJudge, this, std::placeholders::_1)); dispatcher->addCustomEventListener(Event::SpeedEvent::CHANGED, std::bind(&PlayNoteRenderer::onSpeedChanged, this, std::placeholders::_1)); mBaseSprite->retain(); setContentSize(Size(mBaseSprite->getContentSize().width, 0)); }
bool GameScene::init() { if( !CCLayer::init() ) return false; #define KEY_MAP(name) EventKeyboard::KeyCode::KEY_##name mKeyMap[KEY_MAP(LEFT_SHIFT)] = Key::Player1_Scratch; mKeyMap[KEY_MAP(Z)] = Key::Player1_1; mKeyMap[KEY_MAP(S)] = Key::Player1_2; mKeyMap[KEY_MAP(X)] = Key::Player1_3; mKeyMap[KEY_MAP(D)] = Key::Player1_4; mKeyMap[KEY_MAP(C)] = Key::Player1_5; mKeyMap[KEY_MAP(F)] = Key::Player1_6; mKeyMap[KEY_MAP(V)] = Key::Player1_7; mKeyMap[KEY_MAP(RIGHT_SHIFT)] = Key::Player2_Scratch; mKeyMap[KEY_MAP(M)] = Key::Player2_1; mKeyMap[KEY_MAP(K)] = Key::Player2_2; mKeyMap[KEY_MAP(COMMA)] = Key::Player2_3; mKeyMap[KEY_MAP(L)] = Key::Player2_4; mKeyMap[KEY_MAP(PERIOD)] = Key::Player2_5; mKeyMap[KEY_MAP(SEMICOLON)] = Key::Player2_6; mKeyMap[KEY_MAP(SLASH)] = Key::Player2_7; mKeyMap[KEY_MAP(F3)] = Key::SpeedDown; mKeyMap[KEY_MAP(F4)] = Key::SpeedUp; #undef KEY_MAP for(auto it = mKeyMap.begin(); it != mKeyMap.end(); it++) { mKeyState[it->second] = Key::KEY_UP; } scheduleOnce(schedule_selector(GameScene::start), .0f); scheduleUpdate(); auto keyListener = EventListenerKeyboard::create(); keyListener->onKeyPressed = CC_CALLBACK_2(GameScene::onKeyDown, this); keyListener->onKeyReleased = CC_CALLBACK_2(GameScene::onKeyUp, this); auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->addEventListenerWithSceneGraphPriority(keyListener, this); dispatcher->addCustomEventListener(Event::BpmEvent::CHANGED, CC_CALLBACK_1(GameScene::onBpmChanged, this)); dispatcher->addCustomEventListener(Event::JudgeEvent::JUDGE, CC_CALLBACK_1(GameScene::onJudge, this)); return true; }
Moblet::Moblet() : mRun(true) { //printf("Moblet::Moblet - %x", this); //environment = this; addKeyListener(this); addPointerListener(this); addCloseListener(this); addCustomEventListener(this); }
void ArcadeMode::init() { HUDLayer = ArcadeHUD::create(); moveSpeed = ARCADE_MODE_INITIAL_MOVE_SPEED; nextRowToTouch = 1; startTimer(); auto eventDispatcher = Director::getInstance()->getEventDispatcher(); eventDispatcher->addCustomEventListener("ResumeGame", [=](EventCustom* e) { startTimer(); }); eventDispatcher->addCustomEventListener("PauseGame", [=](EventCustom* e) { stopTimer(); }); eventDispatcher->addCustomEventListener("GameWin", [=](EventCustom* e) { stopTimer(); }); }
void GameplayLayer::_setupEventHandlers() { auto dispatcher = Director::getInstance()->getEventDispatcher(); auto diceDragStartedCallback = CC_CALLBACK_1(GameplayLayer::_handleActionDiceDragStarted, this); this->setDiceDragStartedListener( dispatcher->addCustomEventListener(EVT_ACTION_DICE_DRAG_STARTED, diceDragStartedCallback) ); auto diceDragMovedCallback = CC_CALLBACK_1(GameplayLayer::_handleActionDiceDragMoved, this); this->setDiceDragMovedListener( dispatcher->addCustomEventListener(EVT_ACTION_DICE_DRAG_MOVED, diceDragMovedCallback) ); auto diceDragEndedCallback = CC_CALLBACK_1(GameplayLayer::_handleActionDiceDragEnded, this); this->setDiceDragEndedListener( dispatcher->addCustomEventListener(EVT_ACTION_DICE_DRAG_ENDED, diceDragEndedCallback) ); }
void DungeonLayer::_setupEventHandlers() { auto dispatcher = Director::getInstance()->getEventDispatcher(); auto lastTileHasBeenPlacedCallback = CC_CALLBACK_1(DungeonLayer::_handleLastTileHasBeenPlaced, this); this->setLastTileHasBeenPlacedListener( dispatcher->addCustomEventListener(EVT_LAST_TILE_HAS_BEEN_PLACED, lastTileHasBeenPlacedCallback) ); auto monsterDiceGeneratedCallback = CC_CALLBACK_1(DungeonLayer::_handleMonsterDiceGenerated, this); this->setMonsterDiceGeneratedListener( dispatcher->addCustomEventListener(EVT_MONSTER_DICE_GENERATED, monsterDiceGeneratedCallback) ); auto roomsPlacementsCallback = CC_CALLBACK_1(DungeonLayer::_handleRoomsPlacements, this); this->setRoomsPlacementsListener( dispatcher->addCustomEventListener(EVT_ROOMS_HAVE_BEEN_PLACED, roomsPlacementsCallback) ); auto monsterMovedCallback = CC_CALLBACK_1(DungeonLayer::_handleMonsterMoved, this); this->setMonsterMovedListener( dispatcher->addCustomEventListener(EVT_MONSTER_MOVED, monsterMovedCallback) ); auto monstersFinishedMovingCallback = CC_CALLBACK_1(DungeonLayer::_handleMonstersFinishedMoving, this); this->setMonstersFinishedMovingListener( dispatcher->addCustomEventListener(EVT_MONSTERS_FINISHED_MOVING, monstersFinishedMovingCallback) ); auto monstersFinishedRisingCallback = CC_CALLBACK_1(DungeonLayer::_handleMonstersFinishedRising, this); this->setMonstersFinishedRisingListener( dispatcher->addCustomEventListener(EVT_MONSTERS_FINISHED_RISING, monstersFinishedRisingCallback) ); }
InventoryPanel::InventoryPanel(Inventory* inventory) { Node::init(); _inventory = inventory; _inventory->retain(); _build(); _update(); auto eventDispatcher = cocos2d::Director::getInstance()->getEventDispatcher(); _inventoryChangedListener = eventDispatcher->addCustomEventListener(Inventory::ITEM_COUNT_CHANGED, [this](cocos2d::EventCustom* e) { _update(); }); }
void DBCCArmatureNode::registerFrameEventHandler(cocos2d::LUA_FUNCTION func) { unregisterFrameEventHandler(); _frameEventHandler = func; auto dispatcher = getCCEventDispatcher(); auto f = [this](cocos2d::EventCustom *event) { auto eventData = (dragonBones::EventData*)(event->getUserData()); auto type = (int) eventData->getType(); auto movementId = eventData->animationState->name; auto frameLabel = eventData->frameLabel; auto stack = cocos2d::LuaEngine::getInstance()->getLuaStack(); stack->pushObject(this, "db.DBCCArmatureNode"); stack->pushInt(type); stack->pushString(movementId.c_str(), movementId.size()); stack->pushString(frameLabel.c_str(), frameLabel.size()); stack->executeFunctionByHandler(_frameEventHandler, 4); }; dispatcher->addCustomEventListener(dragonBones::EventData::ANIMATION_FRAME_EVENT, f); }
int PlayerWin::run() { FileUtils::getInstance()->setPopupNotify(false); INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(InitCtrls); InitCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&InitCtrls); // set QUICK_V3_ROOT const char *QUICK_V3_ROOT = getenv("QUICK_V3_ROOT"); if (!QUICK_V3_ROOT || strlen(QUICK_V3_ROOT) == 0) { MessageBox("Please run \"setup_win.bat\", set Quick-Cocos2dx-Community root path.", "Quick-Cocos2dx-Community player error"); return 1; } _project.setQuickCocos2dxRootPath(QUICK_V3_ROOT); // load project config from command line args vector<string> args; for (int i = 0; i < __argc; ++i) { wstring ws(__wargv[i]); string s; s.assign(ws.begin(), ws.end()); args.push_back(s); } _project.parseCommandLine(args); if (_project.getProjectDir().empty()) { if (args.size() >= 2) { // for Code IDE before RC2 _project.setProjectDir(args.at(1)); } else { _project.resetToWelcome(); } } // set framework path if (!_project.isLoadPrecompiledFramework()) { FileUtils::getInstance()->addSearchPath(_project.getQuickCocos2dxRootPath() + "quick/"); } // create the application instance _app = new AppDelegate(); _app->setProjectConfig(_project); // set window icon HICON icon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_PLAYER)); // create console window if (_project.isShowConsole()) { AllocConsole(); _hwndConsole = GetConsoleWindow(); if (_hwndConsole != NULL) { SendMessage(_hwndConsole, WM_SETICON, ICON_BIG, (LPARAM)icon); SendMessage(_hwndConsole, WM_SETICON, ICON_SMALL, (LPARAM)icon); ShowWindow(_hwndConsole, SW_SHOW); BringWindowToTop(_hwndConsole); freopen("CONOUT$", "wt", stdout); freopen("CONOUT$", "wt", stderr); HMENU hmenu = GetSystemMenu(_hwndConsole, FALSE); if (hmenu != NULL) { DeleteMenu(hmenu, SC_CLOSE, MF_BYCOMMAND); } } } // log file if (_project.isWriteDebugLogToFile()) { const string debugLogFilePath = _project.getDebugLogFilePath(); _writeDebugLogFile = fopen(debugLogFilePath.c_str(), "w"); if (!_writeDebugLogFile) { CCLOG("Cannot create debug log file %s", debugLogFilePath.c_str()); } } // set environments SetCurrentDirectoryA(_project.getProjectDir().c_str()); FileUtils::getInstance()->setSearchRootPath(_project.getProjectDir().c_str()); FileUtils::getInstance()->setWritablePath(_project.getWritableRealPath().c_str()); // check screen DPI HDC screen = GetDC(0); int dpi = GetDeviceCaps(screen, LOGPIXELSX); ReleaseDC(0, screen); // set scale with DPI // 96 DPI = 100 % scaling // 120 DPI = 125 % scaling // 144 DPI = 150 % scaling // 192 DPI = 200 % scaling // http://msdn.microsoft.com/en-us/library/windows/desktop/dn469266%28v=vs.85%29.aspx#dpi_and_the_desktop_scaling_factor // // enable DPI-Aware with DeclareDPIAware.manifest // http://msdn.microsoft.com/en-us/library/windows/desktop/dn469266%28v=vs.85%29.aspx#declaring_dpi_awareness float screenScale = 1.0f; //if (dpi >= 120 && dpi < 144) //{ // screenScale = 1.25f; //} //else if (dpi >= 144 && dpi < 192) //{ // screenScale = 1.5f; //} //else if (dpi >= 192) //{ // screenScale = 2.0f; //} CCLOG("SCREEN DPI = %d, SCREEN SCALE = %0.2f", dpi, screenScale); // create opengl view Size frameSize = _project.getFrameSize(); float frameScale = 1.0f; if (_project.isRetinaDisplay()) { frameSize.width *= screenScale; frameSize.height *= screenScale; } else { frameScale = screenScale; } const Rect frameRect = Rect(0, 0, frameSize.width, frameSize.height); const bool isResize = _project.isResizeWindow(); GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; GLView::setGLContextAttrs(glContextAttrs); //auto glview = GLView::createWithRect("Quick-Cocos2dx-Community", frameRect, frameScale, isResize, false, true); auto glview = GLViewImpl::createWithRect("Quick-Cocos2dx-Community", frameRect, frameScale); _hwnd = glview->getWin32Window(); SendMessage(_hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon); SendMessage(_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)icon); FreeResource(icon); auto director = Director::getInstance(); director->setOpenGLView(glview); // set window position if (_project.getProjectDir().length()) { setZoom(_project.getFrameScale()); } Vec2 pos = _project.getWindowOffset(); if (pos.x != 0 && pos.y != 0) { RECT rect; GetWindowRect(_hwnd, &rect); MoveWindow(_hwnd, pos.x, pos.y, rect.right - rect.left, rect.bottom - rect.top, FALSE); } // init player services initServices(); loadLuaConfig(); registerKeyboardEvent(); // register event handlers auto eventDispatcher = director->getEventDispatcher(); eventDispatcher->addCustomEventListener("APP.WINDOW_CLOSE_EVENT", CC_CALLBACK_1(PlayerWin::onWindowClose, this)); eventDispatcher->addCustomEventListener("APP.WINDOW_RESIZE_EVENT", CC_CALLBACK_1(PlayerWin::onWindowResize, this)); eventDispatcher->addCustomEventListener("APP.VIEW_SCALE", CC_CALLBACK_1(PlayerWin::onWindowScale, this)); // prepare _project.dump(); auto app = Application::getInstance(); g_oldWindowProc = (WNDPROC)SetWindowLong(_hwnd, GWL_WNDPROC, (LONG)PlayerWin::windowProc); HWND hwnd = _hwnd; HWND hwndConsole = _hwndConsole; const ProjectConfig &project = _project; director->getScheduler()->schedule([hwnd, hwndConsole, project](float dt) { CC_UNUSED_PARAM(dt); ShowWindow(hwnd, SW_RESTORE); auto glview = dynamic_cast<GLViewImpl*>(Director::getInstance()->getOpenGLView()); GLFWwindow *window = glview->getWindow(); glfwShowWindow(window); }, this, 0.0f, 0, 0.001f, false, "SHOW_WINDOW_CALLBACK"); if (project.isAppMenu() && GetMenu(hwnd)) { // 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); } ShowWindow(_hwnd, SW_MINIMIZE); // startup message loop return app->run(); }