mirror of
https://github.com/tobspr/shapez.io.git
synced 2024-10-27 20:34:29 +00:00
Fix mouse handling
This commit is contained in:
parent
4c20094878
commit
61bcc31861
@ -3,7 +3,7 @@ import { createLogger } from "../core/logging";
|
|||||||
import { Signal } from "../core/signal";
|
import { Signal } from "../core/signal";
|
||||||
import { fastArrayDelete, fastArrayDeleteValueIfContained } from "./utils";
|
import { fastArrayDelete, fastArrayDeleteValueIfContained } from "./utils";
|
||||||
import { Vector } from "./vector";
|
import { Vector } from "./vector";
|
||||||
import { IS_MOBILE } from "./config";
|
import { IS_MOBILE, SUPPORT_TOUCH } from "./config";
|
||||||
import { SOUNDS } from "../platform/sound";
|
import { SOUNDS } from "../platform/sound";
|
||||||
import { GLOBAL_APP } from "./globals";
|
import { GLOBAL_APP } from "./globals";
|
||||||
|
|
||||||
@ -119,16 +119,21 @@ export class ClickDetector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const options = this.internalGetEventListenerOptions();
|
const options = this.internalGetEventListenerOptions();
|
||||||
|
|
||||||
|
if (SUPPORT_TOUCH) {
|
||||||
this.element.removeEventListener("touchstart", this.handlerTouchStart, options);
|
this.element.removeEventListener("touchstart", this.handlerTouchStart, options);
|
||||||
this.element.removeEventListener("touchend", this.handlerTouchEnd, options);
|
this.element.removeEventListener("touchend", this.handlerTouchEnd, options);
|
||||||
this.element.removeEventListener("touchcancel", this.handlerTouchCancel, options);
|
this.element.removeEventListener("touchcancel", this.handlerTouchCancel, options);
|
||||||
|
}
|
||||||
|
|
||||||
this.element.removeEventListener("mouseup", this.handlerTouchStart, options);
|
this.element.removeEventListener("mouseup", this.handlerTouchStart, options);
|
||||||
this.element.removeEventListener("mousedown", this.handlerTouchEnd, options);
|
this.element.removeEventListener("mousedown", this.handlerTouchEnd, options);
|
||||||
this.element.removeEventListener("mouseout", this.handlerTouchCancel, options);
|
this.element.removeEventListener("mouseout", this.handlerTouchCancel, options);
|
||||||
|
|
||||||
if (this.captureTouchmove) {
|
if (this.captureTouchmove) {
|
||||||
|
if (SUPPORT_TOUCH) {
|
||||||
this.element.removeEventListener("touchmove", this.handlerTouchMove, options);
|
this.element.removeEventListener("touchmove", this.handlerTouchMove, options);
|
||||||
|
}
|
||||||
this.element.removeEventListener("mousemove", this.handlerTouchMove, options);
|
this.element.removeEventListener("mousemove", this.handlerTouchMove, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,16 +191,20 @@ export class ClickDetector {
|
|||||||
element.addEventListener("click", this.handlerPreventClick, options);
|
element.addEventListener("click", this.handlerPreventClick, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (SUPPORT_TOUCH) {
|
||||||
element.addEventListener("touchstart", this.handlerTouchStart, options);
|
element.addEventListener("touchstart", this.handlerTouchStart, options);
|
||||||
element.addEventListener("touchend", this.handlerTouchEnd, options);
|
element.addEventListener("touchend", this.handlerTouchEnd, options);
|
||||||
element.addEventListener("touchcancel", this.handlerTouchCancel, options);
|
element.addEventListener("touchcancel", this.handlerTouchCancel, options);
|
||||||
|
}
|
||||||
|
|
||||||
element.addEventListener("mousedown", this.handlerTouchStart, options);
|
element.addEventListener("mousedown", this.handlerTouchStart, options);
|
||||||
element.addEventListener("mouseup", this.handlerTouchEnd, options);
|
element.addEventListener("mouseup", this.handlerTouchEnd, options);
|
||||||
element.addEventListener("mouseout", this.handlerTouchCancel, options);
|
element.addEventListener("mouseout", this.handlerTouchCancel, options);
|
||||||
|
|
||||||
if (this.captureTouchmove) {
|
if (this.captureTouchmove) {
|
||||||
|
if (SUPPORT_TOUCH) {
|
||||||
element.addEventListener("touchmove", this.handlerTouchMove, options);
|
element.addEventListener("touchmove", this.handlerTouchMove, options);
|
||||||
|
}
|
||||||
element.addEventListener("mousemove", this.handlerTouchMove, options);
|
element.addEventListener("mousemove", this.handlerTouchMove, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,8 @@ export const IS_DEMO = queryParamOptions.fullVersion
|
|||||||
: (G_IS_PROD && !G_IS_STANDALONE) ||
|
: (G_IS_PROD && !G_IS_STANDALONE) ||
|
||||||
(typeof window !== "undefined" && window.location.search.indexOf("demo") >= 0);
|
(typeof window !== "undefined" && window.location.search.indexOf("demo") >= 0);
|
||||||
|
|
||||||
|
export const SUPPORT_TOUCH = false;
|
||||||
|
|
||||||
const smoothCanvas = true;
|
const smoothCanvas = true;
|
||||||
|
|
||||||
export const THIRDPARTY_URLS = {
|
export const THIRDPARTY_URLS = {
|
||||||
|
@ -8,7 +8,7 @@ import {
|
|||||||
performanceNow,
|
performanceNow,
|
||||||
} from "../core/builtins";
|
} from "../core/builtins";
|
||||||
import { clickDetectorGlobals } from "../core/click_detector";
|
import { clickDetectorGlobals } from "../core/click_detector";
|
||||||
import { globalConfig } from "../core/config";
|
import { globalConfig, SUPPORT_TOUCH } from "../core/config";
|
||||||
import { createLogger } from "../core/logging";
|
import { createLogger } from "../core/logging";
|
||||||
import { Rectangle } from "../core/rectangle";
|
import { Rectangle } from "../core/rectangle";
|
||||||
import { Signal, STOP_PROPAGATION } from "../core/signal";
|
import { Signal, STOP_PROPAGATION } from "../core/signal";
|
||||||
@ -312,32 +312,36 @@ export class Camera extends BasicSerializableObject {
|
|||||||
this.eventListenerMouseMove = this.onMouseMove.bind(this);
|
this.eventListenerMouseMove = this.onMouseMove.bind(this);
|
||||||
this.eventListenerMouseUp = this.onMouseUp.bind(this);
|
this.eventListenerMouseUp = this.onMouseUp.bind(this);
|
||||||
|
|
||||||
|
if (SUPPORT_TOUCH) {
|
||||||
this.root.canvas.addEventListener("touchstart", this.eventListenerTouchStart);
|
this.root.canvas.addEventListener("touchstart", this.eventListenerTouchStart);
|
||||||
this.root.canvas.addEventListener("touchend", this.eventListenerTouchEnd);
|
this.root.canvas.addEventListener("touchend", this.eventListenerTouchEnd);
|
||||||
this.root.canvas.addEventListener("touchcancel", this.eventListenerTouchEnd);
|
this.root.canvas.addEventListener("touchcancel", this.eventListenerTouchEnd);
|
||||||
this.root.canvas.addEventListener("touchmove", this.eventListenerTouchMove);
|
this.root.canvas.addEventListener("touchmove", this.eventListenerTouchMove);
|
||||||
|
}
|
||||||
|
|
||||||
this.root.canvas.addEventListener("wheel", this.eventListenerMousewheel);
|
this.root.canvas.addEventListener("wheel", this.eventListenerMousewheel);
|
||||||
this.root.canvas.addEventListener("mousedown", this.eventListenerMouseDown);
|
this.root.canvas.addEventListener("mousedown", this.eventListenerMouseDown);
|
||||||
this.root.canvas.addEventListener("mousemove", this.eventListenerMouseMove);
|
this.root.canvas.addEventListener("mousemove", this.eventListenerMouseMove);
|
||||||
this.root.canvas.addEventListener("mouseup", this.eventListenerMouseUp);
|
window.addEventListener("mouseup", this.eventListenerMouseUp);
|
||||||
this.root.canvas.addEventListener("mouseout", this.eventListenerMouseUp);
|
// this.root.canvas.addEventListener("mouseout", this.eventListenerMouseUp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cleans up all event listeners
|
* Cleans up all event listeners
|
||||||
*/
|
*/
|
||||||
cleanup() {
|
cleanup() {
|
||||||
|
if (SUPPORT_TOUCH) {
|
||||||
this.root.canvas.removeEventListener("touchstart", this.eventListenerTouchStart);
|
this.root.canvas.removeEventListener("touchstart", this.eventListenerTouchStart);
|
||||||
this.root.canvas.removeEventListener("touchend", this.eventListenerTouchEnd);
|
this.root.canvas.removeEventListener("touchend", this.eventListenerTouchEnd);
|
||||||
this.root.canvas.removeEventListener("touchcancel", this.eventListenerTouchEnd);
|
this.root.canvas.removeEventListener("touchcancel", this.eventListenerTouchEnd);
|
||||||
this.root.canvas.removeEventListener("touchmove", this.eventListenerTouchMove);
|
this.root.canvas.removeEventListener("touchmove", this.eventListenerTouchMove);
|
||||||
|
}
|
||||||
|
|
||||||
this.root.canvas.removeEventListener("wheel", this.eventListenerMousewheel);
|
this.root.canvas.removeEventListener("wheel", this.eventListenerMousewheel);
|
||||||
this.root.canvas.removeEventListener("mousedown", this.eventListenerMouseDown);
|
this.root.canvas.removeEventListener("mousedown", this.eventListenerMouseDown);
|
||||||
this.root.canvas.removeEventListener("mousemove", this.eventListenerMouseMove);
|
this.root.canvas.removeEventListener("mousemove", this.eventListenerMouseMove);
|
||||||
this.root.canvas.removeEventListener("mouseup", this.eventListenerMouseUp);
|
window.removeEventListener("mouseup", this.eventListenerMouseUp);
|
||||||
this.root.canvas.removeEventListener("mouseout", this.eventListenerMouseUp);
|
// this.root.canvas.removeEventListener("mouseout", this.eventListenerMouseUp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -343,12 +343,14 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
*/
|
*/
|
||||||
executeDirectionLockedPlacement() {
|
executeDirectionLockedPlacement() {
|
||||||
const path = this.computeDirectionLockPath();
|
const path = this.computeDirectionLockPath();
|
||||||
|
this.root.logic.performBulkOperation(() => {
|
||||||
for (let i = 0; i < path.length; ++i) {
|
for (let i = 0; i < path.length; ++i) {
|
||||||
const { rotation, tile } = path[i];
|
const { rotation, tile } = path[i];
|
||||||
|
|
||||||
this.currentBaseRotation = rotation;
|
this.currentBaseRotation = rotation;
|
||||||
this.tryPlaceCurrentBuildingAt(tile);
|
this.tryPlaceCurrentBuildingAt(tile);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -6,7 +6,9 @@ import { Vector } from "../core/vector";
|
|||||||
import { SOUNDS } from "../platform/sound";
|
import { SOUNDS } from "../platform/sound";
|
||||||
|
|
||||||
const avgSoundDurationSeconds = 0.25;
|
const avgSoundDurationSeconds = 0.25;
|
||||||
const maxOngoingSounds = 10;
|
const maxOngoingSounds = 2;
|
||||||
|
|
||||||
|
const maxOngoingUiSounds = 2;
|
||||||
|
|
||||||
// Proxy to the application sound instance
|
// Proxy to the application sound instance
|
||||||
export class SoundProxy {
|
export class SoundProxy {
|
||||||
@ -17,7 +19,8 @@ export class SoundProxy {
|
|||||||
this.root = root;
|
this.root = root;
|
||||||
|
|
||||||
// Store a list of sounds and when we started them
|
// Store a list of sounds and when we started them
|
||||||
this.playingSounds = [];
|
this.playing3DSounds = [];
|
||||||
|
this.playingUiSounds = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,7 +29,14 @@ export class SoundProxy {
|
|||||||
*/
|
*/
|
||||||
playUi(id) {
|
playUi(id) {
|
||||||
assert(typeof id === "string", "Not a valid sound id: " + id);
|
assert(typeof id === "string", "Not a valid sound id: " + id);
|
||||||
|
this.internalUpdateOngoingSounds();
|
||||||
|
if (this.playingUiSounds.length > maxOngoingUiSounds) {
|
||||||
|
// Too many ongoing sounds
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
this.root.app.sound.playUiSound(id);
|
this.root.app.sound.playUiSound(id);
|
||||||
|
this.playingUiSounds.push(this.root.time.realtimeNow());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -53,13 +63,13 @@ export class SoundProxy {
|
|||||||
assert(pos instanceof Vector, "Invalid sound position");
|
assert(pos instanceof Vector, "Invalid sound position");
|
||||||
this.internalUpdateOngoingSounds();
|
this.internalUpdateOngoingSounds();
|
||||||
|
|
||||||
if (this.playingSounds.length > maxOngoingSounds) {
|
if (this.playing3DSounds.length > maxOngoingSounds) {
|
||||||
// Too many ongoing sounds
|
// Too many ongoing sounds
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.root.app.sound.play3DSound(id, pos, this.root);
|
this.root.app.sound.play3DSound(id, pos, this.root);
|
||||||
this.playingSounds.push(this.root.time.realtimeNow());
|
this.playing3DSounds.push(this.root.time.realtimeNow());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,9 +78,16 @@ export class SoundProxy {
|
|||||||
*/
|
*/
|
||||||
internalUpdateOngoingSounds() {
|
internalUpdateOngoingSounds() {
|
||||||
const now = this.root.time.realtimeNow();
|
const now = this.root.time.realtimeNow();
|
||||||
for (let i = 0; i < this.playingSounds.length; ++i) {
|
for (let i = 0; i < this.playing3DSounds.length; ++i) {
|
||||||
if (now - this.playingSounds[i] > avgSoundDurationSeconds) {
|
if (now - this.playing3DSounds[i] > avgSoundDurationSeconds) {
|
||||||
this.playingSounds.splice(i, 1);
|
this.playing3DSounds.splice(i, 1);
|
||||||
|
i -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < this.playingUiSounds.length; ++i) {
|
||||||
|
if (now - this.playingUiSounds[i] > avgSoundDurationSeconds) {
|
||||||
|
this.playingUiSounds.splice(i, 1);
|
||||||
i -= 1;
|
i -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user