Text added to PIXI

destroy function added to textures too
docs updated
new example added
This commit is contained in:
Mat Groves 2013-04-24 20:54:03 +01:00
parent 7933cadb77
commit 09dbbd5d13
66 changed files with 10970 additions and 681 deletions

View file

@ -10,7 +10,7 @@
}
</style>
<script src="pixi.js"></script>
<script src="../../src/pixi/renderers/WebGLRenderer.js"></script>
</head>
<body>
<script>

View file

@ -4,7 +4,7 @@
* Copyright (c) 2012, Mat Groves
* http://goodboydigital.com/
*
* Compiled: 2013-04-22
* Compiled: 2013-04-16
*
* Pixi.JS is licensed under the MIT License.
* http://www.opensource.org/licenses/mit-license.php
@ -435,64 +435,6 @@ PIXI.DisplayObjectContainer.prototype.addChildAt = function(child, index)
}
}
/**
* Swaps the depth of 2 displayObjects
* @method swapChildren
* @param DisplayObject {DisplayObject}
* @param DisplayObject2 {DisplayObject}
*/
PIXI.DisplayObjectContainer.prototype.swapChildren = function(child, child2)
{
// TODO I already know this??
var index = this.children.indexOf( child );
var index2 = this.children.indexOf( child2 );
if ( index !== -1 && index2 !== -1 )
{
// cool
if(this.stage)
{
// this is to satisfy the webGL batching..
// TODO sure there is a nicer way to achieve this!
this.stage.__removeChild(child);
this.stage.__removeChild(child2);
this.stage.__addChild(child);
this.stage.__addChild(child2);
}
// swap the indexes..
child.childIndex = index2;
child2.childIndex = index;
// swap the positions..
this.children[index] = child2;
this.children[index2] = child;
}
else
{
throw new Error(child + " Both the supplied DisplayObjects must be a child of the caller " + this);
}
}
/**
* Returns the Child at the specified index
* @method getChildAt
* @param index {Number}
*/
PIXI.DisplayObjectContainer.prototype.getChildAt = function(index)
{
if(index >= 0 && index < this.children.length)
{
return this.children[index];
}
else
{
throw new Error(child + " Both the supplied DisplayObjects must be a child of the caller " + this);
}
}
/**
* Removes a child from the container.
* @method removeChild
@ -587,17 +529,19 @@ PIXI.Sprite = function(texture)
* @property width
* @type #Number
*/
this._width = 0;
this.width = 0;
/**
* The height of the sprite (this is initially set by the texture)
* @property height
* @type #Number
*/
this._height = 0;
this.height = 0;
if(texture.baseTexture.hasLoaded)
{
this.width = this.texture.frame.width;
this.height = this.texture.frame.height;
this.updateFrame = true;
}
else
@ -617,28 +561,6 @@ PIXI.Sprite = function(texture)
PIXI.Sprite.constructor = PIXI.Sprite;
PIXI.Sprite.prototype = Object.create( PIXI.DisplayObjectContainer.prototype );
// OOH! shiney new getters and setters for width and height
// The width and height now modify the scale (this is what flash does, nice and tidy!)
Object.defineProperty(PIXI.Sprite.prototype, 'width', {
get: function() {
return this.scale.x * this.texture.frame.width;
},
set: function(value) {
this.scale.x = value / this.texture.frame.width
this._width = value;
}
});
Object.defineProperty(PIXI.Sprite.prototype, 'height', {
get: function() {
return this.scale.y * this.texture.frame.height;
},
set: function(value) {
this.scale.y = value / this.texture.frame.height
this._height = value;
}
});
/**
@method setTexture
@param texture {Texture} The PIXI texture that is displayed by the sprite
@ -652,6 +574,8 @@ PIXI.Sprite.prototype.setTexture = function(texture)
}
this.texture = texture;
this.width = texture.frame.width;
this.height = texture.frame.height;
this.updateFrame = true;
}
@ -660,12 +584,8 @@ PIXI.Sprite.prototype.setTexture = function(texture)
*/
PIXI.Sprite.prototype.onTextureUpdate = function(event)
{
//this.texture.removeEventListener( 'update', this.onTextureUpdateBind );
// so if _width is 0 then width was not set..
if(this._width)this.scale.x = this._width / this.texture.frame.width;
if(this._height)this.scale.y = this._height / this.texture.frame.height;
this.width = this.width || this.texture.frame.width;
this.height = this.height || this.texture.frame.height;
this.updateFrame = true;
}
@ -738,20 +658,6 @@ PIXI.MovieClip = function(textures)
* @type Number
*/
this.animationSpeed = 1;
/**
* Whether or not the movie clip repeats after playing.
* @property loop
* @type Boolean
*/
this.loop = true;
/**
* Function to call when a MovieClip finishes playing
* @property onComplete
* @type Function
*/
this.onComplete = null;
/**
* [read only] indicates if the MovieClip is currently playing
@ -815,18 +721,7 @@ PIXI.MovieClip.prototype.updateTransform = function()
this.currentFrame += this.animationSpeed;
var round = (this.currentFrame + 0.5) | 0;
if(this.loop || round < this.textures.length)
{
this.setTexture(this.textures[round % this.textures.length]);
}
else if(round >= this.textures.length)
{
this.gotoAndStop(this.textures.length - 1);
if(this.onComplete)
{
this.onComplete();
}
}
this.setTexture(this.textures[round % this.textures.length]);
}
/**
* @author Mat Groves http://matgroves.com/ @Doormat23
@ -1134,7 +1029,7 @@ PIXI.InteractionManager.prototype.hitTest = function(item, interactionData)
var global = interactionData.global;
if(!item.visible)return false;
if(item instanceof PIXI.Sprite)
{
var worldTransform = item.worldTransform;
@ -1146,16 +1041,13 @@ PIXI.InteractionManager.prototype.hitTest = function(item, interactionData)
var x = a11 * id * global.x + -a01 * id * global.y + (a12 * a01 - a02 * a11) * id;
var y = a00 * id * global.y + -a10 * id * global.x + (-a12 * a00 + a02 * a10) * id;
var width = item.texture.frame.width;
var height = item.texture.frame.height;
var x1 = -item.width * item.anchor.x;
var x1 = -width * item.anchor.x;
if(x > x1 && x < x1 + width)
if(x > x1 && x < x1 + item.width)
{
var y1 = -height * item.anchor.y;
var y1 = -item.height * item.anchor.y;
if(y > y1 && y < y1 + height)
if(y > y1 && y < y1 + item.height)
{
return true;
}
@ -1189,8 +1081,8 @@ PIXI.InteractionManager.prototype.hitTest = function(item, interactionData)
for (var i = 0; i < length; i++)
{
var tempItem = item.children[i];
var hit = this.hitTest(tempItem, interactionData);
var item = item.children[i];
var hit = this.hitTest(item, interactionData);
if(hit)return true;
}
@ -1452,16 +1344,6 @@ PIXI.Stage.prototype.setBackgroundColor = function(backgroundColor)
this.backgroundColorString = "#" + this.backgroundColor.toString(16);
}
/**
* This will return the point containing global coords of the mouse.
* @method getMousePosition
* @return {Point} The point containing the coords of the global InteractionData position.
*/
PIXI.Stage.prototype.getMousePosition = function()
{
return this.interactionManager.mouse.global;
}
PIXI.Stage.prototype.__addChild = function(child)
{
if(child.interactive)this.dirty = true;
@ -2126,7 +2008,8 @@ PIXI.WebGLRenderer.prototype.render = function(stage)
{
if(this.contextLost)return;
// if rendering a new stage clear the batchs..
if(this.__stage !== stage)
{
@ -2142,7 +2025,6 @@ PIXI.WebGLRenderer.prototype.render = function(stage)
}
// update any textures
for (var i=0; i < PIXI.texturesToUpdate.length; i++) this.updateTexture(PIXI.texturesToUpdate[i]);
@ -2179,10 +2061,6 @@ PIXI.WebGLRenderer.prototype.render = function(stage)
{
this.batchs[i].render();
}
else if(renderable instanceof PIXI.TilingSprite)
{
if(renderable.visible)this.renderTilingSprite(renderable);
}
else if(renderable instanceof PIXI.Strip)
{
if(renderable.visible)this.renderStrip(renderable);
@ -2200,17 +2078,6 @@ PIXI.WebGLRenderer.prototype.render = function(stage)
stage.interactionManager.setTarget(this);
}
}
// after rendering lets confirm all frames that have been uodated..
if(PIXI.Texture.frameUpdates.length > 0)
{
for (var i=0; i < PIXI.Texture.frameUpdates.length; i++)
{
PIXI.Texture.frameUpdates[i].updateFrame = false;
};
PIXI.Texture.frameUpdates = [];
}
}
/**
@ -2232,22 +2099,9 @@ PIXI.WebGLRenderer.prototype.updateTexture = function(texture)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.source);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
// reguler...
//gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
//gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
if(!texture._powerOf2)
{
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
}
else
{
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
}
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
// gl.generateMipmap(gl.TEXTURE_2D);
gl.bindTexture(gl.TEXTURE_2D, null);
}
@ -2438,13 +2292,6 @@ PIXI.WebGLRenderer.prototype.addDisplayObject = function(displayObject)
this.batchs.push(batch);
}
}
else if(displayObject instanceof PIXI.TilingSprite)
{
// add to a batch!!
this.initTilingSprite(displayObject);
this.batchs.push(displayObject);
}
else if(displayObject instanceof PIXI.Strip)
{
@ -2453,7 +2300,7 @@ PIXI.WebGLRenderer.prototype.addDisplayObject = function(displayObject)
this.batchs.push(displayObject);
}
// if its somthing else... then custom codes!
this.batchUpdate = true;
}
@ -2560,127 +2407,6 @@ PIXI.WebGLRenderer.prototype.resize = function(width, height)
projectionMatrix[13] = 1;
}
/**
* @private
*/
PIXI.WebGLRenderer.prototype.initTilingSprite = function(sprite)
{
var gl = this.gl;
// make the texture tilable..
sprite.verticies = new Float32Array([0, 0,
sprite.width, 0,
sprite.width, sprite.height,
0, sprite.height]);
sprite.uvs = new Float32Array([0, 0,
1, 0,
1, 1,
0, 1]);
sprite.colors = new Float32Array([1,1,1,1]);
sprite.indices = new Uint16Array([0, 1, 3,2])//, 2]);
sprite._vertexBuffer = gl.createBuffer();
sprite._indexBuffer = gl.createBuffer();
sprite._uvBuffer = gl.createBuffer();
sprite._colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, sprite._vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, sprite.verticies, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, sprite._uvBuffer);
gl.bufferData(gl.ARRAY_BUFFER, sprite.uvs, gl.DYNAMIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, sprite._colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, sprite.colors, gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, sprite._indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, sprite.indices, gl.STATIC_DRAW);
// return ( (x > 0) && ((x & (x - 1)) == 0) );
if(sprite.texture.baseTexture._glTexture)
{
gl.bindTexture(gl.TEXTURE_2D, sprite.texture.baseTexture._glTexture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
sprite.texture.baseTexture._powerOf2 = true;
}
else
{
sprite.texture.baseTexture._powerOf2 = true;
}
/*
var context = this.context;
if(!sprite.__tilePattern) sprite.__tilePattern = context.createPattern(sprite.texture.baseTexture.source, "repeat");
context.beginPath();
var tilePosition = sprite.tilePosition;
var tileScale = sprite.tileScale;
// offset
context.scale(tileScale.x,tileScale.y);
context.translate(tilePosition.x, tilePosition.y);
context.fillStyle = sprite.__tilePattern;
context.fillRect(-tilePosition.x,-tilePosition.y,sprite.width / tileScale.x, sprite.height / tileScale.y);
context.translate(-tilePosition.x, -tilePosition.y);
context.scale(1/tileScale.x, 1/tileScale.y);
*/
}
/**
* @private
*/
PIXI.WebGLRenderer.prototype.renderTilingSprite = function(sprite)
{
var gl = this.gl;
var shaderProgram = this.shaderProgram;
var tilePosition = sprite.tilePosition;
var tileScale = sprite.tileScale;
var offsetX = tilePosition.x/sprite.texture.baseTexture.width;
var offsetY = tilePosition.y/sprite.texture.baseTexture.height;
var scaleX = (sprite.width / sprite.texture.baseTexture.width) / tileScale.x///sprite.texture.baseTexture.width;
var scaleY = (sprite.height / sprite.texture.baseTexture.height) / tileScale.y///sprite.texture.baseTexture.height;
//
//sprite.dirty = true;
sprite.uvs[0] = 0 + offsetX
sprite.uvs[1] = 0 - offsetY;
sprite.uvs[2] = (1 * scaleX) +offsetX
sprite.uvs[3] = 0 - offsetY;
sprite.uvs[4] = (1 *scaleX) + offsetX
sprite.uvs[5] = (1 *scaleY) - offsetY;
sprite.uvs[6] = 0 + offsetX
sprite.uvs[7] = (1 *scaleY) - offsetY;
gl.bindBuffer(gl.ARRAY_BUFFER, sprite._uvBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, sprite.uvs)
this.renderStrip(sprite);
}
/**
* @private
*/
@ -2778,7 +2504,7 @@ PIXI.WebGLRenderer.prototype.renderStrip = function(strip)
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, strip.indices, gl.STATIC_DRAW);
}
//console.log(gl.TRIANGLE_STRIP)
gl.drawElements(gl.TRIANGLE_STRIP, strip.indices.length, gl.UNSIGNED_SHORT, 0);
gl.uniformMatrix4fv(this.shaderProgram.mvMatrixUniform, false, this.projectionMatrix);
@ -3239,8 +2965,8 @@ PIXI.WebGLBatch.prototype.update = function()
while(displayObject)
{
width = displayObject.texture.frame.width;
height = displayObject.texture.frame.height;
width = displayObject.width;
height = displayObject.height;
aX = displayObject.anchor.x - displayObject.texture.trim.x
aY = displayObject.anchor.y - displayObject.texture.trim.y
@ -3273,7 +2999,7 @@ PIXI.WebGLBatch.prototype.update = function()
this.verticies[index + 6] = a * w1 + c * h0 + tx;
this.verticies[index + 7] = d * h0 + b * w1 + ty;
if(displayObject.updateFrame || displayObject.texture.updateFrame)
if(displayObject.updateFrame)
{
this.dirtyUVS = true;
@ -3483,12 +3209,6 @@ PIXI.CanvasRenderer.prototype.render = function(stage)
stage.interactionManager.setTarget(this);
}
}
// remove frame updates..
if(PIXI.Texture.frameUpdates.length > 0)
{
PIXI.Texture.frameUpdates = [];
}
}
/**
@ -3557,8 +3277,8 @@ PIXI.CanvasRenderer.prototype.renderDisplayObject = function(displayObject)
frame.height,
(displayObject.anchor.x - displayObject.texture.trim.x) * -frame.width,
(displayObject.anchor.y - displayObject.texture.trim.y) * -frame.height,
frame.width,
frame.height);
displayObject.width,
displayObject.height);
//}
}
}
@ -3567,19 +3287,12 @@ PIXI.CanvasRenderer.prototype.renderDisplayObject = function(displayObject)
context.setTransform(transform[0], transform[3], transform[1], transform[4], transform[2], transform[5])
this.renderStrip(displayObject);
}
else if(displayObject instanceof PIXI.TilingSprite)
{
context.setTransform(transform[0], transform[3], transform[1], transform[4], transform[2], transform[5])
this.renderTilingSprite(displayObject);
}
// render!
for (var i=0; i < displayObject.children.length; i++)
{
this.renderDisplayObject(displayObject.children[i]);
}
}
/**
@ -3617,33 +3330,6 @@ PIXI.CanvasRenderer.prototype.renderStripFlat = function(strip)
//context.globalCompositeOperation = 'source-over';
}
/**
* @private
*/
PIXI.CanvasRenderer.prototype.renderTilingSprite = function(sprite)
{
var context = this.context;
if(!sprite.__tilePattern) sprite.__tilePattern = context.createPattern(sprite.texture.baseTexture.source, "repeat");
context.beginPath();
var tilePosition = sprite.tilePosition;
var tileScale = sprite.tileScale;
// offset
context.scale(tileScale.x,tileScale.y);
context.translate(tilePosition.x, tilePosition.y);
context.fillStyle = sprite.__tilePattern;
context.fillRect(-tilePosition.x,-tilePosition.y,sprite.width / tileScale.x, sprite.height / tileScale.y);
context.scale(1/tileScale.x, 1/tileScale.y);
context.translate(-tilePosition.x, -tilePosition.y);
}
/**
* @private
*/
@ -3666,8 +3352,8 @@ PIXI.CanvasRenderer.prototype.renderStrip = function(strip)
var x0 = verticies[index], x1 = verticies[index+2], x2 = verticies[index+4];
var y0 = verticies[index+1], y1 = verticies[index+3], y2 = verticies[index+5];
var u0 = uvs[index] * strip.texture.width, u1 = uvs[index+2] * strip.texture.width, u2 = uvs[index+4]* strip.texture.width;
var v0 = uvs[index+1]* strip.texture.height, v1 = uvs[index+3] * strip.texture.height, v2 = uvs[index+5]* strip.texture.height;
var u0 = uvs[index] * strip.texture.width, u1 = uvs[index+2]* strip.texture.width, u2 = uvs[index+4]* strip.texture.width;
var v0 = uvs[index+1]* strip.texture.height, v1 = uvs[index+3]* strip.texture.height, v2 = uvs[index+5]* strip.texture.height;
context.save();
@ -3979,65 +3665,6 @@ PIXI.Rope.prototype.setTexture = function(texture)
/**
* @author Mat Groves http://matgroves.com/
*/
/**
* A tiling sprite is a fast way of rendering a tiling image
* @class TilingSprite
* @extends DisplayObjectContainer
* @constructor
* @param texture {Texture} the texture of the tiling sprite
* @param width {Number} the width of the tiling sprite
* @param height {Number} the height of the tiling sprite
*/
PIXI.TilingSprite = function(texture, width, height)
{
PIXI.DisplayObjectContainer.call( this );
this.texture = texture;
this.width = width;
this.height = height;
this.renderable = true;
/**
* The scaling of the image that is being tiled
* @property tileScale
* @type Point
*/
this.tileScale = new PIXI.Point(2,1);
/**
* The offset position of the image that is being tiled
* @property tileScale
* @type Point
*/
this.tilePosition = new PIXI.Point(0,0);
this.blendMode = PIXI.blendModes.NORMAL
}
// constructor
PIXI.TilingSprite.constructor = PIXI.TilingSprite;
PIXI.TilingSprite.prototype = Object.create( PIXI.DisplayObjectContainer.prototype );
PIXI.TilingSprite.prototype.setTexture = function(texture)
{
//TODO SET THE TEXTURES
//TODO VISIBILITY
// stop current texture
this.texture = texture;
this.updateFrame = true;
}
PIXI.TilingSprite.prototype.onTextureUpdate = function(event)
{
this.updateFrame = true;
}
// some helper functions..
/**
* @author Mat Groves http://matgroves.com/ @Doormat23
*/
@ -4120,37 +3747,15 @@ PIXI.BaseTexture = function(source)
PIXI.texturesToUpdate.push(this);
}
this._powerOf2 = false;
}
PIXI.BaseTexture.constructor = PIXI.BaseTexture;
/**
*
* Helper function that returns a base texture based on an image url
* If the image is not in the base texture cache it will be created and loaded
* @static
* @method fromImage
* @param imageUrl {String} The image url of the texture
* @return BaseTexture
*/
PIXI.BaseTexture.fromImage = function(imageUrl, crossorigin)
PIXI.BaseTexture.prototype.fromImage = function(imageUrl)
{
var baseTexture = PIXI.BaseTextureCache[imageUrl];
if(!baseTexture)
{
var image = new Image();
if (crossorigin)
{
image.crossOrigin = '';
}
image.src = imageUrl;
baseTexture = new PIXI.BaseTexture(image);
PIXI.BaseTextureCache[imageUrl] = baseTexture;
}
return baseTexture;
}
/**
@ -4179,9 +3784,6 @@ PIXI.Texture = function(baseTexture, frame)
}
this.trim = new PIXI.Point();
if(baseTexture instanceof PIXI.Texture)
baseTexture = baseTexture.baseTexture;
/**
* The base texture of this texture
@ -4245,11 +3847,7 @@ PIXI.Texture.prototype.setFrame = function(frame)
{
throw new Error("Texture Error: frame does not fit inside the base Texture dimensions " + this);
}
this.updateFrame = true;
PIXI.Texture.frameUpdates.push(this);
//this.dispatchEvent( { type: 'update', content: this } );
//this.updateFrame = true;
}
/**
@ -4267,9 +3865,24 @@ PIXI.Texture.fromImage = function(imageUrl, crossorigin)
if(!texture)
{
texture = new PIXI.Texture(PIXI.BaseTexture.fromImage(imageUrl, crossorigin));
var baseTexture = PIXI.BaseTextureCache[imageUrl];
if(!baseTexture)
{
var image = new Image();//new Image();
if (crossorigin)
{
image.crossOrigin = '';
}
image.src = imageUrl;
baseTexture = new PIXI.BaseTexture(image);
PIXI.BaseTextureCache[imageUrl] = baseTexture;
}
texture = new PIXI.Texture(baseTexture);
PIXI.TextureCache[imageUrl] = texture;
}
return texture;
@ -4332,9 +3945,6 @@ PIXI.Texture.removeTextureFromCache = function(id)
return texture;
}
// this is more for webGL.. it contains updated frames..
PIXI.Texture.frameUpdates = [];
/**
* @author Mat Groves http://matgroves.com/ @Doormat23