l

成都手游码农一枚
随笔 - 32, 文章 - 0, 评论 - 117, 引用 - 0
数据加载中……

[cocos2d-x]分享一个简单的下拉列表控件。

// 点击,弹出,选择,关闭。

#ifndef DROPDOWNLIST_H_INCLUDED
#define DROPDOWNLIST_H_INCLUDED

namespace azl
{

#define DROPDOWNLIST_NORMAL_COLOR       cocos2d::ccc4(128, 128, 128, 255)
#define DROPDOWNLIST_SELECTED_COLOR     cocos2d::ccc4(200, 200, 200, 255)
#define DROPDOWNLIST_HIGHLIGHT_COLOR    cocos2d::ccc4(60, 60, 200, 255)

#define DROPDOWNLIST_NORMAL_COLOR3       cocos2d::ccc3(128, 128, 128)
#define DROPDOWNLIST_SELECTED_COLOR3     cocos2d::ccc3(200, 200, 200)
#define DROPDOWNLIST_HIGHLIGHT_COLOR3    cocos2d::ccc3(60, 60, 200)

class DropDownList : public cocos2d::CCLayer
{
public:
    class TouchHelper : public cocos2d::CCObject, public cocos2d::CCTouchDelegate
    {
    protected:
        TouchHelper(DropDownList& list)
            : list_(list)
        {
            cocos2d::CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, cocos2d::kCCMenuHandlerPriority, true);
        }

    public:
        static TouchHelper* create(DropDownList& list)
        {
            return new TouchHelper(list);
        }

        void destroy()
        {
            cocos2d::CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
            autorelease();
        }

        virtual bool ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)
        {
            list_.onClose();
            return true;
        }

        DropDownList& list_;
    };

protected:
    DropDownList(cocos2d::CCLabelTTF* label, cocos2d::CCSize size)
        : label_(label)
        , show_(false)
        , lastSelectedIndex_(0)
        , helper_(NULL)
        , item_(NULL)
    {
        menu_ = cocos2d::CCMenu::create();
        menu_->setPosition(cocos2d::CCPoint(size.width / 2, size.height / 2));
        menu_->setVertexZ(0.1f);
        menu_->retain();

        label_->setPosition(cocos2d::CCPoint(size.width / 2, size.height / 2));
        addChild(label_);

        setContentSize(size);
    }

public:
    ~DropDownList()
    {
        menu_->release();
    }

    static DropDownList* create(cocos2d::CCLabelTTF* label, cocos2d::CCSize size)
    {
        DropDownList* list = new DropDownList(label, size);
        list->autorelease();
        return list;
    }

    std::string getString()
    {
        return label_->getString();
    }

    int getSelectedIndex()
    {
        return lastSelectedIndex_;
    }

    void setSelectedIndex(int index)
    {
        lastSelectedIndex_ = index;

        for (int i = 0, j = (int)labels_.size(); i < j; ++i)
        {
            if (i == lastSelectedIndex_)
            {
                layers_[i]->setColor(DROPDOWNLIST_HIGHLIGHT_COLOR3);
                label_->setString(labels_[i]->getString());
            }
            else
            {
                layers_[i]->setColor(DROPDOWNLIST_NORMAL_COLOR3);
            }
        }
    }

    void setDropMenuItem(cocos2d::CCMenuItem* item)
    {
        CC_ASSERT(item_ == NULL);
        item_ = item;

        addChild(item_);

        cocos2d::CCSize size = getContentSize();
        item_->setAnchorPoint(cocos2d::CCPoint(1.0f, 0.5f));
        item_->setPosition(size.width, size.height / 2.0f);

        label_->setAnchorPoint(cocos2d::CCPoint(0.0f, 0.5f));
        label_->setPosition(cocos2d::CCPoint(4, size.height / 2.0f));
    }

