diff --git a/cocos2d/platform/CCEGLView.js b/cocos2d/platform/CCEGLView.js index 0794f83dc6..76a3ce2f06 100644 --- a/cocos2d/platform/CCEGLView.js +++ b/cocos2d/platform/CCEGLView.js @@ -68,6 +68,7 @@ cc.EGLView = cc.Class.extend(/** @lends cc.EGLView# */{ initialize:function () { this._initialize = true; this._adjustSize(); + this._contentTranslateLeftTop = {left: 0, top: 0}; var adjustSize = this._adjustSize.bind(this); window.addEventListener('resize', adjustSize, false); @@ -134,6 +135,28 @@ cc.EGLView = cc.Class.extend(/** @lends cc.EGLView# */{ return true; }, + /** + *
+ * The resolution translate on EGLView + *
+ * @param {Number} translateX + * @param {Number} translateY + */ + setContentTranslateLeftTop: function(offsetLeft, offsetTop){ + this._contentTranslateLeftTop = {left : offsetLeft, top : offsetTop}; + }, + + /** + *+ * get the resolution translate on EGLView + *
+ * @param {Number} translateX + * @param {Number} translateY + */ + getContentTranslateLeftTop: function(){ + return this._contentTranslateLeftTop; + }, + /** * Get the frame size of EGL view. * In general, it returns the screen size since the EGL view is a fullscreen view. @@ -173,7 +196,9 @@ cc.EGLView = cc.Class.extend(/** @lends cc.EGLView# */{ return cc.p(0, 0); } }, - + canSetContentScaleFactor:function() { + return true; + }, /** * Set the design resolution size. * @param {Number} width Design resolution width. @@ -223,14 +248,10 @@ cc.EGLView = cc.Class.extend(/** @lends cc.EGLView# */{ diretor._winSizeInPoints = this.getDesignResolutionSize(); if (cc.renderContextType == cc.CANVAS) { - var width, height; + var width = 0, height = 0; switch (this._resolutionPolicy) { case cc.RESOLUTION_POLICY.EXACTFIT: - width = 0; - height = 0; case cc.RESOLUTION_POLICY.NOBORDER: - width = 0; - height = 0; case cc.RESOLUTION_POLICY.SHOW_ALL: width = (this._screenSize.width - viewPortW) / 2; height = -(this._screenSize.height - viewPortH) / 2; @@ -242,6 +263,9 @@ cc.EGLView = cc.Class.extend(/** @lends cc.EGLView# */{ } cc.renderContext.translate(width, height); cc.renderContext.scale(this._scaleX, this._scaleY); + + diretor.setContentScaleFactor(this._scaleX); + this.setContentTranslateLeftTop(width, height); } //diretor.setGLDefaultValues(); }, @@ -320,7 +344,18 @@ cc.EGLView = cc.Class.extend(/** @lends cc.EGLView# */{ getScaleY:function () { return this._scaleY; }, + + /** + * Get the real location in view + */ + convertToLocationInView: function(tx, ty, relatedPos){ + var pos = this.getContentTranslateLeftTop(); + var x = (pos.left + tx - relatedPos.left) / cc.Director.getInstance().getContentScaleFactor(); + var y = (pos.top + relatedPos.height - (ty - relatedPos.top)) / cc.Director.getInstance().getContentScaleFactor(); + + return {x: x, y: y}; + }, /** * Touch events are handled by default; if you want to customize your handlers, please override these functions: diff --git a/cocos2d/touch_dispatcher/CCTouchDispatcher.js b/cocos2d/touch_dispatcher/CCTouchDispatcher.js index 821be0ef73..13bda8bace 100644 --- a/cocos2d/touch_dispatcher/CCTouchDispatcher.js +++ b/cocos2d/touch_dispatcher/CCTouchDispatcher.js @@ -629,19 +629,19 @@ cc.ProcessMouseupEvent = function (element, event) { ty = event.clientY; } - var mouseX = (tx - pos.left) / cc.Director.getInstance().getContentScaleFactor(); - var mouseY = (pos.height - (ty - pos.top)) / cc.Director.getInstance().getContentScaleFactor(); + var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos); - var touch = new cc.Touch(mouseX, mouseY); + var touch = new cc.Touch(location.x, location.y); touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y); - cc.TouchDispatcher.preTouchPoint.x = mouseX; - cc.TouchDispatcher.preTouchPoint.y = mouseY; + cc.TouchDispatcher.preTouchPoint.x = location.x; + cc.TouchDispatcher.preTouchPoint.y = location.y; var posArr = []; posArr.push(touch); //csx cc.Director.getInstance().getTouchDispatcher().touchesEnded(posArr, null); cc.EGLView.getInstance().touchesEnded(posArr, null); }; + /** * @param {HTMLCanvasElement|HTMLDivElement} element */ @@ -670,13 +670,11 @@ cc.TouchDispatcher.registerHtmlElementEvent = function (element) { } if (!cc.rectContainsPoint(new cc.Rect(pos.left, pos.top, pos.width, pos.height), cc.p(tx, ty))) { - var mouseX = (tx - pos.left) / cc.Director.getInstance().getContentScaleFactor(); - var mouseY = (pos.height - (ty - pos.top)) / cc.Director.getInstance().getContentScaleFactor(); - - var touch = new cc.Touch(mouseX, mouseY); + var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos); + var touch = new cc.Touch(location.x, location.y); touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y); - cc.TouchDispatcher.preTouchPoint.x = mouseX; - cc.TouchDispatcher.preTouchPoint.y = mouseY; + cc.TouchDispatcher.preTouchPoint.x = location.x; + cc.TouchDispatcher.preTouchPoint.y = location.y; var posArr = []; posArr.push(touch); @@ -700,13 +698,11 @@ cc.TouchDispatcher.registerHtmlElementEvent = function (element) { ty = event.clientY; } - var mouseX = (tx - pos.left) / cc.Director.getInstance().getContentScaleFactor(); - var mouseY = (pos.height - (ty - pos.top)) / cc.Director.getInstance().getContentScaleFactor(); - - var touch = new cc.Touch(mouseX, mouseY); + var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos); + var touch = new cc.Touch(location.x, location.y); touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y); - cc.TouchDispatcher.preTouchPoint.x = mouseX; - cc.TouchDispatcher.preTouchPoint.y = mouseY; + cc.TouchDispatcher.preTouchPoint.x = location.x; + cc.TouchDispatcher.preTouchPoint.y = location.y; var posArr = []; posArr.push(touch); @@ -732,17 +728,14 @@ cc.TouchDispatcher.registerHtmlElementEvent = function (element) { ty = event.clientY; } - var mouseX = (tx - pos.left) / cc.Director.getInstance().getContentScaleFactor(); - var mouseY = (pos.height - (ty - pos.top)) / cc.Director.getInstance().getContentScaleFactor(); - - var touch = new cc.Touch(mouseX, mouseY); - + var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos); + var touch = new cc.Touch(location.x, location.y); //TODO this feature only chrome support //if((event.button == 0) && (event.which == 1)) // touch._setPressed(true); touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y); - cc.TouchDispatcher.preTouchPoint.x = mouseX; - cc.TouchDispatcher.preTouchPoint.y = mouseY; + cc.TouchDispatcher.preTouchPoint.x = location.x; + cc.TouchDispatcher.preTouchPoint.y = location.y; var posArr = []; posArr.push(touch); @@ -750,7 +743,42 @@ cc.TouchDispatcher.registerHtmlElementEvent = function (element) { //csx cc.Director.getInstance().getTouchDispatcher().touchesMoved(posArr, null); cc.EGLView.getInstance().touchesMoved(posArr, null); }); - } else { + } + else if(window.navigator.msPointerEnabled){ + var _pointerEventsMap = { + "MSPointerDown" : "touchesBegan", + "MSPointerMove" : "touchesMoved", + "MSPointerUp" : "touchesEnded", + "MSPointerCancel" : "touchesCancelled" + }; + + for(var i in _pointerEventsMap){ + (function(_pointerEvent, _touchEvent){ + element.addEventListener(_pointerEvent, function (event){ + var pos = cc.getHTMLElementPosition(element); + + pos.left -= document.body.scrollLeft; + pos.top -= document.body.scrollTop; + + var tx, ty, touch, preLocation; + tx = event.clientX; + ty = event.clientY; + + var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos); + var touch = new cc.Touch(location.x, location.y); + touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y); + cc.TouchDispatcher.preTouchPoint.x = location.x; + cc.TouchDispatcher.preTouchPoint.y = location.y; + + cc.Director.getInstance().getTouchDispatcher()[_touchEvent]([touch], null); + event.stopPropagation(); + event.preventDefault(); + }, false); + })(i, _pointerEventsMap[i]); + } + } + else { + //register canvas touch event element.addEventListener("touchstart", function (event) { if (!event.changedTouches) return; @@ -761,7 +789,7 @@ cc.TouchDispatcher.registerHtmlElementEvent = function (element) { pos.left -= document.body.scrollLeft; pos.top -= document.body.scrollTop; - var touch_event, tx, ty, mouseX, mouseY, touch, preLocation; + var touch_event, tx, ty, touch, preLocation; var length = event.changedTouches.length; for (var i = 0; i < length; i++) { touch_event = event.changedTouches[i]; @@ -771,21 +799,20 @@ cc.TouchDispatcher.registerHtmlElementEvent = function (element) { tx = touch_event.clientX; ty = touch_event.clientY; - mouseX = (tx - pos.left) / cc.Director.getInstance().getContentScaleFactor(); - mouseY = (pos.height - (ty - pos.top)) / cc.Director.getInstance().getContentScaleFactor(); + var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos); touch = null; if (touch_event.hasOwnProperty("identifier")) { - touch = new cc.Touch(mouseX, mouseY, touch_event.identifier); + touch = new cc.Touch(location.x, location.y, touch_event.identifier); //use Touch Pool preLocation = cc.TouchDispatcher._getPreTouch(touch).getLocation(); touch._setPrevPoint(preLocation.x, preLocation.y); cc.TouchDispatcher._setPreTouch(touch); } else { - touch = new cc.Touch(mouseX, mouseY); + touch = new cc.Touch(location.x, location.y); touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y); } - cc.TouchDispatcher.preTouchPoint.x = mouseX; - cc.TouchDispatcher.preTouchPoint.y = mouseY; + cc.TouchDispatcher.preTouchPoint.x = location.x; + cc.TouchDispatcher.preTouchPoint.y = location.y; posArr.push(touch); } @@ -805,7 +832,7 @@ cc.TouchDispatcher.registerHtmlElementEvent = function (element) { pos.left -= document.body.scrollLeft; pos.top -= document.body.scrollTop; - var touch_event, tx, ty, mouseX, mouseY, touch, preLocation; + var touch_event, tx, ty, touch, preLocation; var length = event.changedTouches.length; for (var i = 0; i < length; i++) { touch_event = event.changedTouches[i]; @@ -815,22 +842,21 @@ cc.TouchDispatcher.registerHtmlElementEvent = function (element) { tx = touch_event.clientX; ty = touch_event.clientY; - mouseX = (tx - pos.left) / cc.Director.getInstance().getContentScaleFactor(); - mouseY = (pos.height - (ty - pos.top)) / cc.Director.getInstance().getContentScaleFactor(); + var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos); touch = null; if (touch_event.hasOwnProperty("identifier")) { - touch = new cc.Touch(mouseX, mouseY, touch_event.identifier); + touch = new cc.Touch(location.x, location.y, touch_event.identifier); //use Touch Pool preLocation = cc.TouchDispatcher._getPreTouch(touch).getLocation(); touch._setPrevPoint(preLocation.x, preLocation.y); cc.TouchDispatcher._setPreTouch(touch); } else { - touch = new cc.Touch(mouseX, mouseY); + touch = new cc.Touch(location.x, location.y); touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y); } - cc.TouchDispatcher.preTouchPoint.x = mouseX; - cc.TouchDispatcher.preTouchPoint.y = mouseY; + cc.TouchDispatcher.preTouchPoint.x = location.x; + cc.TouchDispatcher.preTouchPoint.y = location.y; posArr.push(touch); } @@ -850,7 +876,7 @@ cc.TouchDispatcher.registerHtmlElementEvent = function (element) { pos.left -= document.body.scrollLeft; pos.top -= document.body.scrollTop; - var touch_event, tx, ty, mouseX, mouseY, touch, preLocation; + var touch_event, tx, ty, touch, preLocation; var length = event.changedTouches.length; for (var i = 0; i < length; i++) { touch_event = event.changedTouches[i]; @@ -860,22 +886,21 @@ cc.TouchDispatcher.registerHtmlElementEvent = function (element) { tx = touch_event.clientX; ty = touch_event.clientY; - mouseX = (tx - pos.left) / cc.Director.getInstance().getContentScaleFactor(); - mouseY = (pos.height - (ty - pos.top)) / cc.Director.getInstance().getContentScaleFactor(); + var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos); touch = null; if (touch_event.hasOwnProperty("identifier")) { - touch = new cc.Touch(mouseX, mouseY, touch_event.identifier); + touch = new cc.Touch(location.x, location.y, touch_event.identifier); //use Touch Pool preLocation = cc.TouchDispatcher._getPreTouch(touch).getLocation(); touch._setPrevPoint(preLocation.x, preLocation.y); cc.TouchDispatcher._deletePreTouchWithSameId(touch); } else { - touch = new cc.Touch(mouseX, mouseY); + touch = new cc.Touch(location.x, location.y); touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y); } - cc.TouchDispatcher.preTouchPoint.x = mouseX; - cc.TouchDispatcher.preTouchPoint.y = mouseY; + cc.TouchDispatcher.preTouchPoint.x = location.x; + cc.TouchDispatcher.preTouchPoint.y = location.y; posArr.push(touch); } @@ -895,7 +920,7 @@ cc.TouchDispatcher.registerHtmlElementEvent = function (element) { pos.left -= document.body.scrollLeft; pos.top -= document.body.scrollTop; - var touch_event, tx, ty, mouseX, mouseY, touch, preLocation; + var touch_event, tx, ty, touch, preLocation; var length = event.changedTouches.length; for (var i = 0; i < length; i++) { touch_event = event.changedTouches[i]; @@ -905,22 +930,21 @@ cc.TouchDispatcher.registerHtmlElementEvent = function (element) { tx = touch_event.clientX; ty = touch_event.clientY; - mouseX = (tx - pos.left) / cc.Director.getInstance().getContentScaleFactor(); - mouseY = (pos.height - (ty - pos.top)) / cc.Director.getInstance().getContentScaleFactor(); + var location = cc.EGLView.getInstance().convertToLocationInView(tx, ty, pos); touch = null; if (touch_event.hasOwnProperty("identifier")) { - touch = new cc.Touch(mouseX, mouseY, touch_event.identifier); + touch = new cc.Touch(location.x, location.y, touch_event.identifier); //use Touch Pool preLocation = cc.TouchDispatcher._getPreTouch(touch).getLocation(); touch._setPrevPoint(preLocation.x, preLocation.y); cc.TouchDispatcher._deletePreTouchWithSameId(touch); } else { - touch = new cc.Touch(mouseX, mouseY); + touch = new cc.Touch(location.x, location.y); touch._setPrevPoint(cc.TouchDispatcher.preTouchPoint.x, cc.TouchDispatcher.preTouchPoint.y); } - cc.TouchDispatcher.preTouchPoint.x = mouseX; - cc.TouchDispatcher.preTouchPoint.y = mouseY; + cc.TouchDispatcher.preTouchPoint.x = location.x; + cc.TouchDispatcher.preTouchPoint.y = location.y; posArr.push(touch); }