bool TextField::hitTest(const Vec2 &pt, const Camera* camera, Vec3 *p) const { if (false == _useTouchArea) { return Widget::hitTest(pt, camera, nullptr); } auto size = getContentSize(); auto anch = getAnchorPoint(); Rect rect((size.width - _touchWidth) * anch.x, (size.height - _touchHeight) * anch.y, _touchWidth, _touchHeight); return isScreenPointInRect(pt, camera, getWorldToNodeTransform(), rect, nullptr); }
void TextFieldTTF::setCursorFromPoint(const Vec2 &point, const Camera* camera) { if (_cursorEnabled) { // Reset Label, no cursor bool oldIsAttachWithIME = _isAttachWithIME; _isAttachWithIME = false; updateCursorDisplayText(); Rect rect; rect.size = getContentSize(); if (isScreenPointInRect(point, camera, getWorldToNodeTransform(), rect, nullptr)) { int latterPosition = 0; for (; latterPosition < _lengthOfString; ++latterPosition) { if (_lettersInfo[latterPosition].valid) { auto sprite = getLetter(latterPosition); rect.size = sprite->getContentSize(); if (isScreenPointInRect(point, camera, sprite->getWorldToNodeTransform(), rect, nullptr)) { setCursorPosition(latterPosition); break; } } } if (latterPosition == _lengthOfString) { setCursorPosition(latterPosition); } } // Set cursor _isAttachWithIME = oldIsAttachWithIME; updateCursorDisplayText(); } }
cocos2d::MenuItem* ButtonControlLayer::getItemForTouch(cocos2d::Touch *touch, const cocos2d::Camera *camera) { cocos2d::Vec2 touchLocation = touch->getLocation(); for (const auto &item: _children) { auto child = dynamic_cast<cocos2d::MenuItem*>(item); if (child == nullptr || !child->isVisible() || !child->isEnabled()) { continue; } cocos2d::Rect rect; rect.size = child->getContentSize(); if (isScreenPointInRect(touchLocation, camera, child->getWorldToNodeTransform(), rect, nullptr)) { return child; } } return nullptr; }
bool Widget::hitTest(const Vec2 &pt, const Camera* camera, Vec3 *p) const { Rect rect; rect.size = getContentSize(); return isScreenPointInRect(pt, camera, getWorldToNodeTransform(), rect, p); }
bool MapMakerScene::init() { if (!Layer::init()) { return false; } m_lastSelectGroup = nullptr; Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto s = Director::getInstance()->getWinSize(); //input map name auto pTextField = TextFieldTTF::textFieldWithPlaceHolder(std::string(LocalizedCStringByKey("input_map_name")), "fonts/arial.ttf", 48); addChild(pTextField,0,"tbMapName"); pTextField->setPosition(Vec2(s.width / 2,s.height - 250)); //输入事件 auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [pTextField](Touch* touch, Event*) { auto beginPos = touch->getLocation(); Rect rect; rect.size = pTextField->getContentSize(); auto clicked = isScreenPointInRect(beginPos, Camera::getVisitingCamera(), pTextField->getWorldToNodeTransform(), rect, nullptr); if (clicked) { return true; } pTextField->detachWithIME(); return false; }; listener->onTouchEnded = [pTextField](Touch* touch, Event* event) { auto endPos = touch->getLocation(); Rect rect; rect.size = pTextField->getContentSize(); auto clicked = isScreenPointInRect(endPos, Camera::getVisitingCamera(), pTextField->getWorldToNodeTransform(), rect, nullptr); if (clicked) { pTextField->attachWithIME(); } }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //返回主菜单 auto returnToMainMenuItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(MapMakerScene::returnToMainMenuCallback, this) ); returnToMainMenuItem->setPosition(Vec2(origin.x + visibleSize.width - returnToMainMenuItem->getContentSize().width / 2, origin.y + returnToMainMenuItem->getContentSize().height / 2)); auto menu = Menu::create(returnToMainMenuItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); //baseplate auto baseplateLayer = SquareBaseplateLayer::create(); baseplateLayer->createEmptyMap(BaseSize(15,15)); baseplateLayer->drawBasesplate(Vec2(32,32)); baseplateLayer->setPosition(Vec2(100,200)); addChild(baseplateLayer); //菜单,创建group auto menuItemCreateGroup = MenuItemFont::create(LocalizedCStringByKey("create_group")); menuItemCreateGroup->setCallback( [=](Ref*) { auto squareGroup = SquareGroupMapMaker::create(); squareGroup->setPosition(Vec2(100,100)); squareGroup->SetSquareGroup(Vec2(32,32),SquareGroup::SQUAREGROUP_TYPE::ST_Z,Square::SQUARE_COLOR::SC_GREEN); squareGroup->DrawGroup(); squareGroup->setSelectedListener( [=](SquareGroup* sg) { m_lastSelectGroup = sg; }); this->addChild(squareGroup); } ); //菜单,删除group auto menuItemDeleteSelectedGroup = MenuItemFont::create(LocalizedCStringByKey("delete_group")); menuItemDeleteSelectedGroup->setCallback( [=](Ref*) { for(Node* node:this->getChildren()) { SquareGroupMapMaker* sg = dynamic_cast<SquareGroupMapMaker*>(node); if(sg != nullptr) { if(sg->getGroupState() == SGS_SELECTED) { assert(m_lastSelectGroup == sg); m_lastSelectGroup = nullptr; this->removeChild(node); } } } } ); //菜单,保存地图 auto menuItemSaveMap = MenuItemFont::create(LocalizedCStringByKey("save_map")); menuItemSaveMap->setCallback( [=](Ref*) { saveMapToFile(); } ); //创建菜单组 auto operationMenu = Menu::create(menuItemCreateGroup,menuItemDeleteSelectedGroup,menuItemSaveMap, NULL); operationMenu->alignItemsVerticallyWithPadding(20); addChild(operationMenu); operationMenu->setPosition(Vec2(s.width / 2, s.height - 100)); //产生本地图的guid m_guid = std::string(GameUilityTools::CreateGuidString()); //添加选择颜色的层 auto colorLayer = SelectColorLayer::create(); colorLayer->setPosition(Vec2(0, 0)); colorLayer->setColorChangeListener( [=](Square::SQUARE_COLOR color){ //todo xuhua if (m_lastSelectGroup) { m_lastSelectGroup->setSquareGroupColor(color); m_lastSelectGroup->DrawGroup(); } } ); addChild(colorLayer, 0); return true; }