public:
    void onEnter()
    {
        setTouchEnabled(true);
        cocos2d::CCLayer::onEnter();
    }

    void registerWithTouchDispatcher()
    {
        cocos2d::CCDirector* pDirector = cocos2d::CCDirector::sharedDirector();
        pDirector->getTouchDispatcher()->addTargetedDelegate(this, cocos2d::kCCMenuHandlerPriority, true);
    }

    virtual bool ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)
    {
        if (!show_)
        {
            cocos2d::CCRect rect;
            rect.origin = cocos2d::CCPointZero;
            rect.size = getContentSize();
            cocos2d::CCPoint position = convertTouchToNodeSpace(touch);

            if (rect.containsPoint(position))
            {
                show_ = true;
                helper_ = TouchHelper::create(*this);
                addChild(menu_);

                for (int i = 0, j = (int)labels_.size(); i < j; ++i)
                {
                    if (i == lastSelectedIndex_)
                    {
                        layers_[i]->setColor(DROPDOWNLIST_HIGHLIGHT_COLOR3);
                    }
                    else
                    {
                        layers_[i]->setColor(DROPDOWNLIST_NORMAL_COLOR3);
                    }
                }

                if (item_)
                {
                    item_->selected();
                }

                return true;
            }
        }
       
        return false;
    }

    void addLabel(cocos2d::CCLabelTTF* label)
    {
        cocos2d::CCSize size = getContentSize();

        cocos2d::CCLayerColor* normal   = cocos2d::CCLayerColor::create(DROPDOWNLIST_NORMAL_COLOR, size.width, size.height);
        cocos2d::CCLayerColor* selected = cocos2d::CCLayerColor::create(DROPDOWNLIST_SELECTED_COLOR, size.width, size.height);

        layers_.push_back(normal);
        labels_.push_back(label);
        
        cocos2d::CCMenuItem* item = cocos2d::CCMenuItemSprite::create(
                normal, 
                selected, 
                NULL,
                this,
                cocos2d::SEL_MenuHandler(&DropDownList::onSelected)
            );

        label->setPosition(cocos2d::CCPoint(size.width / 2, size.height / 2));
        item->addChild(label);
        item->setTag((int)labels_.size() - 1);
        item->setPosition(0, - (int)labels_.size() * size.height);

        menu_->addChild(item);
    }

    void onSelected(cocos2d::CCObject* sender)
    {
        cocos2d::CCMenuItem* item = dynamic_cast<cocos2d::CCMenuItem*>(sender);

        if (item)
        {
            lastSelectedIndex_ = item->getTag();
            label_->setString(labels_[item->getTag()]->getString());
        }

        onClose();
    }

    void onClose()
    {
        removeChild(menu_, true);
        
        helper_->destroy();
        helper_ = NULL;

        show_ = false;

        if (item_)
        {
            item_->unselected();
        }
    }

private:
    cocos2d::CCMenu* menu_;
    cocos2d::CCLabelTTF* label_;
    std::vector<cocos2d::CCLabelTTF*> labels_;
    std::vector<cocos2d::CCLayerColor*> layers_;
    bool show_;
    int lastSelectedIndex_;
    TouchHelper* helper_;
    cocos2d::CCMenuItem* item_;
};

}

#endif

posted on 2013-07-01 23:50 l1989 阅读(5766) 评论(3)  编辑 收藏 引用 所属分类: C++游戏

评论

# re: [cocos2d-x]分享一个简单的下拉列表控件。  回复  更多评论   

可以用,帮楼主写了个test

Says* says = Says::create();
CCLabelTTF* labeldropdown = new CCLabelTTF();
std::wstring tt2 = L"侵略";
std::string text2 = says->WStrToUTF8(tt2);
labeldropdown->initWithString(text2.c_str(), "黑体", 22);
CCSize itemSize = CCSizeMake(80,30);
azl::DropDownList* pdd = azl::DropDownList::create(labeldropdown,itemSize);

CCLabelTTF* dd1 = new CCLabelTTF();
std::wstring tt3 = L"侵略1";
std::string text3 = says->WStrToUTF8(tt3);
dd1->initWithString(text3.c_str(), "黑体", 22);
pdd->addLabel(dd1);

CCLabelTTF* dd2 = new CCLabelTTF();
std::wstring tt4 = L"侵略2";
std::string text4 = says->WStrToUTF8(tt4);
dd2->initWithString(text4.c_str(), "黑体", 22);
pdd->addLabel(dd2);

pdd->setPosition(20,300);
this->addChild(pdd,12);
2013-07-17 19:42 | alsky

# re: [cocos2d-x]分享一个简单的下拉列表控件。  回复  更多评论   

多谢,不错的组件
2013-10-11 08:19 | xjh

# re: [cocos2d-x]分享一个简单的下拉列表控件。  回复  更多评论   

谢谢分享。
在研读你的代码!
也谢谢楼上的测试代码!
谢谢分享!
2014-09-17 16:47 | 简单

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理