Guide.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import GuideVO from "./GuideVO";
  2. /**
  3. * @description 新手引导
  4. * @author 邹勇
  5. */
  6. const { ccclass, property } = cc._decorator;
  7. @ccclass
  8. export default class Guide extends cc.Component {
  9. @property({ type: cc.Node, displayName: "显示区域" })
  10. node_display: cc.Node = null;
  11. @property({ type: cc.Node, displayName: "点击区域" })
  12. node_click: cc.Node = null;
  13. @property({ type: cc.Node, displayName: "背景" })
  14. node_bg: cc.Node = null;
  15. @property({ type: cc.Node, displayName: "对话框" })
  16. node_dialog: cc.Node = null;
  17. @property({ type: cc.Sprite, displayName: "手指" })
  18. node_finger: cc.Sprite = null;
  19. @property({ type: cc.Sprite, displayName: "头像" })
  20. node_head: cc.Sprite = null;
  21. @property({ type: cc.Sprite, displayName: "形象" })
  22. node_person: cc.Sprite = null;
  23. @property({ type: cc.RichText, displayName: "对话内容" })
  24. rich_dialog: cc.RichText = null;
  25. private guides: GuideVO[];
  26. private crtStep: number;
  27. private crtGuide: GuideVO;
  28. private targetNode: cc.Node;
  29. private event_name: string;
  30. /** 对话框对齐方式 上下左右中 */
  31. private widgets: string[] = ['top', 'bottom', 'left', 'right', 'verticalCenter', 'horizontalCenter'];
  32. onLoad() {
  33. }
  34. start() {
  35. return;
  36. if (gData.guideData.crtID == null) {
  37. this.node.destroy();
  38. return;
  39. }
  40. this.guides = gData.guideData.getGuidesByID(gData.guideData.crtID);
  41. this.crtStep = -1;
  42. this.nextStep();
  43. }
  44. private nextStep() {
  45. this.crtStep++;
  46. this.crtGuide = this.guides[this.crtStep];
  47. this.updateGuide();
  48. }
  49. private updateGuide() {
  50. if (this.crtGuide == null) {
  51. this.close();
  52. }
  53. else {
  54. this.targetNode = null;
  55. this.event_name = null;
  56. //点击全屏下一步,显示区域不显示,点击区域全屏
  57. if (this.crtGuide.click_rect == "all") {
  58. this.node_display.width = 0;
  59. this.node_display.height = 0;
  60. this.node_click.x = 0;
  61. this.node_click.y = 0;
  62. this.node_click.width = this.node.width;
  63. this.node_click.height = this.node.height;
  64. }
  65. else if (this.crtGuide.click_rect.indexOf("event") != -1) {//点击区域下一步,并发送事件,显示区域读配置,点击区域=显示区域
  66. this.event_name = "event_guide_" + this.crtGuide.id + "_" + this.crtStep;
  67. let arr = this.crtGuide.display_rect.split(":");
  68. this.node_display.x = this.node_click.x = parseInt(arr[0]);
  69. this.node_display.y = this.node_click.y = parseInt(arr[1]);
  70. this.node_display.width = this.node_click.width = parseInt(arr[2]);
  71. this.node_display.height = this.node_click.height = parseInt(arr[3]);
  72. }
  73. else {//点击按钮下一步,显示区域为配置的node,点击区域=显示区域
  74. this.targetNode = cc.find("Canvas/" + this.crtGuide.click_rect);
  75. this.node_display.width = this.targetNode.width;
  76. this.node_display.height = this.targetNode.height;
  77. let pos = mk.game.getWorldPos(this.targetNode);
  78. this.node_display.setPosition(pos);
  79. }
  80. //对话框位置
  81. ////全屏对齐
  82. if (this.crtGuide.dialog_pos != null && this.crtGuide.dialog_pos.length > 0) {
  83. let arr = this.crtGuide.dialog_pos.split(":");
  84. let wedgit = this.node_dialog.getComponent(cc.Widget);
  85. for (let i = 0; i < this.widgets.length; i++) {
  86. wedgit[this.widgets[i]] = arr[i] == "" ? null : arr[i];
  87. }
  88. }
  89. else {//显示区域偏移
  90. let arr = this.crtGuide.dialog_pos1.split(":");
  91. this.node_dialog.x = this.node_display.x + parseInt(arr[0]);
  92. this.node_dialog.y = this.node_display.y + parseInt(arr[1]);
  93. }
  94. //对话文字对齐方式
  95. let a = this.crtGuide.dialog_alignment;
  96. this.rich_dialog.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  97. if (this.crtGuide.finger == "") {
  98. this.node_finger.node.active = false;
  99. }
  100. else {
  101. this.node_finger.node.active = true;
  102. let arr = this.crtGuide.finger.split(":");
  103. this.node_finger.node.x = this.node_display.x + parseInt(arr[0]);
  104. this.node_finger.node.y = this.node_display.y + parseInt(arr[1]);
  105. }
  106. if (this.crtGuide.tr_form == 1) {
  107. this.node_head.node.active = true;
  108. this.node_person.node.active = false;
  109. }
  110. else {
  111. this.node_head.node.active = false;
  112. this.node_person.node.active = true;
  113. }
  114. this.node_bg.x = -this.node_display.x;
  115. this.node_bg.y = -this.node_display.y;
  116. this.rich_dialog.string = `<color=#000000>${this.crtGuide.dialog}</color>`;
  117. }
  118. }
  119. async clickNodeClick() {
  120. console.log("clickNodeClick");
  121. if (this.targetNode) {
  122. this.targetNode.getComponent(cc.Button).clickEvents[0].emit([]);
  123. }
  124. if (this.event_name) {
  125. //event_guide_1_2 (1_2为新手引导的id)
  126. mk.event.emit(this.event_name);
  127. }
  128. if (this.crtGuide.lag_next > 0) {
  129. await mk.time.WaitForSeconds(this.crtGuide.lag_next);
  130. }
  131. this.nextStep();
  132. }
  133. private close() {
  134. mk.guide.close();
  135. this.node.destroy();
  136. }
  137. }