mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 03:14:47 +00:00
86 lines
2.1 KiB
TypeScript
86 lines
2.1 KiB
TypeScript
import { _decorator, Component, Node } from 'cc';
|
|
import { Prefab } from 'cc';
|
|
import ChatData from '../../../data/ChatData';
|
|
import { instantiate } from 'cc';
|
|
import { Label } from 'cc';
|
|
import { EditBox } from 'cc';
|
|
import { GUI } from '../../UIConfig';
|
|
import { GUIChatMessage } from '../../../consts/GActionType';
|
|
import { Widget } from 'cc';
|
|
import JScrollExceedHide from '../../../../../extensions/ngame/assets/ngame/util/components/JScrollExceedHide';
|
|
import { JNGLayerBase } from '../../../components/JNComponent';
|
|
import { app } from '../../../App';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass('MainChatView')
|
|
export class MainChatView extends JNGLayerBase {
|
|
|
|
@property(Node)
|
|
content:Node; //聊天内容
|
|
|
|
@property(Prefab)
|
|
chatPrefab:Prefab; //聊天预制体
|
|
|
|
@property(EditBox)
|
|
inputMessage:EditBox; //聊天输入框
|
|
|
|
onJNLoad(data?: any): void {
|
|
|
|
super.onJNLoad(data);
|
|
this.onInitUpdate();
|
|
|
|
//监听消息
|
|
ChatData.getIns().on(this.onMessage.bind(this));
|
|
|
|
}
|
|
|
|
onJNClose(): void {
|
|
super.onJNClose();
|
|
ChatData.getIns().off(this.onMessage.bind(this));
|
|
}
|
|
|
|
//初始化聊天显示
|
|
onInitUpdate(){
|
|
|
|
this.content.destroyAllChildren();
|
|
let messages = ChatData.getIns().datas;
|
|
|
|
messages.forEach(message => {
|
|
let node = instantiate(this.chatPrefab);
|
|
node.getComponent(Label).string = message;
|
|
this.content.addChild(node);
|
|
})
|
|
|
|
}
|
|
|
|
//发送消息
|
|
onClickSendMessage(){
|
|
|
|
if(!this.inputMessage.string){
|
|
app.layer.Open(GUI.Tips,{text:"请输入内容"})
|
|
return;
|
|
}
|
|
|
|
ChatData.getIns().onSend({
|
|
message:this.inputMessage.string
|
|
});
|
|
|
|
}
|
|
|
|
//接受到消息
|
|
onMessage(info:GUIChatMessage){
|
|
|
|
//插入数据
|
|
let node = instantiate(this.chatPrefab);
|
|
node.getComponent(Label).string = info.message;
|
|
this.content.addChild(node);
|
|
this.scheduleOnce(() => {
|
|
this.getComponentInChildren(JScrollExceedHide).onUpdate();
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|