diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index a0ff8c0fb1..37f0ac5661 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1212,7 +1212,7 @@ var Node = cc.Class({ // find mask parent, should hit test it if (parent === mask.node) { var comp = parent.getComponent(cc.Mask); - return (comp && comp.enabledInHierarchy) ? parent._hitTest(point) : true; + return (comp && comp.enabledInHierarchy) ? comp._hitTest(point) : true; } // mask parent no longer exists else { diff --git a/cocos2d/core/components/CCMask.js b/cocos2d/core/components/CCMask.js index 4b93e78e17..3d6f28b019 100644 --- a/cocos2d/core/components/CCMask.js +++ b/cocos2d/core/components/CCMask.js @@ -120,6 +120,40 @@ var Mask = cc.Class({ _initSgNode: function () {}, + _hitTest: function (point) { + var size = this.node.getContentSize(), + w = size.width, + h = size.height, + trans = this.node.getNodeToWorldTransform(); + + if (this._type === MaskType.RECT) { + var rect = cc.rect(0, 0, w, h); + cc._rectApplyAffineTransformIn(rect, trans); + var left = point.x - rect.x, + right = rect.x + rect.width - point.x, + bottom = point.y - rect.y, + top = rect.y + rect.height - point.y; + if (left >= 0 && right >= 0 && top >= 0 && bottom >= 0) { + return true; + } + else { + return false; + } + } + else { + var a = w/2, b = h/2; + var cx = trans.a * a + trans.c * b + trans.tx; + var cy = trans.b * a + trans.d * b + trans.ty; + var px = point.x - cx, py = point.y - cy; + if (px * px / (a * a) + py * py / (b * b) < 1) { + return true; + } + else { + return false; + } + } + }, + onEnable: function () { this._refreshStencil(); this._super(); diff --git a/cocos2d/core/value-types/CCAffineTransform.js b/cocos2d/core/value-types/CCAffineTransform.js index 0dcd42023b..243bea76fb 100644 --- a/cocos2d/core/value-types/CCAffineTransform.js +++ b/cocos2d/core/value-types/CCAffineTransform.js @@ -154,40 +154,45 @@ cc.affineTransformIdentity = function () { * @param {AffineTransform} anAffineTransform * @return {Rect} */ -cc.rectApplyAffineTransform = function (rect, anAffineTransform) { - var top = cc.rectGetMinY(rect); - var left = cc.rectGetMinX(rect); - var right = cc.rectGetMaxX(rect); - var bottom = cc.rectGetMaxY(rect); - - var topLeft = cc.pointApplyAffineTransform(left, top, anAffineTransform); - var topRight = cc.pointApplyAffineTransform(right, top, anAffineTransform); - var bottomLeft = cc.pointApplyAffineTransform(left, bottom, anAffineTransform); - var bottomRight = cc.pointApplyAffineTransform(right, bottom, anAffineTransform); - - var minX = Math.min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x); - var maxX = Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x); - var minY = Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y); - var maxY = Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y); +cc.rectApplyAffineTransform = function (rect, t) { + var ol = rect.x; + var ob = rect.y; + var or = ol + rect.width; + var ot = ob + rect.height; + var lbx = t.a * ol + t.c * ob + t.tx; + var lby = t.b * ol + t.d * ob + t.ty; + var rbx = t.a * or + t.c * ob + t.tx; + var rby = t.b * or + t.d * ob + t.ty; + var ltx = t.a * ol + t.c * ot + t.tx; + var lty = t.b * ol + t.d * ot + t.ty; + var rtx = t.a * or + t.c * ot + t.tx; + var rty = t.b * or + t.d * ot + t.ty; + var minX = Math.min(lbx, rbx, ltx, rtx); + var maxX = Math.max(lbx, rbx, ltx, rtx); + var minY = Math.min(lby, rby, lty, rty); + var maxY = Math.max(lby, rby, lty, rty); return cc.rect(minX, minY, (maxX - minX), (maxY - minY)); }; -cc._rectApplyAffineTransformIn = function(rect, anAffineTransform){ - var top = cc.rectGetMinY(rect); - var left = cc.rectGetMinX(rect); - var right = cc.rectGetMaxX(rect); - var bottom = cc.rectGetMaxY(rect); +cc._rectApplyAffineTransformIn = function(rect, t){ + var ol = rect.x; + var ob = rect.y; + var or = ol + rect.width; + var ot = ob + rect.height; + var lbx = t.a * ol + t.c * ob + t.tx; + var lby = t.b * ol + t.d * ob + t.ty; + var rbx = t.a * or + t.c * ob + t.tx; + var rby = t.b * or + t.d * ob + t.ty; + var ltx = t.a * ol + t.c * ot + t.tx; + var lty = t.b * ol + t.d * ot + t.ty; + var rtx = t.a * or + t.c * ot + t.tx; + var rty = t.b * or + t.d * ot + t.ty; - var topLeft = cc.pointApplyAffineTransform(left, top, anAffineTransform); - var topRight = cc.pointApplyAffineTransform(right, top, anAffineTransform); - var bottomLeft = cc.pointApplyAffineTransform(left, bottom, anAffineTransform); - var bottomRight = cc.pointApplyAffineTransform(right, bottom, anAffineTransform); - - var minX = Math.min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x); - var maxX = Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x); - var minY = Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y); - var maxY = Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y); + var minX = Math.min(lbx, rbx, ltx, rtx); + var maxX = Math.max(lbx, rbx, ltx, rtx); + var minY = Math.min(lby, rby, lty, rty); + var maxY = Math.max(lby, rby, lty, rty); rect.x = minX; rect.y = minY;