Permalink
Browse files

initial version, chrome/windows/xbox360 supported

  • Loading branch information...
0 parents commit aaf95b1b0b2fce1a8dd7aeb0a9841ccc8c377c62 @sgraham committed Dec 6, 2011
Showing with 632 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +32 −0 build.py
  3. BIN closure/compiler.jar
  4. +121 −0 example/index.html
  5. +104 −0 gamepad.js
  6. +167 −0 gamepad_no_images.js
  7. +207 −0 gamepad_uncompressed.js
  8. BIN images/Unknown/dpad.png
  9. BIN images/Unknown/dpadDown.png
  10. BIN images/Unknown/dpadLeft.png
  11. BIN images/Unknown/dpadRight.png
  12. BIN images/Unknown/dpadUp.png
  13. BIN images/Unknown/faceButton0.png
  14. BIN images/Unknown/faceButton1.png
  15. BIN images/Unknown/faceButton2.png
  16. BIN images/Unknown/faceButton3.png
  17. BIN images/Unknown/leftShoulder0.png
  18. BIN images/Unknown/leftShoulder1.png
  19. BIN images/Unknown/leftStick.png
  20. BIN images/Unknown/leftStickButton.png
  21. BIN images/Unknown/rightShoulder0.png
  22. BIN images/Unknown/rightShoulder1.png
  23. BIN images/Unknown/rightStick.png
  24. BIN images/Unknown/rightStickButton.png
  25. BIN images/Unknown/select.png
  26. BIN images/Unknown/start.png
  27. BIN images/Xbox360/dpad.png
  28. BIN images/Xbox360/dpadDown.png
  29. BIN images/Xbox360/dpadLeft.png
  30. BIN images/Xbox360/dpadRight.png
  31. BIN images/Xbox360/dpadUp.png
  32. BIN images/Xbox360/faceButton0.png
  33. BIN images/Xbox360/faceButton1.png
  34. BIN images/Xbox360/faceButton2.png
  35. BIN images/Xbox360/faceButton3.png
  36. BIN images/Xbox360/leftShoulder0.png
  37. BIN images/Xbox360/leftShoulder1.png
  38. BIN images/Xbox360/leftStick.png
  39. BIN images/Xbox360/leftStickButton.png
  40. BIN images/Xbox360/rightShoulder0.png
  41. BIN images/Xbox360/rightShoulder1.png
  42. BIN images/Xbox360/rightStick.png
  43. BIN images/Xbox360/rightStickButton.png
  44. BIN images/Xbox360/select.png
  45. BIN images/Xbox360/start.png
