I’ve implemented HTML/CSS UI via a CSS kit for responsive user interfaces within my game.
The issue is that press the ESC key is required to click on the buttons, etc.
I enable the pointer lock upon a player deploying into the world, yet I would like to disable it upon hovering over the UI so it may properly read the events.
How would they stop moving the camera in this case? It sounds they would have to press another button/do an action to stop being in control of the camera which would be Esc, or something like I for inventory etc.
I’m attempting to do so via the onRelease and onPress mouse events, toggling the enable/disable pointerLock functions when a user either isn’t pressing the screen (camera movement) so the mouse events can be read on the HTML/CSS UI.
The only fix right now is changing the camera movement to the right mouse button.
this.app.mouse.disableContextMenu(); // Disable right-click
this.app.mouse.on("mousemove", this.onMouseMove, this);
this.app.mouse.on('mousedown', this.onPress, this);
this.app.mouse.on('mouseup', this.onRelease, this);
// Listen to when the mouse travels out of the window
window.addEventListener('mouseout', this.onMouseOut, false);
// Unsubscribe from events on destroy
this.on('destroy', function() {
this.app.mouse.off("mousemove", this.onMouseMove, this);
this.app.mouse.off("mousedown", this.onPress, this);
this.app.mouse.off('mouseup', this.onRelease, this);
window.removeEventListener('mouseout', onMouseOut, false);
});
};
CameraMovement.prototype.onPress = function(e) {
window.focus();
//console.log('MOUSE DOWN/PRESS');
// e.button
switch (e.button) {
case (pc.MOUSEBUTTON_LEFT):
break;
case (pc.MOUSEBUTTON_MIDDLE):
break;
case (pc.MOUSEBUTTON_RIGHT):
if (this.clientHandler.playerDeployed && this.clientHandler.allowPlayerMovement) {
this.app.mouse.enablePointerLock();
console.log('LOCK ON:', pc.Mouse.isPointerLocked());
}
break;
default:
break;
}
};
CameraMovement.prototype.onRelease = function(e) {
//console.log('MOUSE RELEASE');
this.app.mouse.disablePointerLock();
console.log('LOCK: OFF', pc.Mouse.isPointerLocked());
};
CameraMovement.prototype.onMouseMove = function (e) {
if (this.app.mouse.isPressed(pc.MOUSEBUTTON_RIGHT) && !this.app.xr.active) {
console.log('CAM movement');
this.eulers.x -= ((this.mouseSpeed * e.dx) / 60) % 360;
this.eulers.y += ((this.mouseSpeed * e.dy) / 60) % 360;
if (this.eulers.x < 0) this.eulers.x += 360;
if (this.eulers.y < 0) this.eulers.y += 360;
// Restrict camera rotation if in first person
if (this.activateFirstPerson) {
if (this.eulers.y <= 378 && this.eulers.y > 296) {
} else {
//console.log('restrict camera movement');
this.eulers.y -= ((this.mouseSpeed * e.dy) / 60) % 360;
}
}
}
};
The HTML UI could stop the event propergration when interactived with so the PlayCanvas event doesn’t get the event. I can’t remember what the order is.
You may also would like to try having your own abstract input layer that listens to the mouse events and checks if the UI is being interacted with before sending its own event that your game listens to for mouse events.
Here’s a quick example based off that HTML/CSS project you have linked: https://playcanvas.com/editor/scene/1302577 (notice that you can’t move the camera while over the HTML container for the UI in the top left)