#include "SecondViewControler.h"
#include "Util/UTF8ToGBK.h"
SecondViewControler::SecondViewControler() {
}
SecondViewControler::~SecondViewControler() {
}
void SecondViewControler::viewDidLoad() {
CCRect winRect = this->getView()->getBounds();
m_winSize = this->getView()->getBounds().size;
CCSize size = winRect.size;
m_Loading = CAImageView::createWithImage(CAImage::create("WaitIcon.png"));
m_Loading->setCenterOrigin(ccp(m_winSize.width*0.5, m_winSize.height*0.1));
m_Loading->setVisible(false);
this->getView()->addSubview(m_Loading);
m_showLabel = CALabel::createWithCenter(CCRect(winRect.size.width*0.5, winRect.size.height*0.2, winRect.size.width*0.9, 100 * CROSSAPP_ADPTATION_RATIO));
m_showLabel->setTextAlignment(CATextAlignmentCenter);
m_showLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
m_showLabel->setFontSize(40 * CROSSAPP_ADPTATION_RATIO);
m_showLabel->setText(UTF8ToGBK::transferToGbk("SecondViewControler").c_str());
m_showLabel->setFontName("fonts/markerfelt.ttf");
m_showLabel->setColor(CAColor_gray);
this->getView()->addSubview(m_showLabel);
CAButton* squareRectBtn = CAButton::create(CAButtonTypeSquareRect);
squareRectBtn->setCenter(CCRect(size.width*0.5, size.height*0.3, size.width*0.25, size.height*0.1));
squareRectBtn->setBackGroundViewForState(CAControlStateNormal, CAScale9ImageView::createWithImage(CAImage::create("source_material/switch_off.png")));
squareRectBtn->setBackGroundViewForState(CAControlStateHighlighted, CAScale9ImageView::createWithImage(CAImage::create("source_material/switch_on.png")));
squareRectBtn->addTarget(this, CAControl_selector(SecondViewControler::buttonClick), CAControlEventTouchUpInSide);
getView()->addSubview(squareRectBtn);
CATextField *textField = CATextField::createWithCenter(CCRect(m_winSize.width*0.5,m_winSize.height*0.8,m_winSize.width*0.5,m_winSize.height*0.1));
textField->setBackGroundImage(CAImage::create("image/btn_square_highlighted.png"));
textField->setPlaceHolder(UTF8ToGBK::transferToGbk("请输入账号").c_str());
textField->setSpaceHolderColor(CAColor_black);
textField->setFontSize(33 * CROSSAPP_ADPTATION_RATIO);
//textField->setVertMargins()
textField->setHoriMargins(11);
textField->setDelegate(this);
this->getView()->addSubview(textField);
}
void SecondViewControler::viewDidUnload() {
}
void SecondViewControler::loadingMovie(float interval) {
//CCLog("%s",__FUNCTION__);
m_Loading->setVisible(true);
float rotation = m_Loading->getRotation();
m_Loading->setRotation(rotation + 30);
}
#include <math.h>
void SecondViewControler::buttonClick(CAControl* btn, CCPoint point) {
CCLog("%s", __FUNCTION__);
//http://image.baidu.com/detail/newindex?col=%E6%98%8E%E6%98%9F&tag=%E6%9D%A8%E5%B9%82&pn=0&pid=9401487172&aid=9401487172&user_id=10086&setid=-1&sort=0&ftag1=&ftag2=&from=1
CAScheduler::schedule(schedule_selector(SecondViewControler::loadingMovie),this,0.1,false);
//高清壁纸
//http://www.08lr.cn/article/UploadPic/2010-7/win7_3.jpg
//http://u.9miao.com/a/2014-06-25/140366214486713868.png
//http://image.zcool.com.cn/2/34/1244129297491.jpg
string url[3];
url[0] = "http://www.08lr.cn/article/UploadPic/2010-7/win7_3.jpg";
url[1] = "http://image.zcool.com.cn/2/34/1244129297491.jpg";
url[2] = "http://u.9miao.com/a/2014-06-25/140366214486713868.png";
static int flag = 0;
flag++;
string url1 = url[flag % 2].c_str();
CCLog("url1:%s",url1.c_str());
CCHttpRequest *request = new CCHttpRequest();
//request->setUrl(url[2].c_str());
request->setUrl(url1.c_str());
request->setRequestType(CCHttpRequest::kHttpGet);
request->setResponseCallback(this, httpresponse_selector(SecondViewControler::requestCallBack));
CCHttpClient::getInstance()->setTimeoutForConnect(20);
CCHttpClient::getInstance()->send(request);
request->release();
}
void SecondViewControler::requestCallBack(CCHttpClient* client, CCHttpResponse* response) {
m_Loading->setVisible(false);
CAScheduler::unschedule(schedule_selector(SecondViewControler::loadingMovie), this);
CCLog("%s:%s",__FUNCTION__,UTF8ToGBK::transferToGbk("测试http请求").c_str());
if (!response->isSucceed()) {
CCLog("False");
return;
}
std::vector<char> *data = response->getResponseData();
int data_length = data->size();
/*std::string res;
for (int i = 0; i < data_length; ++i) {
res += (*data)[i];
}
res += '\0';
CCLog("data:%s",res.c_str());
string path = CCFileUtils::sharedFileUtils()->getWritablePath()+"win7_3.jpg";
CCLog("path:%s",path.c_str());
FILE *fp = fopen(path.c_str(), "wb");
//fwrite(&(*data)[0], data_length, 1, fp);
fwrite(res.c_str(), data_length, 1, fp);
fclose(fp);*///本地存储文件,image
this->getView()->removeSubviewByTag(3322);
CAImageCache::sharedImageCache()->purgeSharedImageCache();
CAImageCache::sharedImageCache()->removeUnusedImages();
//CAImage *pNew = CAImage::create(path.c_str());//本地存取
CAImage *pNew = CAImage::createWithData(&(*data)[0], data_length, "win7_3.jpg");//缓存存取image
//CAImage *pNew = CAImage::createWithData((void*)(res.c_str()), data_length, "win7_3.jpg");//缓存存取image
if (pNew != NULL) {
CAImageView *image = CAImageView::createWithImage(pNew);
image->setCenterOrigin(ccp(m_winSize.width*0.5, m_winSize.height*0.88));
image->setScale(0.5);
image->setTag(3322);
this->getView()->addSubview(image);
}
}
bool SecondViewControler::onTextFieldAttachWithIME(CATextField * sender) {
CC_UNUSED_PARAM(sender);
CCLog("%s : %s", __FUNCTION__, sender->getText().c_str());
return false;
}
bool SecondViewControler::onTextFieldDetachWithIME(CATextField * sender) {
CC_UNUSED_PARAM(sender);
CCLog("%s : %s", __FUNCTION__, sender->getText().c_str());
m_showLabel->setText(sender->getText());
return false;
}