UIChat.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { director, instantiate, RichText, Node, Widget } from "cc";
  2. import { GameUILayers } from "../../scripts/GameUILayers";
  3. import { Layout_UIChat } from "./Layout_UIChat";
  4. import { RoomEvent, RoomMgr } from "../scripts/RoomMgr";
  5. import { MsgChat } from "../shared/protocols/public/chat/MsgChat";
  6. import { getSubGameConf } from "../scripts/SubGameDef";
  7. import { ModuleDef } from "../../scripts/ModuleDef";
  8. import { MsgUserComesToRoomPush } from "../shared/protocols/public/room/MsgUserComesToRoomPush";
  9. import { MsgUserLeavesFromRoomPush } from "../shared/protocols/public/room/MsgUserLeavesFromRoomPush";
  10. import { NetGameServer } from "../scripts/NetGameServer";
  11. @tgx_class(ModuleDef.BASIC)
  12. export class UIChat extends tgx.UIController {
  13. private static _chatItemPool: Node[] = [];
  14. private getFromPool() {
  15. let layout = this._layout as Layout_UIChat;
  16. let item = UIChat._chatItemPool.shift();
  17. if (!item) {
  18. item = instantiate(layout.prefabChatMsgItem);
  19. }
  20. layout.chatMsgs.addChild(item);
  21. return item;
  22. }
  23. private putToPool(item: Node) {
  24. item.removeFromParent();
  25. UIChat._chatItemPool.push(item);
  26. }
  27. private _isFolded = true;
  28. private _chatNet:NetGameServer;
  29. constructor() {
  30. super('ui_chat/ui_chat', GameUILayers.HUD, Layout_UIChat);
  31. }
  32. protected onCreated(net:NetGameServer): void {
  33. this._chatNet = net;
  34. this._chatNet.listenMsg('chat/Chat', this.addChatMsg, this);
  35. director.on(RoomEvent.NEW_USER_COMES, this.onUserComes,this);
  36. director.on(RoomEvent.USER_LEAVES, this.onUserLeaves,this);
  37. let layout = this._layout as Layout_UIChat;
  38. layout.cbInputChatReturn = this.onInputChatReturn.bind(this);
  39. layout.cbBtnSendChat = this.onBtnSendChat.bind(this);
  40. layout.btnExpand.node.active = true;
  41. layout.btnFold.node.active = false;
  42. let w = layout.chatMsgs.getComponent(Widget);
  43. w.bottom = 15;
  44. w.left = 10;
  45. layout.chatBar.active = false;
  46. this._isFolded = true;
  47. this.onButtonEvent(layout.btnExpand, () => {
  48. this._isFolded = false;
  49. layout.btnExpand.node.active = false;
  50. layout.btnFold.node.active = true;
  51. layout.chatBar.active = true;
  52. let w = layout.chatMsgs.getComponent(Widget);
  53. w.bottom = 110;
  54. w.left = 10;
  55. });
  56. this.onButtonEvent(layout.btnFold, () => {
  57. this._isFolded = true;
  58. layout.btnExpand.node.active = true;
  59. layout.btnFold.node.active = false;
  60. layout.chatBar.active = false;
  61. let w = layout.chatMsgs.getComponent(Widget);
  62. w.bottom = 15;
  63. w.left = 10;
  64. });
  65. let messages;// = SubGameMgr.gameMgr.data?.messages;
  66. if (messages && messages.length) {
  67. for (let i = 0; i < messages.length; ++i) {
  68. let msg = messages[i];
  69. this.addChatMsg(msg);
  70. }
  71. }
  72. }
  73. async onUserComes(v:MsgUserComesToRoomPush) {
  74. let info = RoomMgr.inst.getUser(v.uid);
  75. this._pushChatMsg(`<outline width=1 color=#0><color=#33A8E9>${info?.name}(${info?.uid})</color> <color=#999999>加入了房间</color></o>`);
  76. }
  77. async onUserLeaves(v:MsgUserLeavesFromRoomPush) {
  78. let info = RoomMgr.inst.getUser(v.uid);
  79. this._pushChatMsg(`<outline width=1 color=#0><color=#33A8E9>${info?.name}(${info?.uid})</color> <color=#999999>离开了房间</color></o>`);
  80. }
  81. addChatMsg(msg: MsgChat) {
  82. let str = '';
  83. if (msg.channel == 'global') {
  84. str = '[全部]';
  85. }
  86. else if (msg.channel) {
  87. str = `[${getSubGameConf(msg.channel).name || ''}]`;
  88. }
  89. this._pushChatMsg(`<outline width=1 color=#0><color=#33A8E9>${str}${msg.user.name}(${msg.user.uid})</color> <color=#FFFFFF>${msg.content}</color></o>`);
  90. }
  91. protected onDispose(): void {
  92. this._chatNet.unlistenMsg('chat/Chat', this.addChatMsg, this);
  93. director.off(RoomEvent.NEW_USER_COMES, this.onUserComes,this);
  94. director.off(RoomEvent.USER_LEAVES, this.onUserLeaves,this);
  95. }
  96. async onBtnSendChat() {
  97. let layout = this._layout as Layout_UIChat;
  98. if (!layout.inputChat.string) {
  99. return;
  100. }
  101. let content = layout.inputChat.string;
  102. let channel = undefined;
  103. let index = content.indexOf(' ');
  104. if (index != -1) {
  105. let headStr = content.substring(0, index);
  106. if (headStr == '@全部' || headStr == '@g') {
  107. channel = 'global';
  108. }
  109. content = content.substring(index + 1);
  110. }
  111. if (!channel && director.getScene().name.indexOf('lobby') == 0) {
  112. channel = 'lobby';
  113. }
  114. layout.inputChat.string = '';
  115. let ret = await this._chatNet.callApi('chat/SendChat', {
  116. channel: channel,
  117. content: content
  118. });
  119. layout.inputChat.string = '';
  120. if (!ret.isSucc) {
  121. this._pushChatMsg(`<color=#999999>消息发送失败,请重试!</color></o>`);
  122. layout.inputChat.string = content;
  123. return;
  124. }
  125. }
  126. async onInputChatReturn() {
  127. let layout = this._layout as Layout_UIChat;
  128. await this.onBtnSendChat();
  129. layout.inputChat.focus();
  130. }
  131. private _pushChatMsg(richText: string) {
  132. if (this._destroyed) {
  133. console.log('UIChat._pushChatMsg: destroyed');
  134. return;
  135. }
  136. let layout = this._layout as Layout_UIChat;
  137. const maxLen = this._isFolded ? 2 : 7;
  138. // 最多保留 7 条记录
  139. while (layout.chatMsgs.children.length >= maxLen) {
  140. this.putToPool(layout.chatMsgs.children[0]);
  141. }
  142. let node = this.getFromPool();
  143. node.getComponent(RichText)!.string = richText;
  144. }
  145. }