Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions cocos2d/platform/CCEGLView.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -134,6 +135,28 @@ cc.EGLView = cc.Class.extend(/** @lends cc.EGLView# */{
return true;
},

/**
* <p>
* The resolution translate on EGLView
* </p>
* @param {Number} translateX
* @param {Number} translateY
*/
setContentTranslateLeftTop: function(offsetLeft, offsetTop){
this._contentTranslateLeftTop = {left : offsetLeft, top : offsetTop};
},

/**
* <p>
* get the resolution translate on EGLView
* </p>
* @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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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();
},
Expand Down Expand Up @@ -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:
Expand Down
130 changes: 77 additions & 53 deletions cocos2d/touch_dispatcher/CCTouchDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -732,25 +728,57 @@ 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);

//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;
Expand All @@ -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];
Expand All @@ -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);
}
Expand All @@ -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];
Expand All @@ -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);
}
Expand All @@ -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];
Expand All @@ -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);
}
Expand All @@ -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];
Expand All @@ -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);
}
Expand Down