From 277728182414ccc66c44473ff66e14e543f7012d Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Wed, 18 Jan 2023 16:58:10 +0100 Subject: [PATCH] slight adjustments for improved usability --- empty.js | 152 ++++++++++++++++++++++++++----------------------------- 1 file changed, 71 insertions(+), 81 deletions(-) diff --git a/empty.js b/empty.js index bd3be51..18216ae 100644 --- a/empty.js +++ b/empty.js @@ -56,14 +56,14 @@ document.addEventListener("DOMContentLoaded",function() { //ONLOAD var y = e.clientY - boxY; empty_new_mouse_x = e.clientX; empty_new_mouse_y = e.clientY; - if (typeof onmousemove == "function") { - onmousemove(x,y); + if (typeof onMouseMove == "function") { + onMouseMove(x,y); } }); document.addEventListener("keydown", function(e) { //KEY DOWN if (e.isComposing || e.keyCode === 229) { return; } - empty_setKey(e.key, e.keyCode, true); + empty_setKey(e.code, true); if (typeof onKeyDown == "function") { onKeyDown(e); } @@ -72,7 +72,7 @@ document.addEventListener("DOMContentLoaded",function() { //ONLOAD document.addEventListener("keyup", function(e) { //KEY UP if (e.isComposing || e.keyCode === 229) {return;} - empty_setKey(e.key, e.keyCode, false); + empty_setKey(e.code, false); if (typeof onKeyUp == "function") { onKeyUp(e); } @@ -127,8 +127,8 @@ document.addEventListener("DOMContentLoaded",function() { //ONLOAD var boxY = box.left; var x = empty_new_mouse_x - boxX; var y = empty_new_mouse_y - boxY; - if (typeof onmousemove == "function") { - onmousemove(x,y); + if (typeof onMouseMove == "function") { + onMouseMove(x,y); } }) @@ -140,7 +140,7 @@ document.addEventListener("DOMContentLoaded",function() { //ONLOAD var boxY = box.left; var x = empty_new_mouse_x - boxX; var y = empty_new_mouse_y - boxY; - if (typeof onmousemove == "function") { + if (typeof onMouseDown == "function") { onMouseDown(x,y,0); } }) @@ -156,7 +156,6 @@ document.addEventListener("DOMContentLoaded",function() { //ONLOAD empty_mouse_keys[0] = true; if (typeof onMouseDown == "function") { onMouseDown(x,y,0); - } }) document.addEventListener("touchend",function(e) { @@ -204,28 +203,56 @@ function empty_tick() { //TICK setTimeout(empty_tick,1000/FPS); } -function distance(x1,y1,x2,y2) { //DISTANCE - return Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2)); +function empty_fullscreen() { + if (FULLSCREEN) { + FULLSCREEN=false; + if(document.body.requestFullscreen) { + document.body.requestFullscreen(); + } else if(document.body.mozRequestFullScreen) { + document.body.mozRequestFullScreen(); + } else if(document.body.msRequestFullscreen) { + document.body.msRequestFullscreen(); + } else if(document.body.webkitRequestFullscreen) { + document.body.webkitRequestFullscreen(); + } else { + FULLSCREEN=true; + } + } } +function empty_resize() { //WINDOW RESIZE + if (WIDTH == -1) { + cv.width = window.innerWidth - BORDER * 2; + cv.style.marginLeft = 0; + } else { + cv.width = WIDTH - BORDER*2; + cv.style.marginLeft = (window.innerWidth - WIDTH) * LEFT; + } + if (HEIGHT == -1) { + cv.height = window.innerHeight - BORDER * 2; + cv.style.marginTop = 0; + } else { + cv.height = HEIGHT - BORDER*2; + cv.style.marginTop = (window.innerHeight - HEIGHT) * TOP; + } + if (typeof onresize=="function") { + onresize(window.innerWidth, window.innerHeight); + } +} + +function empty_setKey(code, value) { //SET KEYSTATE + empty_keys[code] = value; +} + + +// DATA FUNCTIONS + function getMouseKey(key) { //GET MOUSE KEY return empty_mouse_keys[key]; } -function keystateByCode(code) { //GET KEYSTATE - for (var i in empty_keys) { - if (empty_keys[i].code==code) { - return empty_keys[i].state; - } - } - return false; -} -function keystateByName(name) { - for (var i in empty_keys) { - if (empty_keys[i].name==name) { - return empty_keys[i].state; - } - } +function getKey(code) { //GET KEYSTATE + if (code in empty_keys) return empty_keys[code]; return false; } @@ -258,15 +285,8 @@ function getRelativeMouseMovement() { return ret; } -function empty_setKey(key, keycode, value) { //SET KEYSTATE - for (var i in empty_keys) { - if (empty_keys[i].code==keycode || empty_keys[i].name==key) { - empty_keys[i].state=value; - return; - } - } - empty_keys.push({code: keycode, name: key, state: value}); -} + +// DRAWING FUNCTIONS function fillBackground() { c.fillRect(0,0,cv.width,cv.height); @@ -293,6 +313,22 @@ function line(x1,y1,x2,y2) { c.stroke(); } +function drawRotatedImage(image,x,y,angle,width,height) { + var origin = {x: width/2, y: height/2}; + c.save(); + c.translate(x,y); + c.rotate(angle); + c.drawImage(image,-origin.x,-origin.y,width,height); + c.restore(); +} + + +// CALCULATION FUNCTIONS + +function distance(x1,y1,x2,y2) { //DISTANCE + return Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2)); +} + function randomRange(min,max) { //RANDOM RANGE return Math.floor(Math.random() * (max-min+1) + min); } @@ -352,10 +388,10 @@ function pointInCircle(px,py,cx,cy,cr) { } function rectsCollide(x1,y1,dx1,dy1,x2,y2,dx2,dy2) { - return (empty_2dcollide(x1,dx1,x2,dx2) && empty_2dcollide(y1,dy1,y2,dy2)) + return (empty_1dcollide(x1,dx1,x2,dx2) && empty_1dcollide(y1,dy1,y2,dy2)) } -function empty_2dcollide(x1,dx1,x2,dx2) { +function empty_1dcollide(x1,dx1,x2,dx2) { if (x2 >= x1 && x2 <= x1+dx1) { return true; } @@ -364,49 +400,3 @@ function empty_2dcollide(x1,dx1,x2,dx2) { } return false; } - -function empty_fullscreen() { - if (FULLSCREEN) { - FULLSCREEN=false; - if(document.body.requestFullscreen) { - document.body.requestFullscreen(); - } else if(document.body.mozRequestFullScreen) { - document.body.mozRequestFullScreen(); - } else if(document.body.msRequestFullscreen) { - document.body.msRequestFullscreen(); - } else if(document.body.webkitRequestFullscreen) { - document.body.webkitRequestFullscreen(); - } else { - FULLSCREEN=true; - } - } -} - -function empty_resize() { //WINDOW RESIZE - if (WIDTH==-1) { - cv.width=window.innerWidth-BORDER*2; - cv.style.marginLeft=0; - } else { - cv.width=WIDTH - BORDER*2; - cv.style.marginLeft=(window.innerWidth - WIDTH)*LEFT; - } - if (HEIGHT==-1) { - cv.height=window.innerHeight-BORDER*2; - cv.style.marginTop=0; - } else { - cv.height=HEIGHT - BORDER*2; - cv.style.marginTop=(window.innerHeight - HEIGHT)*TOP; - } - if (typeof onresize=="function") { - onresize(window.innerWidth, window.innerHeight); - } -} - -function drawRotatedImage(image,x,y,angle,width,height) { - var origin = {x: width/2, y: height/2}; - c.save(); - c.translate(x,y); - c.rotate(angle); - c.drawImage(image,-origin.x,-origin.y,width,height); - c.restore(); -}