Added gamepad prelim functionality
This commit is contained in:
parent
90d6424703
commit
9ea8f94277
3 changed files with 68 additions and 7 deletions
1
nxbt/web/__init__.py
Normal file
1
nxbt/web/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
from .app import start_web_app
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import time
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from ..nxbt import Nxbt
|
||||||
from flask import Flask, render_template
|
from flask import Flask, render_template
|
||||||
from flask_socketio import SocketIO, emit
|
from flask_socketio import SocketIO, emit
|
||||||
import eventlet
|
import eventlet
|
||||||
|
|
@ -9,6 +10,7 @@ import eventlet
|
||||||
app = Flask(__name__,
|
app = Flask(__name__,
|
||||||
static_url_path='',
|
static_url_path='',
|
||||||
static_folder='static',)
|
static_folder='static',)
|
||||||
|
nxbt = Nxbt()
|
||||||
|
|
||||||
# Configuring/retrieving secret key
|
# Configuring/retrieving secret key
|
||||||
secrets_path = os.path.join(
|
secrets_path = os.path.join(
|
||||||
|
|
@ -25,7 +27,7 @@ else:
|
||||||
app.config['SECRET_KEY'] = secret_key
|
app.config['SECRET_KEY'] = secret_key
|
||||||
|
|
||||||
# Starting socket server with Flask app
|
# Starting socket server with Flask app
|
||||||
sio = SocketIO(app)
|
sio = SocketIO(app, cookie=False)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
|
|
@ -36,7 +38,7 @@ def index():
|
||||||
@sio.on('connect')
|
@sio.on('connect')
|
||||||
def on_connect():
|
def on_connect():
|
||||||
print("Connected")
|
print("Connected")
|
||||||
emit('my response', {'data': 'Connected'})
|
index = nxbt.create_controller()
|
||||||
|
|
||||||
|
|
||||||
@sio.on('disconnect')
|
@sio.on('disconnect')
|
||||||
|
|
@ -44,10 +46,15 @@ def on_disconnect():
|
||||||
print("Disconnected")
|
print("Disconnected")
|
||||||
|
|
||||||
|
|
||||||
@sio.on('message')
|
@sio.on('create_controller')
|
||||||
def handle_message(message):
|
def on_create_controller():
|
||||||
print("Elapsed Time", (time.time()*1000) - message["timestamp"])
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
@sio.on('input')
|
||||||
|
def handle_input(message):
|
||||||
|
print(json.loads(message))
|
||||||
|
|
||||||
|
|
||||||
|
def start_web_app():
|
||||||
eventlet.wsgi.server(eventlet.listen(('', 8000)), app)
|
eventlet.wsgi.server(eventlet.listen(('', 8000)), app)
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,59 @@
|
||||||
let date = new Date();
|
let date = new Date();
|
||||||
socket.emit('message', {"timestamp": date.getTime()});
|
socket.emit('message', {"timestamp": date.getTime()});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on('response', function(data) {
|
||||||
|
console.log(data)
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('dateResponse', function(data) {
|
||||||
|
console.log(data)
|
||||||
|
});
|
||||||
|
|
||||||
|
window.addEventListener("gamepadconnected", function(e) {
|
||||||
|
let gp = navigator.getGamepads()[e.gamepad.index];
|
||||||
|
console.log("Gamepad connected at index %d: %s. %d buttons, %d axes.",
|
||||||
|
gp.index, gp.id,
|
||||||
|
gp.buttons.length, gp.axes.length);
|
||||||
|
|
||||||
|
setInterval(function() {
|
||||||
|
let gp = navigator.getGamepads()[e.gamepad.index];
|
||||||
|
|
||||||
|
let axes = gp["axes"];
|
||||||
|
let buttons = []
|
||||||
|
for (let i = 0; i < gp["buttons"].length; i++) {
|
||||||
|
let button = gp["buttons"][i];
|
||||||
|
buttons.push({
|
||||||
|
"pressed": button["pressed"],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
gpData = {
|
||||||
|
"axes": gp["axes"],
|
||||||
|
"buttons": {
|
||||||
|
"B": gp["buttons"][0]["pressed"],
|
||||||
|
"A": gp["buttons"][1]["pressed"],
|
||||||
|
"Y": gp["buttons"][2]["pressed"],
|
||||||
|
"X": gp["buttons"][3]["pressed"],
|
||||||
|
"L": gp["buttons"][4]["pressed"],
|
||||||
|
"R": gp["buttons"][5]["pressed"],
|
||||||
|
"ZL": gp["buttons"][6]["pressed"],
|
||||||
|
"ZR": gp["buttons"][7]["pressed"],
|
||||||
|
"-": gp["buttons"][8]["pressed"],
|
||||||
|
"+": gp["buttons"][9]["pressed"],
|
||||||
|
"L_ANALOG": gp["buttons"][10]["pressed"],
|
||||||
|
"R_ANALOG": gp["buttons"][11]["pressed"],
|
||||||
|
"DPAD_UP": gp["buttons"][12]["pressed"],
|
||||||
|
"DPAD_DOWN": gp["buttons"][13]["pressed"],
|
||||||
|
"DPAD_LEFT": gp["buttons"][14]["pressed"],
|
||||||
|
"DPAD_RIGHT": gp["buttons"][15]["pressed"],
|
||||||
|
"HOME": gp["buttons"][16]["pressed"],
|
||||||
|
"SCREENSHOT": gp["buttons"][17]["pressed"],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.emit('input', JSON.stringify(gpData));
|
||||||
|
}, 1/60);
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue