bool WindowProtocol::init(){ bool ret = false; //super init if(!Layer::init()) return ret; //Put blocker Layer* blocker = LayerColor::create(Color4B(0, 0, 0, 255*0.5), VisibleRect::getVisibleRect().size.width, VisibleRect::getVisibleRect().size.height); blocker->setAnchorPoint(Vec2::ZERO); blocker->setPosition(Vec2(VisibleRect::getVisibleRect().origin.x, VisibleRect::getVisibleRect().origin.y)); //Base window _baseWindow = Scale9Sprite::createWithSpriteFrameName("WindowBase.psd",BASE_WINDOW_INSET_RECT); _baseWindow->setContentSize(Size(850,490-10-10)); _baseWindow->setAnchorPoint(Vec2(0.0, 1.0)); _baseWindow->setPosition(Vec2(55, VisibleRect::getVisibleRect().size.height-60)); //Put base window _baseLayout = Layout::create(); CCASSERT(_baseLayout!=nullptr, "_baseLayout cannot be null."); _baseLayout->setLayoutType(Layout::Type::VERTICAL); _baseLayout->setContentSize(_baseWindow->getCapInsets().size); _baseLayout->setAnchorPoint(Vec2(0.0, 1.0)); _baseLayout->setPosition(Vec2(2,_baseWindow->getContentSize().height-2)); //PutLayout on sprite _baseWindow->addChild(_baseLayout,0); //---Title--- //Layout _titleLayout = Layout::create(); CCASSERT(_titleLayout!=nullptr, "_titleLayer cannot be null."); //_titleLayout->setAnchorPoint(Vec2(0.0, 1.0)); _titleLayout->setLayoutType(Layout::Type::HORIZONTAL); _titleLayout->setContentSize(Size(850-2*2, 55)); _titleLayout->setPosition(Vec2(_baseLayout->getContentSize().width/2, _baseLayout->getContentSize().height/2)); //set baseTitle as titleLayout background //No longer need title base image... _titleLayout->setBackGroundColorType(Layout::BackGroundColorType::SOLID); _titleLayout->setBackGroundColor(Color3B(200, 200, 169)); //Close Button LinearLayoutParameter* lpCloseButton = LinearLayoutParameter::create(); _closeButton = Button::create("MainUI_Windows_Close_Normal.png","MainUI_Windows_Close_Pressed.png","",TextureResType::PLIST); CCASSERT(_closeButton!=nullptr, "_closeButton cannot be null."); _closeButton->setPosition(Vec2(_titleLayout->getContentSize().width/2, _titleLayout->getContentSize().height/2)); lpCloseButton->setGravity(LinearLayoutParameter::LinearGravity::LEFT); lpCloseButton->setMargin(Margin(0, 0, 0, 0)); _closeButton->setLayoutParameter(lpCloseButton); if(_closeCallback!=nullptr) _closeButton->addTouchEventListener(_closeCallback); //Title text LinearLayoutParameter* lpTitleText = LinearLayoutParameter::create(); _titleText = Text::create(_titleString, "fonts/Apple_LiGothic_Medium.ttf", computeFontSize(42)); CCASSERT(_titleText!=nullptr, "_titletext cannot be null."); _titleText->setColor(Color3B(59,134,134)); _titleText->setPosition(Vec2(_titleLayout->getContentSize().width/2, _titleLayout->getContentSize().height/2)); lpTitleText->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL); lpTitleText->setMargin(Margin(_titleLayout->getContentSize().width/2-_titleText->getContentSize().width/2-_closeButton->getContentSize().width, 0, 0, 0)); _titleText->setLayoutParameter(lpTitleText); //Add component to title layout _titleLayout->addChild(_closeButton,1); _titleLayout->addChild(_titleText,2); //---add to base layout--- _baseLayout->addChild(_titleLayout); //setBlockerTouchDispatcher auto touchBlocker = EventListenerTouchOneByOne::create(); touchBlocker->setSwallowTouches(true); //lambda decide later touchBlocker->onTouchBegan= [](Touch* touch,Event* e){ return true; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(touchBlocker, blocker); //addChild addChild(blocker,0); addChild(_baseWindow,1); ret = true; return ret; }
void ListView::remedyLayoutParameter(Widget *item) { if (!item) { return; } switch (_direction) { case SCROLLVIEW_DIR_VERTICAL: { LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); if (!llp) { LinearLayoutParameter* defaultLp = LinearLayoutParameter::create(); switch (_gravity) { case LISTVIEW_GRAVITY_LEFT: defaultLp->setGravity(LINEAR_GRAVITY_LEFT); break; case LISTVIEW_GRAVITY_RIGHT: defaultLp->setGravity(LINEAR_GRAVITY_RIGHT); break; case LISTVIEW_GRAVITY_CENTER_HORIZONTAL: defaultLp->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL); break; default: break; } if (getIndex(item) == 0) { defaultLp->setMargin(MarginZero); } else { defaultLp->setMargin(Margin(0.0f, _itemsMargin, 0.0f, 0.0f)); } item->setLayoutParameter(defaultLp); } else { if (getIndex(item) == 0) { llp->setMargin(MarginZero); } else { llp->setMargin(Margin(0.0f, _itemsMargin, 0.0f, 0.0f)); } switch (_gravity) { case LISTVIEW_GRAVITY_LEFT: llp->setGravity(LINEAR_GRAVITY_LEFT); break; case LISTVIEW_GRAVITY_RIGHT: llp->setGravity(LINEAR_GRAVITY_RIGHT); break; case LISTVIEW_GRAVITY_CENTER_HORIZONTAL: llp->setGravity(LINEAR_GRAVITY_CENTER_HORIZONTAL); break; default: break; } } break; } case SCROLLVIEW_DIR_HORIZONTAL: { LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter(LAYOUT_PARAMETER_LINEAR)); if (!llp) { LinearLayoutParameter* defaultLp = LinearLayoutParameter::create(); switch (_gravity) { case LISTVIEW_GRAVITY_TOP: defaultLp->setGravity(LINEAR_GRAVITY_TOP); break; case LISTVIEW_GRAVITY_BOTTOM: defaultLp->setGravity(LINEAR_GRAVITY_BOTTOM); break; case LISTVIEW_GRAVITY_CENTER_VERTICAL: defaultLp->setGravity(LINEAR_GRAVITY_CENTER_VERTICAL); break; default: break; } if (getIndex(item) == 0) { defaultLp->setMargin(MarginZero); } else { defaultLp->setMargin(Margin(_itemsMargin, 0.0f, 0.0f, 0.0f)); } item->setLayoutParameter(defaultLp); } else { if (getIndex(item) == 0) { llp->setMargin(MarginZero); } else { llp->setMargin(Margin(_itemsMargin, 0.0f, 0.0f, 0.0f)); } switch (_gravity) { case LISTVIEW_GRAVITY_TOP: llp->setGravity(LINEAR_GRAVITY_TOP); break; case LISTVIEW_GRAVITY_BOTTOM: llp->setGravity(LINEAR_GRAVITY_BOTTOM); break; case LISTVIEW_GRAVITY_CENTER_VERTICAL: llp->setGravity(LINEAR_GRAVITY_CENTER_VERTICAL); break; default: break; } } break; } default: break; } }
void ListView::remedyLayoutParameter(Widget *item) { if (!item) { return; } switch (_direction) { case Direction::VERTICAL: { LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter()); if (!llp) { LinearLayoutParameter* defaultLp = LinearLayoutParameter::create(); switch (_gravity) { case Gravity::LEFT: defaultLp->setGravity(LinearLayoutParameter::LinearGravity::LEFT); break; case Gravity::RIGHT: defaultLp->setGravity(LinearLayoutParameter::LinearGravity::RIGHT); break; case Gravity::CENTER_HORIZONTAL: defaultLp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL); break; default: break; } if (getIndex(item) == 0) { defaultLp->setMargin(Margin::ZERO); } else { defaultLp->setMargin(Margin(0.0f, _itemsMargin, 0.0f, 0.0f)); } item->setLayoutParameter(defaultLp); } else { if (getIndex(item) == 0) { llp->setMargin(Margin::ZERO); } else { llp->setMargin(Margin(0.0f, _itemsMargin, 0.0f, 0.0f)); } switch (_gravity) { case Gravity::LEFT: llp->setGravity(LinearLayoutParameter::LinearGravity::LEFT); break; case Gravity::RIGHT: llp->setGravity(LinearLayoutParameter::LinearGravity::RIGHT); break; case Gravity::CENTER_HORIZONTAL: llp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL); break; default: break; } } break; } case Direction::HORIZONTAL: { LinearLayoutParameter* llp = (LinearLayoutParameter*)(item->getLayoutParameter()); if (!llp) { LinearLayoutParameter* defaultLp = LinearLayoutParameter::create(); switch (_gravity) { case Gravity::TOP: defaultLp->setGravity(LinearLayoutParameter::LinearGravity::TOP); break; case Gravity::BOTTOM: defaultLp->setGravity(LinearLayoutParameter::LinearGravity::BOTTOM); break; case Gravity::CENTER_VERTICAL: defaultLp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL); break; default: break; } if (getIndex(item) == 0) { defaultLp->setMargin(Margin::ZERO); } else { defaultLp->setMargin(Margin(_itemsMargin, 0.0f, 0.0f, 0.0f)); } item->setLayoutParameter(defaultLp); } else { if (getIndex(item) == 0) { llp->setMargin(Margin::ZERO); } else { llp->setMargin(Margin(_itemsMargin, 0.0f, 0.0f, 0.0f)); } switch (_gravity) { case Gravity::TOP: llp->setGravity(LinearLayoutParameter::LinearGravity::TOP); break; case Gravity::BOTTOM: llp->setGravity(LinearLayoutParameter::LinearGravity::BOTTOM); break; case Gravity::CENTER_VERTICAL: llp->setGravity(LinearLayoutParameter::LinearGravity::CENTER_VERTICAL); break; default: break; } } break; } default: break; } }