Skip to content

Commit

Permalink
handles mouseButton for multi touches
Browse files Browse the repository at this point in the history
  • Loading branch information
diyaayay committed Dec 6, 2024
1 parent 9cf3c8c commit 0d19067
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions src/events/pointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,11 +969,19 @@ function pointer(p5, fn){
id: touch.pointerId,
};
}
fn._setMouseButton = function(e) {
this.mouseButton.left = (e.buttons & 1) !== 0;
this.mouseButton.center = (e.buttons & 4) !== 0;
this.mouseButton.right = (e.buttons & 2) !== 0;
};

fn._setMouseButton = function(e) {
// Check all active touches to determine button states
this.mouseButton.left = Array.from(this._activeTouches.values()).some(touch =>
(touch.buttons & 1) !== 0
);
this.mouseButton.center = Array.from(this._activeTouches.values()).some(touch =>
(touch.buttons & 4) !== 0
);
this.mouseButton.right = Array.from(this._activeTouches.values()).some(touch =>
(touch.buttons & 2) !== 0
);
};

/**
* A function that's called when the mouse moves.
Expand Down Expand Up @@ -1152,10 +1160,9 @@ function pointer(p5, fn){
const context = this._isGlobal ? window : this;
let executeDefault;
this._updatePointerCoords(e);

if(e.pointerType === 'touch') {
this._activeTouches.set(e.pointerId, e);
}
this._activeTouches.set(e.pointerId, e);
this._setMouseButton(e);


if (!this.mouseIsPressed && typeof context.mouseMoved === 'function') {
executeDefault = context.mouseMoved(e);
Expand All @@ -1167,8 +1174,6 @@ function pointer(p5, fn){
if (executeDefault === false) {
e.preventDefault();
}
} else {
this._setMouseButton(e);
}
};

Expand Down Expand Up @@ -1319,13 +1324,9 @@ function pointer(p5, fn){
let executeDefault;
this.mouseIsPressed = true;

if (e.pointerType === 'touch') {
this._activeTouches.set(e.pointerId, e);
} else {
this._setMouseButton(e);
}

this._updatePointerCoords(e);
this._activeTouches.set(e.pointerId, e);
this._setMouseButton(e);
this._updatePointerCoords(e);

if (typeof context.mousePressed === 'function') {
executeDefault = context.mousePressed(e);
Expand Down Expand Up @@ -1482,12 +1483,9 @@ function pointer(p5, fn){
const context = this._isGlobal ? window : this;
let executeDefault;
this.mouseIsPressed = false;

if(e.pointerType == 'touch'){
this._activeTouches.delete(e.pointerId);
} else {
this._setMouseButton(e);
}

this._activeTouches.delete(e.pointerId);
this._setMouseButton(e);

this._updatePointerCoords(e);

Expand Down

0 comments on commit 0d19067

Please sign in to comment.