lib/gamepad/gamepad.js
/**
	 * Reconstruct the UI for the current gamepads
	 */
Reconstruct the UI for the current gamepads
Reconstruct the UI for the current gamepads
function rebuildUI() {
		function onButtonClick(ev) {
			let b = ev.currentTarget;
			let gpIndex = b.getAttribute('data-gamepad-index');
			showController(gpIndex);
		}
		let gp = navigator.getGamepads();
		let bbbox = qs("#button-bar-box");
		bbbox.innerHTML = '';
		let gpContainer = qs("#gamepad-container");
		gpContainer.innerHTML = '';
		let haveControllers = false, curControllerVisible = false, firstController = null;
		for (let i = 0; i < gp.length; i++) {
			if (!gp[i] || !gp[i].connected) { continue; }
			let gpIndex = gp[i].index;
			let button = template("#template-button",
				{
					"id": "button-" + gpIndex,
					"data-gamepad-index": gpIndex,
					"value": gpIndex
				});
			bbbox.appendChild(button);
			button.addEventListener('click', onButtonClick);
			let gamepad = template("#template-gamepad",
				{
					"id": "gamepad-" + gpIndex,
					"data-gamepad-index": gpIndex
				});
			gpContainer.appendChild(gamepad);
			qs(".gamepad-title", gamepad).innerHTML = "Gamepad " + gpIndex;
			qs(".gamepad-id", gamepad).innerHTML = gp[i].id;
			let mapping = gp[i].mapping;
			qs(".gamepad-mapping", gamepad).innerHTML = "mapping: " + (mapping && mapping !== ''? mapping: "[<i>unspecified</i>]");
			let j;
			let buttonBox = qs(".gamepad-buttons-box", gamepad)
			for (j = 0; j < gp[i].buttons.length; j++) {
				let buttonContainer = template("#template-gamepad-button-container",
					{
						"id": "gamepad-" + gpIndex + "-button-container-" + j
					});
					qs(".gamepad-button", buttonContainer).setAttribute("id", "gamepad-" + gpIndex + "-button-" + j);
					qs(".gamepad-button-label", buttonContainer).innerHTML = j;
				buttonBox.appendChild(buttonContainer);
			}
			let axesBox = qs(".gamepad-axes-box", gamepad);
			let axesBoxCount = ((gp[i].axes.length + 1) / 2)|0; 
			for (j = 0; j < axesBoxCount; j++) {
				let axisPairContainer = template("#template-gamepad-axis-pair-container",
					{
						"id": "gamepad-" + gpIndex + "-axis-pair-container-" + j
					});
				qs(".gamepad-axis-pair", axisPairContainer).setAttribute("id", "gamepad-" + gpIndex + "-axispair-" + j);
				let pairLabel;
				if (j == axesBoxCount - 1 && gp[i].axes.length % 2 == 1) {
					pairLabel = j*2;
				} else {
					pairLabel = (j*2) + "," + ((j*2)+1);
				}
				qs(".gamepad-axis-pair-label", axisPairContainer).innerHTML = pairLabel;
				axesBox.appendChild(axisPairContainer);
			}
			haveControllers = true;
			if (i == currentVisibleController) {
				curControllerVisible = true;
			}
			if (firstController === null) {
				firstController = i;
			}
		}
		if (haveControllers) {
			qs("#prompt").classList.add("nodisp");
			qs("#main").classList.remove("nodisp");
		} else {
			qs("#prompt").classList.remove("nodisp");
			qs("#main").classList.add("nodisp");
		}
		if (curControllerVisible) {
			showController(currentVisibleController);
		} else {
			currentVisibleController = firstController;
			showController(firstController);
		}
	}
