-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleApp.xml
More file actions
283 lines (242 loc) · 7.84 KB
/
Copy pathSimpleApp.xml
File metadata and controls
283 lines (242 loc) · 7.84 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<!-- Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License
-->
<ModulePrefs title="Send Message">
<Require feature="rpc" />
<Require feature="views" />
<Require feature="locked-domain" />
</ModulePrefs>
<Content type="html"><![CDATA[
<html>
<style type="text/css">
<!--
.button {
border-radius: 3px;
-moz-border-radius: 3px;
background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#ddd));
background: -moz-linear-gradient(top, #fff, #ddd);
border: 1px solid #bbb;
}
.button:active {
background: -webkit-gradient(linear, left top, left bottom, from(#aaa), to(#333));
background: -moz-linear-gradient(bottom, #ddd, #aaa); }
#wrap {
width:800px;
margin:0 auto;
background:#EEEEEE;
}
#main {
float:left;
width:300px;
margin:0 auto;
background:#FFFFFF;
}
#text {
float:right;
width:400px;
margin-left:20px;
background:#FFFFFF;
}
-->
</style>
<script src="https://talkgadget.google.com/hangouts/_/api/hangout.js?v=1.2" ></script>
<div id="wrap">
<h2>JPII Hangout App Test</h2>
<div id="main">
<canvas id="canvas" width='300' height='300'></canvas><br/>
<p>Choose a color: <span style="width:50px;height:50px;border:1px solid #000; background:#0F0" onClick='setColor("#0F0")'>G</span>
<span style="width:50px;height:50px;border:1px solid #000; background:#F00" onClick='setColor("#F00")'>R</span>
<span style="width:50px;height:50px;border:1px solid #000; background:#00F" onClick='setColor("#00F")'>B</span>
<span style="width:50px;height:50px;border:1px solid #000; background:#FFF" onClick='setColor("#FFF")'>W</span></p>
<div id="eventsSent"></div><br/>
<input type='button' class='button' value='Reset picture' onClick='initColorBoxes()'></input>
</div>
<!--
<div id='text'>
<h2>What am I seeing?</h2>
<p>This application demonstrates the Hangouts API <b>message data channel</b>.
In this data model, you can send rapid, small updates to other participants running the same
application. We are <i>not</i> using shared state.</p>
<p>Mousing over the grid will use <tt>sendMessage</tt> to indicate which square you are over and what color you have chosen.</p>
<p>Note that some messages may be dropped, especially if your application sends lots of them at once. This app is locked to a maximum of
30 messages per second to avoid overloading the channel.</p>
</div>
-->
</div>
<script>
// Keep track of how many messages were sent
var messageLastSeen = {};
var myMessageCount = 0;
var missedMessages = 0;
// Various constants for width and height
var tileSide = 5;
var pixelWidth = 300;
var pixelHeight = 300;
var tileWidth = pixelWidth / tileSide;
var tileHeight = pixelHeight / tileSide;
// Holds all our tile colors
var tileColor = [];
// Current drawing color
var currentColor = '#F00';
var canvas = document.getElementById('canvas');
/** Button callback for selecting a color
* @param {string} value a hex color.
*/
function setColor(value) {
currentColor = value;
}
// Keep track of where the mouse is and whether it's moved across a grid.
var mouseDirty = false;
var lastTileX, lastTileY;
/** Draws the tile grid.
* @param {context} context a canvas 2d context.
*/
function render(context) {
context.fillStyle = '#000';
context.fillRect(0, 0, pixelWidth, pixelHeight);
for (var j = 0; j < tileWidth; j++) {
for (var i = 0; i < tileHeight; i++) {
var color = tileColor[i][j];
context.fillStyle = color;
context.fillRect(i * tileSide + 1,
j * tileSide + 1,
tileSide - 2,
tileSide - 2);
}
}
}
/** Move mouse
* @param {number} x x coordinate.
* @param {number} y y coordinate.
*/
function mouseMove(x, y) {
// Only one update per frame.
if (mouseDirty) {
return;
}
var tileX = Math.floor(x / tileSide);
var tileY = Math.floor(y / tileSide);
mouseDirty = (tileX != lastTileX || tileY != lastTileY);
// Only color if you've moved a significant amount.
if (mouseDirty) {
tileColor[tileX][tileY] = currentColor;
lastTileX = tileX;
lastTileY = tileY;
}
}
/** Standard requestAnimFrame; see paulirish.com */
window.requestAnimFrame = (
function(callback) {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1);
};
})();
/** Draw canvas once per frame. */
function animate() {
// draw
var canvas = document.getElementById('canvas');
render(canvas.getContext('2d'));
// request new frame
requestAnimFrame(function() {
animate();
// Send a message if you've moved.
if (mouseDirty) {
myMessageCount++;
gapi.hangout.data.sendMessage(
JSON.stringify([myMessageCount,
currentColor,
lastTileX,
lastTileY]));
mouseDirty = false;
}
});
}
/** Builds our grid of colors */
function initColorBoxes() {
tileColor = [];
for (var i = 0; i < tileWidth; i++) {
var row = [];
for (var j = 0; j < tileHeight; j++) {
row.push('#FFF');
}
tileColor.push(row);
}
}
/** Draw missing packets, if packets are missing. */
function showLossRates() {
var div = document.getElementById('eventsSent');
var retVal = 'Missed messages: ' + missedMessages;
div.innerHTML = retVal;
}
var missedPackets = 0;
/** Count any dropped packages. Compare incoming message count
* to the number of messages we've seen; any discrepancy counts
* as one miss.
* @param {string} senderid Participant id of sender.
* @param {number} messageid last number send.
*/
function droppedPackageCount(senderid, messageid) {
if (messageLastSeen[senderid] != messageid - 1) {
missedPackets++;
}
messageLastSeen[senderid] = messageid;
console.log('message id = ' + messageid);
}
/** Get a message.
* @param {MessageReceievedEvent} event An event.
*/
function onMessageReceived(event) {
try {
var data = JSON.parse(event.message);
tileColor[data[2]][data[3]] = data[1];
droppedPackageCount(event.senderId, parseInt(data[0]));
showLossRates();
} catch (e) {
console.log(e);
}
}
/** Kick off the app. */
function init() {
// When API is ready...
gapi.hangout.onApiReady.add(
function(eventObj) {
if (eventObj.isApiReady) {
try {
gapi.hangout.data.onMessageReceived.add(onMessageReceived);
document.getElementById('canvas').onmousemove = function(e) {
var ev = e || window.event;
mouseMove(ev.clientX - canvas.offsetLeft,
ev.clientY - canvas.offsetTop);
};
initColorBoxes();
animate();
} catch (e) {
console.log('init:ERROR');
console.log(e);
}
}
});
}
// Wait for gadget to load.
gadgets.util.registerOnLoadHandler(init);
</script>
</body>
]]>
</Content>
</Module>