#include <fstream>
#include <json/json.h>
#include "ListViewLayer.h"
#include "SimpleAudioEngine.h"
#include "AppMacros.h"
#include "DownloadJson.h"
#include "Downloader.h"
#include "SendScene.h"
#include "VisibleRect.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace CocosDenshion;
#define nodebug 0
struct Cell{
std::
string url;
std::
string imgName;
std::
string sound;
std::
string content;
};
static std::vector<Cell> cells;
static void readJson();
static void writeJson() ;
static void scrollToIndex(CCTableView * m_table,
int index){
auto cellHeight = m_table->getContentOffset().y;
m_table->setContentOffset(ccp(0,
cellHeight
+ m_table->getDataSource()->tableCellSizeForIndex(m_table, 0).height
* 10));
}
bool ListViewLayer::init(){
bool bRet =
false;
do {
CC_BREAK_IF( !CCLayer::init() );
//test post
auto path = fullpath("xx.gif");
Downloader::postFileToUrl(path.c_str(), "http://ylg.zs108.com:8080/tmp/upload.php");
bRet =
true;
}
while(0);
return bRet;
}
void ListViewLayer::initWithJsonLink(
const std::
string &jsonLink){
DownloadJson::download(jsonLink);
sleep(0.1);
//test json
readJson();
auto writablepath = CCFileUtils::sharedFileUtils()->getWritablePath();
for(auto itr = cells.begin(); itr != cells.end(); ++itr){
auto fname = writablepath + itr->imgName;
FILE *fp = fopen(fname.c_str(), "a");
if(fp){
fclose(fp);
continue;
}
CCLog("image name: %s", itr->imgName.c_str());
Downloader::download(itr->url, itr->imgName);
Downloader::wait();
}
#if nodebug
this->m_tableNum = cells.size();
#else this->m_tableNum = 20;
#endif m_table = CCTableView::create(
this, CCSizeMake(589, 900));
m_table->setClippingToBounds(
true);
m_table->setDirection(kCCScrollViewDirectionVertical);
m_table->setPosition(CCPointZero);
m_table->setDelegate(
this);
m_table->setVerticalFillOrder(kCCTableViewFillTopDown);
this->addChild(m_table);
m_table->reloadData();
m_table->retain();
scrollToIndex(m_table, 10);
// begin from 0, so the first one in show is actully the 11th cell
}
void ListViewLayer::tableCellTouched(CCTableView* table, CCTableViewCell* cell){
CCLog("ListViewLayer: cell touched at index: %i", cell->getIdx());
auto showContent = SendScene::create();
auto grandfather = getParent()->getParent();
getParent()->addChild(showContent, grandfather->getZOrder() + 1);
//CCDirector::sharedDirector()->pushScene(showContent);
if(cell->getIdx() == m_tableNum-1){
// m_tableNum = 20;
// table->reloadData();
CCLog("m_tableNum %i", m_tableNum);
}
}
CCSize ListViewLayer::cellSizeForTable(CCTableView *table){
return CCSizeMake(589, 135);
}
static bool playing =
false;
//not usefull, in order to detect multiple playing
void ListViewLayer::PlayLanguage::playLocalLanguage(CCObject *sender){
if(playing){
return;
}
else{
playing =
true;
CCLog("Play local language: %s", mp3.c_str());
SimpleAudioEngine::sharedEngine()->playEffect(mp3.c_str());
playing =
false;
}
}
//改变CCMenu的优先级
class MyMenu :
public CCMenu{
public:
MyMenu(){}
~MyMenu(){}
static MyMenu * create(CCMenuItem* item,
){
va_list args;
va_start(args,item);
CCArray* pArray = NULL;
if( item ) {
pArray = CCArray::create(item, NULL);
CCMenuItem *i = va_arg(args, CCMenuItem*);
while(i){
pArray->addObject(i);
i = va_arg(args, CCMenuItem*);
}
}
MyMenu* pRet =
new MyMenu;
if(pRet && pRet->initWithArray(pArray)){
pRet->autorelease();
va_end(args);
return pRet;
}
va_end(args);
CC_SAFE_DELETE(pRet);
return NULL;
}
virtual void registerWithTouchDispatcher(){
//这里优先级设为1,只要比CCScrollView低就可以
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(
this, 1,
true);
}
};
bool stillLoading =
false;
CCTableViewCell* ListViewLayer::tableCellAtIndex(CCTableView *table, unsigned
int idx){
auto pString = CCString::createWithFormat("%d", idx);
CCLog("%d", idx);
if(idx+1 == m_oldNum){
if(stillLoading)
m_tableNum += 100;
}
#if nodebug
if(m_tableNum > cells.size()){
m_tableNum = cells.size();
}
#endif auto pCell = table->dequeueCell();
if (!pCell) {
pCell =
new CCTableViewCell();
pCell->autorelease();
auto verticalClaritySprite = CCSprite::create("主界面-透明横条.png");
verticalClaritySprite->setAnchorPoint(CCPointZero);
verticalClaritySprite->setPosition(ccp(0, 0));
pCell->addChild(verticalClaritySprite);
auto horizontalClaritySprite = CCSprite::create("主界面-透明竖条.png");
horizontalClaritySprite->setAnchorPoint(CCPointZero);
horizontalClaritySprite->setPosition(ccp(0, 0));
pCell->addChild(horizontalClaritySprite);
auto music =
new PlayLanguage;
if(idx % 2){
music->mp3 = std::
string("Loading.mp3");
}
else{
music->mp3 = std::
string("SelectScene.mp3");
}
auto ImgName = "主界面-待播放.png";
auto playSprite = CCMenuItemImage::create(ImgName, ImgName, music, menu_selector(PlayLanguage::playLocalLanguage));
MyMenu *menu = MyMenu::create(playSprite, NULL);
//后期将这个按钮改为CCSprite实现,加入动画,改变触摸事件的优先级
menu->setAnchorPoint(CCPointZero);
menu->setPosition(ccp(horizontalClaritySprite->getContentSize().width + playSprite->getContentSize().width/2,
verticalClaritySprite->getContentSize().height + playSprite->getContentSize().height/2));
pCell->addChild(menu);
auto horizontalClaritySprite2 = CCSprite::create("主界面-透明竖条.png");
horizontalClaritySprite2->setAnchorPoint(CCPointZero);
horizontalClaritySprite2->setPosition(ccp(horizontalClaritySprite->getContentSize().width
+ playSprite->getContentSize().width,
0));
pCell->addChild(horizontalClaritySprite2);
auto pngName = std::
string("主界面-音频信息板.png");
auto writablepath = CCFileUtils::sharedFileUtils()->getWritablePath();
// pngName = writablepath + cells[idx].imgName;
auto audioInforSprite = CCSprite::create(pngName.c_str());
audioInforSprite->setAnchorPoint(CCPointZero);
audioInforSprite->setPosition(ccp(playSprite->getContentSize().width
+ horizontalClaritySprite->getContentSize().width
+ horizontalClaritySprite2->getContentSize().width,
verticalClaritySprite->getContentSize().height));
//CCPointZero);
pCell->addChild(audioInforSprite);
CCLabelTTF *pLabel = CCLabelTTF::create(pString->getCString(), "Arial", 20.0);
pLabel->setPosition(CCPointZero);
pLabel->setAnchorPoint(CCPointZero);
pLabel->setTag(123);
pCell->addChild(pLabel);
}
else{
CCLabelTTF *pLabel = (CCLabelTTF*)pCell->getChildByTag(123);
pLabel->setString(pString->getCString());
}
return pCell;
}
unsigned
int ListViewLayer::numberOfCellsInTableView(CCTableView *table){
m_oldNum = m_tableNum;
return m_tableNum;
}
void ListViewLayer::scrollViewDidScroll(CCScrollView *view){
CCLog("scrollViewDidScroll");
}
void ListViewLayer::scrollViewDidZoom(CCScrollView *view){
CCLog("scrollViewDidZoom");
}
static void readJson() {
std::ifstream ifs;
auto writablepath = CCFileUtils::sharedFileUtils()->getWritablePath();
auto file = writablepath + "test.json";
ifs.open(file.c_str());
Json::Reader reader;
Json::Value root;
if (!reader.parse(ifs, root))
{
return ; }
Json::Value::Members member;
for(Json::Value::iterator itr = root.begin();
itr != root.end(); ++itr){
member = (*itr).getMemberNames();
Cell cell;
cell.url = (*itr)["img"].asString();
int Pos = cell.url.find_last_of('/');
if (Pos < 0)
break;
cell.imgName = cell.url.substr(Pos+1);
cell.sound = (*itr)["sound"].asString();
cell.content = (*itr)["content"].asString();
cells.push_back(cell);
for (Json::Value::Members::iterator iter = member.begin(); iter != member.end(); iter++) {
std::
string str_temp = (*itr)[(*iter)].asString();
CCLog("%s", str_temp.c_str());
}
}
}
static void writeJson() {
using namespace std;
Json::Value root;
Json::Value arrayObj;
Json::Value item;
item["cpp"] = "jsoncpp";
item["java"] = "jsoninjava";
item["php"] = "support";
arrayObj.append(item);
root["name"] = "json";
root["array"] = arrayObj;
root.toStyledString();
std::
string out = root.toStyledString();
std::cout <<
out << std::endl;
}