this.creatine = this.creatine || {}; if (typeof this.tine === 'undefined') { this.tine = this.creatine; } (function() { "use strict"; creatine.DEFAULT_CONFIG = { project : 'creatine_game', width : 800, height : 600, container : null, framerate : 60, showfps : false, background_color : '#000', resources : { base_path : './' } } creatine.version = '1.0.0'; creatine.TOPLEFT = 'topleft'; creatine.TOPRIGHT = 'topright'; creatine.BOTTOMLEFT = 'bottomleft'; creatine.BOTTOMRIGHT = 'bottomright'; creatine.TOP = 'top'; creatine.BOTTOM = 'bottom'; creatine.LEFT = 'left'; creatine.RIGHT = 'right'; creatine.CENTER = 'center'; creatine.HORIZONTAL = 10; creatine.VERTICAL = 11; creatine.LEFT_TO_RIGHT = 12; creatine.RIGHT_TO_LEFT = 13; creatine.TOP_TO_BOTTOM = 14; creatine.BOTTOM_TO_TOP = 15; creatine.STRETCH = 16; creatine.FIT = 17; creatine.FILL = 18; creatine.NOSCALE = 19; })(); creatine.randomInt = function(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } creatine.clip = function(v, min, max) { return Math.max(Math.min(v, max), min); } creatine.degrees = function(radians) { return radians*57.2957795; } creatine.radians = function(degrees) { return degrees*0.0174532925; } /** a1=from, a2=to. Degrees */ creatine.angleDistance = function(a1, a2) { a1 = (a1<0)? 360+a1%360 : a1%360; a2 = (a2<0)? 360+a2%360 : a2%360; var d = a2-a1; if (d<0) d+=360; if (d>180) d = 360-d; return d; } creatine.angleMinDirection = function(a1, a2) { a1 = (a1<0)? 360+a1%360 : a1%360; a2 = (a2<0)? 360+a2%360 : a2%360; var d = a2-a1; if (d<0) d+=360; if (d>180) { return -1; } else { return 1; } } // http://stackoverflow.com/questions/11197247/javascript-equivalent-of-jquerys-extend-method creatine.merge = function() { for(var i=1; i= 0; } p.preUpdate = function() { } p.update = function() {} p.postUpdate = function() { this._lastState = this._state.slice(); } //--------------------------------------------------------------------------- // PUBLIC ACCESS //--------------------------------------------------------------------------- // p.setCommand = function(command, keys) { // // key code // if (typeof keys === 'number') { // keys = [keys]; // } else if (typeof keys !== 'array') { // throw Error('Invalid command keys "'+keys+'". Please provide a keycode or array'); // } // this._commandMap[command] = keys; // } // p.unsetCommand = function(command) { // delete this._commandMap[command]; // } p.get = function(key) { return this._get(key, this._state); } p.isDown = function(key) { return this._get(key, this._state); } p.isUp = function(key) { return !this._get(key, this._state); } p.isPressed = function(key) { return this._get(key, this._state) && !this._get(key, this._lastState); } p.isReleased = function(key) { return !this._get(key, this._state) && this._get(key, this._lastState); } p.isAnyDown = function() { return this._state.length > 0; } p.isAnyPressed = function() { for (var i=0; i= 0; } p.preUpdate = function() { this.x = this.game.stage.mouseX; this.y = this.game.stage.mouseY; } p.update = function() {} p.postUpdate = function() { this._lastState = this._state.slice(); } //--------------------------------------------------------------------------- // PUBLIC ACCESS //--------------------------------------------------------------------------- p.get = function(key) { return this._get(key, this._state); } p.isDown = function(key) { return this._get(key, this._state); } p.isUp = function(key) { return !this._get(key, this._state); } p.isPressed = function(key) { return this._get(key, this._state) && !this._get(key, this._lastState); } p.isReleased = function(key) { return !this._get(key, this._state) && this._get(key, this._lastState); } p.isAnyDown = function() { return this._state.length > 0; } p.isAnyPressed = function() { for (var i=0; i= 0; } p._makeEvent = function(name, e) { var event = new createjs.Event(name); event.target = this; return event; } // -------------------------------------------------------------------------- p.preUpdate = function() { if (this._gamepad && this._listening) { // get raw values for axes var lx = this.rawLeftStickX = this._gamepad.axes[0]; var ly = this.rawLeftStickY = this._gamepad.axes[1]; var rx = this.rawRightStickX = this._gamepad.axes[2]; var ry = this.rawRightStickY = this._gamepad.axes[3]; // compute the scaled radial dead zone // left stick var leftzone = this.deadZoneLeft; var leftnorm = Math.sqrt(lx*lx + ly*ly); if (leftnorm > leftzone) { var scale = ((leftnorm-leftzone)/(1-leftzone)); this.leftStickX = lx = (lx/leftnorm)*scale; this.leftStickY = ly = (ly/leftnorm)*scale; this.leftStickForce = Math.sqrt(lx*lx + ly*ly); // normalize again if necessary (avoid box range) if (this.leftStickForce >= 1) { this.leftStickX /= this.leftStickForce; this.leftStickY /= this.leftStickForce; this.leftStickForce = 1; } } else { this.leftStickX = this.leftStickY = this.leftStickForce = 0; } // right stick var rightzone = this.deadZoneLeft; var rightnorm = Math.sqrt(rx*rx + ry*ry); if (rightnorm > rightzone) { var scale = ((rightnorm-rightzone)/(1-rightzone)); this.rightStickX = rx = (rx/rightnorm)*scale; this.rightStickY = ry = (ry/rightnorm)*scale; this.rightStickForce = Math.sqrt(rx*rx + ry*ry); // normalize again if necessary (avoid box range) if (this.rightStickForce >= 1) { this.rightStickX /= this.rightStickForce; this.rightStickY /= this.rightStickForce; this.rightStickForce = 1; } } else { this.rightStickX = this.rightStickY = this.rightStickForce = 0; } // get button values this.leftTrigger = this._gamepad.buttons[6].value; this.rightTrigger = this._gamepad.buttons[7].value; this._state = []; for (var i=0; i 0; } p.isAnyPressed = function() { for (var i=0; i= 0) { this.stage.removeChild(this.scene); this._sceneStack.splice(index); } // verify in which position should add the new scene in the stage if (this.scene) { var i = this.stage.getChildIndex(this.scene); this.stage.addChildAt(scene, i+1); } else { this.stage.addChild(scene); } } p._startTransition = function(outScene, inScene, transition, callback) { if (transition) { this._transition = transition; transition.run(this, outScene||{}, inScene||{}, callback); } else { callback(); } } p._isValidChange = function(scene) { return !(this._transition!==null || this.scene===scene || !scene); } p._exitCurrentScene = function() { if (!this.scene) return; this.scene.exit(); this.stage.removeChild(this.scene); } p._pauseCurrentScene = function() { if (!this.scene) return; this.scene._pause(); this._sceneStack.push(this.scene); } p._resumeNextScene = function() { if (!this._nextScene) return; this.scene = this._nextScene; this.scene._resume(); this._sceneStack.pop(); this._nextScene = null; } p._enterNextScene = function() { if (!this._nextScene) return; this.scene = this._nextScene; this.scene._enter(); this._nextScene = null; } //--------------------------------------------------------------------------- // CALLBACK (CALLED BY GAME) //--------------------------------------------------------------------------- p.update = function() { if (this.scene && !this.scene.paused) this.scene._update(); for (var i=0; i rate) { this.addParticle(this.emitX, this.emitY); this.emitCounter -= rate; } this.elapsed += fdelta; if (this.duration != -1 && this.duration < this.elapsed) { this.active = false; } } // # update particle count // self.particle_count = numpy.sum(self.particle_life >= 0) // if self.active: // rate = 1.0 / self.emission_rate // self.emit_counter += delta // while self.particle_count < self.total_particles and self.emit_counter > rate: // self.add_particle() // self.emit_counter -= rate // self.elapsed += delta // if self.duration != -1 and self.duration < self.elapsed: // self.stop_system() var i=0, p, p_, step; var startAlpha = this.startAlpha; var endAlpha = this.endAlpha; var N = this._activeParticles; var particles = this._particles; while (iData Format * * In relation to the tilesets properties, the data must be in the * following format: * * var data = { * 'firstgid' : [Integer], // mandatory * 'name' : [String], // mandatory * 'image' : [String], // mandatory * 'imagewidth' : [Integer], // default to image.width * 'imageheight' : [Integer], // default to image.height * 'tilewidth' : [Integer], // default to 32 * 'tileheight' : [Integer], // default to 32 * 'spacing' : [Integer], // default to 0 * 'margin' : [Integer], // default to 0 * 'tileoffset' : [Object], // default to {x:0, y:0} * 'properties' : [Object], * } * * Consult the official TMX description to known more: * https://github.com/bjorn/tiled/wiki/TMX-Map-Format * * @class TMXTileset * @param {Object} data The data object describing the tileset. * @constructor **/ var TMXTileset = function(data) { /** * The first global tile ID of this tileset. * * @property firstgid * @type {Integer} * @readonly **/ this.firstgid = null; /** * The name of this tileset. * * @property name * @type {String} * @readonly **/ this.name = null; /** * The image width in pixels. * * @property width * @type {Integer} * @readonly **/ this.width = null; /** * The image height in pixels. * * @property height * @type {Integer} * @readonly **/ this.height = null; /** * The width of the tiles in this tileset. * * @property width * @type {Integer} * @readonly **/ this.tileWidth = null; /** * The height of the tiles in this tileset. * * @property height * @type {Integer} * @readonly **/ this.tileHeight = null; /** * The path to the image associated to this tilset. * * @property imagePath * @type {String} * @readonly **/ this.imagePath = null; /** * The offsetX to the tile origin. Notice that, tiled assumes (0, 0) as * the left-bottom. * * @property tileOffsetX * @type {Integer} * @readonly **/ this.tileOffsetX = null; /** * The offsetY to the tile origin. Notice that, tiled assumes (0, 0) as * the left-bottom. * * @property tileOffsetY * @type {Integer} * @readonly **/ this.tileOffsetY = null; /** * The spacing between tiles on the image. * * @property spacing * @type {Integer} * @readonly **/ this.spacing = null; /** * The margin of the image. * * @property spacing * @type {Integer} * @readonly **/ this.margin = null; /** * An object with properties defined by user. * * @property properties * @type {Object} * @readonly **/ this.properties = null; /** * An object with tile properties defined by user. * * @property properties * @type {Object} * @readonly **/ this.tileProperties = null; /** * The spritesheet object loaded by this tileset. * * @property spritesheet * @type {createjs.SpriteSheet} * @readonly **/ this.spritesheet = null; /** * Animation dict contained in the tileset. * * @property animations * @type {Object} * @readonly **/ this.animations = null; this._initialize(data); } var p = TMXTileset.prototype; /** * Initialization method. * * @method _initialize * @param {Object} data The data object describing the tileset. * @protected **/ p._initialize = function(data) { this.firstgid = data['firstgid']; this.name = data['name']; this.imagePath = data['image']; this.width = data['imagewidth']; this.height = data['imageheight']; this.tileWidth = data['tilewidth'] || 32; this.tileHeight = data['tileheight'] || 32; this.spacing = data['spacing'] || 0; this.margin = data['margin'] || 0; this.properties = data['properties']; this.tileProperties = data['tileproperties']; this.tileOffsetX = 0; this.tileOffsetY = 0; this.animations = {}; if (data['tileoffset']) { this.tileOffsetX = data['tileoffset']['x']; this.tileOffsetY = data['tileoffset']['y']; } if (data['tiles']) { for (var key in data['tiles']) { var info = data['tiles'][key]; if (info['animation']) { var frames = []; var speeds = []; for (var i=0; iTMXOrthogonalTileLayer, * TMXIsometricTileLayer and * TMXStaggeredTileLayer. The difference among projections is * performed at layer-level, making new projections and extensions of the * current ones a lot simpler to be created. This also allows you to use * multiple projections to the same map (unfortunately, this isn't * supported by Tiled yet). * * @class TMXTileLayer * @param {creatine.TMXMap} map The map object. * @param {Object} data The data object (from tmx format). * @extends createjs.Container * @constructor **/ var TMXTileLayer = function(map, data) { /** * Reference to the TMX map. * * @property map * @type {creatine.TMXMap} * @readonly **/ this.map = null; /** * The name of the layer. * * @property name * @type {String} * @readonly **/ this.name = null; /** * The list of tile ids in the layer. * * @property data * @type {Array} * @readonly **/ this.data = null; /** * The amount of tiles in x axis. * * @property width * @type {Integer} * @readonly **/ this.width = null; /** * The amount of tiles in y axis. * * @property y * @type {Integer} * @readonly **/ this.height = null; this._initialize(map, data); } var p = createjs.extend(TMXTileLayer, createjs.Container); /** * Initialization method. * * @method _initialize * @param {creatine.TMXMap} map The map object. * @param {Object} data The data object (from tmx format). * @protected **/ p._initialize = function(map, data) { this.Container_constructor(); if (!map) return; this.map = map; this.name = data['name']; this.data = data['data']; this.x = data['x']; this.y = data['y']; this.width = data['width']; this.height = data['height']; this.visible = data['visible']; this.alpha = data['opacity']; this._createTiles(); } /** * Create the tiles. * * Override this method on the child class. * * @method _createTiles * @protected **/ p._createTiles = function() { throw new Error('TMXTileLayer._createTiles not implemented.'); } /** * Return the tile value, given a coord of a tile. * * @method getTile * @param {Integer} x The column of a tile. * @param {Integer} y The row of a tile. * @return {Integer} The tile id. **/ p.getTile = function(x, y) { return this.data[(this.width*y + x)] } /** * Set the tile value, given a coord of a tile. * * @method getTile * @param {Integer} x The column of a tile. * @param {Integer} y The row of a tile. * @param {Integer} gid The global ID. **/ p.setTile = function(x, y, gid) { var idx = (this.width*y + x); var tileset = this.map.getTilesetByGid(gid); var tileId = gid-tileset.firstgid; var tile = this.getChildAt(idx) this.data[idx] = tileId; if (tileset.animations['anim-'+(tileId)]) { tile.gotoAndPlay('anim-'+(tileId)); } else { tile.gotoAndStop(tileId); } } /** * Return the coord of a tile given the local position. * * Override this method on the child class. * * @method getTile * @param {Integer} x The position x. * @param {Integer} y The position y. * @return {createjs.Point} The tile coord. **/ p.getCoords = function(x, y) { throw new Error('TMXTileLayer.getCoords not implemented.'); } /** * Return the top-left local position of a tile, considering its coords * (column and row). * * Override this method on the child class. * * @method getTile * @param {Integer} x The column of the tile. * @param {Integer} y The row of the tile. * @return {createjs.Point} The top-left tile local position. **/ p.getPosition = function(x, y) { throw new Error('TMXTileLayer.getPosition not implemented.'); } creatine.TMXTileLayer = createjs.promote(TMXTileLayer, "Container"); }());/* * TMXOrthogonalTileLayer * * Copyright (c) 2014 Renato de Pontes Pereira. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /** * @module Creatine **/ // namespace: this.creatine = this.creatine || {}; (function() { "use strict"; /** * TMXOrthogonalTileLayer represents a TileLayer in the TMX map. * * @class TMXOrthogonalTileLayer * @param {creatine.TMXMap} map The map object. * @param {Object} data The data object (from tmx format). * @extends creatine.TMXTileLayer * @constructor **/ var TMXOrthogonalTileLayer = function(map, data) { this.TMXTileLayer_constructor(map, data); } var p = createjs.extend(TMXOrthogonalTileLayer, creatine.TMXTileLayer); /** * Create the tiles. Override this method on the child class. * * @method _createTiles * @protected **/ p._createTiles = function() { var order = this.map.renderOrder; var startX = 0; var startY = 0; var endX = this.width; var endY = this.height; var dirX = 1; var dirY = 1; var tW = this.map.tileWidth; var tH = this.map.tileHeight; if (order == 'left-down' || order=='left-up') { startX = this.width-1; endX = 0; dirX = -1; } if (order == 'right-up' || order=='left-up') { startY = this.height-1; endY = 0; dirY = -1 } for (var y=startY; y!=endY; y+=dirY) { for (var x=startX; x!=endX; x+=dirX) { var i = (this.width*y + x) var gid = this.data[i]; if (gid <= 0) continue; var tileset = this.map.getTilesetByGid(gid); var tile = new createjs.Sprite(tileset.spritesheet) var tileId = gid-tileset.firstgid; if (tileset.animations['anim-'+(tileId)]) { tile.gotoAndPlay('anim-'+(tileId)); } else { tile.gotoAndStop(tileId); } tile.x = x*tW; tile.y = y*tW + tH; this.addChild(tile); } } } /** * Return the coord of a tile given the local position. * * @method getTile * @param {Integer} x The position x. * @param {Integer} y The position y. * @return {createjs.Point} The tile coord. **/ p.getCoords = function(x, y) { return new createjs.Point( Math.floor(x/this.map.tileWidth), Math.floor(y/this.map.tileHeight) ); } /** * Return the top-left local position of a tile, considering its coords * (column and row). * * @method getTile * @param {Integer} x The column of the tile. * @param {Integer} y The row of the tile. * @return {createjs.Point} The top-left tile local position. **/ p.getPosition = function(x, y) { return new createjs.Point( Math.floor(x*this.map.tileWidth), Math.floor(y*this.map.tileHeight) ); } creatine.TMXOrthogonalTileLayer = createjs.promote(TMXOrthogonalTileLayer, "TMXTileLayer"); }());/* * TMXIsometricTileLayer * * Copyright (c) 2014 Renato de Pontes Pereira. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /** * @module Creatine **/ // namespace: this.creatine = this.creatine || {}; (function() { "use strict"; /** * TMXIsometricTileLayer represents a TileLayer in the TMX map. * * @class TMXIsometricTileLayer * @param {creatine.TMXMap} map The map object. * @param {Object} data The data object (from tmx format). * @extends creatine.TMXTileLayer * @constructor **/ var TMXIsometricTileLayer = function(map, data) { this.TMXTileLayer_constructor(map, data); } var p = createjs.extend(TMXIsometricTileLayer, creatine.TMXTileLayer); /** * Create the tiles. Override this method on the child class. * * @method _createTiles * @protected **/ p._createTiles = function() { var order = this.map.renderOrder; var startX = 0; var startY = 0; var endX = this.width; var endY = this.height; var dirX = 1; var dirY = 1; var tW = this.map.tileWidth; var tH = this.map.tileHeight; if (order == 'left-down' || order=='left-up') { startX = this.width-1; endX = 0; dirX = -1; } if (order == 'right-up' || order=='left-up') { startY = this.height-1; endY = 0; dirY = -1 } for (var y=startY; y!=endY; y+=dirY) { for (var x=startX; x!=endX; x+=dirX) { var i = (this.width*y + x) var gid = this.data[i]; if (gid <= 0) continue; var tileset = this.map.getTilesetByGid(gid); var tile = new createjs.Sprite(tileset.spritesheet) var tileId = gid-tileset.firstgid; if (tileset.animations['anim-'+(tileId)]) { tile.gotoAndPlay('anim-'+(tileId)); } else { tile.gotoAndStop(tileId); } tile.x = (x-y-1)*(this.map.tileWidth/2); tile.y = (x+y+2)*(this.map.tileHeight/2); this.addChild(tile); } } } /** * Return the coord of a tile given the local position. * * Override this method on the child class. * * @method getTile * @param {Integer} x The position x. * @param {Integer} y The position y. * @return {createjs.Point} The tile coord. **/ p.getCoords = function(x, y) { return new createjs.Point( Math.floor(x/this.map.tileWidth + y/this.map.tileHeight), Math.floor(-x/this.map.tileWidth + y/this.map.tileHeight) ); } /** * Return the top-left local position of a tile, considering its coords * (column and row). * * Override this method on the child class. * * @method getTile * @param {Integer} x The column of the tile. * @param {Integer} y The row of the tile. * @return {createjs.Point} The top-left tile local position. **/ p.getPosition = function(x, y) { return new createjs.Point( (x-y-1)*(this.map.tileWidth/2), (x+y+2)*(this.map.tileHeight/2) ) } creatine.TMXIsometricTileLayer = createjs.promote(TMXIsometricTileLayer, "TMXTileLayer"); }());/* * TMXStaggeredTileLayer * * Copyright (c) 2014 Renato de Pontes Pereira. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /** * @module Creatine **/ // namespace: this.creatine = this.creatine || {}; (function() { "use strict"; /** * TMXStaggeredTileLayer represents a TileLayer in the TMX map. * * @class TMXStaggeredTileLayer * @param {creatine.TMXMap} map The map object. * @param {Object} data The data object (from tmx format). * @extends creatine.TMXTileLayer * @constructor **/ var TMXStaggeredTileLayer = function(map, data) { this.TMXTileLayer_constructor(map, data); } var p = createjs.extend(TMXStaggeredTileLayer, creatine.TMXTileLayer); /** * Create the tiles. Override this method on the child class. * * @method _createTiles * @protected **/ p._createTiles = function() { var order = this.map.renderOrder; var startX = 0; var startY = 0; var endX = this.width; var endY = this.height; var dirX = 1; var dirY = 1; var tW = this.map.tileWidth; var tH = this.map.tileHeight; if (order == 'left-down' || order=='left-up') { startX = this.width-1; endX = 0; dirX = -1; } if (order == 'right-up' || order=='left-up') { startY = this.height-1; endY = 0; dirY = -1 } for (var y=startY; y!=endY; y+=dirY) { for (var x=startX; x!=endX; x+=dirX) { // for (var y=startY; y!=2; y+=dirY) { // for (var x=startX; x!=2; x+=dirX) { var i = (this.width*y + x) var gid = this.data[i]; if (gid <= 0) continue; var tileset = this.map.getTilesetByGid(gid); var tile = new createjs.Sprite(tileset.spritesheet) var tileId = gid-tileset.firstgid; if (tileset.animations['anim-'+(tileId)]) { tile.gotoAndPlay('anim-'+(tileId)); } else { tile.gotoAndStop(tileId); } tile.x = tW*(x + (y%2)/2 - 0.5); tile.y = tH*(y/2 + 1); // tile.x = tW*(x+(y%2)/2); // tile.y = tH + tH*y/2; this.addChild(tile); } } } /** * Return the coord of a tile given the local position. * * Override this method on the child class. * * @method getTile * @param {Integer} x The position x. * @param {Integer} y The position y. * @return {createjs.Point} The tile coord. **/ p.getCoords = function(x, y) { // convert to iso first var x_ = Math.floor(x/this.map.tileWidth + y/this.map.tileHeight); var y_ = Math.floor(-x/this.map.tileWidth + y/this.map.tileHeight); // then from iso to stag x = Math.floor((x_-y_)/2); y = x_ + y_; return new createjs.Point(x, y); } /** * Return the top-left local position of a tile, considering its coords * (column and row). * * Override this method on the child class. * * @method getTile * @param {Integer} x The column of the tile. * @param {Integer} y The row of the tile. * @return {createjs.Point} The top-left tile local position. **/ p.getPosition = function(x, y) { return new createjs.Point( this.map.tileWidth*(x + (Math.abs(y)%2)/2 - 0.5), this.map.tileHeight*(y/2 + 1) ) } creatine.TMXStaggeredTileLayer = createjs.promote(TMXStaggeredTileLayer, "TMXTileLayer"); }());/* * TMXImageLayer * * Copyright (c) 2014 Renato de Pontes Pereira. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /** * @module Creatine **/ // namespace: this.creatine = this.creatine || {}; (function() { "use strict"; /** * TMXImageLayer represents an image layer on TMX map. * * @class TMXImageLayer * @param {creatine.TMXMap} map The map object. * @param {Object} data The data object (from tmx format). * @extends createjs.Bitmap * @constructor **/ var TMXImageLayer = function(map, data) { /** * Reference to the TMX map. * * @property map * @type {creatine.TMXMap} * @readonly **/ this.map = null; /** * The path for the image. * * @property imagePath * @type {String} * @readonly **/ this.imagePath = null; /** * The name of the layer. * * @property name * @type {String} * @readonly **/ this.name = null; /** * The amount of tiles in x axis. Not really useful here. * * @property width * @type {Integer} * @readonly **/ this.width = null; /** * The amount of tiles in y axis. Not really useful here. * * @property y * @type {Integer} * @readonly **/ this.height = null; this._initialize(map, data); } var p = createjs.extend(TMXImageLayer, createjs.Bitmap); /** * Initialization method. * * @method _initialize * @param {creatine.TMXMap} map The map object. * @param {Object} data The data object (from tmx format). * @protected **/ p._initialize = function(map, data) { if (!map) return; this.Bitmap_constructor(data['image']||null); this.map = map; this.name = data['name']; this.imagePath = data['image']; this.x = data['x']; this.y = data['y']; this.width = data['width']; this.height = data['height']; this.visible = data['visible']; this.alpha = data['opacity']; } creatine.TMXImageLayer = createjs.promote(TMXImageLayer, "Bitmap"); }());/* * TMXObjectLayer * * Copyright (c) 2014 Renato de Pontes Pereira. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /** * @module Creatine **/ // namespace: this.creatine = this.creatine || {}; (function() { "use strict"; /** * TMXObjectLayer represents an Object Group in TMX maps. * * @class TMXObjectLayer * @param {creatine.TMXMap} map The map object. * @param {Object} data The data object (from tmx format). * @constructor **/ var TMXObjectLayer = function(map, data) { /** * Reference to the TMX map. * * @property map * @type {creatine.TMXMap} * @readonly **/ this.map = null; /** * The name of the layer. * * @property name * @type {String} * @readonly **/ this.name = null; /** * List of objects in the layer. * * @property objects * @type {Array} * @readonly **/ this.objects = null; /** * Is the layer visible? * * @property visible * @type {Boolean} * @readonly **/ this.visible = null; /** * Opacity the layer. * * @property alpha * @type {Float} * @readonly **/ this.alpha = null; this._initialize(map, data); } var p = TMXObjectLayer.prototype; /** * Initialization method. * * @method initialize * @param {creatine.TMXMap} map The map object. * @param {Object} data The data object (from tmx format). * @protected **/ p._initialize = function(map, data) { if (!map) return; this.map = map; this.name = data['name']; this.objects = data['objects']; this.visible = data['visible']; this.alpha = data['opacity']; } creatine.TMXObjectLayer = TMXObjectLayer; }());/* * TMXMap * * Copyright (c) 2014 Renato de Pontes Pereira. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /** * @module Creatine **/ // namespace: this.creatine = this.creatine || {}; (function() { "use strict"; /** * TMXMap is the class that handle tile maps in the TMX format, specified * by the tile software Tiled. A TMX map is composed of one or more layers * (tile layers, object layers or image layers) and one or more tilesets * (image atlases). Creatine supports all types of layers and projections * described by the TMX specification: orthogonal, isometric and staggered. * * The TMXMap receives a data object following the TMX specification. This * is usually loaded from a json map. * *

