test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { _decorator, Color, Component, EventTouch, Graphics, Label, Mask, Node, size, Size, Sprite, UITransform, Vec2, Vec3 } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('test')
  4. export class test extends Component {
  5. @property(Node) draw_bg:Node = null;
  6. @property(Node) btn_type:Node = null;
  7. @property(Node) btn_clear:Node = null;
  8. @property(Node) bottom:Node = null;
  9. @property(Mask) top_mask:Mask = null;
  10. @property(Node) top:Node = null;
  11. private is_circle:boolean = true;
  12. private bottom_size:Size = new Size(0,0);
  13. private top_mask_graphics: Graphics = null;
  14. private smear_rect_size:Size = new Size(40, 50);
  15. private smear_circle_size:number = 45;
  16. private hide_points = []
  17. private hide_points_total_num:number = 0
  18. start() {
  19. this.bottom_size = this.bottom.getComponent(UITransform).contentSize
  20. this.top_mask_graphics = this.top_mask.getComponent(Graphics)
  21. this.top.getComponent(UITransform).contentSize = this.bottom_size
  22. this.btn_type.on(Node.EventType.TOUCH_END, ()=>{
  23. this.is_circle = !this.is_circle
  24. this.updateBtnTypeStatus()
  25. this.clearMaskGraphics()
  26. },this)
  27. this.updateBtnTypeStatus()
  28. this.btn_clear.on(Node.EventType.TOUCH_END, ()=>{
  29. this.clearMaskGraphics()
  30. },this)
  31. this.bottom.on(Node.EventType.TOUCH_START, this.onTouchBegin, this);
  32. this.bottom.on(Node.EventType.TOUCH_MOVE, this.onTouchMoved, this);
  33. this.bottom.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  34. this.clearMaskGraphics()
  35. }
  36. updateBtnTypeStatus() {
  37. if(this.is_circle) {
  38. this.btn_type.getChildByName("Label").getComponent(Label).string = '更改为方形'
  39. } else {
  40. this.btn_type.getChildByName("Label").getComponent(Label).string = '更改为圆形'
  41. }
  42. }
  43. clearMaskGraphics() {
  44. this.top_mask_graphics.clear()
  45. this.initMaskPoints()
  46. }
  47. initMaskPoints() {
  48. var rows = 0
  49. var cols = 0
  50. if(this.is_circle) {
  51. rows = Math.floor(this.bottom_size.height/(this.smear_circle_size*1.5));
  52. cols = Math.floor(this.bottom_size.width/(this.smear_circle_size*1.5));
  53. } else {
  54. rows = Math.floor(this.bottom_size.height/(this.smear_rect_size.height*1.5));
  55. cols = Math.floor(this.bottom_size.width/(this.smear_rect_size.width*1.5));
  56. }
  57. if(rows>0) {
  58. rows = Math.ceil(rows/2);
  59. }
  60. if(cols>0) {
  61. cols = Math.ceil(cols/2);
  62. }
  63. this.hide_points = []
  64. for (let i =-rows; i<=rows; i++) {
  65. for(let j=-cols; j<=cols; j++) {
  66. this.hide_points.push({'x':j, "y":i})
  67. }
  68. }
  69. this.hide_points_total_num = this.hide_points.length
  70. console.log('hidePoints',this.hide_points, '数量:',this.hide_points_total_num)
  71. }
  72. onTouchBegin(event:EventTouch) {
  73. this.scratch(event)
  74. }
  75. onTouchMoved(event:EventTouch) {
  76. this.scratch(event)
  77. }
  78. onTouchEnd(event:EventTouch) {
  79. this.scratch(event)
  80. }
  81. scratch(event:EventTouch) {
  82. let event_point = event.touch.getUILocation();
  83. let v3 = new Vec3(event_point.x, event_point.y,1)
  84. let point = this.bottom.getComponent(UITransform).convertToNodeSpaceAR(v3);
  85. this.addCircle(point)
  86. }
  87. addCircle(point:Vec3) {
  88. // if(point.x < -(this.bottom_size.width / 2) || point.x > this.bottom_size.width / 2) {
  89. // return console.log('超出 X 界限')
  90. // }
  91. // if(point.y < -(this.bottom_size.height / 2) || point.y > this.bottom_size.height / 2) {
  92. // return console.log('超出 Y 界限')
  93. // }
  94. // console.log('point=',point)
  95. this.top_mask_graphics = this.top_mask.getComponent(Graphics)
  96. if(this.is_circle) {
  97. this.top_mask_graphics.circle(point.x, point.y, this.smear_circle_size)
  98. } else {
  99. this.top_mask_graphics.rect(point.x, point.y, this.smear_rect_size.width, this.smear_rect_size.height)
  100. }
  101. this.top_mask_graphics.fill()
  102. this.calculateScratchArea(point)
  103. }
  104. calculateScratchArea(point:Vec3) {
  105. var x = 0
  106. var y = 0
  107. if(this.is_circle) {
  108. x = Math.floor(point.x/this.smear_circle_size)
  109. y = Math.floor(point.y/this.smear_circle_size)
  110. } else {
  111. x = Math.floor(point.x/this.smear_rect_size.width)
  112. y = Math.floor(point.y/this.smear_rect_size.height)
  113. }
  114. for (let i = 0; i < this.hide_points.length; i++) {
  115. const element = this.hide_points[i];
  116. if(x==this.hide_points[i].x && y==this.hide_points[i].y) {
  117. this.hide_points.splice(i,1)
  118. break
  119. }
  120. }
  121. let ratio = Math.floor((1 - (this.hide_points.length/this.hide_points_total_num)) * 100)
  122. console.log('计算=',this.hide_points.length,' 百分比= ',ratio, '%')
  123. }
  124. }
  125. // import { _decorator, Component, EventTouch, Label, Node, UITransform, Vec2, Vec3 } from 'cc';
  126. // const { ccclass, property } = _decorator;
  127. // @ccclass('test')
  128. // export class test extends Component {
  129. // @property(Node) bg:Node = null;
  130. // @property(Node) img:Node = null;
  131. // @property(Node) img_point:Node = null;
  132. // @property(Node) btn_rotate:Node = null;
  133. // protected is_rotate:boolean = false;
  134. // protected anchor_point:Vec2 = new Vec2(0.5,0.5)
  135. // start() {
  136. // this.img.on(Node.EventType.TOUCH_END, (et:EventTouch)=> {
  137. // let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  138. // let n_p = this.img.getComponent(UITransform).convertToNodeSpaceAR(p)
  139. // // console.log('n_p',n_p)
  140. // let img_position_x = n_p.x
  141. // let img_position_y = n_p.y
  142. // this.img_point.position = new Vec3(img_position_x, img_position_y, 1)
  143. // this.anchor_point = new Vec2(0.5,0.5)
  144. // this.img.getComponent(UITransform).setAnchorPoint(this.anchor_point)
  145. // let img_contentSize = this.img.getComponent(UITransform).contentSize
  146. // let anchor_point_x = (img_contentSize.width / 2 + n_p.x) / img_contentSize.width
  147. // let anchor_point_y = (img_contentSize.height / 2 + n_p.y) / img_contentSize.height
  148. // this.anchor_point = new Vec2(anchor_point_x, anchor_point_y)
  149. // console.log('当前锚点=',this.anchor_point)
  150. // },this)
  151. // this.btn_rotate.on(Node.EventType.TOUCH_END, ()=> {
  152. // if(this.is_rotate) {
  153. // // 归位
  154. // this.btn_rotate.children[0].getComponent(Label).string = '旋转'
  155. // this.img.angle = 0
  156. // this.anchor_point = new Vec2(0.5,0.5)
  157. // this.img_point.position = new Vec3(0, 0, 1)
  158. // } else {
  159. // // 旋转
  160. // this.btn_rotate.children[0].getComponent(Label).string = '归位'
  161. // this.img.angle = 10
  162. // }
  163. // this.img.getComponent(UITransform).setAnchorPoint(this.anchor_point)
  164. // this.is_rotate = !this.is_rotate
  165. // },this)
  166. // }
  167. // }