Add touch event handling.

This commit is contained in:
yukirij 2024-12-17 18:46:41 -08:00
parent 6acee5d23b
commit 99ba787b9d
2 changed files with 29 additions and 7 deletions

View File

@ -7,8 +7,6 @@ GAME.Board = class {
this.columns = [ ]; for(let i = 0; i < 9; ++i) { this.columns.push(new GAME.Column()); } this.columns = [ ]; for(let i = 0; i < 9; ++i) { this.columns.push(new GAME.Column()); }
this.pieces = [ ]; this.pieces = [ ];
console.log(config);
if(config !== null) { if(config !== null) {
for(let i = 0; i < config.count_pieces(); ++i) { this.pieces.push(null); } for(let i = 0; i < config.count_pieces(); ++i) { this.pieces.push(null); }
@ -227,7 +225,7 @@ GAME.Game = class {
} }
clone() { clone() {
let game = new GAME.Game(); let game = new GAME.Game(this.config);
game.turn = this.turn; game.turn = this.turn;
game.board = this.board.clone(); game.board = this.board.clone();
@ -313,8 +311,6 @@ GAME.Game = class {
if(moves == 0) { if(moves == 0) {
this.state.code = GAME.Const.State.Checkmate; this.state.code = GAME.Const.State.Checkmate;
} }
console.log(moves);
} }
process(play) { process(play) {

View File

@ -349,6 +349,18 @@ const INTERFACE = {
INTERFACE.game_step(); 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) { contextmenu(event) {
INTERFACE_DATA.select = null; INTERFACE_DATA.select = null;
if(!event.ctrlKey) { 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() { resize() {
let width = INTERFACE_DATA.canvas.width = INTERFACE_DATA.canvas.clientWidth * (window.devicePixelRatio || 1); 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); 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("mousemove", INTERFACE.hover);
canvas.addEventListener("mouseout", INTERFACE.unhover); canvas.addEventListener("mouseout", INTERFACE.unhover);
canvas.addEventListener("mousedown", INTERFACE.click); canvas.addEventListener("mousedown", INTERFACE.click);
canvas.addEventListener("touchstart", INTERFACE.click_touch);
canvas.addEventListener("mouseup", INTERFACE.release); canvas.addEventListener("mouseup", INTERFACE.release);
canvas.addEventListener("touchend", INTERFACE.release_touch);
canvas.addEventListener("contextmenu", INTERFACE.contextmenu); canvas.addEventListener("contextmenu", INTERFACE.contextmenu);
window.addEventListener("resize", INTERFACE.step); 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_source = source;
let next_from = from; let next_from = from;
@ -1962,7 +1988,7 @@ const INTERFACE = {
]); ]);
if(all && next_source < 3) { 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);
} }
} }
}; };