-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathSVGWorld.ts
500 lines (437 loc) · 19 KB
/
SVGWorld.ts
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import {World, WorldState} from "./World";
import "./lib/jquery";
/********************************************************************************
** SVGWorld
This is the implementation of the World interface, for the browser version.
It is used by 'shrdlite-html.ts'.
You don't have to edit this file, but you might want to play around with
the public constants defined below.
********************************************************************************/
export class SVGWorld implements World {
constructor(
public currentState: WorldState,
public useSpeech = false
) {
if (!this.currentState.arm) this.currentState.arm = 0;
this.canvasWidth = this.containers.world.width() - 2 * this.wallSeparation;
this.canvasHeight = this.containers.world.height() - this.floorThickness;
var dropdown : JQuery = this.containers.inputexamples;
dropdown.empty();
dropdown.append($('<option value="">').text("(Select an example utterance)"));
$.each(this.currentState.examples, function(i, value) {
dropdown.append($('<option>').text(value));
});
dropdown.change(() => {
var userinput = dropdown.val().trim();
if (userinput) {
this.containers.userinput.val(userinput).focus();
}
});
this.containers.inputform.submit(() => this.handleUserInput.call(this));
this.disableInput();
}
//////////////////////////////////////////////////////////////////////
// Public constants that can be played around with
public dialogueHistory = 100; // max nr. utterances
public floorThickness = 10; // pixels
public wallSeparation = 4; // pixels
public armSize = 0.2; // of stack width
public animationPause = 0.01;// seconds
public promptPause = 0.5; // seconds
public ajaxTimeout = 5; // seconds
public armSpeed = 1000; // pixels per second
// There is no way of setting male/female voice,
// so this is one way of having different voices for user/system:
public voices : {[participant:string] : {lang:string, rate:number}} = {
system: {lang: "en-GB", rate: 1.1}, // British English, slightly faster
user: {lang: "en-US", rate: 1.0}, // American English
};
// HTML id's for different containers
public containers = {
world: $('#theworld'),
dialogue: $('#dialogue'),
inputform: $('#dialogue form'),
userinput: $('#dialogue form input:text'),
inputexamples: $('#dialogue form select'),
};
//////////////////////////////////////////////////////////////////////
// Public methods
public readUserInput(prompt : string, callback : (input:string) => void) {
this.printSystemOutput(prompt);
this.enableInput();
this.inputCallback = callback;
}
public printSystemOutput(output : string, participant="system", utterance? : string) {
if (utterance == undefined) {
utterance = output;
}
var dialogue = this.containers.dialogue;
if (dialogue.children().length > this.dialogueHistory) {
dialogue.children().first().remove();
}
$('<p>').attr("class", participant)
.text(output)
.insertBefore(this.containers.inputform);
dialogue.scrollTop(dialogue.prop("scrollHeight"));
if (this.useSpeech && utterance && /^\w/.test(utterance)) {
try {
// W3C Speech API (works in Chrome and Safari)
var speech = new SpeechSynthesisUtterance(utterance);
speech.lang = this.voices[participant].lang;
speech.rate = this.voices[participant].rate;
console.log("SPEAKING: " + utterance);
window.speechSynthesis.speak(speech);
} catch(err) {
}
}
}
public printDebugInfo(info : string) : void {
console.log(info);
}
public printError(error : string, message? : string) : void {
console.error(error, message);
if (message) {
error += ": " + message;
}
this.printSystemOutput(error, "error");
}
public printWorld(callback? : () => void) : void {
this.containers.world.empty();
this.printSystemOutput("Please wait while I populate the world.")
var viewBox : number[] = [0, 0, this.canvasWidth + 2 * this.wallSeparation,
this.canvasHeight + this.floorThickness];
var svg = $(this.SVG('svg')).attr({
viewBox: viewBox.join(' '),
width: viewBox[2],
height: viewBox[3],
}).appendTo(this.containers.world);
// The floor:
$(this.SVG('rect')).attr({
x: 0,
y: this.canvasHeight,
width: this.canvasWidth + 2 * this.wallSeparation,
height: this.canvasHeight + this.floorThickness,
fill: 'black',
}).appendTo(svg);
// The arm:
$(this.SVG('line')).attr({
id:'arm',
x1: this.stackWidth() * this.currentState.arm + this.stackWidth() / 2,
y1: this.armSize * this.stackWidth() - this.canvasHeight,
x2: this.stackWidth() * this.currentState.arm + this.stackWidth() / 2,
y2: this.armSize * this.stackWidth(),
stroke: 'black',
'stroke-width': this.armSize * this.stackWidth(),
}).appendTo(svg);
// If the arm is holding an object:
if (this.currentState.holding) {
this.makeObject(svg, this.currentState.holding, this.currentState.arm, 0);
}
// The objects on the floor:
var timeout = 0;
for (var stacknr=0; stacknr < this.currentState.stacks.length; stacknr++) {
for (var objectnr=0; objectnr < this.currentState.stacks[stacknr].length; objectnr++) {
var objectid = this.currentState.stacks[stacknr][objectnr];
this.makeObject(svg, objectid, stacknr, timeout);
timeout += this.animationPause;
}
}
if (callback) {
setTimeout(callback, (timeout + this.promptPause) * 1000);
}
}
public performPlan(plan : string[], callback? : () => void) : void {
if (this.isSpeaking()) {
setTimeout(() => this.performPlan(plan, callback), this.animationPause * 1000);
return;
}
var planctr = 0;
var performNextAction = () => {
planctr++;
if (plan && plan.length) {
var item = (<string>plan.shift()) .trim();
var action = this.getAction(item);
if (action) {
try {
action.call(this, performNextAction);
} catch(err) {
this.printError(err);
if (callback) setTimeout(callback, this.promptPause * 1000);
}
} else {
if (item && item[0] != "#") {
if (this.isSpeaking()) {
plan.unshift(item);
setTimeout(performNextAction, this.animationPause * 1000);
} else {
this.printSystemOutput(item);
performNextAction();
}
} else {
performNextAction();
}
}
} else {
if (callback) setTimeout(callback, this.promptPause * 1000);
}
}
performNextAction();
}
//////////////////////////////////////////////////////////////////////
// Private variables & constants
private canvasWidth : number;
private canvasHeight : number;
private svgNS = 'http://www.w3.org/2000/svg';
//////////////////////////////////////////////////////////////////////
// Object types
private objectData : {[form:string] :
{[size:string] :
{width:number, height:number, thickness?:number}}}
= {brick: {small: {width:0.30, height:0.30},
large: {width:0.70, height:0.60}},
plank: {small: {width:0.60, height:0.10},
large: {width:1.00, height:0.15}},
ball: {small: {width:0.30, height:0.30},
large: {width:0.70, height:0.70}},
pyramid: {small: {width:0.60, height:0.25},
large: {width:1.00, height:0.40}},
box: {small: {width:0.60, height:0.30, thickness: 0.10},
large: {width:1.00, height:0.40, thickness: 0.10}},
table: {small: {width:0.60, height:0.30, thickness: 0.10},
large: {width:1.00, height:0.40, thickness: 0.10}},
};
private stackWidth() : number {
return this.canvasWidth / this.currentState.stacks.length;
}
private boxSpacing() : number {
return Math.min(5, this.stackWidth() / 20);
}
private SVG(tag : string) : Element {
return document.createElementNS(this.svgNS, tag);
}
private animateMotion(object : JQuery, path : (string|number)[], timeout : number, duration : number) {
var animation : Element = this.SVG('animateMotion');
$(animation).attr({
begin: 'indefinite',
fill: 'freeze',
path: path.join(" "),
dur: duration + "s",
}).appendTo(object);
animation.beginElementAt(timeout);
return animation;
}
//////////////////////////////////////////////////////////////////////
// The basic actions: left, right, pick, drop
private getAction(act : string) : (callback:()=>void) => void {
var actions : {[act:string] : (callback:()=>void) => void}
= {p:this.pick, d:this.drop, l:this.left, r:this.right};
return actions[act.toLowerCase()];
}
private left(callback : () => void) : void {
if (this.currentState.arm <= 0) {
throw "Already at left edge!";
}
this.horizontalMove(this.currentState.arm - 1, callback);
}
private right(callback : () => void) : void {
if (this.currentState.arm >= this.currentState.stacks.length - 1) {
throw "Already at right edge!";
}
this.horizontalMove(this.currentState.arm + 1, callback);
}
private drop(callback: () => void) : void {
if (!this.currentState.holding) {
throw "Not holding anything!";
}
this.verticalMove('drop', callback);
this.currentState.stacks[this.currentState.arm].push(this.currentState.holding);
this.currentState.holding = null;
}
private pick(callback: () => void) : void {
if (this.currentState.holding) {
throw "Already holding something!";
}
this.currentState.holding = <string> this.currentState.stacks[this.currentState.arm].pop();
this.verticalMove('pick', callback);
}
//////////////////////////////////////////////////////////////////////
// Moving around
private horizontalMove(newArm : number, callback? : () => void) : void {
var xArm = this.currentState.arm * this.stackWidth() + this.wallSeparation;
var xNewArm = newArm * this.stackWidth() + this.wallSeparation;
var path1 = ["M", xArm, 0, "H", xNewArm];
var duration = Math.abs(xNewArm - xArm) / this.armSpeed;
var arm = $('#arm');
this.animateMotion(arm, path1, 0, duration);
if (this.currentState.holding) {
var objectHeight = this.getObjectDimensions(this.currentState.holding).heightadd;
var yArm = -(this.canvasHeight - this.armSize * this.stackWidth() - objectHeight);
var path2 = ["M", xArm, yArm, "H", xNewArm];
var object = $("#" + this.currentState.holding)
this.animateMotion(object, path2, 0, duration);
}
this.currentState.arm = newArm;
if (callback) setTimeout(callback, (duration + this.animationPause) * 1000);
}
private verticalMove(action : string, callback? : () => void) : void {
var altitude = this.getAltitude(this.currentState.arm);
var yArm = this.canvasHeight - altitude - this.armSize * this.stackWidth();
if (this.currentState.holding) {
yArm -= this.getObjectDimensions(this.currentState.holding).heightadd;
}
var yStack = -altitude;
var xArm = this.currentState.arm * this.stackWidth() + this.wallSeparation;
var path1 = ["M", xArm, 0, "V", yArm];
var path2 = ["M", xArm, yArm, "V", 0];
var duration = (Math.abs(yArm)) / this.armSpeed;
var arm = $('#arm');
var object = $("#" + this.currentState.holding)
this.animateMotion(arm, path1, 0, duration);
this.animateMotion(arm, path2, duration + this.animationPause, duration);
if (action == 'pick') {
var path3 = ["M", xArm, yStack, "V", yStack-yArm];
this.animateMotion(object, path3, duration + this.animationPause, duration)
} else {
var path3 = ["M", xArm, yStack-yArm, "V", yStack];
this.animateMotion(object, path3, 0, duration)
}
if (callback) setTimeout(callback, 2*(duration + this.animationPause) * 1000);
}
//////////////////////////////////////////////////////////////////////
// Methods for getting information about objects
private getObjectDimensions(objectid : string) {
var attrs = this.currentState.objects[objectid];
var size = this.objectData[attrs.form][<string>attrs.size];
var width = size.width * (this.stackWidth() - this.boxSpacing());
var height = size.height * (this.stackWidth() - this.boxSpacing());
var thickness = (size.thickness || 0) * (this.stackWidth() - this.boxSpacing());
var heightadd = attrs.form == 'box' ? thickness : height;
return {
width: width,
height: height,
heightadd: heightadd,
thickness: thickness,
};
}
private getAltitude(stacknr : number, objectid? : string) {
var stack = this.currentState.stacks[stacknr];
var altitude = 0;
for (var i=0; i<stack.length; i++) {
if (objectid == stack[i])
break;
altitude += this.getObjectDimensions(stack[i]).heightadd + this.boxSpacing();
}
return altitude;
}
//////////////////////////////////////////////////////////////////////
// Creating objects
private makeObject(svg : JQuery, objectid : string, stacknr : number, timeout : number) {
var attrs = this.currentState.objects[objectid];
var dim = this.getObjectDimensions(objectid);
if (objectid == this.currentState.holding) {
var altitude = this.canvasHeight - this.armSize * this.stackWidth() - dim.heightadd;
} else {
var altitude = this.getAltitude(stacknr, objectid);
}
var ybottom = this.canvasHeight - this.boxSpacing();
var ytop = ybottom - dim.height;
var ycenter = (ybottom + ytop) / 2;
var yradius = (ybottom - ytop) / 2;
var xleft = (this.stackWidth() - dim.width) / 2
var xright = xleft + dim.width;
var xcenter = (xright + xleft) / 2;
var xradius = (xright - xleft) / 2;
var xmidleft = (xcenter + xleft) / 2;
var xmidright = (xcenter + xright) / 2;
var object : JQuery;
switch (attrs.form) {
case 'brick':
case 'plank':
object = $(this.SVG('rect')).attr({
x: xleft,
y: ytop,
width: dim.width,
height: dim.height
});
break;
case 'ball':
object = $(this.SVG('ellipse')).attr({
cx: xcenter,
cy: ycenter,
rx: xradius,
ry: yradius
});
break;
case 'pyramid':
var points = [xleft, ybottom, xmidleft, ytop, xmidright, ytop, xright, ybottom];
object = $(this.SVG('polygon')).attr({
points: points.join(" ")
});
break;
case 'box':
var points = [xleft, ytop, xleft, ybottom, xright, ybottom, xright, ytop,
xright-dim.thickness, ytop, xright-dim.thickness, ybottom-dim.thickness,
xleft+dim.thickness, ybottom-dim.thickness, xleft+dim.thickness, ytop];
object = $(this.SVG('polygon')).attr({
points: points.join(" ")
});
break;
case 'table':
var points = [xleft, ytop, xright, ytop, xright, ytop+dim.thickness,
xmidright, ytop+dim.thickness, xmidright, ybottom,
xmidright-dim.thickness, ybottom, xmidright-dim.thickness, ytop+dim.thickness,
xmidleft+dim.thickness, ytop+dim.thickness, xmidleft+dim.thickness, ybottom,
xmidleft, ybottom, xmidleft, ytop+dim.thickness, xleft, ytop+dim.thickness];
object = $(this.SVG('polygon')).attr({
points: points.join(" ")
});
break;
default:
throw "Unknown form: " + attrs.form;
}
object.attr({
id: objectid,
stroke: 'black',
'stroke-width': this.boxSpacing() / 2,
fill: attrs.color,
});
object.appendTo(svg);
var path = ["M", stacknr * this.stackWidth() + this.wallSeparation, -(this.canvasHeight + this.floorThickness)];
this.animateMotion(object, path, 0, 0);
path.push("V", -altitude);
this.animateMotion(object, path, timeout, 0.5);
}
//////////////////////////////////////////////////////////////////////
// Methods for handling user input and system output
private enableInput() {
this.containers.inputexamples.prop('disabled', false).val('');
this.containers.inputexamples.find("option:first").attr('selected', 'selected');
this.containers.userinput.prop('disabled', false);
this.containers.userinput.focus().select();
}
private disableInput() {
this.containers.inputexamples.blur();
this.containers.inputexamples.prop('disabled', true);
this.containers.userinput.blur();
this.containers.userinput.prop('disabled', true);
}
private inputCallback : (input:string) => void;
private handleUserInput() {
var userinput = this.containers.userinput.val().trim();
this.disableInput();
this.printSystemOutput(userinput, "user");
this.inputCallback(userinput);
return false;
}
private isSpeaking() {
return this.useSpeech && window && window.speechSynthesis && window.speechSynthesis.speaking;
}
}
//////////////////////////////////////////////////////////////////////
// Additions to the TypeScript standard library
// Support for SVG animations
declare global {
interface Element {
beginElementAt(timeout: number) : void;
}
}