Guide.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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.Node, displayName: "手指" })
  18. node_finger: cc.Node = null;
  19. @property({ type: cc.Sprite, displayName: "头像" })
  20. node_head: cc.Sprite = null;
  21. @property({ type: sp.Skeleton, displayName: "形象" })
  22. node_person: sp.Skeleton = 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_data: string;
  30. /** 对话框对齐方式 上下左右中 */
  31. private widgets: string[] = ['top', 'bottom', 'left', 'right', 'verticalCenter', 'horizontalCenter'];
  32. onLoad() {
  33. // this.initTouch();
  34. }
  35. start() {
  36. if (gData.guideData.crtID == null) {
  37. this.node.destroy();
  38. return;
  39. }
  40. this.reset();
  41. this.guides = gData.guideData.getGuidesByID(gData.guideData.crtID);
  42. this.crtStep = -1;
  43. this.nextStep();
  44. }
  45. private nextStep() {
  46. this.crtStep++;
  47. this.crtGuide = this.guides[this.crtStep];
  48. this.updateGuide();
  49. }
  50. private updateGuide() {
  51. if (this.crtGuide == null) {
  52. this.close();
  53. }
  54. else {
  55. this.node_display.getComponent(cc.Mask).type = this.crtGuide.display_type == 0 ? cc.Mask.Type.RECT : cc.Mask.Type.ELLIPSE;
  56. this.node_bg.opacity = 180;
  57. this.node_finger.opacity = 255;
  58. this.node_dialog.opacity = 255;
  59. this.targetNode = null;
  60. this.event_data = null;
  61. this.ifThrough = true;
  62. let [x, y, w, h] = [null, null, null, null];
  63. //点击全屏下一步,显示区域不显示 || 点击区域下一步,并发送事件,显示区域读配置
  64. if (this.crtGuide.click_rect == "all" || this.crtGuide.click_rect.indexOf("event") != -1) {
  65. //显示区域: display_rect为""表示全屏
  66. if (this.crtGuide.display_rect.length == 0) {
  67. this.node_display.width = this.node_display.height = 0;
  68. }
  69. else {
  70. let arr = this.crtGuide.display_rect.split(":");
  71. x = parseInt(arr[0]);
  72. y = parseInt(arr[1]);
  73. w = parseInt(arr[2]);
  74. h = parseInt(arr[3]);
  75. }
  76. if (this.crtGuide.click_rect == "all") {
  77. this.node_click.x = 0;
  78. this.node_click.y = 0;
  79. this.node_click.width = this.node.width;
  80. this.node_click.height = this.node.height;
  81. this.ifThrough = false;
  82. }
  83. else {
  84. this.event_data = this.crtGuide.id + "_" + (this.crtStep + 1);
  85. this.node_click.x = x;
  86. this.node_click.y = y;
  87. this.node_click.width = w;
  88. this.node_click.height = h;
  89. }
  90. }
  91. else {//点击按钮下一步,显示区域为配置的node,点击区域=显示区域
  92. this.targetNode = cc.find("Canvas/" + this.crtGuide.click_rect);
  93. w = this.targetNode.width;
  94. h = this.targetNode.height;
  95. let pos = mk.game.getWorldPos(this.targetNode);
  96. x = pos.x;
  97. y = pos.y;
  98. //穿透点击目标节点时增加一个事件触发下一步,触发后移除
  99. this.node_click.width = this.node_click.height = 0;
  100. let eventHandler = new cc.Component.EventHandler();
  101. eventHandler.target = this.node;
  102. eventHandler.component = "Guide";
  103. eventHandler.handler = "clickNodeClick";
  104. this.targetNode.getComponent(cc.Button).clickEvents.push(eventHandler);
  105. }
  106. //显示区域由大到小动画
  107. if (x != null && y != null && w != null && h != null) {
  108. if (this.crtGuide.display_type == 0) {//矩形选中框
  109. //先重置
  110. this.node_bg.x = this.node_bg.y = this.node_display.x = this.node_display.y = 0;
  111. this.node_display.width = this.node.width;
  112. this.node_display.height = this.node.height;
  113. //动画
  114. cc.tween(this.node_display)
  115. .to(0.2, { x: x, y: y, width: w, height: h }, {
  116. onUpdate: () => {
  117. this.node_bg.x = -this.node_display.x;
  118. this.node_bg.y = -this.node_display.y;
  119. }
  120. })
  121. .start();
  122. }
  123. else {
  124. //先重置
  125. this.node_bg.x = this.node_display.x = x;
  126. this.node_bg.y = this.node_display.y = y;
  127. this.node_display.width = this.node_display.height = this.node_bg.width = this.node_bg.height = this.node.height * 2;
  128. //动画
  129. let radius = this.crtGuide.display_type;
  130. cc.tween(this.node_display)
  131. .to(0.2, { width: radius, height: radius })
  132. .start();
  133. }
  134. }
  135. //对话框位置
  136. //全屏对齐
  137. if (this.crtGuide.dialog_pos != null && this.crtGuide.dialog_pos.length > 0) {
  138. let arr = this.crtGuide.dialog_pos.split(":");
  139. let wedgit = this.node_dialog.getComponent(cc.Widget);
  140. for (let i = 0; i < this.widgets.length; i++) {
  141. wedgit[this.widgets[i]] = arr[i] == "" ? null : parseInt(arr[i]);
  142. }
  143. }
  144. else {//显示区域偏移
  145. let arr = this.crtGuide.dialog_pos1.split(",");
  146. let wedgit = this.node_dialog.getComponent(cc.Widget);
  147. for (let i = 0; i < this.widgets.length; i++) {
  148. wedgit[this.widgets[i]] = null;
  149. }
  150. this.node_dialog.x = x + parseInt(arr[0]);
  151. this.node_dialog.y = y + parseInt(arr[1]);
  152. }
  153. //对话文字对齐方式
  154. let a = this.crtGuide.dialog_alignment;
  155. this.rich_dialog.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  156. if (this.crtGuide.finger == "") {
  157. this.node_finger.active = false;
  158. }
  159. else {
  160. this.node_finger.active = true;
  161. let arr = this.crtGuide.finger.split(":");
  162. this.node_finger.x = x + parseInt(arr[0]);
  163. this.node_finger.y = y + parseInt(arr[1]);
  164. }
  165. if (this.crtGuide.tr_form == 1) {
  166. this.node_head.node.active = true;
  167. this.node_person.node.active = false;
  168. }
  169. else {
  170. this.node_head.node.active = false;
  171. this.node_person.node.active = true;
  172. }
  173. this.rich_dialog.string = this.crtGuide.dialog;
  174. }
  175. }
  176. async clickNodeClick() {
  177. mk.audio.playEffect("button");
  178. cc.Tween.stopAllByTarget(this.node_display);
  179. console.log("clickNodeClick");
  180. if (this.targetNode) {
  181. this.targetNode.getComponent(cc.Button).clickEvents.pop();
  182. }
  183. if (this.event_data) {
  184. //event_guide data:1_2 (1_2为新手引导的id)
  185. mk.event.emit("event_guide", this.event_data);
  186. }
  187. this.reset();
  188. if (this.crtGuide.lag_next > 0) {
  189. await mk.time.WaitForSeconds(this.crtGuide.lag_next);
  190. }
  191. this.nextStep();
  192. }
  193. /** 等待状态 透明不可点击 */
  194. private reset() {
  195. this.node_display.width = 0;
  196. this.node_display.height = 0;
  197. this.node_bg.width = this.node.width;
  198. this.node_bg.height = this.node.height;
  199. this.node_bg.x = -this.node_display.x;
  200. this.node_bg.y = -this.node_display.y;
  201. this.node_bg.opacity = 0;
  202. this.node_click.width = 0
  203. this.node_click.height = 0;
  204. this.node_finger.opacity = 0;
  205. this.node_dialog.opacity = 0;
  206. }
  207. private close() {
  208. mk.guide.close();
  209. this.node.destroy();
  210. }
  211. ///////////////// 自定义触摸监听 /////////////////
  212. private _eventManager = cc["internal"]["eventManager"];
  213. private _touchListener: any;
  214. private ifThrough: boolean;
  215. private initTouch() {
  216. const EventListener = cc["EventListener"];
  217. this._touchListener = EventListener.create({
  218. event: EventListener.TOUCH_ONE_BY_ONE,
  219. swallowTouches: false,//是否吞噬touch事件
  220. owner: this.node_click,
  221. mask: null,
  222. onTouchBegan: this.onTouchStart.bind(this),
  223. onTouchMoved: null,
  224. onTouchEnded: this.onTouchEnded.bind(this),
  225. onTouchCancelled: null,
  226. });
  227. this._eventManager.addListener(this._touchListener, this.node_click);
  228. }
  229. private onTouchStart(touch: cc.Touch, event: cc.Event.EventTouch): boolean {
  230. // cc.log("touch start");
  231. //此处必须返回true(表示接触到了节点),否则TOUCH_MOVE,TOUCH_END,TOUCH_CANCEL不触发。
  232. if (this.ifThrough) {
  233. const point_world_pos = touch.getLocation();
  234. let localPoint = this.node.parent.convertToNodeSpaceAR(new cc.Vec2(point_world_pos.x, point_world_pos.y));
  235. // let localPoint = mk.game.localConvertWorldPointARCenter()
  236. let result = this.pointInPoly(localPoint, this.node_click);
  237. return result;
  238. }
  239. else {
  240. return false;
  241. }
  242. }
  243. /**
  244. * 点是否在节点宽高范围内
  245. * @param point 点击的点
  246. * @param node
  247. * @returns
  248. */
  249. private pointInPoly(point: cc.Vec2 | cc.Vec3, node: cc.Node) {
  250. const self_node_pos = node.getPosition();
  251. if (point.x >= (self_node_pos.x - (this.node_click.width / 2)) &&
  252. point.x <= (self_node_pos.x + (this.node_click.width / 2)) &&
  253. point.y >= (self_node_pos.y - (this.node_click.height / 2)) &&
  254. point.y <= (self_node_pos.y + (this.node_click.height / 2))) {
  255. return true;
  256. }
  257. return false;
  258. }
  259. private onTouchEnded(touch: cc.Touch, event: cc.Event.EventTouch): void {
  260. // cc.log("touch end");
  261. this.clickNodeClick();
  262. }
  263. protected onDestroy(): void {
  264. // super.onDestroy();
  265. this._eventManager.removeListener(this._touchListener, this.node_click);
  266. }
  267. }