下载图片放到纹理缓存中

23 篇文章 1 订阅
#include "GameMingpian.h"
#include "LibHNLobby/HNLobbyExport.h"

static const char* CREATE_NAME_INFO = "platform/setCareateNameInfo/createInfoName.csb";
static const char* MING_PIAN = "platform/setCareateNameInfo/mingPian.csb";
GameMingPianLayer::GameMingPianLayer()
{

}

GameMingPianLayer::~GameMingPianLayer()
{
	HNHttpRequest::getInstance()->removeObserver(this);
	Director::getInstance()->getEventDispatcher()->removeEventListener(_listenr);
}

bool GameMingPianLayer::init()
{
	if (!HNLayer::init())
	{
		return false;
	}

	_winSize = Director::getInstance()->getWinSize();

	Size winSize = Director::getInstance()->getWinSize();

	auto node = CSLoader::createNode(CREATE_NAME_INFO);
	node->setIgnoreAnchorPointForPosition(false);
	node->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
	node->setPosition(winSize/2);
	addChild(node);

	_createNameInfo._uiMain = dynamic_cast<ImageView*>(node->getChildByName("Image_MainUI"));
	
	_createNameInfo._frindNum = dynamic_cast<Text*>(_createNameInfo._uiMain->getChildByName("Text_friendNum_1"));
	_createNameInfo._rewardNum = dynamic_cast<Text*>(_createNameInfo._uiMain->getChildByName("Text_reward_1"));
	_createNameInfo._Id = dynamic_cast<Text*>(_createNameInfo._uiMain->getChildByName("Text_id_1"));
	_createNameInfo._youke = dynamic_cast<Text*>(_createNameInfo._uiMain->getChildByName("Text_nicheng_1"));

	_createNameInfo._btnGet = dynamic_cast<Button*>(_createNameInfo._uiMain->getChildByName("Button_get"));
	_createNameInfo._btnCreate = dynamic_cast<Button*>(_createNameInfo._uiMain->getChildByName("Button_createM"));
	_createNameInfo._btnTuijian = dynamic_cast<Button*>(_createNameInfo._uiMain->getChildByName("Button_tuijian"));
	_createNameInfo._btnExit = dynamic_cast<Button*>(_createNameInfo._uiMain->getChildByName("Button_exit"));

	_createNameInfo._btnGet->addTouchEventListener(CC_CALLBACK_2(GameMingPianLayer::btnCallBack, this));
	_createNameInfo._btnCreate->addTouchEventListener(CC_CALLBACK_2(GameMingPianLayer::btnCallBack, this));
	_createNameInfo._btnTuijian->addTouchEventListener(CC_CALLBACK_2(GameMingPianLayer::btnCallBack, this));
	_createNameInfo._btnExit->addTouchEventListener(CC_CALLBACK_2(GameMingPianLayer::btnCallBack, this));


	createMp();
	//查询佣金奖励
	checkInfo();
}

void GameMingPianLayer::onHttpResponse(const std::string& requestName, bool isSucceed, const std::string &responseData)
{
	if (requestName.compare("getgood") == 0)
	{
		if (!isSucceed) return;
		rapidjson::Document doc;
		doc.Parse<rapidjson::kParseDefaultFlags>(responseData.c_str());
		// 解析结果
		if (doc.HasParseError())
		{
			return;
		}

		int rs = doc["rs"].GetInt();
		if (rs == 1)
		{
			if (doc.HasMember("RecommendMoney"))
			{
				int moneyRe = doc["RecommendMoney"].GetInt();  //该玩家领取的推荐奖励数
				if (moneyRe > 0)
				GamePromptLayer::create()->showPrompt(GBKToUtf8(StringUtils::format("恭喜玩家获取推荐奖励数: %d", moneyRe)));
			}
			if (doc.HasMember("WalletMoney"))
			{
				int money = doc["WalletMoney"].GetInt();  //领取之后玩家钱包金币数
				PlatformLogic()->loginResult.i64Money = money;

			}
		}
		
		
	}
	if (requestName.compare("erweima") == 0)
	{
		if (!isSucceed) return;
		rapidjson::Document doc;
		doc.Parse<rapidjson::kParseDefaultFlags>(responseData.c_str());
		// 解析结果
		if (doc.HasParseError())
		{
			return;
		}
		int rs = doc["rs"].GetInt();
		if (rs == 1)
		{
			if (doc.HasMember("QRCodeUrl"))
			{
				string erWeima = doc["QRCodeUrl"].GetString();  //二维码图片地址
				loldTextEweima(erWeima);
			}
		}
	}
	if (requestName.compare("checkInfo") == 0)
	{
		if (!isSucceed) return;
		rapidjson::Document doc;
		doc.Parse<rapidjson::kParseDefaultFlags>(responseData.c_str());
		// 解析结果
		if (doc.HasParseError())
		{
			return;
		}
		int rs = doc["rs"].GetInt();
		if (rs == 1)
		{
			if (doc.HasMember("RecommendNum"))
			{
				int renNum = doc["RecommendNum"].GetInt(); //返回该用户已推荐总人数
				//if (renNum > 0)
				{
					_createNameInfo._frindNum->setString(StringUtils::format("%d", renNum));
				}
				
			}
			if (doc.HasMember("RecommendMoney"))
			{
				int renNumRew = doc["RecommendMoney"].GetInt();//返回该用户已推荐获得的奖励总数
				_createNameInfo._rewardNum->setString(StringUtils::format("%d", renNumRew));
				if (renNumRew <= 0)
				{
					_createNameInfo._btnGet->setEnabled(false);
					_createNameInfo._btnGet->setBright(false);
				}
				else
				{
					_createNameInfo._btnGet->setEnabled(true);
					_createNameInfo._btnGet->setBright(true);
				}
				
			}
			//if (doc.HasMember("RecommendMoney"))
			//{
			//	int xianzhishu = doc["RecommendMoney"].GetInt();//返回推荐限制人数
			//	
			//}
			//if (doc.HasMember("RecommendMoneySet"))
			//{
			//	int erWeima = doc["RecommendMoneySet"].GetInt();//返回推荐成功奖励数
			//}
		}

	}
	if(requestName.compare("loadErweima") == 0)
	{
		if (!isSucceed)
		{
			return;
		}

		//create image
		Image* image = new Image;
		bool isOk = image->initWithImageData((unsigned char*)responseData.data(), responseData.size());
		if (!isOk)
		{
			log("Image init fail!");
			return;
		}
		std::string str = StringUtils::format("erweimapng");
		Texture2D* texture = Director::getInstance()->getTextureCache()->addImage(image, str);
		if (texture == nullptr)
		{
			log("texture init fail!");
			return;
		}
		image->release();
		auto sf = SpriteFrame::createWithTexture(texture, Rect(Vec2::ZERO, texture->getContentSize()));
		SpriteFrameCache::getInstance()->addSpriteFrame(sf, str);
		_mingPian._erweima->loadTexture(str, Widget::TextureResType::PLIST);
		_mingPian._erweima->setVisible(true);
		addTouch();
	}
	
}