Example

* * Notice that, TMXMap is a display object, inherited from * createjs.Container, it means that, after loading the map * you can simply add it to the stage: * * var map = new creatine.TMXMap(dataObject); * stage.addChild(map); * *

Data Format

* * In relation to the map properties, the data must be in the following * format: * * var data = { * 'version' : [Integer], // default to 1 * 'orientation' : [String], // default to 'orthogonal' * 'renderorder' : [String], // default to 'right-down' * 'height' : [Integer], // default to 0 * 'width' : [Integer], // default to 0 * 'tileheight' : [Integer], // default to 0 * 'tilewidth' : [Integer], // default to 0 * 'tilesets' : [Array], // mandatory * 'layers' : [Array], // mandatory * 'properties' : [Object], * } * * Consult the specific classes for layer and tileset to known more about * the data format for these structures or consult the official TMX * description: https://github.com/bjorn/tiled/wiki/TMX-Map-Format * * @class TMXMap * @extends createjs.Container * @param {Object} data A data object describing the map. * @constructor **/ var TMXMap = function(data) { /** * Map version * * @property version * @type {Integer} * @readonly **/ this.version = null; /** * Orientation of the map, it supports "orthogonal", "isometric" and * "staggered". * * @property orientation * @type {String} * @readonly **/ this.orientation = null; /** * Order of rendering, supports "right-down", "right-up", "left-down" and * "left-up". * * @property renderOrder * @type {String} * @readonly **/ this.renderOrder = null; /** * Describes how many tiles the map have in the y axis. * * @property height * @type {Integer} * @readonly **/ this.height = null; /** * Describes how many tiles the map have in the x axis. * * @property width * @type {Integer} * @readonly **/ this.width = null; /** * Height of tiles. * * @property tileHeight * @type {Integer} * @readonly **/ this.tileHeight = null; /** * Width of tiles. * * @property tileWidth * @type {Integer} * @readonly **/ this.tileWidth = null; /** * The list of tilesets in the map. * * @property tilesets * @type {Array} * @readonly **/ this.tilesets = null; /** * The list of layers in the map. * * @property layers * @type {Array} * @readonly **/ this.layers = null; /** * User defined properties object. * * @property properties * @type {Object} * @readonly **/ this.properties = null; this._initialize(data); } var p = createjs.extend(TMXMap, createjs.Container); /** * Initialization method. * * @method _initialize * @param {Object} data A data object describing the map. * @protected **/ p._initialize = function(data) { this.Container_constructor(); this.version = data['version'] || 1; this.orientation = data['orientation'] || 'orthogonal'; this.renderOrder = data['renderorder'] || 'right-down'; this.height = data['height'] || 0; this.width = data['width'] || 0; this.tileHeight = data['tileheight'] || 0; this.tileWidth = data['tilewidth'] || 0; this.properties = data['properties']; this.tilesets = []; this.layers = []; this._createTilesets(data); this._createLayers(data); } /** * Create the tileset objects. * * @method _createTilesets * @param {Object} data An object following the TMX specification. * @protected **/ p._createTilesets = function(data) { for (var i=0; i=0; i--) { if (this.tilesets[i].firstgid <= gid) { return this.tilesets[i]; } } } /** * Returns a layer by name * * @method getLayerByName * @param {String} name Layer name. **/ p.getLayerByName = function(name) { for (var i=0; iorientationat * parameter. * * BoxSizer was create to organize the game interface using a basic * geometry, e.g., in a single row or column. But several BoxSizers can be * nested to create a more complex layout. Notice that, for regular grids, * the best option is the GridSizer. * * A BoxSizer requires an orientation parameter, which can be the constants * HORIZONTAL or HORIZONTAL. It also requires a * rectangle containing the information of how much space the sizer can use * to expand and organize its elements. * * By specifying the proportion parameter when adding a child, the BoxSizer * will expand to occupy all the available area. By doing this, the sizer * also can align the component using the anchor position. The anchor * parameter can be specified using the following constants: LEFT, * RIGHT, TOP, BOTTOM, TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT, * CENTER. * * Each component can also have an individual border, by setting the border * parameter when adding it to the sizer. * * *

Example

* * A BoxSizer which grows horizontally and occupying all space of a canvas * can be created as: * * var area = new createjs.Rectangle(0, 0, canvas.width, canvas.height); * var hbox = new creatine.BoxSizer(creatine.HORIZONTAL, area); * * And components can be added as: * * hbox.add(my_text, 1, 0, creatine.CENTER); * hbox.add(my_sprite); * hbox.layout() * * In the example above, the my_sprite component will be put * at the right while my_text will be moved the the center of * the remaining area. * * @class BoxSizer * @constructor * @param {Constant} orientation The orientation of the sizer (horizontal * or vertical). * @param {createjs.Rectangle} area A rectangle containing the usable area * of the sizer. **/ var BoxSizer = function(orientation, area) { /** * The orientation of the sizer (use the HORIZONTAL or VERTICAL constants) * * @property orientation * @type {Constant} **/ this.orientation = orientation; /** * The list within all items in this sizer together with their proportions, * border, and anchor. * * @property children * @type {Array} * @private **/ this.children = []; /** * A rectangle representing the area of which the sizer can use. * * @property area * @type {createjs.Rectangle} **/ this.area = area; } var p = BoxSizer.prototype; /** * Adds a new object to the sizer. * * @method add * @param {Object} object The DisplayObject to be organized by this sizer. * @param {Number} proportion The expansion weight of this item, use 0 * (zero) to not expand. Default to 0. * @param {Number} border The spacing around this item. Default to 0. * @param {Constant} anchor The anchor of the object, only useful if * proportion is greater than 0. Default to * creatine.TOP_LEFT **/ p.add = function(object, proportion, border, anchor) { anchor = anchor || creatine.TOP_LEFT; proportion = proportion || 0; border = border || 0; this.children.push([object, proportion, anchor, border]); } /** * Resize this sizer and its children. * * For the root sizer, the parameter area does not to be * specified, but it is automatically set in nested sizers. * * @method layout * @param {createjs.Rectangle} area A rectangle containing the usable area * of the sizer. **/ p.layout = function(area) { if (area) this.area = area; var borders = 0; var proportions = 0; var fixed_widths = 0; var fixed_heights = 0; // Compute the available area for (var i=0; i 0) { proportions += proportion; } else { var bounds = item.getBounds(); fixed_widths += bounds.width; fixed_heights += bounds.height; } } var avaliable_w = (this.area.width-borders-fixed_widths); var avaliable_h = (this.area.height-borders-fixed_heights); // Resize var step_x = this.area.x; var step_y = this.area.y; var x, y, w, h, weight = 0; for (var i=0; icreatine.TOP_LEFT * @private **/ p._resize_child = function(item, area, anchor) { if (item.layout) { item.layout(area); } if (item.getBounds) { var bounds = item.getBounds(); } if (bounds) { var w = bounds.width*item.scaleX; var h = bounds.height*item.scaleY; } else { var w = 0; var h = 0; } // Set y switch (anchor) { case creatine.TOP: case creatine.TOP_LEFT: case creatine.TOP_RIGHT: item.y = area.y; break; case creatine.LEFT: case creatine.CENTER: case creatine.RIGHT: item.y = (area.y+area.height/2)-h/2; break; case creatine.BOTTOM: case creatine.BOTTOM_LEFT: case creatine.BOTTOM_RIGHT: item.y = area.y+area.height-h; break; } // Set x switch (anchor) { case creatine.LEFT: case creatine.TOP_LEFT: case creatine.BOTTOM_LEFT: item.x = area.x; break; case creatine.TOP: case creatine.CENTER: case creatine.BOTTOM: item.x = (area.x+area.width/2)-w/2; break; case creatine.RIGHT: case creatine.TOP_RIGHT: case creatine.BOTTOM_RIGHT: item.x = area.x+area.width-w; break; } item.x += item.regX; item.y += item.regY; } creatine.BoxSizer = BoxSizer; }()); /* * GridSizer * * Copyright (c) 2014 Renato de Pontes Pereira. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /** * @module Creatine **/ // namespace: this.creatine = this.creatine || {}; (function() { "use strict"; /** * GridSizer is a layout manager that organizes its components in a regular * grid. * * Parameters rows and cols specify how many rows * and columns the grid will have, respectively. All cells in the grid have * the same size. However, each cell can have an individual border, by * setting the border parameter when adding an object to the sizer. * The anchor parameter can be specified using the following constants: * LEFT, RIGHT, TOP, BOTTOM, TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, * BOTTOM_RIGHT, CENTER. * *

Example

* * A GridSizer which with 3 rows and 5 columns, and occupying all space of * a canvas can be created as: * * var area = new createjs.Rectangle(0, 0, canvas.width, canvas.height); * var hbox = new creatine.GridSizer(3, 5, area); * * @class GridSizer * @constructor * @param {Number} rows The number of rows in the grid. * @param {Number} cols The number of columns in the grid. * @param {createjs.Rectangle} area A rectangle containing the usable area * of the sizer. **/ var GridSizer = function(rows, cols, area) { /** * The number of rows in the grid. * * @property rows * @type {Number} **/ this.rows = rows; /** * The number of columns in the grid. * * @property cols * @type {Number} **/ this.cols = cols; /** * The list within all items in this sizer together with their proportions, * border, and anchor. * * @property children * @type {Array} * @private **/ this.children = []; /** * A rectangle representing the area of which the sizer can use. * * @property area * @type {createjs.Rectangle} **/ this.area = area; } var p = GridSizer.prototype; /** * Adds a new object to the sizer. * * @method add * @param {Object} object The DisplayObject to be organized by this sizer. * @param {Number} border The spacing around this item. Default to 0. * @param {Constant} anchor The anchor of the object. Default to * creatine.TOP_LEFT **/ p.add = function(object, border, anchor) { anchor = anchor || creatine.TOP_LEFT; border = border || 0; this.children.push([object, anchor, border]); } /** * Resize this sizer and its children. * * For the root sizer, the parameter area does not to be * specified, but it is automatically set in nested sizers. * * @method layout * @param {createjs.Rectangle} area A rectangle containing the usable area * of the sizer. **/ p.layout = function(area) { if (area) this.area = area; var base_w = this.area.width/this.cols; var base_h = this.area.height/this.rows; for (var i=0; icreatine.TOP_LEFT * @private **/ p._resize_child = function(item, area, anchor) { if (item.layout) item.layout(area); if (item.getBounds) var bounds = item.getBounds(); if (bounds) { var w = bounds.width*item.scaleX; var h = bounds.height*item.scaleY; } else { var w = 0; var h = 0; } // Set y switch (anchor) { case creatine.TOP: case creatine.TOP_LEFT: case creatine.TOP_RIGHT: item.y = area.y; break; case creatine.LEFT: case creatine.CENTER: case creatine.RIGHT: item.y = (area.y+area.height/2)-h/2; break; case creatine.BOTTOM: case creatine.BOTTOM_LEFT: case creatine.BOTTOM_RIGHT: item.y = area.y+area.height-h; break; } // Set x switch (anchor) { case creatine.LEFT: case creatine.TOP_LEFT: case creatine.BOTTOM_LEFT: item.x = area.x; break; case creatine.TOP: case creatine.CENTER: case creatine.BOTTOM: item.x = (area.x+area.width/2)-w/2; break; case creatine.RIGHT: case creatine.TOP_RIGHT: case creatine.BOTTOM_RIGHT: item.x = area.x+area.width-w; break; } item.x += item.regX; item.y += item.regY; } creatine.GridSizer = GridSizer; }()); /* * DiscreteBar * * Copyright (c) 2014 Renato de Pontes Pereira. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /** * @module Creatine **/ // namespace: this.creatine = this.creatine || {}; (function() { "use strict"; /** * DiscreteBar is a progress bar that only handle discrete values. This is * a simple and specific implementation of a progress bar, for a more * general version, consult the ProgressBar. * * It can filled in 4 different directions, by using the parameter * `direction`: LEFT_TO_RIGHT, RIGHT_TO_LEFT, TOP_TO_BOTTOM, * BOTTOM_TO_TOP. * *

Example

* * // Create the Director * var progress = new creatine.DiscreteBar( * image, // The image used to fill. * 5, // The space between one image and other. * creatine.LEFT_TO_RIGHT // The director. * ) * * @class DiscreteBar * @constructor * @param {Bitmap|Image} image A bitmap or image that will be used to fill * the progress bar. * @param {Number} spacing The space between one image and other. Default * to 0. * @param {Constant} direction The filling direction. Default to * `creatine.LEFT_TO_RIGHT`. **/ var DiscreteBar = function(image, spacing, direction) { this.Container_constructor(); /** * The bar filling direction (`LEFT_TO_RIGHT`, `RIGHT_TO_LEFT`, * `TOP_TO_BOTTOM` or `BOTTOM_TO_TOP`). * * @property direction * @type {Constant} **/ this.direction = direction || creatine.LEFT_TO_RIGHT; /** * The image which will be used to fill the bar. * * @property image * @type {Image} **/ if (image.image) { this.image = image.image; } else { this.image = image; } /** * The space between one image and other. * * @property spacing * @type {Image} **/ this.spacing = spacing || 0; /** * The current value of the progress bar. * * @property value * @type {Number} **/ this.value = 0; } var p = createjs.extend(DiscreteBar, createjs.Container); /** * Returns true or false indicating whether the bar would be visible if * drawn to a canvas. This does not account for whether it would be visible * within the boundaries of the stage. * * @method isVisible * @protected **/ p.isVisible = function() { var hasContent = this.value > 0 || this.cacheCanvas; return !!(this.visible && this.alpha > 0 && this.scaleX != 0 && this.scaleY != 0 && hasContent); }; /** * Draws the DiscreteBar into the specified context ignoring its visible, * alpha, shadow, and transform. Returns true if the draw was handled * (useful for overriding functionality). * * @method draw * @param {CanvasRenderingContext2D} ctx The canvas 2D context object to * draw into. * @param {Boolean} [ignoreCache=false] Indicates whether the draw operation * should ignore any current cache. For * example, used for drawing the cache * (to prevent it from simply drawing * an existing cache back into itself). * @protected **/ p.draw = function(ctx, ignoreCache) { if (this.DisplayObject_draw(ctx, ignoreCache)) { return true; } var dir_x = 0; var dir_y = 0; var step_x = 0; var step_y = 0; var w = this.image.width; var h = this.image.height; if (this.direction === creatine.LEFT_TO_RIGHT) {dir_x = 1;} else if (this.direction === creatine.RIGHT_TO_LEFT) {dir_x = -1; step_x = -w;} else if (this.direction === creatine.TOP_TO_BOTTOM) {dir_y = 1;} else if (this.direction === creatine.BOTTOM_TO_TOP) {dir_y = -1; step_y = -h;} this.removeAllChildren(); for (var i=0; iLEFT_TO_RIGHT, RIGHT_TO_LEFT, * TOP_TO_BOTTOM, BOTTOM_TO_TOP. * *

Example

* * // Create the Director * var progress = new creatine.ProgressBar( * 'green', // The fill color * 'black', // The background color * creatine.LEFT_TO_RIGHT // The director * 100, // The bar width * 20, // The bar height * 0, // The minimum value * 100 // The maximum value * ) * * @class ProgressBar * @constructor * @param {String|Array|Bitmap|Image} colorsOrImg A color, list of colors * or bitmap that will be used to fill * the progress bar. * @param {String} backgroundColor A string with the color of the bar * background. If `null`, the background will be * transparent. Default to `null`. * @param {Constant} direction The filling direction. Default to * `creatine.LEFT_TO_RIGHT`. * @param {Number} width The width of the bar. Default to 100. * @param {Number} height The height of the bar. Default to 10. * @param {Number} min The minimum value of the bar. Default to 0. * @param {Number} max The maximum value of the bar. Default to 100. **/ var ProgressBar = function(colorsOrImg, backgroundColor, direction, width, height, min, max) { /** * The bar type ('color', 'gradient' or 'image'). * * @property type * @type {Sting} **/ p.type = null; /** * The bar filling direction (`LEFT_TO_RIGHT`, `RIGHT_TO_LEFT`, * `TOP_TO_BOTTOM` or `BOTTOM_TO_TOP`). * * @property direction * @type {Constant} **/ p.direction = null; /** * The image which will be used to fill the bar (if type = 'image'). * * @property image * @type {Image} **/ p.image = null; /** * The array with colors of the gradient to fill the bar (if type = * 'gradient'). * * @property gradientColors * @type {Array} **/ p.gradientColors = null; /** * The color to fill the bar (if type = 'color'). * * @property fillColor * @type {String} **/ p.fillColor = null; /** * The background color. * * @property backgroundColor * @type {String} **/ p.backgroundColor = null; /** * The current value of the progress bar. * * @property value * @type {Number} **/ p.value = null; /** * The minimum value of the progress bar. * * @property min * @type {Number} **/ p.min = null; /** * The maximum value of the progress bar. * * @property max * @type {Number} **/ p.max = null; /** * The width of the progress bar. * * @property width * @type {Number} **/ p.width = null; /** * The height of the progress bar. * * @property height * @type {Number} **/ p.height = null; if (colorsOrImg) { this._initialize( colorsOrImg, backgroundColor, direction, width, height, min, max ); } } var p = createjs.extend(ProgressBar, createjs.Shape); /** * Initialization method. * * @method initialize * @param {String|Array|Bitmap|Image} colorsOrImg A color, list of colors * or bitmap that will be used to fill * the progress bar. * @param {String} backgroundColor A string with the color of the bar * background. If `null`, the background will be * transparent. Default to `null`. * @param {Constant} direction The filling direction. Default to * `creatine.LEFT_TO_RIGHT`. * @param {Number} width The width of the bar. Default to 100. * @param {Number} height The height of the bar. Default to 10. * @param {Number} min The minimum value of the bar. Default to 0. * @param {Number} max The maximum value of the bar. Default to 100. * @private **/ p._initialize = function(colorsOrImg, backgroundColor, direction, width, height, min, max) { this.Shape_constructor(); if (colorsOrImg instanceof Array) { this.type = 'gradient'; this.gradientColors = colorsOrImg; } else if (typeof colorsOrImg === 'string') { this.type = 'color'; this.fillColor = colorsOrImg; } else { this.type = 'image'; this.image = colorsOrImg; if (this.image.image) { this.image = this.image.image; } } if (backgroundColor == null) backgroundColor = null; if (direction == null) direction = creatine.LEFT_TO_RIGHT; if (width == null) width = 100; if (height == null) height = 10; if (min == null) min = 0; if (max == null) max = 100; this.backgroundColor = backgroundColor; this.direction = direction; this.width = width; this.height = height; this.min = min; this.max = max; this.value = 0; } /** * Returns true or false indicating whether the bar would be visible if * drawn to a canvas. This does not account for whether it would be visible * within the boundaries of the stage. * * @method isVisible * @protected **/ p.isVisible = function() { return !!(this.visible && this.alpha > 0 && this.scaleX != 0 && this.scaleY != 0); }; p.layout = function(area) { this.width = area.width; this.height = area.height; } /** * Draws the ProgressBar into the specified context ignoring its visible, * alpha, shadow, and transform. Returns true if the draw was handled * (useful for overriding functionality). * * @method draw * @param {CanvasRenderingContext2D} ctx The canvas 2D context object to * draw into. * @param {Boolean} [ignoreCache=false] Indicates whether the draw operation * should ignore any current cache. For * example, used for drawing the cache * (to prevent it from simply drawing * an existing cache back into itself). * @protected **/ p.draw = function(ctx, ignoreCache) { if (this.DisplayObject_draw(ctx, ignoreCache)) { return true; } var x = 0; var y = 0; var w = this.width; var h = this.height; var value_w = creatine.clip(w*(this.value-this.min)/(this.max-this.min), 0, w); var value_h = h; var gline = [w, 0]; if (this.direction === creatine.TOP_TO_BOTTOM) { var gline = [0, w]; var w_ = w; w = h; h = w_; w_ = value_w; value_w = value_h; value_h = w_; } else if (this.direction === creatine.BOTTOM_TO_TOP) { var gline = [0, -w]; var w_ = w; w = h; h = -w_; w_ = value_w; value_w = value_h; value_h = -w_; } else if (this.direction === creatine.RIGHT_TO_LEFT) { var gline = [-w, 0]; w = -w; value_w = -value_w; } this.graphics.clear(); if (this.backgroundColor) { this.graphics.beginFill(this.backgroundColor); this.graphics.drawRect(x, y, w, h); } if (this.type === 'color') { this.graphics.beginFill(this.fillColor); this.graphics.drawRect(x, y, value_w, value_h); } else if (this.type === 'gradient') { var ratios = []; for (var i=0; i *
  • creatine.STRETCH: to fill the available area * ignoring the aspect ratio.
  • *
  • creatine.FIT: to fill the available area without * overflow and keeping the aspect ratio.
  • *
  • creatine.FILL: to fill the available area with * overflow and keeping the aspect ratio.
  • *
  • creatine.NOSCALE: to keep the original size. *
  • * * * @property scaleMode * @type {constant} **/ this.scaleMode = scaleMode || creatine.NOSCALE; } var p = createjs.extend(FlexBitmap, createjs.Bitmap); /** * Resize this this image to fit the area. * * @method layout * @param {createjs.Rectangle} area A rectangle containing the usable area * of the bitmap. **/ p.layout = function(area) { var width = this.image.width; var height = this.image.height; var scale_x = 1; var scale_y = 1; if (this.scaleMode === creatine.FIT) { scale_x = scale_y = Math.min( (area.height/height), (area.width /width) ); } else if (this.scaleMode === creatine.FILL) { scale_x = scale_y = Math.max( (area.height/height), (area.width /width) ); } else if (this.scaleMode === creatine.STRETCH) { scale_x = (area.width /width); scale_y = area.height/height; } this.scaleX = scale_x; this.scaleY = scale_y; } /** * Returns a rectangle representing this object's bounds after the resize. * * @method getBounds * @protected **/ p.getBounds = function() { var w = this.image.width*this.scaleX; var h = this.image.height*this.scaleY; return this._rectangle.setValues(0, 0, w, h); } creatine.FlexBitmap = createjs.promote(FlexBitmap, "Bitmap"); }());