src/input/mouse.js in entityjs-0.3.2 vs src/input/mouse.js in entityjs-0.4.0

- old
+ new

@@ -4,13 +4,16 @@ @usage re.e('mouse') .on('mousedown:middle', function(m){ //m.x - x position //m.y - y position - //m.scrX - screen x position - //m.scrY - screen y position + //m.screenX - screen x position + //m.screenY - screen y position }) + +FUTURE: rename triggers to the standard format +mousemove -> mouse:move */ re.c('mouse') .statics({ l:[], @@ -48,33 +51,49 @@ }, event:function(e, extra){ + var canvas = re.sys.canvas; //calculate mouse coordinate - var x = e.offsetX; - var y = e.offsetY; + var x = canvas.width / canvas.offsetWidth; + var y = canvas.height / canvas.offsetHeight; - var that = re.c('mouse'); + //calculate offset + if(e.offsetX != null){ //chrome, opera + x *= e.offsetX; + y *= e.offsetY; + } else { //firefox + x *= e.layerX - canvas.offsetLeft; + y *= e.layerY - canvas.offsetTop; + } + + var listeners = re.c('mouse').l; + /* if(re.preventDefault && re.preventDefault.d[key]){ e.preventDefault(); } */ var c, t, obj; - for(var i=0; i<that.l.length; i++){ - t = that.l[i]; - obj = {posX:x, posY:y}; - obj.screenX = re.screen.toScreenX(x); - obj.screenY = re.screen.toScreenY(y); + for(var i=0; i<listeners.length; i++){ + t = listeners[i]; + if(t.screenable){ + x = re.screen.toScreenX(x); + y = re.screen.toScreenY(y); + } - t.trigger(e.type, obj, e); + //offset mouse coordinates + var tx = x + t.offX; + var ty = y + t.offY; + t.trigger(e.type, tx, ty, e); + if(extra){ - t.trigger(e.type+':'+extra, obj, e); + t.trigger(e.type+':'+extra, tx, ty, e); } } }, @@ -86,9 +105,13 @@ re.listener('click', this.event, c); re.listener('dblclick', this.event, c); re.listener('contextmenu', this.event, c); } +}) +.defaults({ + offX:0, + offY:0 }) .init(function(c){ //add to listener array c.l.push(this); }) \ No newline at end of file