Cocos2dx-detailed instructions on using the cocostudio Interface

Source: Internet
Author: User

Cocos2dx-detailed instructions on using the cocostudio Interface

1. I used cocostudio of the latest version 2.3 to set up a simple interface:

Inheritance relationships (names are messy and unchanged ):

2. Use it in the cocos2dx lua Program

[Loading interface]

Use cocostudio to export the file as a csb file and put it in the Resource Directory. Add the ui code as follows:

 

-- load csblocal node = cc.CSLoader:createNode('Test.csb')layer:addChild(node)

 

Button]

The cocos3.x version emphasizes the use of events to transmit messages, reducing inter-module coupling. Here, gui processing also adds event listening to the control to be processed.

 

-- btnlocal btn = node:getChildByName('btn_ok')btn:addTouchEventListener(function(sender, state)    -- 2    if state == ccui.TouchEventType.ended then        print('on btn ok')    end    end)

[Progress bar LoadingBar and check box CheckBox]

 

 

-- loading barlocal loading = node:getChildByName('LoadingBar_1')-- checkboxlocal chk = node:getChildByName('CheckBox_1')chk:addEventListener(function (sender, type)    -- 0    if type == ccui.CheckBoxEventType.selected then        -- sel        print('sel')        loading:setPercent(50)    else        -- unsel        print('unsel')        loading:setPercent(0)    endend)

[List control ListView]

 

There are several containers, and ScrollView and PageView are both easier. Here we will talk about ListView.

ListView allows you to set the model of the list items. After setting the model, you can easily clone multiple items. His use is that art can edit the template in the editor, and the program can be cloned. If there are multiple types, there will be a few more pieces of Fine Arts. When a program builds a framework, it encapsulates a layer of logic for setting the model to choose which template to use. In this way, different list items can be implemented. For more information, see the following examples.

Set the template and create multiple templates as follows:

 

-- set item modellocal item = list:getChildByName('Image_item')list:setItemModel(item)list:removeAllChildren()for i = 1, 10 do    list:pushBackDefaultItem()end
My template here is placed under listview in the editor and removed after the settings are complete (note that the removal should be done using listview instead of removeFromParent ). The running effect is as follows:

 

Below we want to press the corresponding button:

 

list:addEventListener(function (sender, type)    -- 0    if type == ccui.ListViewEventType.ONSELECTEDITEM_START then        -- sel        print(select child index = ,sender:getCurSelectedIndex())                loading:setPercent(50)    else        -- unsel        print('unsel')        loading:setPercent(0)    endend)
When a button is just pressed (in fact its touchBegan), it is a start event, and when it is lifted (that is, touchEnded) is an end event. At this time, you will ask why the list event is added, but you can receive the message when the button is pressed. This is because their base class widgets and ListView do something.

 

The Widget sends the event to the parent in touch processing such as touchBegan:

 

bool Widget::onTouchBegan(Touch *touch, Event *unusedEvent){    _hitted = false;    if (isVisible() && isEnabled() && isAncestorsEnabled() && isAncestorsVisible(this) )    {        _touchBeganPosition = touch->getLocation();        if(hitTest(_touchBeganPosition) && isClippingParentContainsPoint(_touchBeganPosition))        {            _hitted = true;        }    }    if (!_hitted)    {        return false;    }    setHighlighted(true);    /*     * Propagate touch events to its parents     */    if (_propagateTouchEvents)    {        this->propagateTouchEvent(TouchEventType::BEGAN, this, touch);    }    pushDownEvent();    return true;}void Widget::interceptTouchEvent(cocos2d::ui::Widget::TouchEventType event, cocos2d::ui::Widget *sender, Touch *touch){    Widget* widgetParent = getWidgetParent();    if (widgetParent)    {        widgetParent->interceptTouchEvent(event,sender,touch);    }}
In this way, when a widget is touched, his ancestors will (have the opportunity) know that a later generation has been touched. So when the button is pressed, listview can receive the event and handle it:

 

 

void ListView::interceptTouchEvent(TouchEventType event, Widget *sender, Touch* touch){    ScrollView::interceptTouchEvent(event, sender, touch);    if (event != TouchEventType::MOVED)    {        Widget* parent = sender;        while (parent)        {            if (parent && parent->getParent() == _innerContainer)            {                _curSelectedIndex = getIndex(parent);                break;            }            parent = dynamic_cast
 
  (parent->getParent());        }        if (sender->isHighlighted()) {            selectedItemEvent(event);        }    }}void ListView::selectedItemEvent(TouchEventType event){    this->retain();    switch (event)    {        case TouchEventType::BEGAN:        {            if (_listViewEventListener && _listViewEventSelector)            {                (_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_START);            }            if (_eventCallback) {                _eventCallback(this,EventType::ON_SELECTED_ITEM_START);            }            if (_ccEventCallback)            {                _ccEventCallback(this, static_cast
  
   (EventType::ON_SELECTED_ITEM_START));            }        }        break;        default:        {            if (_listViewEventListener && _listViewEventSelector)            {                (_listViewEventListener->*_listViewEventSelector)(this, LISTVIEW_ONSELECTEDITEM_END);            }            if (_eventCallback) {                _eventCallback(this, EventType::ON_SELECTED_ITEM_END);            }            if (_ccEventCallback)            {                _ccEventCallback(this, static_cast
   
    (EventType::ON_SELECTED_ITEM_END));            }        }        break;    }    this->release();}
   
  
 
The result is that if a button under an item is touched by listview, the problem also arises. What if there are multiple buttons? The event parameters of the listview are only this (A listview pointer) and event type. You cannot know which button is pressed. You can only know which item is pressed through curSel. You need to identify which button under the item is pressed, so it is best to add event processing to the button.

 

If you are too reluctant to add buttons, you can use the template to add event processing to the template. The list items cloned by the template will also be processed. Add the following lua code before setting the template:

 

-- set item callback: we can just add event to model, and diff from curSellocal item0 = list:getItem(0)local btn_0 = item0:getChildByName('Button_2')btn_0:addTouchEventListener(function(sender, state)    if state == 2 then        print('on btn test 0')    end    end)
In this way, you can add events and respond to the buttons of each list item. So the listview touch event can only be used for one button (I wonder if the button parameters will be added to the event later ), add multiple buttons to the template.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.