Move Drawable.NONE into new RenderConstants

This constant will soon be shared with another class.
This commit is contained in:
Christopher Willis-Ford 2016-12-22 12:58:58 -08:00
parent 71ad4a8a80
commit 4730a057ff
3 changed files with 28 additions and 23 deletions

View file

@ -2,6 +2,7 @@ const twgl = require('twgl.js');
const xhr = require('xhr'); const xhr = require('xhr');
const Rectangle = require('./Rectangle'); const Rectangle = require('./Rectangle');
const RenderConstants = require('./RenderConstants');
const SvgRenderer = require('./svg-quirks-mode/svg-renderer'); const SvgRenderer = require('./svg-quirks-mode/svg-renderer');
const ShaderManager = require('./ShaderManager'); const ShaderManager = require('./ShaderManager');
@ -86,14 +87,6 @@ class Drawable {
this.setSkin(Drawable._DEFAULT_SKIN); this.setSkin(Drawable._DEFAULT_SKIN);
} }
/**
* An invalid Drawable ID which can be used to signify absence, etc.
* @type {int}
*/
static get NONE () {
return -1;
}
/** /**
* Fetch a Drawable by its ID number. * Fetch a Drawable by its ID number.
* @param {int} drawableID The ID of the Drawable to fetch. * @param {int} drawableID The ID of the Drawable to fetch.
@ -496,12 +489,12 @@ class Drawable {
/** /**
* Calculate a color to represent the given ID number. At least one component of * Calculate a color to represent the given ID number. At least one component of
* the resulting color will be non-zero if the ID is not Drawable.NONE. * the resulting color will be non-zero if the ID is not RenderConstants.ID_NONE.
* @param {int} id The ID to convert. * @param {int} id The ID to convert.
* @returns {number[]} An array of [r,g,b,a], each component in the range [0,1]. * @returns {number[]} An array of [r,g,b,a], each component in the range [0,1].
*/ */
static color4fFromID (id) { static color4fFromID (id) {
id -= Drawable.NONE; id -= RenderConstants.ID_NONE;
const r = ((id >> 0) & 255) / 255.0; const r = ((id >> 0) & 255) / 255.0;
const g = ((id >> 8) & 255) / 255.0; const g = ((id >> 8) & 255) / 255.0;
const b = ((id >> 16) & 255) / 255.0; const b = ((id >> 16) & 255) / 255.0;
@ -510,7 +503,7 @@ class Drawable {
/** /**
* Calculate the ID number represented by the given color. If all components of * Calculate the ID number represented by the given color. If all components of
* the color are zero, the result will be Drawable.NONE; otherwise the result * the color are zero, the result will be RenderConstants.ID_NONE; otherwise the result
* will be a valid ID. * will be a valid ID.
* @param {int} r The red value of the color, in the range [0,255]. * @param {int} r The red value of the color, in the range [0,255].
* @param {int} g The green value of the color, in the range [0,255]. * @param {int} g The green value of the color, in the range [0,255].
@ -522,7 +515,7 @@ class Drawable {
id = (r & 255) << 0; id = (r & 255) << 0;
id |= (g & 255) << 8; id |= (g & 255) << 8;
id |= (b & 255) << 16; id |= (b & 255) << 16;
return id + Drawable.NONE; return id + RenderConstants.ID_NONE;
} }
} }

11
src/RenderConstants.js Normal file
View file

@ -0,0 +1,11 @@
/**
* Various constants meant for use throughout the renderer.
* @type {object}
*/
module.exports = {
/**
* The ID value to use for "no item" or when an object has been disposed.
* @type {int}
*/
ID_NONE: -1
};

View file

@ -2,6 +2,7 @@ const hull = require('hull.js');
const twgl = require('twgl.js'); const twgl = require('twgl.js');
const Drawable = require('./Drawable'); const Drawable = require('./Drawable');
const RenderConstants = require('./RenderConstants');
const ShaderManager = require('./ShaderManager'); const ShaderManager = require('./ShaderManager');
/** /**
@ -341,7 +342,7 @@ class RenderWebGL {
gl.viewport(0, 0, bounds.width, bounds.height); gl.viewport(0, 0, bounds.width, bounds.height);
const projection = twgl.m4.ortho(bounds.left, bounds.right, bounds.bottom, bounds.top, -1, 1); const projection = twgl.m4.ortho(bounds.left, bounds.right, bounds.bottom, bounds.top, -1, 1);
const noneColor = Drawable.color4fFromID(Drawable.NONE); const noneColor = Drawable.color4fFromID(RenderConstants.ID_NONE);
gl.clearColor.apply(gl, noneColor); gl.clearColor.apply(gl, noneColor);
gl.clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT); gl.clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
@ -383,7 +384,7 @@ class RenderWebGL {
pixels[pixelBase], pixels[pixelBase],
pixels[pixelBase + 1], pixels[pixelBase + 1],
pixels[pixelBase + 2]); pixels[pixelBase + 2]);
if (pixelID > Drawable.NONE) { if (pixelID > RenderConstants.ID_NONE) {
return true; return true;
} }
} }
@ -399,7 +400,7 @@ class RenderWebGL {
* @param {int} touchHeight The client height of the touch event (optional). * @param {int} touchHeight The client height of the touch event (optional).
* @param {int[]} candidateIDs The Drawable IDs to pick from, otherwise all. * @param {int[]} candidateIDs The Drawable IDs to pick from, otherwise all.
* @returns {int} The ID of the topmost Drawable under the picking location, or * @returns {int} The ID of the topmost Drawable under the picking location, or
* Drawable.NONE if there is no Drawable at that location. * RenderConstants.ID_NONE if there is no Drawable at that location.
*/ */
pick (centerX, centerY, touchWidth, touchHeight, candidateIDs) { pick (centerX, centerY, touchWidth, touchHeight, candidateIDs) {
const gl = this._gl; const gl = this._gl;
@ -427,7 +428,7 @@ class RenderWebGL {
twgl.bindFramebufferInfo(gl, this._pickBufferInfo); twgl.bindFramebufferInfo(gl, this._pickBufferInfo);
gl.viewport(0, 0, touchWidth, touchHeight); gl.viewport(0, 0, touchWidth, touchHeight);
const noneColor = Drawable.color4fFromID(Drawable.NONE); const noneColor = Drawable.color4fFromID(RenderConstants.ID_NONE);
gl.clearColor.apply(gl, noneColor); gl.clearColor.apply(gl, noneColor);
gl.clear(gl.COLOR_BUFFER_BIT); gl.clear(gl.COLOR_BUFFER_BIT);
@ -467,9 +468,9 @@ class RenderWebGL {
} }
// Bias toward selecting anything over nothing // Bias toward selecting anything over nothing
hits[Drawable.NONE] = 0; hits[RenderConstants.ID_NONE] = 0;
let hit = Drawable.NONE; let hit = RenderConstants.ID_NONE;
for (const hitID in hits) { for (const hitID in hits) {
if (hits.hasOwnProperty(hitID) && (hits[hitID] > hits[hit])) { if (hits.hasOwnProperty(hitID) && (hits[hitID] > hits[hit])) {
hit = hitID; hit = hitID;
@ -663,8 +664,8 @@ class RenderWebGL {
twgl.bindFramebufferInfo(gl, this._queryBufferInfo); twgl.bindFramebufferInfo(gl, this._queryBufferInfo);
gl.viewport(0, 0, width, height); gl.viewport(0, 0, width, height);
// Clear the canvas with Drawable.NONE. // Clear the canvas with RenderConstants.ID_NONE.
const noneColor = Drawable.color4fFromID(Drawable.NONE); const noneColor = Drawable.color4fFromID(RenderConstants.ID_NONE);
gl.clearColor.apply(gl, noneColor); gl.clearColor.apply(gl, noneColor);
gl.clear(gl.COLOR_BUFFER_BIT); gl.clear(gl.COLOR_BUFFER_BIT);
@ -692,7 +693,7 @@ class RenderWebGL {
* Helper method to look up a pixel. * Helper method to look up a pixel.
* @param {int} x X coordinate of the pixel in `pixels`. * @param {int} x X coordinate of the pixel in `pixels`.
* @param {int} y Y coordinate of the pixel in `pixels`. * @param {int} y Y coordinate of the pixel in `pixels`.
* @return {int} Known ID at that pixel, or Drawable.NONE. * @return {int} Known ID at that pixel, or RenderConstants.ID_NONE.
*/ */
const _getPixel = (x, y) => { const _getPixel = (x, y) => {
const pixelBase = ((width * y) + x) * 4; const pixelBase = ((width * y) + x) * 4;
@ -704,14 +705,14 @@ class RenderWebGL {
for (let y = 0; y <= height; y++) { for (let y = 0; y <= height; y++) {
// Scan from left. // Scan from left.
for (let x = 0; x < width; x++) { for (let x = 0; x < width; x++) {
if (_getPixel(x, y) > Drawable.NONE) { if (_getPixel(x, y) > RenderConstants.ID_NONE) {
boundaryPoints.push([x, y]); boundaryPoints.push([x, y]);
break; break;
} }
} }
// Scan from right. // Scan from right.
for (let x = width - 1; x >= 0; x--) { for (let x = width - 1; x >= 0; x--) {
if (_getPixel(x, y) > Drawable.NONE) { if (_getPixel(x, y) > RenderConstants.ID_NONE) {
boundaryPoints.push([x, y]); boundaryPoints.push([x, y]);
break; break;
} }