Guide.ts 11 KB

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