Guide.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import { TouchHelper } from "../../../before/main/TouchHelper";
  2. import { DataEventId } from "../../data/GameData";
  3. import GuideVO from "./GuideVO";
  4. /**
  5. * @description 新手引导
  6. * @author 邹勇
  7. */
  8. const { ccclass, property } = cc._decorator;
  9. @ccclass
  10. export default class Guide extends cc.Component {
  11. @property({ type: cc.Node, displayName: "显示区域" })
  12. node_display: cc.Node = null;
  13. @property({ type: cc.Node, displayName: "点击区域" })
  14. node_click: cc.Node = null;
  15. @property({ type: cc.Node, displayName: "背景" })
  16. node_bg: cc.Node = null;
  17. @property({ type: cc.Node, displayName: "对话框" })
  18. node_dialog: cc.Node = null;
  19. @property({ type: cc.Node, displayName: "手指" })
  20. node_finger: cc.Node = null;
  21. @property({ type: cc.Sprite, displayName: "头像" })
  22. node_head: cc.Sprite = null;
  23. @property({ type: cc.Node, displayName: "形象" })
  24. node_person: cc.Node = null;
  25. @property({ type: cc.RichText, displayName: "对话内容" })
  26. rich_dialog: cc.RichText = null;
  27. @property({ type: cc.RichText, displayName: "对话内容1" })
  28. rich_dialog1: cc.RichText = null;
  29. @property({ type: cc.Node, displayName: "对话内容1" })
  30. node_skip: cc.Node = null;
  31. private guides: GuideVO[];
  32. private crtStep: number;
  33. private crtGuide: GuideVO;
  34. private targetNode: cc.Node;
  35. private event_data: string;
  36. /** 对话框对齐方式 上下左右中 */
  37. private widgets: string[] = ['top', 'bottom', 'left', 'right', 'verticalCenter', 'horizontalCenter'];
  38. onLoad() {
  39. this.node_click.on(cc.Node.EventType.TOUCH_START, this.clickNodeClick, this);
  40. mk.event.register('next', this.nextStep, this);
  41. }
  42. start() {
  43. if (gData.guideData.crtID == null) {
  44. this.node.destroy();
  45. return;
  46. }
  47. this.reset();
  48. console.log(`=========${gData.guideData.crtID}`);
  49. this.guides = gData.guideData.getGuidesByID(gData.guideData.crtID);
  50. this.crtStep = -1;
  51. this.nextStep();
  52. }
  53. private async nextStep() {
  54. if (mk.guide.crtGuideID == -1) {
  55. return;
  56. }
  57. if (this.crtGuide) {
  58. if (this.crtGuide.lag_next > 0) {
  59. await mk.time.WaitForSeconds(this.crtGuide.lag_next);
  60. }
  61. if (this.crtGuide.continue == "true") {
  62. if (mk.guide.continueCallBack) {
  63. let isContinue = mk.guide.continueCallBack();
  64. if (isContinue) {
  65. --this.crtStep;
  66. }
  67. }
  68. }
  69. }
  70. this.crtStep++;
  71. this.crtGuide = this.guides[this.crtStep];
  72. mk.guide.curGuideIdStr = gData.guideData.crtID + "_" + this.crtStep;
  73. this.updateGuide();
  74. if (this.crtGuide && this.crtGuide.isShowSkip == "true") {
  75. this.node_skip.active = true;
  76. } else {
  77. this.node_skip.active = false;
  78. }
  79. mk.data.sendDataEvent(DataEventId.guide, `新手引导第${gData.guideData.crtID}_${this.crtStep}步完成`);
  80. }
  81. private updateGuide() {
  82. if (this.crtGuide == null) {
  83. this.close();
  84. }
  85. else {
  86. this.node_display.getComponent(cc.Mask).type = this.crtGuide.display_type == 0 ? cc.Mask.Type.RECT : cc.Mask.Type.ELLIPSE;
  87. this.node_bg.opacity = 180;
  88. this.node_finger.opacity = 255;
  89. this.node_dialog.opacity = 255;
  90. this.targetNode = null;
  91. this.event_data = null;
  92. let [x, y, w, h] = [null, null, null, null];
  93. //点击全屏下一步,显示区域不显示 || 点击区域下一步,并发送事件,显示区域读配置
  94. if (this.crtGuide.click_rect == "all" || this.crtGuide.click_rect.indexOf("event") != -1) {
  95. //显示区域: display_rect为""表示全屏
  96. if (this.crtGuide.display_rect.length == 0) {
  97. this.node_display.width = this.node_display.height = 0;
  98. }
  99. else {
  100. let arr = this.crtGuide.display_rect.split(":");
  101. if (arr.length != 4) {
  102. this.targetNode = cc.find("Canvas/" + this.crtGuide.display_rect);
  103. w = this.targetNode.width * this.targetNode.scaleX;
  104. h = this.targetNode.height * this.targetNode.scaleY;
  105. let pos = mk.game.getWorldPos(this.targetNode);
  106. if (this.crtGuide.displayRectOff) {
  107. x = pos.x + w * this.crtGuide.displayRectOff;
  108. }
  109. else {
  110. x = pos.x;
  111. }
  112. y = pos.y;
  113. } else {
  114. x = parseInt(arr[0]);
  115. y = parseInt(arr[1]);
  116. w = parseInt(arr[2]);
  117. h = parseInt(arr[3]);
  118. }
  119. }
  120. if (this.crtGuide.click_rect == "all") {
  121. this.node_click.x = 0;
  122. this.node_click.y = 0;
  123. this.node_click.width = this.node.width;
  124. this.node_click.height = this.node.height;
  125. }
  126. else {
  127. this.event_data = this.crtGuide.id + "_" + (this.crtStep + 1);
  128. if (this.crtGuide.btnEnable == '1') {
  129. this.node_click.width = 0;
  130. this.node_click.height = 0;
  131. }
  132. else {
  133. this.node_click.x = x;
  134. this.node_click.y = y;
  135. this.node_click.width = w;
  136. this.node_click.height = h;
  137. }
  138. }
  139. }
  140. else {//点击按钮下一步,显示区域为配置的node,点击区域=显示区域
  141. this.targetNode = cc.find("Canvas/" + this.crtGuide.click_rect);
  142. w = this.targetNode.width * this.targetNode.scaleX;
  143. h = this.targetNode.height * this.targetNode.scaleY;
  144. let pos = mk.game.getWorldPos(this.targetNode);
  145. x = pos.x;
  146. y = pos.y;
  147. // //lastStruggle
  148. // if (this.crtGuide && this.crtGuide.id == 2 && this.crtStep == 1) {
  149. // w = this.targetNode.parent.width;
  150. // h = this.targetNode.parent.height;
  151. // let pos1 = mk.game.getWorldPos(this.targetNode.parent);
  152. // x = pos1.x;
  153. // y = pos1.y - h / 2;
  154. // }
  155. if (this.crtGuide.btnEnable == '1') {
  156. //穿透点击目标节点时增加一个事件触发下一步,触发后移除
  157. this.node_click.width = this.node_click.height = 0;
  158. let eventHandler = new cc.Component.EventHandler();
  159. eventHandler.target = this.node;
  160. eventHandler.component = "Guide";
  161. eventHandler.handler = "clickNodeClick";
  162. if (this.targetNode.getComponent(cc.Button)) {
  163. this.targetNode.getComponent(cc.Button).clickEvents.push(eventHandler);
  164. }
  165. }
  166. else {
  167. this.node_click.x = 0;
  168. this.node_click.y = 0;
  169. this.node_click.width = this.node.width;
  170. this.node_click.height = this.node.height;
  171. }
  172. }
  173. //显示区域由大到小动画
  174. if (x != null && y != null && w != null && h != null) {
  175. if (this.crtGuide.display_type == 0) {//矩形选中框
  176. //先重置
  177. this.node_bg.x = this.node_bg.y = this.node_display.x = this.node_display.y = 0;
  178. this.node_display.width = this.node.width;
  179. this.node_display.height = this.node.height;
  180. //动画
  181. cc.tween(this.node_display)
  182. .to(0.2, { x: x, y: y, width: w, height: h }, {
  183. onUpdate: () => {
  184. this.node_bg.x = -this.node_display.x;
  185. this.node_bg.y = -this.node_display.y;
  186. }
  187. })
  188. .start();
  189. }
  190. else {
  191. //先重置
  192. this.node_bg.x = this.node_display.x = x;
  193. this.node_bg.y = this.node_display.y = y;
  194. this.node_display.width = this.node_display.height = this.node_bg.width = this.node_bg.height = this.node.height * 2;
  195. //动画
  196. let radius = this.crtGuide.display_type;
  197. cc.tween(this.node_display)
  198. .to(0.2, { width: radius, height: radius })
  199. .start();
  200. }
  201. }
  202. //对话框位置
  203. //全屏对齐
  204. if (this.crtGuide.dialog_pos != null && this.crtGuide.dialog_pos.length > 0) {
  205. let arr = this.crtGuide.dialog_pos.split(":");
  206. let wedgit = this.node_dialog.getComponent(cc.Widget);
  207. for (let i = 0; i < this.widgets.length; i++) {
  208. switch (i) {
  209. case 0:
  210. wedgit.isAlignTop = arr[i] != "";
  211. break;
  212. case 1:
  213. wedgit.isAlignBottom = arr[i] != "";
  214. break;
  215. case 2:
  216. wedgit.isAlignLeft = arr[i] != "";
  217. break;
  218. case 3:
  219. wedgit.isAlignRight = arr[i] != "";
  220. break;
  221. case 4:
  222. wedgit.isAlignVerticalCenter = arr[i] != "";
  223. break;
  224. case 5:
  225. wedgit.isAlignHorizontalCenter = arr[i] != "";
  226. break;
  227. }
  228. if (arr[i] != "") {
  229. wedgit[this.widgets[i]] = parseInt(arr[i]);
  230. }
  231. }
  232. wedgit.enabled = true;
  233. wedgit.updateAlignment();
  234. }
  235. else {//显示区域偏移
  236. let arr = this.crtGuide.dialog_pos1.split(",");
  237. let wedgit = this.node_dialog.getComponent(cc.Widget);
  238. wedgit.enabled = false;
  239. this.node_dialog.x = x + parseInt(arr[0]);
  240. this.node_dialog.y = y + parseInt(arr[1]);
  241. }
  242. //对话文字对齐方式
  243. let a = this.crtGuide.dialog_alignment;
  244. this.rich_dialog.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  245. this.rich_dialog1.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  246. if (this.crtGuide.finger == "") {
  247. this.node_finger.active = false;
  248. }
  249. else {
  250. this.node_finger.active = true;
  251. let arr = this.crtGuide.finger.split(":");
  252. this.node_finger.x = x + parseInt(arr[0]);
  253. this.node_finger.y = y + parseInt(arr[1]);
  254. }
  255. if (this.crtGuide.tr_form == 3 || this.crtGuide.tr_form == 4) {
  256. this.node_bg.active = false;
  257. if (this.node_click['_touchListener']) {
  258. this.node_click['_touchListener'].swallowTouches = false;
  259. }
  260. }
  261. else {
  262. this.node_bg.active = true;
  263. if (this.node_click['_touchListener']) {
  264. this.node_click['_touchListener'].swallowTouches = true;
  265. }
  266. }
  267. if (this.crtGuide.tr_form == 1 || this.crtGuide.tr_form == 3) {
  268. this.node_head.node.active = true;
  269. this.node_person.active = false;
  270. }
  271. else if (this.crtGuide.tr_form == 2 || this.crtGuide.tr_form == 4) {
  272. this.node_head.node.active = false;
  273. this.node_person.active = true;
  274. }
  275. if (mk.guide.curDes != '') {
  276. this.rich_dialog.string = mk.guide.curDes;
  277. this.rich_dialog1.string = mk.guide.curDes;
  278. mk.guide.curDes = '';
  279. }
  280. else {
  281. this.rich_dialog.string = this.crtGuide.dialog;
  282. this.rich_dialog1.string = this.crtGuide.dialog;
  283. }
  284. }
  285. }
  286. async clickNodeClick() {
  287. mk.audio.playEffect("button");
  288. cc.Tween.stopAllByTarget(this.node_display);
  289. console.log("clickNodeClick");
  290. if (this.crtGuide.btnEnable == '1') {
  291. if (this.targetNode && this.targetNode.getComponent(cc.Button)) {
  292. this.targetNode.getComponent(cc.Button).clickEvents.pop();
  293. }
  294. }
  295. if (this.event_data) {
  296. //event_guide data:1_2 (1_2为新手引导的id)
  297. mk.event.emit("event_guide", this.event_data);
  298. }
  299. this.reset();
  300. this.nextStep();
  301. }
  302. /** 等待状态 透明不可点击 */
  303. private reset() {
  304. this.node_display.width = 0;
  305. this.node_display.height = 0;
  306. this.node_bg.width = this.node.width;
  307. this.node_bg.height = this.node.height;
  308. this.node_bg.x = -this.node_display.x;
  309. this.node_bg.y = -this.node_display.y;
  310. this.node_bg.opacity = 0;
  311. this.node_click.width = 0
  312. this.node_click.height = 0;
  313. this.node_finger.opacity = 0;
  314. this.node_dialog.opacity = 0
  315. }
  316. private close() {
  317. mk.guide.close();
  318. // this.node.destroy();
  319. mk.guide.curGuideIdStr = null;
  320. if (mk.guide.continueCallBack) {
  321. mk.guide.continueCallBack = null;
  322. }
  323. console.log("closeGuide ======");
  324. mk.ui.closePanel(this.node.name);
  325. }
  326. private clickSkip() {
  327. mk.audio.playEffect("button");
  328. this.close();
  329. mk.data.sendXYEvent("guide", `skip_${gData.guideData.crtID}_${this.crtStep}`);
  330. mk.data.sendDataEvent(DataEventId.guideSkip, `新手引导第${gData.guideData.crtID}_${this.crtStep}步跳过`);
  331. }
  332. protected onDestroy(): void {
  333. this.node_click.off(cc.Node.EventType.TOUCH_START, this.clickNodeClick, this);
  334. mk.event.remove('next', this);
  335. }
  336. }