TestTouch.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. let _textureIdMapDataContainer = {}
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. },
  6. // LIFE-CYCLE CALLBACKS:
  7. onLoad() {
  8. let spriteComp = this.node.getComponent(cc.Sprite)
  9. if (spriteComp.type !== cc.Sprite.Type.SIMPLE || spriteComp.sizeMode !== cc.Sprite.SizeMode.RAW) {
  10. throw "目前仅支持sprite SizeMode 为RAW, type 为SIMPLE的方式"
  11. }
  12. this.node._hitTest = this.hitTest.bind(this)
  13. },
  14. start() {
  15. },
  16. hitTest(location) {
  17. cc.log('location',location);
  18. let spriteFrame = this.node.getComponent(cc.Sprite).spriteFrame
  19. if (spriteFrame == null) {
  20. return false
  21. }
  22. let posInNode = this.node.convertToNodeSpaceAR(location)
  23. let rect = spriteFrame.getRect()
  24. let offset = spriteFrame.getOffset()
  25. //var type = this.node.getComponent("DecorateItemPrefab").m_type
  26. // cc.log(type + "--", "xxxxxxxxxxxxxx", type)
  27. // cc.log(type + "--", "posInNode", posInNode)
  28. // cc.log(type + "--", "rect", rect)
  29. // cc.log(type + "--", "offset", offset)
  30. if ((posInNode.x < offset.x - rect.width / 2) || (posInNode.y < offset.y - rect.height / 2)
  31. || (posInNode.x > (offset.x + rect.width / 2)) || (posInNode.y > (offset.y + rect.height / 2))) {
  32. return false
  33. }
  34. else {
  35. let posInRect = cc.v2(parseInt(posInNode.x - offset.x + rect.width / 2), parseInt(posInNode.y - offset.y + rect.height / 2))
  36. // cc.log(type + "--", "posInRect", posInRect)
  37. // cc.log(type + "--", "isRotated", spriteFrame.isRotated())
  38. let tex = spriteFrame.getTexture()
  39. let data
  40. if (tex instanceof cc.RenderTexture) { // 细图(width <= 512 && height <= 512) 被引擎自动打包了
  41. if (cc.sys.platform === cc.sys.QQ_PLAY) { // 玩一玩平台
  42. throw "在玩一玩平台,请确保你的SpriteFrame 的宽高至少有一个大于512, 这样不会被Atlas, 不然被引擎自动Atlas之后,因为玩一玩不支持gl.readPixels 或 getImageData这样的接口,像素就读不出来了"
  43. }
  44. // data就是这个texture的rgba值数组
  45. if (spriteFrame.isRotated()) {
  46. data = rt.readPixels(null, rect.x + posInRect.y, rect.y + posInRect.x, 1, 1)
  47. //cc.log(type + "--", "data", data, rect.x + posInRect.y, rect.y + posInRect.x)
  48. }
  49. else {
  50. data = rt.readPixels(null, rect.x + posInRect.x, rect.y + rect.height - posInRect.y, 1, 1)
  51. //cc.log(type + "--", "data", data, rect.x + posInRect.x, rect.y + rect.height - posInRect.y)
  52. }
  53. }
  54. else {
  55. var dataContainer = _textureIdMapDataContainer[tex.getId()]
  56. if (cc.sys.platform === cc.sys.QQ_PLAY) { // 针对玩一玩的特殊方式
  57. if (!dataContainer) {
  58. tex.getHtmlElementObj().bkImage || tex.getHtmlElementObj()._generateBKImage()
  59. dataContainer = tex.getHtmlElementObj().bkImage
  60. _textureIdMapDataContainer[tex.getId()] = dataContainer
  61. }
  62. var buffer = dataContainer.buffer
  63. buffer.rewind()
  64. if (spriteFrame.isRotated()) {
  65. buffer.jumpBytes(((rect.x + posInRect.y) + (rect.y + posInRect.x) * tex.width) * 4)
  66. //cc.log(type + "--", "data", data, rect.x + posInRect.y, rect.y + posInRect.x)
  67. } else {
  68. buffer.jumpBytes(((rect.x + posInRect.x) + (rect.y + rect.height - posInRect.y) * tex.width) * 4)
  69. //cc.log(type + "--", "data", data, rect.x + posInRect.x, rect.y + rect.height - posInRect.y)
  70. }
  71. data = [buffer.readUint8Buffer(), buffer.readUint8Buffer(), buffer.readUint8Buffer(), buffer.readUint8Buffer()]
  72. }
  73. else {
  74. //Canvas 方式
  75. var scale = 8
  76. var cvs = dataContainer
  77. if (!cvs) {
  78. cvs = document.createElement("canvas")
  79. var ctx = cvs.getContext('2d')
  80. cvs.width = tex.width
  81. cvs.height = tex.height
  82. ctx.drawImage(tex.getHtmlElementObj(), 0, 0, tex.width, tex.height, 0, 0, tex.width / scale, tex.height / scale)
  83. _textureIdMapDataContainer[tex.getId()] = cvs
  84. }
  85. var ctx = cvs.getContext('2d')
  86. if (spriteFrame.isRotated()) {
  87. data = ctx.getImageData((rect.x + posInRect.y) / scale, (rect.y + posInRect.x) / scale, 1, 1).data
  88. //cc.log(type + "--", "data", data, rect.x + posInRect.y, rect.y + posInRect.x)
  89. }
  90. else {
  91. data = ctx.getImageData((rect.x + posInRect.x) / scale, (rect.y + rect.height - posInRect.y) / scale, 1, 1).data
  92. //cc.log(type + "--", "data", data, rect.x + posInRect.x, rect.y + rect.height - posInRect.y)
  93. }
  94. /*
  95. //RenderTexture 方式
  96. var rt = dataContainer
  97. if (!rt){
  98. rt = new cc.RenderTexture()
  99. rt.initWithSize(tex.width, tex.height)
  100. rt.drawTextureAt(tex, 0, 0)
  101. _textureIdMapDataContainer[tex.getId()] = rt
  102. }
  103. // data就是这个texture的rgba值数组
  104. if (spriteFrame.isRotated()) {
  105. data = rt.readPixels(null, rect.x + posInRect.y, rect.y + posInRect.x, 1, 1)
  106. //cc.log(type + "--", "data", data, rect.x + posInRect.y, rect.y + posInRect.x)
  107. }
  108. else {
  109. data = rt.readPixels(null, rect.x + posInRect.x, rect.y + rect.height - posInRect.y, 1, 1)
  110. //cc.log(type + "--", "data", data, rect.x + posInRect.x, rect.y + rect.height - posInRect.y)
  111. }
  112. */
  113. }
  114. }
  115. if (data[3] <= 0) {
  116. cc.log("false");
  117. return false
  118. }
  119. else {
  120. cc.log("true");
  121. return true
  122. }
  123. }
  124. },
  125. onDisable() {
  126. cc.log("DecorateHitTest onDisable")
  127. let spriteFrame = this.node.getComponent(cc.Sprite).spriteFrame
  128. if (spriteFrame == null) {
  129. return false
  130. }
  131. var tex = spriteFrame.getTexture()
  132. if (cc.sys.platform === cc.sys.QQ_PLAY) { // 针对玩一玩的特殊方式
  133. var img = _textureIdMapDataContainer[tex.getId()]
  134. if (img) {
  135. cc.log("DecorateHitTest onDisable QQ bkImage")
  136. img.dispose()
  137. _textureIdMapDataContainer[tex.getId()] = null
  138. }
  139. }
  140. else {
  141. var dataContainer = _textureIdMapDataContainer[tex.getId()]
  142. if (dataContainer) {
  143. if (dataContainer instanceof cc.RenderTexture) { // RenderTexture 方式
  144. cc.log("DecorateHitTest onDisable renderTexture")
  145. dataContainer.destroy()
  146. }
  147. else { // Canvas 方式
  148. cc.log("DecorateHitTest onDisable canvas")
  149. // 暂时不知道如何释放这个引用的Canvas
  150. }
  151. }
  152. _textureIdMapDataContainer[tex.getId()] = null
  153. }
  154. }
  155. // update (dt) {},
  156. });