void GameMingPianLayer::btnCallBack(Ref* pSender, Widget::TouchEventType type)
{
	if (Widget::TouchEventType::ENDED == type)
	{
		Button* btn = (Button*)pSender;
		if (btn->getName().compare("Button_get") == 0)
		{
			//领取奖励(查询奖励)
			std::string param = StringUtils::format("type=ReceiveRecommend&userid=%d", PlatformLogic()->loginResult.dwUserID);
			HNHttpRequest::getInstance()->request("getgood", network::HttpRequest::Type::POST, PlatformConfig::getInstance()->getErweima(), param);
			HNHttpRequest::getInstance()->addObserver(this);

		}
		if (btn->getName().compare("Button_createM") == 0)
		{
			//创建名片
			//领取奖励(查询奖励)
			std::string param = StringUtils::format("type=GetUserFxQRCode&userid=%d", PlatformLogic()->loginResult.dwUserID);
			HNHttpRequest::getInstance()->request("erweima", network::HttpRequest::Type::POST, PlatformConfig::getInstance()->getErweima(), param);
			HNHttpRequest::getInstance()->addObserver(this);
		}
		if (btn->getName().compare("Button_tuijian") == 0)
		{
			//分享
			std::string _shareContent = "";

			UMengSocial::getInstance()->doShare(_shareContent, GBKToUtf8("分享消息"), "");
		}
		if (btn->getName().compare("Button_exit") == 0)
		{
			if (onCloseCallBack)
			onCloseCallBack();

		}
	}
}



void GameMingPianLayer::createMp()
{
	_winSize = Director::getInstance()->getWinSize();
	Size winSize = Director::getInstance()->getWinSize();
	auto node = CSLoader::createNode(MING_PIAN);
	node->setPosition(winSize / 2);

	addChild(node, 3);

	_mingPian._UiImg = dynamic_cast<ImageView*>(node->getChildByName("Image_UIMain"));
	_mingPian._UiImg->setVisible(false);
	_mingPian._UiImg->setTouchEnabled(false);
	_mingPian._erweima = dynamic_cast<ImageView*>(_mingPian._UiImg->getChildByName("Image_erweima"));	
}

void GameMingPianLayer::loldTextEweima(std::string str)
{
	HNHttpRequest::getInstance()->request("loadErweima", HttpRequest::Type::GET, str);
}

void GameMingPianLayer::checkInfo()
{
	std::string param = StringUtils::format("type=GetRecommendConfig&userid=%d", PlatformLogic()->loginResult.dwUserID);
	HNHttpRequest::getInstance()->request("checkInfo", network::HttpRequest::Type::POST, PlatformConfig::getInstance()->getErweima(), param);
	HNHttpRequest::getInstance()->addObserver(this);
}

void GameMingPianLayer::addTouch()
{
	_listenr = EventListenerTouchOneByOne::create();
	_listenr->onTouchBegan = [=](Touch* touch, Event* event)->bool{
		Rect img(0, 0, _mingPian._UiImg->getContentSize().width, _mingPian._UiImg->getContentSize().height);  //	Rect rect(0, 0, this->getContentSize().width, this->getContentSize().height);
		auto pos = touch->getLocation();
		pos = this->convertToNodeSpace(pos);
		//if (img.containsPoint(pos))
		{
			//将图片保存到相册中
			GamePromptLayer::create()->showPrompt(GBKToUtf8("图片将保存到手机相册中..."));
			_mingPian._UiImg->runAction(Sequence::create(DelayTime::create(1), CallFunc::create([=](){ _mingPian._UiImg->setVisible(false); _mingPian._UiImg->setTouchEnabled(false); }), NULL));
			//Director::getInstance()->getEventDispatcher()->removeEventListener(tlisten);
		}
		_listenr->onTouchMoved = [](Touch* touch, Event* event){

		};
		Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_listenr, _mingPian._UiImg);
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间溜走了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值