*.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> #include "iconv/UTF8.h" #include "xml/XML.h" #include "socket/Private/MemoryPool.h" 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; float m_CreateCellTime = 0.0f; TiXmlDocument *m_XMLDoc = nullptr; MemoryPool m_MemoryPool; 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); void createXMLSavePathName(string filename); void addPath(string path); void addPathChild(string path, string data); void refreshTableView(); }; #endif // !__MUSICLAYER_H__
*.cpp
{
#include "MusicLayer.h" #ifdef _WIN32 #include "ATBAudioEngine/ATBAudioEngine.h" #endif #define DISPLAY Director::getInstance()->getVisibleSize() #define displayex Director::getInstance()->getVisibleSize() MusicLayer::MusicLayer() { this->inits(); } MusicLayer::~MusicLayer() { this->free(); } void MusicLayer::free() { if (m_XMLDoc != nullptr) { m_XMLDoc->SaveFile((FileUtils::getInstance()->getWritablePath() + "MusicData.xml").c_str()); XML::GetInstance()->free(m_XMLDoc); } } void MusicLayer::inits() { log("MusicLayer init"); FileUtils::getInstance()->addSearchPath(FileUtils::getInstance()->getWritablePath()); 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(); this->createXMLSavePathName(FileUtils::getInstance()->getWritablePath() + "MusicData.xml"); this->refreshTableView(); } 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(100, 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::createXMLSavePathName(string filename) { auto doc = XML::GetInstance()->create(); ssize_t size = 0; auto retData = FileUtils::getInstance()->getFileData(filename, "rb+", &size); char *data = (char*)(m_MemoryPool.Allocate(size + 1)); memcpy(data, retData, size); std::cout << data << std::endl; doc->Parse((const char*)(data)); if (!retData) { m_XMLDoc = doc; XML::GetInstance()->createRoot(doc, "root"); } else { XML::GetInstance()->free(doc); m_XMLDoc = new TiXmlDocument(); m_XMLDoc->Parse((const char*)(data)); auto root = m_XMLDoc->FirstChildElement("root"); for (auto i = root->FirstChildElement(); i != nullptr; i = i->NextSiblingElement()) { if (i != nullptr) { auto text = i->GetText(); if (text != nullptr) { FileUtils::getInstance()->addSearchPath(text); for (auto j = i->FirstChildElement(); j != nullptr; j = j->NextSiblingElement()) { std::cout << j->GetText() << std::endl; this->m_ListPath.push_back(j->GetText()); this->m_TableNum++; } } } } } } void MusicLayer::addPath(string path) { if (m_XMLDoc != nullptr) { auto node = XML::GetInstance()->createNode("path"); auto text = XML::GetInstance()->createText(path); node->LinkEndChild(text); this->m_XMLDoc->RootElement()->LinkEndChild(node); } } void MusicLayer::addPathChild(string path, string data) { if (m_XMLDoc != nullptr) { auto root = this->m_XMLDoc->RootElement()->FirstChildElement(); for (auto i = root; i != nullptr; i = root->NextSiblingElement()) { std::cout << i->GetText() << std::endl; if (i->GetText() == path) { auto node = XML::GetInstance()->createNode("child"); auto text = XML::GetInstance()->createText(data); node->LinkEndChild(text); i->LinkEndChild(node); break; } } } } void MusicLayer::refreshTableView() { auto setLayer = this->getChildByName("setLayer"); if (setLayer != nullptr) { TableView *tableView = (TableView*)(setLayer->getChildByName("tableView")); if (tableView != nullptr) { tableView->reloadData(); } } } void MusicLayer::OnCallback(cocos2d::Ref* pSender) { string name = ((Node*)(pSender))->getName(); if (name == "play") { SimpleAudioEngine::getInstance()->playBackgroundMusic("D:/Audio/YZFHKMS-H.mp3", true); } 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"); edit->setTag(0); this->addChild(edit); edit->setDelegate(this); } else { if (editBox->getTag() == 0) { editBox->runAction(MoveTo::create(0.2f, Vec3(displayex.width / 2 + 10, displayex.height - editBox->getContentSize().height / 2, 0))); editBox->setTag(1); } else if (editBox->getTag() == 1) { editBox->runAction(MoveTo::create(0.2f, Vec3(-(editBox->getContentSize().width), editBox->getPosition().y, 0))); editBox->setTag(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(); editBox->setTag(0); if (str == "") { log("editBoxReturn: %s", "NULL"); editBox->runAction(MoveTo::create(0.2f, Vec3(-(editBox->getContentSize().width), editBox->getPosition().y, 0))); return; } else { log("editBoxReturn: %s", str.c_str()); } auto setLayer = this->getChildByName("setLayer"); TableView *tableView = (TableView*)(setLayer->getChildByName("tableView")); tableView->reloadData(); auto sePath = FileUtils::getInstance()->getSearchPaths(); bool isExist = false; for (auto sp = sePath.begin(); sp != sePath.end(); sp++) { if (str.c_str()[str.length() - 1] != '/') { str.push_back('/'); } if ((*sp) == str) { isExist = true; break; } } //如果路径不存在 if (!isExist) { FileUtils::getInstance()->addSearchPath(str); this->addPath(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->addPathChild(str, tmp); 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()); log("fullPathForFilename %s", FileUtils::getInstance()->fullPathForFilename(file).c_str()); string GB2312File = UTF8::GetInstance()->UTF_8_TO_GB2312(FileUtils::getInstance()->fullPathForFilename(file)); string utf8File = FileUtils::getInstance()->fullPathForFilename(file); #ifdef _WIN32 ATBAE::GetInstance()->PauseAllMusicAndEffects(); ATBAE::GetInstance()->LoadMusicsAndPlay(GB2312File); #else SimpleAudioEngine::getInstance()->playBackgroundMusic(utf8File.c_str(), true); #endif } 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(displayex.width / 2, 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); sprite->runAction(MoveTo::create(m_CreateCellTime,Vec3(0,0,0))); m_CreateCellTime += 0.2f; 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; }
}
}