git @ Cat's Eye Technologies noit-o-mnain-worb / ff6422c
Update yoob.js files to latest version, including maxY bug fix. Cat's Eye Technologies 8 years ago
2 changed file(s) with 56 addition(s) and 68 deletion(s). Raw diff Collapse all Expand all
00 /*
1 * This file is part of yoob.js version 0.3-PRE
1 * This file is part of yoob.js version 0.3
2 * Available from https://github.com/catseye/yoob.js/
23 * This file is in the public domain. See http://unlicense.org/ for details.
34 */
45 if (window.yoob === undefined) yoob = {};
00 /*
1 * This file is part of yoob.js version 0.2
1 * This file is part of yoob.js version 0.6-PRE
2 * Available from https://github.com/catseye/yoob.js/
23 * This file is in the public domain. See http://unlicense.org/ for details.
34 */
45 if (window.yoob === undefined) yoob = {};
2122 */
2223 this.setDefault = function(v) {
2324 this._default = v;
25 return this;
2426 };
2527
2628 /*
7779 this.minX = undefined;
7880 this.minY = undefined;
7981 this.maxX = undefined;
80 this.maxX = undefined;
82 this.maxY = undefined;
8183
8284 for (var cell in this._store) {
8385 var pos = cell.split(',');
98100 this.minX = undefined;
99101 this.minY = undefined;
100102 this.maxX = undefined;
101 this.maxX = undefined;
103 this.maxY = undefined;
104 };
105
106 /*
107 * Scroll a rectangular subrectangle of this Playfield, up.
108 * TODO: support other directions.
109 */
110 this.scrollRectangleY = function(dy, minX, minY, maxX, maxY) {
111 if (dy < 1) {
112 for (var y = minY; y <= (maxY + dy); y++) {
113 for (var x = minX; x <= maxX; x++) {
114 this.put(x, y, this.get(x, y - dy));
115 }
116 }
117 } else { alert("scrollRectangleY(" + dy + ") notImplemented"); }
118 };
119
120 this.clearRectangle = function(minX, minY, maxX, maxY) {
121 // Could also do this with a foreach that checks
122 // each position. Would be faster on sparser playfields.
123 for (var y = minY; y <= maxY; y++) {
124 for (var x = minX; x <= maxX; x++) {
125 this.put(x, y, undefined);
126 }
127 }
102128 };
103129
104130 /*
216242 };
217243
218244 /*
219 * Draws elements of the Playfield in a drawing context.
220 * x and y are canvas coordinates, and width and height
221 * are canvas units of measure.
222 * The default implementation just renders them as text,
223 * in black.
224 * Override if you wish to draw them differently.
225 */
226 this.drawElement = function(ctx, x, y, cellWidth, cellHeight, elem) {
227 ctx.fillStyle = "black";
228 ctx.fillText(elem.toString(), x, y);
229 };
230
231 /*
232 * Draws the Playfield in a drawing context.
233 * cellWidth and cellHeight are canvas units of measure for each cell.
234 */
235 this.drawContext = function(ctx, offsetX, offsetY, cellWidth, cellHeight) {
236 var me = this;
237 this.foreach(function (x, y, elem) {
238 me.drawElement(ctx, offsetX + x * cellWidth, offsetY + y * cellHeight,
239 cellWidth, cellHeight, elem);
240 });
241 };
242
245 * Accessors for the minimum (resp. maximum) x (resp. y) values of
246 * occupied (non-default-valued) cells in this Playfield. If there are
247 * no cells in this Playfield, these will refturn undefined. Note that
248 * these are not guaranteed to be tight bounds; if values in cells
249 * are deleted, these bounds may still be considered to be outside them.
250 */
251 this.getMinX = function() {
252 return this.minX;
253 };
254 this.getMaxX = function() {
255 return this.maxX;
256 };
257 this.getMinY = function() {
258 return this.minY;
259 };
260 this.getMaxY = function() {
261 return this.maxY;
262 };
263
264 /*
265 * Returns the number of occupied cells in the x direction.
266 */
243267 this.getExtentX = function() {
244268 if (this.maxX === undefined || this.minX === undefined) {
245269 return 0;
248272 }
249273 };
250274
275 /*
276 * Returns the number of occupied cells in the y direction.
277 */
251278 this.getExtentY = function() {
252279 if (this.maxY === undefined || this.minY === undefined) {
253280 return 0;
255282 return this.maxY - this.minY + 1;
256283 }
257284 };
258
259 /*
260 * Draws the Playfield, and a set of Cursors, on a canvas element.
261 * Resizes the canvas to the needed dimensions.
262 * cellWidth and cellHeight are canvas units of measure for each cell.
263 */
264 this.drawCanvas = function(canvas, cellWidth, cellHeight, cursors) {
265 var ctx = canvas.getContext('2d');
266
267 var width = this.getExtentX();
268 var height = this.getExtentY();
269
270 if (cellWidth === undefined) {
271 ctx.textBaseline = "top";
272 ctx.font = cellHeight + "px monospace";
273 cellWidth = ctx.measureText("@").width;
274 }
275
276 canvas.width = width * cellWidth;
277 canvas.height = height * cellHeight;
278
279 ctx.clearRect(0, 0, canvas.width, canvas.height);
280
281 ctx.textBaseline = "top";
282 ctx.font = cellHeight + "px monospace";
283
284 var offsetX = this.minX * cellWidth * -1;
285 var offsetY = this.minY * cellHeight * -1;
286
287 for (var i = 0; i < cursors.length; i++) {
288 cursors[i].drawContext(
289 ctx,
290 cursors[i].x * cellWidth, cursors[i].y * cellHeight,
291 cellWidth, cellHeight
292 );
293 }
294
295 this.drawContext(ctx, offsetX, offsetY, cellWidth, cellHeight);
296 };
297
298285 };