-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplayback.js
125 lines (119 loc) · 3.6 KB
/
playback.js
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
function Playback(interpreter, renderer, callback, resetCallback) {
this.interpreter = interpreter;
this.renderer = renderer;
this.callback = callback;
this.resetCallback = resetCallback;
this.running = false;
this.playing = false;
this.intervalId = -1;
this.delay = 400;
this.times = 1;
this.registerEvents();
}
Playback.prototype.resetInterval = function() {
var self = this;
if(this.intervalId != -1) clearInterval(this.intervalId);
this.intervalId = setInterval(function() {
if (!self.running) return;
self.step();
}, this.delay);
}
Playback.prototype.updateDelay = function(id) {
var element = document.getElementById('icon-play'+id+'x');
if(this.preElement) {
this.preElement.className = 'icon';
this.preElement = element;
}
element.className = 'icon selected';
// Reset timing
this.times = 1;
if(id == 1) this.delay = 400;
if(id == 2) this.delay = 20;
if(id == 3) {
this.delay = 20;
this.times = 10;
}
this.resetInterval();
}
Playback.prototype.registerEvents = function() {
var self = this;
// TODO let's not use getElementById in classes
document.getElementById('icon-play').onclick = function() {
document.activeElement.blur();
self.playing = true;
self.running = true;
self.update();
};
document.getElementById('icon-pause').onclick = function() {
document.activeElement.blur();
if(!self.playing) return;
self.running = false;
self.update();
};
document.getElementById('icon-step').onclick = function() {
document.activeElement.blur();
self.playing = true;
self.step(true);
self.running = false;
self.update();
};
document.getElementById('icon-stop').onclick = function() {
self.playing = false;
self.running = false;
self.resetCallback();
self.update();
};
document.getElementById('icon-play1x').onclick =
this.updateDelay.bind(this, 1);
document.getElementById('icon-play2x').onclick =
this.updateDelay.bind(this, 2);
document.getElementById('icon-play3x').onclick =
this.updateDelay.bind(this, 3);
this.preElement = document.getElementById('icon-play1x');
this.resetInterval();
self.update();
}
Playback.prototype.update = function() {
// TODO PLEASE CLEANUP THIS CODE
if(this.playing) {
// Stop button is enabled
document.getElementById('icon-stop').className = 'icon';
if(this.running) {
// Play button is selected
document.getElementById('icon-play').className = 'icon selected';
document.getElementById('icon-pause').className = 'icon';
document.getElementById('icon-step').className = 'icon disabled';
} else {
// Pause button is selected
document.getElementById('icon-play').className = 'icon';
document.getElementById('icon-pause').className = 'icon selected';
document.getElementById('icon-step').className = 'icon';
}
} else {
// Stop, pause button is disabled
document.getElementById('icon-stop').className = 'icon disabled';
document.getElementById('icon-play').className = 'icon';
document.getElementById('icon-pause').className = 'icon disabled';
}
}
Playback.prototype.step = function(once) {
if(!this.interpreter || !this.renderer) return;
for(var i = 0; i < this.times; ++i) {
this.interpreter.next();
if(this.interpreter.state.breakpoint) {
this.running = false;
this.update();
break;
}
if(!this.interpreter.state.running) {
this.running = false;
this.stopped = true;
this.update();
break;
}
if(once) break;
}
this.renderer.render();
if(this.callback) this.callback();
}
module.exports = Playback;