Guide.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. console.log("===[this.crtGuide.click_rect",this.crtGuide.click_rect);
  142. this.targetNode = cc.find("Canvas/" + this.crtGuide.click_rect);
  143. w = this.targetNode.width * this.targetNode.scaleX;
  144. h = this.targetNode.height * this.targetNode.scaleY;
  145. let pos = mk.game.getWorldPos(this.targetNode);
  146. x = pos.x;
  147. y = pos.y;
  148. // //lastStruggle
  149. // if (this.crtGuide && this.crtGuide.id == 2 && this.crtStep == 1) {
  150. // w = this.targetNode.parent.width;
  151. // h = this.targetNode.parent.height;
  152. // let pos1 = mk.game.getWorldPos(this.targetNode.parent);
  153. // x = pos1.x;
  154. // y = pos1.y - h / 2;
  155. // }
  156. if (this.crtGuide.btnEnable == '1') {
  157. //穿透点击目标节点时增加一个事件触发下一步,触发后移除
  158. this.node_click.width = this.node_click.height = 0;
  159. let eventHandler = new cc.Component.EventHandler();
  160. eventHandler.target = this.node;
  161. eventHandler.component = "Guide";
  162. eventHandler.handler = "clickNodeClick";
  163. if (this.targetNode.getComponent(cc.Button)) {
  164. this.targetNode.getComponent(cc.Button).clickEvents.push(eventHandler);
  165. }
  166. }
  167. else {
  168. this.node_click.x = 0;
  169. this.node_click.y = 0;
  170. this.node_click.width = this.node.width;
  171. this.node_click.height = this.node.height;
  172. }
  173. }
  174. //显示区域由大到小动画
  175. if (x != null && y != null && w != null && h != null) {
  176. if (this.crtGuide.display_type == 0) {//矩形选中框
  177. //先重置
  178. this.node_bg.x = this.node_bg.y = this.node_display.x = this.node_display.y = 0;
  179. this.node_display.width = this.node.width;
  180. this.node_display.height = this.node.height;
  181. //动画
  182. cc.tween(this.node_display)
  183. .to(0.2, { x: x, y: y, width: w, height: h }, {
  184. onUpdate: () => {
  185. this.node_bg.x = -this.node_display.x;
  186. this.node_bg.y = -this.node_display.y;
  187. }
  188. })
  189. .start();
  190. }
  191. else {
  192. //先重置
  193. this.node_bg.x = this.node_display.x = x;
  194. this.node_bg.y = this.node_display.y = y;
  195. this.node_display.width = this.node_display.height = this.node_bg.width = this.node_bg.height = this.node.height * 2;
  196. //动画
  197. let radius = this.crtGuide.display_type;
  198. cc.tween(this.node_display)
  199. .to(0.2, { width: radius, height: radius })
  200. .start();
  201. }
  202. }
  203. //对话框位置
  204. //全屏对齐
  205. if (this.crtGuide.dialog_pos != null && this.crtGuide.dialog_pos.length > 0) {
  206. let arr = this.crtGuide.dialog_pos.split(":");
  207. let wedgit = this.node_dialog.getComponent(cc.Widget);
  208. for (let i = 0; i < this.widgets.length; i++) {
  209. switch (i) {
  210. case 0:
  211. wedgit.isAlignTop = arr[i] != "";
  212. break;
  213. case 1:
  214. wedgit.isAlignBottom = arr[i] != "";
  215. break;
  216. case 2:
  217. wedgit.isAlignLeft = arr[i] != "";
  218. break;
  219. case 3:
  220. wedgit.isAlignRight = arr[i] != "";
  221. break;
  222. case 4:
  223. wedgit.isAlignVerticalCenter = arr[i] != "";
  224. break;
  225. case 5:
  226. wedgit.isAlignHorizontalCenter = arr[i] != "";
  227. break;
  228. }
  229. if (arr[i] != "") {
  230. wedgit[this.widgets[i]] = parseInt(arr[i]);
  231. }
  232. }
  233. wedgit.enabled = true;
  234. wedgit.updateAlignment();
  235. }
  236. else {//显示区域偏移
  237. let arr = this.crtGuide.dialog_pos1.split(",");
  238. let wedgit = this.node_dialog.getComponent(cc.Widget);
  239. wedgit.enabled = false;
  240. this.node_dialog.x = x + parseInt(arr[0]);
  241. this.node_dialog.y = y + parseInt(arr[1]);
  242. }
  243. //对话文字对齐方式
  244. let a = this.crtGuide.dialog_alignment;
  245. this.rich_dialog.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  246. this.rich_dialog1.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  247. if (this.crtGuide.finger == "") {
  248. this.node_finger.active = false;
  249. }
  250. else {
  251. this.node_finger.active = true;
  252. let arr = this.crtGuide.finger.split(":");
  253. this.node_finger.x = x + parseInt(arr[0]);
  254. this.node_finger.y = y + parseInt(arr[1]);
  255. }
  256. if (this.crtGuide.tr_form == 3 || this.crtGuide.tr_form == 4) {
  257. this.node_bg.active = false;
  258. if (this.node_click['_touchListener']) {
  259. this.node_click['_touchListener'].swallowTouches = false;
  260. }
  261. }
  262. else {
  263. this.node_bg.active = true;
  264. if (this.node_click['_touchListener']) {
  265. this.node_click['_touchListener'].swallowTouches = true;
  266. }
  267. }
  268. if (this.crtGuide.tr_form == 1 || this.crtGuide.tr_form == 3) {
  269. this.node_head.node.active = true;
  270. this.node_person.active = false;
  271. }
  272. else if (this.crtGuide.tr_form == 2 || this.crtGuide.tr_form == 4) {
  273. this.node_head.node.active = false;
  274. this.node_person.active = true;
  275. }
  276. if (mk.guide.curDes != '') {
  277. this.rich_dialog.string = mk.guide.curDes;
  278. this.rich_dialog1.string = mk.guide.curDes;
  279. mk.guide.curDes = '';
  280. }
  281. else {
  282. this.rich_dialog.string = this.crtGuide.dialog;
  283. this.rich_dialog1.string = this.crtGuide.dialog;
  284. }
  285. }
  286. }
  287. async clickNodeClick() {
  288. mk.audio.playEffect("button");
  289. cc.Tween.stopAllByTarget(this.node_display);
  290. console.log("clickNodeClick");
  291. if (this.crtGuide.btnEnable == '1') {
  292. if (this.targetNode && this.targetNode.getComponent(cc.Button)) {
  293. this.targetNode.getComponent(cc.Button).clickEvents.pop();
  294. }
  295. }
  296. if (this.event_data) {
  297. //event_guide data:1_2 (1_2为新手引导的id)
  298. mk.event.emit("event_guide", this.event_data);
  299. }
  300. this.reset();
  301. this.nextStep();
  302. }
  303. /** 等待状态 透明不可点击 */
  304. private reset() {
  305. this.node_display.width = 0;
  306. this.node_display.height = 0;
  307. this.node_bg.width = this.node.width;
  308. this.node_bg.height = this.node.height;
  309. this.node_bg.x = -this.node_display.x;
  310. this.node_bg.y = -this.node_display.y;
  311. this.node_bg.opacity = 0;
  312. this.node_click.width = 0
  313. this.node_click.height = 0;
  314. this.node_finger.opacity = 0;
  315. this.node_dialog.opacity = 0
  316. }
  317. private close() {
  318. mk.guide.close();
  319. // this.node.destroy();
  320. mk.guide.curGuideIdStr = null;
  321. if (mk.guide.continueCallBack) {
  322. mk.guide.continueCallBack = null;
  323. }
  324. console.log("closeGuide ======");
  325. mk.ui.closePanel(this.node.name);
  326. }
  327. private clickSkip() {
  328. mk.audio.playEffect("button");
  329. this.close();
  330. mk.data.sendXYEvent("guide", `skip_${gData.guideData.crtID}_${this.crtStep}`);
  331. mk.data.sendDataEvent(DataEventId.guideSkip, `新手引导第${gData.guideData.crtID}_${this.crtStep}步跳过`);
  332. }
  333. protected onDestroy(): void {
  334. this.node_click.off(cc.Node.EventType.TOUCH_START, this.clickNodeClick, this);
  335. mk.event.remove('next', this);
  336. }
  337. }