Skip to content
Browse files

Emit ids of elements under the pointer if experimental option is enabled

  • Loading branch information...
1 parent e3ec4b2 commit 417254156ef245f92d828531d545be09e96d9487 @davidaurelio davidaurelio committed
Showing with 36 additions and 8 deletions.
  1. +36 −8 src/renderer/svg/svg_event_handlers.js
View
44 src/renderer/svg/svg_event_handlers.js
@@ -81,13 +81,14 @@ define([
* @param {PointerEvent} event The pointer event to dispatch
* @param {number} targetId The bonsai id of the event target
* @param {number} [relatedTargetId] The bonsai id of the related target, if any
+ * @param {Array} [elementsUnderPointerIds] An array of element ids under the mouse pointer
*/
- function emitMouseEvent(emitter, event, targetId, relatedTargetId) {
+ function emitMouseEvent(emitter, event, targetId, relatedTargetId, elementsUnderPointerIds) {
emitter.emit('userevent', event, targetId, relatedTargetId);
if (!TOUCH_SUPPORT) {
// If we're on a non-touch platform (e.g. regular desktop)
// then fire the mutli: event so we get cross-platform support:
- emitter.emit('userevent', event.clone('multi:' + event.type), targetId, relatedTargetId);
+ emitter.emit('userevent', event.clone('multi:' + event.type), targetId, relatedTargetId, elementsUnderPointerIds);
}
}
@@ -96,13 +97,14 @@ define([
* @param {PointerEvent} event The pointer event to dispatch
* @param {number} targetId The bonsai id of the event target
* @param {boolean} isMultiTouch Whether the touch is part of a multitouch gesture
+ * @param {Array} [elementsUnderPointerIds] An array of element ids under the finger tip
*/
- function emitTouchEvent(emitter, event, targetId, isMultiTouch) {
+ function emitTouchEvent(emitter, event, targetId, isMultiTouch, elementsUnderPointerIds) {
var type = event.type;
event.type = 'multi:' + type;
emitter.emit('userevent', event, targetId);
if (!isMultiTouch) {
- emitter.emit('userevent', event.clone(type), targetId);
+ emitter.emit('userevent', event.clone(type), targetId, null, elementsUnderPointerIds);
}
}
@@ -195,6 +197,11 @@ define([
var type = pointerEvent.type, x = pointerEvent.x, y = pointerEvent.y;
if (!type) { return; }
+ var elementIdsUnderPointer;
+ if (this.elementsUnderPointer) {
+ elementIdsUnderPointer = this.getElementIdsUnderPointer(x, y);
+ }
+
if (type === 'pointerdown') {
this._mouseDragId = targetId;
this._mouseDragStartX = x;
@@ -207,14 +214,14 @@ define([
var dragEvent = pointerEvent.clone('drag');
dragEvent.diffX = x - this._mouseDragStartX;
dragEvent.diffY = y - this._mouseDragStartY;
- emitMouseEvent(this, dragEvent, dragId);
+ emitMouseEvent(this, dragEvent, dragId, null, elementIdsUnderPointer);
}
} else if (type === 'pointerup') {
this._mouseDragId = this._mouseDragStartX = this._mouseDragStartY = undefined;
}
this._mouseMoveLastX = x;
this._mouseMoveLastY = y;
- emitMouseEvent(this, pointerEvent, targetId, relatedTargetId);
+ emitMouseEvent(this, pointerEvent, targetId, relatedTargetId, elementIdsUnderPointer);
},
/**
@@ -228,6 +235,12 @@ define([
if (!type) { return; }
var x = pointerEvent.x, y = pointerEvent.y;
+
+ var elementIdsUnderPointer;
+ if (this.elementsUnderPointer) {
+ elementIdsUnderPointer = this.getElementIdsUnderPointer(x, y);
+ }
+
var isMultiTouch = this._isMultiTouch;
var touchStates = this._touchStates || (this._touchStates = {});
@@ -244,12 +257,27 @@ define([
pointerEvent.diffY = y - touchData.dragStartY;
pointerEvent.deltaX = x - touchData.lastX;
pointerEvent.deltaY = y - touchData.lastY;
- emitTouchEvent(this, pointerEvent.clone('drag'), targetId, isMultiTouch);
+ emitTouchEvent(this, pointerEvent.clone('drag'), targetId, isMultiTouch, elementIdsUnderPointer);
}
touchData.lastX = x;
touchData.lastY = y;
}
- emitTouchEvent(this, pointerEvent, targetId, isMultiTouch);
+ emitTouchEvent(this, pointerEvent, targetId, isMultiTouch, elementIdsUnderPointer);
+ },
+
+ getElementIdsUnderPointer: function(x, y) {
+ var elements = checkIntersection(this.svg.root, x, y);
+ if (!elements) { return null; }
+ var ids = [], hasIds = false;
+ for (var i = 0, element; (element = elements[i]); i += 1) {
+ var id = getBonsaiIdOf(element);
+ if (id > 0 && ids.indexOf(id) !== 1) {
+ hasIds = true;
+ ids.push(id);
+ }
+ }
+
+ return hasIds ? ids : null;
}
};
});

0 comments on commit 4172541

Please sign in to comment.
Something went wrong with that request. Please try again.