zoukankan      html  css  js  c++  java
  • coco2d-x create tableView

    *.h

    #ifndef __MUSICLAYER_H__
    #define __MUSICLAYER_H__
    #include "cocos2d.h"
    #include "cocos/ui/UIButton.h"
    #include "cocos/ui/UIEditBox/UIEditBox.h"
    #include "extensions/GUI/CCScrollView/CCTableView.h"
    #include "SimpleAudioEngine.h"
    #include <vector>
    using namespace cocos2d;
    using namespace ui;
    using namespace CocosDenshion;
    using namespace extension;
    using namespace std;
    
    
    
    class CustomTableViewCell : public TableViewCell
    {
    public:
        virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) override;
    };
    
    class MusicLayer:public LayerColor, 
        EditBoxDelegate, 
        TableViewDataSource,
        TableViewDelegate
    {
        int m_TableNum = 0;
        vector<string> m_ListPath;
    public:
        void inits();
        void free();
        MusicLayer();
        ~MusicLayer();
    private:
        void OnCallback(cocos2d::Ref* pSender);
        virtual bool onTouchBegan(Touch *touch, Event *unused_event) override;
    
        virtual void editBoxEditingDidBegin(EditBox* editBox)override;
        CC_DEPRECATED_ATTRIBUTE virtual void editBoxEditingDidEnd(EditBox* editBox)override;
        virtual void editBoxTextChanged(EditBox* editBox, const std::string& text)override;
        virtual void editBoxReturn(EditBox* editBox)override;
        virtual void editBoxEditingDidEndWithAction(EditBox* editBox, EditBoxDelegate::EditBoxEndAction action)override;
    
        virtual void scrollViewDidScroll(ScrollView* view) override {}
        virtual void scrollViewDidZoom(ScrollView* view) override {}
        virtual void tableCellTouched(TableView* table, TableViewCell* cell)override;
        virtual Size tableCellSizeForIndex(TableView *table, ssize_t idx)override;
        virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx)override;
        virtual ssize_t numberOfCellsInTableView(TableView *table)override;
    private:
        void initFileSearchPathLayer();
        void initPlayLayer();
        void initTableView();
        void refreshCellItem(Ref*node,ssize_t idx);
    
    };
    #endif // !__MUSICLAYER_H__

    *.cpp

    #include "MusicLayer.h"
    //using namespace cocos2d;
    //using namespace ui;
    //using namespace CocosDenshion;
    //using namespace std;
    
    
    
    
    #define DISPLAY Director::getInstance()->getVisibleSize()
    #define displayex Director::getInstance()->getVisibleSize()
    
    MusicLayer::MusicLayer()
    {
        this->inits();
    }
    
    MusicLayer::~MusicLayer()
    {
        this->free();
    }
    
    void MusicLayer::free()
    {
    
    }
    
    void MusicLayer::inits()
    {
        log("MusicLayer init");
        this->setTouchEnabled(true);
        auto ELTOBO = EventListenerTouchOneByOne::create();
        ELTOBO->setSwallowTouches(true);
        ELTOBO->onTouchBegan = std::move(std::bind(&MusicLayer::onTouchBegan, this, std::placeholders::_1, std::placeholders::_2));
        this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(ELTOBO, this);
        //this->getEventDispatcher()->addEventListenerWithFixedPriority();
        //this->getEventDispatcher()->addCustomEventListener();
    
        string file("res/Button.png");
        auto btn = cocos2d::ui::Button::create(file, file, file);
        btn->setColor(Color3B(24, 48, 64));
        btn->setPressedActionEnabled(true);
        btn->setScale9Enabled(true);
        btn->setContentSize(Size(100, 50));
        btn->setPosition(Vec2(displayex.width - btn->getContentSize().width / 2,
            displayex.height - btn->getContentSize().height / 2));
        btn->setTitleColor(Color3B::BLUE);
        btn->setTitleFontSize(50);
        btn->setName("X");
        btn->setTitleText("X");
        btn->addClickEventListener(std::bind(&MusicLayer::OnCallback, this, std::placeholders::_1));
        this->addChild(btn);
    
    
        this->initFileSearchPathLayer();
    
        this->initPlayLayer();
    
        this->initTableView();
    }
    
    void MusicLayer::initFileSearchPathLayer()
    {
        //seting layer
        auto setLayer = LayerColor::create(Color4B(24, 48, 64, 255));
        setLayer->setContentSize(Size(displayex.width / 3, displayex.height - 50));
        setLayer->setPosition(0, 50);
        setLayer->setName("setLayer");
        this->addChild(setLayer, 1);
    
        string file("res/Button.png");
        auto btn = cocos2d::ui::Button::create(file, file, file);
        btn->setColor(Color3B(24, 48, 64));
        btn->setPressedActionEnabled(true);
        btn->setScale9Enabled(true);
        btn->setContentSize(Size(setLayer->getContentSize().width - 100, 40));
        btn->setPosition(Vec2(setLayer->getContentSize().width / 2,
            setLayer->getContentSize().height - btn->getContentSize().height / 2));
        btn->setTitleColor(Color3B::BLUE);
        btn->setTitleFontSize(30);
        btn->setName("Search");
        btn->setTitleText("SEARCH");
        btn->addClickEventListener(std::bind(&MusicLayer::OnCallback, this, std::placeholders::_1));
        setLayer->addChild(btn);
    }
    
    void MusicLayer::initPlayLayer()
    {
        //player layer
        auto playLayer = LayerColor::create(Color4B(16, 65, 86, 255));
        playLayer->setContentSize(Size(displayex.width, 50));
        this->addChild(playLayer, 1);
    
    
    
        string file("res/Button.png");
        auto btn = cocos2d::ui::Button::create(file, file, file);
        btn->setColor(Color3B(24, 48, 64));
        btn->setPosition(Vec2(playLayer->getContentSize().width / 2,
            playLayer->getContentSize().height / 2));
        btn->setPressedActionEnabled(true);
        btn->setScale9Enabled(true);
        btn->setContentSize(Size(50, 50));
        btn->setTitleColor(Color3B::BLUE);
        btn->setTitleFontSize(20);
        btn->setName("play");
        btn->setTitleText("PLAY");
        btn->addClickEventListener(std::bind(&MusicLayer::OnCallback, this, std::placeholders::_1));
        playLayer->addChild(btn);
    }
    
    void MusicLayer::initTableView()
    {
        auto setLayer = this->getChildByName("setLayer");
        auto tableView = TableView::create(this,Size(setLayer->getContentSize().width,setLayer->getContentSize().height - 50));
        tableView->setColor(Color3B(13, 64, 98));
        tableView->setName("tableView");
        tableView->setContentSize(Size(setLayer->getContentSize().width, setLayer->getContentSize().height - 100));
        tableView->setDirection(ScrollView::Direction::VERTICAL);
        tableView->setDelegate(this);
        tableView->setTouchEnabled(true);
        tableView->reloadData();
        setLayer->addChild(tableView);
    }
    
    
    void MusicLayer::OnCallback(cocos2d::Ref* pSender)
    {
        string name = ((Node*)(pSender))->getName();
        if (name == "play")
        {
            SimpleAudioEngine::getInstance()->playBackgroundMusic("D:\Audio/YZFHKMS-H.mp3");
        }
        else if (name == "X")
        {
            auto action = Sequence::create(MoveTo::create(0.2f, Vec3(-(displayex.width), 0, 0)),
                DelayTime::create(0.1f),
                CallFunc::create([=]()
            {
                this->removeFromParent();
            }), nullptr);
            this->runAction(action);
        }
        else if (name == "Search")
        {
            auto editBox = this->getChildByName("path");
            if (!(editBox))
            {
                auto edit = ui::EditBox::create(Size(400, 50), "res/input.png");
                edit->setPosition(Vec2(displayex.width / 2 + 10, displayex.height - edit->getContentSize().height / 2));
                edit->setName("path");
                this->addChild(edit);
                edit->setDelegate(this);
            }
            else
            {
                editBox->runAction(MoveTo::create(0.2f, Vec3(displayex.width / 2 + 10, displayex.height - editBox->getContentSize().height / 2, 0)));
            }
        }
        else if (name == "path")
        {
            log("path");
        }
    }
    
    void MusicLayer::editBoxEditingDidBegin(EditBox* editBox)
    {
        log("editBoxEditingDidBegin");
    }
    void MusicLayer::editBoxEditingDidEnd(EditBox* editBox)
    {
        log("editBoxEditingDidEnd");
    }
    void MusicLayer::editBoxTextChanged(EditBox* editBox, const std::string& text)
    {
        log("editBoxTextChanged");
    }
    void MusicLayer::editBoxReturn(EditBox* editBox)
    {
        string str = editBox->getText();
        if (str == "")
        {
            log("editBoxReturn: %s", "NULL");
            return;
        }
        else
        {
            log("editBoxReturn: %s", str.c_str());
        }
    
        auto setLayer = this->getChildByName("setLayer");
        TableView *tableView = (TableView*)(setLayer->getChildByName("tableView"));
        tableView->reloadData();
    
        FileUtils::getInstance()->addSearchPath(str);
        auto VV = FileUtils::getInstance()->listFiles(str);
        for (auto i = VV.begin(); i != VV.end(); i++)
        {
            auto is_0 = i->find("./", 0);
            auto is_1 = i->find("../", 0);
            int pos = 0;
            int sPos = 0;
            char tmp[128] = { 0 };
            if (is_0 == -1 && is_1 == -1)
            {
                log("%s", i->c_str());
                pos = i->find(".mp3", 0);
                if (pos != 0 && pos != -1)
                {
                    for (int j = i->size(); j >= 0; j--)
                    {
                        if ((i->c_str())[j] == '/')
                        {
                            sPos = j;
                            break;
                        }
                    }
                    memcpy(tmp, i->c_str() + sPos + 1,(i->size() - sPos));
    
                    this->m_ListPath.push_back(tmp);
                    this->m_TableNum++;
                }
            }
        }
    
        tableView->reloadData();
    
        editBox->runAction(MoveTo::create(0.2f,Vec3(-(editBox->getContentSize().width), editBox->getPosition().y,0)));
    
    }
    void MusicLayer::editBoxEditingDidEndWithAction(EditBox* editBox, EditBoxDelegate::EditBoxEndAction action)
    {
    
        log("editBoxEditingDidEndWithAction");
    }
    
    void CustomTableViewCell::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
    {
        TableViewCell::draw(renderer, transform, flags);
        // draw bounding box
        //auto pos = getPosition();
        //auto size = Size(178, 200);
        //Vec2 vertices[4] = {
        //    Vec2(pos.x + 1, pos.y + 1),
        //    Vec2(pos.x + size.width - 1, pos.y + 1),
        //    Vec2(pos.x + size.width - 1, pos.y + size.height - 1),
        //    Vec2(pos.x + 1, pos.y + size.height - 1),
        //};
        //DrawPrimitives::drawColor4B(0, 0, 255, 255);
        //DrawPrimitives::drawPoly(vertices, 4, true);
    }
    void MusicLayer::tableCellTouched(TableView* table, TableViewCell* cell)
    {
        CCLOG("cell touched at index: %ld", static_cast<long>(cell->getIdx()));
        string file = m_ListPath[(int)(cell->getIdx())].c_str();
        log("tableCellTouched %s",file.c_str());
        SimpleAudioEngine::getInstance()->playBackgroundMusic(file.c_str(),true);
    }
    Size MusicLayer::tableCellSizeForIndex(TableView *table, ssize_t idx)
    {
        return Size(displayex.width / 3 - 10, 50);
    }
    TableViewCell* MusicLayer::tableCellAtIndex(TableView *table, ssize_t idx)
    {
        //auto string = StringUtils::format("%ld", static_cast<long>(idx));
        TableViewCell *cell = table->dequeueCell();
        if (!cell)
        {
            cell = new (std::nothrow) CustomTableViewCell();
            cell->autorelease();
    
            auto sprite = Sprite::create("res/Button.png");
            sprite->setAnchorPoint(Vec2::ZERO);
            sprite->setColor(Color3B(24, 48, 64));
            sprite->setPosition(Vec2(0, 0));
            sprite->setContentSize(Size(displayex.width / 3 - 10,50));
            sprite->setTag(123);
            cell->addChild(sprite);
    
            auto label = Label::createWithSystemFont(m_ListPath[idx], "Helvetica", 21.0F);
            label->setAnchorPoint(Vec2(0.0f,0.5f));
            label->setPosition(Vec2(10, sprite->getContentSize().height / 2));
            label->setTag(123);
            sprite->addChild(label);
            log("create cell ");
        }
        else
        {
            //auto label = (Label*)(cell->getChildByTag(123)->getChildByTag(123));
            //label->setString(string);
            //label->setAnchorPoint(Vec2(0.0f, 0.5f));
            //label->setPosition(Vec2(0, label->getPosition().y));
            //log("update cell set string %s",string.c_str());
            this->refreshCellItem((Ref*)(cell), idx);
        }
        return cell;
    }
    void MusicLayer::refreshCellItem(Ref*node, ssize_t idx)
    {
        auto label = (Label*)(((Node*)(node))->getChildByTag(123)->getChildByTag(123));
        label->setString(m_ListPath[(int)(idx)]);
    }
    ssize_t MusicLayer::numberOfCellsInTableView(TableView *table)
    {
        log("cell num");
        return m_TableNum;
    }
    
    bool MusicLayer::onTouchBegan(Touch *pTouch, Event *pEvent)
    {
        log("touch MusicLayer");
        return true;
    }
  • 相关阅读:
    《TCP/IP详解》之二:流式数据交互
    《TCP/IP详解》之一:连接建立、断开
    异步日志实现
    关于继承和组合的一点总结
    GitHub源代码管理基本操作 Mossad
    移动APP的开发需求分析 Mossad
    对理想团队模式构建的设想以及对软件流程的理解 Mossad
    C语言I博客作业06
    C语言I博客作业02
    C语言I博客作业03
  • 原文地址:https://www.cnblogs.com/YZFHKMS-X/p/12722857.html
Copyright © 2011-2022 走看看