Guide.ts 10 KB

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