UINotice.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { ImageAsset, Label, SpriteFrame, Texture2D, Toggle, assetManager, instantiate } from "cc";
  2. import { GameUILayers } from "../../scripts/GameUILayers";
  3. import { Layout_UINotice } from "./Layout_UINotice";
  4. import { ModuleDef } from "../../scripts/ModuleDef";
  5. import { LobbyMgr } from "../scripts/LobbyMgr";
  6. @tgx_class(ModuleDef.BASIC)
  7. export class UINotice extends tgx.UIController {
  8. constructor() {
  9. super("ui_notice/ui_notice", GameUILayers.POPUP, Layout_UINotice);
  10. }
  11. protected async onCreated(): Promise<void> {
  12. let layout = this._layout as Layout_UINotice;
  13. this.onButtonEvent(layout.btnClose, () => {
  14. this.close();
  15. });
  16. let tab = layout.tabs.node.children[0];
  17. layout.tabs.node.removeAllChildren();
  18. layout.emptyTips.active = true;
  19. layout.lblContent.node.active = false;
  20. layout.sprContent.node.active = false;
  21. layout.sprContent.spriteFrame = null;
  22. let ret = await LobbyMgr.inst.rpc_GetNotice();
  23. if (ret.isSucc && ret.res.noticeList.length) {
  24. layout.emptyTips.active = false;
  25. for (let i = 0; i < ret.res.noticeList.length; ++i) {
  26. let notice = ret.res.noticeList[i];
  27. let newTab = instantiate(tab);
  28. layout.tabs.node.addChild(newTab);
  29. newTab.getChildByName('Label').getComponent(Label).string = notice.title;
  30. }
  31. this.refreshContent(ret.res.noticeList[0]);
  32. }
  33. this.onToggleEvent(layout.tabs, (checkedItem: Toggle) => {
  34. let index = layout.tabs.toggleItems.indexOf(checkedItem);
  35. if (index != -1) {
  36. layout.lblContent.node.active = false;
  37. layout.sprContent.node.active = false;
  38. let notice = ret.res.noticeList[index];
  39. this.refreshContent(notice);
  40. }
  41. });
  42. }
  43. refreshContent(notice: any) {
  44. let layout = this._layout as Layout_UINotice;
  45. if (notice.contentType == 'text') {
  46. layout.lblContent.node.active = true;
  47. layout.lblContent.string = notice.content;
  48. }
  49. else if (notice.contentType == 'image') {
  50. layout.sprContent.node.active = true;
  51. assetManager.loadRemote(notice.content, ImageAsset, (err, img: ImageAsset) => {
  52. let texture = new Texture2D();
  53. texture.image = img;
  54. let spriteFrame = new SpriteFrame();
  55. spriteFrame.texture = texture;
  56. layout.sprContent.spriteFrame = spriteFrame;
  57. });
  58. }
  59. }
  60. }