From 2dec257dd9651cb44c9fd0055045121e8180d91f Mon Sep 17 00:00:00 2001 From: tobspr Date: Sun, 21 Jun 2020 18:11:41 +0200 Subject: [PATCH] Cleanup click detector code --- src/js/game/canvas_click_interceptor.js | 70 ------------------- src/js/game/core.js | 2 - .../game/hud/parts/building_placer_logic.js | 21 +++--- src/js/game/root.js | 4 -- 4 files changed, 8 insertions(+), 89 deletions(-) delete mode 100644 src/js/game/canvas_click_interceptor.js diff --git a/src/js/game/canvas_click_interceptor.js b/src/js/game/canvas_click_interceptor.js deleted file mode 100644 index 675c1387..00000000 --- a/src/js/game/canvas_click_interceptor.js +++ /dev/null @@ -1,70 +0,0 @@ -import { STOP_PROPAGATION } from "../core/signal"; -import { GameRoot } from "./root"; -import { ClickDetector } from "../core/click_detector"; -import { createLogger } from "../core/logging"; - -const logger = createLogger("canvas_click_interceptor"); - -export class CanvasClickInterceptor { - /** - * @param {GameRoot} root - */ - constructor(root) { - this.root = root; - - this.root.signals.postLoadHook.add(this.initialize, this); - this.root.signals.aboutToDestruct.add(this.cleanup, this); - - /** @type {Array} */ - this.interceptors = []; - } - - initialize() { - this.clickDetector = new ClickDetector(this.root.canvas, { - applyCssClass: null, - captureTouchmove: false, - targetOnly: true, - preventDefault: true, - maxDistance: 13, - clickSound: null, - }); - this.clickDetector.click.add(this.onCanvasClick, this); - this.clickDetector.rightClick.add(this.onCanvasRightClick, this); - - if (this.root.hud.parts.buildingPlacer) { - this.interceptors.push(this.root.hud.parts.buildingPlacer); - } - - logger.log("Registered", this.interceptors.length, "interceptors"); - } - - cleanup() { - if (this.clickDetector) { - this.clickDetector.cleanup(); - } - this.interceptors = []; - } - - onCanvasClick(position, event, cancelAction = false) { - if (!this.root.gameInitialized) { - logger.warn("Skipping click outside of game initiaization!"); - return; - } - - if (this.root.hud.hasBlockingOverlayOpen()) { - return; - } - - for (let i = 0; i < this.interceptors.length; ++i) { - const interceptor = this.interceptors[i]; - if (interceptor.onCanvasClick(position, cancelAction) === STOP_PROPAGATION) { - // log(this, "Interceptor", interceptor.constructor.name, "catched click"); - break; - } - } - } - - onCanvasRightClick(position, event) { - this.onCanvasClick(position, event, true); - } -} diff --git a/src/js/game/core.js b/src/js/game/core.js index 8b1c464d..3fc02397 100644 --- a/src/js/game/core.js +++ b/src/js/game/core.js @@ -17,7 +17,6 @@ import { SavegameSerializer } from "../savegame/savegame_serializer"; import { AutomaticSave } from "./automatic_save"; import { MetaHubBuilding } from "./buildings/hub"; import { Camera } from "./camera"; -import { CanvasClickInterceptor } from "./canvas_click_interceptor"; import { EntityManager } from "./entity_manager"; import { GameSystemManager } from "./game_system_manager"; import { HubGoals } from "./hub_goals"; @@ -98,7 +97,6 @@ export class GameCore { root.logic = new GameLogic(root); root.hud = new GameHUD(root); root.time = new GameTime(root); - root.canvasClickInterceptor = new CanvasClickInterceptor(root); root.automaticSave = new AutomaticSave(root); root.soundProxy = new SoundProxy(root); diff --git a/src/js/game/hud/parts/building_placer_logic.js b/src/js/game/hud/parts/building_placer_logic.js index 92ce44bd..647fe5b9 100644 --- a/src/js/game/hud/parts/building_placer_logic.js +++ b/src/js/game/hud/parts/building_placer_logic.js @@ -243,20 +243,10 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart { * @param {boolean} cancelAction */ onCanvasClick(mousePos, cancelAction = false) { - if (cancelAction) { - if (this.currentMetaBuilding.get()) { - this.currentMetaBuilding.set(null); - } else { - this.deleteBelowCursor(); - } + // Prevent any other canvas clicks + if (this.currentMetaBuilding.get()) { return STOP_PROPAGATION; } - - if (!this.currentMetaBuilding.get()) { - return; - } - - return STOP_PROPAGATION; } /** @@ -479,13 +469,18 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart { } // Deletion - if (button === enumMouseButton.right && !this.currentMetaBuilding.get()) { + if (button === enumMouseButton.right && !metaBuilding) { this.currentlyDragging = true; this.currentlyDeleting = true; this.lastDragTile = this.root.camera.screenToWorld(pos).toTileSpace(); this.currentMetaBuilding.set(null); return STOP_PROPAGATION; } + + // Cancel placement + if (button === enumMouseButton.right && metaBuilding) { + this.currentMetaBuilding.set(null); + } } /** diff --git a/src/js/game/root.js b/src/js/game/root.js index cc6007de..0456952c 100644 --- a/src/js/game/root.js +++ b/src/js/game/root.js @@ -19,7 +19,6 @@ import { SoundProxy } from "./sound_proxy"; import { Savegame } from "../savegame/savegame"; import { GameLogic } from "./logic"; import { ShapeDefinitionManager } from "./shape_definition_manager"; -import { CanvasClickInterceptor } from "./canvas_click_interceptor"; import { HubGoals } from "./hub_goals"; import { BufferMaintainer } from "../core/buffer_maintainer"; import { ProductionAnalytics } from "./production_analytics"; @@ -110,9 +109,6 @@ export class GameRoot { /** @type {BufferMaintainer} */ this.buffers = null; - /** @type {CanvasClickInterceptor} */ - this.canvasClickInterceptor = null; - /** @type {AutomaticSave} */ this.automaticSave = null;