1 /** 2 * The Render Engine 3 * SpriteComponent 4 * 5 * @fileoverview An extension of the render component which handles sprite 6 * resource rendering. 7 * 8 * @author: Brett Fattori (brettf@renderengine.com) 9 * @author: $Author: bfattori $ 10 * @version: $Revision: 1555 $ 11 * 12 * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com) 13 * 14 * Permission is hereby granted, free of charge, to any person obtaining a copy 15 * of this software and associated documentation files (the "Software"), to deal 16 * in the Software without restriction, including without limitation the rights 17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 * copies of the Software, and to permit persons to whom the Software is 19 * furnished to do so, subject to the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be included in 22 * all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 * THE SOFTWARE. 31 * 32 */ 33 34 // The class this file defines and its required classes 35 R.Engine.define({ 36 "class":"R.components.render.Sprite", 37 "requires":[ 38 "R.components.Render" 39 ] 40 }); 41 42 /** 43 * @class A render component that renders its contents from a {@link R.resources.types.Sprite}. Sprites 44 * are 2d graphics which are either a single frame (static) or multiple frames 45 * (animation). The sprite's descriptor will define that for the component. 46 * 47 * @param name {String} The component name 48 * @param [priority=0.1] {Number} The render priority 49 * @param sprite {R.resources.types.Sprite} The sprite to render 50 * @extends R.components.Render 51 * @constructor 52 * @description Create a sprite component. 53 */ 54 R.components.render.Sprite = function () { 55 "use strict"; 56 return R.components.Render.extend(/** @scope R.components.render.Sprite.prototype */{ 57 58 currentSprite:null, 59 60 /** 61 * @private 62 */ 63 constructor:function (name, priority, sprite) { 64 if (priority instanceof R.resources.types.Sprite) { 65 sprite = priority; 66 priority = 0.1; 67 } 68 this.base(name, priority); 69 this.currentSprite = sprite; 70 }, 71 72 /** 73 * Releases the component back into the object pool. See {@link R.engine.PooledObject#release} for 74 * more information. 75 */ 76 release:function () { 77 this.base(); 78 this.currentSprite = null; 79 }, 80 81 /** 82 * Calculate the bounding box from the set of 83 * points which comprise the shape to be rendered. 84 * @private 85 */ 86 calculateBoundingBox:function () { 87 return this.currentSprite.getBoundingBox(); 88 }, 89 90 /** 91 * Set the sprite the component will render. 92 * 93 * @param sprite {R.resources.types.Sprite} The sprite to render 94 */ 95 setSprite:function (sprite) { 96 this.currentSprite = sprite; 97 98 if (this.getGameObject().jQ()) { 99 this.getGameObject().jQ().css({ 100 width:sprite.getBoundingBox().len_x(), 101 height:sprite.getBoundingBox().len_y(), 102 background:"url('" + sprite.getSourceImage().src + "') no-repeat" 103 }); 104 } 105 this.getGameObject().markDirty(); 106 }, 107 108 /** 109 * Get the sprite the component is rendering. 110 * 111 * @return {R.resources.types.Sprite} A <tt>R.resources.types.Sprite</tt> instance 112 */ 113 getSprite:function () { 114 return this.currentSprite; 115 }, 116 117 /** 118 * Draw the sprite to the render context. The frame, for animated 119 * sprites, will be automatically determined based on the current 120 * time passed as the second argument. 121 * 122 * @param renderContext {R.rendercontexts.AbstractRenderContext} The context to render to 123 * @param time {Number} The engine time in milliseconds 124 * @param dt {Number} The delta between the world time and the last time the world was updated 125 * in milliseconds. 126 */ 127 execute:function (renderContext, time, dt) { 128 129 if (!this.base(renderContext, time, dt)) { 130 return; 131 } 132 133 if (this.currentSprite) { 134 this.transformOrigin(renderContext, true); 135 renderContext.drawSprite(this.currentSprite, time, dt, this.getGameObject()); 136 this.transformOrigin(renderContext, false); 137 } 138 } 139 }, /** @scope R.components.render.Sprite.prototype */{ 140 /** 141 * Get the class name of this object 142 * 143 * @return {String} "R.components.render.Sprite" 144 */ 145 getClassName:function () { 146 return "R.components.render.Sprite"; 147 } 148 }); 149 };