void MouseTest::onMouseScroll(Event *event) { EventMouse* e = (EventMouse*)event; std::string str = "Mouse Scroll detected, X: "; str = str + tostr(e->getScrollX()) + " Y: " + tostr(e->getScrollY()); _labelAction->setString(str.c_str()); }
void Tower::onMouseScroll(cocos2d::Event* _event) { if(_isClosing == true || _returnFromSave == true) return;// if we are pressing the close button, we dont want to build things! EventMouse* e = (EventMouse*)_event; _towerLayer->runAction(MoveBy::create(1/SCROLL_SPEED ,Vec2((-1*e->getScrollX())*SCROLL_MULTIPLIER, e->getScrollY()*SCROLL_MULTIPLIER))); }
bool EditorScene::init() { if (!Scene::init()) { return false; } auto size = Director::getInstance()->getVisibleSize(); // 3d _3dLayer = Layer::create(); this->addChild(_3dLayer, 1); auto bg = BillBoard::create("images/logo_only.png"); bg->setPosition3D({0,0,0}); // _3dLayer->addChild(bg); bg->setScale(0.5f); // cubepack CubeCenter::get()->initWithTest(); for (int i = 0; i < CubeCenter::get()->cubePack.cubes.size(); i++) { Cube cube =CubeCenter::get()->cubePack.cubes[i]; auto sp3d = Sprite3D::create("3d/jingbix.c3b"); float length_scale = 10.f; sp3d->setPosition3D(Vec3{static_cast<float>(cube.x), static_cast<float>(cube.y), static_cast<float>(cube.z)}*length_scale); sp3d->setScale(4.f); _3dLayer->addChild(sp3d); } // cube sprite auto cubeSprite = CubeSprite::create(Vec3(0,0,0), 0); cubeSprite->setPosition3D({0,0,20}); cubeSprite->setScale(2.f); _3dLayer->addChild(cubeSprite); // camera _camera = Camera::createPerspective(60, size.width/size.height, 1, 1000); _camera->setPosition3D({0, 0, 200}); _camera->lookAt({0,0,0}); _camera->setCameraFlag(CameraFlag::USER1); _3dLayer->addChild(_camera); _3dLayer->setCameraMask((unsigned short)CameraFlag::USER1); // UI layer _uiLayer = EditorUILayer::create(); this->addChild(_uiLayer, 2); // state label _stateLabel = Label::createWithTTF("state string", "fonts/consola.ttf", 14); _stateLabel->setPosition(Vec2{size.width*0.1f,size.height*0.9f}); _uiLayer->addChild(_stateLabel, 0); // keyboard _keyboardListener = EventListenerKeyboard::create(); _keyboardListener->onKeyPressed = [&](EventKeyboard::KeyCode code, Event* event) { switch (code) { case EventKeyboard::KeyCode::KEY_SPACE: CCLOG("key space, shot"); //TODO break; case EventKeyboard::KeyCode::KEY_W: case EventKeyboard::KeyCode::KEY_CAPITAL_W: CCLOG("key W down"); _zoomIn = true; break; case EventKeyboard::KeyCode::KEY_S: case EventKeyboard::KeyCode::KEY_CAPITAL_S: CCLOG("key S down"); _zoomOut = true; break; case EventKeyboard::KeyCode::KEY_A: case EventKeyboard::KeyCode::KEY_CAPITAL_A: CCLOG("key A down"); _left = true; break; case EventKeyboard::KeyCode::KEY_D: case EventKeyboard::KeyCode::KEY_CAPITAL_D: CCLOG("key D down"); _right = true; break; case EventKeyboard::KeyCode::KEY_Q: case EventKeyboard::KeyCode::KEY_CAPITAL_Q: CCLOG("key Q down"); _up = true; break; case EventKeyboard::KeyCode::KEY_E: case EventKeyboard::KeyCode::KEY_CAPITAL_E: CCLOG("key E down"); _down = true; break; case EventKeyboard::KeyCode::KEY_UP_ARROW: up(); break; case EventKeyboard::KeyCode::KEY_DOWN_ARROW: down(); break; case EventKeyboard::KeyCode::KEY_LEFT_ARROW: left(); break; case EventKeyboard::KeyCode::KEY_RIGHT_ARROW: right(); break; default: break; } }; _keyboardListener->onKeyReleased = [&](EventKeyboard::KeyCode code, Event* event) { switch (code) { case EventKeyboard::KeyCode::KEY_SPACE: CCLOG("key space, shot"); //TODO break; case EventKeyboard::KeyCode::KEY_W: case EventKeyboard::KeyCode::KEY_CAPITAL_W: CCLOG("key W up"); _zoomIn = false; break; case EventKeyboard::KeyCode::KEY_S: case EventKeyboard::KeyCode::KEY_CAPITAL_S: CCLOG("key S up"); _zoomOut = false; break; case EventKeyboard::KeyCode::KEY_A: case EventKeyboard::KeyCode::KEY_CAPITAL_A: CCLOG("key A up"); _left = false; break; case EventKeyboard::KeyCode::KEY_D: case EventKeyboard::KeyCode::KEY_CAPITAL_D: CCLOG("key D up"); _right = false; break; case EventKeyboard::KeyCode::KEY_Q: case EventKeyboard::KeyCode::KEY_CAPITAL_Q: CCLOG("key Q up"); _up = false; break; case EventKeyboard::KeyCode::KEY_E: case EventKeyboard::KeyCode::KEY_CAPITAL_E: CCLOG("key E up"); _down = false; break; // case EventKeyboard::KeyCode::KEY_UP_ARROW: // up(); // break; // case EventKeyboard::KeyCode::KEY_DOWN_ARROW: // down(); // break; // case EventKeyboard::KeyCode::KEY_LEFT_ARROW: // left(); // break; // case EventKeyboard::KeyCode::KEY_RIGHT_ARROW: // right(); // break; default: break; } }; _eventDispatcher->addEventListenerWithSceneGraphPriority(_keyboardListener, this); // mouse _mouseListener = EventListenerMouse::create(); _mouseListener->onMouseMove = [&](Event *event) { if (!_isFPS) return; EventMouse* e = (EventMouse*)event; auto now = e->getLocation(); Vec2 diff = now - _fpsAnchor; _fpsAnchor = now; _rotateY -= ROTATE_SCALE*diff.x; _rotateX -= ROTATE_SCALE*diff.y; if (_rotateX > UP_DOWN_MAX*0.5f*PI) _rotateX = UP_DOWN_MAX*0.5f*PI; if (_rotateX < -UP_DOWN_MAX*0.5f*PI) _rotateX = -UP_DOWN_MAX*0.5f*PI; }; _mouseListener->onMouseUp = [&](Event *event) { EventMouse* e = (EventMouse*)event; switch(e->getMouseButton()) { case MOUSE_BUTTON_LEFT: break; case MOUSE_BUTTON_RIGHT: _isFPS = false; break; default: break; } }; _mouseListener->onMouseDown = [&](Event *event) { EventMouse* e = (EventMouse*)event; switch(e->getMouseButton()) { case MOUSE_BUTTON_LEFT: break; case MOUSE_BUTTON_RIGHT: { _isFPS = true; _fpsAnchor = e->getLocation(); auto ro = _camera->getRotation3D(); CCLOG("%f,%f,%f", ro.x, ro.y, ro.z); break; } default: break; } }; _mouseListener->onMouseScroll = [&](Event *event) { EventMouse* e = (EventMouse*)event; CCLOG("%f, %f", e->getScrollX(), e->getScrollY()); float diff = e->getScrollY(); Vec3 dir = _camera->getRotationQuat() * Vec3{0.f, 0.f, -1.f}; dir.normalize(); _camera->setPosition3D(_camera->getPosition3D() + SCALL_MOVE_SCALE * -diff * dir); }; _eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener, this); scheduleUpdate(); return true; }
bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = CCQGLView::getInstance(); //glview->setBgColor(Color4B(50, 50, 50, 255)); director->setOpenGLView(glview); _mainWindow.setGLView(glview->getGLWidget()); _mainWindow.initActionListWidget(); _mainWindow.show(); // turn on display FPS director->setDisplayStats(false); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object auto scene = Scene::create(); scene->setAnchorPoint(Vec2::ANCHOR_MIDDLE); scene->setContentSize(Director::getInstance()->getVisibleSize()); //scene->setPosition(Director::getInstance()->getVisibleSize() / 2); QString path; QDir dir; path = dir.currentPath(); FileUtils::getInstance()->addSearchPath(path.toStdString() + "/Resources"); FileUtils::getInstance()->addSearchPath("D://Resources"); auto keyboardEvent = EventListenerKeyboard::create(); keyboardEvent->onKeyPressed = [=](EventKeyboard::KeyCode code, Event*event) { if (code == EventKeyboard::KeyCode::KEY_CTRL) { m_dragMode = true; } }; keyboardEvent->onKeyReleased = [=](EventKeyboard::KeyCode code, Event*event) { if (code == EventKeyboard::KeyCode::KEY_CTRL) { m_dragMode = false; } }; Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(keyboardEvent, -1); auto scrollEvent = EventListenerMouse::create(); scrollEvent->onMouseScroll = [=](Event* event) { EventMouse* e = (EventMouse*)event; int scrollX = e->getScrollX(); int scrollY = e->getScrollY(); scene->setScale(scene->getScale() + (float)scrollY / 100.0f); Size currentSize = scene->getContentSize() * scene->getScale(); // scene->setScale(); }; auto eventListener = EventListenerTouchOneByOne::create(); eventListener->onTouchBegan = [=](Touch*touch, Event*event)->bool{ if (!m_dragMode) return false; return true; }; eventListener->onTouchMoved = [=](Touch*touch, Event*event) { scene->setPosition(scene->getPosition() + touch->getDelta()); }; eventListener->setSwallowTouches(true); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(eventListener, -3); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(scrollEvent, -2); // run director->runWithScene(scene); CCRect rect(0, 0, scene->getContentSize().width, scene->getContentSize().height); CCSprite* grid = CCSprite::create("grid1_16x16_new_aaa.png"); grid->setTextureRect(rect); Texture2D::TexParams params = { GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT }; grid->getTexture()->setTexParameters(¶ms); grid->setAnchorPoint(Vec2::ZERO); grid->setPosition(Vec2::ZERO); scene->addChild(grid,0); // return true; }