Move files and prepare for rewrite

This commit is contained in:
danbulant 2019-12-30 18:59:50 +01:00
parent 8e9015e6ba
commit 07bd26a9a6
11 changed files with 3394 additions and 873 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules

File diff suppressed because it is too large Load diff

295
html/gamepad/input.js Normal file
View file

@ -0,0 +1,295 @@
/**
* (C) Daniel Bulant
* This file IS NOT subject to the LICENSE.
* Permission is hereby granted by Daniel Bulant to be used exclusively in console-hub (unless otherwise stated).
* Want to use this script? Contact me at admin@danbulant.eu
*/
class EventEmitter {
constructor() {
var delegate = document.createDocumentFragment();
[
'addEventListener',
'dispatchEvent',
'removeEventListener'
].forEach(f =>
this[f] = (...xs) => delegate[f](...xs)
)
}
}
class Controls extends EventEmitter {
players = [];
binds = {};
gamepads = 0;
gamepadArray = {};
axisMinimum = 0.7;
constructor() {
super();
this.isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
//Gamepad support
if (!this.hasGamepads()) {
console.log("Gamepads not supported!");
} else {
this.initGamepads();
}
var player = this.addPlayer();
console.log("(DEFAULT) Player joined using Keyboard");
player.controlledBy = {
type: "keyboard",
id: 1
}
this.defaultBindings(player, "KEY1");
var interval = setInterval(this.update, 1000 / 60, this, this.binds);//update the properties at 60FPS
this.listenForKeys();
}
listenForKeys() {
this.keys = {};
window.onkeyup = e => { this.keys[e.key] = false; if (e.key == "Tab") { e.preventDefault() } }
window.onkeydown = e => { this.keys[e.key] = true; if (e.key == "Tab") { e.preventDefault() } }
}
update(controls, bindings) {
controls.players.forEach((player) => {
var gamepad = {};
if (player.controlledBy.type == "controller") {
gamepad = navigator.getGamepads()[player.controlledBy.id];
}
var binds = bindings[player.id];
for (var bind in binds) {
if (!binds.hasOwnProperty(bind)) continue;
var isPressed = controls.checkPressed(binds[bind], gamepad);
if (isPressed && isPressed != player[bind + "Pressed"]) {
var event = new Event(player.id + "-" + bind);
event.player = player;
event.bindings = binds;
controls.dispatchEvent(event);
}
if (isPressed) {
var event = new Event("while_" + player.id + "-" + bind);
event.player = player;
event.bindings = binds;
controls.dispatchEvent(event);
}
player[bind + "Pressed"] = isPressed;
}
});
}
while(player, event, callback) {
return this.addEventListener("while_" + player + "-" + event, callback);
}
on(player, event, callback) {
return this.addEventListener(player + "-" + event, callback);
}
once(player, event, callback) {
var controls = this;
return this.addEventListener(player + "-" + event, function cb(e) {
controls.removeEventListener(e.type, cb);
callback(e);
});
}
checkPressed(str, gamepad) {
var bind = this.parseBind(str);
if (bind.type == "button") {
return gamepad.buttons[bind.value].pressed;
}
if (bind.type == "key") {
return this.keys[bind.value] == true;
}
if (bind.type == "unknown") {
return null;
}
if (bind.type == "axis") {
var value = gamepad.axes[bind.value];
if (bind.axis == "+") {
return value > this.axisMinimum;
} else if (bind.axis == "-") {
return value < -this.axisMinimum;
}
}
return null;
}
parseBind(str) {
var bind = {};
if (str.substr(0, 1) == "b") {
bind.type = "button";
} else if (str.substr(0, 1) == "a") {
bind.type = "axis";
} else if (str.substr(0, 1) == "k") {
bind.type = "key";
str = str.substr(1);//remove the -
} else {
bind.type = "unknown";
}
str = str.substr(1);
if (bind.type == "axis") {
bind.axis = str.substr(str.length - 1);
str = str.substr(0, str.length - 1);
} else {
bind.axis = null;
}
bind.value = str;
return bind;
}
initGamepads() {
var controls = this;
window.addEventListener("gamepadconnected", function (e) {
console.log("Player joined using %s", e.gamepad.id);
controls.gamepads++;
controls.gamepadArray[e.gamepad.index] = e.gamepad;
var player = controls.addPlayer();
player.controlledBy = {
type: "controller",
id: e.gamepad.index
}
controls.defaultBindings(player, "DS4");
});
window.addEventListener("gamepaddisconnected", function (e) {
console.log("Player %d disconnected", e.gamepad.index);
controls.gamepads--;
controls.gamepadArray[e.gamepad.index] = undefined;
controls.removeBindings()
});
}
defaultBindings(player, type) {
var binding = {};
switch (type) {
case "DS4":
binding = {
home: "b16",
action: "b0",
jump: "b1",
forward: "a1-",
backward: "a1+",
left: "a0-",
right: "a0+",
options: "b9"
}
break;
case "KEY1":
binding = {
home: "k-Tab",
action: "k-Enter",
jump: "k- ",
forward: "k-w",
backward: "k-s",
left: "k-a",
right: "k-d",
options: "k-Escape"
}
break;
}
this.binds[player.id] = binding
}
/**
* Try vibrating
* @param {*} player player which controller is to be vibrated
* @param {*} length ms to vibrate
* @param {*} weak percents to vibrate
* @param {*} strong percents to vibrate
* @returns {Boolean} if vibration was possible (is supported)
*/
vibrate(player, length = 200, weak = 100, strong = 100) {
try {
if (player.controlledBy.type == "controller") {
navigator.getGamepads()[player.controlledBy.id].vibrationActuator.playEffect("dual-rumble", {
startDelay: 0,
duration: length,
weakMagnitude: weak / 100,
strongMagnitude: strong / 100
});
return true;
} else {
return false;
}
} catch (e) {
console.log(e);
return false;
}
}
/**
* DEBUG
*/
reportOnGamepad() {
var gp = navigator.getGamepads()[0];
var html = "";
html += "id: " + gp.id + "<br/>";
for (var i = 0; i < gp.buttons.length; i++) {
html += "Button " + (i + 1) + ": ";
if (gp.buttons[i].pressed) html += " pressed";
html += "<br/>";
}
for (var i = 0; i < gp.axes.length; i += 2) {
html += "Stick " + (Math.ceil(i / 2) + 1) + ": " + gp.axes[i] + ", " + gp.axes[i + 1] + "<br/>";
}
document.getElementById("gamepadDisplay").innerHTML = html;
}
hasGamepads() {
return "getGamepads" in navigator;
}
playerCount() {
return this.players.length;
}
addPlayer() {
var player = this.players[this.players.length] = {};
player.id = this.players.length;
player.forwardPressed = false;
player.backwardPressed = false;
player.rightPressed = false;
player.leftPressed = false;
player.jumpPressed = false;
player.actionPressed = false;
player.homePressed = false;
player.optionsPressed = false;
player.controlledBy = null;
return player;
}
addBinding(player, key, action) {
if (!this.binds[player]) this.binds[player] = {};
if (!this.binds[player][key]) this.binds[player][key] = [];
this.binds[player][key][this.binds[player][key].length] = action;
return this;//chaining
}
removeBinding(player, key, action = null) {
if (action = null) {
this.binds[player][key] = [];
} else {
this.removeItem(this.binds[player][key], action);
}
return this;
}
removeBindings(player) {
this.binds[player] = {};
}
removeItem(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax = arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
}

View file

@ -1,79 +1,79 @@
$(document).keypress((event) => {
if(event.which == 39){//RIGHT
if(menuType == 'horizontal'){
goRight();
}
} else if(event.which == 40){//DOWN
if(menuType != 'horizontal'){
goRight();
}
} else if(event.which == 37){//LEFT
if(menuType == 'horizontal'){
goLeft();
}
} else if(event.which == 38){//UP
if(menuType != 'horizontal'){
goLeft();
}
} else if(event.which == 13){
select();
event.preventDefault();
} else if(event.which == 27){
deselect();
event.preventDefault();
}
})
var keyBinds = new Map();
keyBinds.set('button_1', 'enter')//set button_1 (A) as enter
var pressed = [];
var gameLooper = 0;
//Sending keys from gamepad
function changeToGame(){
//Called when game is running
//reset events
gamepad.off('press', 'start');
gamepad.off('press', 'd_pad_left');
gamepad.off('press', 'd_pad_right');
gamepad.off('press', 'button_1');
gamepad.off('press', 'button_2');
//add custom ones
keyBinds.forEach((val, key) => {
gamepad.on('press', key, () => {
sendKeys([], val);
})
})
// gameLooper = setInterval(() => {
// if(pressed == []) return;
// sendKeys(pressed);
// pressed = [];
// }, 100);
}
function changeToMenu(){
clearInterval(gameLooper); //stop sending keys
//Called when game is stopped
//reset events
gamepad.off('press', 'start');
gamepad.off('press', 'd_pad_left');
gamepad.off('press', 'd_pad_right');
gamepad.off('press', 'button_1');
gamepad.off('press', 'button_2');
//restore original events
gamepad.on('press', 'start', () => {
showMainMenu();
});
gamepad.on('press', 'd_pad_left', () => {
goLeft();
});
gamepad.on('press', 'd_pad_right', () => {
goRight();
});
gamepad.on('press', 'button_1', () => {
select();
});
gamepad.on('press', 'button_2', () => {
deselect();
});
}
$(document).keypress((event) => {
if(event.which == 39){//RIGHT
if(menuType == 'horizontal'){
goRight();
}
} else if(event.which == 40){//DOWN
if(menuType != 'horizontal'){
goRight();
}
} else if(event.which == 37){//LEFT
if(menuType == 'horizontal'){
goLeft();
}
} else if(event.which == 38){//UP
if(menuType != 'horizontal'){
goLeft();
}
} else if(event.which == 13){
select();
event.preventDefault();
} else if(event.which == 27){
deselect();
event.preventDefault();
}
})
var keyBinds = new Map();
keyBinds.set('button_1', 'enter')//set button_1 (A) as enter
var pressed = [];
var gameLooper = 0;
//Sending keys from gamepad
function changeToGame(){
//Called when game is running
//reset events
gamepad.off('press', 'start');
gamepad.off('press', 'd_pad_left');
gamepad.off('press', 'd_pad_right');
gamepad.off('press', 'button_1');
gamepad.off('press', 'button_2');
//add custom ones
keyBinds.forEach((val, key) => {
gamepad.on('press', key, () => {
sendKeys([], val);
})
})
// gameLooper = setInterval(() => {
// if(pressed == []) return;
// sendKeys(pressed);
// pressed = [];
// }, 100);
}
function changeToMenu(){
clearInterval(gameLooper); //stop sending keys
//Called when game is stopped
//reset events
gamepad.off('press', 'start');
gamepad.off('press', 'd_pad_left');
gamepad.off('press', 'd_pad_right');
gamepad.off('press', 'button_1');
gamepad.off('press', 'button_2');
//restore original events
gamepad.on('press', 'start', () => {
showMainMenu();
});
gamepad.on('press', 'd_pad_left', () => {
goLeft();
});
gamepad.on('press', 'd_pad_right', () => {
goRight();
});
gamepad.on('press', 'button_1', () => {
select();
});
gamepad.on('press', 'button_2', () => {
deselect();
});
}

View file

@ -1,181 +0,0 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Console hub (ALPHA)</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"><!-- include material icons -->
<link rel="stylesheet" href="styles.min.css"><!-- include custom stylesheet -->
</head>
<body>
<div class="welcome-main">
<h1 class="title-font">Welcome</h1>
<h5 class="glow" id="welcome-continue">Press <b>START</b> to procceed</h5>
</div>
<nav>
<div class="profile">
Not loged in
</div>
<div class="info">
<i class="material-icons">
network_wifi
</i>
<span class="time" id="time">
00:00
</span>
</div>
</nav>
<div id="selection" class="menu hidden">
<div id="games-view" class="menu-item hidden">
<h1 class="title-font">Games</h1>
<p>No games available</p>
<div class="previews">
<!-- <div class="game" id="game-slimey">
<img src="https://console-hub.danbulant.eu/games/slimey.png" class="game-preview">
<span>Slimey, JUMP</span>
</div> -->
</div>
</div>
<div id="settings-view" class="menu-item hidden">
<h1 class="title-font">Settings</h1>
<div class="switch-label option switchable" id="option-fullscreen">
<span>Fullscreen</span>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</div>
<div class="switch-label option switchable" id="option-auto-update">
<span>Auto updates</span>
<label class="switch">
<input type="checkbox" checked>
<span class="slider round"></span>
</label>
</div>
</div>
<div id="account-view" class="menu-item hidden">
<h1 class="title-font">Account</h1>
<p>You aren't signed in</p>
<button>Login/register</button>
</div>
<div id="store-view" class="menu-item hidden">
<h1 class="title-font">Store</h1>
<p>Not available in DEVELOPMENT stage</p>
</div>
<div id="files-view" class="menu-item hidden">
<h1 class="title-font">Files</h1>
<p class="path"></p>
<div id="files-list-container">
<ul id="files-list">
</ul>
</div>
</div>
</div>
<div id="dialog" class="hidden">
<h1 id="dialog-title">TITLE</h1>
<p id="dialog-text">TEXT</p>
<div id="dialog-buttons">
<button id="dialog-button_1">OK</button>
</div>
</div>
<div id="main-menu" class="main hidden">
<div class="button-main-container">
<div class="button-main selected" id="games">
<div class="button-menu" id="games-menu">
<i class="material-icons">
videogame_asset
</i>
</div>
<span>
Games
</span>
</div>
</div>
<div class="button-main-container">
<div class="button-main " id="settings">
<div class="button-menu" id="settings-menu">
<i class="material-icons">
settings
</i>
</div>
<span>
Settings
</span>
</div>
</div>
<div class="button-main-container">
<div class="button-main " id="account">
<div class="button-menu" id="account-menu">
<i class="material-icons">
account_box
</i>
</div>
<span>
Account
</span>
</div>
</div>
<div class="button-main-container">
<div class="button-main " id="store">
<div class="button-menu" id="store-menu">
<i class="material-icons">
local_grocery_store
</i>
</div>
<span>
Store
</span>
</div>
</div>
<div class="button-main-container">
<div class="button-main " id="files">
<div class="button-menu" id="files-menu">
<i class="material-icons">
folder_open
</i>
</div>
<span>
Files
</span>
</div>
</div>
</div>
<div id="back" class="back-arrow hidden">
<i class="material-icons circle">
arrow_back
</i>
<span id="back-text">back</span>
</div>
<div id="snackbar"></div>
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>
<script src="jquery.min.js"></script>
<script>if (window.module) module = window.module;</script>
<script src="gamepad.js"></script>
<script src="functions.js"></script>
<script src="script.js"></script>
<script src="onlineChecker.js"></script>
<script src="filesystem.js"></script>
<script src="node.js"></script>
<script src="keyboard.js"></script>
<script>
</script>
</body>
</html>

View file

@ -1,10 +1,6 @@
var start = true;
var screen = 0;
var menu = 0;
var gamepads = 0;
const gamepad = new Gamepad();
var toastCancel;
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes();
function pad(num, size){ return ('00000000000000' + num).substr(-size); }
@ -15,90 +11,27 @@ setInterval((function(){
$("#time").html(time);
}), 1000);
function toast(string) {
clearTimeout(toastCancel);
var x = document.getElementById("snackbar");
x.className = "show";
x.innerHTML = string;
toastCancel = setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
}
function showWelcomeText(){
var x = document.getElementById("welcome-continue");
if(gamepads > 0){
x.innerHTML = "Press <b>START</b> to procceed";
} else {
x.innerHTML = "Connect your gamepad!";
}
}
function showMainMenu(){
window.start = false;
window.screen = 1;
window.menu = 1;
goRight();
goLeft();
$(".game").width((function(offset, width){
return $(".game .game-preview:eq(" + offset + ")").width();
}));
$("#main-menu").removeClass('hidden');
$("#selection").removeClass("hidden");
$(".welcome-main").addClass("hide");
setTimeout(1000, (function(){$(".welcome-main").hide();}));
}
function showStartMenu(){
window.start = true;
window.screen = 0;
window.menu = 0;
$("#main-menu").addClass('hidden');
$("#selection").addClass("hidden");
$(".welcome-main").removeClass("hide");
$(".welcome-main").show();
}
gamepad.on('connect', e => {
gamepads++;
showWelcomeText();
toast(`Player ${e.index + 1} has connected`);
console.log(`controller ${e.index} connected!`);
});
gamepad.on('disconnect', e => {
toast(`Player ${e.index + 1} has disconnected`);
gamepads --;
console.log(`controller ${e.index} disconnected!`);
});
gamepad.on('press', 'start', () => {
showMainMenu();
});
gamepad.on('press', 'd_pad_left', () => {
goLeft();
});
gamepad.on('press', 'd_pad_right', () => {
goRight();
});
gamepad.on('press', 'button_1', () => {
select();
});
gamepad.on('press', 'button_2', () => {
deselect();
});
function goLeft(){
if(menu == 1 || menu == 2 || menu == 3){
selected--;
callMenuItem();
}
console.log("Going left");
}
function goRight(){
if(menu == 1 || menu == 2 || menu == 3){
selected++;
callMenuItem();
}
console.log("Going right");
}
var leftCounter = 0;
var rightCounter = 0;

28
html/views/index.html Normal file
View file

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Console hub</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"><!-- include material icons -->
<link rel="stylesheet" href="styles.min.css"><!-- include custom stylesheet -->
</head>
<body>
<div id="view">
</div>
<script>
if (typeof module === 'object') {window.module = module; module = undefined;}
</script>
<script src="jquery.min.js"></script>
<script>if (window.module) module = window.module;</script>
<script src="gamepad.js"></script>
<script src="functions.js"></script>
<script src="script.js"></script>
<script src="onlineChecker.js"></script>
<script src="filesystem.js"></script>
<script src="node.js"></script>
<script src="keyboard.js"></script>
</body>
</html>

View file

@ -1,16 +1,16 @@
{
"short_name": "Console hub",
"name": "Console hub beta",
"icons": [
{
"src": "/favicon.ico",
"type": "image/ico"
}
],
"start_url": "/",
"background_color": "#5d75ad",
"display": "standalone",
"orientation": "landscape",
"scope": "/",
"theme_color": "#5d75ad"
}
{
"short_name": "Console hub",
"name": "Console hub beta",
"icons": [
{
"src": "/favicon.ico",
"type": "image/ico"
}
],
"start_url": "/",
"background_color": "#5d75ad",
"display": "standalone",
"orientation": "landscape",
"scope": "/",
"theme_color": "#5d75ad"
}

View file

@ -34,7 +34,7 @@ function createWindow () {
webSecurity: false
}
})
win.setTitle('Console hub (ALPHA) DEV');
win.setTitle('Console hub (ALPHA)');
win.setProgressBar(1.1);
win.setFullScreenable(true);
var template = [{
@ -125,7 +125,7 @@ function createWindow () {
const menu = Menu.buildFromTemplate(template);
win.setAutoHideMenuBar(true);
Menu.setApplicationMenu(menu);
win.loadFile('html/index.html')
win.loadFile('html/views/index.html')
win.webContents.once('dom-ready', () => {
// win.webContents.openDevTools() //debug tools
win.setProgressBar(0);

2445
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff