86 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-11-14 03:35:48 +08:00
import { _decorator, Component, Node } from 'cc';
2023-11-14 18:52:25 +08:00
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';
2023-11-15 02:32:00 +08:00
import { JNGLayerBase } from '../../../components/JNComponent';
import { app } from '../../../App';
2023-11-14 03:35:48 +08:00
const { ccclass, property } = _decorator;
@ccclass('MainChatView')
export class MainChatView extends JNGLayerBase {
2023-11-14 18:52:25 +08:00
@property(Node)
content:Node; //聊天内容
@property(Prefab)
chatPrefab:Prefab; //聊天预制体
@property(EditBox)
inputMessage:EditBox; //聊天输入框
onJNLoad(data?: any): void {
super.onJNLoad(data);
this.onInitUpdate();
//监听消息
2023-11-15 18:38:00 +08:00
ChatData.getIns().on(this.onMessage.bind(this),this);
2023-11-14 18:52:25 +08:00
}
onJNClose(): void {
super.onJNClose();
2023-11-15 18:38:00 +08:00
ChatData.getIns().off(this.onMessage.bind(this),this);
2023-11-14 18:52:25 +08:00
}
//初始化聊天显示
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();
})
}
2023-11-14 03:35:48 +08:00
}