import NetUtil from "../utils/NetUtil" ; import { User , Message } from "../objs/Params" ; import GameUtil from "../utils/GameUtil" ; import UserNode from "../nodes/UserNode" ; import Toast from "../utils/Toast" ; const { ccclass , property } = cc . _decorator ; @ ccclass export default class ChatCtrl extends cc . Component { @ property ( cc . Node ) userContent : cc . Node = null ; @ property ( cc . Prefab ) userItem : cc . Prefab = null ; @ property ( cc . Node ) msgContent : cc . Node = null ; @ property ( cc . Prefab ) otherMsgItem : cc . Prefab = null ; @ property ( cc . Prefab ) selfMsgItem : cc . Prefab = null ; @ property ( cc . EditBox ) sendAllBox : cc . EditBox = null ; @ property ( cc . EditBox ) sendOneBox : cc . EditBox = null ; @ property ( cc . ScrollView ) msgScrollView : cc . ScrollView = null ; @ property ( cc . Node ) singleBox : cc . Node = null ; @ property ( cc . Label ) userInfoLabel : cc . Label = null ; userList : Array < User > = [ ] ; singleUser : User = null ; onLoad ( ) { NetUtil . Instance . on ( 'loginInfo' , ( msg ) => { cc . log ( msg ) ; Toast . makeText ( msg , Toast . LENGTH_LONG ) . show ( ) ; } ) ; NetUtil . Instance . on ( 'userInfo' , ( user ) => { GameUtil . Instance . userInfo = user ; cc . log ( user ) ; this . userInfoLabel . string = `当前用户是：${ user . name }  当前所处的频道是:${ user . channel }` ; } ) ; NetUtil . Instance . on ( 'userList' , ( userList ) => { cc . find ( 'Canvas/login_room' ) . active = false ; this . userList = userList ; this . updateUserList ( this . userList ) ; } ) ; NetUtil . Instance . on ( 'toAll' , ( msg ) => { let node = cc . instantiate ( this . otherMsgItem ) ; node . getChildByName ( 'name' ) . getComponent ( cc . Label ) . string = msg . from . name ; node . getChildByName ( 'msgBox' ) . getChildByName ( 'msg' ) . getComponent ( cc . Label ) . string = msg . msg ; this . msgContent . addChild ( node ) ; if ( this . msgContent . height > 480 ) { this . msgScrollView . scrollToBottom ( 0.3 ) ; } } ) ; NetUtil . Instance . on ( 'toOne' , ( msg ) => { let node = cc . instantiate ( this . otherMsgItem ) ; node . getChildByName ( 'name' ) . getComponent ( cc . Label ) . string = msg . from . name ; node . getChildByName ( 'msgBox' ) . getChildByName ( 'msg' ) . getComponent ( cc . Label ) . string = msg . msg ; node . getChildByName ( 'msgBox' ) . getChildByName ( 'msg' ) . color = cc . Color . RED ; this . msgContent . addChild ( node ) ; if ( this . msgContent . height > 480 ) { this . msgScrollView . scrollToBottom ( 0.3 ) ; } } ) ; } updateUserList ( userList ) { this . userContent . removeAllChildren ( ) ; userList . forEach ( user => { let node = cc . instantiate ( this . userItem ) ; node . getComponent ( UserNode ) . init ( user ) ; node . getChildByName ( 'name' ) . getComponent ( cc . Label ) . string = user . name ; this . userContent . addChild ( node ) ; } ) ; } sendToAll ( ) { let str = this . sendAllBox . string ; if ( str . length < 1 ) { return ; } this . sendAllBox . string = "" ; let node = cc . instantiate ( this . selfMsgItem ) ; node . getChildByName ( 'name' ) . getComponent ( cc . Label ) . string = GameUtil . Instance . userInfo . name ; node . getChildByName ( 'msgBox' ) . getChildByName ( 'msg' ) . getComponent ( cc . Label ) . string = str ; this . msgContent . addChild ( node ) ; let msg = { from : GameUtil . Instance . userInfo , msg : str } ; NetUtil . Instance . emit ( 'toAll' , msg ) ; if ( this . msgContent . height > 480 ) { this . msgScrollView . scrollToBottom ( 0.3 ) ; } } sendToOne ( ) { let str = this . sendOneBox . string ; if ( str . length < 1 ) { return ; } this . sendOneBox . string = "" ; let node = cc . instantiate ( this . selfMsgItem ) ; node . getChildByName ( 'name' ) . getComponent ( cc . Label ) . string = GameUtil . Instance . userInfo . name ; node . getChildByName ( 'msgBox' ) . getChildByName ( 'msg' ) . getComponent ( cc . Label ) . string = str ; this . msgContent . addChild ( node ) ; let msg = { from : GameUtil . Instance . userInfo , msg : str , to : this . singleUser . id } ; NetUtil . Instance . emit ( 'toOne' , msg ) ; if ( this . msgContent . height > 480 ) { this . msgScrollView . scrollToBottom ( 0.3 ) ; } this . singleBox . active = false ; } showSingleBox ( user ) { this . singleUser = user ; this . singleBox . getChildByName ( 'board' ) . getChildByName ( 'to' ) . getComponent ( cc . Label ) . string = user . name ; this . singleBox . active = true ; } }