-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.html
204 lines (184 loc) · 8.51 KB
/
controller.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<html>
<head>
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0" />
<title>Game Controller</title>
<style>
html, body, *{
padding: 0px;
margin: 0px;
}
#controller{
position: absolute;
top: 0px;
left; 0px;
width: 100%;
height: 100%;
}
</style>
<!-- Include jQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="socket.io.js"></script>
<script>
function sendKeypress(){
var key = $(this).attr("button-id");
console.log("Key press: "+key);
socket.emit("KEYPRESS",{"key": key})
}
function safeCapture(data,property){
if (data.hasOwnProperty(property)){
return data[property];
}
return "";
}
var socket = io.connect('http://us-ch01.upadvisor.com:9000');
//The server immediately responds to connections with an IDENTIFY.
socket.on('IDENTIFY', function (data) {
socket.emit('IAM', { "IAM":"controller" });
});
//socket.emit("CONTROLLER",{"controller":controllerData});
socket.on("CONTROLLER",function (data){
if ((controllerPage=safeCapture(data,"controller"))!=""){
$("#controller").html(controllerPage);
$(".controller_button").each( function (index, value){
$(value).html("<span style='width:100%;height:100%;'>"+$(value).html()+"</span>");
});
$(".controller_button").on("click", sendKeypress);
$("#setup").hide();
$("#controller").show();
}
});
$(document).ready(function (){
$("#controller").hide();
});
function sendIdentification(identifier){
socket.emit("PAIR", {"IDENTIFIER":identifier});
}
</script>
<script>
function setupEventListeners() {
if (window.DeviceOrientationEvent) {
$("body").css("background-color","#00FF00");
$("#doEvent").text("DeviceOrientationEvent");
window.addEventListener('deviceorientation', function(eventData) {
// gamma is the left-to-right tilt in degrees, where right is positive
var tiltLR = eventData.gamma;
// beta is the front-to-back tilt in degrees, where front is positive
var tiltFB = eventData.beta;
// alpha is the compass direction the device is facing in degrees
var dir = eventData.alpha
// deviceorientation does not provide this data
var motUD = null;
// call our orientation event handler
deviceOrientationHandler(tiltLR, tiltFB, dir, motUD);
}, false);
} else if (window.OrientationEvent) {
$("body").css("background-color","#00FF00");
$("#doEvent").text("OrientationEvent");
window.addEventListener('MozOrientation', function(eventData) {
// x is the left-to-right tilt from -1 to +1, so we need to convert to degrees
var tiltLR = eventData.x * 90;
// y is the front-to-back tilt from -1 to +1, so we need to convert to degrees
// We also need to invert the value so tilting the device towards us (forward)
// results in a positive value.
var tiltFB = eventData.y * -90;
// MozOrientation does not provide this data
var dir = null;
// z is the vertical acceleration of the device
var motUD = eventData.z;
// call our orientation event handler
deviceOrientationHandler(tiltLR, tiltFB, dir, motUD);
}, false);
} else {
$("body").css("background-color","#FF0000");
}
}
function time () {
// http://kevin.vanzonneveld.net
// + original by: GeekFG (http://geekfg.blogspot.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: metjay
// + improved by: HKM
// * example 1: timeStamp = time();
// * results 1: timeStamp > 1000000000 && timeStamp < 2000000000
return (new Date()).getTime();
}
var latestUpdate = time();
function deviceOrientationHandler(tiltLR, tiltFB, dir, motUD) {
var cTime = time();
if (latestUpdate+50 < cTime){
console.log("handler");
$("#doTiltLR").text(Math.round(tiltLR));
$("#doTiltFB").text(Math.round(tiltFB));
$("#doDirection").text(Math.round(dir));
$("#doMotionUD").text(motUD);
//Send to Socket.IO server
socket.emit("ORIENTATION",{ "tiltLR" : tiltLR, "tiltFB" : tiltFB, "direction" : dir, "motionUD" : motUD});
latestUpdate = time();
}
// Apply the transform to the image
/*document.getElementById("imgLogo").style.webkitTransform = "rotate(" +
tiltLR + "deg) rotate3d(1,0,0, " + (tiltFB * -1) + "deg)";
document.getElementById("imgLogo").style.MozTransform = "rotate(" + tiltLR + "deg)";
document.getElementById("imgLogo").style.transform = "rotate(" + tiltLR +
"deg) rotate3d(1,0,0, " + (tiltFB * -1) + "deg)";*/
}
$(document).ready(function() {
setupEventListeners();
var $_GET = {};
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
function decode(s) {
return decodeURIComponent(s.split("+").join(" "));
}
$_GET[decode(arguments[1])] = decode(arguments[2]);
});
var clientID = $_GET["id"];
console.log("clientid: " + clientID);
if (clientID != null) {
$("#clientIDSetup").hide();
sendIdentification(clientID);
}
});
</script>
</head>
<body>
<div id="setup">
<div class="main">
<h2>Device Orientation</h2>
<table>
<tr>
<td>Event Supported</td>
<td id="doEvent"></td>
</tr>
<tr>
<td>Tilt Left/Right [tiltLR]</td>
<td id="doTiltLR"></td>
</tr>
<tr>
<td>Tilt Front/Back [tiltFB]</td>
<td id="doTiltFB"></td>
</tr>
<tr>
<td>Direction [direction]</td>
<td id="doDirection"></td>
</tr>
<tr>
<td>Motion Up/Down</td>
<td id="doMotionUD"></td>
</tr>
</table>
</div>
<div id="clientIDSetup">
Game Controller Code: <input type="text" id="idField"><br />
<input type="submit" value="Submit" id="submitButton">
<script>
$("#submitButton").click(function (){
sendIdentification($("#idField").val());
});
</script>
</div>
</div>
<div id="controller">
{NOT LOADED}
</div>
</body>
</html>