|
0 |
/*
|
|
1 |
* This file is part of yoob.js version 0.11-PRE
|
|
2 |
* Available from https://github.com/catseye/yoob.js/
|
|
3 |
* This file is in the public domain. See http://unlicense.org/ for details.
|
|
4 |
*/
|
|
5 |
if (window.yoob === undefined) yoob = {};
|
|
6 |
|
|
7 |
/*
|
|
8 |
* A view (in the MVC sense) for depicting a yoob.Tape (-compatible)
|
|
9 |
* object on an HTML5 <canvas> element (or compatible object).
|
|
10 |
*
|
|
11 |
* drawCursorsFirst defaults to true. This produces the pleasing visual
|
|
12 |
* effect of the cursor being behind the cell values, but only if the cell values
|
|
13 |
* themselves have transparent areas (e.g. if they're glyphs in some font.)
|
|
14 |
* If the cell values are solid and fill the entire cell, drawCursorsFirst: false
|
|
15 |
* may be in order.
|
|
16 |
*/
|
|
17 |
yoob.TapeCanvasView = function() {
|
|
18 |
this.init = function(cfg) {
|
|
19 |
this.tape = cfg.tape;
|
|
20 |
this.canvas = cfg.canvas;
|
|
21 |
this.ctx = this.canvas.getContext('2d');
|
|
22 |
this.fixedPosition = !!cfg.fixedPosition;
|
|
23 |
this.fixedSizeCanvas = !!cfg.fixedSizeCanvas;
|
|
24 |
this.drawCursorsFirst = (cfg.drawCursorsFirst === undefined) ? true : !!cfg.drawCursorsFirst;
|
|
25 |
this.setCellDimensions(cfg.cellWidth || 8, cfg.cellHeight || 8);
|
|
26 |
return this;
|
|
27 |
};
|
|
28 |
|
|
29 |
/*** Chainable setters ***/
|
|
30 |
|
|
31 |
this.setTape = function(tape) {
|
|
32 |
this.tape = tape;
|
|
33 |
return this;
|
|
34 |
};
|
|
35 |
|
|
36 |
this.setCanvas = function(element) {
|
|
37 |
this.canvas = element;
|
|
38 |
this.ctx = this.canvas.getContext('2d');
|
|
39 |
return this;
|
|
40 |
};
|
|
41 |
|
|
42 |
/*
|
|
43 |
* Set the displayed dimensions of every cell.
|
|
44 |
* cellWidth and cellHeight are canvas units of measure for each cell.
|
|
45 |
* If cellWidth is undefined, the width of a character in the monospace
|
|
46 |
* font of cellHeight pixels is used.
|
|
47 |
*/
|
|
48 |
this.setCellDimensions = function(cellWidth, cellHeight) {
|
|
49 |
this.ctx.textBaseline = "top";
|
|
50 |
this.ctx.font = cellHeight + "px monospace";
|
|
51 |
|
|
52 |
if (cellWidth === undefined) {
|
|
53 |
cellWidth = this.ctx.measureText("@").width;
|
|
54 |
}
|
|
55 |
|
|
56 |
this.cellWidth = cellWidth;
|
|
57 |
this.cellHeight = cellHeight;
|
|
58 |
return this;
|
|
59 |
};
|
|
60 |
|
|
61 |
/*
|
|
62 |
* Draws cells of the Tape in a drawing context.
|
|
63 |
* cellWidth and cellHeight are canvas units of measure.
|
|
64 |
*
|
|
65 |
* The default implementation tries to call a .draw() method on the cell's
|
|
66 |
* value, if one exists, and just renders it as text, in black, if not.
|
|
67 |
*
|
|
68 |
* Override if you wish to draw elements in some other way.
|
|
69 |
*/
|
|
70 |
this.drawCell = function(ctx, value, pos,
|
|
71 |
canvasX, canvasY, cellWidth, cellHeight) {
|
|
72 |
if (value.draw !== undefined) {
|
|
73 |
value.draw(ctx, pos, canvasX, canvasY,
|
|
74 |
cellWidth, cellHeight);
|
|
75 |
} else {
|
|
76 |
ctx.fillStyle = "black";
|
|
77 |
ctx.fillText(value.toString(), canvasX, canvasY);
|
|
78 |
}
|
|
79 |
};
|
|
80 |
|
|
81 |
/*
|
|
82 |
* Draws the Tape in a drawing context.
|
|
83 |
* cellWidth and cellHeight are canvas units of measure for each cell.
|
|
84 |
* offsetX and offsetY are canvas units of measure for the top-left
|
|
85 |
* of the entire playfield.
|
|
86 |
*/
|
|
87 |
this.drawContext = function(ctx, offsetX, offsetY, cellWidth, cellHeight) {
|
|
88 |
var $this = this;
|
|
89 |
this.tape.foreach(function (pos, value) {
|
|
90 |
$this.drawCell(ctx, value, pos,
|
|
91 |
offsetX + pos * cellWidth, offsetY,
|
|
92 |
cellWidth, cellHeight);
|
|
93 |
});
|
|
94 |
};
|
|
95 |
|
|
96 |
/*
|
|
97 |
* Override if you like.
|
|
98 |
*/
|
|
99 |
this.drawCursor = function(ctx, cursor, canvasX, canvasY, cellWidth, cellHeight) {
|
|
100 |
ctx.fillStyle = this.cursorFillStyle || "#50ff50";
|
|
101 |
ctx.fillRect(canvasX, canvasY, cellWidth, cellHeight);
|
|
102 |
};
|
|
103 |
|
|
104 |
this.drawCursors = function(ctx, offsetX, offsetY, cellWidth, cellHeight) {
|
|
105 |
var cursors = this.tape.cursors;
|
|
106 |
for (var i = 0; i < cursors.length; i++) {
|
|
107 |
var cursor = cursors[i];
|
|
108 |
this.drawCursor(ctx, cursor,
|
|
109 |
offsetX + cursor.getX() * cellWidth, offsetY,
|
|
110 |
cellWidth, cellHeight);
|
|
111 |
}
|
|
112 |
};
|
|
113 |
|
|
114 |
/*
|
|
115 |
* Draw the Tape, and its set of Cursors, on the canvas element.
|
|
116 |
* Resizes the canvas to the needed dimensions first.
|
|
117 |
*/
|
|
118 |
this.draw = function() {
|
|
119 |
var canvas = this.canvas;
|
|
120 |
var cellWidth = this.cellWidth;
|
|
121 |
var cellHeight = this.cellHeight;
|
|
122 |
|
|
123 |
var width = this.tape.getCursoredExtent(); // this.max - this.min + 1;
|
|
124 |
var height = 1;
|
|
125 |
|
|
126 |
canvas.width = width * cellWidth;
|
|
127 |
canvas.height = height * cellHeight;
|
|
128 |
this.ctx = canvas.getContext('2d');
|
|
129 |
var ctx = this.ctx;
|
|
130 |
|
|
131 |
ctx.textBaseline = "top";
|
|
132 |
ctx.font = cellHeight + "px monospace";
|
|
133 |
|
|
134 |
if (cellWidth === undefined) {
|
|
135 |
cellWidth = ctx.measureText("@").width;
|
|
136 |
}
|
|
137 |
|
|
138 |
var offsetX = 0; // this.min * cellWidth * -1;
|
|
139 |
var offsetY = 0;
|
|
140 |
|
|
141 |
/*
|
|
142 |
if (!this.fixedPosition) {
|
|
143 |
offsetX = (this.pf.getLowerX() || 0) * cellWidth * -1;
|
|
144 |
offsetY = (this.pf.getLowerY() || 0) * cellHeight * -1;
|
|
145 |
}
|
|
146 |
*/
|
|
147 |
|
|
148 |
if (this.drawCursorsFirst) {
|
|
149 |
this.drawCursors(ctx, offsetX, offsetY, cellWidth, cellHeight);
|
|
150 |
}
|
|
151 |
|
|
152 |
this.drawContext(ctx, offsetX, offsetY, cellWidth, cellHeight);
|
|
153 |
|
|
154 |
if (!this.drawCursorsFirst) {
|
|
155 |
this.drawCursors(ctx, offsetX, offsetY, cellWidth, cellHeight);
|
|
156 |
}
|
|
157 |
};
|
|
158 |
};
|