1 /**
  2  * The Render Engine
  3  * ImageComponent
  4  *
  5  * @fileoverview An extension of the render component which handles
  6  *               image 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.Image",
 37     "requires":[
 38         "R.components.Render",
 39         "R.resources.types.Image"
 40     ]
 41 });
 42 
 43 /**
 44  * @class A {@link R.components.Render render component} that draws an image to the render context.
 45  *        Images used by this component are loaded via an {@link R.resources.loader.ImageLoader}
 46  *        so that client-side caching can be used.
 47  *
 48  * @param name {String} The name of the component
 49  * @param [priority=0.1] {Number} The render priority
 50  * @param image {R.resources.types.Image} The image object, acquired with {@link R.resources.loaders.ImageLoader#getImage}.
 51  * @extends R.components.Render
 52  * @constructor
 53  * @description Creates a component which renders images from an {@link ImageLoader}.
 54  */
 55 R.components.render.Image = function () {
 56     "use strict";
 57     return R.components.Render.extend(/** @scope R.components.render.Image.prototype */{
 58 
 59         currentImage:null,
 60         bbox:null,
 61         imageLoader:null,
 62 
 63         /**
 64          * @private
 65          */
 66         constructor:function (name, priority, image) {
 67             if (priority instanceof R.resources.types.Image) {
 68                 image = priority;
 69                 priority = 0.1;
 70             }
 71             this.base(name, priority);
 72             if (image != null) {
 73                 this.currentImage = image;
 74                 this.bbox = this.currentImage.getBoundingBox();
 75             }
 76         },
 77 
 78         /**
 79          * Releases the component back into the object pool. See {@link R.engine.PooledObject#release}
 80          * for more information.
 81          */
 82         release:function () {
 83             this.base();
 84             this.currentImage = null;
 85             this.bbox = null;
 86         },
 87 
 88         /**
 89          * Calculates the bounding box which encloses the image.
 90          * @private
 91          */
 92         calculateBoundingBox:function () {
 93             return this.bbox;
 94         },
 95 
 96         /**
 97          * Set the image the component will render from the {@link R.resources.loaders.ImageLoader}
 98          * specified when creating the component.  This allows the user to change
 99          * the image on the fly.
100          *
101          * @param image {R.resources.types.Image} The image to render
102          */
103         setImage:function (image) {
104             this.currentImage = image;
105             this.bbox = image.getBoundingBox();
106             this.getGameObject().markDirty();
107         },
108 
109         /**
110          * Get the image the component is rendering.
111          * @return {HTMLImage}
112          */
113         getImage:function () {
114             return this.currentImage;
115         },
116 
117         /**
118          * Draw the image to the render context.
119          *
120          * @param renderContext {R.rendercontexts.AbstractRenderContext} The context to render to
121          * @param time {Number} The engine time in milliseconds
122          * @param dt {Number} The delta between the world time and the last time the world was updated
123          *          in milliseconds.
124          */
125         execute:function (renderContext, time, dt) {
126 
127             if (!this.base(renderContext, time, dt)) {
128                 return;
129             }
130 
131             if (this.currentImage) {
132                 this.transformOrigin(renderContext, true);
133                 renderContext.drawImage(this.bbox, this.currentImage.getImage(), null, this.getGameObject());
134                 this.transformOrigin(renderContext, false);
135             }
136         }
137     }, /** @scope R.components.render.Image.prototype */{
138         /**
139          * Get the class name of this object
140          * @return {String} "R.components.render.Image"
141          */
142         getClassName:function () {
143             return "R.components.render.Image";
144         }
145     });
146 };
147