Commits

Cat's Eye Technologies  committed 23e14fe

Deprecate yoob.TapeHead. Use yoob.Cursor instead!

  • Participants
  • Parent commits af366a2

Comments (0)

Files changed (4)

File README.markdown

 
 *   `yoob.Cursor`, in `yoob/cursor.js`
     
-    A pointer (position vector) into two-dimensional Cartesian space
-    (typically a `yoob.Playfield`) which also has a _delta_ (direction
-    vector) which need not necessarily be used.
+    An object representing position and direction in some space, which may
+    be one-dimensional (typically a `yoob.Tape`, where it serves as a tape
+    head) or two-dimensional Cartesian space (typically a `yoob.Playfield`).
+    The direction aspect need not not necessarily be used.
 
 *   `yoob.PlayfieldCanvasView`, in `yoob/playfield-canvas-view.js`
     
     A (theoretically) unbounded tape, like you'd find on a Turing machine,
     optionally associated with a `<canvas>` on which it is depicted.
 
-*   `yoob.TapeHead`, in `yoob/tape-head.js`
-    
-    An object representing a position on a Tape.
-
 *   `yoob.Stack`, in `yoob/stack.js`
     
     An object implementing a push-down, first-in-first-out stack of values,

File eg/tape.html

   <meta charset="utf-8">
   <title>yoob.Tape Demo</title>
   <script src="../src/yoob/tape.js"></script>
-  <script src="../src/yoob/tape-head.js"></script>
+  <script src="../src/yoob/cursor.js"></script>
   <style>
 #canvas { border: 1px solid blue; }
   </style>
 <script>
   var canvas = document.getElementById('canvas');
   var tape = new yoob.Tape();
-  var th = new yoob.TapeHead(tape, 0);
+  var th = new yoob.Cursor(0);
+  th.tape = tape;
 
   document.getElementById('write_0').onclick = function(e) {
     th.write("0");

File src/yoob/cursor.js

 if (window.yoob === undefined) yoob = {};
 
 /*
- * An object representing a pointer (position vector) into two-dimensional
- * Cartesian space (possibly a yoob.Playfield) with a direction vector
- * (that need not be used).
+ * An object representing a position and direction in some space.  The space
+ * may be one-dimensional (a yoob.Tape, or a string representing a program
+ * source) or a two-dimensional Cartesian space (such as a yoob.Playfield.)
+ * A direction vector accompanies the position, so the cursor can "know which
+ * way it's headed", but this facility need not be used.
  *
  * A cursor contains a built-in simple view, i.e. it knows how to render
  * itself on a canvas (drawContext method) or in the midst of HTML text
- * (wrapText method).  These methods are used by the playfield view classes.
- * The supplied methods draw basic block cursors in the colour given by
- * the fillStyle attribute, if present, or a light green if it is not set.
+ * (wrapText method).  These methods are used by the view classes (playfield,
+ * tape, source, etc.)  The default methods draw basic block cursors in the
+ * colour given by the fillStyle attribute, if present, or a light green if
+ * it is not defined.
  */
 yoob.Cursor = function(x, y, dx, dy) {
     this.x = x;
     this.dx = dx;
     this.dy = dy;
 
+    this.getX = function() {
+        return this.x;
+    };
+
+    this.getY = function() {
+        return this.y;
+    };
+
+    this.setX = function(x) {
+        this.x = x;
+    };
+
+    this.setY = function(y) {
+        this.y = y;
+    };
+
     this.isHeaded = function(dx, dy) {
         return this.dx === dx && this.dy === dy;
     };
         }
     };
 
+    /* from yoob.TapeHead; may go away or change slightly */
+    this.move = function(delta) {
+        this.x += delta;
+    };
+
+    this.moveLeft = function(amount) {
+        if (amount === undefined) amount = 1;
+        this.x -= amount;
+    };
+
+    this.moveRight = function(amount) {
+        if (amount === undefined) amount = 1;
+        this.x += amount;
+    };
+
+    this.read = function() {
+        if (!this.tape) return undefined;
+        return this.tape.get(this.x);
+    };
+
+    this.write = function(value) {
+        if (!this.tape) return;
+        this.tape.put(this.x, value);
+    };
+
     /*
      * For HTML views.  Override if you like.
      */

File src/yoob/tape-head.js

-/*
- * This file is part of yoob.js version 0.3
- * Available from https://github.com/catseye/yoob.js/
- * This file is in the public domain.  See http://unlicense.org/ for details.
- */
-if (window.yoob === undefined) yoob = {};
-
-/*
- * An object representing a location on a Tape.
- */
-yoob.TapeHead = function(tape, pos) {
-    this.tape = tape;
-    this.pos = pos;
-
-    this.move = function(delta) {
-        this.pos += delta;
-    };
-
-    this.moveLeft = function(amount) {
-        if (amount === undefined) amount = 1;
-        this.pos -= amount;
-    };
-
-    this.moveRight = function(amount) {
-        if (amount === undefined) amount = 1;
-        this.pos += amount;
-    };
-
-    this.read = function() {
-        if (this.tape === undefined) return undefined;
-        return this.tape.get(this.pos);
-    };
-
-    this.write = function(value) {
-        if (this.tape === undefined) return;
-        this.tape.put(this.pos, value);
-    };
-
-    this.drawContext = function(ctx, x, y, cellWidth, cellHeight) {
-        ctx.fillStyle = "#50ff50";
-        ctx.fillRect(x, y, cellWidth, cellHeight);
-    };
-}