void AdvScrollArea::ensureVisibleY(int y) { int ch = getScrollAreaHeight(); int cy = getScrollY(); scrollV->setValue(y); if (y < cy) {setScrollX(y - ch);} if (y > cy + ch) {setScrollY(y);} }
bool MGMap::mouseScrollingUpdate(int x, int y) { if(m_MouseScrollingOngoing) { int setY = getScrollY() + m_MouseScrollingYClick - y; int setX = getScrollX() + m_MouseScrollingXClick - x; if(setY > getTopEdge()) { setY = getTopEdge(); } else if(setY < getWindowHeight() - getHeight() * getTileHeight() - getBottomEdge()) { setY = getWindowHeight() - getHeight() * getTileHeight() - getBottomEdge(); } if(setX > getLeftEdge()) { setX = getLeftEdge(); } else if(setX < getWindowWidth() - getWidth() * getTileWidth() - getRightEdge()) { setX = getWindowWidth() - getWidth() * getTileWidth() - getRightEdge(); } setScrollOffset(setX, setY); } return m_MouseScrollingOngoing; }
int MGMap::getTileIndex(int clickX, int clickY) { MGFLOG_INFO("MGMap::getTileIndex(" << clickX << ", " << clickY << ")"); if( clickX > getLeftEdge() && clickX < (getWindowWidth() - getRightEdge()) && clickY > getTopEdge() && clickY < (getWindowHeight() - getBottomEdge())) { int x = (clickX - getScrollX()) / getTileWidth(); int y = (clickY - getScrollY()) / getTileHeight(); if(x < getWidth() && y < getHeight()) { return y * getWidth() + x; } } return -1; // Click outside of map }
void onMouseScroll(Event *event) { auto mouseEvent = static_cast<EventMouse*>(event); float moveY = mouseEvent->getScrollY() * 20; auto minOffset = this->minContainerOffset(); auto maxOffset = this->maxContainerOffset(); auto offset = this->getContentOffset(); offset.y += moveY; if (offset.y < minOffset.y) { offset.y = minOffset.y; } else if (offset.y > maxOffset.y) { offset.y = maxOffset.y; } this->setContentOffset(offset); }
void TestController::onMouseScroll(Event *event) { auto mouseEvent = static_cast<EventMouse*>(event); float nMoveY = mouseEvent->getScrollY() * 6; auto curPos = _itemMenu->getPosition(); auto nextPos = Vec2(curPos.x, curPos.y + nMoveY); if (nextPos.y < 0.0f) { _itemMenu->setPosition(Vec2::ZERO); return; } if (nextPos.y > ((g_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height)) { _itemMenu->setPosition(Vec2(0, ((g_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height))); return; } _itemMenu->setPosition(nextPos); s_tCurPos = nextPos; }
bool SelectMissionScene::init() { if( !Layer::init()) return false; GameManager::getInstance()->loadMissionData(); Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); Size winSize = Director::getInstance()->getWinSize(); //set background image // Sprite *bg = Sprite::create("choose_bg.png"); // bg->setAnchorPoint(Vec2(0.5, 0.5)); // bg->setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2 ); Sprite *bgSprite = Sprite::create("choose_bg.png"); bgSprite->setAnchorPoint(Vec2::ANCHOR_MIDDLE); bgSprite->setPosition(winSize.width / 2, winSize.height / 2); float scale_x = winSize.width / bgSprite->getContentSize().width; float scale_y = winSize.height / bgSprite->getContentSize().height; bgSprite->setScaleX(scale_x); bgSprite->setScaleY(scale_y); // if(scale_x > scale_y) // bgSprite->setScaleX(scale_x); // else // bgSprite->setScale(scale_y); #ifdef USE_MENU_ITEM //set a sub layer to contain menu Layer *menuLayer = Layer::create(); menuLayer->setAnchorPoint(Vec2(menuLayer->getContentSize().width / 2, menuLayer->getContentSize().height / 2)); menuLayer->setContentSize(Size(visibleSize.width - 120, visibleSize.height - 200)); menuLayer->setPosition(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y); //set menu list _itemMenu = Menu::create(); for( int i = 0; i < MISSION_COUNT; ++i ) { MissionMenuItem *menuItem = MissionMenuItem::create("ic_launcher.png", g_mission_items[i]); menuItem->setPosition(origin.x + 60, visibleSize.height + origin.y - 100 - (i+1) * 150); _itemMenu->addChild(menuItem); } _itemMenu->setPosition(Vec2::ZERO - menuLayer->getAnchorPoint()); _itemMenu->setContentSize(Size(visibleSize.width - 120, (MISSION_COUNT + 1) * 150)); // Register Touch Event auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(SelectMissionScene::onTouchBeganCallback, this); listener->onTouchMoved = CC_CALLBACK_2(SelectMissionScene::onTouchMovedCallback, this); this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto mouseListener = EventListenerMouse::create(); mouseListener->onMouseScroll = [&](Event *event){ auto mouseEvent = static_cast<EventMouse*>(event); float nMoveY = mouseEvent->getScrollY() * 10; auto curPos = _itemMenu->getPosition(); auto nextPos = Vec2(curPos.x, curPos.y + nMoveY); _itemMenu->setPosition(nextPos); }; this->_eventDispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this); menuLayer->addChild(_itemMenu); this->addChild(menuLayer, 2); #endif // !USE_TABLE_VIEW #ifdef USE_LIST_VIEW //set ListView cocos2d::ui::ListView *listView = ListView::create(); for(int i = 0; i < MISSION_COUNT; ++i) { auto item = MissionInfoItem::create(g_mission_items[i]); listView->pushBackCustomItem(item); } listView->setContentSize(Size(520, 700)); listView->setItemsMargin(1); listView->setPosition(Vec2(60, 130)); // Register Touch Event auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = [&](Touch *touch, Event *event){ _beginPos = touch->getLocation(); return true; }; listener->onTouchEnded = [&](Touch *touch, Event *event){ auto curPos = touch->getLocation(); if (curPos == _beginPos) { int idx = listView->getCurSelectedIndex(); Director::getInstance()->replaceScene(TransitionProgress::create(1.0f, Scene::create())); } }; this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, listView); this->addChild(listView); #endif // USE_LIST_VIEW //add slider bar slider = ControlSlider::create("extensions/sliderBg.png","extensions/sliderBg.png" , "extensions/rect.png"); slider->setPosition(visibleSize.width - 50, visibleSize.height / 2 ); //slider->setContentSize(Size(slider->getContentSize().width, 700)); slider->setScaleX(700 / slider->getContentSize().width); slider->setMinimumValue(0.0f); slider->setMaximumValue(100.0f); slider->setRotation(90); slider->setTag(180); slider->setEnabled(false); slider->addTargetWithActionForControlEvents(this, cccontrol_selector(SelectMissionScene::slideCallback), Control::EventType::VALUE_CHANGED); table = TableView::create(this, Size(520, 700)); table->setDelegate(this); table->setDirection(cocos2d::extension::ScrollView::Direction::VERTICAL); table->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN); //table->reloadData(); table->setTag(500); // table->setAnchorPoint(Vec2::ZERO); // table->setPosition(origin.x + 60, origin.y + 140); table->setAnchorPoint(Vec2(0.5, 0.5)); table->ignoreAnchorPointForPosition(false); table->setPosition(origin.x + visibleSize.width / 2 , origin.y + visibleSize.height / 2); MenuItemImage *returnItem = MenuItemImage::create("gohome.png", "gohome.png", [](Ref *pSender){ Director::getInstance()->popScene(); }); returnItem->setPosition(visibleSize.width / 2, origin.y + 50); Menu *menu = Menu::create(); menu->setPosition(Vec2::ZERO); menu->addChild(returnItem); this->addChild(slider, 15); this->addChild(table, 10); this->addChild(menu, 10); this->addChild(bgSprite); EventListenerKeyboard *keyListener = EventListenerKeyboard::create(); keyListener->onKeyReleased = [&](EventKeyboard::KeyCode keyCode, Event* event) { log("key Press!"); if(keyCode == EventKeyboard::KeyCode::KEY_BACK || keyCode == EventKeyboard::KeyCode::KEY_BACKSPACE ) { Director::getInstance()->popToRootScene(); } }; this->_eventDispatcher->addEventListenerWithSceneGraphPriority(keyListener, this); return true; }