function rebuildUI ( ) { function onButtonClick ( ev ) { let b = ev . currentTarget ; let gpIndex = b . getAttribute ( 'data-gamepad-index' ) ; showController ( gpIndex ) ; } let gp = navigator . getGamepads ( ) ; let bbbox = qs ( "#button-bar-box" ) ; bbbox . innerHTML = '' ; let gpContainer = qs ( "#gamepad-container" ) ; gpContainer . innerHTML = '' ; let haveControllers = false , curControllerVisible = false , firstController = null ; for ( let i = 0 ; i < gp . length ; i ++ ) { if ( ! gp [ i ] || ! gp [ i ] . connected ) { continue ; } let gpIndex = gp [ i ] . index ; let button = template ( "#template-button" , { "id" : "button-" + gpIndex , "data-gamepad-index" : gpIndex , "value" : gpIndex } ) ; bbbox . appendChild ( button ) ; button . addEventListener ( 'click' , onButtonClick ) ; let gamepad = template ( "#template-gamepad" , { "id" : "gamepad-" + gpIndex , "data-gamepad-index" : gpIndex } ) ; gpContainer . appendChild ( gamepad ) ; qs ( ".gamepad-title" , gamepad ) . innerHTML = "Gamepad " + gpIndex ; qs ( ".gamepad-id" , gamepad ) . innerHTML = gp [ i ] . id ; let mapping = gp [ i ] . mapping ; qs ( ".gamepad-mapping" , gamepad ) . innerHTML = "mapping: " + ( mapping && mapping !== '' ? mapping : "[<i>unspecified</i>]" ) ; let j ; let buttonBox = qs ( ".gamepad-buttons-box" , gamepad ) for ( j = 0 ; j < gp [ i ] . buttons . length ; j ++ ) { let buttonContainer = template ( "#template-gamepad-button-container" , { "id" : "gamepad-" + gpIndex + "-button-container-" + j } ) ; qs ( ".gamepad-button" , buttonContainer ) . setAttribute ( "id" , "gamepad-" + gpIndex + "-button-" + j ) ; qs ( ".gamepad-button-label" , buttonContainer ) . innerHTML = j ; buttonBox . appendChild ( buttonContainer ) ; } let axesBox = qs ( ".gamepad-axes-box" , gamepad ) ; let axesBoxCount = ( ( gp [ i ] . axes . length + 1 ) / 2 ) | 0 ; for ( j = 0 ; j < axesBoxCount ; j ++ ) { let axisPairContainer = template ( "#template-gamepad-axis-pair-container" , { "id" : "gamepad-" + gpIndex + "-axis-pair-container-" + j } ) ; qs ( ".gamepad-axis-pair" , axisPairContainer ) . setAttribute ( "id" , "gamepad-" + gpIndex + "-axispair-" + j ) ; let pairLabel ; if ( j == axesBoxCount - 1 && gp [ i ] . axes . length % 2 == 1 ) { pairLabel = j * 2 ; } else { pairLabel = ( j * 2 ) + "," + ( ( j * 2 ) + 1 ) ; } qs ( ".gamepad-axis-pair-label" , axisPairContainer ) . innerHTML = pairLabel ; axesBox . appendChild ( axisPairContainer ) ; } haveControllers = true ; if ( i == currentVisibleController ) { curControllerVisible = true ; } if ( firstController === null ) { firstController = i ; } } if ( haveControllers ) { qs ( "#prompt" ) . classList . add ( "nodisp" ) ; qs ( "#main" ) . classList . remove ( "nodisp" ) ; } else { qs ( "#prompt" ) . classList . remove ( "nodisp" ) ; qs ( "#main" ) . classList . add ( "nodisp" ) ; } if ( curControllerVisible ) { showController ( currentVisibleController ) ; } else { currentVisibleController = firstController ; showController ( firstController ) ; } }
Reconstruct the UI for the current gamepads
Reconstruct the UI for the current gamepads
// Handle gamepad selector button clicks
 // For each controller, generate a button from the
 // button template, set up a click handler, and append
 // it to the button box
 // Chrome has null controllers in the array
 // sometimes when nothing's plugged in there--ignore
 // them
 // Clone the selector button
 // Add the selector click listener
 // Clone the main holder
 // Add the buttons for this gamepad
 // Add the axes for this gamepad
 // Round up (e.g. 3 axes is 2 boxes)
 // If we're on the last box and the number of axes is odd, just put one label on there
 // And remember that we have controllers now
 // Show or hide the "plug in a controller" prompt as
 // necessary
