git @ Cat's Eye Technologies Gemooy / c9ce99e
Update yoob.js files to version 0.7; add Reset button. Chris Pressey 10 years ago
3 changed file(s) with 89 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
3434 <button id="start">Start</button>
3535 <button id="stop">Stop</button>
3636 <button id="step">Step</button>
37 <button id="reset">Reset</button>
3738 Speed: <input id="speed" type="range" min="0" max="200" value="0" />
3839
3940 <div>
136137 'step': 'step',
137138 'load': 'load',
138139 'edit': 'edit',
140 'reset': 'reset',
139141 'speed': 'speed',
140142 'source': 'program',
141143 'display': 'canvas_viewport'
00 /*
1 * This file is part of yoob.js version 0.6
1 * This file is part of yoob.js version 0.7
22 * Available from https://github.com/catseye/yoob.js/
33 * This file is in the public domain. See http://unlicense.org/ for details.
44 */
1818 * - step
1919 * - load
2020 * - edit
21 * - reset
2122 *
2223 * - a slider control which adjusts the speed of program state evolution.
2324 *
9091 * are the actions a controller can undertake, and the values
9192 * are either DOM elements or strings; if strings, DOM elements
9293 * with those ids will be obtained from the document and used.
94 *
95 * When the button associated with e.g. 'start' is clicked,
96 * the corresponding method (in this case, 'click_start()')
97 * on this Controller will be called. These functions are
98 * responsible for changing the state of the Controller (both
99 * the internal state, and the enabled status, etc. of the
100 * controls), and for calling other methods on the Controller
101 * to implement the particulars of the action.
102 *
103 * For example, 'click_step()' calls 'performStep()' which
104 * calls 'step()' (which a subclass or instantiator must
105 * provide an implementation for.)
106 *
107 * To simulate one of the buttons being clicked, you may
108 * call 'click_foo()' yourself in code. However, that will
109 * be subject to the current restrictions of the interface.
110 * You may be better off calling one of the "internal" methods
111 * like 'performStep()'.
93112 */
94113 this.connect = function(dict) {
95114 var $this = this;
96115
97 var keys = ["start", "stop", "step", "load", "edit"];
116 var keys = ["start", "stop", "step", "load", "edit", "reset"];
98117 for (var i in keys) {
99118 var key = keys[i];
100119 var value = dict[key];
120139 if (key === 'speed') {
121140 this.speed.value = this.delay;
122141 this.speed.onchange = function(e) {
123 $this.delay = speed.value;
142 $this.setDelayFrom($this.speed);
124143 if ($this.intervalId !== undefined) {
125144 $this.stop();
126145 $this.start();
205224 this.loadSource(text);
206225 };
207226
227 /*
228 * This is the basic idea, but not fleshed out yet.
229 * - Should we cache the source somewhere?
230 * - While we're waiting, should we disable the UI / show a spinny?
231 */
232 this.loadSourceFromURL = function(url, errorCallback) {
233 var http = new XMLHttpRequest();
234 var $this = this;
235 if (!errorCallback) {
236 errorCallback = function(http) {
237 $this.loadSource(
238 "Error: could not load " + url + ": " + http.statusText
239 );
240 }
241 }
242 http.open("get", url, true);
243 http.onload = function(e) {
244 if (http.readyState === 4 && http.responseText) {
245 if (http.status === 200) {
246 $this.loadSource(http.responseText);
247 } else {
248 errorCallback(http);
249 }
250 }
251 };
252 http.send(null);
253 };
254
208255 this.click_edit = function(e) {
209256 this.click_stop();
210257 if (this.controls.edit) this.controls.edit.style.display = "none";
237284 this.click_stop = function(e) {
238285 this.stop();
239286 this.state = PAUSED;
287 /* why is this check here? ... */
240288 if (this.controls.stop && this.controls.stop.disabled) {
241289 return;
242290 }
259307 clearInterval(this.intervalId);
260308 this.intervalId = undefined;
261309 };
310
311 this.click_reset = function(e) {
312 this.click_stop();
313 this.load(this.source.value);
314 if (this.controls.start) this.controls.start.disabled = false;
315 if (this.controls.step) this.controls.step.disabled = false;
316 if (this.controls.stop) this.controls.stop.disabled = true;
317 };
318
319 /*
320 * Override this to change how the delay is acquired from the 'speed'
321 * element.
322 */
323 this.setDelayFrom = function(elem) {
324 this.delay = elem.max - elem.value;
325 };
262326 };
00 /*
1 * This file is part of yoob.js version 0.6
1 * This file is part of yoob.js version 0.7
22 * Available from https://github.com/catseye/yoob.js/
33 * This file is in the public domain. See http://unlicense.org/ for details.
44 */
1818 * colour given by the fillStyle attribute, if present, or a light green if
1919 * it is not defined.
2020 */
21 yoob.Cursor = function(x, y, dx, dy) {
22 this.x = x;
23 this.y = y;
24 this.dx = dx;
25 this.dy = dy;
21 yoob.Cursor = function() {
22 this.init = function(x, y, dx, dy) {
23 this.x = x;
24 this.y = y;
25 this.dx = dx;
26 this.dy = dy;
27 return this;
28 };
29
30 this.clone = function() {
31 return new yoob.Cursor().init(this.x, this.y, this.dx, this.dy);
32 };
2633
2734 this.getX = function() {
2835 return this.x;
8996 }
9097 };
9198
99 this.rotateDegrees = function(degrees) {
100 while (degrees > 0) {
101 this.rotateCounterclockwise();
102 degrees -= 45;
103 }
104 };
105
92106 /* from yoob.TapeHead; may go away or change slightly */
93107 this.move = function(delta) {
94108 this.x += delta;