@@ -0,0 +1 @@
+*.sw[po]
@@ -0,0 +1,32 @@
+# Simple script to slurp all the icons we have, convert them to data urls, and
+# append to gamepad_no_images.js to create the final gamepad.js that most
+# people will want to use.
+
+import base64
+import platform
+import os
+import sys
+
+def main():
+ result = ''
+ for device in os.listdir("images"):
+ cur_dir = os.path.join("images", device)
+ result += "Gamepad.ImageDataUrls_%s = {};\n" % device
+ for image in os.listdir(cur_dir):
+ basename, ext = os.path.splitext(image)
+ if ext == '.png':
+ data = base64.standard_b64encode(open(os.path.join(cur_dir, image), 'rb').read())
+ result += ("Gamepad.ImageDataUrls_%s['%s'] = 'data:image/png;base64,%s';\n" % (device, basename, data))
+ final = open("gamepad_no_images.js", "rb").read() + result
+ open("gamepad_uncompressed.js", "wb").write(final)
+
+ javaPath = 'java'
+ # no idea.
+ if sys.platform == 'win32' and platform.architecture()[0] == '64bit':
+ javaPath = "c:\\windows\\SysWow64\\java.exe"
+ ret = os.system(javaPath + " -jar closure\\compiler.jar --js gamepad_uncompressed.js --js_output_file gamepad.js --compilation_level ADVANCED_OPTIMIZATIONS")
+ if ret != 0:
+ return ret
+
+if __name__ == "__main__":
+ sys.exit(main())
Binary file not shown.
@@ -0,0 +1,121 @@
+<!doctype html>
+<html>
+<head>
+ <script src="../gamepad_uncompressed.js"></script>
+</head>
+<body>
+ <h1 id='title'></h1>
+ <div id='pads'></div>
+ <style type="text/css">
+ body { font-family: 'Segoe UI', 'Helvetica', 'Arial'; }
+ </style>
+ <script type="text/javascript">
+ if ( !window.requestAnimationFrame ) {
+ window.requestAnimationFrame = ( function() {
+ return window.webkitRequestAnimationFrame ||
+ window.mozRequestAnimationFrame ||
+ window.oRequestAnimationFrame ||
+ window.msRequestAnimationFrame ||
+ function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
+ window.setTimeout( callback, 1000 / 60 );
+ };
+ })();
+ }
+
+ var names = [
+ 'leftStick',
+ 'rightStick',
+ 'faceButton0',
+ 'faceButton1',
+ 'faceButton2',
+ 'faceButton3',
+ 'leftShoulder0',
+ 'rightShoulder0',
+ 'leftShoulder1',
+ 'rightShoulder1',
+ 'select',
+ 'start',
+ 'leftStickButton',
+ 'rightStickButton',
+ 'dpadUp',
+ 'dpadDown',
+ 'dpadLeft',
+ 'dpadRight'
+ ];
+ function createDomElements() {
+ var root = document.getElementById('pads');
+ for (var i = 0; i < 8; ++i) {
+ var pad = document.createElement('div');
+ pad.id = 'pad' + i;
+ root.appendChild(pad);
+ pad.style.display = 'none';
+
+ var title = document.createElement('h2');
+ title.id = 'pad' + i + '_title';
+ pad.appendChild(title);
+
+ for (var j = 0; j < 2; ++j) {
+ var stickouter = document.createElement('div');
+ stickouter.style.width = '256px';
+ stickouter.style.height = '256px';
+ stickouter.style.float = 'left';
+ stickouter.style.border = '1px dotted #888';
+ stickouter.style.margin = '1em';
+ stickouter.style.marginTop = '32px';
+ var stickimg = document.createElement('img');
+ stickimg.id = 'pad' + i + '_' + names[j];
+ stickimg.style.position = 'relative';
+ stickouter.appendChild(stickimg);
+ pad.appendChild(stickouter);
+ }
+
+ for (var j = 2; j < names.length; ++j) {
+ var name = names[j];
+ if ((j - 2) % 4 == 0)
+ pad.appendChild(document.createElement('br'));
+ var item = document.createElement('img');
+ item.style.width = '64px';
+ item.style.clear = 'both';
+ item.id = 'pad' + i + '_' + name;
+ pad.appendChild(item);
+ }
+ }
+ }
+
+ function update() {
+ window.requestAnimationFrame(update);
+
+ var pads = Gamepad.getPads();
+ for (var i = 0; i < pads.length; ++i) {
+ var pad = pads[i];
+ if (pad) {
+ document.getElementById('pad' + i).style.display = '';
+ document.getElementById('pad' + i + '_title').innerHTML = pad.name;
+ for (var j = 0; j < names.length; ++j) {
+ var name = names[j];
+ var buttonDom = document.getElementById('pad' + i + '_' + names[j]);
+ buttonDom.src = pad.images[name];
+ if (j >= 2)
+ buttonDom.style.opacity = pad[name] / 0.8 + 0.2;
+ }
+ var leftStick = document.getElementById('pad' + i + '_leftStick');
+ var rightStick = document.getElementById('pad' + i + '_rightStick');
+ var imgSize = 32;
+ leftStick.style.left = Math.floor((pad.leftStickX + 1.0) / 2.0 * 256 - imgSize) + 'px';
+ leftStick.style.top = Math.floor((pad.leftStickY + 1.0) / 2.0 * 256 - imgSize) + 'px';
+ rightStick.style.left = Math.floor((pad.rightStickX + 1.0) / 2.0 * 256 - imgSize) + 'px';
+ rightStick.style.top = Math.floor((pad.rightStickY + 1.0) / 2.0 * 256 - imgSize) + 'px';
+ }
+ }
+ }
+
+ if (Gamepad.supported) {
+ document.getElementById('title').innerHTML = "Press buttons on your gamepads.";
+ createDomElements();
+ update();
+ } else {
+ document.getElementById('title').innerHTML = "Sorry, your browser doesn't seem to support the Gamepad API";
+ }
+ </script>
+</body>
+</html>
Oops, something went wrong.

0 comments on commit aaf95b1

Please sign in to comment.