slight adjustments for improved usability
This commit is contained in:
parent
a3a74e629c
commit
2777281824
152
empty.js
152
empty.js
|
@ -56,14 +56,14 @@ document.addEventListener("DOMContentLoaded",function() { //ONLOAD
|
||||||
var y = e.clientY - boxY;
|
var y = e.clientY - boxY;
|
||||||
empty_new_mouse_x = e.clientX;
|
empty_new_mouse_x = e.clientX;
|
||||||
empty_new_mouse_y = e.clientY;
|
empty_new_mouse_y = e.clientY;
|
||||||
if (typeof onmousemove == "function") {
|
if (typeof onMouseMove == "function") {
|
||||||
onmousemove(x,y);
|
onMouseMove(x,y);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
document.addEventListener("keydown", function(e) { //KEY DOWN
|
document.addEventListener("keydown", function(e) { //KEY DOWN
|
||||||
if (e.isComposing || e.keyCode === 229) { return; }
|
if (e.isComposing || e.keyCode === 229) { return; }
|
||||||
empty_setKey(e.key, e.keyCode, true);
|
empty_setKey(e.code, true);
|
||||||
if (typeof onKeyDown == "function") {
|
if (typeof onKeyDown == "function") {
|
||||||
onKeyDown(e);
|
onKeyDown(e);
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ document.addEventListener("DOMContentLoaded",function() { //ONLOAD
|
||||||
|
|
||||||
document.addEventListener("keyup", function(e) { //KEY UP
|
document.addEventListener("keyup", function(e) { //KEY UP
|
||||||
if (e.isComposing || e.keyCode === 229) {return;}
|
if (e.isComposing || e.keyCode === 229) {return;}
|
||||||
empty_setKey(e.key, e.keyCode, false);
|
empty_setKey(e.code, false);
|
||||||
if (typeof onKeyUp == "function") {
|
if (typeof onKeyUp == "function") {
|
||||||
onKeyUp(e);
|
onKeyUp(e);
|
||||||
}
|
}
|
||||||
|
@ -127,8 +127,8 @@ document.addEventListener("DOMContentLoaded",function() { //ONLOAD
|
||||||
var boxY = box.left;
|
var boxY = box.left;
|
||||||
var x = empty_new_mouse_x - boxX;
|
var x = empty_new_mouse_x - boxX;
|
||||||
var y = empty_new_mouse_y - boxY;
|
var y = empty_new_mouse_y - boxY;
|
||||||
if (typeof onmousemove == "function") {
|
if (typeof onMouseMove == "function") {
|
||||||
onmousemove(x,y);
|
onMouseMove(x,y);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -140,7 +140,7 @@ document.addEventListener("DOMContentLoaded",function() { //ONLOAD
|
||||||
var boxY = box.left;
|
var boxY = box.left;
|
||||||
var x = empty_new_mouse_x - boxX;
|
var x = empty_new_mouse_x - boxX;
|
||||||
var y = empty_new_mouse_y - boxY;
|
var y = empty_new_mouse_y - boxY;
|
||||||
if (typeof onmousemove == "function") {
|
if (typeof onMouseDown == "function") {
|
||||||
onMouseDown(x,y,0);
|
onMouseDown(x,y,0);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -156,7 +156,6 @@ document.addEventListener("DOMContentLoaded",function() { //ONLOAD
|
||||||
empty_mouse_keys[0] = true;
|
empty_mouse_keys[0] = true;
|
||||||
if (typeof onMouseDown == "function") {
|
if (typeof onMouseDown == "function") {
|
||||||
onMouseDown(x,y,0);
|
onMouseDown(x,y,0);
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
document.addEventListener("touchend",function(e) {
|
document.addEventListener("touchend",function(e) {
|
||||||
|
@ -204,28 +203,56 @@ function empty_tick() { //TICK
|
||||||
setTimeout(empty_tick,1000/FPS);
|
setTimeout(empty_tick,1000/FPS);
|
||||||
}
|
}
|
||||||
|
|
||||||
function distance(x1,y1,x2,y2) { //DISTANCE
|
function empty_fullscreen() {
|
||||||
return Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2));
|
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
|
function getMouseKey(key) { //GET MOUSE KEY
|
||||||
return empty_mouse_keys[key];
|
return empty_mouse_keys[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
function keystateByCode(code) { //GET KEYSTATE
|
function getKey(code) { //GET KEYSTATE
|
||||||
for (var i in empty_keys) {
|
if (code in empty_keys) return empty_keys[code];
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -258,15 +285,8 @@ function getRelativeMouseMovement() {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
function empty_setKey(key, keycode, value) { //SET KEYSTATE
|
|
||||||
for (var i in empty_keys) {
|
// DRAWING FUNCTIONS
|
||||||
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});
|
|
||||||
}
|
|
||||||
|
|
||||||
function fillBackground() {
|
function fillBackground() {
|
||||||
c.fillRect(0,0,cv.width,cv.height);
|
c.fillRect(0,0,cv.width,cv.height);
|
||||||
|
@ -293,6 +313,22 @@ function line(x1,y1,x2,y2) {
|
||||||
c.stroke();
|
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
|
function randomRange(min,max) { //RANDOM RANGE
|
||||||
return Math.floor(Math.random() * (max-min+1) + min);
|
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) {
|
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) {
|
if (x2 >= x1 && x2 <= x1+dx1) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -364,49 +400,3 @@ function empty_2dcollide(x1,dx1,x2,dx2) {
|
||||||
}
|
}
|
||||||
return false;
|
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();
|
|
||||||
}
|
|
||||||
|
|
Reference in New Issue