void UIConveyorLayer::createConveyorList() { auto node = this->getContentInfoNode(); auto Nodesize = Size(120,node->getContentSize().height); ListView* listView = ListView::create(); // set list view ex direction listView->setDirection(SCROLLVIEW_DIR_VERTICAL); listView->setTouchEnabled(true); listView->setBounceEnabled(true); //listView->setBackGroundImage("ui/1269.0.png"); //listView->setBackGroundImageScale9Enabled(true); listView->setSize(Nodesize*0.98f); listView->setPosition(Point(0,5)); node->addChild(listView); auto map = BuildingFunc::getInstance()->getAllSoldier(); int MAXNum= 0; auto it = map.begin(); for (it;it != map.end();it++) { auto itme = it->second; if(itme->soldierid /100 == CONVEYORID) { m_iCurState = 1; MAXNum = itme->number; break; } } for (int i = 0; i < MAXNum; i++) { auto btn = UITouchButton::create("btn/btn_hero_head01.png"); btn->setIsUsedForScroller(true); Layout* custom_item = Layout::create(); custom_item->setSize(btn->getContentSize()*1.1f); btn->setPosition(Point(custom_item->getSize().width / 2.0f, custom_item->getSize().height / 2.0f)); custom_item->addChild(btn); listView->pushBackCustomItem(custom_item); ConveyorConfig::CreateItem(i+1); //添加数据 } }
bool UIListViewTest_Horizontal::init() { if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); _displayValueLabel = Text::create("Move by horizontal direction", "fonts/Marker Felt.ttf", 32); _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f)); _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f)); _uiLayer->addChild(_displayValueLabel); Text* alert = Text::create("ListView horizontal", "fonts/Marker Felt.ttf", 30); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f)); _uiLayer->addChild(alert); Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81)); Layout* background = static_cast<Layout*>(root->getChildByName("background_Panel")); Size backgroundSize = background->getContentSize(); // create list view ex data for (int i = 0; i < 20; ++i) { std::string ccstr = StringUtils::format("listview_item_%d", i); _array.push_back(ccstr); } // Create the list view ex ListView* listView = ListView::create(); // set list view ex direction listView->setDirection(ui::ScrollView::Direction::HORIZONTAL); listView->setTouchEnabled(true); listView->setBounceEnabled(true); listView->setBackGroundImage("cocosui/green_edit.png"); listView->setBackGroundImageScale9Enabled(true); listView->setContentSize(Size(240, 130)); listView->setPosition(Vec2((widgetSize.width - backgroundSize.width) / 2.0f + (backgroundSize.width - listView->getContentSize().width) / 2.0f, (widgetSize.height - backgroundSize.height) / 2.0f + (backgroundSize.height - listView->getContentSize().height) / 2.0f)); listView->addEventListener((ui::ListView::ccListViewCallback)CC_CALLBACK_2(UIListViewTest_Horizontal::selectedItemEvent, this)); _uiLayer->addChild(listView); // create model Button* default_button = Button::create("cocosui/backtotoppressed.png", "cocosui/backtotopnormal.png"); default_button->setName("Title Button"); Layout *default_item = Layout::create(); default_item->setTouchEnabled(true); default_item->setContentSize(default_button->getContentSize()); default_button->setPosition(Vec2(default_item->getContentSize().width / 2.0f, default_item->getContentSize().height / 2.0f)); default_item->addChild(default_button); // set model listView->setItemModel(default_item); // add default item ssize_t count = _array.size(); for (int i = 0; i < count / 4; ++i) { listView->pushBackDefaultItem(); } // insert default item for (int i = 0; i < count / 4; ++i) { listView->insertDefaultItem(0); } // add custom item for (int i = 0; i < count / 4; ++i) { Button* custom_button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); custom_button->setName("Title Button"); custom_button->setScale9Enabled(true); custom_button->setContentSize(default_button->getContentSize()); Layout* custom_item = Layout::create(); custom_item->setContentSize(custom_button->getContentSize()); custom_button->setPosition(Vec2(custom_item->getContentSize().width / 2.0f, custom_item->getContentSize().height / 2.0f)); custom_item->addChild(custom_button); listView->pushBackCustomItem(custom_item); } // insert custom item Vector<Widget*>& items = listView->getItems(); ssize_t items_count = items.size(); for (int i = 0; i < count / 4; ++i) { Button* custom_button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); custom_button->setName("Title Button"); custom_button->setScale9Enabled(true); custom_button->setContentSize(default_button->getContentSize()); Layout* custom_item = Layout::create(); custom_item->setContentSize(custom_button->getContentSize()); custom_button->setPosition(Vec2(custom_item->getContentSize().width / 2.0f, custom_item->getContentSize().height / 2.0f)); custom_item->addChild(custom_button); listView->insertCustomItem(custom_item, items_count); } // set item data items_count = items.size(); for (int i = 0; i < items_count; ++i) { Widget *item = listView->getItem(i); Button *button = static_cast<Button*>(item->getChildByName("Title Button")); ssize_t index = listView->getIndex(item); button->setTitleText(_array[index]); } // remove last item listView->removeLastItem(); // remove item by index items_count = items.size(); listView->removeItem(items_count - 1); // set all items layout gravity listView->setGravity(ListView::Gravity::CENTER_VERTICAL); // set items margin listView->setItemsMargin(2); return true; } return false; }
void MainScene2::refresh_listview() { //listview ListView * answerlist = static_cast<ListView*>(rootNode->getChildByName("ListView")); answerlist->removeAllChildren(); if (searchState == "N")//当在查看某个用户的回答时,不显示问题 { //问题详情放入listview Node * question_and_contentNode = CSLoader::createNode("question_and_content.csb"); question_and_contentNode->setTag(0); //关注提问者按钮 Button * btn_focus2 = static_cast<Button*>(question_and_contentNode->getChildByName("Button_focus")); if (AppDelegate::this_user_is_focus == "1")//提问用户已经是“我”的关注对象了 { btn_focus2->setBright(false); btn_focus2->setEnabled(false); } else { btn_focus2->addClickEventListener(CC_CALLBACK_1(MainScene2::Add_focus_ClickCallback, this, -1)); } //提问者名字显示 Text * ask_user_name = static_cast<Text *>(question_and_contentNode->getChildByName("Text_user")); ask_user_name->setText(AppDelegate::this_question_username); //问题标题显示 Text * question_title = static_cast<Text *>(question_and_contentNode->getChildByName("Text_question_title")); question_title->setText(AppDelegate::this_question_title); //问题内容显示 Text * question_content = static_cast<Text *>(question_and_contentNode->getChildByName("Text_question_content")); question_content->setText(this_question_content); Widget * question_and_contentWidget = Widget::create(); question_and_contentWidget->setSize(Size(620, 380)); question_and_contentWidget->addChild(question_and_contentNode); answerlist->pushBackCustomItem(question_and_contentWidget); } //回答列表放入listview for (int i = 0; i < this_page_answer_num; i++) { Node * answerNode = CSLoader::createNode("answer.csb"); answerNode->setTag(0); Text * userText = static_cast<Text*>(answerNode->getChildByName("Text_user")); userText->setText(name[i]); //回答者的关注按钮 Button * focusBtn = static_cast<Button*>(answerNode->getChildByName("Button_focus")); if (is_focus[i]=="1")//回答这个问题的用户已经是“我”的关注对象了 { focusBtn->setBright(false); focusBtn->setEnabled(false); } else { focusBtn->addClickEventListener(CC_CALLBACK_1(MainScene2::Add_focus_ClickCallback, this, i)); } //回答的内容 Text * answerText = static_cast<Text*>(answerNode->getChildByName("Text_answer")); answerText->setText(A_content[i]); Text * timeText = static_cast<Text*>(answerNode->getChildByName("Text_time"));//时间 timeText->setText(A_createtime[i]); //赞的次数显示 Text * praiseText = static_cast<Text*>(answerNode->getChildByName("Text_praise_num"));; praiseText->setText(A_praisenum[i]); //点赞按钮 Button * praiseBtn = static_cast<Button*>(answerNode->getChildByName("Button_praise")); if (is_praise[i]=="1")//我已经赞过 { praiseBtn->setBright(false); praiseBtn->setEnabled(false); } else { praiseBtn->addClickEventListener(CC_CALLBACK_1(MainScene2::Praise_button_ClickCallback, this, i)); } Widget * answerWidget = Widget::create(); answerWidget->setSize(Size(620, 170)); answerWidget->addChild(answerNode);//Node放入Widget中 answerlist->pushBackCustomItem(answerWidget);//Widget放入listview } if (searchState == "N")//当查看某个用户的答案时,不能回答 { //回答问题模块 Node * my_answer_Node = CSLoader::createNode("my_answer.csb"); my_answer_Node->setTag(0); Button * btn_up_answer = static_cast<Button*>(my_answer_Node->getChildByName("Button_up_answer")); btn_up_answer->addClickEventListener(CC_CALLBACK_1(MainScene2::Up_answer_ClickCallback, this)); TextField * answer_content_TF = static_cast<TextField *>(my_answer_Node->getChildByName("TextField_answer")); answer_content_TF->ignoreContentAdaptWithSize(false);//设置换行 Widget * my_answer_Widget = Widget::create(); my_answer_Widget->setSize(Size(620, 290)); my_answer_Widget->addChild(my_answer_Node);//Node放入Widget中 answerlist->pushBackCustomItem(my_answer_Widget);//Widget放入listview } }
bool UIListViewTest_Vertical::init() { if (UIScene::init()) { Size widgetSize = _widget->getSize(); _displayValueLabel = Text::create("Move by vertical direction", "fonts/Marker Felt.ttf", 32); _displayValueLabel->setAnchorPoint(Point(0.5f, -1.0f)); _displayValueLabel->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f)); _uiLayer->addChild(_displayValueLabel); Text* alert = Text::create("ListView vertical", "fonts/Marker Felt.ttf", 30); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 3.075f)); _uiLayer->addChild(alert); Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81)); Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel")); Size backgroundSize = background->getContentSize(); // create list view ex data _array = Array::create(); CC_SAFE_RETAIN(_array); for (int i = 0; i < 20; ++i) { __String* ccstr = __String::createWithFormat("listview_item_%d", i); _array->addObject(ccstr); } // Create the list view ex ListView* listView = ListView::create(); // set list view ex direction listView->setDirection(SCROLLVIEW_DIR_VERTICAL); listView->setTouchEnabled(true); listView->setBounceEnabled(true); listView->setBackGroundImage("cocosui/green_edit.png"); listView->setBackGroundImageScale9Enabled(true); listView->setSize(Size(240, 130)); listView->setPosition(Point((widgetSize.width - backgroundSize.width) / 2.0f + (backgroundSize.width - listView->getSize().width) / 2.0f, (widgetSize.height - backgroundSize.height) / 2.0f + (backgroundSize.height - listView->getSize().height) / 2.0f)); listView->addEventListenerListView(this, listvieweventselector(UIListViewTest_Vertical::selectedItemEvent)); _uiLayer->addChild(listView); // create model Button* default_button = Button::create("cocosui/backtotoppressed.png", "cocosui/backtotopnormal.png"); default_button->setName("Title Button"); Layout* default_item = Layout::create(); default_item->setTouchEnabled(true); default_item->setSize(default_button->getSize()); default_button->setPosition(Point(default_item->getSize().width / 2.0f, default_item->getSize().height / 2.0f)); default_item->addChild(default_button); // set model listView->setItemModel(default_item); // add default item ssize_t count = _array->count(); for (int i = 0; i < count / 4; ++i) { listView->pushBackDefaultItem(); } // insert default item for (int i = 0; i < count / 4; ++i) { listView->insertDefaultItem(0); } // add custom item for (int i = 0; i < count / 4; ++i) { Button* custom_button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); custom_button->setName("Title Button"); custom_button->setScale9Enabled(true); custom_button->setSize(default_button->getSize()); Layout *custom_item = Layout::create(); custom_item->setSize(custom_button->getSize()); custom_button->setPosition(Point(custom_item->getSize().width / 2.0f, custom_item->getSize().height / 2.0f)); custom_item->addChild(custom_button); listView->pushBackCustomItem(custom_item); } // insert custom item Vector<Widget*>& items = listView->getItems(); ssize_t items_count = items.size(); for (int i = 0; i < count / 4; ++i) { Button* custom_button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png"); custom_button->setName("Title Button"); custom_button->setScale9Enabled(true); custom_button->setSize(default_button->getSize()); Layout *custom_item = Layout::create(); custom_item->setSize(custom_button->getSize()); custom_button->setPosition(Point(custom_item->getSize().width / 2.0f, custom_item->getSize().height / 2.0f)); custom_item->addChild(custom_button); listView->insertCustomItem(custom_item, items_count); } // set item data items_count = items.size(); for (int i = 0; i < items_count; ++i) { Widget* item = listView->getItem(i); Button* button = static_cast<Button*>(item->getChildByName("Title Button")); ssize_t index = listView->getIndex(item); button->setTitleText(static_cast<__String*>(_array->getObjectAtIndex(index))->getCString()); } // remove last item listView->removeLastItem(); // remove item by index items_count = items.size(); listView->removeItem(items_count - 1); // set all items layout gravity listView->setGravity(LISTVIEW_GRAVITY_CENTER_VERTICAL); // set items margin listView->setItemsMargin(2.0f); return true; } return false; }
Node* CSLoader::loadNode(const rapidjson::Value& json) { Node* node = nullptr; std::string nodeType = DICTOOL->getStringValue_json(json, CLASSNAME); NodeCreateFunc func = _funcs[nodeType]; if (func != nullptr) { const rapidjson::Value& options = DICTOOL->getSubDictionary_json(json, OPTIONS); node = func(options); // component if (node) { const rapidjson::Value& components = DICTOOL->getSubDictionary_json(options, COMPONENTS); int componentSize = DICTOOL->getArrayCount_json(options, COMPONENTS, 0); for (int i = 0; i < componentSize; ++i) { const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(components, COMPONENTS, i); Component* component = loadComponent(dic); if (component) { node->addComponent(component); } } } } if(node) { int length = DICTOOL->getArrayCount_json(json, CHILDREN, 0); for (int i = 0; i<length; i++) { const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(json, CHILDREN, i); Node* child = loadNode(dic); if (child) { PageView* pageView = dynamic_cast<PageView*>(node); ListView* listView = dynamic_cast<ListView*>(node); if (pageView) { Layout* layout = dynamic_cast<Layout*>(child); if (layout) { pageView->addPage(layout); } } else if (listView) { Widget* widget = dynamic_cast<Widget*>(child); if (widget) { listView->pushBackCustomItem(widget); } } else { if (_monoCocos2dxVersion != "3.x") { Widget* widget = dynamic_cast<Widget*>(child); Widget* parent = dynamic_cast<Widget*>(node); if (widget && parent && !dynamic_cast<Layout*>(parent)) { if (widget->getPositionType() == ui::Widget::PositionType::PERCENT) { widget->setPositionPercent(Vec2(widget->getPositionPercent().x + parent->getAnchorPoint().x, widget->getPositionPercent().y + parent->getAnchorPoint().y)); widget->setPosition(Vec2(widget->getPositionX() + parent->getAnchorPointInPoints().x, widget->getPositionY() + parent->getAnchorPointInPoints().y)); } else { Size parentSize = parent->getContentSize(); widget->setPosition(Vec2(widget->getPositionX() + parentSize.width * parent->getAnchorPoint().x, widget->getPositionY() + parentSize.height * parent->getAnchorPoint().y)); } } } node->addChild(child); } child->release(); } } } else { CCLOG("Not supported NodeType: %s", nodeType.c_str()); } return node; }
Node* CSLoader::nodeWithFlatBuffersForSimulator(const flatbuffers::NodeTree *nodetree) { Node* node = nullptr; std::string classname = nodetree->classname()->c_str(); auto options = nodetree->options(); if (classname == "ProjectNode") { auto reader = ProjectNodeReader::getInstance(); auto projectNodeOptions = (ProjectNodeOptions*)options->data(); std::string filePath = projectNodeOptions->fileName()->c_str(); cocostudio::timeline::ActionTimeline* action = nullptr; if (filePath != "" && FileUtils::getInstance()->isFileExist(filePath)) { node = createNodeWithFlatBuffersForSimulator(filePath); action = cocostudio::timeline::ActionTimelineCache::getInstance()->createActionWithFlatBuffersForSimulator(filePath); } else { node = Node::create(); } reader->setPropsWithFlatBuffers(node, (const flatbuffers::Table*)options->data()); if (action) { action->setTimeSpeed(projectNodeOptions->innerActionSpeed()); node->runAction(action); action->gotoFrameAndPause(0); } } else if (classname == "SimpleAudio") { node = Node::create(); auto reader = ComAudioReader::getInstance(); Component* component = reader->createComAudioWithFlatBuffers((const flatbuffers::Table*)options->data()); if (component) { node->addComponent(component); reader->setPropsWithFlatBuffers(node, (const flatbuffers::Table*)options->data()); } } else { std::string readername = getGUIClassName(classname); readername.append("Reader"); NodeReaderProtocol* reader = dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername)); if (reader) { node = reader->createNodeWithFlatBuffers((const flatbuffers::Table*)options->data()); } Widget* widget = dynamic_cast<Widget*>(node); if (widget) { std::string callbackName = widget->getCallbackName(); std::string callbackType = widget->getCallbackType(); bindCallback(callbackName, callbackType, widget, _rootNode); } if (_rootNode == nullptr) { _rootNode = node; } // _loadingNodeParentHierarchy.push_back(node); } // If node is invalid, there is no necessity to process children of node. if (!node) { return nullptr; } auto children = nodetree->children(); int size = children->size(); for (int i = 0; i < size; ++i) { auto subNodeTree = children->Get(i); Node* child = nodeWithFlatBuffersForSimulator(subNodeTree); if (child) { PageView* pageView = dynamic_cast<PageView*>(node); ListView* listView = dynamic_cast<ListView*>(node); if (pageView) { Layout* layout = dynamic_cast<Layout*>(child); if (layout) { pageView->addPage(layout); } } else if (listView) { Widget* widget = dynamic_cast<Widget*>(child); if (widget) { listView->pushBackCustomItem(widget); } } else { node->addChild(child); } } } // _loadingNodeParentHierarchy.pop_back(); return node; }
//------------------------------------------- bool DialogView::init(const std::string& strTitle, const std::string& strContent) { if (!Node::init()) return false; Size winSize = Director::getInstance()->getWinSize(); Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); // 截断事件传递 EventListenerTouchOneByOne* eve = EventListenerTouchOneByOne::create(); eve->setSwallowTouches(true); eve->onTouchBegan = [](Touch* t, Event* e) { return true; }; Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(eve, this); // 背景 Sprite* pPlane = Sprite::create(RES::DIALOG_PLANE); pPlane->setPosition(origin + visibleSize / 2); this->addChild(pPlane); // 确认按钮 Button* pBtnOK = Button::create(RES::DIALOG_OK); pBtnOK->setPosition(Vec2(pPlane->getContentSize().width / 2, 0)); pBtnOK->addClickEventListener([=](Ref* pS) { if (onOkButtonClick) onOkButtonClick(this); }); pPlane->addChild(pBtnOK); // 标题 Text* pTextTitle = Text::create(strTitle, "", 32); pTextTitle->setPosition(Vec2(pPlane->getContentSize().width / 2, pPlane->getContentSize().height - 28)); pPlane->addChild(pTextTitle); // 内容List ListView* pListView = ListView::create(); pListView->setAnchorPoint(Vec2(0.5, 0.5)); pListView->setPosition(Vec2(pPlane->getContentSize().width/2, 146)); pListView->setContentSize(Size(pPlane->getContentSize().width - 60, 210)); pListView->setBounceEnabled(true); pPlane->addChild(pListView); // 内容Text Text* pTextContent = Text::create(strContent, "", 26); pTextContent->setTextHorizontalAlignment(TextHAlignment::CENTER); pTextContent->setTextAreaSize(Size(pListView->getContentSize().width, 0)); pTextContent->ignoreContentAdaptWithSize(false); pListView->pushBackCustomItem(pTextContent); // 播放动画 pPlane->setScaleX(0); pPlane->setScaleY(0.5); ScaleTo* pActionScale = ScaleTo::create(0.35, 1); pPlane->runAction(EaseBackOut::create(pActionScale)); return true; }