Fix mouse handling

This commit is contained in:
tobspr 2020-06-17 14:56:21 +02:00
parent 4c20094878
commit 61bcc31861
5 changed files with 68 additions and 34 deletions

View File

@ -3,7 +3,7 @@ import { createLogger } from "../core/logging";
import { Signal } from "../core/signal";
import { fastArrayDelete, fastArrayDeleteValueIfContained } from "./utils";
import { Vector } from "./vector";
import { IS_MOBILE } from "./config";
import { IS_MOBILE, SUPPORT_TOUCH } from "./config";
import { SOUNDS } from "../platform/sound";
import { GLOBAL_APP } from "./globals";
@ -119,16 +119,21 @@ export class ClickDetector {
}
}
const options = this.internalGetEventListenerOptions();
this.element.removeEventListener("touchstart", this.handlerTouchStart, options);
this.element.removeEventListener("touchend", this.handlerTouchEnd, options);
this.element.removeEventListener("touchcancel", this.handlerTouchCancel, options);
if (SUPPORT_TOUCH) {
this.element.removeEventListener("touchstart", this.handlerTouchStart, options);
this.element.removeEventListener("touchend", this.handlerTouchEnd, options);
this.element.removeEventListener("touchcancel", this.handlerTouchCancel, options);
}
this.element.removeEventListener("mouseup", this.handlerTouchStart, options);
this.element.removeEventListener("mousedown", this.handlerTouchEnd, options);
this.element.removeEventListener("mouseout", this.handlerTouchCancel, options);
if (this.captureTouchmove) {
this.element.removeEventListener("touchmove", this.handlerTouchMove, options);
if (SUPPORT_TOUCH) {
this.element.removeEventListener("touchmove", 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("touchstart", this.handlerTouchStart, options);
element.addEventListener("touchend", this.handlerTouchEnd, options);
element.addEventListener("touchcancel", this.handlerTouchCancel, options);
if (SUPPORT_TOUCH) {
element.addEventListener("touchstart", this.handlerTouchStart, options);
element.addEventListener("touchend", this.handlerTouchEnd, options);
element.addEventListener("touchcancel", this.handlerTouchCancel, options);
}
element.addEventListener("mousedown", this.handlerTouchStart, options);
element.addEventListener("mouseup", this.handlerTouchEnd, options);
element.addEventListener("mouseout", this.handlerTouchCancel, options);
if (this.captureTouchmove) {
element.addEventListener("touchmove", this.handlerTouchMove, options);
if (SUPPORT_TOUCH) {
element.addEventListener("touchmove", this.handlerTouchMove, options);
}
element.addEventListener("mousemove", this.handlerTouchMove, options);
}

View File

@ -12,6 +12,8 @@ export const IS_DEMO = queryParamOptions.fullVersion
: (G_IS_PROD && !G_IS_STANDALONE) ||
(typeof window !== "undefined" && window.location.search.indexOf("demo") >= 0);
export const SUPPORT_TOUCH = false;
const smoothCanvas = true;
export const THIRDPARTY_URLS = {

View File

@ -8,7 +8,7 @@ import {
performanceNow,
} from "../core/builtins";
import { clickDetectorGlobals } from "../core/click_detector";
import { globalConfig } from "../core/config";
import { globalConfig, SUPPORT_TOUCH } from "../core/config";
import { createLogger } from "../core/logging";
import { Rectangle } from "../core/rectangle";
import { Signal, STOP_PROPAGATION } from "../core/signal";
@ -312,32 +312,36 @@ export class Camera extends BasicSerializableObject {
this.eventListenerMouseMove = this.onMouseMove.bind(this);
this.eventListenerMouseUp = this.onMouseUp.bind(this);
this.root.canvas.addEventListener("touchstart", this.eventListenerTouchStart);
this.root.canvas.addEventListener("touchend", this.eventListenerTouchEnd);
this.root.canvas.addEventListener("touchcancel", this.eventListenerTouchEnd);
this.root.canvas.addEventListener("touchmove", this.eventListenerTouchMove);
if (SUPPORT_TOUCH) {
this.root.canvas.addEventListener("touchstart", this.eventListenerTouchStart);
this.root.canvas.addEventListener("touchend", this.eventListenerTouchEnd);
this.root.canvas.addEventListener("touchcancel", this.eventListenerTouchEnd);
this.root.canvas.addEventListener("touchmove", this.eventListenerTouchMove);
}
this.root.canvas.addEventListener("wheel", this.eventListenerMousewheel);
this.root.canvas.addEventListener("mousedown", this.eventListenerMouseDown);
this.root.canvas.addEventListener("mousemove", this.eventListenerMouseMove);
this.root.canvas.addEventListener("mouseup", this.eventListenerMouseUp);
this.root.canvas.addEventListener("mouseout", this.eventListenerMouseUp);
window.addEventListener("mouseup", this.eventListenerMouseUp);
// this.root.canvas.addEventListener("mouseout", this.eventListenerMouseUp);
}
/**
* Cleans up all event listeners
*/
cleanup() {
this.root.canvas.removeEventListener("touchstart", this.eventListenerTouchStart);
this.root.canvas.removeEventListener("touchend", this.eventListenerTouchEnd);
this.root.canvas.removeEventListener("touchcancel", this.eventListenerTouchEnd);
this.root.canvas.removeEventListener("touchmove", this.eventListenerTouchMove);
if (SUPPORT_TOUCH) {
this.root.canvas.removeEventListener("touchstart", this.eventListenerTouchStart);
this.root.canvas.removeEventListener("touchend", this.eventListenerTouchEnd);
this.root.canvas.removeEventListener("touchcancel", this.eventListenerTouchEnd);
this.root.canvas.removeEventListener("touchmove", this.eventListenerTouchMove);
}
this.root.canvas.removeEventListener("wheel", this.eventListenerMousewheel);
this.root.canvas.removeEventListener("mousedown", this.eventListenerMouseDown);
this.root.canvas.removeEventListener("mousemove", this.eventListenerMouseMove);
this.root.canvas.removeEventListener("mouseup", this.eventListenerMouseUp);
this.root.canvas.removeEventListener("mouseout", this.eventListenerMouseUp);
window.removeEventListener("mouseup", this.eventListenerMouseUp);
// this.root.canvas.removeEventListener("mouseout", this.eventListenerMouseUp);
}
/**

View File

@ -343,12 +343,14 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
*/
executeDirectionLockedPlacement() {
const path = this.computeDirectionLockPath();
for (let i = 0; i < path.length; ++i) {
const { rotation, tile } = path[i];
this.root.logic.performBulkOperation(() => {
for (let i = 0; i < path.length; ++i) {
const { rotation, tile } = path[i];
this.currentBaseRotation = rotation;
this.tryPlaceCurrentBuildingAt(tile);
}
this.currentBaseRotation = rotation;
this.tryPlaceCurrentBuildingAt(tile);
}
});
}
/**

View File

@ -6,7 +6,9 @@ import { Vector } from "../core/vector";
import { SOUNDS } from "../platform/sound";
const avgSoundDurationSeconds = 0.25;
const maxOngoingSounds = 10;
const maxOngoingSounds = 2;
const maxOngoingUiSounds = 2;
// Proxy to the application sound instance
export class SoundProxy {
@ -17,7 +19,8 @@ export class SoundProxy {
this.root = root;
// 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) {
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.playingUiSounds.push(this.root.time.realtimeNow());
}
/**
@ -53,13 +63,13 @@ export class SoundProxy {
assert(pos instanceof Vector, "Invalid sound position");
this.internalUpdateOngoingSounds();
if (this.playingSounds.length > maxOngoingSounds) {
if (this.playing3DSounds.length > maxOngoingSounds) {
// Too many ongoing sounds
return false;
}
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;
}
@ -68,9 +78,16 @@ export class SoundProxy {
*/
internalUpdateOngoingSounds() {
const now = this.root.time.realtimeNow();
for (let i = 0; i < this.playingSounds.length; ++i) {
if (now - this.playingSounds[i] > avgSoundDurationSeconds) {
this.playingSounds.splice(i, 1);
for (let i = 0; i < this.playing3DSounds.length; ++i) {
if (now - this.playing3DSounds[i] > avgSoundDurationSeconds) {
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;
}
}