1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-03-02 03:39:21 +00:00

Allow to pan the map with the mouse by moving the cursor to the edges of the screen

This commit is contained in:
tobspr
2020-09-19 20:30:35 +02:00
parent d6f56da9e4
commit fbff0a0ad4
5 changed files with 484 additions and 412 deletions

View File

@@ -769,6 +769,7 @@ export class Camera extends BasicSerializableObject {
this.cameraUpdateTimeBucket -= physicsStepSizeMs;
this.internalUpdatePanning(now, physicsStepSizeMs);
this.internalUpdateMousePanning(now, physicsStepSizeMs);
this.internalUpdateZooming(now, physicsStepSizeMs);
this.internalUpdateCentering(now, physicsStepSizeMs);
this.internalUpdateShake(now, physicsStepSizeMs);
@@ -855,6 +856,61 @@ export class Camera extends BasicSerializableObject {
}
}
/**
* Internal screen panning handler
* @param {number} now
* @param {number} dt
*/
internalUpdateMousePanning(now, dt) {
if (!this.root.app.settings.getAllSettings().enableMousePan) {
// Not enabled
return;
}
const mousePos = this.root.app.mousePosition;
if (!mousePos) {
return;
}
if (this.desiredCenter || this.desiredZoom || this.currentlyMoving || this.currentlyPinching) {
// Performing another method of movement right now
return;
}
if (
mousePos.x < 0 ||
mousePos.y < 0 ||
mousePos.x > this.root.gameWidth ||
mousePos.y > this.root.gameHeight
) {
// Out of screen
return;
}
const panAreaPixels = Math.min(this.root.gameWidth, this.root.gameHeight) * 0.015;
const panVelocity = new Vector();
if (mousePos.x < panAreaPixels) {
panVelocity.x -= 1;
}
if (mousePos.x > this.root.gameWidth - panAreaPixels) {
panVelocity.x += 1;
}
if (mousePos.y < panAreaPixels) {
panVelocity.y -= 1;
}
if (mousePos.y > this.root.gameHeight - panAreaPixels) {
panVelocity.y += 1;
}
this.center = this.center.add(
panVelocity.multiplyScalar(
((0.5 * dt) / this.zoomLevel) * this.root.app.settings.getMovementSpeed()
)
);
}
/**
* Updates the non user interaction zooming
* @param {number} now Time now in seconds