Files
wnmj/Classes/Scenes/ChatScene.cpp
2026-02-13 14:34:15 +08:00

253 lines
5.7 KiB
C++

#include "ChatScene.h"
#include "CMD_GameServer.h"
#include "PopScene.h"
USING_NS_CC;
#define ANIM_DELAY_TIME 0.12f
ChatLayer::ChatLayer()
{
}
ChatLayer::~ChatLayer()
{
m_ChatShotList->removeAllChildren();
}
ChatLayer* ChatLayer::create(const std::vector<std::string>& chatList)
{
ChatLayer *pRet = new ChatLayer();
if (pRet && pRet->init(chatList))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
bool ChatLayer::init(const std::vector<std::string>& chatList)
{
if (!Node::init())
{
return false;
}
m_pRootNode = CSLoader::createNode("Platform/ChatScene.csb");
CC_ASSERT(m_pRootNode != nullptr);
this->addChild(m_pRootNode);
m_ImageView_BG = (ImageView*)m_pRootNode->getChildByName("ImgBg");
CC_ASSERT(m_ImageView_BG);
//获取关闭按钮;
auto btnClose = (Button*)m_ImageView_BG->getChildByName("btnClose");
CC_ASSERT(btnClose != nullptr);
//注册关闭按钮事件;
btnClose->addClickEventListener([this](Ref*){
YSAudioEngine::Instance().playBtnClickEffect();
this->popScene();
});
// 快捷语节点;
m_ShotItem = (Layout*)m_ImageView_BG->getChildByName("ShotItem");
CC_ASSERT(m_ShotItem);
m_ShotItem->setVisible(false);
// 快捷语;
m_ChatShotList = (ListView*)m_ImageView_BG->getChildByName("ListView_Com");
CC_ASSERT(m_ChatShotList);
//m_ChatShotList->setVisible(false);
// 表情;
m_Panel_Face = (Layout*)m_ImageView_BG->getChildByName("Panel_Face");
CC_ASSERT(m_Panel_Face);
// 自定义聊天发送;
m_ptxtSendInfo = (TextField*)m_ImageView_BG->getChildByName("TextFieldInfo");
CC_ASSERT(m_ptxtSendInfo);
m_ptxtSendInfo->setTextColor(Color4B(0x4D, 0x4D, 0x4D, 0xff));
m_ptxtSendInfo->setPlaceHolderColor(Color4B(0x7F, 0x7F, 0x7F, 0xff));
m_btnSend = (Button*)m_ImageView_BG->getChildByName("btnSend");
CC_ASSERT(m_btnSend);
m_btnSend->addClickEventListener(CC_CALLBACK_1(ChatLayer::onSendChatInfo, this));
InitChatShot(chatList);
return true;
}
void ChatLayer::onEnter()
{
Node::onEnter();
}
void ChatLayer::onEnterTransitionDidFinish()
{
Node::onEnterTransitionDidFinish();
InitChatFace();
}
void ChatLayer::onExit()
{
Node::onExit();
}
void ChatLayer::popScene()
{
if (m_ImageView_BG)
{
m_ImageView_BG->runAction(Sequence::create(Hide::create(), ScaleTo::create(0.1f, 0.1f), CallFunc::create([&]()
{
this->removeFromParent();
}), nullptr));
}
}
void ChatLayer::InitChatShot(const std::vector<std::string>& chatList)
{
if (m_ChatShotList && m_ShotItem)
{
m_ChatShotList->removeAllChildren();
//std::vector<std::string>& chatList = GlobalJosn::getInstance()->getChatList();
for (int i = 0; i < (int)chatList.size(); ++i)
{
std::string strShot = chatList[i];
if (strShot.size() <= 0) continue;
auto item = (Layout*)m_ShotItem->clone();
if (item == nullptr) continue;
item->setVisible(true);
auto txtShot = (Text*)item->getChildByName("txtInfo");
CC_ASSERT(txtShot);
txtShot->setString(strShot);
m_ChatShotList->pushBackCustomItem(item);
}
m_ChatShotList->addEventListener((ui::ListView::ccListViewCallback)CC_CALLBACK_2(ChatLayer::onSendChatShot, this));
}
}
void ChatLayer::InitChatFace()
{
if (m_Panel_Face)
{
for (int i = 0; i < 12; i++)
{
auto btn = (Button*)m_Panel_Face->getChildByName(StringUtils::format("btn_%d", i));
if (btn)
{
btn->addClickEventListener([i, this](Ref* ref){
YSAudioEngine::Instance().playBtnClickEffect();
// 发送表情SendUserFace
auto dispatcher = Director::getInstance()->getEventDispatcher();
EventCustom event(SEND_USER_CHAT_FACE);
CMD_GF_C_UserExpression kUserFace;
zeromemory(&kUserFace, sizeof(kUserFace));
kUserFace.isExpression = true;
kUserFace.wItemIndex = i;
event.setUserData(&kUserFace);
//派发创建游戏事件至MainScene
dispatcher->dispatchEvent(&event);
popScene();
});
}
}
}
}
void ChatLayer::onSendChatShot(Ref* ref, ListView::EventType eventType)
{
//YSAudioEngine::Instance().playBtnClickEffect();
ListView* listView = dynamic_cast<ListView*>(ref);
if (listView == nullptr || eventType != ListView::EventType::ON_SELECTED_ITEM_END)
{
return;
}
if (listView != m_ChatShotList) return;
ssize_t index = m_ChatShotList->getCurSelectedIndex();
auto selectItem = (Text*)m_ChatShotList->getItem(index);
if (nullptr == selectItem) return;
auto txtShot = (Text*)selectItem->getChildByName("txtInfo");
CC_ASSERT(txtShot);
std::string strChatShot = txtShot->getString();
cocos2d::log("%s", strChatShot.c_str());
// 发送消息
auto dispatcher = Director::getInstance()->getEventDispatcher();
EventCustom event(SEND_USER_CHAT_FACE);
CMD_GF_C_UserExpression kUserFace;
zeromemory(&kUserFace, sizeof(kUserFace));
kUserFace.isExpression = false;
kUserFace.wItemIndex = index;
event.setUserData(&kUserFace);
//派发创建游戏事件至MainScene
dispatcher->dispatchEvent(&event);
popScene();
}
void ChatLayer::onSendChatInfo(Ref* ref)
{
YSAudioEngine::Instance().playBtnClickEffect();
if (m_ptxtSendInfo == nullptr) return;
std::string strChatInfo = m_ptxtSendInfo->getString();
if (strChatInfo.size() <= 0) return;
cocos2d::log("%s", strChatInfo.c_str());
if (strlen(strChatInfo.c_str()) >= 128)
{
PopScene::Instance().show(utility::a_u8("输入信息过多,无法发送!"));
return;
}
//std::string strGbk = utility::u8_a(strChatInfo);
// 发送消息
auto dispatcher = Director::getInstance()->getEventDispatcher();
EventCustom event(SEND_USER_CHAT_TEXT);
CMD_GF_C_UserChat kUserChat;
zeromemory(&kUserChat, sizeof(kUserChat));
kUserChat.wChatLength = strlen(strChatInfo.c_str());
strcpy(kUserChat.szChatString, strChatInfo.c_str());
event.setUserData(&kUserChat);
//派发创建游戏事件至MainScene
dispatcher->dispatchEvent(&event);
popScene();
}
Animation* ChatLayer::GetAnimationFace(int index)
{
cocos2d::log("face %d", index);
return nullptr;
}