From 99ba787b9d15f6f17efb6973c3ecaa3be513014c Mon Sep 17 00:00:00 2001 From: yukirij Date: Tue, 17 Dec 2024 18:46:41 -0800 Subject: [PATCH] Add touch event handling. --- www/js/game.js | 6 +----- www/js/interface.js | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/www/js/game.js b/www/js/game.js index 41dff17..cdf0f55 100644 --- a/www/js/game.js +++ b/www/js/game.js @@ -7,8 +7,6 @@ GAME.Board = class { this.columns = [ ]; for(let i = 0; i < 9; ++i) { this.columns.push(new GAME.Column()); } this.pieces = [ ]; - console.log(config); - if(config !== null) { for(let i = 0; i < config.count_pieces(); ++i) { this.pieces.push(null); } @@ -227,7 +225,7 @@ GAME.Game = class { } clone() { - let game = new GAME.Game(); + let game = new GAME.Game(this.config); game.turn = this.turn; game.board = this.board.clone(); @@ -313,8 +311,6 @@ GAME.Game = class { if(moves == 0) { this.state.code = GAME.Const.State.Checkmate; } - - console.log(moves); } process(play) { diff --git a/www/js/interface.js b/www/js/interface.js index 97fa1b3..e934020 100644 --- a/www/js/interface.js +++ b/www/js/interface.js @@ -349,6 +349,18 @@ const INTERFACE = { INTERFACE.game_step(); }, + click_touch(event) { + let rect = event.target.getBoundingClientRect(); + let touch = event.tocuhes[0] || event.changedTouches[0]; + let x = touch.clientX - rect.left; + let y = touch.clientY - rect.top; + + this.hover({ offsetX:x, offsetY:y }); + this.click({ button:0 }); + + event.preventDefault(); + }, + contextmenu(event) { INTERFACE_DATA.select = null; if(!event.ctrlKey) { @@ -376,6 +388,18 @@ const INTERFACE = { } }, + release_touch(event) { + let rect = event.target.getBoundingClientRect(); + let touch = event.tocuhes[0] || event.changedTouches[0]; + let x = touch.clientX - rect.left; + let y = touch.clientY - rect.top; + + this.hover({ offsetX:x, offsetY:y }); + this.release({ button:0 }); + + event.preventDefault(); + }, + resize() { let width = INTERFACE_DATA.canvas.width = INTERFACE_DATA.canvas.clientWidth * (window.devicePixelRatio || 1); let height = INTERFACE_DATA.canvas.height = INTERFACE_DATA.canvas.clientHeight * (window.devicePixelRatio || 1); @@ -1289,7 +1313,9 @@ const INTERFACE = { canvas.addEventListener("mousemove", INTERFACE.hover); canvas.addEventListener("mouseout", INTERFACE.unhover); canvas.addEventListener("mousedown", INTERFACE.click); + canvas.addEventListener("touchstart", INTERFACE.click_touch); canvas.addEventListener("mouseup", INTERFACE.release); + canvas.addEventListener("touchend", INTERFACE.release_touch); canvas.addEventListener("contextmenu", INTERFACE.contextmenu); window.addEventListener("resize", INTERFACE.step); @@ -1921,7 +1947,7 @@ const INTERFACE = { } }, - run_test(source=0, from=0, to=0, all=true) + run_test(source=0, from=0, to=0, all=false) { let next_source = source; let next_from = from; @@ -1962,7 +1988,7 @@ const INTERFACE = { ]); if(all && next_source < 3) { - setTimeout(INTERFACE.run_test, 100, next_source, next_from, next_to); + setTimeout(INTERFACE.run_test, 100, next_source, next_from, next_to, true); } } };