UIAnnouncement.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { UITransform, isValid } from "cc";
  2. import { GameUILayers } from "../../scripts/GameUILayers";
  3. import { Layout_UIAnnouncement } from "./Layout_UIAnnouncement";
  4. import { GameSceneUtil } from "../scripts/GameSceneUtil";
  5. import { ModuleDef } from "../../scripts/ModuleDef";
  6. import { LobbyMgr } from "../scripts/LobbyMgr";
  7. @tgx_class(ModuleDef.BASIC)
  8. export class UIAnnouncement extends tgx.UIController {
  9. constructor() {
  10. super('ui_announcement/ui_announcement', GameUILayers.HUD, Layout_UIAnnouncement);
  11. }
  12. private _maskWidth = 0;
  13. private _contentWidth = 0;
  14. protected async onCreated(): Promise<void> {
  15. let ret = await LobbyMgr.inst.rpc_GetAnnouncement('lobby');
  16. if (!isValid(this.node)) {
  17. return;
  18. }
  19. let layout = this._layout as Layout_UIAnnouncement;
  20. let pos = layout.maskNode.position;
  21. this._maskWidth = layout.maskNode.getComponent(UITransform).width;
  22. layout.lblContent.node.setPosition(this._maskWidth / 2, pos.y, pos.z);
  23. if (ret.isSucc) {
  24. layout.lblContent.string = ret.res.content;
  25. }
  26. else {
  27. layout.lblContent.string = 'rpc_GetAnnouncement Failed';
  28. }
  29. layout.lblContent.updateRenderData();
  30. this._contentWidth = layout.lblContent.getComponent(UITransform).width;
  31. }
  32. setPosition(x: number, y: number) {
  33. this.node.setPosition(x, y, 0);
  34. }
  35. protected onUpdate(dt: number): void {
  36. let layout = this._layout as Layout_UIAnnouncement;
  37. if (layout) {
  38. let pos = layout.lblContent.node.position;
  39. if (this._contentWidth && pos.x + this._contentWidth < -this._maskWidth / 2) {
  40. layout.lblContent.node.setPosition(this._maskWidth / 2, pos.y, pos.z);
  41. }
  42. else {
  43. layout.lblContent.node.setPosition(pos.x - dt * 200, pos.y, pos.z);
  44. }
  45. }
  46. }
  47. }