LogOutView.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { utils } from "./Utils";
  2. const { ccclass, property } = cc._decorator;
  3. @ccclass
  4. export default class LogOutView extends cc.Component {
  5. scrollView: cc.ScrollView = null;
  6. content: cc.Node = null;
  7. logLabel: cc.Node = null;
  8. logArray: Array<string> = [];
  9. clearBtn: cc.Node = null;
  10. showLogViewBtn: cc.Node = null;
  11. hideLogViewBtn: cc.Node = null;
  12. onLoad() {
  13. if (utils.otherConfig && utils.otherConfig.group) {
  14. this.node.group = utils.otherConfig.group;
  15. }
  16. let ratio: number = 1;
  17. if (cc.winSize.height < cc.winSize.width) {
  18. // 横屏游戏
  19. ratio = cc.winSize.width / 1920 * 0.7;
  20. } else {
  21. ratio = cc.winSize.width / 1080;
  22. }
  23. this.node.scale = ratio;
  24. }
  25. start() {
  26. this.initUi();
  27. this.initListener();
  28. this.initData();
  29. this.schedule(() => {
  30. this.showLog();
  31. }, 0);
  32. }
  33. /**
  34. * 初始化UI
  35. */
  36. protected initUi(): void {
  37. this.scrollView = this.node.getChildByName("ScrollView").getComponent(cc.ScrollView);
  38. this.showLogViewBtn = this.node.getChildByName("BtnShowLogView");
  39. this.hideLogViewBtn = this.node.getChildByName("BtnHideLogView");
  40. this.clearBtn = this.node.getChildByName("BtnClearLog");
  41. this.content = this.scrollView.content;
  42. this.logLabel = this.content.children[0];
  43. this.content.removeAllChildren();
  44. }
  45. /**
  46. * 添加LOG输出
  47. * @param logContent log
  48. */
  49. public addLog(logContent: any, ...optionalParams: any[]): void {
  50. let str = "";
  51. str += logContent;
  52. optionalParams.forEach(element => {
  53. str += "," + element;
  54. });
  55. this.logArray.push(str);
  56. }
  57. public showLog() {
  58. if (this.logArray.length > 0) {
  59. let tempAry = this.logArray;
  60. this.logArray = [];
  61. tempAry.forEach(log => {
  62. let tempLogLabel = cc.instantiate(this.logLabel);
  63. tempLogLabel.getComponent(cc.Label).string = `日志输出:${log}`;
  64. this.content.addChild(tempLogLabel);
  65. });
  66. }
  67. }
  68. /**
  69. * 初始化监听事件
  70. */
  71. protected initListener(): void {
  72. }
  73. /**
  74. * 初始化数据
  75. */
  76. protected initData(): void {
  77. }
  78. /**
  79. * 显示日志框
  80. */
  81. onShowLogView() {
  82. this.scrollView.node.active = true;
  83. this.clearBtn.active = true;
  84. this.showLogViewBtn.active = false;
  85. this.hideLogViewBtn.active = true;
  86. }
  87. /**
  88. * 隐藏日志框
  89. */
  90. onHideLogView() {
  91. this.scrollView.node.active = false;
  92. this.clearBtn.active = false;
  93. this.showLogViewBtn.active = true;
  94. this.hideLogViewBtn.active = false;
  95. }
  96. /**
  97. * 清空日志
  98. */
  99. clearLogView() {
  100. this.content.removeAllChildren();
  101. }
  102. // update (dt) {}
  103. }