void HelloWorld::testMouseEvent() { auto listener = EventListenerMouse::create(); listener->onMouseDown = [&](Event* event){ log("onMouseDown"); }; listener->onMouseMove = [&](Event* event){ log("onMouseMove"); EventMouse* e = (EventMouse*)event; touch_pos = e->getLocation(); streak->setPosition(touch_pos); }; listener->onMouseUp = [&](Event* event){ log("onMouseUp"); }; listener->onMouseScroll = [&](Event* event){ log("onMouseScroll"); }; dispatcher->addEventListenerWithSceneGraphPriority(listener, this); }
void WorldScene::mouseClick(cocos2d::Event* event, Player* player) { CCLOG("mouse"); CCLOG("Category %x", player->getPhysicsBody()->getCategoryBitmask()); CCLOG("Collision %x", player->getPhysicsBody()->getCollisionBitmask()); if (player->getArrow() > 0) { Vec2 loc = player->getPosition(); CCLOG("%d", player->getArrow()); EventMouse* mouseEvent = dynamic_cast<EventMouse*>(event); mouseEvent->getMouseButton(); //auto pro = Sprite::create("CloseNormal.png"); auto pro = Projectile::create(); pro->setPosition(loc); this->addChild(pro, 1); //pro->setPosition(mouseEvent->getLocation().x, this->getContentSize().height-mouseEvent->getLocation().y); if (player->getMode() == 'a') { pro->setMode('a'); Vec2 offset = mouseEvent->getLocation(); offset.y = this->getContentSize().height - offset.y; offset -= player->getPosition(); offset.normalize(); auto deg = atan(offset.x / offset.y) * 180 /3.14; if (offset.y<0) { deg += 180; } CCLOG("%f", deg); auto target = offset*1000 + player->getPosition(); auto rotate = RotateBy::create(0, deg); auto shoot = MoveTo::create(2.0f, target); auto remove = RemoveSelf::create(); pro->runAction(Sequence::create(rotate, shoot, remove, nullptr)); player->setArrow(player->getArrow() - 1); } else { pro->setMode('b'); auto shoot = MoveBy::create(2.0f, Vec2(0, 500)); auto remove = RemoveSelf::create(); pro->runAction(Sequence::create(shoot, remove, nullptr)); player->setArrow(player->getArrow() - 4); } setArrow(player->getArrow()); } }
void GameScene::onMouseDown(Event* ev1) { EventMouse* ev = (EventMouse*)ev1; Vec2 loc = ev->getLocation(); int size = map.size(); /*for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { map.at(i + j).label->setColor(Color3B::BLUE); Vec2 mao = (map.at(i + j)).image->getPosition(); Vec2 origin = Vec2(mao.x - 19, mao.y - 19); Size size = Size(38.0, 38.0); Rect rect = Rect(origin, size); if (rect.containsPoint(loc)) { current = map.at(i + j); map.at(i + j).label->setColor(Color3B::RED); return; } } }*/ }
void MapLayer::onMouseMove(Event *event){ if(!mapEdit) return; EventMouse *e = (EventMouse*)event; Vec2 mousePos = e->getLocation(); mousePos.y = Director::getInstance()->getWinSize().height - mousePos.y; mousePos -= CreatureLayer::GetInstance()->getPosition(); mouseX = (int)mousePos.x/TILE_SIZE; mouseY = (int)mousePos.y/TILE_SIZE; if(mouseX < 0 || mouseX > width-1) mouseX = oldMouseX; if(mouseY < 0 || mouseY > height-1) mouseY = oldMouseY; if(oldMouseX != mouseX || oldMouseY != mouseY){ SetNavigationString(); if(isMouseDown){ if(isEraser) { Erase(mouseX, mouseY); } else{ Add(nowTile, mouseX, mouseY, 0.0f, false); if(nowTile < 10){ } else if(nowTile < 100){ obstacleCount++; } else{ objectCount++; } } } oldMouseX = mouseX; oldMouseY = mouseY; } }
bool MouseScene::init() { if (!Layer::init()) { return false; } auto listener = EventListenerMouse::create(); listener->onMouseDown = [](cocos2d::Event* event) { try { EventMouse* mouseEvent = dynamic_cast<EventMouse*>(event); mouseEvent->getMouseButton(); std::stringstream message; message << "Mouse event: Button: " << mouseEvent->getMouseButton() << "pressed at point (" << mouseEvent->getLocation().x << ", " << mouseEvent->getLocation().y << ")"; MessageBox(message.str().c_str(), "Mouse Event Details"); } catch (std::bad_cast& e) { //not sure what kind of event you passed us cocos, but it was a wrong one return; } }; listener->onMouseMove = [](cocos2d::Event* event){ // Cast Event to EventMouse for position details like above cocos2d::log("Mouse moved event"); }; listener->onMouseScroll = [](cocos2d::Event* event){ cocos2d::log("Mouse wheel scrolled"); }; listener->onMouseUp = [](cocos2d::Event* event){ cocos2d::log("Mouse button released"); }; _eventDispatcher->addEventListenerWithFixedPriority(listener, 1); return true; }
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; }