NodeEvents.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. let NodeEvents = (function () {
  2. let _this = {};
  3. //移动目标节点
  4. let moveTargetNode = null;
  5. let isMouseDown = null;
  6. let isOperate = true;
  7. // 屏幕坐标转换到节点坐标
  8. function convertToNodeSpace(event) {
  9. return cc.find("Canvas").convertToNodeSpaceAR(event.getLocation());
  10. }
  11. // 是否能删除
  12. function isDelete(node) {
  13. return node.ident == lcl.Ident.point;
  14. }
  15. // 是否在绘制区域
  16. function atDrawingArea(pos) {
  17. let resolution = lcl.BezierData.getResolution();
  18. let halfW = resolution.width / 2;
  19. let halfH = resolution.height / 2;
  20. return pos.x > -halfW && pos.x < halfW &&
  21. pos.y > -halfH && pos.y < halfH
  22. }
  23. // 是否能拖拽
  24. function isDragMove(mousePos, target) {
  25. let flag = false;
  26. switch (target.ident) {
  27. case lcl.Ident.point:
  28. case lcl.Ident.control:
  29. flag = atDrawingArea(mousePos)
  30. break;
  31. case lcl.Ident.window:
  32. flag = true;
  33. }
  34. console.log(target.name, target.ident);
  35. return flag;
  36. }
  37. _this.setMoveTargetNode = function (target) {
  38. moveTargetNode = target;
  39. }
  40. _this.setOperateStatus = function (bol) {
  41. isOperate = bol;
  42. }
  43. // 添加拖拽事件
  44. _this.addDragEvents = function (node, target = node) {
  45. // 鼠标按下
  46. node.on(cc.Node.EventType.MOUSE_DOWN, (event) => {
  47. event.stopPropagation();
  48. // 鼠标右键
  49. if (event.getButton() == cc.Event.EventMouse.BUTTON_LEFT /**&& _this.isOperate()**/) {
  50. moveTargetNode = target;
  51. isMouseDown = true;
  52. }
  53. });
  54. // 鼠标移动
  55. node.on(cc.Node.EventType.MOUSE_MOVE, (event) => {
  56. target.opacity = 100;
  57. cc.game.canvas.style.cursor = "all-scroll";
  58. //鼠标按下并且有指定目标节点
  59. if (isMouseDown && moveTargetNode) {
  60. //把屏幕坐标转换到节点坐标下
  61. let mousePos = convertToNodeSpace(event);
  62. if (isDragMove(mousePos, moveTargetNode))
  63. moveTargetNode.setPosition(mousePos);
  64. }
  65. });
  66. // 鼠标离开
  67. node.on(cc.Node.EventType.MOUSE_LEAVE, (event) => {
  68. target.opacity = 255;
  69. cc.game.canvas.style.cursor = "auto";
  70. });
  71. // 鼠标抬起
  72. node.on(cc.Node.EventType.MOUSE_UP, (event) => {
  73. isMouseDown = false;
  74. moveTargetNode = null;
  75. lcl.BezierData.saveBezierPath();//保存坐标点
  76. lcl.Events.emit('drawFillLine')
  77. });
  78. }
  79. // 添加节点的删除事件
  80. _this.addPointDeleteEvents = function (node) {
  81. // 鼠标按下
  82. node.on(cc.Node.EventType.MOUSE_DOWN, (event) => {
  83. // 鼠标右键
  84. if (event.getButton() == cc.Event.EventMouse.BUTTON_RIGHT) {
  85. if (isDelete(event.target)) {
  86. let mousePos = convertToNodeSpace(event);
  87. // this.deleteTarget = event.target;
  88. lcl.BezierData.setDeleteTarget(event.target);
  89. lcl.Events.emit("showDeleteBtn", mousePos);
  90. }
  91. return
  92. }
  93. });
  94. }
  95. // 添加Canvas节点事件
  96. _this.addCanvasTouchEvents = function (canvasNode = cc.find("Canvas")) {
  97. let target;
  98. // 鼠标按下
  99. canvasNode.on(cc.Node.EventType.MOUSE_DOWN, (event) => {
  100. // 鼠标左键
  101. if (event.getButton() == cc.Event.EventMouse.BUTTON_LEFT) {
  102. if (!lcl.BezierData.canAddPos) return;
  103. event.stopPropagation();
  104. target = event.target;
  105. //创建坐标点,需要先把屏幕坐标转换到节点坐标下
  106. let mousePos = convertToNodeSpace(event);
  107. if (!atDrawingArea(mousePos))
  108. return
  109. if (!isOperate)
  110. lcl.Events.emit("hideDeleteBtn");
  111. // 二阶
  112. if (lcl.BezierData.getBezierCurveType() == lcl.BezierCurveType.SecondOrder) {
  113. lcl.BezierData.createCurve(mousePos);
  114. }
  115. // 三阶
  116. if (lcl.BezierData.getBezierCurveType() == lcl.BezierCurveType.ThirdOrder) {
  117. lcl.BezierData.createThirdOrderCurve(mousePos);
  118. }
  119. isMouseDown = true;
  120. }
  121. });
  122. // 鼠标移动
  123. canvasNode.on(cc.Node.EventType.MOUSE_MOVE, (event) => {
  124. target = event.target;
  125. //创建坐标点,需要先把屏幕坐标转换到节点坐标下
  126. let mousePos = convertToNodeSpace(event);
  127. lcl.Events.emit("setMouseLocation", mousePos);
  128. //鼠标按下并且有指定目标节点
  129. if (isMouseDown && moveTargetNode && isDragMove(mousePos, moveTargetNode)) {
  130. console.log("moveTargetNode.setPosition(mousePos);");
  131. moveTargetNode.setPosition(mousePos);
  132. }
  133. });
  134. // 鼠标抬起
  135. canvasNode.on(cc.Node.EventType.MOUSE_UP, (event) => {
  136. target = event.target;
  137. isMouseDown = false;
  138. moveTargetNode = null;
  139. });
  140. }
  141. return _this;
  142. }());
  143. module.exports = NodeEvents;