Skip to content

Commit

Permalink
Merge branch 'bugfix-mousewheel' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
csvurt committed Mar 9, 2015
2 parents 906589c + 0200c74 commit 4ea9ac7
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 19 deletions.
82 changes: 63 additions & 19 deletions src/renderer/renderer/events/mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var OPTION_MOVEMENT_AWARE_CLICK_HANDLER = "renderer-movement-aware-click-handler
XML3D.options.register(OPTION_MOUSEMOVE_PICKING, true);
XML3D.options.register(OPTION_MOVEMENT_AWARE_CLICK_HANDLER, false);

var EVENTS = ["click", "dblclick", "mousedown", "mouseup", "mouseover", "mousemove", "mouseout", "mousewheel"];
var EVENTS = ["click", "dblclick", "mousedown", "mouseup", "mouseover", "mousemove", "mouseout", "wheel"];

/**
*
Expand All @@ -17,6 +17,15 @@ var MouseEventHandler = function(defaultTarget, canvasHandler) {
this._lastMousePosition = {x: 0, y: 0};
};

var supportsEventConstructors = (function() {
try {
new MouseEvent("click", {});
return true;
} catch (e){
return false;
}
})();

MouseEventHandler.prototype = {

/**
Expand All @@ -32,23 +41,40 @@ MouseEventHandler.prototype = {
var noCopy = opt.noCopy || false;
// Copy event to avoid DOM dispatch errors (cannot dispatch event more
// than once)
event = noCopy ? event : this.copyMouseEvent(event);
if (!noCopy) {
event = this.copyMouseEvent(event);
}
this.initExtendedMouseEvent(event, x, y);

target.dispatchEvent(event);
},

/**
* @param {MouseEvent} event the event to copy
* @param {MouseEvent|WheelEvent} event the event to copy
* @return {MouseEvent} the new event
*/
copyMouseEvent: function (event) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent(event.type, // canBubble, cancelable, view, detail
event.bubbles, event.cancelable, event.view, event.detail, // screenX, screenY, clientX, clientY
event.screenX, event.screenY, event.clientX, event.clientY, // ctrl, alt, shift, meta, button
event.ctrlKey, event.altKey, event.shiftKey, event.metaKey, event.button, // relatedTarget
event.relatedTarget);
var evt;
if (supportsEventConstructors) {
if (event.toString() === "[object WheelEvent]") {
evt = new WheelEvent(event.type, event);
} else {
evt = new MouseEvent(event.type, event);
}
} else {
//These event APIs are deprecated but still required by IE, which doesn't support event constructors yet
if (event.toString() === "[object WheelEvent]") {
evt = document.createEvent("WheelEvent");
evt.initWheelEvent(event.type, event.canBubble, event.cancelable, event.view, event.detail,
event.screenX, event.screenY, event.clientX, event.clientY, event.button, event.relatedTarget, "",
event.deltaX, event.deltaY, event.deltaZ, event.deltaMode);
} else {
evt = document.createEvent("MouseEvent");
evt.initMouseEvent(event.type, event.canBubble, event.cancelable, event.view, event.detail,
event.screenX, event.screenY, event.clientX, event.clientY, event.ctrlKey, event.altKey,
event.shiftKey, event.metaKey, event.button, event.relatedTarget);
}
}
if (event.dataTransfer)
evt.data = {url: event.dataTransfer.getData("URL"), text: event.dataTransfer.getData("Text")};
// override preventDefault to actually prevent the default of the original event
Expand All @@ -60,9 +86,31 @@ MouseEventHandler.prototype = {

createMouseEvent: function (type, opts) {
opts = opts || {};
var event = document.createEvent("MouseEvents");
event.initMouseEvent(type, opts.canBubble !== undefined ? opts.canBubble : true, opts.cancelable !== undefined ? opts.cancelable : true, opts.view || window, opts.detail != undefined ? opts.detail : 0, opts.screenX != undefined ? opts.screenX : 0, opts.screenY != undefined ? opts.screenY : 0, opts.clientX != undefined ? opts.clientX : 0, opts.clientY != undefined ? opts.clientY : 0, opts.ctrl != undefined ? opts.ctrl : false, opts.alt != undefined ? opts.alt : false, opts.shift != undefined ? opts.shift : false, opts.meta != undefined ? opts.meta : false, opts.button != undefined ? opts.button : 0, opts.relatedTarget);
return event;
var dict = {
bubbles : opts.bubbles !== undefined ? opts.bubbles : true,
cancelable : opts.cancelable !== undefined ? opts.cancelable : true,
view : opts.view || window,
detail : opts.detail !== undefined ? opts.detail : 0,
screenX : opts.screenX !== undefined ? opts.screenX : 0,
screenY : opts.screenY !== undefined ? opts.screenY : 0,
clientX : opts.clientX !== undefined ? opts.clientX : 0,
clientY : opts.clientY !== undefined ? opts.clientY : 0,
ctrl : opts.ctrl !== undefined ? opts.ctrl : false,
alt : opts.alt !== undefined ? opts.alt : false,
shift : opts.shift !== undefined ? opts.shift : false,
meta : opts.meta !== undefined ? opts.meta : false,
button : opts.button !== undefined ? opts.button : 0,
relatedTarget : opts.relatedTarget
};
if (supportsEventConstructors) {
return new MouseEvent(type, dict);
} else {
var evt = document.createEvent("MouseEvent");
evt.initMouseEvent(type, dict.bubbles, dict.cancelable, dict.view, dict.detail,
dict.screenX, dict.screenY, dict.clientX, dict.clientY, dict.ctrlKey, dict.altKey,
dict.shiftKey, dict.metaKey, dict.button, dict.relatedTarget);
return evt;
}
},

/**
Expand Down Expand Up @@ -93,10 +141,7 @@ MouseEventHandler.prototype = {
}
return cachedPosition;
});

})();


},

/**
Expand Down Expand Up @@ -221,11 +266,10 @@ MouseEventHandler.prototype = {
},

/**
* @param {MouseEvent} evt
* @param {WheelEvent} evt
*/
mousewheel: function (evt) {
// note: mousewheel type is not W3C standard, used in WebKit!
this.dispatchMouseEventOnPickedObject(evt);
wheel: function (evt) {
this.dispatchMouseEventOnPickedObject(evt, { type: "wheel" });
}


Expand Down
45 changes: 45 additions & 0 deletions tests/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,51 @@ test("Mousedown Event", function() {
test("Mouseup Event", function() {
trippleMouseEventCheck.call(this, 'mouseup');
});
test("Wheel Event", function() {
var mesh01 = this.doc.getElementById("myMesh01");
mesh01.addEventListener("wheel", function(evt){
ok(true, "#myMesh01 mesh event received");
ok(evt.toString() === "[object WheelEvent]", "Event was of the WheelEvent class");
ok(evt.deltaX === 5 && evt.deltaMode === 1 && event.type === "wheel", "Event was the right WheelEvent");
equal(evt.target, mesh01, "Event target is #myMesh01");
start();
});
var opts = {
canBubble: true,
cancelable: true,
view: this.xml3dEl.ownerDocument.defaultView,
detail: 1,
deltaX: 5,
deltaY: 0,
deltaZ: 0,
deltaMode: 1,
screenX: 0,
screenY: 0,
clientX: 157,
clientY: 104,
relatedTarget: null
};
var eventConstructorsSupported = (function() {
try {
new WheelEvent("wheel", {});
return true;
} catch(e) {
return false;
}
})();
stop();
var event;
if (eventConstructorsSupported) {
event = new WheelEvent("wheel", opts);
} else {
// IE 11...
event = this.doc.createEvent("WheelEvent");
event.initWheelEvent("wheel", opts.canBubble, opts.cancelable, opts.view, opts.detail,
opts.screenX, opts.screenY, opts.clientX, opts.clientY, opts.button, opts.relatedTarget, "", opts.deltaX,
opts.deltaY, opts.deltaZ, opts.deltaMode);
}
this.xml3dEl._configured.canvas.dispatchEvent(event);
});

module("Event tests", {
setup : function() {
Expand Down

0 comments on commit 4ea9ac7

Please sign in to comment.